2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
5 completely rewritten, based on the MD driver code from Marc Zyngier
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
20 Neil Brown <neilb@cse.unsw.edu.au>.
22 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
25 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include <linux/kthread.h>
36 #include <linux/blkdev.h>
37 #include <linux/sysctl.h>
38 #include <linux/seq_file.h>
39 #include <linux/mutex.h>
40 #include <linux/buffer_head.h> /* for invalidate_bdev */
41 #include <linux/poll.h>
42 #include <linux/ctype.h>
43 #include <linux/string.h>
44 #include <linux/hdreg.h>
45 #include <linux/proc_fs.h>
46 #include <linux/random.h>
47 #include <linux/reboot.h>
48 #include <linux/file.h>
49 #include <linux/compat.h>
50 #include <linux/delay.h>
51 #include <linux/raid/md_p.h>
52 #include <linux/raid/md_u.h>
53 #include <linux/slab.h>
58 #define dprintk(x...) ((void)(DEBUG && printk(x)))
61 static void autostart_arrays(int part
);
64 static LIST_HEAD(pers_list
);
65 static DEFINE_SPINLOCK(pers_lock
);
67 static void md_print_devices(void);
69 static DECLARE_WAIT_QUEUE_HEAD(resync_wait
);
70 static struct workqueue_struct
*md_wq
;
71 static struct workqueue_struct
*md_misc_wq
;
73 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
76 * Default number of read corrections we'll attempt on an rdev
77 * before ejecting it from the array. We divide the read error
78 * count by 2 for every hour elapsed between read errors.
80 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
82 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
83 * is 1000 KB/sec, so the extra system load does not show up that much.
84 * Increase it if you want to have more _guaranteed_ speed. Note that
85 * the RAID driver will use the maximum available bandwidth if the IO
86 * subsystem is idle. There is also an 'absolute maximum' reconstruction
87 * speed limit - in case reconstruction slows down your system despite
90 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
91 * or /sys/block/mdX/md/sync_speed_{min,max}
94 static int sysctl_speed_limit_min
= 1000;
95 static int sysctl_speed_limit_max
= 200000;
96 static inline int speed_min(mddev_t
*mddev
)
98 return mddev
->sync_speed_min
?
99 mddev
->sync_speed_min
: sysctl_speed_limit_min
;
102 static inline int speed_max(mddev_t
*mddev
)
104 return mddev
->sync_speed_max
?
105 mddev
->sync_speed_max
: sysctl_speed_limit_max
;
108 static struct ctl_table_header
*raid_table_header
;
110 static ctl_table raid_table
[] = {
112 .procname
= "speed_limit_min",
113 .data
= &sysctl_speed_limit_min
,
114 .maxlen
= sizeof(int),
115 .mode
= S_IRUGO
|S_IWUSR
,
116 .proc_handler
= proc_dointvec
,
119 .procname
= "speed_limit_max",
120 .data
= &sysctl_speed_limit_max
,
121 .maxlen
= sizeof(int),
122 .mode
= S_IRUGO
|S_IWUSR
,
123 .proc_handler
= proc_dointvec
,
128 static ctl_table raid_dir_table
[] = {
132 .mode
= S_IRUGO
|S_IXUGO
,
138 static ctl_table raid_root_table
[] = {
143 .child
= raid_dir_table
,
148 static const struct block_device_operations md_fops
;
150 static int start_readonly
;
153 * like bio_clone, but with a local bio set
156 static void mddev_bio_destructor(struct bio
*bio
)
158 mddev_t
*mddev
, **mddevp
;
163 bio_free(bio
, mddev
->bio_set
);
166 struct bio
*bio_alloc_mddev(gfp_t gfp_mask
, int nr_iovecs
,
172 if (!mddev
|| !mddev
->bio_set
)
173 return bio_alloc(gfp_mask
, nr_iovecs
);
175 b
= bio_alloc_bioset(gfp_mask
, nr_iovecs
,
181 b
->bi_destructor
= mddev_bio_destructor
;
184 EXPORT_SYMBOL_GPL(bio_alloc_mddev
);
186 struct bio
*bio_clone_mddev(struct bio
*bio
, gfp_t gfp_mask
,
192 if (!mddev
|| !mddev
->bio_set
)
193 return bio_clone(bio
, gfp_mask
);
195 b
= bio_alloc_bioset(gfp_mask
, bio
->bi_max_vecs
,
201 b
->bi_destructor
= mddev_bio_destructor
;
203 if (bio_integrity(bio
)) {
206 ret
= bio_integrity_clone(b
, bio
, gfp_mask
, mddev
->bio_set
);
216 EXPORT_SYMBOL_GPL(bio_clone_mddev
);
219 * We have a system wide 'event count' that is incremented
220 * on any 'interesting' event, and readers of /proc/mdstat
221 * can use 'poll' or 'select' to find out when the event
225 * start array, stop array, error, add device, remove device,
226 * start build, activate spare
228 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters
);
229 static atomic_t md_event_count
;
230 void md_new_event(mddev_t
*mddev
)
232 atomic_inc(&md_event_count
);
233 wake_up(&md_event_waiters
);
235 EXPORT_SYMBOL_GPL(md_new_event
);
237 /* Alternate version that can be called from interrupts
238 * when calling sysfs_notify isn't needed.
240 static void md_new_event_inintr(mddev_t
*mddev
)
242 atomic_inc(&md_event_count
);
243 wake_up(&md_event_waiters
);
247 * Enables to iterate over all existing md arrays
248 * all_mddevs_lock protects this list.
250 static LIST_HEAD(all_mddevs
);
251 static DEFINE_SPINLOCK(all_mddevs_lock
);
255 * iterates through all used mddevs in the system.
256 * We take care to grab the all_mddevs_lock whenever navigating
257 * the list, and to always hold a refcount when unlocked.
258 * Any code which breaks out of this loop while own
259 * a reference to the current mddev and must mddev_put it.
261 #define for_each_mddev(mddev,tmp) \
263 for (({ spin_lock(&all_mddevs_lock); \
264 tmp = all_mddevs.next; \
266 ({ if (tmp != &all_mddevs) \
267 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
268 spin_unlock(&all_mddevs_lock); \
269 if (mddev) mddev_put(mddev); \
270 mddev = list_entry(tmp, mddev_t, all_mddevs); \
271 tmp != &all_mddevs;}); \
272 ({ spin_lock(&all_mddevs_lock); \
277 /* Rather than calling directly into the personality make_request function,
278 * IO requests come here first so that we can check if the device is
279 * being suspended pending a reconfiguration.
280 * We hold a refcount over the call to ->make_request. By the time that
281 * call has finished, the bio has been linked into some internal structure
282 * and so is visible to ->quiesce(), so we don't need the refcount any more.
284 static int md_make_request(struct request_queue
*q
, struct bio
*bio
)
286 const int rw
= bio_data_dir(bio
);
287 mddev_t
*mddev
= q
->queuedata
;
290 unsigned int sectors
;
292 if (mddev
== NULL
|| mddev
->pers
== NULL
297 smp_rmb(); /* Ensure implications of 'active' are visible */
299 if (mddev
->suspended
) {
302 prepare_to_wait(&mddev
->sb_wait
, &__wait
,
303 TASK_UNINTERRUPTIBLE
);
304 if (!mddev
->suspended
)
310 finish_wait(&mddev
->sb_wait
, &__wait
);
312 atomic_inc(&mddev
->active_io
);
316 * save the sectors now since our bio can
317 * go away inside make_request
319 sectors
= bio_sectors(bio
);
320 rv
= mddev
->pers
->make_request(mddev
, bio
);
322 cpu
= part_stat_lock();
323 part_stat_inc(cpu
, &mddev
->gendisk
->part0
, ios
[rw
]);
324 part_stat_add(cpu
, &mddev
->gendisk
->part0
, sectors
[rw
], sectors
);
327 if (atomic_dec_and_test(&mddev
->active_io
) && mddev
->suspended
)
328 wake_up(&mddev
->sb_wait
);
333 /* mddev_suspend makes sure no new requests are submitted
334 * to the device, and that any requests that have been submitted
335 * are completely handled.
336 * Once ->stop is called and completes, the module will be completely
339 void mddev_suspend(mddev_t
*mddev
)
341 BUG_ON(mddev
->suspended
);
342 mddev
->suspended
= 1;
344 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->active_io
) == 0);
345 mddev
->pers
->quiesce(mddev
, 1);
347 EXPORT_SYMBOL_GPL(mddev_suspend
);
349 void mddev_resume(mddev_t
*mddev
)
351 mddev
->suspended
= 0;
352 wake_up(&mddev
->sb_wait
);
353 mddev
->pers
->quiesce(mddev
, 0);
355 EXPORT_SYMBOL_GPL(mddev_resume
);
357 int mddev_congested(mddev_t
*mddev
, int bits
)
359 return mddev
->suspended
;
361 EXPORT_SYMBOL(mddev_congested
);
364 * Generic flush handling for md
367 static void md_end_flush(struct bio
*bio
, int err
)
369 mdk_rdev_t
*rdev
= bio
->bi_private
;
370 mddev_t
*mddev
= rdev
->mddev
;
372 rdev_dec_pending(rdev
, mddev
);
374 if (atomic_dec_and_test(&mddev
->flush_pending
)) {
375 /* The pre-request flush has finished */
376 queue_work(md_wq
, &mddev
->flush_work
);
381 static void md_submit_flush_data(struct work_struct
*ws
);
383 static void submit_flushes(struct work_struct
*ws
)
385 mddev_t
*mddev
= container_of(ws
, mddev_t
, flush_work
);
388 INIT_WORK(&mddev
->flush_work
, md_submit_flush_data
);
389 atomic_set(&mddev
->flush_pending
, 1);
391 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
392 if (rdev
->raid_disk
>= 0 &&
393 !test_bit(Faulty
, &rdev
->flags
)) {
394 /* Take two references, one is dropped
395 * when request finishes, one after
396 * we reclaim rcu_read_lock
399 atomic_inc(&rdev
->nr_pending
);
400 atomic_inc(&rdev
->nr_pending
);
402 bi
= bio_alloc_mddev(GFP_KERNEL
, 0, mddev
);
403 bi
->bi_end_io
= md_end_flush
;
404 bi
->bi_private
= rdev
;
405 bi
->bi_bdev
= rdev
->bdev
;
406 atomic_inc(&mddev
->flush_pending
);
407 submit_bio(WRITE_FLUSH
, bi
);
409 rdev_dec_pending(rdev
, mddev
);
412 if (atomic_dec_and_test(&mddev
->flush_pending
))
413 queue_work(md_wq
, &mddev
->flush_work
);
416 static void md_submit_flush_data(struct work_struct
*ws
)
418 mddev_t
*mddev
= container_of(ws
, mddev_t
, flush_work
);
419 struct bio
*bio
= mddev
->flush_bio
;
421 if (bio
->bi_size
== 0)
422 /* an empty barrier - all done */
425 bio
->bi_rw
&= ~REQ_FLUSH
;
426 if (mddev
->pers
->make_request(mddev
, bio
))
427 generic_make_request(bio
);
430 mddev
->flush_bio
= NULL
;
431 wake_up(&mddev
->sb_wait
);
434 void md_flush_request(mddev_t
*mddev
, struct bio
*bio
)
436 spin_lock_irq(&mddev
->write_lock
);
437 wait_event_lock_irq(mddev
->sb_wait
,
439 mddev
->write_lock
, /*nothing*/);
440 mddev
->flush_bio
= bio
;
441 spin_unlock_irq(&mddev
->write_lock
);
443 INIT_WORK(&mddev
->flush_work
, submit_flushes
);
444 queue_work(md_wq
, &mddev
->flush_work
);
446 EXPORT_SYMBOL(md_flush_request
);
448 /* Support for plugging.
449 * This mirrors the plugging support in request_queue, but does not
450 * require having a whole queue or request structures.
451 * We allocate an md_plug_cb for each md device and each thread it gets
452 * plugged on. This links tot the private plug_handle structure in the
453 * personality data where we keep a count of the number of outstanding
454 * plugs so other code can see if a plug is active.
457 struct blk_plug_cb cb
;
461 static void plugger_unplug(struct blk_plug_cb
*cb
)
463 struct md_plug_cb
*mdcb
= container_of(cb
, struct md_plug_cb
, cb
);
464 if (atomic_dec_and_test(&mdcb
->mddev
->plug_cnt
))
465 md_wakeup_thread(mdcb
->mddev
->thread
);
469 /* Check that an unplug wakeup will come shortly.
470 * If not, wakeup the md thread immediately
472 int mddev_check_plugged(mddev_t
*mddev
)
474 struct blk_plug
*plug
= current
->plug
;
475 struct md_plug_cb
*mdcb
;
480 list_for_each_entry(mdcb
, &plug
->cb_list
, cb
.list
) {
481 if (mdcb
->cb
.callback
== plugger_unplug
&&
482 mdcb
->mddev
== mddev
) {
483 /* Already on the list, move to top */
484 if (mdcb
!= list_first_entry(&plug
->cb_list
,
487 list_move(&mdcb
->cb
.list
, &plug
->cb_list
);
491 /* Not currently on the callback list */
492 mdcb
= kmalloc(sizeof(*mdcb
), GFP_ATOMIC
);
497 mdcb
->cb
.callback
= plugger_unplug
;
498 atomic_inc(&mddev
->plug_cnt
);
499 list_add(&mdcb
->cb
.list
, &plug
->cb_list
);
502 EXPORT_SYMBOL_GPL(mddev_check_plugged
);
504 static inline mddev_t
*mddev_get(mddev_t
*mddev
)
506 atomic_inc(&mddev
->active
);
510 static void mddev_delayed_delete(struct work_struct
*ws
);
512 static void mddev_put(mddev_t
*mddev
)
514 struct bio_set
*bs
= NULL
;
516 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
518 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
) &&
519 mddev
->ctime
== 0 && !mddev
->hold_active
) {
520 /* Array is not configured at all, and not held active,
522 list_del(&mddev
->all_mddevs
);
524 mddev
->bio_set
= NULL
;
525 if (mddev
->gendisk
) {
526 /* We did a probe so need to clean up. Call
527 * queue_work inside the spinlock so that
528 * flush_workqueue() after mddev_find will
529 * succeed in waiting for the work to be done.
531 INIT_WORK(&mddev
->del_work
, mddev_delayed_delete
);
532 queue_work(md_misc_wq
, &mddev
->del_work
);
536 spin_unlock(&all_mddevs_lock
);
541 void mddev_init(mddev_t
*mddev
)
543 mutex_init(&mddev
->open_mutex
);
544 mutex_init(&mddev
->reconfig_mutex
);
545 mutex_init(&mddev
->bitmap_info
.mutex
);
546 INIT_LIST_HEAD(&mddev
->disks
);
547 INIT_LIST_HEAD(&mddev
->all_mddevs
);
548 init_timer(&mddev
->safemode_timer
);
549 atomic_set(&mddev
->active
, 1);
550 atomic_set(&mddev
->openers
, 0);
551 atomic_set(&mddev
->active_io
, 0);
552 atomic_set(&mddev
->plug_cnt
, 0);
553 spin_lock_init(&mddev
->write_lock
);
554 atomic_set(&mddev
->flush_pending
, 0);
555 init_waitqueue_head(&mddev
->sb_wait
);
556 init_waitqueue_head(&mddev
->recovery_wait
);
557 mddev
->reshape_position
= MaxSector
;
558 mddev
->resync_min
= 0;
559 mddev
->resync_max
= MaxSector
;
560 mddev
->level
= LEVEL_NONE
;
562 EXPORT_SYMBOL_GPL(mddev_init
);
564 static mddev_t
* mddev_find(dev_t unit
)
566 mddev_t
*mddev
, *new = NULL
;
568 if (unit
&& MAJOR(unit
) != MD_MAJOR
)
569 unit
&= ~((1<<MdpMinorShift
)-1);
572 spin_lock(&all_mddevs_lock
);
575 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
576 if (mddev
->unit
== unit
) {
578 spin_unlock(&all_mddevs_lock
);
584 list_add(&new->all_mddevs
, &all_mddevs
);
585 spin_unlock(&all_mddevs_lock
);
586 new->hold_active
= UNTIL_IOCTL
;
590 /* find an unused unit number */
591 static int next_minor
= 512;
592 int start
= next_minor
;
596 dev
= MKDEV(MD_MAJOR
, next_minor
);
598 if (next_minor
> MINORMASK
)
600 if (next_minor
== start
) {
601 /* Oh dear, all in use. */
602 spin_unlock(&all_mddevs_lock
);
608 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
609 if (mddev
->unit
== dev
) {
615 new->md_minor
= MINOR(dev
);
616 new->hold_active
= UNTIL_STOP
;
617 list_add(&new->all_mddevs
, &all_mddevs
);
618 spin_unlock(&all_mddevs_lock
);
621 spin_unlock(&all_mddevs_lock
);
623 new = kzalloc(sizeof(*new), GFP_KERNEL
);
628 if (MAJOR(unit
) == MD_MAJOR
)
629 new->md_minor
= MINOR(unit
);
631 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
638 static inline int mddev_lock(mddev_t
* mddev
)
640 return mutex_lock_interruptible(&mddev
->reconfig_mutex
);
643 static inline int mddev_is_locked(mddev_t
*mddev
)
645 return mutex_is_locked(&mddev
->reconfig_mutex
);
648 static inline int mddev_trylock(mddev_t
* mddev
)
650 return mutex_trylock(&mddev
->reconfig_mutex
);
653 static struct attribute_group md_redundancy_group
;
655 static void mddev_unlock(mddev_t
* mddev
)
657 if (mddev
->to_remove
) {
658 /* These cannot be removed under reconfig_mutex as
659 * an access to the files will try to take reconfig_mutex
660 * while holding the file unremovable, which leads to
662 * So hold set sysfs_active while the remove in happeing,
663 * and anything else which might set ->to_remove or my
664 * otherwise change the sysfs namespace will fail with
665 * -EBUSY if sysfs_active is still set.
666 * We set sysfs_active under reconfig_mutex and elsewhere
667 * test it under the same mutex to ensure its correct value
670 struct attribute_group
*to_remove
= mddev
->to_remove
;
671 mddev
->to_remove
= NULL
;
672 mddev
->sysfs_active
= 1;
673 mutex_unlock(&mddev
->reconfig_mutex
);
675 if (mddev
->kobj
.sd
) {
676 if (to_remove
!= &md_redundancy_group
)
677 sysfs_remove_group(&mddev
->kobj
, to_remove
);
678 if (mddev
->pers
== NULL
||
679 mddev
->pers
->sync_request
== NULL
) {
680 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
681 if (mddev
->sysfs_action
)
682 sysfs_put(mddev
->sysfs_action
);
683 mddev
->sysfs_action
= NULL
;
686 mddev
->sysfs_active
= 0;
688 mutex_unlock(&mddev
->reconfig_mutex
);
690 md_wakeup_thread(mddev
->thread
);
693 static mdk_rdev_t
* find_rdev_nr(mddev_t
*mddev
, int nr
)
697 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
698 if (rdev
->desc_nr
== nr
)
704 static mdk_rdev_t
* find_rdev(mddev_t
* mddev
, dev_t dev
)
708 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
709 if (rdev
->bdev
->bd_dev
== dev
)
715 static struct mdk_personality
*find_pers(int level
, char *clevel
)
717 struct mdk_personality
*pers
;
718 list_for_each_entry(pers
, &pers_list
, list
) {
719 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
721 if (strcmp(pers
->name
, clevel
)==0)
727 /* return the offset of the super block in 512byte sectors */
728 static inline sector_t
calc_dev_sboffset(mdk_rdev_t
*rdev
)
730 sector_t num_sectors
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
731 return MD_NEW_SIZE_SECTORS(num_sectors
);
734 static int alloc_disk_sb(mdk_rdev_t
* rdev
)
739 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
740 if (!rdev
->sb_page
) {
741 printk(KERN_ALERT
"md: out of memory.\n");
748 static void free_disk_sb(mdk_rdev_t
* rdev
)
751 put_page(rdev
->sb_page
);
753 rdev
->sb_page
= NULL
;
760 static void super_written(struct bio
*bio
, int error
)
762 mdk_rdev_t
*rdev
= bio
->bi_private
;
763 mddev_t
*mddev
= rdev
->mddev
;
765 if (error
|| !test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
766 printk("md: super_written gets error=%d, uptodate=%d\n",
767 error
, test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
768 WARN_ON(test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
769 md_error(mddev
, rdev
);
772 if (atomic_dec_and_test(&mddev
->pending_writes
))
773 wake_up(&mddev
->sb_wait
);
777 void md_super_write(mddev_t
*mddev
, mdk_rdev_t
*rdev
,
778 sector_t sector
, int size
, struct page
*page
)
780 /* write first size bytes of page to sector of rdev
781 * Increment mddev->pending_writes before returning
782 * and decrement it on completion, waking up sb_wait
783 * if zero is reached.
784 * If an error occurred, call md_error
786 struct bio
*bio
= bio_alloc_mddev(GFP_NOIO
, 1, mddev
);
788 bio
->bi_bdev
= rdev
->meta_bdev
? rdev
->meta_bdev
: rdev
->bdev
;
789 bio
->bi_sector
= sector
;
790 bio_add_page(bio
, page
, size
, 0);
791 bio
->bi_private
= rdev
;
792 bio
->bi_end_io
= super_written
;
794 atomic_inc(&mddev
->pending_writes
);
795 submit_bio(REQ_WRITE
| REQ_SYNC
| REQ_FLUSH
| REQ_FUA
, bio
);
798 void md_super_wait(mddev_t
*mddev
)
800 /* wait for all superblock writes that were scheduled to complete */
803 prepare_to_wait(&mddev
->sb_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
804 if (atomic_read(&mddev
->pending_writes
)==0)
808 finish_wait(&mddev
->sb_wait
, &wq
);
811 static void bi_complete(struct bio
*bio
, int error
)
813 complete((struct completion
*)bio
->bi_private
);
816 int sync_page_io(mdk_rdev_t
*rdev
, sector_t sector
, int size
,
817 struct page
*page
, int rw
, bool metadata_op
)
819 struct bio
*bio
= bio_alloc_mddev(GFP_NOIO
, 1, rdev
->mddev
);
820 struct completion event
;
825 bio
->bi_bdev
= (metadata_op
&& rdev
->meta_bdev
) ?
826 rdev
->meta_bdev
: rdev
->bdev
;
828 bio
->bi_sector
= sector
+ rdev
->sb_start
;
830 bio
->bi_sector
= sector
+ rdev
->data_offset
;
831 bio_add_page(bio
, page
, size
, 0);
832 init_completion(&event
);
833 bio
->bi_private
= &event
;
834 bio
->bi_end_io
= bi_complete
;
836 wait_for_completion(&event
);
838 ret
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
842 EXPORT_SYMBOL_GPL(sync_page_io
);
844 static int read_disk_sb(mdk_rdev_t
* rdev
, int size
)
846 char b
[BDEVNAME_SIZE
];
847 if (!rdev
->sb_page
) {
855 if (!sync_page_io(rdev
, 0, size
, rdev
->sb_page
, READ
, true))
861 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
862 bdevname(rdev
->bdev
,b
));
866 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
868 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
869 sb1
->set_uuid1
== sb2
->set_uuid1
&&
870 sb1
->set_uuid2
== sb2
->set_uuid2
&&
871 sb1
->set_uuid3
== sb2
->set_uuid3
;
874 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
877 mdp_super_t
*tmp1
, *tmp2
;
879 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
880 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
882 if (!tmp1
|| !tmp2
) {
884 printk(KERN_INFO
"md.c sb_equal(): failed to allocate memory!\n");
892 * nr_disks is not constant
897 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
905 static u32
md_csum_fold(u32 csum
)
907 csum
= (csum
& 0xffff) + (csum
>> 16);
908 return (csum
& 0xffff) + (csum
>> 16);
911 static unsigned int calc_sb_csum(mdp_super_t
* sb
)
914 u32
*sb32
= (u32
*)sb
;
916 unsigned int disk_csum
, csum
;
918 disk_csum
= sb
->sb_csum
;
921 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
923 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
927 /* This used to use csum_partial, which was wrong for several
928 * reasons including that different results are returned on
929 * different architectures. It isn't critical that we get exactly
930 * the same return value as before (we always csum_fold before
931 * testing, and that removes any differences). However as we
932 * know that csum_partial always returned a 16bit value on
933 * alphas, do a fold to maximise conformity to previous behaviour.
935 sb
->sb_csum
= md_csum_fold(disk_csum
);
937 sb
->sb_csum
= disk_csum
;
944 * Handle superblock details.
945 * We want to be able to handle multiple superblock formats
946 * so we have a common interface to them all, and an array of
947 * different handlers.
948 * We rely on user-space to write the initial superblock, and support
949 * reading and updating of superblocks.
950 * Interface methods are:
951 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
952 * loads and validates a superblock on dev.
953 * if refdev != NULL, compare superblocks on both devices
955 * 0 - dev has a superblock that is compatible with refdev
956 * 1 - dev has a superblock that is compatible and newer than refdev
957 * so dev should be used as the refdev in future
958 * -EINVAL superblock incompatible or invalid
959 * -othererror e.g. -EIO
961 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
962 * Verify that dev is acceptable into mddev.
963 * The first time, mddev->raid_disks will be 0, and data from
964 * dev should be merged in. Subsequent calls check that dev
965 * is new enough. Return 0 or -EINVAL
967 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
968 * Update the superblock for rdev with data in mddev
969 * This does not write to disc.
975 struct module
*owner
;
976 int (*load_super
)(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
,
978 int (*validate_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
979 void (*sync_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
980 unsigned long long (*rdev_size_change
)(mdk_rdev_t
*rdev
,
981 sector_t num_sectors
);
985 * Check that the given mddev has no bitmap.
987 * This function is called from the run method of all personalities that do not
988 * support bitmaps. It prints an error message and returns non-zero if mddev
989 * has a bitmap. Otherwise, it returns 0.
992 int md_check_no_bitmap(mddev_t
*mddev
)
994 if (!mddev
->bitmap_info
.file
&& !mddev
->bitmap_info
.offset
)
996 printk(KERN_ERR
"%s: bitmaps are not supported for %s\n",
997 mdname(mddev
), mddev
->pers
->name
);
1000 EXPORT_SYMBOL(md_check_no_bitmap
);
1003 * load_super for 0.90.0
1005 static int super_90_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
1007 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1012 * Calculate the position of the superblock (512byte sectors),
1013 * it's at the end of the disk.
1015 * It also happens to be a multiple of 4Kb.
1017 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1019 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
1020 if (ret
) return ret
;
1024 bdevname(rdev
->bdev
, b
);
1025 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
1027 if (sb
->md_magic
!= MD_SB_MAGIC
) {
1028 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
1033 if (sb
->major_version
!= 0 ||
1034 sb
->minor_version
< 90 ||
1035 sb
->minor_version
> 91) {
1036 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
1037 sb
->major_version
, sb
->minor_version
,
1042 if (sb
->raid_disks
<= 0)
1045 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
1046 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
1051 rdev
->preferred_minor
= sb
->md_minor
;
1052 rdev
->data_offset
= 0;
1053 rdev
->sb_size
= MD_SB_BYTES
;
1055 if (sb
->level
== LEVEL_MULTIPATH
)
1058 rdev
->desc_nr
= sb
->this_disk
.number
;
1064 mdp_super_t
*refsb
= (mdp_super_t
*)page_address(refdev
->sb_page
);
1065 if (!uuid_equal(refsb
, sb
)) {
1066 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
1067 b
, bdevname(refdev
->bdev
,b2
));
1070 if (!sb_equal(refsb
, sb
)) {
1071 printk(KERN_WARNING
"md: %s has same UUID"
1072 " but different superblock to %s\n",
1073 b
, bdevname(refdev
->bdev
, b2
));
1077 ev2
= md_event(refsb
);
1083 rdev
->sectors
= rdev
->sb_start
;
1085 if (rdev
->sectors
< sb
->size
* 2 && sb
->level
> 1)
1086 /* "this cannot possibly happen" ... */
1094 * validate_super for 0.90.0
1096 static int super_90_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1099 mdp_super_t
*sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
1100 __u64 ev1
= md_event(sb
);
1102 rdev
->raid_disk
= -1;
1103 clear_bit(Faulty
, &rdev
->flags
);
1104 clear_bit(In_sync
, &rdev
->flags
);
1105 clear_bit(WriteMostly
, &rdev
->flags
);
1107 if (mddev
->raid_disks
== 0) {
1108 mddev
->major_version
= 0;
1109 mddev
->minor_version
= sb
->minor_version
;
1110 mddev
->patch_version
= sb
->patch_version
;
1111 mddev
->external
= 0;
1112 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
1113 mddev
->ctime
= sb
->ctime
;
1114 mddev
->utime
= sb
->utime
;
1115 mddev
->level
= sb
->level
;
1116 mddev
->clevel
[0] = 0;
1117 mddev
->layout
= sb
->layout
;
1118 mddev
->raid_disks
= sb
->raid_disks
;
1119 mddev
->dev_sectors
= sb
->size
* 2;
1120 mddev
->events
= ev1
;
1121 mddev
->bitmap_info
.offset
= 0;
1122 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
1124 if (mddev
->minor_version
>= 91) {
1125 mddev
->reshape_position
= sb
->reshape_position
;
1126 mddev
->delta_disks
= sb
->delta_disks
;
1127 mddev
->new_level
= sb
->new_level
;
1128 mddev
->new_layout
= sb
->new_layout
;
1129 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
1131 mddev
->reshape_position
= MaxSector
;
1132 mddev
->delta_disks
= 0;
1133 mddev
->new_level
= mddev
->level
;
1134 mddev
->new_layout
= mddev
->layout
;
1135 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1138 if (sb
->state
& (1<<MD_SB_CLEAN
))
1139 mddev
->recovery_cp
= MaxSector
;
1141 if (sb
->events_hi
== sb
->cp_events_hi
&&
1142 sb
->events_lo
== sb
->cp_events_lo
) {
1143 mddev
->recovery_cp
= sb
->recovery_cp
;
1145 mddev
->recovery_cp
= 0;
1148 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
1149 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
1150 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
1151 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
1153 mddev
->max_disks
= MD_SB_DISKS
;
1155 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
1156 mddev
->bitmap_info
.file
== NULL
)
1157 mddev
->bitmap_info
.offset
=
1158 mddev
->bitmap_info
.default_offset
;
1160 } else if (mddev
->pers
== NULL
) {
1161 /* Insist on good event counter while assembling, except
1162 * for spares (which don't need an event count) */
1164 if (sb
->disks
[rdev
->desc_nr
].state
& (
1165 (1<<MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
)))
1166 if (ev1
< mddev
->events
)
1168 } else if (mddev
->bitmap
) {
1169 /* if adding to array with a bitmap, then we can accept an
1170 * older device ... but not too old.
1172 if (ev1
< mddev
->bitmap
->events_cleared
)
1175 if (ev1
< mddev
->events
)
1176 /* just a hot-add of a new device, leave raid_disk at -1 */
1180 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1181 desc
= sb
->disks
+ rdev
->desc_nr
;
1183 if (desc
->state
& (1<<MD_DISK_FAULTY
))
1184 set_bit(Faulty
, &rdev
->flags
);
1185 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
1186 desc->raid_disk < mddev->raid_disks */) {
1187 set_bit(In_sync
, &rdev
->flags
);
1188 rdev
->raid_disk
= desc
->raid_disk
;
1189 } else if (desc
->state
& (1<<MD_DISK_ACTIVE
)) {
1190 /* active but not in sync implies recovery up to
1191 * reshape position. We don't know exactly where
1192 * that is, so set to zero for now */
1193 if (mddev
->minor_version
>= 91) {
1194 rdev
->recovery_offset
= 0;
1195 rdev
->raid_disk
= desc
->raid_disk
;
1198 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
1199 set_bit(WriteMostly
, &rdev
->flags
);
1200 } else /* MULTIPATH are always insync */
1201 set_bit(In_sync
, &rdev
->flags
);
1206 * sync_super for 0.90.0
1208 static void super_90_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1212 int next_spare
= mddev
->raid_disks
;
1215 /* make rdev->sb match mddev data..
1218 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1219 * 3/ any empty disks < next_spare become removed
1221 * disks[0] gets initialised to REMOVED because
1222 * we cannot be sure from other fields if it has
1223 * been initialised or not.
1226 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
1228 rdev
->sb_size
= MD_SB_BYTES
;
1230 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
1232 memset(sb
, 0, sizeof(*sb
));
1234 sb
->md_magic
= MD_SB_MAGIC
;
1235 sb
->major_version
= mddev
->major_version
;
1236 sb
->patch_version
= mddev
->patch_version
;
1237 sb
->gvalid_words
= 0; /* ignored */
1238 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
1239 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
1240 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
1241 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
1243 sb
->ctime
= mddev
->ctime
;
1244 sb
->level
= mddev
->level
;
1245 sb
->size
= mddev
->dev_sectors
/ 2;
1246 sb
->raid_disks
= mddev
->raid_disks
;
1247 sb
->md_minor
= mddev
->md_minor
;
1248 sb
->not_persistent
= 0;
1249 sb
->utime
= mddev
->utime
;
1251 sb
->events_hi
= (mddev
->events
>>32);
1252 sb
->events_lo
= (u32
)mddev
->events
;
1254 if (mddev
->reshape_position
== MaxSector
)
1255 sb
->minor_version
= 90;
1257 sb
->minor_version
= 91;
1258 sb
->reshape_position
= mddev
->reshape_position
;
1259 sb
->new_level
= mddev
->new_level
;
1260 sb
->delta_disks
= mddev
->delta_disks
;
1261 sb
->new_layout
= mddev
->new_layout
;
1262 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1264 mddev
->minor_version
= sb
->minor_version
;
1267 sb
->recovery_cp
= mddev
->recovery_cp
;
1268 sb
->cp_events_hi
= (mddev
->events
>>32);
1269 sb
->cp_events_lo
= (u32
)mddev
->events
;
1270 if (mddev
->recovery_cp
== MaxSector
)
1271 sb
->state
= (1<< MD_SB_CLEAN
);
1273 sb
->recovery_cp
= 0;
1275 sb
->layout
= mddev
->layout
;
1276 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1278 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
)
1279 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1281 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1282 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1285 int is_active
= test_bit(In_sync
, &rdev2
->flags
);
1287 if (rdev2
->raid_disk
>= 0 &&
1288 sb
->minor_version
>= 91)
1289 /* we have nowhere to store the recovery_offset,
1290 * but if it is not below the reshape_position,
1291 * we can piggy-back on that.
1294 if (rdev2
->raid_disk
< 0 ||
1295 test_bit(Faulty
, &rdev2
->flags
))
1298 desc_nr
= rdev2
->raid_disk
;
1300 desc_nr
= next_spare
++;
1301 rdev2
->desc_nr
= desc_nr
;
1302 d
= &sb
->disks
[rdev2
->desc_nr
];
1304 d
->number
= rdev2
->desc_nr
;
1305 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1306 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1308 d
->raid_disk
= rdev2
->raid_disk
;
1310 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1311 if (test_bit(Faulty
, &rdev2
->flags
))
1312 d
->state
= (1<<MD_DISK_FAULTY
);
1313 else if (is_active
) {
1314 d
->state
= (1<<MD_DISK_ACTIVE
);
1315 if (test_bit(In_sync
, &rdev2
->flags
))
1316 d
->state
|= (1<<MD_DISK_SYNC
);
1324 if (test_bit(WriteMostly
, &rdev2
->flags
))
1325 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1327 /* now set the "removed" and "faulty" bits on any missing devices */
1328 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1329 mdp_disk_t
*d
= &sb
->disks
[i
];
1330 if (d
->state
== 0 && d
->number
== 0) {
1333 d
->state
= (1<<MD_DISK_REMOVED
);
1334 d
->state
|= (1<<MD_DISK_FAULTY
);
1338 sb
->nr_disks
= nr_disks
;
1339 sb
->active_disks
= active
;
1340 sb
->working_disks
= working
;
1341 sb
->failed_disks
= failed
;
1342 sb
->spare_disks
= spare
;
1344 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1345 sb
->sb_csum
= calc_sb_csum(sb
);
1349 * rdev_size_change for 0.90.0
1351 static unsigned long long
1352 super_90_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1354 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1355 return 0; /* component must fit device */
1356 if (rdev
->mddev
->bitmap_info
.offset
)
1357 return 0; /* can't move bitmap */
1358 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1359 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1360 num_sectors
= rdev
->sb_start
;
1361 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1363 md_super_wait(rdev
->mddev
);
1369 * version 1 superblock
1372 static __le32
calc_sb_1_csum(struct mdp_superblock_1
* sb
)
1376 unsigned long long newcsum
;
1377 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1378 __le32
*isuper
= (__le32
*)sb
;
1381 disk_csum
= sb
->sb_csum
;
1384 for (i
=0; size
>=4; size
-= 4 )
1385 newcsum
+= le32_to_cpu(*isuper
++);
1388 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1390 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1391 sb
->sb_csum
= disk_csum
;
1392 return cpu_to_le32(csum
);
1395 static int super_1_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
1397 struct mdp_superblock_1
*sb
;
1400 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1404 * Calculate the position of the superblock in 512byte sectors.
1405 * It is always aligned to a 4K boundary and
1406 * depeding on minor_version, it can be:
1407 * 0: At least 8K, but less than 12K, from end of device
1408 * 1: At start of device
1409 * 2: 4K from start of device.
1411 switch(minor_version
) {
1413 sb_start
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1415 sb_start
&= ~(sector_t
)(4*2-1);
1426 rdev
->sb_start
= sb_start
;
1428 /* superblock is rarely larger than 1K, but it can be larger,
1429 * and it is safe to read 4k, so we do that
1431 ret
= read_disk_sb(rdev
, 4096);
1432 if (ret
) return ret
;
1435 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1437 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1438 sb
->major_version
!= cpu_to_le32(1) ||
1439 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1440 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1441 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1444 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1445 printk("md: invalid superblock checksum on %s\n",
1446 bdevname(rdev
->bdev
,b
));
1449 if (le64_to_cpu(sb
->data_size
) < 10) {
1450 printk("md: data_size too small on %s\n",
1451 bdevname(rdev
->bdev
,b
));
1455 rdev
->preferred_minor
= 0xffff;
1456 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1457 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1459 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1460 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1461 if (rdev
->sb_size
& bmask
)
1462 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1465 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1468 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1471 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1477 struct mdp_superblock_1
*refsb
=
1478 (struct mdp_superblock_1
*)page_address(refdev
->sb_page
);
1480 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1481 sb
->level
!= refsb
->level
||
1482 sb
->layout
!= refsb
->layout
||
1483 sb
->chunksize
!= refsb
->chunksize
) {
1484 printk(KERN_WARNING
"md: %s has strangely different"
1485 " superblock to %s\n",
1486 bdevname(rdev
->bdev
,b
),
1487 bdevname(refdev
->bdev
,b2
));
1490 ev1
= le64_to_cpu(sb
->events
);
1491 ev2
= le64_to_cpu(refsb
->events
);
1499 rdev
->sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
1500 le64_to_cpu(sb
->data_offset
);
1502 rdev
->sectors
= rdev
->sb_start
;
1503 if (rdev
->sectors
< le64_to_cpu(sb
->data_size
))
1505 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1506 if (le64_to_cpu(sb
->size
) > rdev
->sectors
)
1511 static int super_1_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1513 struct mdp_superblock_1
*sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1514 __u64 ev1
= le64_to_cpu(sb
->events
);
1516 rdev
->raid_disk
= -1;
1517 clear_bit(Faulty
, &rdev
->flags
);
1518 clear_bit(In_sync
, &rdev
->flags
);
1519 clear_bit(WriteMostly
, &rdev
->flags
);
1521 if (mddev
->raid_disks
== 0) {
1522 mddev
->major_version
= 1;
1523 mddev
->patch_version
= 0;
1524 mddev
->external
= 0;
1525 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1526 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
1527 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
1528 mddev
->level
= le32_to_cpu(sb
->level
);
1529 mddev
->clevel
[0] = 0;
1530 mddev
->layout
= le32_to_cpu(sb
->layout
);
1531 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1532 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1533 mddev
->events
= ev1
;
1534 mddev
->bitmap_info
.offset
= 0;
1535 mddev
->bitmap_info
.default_offset
= 1024 >> 9;
1537 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1538 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1540 mddev
->max_disks
= (4096-256)/2;
1542 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1543 mddev
->bitmap_info
.file
== NULL
)
1544 mddev
->bitmap_info
.offset
=
1545 (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1547 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1548 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1549 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1550 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1551 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1552 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1554 mddev
->reshape_position
= MaxSector
;
1555 mddev
->delta_disks
= 0;
1556 mddev
->new_level
= mddev
->level
;
1557 mddev
->new_layout
= mddev
->layout
;
1558 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1561 } else if (mddev
->pers
== NULL
) {
1562 /* Insist of good event counter while assembling, except for
1563 * spares (which don't need an event count) */
1565 if (rdev
->desc_nr
>= 0 &&
1566 rdev
->desc_nr
< le32_to_cpu(sb
->max_dev
) &&
1567 le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) < 0xfffe)
1568 if (ev1
< mddev
->events
)
1570 } else if (mddev
->bitmap
) {
1571 /* If adding to array with a bitmap, then we can accept an
1572 * older device, but not too old.
1574 if (ev1
< mddev
->bitmap
->events_cleared
)
1577 if (ev1
< mddev
->events
)
1578 /* just a hot-add of a new device, leave raid_disk at -1 */
1581 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1583 if (rdev
->desc_nr
< 0 ||
1584 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1588 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1590 case 0xffff: /* spare */
1592 case 0xfffe: /* faulty */
1593 set_bit(Faulty
, &rdev
->flags
);
1596 if ((le32_to_cpu(sb
->feature_map
) &
1597 MD_FEATURE_RECOVERY_OFFSET
))
1598 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1600 set_bit(In_sync
, &rdev
->flags
);
1601 rdev
->raid_disk
= role
;
1604 if (sb
->devflags
& WriteMostly1
)
1605 set_bit(WriteMostly
, &rdev
->flags
);
1606 } else /* MULTIPATH are always insync */
1607 set_bit(In_sync
, &rdev
->flags
);
1612 static void super_1_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1614 struct mdp_superblock_1
*sb
;
1617 /* make rdev->sb match mddev and rdev data. */
1619 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1621 sb
->feature_map
= 0;
1623 sb
->recovery_offset
= cpu_to_le64(0);
1624 memset(sb
->pad1
, 0, sizeof(sb
->pad1
));
1625 memset(sb
->pad2
, 0, sizeof(sb
->pad2
));
1626 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1628 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1629 sb
->events
= cpu_to_le64(mddev
->events
);
1631 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1633 sb
->resync_offset
= cpu_to_le64(0);
1635 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1637 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1638 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
1639 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
1640 sb
->level
= cpu_to_le32(mddev
->level
);
1641 sb
->layout
= cpu_to_le32(mddev
->layout
);
1643 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
) {
1644 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_info
.offset
);
1645 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1648 if (rdev
->raid_disk
>= 0 &&
1649 !test_bit(In_sync
, &rdev
->flags
)) {
1651 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1652 sb
->recovery_offset
=
1653 cpu_to_le64(rdev
->recovery_offset
);
1656 if (mddev
->reshape_position
!= MaxSector
) {
1657 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1658 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1659 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1660 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1661 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1662 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
1666 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
1667 if (rdev2
->desc_nr
+1 > max_dev
)
1668 max_dev
= rdev2
->desc_nr
+1;
1670 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
1672 sb
->max_dev
= cpu_to_le32(max_dev
);
1673 rdev
->sb_size
= max_dev
* 2 + 256;
1674 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1675 if (rdev
->sb_size
& bmask
)
1676 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1678 max_dev
= le32_to_cpu(sb
->max_dev
);
1680 for (i
=0; i
<max_dev
;i
++)
1681 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1683 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1685 if (test_bit(Faulty
, &rdev2
->flags
))
1686 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1687 else if (test_bit(In_sync
, &rdev2
->flags
))
1688 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1689 else if (rdev2
->raid_disk
>= 0)
1690 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1692 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1695 sb
->sb_csum
= calc_sb_1_csum(sb
);
1698 static unsigned long long
1699 super_1_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1701 struct mdp_superblock_1
*sb
;
1702 sector_t max_sectors
;
1703 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1704 return 0; /* component must fit device */
1705 if (rdev
->sb_start
< rdev
->data_offset
) {
1706 /* minor versions 1 and 2; superblock before data */
1707 max_sectors
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1708 max_sectors
-= rdev
->data_offset
;
1709 if (!num_sectors
|| num_sectors
> max_sectors
)
1710 num_sectors
= max_sectors
;
1711 } else if (rdev
->mddev
->bitmap_info
.offset
) {
1712 /* minor version 0 with bitmap we can't move */
1715 /* minor version 0; superblock after data */
1717 sb_start
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) - 8*2;
1718 sb_start
&= ~(sector_t
)(4*2 - 1);
1719 max_sectors
= rdev
->sectors
+ sb_start
- rdev
->sb_start
;
1720 if (!num_sectors
|| num_sectors
> max_sectors
)
1721 num_sectors
= max_sectors
;
1722 rdev
->sb_start
= sb_start
;
1724 sb
= (struct mdp_superblock_1
*) page_address(rdev
->sb_page
);
1725 sb
->data_size
= cpu_to_le64(num_sectors
);
1726 sb
->super_offset
= rdev
->sb_start
;
1727 sb
->sb_csum
= calc_sb_1_csum(sb
);
1728 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1730 md_super_wait(rdev
->mddev
);
1734 static struct super_type super_types
[] = {
1737 .owner
= THIS_MODULE
,
1738 .load_super
= super_90_load
,
1739 .validate_super
= super_90_validate
,
1740 .sync_super
= super_90_sync
,
1741 .rdev_size_change
= super_90_rdev_size_change
,
1745 .owner
= THIS_MODULE
,
1746 .load_super
= super_1_load
,
1747 .validate_super
= super_1_validate
,
1748 .sync_super
= super_1_sync
,
1749 .rdev_size_change
= super_1_rdev_size_change
,
1753 static int match_mddev_units(mddev_t
*mddev1
, mddev_t
*mddev2
)
1755 mdk_rdev_t
*rdev
, *rdev2
;
1758 rdev_for_each_rcu(rdev
, mddev1
)
1759 rdev_for_each_rcu(rdev2
, mddev2
)
1760 if (rdev
->bdev
->bd_contains
==
1761 rdev2
->bdev
->bd_contains
) {
1769 static LIST_HEAD(pending_raid_disks
);
1772 * Try to register data integrity profile for an mddev
1774 * This is called when an array is started and after a disk has been kicked
1775 * from the array. It only succeeds if all working and active component devices
1776 * are integrity capable with matching profiles.
1778 int md_integrity_register(mddev_t
*mddev
)
1780 mdk_rdev_t
*rdev
, *reference
= NULL
;
1782 if (list_empty(&mddev
->disks
))
1783 return 0; /* nothing to do */
1784 if (blk_get_integrity(mddev
->gendisk
))
1785 return 0; /* already registered */
1786 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
1787 /* skip spares and non-functional disks */
1788 if (test_bit(Faulty
, &rdev
->flags
))
1790 if (rdev
->raid_disk
< 0)
1793 /* Use the first rdev as the reference */
1797 /* does this rdev's profile match the reference profile? */
1798 if (blk_integrity_compare(reference
->bdev
->bd_disk
,
1799 rdev
->bdev
->bd_disk
) < 0)
1802 if (!reference
|| !bdev_get_integrity(reference
->bdev
))
1805 * All component devices are integrity capable and have matching
1806 * profiles, register the common profile for the md device.
1808 if (blk_integrity_register(mddev
->gendisk
,
1809 bdev_get_integrity(reference
->bdev
)) != 0) {
1810 printk(KERN_ERR
"md: failed to register integrity for %s\n",
1814 printk(KERN_NOTICE
"md: data integrity enabled on %s\n", mdname(mddev
));
1815 if (bioset_integrity_create(mddev
->bio_set
, BIO_POOL_SIZE
)) {
1816 printk(KERN_ERR
"md: failed to create integrity pool for %s\n",
1822 EXPORT_SYMBOL(md_integrity_register
);
1824 /* Disable data integrity if non-capable/non-matching disk is being added */
1825 void md_integrity_add_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
1827 struct blk_integrity
*bi_rdev
= bdev_get_integrity(rdev
->bdev
);
1828 struct blk_integrity
*bi_mddev
= blk_get_integrity(mddev
->gendisk
);
1830 if (!bi_mddev
) /* nothing to do */
1832 if (rdev
->raid_disk
< 0) /* skip spares */
1834 if (bi_rdev
&& blk_integrity_compare(mddev
->gendisk
,
1835 rdev
->bdev
->bd_disk
) >= 0)
1837 printk(KERN_NOTICE
"disabling data integrity on %s\n", mdname(mddev
));
1838 blk_integrity_unregister(mddev
->gendisk
);
1840 EXPORT_SYMBOL(md_integrity_add_rdev
);
1842 static int bind_rdev_to_array(mdk_rdev_t
* rdev
, mddev_t
* mddev
)
1844 char b
[BDEVNAME_SIZE
];
1854 /* prevent duplicates */
1855 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
1858 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1859 if (rdev
->sectors
&& (mddev
->dev_sectors
== 0 ||
1860 rdev
->sectors
< mddev
->dev_sectors
)) {
1862 /* Cannot change size, so fail
1863 * If mddev->level <= 0, then we don't care
1864 * about aligning sizes (e.g. linear)
1866 if (mddev
->level
> 0)
1869 mddev
->dev_sectors
= rdev
->sectors
;
1872 /* Verify rdev->desc_nr is unique.
1873 * If it is -1, assign a free number, else
1874 * check number is not in use
1876 if (rdev
->desc_nr
< 0) {
1878 if (mddev
->pers
) choice
= mddev
->raid_disks
;
1879 while (find_rdev_nr(mddev
, choice
))
1881 rdev
->desc_nr
= choice
;
1883 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
1886 if (mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
1887 printk(KERN_WARNING
"md: %s: array is limited to %d devices\n",
1888 mdname(mddev
), mddev
->max_disks
);
1891 bdevname(rdev
->bdev
,b
);
1892 while ( (s
=strchr(b
, '/')) != NULL
)
1895 rdev
->mddev
= mddev
;
1896 printk(KERN_INFO
"md: bind<%s>\n", b
);
1898 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
1901 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
1902 if (sysfs_create_link(&rdev
->kobj
, ko
, "block"))
1903 /* failure here is OK */;
1904 rdev
->sysfs_state
= sysfs_get_dirent_safe(rdev
->kobj
.sd
, "state");
1906 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
1907 bd_link_disk_holder(rdev
->bdev
, mddev
->gendisk
);
1909 /* May as well allow recovery to be retried once */
1910 mddev
->recovery_disabled
= 0;
1915 printk(KERN_WARNING
"md: failed to register dev-%s for %s\n",
1920 static void md_delayed_delete(struct work_struct
*ws
)
1922 mdk_rdev_t
*rdev
= container_of(ws
, mdk_rdev_t
, del_work
);
1923 kobject_del(&rdev
->kobj
);
1924 kobject_put(&rdev
->kobj
);
1927 static void unbind_rdev_from_array(mdk_rdev_t
* rdev
)
1929 char b
[BDEVNAME_SIZE
];
1934 bd_unlink_disk_holder(rdev
->bdev
, rdev
->mddev
->gendisk
);
1935 list_del_rcu(&rdev
->same_set
);
1936 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
1938 sysfs_remove_link(&rdev
->kobj
, "block");
1939 sysfs_put(rdev
->sysfs_state
);
1940 rdev
->sysfs_state
= NULL
;
1941 /* We need to delay this, otherwise we can deadlock when
1942 * writing to 'remove' to "dev/state". We also need
1943 * to delay it due to rcu usage.
1946 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
1947 kobject_get(&rdev
->kobj
);
1948 queue_work(md_misc_wq
, &rdev
->del_work
);
1952 * prevent the device from being mounted, repartitioned or
1953 * otherwise reused by a RAID array (or any other kernel
1954 * subsystem), by bd_claiming the device.
1956 static int lock_rdev(mdk_rdev_t
*rdev
, dev_t dev
, int shared
)
1959 struct block_device
*bdev
;
1960 char b
[BDEVNAME_SIZE
];
1962 bdev
= blkdev_get_by_dev(dev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
,
1963 shared
? (mdk_rdev_t
*)lock_rdev
: rdev
);
1965 printk(KERN_ERR
"md: could not open %s.\n",
1966 __bdevname(dev
, b
));
1967 return PTR_ERR(bdev
);
1973 static void unlock_rdev(mdk_rdev_t
*rdev
)
1975 struct block_device
*bdev
= rdev
->bdev
;
1979 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
1982 void md_autodetect_dev(dev_t dev
);
1984 static void export_rdev(mdk_rdev_t
* rdev
)
1986 char b
[BDEVNAME_SIZE
];
1987 printk(KERN_INFO
"md: export_rdev(%s)\n",
1988 bdevname(rdev
->bdev
,b
));
1993 if (test_bit(AutoDetected
, &rdev
->flags
))
1994 md_autodetect_dev(rdev
->bdev
->bd_dev
);
1997 kobject_put(&rdev
->kobj
);
2000 static void kick_rdev_from_array(mdk_rdev_t
* rdev
)
2002 unbind_rdev_from_array(rdev
);
2006 static void export_array(mddev_t
*mddev
)
2008 mdk_rdev_t
*rdev
, *tmp
;
2010 rdev_for_each(rdev
, tmp
, mddev
) {
2015 kick_rdev_from_array(rdev
);
2017 if (!list_empty(&mddev
->disks
))
2019 mddev
->raid_disks
= 0;
2020 mddev
->major_version
= 0;
2023 static void print_desc(mdp_disk_t
*desc
)
2025 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc
->number
,
2026 desc
->major
,desc
->minor
,desc
->raid_disk
,desc
->state
);
2029 static void print_sb_90(mdp_super_t
*sb
)
2034 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2035 sb
->major_version
, sb
->minor_version
, sb
->patch_version
,
2036 sb
->set_uuid0
, sb
->set_uuid1
, sb
->set_uuid2
, sb
->set_uuid3
,
2038 printk(KERN_INFO
"md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2039 sb
->level
, sb
->size
, sb
->nr_disks
, sb
->raid_disks
,
2040 sb
->md_minor
, sb
->layout
, sb
->chunk_size
);
2041 printk(KERN_INFO
"md: UT:%08x ST:%d AD:%d WD:%d"
2042 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2043 sb
->utime
, sb
->state
, sb
->active_disks
, sb
->working_disks
,
2044 sb
->failed_disks
, sb
->spare_disks
,
2045 sb
->sb_csum
, (unsigned long)sb
->events_lo
);
2048 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
2051 desc
= sb
->disks
+ i
;
2052 if (desc
->number
|| desc
->major
|| desc
->minor
||
2053 desc
->raid_disk
|| (desc
->state
&& (desc
->state
!= 4))) {
2054 printk(" D %2d: ", i
);
2058 printk(KERN_INFO
"md: THIS: ");
2059 print_desc(&sb
->this_disk
);
2062 static void print_sb_1(struct mdp_superblock_1
*sb
)
2066 uuid
= sb
->set_uuid
;
2068 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
2069 "md: Name: \"%s\" CT:%llu\n",
2070 le32_to_cpu(sb
->major_version
),
2071 le32_to_cpu(sb
->feature_map
),
2074 (unsigned long long)le64_to_cpu(sb
->ctime
)
2075 & MD_SUPERBLOCK_1_TIME_SEC_MASK
);
2077 uuid
= sb
->device_uuid
;
2079 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
2081 "md: Dev:%08x UUID: %pU\n"
2082 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2083 "md: (MaxDev:%u) \n",
2084 le32_to_cpu(sb
->level
),
2085 (unsigned long long)le64_to_cpu(sb
->size
),
2086 le32_to_cpu(sb
->raid_disks
),
2087 le32_to_cpu(sb
->layout
),
2088 le32_to_cpu(sb
->chunksize
),
2089 (unsigned long long)le64_to_cpu(sb
->data_offset
),
2090 (unsigned long long)le64_to_cpu(sb
->data_size
),
2091 (unsigned long long)le64_to_cpu(sb
->super_offset
),
2092 (unsigned long long)le64_to_cpu(sb
->recovery_offset
),
2093 le32_to_cpu(sb
->dev_number
),
2096 (unsigned long long)le64_to_cpu(sb
->utime
) & MD_SUPERBLOCK_1_TIME_SEC_MASK
,
2097 (unsigned long long)le64_to_cpu(sb
->events
),
2098 (unsigned long long)le64_to_cpu(sb
->resync_offset
),
2099 le32_to_cpu(sb
->sb_csum
),
2100 le32_to_cpu(sb
->max_dev
)
2104 static void print_rdev(mdk_rdev_t
*rdev
, int major_version
)
2106 char b
[BDEVNAME_SIZE
];
2107 printk(KERN_INFO
"md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2108 bdevname(rdev
->bdev
, b
), (unsigned long long)rdev
->sectors
,
2109 test_bit(Faulty
, &rdev
->flags
), test_bit(In_sync
, &rdev
->flags
),
2111 if (rdev
->sb_loaded
) {
2112 printk(KERN_INFO
"md: rdev superblock (MJ:%d):\n", major_version
);
2113 switch (major_version
) {
2115 print_sb_90((mdp_super_t
*)page_address(rdev
->sb_page
));
2118 print_sb_1((struct mdp_superblock_1
*)page_address(rdev
->sb_page
));
2122 printk(KERN_INFO
"md: no rdev superblock!\n");
2125 static void md_print_devices(void)
2127 struct list_head
*tmp
;
2130 char b
[BDEVNAME_SIZE
];
2133 printk("md: **********************************\n");
2134 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2135 printk("md: **********************************\n");
2136 for_each_mddev(mddev
, tmp
) {
2139 bitmap_print_sb(mddev
->bitmap
);
2141 printk("%s: ", mdname(mddev
));
2142 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2143 printk("<%s>", bdevname(rdev
->bdev
,b
));
2146 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2147 print_rdev(rdev
, mddev
->major_version
);
2149 printk("md: **********************************\n");
2154 static void sync_sbs(mddev_t
* mddev
, int nospares
)
2156 /* Update each superblock (in-memory image), but
2157 * if we are allowed to, skip spares which already
2158 * have the right event counter, or have one earlier
2159 * (which would mean they aren't being marked as dirty
2160 * with the rest of the array)
2163 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2164 if (rdev
->sb_events
== mddev
->events
||
2166 rdev
->raid_disk
< 0 &&
2167 rdev
->sb_events
+1 == mddev
->events
)) {
2168 /* Don't update this superblock */
2169 rdev
->sb_loaded
= 2;
2171 super_types
[mddev
->major_version
].
2172 sync_super(mddev
, rdev
);
2173 rdev
->sb_loaded
= 1;
2178 static void md_update_sb(mddev_t
* mddev
, int force_change
)
2185 /* First make sure individual recovery_offsets are correct */
2186 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2187 if (rdev
->raid_disk
>= 0 &&
2188 mddev
->delta_disks
>= 0 &&
2189 !test_bit(In_sync
, &rdev
->flags
) &&
2190 mddev
->curr_resync_completed
> rdev
->recovery_offset
)
2191 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
2194 if (!mddev
->persistent
) {
2195 clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
2196 clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2197 if (!mddev
->external
)
2198 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2199 wake_up(&mddev
->sb_wait
);
2203 spin_lock_irq(&mddev
->write_lock
);
2205 mddev
->utime
= get_seconds();
2207 if (test_and_clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
))
2209 if (test_and_clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
2210 /* just a clean<-> dirty transition, possibly leave spares alone,
2211 * though if events isn't the right even/odd, we will have to do
2217 if (mddev
->degraded
)
2218 /* If the array is degraded, then skipping spares is both
2219 * dangerous and fairly pointless.
2220 * Dangerous because a device that was removed from the array
2221 * might have a event_count that still looks up-to-date,
2222 * so it can be re-added without a resync.
2223 * Pointless because if there are any spares to skip,
2224 * then a recovery will happen and soon that array won't
2225 * be degraded any more and the spare can go back to sleep then.
2229 sync_req
= mddev
->in_sync
;
2231 /* If this is just a dirty<->clean transition, and the array is clean
2232 * and 'events' is odd, we can roll back to the previous clean state */
2234 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
2235 && mddev
->can_decrease_events
2236 && mddev
->events
!= 1) {
2238 mddev
->can_decrease_events
= 0;
2240 /* otherwise we have to go forward and ... */
2242 mddev
->can_decrease_events
= nospares
;
2245 if (!mddev
->events
) {
2247 * oops, this 64-bit counter should never wrap.
2248 * Either we are in around ~1 trillion A.C., assuming
2249 * 1 reboot per second, or we have a bug:
2254 sync_sbs(mddev
, nospares
);
2255 spin_unlock_irq(&mddev
->write_lock
);
2258 "md: updating %s RAID superblock on device (in sync %d)\n",
2259 mdname(mddev
),mddev
->in_sync
);
2261 bitmap_update_sb(mddev
->bitmap
);
2262 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2263 char b
[BDEVNAME_SIZE
];
2264 dprintk(KERN_INFO
"md: ");
2265 if (rdev
->sb_loaded
!= 1)
2266 continue; /* no noise on spare devices */
2267 if (test_bit(Faulty
, &rdev
->flags
))
2268 dprintk("(skipping faulty ");
2270 dprintk("%s ", bdevname(rdev
->bdev
,b
));
2271 if (!test_bit(Faulty
, &rdev
->flags
)) {
2272 md_super_write(mddev
,rdev
,
2273 rdev
->sb_start
, rdev
->sb_size
,
2275 dprintk(KERN_INFO
"(write) %s's sb offset: %llu\n",
2276 bdevname(rdev
->bdev
,b
),
2277 (unsigned long long)rdev
->sb_start
);
2278 rdev
->sb_events
= mddev
->events
;
2282 if (mddev
->level
== LEVEL_MULTIPATH
)
2283 /* only need to write one superblock... */
2286 md_super_wait(mddev
);
2287 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2289 spin_lock_irq(&mddev
->write_lock
);
2290 if (mddev
->in_sync
!= sync_req
||
2291 test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)) {
2292 /* have to write it out again */
2293 spin_unlock_irq(&mddev
->write_lock
);
2296 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2297 spin_unlock_irq(&mddev
->write_lock
);
2298 wake_up(&mddev
->sb_wait
);
2299 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2300 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
2304 /* words written to sysfs files may, or may not, be \n terminated.
2305 * We want to accept with case. For this we use cmd_match.
2307 static int cmd_match(const char *cmd
, const char *str
)
2309 /* See if cmd, written into a sysfs file, matches
2310 * str. They must either be the same, or cmd can
2311 * have a trailing newline
2313 while (*cmd
&& *str
&& *cmd
== *str
) {
2324 struct rdev_sysfs_entry
{
2325 struct attribute attr
;
2326 ssize_t (*show
)(mdk_rdev_t
*, char *);
2327 ssize_t (*store
)(mdk_rdev_t
*, const char *, size_t);
2331 state_show(mdk_rdev_t
*rdev
, char *page
)
2336 if (test_bit(Faulty
, &rdev
->flags
)) {
2337 len
+= sprintf(page
+len
, "%sfaulty",sep
);
2340 if (test_bit(In_sync
, &rdev
->flags
)) {
2341 len
+= sprintf(page
+len
, "%sin_sync",sep
);
2344 if (test_bit(WriteMostly
, &rdev
->flags
)) {
2345 len
+= sprintf(page
+len
, "%swrite_mostly",sep
);
2348 if (test_bit(Blocked
, &rdev
->flags
)) {
2349 len
+= sprintf(page
+len
, "%sblocked", sep
);
2352 if (!test_bit(Faulty
, &rdev
->flags
) &&
2353 !test_bit(In_sync
, &rdev
->flags
)) {
2354 len
+= sprintf(page
+len
, "%sspare", sep
);
2357 return len
+sprintf(page
+len
, "\n");
2361 state_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2364 * faulty - simulates and error
2365 * remove - disconnects the device
2366 * writemostly - sets write_mostly
2367 * -writemostly - clears write_mostly
2368 * blocked - sets the Blocked flag
2369 * -blocked - clears the Blocked flag
2370 * insync - sets Insync providing device isn't active
2373 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
2374 md_error(rdev
->mddev
, rdev
);
2376 } else if (cmd_match(buf
, "remove")) {
2377 if (rdev
->raid_disk
>= 0)
2380 mddev_t
*mddev
= rdev
->mddev
;
2381 kick_rdev_from_array(rdev
);
2383 md_update_sb(mddev
, 1);
2384 md_new_event(mddev
);
2387 } else if (cmd_match(buf
, "writemostly")) {
2388 set_bit(WriteMostly
, &rdev
->flags
);
2390 } else if (cmd_match(buf
, "-writemostly")) {
2391 clear_bit(WriteMostly
, &rdev
->flags
);
2393 } else if (cmd_match(buf
, "blocked")) {
2394 set_bit(Blocked
, &rdev
->flags
);
2396 } else if (cmd_match(buf
, "-blocked")) {
2397 clear_bit(Blocked
, &rdev
->flags
);
2398 wake_up(&rdev
->blocked_wait
);
2399 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2400 md_wakeup_thread(rdev
->mddev
->thread
);
2403 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
2404 set_bit(In_sync
, &rdev
->flags
);
2408 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2409 return err
? err
: len
;
2411 static struct rdev_sysfs_entry rdev_state
=
2412 __ATTR(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
2415 errors_show(mdk_rdev_t
*rdev
, char *page
)
2417 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
2421 errors_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2424 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2425 if (*buf
&& (*e
== 0 || *e
== '\n')) {
2426 atomic_set(&rdev
->corrected_errors
, n
);
2431 static struct rdev_sysfs_entry rdev_errors
=
2432 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
2435 slot_show(mdk_rdev_t
*rdev
, char *page
)
2437 if (rdev
->raid_disk
< 0)
2438 return sprintf(page
, "none\n");
2440 return sprintf(page
, "%d\n", rdev
->raid_disk
);
2444 slot_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2449 int slot
= simple_strtoul(buf
, &e
, 10);
2450 if (strncmp(buf
, "none", 4)==0)
2452 else if (e
==buf
|| (*e
&& *e
!= '\n'))
2454 if (rdev
->mddev
->pers
&& slot
== -1) {
2455 /* Setting 'slot' on an active array requires also
2456 * updating the 'rd%d' link, and communicating
2457 * with the personality with ->hot_*_disk.
2458 * For now we only support removing
2459 * failed/spare devices. This normally happens automatically,
2460 * but not when the metadata is externally managed.
2462 if (rdev
->raid_disk
== -1)
2464 /* personality does all needed checks */
2465 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2467 err
= rdev
->mddev
->pers
->
2468 hot_remove_disk(rdev
->mddev
, rdev
->raid_disk
);
2471 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2472 sysfs_remove_link(&rdev
->mddev
->kobj
, nm
);
2473 rdev
->raid_disk
= -1;
2474 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2475 md_wakeup_thread(rdev
->mddev
->thread
);
2476 } else if (rdev
->mddev
->pers
) {
2478 /* Activating a spare .. or possibly reactivating
2479 * if we ever get bitmaps working here.
2482 if (rdev
->raid_disk
!= -1)
2485 if (test_bit(MD_RECOVERY_RUNNING
, &rdev
->mddev
->recovery
))
2488 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2491 list_for_each_entry(rdev2
, &rdev
->mddev
->disks
, same_set
)
2492 if (rdev2
->raid_disk
== slot
)
2495 if (slot
>= rdev
->mddev
->raid_disks
&&
2496 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2499 rdev
->raid_disk
= slot
;
2500 if (test_bit(In_sync
, &rdev
->flags
))
2501 rdev
->saved_raid_disk
= slot
;
2503 rdev
->saved_raid_disk
= -1;
2504 err
= rdev
->mddev
->pers
->
2505 hot_add_disk(rdev
->mddev
, rdev
);
2507 rdev
->raid_disk
= -1;
2510 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2511 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2512 if (sysfs_create_link(&rdev
->mddev
->kobj
, &rdev
->kobj
, nm
))
2513 /* failure here is OK */;
2514 /* don't wakeup anyone, leave that to userspace. */
2516 if (slot
>= rdev
->mddev
->raid_disks
&&
2517 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2519 rdev
->raid_disk
= slot
;
2520 /* assume it is working */
2521 clear_bit(Faulty
, &rdev
->flags
);
2522 clear_bit(WriteMostly
, &rdev
->flags
);
2523 set_bit(In_sync
, &rdev
->flags
);
2524 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2530 static struct rdev_sysfs_entry rdev_slot
=
2531 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
2534 offset_show(mdk_rdev_t
*rdev
, char *page
)
2536 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
2540 offset_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2543 unsigned long long offset
= simple_strtoull(buf
, &e
, 10);
2544 if (e
==buf
|| (*e
&& *e
!= '\n'))
2546 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
2548 if (rdev
->sectors
&& rdev
->mddev
->external
)
2549 /* Must set offset before size, so overlap checks
2552 rdev
->data_offset
= offset
;
2556 static struct rdev_sysfs_entry rdev_offset
=
2557 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
2560 rdev_size_show(mdk_rdev_t
*rdev
, char *page
)
2562 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
2565 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
2567 /* check if two start/length pairs overlap */
2575 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
2577 unsigned long long blocks
;
2580 if (strict_strtoull(buf
, 10, &blocks
) < 0)
2583 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
2584 return -EINVAL
; /* sector conversion overflow */
2587 if (new != blocks
* 2)
2588 return -EINVAL
; /* unsigned long long to sector_t overflow */
2595 rdev_size_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2597 mddev_t
*my_mddev
= rdev
->mddev
;
2598 sector_t oldsectors
= rdev
->sectors
;
2601 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
2603 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
2604 if (my_mddev
->persistent
) {
2605 sectors
= super_types
[my_mddev
->major_version
].
2606 rdev_size_change(rdev
, sectors
);
2609 } else if (!sectors
)
2610 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
2613 if (sectors
< my_mddev
->dev_sectors
)
2614 return -EINVAL
; /* component must fit device */
2616 rdev
->sectors
= sectors
;
2617 if (sectors
> oldsectors
&& my_mddev
->external
) {
2618 /* need to check that all other rdevs with the same ->bdev
2619 * do not overlap. We need to unlock the mddev to avoid
2620 * a deadlock. We have already changed rdev->sectors, and if
2621 * we have to change it back, we will have the lock again.
2625 struct list_head
*tmp
;
2627 mddev_unlock(my_mddev
);
2628 for_each_mddev(mddev
, tmp
) {
2632 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
2633 if (rdev
->bdev
== rdev2
->bdev
&&
2635 overlaps(rdev
->data_offset
, rdev
->sectors
,
2641 mddev_unlock(mddev
);
2647 mddev_lock(my_mddev
);
2649 /* Someone else could have slipped in a size
2650 * change here, but doing so is just silly.
2651 * We put oldsectors back because we *know* it is
2652 * safe, and trust userspace not to race with
2655 rdev
->sectors
= oldsectors
;
2662 static struct rdev_sysfs_entry rdev_size
=
2663 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
2666 static ssize_t
recovery_start_show(mdk_rdev_t
*rdev
, char *page
)
2668 unsigned long long recovery_start
= rdev
->recovery_offset
;
2670 if (test_bit(In_sync
, &rdev
->flags
) ||
2671 recovery_start
== MaxSector
)
2672 return sprintf(page
, "none\n");
2674 return sprintf(page
, "%llu\n", recovery_start
);
2677 static ssize_t
recovery_start_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2679 unsigned long long recovery_start
;
2681 if (cmd_match(buf
, "none"))
2682 recovery_start
= MaxSector
;
2683 else if (strict_strtoull(buf
, 10, &recovery_start
))
2686 if (rdev
->mddev
->pers
&&
2687 rdev
->raid_disk
>= 0)
2690 rdev
->recovery_offset
= recovery_start
;
2691 if (recovery_start
== MaxSector
)
2692 set_bit(In_sync
, &rdev
->flags
);
2694 clear_bit(In_sync
, &rdev
->flags
);
2698 static struct rdev_sysfs_entry rdev_recovery_start
=
2699 __ATTR(recovery_start
, S_IRUGO
|S_IWUSR
, recovery_start_show
, recovery_start_store
);
2701 static struct attribute
*rdev_default_attrs
[] = {
2707 &rdev_recovery_start
.attr
,
2711 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
2713 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2714 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2715 mddev_t
*mddev
= rdev
->mddev
;
2721 rv
= mddev
? mddev_lock(mddev
) : -EBUSY
;
2723 if (rdev
->mddev
== NULL
)
2726 rv
= entry
->show(rdev
, page
);
2727 mddev_unlock(mddev
);
2733 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
2734 const char *page
, size_t length
)
2736 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2737 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2739 mddev_t
*mddev
= rdev
->mddev
;
2743 if (!capable(CAP_SYS_ADMIN
))
2745 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
2747 if (rdev
->mddev
== NULL
)
2750 rv
= entry
->store(rdev
, page
, length
);
2751 mddev_unlock(mddev
);
2756 static void rdev_free(struct kobject
*ko
)
2758 mdk_rdev_t
*rdev
= container_of(ko
, mdk_rdev_t
, kobj
);
2761 static const struct sysfs_ops rdev_sysfs_ops
= {
2762 .show
= rdev_attr_show
,
2763 .store
= rdev_attr_store
,
2765 static struct kobj_type rdev_ktype
= {
2766 .release
= rdev_free
,
2767 .sysfs_ops
= &rdev_sysfs_ops
,
2768 .default_attrs
= rdev_default_attrs
,
2771 void md_rdev_init(mdk_rdev_t
*rdev
)
2774 rdev
->saved_raid_disk
= -1;
2775 rdev
->raid_disk
= -1;
2777 rdev
->data_offset
= 0;
2778 rdev
->sb_events
= 0;
2779 rdev
->last_read_error
.tv_sec
= 0;
2780 rdev
->last_read_error
.tv_nsec
= 0;
2781 atomic_set(&rdev
->nr_pending
, 0);
2782 atomic_set(&rdev
->read_errors
, 0);
2783 atomic_set(&rdev
->corrected_errors
, 0);
2785 INIT_LIST_HEAD(&rdev
->same_set
);
2786 init_waitqueue_head(&rdev
->blocked_wait
);
2788 EXPORT_SYMBOL_GPL(md_rdev_init
);
2790 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2792 * mark the device faulty if:
2794 * - the device is nonexistent (zero size)
2795 * - the device has no valid superblock
2797 * a faulty rdev _never_ has rdev->sb set.
2799 static mdk_rdev_t
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
2801 char b
[BDEVNAME_SIZE
];
2806 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
2808 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
2809 return ERR_PTR(-ENOMEM
);
2813 if ((err
= alloc_disk_sb(rdev
)))
2816 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
2820 kobject_init(&rdev
->kobj
, &rdev_ktype
);
2822 size
= i_size_read(rdev
->bdev
->bd_inode
) >> BLOCK_SIZE_BITS
;
2825 "md: %s has zero or unknown size, marking faulty!\n",
2826 bdevname(rdev
->bdev
,b
));
2831 if (super_format
>= 0) {
2832 err
= super_types
[super_format
].
2833 load_super(rdev
, NULL
, super_minor
);
2834 if (err
== -EINVAL
) {
2836 "md: %s does not have a valid v%d.%d "
2837 "superblock, not importing!\n",
2838 bdevname(rdev
->bdev
,b
),
2839 super_format
, super_minor
);
2844 "md: could not read %s's sb, not importing!\n",
2845 bdevname(rdev
->bdev
,b
));
2853 if (rdev
->sb_page
) {
2859 return ERR_PTR(err
);
2863 * Check a full RAID array for plausibility
2867 static void analyze_sbs(mddev_t
* mddev
)
2870 mdk_rdev_t
*rdev
, *freshest
, *tmp
;
2871 char b
[BDEVNAME_SIZE
];
2874 rdev_for_each(rdev
, tmp
, mddev
)
2875 switch (super_types
[mddev
->major_version
].
2876 load_super(rdev
, freshest
, mddev
->minor_version
)) {
2884 "md: fatal superblock inconsistency in %s"
2885 " -- removing from array\n",
2886 bdevname(rdev
->bdev
,b
));
2887 kick_rdev_from_array(rdev
);
2891 super_types
[mddev
->major_version
].
2892 validate_super(mddev
, freshest
);
2895 rdev_for_each(rdev
, tmp
, mddev
) {
2896 if (mddev
->max_disks
&&
2897 (rdev
->desc_nr
>= mddev
->max_disks
||
2898 i
> mddev
->max_disks
)) {
2900 "md: %s: %s: only %d devices permitted\n",
2901 mdname(mddev
), bdevname(rdev
->bdev
, b
),
2903 kick_rdev_from_array(rdev
);
2906 if (rdev
!= freshest
)
2907 if (super_types
[mddev
->major_version
].
2908 validate_super(mddev
, rdev
)) {
2909 printk(KERN_WARNING
"md: kicking non-fresh %s"
2911 bdevname(rdev
->bdev
,b
));
2912 kick_rdev_from_array(rdev
);
2915 if (mddev
->level
== LEVEL_MULTIPATH
) {
2916 rdev
->desc_nr
= i
++;
2917 rdev
->raid_disk
= rdev
->desc_nr
;
2918 set_bit(In_sync
, &rdev
->flags
);
2919 } else if (rdev
->raid_disk
>= (mddev
->raid_disks
- min(0, mddev
->delta_disks
))) {
2920 rdev
->raid_disk
= -1;
2921 clear_bit(In_sync
, &rdev
->flags
);
2926 /* Read a fixed-point number.
2927 * Numbers in sysfs attributes should be in "standard" units where
2928 * possible, so time should be in seconds.
2929 * However we internally use a a much smaller unit such as
2930 * milliseconds or jiffies.
2931 * This function takes a decimal number with a possible fractional
2932 * component, and produces an integer which is the result of
2933 * multiplying that number by 10^'scale'.
2934 * all without any floating-point arithmetic.
2936 int strict_strtoul_scaled(const char *cp
, unsigned long *res
, int scale
)
2938 unsigned long result
= 0;
2940 while (isdigit(*cp
) || (*cp
== '.' && decimals
< 0)) {
2943 else if (decimals
< scale
) {
2946 result
= result
* 10 + value
;
2958 while (decimals
< scale
) {
2967 static void md_safemode_timeout(unsigned long data
);
2970 safe_delay_show(mddev_t
*mddev
, char *page
)
2972 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
2973 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
2976 safe_delay_store(mddev_t
*mddev
, const char *cbuf
, size_t len
)
2980 if (strict_strtoul_scaled(cbuf
, &msec
, 3) < 0)
2983 mddev
->safemode_delay
= 0;
2985 unsigned long old_delay
= mddev
->safemode_delay
;
2986 mddev
->safemode_delay
= (msec
*HZ
)/1000;
2987 if (mddev
->safemode_delay
== 0)
2988 mddev
->safemode_delay
= 1;
2989 if (mddev
->safemode_delay
< old_delay
)
2990 md_safemode_timeout((unsigned long)mddev
);
2994 static struct md_sysfs_entry md_safe_delay
=
2995 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
2998 level_show(mddev_t
*mddev
, char *page
)
3000 struct mdk_personality
*p
= mddev
->pers
;
3002 return sprintf(page
, "%s\n", p
->name
);
3003 else if (mddev
->clevel
[0])
3004 return sprintf(page
, "%s\n", mddev
->clevel
);
3005 else if (mddev
->level
!= LEVEL_NONE
)
3006 return sprintf(page
, "%d\n", mddev
->level
);
3012 level_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3016 struct mdk_personality
*pers
;
3021 if (mddev
->pers
== NULL
) {
3024 if (len
>= sizeof(mddev
->clevel
))
3026 strncpy(mddev
->clevel
, buf
, len
);
3027 if (mddev
->clevel
[len
-1] == '\n')
3029 mddev
->clevel
[len
] = 0;
3030 mddev
->level
= LEVEL_NONE
;
3034 /* request to change the personality. Need to ensure:
3035 * - array is not engaged in resync/recovery/reshape
3036 * - old personality can be suspended
3037 * - new personality will access other array.
3040 if (mddev
->sync_thread
||
3041 mddev
->reshape_position
!= MaxSector
||
3042 mddev
->sysfs_active
)
3045 if (!mddev
->pers
->quiesce
) {
3046 printk(KERN_WARNING
"md: %s: %s does not support online personality change\n",
3047 mdname(mddev
), mddev
->pers
->name
);
3051 /* Now find the new personality */
3052 if (len
== 0 || len
>= sizeof(clevel
))
3054 strncpy(clevel
, buf
, len
);
3055 if (clevel
[len
-1] == '\n')
3058 if (strict_strtol(clevel
, 10, &level
))
3061 if (request_module("md-%s", clevel
) != 0)
3062 request_module("md-level-%s", clevel
);
3063 spin_lock(&pers_lock
);
3064 pers
= find_pers(level
, clevel
);
3065 if (!pers
|| !try_module_get(pers
->owner
)) {
3066 spin_unlock(&pers_lock
);
3067 printk(KERN_WARNING
"md: personality %s not loaded\n", clevel
);
3070 spin_unlock(&pers_lock
);
3072 if (pers
== mddev
->pers
) {
3073 /* Nothing to do! */
3074 module_put(pers
->owner
);
3077 if (!pers
->takeover
) {
3078 module_put(pers
->owner
);
3079 printk(KERN_WARNING
"md: %s: %s does not support personality takeover\n",
3080 mdname(mddev
), clevel
);
3084 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
3085 rdev
->new_raid_disk
= rdev
->raid_disk
;
3087 /* ->takeover must set new_* and/or delta_disks
3088 * if it succeeds, and may set them when it fails.
3090 priv
= pers
->takeover(mddev
);
3092 mddev
->new_level
= mddev
->level
;
3093 mddev
->new_layout
= mddev
->layout
;
3094 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3095 mddev
->raid_disks
-= mddev
->delta_disks
;
3096 mddev
->delta_disks
= 0;
3097 module_put(pers
->owner
);
3098 printk(KERN_WARNING
"md: %s: %s would not accept array\n",
3099 mdname(mddev
), clevel
);
3100 return PTR_ERR(priv
);
3103 /* Looks like we have a winner */
3104 mddev_suspend(mddev
);
3105 mddev
->pers
->stop(mddev
);
3107 if (mddev
->pers
->sync_request
== NULL
&&
3108 pers
->sync_request
!= NULL
) {
3109 /* need to add the md_redundancy_group */
3110 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
3112 "md: cannot register extra attributes for %s\n",
3114 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, NULL
, "sync_action");
3116 if (mddev
->pers
->sync_request
!= NULL
&&
3117 pers
->sync_request
== NULL
) {
3118 /* need to remove the md_redundancy_group */
3119 if (mddev
->to_remove
== NULL
)
3120 mddev
->to_remove
= &md_redundancy_group
;
3123 if (mddev
->pers
->sync_request
== NULL
&&
3125 /* We are converting from a no-redundancy array
3126 * to a redundancy array and metadata is managed
3127 * externally so we need to be sure that writes
3128 * won't block due to a need to transition
3130 * until external management is started.
3133 mddev
->safemode_delay
= 0;
3134 mddev
->safemode
= 0;
3137 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
3139 if (rdev
->raid_disk
< 0)
3141 if (rdev
->new_raid_disk
>= mddev
->raid_disks
)
3142 rdev
->new_raid_disk
= -1;
3143 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3145 sprintf(nm
, "rd%d", rdev
->raid_disk
);
3146 sysfs_remove_link(&mddev
->kobj
, nm
);
3148 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
3149 if (rdev
->raid_disk
< 0)
3151 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3153 rdev
->raid_disk
= rdev
->new_raid_disk
;
3154 if (rdev
->raid_disk
< 0)
3155 clear_bit(In_sync
, &rdev
->flags
);
3158 sprintf(nm
, "rd%d", rdev
->raid_disk
);
3159 if(sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
))
3160 printk("md: cannot register %s for %s after level change\n",
3165 module_put(mddev
->pers
->owner
);
3167 mddev
->private = priv
;
3168 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3169 mddev
->level
= mddev
->new_level
;
3170 mddev
->layout
= mddev
->new_layout
;
3171 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
3172 mddev
->delta_disks
= 0;
3173 mddev
->degraded
= 0;
3174 if (mddev
->pers
->sync_request
== NULL
) {
3175 /* this is now an array without redundancy, so
3176 * it must always be in_sync
3179 del_timer_sync(&mddev
->safemode_timer
);
3182 mddev_resume(mddev
);
3183 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
3184 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3185 md_wakeup_thread(mddev
->thread
);
3186 sysfs_notify(&mddev
->kobj
, NULL
, "level");
3187 md_new_event(mddev
);
3191 static struct md_sysfs_entry md_level
=
3192 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
3196 layout_show(mddev_t
*mddev
, char *page
)
3198 /* just a number, not meaningful for all levels */
3199 if (mddev
->reshape_position
!= MaxSector
&&
3200 mddev
->layout
!= mddev
->new_layout
)
3201 return sprintf(page
, "%d (%d)\n",
3202 mddev
->new_layout
, mddev
->layout
);
3203 return sprintf(page
, "%d\n", mddev
->layout
);
3207 layout_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3210 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3212 if (!*buf
|| (*e
&& *e
!= '\n'))
3217 if (mddev
->pers
->check_reshape
== NULL
)
3219 mddev
->new_layout
= n
;
3220 err
= mddev
->pers
->check_reshape(mddev
);
3222 mddev
->new_layout
= mddev
->layout
;
3226 mddev
->new_layout
= n
;
3227 if (mddev
->reshape_position
== MaxSector
)
3232 static struct md_sysfs_entry md_layout
=
3233 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
3237 raid_disks_show(mddev_t
*mddev
, char *page
)
3239 if (mddev
->raid_disks
== 0)
3241 if (mddev
->reshape_position
!= MaxSector
&&
3242 mddev
->delta_disks
!= 0)
3243 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
3244 mddev
->raid_disks
- mddev
->delta_disks
);
3245 return sprintf(page
, "%d\n", mddev
->raid_disks
);
3248 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
);
3251 raid_disks_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3255 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3257 if (!*buf
|| (*e
&& *e
!= '\n'))
3261 rv
= update_raid_disks(mddev
, n
);
3262 else if (mddev
->reshape_position
!= MaxSector
) {
3263 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
3264 mddev
->delta_disks
= n
- olddisks
;
3265 mddev
->raid_disks
= n
;
3267 mddev
->raid_disks
= n
;
3268 return rv
? rv
: len
;
3270 static struct md_sysfs_entry md_raid_disks
=
3271 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
3274 chunk_size_show(mddev_t
*mddev
, char *page
)
3276 if (mddev
->reshape_position
!= MaxSector
&&
3277 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
3278 return sprintf(page
, "%d (%d)\n",
3279 mddev
->new_chunk_sectors
<< 9,
3280 mddev
->chunk_sectors
<< 9);
3281 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
3285 chunk_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3288 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3290 if (!*buf
|| (*e
&& *e
!= '\n'))
3295 if (mddev
->pers
->check_reshape
== NULL
)
3297 mddev
->new_chunk_sectors
= n
>> 9;
3298 err
= mddev
->pers
->check_reshape(mddev
);
3300 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3304 mddev
->new_chunk_sectors
= n
>> 9;
3305 if (mddev
->reshape_position
== MaxSector
)
3306 mddev
->chunk_sectors
= n
>> 9;
3310 static struct md_sysfs_entry md_chunk_size
=
3311 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
3314 resync_start_show(mddev_t
*mddev
, char *page
)
3316 if (mddev
->recovery_cp
== MaxSector
)
3317 return sprintf(page
, "none\n");
3318 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
3322 resync_start_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3325 unsigned long long n
= simple_strtoull(buf
, &e
, 10);
3329 if (cmd_match(buf
, "none"))
3331 else if (!*buf
|| (*e
&& *e
!= '\n'))
3334 mddev
->recovery_cp
= n
;
3337 static struct md_sysfs_entry md_resync_start
=
3338 __ATTR(resync_start
, S_IRUGO
|S_IWUSR
, resync_start_show
, resync_start_store
);
3341 * The array state can be:
3344 * No devices, no size, no level
3345 * Equivalent to STOP_ARRAY ioctl
3347 * May have some settings, but array is not active
3348 * all IO results in error
3349 * When written, doesn't tear down array, but just stops it
3350 * suspended (not supported yet)
3351 * All IO requests will block. The array can be reconfigured.
3352 * Writing this, if accepted, will block until array is quiescent
3354 * no resync can happen. no superblocks get written.
3355 * write requests fail
3357 * like readonly, but behaves like 'clean' on a write request.
3359 * clean - no pending writes, but otherwise active.
3360 * When written to inactive array, starts without resync
3361 * If a write request arrives then
3362 * if metadata is known, mark 'dirty' and switch to 'active'.
3363 * if not known, block and switch to write-pending
3364 * If written to an active array that has pending writes, then fails.
3366 * fully active: IO and resync can be happening.
3367 * When written to inactive array, starts with resync
3370 * clean, but writes are blocked waiting for 'active' to be written.
3373 * like active, but no writes have been seen for a while (100msec).
3376 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
3377 write_pending
, active_idle
, bad_word
};
3378 static char *array_states
[] = {
3379 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3380 "write-pending", "active-idle", NULL
};
3382 static int match_word(const char *word
, char **list
)
3385 for (n
=0; list
[n
]; n
++)
3386 if (cmd_match(word
, list
[n
]))
3392 array_state_show(mddev_t
*mddev
, char *page
)
3394 enum array_state st
= inactive
;
3407 else if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
3409 else if (mddev
->safemode
)
3415 if (list_empty(&mddev
->disks
) &&
3416 mddev
->raid_disks
== 0 &&
3417 mddev
->dev_sectors
== 0)
3422 return sprintf(page
, "%s\n", array_states
[st
]);
3425 static int do_md_stop(mddev_t
* mddev
, int ro
, int is_open
);
3426 static int md_set_readonly(mddev_t
* mddev
, int is_open
);
3427 static int do_md_run(mddev_t
* mddev
);
3428 static int restart_array(mddev_t
*mddev
);
3431 array_state_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3434 enum array_state st
= match_word(buf
, array_states
);
3439 /* stopping an active array */
3440 if (atomic_read(&mddev
->openers
) > 0)
3442 err
= do_md_stop(mddev
, 0, 0);
3445 /* stopping an active array */
3447 if (atomic_read(&mddev
->openers
) > 0)
3449 err
= do_md_stop(mddev
, 2, 0);
3451 err
= 0; /* already inactive */
3454 break; /* not supported yet */
3457 err
= md_set_readonly(mddev
, 0);
3460 set_disk_ro(mddev
->gendisk
, 1);
3461 err
= do_md_run(mddev
);
3467 err
= md_set_readonly(mddev
, 0);
3468 else if (mddev
->ro
== 1)
3469 err
= restart_array(mddev
);
3472 set_disk_ro(mddev
->gendisk
, 0);
3476 err
= do_md_run(mddev
);
3481 restart_array(mddev
);
3482 spin_lock_irq(&mddev
->write_lock
);
3483 if (atomic_read(&mddev
->writes_pending
) == 0) {
3484 if (mddev
->in_sync
== 0) {
3486 if (mddev
->safemode
== 1)
3487 mddev
->safemode
= 0;
3488 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
3493 spin_unlock_irq(&mddev
->write_lock
);
3499 restart_array(mddev
);
3500 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
3501 wake_up(&mddev
->sb_wait
);
3505 set_disk_ro(mddev
->gendisk
, 0);
3506 err
= do_md_run(mddev
);
3511 /* these cannot be set */
3517 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
3521 static struct md_sysfs_entry md_array_state
=
3522 __ATTR(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
3525 max_corrected_read_errors_show(mddev_t
*mddev
, char *page
) {
3526 return sprintf(page
, "%d\n",
3527 atomic_read(&mddev
->max_corr_read_errors
));
3531 max_corrected_read_errors_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3534 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3536 if (*buf
&& (*e
== 0 || *e
== '\n')) {
3537 atomic_set(&mddev
->max_corr_read_errors
, n
);
3543 static struct md_sysfs_entry max_corr_read_errors
=
3544 __ATTR(max_read_errors
, S_IRUGO
|S_IWUSR
, max_corrected_read_errors_show
,
3545 max_corrected_read_errors_store
);
3548 null_show(mddev_t
*mddev
, char *page
)
3554 new_dev_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3556 /* buf must be %d:%d\n? giving major and minor numbers */
3557 /* The new device is added to the array.
3558 * If the array has a persistent superblock, we read the
3559 * superblock to initialise info and check validity.
3560 * Otherwise, only checking done is that in bind_rdev_to_array,
3561 * which mainly checks size.
3564 int major
= simple_strtoul(buf
, &e
, 10);
3570 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
3572 minor
= simple_strtoul(e
+1, &e
, 10);
3573 if (*e
&& *e
!= '\n')
3575 dev
= MKDEV(major
, minor
);
3576 if (major
!= MAJOR(dev
) ||
3577 minor
!= MINOR(dev
))
3581 if (mddev
->persistent
) {
3582 rdev
= md_import_device(dev
, mddev
->major_version
,
3583 mddev
->minor_version
);
3584 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
3585 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
3586 mdk_rdev_t
, same_set
);
3587 err
= super_types
[mddev
->major_version
]
3588 .load_super(rdev
, rdev0
, mddev
->minor_version
);
3592 } else if (mddev
->external
)
3593 rdev
= md_import_device(dev
, -2, -1);
3595 rdev
= md_import_device(dev
, -1, -1);
3598 return PTR_ERR(rdev
);
3599 err
= bind_rdev_to_array(rdev
, mddev
);
3603 return err
? err
: len
;
3606 static struct md_sysfs_entry md_new_device
=
3607 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
3610 bitmap_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3613 unsigned long chunk
, end_chunk
;
3617 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3619 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
3620 if (buf
== end
) break;
3621 if (*end
== '-') { /* range */
3623 end_chunk
= simple_strtoul(buf
, &end
, 0);
3624 if (buf
== end
) break;
3626 if (*end
&& !isspace(*end
)) break;
3627 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
3628 buf
= skip_spaces(end
);
3630 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
3635 static struct md_sysfs_entry md_bitmap
=
3636 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
3639 size_show(mddev_t
*mddev
, char *page
)
3641 return sprintf(page
, "%llu\n",
3642 (unsigned long long)mddev
->dev_sectors
/ 2);
3645 static int update_size(mddev_t
*mddev
, sector_t num_sectors
);
3648 size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3650 /* If array is inactive, we can reduce the component size, but
3651 * not increase it (except from 0).
3652 * If array is active, we can try an on-line resize
3655 int err
= strict_blocks_to_sectors(buf
, §ors
);
3660 err
= update_size(mddev
, sectors
);
3661 md_update_sb(mddev
, 1);
3663 if (mddev
->dev_sectors
== 0 ||
3664 mddev
->dev_sectors
> sectors
)
3665 mddev
->dev_sectors
= sectors
;
3669 return err
? err
: len
;
3672 static struct md_sysfs_entry md_size
=
3673 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
3678 * 'none' for arrays with no metadata (good luck...)
3679 * 'external' for arrays with externally managed metadata,
3680 * or N.M for internally known formats
3683 metadata_show(mddev_t
*mddev
, char *page
)
3685 if (mddev
->persistent
)
3686 return sprintf(page
, "%d.%d\n",
3687 mddev
->major_version
, mddev
->minor_version
);
3688 else if (mddev
->external
)
3689 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
3691 return sprintf(page
, "none\n");
3695 metadata_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3699 /* Changing the details of 'external' metadata is
3700 * always permitted. Otherwise there must be
3701 * no devices attached to the array.
3703 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
3705 else if (!list_empty(&mddev
->disks
))
3708 if (cmd_match(buf
, "none")) {
3709 mddev
->persistent
= 0;
3710 mddev
->external
= 0;
3711 mddev
->major_version
= 0;
3712 mddev
->minor_version
= 90;
3715 if (strncmp(buf
, "external:", 9) == 0) {
3716 size_t namelen
= len
-9;
3717 if (namelen
>= sizeof(mddev
->metadata_type
))
3718 namelen
= sizeof(mddev
->metadata_type
)-1;
3719 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
3720 mddev
->metadata_type
[namelen
] = 0;
3721 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
3722 mddev
->metadata_type
[--namelen
] = 0;
3723 mddev
->persistent
= 0;
3724 mddev
->external
= 1;
3725 mddev
->major_version
= 0;
3726 mddev
->minor_version
= 90;
3729 major
= simple_strtoul(buf
, &e
, 10);
3730 if (e
==buf
|| *e
!= '.')
3733 minor
= simple_strtoul(buf
, &e
, 10);
3734 if (e
==buf
|| (*e
&& *e
!= '\n') )
3736 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
3738 mddev
->major_version
= major
;
3739 mddev
->minor_version
= minor
;
3740 mddev
->persistent
= 1;
3741 mddev
->external
= 0;
3745 static struct md_sysfs_entry md_metadata
=
3746 __ATTR(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
3749 action_show(mddev_t
*mddev
, char *page
)
3751 char *type
= "idle";
3752 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
3754 else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3755 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))) {
3756 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
3758 else if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
3759 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
3761 else if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
3765 } else if (test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
3768 return sprintf(page
, "%s\n", type
);
3771 static void reap_sync_thread(mddev_t
*mddev
);
3774 action_store(mddev_t
*mddev
, const char *page
, size_t len
)
3776 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
3779 if (cmd_match(page
, "frozen"))
3780 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3782 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3784 if (cmd_match(page
, "idle") || cmd_match(page
, "frozen")) {
3785 if (mddev
->sync_thread
) {
3786 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
3787 reap_sync_thread(mddev
);
3789 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3790 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
3792 else if (cmd_match(page
, "resync"))
3793 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3794 else if (cmd_match(page
, "recover")) {
3795 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
3796 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3797 } else if (cmd_match(page
, "reshape")) {
3799 if (mddev
->pers
->start_reshape
== NULL
)
3801 err
= mddev
->pers
->start_reshape(mddev
);
3804 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
3806 if (cmd_match(page
, "check"))
3807 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
3808 else if (!cmd_match(page
, "repair"))
3810 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
3811 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
3813 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3814 md_wakeup_thread(mddev
->thread
);
3815 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
3820 mismatch_cnt_show(mddev_t
*mddev
, char *page
)
3822 return sprintf(page
, "%llu\n",
3823 (unsigned long long) mddev
->resync_mismatches
);
3826 static struct md_sysfs_entry md_scan_mode
=
3827 __ATTR(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
3830 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
3833 sync_min_show(mddev_t
*mddev
, char *page
)
3835 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
3836 mddev
->sync_speed_min
? "local": "system");
3840 sync_min_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3844 if (strncmp(buf
, "system", 6)==0) {
3845 mddev
->sync_speed_min
= 0;
3848 min
= simple_strtoul(buf
, &e
, 10);
3849 if (buf
== e
|| (*e
&& *e
!= '\n') || min
<= 0)
3851 mddev
->sync_speed_min
= min
;
3855 static struct md_sysfs_entry md_sync_min
=
3856 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
3859 sync_max_show(mddev_t
*mddev
, char *page
)
3861 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
3862 mddev
->sync_speed_max
? "local": "system");
3866 sync_max_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3870 if (strncmp(buf
, "system", 6)==0) {
3871 mddev
->sync_speed_max
= 0;
3874 max
= simple_strtoul(buf
, &e
, 10);
3875 if (buf
== e
|| (*e
&& *e
!= '\n') || max
<= 0)
3877 mddev
->sync_speed_max
= max
;
3881 static struct md_sysfs_entry md_sync_max
=
3882 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
3885 degraded_show(mddev_t
*mddev
, char *page
)
3887 return sprintf(page
, "%d\n", mddev
->degraded
);
3889 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
3892 sync_force_parallel_show(mddev_t
*mddev
, char *page
)
3894 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
3898 sync_force_parallel_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3902 if (strict_strtol(buf
, 10, &n
))
3905 if (n
!= 0 && n
!= 1)
3908 mddev
->parallel_resync
= n
;
3910 if (mddev
->sync_thread
)
3911 wake_up(&resync_wait
);
3916 /* force parallel resync, even with shared block devices */
3917 static struct md_sysfs_entry md_sync_force_parallel
=
3918 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
3919 sync_force_parallel_show
, sync_force_parallel_store
);
3922 sync_speed_show(mddev_t
*mddev
, char *page
)
3924 unsigned long resync
, dt
, db
;
3925 if (mddev
->curr_resync
== 0)
3926 return sprintf(page
, "none\n");
3927 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
3928 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
3930 db
= resync
- mddev
->resync_mark_cnt
;
3931 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
3934 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
3937 sync_completed_show(mddev_t
*mddev
, char *page
)
3939 unsigned long long max_sectors
, resync
;
3941 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3942 return sprintf(page
, "none\n");
3944 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
3945 max_sectors
= mddev
->resync_max_sectors
;
3947 max_sectors
= mddev
->dev_sectors
;
3949 resync
= mddev
->curr_resync_completed
;
3950 return sprintf(page
, "%llu / %llu\n", resync
, max_sectors
);
3953 static struct md_sysfs_entry md_sync_completed
= __ATTR_RO(sync_completed
);
3956 min_sync_show(mddev_t
*mddev
, char *page
)
3958 return sprintf(page
, "%llu\n",
3959 (unsigned long long)mddev
->resync_min
);
3962 min_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3964 unsigned long long min
;
3965 if (strict_strtoull(buf
, 10, &min
))
3967 if (min
> mddev
->resync_max
)
3969 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3972 /* Must be a multiple of chunk_size */
3973 if (mddev
->chunk_sectors
) {
3974 sector_t temp
= min
;
3975 if (sector_div(temp
, mddev
->chunk_sectors
))
3978 mddev
->resync_min
= min
;
3983 static struct md_sysfs_entry md_min_sync
=
3984 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
3987 max_sync_show(mddev_t
*mddev
, char *page
)
3989 if (mddev
->resync_max
== MaxSector
)
3990 return sprintf(page
, "max\n");
3992 return sprintf(page
, "%llu\n",
3993 (unsigned long long)mddev
->resync_max
);
3996 max_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3998 if (strncmp(buf
, "max", 3) == 0)
3999 mddev
->resync_max
= MaxSector
;
4001 unsigned long long max
;
4002 if (strict_strtoull(buf
, 10, &max
))
4004 if (max
< mddev
->resync_min
)
4006 if (max
< mddev
->resync_max
&&
4008 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4011 /* Must be a multiple of chunk_size */
4012 if (mddev
->chunk_sectors
) {
4013 sector_t temp
= max
;
4014 if (sector_div(temp
, mddev
->chunk_sectors
))
4017 mddev
->resync_max
= max
;
4019 wake_up(&mddev
->recovery_wait
);
4023 static struct md_sysfs_entry md_max_sync
=
4024 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
4027 suspend_lo_show(mddev_t
*mddev
, char *page
)
4029 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
4033 suspend_lo_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4036 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4037 unsigned long long old
= mddev
->suspend_lo
;
4039 if (mddev
->pers
== NULL
||
4040 mddev
->pers
->quiesce
== NULL
)
4042 if (buf
== e
|| (*e
&& *e
!= '\n'))
4045 mddev
->suspend_lo
= new;
4047 /* Shrinking suspended region */
4048 mddev
->pers
->quiesce(mddev
, 2);
4050 /* Expanding suspended region - need to wait */
4051 mddev
->pers
->quiesce(mddev
, 1);
4052 mddev
->pers
->quiesce(mddev
, 0);
4056 static struct md_sysfs_entry md_suspend_lo
=
4057 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
4061 suspend_hi_show(mddev_t
*mddev
, char *page
)
4063 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
4067 suspend_hi_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4070 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4071 unsigned long long old
= mddev
->suspend_hi
;
4073 if (mddev
->pers
== NULL
||
4074 mddev
->pers
->quiesce
== NULL
)
4076 if (buf
== e
|| (*e
&& *e
!= '\n'))
4079 mddev
->suspend_hi
= new;
4081 /* Shrinking suspended region */
4082 mddev
->pers
->quiesce(mddev
, 2);
4084 /* Expanding suspended region - need to wait */
4085 mddev
->pers
->quiesce(mddev
, 1);
4086 mddev
->pers
->quiesce(mddev
, 0);
4090 static struct md_sysfs_entry md_suspend_hi
=
4091 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
4094 reshape_position_show(mddev_t
*mddev
, char *page
)
4096 if (mddev
->reshape_position
!= MaxSector
)
4097 return sprintf(page
, "%llu\n",
4098 (unsigned long long)mddev
->reshape_position
);
4099 strcpy(page
, "none\n");
4104 reshape_position_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4107 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4110 if (buf
== e
|| (*e
&& *e
!= '\n'))
4112 mddev
->reshape_position
= new;
4113 mddev
->delta_disks
= 0;
4114 mddev
->new_level
= mddev
->level
;
4115 mddev
->new_layout
= mddev
->layout
;
4116 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4120 static struct md_sysfs_entry md_reshape_position
=
4121 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
4122 reshape_position_store
);
4125 array_size_show(mddev_t
*mddev
, char *page
)
4127 if (mddev
->external_size
)
4128 return sprintf(page
, "%llu\n",
4129 (unsigned long long)mddev
->array_sectors
/2);
4131 return sprintf(page
, "default\n");
4135 array_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4139 if (strncmp(buf
, "default", 7) == 0) {
4141 sectors
= mddev
->pers
->size(mddev
, 0, 0);
4143 sectors
= mddev
->array_sectors
;
4145 mddev
->external_size
= 0;
4147 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
4149 if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
4152 mddev
->external_size
= 1;
4155 mddev
->array_sectors
= sectors
;
4157 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4158 revalidate_disk(mddev
->gendisk
);
4163 static struct md_sysfs_entry md_array_size
=
4164 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
4167 static struct attribute
*md_default_attrs
[] = {
4170 &md_raid_disks
.attr
,
4171 &md_chunk_size
.attr
,
4173 &md_resync_start
.attr
,
4175 &md_new_device
.attr
,
4176 &md_safe_delay
.attr
,
4177 &md_array_state
.attr
,
4178 &md_reshape_position
.attr
,
4179 &md_array_size
.attr
,
4180 &max_corr_read_errors
.attr
,
4184 static struct attribute
*md_redundancy_attrs
[] = {
4186 &md_mismatches
.attr
,
4189 &md_sync_speed
.attr
,
4190 &md_sync_force_parallel
.attr
,
4191 &md_sync_completed
.attr
,
4194 &md_suspend_lo
.attr
,
4195 &md_suspend_hi
.attr
,
4200 static struct attribute_group md_redundancy_group
= {
4202 .attrs
= md_redundancy_attrs
,
4207 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
4209 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4210 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
4215 rv
= mddev_lock(mddev
);
4217 rv
= entry
->show(mddev
, page
);
4218 mddev_unlock(mddev
);
4224 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
4225 const char *page
, size_t length
)
4227 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4228 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
4233 if (!capable(CAP_SYS_ADMIN
))
4235 rv
= mddev_lock(mddev
);
4236 if (mddev
->hold_active
== UNTIL_IOCTL
)
4237 mddev
->hold_active
= 0;
4239 rv
= entry
->store(mddev
, page
, length
);
4240 mddev_unlock(mddev
);
4245 static void md_free(struct kobject
*ko
)
4247 mddev_t
*mddev
= container_of(ko
, mddev_t
, kobj
);
4249 if (mddev
->sysfs_state
)
4250 sysfs_put(mddev
->sysfs_state
);
4252 if (mddev
->gendisk
) {
4253 del_gendisk(mddev
->gendisk
);
4254 put_disk(mddev
->gendisk
);
4257 blk_cleanup_queue(mddev
->queue
);
4262 static const struct sysfs_ops md_sysfs_ops
= {
4263 .show
= md_attr_show
,
4264 .store
= md_attr_store
,
4266 static struct kobj_type md_ktype
= {
4268 .sysfs_ops
= &md_sysfs_ops
,
4269 .default_attrs
= md_default_attrs
,
4274 static void mddev_delayed_delete(struct work_struct
*ws
)
4276 mddev_t
*mddev
= container_of(ws
, mddev_t
, del_work
);
4278 sysfs_remove_group(&mddev
->kobj
, &md_bitmap_group
);
4279 kobject_del(&mddev
->kobj
);
4280 kobject_put(&mddev
->kobj
);
4283 static int md_alloc(dev_t dev
, char *name
)
4285 static DEFINE_MUTEX(disks_mutex
);
4286 mddev_t
*mddev
= mddev_find(dev
);
4287 struct gendisk
*disk
;
4296 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
4297 shift
= partitioned
? MdpMinorShift
: 0;
4298 unit
= MINOR(mddev
->unit
) >> shift
;
4300 /* wait for any previous instance of this device to be
4301 * completely removed (mddev_delayed_delete).
4303 flush_workqueue(md_misc_wq
);
4305 mutex_lock(&disks_mutex
);
4311 /* Need to ensure that 'name' is not a duplicate.
4314 spin_lock(&all_mddevs_lock
);
4316 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
4317 if (mddev2
->gendisk
&&
4318 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
4319 spin_unlock(&all_mddevs_lock
);
4322 spin_unlock(&all_mddevs_lock
);
4326 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
4329 mddev
->queue
->queuedata
= mddev
;
4331 blk_queue_make_request(mddev
->queue
, md_make_request
);
4333 disk
= alloc_disk(1 << shift
);
4335 blk_cleanup_queue(mddev
->queue
);
4336 mddev
->queue
= NULL
;
4339 disk
->major
= MAJOR(mddev
->unit
);
4340 disk
->first_minor
= unit
<< shift
;
4342 strcpy(disk
->disk_name
, name
);
4343 else if (partitioned
)
4344 sprintf(disk
->disk_name
, "md_d%d", unit
);
4346 sprintf(disk
->disk_name
, "md%d", unit
);
4347 disk
->fops
= &md_fops
;
4348 disk
->private_data
= mddev
;
4349 disk
->queue
= mddev
->queue
;
4350 /* Allow extended partitions. This makes the
4351 * 'mdp' device redundant, but we can't really
4354 disk
->flags
|= GENHD_FL_EXT_DEVT
;
4356 mddev
->gendisk
= disk
;
4357 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
,
4358 &disk_to_dev(disk
)->kobj
, "%s", "md");
4360 /* This isn't possible, but as kobject_init_and_add is marked
4361 * __must_check, we must do something with the result
4363 printk(KERN_WARNING
"md: cannot register %s/md - name in use\n",
4367 if (mddev
->kobj
.sd
&&
4368 sysfs_create_group(&mddev
->kobj
, &md_bitmap_group
))
4369 printk(KERN_DEBUG
"pointless warning\n");
4371 blk_queue_flush(mddev
->queue
, REQ_FLUSH
| REQ_FUA
);
4373 mutex_unlock(&disks_mutex
);
4374 if (!error
&& mddev
->kobj
.sd
) {
4375 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
4376 mddev
->sysfs_state
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "array_state");
4382 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
4384 md_alloc(dev
, NULL
);
4388 static int add_named_array(const char *val
, struct kernel_param
*kp
)
4390 /* val must be "md_*" where * is not all digits.
4391 * We allocate an array with a large free minor number, and
4392 * set the name to val. val must not already be an active name.
4394 int len
= strlen(val
);
4395 char buf
[DISK_NAME_LEN
];
4397 while (len
&& val
[len
-1] == '\n')
4399 if (len
>= DISK_NAME_LEN
)
4401 strlcpy(buf
, val
, len
+1);
4402 if (strncmp(buf
, "md_", 3) != 0)
4404 return md_alloc(0, buf
);
4407 static void md_safemode_timeout(unsigned long data
)
4409 mddev_t
*mddev
= (mddev_t
*) data
;
4411 if (!atomic_read(&mddev
->writes_pending
)) {
4412 mddev
->safemode
= 1;
4413 if (mddev
->external
)
4414 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4416 md_wakeup_thread(mddev
->thread
);
4419 static int start_dirty_degraded
;
4421 int md_run(mddev_t
*mddev
)
4425 struct mdk_personality
*pers
;
4427 if (list_empty(&mddev
->disks
))
4428 /* cannot run an array with no devices.. */
4433 /* Cannot run until previous stop completes properly */
4434 if (mddev
->sysfs_active
)
4438 * Analyze all RAID superblock(s)
4440 if (!mddev
->raid_disks
) {
4441 if (!mddev
->persistent
)
4446 if (mddev
->level
!= LEVEL_NONE
)
4447 request_module("md-level-%d", mddev
->level
);
4448 else if (mddev
->clevel
[0])
4449 request_module("md-%s", mddev
->clevel
);
4452 * Drop all container device buffers, from now on
4453 * the only valid external interface is through the md
4456 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4457 if (test_bit(Faulty
, &rdev
->flags
))
4459 sync_blockdev(rdev
->bdev
);
4460 invalidate_bdev(rdev
->bdev
);
4462 /* perform some consistency tests on the device.
4463 * We don't want the data to overlap the metadata,
4464 * Internal Bitmap issues have been handled elsewhere.
4466 if (rdev
->meta_bdev
) {
4467 /* Nothing to check */;
4468 } else if (rdev
->data_offset
< rdev
->sb_start
) {
4469 if (mddev
->dev_sectors
&&
4470 rdev
->data_offset
+ mddev
->dev_sectors
4472 printk("md: %s: data overlaps metadata\n",
4477 if (rdev
->sb_start
+ rdev
->sb_size
/512
4478 > rdev
->data_offset
) {
4479 printk("md: %s: metadata overlaps data\n",
4484 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
4487 if (mddev
->bio_set
== NULL
)
4488 mddev
->bio_set
= bioset_create(BIO_POOL_SIZE
, sizeof(mddev
));
4490 spin_lock(&pers_lock
);
4491 pers
= find_pers(mddev
->level
, mddev
->clevel
);
4492 if (!pers
|| !try_module_get(pers
->owner
)) {
4493 spin_unlock(&pers_lock
);
4494 if (mddev
->level
!= LEVEL_NONE
)
4495 printk(KERN_WARNING
"md: personality for level %d is not loaded!\n",
4498 printk(KERN_WARNING
"md: personality for level %s is not loaded!\n",
4503 spin_unlock(&pers_lock
);
4504 if (mddev
->level
!= pers
->level
) {
4505 mddev
->level
= pers
->level
;
4506 mddev
->new_level
= pers
->level
;
4508 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
4510 if (mddev
->reshape_position
!= MaxSector
&&
4511 pers
->start_reshape
== NULL
) {
4512 /* This personality cannot handle reshaping... */
4514 module_put(pers
->owner
);
4518 if (pers
->sync_request
) {
4519 /* Warn if this is a potentially silly
4522 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
4526 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4527 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
4529 rdev
->bdev
->bd_contains
==
4530 rdev2
->bdev
->bd_contains
) {
4532 "%s: WARNING: %s appears to be"
4533 " on the same physical disk as"
4536 bdevname(rdev
->bdev
,b
),
4537 bdevname(rdev2
->bdev
,b2
));
4544 "True protection against single-disk"
4545 " failure might be compromised.\n");
4548 mddev
->recovery
= 0;
4549 /* may be over-ridden by personality */
4550 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
4552 mddev
->ok_start_degraded
= start_dirty_degraded
;
4554 if (start_readonly
&& mddev
->ro
== 0)
4555 mddev
->ro
= 2; /* read-only, but switch on first write */
4557 err
= mddev
->pers
->run(mddev
);
4559 printk(KERN_ERR
"md: pers->run() failed ...\n");
4560 else if (mddev
->pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
4561 WARN_ONCE(!mddev
->external_size
, "%s: default size too small,"
4562 " but 'external_size' not in effect?\n", __func__
);
4564 "md: invalid array_size %llu > default size %llu\n",
4565 (unsigned long long)mddev
->array_sectors
/ 2,
4566 (unsigned long long)mddev
->pers
->size(mddev
, 0, 0) / 2);
4568 mddev
->pers
->stop(mddev
);
4570 if (err
== 0 && mddev
->pers
->sync_request
) {
4571 err
= bitmap_create(mddev
);
4573 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
4574 mdname(mddev
), err
);
4575 mddev
->pers
->stop(mddev
);
4579 module_put(mddev
->pers
->owner
);
4581 bitmap_destroy(mddev
);
4584 if (mddev
->pers
->sync_request
) {
4585 if (mddev
->kobj
.sd
&&
4586 sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
4588 "md: cannot register extra attributes for %s\n",
4590 mddev
->sysfs_action
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "sync_action");
4591 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
4594 atomic_set(&mddev
->writes_pending
,0);
4595 atomic_set(&mddev
->max_corr_read_errors
,
4596 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS
);
4597 mddev
->safemode
= 0;
4598 mddev
->safemode_timer
.function
= md_safemode_timeout
;
4599 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
4600 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
4604 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4605 if (rdev
->raid_disk
>= 0) {
4607 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4608 if (sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
))
4609 /* failure here is OK */;
4612 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4615 md_update_sb(mddev
, 0);
4617 md_wakeup_thread(mddev
->thread
);
4618 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
4620 md_new_event(mddev
);
4621 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4622 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4623 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4626 EXPORT_SYMBOL_GPL(md_run
);
4628 static int do_md_run(mddev_t
*mddev
)
4632 err
= md_run(mddev
);
4635 err
= bitmap_load(mddev
);
4637 bitmap_destroy(mddev
);
4640 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4641 revalidate_disk(mddev
->gendisk
);
4643 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4648 static int restart_array(mddev_t
*mddev
)
4650 struct gendisk
*disk
= mddev
->gendisk
;
4652 /* Complain if it has no devices */
4653 if (list_empty(&mddev
->disks
))
4659 mddev
->safemode
= 0;
4661 set_disk_ro(disk
, 0);
4662 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
4664 /* Kick recovery or resync if necessary */
4665 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4666 md_wakeup_thread(mddev
->thread
);
4667 md_wakeup_thread(mddev
->sync_thread
);
4668 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4672 /* similar to deny_write_access, but accounts for our holding a reference
4673 * to the file ourselves */
4674 static int deny_bitmap_write_access(struct file
* file
)
4676 struct inode
*inode
= file
->f_mapping
->host
;
4678 spin_lock(&inode
->i_lock
);
4679 if (atomic_read(&inode
->i_writecount
) > 1) {
4680 spin_unlock(&inode
->i_lock
);
4683 atomic_set(&inode
->i_writecount
, -1);
4684 spin_unlock(&inode
->i_lock
);
4689 void restore_bitmap_write_access(struct file
*file
)
4691 struct inode
*inode
= file
->f_mapping
->host
;
4693 spin_lock(&inode
->i_lock
);
4694 atomic_set(&inode
->i_writecount
, 1);
4695 spin_unlock(&inode
->i_lock
);
4698 static void md_clean(mddev_t
*mddev
)
4700 mddev
->array_sectors
= 0;
4701 mddev
->external_size
= 0;
4702 mddev
->dev_sectors
= 0;
4703 mddev
->raid_disks
= 0;
4704 mddev
->recovery_cp
= 0;
4705 mddev
->resync_min
= 0;
4706 mddev
->resync_max
= MaxSector
;
4707 mddev
->reshape_position
= MaxSector
;
4708 mddev
->external
= 0;
4709 mddev
->persistent
= 0;
4710 mddev
->level
= LEVEL_NONE
;
4711 mddev
->clevel
[0] = 0;
4714 mddev
->metadata_type
[0] = 0;
4715 mddev
->chunk_sectors
= 0;
4716 mddev
->ctime
= mddev
->utime
= 0;
4718 mddev
->max_disks
= 0;
4720 mddev
->can_decrease_events
= 0;
4721 mddev
->delta_disks
= 0;
4722 mddev
->new_level
= LEVEL_NONE
;
4723 mddev
->new_layout
= 0;
4724 mddev
->new_chunk_sectors
= 0;
4725 mddev
->curr_resync
= 0;
4726 mddev
->resync_mismatches
= 0;
4727 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
4728 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
4729 mddev
->recovery
= 0;
4732 mddev
->degraded
= 0;
4733 mddev
->safemode
= 0;
4734 mddev
->bitmap_info
.offset
= 0;
4735 mddev
->bitmap_info
.default_offset
= 0;
4736 mddev
->bitmap_info
.chunksize
= 0;
4737 mddev
->bitmap_info
.daemon_sleep
= 0;
4738 mddev
->bitmap_info
.max_write_behind
= 0;
4741 static void __md_stop_writes(mddev_t
*mddev
)
4743 if (mddev
->sync_thread
) {
4744 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4745 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4746 reap_sync_thread(mddev
);
4749 del_timer_sync(&mddev
->safemode_timer
);
4751 bitmap_flush(mddev
);
4752 md_super_wait(mddev
);
4754 if (!mddev
->in_sync
|| mddev
->flags
) {
4755 /* mark array as shutdown cleanly */
4757 md_update_sb(mddev
, 1);
4761 void md_stop_writes(mddev_t
*mddev
)
4764 __md_stop_writes(mddev
);
4765 mddev_unlock(mddev
);
4767 EXPORT_SYMBOL_GPL(md_stop_writes
);
4769 void md_stop(mddev_t
*mddev
)
4772 mddev
->pers
->stop(mddev
);
4773 if (mddev
->pers
->sync_request
&& mddev
->to_remove
== NULL
)
4774 mddev
->to_remove
= &md_redundancy_group
;
4775 module_put(mddev
->pers
->owner
);
4777 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4779 EXPORT_SYMBOL_GPL(md_stop
);
4781 static int md_set_readonly(mddev_t
*mddev
, int is_open
)
4784 mutex_lock(&mddev
->open_mutex
);
4785 if (atomic_read(&mddev
->openers
) > is_open
) {
4786 printk("md: %s still in use.\n",mdname(mddev
));
4791 __md_stop_writes(mddev
);
4797 set_disk_ro(mddev
->gendisk
, 1);
4798 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4799 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4803 mutex_unlock(&mddev
->open_mutex
);
4808 * 0 - completely stop and dis-assemble array
4809 * 2 - stop but do not disassemble array
4811 static int do_md_stop(mddev_t
* mddev
, int mode
, int is_open
)
4813 struct gendisk
*disk
= mddev
->gendisk
;
4816 mutex_lock(&mddev
->open_mutex
);
4817 if (atomic_read(&mddev
->openers
) > is_open
||
4818 mddev
->sysfs_active
) {
4819 printk("md: %s still in use.\n",mdname(mddev
));
4820 mutex_unlock(&mddev
->open_mutex
);
4826 set_disk_ro(disk
, 0);
4828 __md_stop_writes(mddev
);
4830 mddev
->queue
->merge_bvec_fn
= NULL
;
4831 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
4833 /* tell userspace to handle 'inactive' */
4834 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4836 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4837 if (rdev
->raid_disk
>= 0) {
4839 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4840 sysfs_remove_link(&mddev
->kobj
, nm
);
4843 set_capacity(disk
, 0);
4844 mutex_unlock(&mddev
->open_mutex
);
4846 revalidate_disk(disk
);
4851 mutex_unlock(&mddev
->open_mutex
);
4853 * Free resources if final stop
4856 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
4858 bitmap_destroy(mddev
);
4859 if (mddev
->bitmap_info
.file
) {
4860 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
4861 fput(mddev
->bitmap_info
.file
);
4862 mddev
->bitmap_info
.file
= NULL
;
4864 mddev
->bitmap_info
.offset
= 0;
4866 export_array(mddev
);
4869 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4870 if (mddev
->hold_active
== UNTIL_STOP
)
4871 mddev
->hold_active
= 0;
4873 blk_integrity_unregister(disk
);
4874 md_new_event(mddev
);
4875 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4880 static void autorun_array(mddev_t
*mddev
)
4885 if (list_empty(&mddev
->disks
))
4888 printk(KERN_INFO
"md: running: ");
4890 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4891 char b
[BDEVNAME_SIZE
];
4892 printk("<%s>", bdevname(rdev
->bdev
,b
));
4896 err
= do_md_run(mddev
);
4898 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
4899 do_md_stop(mddev
, 0, 0);
4904 * lets try to run arrays based on all disks that have arrived
4905 * until now. (those are in pending_raid_disks)
4907 * the method: pick the first pending disk, collect all disks with
4908 * the same UUID, remove all from the pending list and put them into
4909 * the 'same_array' list. Then order this list based on superblock
4910 * update time (freshest comes first), kick out 'old' disks and
4911 * compare superblocks. If everything's fine then run it.
4913 * If "unit" is allocated, then bump its reference count
4915 static void autorun_devices(int part
)
4917 mdk_rdev_t
*rdev0
, *rdev
, *tmp
;
4919 char b
[BDEVNAME_SIZE
];
4921 printk(KERN_INFO
"md: autorun ...\n");
4922 while (!list_empty(&pending_raid_disks
)) {
4925 LIST_HEAD(candidates
);
4926 rdev0
= list_entry(pending_raid_disks
.next
,
4927 mdk_rdev_t
, same_set
);
4929 printk(KERN_INFO
"md: considering %s ...\n",
4930 bdevname(rdev0
->bdev
,b
));
4931 INIT_LIST_HEAD(&candidates
);
4932 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
4933 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
4934 printk(KERN_INFO
"md: adding %s ...\n",
4935 bdevname(rdev
->bdev
,b
));
4936 list_move(&rdev
->same_set
, &candidates
);
4939 * now we have a set of devices, with all of them having
4940 * mostly sane superblocks. It's time to allocate the
4944 dev
= MKDEV(mdp_major
,
4945 rdev0
->preferred_minor
<< MdpMinorShift
);
4946 unit
= MINOR(dev
) >> MdpMinorShift
;
4948 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
4951 if (rdev0
->preferred_minor
!= unit
) {
4952 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
4953 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
4957 md_probe(dev
, NULL
, NULL
);
4958 mddev
= mddev_find(dev
);
4959 if (!mddev
|| !mddev
->gendisk
) {
4963 "md: cannot allocate memory for md drive.\n");
4966 if (mddev_lock(mddev
))
4967 printk(KERN_WARNING
"md: %s locked, cannot run\n",
4969 else if (mddev
->raid_disks
|| mddev
->major_version
4970 || !list_empty(&mddev
->disks
)) {
4972 "md: %s already running, cannot run %s\n",
4973 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
4974 mddev_unlock(mddev
);
4976 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
4977 mddev
->persistent
= 1;
4978 rdev_for_each_list(rdev
, tmp
, &candidates
) {
4979 list_del_init(&rdev
->same_set
);
4980 if (bind_rdev_to_array(rdev
, mddev
))
4983 autorun_array(mddev
);
4984 mddev_unlock(mddev
);
4986 /* on success, candidates will be empty, on error
4989 rdev_for_each_list(rdev
, tmp
, &candidates
) {
4990 list_del_init(&rdev
->same_set
);
4995 printk(KERN_INFO
"md: ... autorun DONE.\n");
4997 #endif /* !MODULE */
4999 static int get_version(void __user
* arg
)
5003 ver
.major
= MD_MAJOR_VERSION
;
5004 ver
.minor
= MD_MINOR_VERSION
;
5005 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
5007 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
5013 static int get_array_info(mddev_t
* mddev
, void __user
* arg
)
5015 mdu_array_info_t info
;
5016 int nr
,working
,insync
,failed
,spare
;
5019 nr
=working
=insync
=failed
=spare
=0;
5020 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5022 if (test_bit(Faulty
, &rdev
->flags
))
5026 if (test_bit(In_sync
, &rdev
->flags
))
5033 info
.major_version
= mddev
->major_version
;
5034 info
.minor_version
= mddev
->minor_version
;
5035 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
5036 info
.ctime
= mddev
->ctime
;
5037 info
.level
= mddev
->level
;
5038 info
.size
= mddev
->dev_sectors
/ 2;
5039 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
5042 info
.raid_disks
= mddev
->raid_disks
;
5043 info
.md_minor
= mddev
->md_minor
;
5044 info
.not_persistent
= !mddev
->persistent
;
5046 info
.utime
= mddev
->utime
;
5049 info
.state
= (1<<MD_SB_CLEAN
);
5050 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5051 info
.state
= (1<<MD_SB_BITMAP_PRESENT
);
5052 info
.active_disks
= insync
;
5053 info
.working_disks
= working
;
5054 info
.failed_disks
= failed
;
5055 info
.spare_disks
= spare
;
5057 info
.layout
= mddev
->layout
;
5058 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
5060 if (copy_to_user(arg
, &info
, sizeof(info
)))
5066 static int get_bitmap_file(mddev_t
* mddev
, void __user
* arg
)
5068 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
5069 char *ptr
, *buf
= NULL
;
5072 if (md_allow_write(mddev
))
5073 file
= kmalloc(sizeof(*file
), GFP_NOIO
);
5075 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
5080 /* bitmap disabled, zero the first byte and copy out */
5081 if (!mddev
->bitmap
|| !mddev
->bitmap
->file
) {
5082 file
->pathname
[0] = '\0';
5086 buf
= kmalloc(sizeof(file
->pathname
), GFP_KERNEL
);
5090 ptr
= d_path(&mddev
->bitmap
->file
->f_path
, buf
, sizeof(file
->pathname
));
5094 strcpy(file
->pathname
, ptr
);
5098 if (copy_to_user(arg
, file
, sizeof(*file
)))
5106 static int get_disk_info(mddev_t
* mddev
, void __user
* arg
)
5108 mdu_disk_info_t info
;
5111 if (copy_from_user(&info
, arg
, sizeof(info
)))
5114 rdev
= find_rdev_nr(mddev
, info
.number
);
5116 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
5117 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
5118 info
.raid_disk
= rdev
->raid_disk
;
5120 if (test_bit(Faulty
, &rdev
->flags
))
5121 info
.state
|= (1<<MD_DISK_FAULTY
);
5122 else if (test_bit(In_sync
, &rdev
->flags
)) {
5123 info
.state
|= (1<<MD_DISK_ACTIVE
);
5124 info
.state
|= (1<<MD_DISK_SYNC
);
5126 if (test_bit(WriteMostly
, &rdev
->flags
))
5127 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
5129 info
.major
= info
.minor
= 0;
5130 info
.raid_disk
= -1;
5131 info
.state
= (1<<MD_DISK_REMOVED
);
5134 if (copy_to_user(arg
, &info
, sizeof(info
)))
5140 static int add_new_disk(mddev_t
* mddev
, mdu_disk_info_t
*info
)
5142 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
5144 dev_t dev
= MKDEV(info
->major
,info
->minor
);
5146 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
5149 if (!mddev
->raid_disks
) {
5151 /* expecting a device which has a superblock */
5152 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
5155 "md: md_import_device returned %ld\n",
5157 return PTR_ERR(rdev
);
5159 if (!list_empty(&mddev
->disks
)) {
5160 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
5161 mdk_rdev_t
, same_set
);
5162 err
= super_types
[mddev
->major_version
]
5163 .load_super(rdev
, rdev0
, mddev
->minor_version
);
5166 "md: %s has different UUID to %s\n",
5167 bdevname(rdev
->bdev
,b
),
5168 bdevname(rdev0
->bdev
,b2
));
5173 err
= bind_rdev_to_array(rdev
, mddev
);
5180 * add_new_disk can be used once the array is assembled
5181 * to add "hot spares". They must already have a superblock
5186 if (!mddev
->pers
->hot_add_disk
) {
5188 "%s: personality does not support diskops!\n",
5192 if (mddev
->persistent
)
5193 rdev
= md_import_device(dev
, mddev
->major_version
,
5194 mddev
->minor_version
);
5196 rdev
= md_import_device(dev
, -1, -1);
5199 "md: md_import_device returned %ld\n",
5201 return PTR_ERR(rdev
);
5203 /* set saved_raid_disk if appropriate */
5204 if (!mddev
->persistent
) {
5205 if (info
->state
& (1<<MD_DISK_SYNC
) &&
5206 info
->raid_disk
< mddev
->raid_disks
) {
5207 rdev
->raid_disk
= info
->raid_disk
;
5208 set_bit(In_sync
, &rdev
->flags
);
5210 rdev
->raid_disk
= -1;
5212 super_types
[mddev
->major_version
].
5213 validate_super(mddev
, rdev
);
5214 if (test_bit(In_sync
, &rdev
->flags
))
5215 rdev
->saved_raid_disk
= rdev
->raid_disk
;
5217 rdev
->saved_raid_disk
= -1;
5219 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
5220 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5221 set_bit(WriteMostly
, &rdev
->flags
);
5223 clear_bit(WriteMostly
, &rdev
->flags
);
5225 rdev
->raid_disk
= -1;
5226 err
= bind_rdev_to_array(rdev
, mddev
);
5227 if (!err
&& !mddev
->pers
->hot_remove_disk
) {
5228 /* If there is hot_add_disk but no hot_remove_disk
5229 * then added disks for geometry changes,
5230 * and should be added immediately.
5232 super_types
[mddev
->major_version
].
5233 validate_super(mddev
, rdev
);
5234 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
5236 unbind_rdev_from_array(rdev
);
5241 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
5243 md_update_sb(mddev
, 1);
5244 if (mddev
->degraded
)
5245 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5246 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5247 md_wakeup_thread(mddev
->thread
);
5251 /* otherwise, add_new_disk is only allowed
5252 * for major_version==0 superblocks
5254 if (mddev
->major_version
!= 0) {
5255 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
5260 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
5262 rdev
= md_import_device(dev
, -1, 0);
5265 "md: error, md_import_device() returned %ld\n",
5267 return PTR_ERR(rdev
);
5269 rdev
->desc_nr
= info
->number
;
5270 if (info
->raid_disk
< mddev
->raid_disks
)
5271 rdev
->raid_disk
= info
->raid_disk
;
5273 rdev
->raid_disk
= -1;
5275 if (rdev
->raid_disk
< mddev
->raid_disks
)
5276 if (info
->state
& (1<<MD_DISK_SYNC
))
5277 set_bit(In_sync
, &rdev
->flags
);
5279 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5280 set_bit(WriteMostly
, &rdev
->flags
);
5282 if (!mddev
->persistent
) {
5283 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
5284 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
5286 rdev
->sb_start
= calc_dev_sboffset(rdev
);
5287 rdev
->sectors
= rdev
->sb_start
;
5289 err
= bind_rdev_to_array(rdev
, mddev
);
5299 static int hot_remove_disk(mddev_t
* mddev
, dev_t dev
)
5301 char b
[BDEVNAME_SIZE
];
5304 rdev
= find_rdev(mddev
, dev
);
5308 if (rdev
->raid_disk
>= 0)
5311 kick_rdev_from_array(rdev
);
5312 md_update_sb(mddev
, 1);
5313 md_new_event(mddev
);
5317 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ...\n",
5318 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5322 static int hot_add_disk(mddev_t
* mddev
, dev_t dev
)
5324 char b
[BDEVNAME_SIZE
];
5331 if (mddev
->major_version
!= 0) {
5332 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
5333 " version-0 superblocks.\n",
5337 if (!mddev
->pers
->hot_add_disk
) {
5339 "%s: personality does not support diskops!\n",
5344 rdev
= md_import_device(dev
, -1, 0);
5347 "md: error, md_import_device() returned %ld\n",
5352 if (mddev
->persistent
)
5353 rdev
->sb_start
= calc_dev_sboffset(rdev
);
5355 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
5357 rdev
->sectors
= rdev
->sb_start
;
5359 if (test_bit(Faulty
, &rdev
->flags
)) {
5361 "md: can not hot-add faulty %s disk to %s!\n",
5362 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5366 clear_bit(In_sync
, &rdev
->flags
);
5368 rdev
->saved_raid_disk
= -1;
5369 err
= bind_rdev_to_array(rdev
, mddev
);
5374 * The rest should better be atomic, we can have disk failures
5375 * noticed in interrupt contexts ...
5378 rdev
->raid_disk
= -1;
5380 md_update_sb(mddev
, 1);
5383 * Kick recovery, maybe this spare has to be added to the
5384 * array immediately.
5386 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5387 md_wakeup_thread(mddev
->thread
);
5388 md_new_event(mddev
);
5396 static int set_bitmap_file(mddev_t
*mddev
, int fd
)
5401 if (!mddev
->pers
->quiesce
)
5403 if (mddev
->recovery
|| mddev
->sync_thread
)
5405 /* we should be able to change the bitmap.. */
5411 return -EEXIST
; /* cannot add when bitmap is present */
5412 mddev
->bitmap_info
.file
= fget(fd
);
5414 if (mddev
->bitmap_info
.file
== NULL
) {
5415 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
5420 err
= deny_bitmap_write_access(mddev
->bitmap_info
.file
);
5422 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
5424 fput(mddev
->bitmap_info
.file
);
5425 mddev
->bitmap_info
.file
= NULL
;
5428 mddev
->bitmap_info
.offset
= 0; /* file overrides offset */
5429 } else if (mddev
->bitmap
== NULL
)
5430 return -ENOENT
; /* cannot remove what isn't there */
5433 mddev
->pers
->quiesce(mddev
, 1);
5435 err
= bitmap_create(mddev
);
5437 err
= bitmap_load(mddev
);
5439 if (fd
< 0 || err
) {
5440 bitmap_destroy(mddev
);
5441 fd
= -1; /* make sure to put the file */
5443 mddev
->pers
->quiesce(mddev
, 0);
5446 if (mddev
->bitmap_info
.file
) {
5447 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
5448 fput(mddev
->bitmap_info
.file
);
5450 mddev
->bitmap_info
.file
= NULL
;
5457 * set_array_info is used two different ways
5458 * The original usage is when creating a new array.
5459 * In this usage, raid_disks is > 0 and it together with
5460 * level, size, not_persistent,layout,chunksize determine the
5461 * shape of the array.
5462 * This will always create an array with a type-0.90.0 superblock.
5463 * The newer usage is when assembling an array.
5464 * In this case raid_disks will be 0, and the major_version field is
5465 * use to determine which style super-blocks are to be found on the devices.
5466 * The minor and patch _version numbers are also kept incase the
5467 * super_block handler wishes to interpret them.
5469 static int set_array_info(mddev_t
* mddev
, mdu_array_info_t
*info
)
5472 if (info
->raid_disks
== 0) {
5473 /* just setting version number for superblock loading */
5474 if (info
->major_version
< 0 ||
5475 info
->major_version
>= ARRAY_SIZE(super_types
) ||
5476 super_types
[info
->major_version
].name
== NULL
) {
5477 /* maybe try to auto-load a module? */
5479 "md: superblock version %d not known\n",
5480 info
->major_version
);
5483 mddev
->major_version
= info
->major_version
;
5484 mddev
->minor_version
= info
->minor_version
;
5485 mddev
->patch_version
= info
->patch_version
;
5486 mddev
->persistent
= !info
->not_persistent
;
5487 /* ensure mddev_put doesn't delete this now that there
5488 * is some minimal configuration.
5490 mddev
->ctime
= get_seconds();
5493 mddev
->major_version
= MD_MAJOR_VERSION
;
5494 mddev
->minor_version
= MD_MINOR_VERSION
;
5495 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
5496 mddev
->ctime
= get_seconds();
5498 mddev
->level
= info
->level
;
5499 mddev
->clevel
[0] = 0;
5500 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
5501 mddev
->raid_disks
= info
->raid_disks
;
5502 /* don't set md_minor, it is determined by which /dev/md* was
5505 if (info
->state
& (1<<MD_SB_CLEAN
))
5506 mddev
->recovery_cp
= MaxSector
;
5508 mddev
->recovery_cp
= 0;
5509 mddev
->persistent
= ! info
->not_persistent
;
5510 mddev
->external
= 0;
5512 mddev
->layout
= info
->layout
;
5513 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
5515 mddev
->max_disks
= MD_SB_DISKS
;
5517 if (mddev
->persistent
)
5519 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5521 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
5522 mddev
->bitmap_info
.offset
= 0;
5524 mddev
->reshape_position
= MaxSector
;
5527 * Generate a 128 bit UUID
5529 get_random_bytes(mddev
->uuid
, 16);
5531 mddev
->new_level
= mddev
->level
;
5532 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
5533 mddev
->new_layout
= mddev
->layout
;
5534 mddev
->delta_disks
= 0;
5539 void md_set_array_sectors(mddev_t
*mddev
, sector_t array_sectors
)
5541 WARN(!mddev_is_locked(mddev
), "%s: unlocked mddev!\n", __func__
);
5543 if (mddev
->external_size
)
5546 mddev
->array_sectors
= array_sectors
;
5548 EXPORT_SYMBOL(md_set_array_sectors
);
5550 static int update_size(mddev_t
*mddev
, sector_t num_sectors
)
5554 int fit
= (num_sectors
== 0);
5556 if (mddev
->pers
->resize
== NULL
)
5558 /* The "num_sectors" is the number of sectors of each device that
5559 * is used. This can only make sense for arrays with redundancy.
5560 * linear and raid0 always use whatever space is available. We can only
5561 * consider changing this number if no resync or reconstruction is
5562 * happening, and if the new size is acceptable. It must fit before the
5563 * sb_start or, if that is <data_offset, it must fit before the size
5564 * of each device. If num_sectors is zero, we find the largest size
5567 if (mddev
->sync_thread
)
5570 /* Sorry, cannot grow a bitmap yet, just remove it,
5574 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5575 sector_t avail
= rdev
->sectors
;
5577 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
5578 num_sectors
= avail
;
5579 if (avail
< num_sectors
)
5582 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
5584 revalidate_disk(mddev
->gendisk
);
5588 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
)
5591 /* change the number of raid disks */
5592 if (mddev
->pers
->check_reshape
== NULL
)
5594 if (raid_disks
<= 0 ||
5595 (mddev
->max_disks
&& raid_disks
>= mddev
->max_disks
))
5597 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
5599 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
5601 rv
= mddev
->pers
->check_reshape(mddev
);
5603 mddev
->delta_disks
= 0;
5609 * update_array_info is used to change the configuration of an
5611 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5612 * fields in the info are checked against the array.
5613 * Any differences that cannot be handled will cause an error.
5614 * Normally, only one change can be managed at a time.
5616 static int update_array_info(mddev_t
*mddev
, mdu_array_info_t
*info
)
5622 /* calculate expected state,ignoring low bits */
5623 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5624 state
|= (1 << MD_SB_BITMAP_PRESENT
);
5626 if (mddev
->major_version
!= info
->major_version
||
5627 mddev
->minor_version
!= info
->minor_version
||
5628 /* mddev->patch_version != info->patch_version || */
5629 mddev
->ctime
!= info
->ctime
||
5630 mddev
->level
!= info
->level
||
5631 /* mddev->layout != info->layout || */
5632 !mddev
->persistent
!= info
->not_persistent
||
5633 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
5634 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5635 ((state
^info
->state
) & 0xfffffe00)
5638 /* Check there is only one change */
5639 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5641 if (mddev
->raid_disks
!= info
->raid_disks
)
5643 if (mddev
->layout
!= info
->layout
)
5645 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
5652 if (mddev
->layout
!= info
->layout
) {
5654 * we don't need to do anything at the md level, the
5655 * personality will take care of it all.
5657 if (mddev
->pers
->check_reshape
== NULL
)
5660 mddev
->new_layout
= info
->layout
;
5661 rv
= mddev
->pers
->check_reshape(mddev
);
5663 mddev
->new_layout
= mddev
->layout
;
5667 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5668 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
5670 if (mddev
->raid_disks
!= info
->raid_disks
)
5671 rv
= update_raid_disks(mddev
, info
->raid_disks
);
5673 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
5674 if (mddev
->pers
->quiesce
== NULL
)
5676 if (mddev
->recovery
|| mddev
->sync_thread
)
5678 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
5679 /* add the bitmap */
5682 if (mddev
->bitmap_info
.default_offset
== 0)
5684 mddev
->bitmap_info
.offset
=
5685 mddev
->bitmap_info
.default_offset
;
5686 mddev
->pers
->quiesce(mddev
, 1);
5687 rv
= bitmap_create(mddev
);
5689 rv
= bitmap_load(mddev
);
5691 bitmap_destroy(mddev
);
5692 mddev
->pers
->quiesce(mddev
, 0);
5694 /* remove the bitmap */
5697 if (mddev
->bitmap
->file
)
5699 mddev
->pers
->quiesce(mddev
, 1);
5700 bitmap_destroy(mddev
);
5701 mddev
->pers
->quiesce(mddev
, 0);
5702 mddev
->bitmap_info
.offset
= 0;
5705 md_update_sb(mddev
, 1);
5709 static int set_disk_faulty(mddev_t
*mddev
, dev_t dev
)
5713 if (mddev
->pers
== NULL
)
5716 rdev
= find_rdev(mddev
, dev
);
5720 md_error(mddev
, rdev
);
5725 * We have a problem here : there is no easy way to give a CHS
5726 * virtual geometry. We currently pretend that we have a 2 heads
5727 * 4 sectors (with a BIG number of cylinders...). This drives
5728 * dosfs just mad... ;-)
5730 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
5732 mddev_t
*mddev
= bdev
->bd_disk
->private_data
;
5736 geo
->cylinders
= mddev
->array_sectors
/ 8;
5740 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
5741 unsigned int cmd
, unsigned long arg
)
5744 void __user
*argp
= (void __user
*)arg
;
5745 mddev_t
*mddev
= NULL
;
5748 if (!capable(CAP_SYS_ADMIN
))
5752 * Commands dealing with the RAID driver but not any
5758 err
= get_version(argp
);
5761 case PRINT_RAID_DEBUG
:
5769 autostart_arrays(arg
);
5776 * Commands creating/starting a new array:
5779 mddev
= bdev
->bd_disk
->private_data
;
5786 err
= mddev_lock(mddev
);
5789 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5796 case SET_ARRAY_INFO
:
5798 mdu_array_info_t info
;
5800 memset(&info
, 0, sizeof(info
));
5801 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
5806 err
= update_array_info(mddev
, &info
);
5808 printk(KERN_WARNING
"md: couldn't update"
5809 " array info. %d\n", err
);
5814 if (!list_empty(&mddev
->disks
)) {
5816 "md: array %s already has disks!\n",
5821 if (mddev
->raid_disks
) {
5823 "md: array %s already initialised!\n",
5828 err
= set_array_info(mddev
, &info
);
5830 printk(KERN_WARNING
"md: couldn't set"
5831 " array info. %d\n", err
);
5841 * Commands querying/configuring an existing array:
5843 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
5844 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
5845 if ((!mddev
->raid_disks
&& !mddev
->external
)
5846 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
5847 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
5848 && cmd
!= GET_BITMAP_FILE
) {
5854 * Commands even a read-only array can execute:
5858 case GET_ARRAY_INFO
:
5859 err
= get_array_info(mddev
, argp
);
5862 case GET_BITMAP_FILE
:
5863 err
= get_bitmap_file(mddev
, argp
);
5867 err
= get_disk_info(mddev
, argp
);
5870 case RESTART_ARRAY_RW
:
5871 err
= restart_array(mddev
);
5875 err
= do_md_stop(mddev
, 0, 1);
5879 err
= md_set_readonly(mddev
, 1);
5883 if (get_user(ro
, (int __user
*)(arg
))) {
5889 /* if the bdev is going readonly the value of mddev->ro
5890 * does not matter, no writes are coming
5895 /* are we are already prepared for writes? */
5899 /* transitioning to readauto need only happen for
5900 * arrays that call md_write_start
5903 err
= restart_array(mddev
);
5906 set_disk_ro(mddev
->gendisk
, 0);
5913 * The remaining ioctls are changing the state of the
5914 * superblock, so we do not allow them on read-only arrays.
5915 * However non-MD ioctls (e.g. get-size) will still come through
5916 * here and hit the 'default' below, so only disallow
5917 * 'md' ioctls, and switch to rw mode if started auto-readonly.
5919 if (_IOC_TYPE(cmd
) == MD_MAJOR
&& mddev
->ro
&& mddev
->pers
) {
5920 if (mddev
->ro
== 2) {
5922 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5923 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5924 md_wakeup_thread(mddev
->thread
);
5935 mdu_disk_info_t info
;
5936 if (copy_from_user(&info
, argp
, sizeof(info
)))
5939 err
= add_new_disk(mddev
, &info
);
5943 case HOT_REMOVE_DISK
:
5944 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
5948 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
5951 case SET_DISK_FAULTY
:
5952 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
5956 err
= do_md_run(mddev
);
5959 case SET_BITMAP_FILE
:
5960 err
= set_bitmap_file(mddev
, (int)arg
);
5970 if (mddev
->hold_active
== UNTIL_IOCTL
&&
5972 mddev
->hold_active
= 0;
5973 mddev_unlock(mddev
);
5982 #ifdef CONFIG_COMPAT
5983 static int md_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
5984 unsigned int cmd
, unsigned long arg
)
5987 case HOT_REMOVE_DISK
:
5989 case SET_DISK_FAULTY
:
5990 case SET_BITMAP_FILE
:
5991 /* These take in integer arg, do not convert */
5994 arg
= (unsigned long)compat_ptr(arg
);
5998 return md_ioctl(bdev
, mode
, cmd
, arg
);
6000 #endif /* CONFIG_COMPAT */
6002 static int md_open(struct block_device
*bdev
, fmode_t mode
)
6005 * Succeed if we can lock the mddev, which confirms that
6006 * it isn't being stopped right now.
6008 mddev_t
*mddev
= mddev_find(bdev
->bd_dev
);
6011 if (mddev
->gendisk
!= bdev
->bd_disk
) {
6012 /* we are racing with mddev_put which is discarding this
6016 /* Wait until bdev->bd_disk is definitely gone */
6017 flush_workqueue(md_misc_wq
);
6018 /* Then retry the open from the top */
6019 return -ERESTARTSYS
;
6021 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
6023 if ((err
= mutex_lock_interruptible(&mddev
->open_mutex
)))
6027 atomic_inc(&mddev
->openers
);
6028 mutex_unlock(&mddev
->open_mutex
);
6030 check_disk_change(bdev
);
6035 static int md_release(struct gendisk
*disk
, fmode_t mode
)
6037 mddev_t
*mddev
= disk
->private_data
;
6040 atomic_dec(&mddev
->openers
);
6046 static int md_media_changed(struct gendisk
*disk
)
6048 mddev_t
*mddev
= disk
->private_data
;
6050 return mddev
->changed
;
6053 static int md_revalidate(struct gendisk
*disk
)
6055 mddev_t
*mddev
= disk
->private_data
;
6060 static const struct block_device_operations md_fops
=
6062 .owner
= THIS_MODULE
,
6064 .release
= md_release
,
6066 #ifdef CONFIG_COMPAT
6067 .compat_ioctl
= md_compat_ioctl
,
6069 .getgeo
= md_getgeo
,
6070 .media_changed
= md_media_changed
,
6071 .revalidate_disk
= md_revalidate
,
6074 static int md_thread(void * arg
)
6076 mdk_thread_t
*thread
= arg
;
6079 * md_thread is a 'system-thread', it's priority should be very
6080 * high. We avoid resource deadlocks individually in each
6081 * raid personality. (RAID5 does preallocation) We also use RR and
6082 * the very same RT priority as kswapd, thus we will never get
6083 * into a priority inversion deadlock.
6085 * we definitely have to have equal or higher priority than
6086 * bdflush, otherwise bdflush will deadlock if there are too
6087 * many dirty RAID5 blocks.
6090 allow_signal(SIGKILL
);
6091 while (!kthread_should_stop()) {
6093 /* We need to wait INTERRUPTIBLE so that
6094 * we don't add to the load-average.
6095 * That means we need to be sure no signals are
6098 if (signal_pending(current
))
6099 flush_signals(current
);
6101 wait_event_interruptible_timeout
6103 test_bit(THREAD_WAKEUP
, &thread
->flags
)
6104 || kthread_should_stop(),
6107 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
6108 if (!kthread_should_stop())
6109 thread
->run(thread
->mddev
);
6115 void md_wakeup_thread(mdk_thread_t
*thread
)
6118 dprintk("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
6119 set_bit(THREAD_WAKEUP
, &thread
->flags
);
6120 wake_up(&thread
->wqueue
);
6124 mdk_thread_t
*md_register_thread(void (*run
) (mddev_t
*), mddev_t
*mddev
,
6127 mdk_thread_t
*thread
;
6129 thread
= kzalloc(sizeof(mdk_thread_t
), GFP_KERNEL
);
6133 init_waitqueue_head(&thread
->wqueue
);
6136 thread
->mddev
= mddev
;
6137 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
6138 thread
->tsk
= kthread_run(md_thread
, thread
,
6140 mdname(thread
->mddev
),
6141 name
?: mddev
->pers
->name
);
6142 if (IS_ERR(thread
->tsk
)) {
6149 void md_unregister_thread(mdk_thread_t
*thread
)
6153 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
6155 kthread_stop(thread
->tsk
);
6159 void md_error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
6166 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
6169 if (mddev
->external
)
6170 set_bit(Blocked
, &rdev
->flags
);
6172 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6174 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6175 __builtin_return_address(0),__builtin_return_address(1),
6176 __builtin_return_address(2),__builtin_return_address(3));
6180 if (!mddev
->pers
->error_handler
)
6182 mddev
->pers
->error_handler(mddev
,rdev
);
6183 if (mddev
->degraded
)
6184 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6185 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
6186 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6187 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6188 md_wakeup_thread(mddev
->thread
);
6189 if (mddev
->event_work
.func
)
6190 queue_work(md_misc_wq
, &mddev
->event_work
);
6191 md_new_event_inintr(mddev
);
6194 /* seq_file implementation /proc/mdstat */
6196 static void status_unused(struct seq_file
*seq
)
6201 seq_printf(seq
, "unused devices: ");
6203 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
6204 char b
[BDEVNAME_SIZE
];
6206 seq_printf(seq
, "%s ",
6207 bdevname(rdev
->bdev
,b
));
6210 seq_printf(seq
, "<none>");
6212 seq_printf(seq
, "\n");
6216 static void status_resync(struct seq_file
*seq
, mddev_t
* mddev
)
6218 sector_t max_sectors
, resync
, res
;
6219 unsigned long dt
, db
;
6222 unsigned int per_milli
;
6224 resync
= mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
);
6226 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
6227 max_sectors
= mddev
->resync_max_sectors
;
6229 max_sectors
= mddev
->dev_sectors
;
6232 * Should not happen.
6238 /* Pick 'scale' such that (resync>>scale)*1000 will fit
6239 * in a sector_t, and (max_sectors>>scale) will fit in a
6240 * u32, as those are the requirements for sector_div.
6241 * Thus 'scale' must be at least 10
6244 if (sizeof(sector_t
) > sizeof(unsigned long)) {
6245 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
6248 res
= (resync
>>scale
)*1000;
6249 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
6253 int i
, x
= per_milli
/50, y
= 20-x
;
6254 seq_printf(seq
, "[");
6255 for (i
= 0; i
< x
; i
++)
6256 seq_printf(seq
, "=");
6257 seq_printf(seq
, ">");
6258 for (i
= 0; i
< y
; i
++)
6259 seq_printf(seq
, ".");
6260 seq_printf(seq
, "] ");
6262 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
6263 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
6265 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
6267 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
6268 "resync" : "recovery"))),
6269 per_milli
/10, per_milli
% 10,
6270 (unsigned long long) resync
/2,
6271 (unsigned long long) max_sectors
/2);
6274 * dt: time from mark until now
6275 * db: blocks written from mark until now
6276 * rt: remaining time
6278 * rt is a sector_t, so could be 32bit or 64bit.
6279 * So we divide before multiply in case it is 32bit and close
6281 * We scale the divisor (db) by 32 to avoid losing precision
6282 * near the end of resync when the number of remaining sectors
6284 * We then divide rt by 32 after multiplying by db to compensate.
6285 * The '+1' avoids division by zero if db is very small.
6287 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
6289 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
6290 - mddev
->resync_mark_cnt
;
6292 rt
= max_sectors
- resync
; /* number of remaining sectors */
6293 sector_div(rt
, db
/32+1);
6297 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
6298 ((unsigned long)rt
% 60)/6);
6300 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
6303 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
6305 struct list_head
*tmp
;
6315 spin_lock(&all_mddevs_lock
);
6316 list_for_each(tmp
,&all_mddevs
)
6318 mddev
= list_entry(tmp
, mddev_t
, all_mddevs
);
6320 spin_unlock(&all_mddevs_lock
);
6323 spin_unlock(&all_mddevs_lock
);
6325 return (void*)2;/* tail */
6329 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
6331 struct list_head
*tmp
;
6332 mddev_t
*next_mddev
, *mddev
= v
;
6338 spin_lock(&all_mddevs_lock
);
6340 tmp
= all_mddevs
.next
;
6342 tmp
= mddev
->all_mddevs
.next
;
6343 if (tmp
!= &all_mddevs
)
6344 next_mddev
= mddev_get(list_entry(tmp
,mddev_t
,all_mddevs
));
6346 next_mddev
= (void*)2;
6349 spin_unlock(&all_mddevs_lock
);
6357 static void md_seq_stop(struct seq_file
*seq
, void *v
)
6361 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
6365 struct mdstat_info
{
6369 static int md_seq_show(struct seq_file
*seq
, void *v
)
6374 struct mdstat_info
*mi
= seq
->private;
6375 struct bitmap
*bitmap
;
6377 if (v
== (void*)1) {
6378 struct mdk_personality
*pers
;
6379 seq_printf(seq
, "Personalities : ");
6380 spin_lock(&pers_lock
);
6381 list_for_each_entry(pers
, &pers_list
, list
)
6382 seq_printf(seq
, "[%s] ", pers
->name
);
6384 spin_unlock(&pers_lock
);
6385 seq_printf(seq
, "\n");
6386 mi
->event
= atomic_read(&md_event_count
);
6389 if (v
== (void*)2) {
6394 if (mddev_lock(mddev
) < 0)
6397 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
6398 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
6399 mddev
->pers
? "" : "in");
6402 seq_printf(seq
, " (read-only)");
6404 seq_printf(seq
, " (auto-read-only)");
6405 seq_printf(seq
, " %s", mddev
->pers
->name
);
6409 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
6410 char b
[BDEVNAME_SIZE
];
6411 seq_printf(seq
, " %s[%d]",
6412 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
6413 if (test_bit(WriteMostly
, &rdev
->flags
))
6414 seq_printf(seq
, "(W)");
6415 if (test_bit(Faulty
, &rdev
->flags
)) {
6416 seq_printf(seq
, "(F)");
6418 } else if (rdev
->raid_disk
< 0)
6419 seq_printf(seq
, "(S)"); /* spare */
6420 sectors
+= rdev
->sectors
;
6423 if (!list_empty(&mddev
->disks
)) {
6425 seq_printf(seq
, "\n %llu blocks",
6426 (unsigned long long)
6427 mddev
->array_sectors
/ 2);
6429 seq_printf(seq
, "\n %llu blocks",
6430 (unsigned long long)sectors
/ 2);
6432 if (mddev
->persistent
) {
6433 if (mddev
->major_version
!= 0 ||
6434 mddev
->minor_version
!= 90) {
6435 seq_printf(seq
," super %d.%d",
6436 mddev
->major_version
,
6437 mddev
->minor_version
);
6439 } else if (mddev
->external
)
6440 seq_printf(seq
, " super external:%s",
6441 mddev
->metadata_type
);
6443 seq_printf(seq
, " super non-persistent");
6446 mddev
->pers
->status(seq
, mddev
);
6447 seq_printf(seq
, "\n ");
6448 if (mddev
->pers
->sync_request
) {
6449 if (mddev
->curr_resync
> 2) {
6450 status_resync(seq
, mddev
);
6451 seq_printf(seq
, "\n ");
6452 } else if (mddev
->curr_resync
== 1 || mddev
->curr_resync
== 2)
6453 seq_printf(seq
, "\tresync=DELAYED\n ");
6454 else if (mddev
->recovery_cp
< MaxSector
)
6455 seq_printf(seq
, "\tresync=PENDING\n ");
6458 seq_printf(seq
, "\n ");
6460 if ((bitmap
= mddev
->bitmap
)) {
6461 unsigned long chunk_kb
;
6462 unsigned long flags
;
6463 spin_lock_irqsave(&bitmap
->lock
, flags
);
6464 chunk_kb
= mddev
->bitmap_info
.chunksize
>> 10;
6465 seq_printf(seq
, "bitmap: %lu/%lu pages [%luKB], "
6467 bitmap
->pages
- bitmap
->missing_pages
,
6469 (bitmap
->pages
- bitmap
->missing_pages
)
6470 << (PAGE_SHIFT
- 10),
6471 chunk_kb
? chunk_kb
: mddev
->bitmap_info
.chunksize
,
6472 chunk_kb
? "KB" : "B");
6474 seq_printf(seq
, ", file: ");
6475 seq_path(seq
, &bitmap
->file
->f_path
, " \t\n");
6478 seq_printf(seq
, "\n");
6479 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
6482 seq_printf(seq
, "\n");
6484 mddev_unlock(mddev
);
6489 static const struct seq_operations md_seq_ops
= {
6490 .start
= md_seq_start
,
6491 .next
= md_seq_next
,
6492 .stop
= md_seq_stop
,
6493 .show
= md_seq_show
,
6496 static int md_seq_open(struct inode
*inode
, struct file
*file
)
6499 struct mdstat_info
*mi
= kmalloc(sizeof(*mi
), GFP_KERNEL
);
6503 error
= seq_open(file
, &md_seq_ops
);
6507 struct seq_file
*p
= file
->private_data
;
6509 mi
->event
= atomic_read(&md_event_count
);
6514 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
6516 struct seq_file
*m
= filp
->private_data
;
6517 struct mdstat_info
*mi
= m
->private;
6520 poll_wait(filp
, &md_event_waiters
, wait
);
6522 /* always allow read */
6523 mask
= POLLIN
| POLLRDNORM
;
6525 if (mi
->event
!= atomic_read(&md_event_count
))
6526 mask
|= POLLERR
| POLLPRI
;
6530 static const struct file_operations md_seq_fops
= {
6531 .owner
= THIS_MODULE
,
6532 .open
= md_seq_open
,
6534 .llseek
= seq_lseek
,
6535 .release
= seq_release_private
,
6536 .poll
= mdstat_poll
,
6539 int register_md_personality(struct mdk_personality
*p
)
6541 spin_lock(&pers_lock
);
6542 list_add_tail(&p
->list
, &pers_list
);
6543 printk(KERN_INFO
"md: %s personality registered for level %d\n", p
->name
, p
->level
);
6544 spin_unlock(&pers_lock
);
6548 int unregister_md_personality(struct mdk_personality
*p
)
6550 printk(KERN_INFO
"md: %s personality unregistered\n", p
->name
);
6551 spin_lock(&pers_lock
);
6552 list_del_init(&p
->list
);
6553 spin_unlock(&pers_lock
);
6557 static int is_mddev_idle(mddev_t
*mddev
, int init
)
6565 rdev_for_each_rcu(rdev
, mddev
) {
6566 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
6567 curr_events
= (int)part_stat_read(&disk
->part0
, sectors
[0]) +
6568 (int)part_stat_read(&disk
->part0
, sectors
[1]) -
6569 atomic_read(&disk
->sync_io
);
6570 /* sync IO will cause sync_io to increase before the disk_stats
6571 * as sync_io is counted when a request starts, and
6572 * disk_stats is counted when it completes.
6573 * So resync activity will cause curr_events to be smaller than
6574 * when there was no such activity.
6575 * non-sync IO will cause disk_stat to increase without
6576 * increasing sync_io so curr_events will (eventually)
6577 * be larger than it was before. Once it becomes
6578 * substantially larger, the test below will cause
6579 * the array to appear non-idle, and resync will slow
6581 * If there is a lot of outstanding resync activity when
6582 * we set last_event to curr_events, then all that activity
6583 * completing might cause the array to appear non-idle
6584 * and resync will be slowed down even though there might
6585 * not have been non-resync activity. This will only
6586 * happen once though. 'last_events' will soon reflect
6587 * the state where there is little or no outstanding
6588 * resync requests, and further resync activity will
6589 * always make curr_events less than last_events.
6592 if (init
|| curr_events
- rdev
->last_events
> 64) {
6593 rdev
->last_events
= curr_events
;
6601 void md_done_sync(mddev_t
*mddev
, int blocks
, int ok
)
6603 /* another "blocks" (512byte) blocks have been synced */
6604 atomic_sub(blocks
, &mddev
->recovery_active
);
6605 wake_up(&mddev
->recovery_wait
);
6607 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6608 md_wakeup_thread(mddev
->thread
);
6609 // stop recovery, signal do_sync ....
6614 /* md_write_start(mddev, bi)
6615 * If we need to update some array metadata (e.g. 'active' flag
6616 * in superblock) before writing, schedule a superblock update
6617 * and wait for it to complete.
6619 void md_write_start(mddev_t
*mddev
, struct bio
*bi
)
6622 if (bio_data_dir(bi
) != WRITE
)
6625 BUG_ON(mddev
->ro
== 1);
6626 if (mddev
->ro
== 2) {
6627 /* need to switch to read/write */
6629 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6630 md_wakeup_thread(mddev
->thread
);
6631 md_wakeup_thread(mddev
->sync_thread
);
6634 atomic_inc(&mddev
->writes_pending
);
6635 if (mddev
->safemode
== 1)
6636 mddev
->safemode
= 0;
6637 if (mddev
->in_sync
) {
6638 spin_lock_irq(&mddev
->write_lock
);
6639 if (mddev
->in_sync
) {
6641 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6642 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
6643 md_wakeup_thread(mddev
->thread
);
6646 spin_unlock_irq(&mddev
->write_lock
);
6649 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6650 wait_event(mddev
->sb_wait
,
6651 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
6654 void md_write_end(mddev_t
*mddev
)
6656 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
6657 if (mddev
->safemode
== 2)
6658 md_wakeup_thread(mddev
->thread
);
6659 else if (mddev
->safemode_delay
)
6660 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
6664 /* md_allow_write(mddev)
6665 * Calling this ensures that the array is marked 'active' so that writes
6666 * may proceed without blocking. It is important to call this before
6667 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6668 * Must be called with mddev_lock held.
6670 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6671 * is dropped, so return -EAGAIN after notifying userspace.
6673 int md_allow_write(mddev_t
*mddev
)
6679 if (!mddev
->pers
->sync_request
)
6682 spin_lock_irq(&mddev
->write_lock
);
6683 if (mddev
->in_sync
) {
6685 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6686 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
6687 if (mddev
->safemode_delay
&&
6688 mddev
->safemode
== 0)
6689 mddev
->safemode
= 1;
6690 spin_unlock_irq(&mddev
->write_lock
);
6691 md_update_sb(mddev
, 0);
6692 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6694 spin_unlock_irq(&mddev
->write_lock
);
6696 if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
6701 EXPORT_SYMBOL_GPL(md_allow_write
);
6703 #define SYNC_MARKS 10
6704 #define SYNC_MARK_STEP (3*HZ)
6705 void md_do_sync(mddev_t
*mddev
)
6708 unsigned int currspeed
= 0,
6710 sector_t max_sectors
,j
, io_sectors
;
6711 unsigned long mark
[SYNC_MARKS
];
6712 sector_t mark_cnt
[SYNC_MARKS
];
6714 struct list_head
*tmp
;
6715 sector_t last_check
;
6720 /* just incase thread restarts... */
6721 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
6723 if (mddev
->ro
) /* never try to sync a read-only array */
6726 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6727 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
6728 desc
= "data-check";
6729 else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6730 desc
= "requested-resync";
6733 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
6738 /* we overload curr_resync somewhat here.
6739 * 0 == not engaged in resync at all
6740 * 2 == checking that there is no conflict with another sync
6741 * 1 == like 2, but have yielded to allow conflicting resync to
6743 * other == active in resync - this many blocks
6745 * Before starting a resync we must have set curr_resync to
6746 * 2, and then checked that every "conflicting" array has curr_resync
6747 * less than ours. When we find one that is the same or higher
6748 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6749 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6750 * This will mean we have to start checking from the beginning again.
6755 mddev
->curr_resync
= 2;
6758 if (kthread_should_stop())
6759 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6761 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6763 for_each_mddev(mddev2
, tmp
) {
6764 if (mddev2
== mddev
)
6766 if (!mddev
->parallel_resync
6767 && mddev2
->curr_resync
6768 && match_mddev_units(mddev
, mddev2
)) {
6770 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
6771 /* arbitrarily yield */
6772 mddev
->curr_resync
= 1;
6773 wake_up(&resync_wait
);
6775 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
6776 /* no need to wait here, we can wait the next
6777 * time 'round when curr_resync == 2
6780 /* We need to wait 'interruptible' so as not to
6781 * contribute to the load average, and not to
6782 * be caught by 'softlockup'
6784 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
6785 if (!kthread_should_stop() &&
6786 mddev2
->curr_resync
>= mddev
->curr_resync
) {
6787 printk(KERN_INFO
"md: delaying %s of %s"
6788 " until %s has finished (they"
6789 " share one or more physical units)\n",
6790 desc
, mdname(mddev
), mdname(mddev2
));
6792 if (signal_pending(current
))
6793 flush_signals(current
);
6795 finish_wait(&resync_wait
, &wq
);
6798 finish_wait(&resync_wait
, &wq
);
6801 } while (mddev
->curr_resync
< 2);
6804 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6805 /* resync follows the size requested by the personality,
6806 * which defaults to physical size, but can be virtual size
6808 max_sectors
= mddev
->resync_max_sectors
;
6809 mddev
->resync_mismatches
= 0;
6810 /* we don't use the checkpoint if there's a bitmap */
6811 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6812 j
= mddev
->resync_min
;
6813 else if (!mddev
->bitmap
)
6814 j
= mddev
->recovery_cp
;
6816 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
6817 max_sectors
= mddev
->dev_sectors
;
6819 /* recovery follows the physical size of devices */
6820 max_sectors
= mddev
->dev_sectors
;
6823 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
6824 if (rdev
->raid_disk
>= 0 &&
6825 !test_bit(Faulty
, &rdev
->flags
) &&
6826 !test_bit(In_sync
, &rdev
->flags
) &&
6827 rdev
->recovery_offset
< j
)
6828 j
= rdev
->recovery_offset
;
6832 printk(KERN_INFO
"md: %s of RAID array %s\n", desc
, mdname(mddev
));
6833 printk(KERN_INFO
"md: minimum _guaranteed_ speed:"
6834 " %d KB/sec/disk.\n", speed_min(mddev
));
6835 printk(KERN_INFO
"md: using maximum available idle IO bandwidth "
6836 "(but not more than %d KB/sec) for %s.\n",
6837 speed_max(mddev
), desc
);
6839 is_mddev_idle(mddev
, 1); /* this initializes IO event counters */
6842 for (m
= 0; m
< SYNC_MARKS
; m
++) {
6844 mark_cnt
[m
] = io_sectors
;
6847 mddev
->resync_mark
= mark
[last_mark
];
6848 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
6851 * Tune reconstruction:
6853 window
= 32*(PAGE_SIZE
/512);
6854 printk(KERN_INFO
"md: using %dk window, over a total of %llu blocks.\n",
6855 window
/2,(unsigned long long) max_sectors
/2);
6857 atomic_set(&mddev
->recovery_active
, 0);
6862 "md: resuming %s of %s from checkpoint.\n",
6863 desc
, mdname(mddev
));
6864 mddev
->curr_resync
= j
;
6866 mddev
->curr_resync_completed
= j
;
6868 while (j
< max_sectors
) {
6873 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
6874 ((mddev
->curr_resync
> mddev
->curr_resync_completed
&&
6875 (mddev
->curr_resync
- mddev
->curr_resync_completed
)
6876 > (max_sectors
>> 4)) ||
6877 (j
- mddev
->curr_resync_completed
)*2
6878 >= mddev
->resync_max
- mddev
->curr_resync_completed
6880 /* time to update curr_resync_completed */
6881 wait_event(mddev
->recovery_wait
,
6882 atomic_read(&mddev
->recovery_active
) == 0);
6883 mddev
->curr_resync_completed
= j
;
6884 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6885 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
6888 while (j
>= mddev
->resync_max
&& !kthread_should_stop()) {
6889 /* As this condition is controlled by user-space,
6890 * we can block indefinitely, so use '_interruptible'
6891 * to avoid triggering warnings.
6893 flush_signals(current
); /* just in case */
6894 wait_event_interruptible(mddev
->recovery_wait
,
6895 mddev
->resync_max
> j
6896 || kthread_should_stop());
6899 if (kthread_should_stop())
6902 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
,
6903 currspeed
< speed_min(mddev
));
6905 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6909 if (!skipped
) { /* actual IO requested */
6910 io_sectors
+= sectors
;
6911 atomic_add(sectors
, &mddev
->recovery_active
);
6915 if (j
>1) mddev
->curr_resync
= j
;
6916 mddev
->curr_mark_cnt
= io_sectors
;
6917 if (last_check
== 0)
6918 /* this is the earliers that rebuilt will be
6919 * visible in /proc/mdstat
6921 md_new_event(mddev
);
6923 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
6926 last_check
= io_sectors
;
6928 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6932 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
6934 int next
= (last_mark
+1) % SYNC_MARKS
;
6936 mddev
->resync_mark
= mark
[next
];
6937 mddev
->resync_mark_cnt
= mark_cnt
[next
];
6938 mark
[next
] = jiffies
;
6939 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
6944 if (kthread_should_stop())
6949 * this loop exits only if either when we are slower than
6950 * the 'hard' speed limit, or the system was IO-idle for
6952 * the system might be non-idle CPU-wise, but we only care
6953 * about not overloading the IO subsystem. (things like an
6954 * e2fsck being done on the RAID array should execute fast)
6958 currspeed
= ((unsigned long)(io_sectors
-mddev
->resync_mark_cnt
))/2
6959 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
6961 if (currspeed
> speed_min(mddev
)) {
6962 if ((currspeed
> speed_max(mddev
)) ||
6963 !is_mddev_idle(mddev
, 0)) {
6969 printk(KERN_INFO
"md: %s: %s done.\n",mdname(mddev
), desc
);
6971 * this also signals 'finished resyncing' to md_stop
6974 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
6976 /* tell personality that we are finished */
6977 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
, 1);
6979 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
6980 mddev
->curr_resync
> 2) {
6981 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6982 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
6983 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
6985 "md: checkpointing %s of %s.\n",
6986 desc
, mdname(mddev
));
6987 mddev
->recovery_cp
= mddev
->curr_resync
;
6990 mddev
->recovery_cp
= MaxSector
;
6992 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6993 mddev
->curr_resync
= MaxSector
;
6995 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
6996 if (rdev
->raid_disk
>= 0 &&
6997 mddev
->delta_disks
>= 0 &&
6998 !test_bit(Faulty
, &rdev
->flags
) &&
6999 !test_bit(In_sync
, &rdev
->flags
) &&
7000 rdev
->recovery_offset
< mddev
->curr_resync
)
7001 rdev
->recovery_offset
= mddev
->curr_resync
;
7005 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
7008 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
7009 /* We completed so min/max setting can be forgotten if used. */
7010 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7011 mddev
->resync_min
= 0;
7012 mddev
->resync_max
= MaxSector
;
7013 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7014 mddev
->resync_min
= mddev
->curr_resync_completed
;
7015 mddev
->curr_resync
= 0;
7016 wake_up(&resync_wait
);
7017 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
7018 md_wakeup_thread(mddev
->thread
);
7023 * got a signal, exit.
7026 "md: md_do_sync() got signal ... exiting\n");
7027 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7031 EXPORT_SYMBOL_GPL(md_do_sync
);
7034 static int remove_and_add_spares(mddev_t
*mddev
)
7039 mddev
->curr_resync_completed
= 0;
7041 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7042 if (rdev
->raid_disk
>= 0 &&
7043 !test_bit(Blocked
, &rdev
->flags
) &&
7044 (test_bit(Faulty
, &rdev
->flags
) ||
7045 ! test_bit(In_sync
, &rdev
->flags
)) &&
7046 atomic_read(&rdev
->nr_pending
)==0) {
7047 if (mddev
->pers
->hot_remove_disk(
7048 mddev
, rdev
->raid_disk
)==0) {
7050 sprintf(nm
,"rd%d", rdev
->raid_disk
);
7051 sysfs_remove_link(&mddev
->kobj
, nm
);
7052 rdev
->raid_disk
= -1;
7056 if (mddev
->degraded
&& !mddev
->recovery_disabled
) {
7057 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
7058 if (rdev
->raid_disk
>= 0 &&
7059 !test_bit(In_sync
, &rdev
->flags
) &&
7060 !test_bit(Blocked
, &rdev
->flags
))
7062 if (rdev
->raid_disk
< 0
7063 && !test_bit(Faulty
, &rdev
->flags
)) {
7064 rdev
->recovery_offset
= 0;
7066 hot_add_disk(mddev
, rdev
) == 0) {
7068 sprintf(nm
, "rd%d", rdev
->raid_disk
);
7069 if (sysfs_create_link(&mddev
->kobj
,
7071 /* failure here is OK */;
7073 md_new_event(mddev
);
7074 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
7083 static void reap_sync_thread(mddev_t
*mddev
)
7087 /* resync has finished, collect result */
7088 md_unregister_thread(mddev
->sync_thread
);
7089 mddev
->sync_thread
= NULL
;
7090 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
7091 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
7093 /* activate any spares */
7094 if (mddev
->pers
->spare_active(mddev
))
7095 sysfs_notify(&mddev
->kobj
, NULL
,
7098 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
7099 mddev
->pers
->finish_reshape
)
7100 mddev
->pers
->finish_reshape(mddev
);
7101 md_update_sb(mddev
, 1);
7103 /* if array is no-longer degraded, then any saved_raid_disk
7104 * information must be scrapped
7106 if (!mddev
->degraded
)
7107 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7108 rdev
->saved_raid_disk
= -1;
7110 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7111 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7112 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7113 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7114 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7115 /* flag recovery needed just to double check */
7116 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7117 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7118 md_new_event(mddev
);
7122 * This routine is regularly called by all per-raid-array threads to
7123 * deal with generic issues like resync and super-block update.
7124 * Raid personalities that don't have a thread (linear/raid0) do not
7125 * need this as they never do any recovery or update the superblock.
7127 * It does not do any resync itself, but rather "forks" off other threads
7128 * to do that as needed.
7129 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7130 * "->recovery" and create a thread at ->sync_thread.
7131 * When the thread finishes it sets MD_RECOVERY_DONE
7132 * and wakeups up this thread which will reap the thread and finish up.
7133 * This thread also removes any faulty devices (with nr_pending == 0).
7135 * The overall approach is:
7136 * 1/ if the superblock needs updating, update it.
7137 * 2/ If a recovery thread is running, don't do anything else.
7138 * 3/ If recovery has finished, clean up, possibly marking spares active.
7139 * 4/ If there are any faulty devices, remove them.
7140 * 5/ If array is degraded, try to add spares devices
7141 * 6/ If array has spares or is not in-sync, start a resync thread.
7143 void md_check_recovery(mddev_t
*mddev
)
7146 bitmap_daemon_work(mddev
);
7151 if (signal_pending(current
)) {
7152 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
7153 printk(KERN_INFO
"md: %s in immediate safe mode\n",
7155 mddev
->safemode
= 2;
7157 flush_signals(current
);
7160 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
7163 (mddev
->flags
& ~ (1<<MD_CHANGE_PENDING
)) ||
7164 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
7165 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
7166 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
7167 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
7168 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
7172 if (mddev_trylock(mddev
)) {
7176 /* Only thing we do on a ro array is remove
7180 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7181 if (rdev
->raid_disk
>= 0 &&
7182 !test_bit(Blocked
, &rdev
->flags
) &&
7183 test_bit(Faulty
, &rdev
->flags
) &&
7184 atomic_read(&rdev
->nr_pending
)==0) {
7185 if (mddev
->pers
->hot_remove_disk(
7186 mddev
, rdev
->raid_disk
)==0) {
7188 sprintf(nm
,"rd%d", rdev
->raid_disk
);
7189 sysfs_remove_link(&mddev
->kobj
, nm
);
7190 rdev
->raid_disk
= -1;
7193 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7197 if (!mddev
->external
) {
7199 spin_lock_irq(&mddev
->write_lock
);
7200 if (mddev
->safemode
&&
7201 !atomic_read(&mddev
->writes_pending
) &&
7203 mddev
->recovery_cp
== MaxSector
) {
7206 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7208 if (mddev
->safemode
== 1)
7209 mddev
->safemode
= 0;
7210 spin_unlock_irq(&mddev
->write_lock
);
7212 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7216 md_update_sb(mddev
, 0);
7218 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
7219 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
7220 /* resync/recovery still happening */
7221 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7224 if (mddev
->sync_thread
) {
7225 reap_sync_thread(mddev
);
7228 /* Set RUNNING before clearing NEEDED to avoid
7229 * any transients in the value of "sync_action".
7231 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7232 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7233 /* Clear some bits that don't mean anything, but
7236 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7237 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
7239 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
7241 /* no recovery is running.
7242 * remove any failed drives, then
7243 * add spares if possible.
7244 * Spare are also removed and re-added, to allow
7245 * the personality to fail the re-add.
7248 if (mddev
->reshape_position
!= MaxSector
) {
7249 if (mddev
->pers
->check_reshape
== NULL
||
7250 mddev
->pers
->check_reshape(mddev
) != 0)
7251 /* Cannot proceed */
7253 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7254 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7255 } else if ((spares
= remove_and_add_spares(mddev
))) {
7256 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7257 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7258 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7259 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7260 } else if (mddev
->recovery_cp
< MaxSector
) {
7261 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7262 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7263 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
7264 /* nothing to be done ... */
7267 if (mddev
->pers
->sync_request
) {
7268 if (spares
&& mddev
->bitmap
&& ! mddev
->bitmap
->file
) {
7269 /* We are adding a device or devices to an array
7270 * which has the bitmap stored on all devices.
7271 * So make sure all bitmap pages get written
7273 bitmap_write_all(mddev
->bitmap
);
7275 mddev
->sync_thread
= md_register_thread(md_do_sync
,
7278 if (!mddev
->sync_thread
) {
7279 printk(KERN_ERR
"%s: could not start resync"
7282 /* leave the spares where they are, it shouldn't hurt */
7283 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7284 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7285 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7286 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7287 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7289 md_wakeup_thread(mddev
->sync_thread
);
7290 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7291 md_new_event(mddev
);
7294 if (!mddev
->sync_thread
) {
7295 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7296 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
7298 if (mddev
->sysfs_action
)
7299 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7301 mddev_unlock(mddev
);
7305 void md_wait_for_blocked_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
7307 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
7308 wait_event_timeout(rdev
->blocked_wait
,
7309 !test_bit(Blocked
, &rdev
->flags
),
7310 msecs_to_jiffies(5000));
7311 rdev_dec_pending(rdev
, mddev
);
7313 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
7315 static int md_notify_reboot(struct notifier_block
*this,
7316 unsigned long code
, void *x
)
7318 struct list_head
*tmp
;
7321 if ((code
== SYS_DOWN
) || (code
== SYS_HALT
) || (code
== SYS_POWER_OFF
)) {
7323 printk(KERN_INFO
"md: stopping all md devices.\n");
7325 for_each_mddev(mddev
, tmp
)
7326 if (mddev_trylock(mddev
)) {
7327 /* Force a switch to readonly even array
7328 * appears to still be in use. Hence
7331 md_set_readonly(mddev
, 100);
7332 mddev_unlock(mddev
);
7335 * certain more exotic SCSI devices are known to be
7336 * volatile wrt too early system reboots. While the
7337 * right place to handle this issue is the given
7338 * driver, we do want to have a safe RAID driver ...
7345 static struct notifier_block md_notifier
= {
7346 .notifier_call
= md_notify_reboot
,
7348 .priority
= INT_MAX
, /* before any real devices */
7351 static void md_geninit(void)
7353 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
7355 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
7358 static int __init
md_init(void)
7362 md_wq
= alloc_workqueue("md", WQ_MEM_RECLAIM
, 0);
7366 md_misc_wq
= alloc_workqueue("md_misc", 0, 0);
7370 if ((ret
= register_blkdev(MD_MAJOR
, "md")) < 0)
7373 if ((ret
= register_blkdev(0, "mdp")) < 0)
7377 blk_register_region(MKDEV(MD_MAJOR
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
7378 md_probe
, NULL
, NULL
);
7379 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
7380 md_probe
, NULL
, NULL
);
7382 register_reboot_notifier(&md_notifier
);
7383 raid_table_header
= register_sysctl_table(raid_root_table
);
7389 unregister_blkdev(MD_MAJOR
, "md");
7391 destroy_workqueue(md_misc_wq
);
7393 destroy_workqueue(md_wq
);
7401 * Searches all registered partitions for autorun RAID arrays
7405 static LIST_HEAD(all_detected_devices
);
7406 struct detected_devices_node
{
7407 struct list_head list
;
7411 void md_autodetect_dev(dev_t dev
)
7413 struct detected_devices_node
*node_detected_dev
;
7415 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
7416 if (node_detected_dev
) {
7417 node_detected_dev
->dev
= dev
;
7418 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
7420 printk(KERN_CRIT
"md: md_autodetect_dev: kzalloc failed"
7421 ", skipping dev(%d,%d)\n", MAJOR(dev
), MINOR(dev
));
7426 static void autostart_arrays(int part
)
7429 struct detected_devices_node
*node_detected_dev
;
7431 int i_scanned
, i_passed
;
7436 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
7438 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
7440 node_detected_dev
= list_entry(all_detected_devices
.next
,
7441 struct detected_devices_node
, list
);
7442 list_del(&node_detected_dev
->list
);
7443 dev
= node_detected_dev
->dev
;
7444 kfree(node_detected_dev
);
7445 rdev
= md_import_device(dev
,0, 90);
7449 if (test_bit(Faulty
, &rdev
->flags
)) {
7453 set_bit(AutoDetected
, &rdev
->flags
);
7454 list_add(&rdev
->same_set
, &pending_raid_disks
);
7458 printk(KERN_INFO
"md: Scanned %d and added %d devices.\n",
7459 i_scanned
, i_passed
);
7461 autorun_devices(part
);
7464 #endif /* !MODULE */
7466 static __exit
void md_exit(void)
7469 struct list_head
*tmp
;
7471 blk_unregister_region(MKDEV(MD_MAJOR
,0), 1U << MINORBITS
);
7472 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
7474 unregister_blkdev(MD_MAJOR
,"md");
7475 unregister_blkdev(mdp_major
, "mdp");
7476 unregister_reboot_notifier(&md_notifier
);
7477 unregister_sysctl_table(raid_table_header
);
7478 remove_proc_entry("mdstat", NULL
);
7479 for_each_mddev(mddev
, tmp
) {
7480 export_array(mddev
);
7481 mddev
->hold_active
= 0;
7483 destroy_workqueue(md_misc_wq
);
7484 destroy_workqueue(md_wq
);
7487 subsys_initcall(md_init
);
7488 module_exit(md_exit
)
7490 static int get_ro(char *buffer
, struct kernel_param
*kp
)
7492 return sprintf(buffer
, "%d", start_readonly
);
7494 static int set_ro(const char *val
, struct kernel_param
*kp
)
7497 int num
= simple_strtoul(val
, &e
, 10);
7498 if (*val
&& (*e
== '\0' || *e
== '\n')) {
7499 start_readonly
= num
;
7505 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
7506 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
7508 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
7510 EXPORT_SYMBOL(register_md_personality
);
7511 EXPORT_SYMBOL(unregister_md_personality
);
7512 EXPORT_SYMBOL(md_error
);
7513 EXPORT_SYMBOL(md_done_sync
);
7514 EXPORT_SYMBOL(md_write_start
);
7515 EXPORT_SYMBOL(md_write_end
);
7516 EXPORT_SYMBOL(md_register_thread
);
7517 EXPORT_SYMBOL(md_unregister_thread
);
7518 EXPORT_SYMBOL(md_wakeup_thread
);
7519 EXPORT_SYMBOL(md_check_recovery
);
7520 MODULE_LICENSE("GPL");
7521 MODULE_DESCRIPTION("MD RAID framework");
7523 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);