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
13 - kmod support by: Cyrus Durgin
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
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.
34 Errors, Warnings, etc.
36 pr_crit() for error conditions that risk data loss
37 pr_err() for error conditions that are unexpected, like an IO error
38 or internal inconsistency
39 pr_warn() for error conditions that could have been predicated, like
40 adding a device to an array when it has incompatible metadata
41 pr_info() for every interesting, very rare events, like an array starting
42 or stopping, or resync starting or stopping
43 pr_debug() for everything else.
47 #include <linux/sched/signal.h>
48 #include <linux/kthread.h>
49 #include <linux/blkdev.h>
50 #include <linux/badblocks.h>
51 #include <linux/sysctl.h>
52 #include <linux/seq_file.h>
54 #include <linux/poll.h>
55 #include <linux/ctype.h>
56 #include <linux/string.h>
57 #include <linux/hdreg.h>
58 #include <linux/proc_fs.h>
59 #include <linux/random.h>
60 #include <linux/module.h>
61 #include <linux/reboot.h>
62 #include <linux/file.h>
63 #include <linux/compat.h>
64 #include <linux/delay.h>
65 #include <linux/raid/md_p.h>
66 #include <linux/raid/md_u.h>
67 #include <linux/slab.h>
68 #include <trace/events/block.h>
71 #include "md-cluster.h"
74 static void autostart_arrays(int part);
77 /* pers_list is a list of registered personalities protected
79 * pers_lock does extra service to protect accesses to
80 * mddev->thread when the mutex cannot be held.
82 static LIST_HEAD(pers_list);
83 static DEFINE_SPINLOCK(pers_lock);
85 struct md_cluster_operations *md_cluster_ops;
86 EXPORT_SYMBOL(md_cluster_ops);
87 struct module *md_cluster_mod;
88 EXPORT_SYMBOL(md_cluster_mod);
90 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
91 static struct workqueue_struct *md_wq;
92 static struct workqueue_struct *md_misc_wq;
94 static int remove_and_add_spares(struct mddev *mddev,
95 struct md_rdev *this);
96 static void mddev_detach(struct mddev *mddev);
99 * Default number of read corrections we'll attempt on an rdev
100 * before ejecting it from the array. We divide the read error
101 * count by 2 for every hour elapsed between read errors.
103 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
105 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
106 * is 1000 KB/sec, so the extra system load does not show up that much.
107 * Increase it if you want to have more _guaranteed_ speed. Note that
108 * the RAID driver will use the maximum available bandwidth if the IO
109 * subsystem is idle. There is also an 'absolute maximum' reconstruction
110 * speed limit - in case reconstruction slows down your system despite
113 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
114 * or /sys/block/mdX/md/sync_speed_{min,max}
117 static int sysctl_speed_limit_min = 1000;
118 static int sysctl_speed_limit_max = 200000;
119 static inline int speed_min(struct mddev *mddev)
121 return mddev->sync_speed_min ?
122 mddev->sync_speed_min : sysctl_speed_limit_min;
125 static inline int speed_max(struct mddev *mddev)
127 return mddev->sync_speed_max ?
128 mddev->sync_speed_max : sysctl_speed_limit_max;
131 static struct ctl_table_header *raid_table_header;
133 static struct ctl_table raid_table[] = {
135 .procname = "speed_limit_min",
136 .data = &sysctl_speed_limit_min,
137 .maxlen = sizeof(int),
138 .mode = S_IRUGO|S_IWUSR,
139 .proc_handler = proc_dointvec,
142 .procname = "speed_limit_max",
143 .data = &sysctl_speed_limit_max,
144 .maxlen = sizeof(int),
145 .mode = S_IRUGO|S_IWUSR,
146 .proc_handler = proc_dointvec,
151 static struct ctl_table raid_dir_table[] = {
155 .mode = S_IRUGO|S_IXUGO,
161 static struct ctl_table raid_root_table[] = {
166 .child = raid_dir_table,
171 static const struct block_device_operations md_fops;
173 static int start_readonly;
176 * like bio_clone, but with a local bio set
179 struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
184 if (!mddev || !mddev->bio_set)
185 return bio_alloc(gfp_mask, nr_iovecs);
187 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
192 EXPORT_SYMBOL_GPL(bio_alloc_mddev);
195 * We have a system wide 'event count' that is incremented
196 * on any 'interesting' event, and readers of /proc/mdstat
197 * can use 'poll' or 'select' to find out when the event
201 * start array, stop array, error, add device, remove device,
202 * start build, activate spare
204 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
205 static atomic_t md_event_count;
206 void md_new_event(struct mddev *mddev)
208 atomic_inc(&md_event_count);
209 wake_up(&md_event_waiters);
211 EXPORT_SYMBOL_GPL(md_new_event);
214 * Enables to iterate over all existing md arrays
215 * all_mddevs_lock protects this list.
217 static LIST_HEAD(all_mddevs);
218 static DEFINE_SPINLOCK(all_mddevs_lock);
221 * iterates through all used mddevs in the system.
222 * We take care to grab the all_mddevs_lock whenever navigating
223 * the list, and to always hold a refcount when unlocked.
224 * Any code which breaks out of this loop while own
225 * a reference to the current mddev and must mddev_put it.
227 #define for_each_mddev(_mddev,_tmp) \
229 for (({ spin_lock(&all_mddevs_lock); \
230 _tmp = all_mddevs.next; \
232 ({ if (_tmp != &all_mddevs) \
233 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
234 spin_unlock(&all_mddevs_lock); \
235 if (_mddev) mddev_put(_mddev); \
236 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
237 _tmp != &all_mddevs;}); \
238 ({ spin_lock(&all_mddevs_lock); \
239 _tmp = _tmp->next;}) \
242 /* Rather than calling directly into the personality make_request function,
243 * IO requests come here first so that we can check if the device is
244 * being suspended pending a reconfiguration.
245 * We hold a refcount over the call to ->make_request. By the time that
246 * call has finished, the bio has been linked into some internal structure
247 * and so is visible to ->quiesce(), so we don't need the refcount any more.
249 static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio)
251 const int rw = bio_data_dir(bio);
252 struct mddev *mddev = q->queuedata;
253 unsigned int sectors;
256 blk_queue_split(q, &bio, q->bio_split);
258 if (mddev == NULL || mddev->pers == NULL) {
260 return BLK_QC_T_NONE;
262 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
263 if (bio_sectors(bio) != 0)
264 bio->bi_error = -EROFS;
266 return BLK_QC_T_NONE;
268 smp_rmb(); /* Ensure implications of 'active' are visible */
270 if (mddev->suspended) {
273 prepare_to_wait(&mddev->sb_wait, &__wait,
274 TASK_UNINTERRUPTIBLE);
275 if (!mddev->suspended)
281 finish_wait(&mddev->sb_wait, &__wait);
283 atomic_inc(&mddev->active_io);
287 * save the sectors now since our bio can
288 * go away inside make_request
290 sectors = bio_sectors(bio);
291 /* bio could be mergeable after passing to underlayer */
292 bio->bi_opf &= ~REQ_NOMERGE;
293 mddev->pers->make_request(mddev, bio);
295 cpu = part_stat_lock();
296 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
297 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
300 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
301 wake_up(&mddev->sb_wait);
303 return BLK_QC_T_NONE;
306 /* mddev_suspend makes sure no new requests are submitted
307 * to the device, and that any requests that have been submitted
308 * are completely handled.
309 * Once mddev_detach() is called and completes, the module will be
312 void mddev_suspend(struct mddev *mddev)
314 WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk);
315 if (mddev->suspended++)
318 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
319 mddev->pers->quiesce(mddev, 1);
321 del_timer_sync(&mddev->safemode_timer);
323 EXPORT_SYMBOL_GPL(mddev_suspend);
325 void mddev_resume(struct mddev *mddev)
327 if (--mddev->suspended)
329 wake_up(&mddev->sb_wait);
330 mddev->pers->quiesce(mddev, 0);
332 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
333 md_wakeup_thread(mddev->thread);
334 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
336 EXPORT_SYMBOL_GPL(mddev_resume);
338 int mddev_congested(struct mddev *mddev, int bits)
340 struct md_personality *pers = mddev->pers;
344 if (mddev->suspended)
346 else if (pers && pers->congested)
347 ret = pers->congested(mddev, bits);
351 EXPORT_SYMBOL_GPL(mddev_congested);
352 static int md_congested(void *data, int bits)
354 struct mddev *mddev = data;
355 return mddev_congested(mddev, bits);
359 * Generic flush handling for md
362 static void md_end_flush(struct bio *bio)
364 struct md_rdev *rdev = bio->bi_private;
365 struct mddev *mddev = rdev->mddev;
367 rdev_dec_pending(rdev, mddev);
369 if (atomic_dec_and_test(&mddev->flush_pending)) {
370 /* The pre-request flush has finished */
371 queue_work(md_wq, &mddev->flush_work);
376 static void md_submit_flush_data(struct work_struct *ws);
378 static void submit_flushes(struct work_struct *ws)
380 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
381 struct md_rdev *rdev;
383 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
384 atomic_set(&mddev->flush_pending, 1);
386 rdev_for_each_rcu(rdev, mddev)
387 if (rdev->raid_disk >= 0 &&
388 !test_bit(Faulty, &rdev->flags)) {
389 /* Take two references, one is dropped
390 * when request finishes, one after
391 * we reclaim rcu_read_lock
394 atomic_inc(&rdev->nr_pending);
395 atomic_inc(&rdev->nr_pending);
397 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
398 bi->bi_end_io = md_end_flush;
399 bi->bi_private = rdev;
400 bi->bi_bdev = rdev->bdev;
401 bi->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
402 atomic_inc(&mddev->flush_pending);
405 rdev_dec_pending(rdev, mddev);
408 if (atomic_dec_and_test(&mddev->flush_pending))
409 queue_work(md_wq, &mddev->flush_work);
412 static void md_submit_flush_data(struct work_struct *ws)
414 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
415 struct bio *bio = mddev->flush_bio;
417 if (bio->bi_iter.bi_size == 0)
418 /* an empty barrier - all done */
421 bio->bi_opf &= ~REQ_PREFLUSH;
422 mddev->pers->make_request(mddev, bio);
425 mddev->flush_bio = NULL;
426 wake_up(&mddev->sb_wait);
429 void md_flush_request(struct mddev *mddev, struct bio *bio)
431 spin_lock_irq(&mddev->lock);
432 wait_event_lock_irq(mddev->sb_wait,
435 mddev->flush_bio = bio;
436 spin_unlock_irq(&mddev->lock);
438 INIT_WORK(&mddev->flush_work, submit_flushes);
439 queue_work(md_wq, &mddev->flush_work);
441 EXPORT_SYMBOL(md_flush_request);
443 static inline struct mddev *mddev_get(struct mddev *mddev)
445 atomic_inc(&mddev->active);
449 static void mddev_delayed_delete(struct work_struct *ws);
451 static void mddev_put(struct mddev *mddev)
453 struct bio_set *bs = NULL;
455 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
457 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
458 mddev->ctime == 0 && !mddev->hold_active) {
459 /* Array is not configured at all, and not held active,
461 list_del_init(&mddev->all_mddevs);
463 mddev->bio_set = NULL;
464 if (mddev->gendisk) {
465 /* We did a probe so need to clean up. Call
466 * queue_work inside the spinlock so that
467 * flush_workqueue() after mddev_find will
468 * succeed in waiting for the work to be done.
470 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
471 queue_work(md_misc_wq, &mddev->del_work);
475 spin_unlock(&all_mddevs_lock);
480 static void md_safemode_timeout(unsigned long data);
482 void mddev_init(struct mddev *mddev)
484 mutex_init(&mddev->open_mutex);
485 mutex_init(&mddev->reconfig_mutex);
486 mutex_init(&mddev->bitmap_info.mutex);
487 INIT_LIST_HEAD(&mddev->disks);
488 INIT_LIST_HEAD(&mddev->all_mddevs);
489 setup_timer(&mddev->safemode_timer, md_safemode_timeout,
490 (unsigned long) mddev);
491 atomic_set(&mddev->active, 1);
492 atomic_set(&mddev->openers, 0);
493 atomic_set(&mddev->active_io, 0);
494 spin_lock_init(&mddev->lock);
495 atomic_set(&mddev->flush_pending, 0);
496 init_waitqueue_head(&mddev->sb_wait);
497 init_waitqueue_head(&mddev->recovery_wait);
498 mddev->reshape_position = MaxSector;
499 mddev->reshape_backwards = 0;
500 mddev->last_sync_action = "none";
501 mddev->resync_min = 0;
502 mddev->resync_max = MaxSector;
503 mddev->level = LEVEL_NONE;
505 EXPORT_SYMBOL_GPL(mddev_init);
507 static struct mddev *mddev_find(dev_t unit)
509 struct mddev *mddev, *new = NULL;
511 if (unit && MAJOR(unit) != MD_MAJOR)
512 unit &= ~((1<<MdpMinorShift)-1);
515 spin_lock(&all_mddevs_lock);
518 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
519 if (mddev->unit == unit) {
521 spin_unlock(&all_mddevs_lock);
527 list_add(&new->all_mddevs, &all_mddevs);
528 spin_unlock(&all_mddevs_lock);
529 new->hold_active = UNTIL_IOCTL;
533 /* find an unused unit number */
534 static int next_minor = 512;
535 int start = next_minor;
539 dev = MKDEV(MD_MAJOR, next_minor);
541 if (next_minor > MINORMASK)
543 if (next_minor == start) {
544 /* Oh dear, all in use. */
545 spin_unlock(&all_mddevs_lock);
551 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
552 if (mddev->unit == dev) {
558 new->md_minor = MINOR(dev);
559 new->hold_active = UNTIL_STOP;
560 list_add(&new->all_mddevs, &all_mddevs);
561 spin_unlock(&all_mddevs_lock);
564 spin_unlock(&all_mddevs_lock);
566 new = kzalloc(sizeof(*new), GFP_KERNEL);
571 if (MAJOR(unit) == MD_MAJOR)
572 new->md_minor = MINOR(unit);
574 new->md_minor = MINOR(unit) >> MdpMinorShift;
581 static struct attribute_group md_redundancy_group;
583 void mddev_unlock(struct mddev *mddev)
585 if (mddev->to_remove) {
586 /* These cannot be removed under reconfig_mutex as
587 * an access to the files will try to take reconfig_mutex
588 * while holding the file unremovable, which leads to
590 * So hold set sysfs_active while the remove in happeing,
591 * and anything else which might set ->to_remove or my
592 * otherwise change the sysfs namespace will fail with
593 * -EBUSY if sysfs_active is still set.
594 * We set sysfs_active under reconfig_mutex and elsewhere
595 * test it under the same mutex to ensure its correct value
598 struct attribute_group *to_remove = mddev->to_remove;
599 mddev->to_remove = NULL;
600 mddev->sysfs_active = 1;
601 mutex_unlock(&mddev->reconfig_mutex);
603 if (mddev->kobj.sd) {
604 if (to_remove != &md_redundancy_group)
605 sysfs_remove_group(&mddev->kobj, to_remove);
606 if (mddev->pers == NULL ||
607 mddev->pers->sync_request == NULL) {
608 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
609 if (mddev->sysfs_action)
610 sysfs_put(mddev->sysfs_action);
611 mddev->sysfs_action = NULL;
614 mddev->sysfs_active = 0;
616 mutex_unlock(&mddev->reconfig_mutex);
618 /* As we've dropped the mutex we need a spinlock to
619 * make sure the thread doesn't disappear
621 spin_lock(&pers_lock);
622 md_wakeup_thread(mddev->thread);
623 spin_unlock(&pers_lock);
625 EXPORT_SYMBOL_GPL(mddev_unlock);
627 struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
629 struct md_rdev *rdev;
631 rdev_for_each_rcu(rdev, mddev)
632 if (rdev->desc_nr == nr)
637 EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
639 static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
641 struct md_rdev *rdev;
643 rdev_for_each(rdev, mddev)
644 if (rdev->bdev->bd_dev == dev)
650 static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
652 struct md_rdev *rdev;
654 rdev_for_each_rcu(rdev, mddev)
655 if (rdev->bdev->bd_dev == dev)
661 static struct md_personality *find_pers(int level, char *clevel)
663 struct md_personality *pers;
664 list_for_each_entry(pers, &pers_list, list) {
665 if (level != LEVEL_NONE && pers->level == level)
667 if (strcmp(pers->name, clevel)==0)
673 /* return the offset of the super block in 512byte sectors */
674 static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
676 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
677 return MD_NEW_SIZE_SECTORS(num_sectors);
680 static int alloc_disk_sb(struct md_rdev *rdev)
682 rdev->sb_page = alloc_page(GFP_KERNEL);
688 void md_rdev_clear(struct md_rdev *rdev)
691 put_page(rdev->sb_page);
693 rdev->sb_page = NULL;
698 put_page(rdev->bb_page);
699 rdev->bb_page = NULL;
701 badblocks_exit(&rdev->badblocks);
703 EXPORT_SYMBOL_GPL(md_rdev_clear);
705 static void super_written(struct bio *bio)
707 struct md_rdev *rdev = bio->bi_private;
708 struct mddev *mddev = rdev->mddev;
711 pr_err("md: super_written gets error=%d\n", bio->bi_error);
712 md_error(mddev, rdev);
713 if (!test_bit(Faulty, &rdev->flags)
714 && (bio->bi_opf & MD_FAILFAST)) {
715 set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags);
716 set_bit(LastDev, &rdev->flags);
719 clear_bit(LastDev, &rdev->flags);
721 if (atomic_dec_and_test(&mddev->pending_writes))
722 wake_up(&mddev->sb_wait);
723 rdev_dec_pending(rdev, mddev);
727 void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
728 sector_t sector, int size, struct page *page)
730 /* write first size bytes of page to sector of rdev
731 * Increment mddev->pending_writes before returning
732 * and decrement it on completion, waking up sb_wait
733 * if zero is reached.
734 * If an error occurred, call md_error
739 if (test_bit(Faulty, &rdev->flags))
742 bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
744 atomic_inc(&rdev->nr_pending);
746 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
747 bio->bi_iter.bi_sector = sector;
748 bio_add_page(bio, page, size, 0);
749 bio->bi_private = rdev;
750 bio->bi_end_io = super_written;
752 if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) &&
753 test_bit(FailFast, &rdev->flags) &&
754 !test_bit(LastDev, &rdev->flags))
756 bio->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_FUA | ff;
758 atomic_inc(&mddev->pending_writes);
762 int md_super_wait(struct mddev *mddev)
764 /* wait for all superblock writes that were scheduled to complete */
765 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
766 if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags))
771 int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
772 struct page *page, int op, int op_flags, bool metadata_op)
774 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
777 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
778 rdev->meta_bdev : rdev->bdev;
779 bio_set_op_attrs(bio, op, op_flags);
781 bio->bi_iter.bi_sector = sector + rdev->sb_start;
782 else if (rdev->mddev->reshape_position != MaxSector &&
783 (rdev->mddev->reshape_backwards ==
784 (sector >= rdev->mddev->reshape_position)))
785 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
787 bio->bi_iter.bi_sector = sector + rdev->data_offset;
788 bio_add_page(bio, page, size, 0);
790 submit_bio_wait(bio);
792 ret = !bio->bi_error;
796 EXPORT_SYMBOL_GPL(sync_page_io);
798 static int read_disk_sb(struct md_rdev *rdev, int size)
800 char b[BDEVNAME_SIZE];
805 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true))
811 pr_err("md: disabled device %s, could not read superblock.\n",
812 bdevname(rdev->bdev,b));
816 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
818 return sb1->set_uuid0 == sb2->set_uuid0 &&
819 sb1->set_uuid1 == sb2->set_uuid1 &&
820 sb1->set_uuid2 == sb2->set_uuid2 &&
821 sb1->set_uuid3 == sb2->set_uuid3;
824 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
827 mdp_super_t *tmp1, *tmp2;
829 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
830 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
832 if (!tmp1 || !tmp2) {
841 * nr_disks is not constant
846 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
853 static u32 md_csum_fold(u32 csum)
855 csum = (csum & 0xffff) + (csum >> 16);
856 return (csum & 0xffff) + (csum >> 16);
859 static unsigned int calc_sb_csum(mdp_super_t *sb)
862 u32 *sb32 = (u32*)sb;
864 unsigned int disk_csum, csum;
866 disk_csum = sb->sb_csum;
869 for (i = 0; i < MD_SB_BYTES/4 ; i++)
871 csum = (newcsum & 0xffffffff) + (newcsum>>32);
874 /* This used to use csum_partial, which was wrong for several
875 * reasons including that different results are returned on
876 * different architectures. It isn't critical that we get exactly
877 * the same return value as before (we always csum_fold before
878 * testing, and that removes any differences). However as we
879 * know that csum_partial always returned a 16bit value on
880 * alphas, do a fold to maximise conformity to previous behaviour.
882 sb->sb_csum = md_csum_fold(disk_csum);
884 sb->sb_csum = disk_csum;
890 * Handle superblock details.
891 * We want to be able to handle multiple superblock formats
892 * so we have a common interface to them all, and an array of
893 * different handlers.
894 * We rely on user-space to write the initial superblock, and support
895 * reading and updating of superblocks.
896 * Interface methods are:
897 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
898 * loads and validates a superblock on dev.
899 * if refdev != NULL, compare superblocks on both devices
901 * 0 - dev has a superblock that is compatible with refdev
902 * 1 - dev has a superblock that is compatible and newer than refdev
903 * so dev should be used as the refdev in future
904 * -EINVAL superblock incompatible or invalid
905 * -othererror e.g. -EIO
907 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
908 * Verify that dev is acceptable into mddev.
909 * The first time, mddev->raid_disks will be 0, and data from
910 * dev should be merged in. Subsequent calls check that dev
911 * is new enough. Return 0 or -EINVAL
913 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
914 * Update the superblock for rdev with data in mddev
915 * This does not write to disc.
921 struct module *owner;
922 int (*load_super)(struct md_rdev *rdev,
923 struct md_rdev *refdev,
925 int (*validate_super)(struct mddev *mddev,
926 struct md_rdev *rdev);
927 void (*sync_super)(struct mddev *mddev,
928 struct md_rdev *rdev);
929 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
930 sector_t num_sectors);
931 int (*allow_new_offset)(struct md_rdev *rdev,
932 unsigned long long new_offset);
936 * Check that the given mddev has no bitmap.
938 * This function is called from the run method of all personalities that do not
939 * support bitmaps. It prints an error message and returns non-zero if mddev
940 * has a bitmap. Otherwise, it returns 0.
943 int md_check_no_bitmap(struct mddev *mddev)
945 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
947 pr_warn("%s: bitmaps are not supported for %s\n",
948 mdname(mddev), mddev->pers->name);
951 EXPORT_SYMBOL(md_check_no_bitmap);
954 * load_super for 0.90.0
956 static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
958 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
963 * Calculate the position of the superblock (512byte sectors),
964 * it's at the end of the disk.
966 * It also happens to be a multiple of 4Kb.
968 rdev->sb_start = calc_dev_sboffset(rdev);
970 ret = read_disk_sb(rdev, MD_SB_BYTES);
976 bdevname(rdev->bdev, b);
977 sb = page_address(rdev->sb_page);
979 if (sb->md_magic != MD_SB_MAGIC) {
980 pr_warn("md: invalid raid superblock magic on %s\n", b);
984 if (sb->major_version != 0 ||
985 sb->minor_version < 90 ||
986 sb->minor_version > 91) {
987 pr_warn("Bad version number %d.%d on %s\n",
988 sb->major_version, sb->minor_version, b);
992 if (sb->raid_disks <= 0)
995 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
996 pr_warn("md: invalid superblock checksum on %s\n", b);
1000 rdev->preferred_minor = sb->md_minor;
1001 rdev->data_offset = 0;
1002 rdev->new_data_offset = 0;
1003 rdev->sb_size = MD_SB_BYTES;
1004 rdev->badblocks.shift = -1;
1006 if (sb->level == LEVEL_MULTIPATH)
1009 rdev->desc_nr = sb->this_disk.number;
1015 mdp_super_t *refsb = page_address(refdev->sb_page);
1016 if (!uuid_equal(refsb, sb)) {
1017 pr_warn("md: %s has different UUID to %s\n",
1018 b, bdevname(refdev->bdev,b2));
1021 if (!sb_equal(refsb, sb)) {
1022 pr_warn("md: %s has same UUID but different superblock to %s\n",
1023 b, bdevname(refdev->bdev, b2));
1027 ev2 = md_event(refsb);
1033 rdev->sectors = rdev->sb_start;
1034 /* Limit to 4TB as metadata cannot record more than that.
1035 * (not needed for Linear and RAID0 as metadata doesn't
1038 if (IS_ENABLED(CONFIG_LBDAF) && (u64)rdev->sectors >= (2ULL << 32) &&
1040 rdev->sectors = (sector_t)(2ULL << 32) - 2;
1042 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
1043 /* "this cannot possibly happen" ... */
1051 * validate_super for 0.90.0
1053 static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
1056 mdp_super_t *sb = page_address(rdev->sb_page);
1057 __u64 ev1 = md_event(sb);
1059 rdev->raid_disk = -1;
1060 clear_bit(Faulty, &rdev->flags);
1061 clear_bit(In_sync, &rdev->flags);
1062 clear_bit(Bitmap_sync, &rdev->flags);
1063 clear_bit(WriteMostly, &rdev->flags);
1065 if (mddev->raid_disks == 0) {
1066 mddev->major_version = 0;
1067 mddev->minor_version = sb->minor_version;
1068 mddev->patch_version = sb->patch_version;
1069 mddev->external = 0;
1070 mddev->chunk_sectors = sb->chunk_size >> 9;
1071 mddev->ctime = sb->ctime;
1072 mddev->utime = sb->utime;
1073 mddev->level = sb->level;
1074 mddev->clevel[0] = 0;
1075 mddev->layout = sb->layout;
1076 mddev->raid_disks = sb->raid_disks;
1077 mddev->dev_sectors = ((sector_t)sb->size) * 2;
1078 mddev->events = ev1;
1079 mddev->bitmap_info.offset = 0;
1080 mddev->bitmap_info.space = 0;
1081 /* bitmap can use 60 K after the 4K superblocks */
1082 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
1083 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
1084 mddev->reshape_backwards = 0;
1086 if (mddev->minor_version >= 91) {
1087 mddev->reshape_position = sb->reshape_position;
1088 mddev->delta_disks = sb->delta_disks;
1089 mddev->new_level = sb->new_level;
1090 mddev->new_layout = sb->new_layout;
1091 mddev->new_chunk_sectors = sb->new_chunk >> 9;
1092 if (mddev->delta_disks < 0)
1093 mddev->reshape_backwards = 1;
1095 mddev->reshape_position = MaxSector;
1096 mddev->delta_disks = 0;
1097 mddev->new_level = mddev->level;
1098 mddev->new_layout = mddev->layout;
1099 mddev->new_chunk_sectors = mddev->chunk_sectors;
1102 if (sb->state & (1<<MD_SB_CLEAN))
1103 mddev->recovery_cp = MaxSector;
1105 if (sb->events_hi == sb->cp_events_hi &&
1106 sb->events_lo == sb->cp_events_lo) {
1107 mddev->recovery_cp = sb->recovery_cp;
1109 mddev->recovery_cp = 0;
1112 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1113 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1114 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1115 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1117 mddev->max_disks = MD_SB_DISKS;
1119 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
1120 mddev->bitmap_info.file == NULL) {
1121 mddev->bitmap_info.offset =
1122 mddev->bitmap_info.default_offset;
1123 mddev->bitmap_info.space =
1124 mddev->bitmap_info.default_space;
1127 } else if (mddev->pers == NULL) {
1128 /* Insist on good event counter while assembling, except
1129 * for spares (which don't need an event count) */
1131 if (sb->disks[rdev->desc_nr].state & (
1132 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1133 if (ev1 < mddev->events)
1135 } else if (mddev->bitmap) {
1136 /* if adding to array with a bitmap, then we can accept an
1137 * older device ... but not too old.
1139 if (ev1 < mddev->bitmap->events_cleared)
1141 if (ev1 < mddev->events)
1142 set_bit(Bitmap_sync, &rdev->flags);
1144 if (ev1 < mddev->events)
1145 /* just a hot-add of a new device, leave raid_disk at -1 */
1149 if (mddev->level != LEVEL_MULTIPATH) {
1150 desc = sb->disks + rdev->desc_nr;
1152 if (desc->state & (1<<MD_DISK_FAULTY))
1153 set_bit(Faulty, &rdev->flags);
1154 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1155 desc->raid_disk < mddev->raid_disks */) {
1156 set_bit(In_sync, &rdev->flags);
1157 rdev->raid_disk = desc->raid_disk;
1158 rdev->saved_raid_disk = desc->raid_disk;
1159 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1160 /* active but not in sync implies recovery up to
1161 * reshape position. We don't know exactly where
1162 * that is, so set to zero for now */
1163 if (mddev->minor_version >= 91) {
1164 rdev->recovery_offset = 0;
1165 rdev->raid_disk = desc->raid_disk;
1168 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1169 set_bit(WriteMostly, &rdev->flags);
1170 if (desc->state & (1<<MD_DISK_FAILFAST))
1171 set_bit(FailFast, &rdev->flags);
1172 } else /* MULTIPATH are always insync */
1173 set_bit(In_sync, &rdev->flags);
1178 * sync_super for 0.90.0
1180 static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
1183 struct md_rdev *rdev2;
1184 int next_spare = mddev->raid_disks;
1186 /* make rdev->sb match mddev data..
1189 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1190 * 3/ any empty disks < next_spare become removed
1192 * disks[0] gets initialised to REMOVED because
1193 * we cannot be sure from other fields if it has
1194 * been initialised or not.
1197 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1199 rdev->sb_size = MD_SB_BYTES;
1201 sb = page_address(rdev->sb_page);
1203 memset(sb, 0, sizeof(*sb));
1205 sb->md_magic = MD_SB_MAGIC;
1206 sb->major_version = mddev->major_version;
1207 sb->patch_version = mddev->patch_version;
1208 sb->gvalid_words = 0; /* ignored */
1209 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1210 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1211 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1212 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1214 sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
1215 sb->level = mddev->level;
1216 sb->size = mddev->dev_sectors / 2;
1217 sb->raid_disks = mddev->raid_disks;
1218 sb->md_minor = mddev->md_minor;
1219 sb->not_persistent = 0;
1220 sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
1222 sb->events_hi = (mddev->events>>32);
1223 sb->events_lo = (u32)mddev->events;
1225 if (mddev->reshape_position == MaxSector)
1226 sb->minor_version = 90;
1228 sb->minor_version = 91;
1229 sb->reshape_position = mddev->reshape_position;
1230 sb->new_level = mddev->new_level;
1231 sb->delta_disks = mddev->delta_disks;
1232 sb->new_layout = mddev->new_layout;
1233 sb->new_chunk = mddev->new_chunk_sectors << 9;
1235 mddev->minor_version = sb->minor_version;
1238 sb->recovery_cp = mddev->recovery_cp;
1239 sb->cp_events_hi = (mddev->events>>32);
1240 sb->cp_events_lo = (u32)mddev->events;
1241 if (mddev->recovery_cp == MaxSector)
1242 sb->state = (1<< MD_SB_CLEAN);
1244 sb->recovery_cp = 0;
1246 sb->layout = mddev->layout;
1247 sb->chunk_size = mddev->chunk_sectors << 9;
1249 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
1250 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1252 sb->disks[0].state = (1<<MD_DISK_REMOVED);
1253 rdev_for_each(rdev2, mddev) {
1256 int is_active = test_bit(In_sync, &rdev2->flags);
1258 if (rdev2->raid_disk >= 0 &&
1259 sb->minor_version >= 91)
1260 /* we have nowhere to store the recovery_offset,
1261 * but if it is not below the reshape_position,
1262 * we can piggy-back on that.
1265 if (rdev2->raid_disk < 0 ||
1266 test_bit(Faulty, &rdev2->flags))
1269 desc_nr = rdev2->raid_disk;
1271 desc_nr = next_spare++;
1272 rdev2->desc_nr = desc_nr;
1273 d = &sb->disks[rdev2->desc_nr];
1275 d->number = rdev2->desc_nr;
1276 d->major = MAJOR(rdev2->bdev->bd_dev);
1277 d->minor = MINOR(rdev2->bdev->bd_dev);
1279 d->raid_disk = rdev2->raid_disk;
1281 d->raid_disk = rdev2->desc_nr; /* compatibility */
1282 if (test_bit(Faulty, &rdev2->flags))
1283 d->state = (1<<MD_DISK_FAULTY);
1284 else if (is_active) {
1285 d->state = (1<<MD_DISK_ACTIVE);
1286 if (test_bit(In_sync, &rdev2->flags))
1287 d->state |= (1<<MD_DISK_SYNC);
1295 if (test_bit(WriteMostly, &rdev2->flags))
1296 d->state |= (1<<MD_DISK_WRITEMOSTLY);
1297 if (test_bit(FailFast, &rdev2->flags))
1298 d->state |= (1<<MD_DISK_FAILFAST);
1300 /* now set the "removed" and "faulty" bits on any missing devices */
1301 for (i=0 ; i < mddev->raid_disks ; i++) {
1302 mdp_disk_t *d = &sb->disks[i];
1303 if (d->state == 0 && d->number == 0) {
1306 d->state = (1<<MD_DISK_REMOVED);
1307 d->state |= (1<<MD_DISK_FAULTY);
1311 sb->nr_disks = nr_disks;
1312 sb->active_disks = active;
1313 sb->working_disks = working;
1314 sb->failed_disks = failed;
1315 sb->spare_disks = spare;
1317 sb->this_disk = sb->disks[rdev->desc_nr];
1318 sb->sb_csum = calc_sb_csum(sb);
1322 * rdev_size_change for 0.90.0
1324 static unsigned long long
1325 super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
1327 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1328 return 0; /* component must fit device */
1329 if (rdev->mddev->bitmap_info.offset)
1330 return 0; /* can't move bitmap */
1331 rdev->sb_start = calc_dev_sboffset(rdev);
1332 if (!num_sectors || num_sectors > rdev->sb_start)
1333 num_sectors = rdev->sb_start;
1334 /* Limit to 4TB as metadata cannot record more than that.
1335 * 4TB == 2^32 KB, or 2*2^32 sectors.
1337 if (IS_ENABLED(CONFIG_LBDAF) && (u64)num_sectors >= (2ULL << 32) &&
1338 rdev->mddev->level >= 1)
1339 num_sectors = (sector_t)(2ULL << 32) - 2;
1341 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1343 } while (md_super_wait(rdev->mddev) < 0);
1348 super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1350 /* non-zero offset changes not possible with v0.90 */
1351 return new_offset == 0;
1355 * version 1 superblock
1358 static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
1362 unsigned long long newcsum;
1363 int size = 256 + le32_to_cpu(sb->max_dev)*2;
1364 __le32 *isuper = (__le32*)sb;
1366 disk_csum = sb->sb_csum;
1369 for (; size >= 4; size -= 4)
1370 newcsum += le32_to_cpu(*isuper++);
1373 newcsum += le16_to_cpu(*(__le16*) isuper);
1375 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1376 sb->sb_csum = disk_csum;
1377 return cpu_to_le32(csum);
1380 static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
1382 struct mdp_superblock_1 *sb;
1386 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1390 * Calculate the position of the superblock in 512byte sectors.
1391 * It is always aligned to a 4K boundary and
1392 * depeding on minor_version, it can be:
1393 * 0: At least 8K, but less than 12K, from end of device
1394 * 1: At start of device
1395 * 2: 4K from start of device.
1397 switch(minor_version) {
1399 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
1401 sb_start &= ~(sector_t)(4*2-1);
1412 rdev->sb_start = sb_start;
1414 /* superblock is rarely larger than 1K, but it can be larger,
1415 * and it is safe to read 4k, so we do that
1417 ret = read_disk_sb(rdev, 4096);
1418 if (ret) return ret;
1420 sb = page_address(rdev->sb_page);
1422 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1423 sb->major_version != cpu_to_le32(1) ||
1424 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1425 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1426 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1429 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1430 pr_warn("md: invalid superblock checksum on %s\n",
1431 bdevname(rdev->bdev,b));
1434 if (le64_to_cpu(sb->data_size) < 10) {
1435 pr_warn("md: data_size too small on %s\n",
1436 bdevname(rdev->bdev,b));
1441 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1442 /* Some padding is non-zero, might be a new feature */
1445 rdev->preferred_minor = 0xffff;
1446 rdev->data_offset = le64_to_cpu(sb->data_offset);
1447 rdev->new_data_offset = rdev->data_offset;
1448 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1449 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1450 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
1451 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1453 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1454 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1455 if (rdev->sb_size & bmask)
1456 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1459 && rdev->data_offset < sb_start + (rdev->sb_size/512))
1462 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1465 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1468 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1470 if (!rdev->bb_page) {
1471 rdev->bb_page = alloc_page(GFP_KERNEL);
1475 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1476 rdev->badblocks.count == 0) {
1477 /* need to load the bad block list.
1478 * Currently we limit it to one page.
1484 int sectors = le16_to_cpu(sb->bblog_size);
1485 if (sectors > (PAGE_SIZE / 512))
1487 offset = le32_to_cpu(sb->bblog_offset);
1490 bb_sector = (long long)offset;
1491 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1492 rdev->bb_page, REQ_OP_READ, 0, true))
1494 bbp = (u64 *)page_address(rdev->bb_page);
1495 rdev->badblocks.shift = sb->bblog_shift;
1496 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1497 u64 bb = le64_to_cpu(*bbp);
1498 int count = bb & (0x3ff);
1499 u64 sector = bb >> 10;
1500 sector <<= sb->bblog_shift;
1501 count <<= sb->bblog_shift;
1504 if (badblocks_set(&rdev->badblocks, sector, count, 1))
1507 } else if (sb->bblog_offset != 0)
1508 rdev->badblocks.shift = 0;
1514 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
1516 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1517 sb->level != refsb->level ||
1518 sb->layout != refsb->layout ||
1519 sb->chunksize != refsb->chunksize) {
1520 pr_warn("md: %s has strangely different superblock to %s\n",
1521 bdevname(rdev->bdev,b),
1522 bdevname(refdev->bdev,b2));
1525 ev1 = le64_to_cpu(sb->events);
1526 ev2 = le64_to_cpu(refsb->events);
1533 if (minor_version) {
1534 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1535 sectors -= rdev->data_offset;
1537 sectors = rdev->sb_start;
1538 if (sectors < le64_to_cpu(sb->data_size))
1540 rdev->sectors = le64_to_cpu(sb->data_size);
1544 static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
1546 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
1547 __u64 ev1 = le64_to_cpu(sb->events);
1549 rdev->raid_disk = -1;
1550 clear_bit(Faulty, &rdev->flags);
1551 clear_bit(In_sync, &rdev->flags);
1552 clear_bit(Bitmap_sync, &rdev->flags);
1553 clear_bit(WriteMostly, &rdev->flags);
1555 if (mddev->raid_disks == 0) {
1556 mddev->major_version = 1;
1557 mddev->patch_version = 0;
1558 mddev->external = 0;
1559 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
1560 mddev->ctime = le64_to_cpu(sb->ctime);
1561 mddev->utime = le64_to_cpu(sb->utime);
1562 mddev->level = le32_to_cpu(sb->level);
1563 mddev->clevel[0] = 0;
1564 mddev->layout = le32_to_cpu(sb->layout);
1565 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1566 mddev->dev_sectors = le64_to_cpu(sb->size);
1567 mddev->events = ev1;
1568 mddev->bitmap_info.offset = 0;
1569 mddev->bitmap_info.space = 0;
1570 /* Default location for bitmap is 1K after superblock
1571 * using 3K - total of 4K
1573 mddev->bitmap_info.default_offset = 1024 >> 9;
1574 mddev->bitmap_info.default_space = (4096-1024) >> 9;
1575 mddev->reshape_backwards = 0;
1577 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1578 memcpy(mddev->uuid, sb->set_uuid, 16);
1580 mddev->max_disks = (4096-256)/2;
1582 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1583 mddev->bitmap_info.file == NULL) {
1584 mddev->bitmap_info.offset =
1585 (__s32)le32_to_cpu(sb->bitmap_offset);
1586 /* Metadata doesn't record how much space is available.
1587 * For 1.0, we assume we can use up to the superblock
1588 * if before, else to 4K beyond superblock.
1589 * For others, assume no change is possible.
1591 if (mddev->minor_version > 0)
1592 mddev->bitmap_info.space = 0;
1593 else if (mddev->bitmap_info.offset > 0)
1594 mddev->bitmap_info.space =
1595 8 - mddev->bitmap_info.offset;
1597 mddev->bitmap_info.space =
1598 -mddev->bitmap_info.offset;
1601 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1602 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1603 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1604 mddev->new_level = le32_to_cpu(sb->new_level);
1605 mddev->new_layout = le32_to_cpu(sb->new_layout);
1606 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
1607 if (mddev->delta_disks < 0 ||
1608 (mddev->delta_disks == 0 &&
1609 (le32_to_cpu(sb->feature_map)
1610 & MD_FEATURE_RESHAPE_BACKWARDS)))
1611 mddev->reshape_backwards = 1;
1613 mddev->reshape_position = MaxSector;
1614 mddev->delta_disks = 0;
1615 mddev->new_level = mddev->level;
1616 mddev->new_layout = mddev->layout;
1617 mddev->new_chunk_sectors = mddev->chunk_sectors;
1620 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
1621 set_bit(MD_HAS_JOURNAL, &mddev->flags);
1622 } else if (mddev->pers == NULL) {
1623 /* Insist of good event counter while assembling, except for
1624 * spares (which don't need an event count) */
1626 if (rdev->desc_nr >= 0 &&
1627 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1628 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1629 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
1630 if (ev1 < mddev->events)
1632 } else if (mddev->bitmap) {
1633 /* If adding to array with a bitmap, then we can accept an
1634 * older device, but not too old.
1636 if (ev1 < mddev->bitmap->events_cleared)
1638 if (ev1 < mddev->events)
1639 set_bit(Bitmap_sync, &rdev->flags);
1641 if (ev1 < mddev->events)
1642 /* just a hot-add of a new device, leave raid_disk at -1 */
1645 if (mddev->level != LEVEL_MULTIPATH) {
1647 if (rdev->desc_nr < 0 ||
1648 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1649 role = MD_DISK_ROLE_SPARE;
1652 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1654 case MD_DISK_ROLE_SPARE: /* spare */
1656 case MD_DISK_ROLE_FAULTY: /* faulty */
1657 set_bit(Faulty, &rdev->flags);
1659 case MD_DISK_ROLE_JOURNAL: /* journal device */
1660 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1661 /* journal device without journal feature */
1662 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
1665 set_bit(Journal, &rdev->flags);
1666 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
1667 rdev->raid_disk = 0;
1670 rdev->saved_raid_disk = role;
1671 if ((le32_to_cpu(sb->feature_map) &
1672 MD_FEATURE_RECOVERY_OFFSET)) {
1673 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1674 if (!(le32_to_cpu(sb->feature_map) &
1675 MD_FEATURE_RECOVERY_BITMAP))
1676 rdev->saved_raid_disk = -1;
1678 set_bit(In_sync, &rdev->flags);
1679 rdev->raid_disk = role;
1682 if (sb->devflags & WriteMostly1)
1683 set_bit(WriteMostly, &rdev->flags);
1684 if (sb->devflags & FailFast1)
1685 set_bit(FailFast, &rdev->flags);
1686 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1687 set_bit(Replacement, &rdev->flags);
1688 } else /* MULTIPATH are always insync */
1689 set_bit(In_sync, &rdev->flags);
1694 static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
1696 struct mdp_superblock_1 *sb;
1697 struct md_rdev *rdev2;
1699 /* make rdev->sb match mddev and rdev data. */
1701 sb = page_address(rdev->sb_page);
1703 sb->feature_map = 0;
1705 sb->recovery_offset = cpu_to_le64(0);
1706 memset(sb->pad3, 0, sizeof(sb->pad3));
1708 sb->utime = cpu_to_le64((__u64)mddev->utime);
1709 sb->events = cpu_to_le64(mddev->events);
1711 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1712 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
1713 sb->resync_offset = cpu_to_le64(MaxSector);
1715 sb->resync_offset = cpu_to_le64(0);
1717 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1719 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1720 sb->size = cpu_to_le64(mddev->dev_sectors);
1721 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
1722 sb->level = cpu_to_le32(mddev->level);
1723 sb->layout = cpu_to_le32(mddev->layout);
1724 if (test_bit(FailFast, &rdev->flags))
1725 sb->devflags |= FailFast1;
1727 sb->devflags &= ~FailFast1;
1729 if (test_bit(WriteMostly, &rdev->flags))
1730 sb->devflags |= WriteMostly1;
1732 sb->devflags &= ~WriteMostly1;
1733 sb->data_offset = cpu_to_le64(rdev->data_offset);
1734 sb->data_size = cpu_to_le64(rdev->sectors);
1736 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1737 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
1738 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1741 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
1742 !test_bit(In_sync, &rdev->flags)) {
1744 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1745 sb->recovery_offset =
1746 cpu_to_le64(rdev->recovery_offset);
1747 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1749 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
1751 /* Note: recovery_offset and journal_tail share space */
1752 if (test_bit(Journal, &rdev->flags))
1753 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
1754 if (test_bit(Replacement, &rdev->flags))
1756 cpu_to_le32(MD_FEATURE_REPLACEMENT);
1758 if (mddev->reshape_position != MaxSector) {
1759 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1760 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1761 sb->new_layout = cpu_to_le32(mddev->new_layout);
1762 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1763 sb->new_level = cpu_to_le32(mddev->new_level);
1764 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
1765 if (mddev->delta_disks == 0 &&
1766 mddev->reshape_backwards)
1768 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
1769 if (rdev->new_data_offset != rdev->data_offset) {
1771 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1772 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1773 - rdev->data_offset));
1777 if (mddev_is_clustered(mddev))
1778 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
1780 if (rdev->badblocks.count == 0)
1781 /* Nothing to do for bad blocks*/ ;
1782 else if (sb->bblog_offset == 0)
1783 /* Cannot record bad blocks on this device */
1784 md_error(mddev, rdev);
1786 struct badblocks *bb = &rdev->badblocks;
1787 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1789 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1794 seq = read_seqbegin(&bb->lock);
1796 memset(bbp, 0xff, PAGE_SIZE);
1798 for (i = 0 ; i < bb->count ; i++) {
1799 u64 internal_bb = p[i];
1800 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1801 | BB_LEN(internal_bb));
1802 bbp[i] = cpu_to_le64(store_bb);
1805 if (read_seqretry(&bb->lock, seq))
1808 bb->sector = (rdev->sb_start +
1809 (int)le32_to_cpu(sb->bblog_offset));
1810 bb->size = le16_to_cpu(sb->bblog_size);
1815 rdev_for_each(rdev2, mddev)
1816 if (rdev2->desc_nr+1 > max_dev)
1817 max_dev = rdev2->desc_nr+1;
1819 if (max_dev > le32_to_cpu(sb->max_dev)) {
1821 sb->max_dev = cpu_to_le32(max_dev);
1822 rdev->sb_size = max_dev * 2 + 256;
1823 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1824 if (rdev->sb_size & bmask)
1825 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1827 max_dev = le32_to_cpu(sb->max_dev);
1829 for (i=0; i<max_dev;i++)
1830 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
1832 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
1833 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
1835 rdev_for_each(rdev2, mddev) {
1837 if (test_bit(Faulty, &rdev2->flags))
1838 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
1839 else if (test_bit(In_sync, &rdev2->flags))
1840 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1841 else if (test_bit(Journal, &rdev2->flags))
1842 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
1843 else if (rdev2->raid_disk >= 0)
1844 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1846 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
1849 sb->sb_csum = calc_sb_1_csum(sb);
1852 static unsigned long long
1853 super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
1855 struct mdp_superblock_1 *sb;
1856 sector_t max_sectors;
1857 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1858 return 0; /* component must fit device */
1859 if (rdev->data_offset != rdev->new_data_offset)
1860 return 0; /* too confusing */
1861 if (rdev->sb_start < rdev->data_offset) {
1862 /* minor versions 1 and 2; superblock before data */
1863 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
1864 max_sectors -= rdev->data_offset;
1865 if (!num_sectors || num_sectors > max_sectors)
1866 num_sectors = max_sectors;
1867 } else if (rdev->mddev->bitmap_info.offset) {
1868 /* minor version 0 with bitmap we can't move */
1871 /* minor version 0; superblock after data */
1873 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
1874 sb_start &= ~(sector_t)(4*2 - 1);
1875 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
1876 if (!num_sectors || num_sectors > max_sectors)
1877 num_sectors = max_sectors;
1878 rdev->sb_start = sb_start;
1880 sb = page_address(rdev->sb_page);
1881 sb->data_size = cpu_to_le64(num_sectors);
1882 sb->super_offset = cpu_to_le64(rdev->sb_start);
1883 sb->sb_csum = calc_sb_1_csum(sb);
1885 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1887 } while (md_super_wait(rdev->mddev) < 0);
1893 super_1_allow_new_offset(struct md_rdev *rdev,
1894 unsigned long long new_offset)
1896 /* All necessary checks on new >= old have been done */
1897 struct bitmap *bitmap;
1898 if (new_offset >= rdev->data_offset)
1901 /* with 1.0 metadata, there is no metadata to tread on
1902 * so we can always move back */
1903 if (rdev->mddev->minor_version == 0)
1906 /* otherwise we must be sure not to step on
1907 * any metadata, so stay:
1908 * 36K beyond start of superblock
1909 * beyond end of badblocks
1910 * beyond write-intent bitmap
1912 if (rdev->sb_start + (32+4)*2 > new_offset)
1914 bitmap = rdev->mddev->bitmap;
1915 if (bitmap && !rdev->mddev->bitmap_info.file &&
1916 rdev->sb_start + rdev->mddev->bitmap_info.offset +
1917 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
1919 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1925 static struct super_type super_types[] = {
1928 .owner = THIS_MODULE,
1929 .load_super = super_90_load,
1930 .validate_super = super_90_validate,
1931 .sync_super = super_90_sync,
1932 .rdev_size_change = super_90_rdev_size_change,
1933 .allow_new_offset = super_90_allow_new_offset,
1937 .owner = THIS_MODULE,
1938 .load_super = super_1_load,
1939 .validate_super = super_1_validate,
1940 .sync_super = super_1_sync,
1941 .rdev_size_change = super_1_rdev_size_change,
1942 .allow_new_offset = super_1_allow_new_offset,
1946 static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
1948 if (mddev->sync_super) {
1949 mddev->sync_super(mddev, rdev);
1953 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1955 super_types[mddev->major_version].sync_super(mddev, rdev);
1958 static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
1960 struct md_rdev *rdev, *rdev2;
1963 rdev_for_each_rcu(rdev, mddev1) {
1964 if (test_bit(Faulty, &rdev->flags) ||
1965 test_bit(Journal, &rdev->flags) ||
1966 rdev->raid_disk == -1)
1968 rdev_for_each_rcu(rdev2, mddev2) {
1969 if (test_bit(Faulty, &rdev2->flags) ||
1970 test_bit(Journal, &rdev2->flags) ||
1971 rdev2->raid_disk == -1)
1973 if (rdev->bdev->bd_contains ==
1974 rdev2->bdev->bd_contains) {
1984 static LIST_HEAD(pending_raid_disks);
1987 * Try to register data integrity profile for an mddev
1989 * This is called when an array is started and after a disk has been kicked
1990 * from the array. It only succeeds if all working and active component devices
1991 * are integrity capable with matching profiles.
1993 int md_integrity_register(struct mddev *mddev)
1995 struct md_rdev *rdev, *reference = NULL;
1997 if (list_empty(&mddev->disks))
1998 return 0; /* nothing to do */
1999 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2000 return 0; /* shouldn't register, or already is */
2001 rdev_for_each(rdev, mddev) {
2002 /* skip spares and non-functional disks */
2003 if (test_bit(Faulty, &rdev->flags))
2005 if (rdev->raid_disk < 0)
2008 /* Use the first rdev as the reference */
2012 /* does this rdev's profile match the reference profile? */
2013 if (blk_integrity_compare(reference->bdev->bd_disk,
2014 rdev->bdev->bd_disk) < 0)
2017 if (!reference || !bdev_get_integrity(reference->bdev))
2020 * All component devices are integrity capable and have matching
2021 * profiles, register the common profile for the md device.
2023 blk_integrity_register(mddev->gendisk,
2024 bdev_get_integrity(reference->bdev));
2026 pr_debug("md: data integrity enabled on %s\n", mdname(mddev));
2027 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
2028 pr_err("md: failed to create integrity pool for %s\n",
2034 EXPORT_SYMBOL(md_integrity_register);
2037 * Attempt to add an rdev, but only if it is consistent with the current
2040 int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
2042 struct blk_integrity *bi_rdev;
2043 struct blk_integrity *bi_mddev;
2044 char name[BDEVNAME_SIZE];
2046 if (!mddev->gendisk)
2049 bi_rdev = bdev_get_integrity(rdev->bdev);
2050 bi_mddev = blk_get_integrity(mddev->gendisk);
2052 if (!bi_mddev) /* nothing to do */
2055 if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
2056 pr_err("%s: incompatible integrity profile for %s\n",
2057 mdname(mddev), bdevname(rdev->bdev, name));
2063 EXPORT_SYMBOL(md_integrity_add_rdev);
2065 static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
2067 char b[BDEVNAME_SIZE];
2071 /* prevent duplicates */
2072 if (find_rdev(mddev, rdev->bdev->bd_dev))
2075 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2076 if (!test_bit(Journal, &rdev->flags) &&
2078 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
2080 /* Cannot change size, so fail
2081 * If mddev->level <= 0, then we don't care
2082 * about aligning sizes (e.g. linear)
2084 if (mddev->level > 0)
2087 mddev->dev_sectors = rdev->sectors;
2090 /* Verify rdev->desc_nr is unique.
2091 * If it is -1, assign a free number, else
2092 * check number is not in use
2095 if (rdev->desc_nr < 0) {
2098 choice = mddev->raid_disks;
2099 while (md_find_rdev_nr_rcu(mddev, choice))
2101 rdev->desc_nr = choice;
2103 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
2109 if (!test_bit(Journal, &rdev->flags) &&
2110 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2111 pr_warn("md: %s: array is limited to %d devices\n",
2112 mdname(mddev), mddev->max_disks);
2115 bdevname(rdev->bdev,b);
2116 strreplace(b, '/', '!');
2118 rdev->mddev = mddev;
2119 pr_debug("md: bind<%s>\n", b);
2121 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
2124 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
2125 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2126 /* failure here is OK */;
2127 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
2129 list_add_rcu(&rdev->same_set, &mddev->disks);
2130 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
2132 /* May as well allow recovery to be retried once */
2133 mddev->recovery_disabled++;
2138 pr_warn("md: failed to register dev-%s for %s\n",
2143 static void md_delayed_delete(struct work_struct *ws)
2145 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
2146 kobject_del(&rdev->kobj);
2147 kobject_put(&rdev->kobj);
2150 static void unbind_rdev_from_array(struct md_rdev *rdev)
2152 char b[BDEVNAME_SIZE];
2154 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
2155 list_del_rcu(&rdev->same_set);
2156 pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b));
2158 sysfs_remove_link(&rdev->kobj, "block");
2159 sysfs_put(rdev->sysfs_state);
2160 rdev->sysfs_state = NULL;
2161 rdev->badblocks.count = 0;
2162 /* We need to delay this, otherwise we can deadlock when
2163 * writing to 'remove' to "dev/state". We also need
2164 * to delay it due to rcu usage.
2167 INIT_WORK(&rdev->del_work, md_delayed_delete);
2168 kobject_get(&rdev->kobj);
2169 queue_work(md_misc_wq, &rdev->del_work);
2173 * prevent the device from being mounted, repartitioned or
2174 * otherwise reused by a RAID array (or any other kernel
2175 * subsystem), by bd_claiming the device.
2177 static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
2180 struct block_device *bdev;
2181 char b[BDEVNAME_SIZE];
2183 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
2184 shared ? (struct md_rdev *)lock_rdev : rdev);
2186 pr_warn("md: could not open %s.\n", __bdevname(dev, b));
2187 return PTR_ERR(bdev);
2193 static void unlock_rdev(struct md_rdev *rdev)
2195 struct block_device *bdev = rdev->bdev;
2197 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
2200 void md_autodetect_dev(dev_t dev);
2202 static void export_rdev(struct md_rdev *rdev)
2204 char b[BDEVNAME_SIZE];
2206 pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b));
2207 md_rdev_clear(rdev);
2209 if (test_bit(AutoDetected, &rdev->flags))
2210 md_autodetect_dev(rdev->bdev->bd_dev);
2213 kobject_put(&rdev->kobj);
2216 void md_kick_rdev_from_array(struct md_rdev *rdev)
2218 unbind_rdev_from_array(rdev);
2221 EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
2223 static void export_array(struct mddev *mddev)
2225 struct md_rdev *rdev;
2227 while (!list_empty(&mddev->disks)) {
2228 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2230 md_kick_rdev_from_array(rdev);
2232 mddev->raid_disks = 0;
2233 mddev->major_version = 0;
2236 static void sync_sbs(struct mddev *mddev, int nospares)
2238 /* Update each superblock (in-memory image), but
2239 * if we are allowed to, skip spares which already
2240 * have the right event counter, or have one earlier
2241 * (which would mean they aren't being marked as dirty
2242 * with the rest of the array)
2244 struct md_rdev *rdev;
2245 rdev_for_each(rdev, mddev) {
2246 if (rdev->sb_events == mddev->events ||
2248 rdev->raid_disk < 0 &&
2249 rdev->sb_events+1 == mddev->events)) {
2250 /* Don't update this superblock */
2251 rdev->sb_loaded = 2;
2253 sync_super(mddev, rdev);
2254 rdev->sb_loaded = 1;
2259 static bool does_sb_need_changing(struct mddev *mddev)
2261 struct md_rdev *rdev;
2262 struct mdp_superblock_1 *sb;
2265 /* Find a good rdev */
2266 rdev_for_each(rdev, mddev)
2267 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2270 /* No good device found. */
2274 sb = page_address(rdev->sb_page);
2275 /* Check if a device has become faulty or a spare become active */
2276 rdev_for_each(rdev, mddev) {
2277 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2278 /* Device activated? */
2279 if (role == 0xffff && rdev->raid_disk >=0 &&
2280 !test_bit(Faulty, &rdev->flags))
2282 /* Device turned faulty? */
2283 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2287 /* Check if any mddev parameters have changed */
2288 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2289 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
2290 (mddev->layout != le32_to_cpu(sb->layout)) ||
2291 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2292 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2298 void md_update_sb(struct mddev *mddev, int force_change)
2300 struct md_rdev *rdev;
2303 int any_badblocks_changed = 0;
2308 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2313 if (mddev_is_clustered(mddev)) {
2314 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
2316 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
2318 ret = md_cluster_ops->metadata_update_start(mddev);
2319 /* Has someone else has updated the sb */
2320 if (!does_sb_need_changing(mddev)) {
2322 md_cluster_ops->metadata_update_cancel(mddev);
2323 bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2324 BIT(MD_SB_CHANGE_DEVS) |
2325 BIT(MD_SB_CHANGE_CLEAN));
2330 /* First make sure individual recovery_offsets are correct */
2331 rdev_for_each(rdev, mddev) {
2332 if (rdev->raid_disk >= 0 &&
2333 mddev->delta_disks >= 0 &&
2334 !test_bit(Journal, &rdev->flags) &&
2335 !test_bit(In_sync, &rdev->flags) &&
2336 mddev->curr_resync_completed > rdev->recovery_offset)
2337 rdev->recovery_offset = mddev->curr_resync_completed;
2340 if (!mddev->persistent) {
2341 clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2342 clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2343 if (!mddev->external) {
2344 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
2345 rdev_for_each(rdev, mddev) {
2346 if (rdev->badblocks.changed) {
2347 rdev->badblocks.changed = 0;
2348 ack_all_badblocks(&rdev->badblocks);
2349 md_error(mddev, rdev);
2351 clear_bit(Blocked, &rdev->flags);
2352 clear_bit(BlockedBadBlocks, &rdev->flags);
2353 wake_up(&rdev->blocked_wait);
2356 wake_up(&mddev->sb_wait);
2360 spin_lock(&mddev->lock);
2362 mddev->utime = ktime_get_real_seconds();
2364 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
2366 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
2367 /* just a clean<-> dirty transition, possibly leave spares alone,
2368 * though if events isn't the right even/odd, we will have to do
2374 if (mddev->degraded)
2375 /* If the array is degraded, then skipping spares is both
2376 * dangerous and fairly pointless.
2377 * Dangerous because a device that was removed from the array
2378 * might have a event_count that still looks up-to-date,
2379 * so it can be re-added without a resync.
2380 * Pointless because if there are any spares to skip,
2381 * then a recovery will happen and soon that array won't
2382 * be degraded any more and the spare can go back to sleep then.
2386 sync_req = mddev->in_sync;
2388 /* If this is just a dirty<->clean transition, and the array is clean
2389 * and 'events' is odd, we can roll back to the previous clean state */
2391 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
2392 && mddev->can_decrease_events
2393 && mddev->events != 1) {
2395 mddev->can_decrease_events = 0;
2397 /* otherwise we have to go forward and ... */
2399 mddev->can_decrease_events = nospares;
2403 * This 64-bit counter should never wrap.
2404 * Either we are in around ~1 trillion A.C., assuming
2405 * 1 reboot per second, or we have a bug...
2407 WARN_ON(mddev->events == 0);
2409 rdev_for_each(rdev, mddev) {
2410 if (rdev->badblocks.changed)
2411 any_badblocks_changed++;
2412 if (test_bit(Faulty, &rdev->flags))
2413 set_bit(FaultRecorded, &rdev->flags);
2416 sync_sbs(mddev, nospares);
2417 spin_unlock(&mddev->lock);
2419 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2420 mdname(mddev), mddev->in_sync);
2423 blk_add_trace_msg(mddev->queue, "md md_update_sb");
2425 bitmap_update_sb(mddev->bitmap);
2426 rdev_for_each(rdev, mddev) {
2427 char b[BDEVNAME_SIZE];
2429 if (rdev->sb_loaded != 1)
2430 continue; /* no noise on spare devices */
2432 if (!test_bit(Faulty, &rdev->flags)) {
2433 md_super_write(mddev,rdev,
2434 rdev->sb_start, rdev->sb_size,
2436 pr_debug("md: (write) %s's sb offset: %llu\n",
2437 bdevname(rdev->bdev, b),
2438 (unsigned long long)rdev->sb_start);
2439 rdev->sb_events = mddev->events;
2440 if (rdev->badblocks.size) {
2441 md_super_write(mddev, rdev,
2442 rdev->badblocks.sector,
2443 rdev->badblocks.size << 9,
2445 rdev->badblocks.size = 0;
2449 pr_debug("md: %s (skipping faulty)\n",
2450 bdevname(rdev->bdev, b));
2452 if (mddev->level == LEVEL_MULTIPATH)
2453 /* only need to write one superblock... */
2456 if (md_super_wait(mddev) < 0)
2458 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
2460 if (mddev_is_clustered(mddev) && ret == 0)
2461 md_cluster_ops->metadata_update_finish(mddev);
2463 if (mddev->in_sync != sync_req ||
2464 !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2465 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN)))
2466 /* have to write it out again */
2468 wake_up(&mddev->sb_wait);
2469 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2470 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
2472 rdev_for_each(rdev, mddev) {
2473 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2474 clear_bit(Blocked, &rdev->flags);
2476 if (any_badblocks_changed)
2477 ack_all_badblocks(&rdev->badblocks);
2478 clear_bit(BlockedBadBlocks, &rdev->flags);
2479 wake_up(&rdev->blocked_wait);
2482 EXPORT_SYMBOL(md_update_sb);
2484 static int add_bound_rdev(struct md_rdev *rdev)
2486 struct mddev *mddev = rdev->mddev;
2488 bool add_journal = test_bit(Journal, &rdev->flags);
2490 if (!mddev->pers->hot_remove_disk || add_journal) {
2491 /* If there is hot_add_disk but no hot_remove_disk
2492 * then added disks for geometry changes,
2493 * and should be added immediately.
2495 super_types[mddev->major_version].
2496 validate_super(mddev, rdev);
2498 mddev_suspend(mddev);
2499 err = mddev->pers->hot_add_disk(mddev, rdev);
2501 mddev_resume(mddev);
2503 md_kick_rdev_from_array(rdev);
2507 sysfs_notify_dirent_safe(rdev->sysfs_state);
2509 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2510 if (mddev->degraded)
2511 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2512 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2513 md_new_event(mddev);
2514 md_wakeup_thread(mddev->thread);
2518 /* words written to sysfs files may, or may not, be \n terminated.
2519 * We want to accept with case. For this we use cmd_match.
2521 static int cmd_match(const char *cmd, const char *str)
2523 /* See if cmd, written into a sysfs file, matches
2524 * str. They must either be the same, or cmd can
2525 * have a trailing newline
2527 while (*cmd && *str && *cmd == *str) {
2538 struct rdev_sysfs_entry {
2539 struct attribute attr;
2540 ssize_t (*show)(struct md_rdev *, char *);
2541 ssize_t (*store)(struct md_rdev *, const char *, size_t);
2545 state_show(struct md_rdev *rdev, char *page)
2549 unsigned long flags = ACCESS_ONCE(rdev->flags);
2551 if (test_bit(Faulty, &flags) ||
2552 (!test_bit(ExternalBbl, &flags) &&
2553 rdev->badblocks.unacked_exist))
2554 len += sprintf(page+len, "faulty%s", sep);
2555 if (test_bit(In_sync, &flags))
2556 len += sprintf(page+len, "in_sync%s", sep);
2557 if (test_bit(Journal, &flags))
2558 len += sprintf(page+len, "journal%s", sep);
2559 if (test_bit(WriteMostly, &flags))
2560 len += sprintf(page+len, "write_mostly%s", sep);
2561 if (test_bit(Blocked, &flags) ||
2562 (rdev->badblocks.unacked_exist
2563 && !test_bit(Faulty, &flags)))
2564 len += sprintf(page+len, "blocked%s", sep);
2565 if (!test_bit(Faulty, &flags) &&
2566 !test_bit(Journal, &flags) &&
2567 !test_bit(In_sync, &flags))
2568 len += sprintf(page+len, "spare%s", sep);
2569 if (test_bit(WriteErrorSeen, &flags))
2570 len += sprintf(page+len, "write_error%s", sep);
2571 if (test_bit(WantReplacement, &flags))
2572 len += sprintf(page+len, "want_replacement%s", sep);
2573 if (test_bit(Replacement, &flags))
2574 len += sprintf(page+len, "replacement%s", sep);
2575 if (test_bit(ExternalBbl, &flags))
2576 len += sprintf(page+len, "external_bbl%s", sep);
2577 if (test_bit(FailFast, &flags))
2578 len += sprintf(page+len, "failfast%s", sep);
2583 return len+sprintf(page+len, "\n");
2587 state_store(struct md_rdev *rdev, const char *buf, size_t len)
2590 * faulty - simulates an error
2591 * remove - disconnects the device
2592 * writemostly - sets write_mostly
2593 * -writemostly - clears write_mostly
2594 * blocked - sets the Blocked flags
2595 * -blocked - clears the Blocked and possibly simulates an error
2596 * insync - sets Insync providing device isn't active
2597 * -insync - clear Insync for a device with a slot assigned,
2598 * so that it gets rebuilt based on bitmap
2599 * write_error - sets WriteErrorSeen
2600 * -write_error - clears WriteErrorSeen
2601 * {,-}failfast - set/clear FailFast
2604 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2605 md_error(rdev->mddev, rdev);
2606 if (test_bit(Faulty, &rdev->flags))
2610 } else if (cmd_match(buf, "remove")) {
2611 if (rdev->mddev->pers) {
2612 clear_bit(Blocked, &rdev->flags);
2613 remove_and_add_spares(rdev->mddev, rdev);
2615 if (rdev->raid_disk >= 0)
2618 struct mddev *mddev = rdev->mddev;
2620 if (mddev_is_clustered(mddev))
2621 err = md_cluster_ops->remove_disk(mddev, rdev);
2624 md_kick_rdev_from_array(rdev);
2626 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2627 md_wakeup_thread(mddev->thread);
2629 md_new_event(mddev);
2632 } else if (cmd_match(buf, "writemostly")) {
2633 set_bit(WriteMostly, &rdev->flags);
2635 } else if (cmd_match(buf, "-writemostly")) {
2636 clear_bit(WriteMostly, &rdev->flags);
2638 } else if (cmd_match(buf, "blocked")) {
2639 set_bit(Blocked, &rdev->flags);
2641 } else if (cmd_match(buf, "-blocked")) {
2642 if (!test_bit(Faulty, &rdev->flags) &&
2643 !test_bit(ExternalBbl, &rdev->flags) &&
2644 rdev->badblocks.unacked_exist) {
2645 /* metadata handler doesn't understand badblocks,
2646 * so we need to fail the device
2648 md_error(rdev->mddev, rdev);
2650 clear_bit(Blocked, &rdev->flags);
2651 clear_bit(BlockedBadBlocks, &rdev->flags);
2652 wake_up(&rdev->blocked_wait);
2653 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2654 md_wakeup_thread(rdev->mddev->thread);
2657 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2658 set_bit(In_sync, &rdev->flags);
2660 } else if (cmd_match(buf, "failfast")) {
2661 set_bit(FailFast, &rdev->flags);
2663 } else if (cmd_match(buf, "-failfast")) {
2664 clear_bit(FailFast, &rdev->flags);
2666 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
2667 !test_bit(Journal, &rdev->flags)) {
2668 if (rdev->mddev->pers == NULL) {
2669 clear_bit(In_sync, &rdev->flags);
2670 rdev->saved_raid_disk = rdev->raid_disk;
2671 rdev->raid_disk = -1;
2674 } else if (cmd_match(buf, "write_error")) {
2675 set_bit(WriteErrorSeen, &rdev->flags);
2677 } else if (cmd_match(buf, "-write_error")) {
2678 clear_bit(WriteErrorSeen, &rdev->flags);
2680 } else if (cmd_match(buf, "want_replacement")) {
2681 /* Any non-spare device that is not a replacement can
2682 * become want_replacement at any time, but we then need to
2683 * check if recovery is needed.
2685 if (rdev->raid_disk >= 0 &&
2686 !test_bit(Journal, &rdev->flags) &&
2687 !test_bit(Replacement, &rdev->flags))
2688 set_bit(WantReplacement, &rdev->flags);
2689 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2690 md_wakeup_thread(rdev->mddev->thread);
2692 } else if (cmd_match(buf, "-want_replacement")) {
2693 /* Clearing 'want_replacement' is always allowed.
2694 * Once replacements starts it is too late though.
2697 clear_bit(WantReplacement, &rdev->flags);
2698 } else if (cmd_match(buf, "replacement")) {
2699 /* Can only set a device as a replacement when array has not
2700 * yet been started. Once running, replacement is automatic
2701 * from spares, or by assigning 'slot'.
2703 if (rdev->mddev->pers)
2706 set_bit(Replacement, &rdev->flags);
2709 } else if (cmd_match(buf, "-replacement")) {
2710 /* Similarly, can only clear Replacement before start */
2711 if (rdev->mddev->pers)
2714 clear_bit(Replacement, &rdev->flags);
2717 } else if (cmd_match(buf, "re-add")) {
2718 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) {
2719 /* clear_bit is performed _after_ all the devices
2720 * have their local Faulty bit cleared. If any writes
2721 * happen in the meantime in the local node, they
2722 * will land in the local bitmap, which will be synced
2723 * by this node eventually
2725 if (!mddev_is_clustered(rdev->mddev) ||
2726 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2727 clear_bit(Faulty, &rdev->flags);
2728 err = add_bound_rdev(rdev);
2732 } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) {
2733 set_bit(ExternalBbl, &rdev->flags);
2734 rdev->badblocks.shift = 0;
2736 } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) {
2737 clear_bit(ExternalBbl, &rdev->flags);
2741 sysfs_notify_dirent_safe(rdev->sysfs_state);
2742 return err ? err : len;
2744 static struct rdev_sysfs_entry rdev_state =
2745 __ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
2748 errors_show(struct md_rdev *rdev, char *page)
2750 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2754 errors_store(struct md_rdev *rdev, const char *buf, size_t len)
2759 rv = kstrtouint(buf, 10, &n);
2762 atomic_set(&rdev->corrected_errors, n);
2765 static struct rdev_sysfs_entry rdev_errors =
2766 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
2769 slot_show(struct md_rdev *rdev, char *page)
2771 if (test_bit(Journal, &rdev->flags))
2772 return sprintf(page, "journal\n");
2773 else if (rdev->raid_disk < 0)
2774 return sprintf(page, "none\n");
2776 return sprintf(page, "%d\n", rdev->raid_disk);
2780 slot_store(struct md_rdev *rdev, const char *buf, size_t len)
2785 if (test_bit(Journal, &rdev->flags))
2787 if (strncmp(buf, "none", 4)==0)
2790 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2794 if (rdev->mddev->pers && slot == -1) {
2795 /* Setting 'slot' on an active array requires also
2796 * updating the 'rd%d' link, and communicating
2797 * with the personality with ->hot_*_disk.
2798 * For now we only support removing
2799 * failed/spare devices. This normally happens automatically,
2800 * but not when the metadata is externally managed.
2802 if (rdev->raid_disk == -1)
2804 /* personality does all needed checks */
2805 if (rdev->mddev->pers->hot_remove_disk == NULL)
2807 clear_bit(Blocked, &rdev->flags);
2808 remove_and_add_spares(rdev->mddev, rdev);
2809 if (rdev->raid_disk >= 0)
2811 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2812 md_wakeup_thread(rdev->mddev->thread);
2813 } else if (rdev->mddev->pers) {
2814 /* Activating a spare .. or possibly reactivating
2815 * if we ever get bitmaps working here.
2819 if (rdev->raid_disk != -1)
2822 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2825 if (rdev->mddev->pers->hot_add_disk == NULL)
2828 if (slot >= rdev->mddev->raid_disks &&
2829 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2832 rdev->raid_disk = slot;
2833 if (test_bit(In_sync, &rdev->flags))
2834 rdev->saved_raid_disk = slot;
2836 rdev->saved_raid_disk = -1;
2837 clear_bit(In_sync, &rdev->flags);
2838 clear_bit(Bitmap_sync, &rdev->flags);
2839 err = rdev->mddev->pers->
2840 hot_add_disk(rdev->mddev, rdev);
2842 rdev->raid_disk = -1;
2845 sysfs_notify_dirent_safe(rdev->sysfs_state);
2846 if (sysfs_link_rdev(rdev->mddev, rdev))
2847 /* failure here is OK */;
2848 /* don't wakeup anyone, leave that to userspace. */
2850 if (slot >= rdev->mddev->raid_disks &&
2851 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2853 rdev->raid_disk = slot;
2854 /* assume it is working */
2855 clear_bit(Faulty, &rdev->flags);
2856 clear_bit(WriteMostly, &rdev->flags);
2857 set_bit(In_sync, &rdev->flags);
2858 sysfs_notify_dirent_safe(rdev->sysfs_state);
2863 static struct rdev_sysfs_entry rdev_slot =
2864 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
2867 offset_show(struct md_rdev *rdev, char *page)
2869 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
2873 offset_store(struct md_rdev *rdev, const char *buf, size_t len)
2875 unsigned long long offset;
2876 if (kstrtoull(buf, 10, &offset) < 0)
2878 if (rdev->mddev->pers && rdev->raid_disk >= 0)
2880 if (rdev->sectors && rdev->mddev->external)
2881 /* Must set offset before size, so overlap checks
2884 rdev->data_offset = offset;
2885 rdev->new_data_offset = offset;
2889 static struct rdev_sysfs_entry rdev_offset =
2890 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
2892 static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2894 return sprintf(page, "%llu\n",
2895 (unsigned long long)rdev->new_data_offset);
2898 static ssize_t new_offset_store(struct md_rdev *rdev,
2899 const char *buf, size_t len)
2901 unsigned long long new_offset;
2902 struct mddev *mddev = rdev->mddev;
2904 if (kstrtoull(buf, 10, &new_offset) < 0)
2907 if (mddev->sync_thread ||
2908 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
2910 if (new_offset == rdev->data_offset)
2911 /* reset is always permitted */
2913 else if (new_offset > rdev->data_offset) {
2914 /* must not push array size beyond rdev_sectors */
2915 if (new_offset - rdev->data_offset
2916 + mddev->dev_sectors > rdev->sectors)
2919 /* Metadata worries about other space details. */
2921 /* decreasing the offset is inconsistent with a backwards
2924 if (new_offset < rdev->data_offset &&
2925 mddev->reshape_backwards)
2927 /* Increasing offset is inconsistent with forwards
2928 * reshape. reshape_direction should be set to
2929 * 'backwards' first.
2931 if (new_offset > rdev->data_offset &&
2932 !mddev->reshape_backwards)
2935 if (mddev->pers && mddev->persistent &&
2936 !super_types[mddev->major_version]
2937 .allow_new_offset(rdev, new_offset))
2939 rdev->new_data_offset = new_offset;
2940 if (new_offset > rdev->data_offset)
2941 mddev->reshape_backwards = 1;
2942 else if (new_offset < rdev->data_offset)
2943 mddev->reshape_backwards = 0;
2947 static struct rdev_sysfs_entry rdev_new_offset =
2948 __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2951 rdev_size_show(struct md_rdev *rdev, char *page)
2953 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
2956 static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2958 /* check if two start/length pairs overlap */
2966 static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2968 unsigned long long blocks;
2971 if (kstrtoull(buf, 10, &blocks) < 0)
2974 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2975 return -EINVAL; /* sector conversion overflow */
2978 if (new != blocks * 2)
2979 return -EINVAL; /* unsigned long long to sector_t overflow */
2986 rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
2988 struct mddev *my_mddev = rdev->mddev;
2989 sector_t oldsectors = rdev->sectors;
2992 if (test_bit(Journal, &rdev->flags))
2994 if (strict_blocks_to_sectors(buf, §ors) < 0)
2996 if (rdev->data_offset != rdev->new_data_offset)
2997 return -EINVAL; /* too confusing */
2998 if (my_mddev->pers && rdev->raid_disk >= 0) {
2999 if (my_mddev->persistent) {
3000 sectors = super_types[my_mddev->major_version].
3001 rdev_size_change(rdev, sectors);
3004 } else if (!sectors)
3005 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
3007 if (!my_mddev->pers->resize)
3008 /* Cannot change size for RAID0 or Linear etc */
3011 if (sectors < my_mddev->dev_sectors)
3012 return -EINVAL; /* component must fit device */
3014 rdev->sectors = sectors;
3015 if (sectors > oldsectors && my_mddev->external) {
3016 /* Need to check that all other rdevs with the same
3017 * ->bdev do not overlap. 'rcu' is sufficient to walk
3018 * the rdev lists safely.
3019 * This check does not provide a hard guarantee, it
3020 * just helps avoid dangerous mistakes.
3022 struct mddev *mddev;
3024 struct list_head *tmp;
3027 for_each_mddev(mddev, tmp) {
3028 struct md_rdev *rdev2;
3030 rdev_for_each(rdev2, mddev)
3031 if (rdev->bdev == rdev2->bdev &&
3033 overlaps(rdev->data_offset, rdev->sectors,
3046 /* Someone else could have slipped in a size
3047 * change here, but doing so is just silly.
3048 * We put oldsectors back because we *know* it is
3049 * safe, and trust userspace not to race with
3052 rdev->sectors = oldsectors;
3059 static struct rdev_sysfs_entry rdev_size =
3060 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
3062 static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
3064 unsigned long long recovery_start = rdev->recovery_offset;
3066 if (test_bit(In_sync, &rdev->flags) ||
3067 recovery_start == MaxSector)
3068 return sprintf(page, "none\n");
3070 return sprintf(page, "%llu\n", recovery_start);
3073 static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
3075 unsigned long long recovery_start;
3077 if (cmd_match(buf, "none"))
3078 recovery_start = MaxSector;
3079 else if (kstrtoull(buf, 10, &recovery_start))
3082 if (rdev->mddev->pers &&
3083 rdev->raid_disk >= 0)
3086 rdev->recovery_offset = recovery_start;
3087 if (recovery_start == MaxSector)
3088 set_bit(In_sync, &rdev->flags);
3090 clear_bit(In_sync, &rdev->flags);
3094 static struct rdev_sysfs_entry rdev_recovery_start =
3095 __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3097 /* sysfs access to bad-blocks list.
3098 * We present two files.
3099 * 'bad-blocks' lists sector numbers and lengths of ranges that
3100 * are recorded as bad. The list is truncated to fit within
3101 * the one-page limit of sysfs.
3102 * Writing "sector length" to this file adds an acknowledged
3104 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3105 * been acknowledged. Writing to this file adds bad blocks
3106 * without acknowledging them. This is largely for testing.
3108 static ssize_t bb_show(struct md_rdev *rdev, char *page)
3110 return badblocks_show(&rdev->badblocks, page, 0);
3112 static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
3114 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3115 /* Maybe that ack was all we needed */
3116 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3117 wake_up(&rdev->blocked_wait);
3120 static struct rdev_sysfs_entry rdev_bad_blocks =
3121 __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3123 static ssize_t ubb_show(struct md_rdev *rdev, char *page)
3125 return badblocks_show(&rdev->badblocks, page, 1);
3127 static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
3129 return badblocks_store(&rdev->badblocks, page, len, 1);
3131 static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3132 __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3134 static struct attribute *rdev_default_attrs[] = {
3139 &rdev_new_offset.attr,
3141 &rdev_recovery_start.attr,
3142 &rdev_bad_blocks.attr,
3143 &rdev_unack_bad_blocks.attr,
3147 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3149 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
3150 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
3156 return entry->show(rdev, page);
3160 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3161 const char *page, size_t length)
3163 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
3164 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
3166 struct mddev *mddev = rdev->mddev;
3170 if (!capable(CAP_SYS_ADMIN))
3172 rv = mddev ? mddev_lock(mddev): -EBUSY;
3174 if (rdev->mddev == NULL)
3177 rv = entry->store(rdev, page, length);
3178 mddev_unlock(mddev);
3183 static void rdev_free(struct kobject *ko)
3185 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
3188 static const struct sysfs_ops rdev_sysfs_ops = {
3189 .show = rdev_attr_show,
3190 .store = rdev_attr_store,
3192 static struct kobj_type rdev_ktype = {
3193 .release = rdev_free,
3194 .sysfs_ops = &rdev_sysfs_ops,
3195 .default_attrs = rdev_default_attrs,
3198 int md_rdev_init(struct md_rdev *rdev)
3201 rdev->saved_raid_disk = -1;
3202 rdev->raid_disk = -1;
3204 rdev->data_offset = 0;
3205 rdev->new_data_offset = 0;
3206 rdev->sb_events = 0;
3207 rdev->last_read_error = 0;
3208 rdev->sb_loaded = 0;
3209 rdev->bb_page = NULL;
3210 atomic_set(&rdev->nr_pending, 0);
3211 atomic_set(&rdev->read_errors, 0);
3212 atomic_set(&rdev->corrected_errors, 0);
3214 INIT_LIST_HEAD(&rdev->same_set);
3215 init_waitqueue_head(&rdev->blocked_wait);
3217 /* Add space to store bad block list.
3218 * This reserves the space even on arrays where it cannot
3219 * be used - I wonder if that matters
3221 return badblocks_init(&rdev->badblocks, 0);
3223 EXPORT_SYMBOL_GPL(md_rdev_init);
3225 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3227 * mark the device faulty if:
3229 * - the device is nonexistent (zero size)
3230 * - the device has no valid superblock
3232 * a faulty rdev _never_ has rdev->sb set.
3234 static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
3236 char b[BDEVNAME_SIZE];
3238 struct md_rdev *rdev;
3241 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
3243 return ERR_PTR(-ENOMEM);
3245 err = md_rdev_init(rdev);
3248 err = alloc_disk_sb(rdev);
3252 err = lock_rdev(rdev, newdev, super_format == -2);
3256 kobject_init(&rdev->kobj, &rdev_ktype);
3258 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
3260 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
3261 bdevname(rdev->bdev,b));
3266 if (super_format >= 0) {
3267 err = super_types[super_format].
3268 load_super(rdev, NULL, super_minor);
3269 if (err == -EINVAL) {
3270 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
3271 bdevname(rdev->bdev,b),
3272 super_format, super_minor);
3276 pr_warn("md: could not read %s's sb, not importing!\n",
3277 bdevname(rdev->bdev,b));
3287 md_rdev_clear(rdev);
3289 return ERR_PTR(err);
3293 * Check a full RAID array for plausibility
3296 static void analyze_sbs(struct mddev *mddev)
3299 struct md_rdev *rdev, *freshest, *tmp;
3300 char b[BDEVNAME_SIZE];
3303 rdev_for_each_safe(rdev, tmp, mddev)
3304 switch (super_types[mddev->major_version].
3305 load_super(rdev, freshest, mddev->minor_version)) {
3312 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
3313 bdevname(rdev->bdev,b));
3314 md_kick_rdev_from_array(rdev);
3317 super_types[mddev->major_version].
3318 validate_super(mddev, freshest);
3321 rdev_for_each_safe(rdev, tmp, mddev) {
3322 if (mddev->max_disks &&
3323 (rdev->desc_nr >= mddev->max_disks ||
3324 i > mddev->max_disks)) {
3325 pr_warn("md: %s: %s: only %d devices permitted\n",
3326 mdname(mddev), bdevname(rdev->bdev, b),
3328 md_kick_rdev_from_array(rdev);
3331 if (rdev != freshest) {
3332 if (super_types[mddev->major_version].
3333 validate_super(mddev, rdev)) {
3334 pr_warn("md: kicking non-fresh %s from array!\n",
3335 bdevname(rdev->bdev,b));
3336 md_kick_rdev_from_array(rdev);
3340 if (mddev->level == LEVEL_MULTIPATH) {
3341 rdev->desc_nr = i++;
3342 rdev->raid_disk = rdev->desc_nr;
3343 set_bit(In_sync, &rdev->flags);
3344 } else if (rdev->raid_disk >=
3345 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3346 !test_bit(Journal, &rdev->flags)) {
3347 rdev->raid_disk = -1;
3348 clear_bit(In_sync, &rdev->flags);
3353 /* Read a fixed-point number.
3354 * Numbers in sysfs attributes should be in "standard" units where
3355 * possible, so time should be in seconds.
3356 * However we internally use a a much smaller unit such as
3357 * milliseconds or jiffies.
3358 * This function takes a decimal number with a possible fractional
3359 * component, and produces an integer which is the result of
3360 * multiplying that number by 10^'scale'.
3361 * all without any floating-point arithmetic.
3363 int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3365 unsigned long result = 0;
3367 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3370 else if (decimals < scale) {
3373 result = result * 10 + value;
3385 while (decimals < scale) {
3394 safe_delay_show(struct mddev *mddev, char *page)
3396 int msec = (mddev->safemode_delay*1000)/HZ;
3397 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3400 safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
3404 if (mddev_is_clustered(mddev)) {
3405 pr_warn("md: Safemode is disabled for clustered mode\n");
3409 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
3412 mddev->safemode_delay = 0;
3414 unsigned long old_delay = mddev->safemode_delay;
3415 unsigned long new_delay = (msec*HZ)/1000;
3419 mddev->safemode_delay = new_delay;
3420 if (new_delay < old_delay || old_delay == 0)
3421 mod_timer(&mddev->safemode_timer, jiffies+1);
3425 static struct md_sysfs_entry md_safe_delay =
3426 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
3429 level_show(struct mddev *mddev, char *page)
3431 struct md_personality *p;
3433 spin_lock(&mddev->lock);
3436 ret = sprintf(page, "%s\n", p->name);
3437 else if (mddev->clevel[0])
3438 ret = sprintf(page, "%s\n", mddev->clevel);
3439 else if (mddev->level != LEVEL_NONE)
3440 ret = sprintf(page, "%d\n", mddev->level);
3443 spin_unlock(&mddev->lock);
3448 level_store(struct mddev *mddev, const char *buf, size_t len)
3453 struct md_personality *pers, *oldpers;
3455 void *priv, *oldpriv;
3456 struct md_rdev *rdev;
3458 if (slen == 0 || slen >= sizeof(clevel))
3461 rv = mddev_lock(mddev);
3465 if (mddev->pers == NULL) {
3466 strncpy(mddev->clevel, buf, slen);
3467 if (mddev->clevel[slen-1] == '\n')
3469 mddev->clevel[slen] = 0;
3470 mddev->level = LEVEL_NONE;
3478 /* request to change the personality. Need to ensure:
3479 * - array is not engaged in resync/recovery/reshape
3480 * - old personality can be suspended
3481 * - new personality will access other array.
3485 if (mddev->sync_thread ||
3486 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3487 mddev->reshape_position != MaxSector ||
3488 mddev->sysfs_active)
3492 if (!mddev->pers->quiesce) {
3493 pr_warn("md: %s: %s does not support online personality change\n",
3494 mdname(mddev), mddev->pers->name);
3498 /* Now find the new personality */
3499 strncpy(clevel, buf, slen);
3500 if (clevel[slen-1] == '\n')
3503 if (kstrtol(clevel, 10, &level))
3506 if (request_module("md-%s", clevel) != 0)
3507 request_module("md-level-%s", clevel);
3508 spin_lock(&pers_lock);
3509 pers = find_pers(level, clevel);
3510 if (!pers || !try_module_get(pers->owner)) {
3511 spin_unlock(&pers_lock);
3512 pr_warn("md: personality %s not loaded\n", clevel);
3516 spin_unlock(&pers_lock);
3518 if (pers == mddev->pers) {
3519 /* Nothing to do! */
3520 module_put(pers->owner);
3524 if (!pers->takeover) {
3525 module_put(pers->owner);
3526 pr_warn("md: %s: %s does not support personality takeover\n",
3527 mdname(mddev), clevel);
3532 rdev_for_each(rdev, mddev)
3533 rdev->new_raid_disk = rdev->raid_disk;
3535 /* ->takeover must set new_* and/or delta_disks
3536 * if it succeeds, and may set them when it fails.
3538 priv = pers->takeover(mddev);
3540 mddev->new_level = mddev->level;
3541 mddev->new_layout = mddev->layout;
3542 mddev->new_chunk_sectors = mddev->chunk_sectors;
3543 mddev->raid_disks -= mddev->delta_disks;
3544 mddev->delta_disks = 0;
3545 mddev->reshape_backwards = 0;
3546 module_put(pers->owner);
3547 pr_warn("md: %s: %s would not accept array\n",
3548 mdname(mddev), clevel);
3553 /* Looks like we have a winner */
3554 mddev_suspend(mddev);
3555 mddev_detach(mddev);
3557 spin_lock(&mddev->lock);
3558 oldpers = mddev->pers;
3559 oldpriv = mddev->private;
3561 mddev->private = priv;
3562 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3563 mddev->level = mddev->new_level;
3564 mddev->layout = mddev->new_layout;
3565 mddev->chunk_sectors = mddev->new_chunk_sectors;
3566 mddev->delta_disks = 0;
3567 mddev->reshape_backwards = 0;
3568 mddev->degraded = 0;
3569 spin_unlock(&mddev->lock);
3571 if (oldpers->sync_request == NULL &&
3573 /* We are converting from a no-redundancy array
3574 * to a redundancy array and metadata is managed
3575 * externally so we need to be sure that writes
3576 * won't block due to a need to transition
3578 * until external management is started.
3581 mddev->safemode_delay = 0;
3582 mddev->safemode = 0;
3585 oldpers->free(mddev, oldpriv);
3587 if (oldpers->sync_request == NULL &&
3588 pers->sync_request != NULL) {
3589 /* need to add the md_redundancy_group */
3590 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3591 pr_warn("md: cannot register extra attributes for %s\n",
3593 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3595 if (oldpers->sync_request != NULL &&
3596 pers->sync_request == NULL) {
3597 /* need to remove the md_redundancy_group */
3598 if (mddev->to_remove == NULL)
3599 mddev->to_remove = &md_redundancy_group;
3602 module_put(oldpers->owner);
3604 rdev_for_each(rdev, mddev) {
3605 if (rdev->raid_disk < 0)
3607 if (rdev->new_raid_disk >= mddev->raid_disks)
3608 rdev->new_raid_disk = -1;
3609 if (rdev->new_raid_disk == rdev->raid_disk)
3611 sysfs_unlink_rdev(mddev, rdev);
3613 rdev_for_each(rdev, mddev) {
3614 if (rdev->raid_disk < 0)
3616 if (rdev->new_raid_disk == rdev->raid_disk)
3618 rdev->raid_disk = rdev->new_raid_disk;
3619 if (rdev->raid_disk < 0)
3620 clear_bit(In_sync, &rdev->flags);
3622 if (sysfs_link_rdev(mddev, rdev))
3623 pr_warn("md: cannot register rd%d for %s after level change\n",
3624 rdev->raid_disk, mdname(mddev));
3628 if (pers->sync_request == NULL) {
3629 /* this is now an array without redundancy, so
3630 * it must always be in_sync
3633 del_timer_sync(&mddev->safemode_timer);
3635 blk_set_stacking_limits(&mddev->queue->limits);
3637 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
3638 mddev_resume(mddev);
3640 md_update_sb(mddev, 1);
3641 sysfs_notify(&mddev->kobj, NULL, "level");
3642 md_new_event(mddev);
3645 mddev_unlock(mddev);
3649 static struct md_sysfs_entry md_level =
3650 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
3653 layout_show(struct mddev *mddev, char *page)
3655 /* just a number, not meaningful for all levels */
3656 if (mddev->reshape_position != MaxSector &&
3657 mddev->layout != mddev->new_layout)
3658 return sprintf(page, "%d (%d)\n",
3659 mddev->new_layout, mddev->layout);
3660 return sprintf(page, "%d\n", mddev->layout);
3664 layout_store(struct mddev *mddev, const char *buf, size_t len)
3669 err = kstrtouint(buf, 10, &n);
3672 err = mddev_lock(mddev);
3677 if (mddev->pers->check_reshape == NULL)
3682 mddev->new_layout = n;
3683 err = mddev->pers->check_reshape(mddev);
3685 mddev->new_layout = mddev->layout;
3688 mddev->new_layout = n;
3689 if (mddev->reshape_position == MaxSector)
3692 mddev_unlock(mddev);
3695 static struct md_sysfs_entry md_layout =
3696 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
3699 raid_disks_show(struct mddev *mddev, char *page)
3701 if (mddev->raid_disks == 0)
3703 if (mddev->reshape_position != MaxSector &&
3704 mddev->delta_disks != 0)
3705 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3706 mddev->raid_disks - mddev->delta_disks);
3707 return sprintf(page, "%d\n", mddev->raid_disks);
3710 static int update_raid_disks(struct mddev *mddev, int raid_disks);
3713 raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
3718 err = kstrtouint(buf, 10, &n);
3722 err = mddev_lock(mddev);
3726 err = update_raid_disks(mddev, n);
3727 else if (mddev->reshape_position != MaxSector) {
3728 struct md_rdev *rdev;
3729 int olddisks = mddev->raid_disks - mddev->delta_disks;
3732 rdev_for_each(rdev, mddev) {
3734 rdev->data_offset < rdev->new_data_offset)
3737 rdev->data_offset > rdev->new_data_offset)
3741 mddev->delta_disks = n - olddisks;
3742 mddev->raid_disks = n;
3743 mddev->reshape_backwards = (mddev->delta_disks < 0);
3745 mddev->raid_disks = n;
3747 mddev_unlock(mddev);
3748 return err ? err : len;
3750 static struct md_sysfs_entry md_raid_disks =
3751 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
3754 chunk_size_show(struct mddev *mddev, char *page)
3756 if (mddev->reshape_position != MaxSector &&
3757 mddev->chunk_sectors != mddev->new_chunk_sectors)
3758 return sprintf(page, "%d (%d)\n",
3759 mddev->new_chunk_sectors << 9,
3760 mddev->chunk_sectors << 9);
3761 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
3765 chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
3770 err = kstrtoul(buf, 10, &n);
3774 err = mddev_lock(mddev);
3778 if (mddev->pers->check_reshape == NULL)
3783 mddev->new_chunk_sectors = n >> 9;
3784 err = mddev->pers->check_reshape(mddev);
3786 mddev->new_chunk_sectors = mddev->chunk_sectors;
3789 mddev->new_chunk_sectors = n >> 9;
3790 if (mddev->reshape_position == MaxSector)
3791 mddev->chunk_sectors = n >> 9;
3793 mddev_unlock(mddev);
3796 static struct md_sysfs_entry md_chunk_size =
3797 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
3800 resync_start_show(struct mddev *mddev, char *page)
3802 if (mddev->recovery_cp == MaxSector)
3803 return sprintf(page, "none\n");
3804 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3808 resync_start_store(struct mddev *mddev, const char *buf, size_t len)
3810 unsigned long long n;
3813 if (cmd_match(buf, "none"))
3816 err = kstrtoull(buf, 10, &n);
3819 if (n != (sector_t)n)
3823 err = mddev_lock(mddev);
3826 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3830 mddev->recovery_cp = n;
3832 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
3834 mddev_unlock(mddev);
3837 static struct md_sysfs_entry md_resync_start =
3838 __ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
3839 resync_start_show, resync_start_store);
3842 * The array state can be:
3845 * No devices, no size, no level
3846 * Equivalent to STOP_ARRAY ioctl
3848 * May have some settings, but array is not active
3849 * all IO results in error
3850 * When written, doesn't tear down array, but just stops it
3851 * suspended (not supported yet)
3852 * All IO requests will block. The array can be reconfigured.
3853 * Writing this, if accepted, will block until array is quiescent
3855 * no resync can happen. no superblocks get written.
3856 * write requests fail
3858 * like readonly, but behaves like 'clean' on a write request.
3860 * clean - no pending writes, but otherwise active.
3861 * When written to inactive array, starts without resync
3862 * If a write request arrives then
3863 * if metadata is known, mark 'dirty' and switch to 'active'.
3864 * if not known, block and switch to write-pending
3865 * If written to an active array that has pending writes, then fails.
3867 * fully active: IO and resync can be happening.
3868 * When written to inactive array, starts with resync
3871 * clean, but writes are blocked waiting for 'active' to be written.
3874 * like active, but no writes have been seen for a while (100msec).
3877 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3878 write_pending, active_idle, bad_word};
3879 static char *array_states[] = {
3880 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3881 "write-pending", "active-idle", NULL };
3883 static int match_word(const char *word, char **list)
3886 for (n=0; list[n]; n++)
3887 if (cmd_match(word, list[n]))
3893 array_state_show(struct mddev *mddev, char *page)
3895 enum array_state st = inactive;
3906 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
3908 else if (mddev->in_sync)
3910 else if (mddev->safemode)
3916 if (list_empty(&mddev->disks) &&
3917 mddev->raid_disks == 0 &&
3918 mddev->dev_sectors == 0)
3923 return sprintf(page, "%s\n", array_states[st]);
3926 static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
3927 static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
3928 static int do_md_run(struct mddev *mddev);
3929 static int restart_array(struct mddev *mddev);
3932 array_state_store(struct mddev *mddev, const char *buf, size_t len)
3935 enum array_state st = match_word(buf, array_states);
3937 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
3938 /* don't take reconfig_mutex when toggling between
3941 spin_lock(&mddev->lock);
3943 restart_array(mddev);
3944 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
3945 md_wakeup_thread(mddev->thread);
3946 wake_up(&mddev->sb_wait);
3948 } else /* st == clean */ {
3949 restart_array(mddev);
3950 if (atomic_read(&mddev->writes_pending) == 0) {
3951 if (mddev->in_sync == 0) {
3953 if (mddev->safemode == 1)
3954 mddev->safemode = 0;
3955 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
3962 sysfs_notify_dirent_safe(mddev->sysfs_state);
3963 spin_unlock(&mddev->lock);
3966 err = mddev_lock(mddev);
3974 /* stopping an active array */
3975 err = do_md_stop(mddev, 0, NULL);
3978 /* stopping an active array */
3980 err = do_md_stop(mddev, 2, NULL);
3982 err = 0; /* already inactive */
3985 break; /* not supported yet */
3988 err = md_set_readonly(mddev, NULL);
3991 set_disk_ro(mddev->gendisk, 1);
3992 err = do_md_run(mddev);
3998 err = md_set_readonly(mddev, NULL);
3999 else if (mddev->ro == 1)
4000 err = restart_array(mddev);
4003 set_disk_ro(mddev->gendisk, 0);
4007 err = do_md_run(mddev);
4012 err = restart_array(mddev);
4015 spin_lock(&mddev->lock);
4016 if (atomic_read(&mddev->writes_pending) == 0) {
4017 if (mddev->in_sync == 0) {
4019 if (mddev->safemode == 1)
4020 mddev->safemode = 0;
4021 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
4026 spin_unlock(&mddev->lock);
4032 err = restart_array(mddev);
4035 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
4036 wake_up(&mddev->sb_wait);
4040 set_disk_ro(mddev->gendisk, 0);
4041 err = do_md_run(mddev);
4046 /* these cannot be set */
4051 if (mddev->hold_active == UNTIL_IOCTL)
4052 mddev->hold_active = 0;
4053 sysfs_notify_dirent_safe(mddev->sysfs_state);
4055 mddev_unlock(mddev);
4058 static struct md_sysfs_entry md_array_state =
4059 __ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
4062 max_corrected_read_errors_show(struct mddev *mddev, char *page) {
4063 return sprintf(page, "%d\n",
4064 atomic_read(&mddev->max_corr_read_errors));
4068 max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
4073 rv = kstrtouint(buf, 10, &n);
4076 atomic_set(&mddev->max_corr_read_errors, n);
4080 static struct md_sysfs_entry max_corr_read_errors =
4081 __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4082 max_corrected_read_errors_store);
4085 null_show(struct mddev *mddev, char *page)
4091 new_dev_store(struct mddev *mddev, const char *buf, size_t len)
4093 /* buf must be %d:%d\n? giving major and minor numbers */
4094 /* The new device is added to the array.
4095 * If the array has a persistent superblock, we read the
4096 * superblock to initialise info and check validity.
4097 * Otherwise, only checking done is that in bind_rdev_to_array,
4098 * which mainly checks size.
4101 int major = simple_strtoul(buf, &e, 10);
4104 struct md_rdev *rdev;
4107 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4109 minor = simple_strtoul(e+1, &e, 10);
4110 if (*e && *e != '\n')
4112 dev = MKDEV(major, minor);
4113 if (major != MAJOR(dev) ||
4114 minor != MINOR(dev))
4117 flush_workqueue(md_misc_wq);
4119 err = mddev_lock(mddev);
4122 if (mddev->persistent) {
4123 rdev = md_import_device(dev, mddev->major_version,
4124 mddev->minor_version);
4125 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
4126 struct md_rdev *rdev0
4127 = list_entry(mddev->disks.next,
4128 struct md_rdev, same_set);
4129 err = super_types[mddev->major_version]
4130 .load_super(rdev, rdev0, mddev->minor_version);
4134 } else if (mddev->external)
4135 rdev = md_import_device(dev, -2, -1);
4137 rdev = md_import_device(dev, -1, -1);
4140 mddev_unlock(mddev);
4141 return PTR_ERR(rdev);
4143 err = bind_rdev_to_array(rdev, mddev);
4147 mddev_unlock(mddev);
4148 return err ? err : len;
4151 static struct md_sysfs_entry md_new_device =
4152 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
4155 bitmap_store(struct mddev *mddev, const char *buf, size_t len)
4158 unsigned long chunk, end_chunk;
4161 err = mddev_lock(mddev);
4166 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4168 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4169 if (buf == end) break;
4170 if (*end == '-') { /* range */
4172 end_chunk = simple_strtoul(buf, &end, 0);
4173 if (buf == end) break;
4175 if (*end && !isspace(*end)) break;
4176 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
4177 buf = skip_spaces(end);
4179 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4181 mddev_unlock(mddev);
4185 static struct md_sysfs_entry md_bitmap =
4186 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4189 size_show(struct mddev *mddev, char *page)
4191 return sprintf(page, "%llu\n",
4192 (unsigned long long)mddev->dev_sectors / 2);
4195 static int update_size(struct mddev *mddev, sector_t num_sectors);
4198 size_store(struct mddev *mddev, const char *buf, size_t len)
4200 /* If array is inactive, we can reduce the component size, but
4201 * not increase it (except from 0).
4202 * If array is active, we can try an on-line resize
4205 int err = strict_blocks_to_sectors(buf, §ors);
4209 err = mddev_lock(mddev);
4213 err = update_size(mddev, sectors);
4215 md_update_sb(mddev, 1);
4217 if (mddev->dev_sectors == 0 ||
4218 mddev->dev_sectors > sectors)
4219 mddev->dev_sectors = sectors;
4223 mddev_unlock(mddev);
4224 return err ? err : len;
4227 static struct md_sysfs_entry md_size =
4228 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
4230 /* Metadata version.
4232 * 'none' for arrays with no metadata (good luck...)
4233 * 'external' for arrays with externally managed metadata,
4234 * or N.M for internally known formats
4237 metadata_show(struct mddev *mddev, char *page)
4239 if (mddev->persistent)
4240 return sprintf(page, "%d.%d\n",
4241 mddev->major_version, mddev->minor_version);
4242 else if (mddev->external)
4243 return sprintf(page, "external:%s\n", mddev->metadata_type);
4245 return sprintf(page, "none\n");
4249 metadata_store(struct mddev *mddev, const char *buf, size_t len)
4254 /* Changing the details of 'external' metadata is
4255 * always permitted. Otherwise there must be
4256 * no devices attached to the array.
4259 err = mddev_lock(mddev);
4263 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4265 else if (!list_empty(&mddev->disks))
4269 if (cmd_match(buf, "none")) {
4270 mddev->persistent = 0;
4271 mddev->external = 0;
4272 mddev->major_version = 0;
4273 mddev->minor_version = 90;
4276 if (strncmp(buf, "external:", 9) == 0) {
4277 size_t namelen = len-9;
4278 if (namelen >= sizeof(mddev->metadata_type))
4279 namelen = sizeof(mddev->metadata_type)-1;
4280 strncpy(mddev->metadata_type, buf+9, namelen);
4281 mddev->metadata_type[namelen] = 0;
4282 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4283 mddev->metadata_type[--namelen] = 0;
4284 mddev->persistent = 0;
4285 mddev->external = 1;
4286 mddev->major_version = 0;
4287 mddev->minor_version = 90;
4290 major = simple_strtoul(buf, &e, 10);
4292 if (e==buf || *e != '.')
4295 minor = simple_strtoul(buf, &e, 10);
4296 if (e==buf || (*e && *e != '\n') )
4299 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
4301 mddev->major_version = major;
4302 mddev->minor_version = minor;
4303 mddev->persistent = 1;
4304 mddev->external = 0;
4307 mddev_unlock(mddev);
4311 static struct md_sysfs_entry md_metadata =
4312 __ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
4315 action_show(struct mddev *mddev, char *page)
4317 char *type = "idle";
4318 unsigned long recovery = mddev->recovery;
4319 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
4321 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4322 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4323 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
4325 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4326 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
4328 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
4332 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
4334 else if (mddev->reshape_position != MaxSector)
4337 return sprintf(page, "%s\n", type);
4341 action_store(struct mddev *mddev, const char *page, size_t len)
4343 if (!mddev->pers || !mddev->pers->sync_request)
4347 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
4348 if (cmd_match(page, "frozen"))
4349 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4351 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4352 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4353 mddev_lock(mddev) == 0) {
4354 flush_workqueue(md_misc_wq);
4355 if (mddev->sync_thread) {
4356 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4357 md_reap_sync_thread(mddev);
4359 mddev_unlock(mddev);
4361 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4363 else if (cmd_match(page, "resync"))
4364 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4365 else if (cmd_match(page, "recover")) {
4366 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4367 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4368 } else if (cmd_match(page, "reshape")) {
4370 if (mddev->pers->start_reshape == NULL)
4372 err = mddev_lock(mddev);
4374 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4377 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4378 err = mddev->pers->start_reshape(mddev);
4380 mddev_unlock(mddev);
4384 sysfs_notify(&mddev->kobj, NULL, "degraded");
4386 if (cmd_match(page, "check"))
4387 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4388 else if (!cmd_match(page, "repair"))
4390 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4391 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4392 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4394 if (mddev->ro == 2) {
4395 /* A write to sync_action is enough to justify
4396 * canceling read-auto mode
4399 md_wakeup_thread(mddev->sync_thread);
4401 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4402 md_wakeup_thread(mddev->thread);
4403 sysfs_notify_dirent_safe(mddev->sysfs_action);
4407 static struct md_sysfs_entry md_scan_mode =
4408 __ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
4411 last_sync_action_show(struct mddev *mddev, char *page)
4413 return sprintf(page, "%s\n", mddev->last_sync_action);
4416 static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4419 mismatch_cnt_show(struct mddev *mddev, char *page)
4421 return sprintf(page, "%llu\n",
4422 (unsigned long long)
4423 atomic64_read(&mddev->resync_mismatches));
4426 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
4429 sync_min_show(struct mddev *mddev, char *page)
4431 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4432 mddev->sync_speed_min ? "local": "system");
4436 sync_min_store(struct mddev *mddev, const char *buf, size_t len)
4441 if (strncmp(buf, "system", 6)==0) {
4444 rv = kstrtouint(buf, 10, &min);
4450 mddev->sync_speed_min = min;
4454 static struct md_sysfs_entry md_sync_min =
4455 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4458 sync_max_show(struct mddev *mddev, char *page)
4460 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4461 mddev->sync_speed_max ? "local": "system");
4465 sync_max_store(struct mddev *mddev, const char *buf, size_t len)
4470 if (strncmp(buf, "system", 6)==0) {
4473 rv = kstrtouint(buf, 10, &max);
4479 mddev->sync_speed_max = max;
4483 static struct md_sysfs_entry md_sync_max =
4484 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4487 degraded_show(struct mddev *mddev, char *page)
4489 return sprintf(page, "%d\n", mddev->degraded);
4491 static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
4494 sync_force_parallel_show(struct mddev *mddev, char *page)
4496 return sprintf(page, "%d\n", mddev->parallel_resync);
4500 sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
4504 if (kstrtol(buf, 10, &n))
4507 if (n != 0 && n != 1)
4510 mddev->parallel_resync = n;
4512 if (mddev->sync_thread)
4513 wake_up(&resync_wait);
4518 /* force parallel resync, even with shared block devices */
4519 static struct md_sysfs_entry md_sync_force_parallel =
4520 __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4521 sync_force_parallel_show, sync_force_parallel_store);
4524 sync_speed_show(struct mddev *mddev, char *page)
4526 unsigned long resync, dt, db;
4527 if (mddev->curr_resync == 0)
4528 return sprintf(page, "none\n");
4529 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4530 dt = (jiffies - mddev->resync_mark) / HZ;
4532 db = resync - mddev->resync_mark_cnt;
4533 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
4536 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
4539 sync_completed_show(struct mddev *mddev, char *page)
4541 unsigned long long max_sectors, resync;
4543 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4544 return sprintf(page, "none\n");
4546 if (mddev->curr_resync == 1 ||
4547 mddev->curr_resync == 2)
4548 return sprintf(page, "delayed\n");
4550 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4551 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4552 max_sectors = mddev->resync_max_sectors;
4554 max_sectors = mddev->dev_sectors;
4556 resync = mddev->curr_resync_completed;
4557 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
4560 static struct md_sysfs_entry md_sync_completed =
4561 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
4564 min_sync_show(struct mddev *mddev, char *page)
4566 return sprintf(page, "%llu\n",
4567 (unsigned long long)mddev->resync_min);
4570 min_sync_store(struct mddev *mddev, const char *buf, size_t len)
4572 unsigned long long min;
4575 if (kstrtoull(buf, 10, &min))
4578 spin_lock(&mddev->lock);
4580 if (min > mddev->resync_max)
4584 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4587 /* Round down to multiple of 4K for safety */
4588 mddev->resync_min = round_down(min, 8);
4592 spin_unlock(&mddev->lock);
4596 static struct md_sysfs_entry md_min_sync =
4597 __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4600 max_sync_show(struct mddev *mddev, char *page)
4602 if (mddev->resync_max == MaxSector)
4603 return sprintf(page, "max\n");
4605 return sprintf(page, "%llu\n",
4606 (unsigned long long)mddev->resync_max);
4609 max_sync_store(struct mddev *mddev, const char *buf, size_t len)
4612 spin_lock(&mddev->lock);
4613 if (strncmp(buf, "max", 3) == 0)
4614 mddev->resync_max = MaxSector;
4616 unsigned long long max;
4620 if (kstrtoull(buf, 10, &max))
4622 if (max < mddev->resync_min)
4626 if (max < mddev->resync_max &&
4628 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4631 /* Must be a multiple of chunk_size */
4632 chunk = mddev->chunk_sectors;
4634 sector_t temp = max;
4637 if (sector_div(temp, chunk))
4640 mddev->resync_max = max;
4642 wake_up(&mddev->recovery_wait);
4645 spin_unlock(&mddev->lock);
4649 static struct md_sysfs_entry md_max_sync =
4650 __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4653 suspend_lo_show(struct mddev *mddev, char *page)
4655 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4659 suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
4661 unsigned long long old, new;
4664 err = kstrtoull(buf, 10, &new);
4667 if (new != (sector_t)new)
4670 err = mddev_lock(mddev);
4674 if (mddev->pers == NULL ||
4675 mddev->pers->quiesce == NULL)
4677 old = mddev->suspend_lo;
4678 mddev->suspend_lo = new;
4680 /* Shrinking suspended region */
4681 mddev->pers->quiesce(mddev, 2);
4683 /* Expanding suspended region - need to wait */
4684 mddev->pers->quiesce(mddev, 1);
4685 mddev->pers->quiesce(mddev, 0);
4689 mddev_unlock(mddev);
4692 static struct md_sysfs_entry md_suspend_lo =
4693 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4696 suspend_hi_show(struct mddev *mddev, char *page)
4698 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4702 suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
4704 unsigned long long old, new;
4707 err = kstrtoull(buf, 10, &new);
4710 if (new != (sector_t)new)
4713 err = mddev_lock(mddev);
4717 if (mddev->pers == NULL ||
4718 mddev->pers->quiesce == NULL)
4720 old = mddev->suspend_hi;
4721 mddev->suspend_hi = new;
4723 /* Shrinking suspended region */
4724 mddev->pers->quiesce(mddev, 2);
4726 /* Expanding suspended region - need to wait */
4727 mddev->pers->quiesce(mddev, 1);
4728 mddev->pers->quiesce(mddev, 0);
4732 mddev_unlock(mddev);
4735 static struct md_sysfs_entry md_suspend_hi =
4736 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4739 reshape_position_show(struct mddev *mddev, char *page)
4741 if (mddev->reshape_position != MaxSector)
4742 return sprintf(page, "%llu\n",
4743 (unsigned long long)mddev->reshape_position);
4744 strcpy(page, "none\n");
4749 reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
4751 struct md_rdev *rdev;
4752 unsigned long long new;
4755 err = kstrtoull(buf, 10, &new);
4758 if (new != (sector_t)new)
4760 err = mddev_lock(mddev);
4766 mddev->reshape_position = new;
4767 mddev->delta_disks = 0;
4768 mddev->reshape_backwards = 0;
4769 mddev->new_level = mddev->level;
4770 mddev->new_layout = mddev->layout;
4771 mddev->new_chunk_sectors = mddev->chunk_sectors;
4772 rdev_for_each(rdev, mddev)
4773 rdev->new_data_offset = rdev->data_offset;
4776 mddev_unlock(mddev);
4780 static struct md_sysfs_entry md_reshape_position =
4781 __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4782 reshape_position_store);
4785 reshape_direction_show(struct mddev *mddev, char *page)
4787 return sprintf(page, "%s\n",
4788 mddev->reshape_backwards ? "backwards" : "forwards");
4792 reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4797 if (cmd_match(buf, "forwards"))
4799 else if (cmd_match(buf, "backwards"))
4803 if (mddev->reshape_backwards == backwards)
4806 err = mddev_lock(mddev);
4809 /* check if we are allowed to change */
4810 if (mddev->delta_disks)
4812 else if (mddev->persistent &&
4813 mddev->major_version == 0)
4816 mddev->reshape_backwards = backwards;
4817 mddev_unlock(mddev);
4821 static struct md_sysfs_entry md_reshape_direction =
4822 __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4823 reshape_direction_store);
4826 array_size_show(struct mddev *mddev, char *page)
4828 if (mddev->external_size)
4829 return sprintf(page, "%llu\n",
4830 (unsigned long long)mddev->array_sectors/2);
4832 return sprintf(page, "default\n");
4836 array_size_store(struct mddev *mddev, const char *buf, size_t len)
4841 err = mddev_lock(mddev);
4845 /* cluster raid doesn't support change array_sectors */
4846 if (mddev_is_clustered(mddev))
4849 if (strncmp(buf, "default", 7) == 0) {
4851 sectors = mddev->pers->size(mddev, 0, 0);
4853 sectors = mddev->array_sectors;
4855 mddev->external_size = 0;
4857 if (strict_blocks_to_sectors(buf, §ors) < 0)
4859 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4862 mddev->external_size = 1;
4866 mddev->array_sectors = sectors;
4868 set_capacity(mddev->gendisk, mddev->array_sectors);
4869 revalidate_disk(mddev->gendisk);
4872 mddev_unlock(mddev);
4876 static struct md_sysfs_entry md_array_size =
4877 __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4880 static struct attribute *md_default_attrs[] = {
4883 &md_raid_disks.attr,
4884 &md_chunk_size.attr,
4886 &md_resync_start.attr,
4888 &md_new_device.attr,
4889 &md_safe_delay.attr,
4890 &md_array_state.attr,
4891 &md_reshape_position.attr,
4892 &md_reshape_direction.attr,
4893 &md_array_size.attr,
4894 &max_corr_read_errors.attr,
4898 static struct attribute *md_redundancy_attrs[] = {
4900 &md_last_scan_mode.attr,
4901 &md_mismatches.attr,
4904 &md_sync_speed.attr,
4905 &md_sync_force_parallel.attr,
4906 &md_sync_completed.attr,
4909 &md_suspend_lo.attr,
4910 &md_suspend_hi.attr,
4915 static struct attribute_group md_redundancy_group = {
4917 .attrs = md_redundancy_attrs,
4921 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4923 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4924 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
4929 spin_lock(&all_mddevs_lock);
4930 if (list_empty(&mddev->all_mddevs)) {
4931 spin_unlock(&all_mddevs_lock);
4935 spin_unlock(&all_mddevs_lock);
4937 rv = entry->show(mddev, page);
4943 md_attr_store(struct kobject *kobj, struct attribute *attr,
4944 const char *page, size_t length)
4946 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4947 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
4952 if (!capable(CAP_SYS_ADMIN))
4954 spin_lock(&all_mddevs_lock);
4955 if (list_empty(&mddev->all_mddevs)) {
4956 spin_unlock(&all_mddevs_lock);
4960 spin_unlock(&all_mddevs_lock);
4961 rv = entry->store(mddev, page, length);
4966 static void md_free(struct kobject *ko)
4968 struct mddev *mddev = container_of(ko, struct mddev, kobj);
4970 if (mddev->sysfs_state)
4971 sysfs_put(mddev->sysfs_state);
4974 blk_cleanup_queue(mddev->queue);
4975 if (mddev->gendisk) {
4976 del_gendisk(mddev->gendisk);
4977 put_disk(mddev->gendisk);
4983 static const struct sysfs_ops md_sysfs_ops = {
4984 .show = md_attr_show,
4985 .store = md_attr_store,
4987 static struct kobj_type md_ktype = {
4989 .sysfs_ops = &md_sysfs_ops,
4990 .default_attrs = md_default_attrs,
4995 static void mddev_delayed_delete(struct work_struct *ws)
4997 struct mddev *mddev = container_of(ws, struct mddev, del_work);
4999 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
5000 kobject_del(&mddev->kobj);
5001 kobject_put(&mddev->kobj);
5004 static int md_alloc(dev_t dev, char *name)
5006 static DEFINE_MUTEX(disks_mutex);
5007 struct mddev *mddev = mddev_find(dev);
5008 struct gendisk *disk;
5017 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
5018 shift = partitioned ? MdpMinorShift : 0;
5019 unit = MINOR(mddev->unit) >> shift;
5021 /* wait for any previous instance of this device to be
5022 * completely removed (mddev_delayed_delete).
5024 flush_workqueue(md_misc_wq);
5026 mutex_lock(&disks_mutex);
5032 /* Need to ensure that 'name' is not a duplicate.
5034 struct mddev *mddev2;
5035 spin_lock(&all_mddevs_lock);
5037 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5038 if (mddev2->gendisk &&
5039 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5040 spin_unlock(&all_mddevs_lock);
5043 spin_unlock(&all_mddevs_lock);
5047 mddev->queue = blk_alloc_queue(GFP_KERNEL);
5050 mddev->queue->queuedata = mddev;
5052 blk_queue_make_request(mddev->queue, md_make_request);
5053 blk_set_stacking_limits(&mddev->queue->limits);
5055 disk = alloc_disk(1 << shift);
5057 blk_cleanup_queue(mddev->queue);
5058 mddev->queue = NULL;
5061 disk->major = MAJOR(mddev->unit);
5062 disk->first_minor = unit << shift;
5064 strcpy(disk->disk_name, name);
5065 else if (partitioned)
5066 sprintf(disk->disk_name, "md_d%d", unit);
5068 sprintf(disk->disk_name, "md%d", unit);
5069 disk->fops = &md_fops;
5070 disk->private_data = mddev;
5071 disk->queue = mddev->queue;
5072 blk_queue_write_cache(mddev->queue, true, true);
5073 /* Allow extended partitions. This makes the
5074 * 'mdp' device redundant, but we can't really
5077 disk->flags |= GENHD_FL_EXT_DEVT;
5078 mddev->gendisk = disk;
5079 /* As soon as we call add_disk(), another thread could get
5080 * through to md_open, so make sure it doesn't get too far
5082 mutex_lock(&mddev->open_mutex);
5085 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
5086 &disk_to_dev(disk)->kobj, "%s", "md");
5088 /* This isn't possible, but as kobject_init_and_add is marked
5089 * __must_check, we must do something with the result
5091 pr_debug("md: cannot register %s/md - name in use\n",
5095 if (mddev->kobj.sd &&
5096 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
5097 pr_debug("pointless warning\n");
5098 mutex_unlock(&mddev->open_mutex);
5100 mutex_unlock(&disks_mutex);
5101 if (!error && mddev->kobj.sd) {
5102 kobject_uevent(&mddev->kobj, KOBJ_ADD);
5103 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
5109 static struct kobject *md_probe(dev_t dev, int *part, void *data)
5111 md_alloc(dev, NULL);
5115 static int add_named_array(const char *val, struct kernel_param *kp)
5117 /* val must be "md_*" where * is not all digits.
5118 * We allocate an array with a large free minor number, and
5119 * set the name to val. val must not already be an active name.
5121 int len = strlen(val);
5122 char buf[DISK_NAME_LEN];
5124 while (len && val[len-1] == '\n')
5126 if (len >= DISK_NAME_LEN)
5128 strlcpy(buf, val, len+1);
5129 if (strncmp(buf, "md_", 3) != 0)
5131 return md_alloc(0, buf);
5134 static void md_safemode_timeout(unsigned long data)
5136 struct mddev *mddev = (struct mddev *) data;
5138 if (!atomic_read(&mddev->writes_pending)) {
5139 mddev->safemode = 1;
5140 if (mddev->external)
5141 sysfs_notify_dirent_safe(mddev->sysfs_state);
5143 md_wakeup_thread(mddev->thread);
5146 static int start_dirty_degraded;
5148 int md_run(struct mddev *mddev)
5151 struct md_rdev *rdev;
5152 struct md_personality *pers;
5154 if (list_empty(&mddev->disks))
5155 /* cannot run an array with no devices.. */
5160 /* Cannot run until previous stop completes properly */
5161 if (mddev->sysfs_active)
5165 * Analyze all RAID superblock(s)
5167 if (!mddev->raid_disks) {
5168 if (!mddev->persistent)
5173 if (mddev->level != LEVEL_NONE)
5174 request_module("md-level-%d", mddev->level);
5175 else if (mddev->clevel[0])
5176 request_module("md-%s", mddev->clevel);
5179 * Drop all container device buffers, from now on
5180 * the only valid external interface is through the md
5183 rdev_for_each(rdev, mddev) {
5184 if (test_bit(Faulty, &rdev->flags))
5186 sync_blockdev(rdev->bdev);
5187 invalidate_bdev(rdev->bdev);
5189 /* perform some consistency tests on the device.
5190 * We don't want the data to overlap the metadata,
5191 * Internal Bitmap issues have been handled elsewhere.
5193 if (rdev->meta_bdev) {
5194 /* Nothing to check */;
5195 } else if (rdev->data_offset < rdev->sb_start) {
5196 if (mddev->dev_sectors &&
5197 rdev->data_offset + mddev->dev_sectors
5199 pr_warn("md: %s: data overlaps metadata\n",
5204 if (rdev->sb_start + rdev->sb_size/512
5205 > rdev->data_offset) {
5206 pr_warn("md: %s: metadata overlaps data\n",
5211 sysfs_notify_dirent_safe(rdev->sysfs_state);
5214 if (mddev->bio_set == NULL) {
5215 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
5216 if (!mddev->bio_set)
5220 spin_lock(&pers_lock);
5221 pers = find_pers(mddev->level, mddev->clevel);
5222 if (!pers || !try_module_get(pers->owner)) {
5223 spin_unlock(&pers_lock);
5224 if (mddev->level != LEVEL_NONE)
5225 pr_warn("md: personality for level %d is not loaded!\n",
5228 pr_warn("md: personality for level %s is not loaded!\n",
5232 spin_unlock(&pers_lock);
5233 if (mddev->level != pers->level) {
5234 mddev->level = pers->level;
5235 mddev->new_level = pers->level;
5237 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
5239 if (mddev->reshape_position != MaxSector &&
5240 pers->start_reshape == NULL) {
5241 /* This personality cannot handle reshaping... */
5242 module_put(pers->owner);
5246 if (pers->sync_request) {
5247 /* Warn if this is a potentially silly
5250 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5251 struct md_rdev *rdev2;
5254 rdev_for_each(rdev, mddev)
5255 rdev_for_each(rdev2, mddev) {
5257 rdev->bdev->bd_contains ==
5258 rdev2->bdev->bd_contains) {
5259 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5261 bdevname(rdev->bdev,b),
5262 bdevname(rdev2->bdev,b2));
5268 pr_warn("True protection against single-disk failure might be compromised.\n");
5271 mddev->recovery = 0;
5272 /* may be over-ridden by personality */
5273 mddev->resync_max_sectors = mddev->dev_sectors;
5275 mddev->ok_start_degraded = start_dirty_degraded;
5277 if (start_readonly && mddev->ro == 0)
5278 mddev->ro = 2; /* read-only, but switch on first write */
5281 * NOTE: some pers->run(), for example r5l_recovery_log(), wakes
5282 * up mddev->thread. It is important to initialize critical
5283 * resources for mddev->thread BEFORE calling pers->run().
5285 err = pers->run(mddev);
5287 pr_warn("md: pers->run() failed ...\n");
5288 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
5289 WARN_ONCE(!mddev->external_size,
5290 "%s: default size too small, but 'external_size' not in effect?\n",
5292 pr_warn("md: invalid array_size %llu > default size %llu\n",
5293 (unsigned long long)mddev->array_sectors / 2,
5294 (unsigned long long)pers->size(mddev, 0, 0) / 2);
5297 if (err == 0 && pers->sync_request &&
5298 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
5299 struct bitmap *bitmap;
5301 bitmap = bitmap_create(mddev, -1);
5302 if (IS_ERR(bitmap)) {
5303 err = PTR_ERR(bitmap);
5304 pr_warn("%s: failed to create bitmap (%d)\n",
5305 mdname(mddev), err);
5307 mddev->bitmap = bitmap;
5311 mddev_detach(mddev);
5313 pers->free(mddev, mddev->private);
5314 mddev->private = NULL;
5315 module_put(pers->owner);
5316 bitmap_destroy(mddev);
5322 rdev_for_each(rdev, mddev) {
5323 if (rdev->raid_disk >= 0 &&
5324 !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
5329 if (mddev->degraded)
5332 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mddev->queue);
5334 queue_flag_clear_unlocked(QUEUE_FLAG_NONROT, mddev->queue);
5335 mddev->queue->backing_dev_info->congested_data = mddev;
5336 mddev->queue->backing_dev_info->congested_fn = md_congested;
5338 if (pers->sync_request) {
5339 if (mddev->kobj.sd &&
5340 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
5341 pr_warn("md: cannot register extra attributes for %s\n",
5343 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
5344 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
5347 atomic_set(&mddev->writes_pending,0);
5348 atomic_set(&mddev->max_corr_read_errors,
5349 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
5350 mddev->safemode = 0;
5351 if (mddev_is_clustered(mddev))
5352 mddev->safemode_delay = 0;
5354 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
5357 spin_lock(&mddev->lock);
5359 spin_unlock(&mddev->lock);
5360 rdev_for_each(rdev, mddev)
5361 if (rdev->raid_disk >= 0)
5362 if (sysfs_link_rdev(mddev, rdev))
5363 /* failure here is OK */;
5365 if (mddev->degraded && !mddev->ro)
5366 /* This ensures that recovering status is reported immediately
5367 * via sysfs - until a lack of spares is confirmed.
5369 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
5370 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5372 if (mddev->sb_flags)
5373 md_update_sb(mddev, 0);
5375 md_new_event(mddev);
5376 sysfs_notify_dirent_safe(mddev->sysfs_state);
5377 sysfs_notify_dirent_safe(mddev->sysfs_action);
5378 sysfs_notify(&mddev->kobj, NULL, "degraded");
5381 EXPORT_SYMBOL_GPL(md_run);
5383 static int do_md_run(struct mddev *mddev)
5387 err = md_run(mddev);
5390 err = bitmap_load(mddev);
5392 bitmap_destroy(mddev);
5396 if (mddev_is_clustered(mddev))
5397 md_allow_write(mddev);
5399 md_wakeup_thread(mddev->thread);
5400 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5402 set_capacity(mddev->gendisk, mddev->array_sectors);
5403 revalidate_disk(mddev->gendisk);
5405 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5410 static int restart_array(struct mddev *mddev)
5412 struct gendisk *disk = mddev->gendisk;
5414 /* Complain if it has no devices */
5415 if (list_empty(&mddev->disks))
5421 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5422 struct md_rdev *rdev;
5423 bool has_journal = false;
5426 rdev_for_each_rcu(rdev, mddev) {
5427 if (test_bit(Journal, &rdev->flags) &&
5428 !test_bit(Faulty, &rdev->flags)) {
5435 /* Don't restart rw with journal missing/faulty */
5440 mddev->safemode = 0;
5442 set_disk_ro(disk, 0);
5443 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev));
5444 /* Kick recovery or resync if necessary */
5445 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5446 md_wakeup_thread(mddev->thread);
5447 md_wakeup_thread(mddev->sync_thread);
5448 sysfs_notify_dirent_safe(mddev->sysfs_state);
5452 static void md_clean(struct mddev *mddev)
5454 mddev->array_sectors = 0;
5455 mddev->external_size = 0;
5456 mddev->dev_sectors = 0;
5457 mddev->raid_disks = 0;
5458 mddev->recovery_cp = 0;
5459 mddev->resync_min = 0;
5460 mddev->resync_max = MaxSector;
5461 mddev->reshape_position = MaxSector;
5462 mddev->external = 0;
5463 mddev->persistent = 0;
5464 mddev->level = LEVEL_NONE;
5465 mddev->clevel[0] = 0;
5467 mddev->sb_flags = 0;
5469 mddev->metadata_type[0] = 0;
5470 mddev->chunk_sectors = 0;
5471 mddev->ctime = mddev->utime = 0;
5473 mddev->max_disks = 0;
5475 mddev->can_decrease_events = 0;
5476 mddev->delta_disks = 0;
5477 mddev->reshape_backwards = 0;
5478 mddev->new_level = LEVEL_NONE;
5479 mddev->new_layout = 0;
5480 mddev->new_chunk_sectors = 0;
5481 mddev->curr_resync = 0;
5482 atomic64_set(&mddev->resync_mismatches, 0);
5483 mddev->suspend_lo = mddev->suspend_hi = 0;
5484 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5485 mddev->recovery = 0;
5488 mddev->degraded = 0;
5489 mddev->safemode = 0;
5490 mddev->private = NULL;
5491 mddev->cluster_info = NULL;
5492 mddev->bitmap_info.offset = 0;
5493 mddev->bitmap_info.default_offset = 0;
5494 mddev->bitmap_info.default_space = 0;
5495 mddev->bitmap_info.chunksize = 0;
5496 mddev->bitmap_info.daemon_sleep = 0;
5497 mddev->bitmap_info.max_write_behind = 0;
5498 mddev->bitmap_info.nodes = 0;
5501 static void __md_stop_writes(struct mddev *mddev)
5503 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5504 flush_workqueue(md_misc_wq);
5505 if (mddev->sync_thread) {
5506 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5507 md_reap_sync_thread(mddev);
5510 del_timer_sync(&mddev->safemode_timer);
5512 if (mddev->pers && mddev->pers->quiesce) {
5513 mddev->pers->quiesce(mddev, 1);
5514 mddev->pers->quiesce(mddev, 0);
5516 bitmap_flush(mddev);
5518 if (mddev->ro == 0 &&
5519 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
5521 /* mark array as shutdown cleanly */
5522 if (!mddev_is_clustered(mddev))
5524 md_update_sb(mddev, 1);
5528 void md_stop_writes(struct mddev *mddev)
5530 mddev_lock_nointr(mddev);
5531 __md_stop_writes(mddev);
5532 mddev_unlock(mddev);
5534 EXPORT_SYMBOL_GPL(md_stop_writes);
5536 static void mddev_detach(struct mddev *mddev)
5538 struct bitmap *bitmap = mddev->bitmap;
5539 /* wait for behind writes to complete */
5540 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
5541 pr_debug("md:%s: behind writes in progress - waiting to stop.\n",
5543 /* need to kick something here to make sure I/O goes? */
5544 wait_event(bitmap->behind_wait,
5545 atomic_read(&bitmap->behind_writes) == 0);
5547 if (mddev->pers && mddev->pers->quiesce) {
5548 mddev->pers->quiesce(mddev, 1);
5549 mddev->pers->quiesce(mddev, 0);
5551 md_unregister_thread(&mddev->thread);
5553 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5556 static void __md_stop(struct mddev *mddev)
5558 struct md_personality *pers = mddev->pers;
5559 mddev_detach(mddev);
5560 /* Ensure ->event_work is done */
5561 flush_workqueue(md_misc_wq);
5562 spin_lock(&mddev->lock);
5564 spin_unlock(&mddev->lock);
5565 pers->free(mddev, mddev->private);
5566 mddev->private = NULL;
5567 if (pers->sync_request && mddev->to_remove == NULL)
5568 mddev->to_remove = &md_redundancy_group;
5569 module_put(pers->owner);
5570 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5573 void md_stop(struct mddev *mddev)
5575 /* stop the array and free an attached data structures.
5576 * This is called from dm-raid
5579 bitmap_destroy(mddev);
5581 bioset_free(mddev->bio_set);
5584 EXPORT_SYMBOL_GPL(md_stop);
5586 static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
5591 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5593 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5594 md_wakeup_thread(mddev->thread);
5596 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
5597 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5598 if (mddev->sync_thread)
5599 /* Thread might be blocked waiting for metadata update
5600 * which will now never happen */
5601 wake_up_process(mddev->sync_thread->tsk);
5603 if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
5605 mddev_unlock(mddev);
5606 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5608 wait_event(mddev->sb_wait,
5609 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
5610 mddev_lock_nointr(mddev);
5612 mutex_lock(&mddev->open_mutex);
5613 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
5614 mddev->sync_thread ||
5615 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
5616 pr_warn("md: %s still in use.\n",mdname(mddev));
5618 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5619 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5620 md_wakeup_thread(mddev->thread);
5626 __md_stop_writes(mddev);
5632 set_disk_ro(mddev->gendisk, 1);
5633 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5634 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5635 md_wakeup_thread(mddev->thread);
5636 sysfs_notify_dirent_safe(mddev->sysfs_state);
5640 mutex_unlock(&mddev->open_mutex);
5645 * 0 - completely stop and dis-assemble array
5646 * 2 - stop but do not disassemble array
5648 static int do_md_stop(struct mddev *mddev, int mode,
5649 struct block_device *bdev)
5651 struct gendisk *disk = mddev->gendisk;
5652 struct md_rdev *rdev;
5655 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5657 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5658 md_wakeup_thread(mddev->thread);
5660 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
5661 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5662 if (mddev->sync_thread)
5663 /* Thread might be blocked waiting for metadata update
5664 * which will now never happen */
5665 wake_up_process(mddev->sync_thread->tsk);
5667 mddev_unlock(mddev);
5668 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5669 !test_bit(MD_RECOVERY_RUNNING,
5670 &mddev->recovery)));
5671 mddev_lock_nointr(mddev);
5673 mutex_lock(&mddev->open_mutex);
5674 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
5675 mddev->sysfs_active ||
5676 mddev->sync_thread ||
5677 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
5678 pr_warn("md: %s still in use.\n",mdname(mddev));
5679 mutex_unlock(&mddev->open_mutex);
5681 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5682 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5683 md_wakeup_thread(mddev->thread);
5689 set_disk_ro(disk, 0);
5691 __md_stop_writes(mddev);
5693 mddev->queue->backing_dev_info->congested_fn = NULL;
5695 /* tell userspace to handle 'inactive' */
5696 sysfs_notify_dirent_safe(mddev->sysfs_state);
5698 rdev_for_each(rdev, mddev)
5699 if (rdev->raid_disk >= 0)
5700 sysfs_unlink_rdev(mddev, rdev);
5702 set_capacity(disk, 0);
5703 mutex_unlock(&mddev->open_mutex);
5705 revalidate_disk(disk);
5710 mutex_unlock(&mddev->open_mutex);
5712 * Free resources if final stop
5715 pr_info("md: %s stopped.\n", mdname(mddev));
5717 bitmap_destroy(mddev);
5718 if (mddev->bitmap_info.file) {
5719 struct file *f = mddev->bitmap_info.file;
5720 spin_lock(&mddev->lock);
5721 mddev->bitmap_info.file = NULL;
5722 spin_unlock(&mddev->lock);
5725 mddev->bitmap_info.offset = 0;
5727 export_array(mddev);
5730 if (mddev->hold_active == UNTIL_STOP)
5731 mddev->hold_active = 0;
5733 md_new_event(mddev);
5734 sysfs_notify_dirent_safe(mddev->sysfs_state);
5739 static void autorun_array(struct mddev *mddev)
5741 struct md_rdev *rdev;
5744 if (list_empty(&mddev->disks))
5747 pr_info("md: running: ");
5749 rdev_for_each(rdev, mddev) {
5750 char b[BDEVNAME_SIZE];
5751 pr_cont("<%s>", bdevname(rdev->bdev,b));
5755 err = do_md_run(mddev);
5757 pr_warn("md: do_md_run() returned %d\n", err);
5758 do_md_stop(mddev, 0, NULL);
5763 * lets try to run arrays based on all disks that have arrived
5764 * until now. (those are in pending_raid_disks)
5766 * the method: pick the first pending disk, collect all disks with
5767 * the same UUID, remove all from the pending list and put them into
5768 * the 'same_array' list. Then order this list based on superblock
5769 * update time (freshest comes first), kick out 'old' disks and
5770 * compare superblocks. If everything's fine then run it.
5772 * If "unit" is allocated, then bump its reference count
5774 static void autorun_devices(int part)
5776 struct md_rdev *rdev0, *rdev, *tmp;
5777 struct mddev *mddev;
5778 char b[BDEVNAME_SIZE];
5780 pr_info("md: autorun ...\n");
5781 while (!list_empty(&pending_raid_disks)) {
5784 LIST_HEAD(candidates);
5785 rdev0 = list_entry(pending_raid_disks.next,
5786 struct md_rdev, same_set);
5788 pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b));
5789 INIT_LIST_HEAD(&candidates);
5790 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
5791 if (super_90_load(rdev, rdev0, 0) >= 0) {
5792 pr_debug("md: adding %s ...\n",
5793 bdevname(rdev->bdev,b));
5794 list_move(&rdev->same_set, &candidates);
5797 * now we have a set of devices, with all of them having
5798 * mostly sane superblocks. It's time to allocate the
5802 dev = MKDEV(mdp_major,
5803 rdev0->preferred_minor << MdpMinorShift);
5804 unit = MINOR(dev) >> MdpMinorShift;
5806 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5809 if (rdev0->preferred_minor != unit) {
5810 pr_warn("md: unit number in %s is bad: %d\n",
5811 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5815 md_probe(dev, NULL, NULL);
5816 mddev = mddev_find(dev);
5817 if (!mddev || !mddev->gendisk) {
5822 if (mddev_lock(mddev))
5823 pr_warn("md: %s locked, cannot run\n", mdname(mddev));
5824 else if (mddev->raid_disks || mddev->major_version
5825 || !list_empty(&mddev->disks)) {
5826 pr_warn("md: %s already running, cannot run %s\n",
5827 mdname(mddev), bdevname(rdev0->bdev,b));
5828 mddev_unlock(mddev);
5830 pr_debug("md: created %s\n", mdname(mddev));
5831 mddev->persistent = 1;
5832 rdev_for_each_list(rdev, tmp, &candidates) {
5833 list_del_init(&rdev->same_set);
5834 if (bind_rdev_to_array(rdev, mddev))
5837 autorun_array(mddev);
5838 mddev_unlock(mddev);
5840 /* on success, candidates will be empty, on error
5843 rdev_for_each_list(rdev, tmp, &candidates) {
5844 list_del_init(&rdev->same_set);
5849 pr_info("md: ... autorun DONE.\n");
5851 #endif /* !MODULE */
5853 static int get_version(void __user *arg)
5857 ver.major = MD_MAJOR_VERSION;
5858 ver.minor = MD_MINOR_VERSION;
5859 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5861 if (copy_to_user(arg, &ver, sizeof(ver)))
5867 static int get_array_info(struct mddev *mddev, void __user *arg)
5869 mdu_array_info_t info;
5870 int nr,working,insync,failed,spare;
5871 struct md_rdev *rdev;
5873 nr = working = insync = failed = spare = 0;
5875 rdev_for_each_rcu(rdev, mddev) {
5877 if (test_bit(Faulty, &rdev->flags))
5881 if (test_bit(In_sync, &rdev->flags))
5883 else if (test_bit(Journal, &rdev->flags))
5884 /* TODO: add journal count to md_u.h */
5892 info.major_version = mddev->major_version;
5893 info.minor_version = mddev->minor_version;
5894 info.patch_version = MD_PATCHLEVEL_VERSION;
5895 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
5896 info.level = mddev->level;
5897 info.size = mddev->dev_sectors / 2;
5898 if (info.size != mddev->dev_sectors / 2) /* overflow */
5901 info.raid_disks = mddev->raid_disks;
5902 info.md_minor = mddev->md_minor;
5903 info.not_persistent= !mddev->persistent;
5905 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
5908 info.state = (1<<MD_SB_CLEAN);
5909 if (mddev->bitmap && mddev->bitmap_info.offset)
5910 info.state |= (1<<MD_SB_BITMAP_PRESENT);
5911 if (mddev_is_clustered(mddev))
5912 info.state |= (1<<MD_SB_CLUSTERED);
5913 info.active_disks = insync;
5914 info.working_disks = working;
5915 info.failed_disks = failed;
5916 info.spare_disks = spare;
5918 info.layout = mddev->layout;
5919 info.chunk_size = mddev->chunk_sectors << 9;
5921 if (copy_to_user(arg, &info, sizeof(info)))
5927 static int get_bitmap_file(struct mddev *mddev, void __user * arg)
5929 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5933 file = kzalloc(sizeof(*file), GFP_NOIO);
5938 spin_lock(&mddev->lock);
5939 /* bitmap enabled */
5940 if (mddev->bitmap_info.file) {
5941 ptr = file_path(mddev->bitmap_info.file, file->pathname,
5942 sizeof(file->pathname));
5946 memmove(file->pathname, ptr,
5947 sizeof(file->pathname)-(ptr-file->pathname));
5949 spin_unlock(&mddev->lock);
5952 copy_to_user(arg, file, sizeof(*file)))
5959 static int get_disk_info(struct mddev *mddev, void __user * arg)
5961 mdu_disk_info_t info;
5962 struct md_rdev *rdev;
5964 if (copy_from_user(&info, arg, sizeof(info)))
5968 rdev = md_find_rdev_nr_rcu(mddev, info.number);
5970 info.major = MAJOR(rdev->bdev->bd_dev);
5971 info.minor = MINOR(rdev->bdev->bd_dev);
5972 info.raid_disk = rdev->raid_disk;
5974 if (test_bit(Faulty, &rdev->flags))
5975 info.state |= (1<<MD_DISK_FAULTY);
5976 else if (test_bit(In_sync, &rdev->flags)) {
5977 info.state |= (1<<MD_DISK_ACTIVE);
5978 info.state |= (1<<MD_DISK_SYNC);
5980 if (test_bit(Journal, &rdev->flags))
5981 info.state |= (1<<MD_DISK_JOURNAL);
5982 if (test_bit(WriteMostly, &rdev->flags))
5983 info.state |= (1<<MD_DISK_WRITEMOSTLY);
5984 if (test_bit(FailFast, &rdev->flags))
5985 info.state |= (1<<MD_DISK_FAILFAST);
5987 info.major = info.minor = 0;
5988 info.raid_disk = -1;
5989 info.state = (1<<MD_DISK_REMOVED);
5993 if (copy_to_user(arg, &info, sizeof(info)))
5999 static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
6001 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
6002 struct md_rdev *rdev;
6003 dev_t dev = MKDEV(info->major,info->minor);
6005 if (mddev_is_clustered(mddev) &&
6006 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
6007 pr_warn("%s: Cannot add to clustered mddev.\n",
6012 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
6015 if (!mddev->raid_disks) {
6017 /* expecting a device which has a superblock */
6018 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
6020 pr_warn("md: md_import_device returned %ld\n",
6022 return PTR_ERR(rdev);
6024 if (!list_empty(&mddev->disks)) {
6025 struct md_rdev *rdev0
6026 = list_entry(mddev->disks.next,
6027 struct md_rdev, same_set);
6028 err = super_types[mddev->major_version]
6029 .load_super(rdev, rdev0, mddev->minor_version);
6031 pr_warn("md: %s has different UUID to %s\n",
6032 bdevname(rdev->bdev,b),
6033 bdevname(rdev0->bdev,b2));
6038 err = bind_rdev_to_array(rdev, mddev);
6045 * add_new_disk can be used once the array is assembled
6046 * to add "hot spares". They must already have a superblock
6051 if (!mddev->pers->hot_add_disk) {
6052 pr_warn("%s: personality does not support diskops!\n",
6056 if (mddev->persistent)
6057 rdev = md_import_device(dev, mddev->major_version,
6058 mddev->minor_version);
6060 rdev = md_import_device(dev, -1, -1);
6062 pr_warn("md: md_import_device returned %ld\n",
6064 return PTR_ERR(rdev);
6066 /* set saved_raid_disk if appropriate */
6067 if (!mddev->persistent) {
6068 if (info->state & (1<<MD_DISK_SYNC) &&
6069 info->raid_disk < mddev->raid_disks) {
6070 rdev->raid_disk = info->raid_disk;
6071 set_bit(In_sync, &rdev->flags);
6072 clear_bit(Bitmap_sync, &rdev->flags);
6074 rdev->raid_disk = -1;
6075 rdev->saved_raid_disk = rdev->raid_disk;
6077 super_types[mddev->major_version].
6078 validate_super(mddev, rdev);
6079 if ((info->state & (1<<MD_DISK_SYNC)) &&
6080 rdev->raid_disk != info->raid_disk) {
6081 /* This was a hot-add request, but events doesn't
6082 * match, so reject it.
6088 clear_bit(In_sync, &rdev->flags); /* just to be sure */
6089 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6090 set_bit(WriteMostly, &rdev->flags);
6092 clear_bit(WriteMostly, &rdev->flags);
6093 if (info->state & (1<<MD_DISK_FAILFAST))
6094 set_bit(FailFast, &rdev->flags);
6096 clear_bit(FailFast, &rdev->flags);
6098 if (info->state & (1<<MD_DISK_JOURNAL)) {
6099 struct md_rdev *rdev2;
6100 bool has_journal = false;
6102 /* make sure no existing journal disk */
6103 rdev_for_each(rdev2, mddev) {
6104 if (test_bit(Journal, &rdev2->flags)) {
6113 set_bit(Journal, &rdev->flags);
6116 * check whether the device shows up in other nodes
6118 if (mddev_is_clustered(mddev)) {
6119 if (info->state & (1 << MD_DISK_CANDIDATE))
6120 set_bit(Candidate, &rdev->flags);
6121 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
6122 /* --add initiated by this node */
6123 err = md_cluster_ops->add_new_disk(mddev, rdev);
6131 rdev->raid_disk = -1;
6132 err = bind_rdev_to_array(rdev, mddev);
6137 if (mddev_is_clustered(mddev)) {
6138 if (info->state & (1 << MD_DISK_CANDIDATE)) {
6140 err = md_cluster_ops->new_disk_ack(mddev,
6143 md_kick_rdev_from_array(rdev);
6147 md_cluster_ops->add_new_disk_cancel(mddev);
6149 err = add_bound_rdev(rdev);
6153 err = add_bound_rdev(rdev);
6158 /* otherwise, add_new_disk is only allowed
6159 * for major_version==0 superblocks
6161 if (mddev->major_version != 0) {
6162 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev));
6166 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6168 rdev = md_import_device(dev, -1, 0);
6170 pr_warn("md: error, md_import_device() returned %ld\n",
6172 return PTR_ERR(rdev);
6174 rdev->desc_nr = info->number;
6175 if (info->raid_disk < mddev->raid_disks)
6176 rdev->raid_disk = info->raid_disk;
6178 rdev->raid_disk = -1;
6180 if (rdev->raid_disk < mddev->raid_disks)
6181 if (info->state & (1<<MD_DISK_SYNC))
6182 set_bit(In_sync, &rdev->flags);
6184 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6185 set_bit(WriteMostly, &rdev->flags);
6186 if (info->state & (1<<MD_DISK_FAILFAST))
6187 set_bit(FailFast, &rdev->flags);
6189 if (!mddev->persistent) {
6190 pr_debug("md: nonpersistent superblock ...\n");
6191 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6193 rdev->sb_start = calc_dev_sboffset(rdev);
6194 rdev->sectors = rdev->sb_start;
6196 err = bind_rdev_to_array(rdev, mddev);
6206 static int hot_remove_disk(struct mddev *mddev, dev_t dev)
6208 char b[BDEVNAME_SIZE];
6209 struct md_rdev *rdev;
6211 rdev = find_rdev(mddev, dev);
6215 if (rdev->raid_disk < 0)
6218 clear_bit(Blocked, &rdev->flags);
6219 remove_and_add_spares(mddev, rdev);
6221 if (rdev->raid_disk >= 0)
6225 if (mddev_is_clustered(mddev))
6226 md_cluster_ops->remove_disk(mddev, rdev);
6228 md_kick_rdev_from_array(rdev);
6229 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
6231 md_wakeup_thread(mddev->thread);
6233 md_update_sb(mddev, 1);
6234 md_new_event(mddev);
6238 pr_debug("md: cannot remove active disk %s from %s ...\n",
6239 bdevname(rdev->bdev,b), mdname(mddev));
6243 static int hot_add_disk(struct mddev *mddev, dev_t dev)
6245 char b[BDEVNAME_SIZE];
6247 struct md_rdev *rdev;
6252 if (mddev->major_version != 0) {
6253 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
6257 if (!mddev->pers->hot_add_disk) {
6258 pr_warn("%s: personality does not support diskops!\n",
6263 rdev = md_import_device(dev, -1, 0);
6265 pr_warn("md: error, md_import_device() returned %ld\n",
6270 if (mddev->persistent)
6271 rdev->sb_start = calc_dev_sboffset(rdev);
6273 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6275 rdev->sectors = rdev->sb_start;
6277 if (test_bit(Faulty, &rdev->flags)) {
6278 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
6279 bdevname(rdev->bdev,b), mdname(mddev));
6284 clear_bit(In_sync, &rdev->flags);
6286 rdev->saved_raid_disk = -1;
6287 err = bind_rdev_to_array(rdev, mddev);
6292 * The rest should better be atomic, we can have disk failures
6293 * noticed in interrupt contexts ...
6296 rdev->raid_disk = -1;
6298 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
6300 md_update_sb(mddev, 1);
6302 * Kick recovery, maybe this spare has to be added to the
6303 * array immediately.
6305 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6306 md_wakeup_thread(mddev->thread);
6307 md_new_event(mddev);
6315 static int set_bitmap_file(struct mddev *mddev, int fd)
6320 if (!mddev->pers->quiesce || !mddev->thread)
6322 if (mddev->recovery || mddev->sync_thread)
6324 /* we should be able to change the bitmap.. */
6328 struct inode *inode;
6331 if (mddev->bitmap || mddev->bitmap_info.file)
6332 return -EEXIST; /* cannot add when bitmap is present */
6336 pr_warn("%s: error: failed to get bitmap file\n",
6341 inode = f->f_mapping->host;
6342 if (!S_ISREG(inode->i_mode)) {
6343 pr_warn("%s: error: bitmap file must be a regular file\n",
6346 } else if (!(f->f_mode & FMODE_WRITE)) {
6347 pr_warn("%s: error: bitmap file must open for write\n",
6350 } else if (atomic_read(&inode->i_writecount) != 1) {
6351 pr_warn("%s: error: bitmap file is already in use\n",
6359 mddev->bitmap_info.file = f;
6360 mddev->bitmap_info.offset = 0; /* file overrides offset */
6361 } else if (mddev->bitmap == NULL)
6362 return -ENOENT; /* cannot remove what isn't there */
6365 mddev->pers->quiesce(mddev, 1);
6367 struct bitmap *bitmap;
6369 bitmap = bitmap_create(mddev, -1);
6370 if (!IS_ERR(bitmap)) {
6371 mddev->bitmap = bitmap;
6372 err = bitmap_load(mddev);
6374 err = PTR_ERR(bitmap);
6376 if (fd < 0 || err) {
6377 bitmap_destroy(mddev);
6378 fd = -1; /* make sure to put the file */
6380 mddev->pers->quiesce(mddev, 0);
6383 struct file *f = mddev->bitmap_info.file;
6385 spin_lock(&mddev->lock);
6386 mddev->bitmap_info.file = NULL;
6387 spin_unlock(&mddev->lock);
6396 * set_array_info is used two different ways
6397 * The original usage is when creating a new array.
6398 * In this usage, raid_disks is > 0 and it together with
6399 * level, size, not_persistent,layout,chunksize determine the
6400 * shape of the array.
6401 * This will always create an array with a type-0.90.0 superblock.
6402 * The newer usage is when assembling an array.
6403 * In this case raid_disks will be 0, and the major_version field is
6404 * use to determine which style super-blocks are to be found on the devices.
6405 * The minor and patch _version numbers are also kept incase the
6406 * super_block handler wishes to interpret them.
6408 static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
6411 if (info->raid_disks == 0) {
6412 /* just setting version number for superblock loading */
6413 if (info->major_version < 0 ||
6414 info->major_version >= ARRAY_SIZE(super_types) ||
6415 super_types[info->major_version].name == NULL) {
6416 /* maybe try to auto-load a module? */
6417 pr_warn("md: superblock version %d not known\n",
6418 info->major_version);
6421 mddev->major_version = info->major_version;
6422 mddev->minor_version = info->minor_version;
6423 mddev->patch_version = info->patch_version;
6424 mddev->persistent = !info->not_persistent;
6425 /* ensure mddev_put doesn't delete this now that there
6426 * is some minimal configuration.
6428 mddev->ctime = ktime_get_real_seconds();
6431 mddev->major_version = MD_MAJOR_VERSION;
6432 mddev->minor_version = MD_MINOR_VERSION;
6433 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6434 mddev->ctime = ktime_get_real_seconds();
6436 mddev->level = info->level;
6437 mddev->clevel[0] = 0;
6438 mddev->dev_sectors = 2 * (sector_t)info->size;
6439 mddev->raid_disks = info->raid_disks;
6440 /* don't set md_minor, it is determined by which /dev/md* was
6443 if (info->state & (1<<MD_SB_CLEAN))
6444 mddev->recovery_cp = MaxSector;
6446 mddev->recovery_cp = 0;
6447 mddev->persistent = ! info->not_persistent;
6448 mddev->external = 0;
6450 mddev->layout = info->layout;
6451 mddev->chunk_sectors = info->chunk_size >> 9;
6453 if (mddev->persistent) {
6454 mddev->max_disks = MD_SB_DISKS;
6456 mddev->sb_flags = 0;
6458 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
6460 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
6461 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
6462 mddev->bitmap_info.offset = 0;
6464 mddev->reshape_position = MaxSector;
6467 * Generate a 128 bit UUID
6469 get_random_bytes(mddev->uuid, 16);
6471 mddev->new_level = mddev->level;
6472 mddev->new_chunk_sectors = mddev->chunk_sectors;
6473 mddev->new_layout = mddev->layout;
6474 mddev->delta_disks = 0;
6475 mddev->reshape_backwards = 0;
6480 void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
6482 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6484 if (mddev->external_size)
6487 mddev->array_sectors = array_sectors;
6489 EXPORT_SYMBOL(md_set_array_sectors);
6491 static int update_size(struct mddev *mddev, sector_t num_sectors)
6493 struct md_rdev *rdev;
6495 int fit = (num_sectors == 0);
6497 /* cluster raid doesn't support update size */
6498 if (mddev_is_clustered(mddev))
6501 if (mddev->pers->resize == NULL)
6503 /* The "num_sectors" is the number of sectors of each device that
6504 * is used. This can only make sense for arrays with redundancy.
6505 * linear and raid0 always use whatever space is available. We can only
6506 * consider changing this number if no resync or reconstruction is
6507 * happening, and if the new size is acceptable. It must fit before the
6508 * sb_start or, if that is <data_offset, it must fit before the size
6509 * of each device. If num_sectors is zero, we find the largest size
6512 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6518 rdev_for_each(rdev, mddev) {
6519 sector_t avail = rdev->sectors;
6521 if (fit && (num_sectors == 0 || num_sectors > avail))
6522 num_sectors = avail;
6523 if (avail < num_sectors)
6526 rv = mddev->pers->resize(mddev, num_sectors);
6529 set_capacity(mddev->gendisk, mddev->array_sectors);
6530 revalidate_disk(mddev->gendisk);
6536 static int update_raid_disks(struct mddev *mddev, int raid_disks)
6539 struct md_rdev *rdev;
6540 /* change the number of raid disks */
6541 if (mddev->pers->check_reshape == NULL)
6545 if (raid_disks <= 0 ||
6546 (mddev->max_disks && raid_disks >= mddev->max_disks))
6548 if (mddev->sync_thread ||
6549 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6550 mddev->reshape_position != MaxSector)
6553 rdev_for_each(rdev, mddev) {
6554 if (mddev->raid_disks < raid_disks &&
6555 rdev->data_offset < rdev->new_data_offset)
6557 if (mddev->raid_disks > raid_disks &&
6558 rdev->data_offset > rdev->new_data_offset)
6562 mddev->delta_disks = raid_disks - mddev->raid_disks;
6563 if (mddev->delta_disks < 0)
6564 mddev->reshape_backwards = 1;
6565 else if (mddev->delta_disks > 0)
6566 mddev->reshape_backwards = 0;
6568 rv = mddev->pers->check_reshape(mddev);
6570 mddev->delta_disks = 0;
6571 mddev->reshape_backwards = 0;
6577 * update_array_info is used to change the configuration of an
6579 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6580 * fields in the info are checked against the array.
6581 * Any differences that cannot be handled will cause an error.
6582 * Normally, only one change can be managed at a time.
6584 static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
6590 /* calculate expected state,ignoring low bits */
6591 if (mddev->bitmap && mddev->bitmap_info.offset)
6592 state |= (1 << MD_SB_BITMAP_PRESENT);
6594 if (mddev->major_version != info->major_version ||
6595 mddev->minor_version != info->minor_version ||
6596 /* mddev->patch_version != info->patch_version || */
6597 mddev->ctime != info->ctime ||
6598 mddev->level != info->level ||
6599 /* mddev->layout != info->layout || */
6600 mddev->persistent != !info->not_persistent ||
6601 mddev->chunk_sectors != info->chunk_size >> 9 ||
6602 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6603 ((state^info->state) & 0xfffffe00)
6606 /* Check there is only one change */
6607 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6609 if (mddev->raid_disks != info->raid_disks)
6611 if (mddev->layout != info->layout)
6613 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6620 if (mddev->layout != info->layout) {
6622 * we don't need to do anything at the md level, the
6623 * personality will take care of it all.
6625 if (mddev->pers->check_reshape == NULL)
6628 mddev->new_layout = info->layout;
6629 rv = mddev->pers->check_reshape(mddev);
6631 mddev->new_layout = mddev->layout;
6635 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6636 rv = update_size(mddev, (sector_t)info->size * 2);
6638 if (mddev->raid_disks != info->raid_disks)
6639 rv = update_raid_disks(mddev, info->raid_disks);
6641 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
6642 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6646 if (mddev->recovery || mddev->sync_thread) {
6650 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6651 struct bitmap *bitmap;
6652 /* add the bitmap */
6653 if (mddev->bitmap) {
6657 if (mddev->bitmap_info.default_offset == 0) {
6661 mddev->bitmap_info.offset =
6662 mddev->bitmap_info.default_offset;
6663 mddev->bitmap_info.space =
6664 mddev->bitmap_info.default_space;
6665 mddev->pers->quiesce(mddev, 1);
6666 bitmap = bitmap_create(mddev, -1);
6667 if (!IS_ERR(bitmap)) {
6668 mddev->bitmap = bitmap;
6669 rv = bitmap_load(mddev);
6671 rv = PTR_ERR(bitmap);
6673 bitmap_destroy(mddev);
6674 mddev->pers->quiesce(mddev, 0);
6676 /* remove the bitmap */
6677 if (!mddev->bitmap) {
6681 if (mddev->bitmap->storage.file) {
6685 if (mddev->bitmap_info.nodes) {
6686 /* hold PW on all the bitmap lock */
6687 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
6688 pr_warn("md: can't change bitmap to none since the array is in use by more than one node\n");
6690 md_cluster_ops->unlock_all_bitmaps(mddev);
6694 mddev->bitmap_info.nodes = 0;
6695 md_cluster_ops->leave(mddev);
6697 mddev->pers->quiesce(mddev, 1);
6698 bitmap_destroy(mddev);
6699 mddev->pers->quiesce(mddev, 0);
6700 mddev->bitmap_info.offset = 0;
6703 md_update_sb(mddev, 1);
6709 static int set_disk_faulty(struct mddev *mddev, dev_t dev)
6711 struct md_rdev *rdev;
6714 if (mddev->pers == NULL)
6718 rdev = find_rdev_rcu(mddev, dev);
6722 md_error(mddev, rdev);
6723 if (!test_bit(Faulty, &rdev->flags))
6731 * We have a problem here : there is no easy way to give a CHS
6732 * virtual geometry. We currently pretend that we have a 2 heads
6733 * 4 sectors (with a BIG number of cylinders...). This drives
6734 * dosfs just mad... ;-)
6736 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6738 struct mddev *mddev = bdev->bd_disk->private_data;
6742 geo->cylinders = mddev->array_sectors / 8;
6746 static inline bool md_ioctl_valid(unsigned int cmd)
6751 case GET_ARRAY_INFO:
6752 case GET_BITMAP_FILE:
6755 case HOT_REMOVE_DISK:
6758 case RESTART_ARRAY_RW:
6760 case SET_ARRAY_INFO:
6761 case SET_BITMAP_FILE:
6762 case SET_DISK_FAULTY:
6765 case CLUSTERED_DISK_NACK:
6772 static int md_ioctl(struct block_device *bdev, fmode_t mode,
6773 unsigned int cmd, unsigned long arg)
6776 void __user *argp = (void __user *)arg;
6777 struct mddev *mddev = NULL;
6780 if (!md_ioctl_valid(cmd))
6785 case GET_ARRAY_INFO:
6789 if (!capable(CAP_SYS_ADMIN))
6794 * Commands dealing with the RAID driver but not any
6799 err = get_version(argp);
6805 autostart_arrays(arg);
6812 * Commands creating/starting a new array:
6815 mddev = bdev->bd_disk->private_data;
6822 /* Some actions do not requires the mutex */
6824 case GET_ARRAY_INFO:
6825 if (!mddev->raid_disks && !mddev->external)
6828 err = get_array_info(mddev, argp);
6832 if (!mddev->raid_disks && !mddev->external)
6835 err = get_disk_info(mddev, argp);
6838 case SET_DISK_FAULTY:
6839 err = set_disk_faulty(mddev, new_decode_dev(arg));
6842 case GET_BITMAP_FILE:
6843 err = get_bitmap_file(mddev, argp);
6848 if (cmd == ADD_NEW_DISK)
6849 /* need to ensure md_delayed_delete() has completed */
6850 flush_workqueue(md_misc_wq);
6852 if (cmd == HOT_REMOVE_DISK)
6853 /* need to ensure recovery thread has run */
6854 wait_event_interruptible_timeout(mddev->sb_wait,
6855 !test_bit(MD_RECOVERY_NEEDED,
6857 msecs_to_jiffies(5000));
6858 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6859 /* Need to flush page cache, and ensure no-one else opens
6862 mutex_lock(&mddev->open_mutex);
6863 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
6864 mutex_unlock(&mddev->open_mutex);
6868 set_bit(MD_CLOSING, &mddev->flags);
6869 mutex_unlock(&mddev->open_mutex);
6870 sync_blockdev(bdev);
6872 err = mddev_lock(mddev);
6874 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
6879 if (cmd == SET_ARRAY_INFO) {
6880 mdu_array_info_t info;
6882 memset(&info, 0, sizeof(info));
6883 else if (copy_from_user(&info, argp, sizeof(info))) {
6888 err = update_array_info(mddev, &info);
6890 pr_warn("md: couldn't update array info. %d\n", err);
6895 if (!list_empty(&mddev->disks)) {
6896 pr_warn("md: array %s already has disks!\n", mdname(mddev));
6900 if (mddev->raid_disks) {
6901 pr_warn("md: array %s already initialised!\n", mdname(mddev));
6905 err = set_array_info(mddev, &info);
6907 pr_warn("md: couldn't set array info. %d\n", err);
6914 * Commands querying/configuring an existing array:
6916 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
6917 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
6918 if ((!mddev->raid_disks && !mddev->external)
6919 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6920 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6921 && cmd != GET_BITMAP_FILE) {
6927 * Commands even a read-only array can execute:
6930 case RESTART_ARRAY_RW:
6931 err = restart_array(mddev);
6935 err = do_md_stop(mddev, 0, bdev);
6939 err = md_set_readonly(mddev, bdev);
6942 case HOT_REMOVE_DISK:
6943 err = hot_remove_disk(mddev, new_decode_dev(arg));
6947 /* We can support ADD_NEW_DISK on read-only arrays
6948 * only if we are re-adding a preexisting device.
6949 * So require mddev->pers and MD_DISK_SYNC.
6952 mdu_disk_info_t info;
6953 if (copy_from_user(&info, argp, sizeof(info)))
6955 else if (!(info.state & (1<<MD_DISK_SYNC)))
6956 /* Need to clear read-only for this */
6959 err = add_new_disk(mddev, &info);
6965 if (get_user(ro, (int __user *)(arg))) {
6971 /* if the bdev is going readonly the value of mddev->ro
6972 * does not matter, no writes are coming
6977 /* are we are already prepared for writes? */
6981 /* transitioning to readauto need only happen for
6982 * arrays that call md_write_start
6985 err = restart_array(mddev);
6988 set_disk_ro(mddev->gendisk, 0);
6995 * The remaining ioctls are changing the state of the
6996 * superblock, so we do not allow them on read-only arrays.
6998 if (mddev->ro && mddev->pers) {
6999 if (mddev->ro == 2) {
7001 sysfs_notify_dirent_safe(mddev->sysfs_state);
7002 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7003 /* mddev_unlock will wake thread */
7004 /* If a device failed while we were read-only, we
7005 * need to make sure the metadata is updated now.
7007 if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) {
7008 mddev_unlock(mddev);
7009 wait_event(mddev->sb_wait,
7010 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) &&
7011 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
7012 mddev_lock_nointr(mddev);
7023 mdu_disk_info_t info;
7024 if (copy_from_user(&info, argp, sizeof(info)))
7027 err = add_new_disk(mddev, &info);
7031 case CLUSTERED_DISK_NACK:
7032 if (mddev_is_clustered(mddev))
7033 md_cluster_ops->new_disk_ack(mddev, false);
7039 err = hot_add_disk(mddev, new_decode_dev(arg));
7043 err = do_md_run(mddev);
7046 case SET_BITMAP_FILE:
7047 err = set_bitmap_file(mddev, (int)arg);
7056 if (mddev->hold_active == UNTIL_IOCTL &&
7058 mddev->hold_active = 0;
7059 mddev_unlock(mddev);
7063 #ifdef CONFIG_COMPAT
7064 static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7065 unsigned int cmd, unsigned long arg)
7068 case HOT_REMOVE_DISK:
7070 case SET_DISK_FAULTY:
7071 case SET_BITMAP_FILE:
7072 /* These take in integer arg, do not convert */
7075 arg = (unsigned long)compat_ptr(arg);
7079 return md_ioctl(bdev, mode, cmd, arg);
7081 #endif /* CONFIG_COMPAT */
7083 static int md_open(struct block_device *bdev, fmode_t mode)
7086 * Succeed if we can lock the mddev, which confirms that
7087 * it isn't being stopped right now.
7089 struct mddev *mddev = mddev_find(bdev->bd_dev);
7095 if (mddev->gendisk != bdev->bd_disk) {
7096 /* we are racing with mddev_put which is discarding this
7100 /* Wait until bdev->bd_disk is definitely gone */
7101 flush_workqueue(md_misc_wq);
7102 /* Then retry the open from the top */
7103 return -ERESTARTSYS;
7105 BUG_ON(mddev != bdev->bd_disk->private_data);
7107 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
7110 if (test_bit(MD_CLOSING, &mddev->flags)) {
7111 mutex_unlock(&mddev->open_mutex);
7117 atomic_inc(&mddev->openers);
7118 mutex_unlock(&mddev->open_mutex);
7120 check_disk_change(bdev);
7127 static void md_release(struct gendisk *disk, fmode_t mode)
7129 struct mddev *mddev = disk->private_data;
7132 atomic_dec(&mddev->openers);
7136 static int md_media_changed(struct gendisk *disk)
7138 struct mddev *mddev = disk->private_data;
7140 return mddev->changed;
7143 static int md_revalidate(struct gendisk *disk)
7145 struct mddev *mddev = disk->private_data;
7150 static const struct block_device_operations md_fops =
7152 .owner = THIS_MODULE,
7154 .release = md_release,
7156 #ifdef CONFIG_COMPAT
7157 .compat_ioctl = md_compat_ioctl,
7159 .getgeo = md_getgeo,
7160 .media_changed = md_media_changed,
7161 .revalidate_disk= md_revalidate,
7164 static int md_thread(void *arg)
7166 struct md_thread *thread = arg;
7169 * md_thread is a 'system-thread', it's priority should be very
7170 * high. We avoid resource deadlocks individually in each
7171 * raid personality. (RAID5 does preallocation) We also use RR and
7172 * the very same RT priority as kswapd, thus we will never get
7173 * into a priority inversion deadlock.
7175 * we definitely have to have equal or higher priority than
7176 * bdflush, otherwise bdflush will deadlock if there are too
7177 * many dirty RAID5 blocks.
7180 allow_signal(SIGKILL);
7181 while (!kthread_should_stop()) {
7183 /* We need to wait INTERRUPTIBLE so that
7184 * we don't add to the load-average.
7185 * That means we need to be sure no signals are
7188 if (signal_pending(current))
7189 flush_signals(current);
7191 wait_event_interruptible_timeout
7193 test_bit(THREAD_WAKEUP, &thread->flags)
7194 || kthread_should_stop() || kthread_should_park(),
7197 clear_bit(THREAD_WAKEUP, &thread->flags);
7198 if (kthread_should_park())
7200 if (!kthread_should_stop())
7201 thread->run(thread);
7207 void md_wakeup_thread(struct md_thread *thread)
7210 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
7211 set_bit(THREAD_WAKEUP, &thread->flags);
7212 wake_up(&thread->wqueue);
7215 EXPORT_SYMBOL(md_wakeup_thread);
7217 struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7218 struct mddev *mddev, const char *name)
7220 struct md_thread *thread;
7222 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
7226 init_waitqueue_head(&thread->wqueue);
7229 thread->mddev = mddev;
7230 thread->timeout = MAX_SCHEDULE_TIMEOUT;
7231 thread->tsk = kthread_run(md_thread, thread,
7233 mdname(thread->mddev),
7235 if (IS_ERR(thread->tsk)) {
7241 EXPORT_SYMBOL(md_register_thread);
7243 void md_unregister_thread(struct md_thread **threadp)
7245 struct md_thread *thread = *threadp;
7248 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
7249 /* Locking ensures that mddev_unlock does not wake_up a
7250 * non-existent thread
7252 spin_lock(&pers_lock);
7254 spin_unlock(&pers_lock);
7256 kthread_stop(thread->tsk);
7259 EXPORT_SYMBOL(md_unregister_thread);
7261 void md_error(struct mddev *mddev, struct md_rdev *rdev)
7263 if (!rdev || test_bit(Faulty, &rdev->flags))
7266 if (!mddev->pers || !mddev->pers->error_handler)
7268 mddev->pers->error_handler(mddev,rdev);
7269 if (mddev->degraded)
7270 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7271 sysfs_notify_dirent_safe(rdev->sysfs_state);
7272 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7273 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7274 md_wakeup_thread(mddev->thread);
7275 if (mddev->event_work.func)
7276 queue_work(md_misc_wq, &mddev->event_work);
7277 md_new_event(mddev);
7279 EXPORT_SYMBOL(md_error);
7281 /* seq_file implementation /proc/mdstat */
7283 static void status_unused(struct seq_file *seq)
7286 struct md_rdev *rdev;
7288 seq_printf(seq, "unused devices: ");
7290 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
7291 char b[BDEVNAME_SIZE];
7293 seq_printf(seq, "%s ",
7294 bdevname(rdev->bdev,b));
7297 seq_printf(seq, "<none>");
7299 seq_printf(seq, "\n");
7302 static int status_resync(struct seq_file *seq, struct mddev *mddev)
7304 sector_t max_sectors, resync, res;
7305 unsigned long dt, db;
7308 unsigned int per_milli;
7310 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7311 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7312 max_sectors = mddev->resync_max_sectors;
7314 max_sectors = mddev->dev_sectors;
7316 resync = mddev->curr_resync;
7318 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7319 /* Still cleaning up */
7320 resync = max_sectors;
7322 resync -= atomic_read(&mddev->recovery_active);
7325 if (mddev->recovery_cp < MaxSector) {
7326 seq_printf(seq, "\tresync=PENDING");
7332 seq_printf(seq, "\tresync=DELAYED");
7336 WARN_ON(max_sectors == 0);
7337 /* Pick 'scale' such that (resync>>scale)*1000 will fit
7338 * in a sector_t, and (max_sectors>>scale) will fit in a
7339 * u32, as those are the requirements for sector_div.
7340 * Thus 'scale' must be at least 10
7343 if (sizeof(sector_t) > sizeof(unsigned long)) {
7344 while ( max_sectors/2 > (1ULL<<(scale+32)))
7347 res = (resync>>scale)*1000;
7348 sector_div(res, (u32)((max_sectors>>scale)+1));
7352 int i, x = per_milli/50, y = 20-x;
7353 seq_printf(seq, "[");
7354 for (i = 0; i < x; i++)
7355 seq_printf(seq, "=");
7356 seq_printf(seq, ">");
7357 for (i = 0; i < y; i++)
7358 seq_printf(seq, ".");
7359 seq_printf(seq, "] ");
7361 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
7362 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7364 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7366 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7367 "resync" : "recovery"))),
7368 per_milli/10, per_milli % 10,
7369 (unsigned long long) resync/2,
7370 (unsigned long long) max_sectors/2);
7373 * dt: time from mark until now
7374 * db: blocks written from mark until now
7375 * rt: remaining time
7377 * rt is a sector_t, so could be 32bit or 64bit.
7378 * So we divide before multiply in case it is 32bit and close
7380 * We scale the divisor (db) by 32 to avoid losing precision
7381 * near the end of resync when the number of remaining sectors
7383 * We then divide rt by 32 after multiplying by db to compensate.
7384 * The '+1' avoids division by zero if db is very small.
7386 dt = ((jiffies - mddev->resync_mark) / HZ);
7388 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7389 - mddev->resync_mark_cnt;
7391 rt = max_sectors - resync; /* number of remaining sectors */
7392 sector_div(rt, db/32+1);
7396 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7397 ((unsigned long)rt % 60)/6);
7399 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
7403 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7405 struct list_head *tmp;
7407 struct mddev *mddev;
7415 spin_lock(&all_mddevs_lock);
7416 list_for_each(tmp,&all_mddevs)
7418 mddev = list_entry(tmp, struct mddev, all_mddevs);
7420 spin_unlock(&all_mddevs_lock);
7423 spin_unlock(&all_mddevs_lock);
7425 return (void*)2;/* tail */
7429 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7431 struct list_head *tmp;
7432 struct mddev *next_mddev, *mddev = v;
7438 spin_lock(&all_mddevs_lock);
7440 tmp = all_mddevs.next;
7442 tmp = mddev->all_mddevs.next;
7443 if (tmp != &all_mddevs)
7444 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
7446 next_mddev = (void*)2;
7449 spin_unlock(&all_mddevs_lock);
7457 static void md_seq_stop(struct seq_file *seq, void *v)
7459 struct mddev *mddev = v;
7461 if (mddev && v != (void*)1 && v != (void*)2)
7465 static int md_seq_show(struct seq_file *seq, void *v)
7467 struct mddev *mddev = v;
7469 struct md_rdev *rdev;
7471 if (v == (void*)1) {
7472 struct md_personality *pers;
7473 seq_printf(seq, "Personalities : ");
7474 spin_lock(&pers_lock);
7475 list_for_each_entry(pers, &pers_list, list)
7476 seq_printf(seq, "[%s] ", pers->name);
7478 spin_unlock(&pers_lock);
7479 seq_printf(seq, "\n");
7480 seq->poll_event = atomic_read(&md_event_count);
7483 if (v == (void*)2) {
7488 spin_lock(&mddev->lock);
7489 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7490 seq_printf(seq, "%s : %sactive", mdname(mddev),
7491 mddev->pers ? "" : "in");
7494 seq_printf(seq, " (read-only)");
7496 seq_printf(seq, " (auto-read-only)");
7497 seq_printf(seq, " %s", mddev->pers->name);
7502 rdev_for_each_rcu(rdev, mddev) {
7503 char b[BDEVNAME_SIZE];
7504 seq_printf(seq, " %s[%d]",
7505 bdevname(rdev->bdev,b), rdev->desc_nr);
7506 if (test_bit(WriteMostly, &rdev->flags))
7507 seq_printf(seq, "(W)");
7508 if (test_bit(Journal, &rdev->flags))
7509 seq_printf(seq, "(J)");
7510 if (test_bit(Faulty, &rdev->flags)) {
7511 seq_printf(seq, "(F)");
7514 if (rdev->raid_disk < 0)
7515 seq_printf(seq, "(S)"); /* spare */
7516 if (test_bit(Replacement, &rdev->flags))
7517 seq_printf(seq, "(R)");
7518 sectors += rdev->sectors;
7522 if (!list_empty(&mddev->disks)) {
7524 seq_printf(seq, "\n %llu blocks",
7525 (unsigned long long)
7526 mddev->array_sectors / 2);
7528 seq_printf(seq, "\n %llu blocks",
7529 (unsigned long long)sectors / 2);
7531 if (mddev->persistent) {
7532 if (mddev->major_version != 0 ||
7533 mddev->minor_version != 90) {
7534 seq_printf(seq," super %d.%d",
7535 mddev->major_version,
7536 mddev->minor_version);
7538 } else if (mddev->external)
7539 seq_printf(seq, " super external:%s",
7540 mddev->metadata_type);
7542 seq_printf(seq, " super non-persistent");
7545 mddev->pers->status(seq, mddev);
7546 seq_printf(seq, "\n ");
7547 if (mddev->pers->sync_request) {
7548 if (status_resync(seq, mddev))
7549 seq_printf(seq, "\n ");
7552 seq_printf(seq, "\n ");
7554 bitmap_status(seq, mddev->bitmap);
7556 seq_printf(seq, "\n");
7558 spin_unlock(&mddev->lock);
7563 static const struct seq_operations md_seq_ops = {
7564 .start = md_seq_start,
7565 .next = md_seq_next,
7566 .stop = md_seq_stop,
7567 .show = md_seq_show,
7570 static int md_seq_open(struct inode *inode, struct file *file)
7572 struct seq_file *seq;
7575 error = seq_open(file, &md_seq_ops);
7579 seq = file->private_data;
7580 seq->poll_event = atomic_read(&md_event_count);
7584 static int md_unloading;
7585 static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7587 struct seq_file *seq = filp->private_data;
7591 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
7592 poll_wait(filp, &md_event_waiters, wait);
7594 /* always allow read */
7595 mask = POLLIN | POLLRDNORM;
7597 if (seq->poll_event != atomic_read(&md_event_count))
7598 mask |= POLLERR | POLLPRI;
7602 static const struct file_operations md_seq_fops = {
7603 .owner = THIS_MODULE,
7604 .open = md_seq_open,
7606 .llseek = seq_lseek,
7607 .release = seq_release_private,
7608 .poll = mdstat_poll,
7611 int register_md_personality(struct md_personality *p)
7613 pr_debug("md: %s personality registered for level %d\n",
7615 spin_lock(&pers_lock);
7616 list_add_tail(&p->list, &pers_list);
7617 spin_unlock(&pers_lock);
7620 EXPORT_SYMBOL(register_md_personality);
7622 int unregister_md_personality(struct md_personality *p)
7624 pr_debug("md: %s personality unregistered\n", p->name);
7625 spin_lock(&pers_lock);
7626 list_del_init(&p->list);
7627 spin_unlock(&pers_lock);
7630 EXPORT_SYMBOL(unregister_md_personality);
7632 int register_md_cluster_operations(struct md_cluster_operations *ops,
7633 struct module *module)
7636 spin_lock(&pers_lock);
7637 if (md_cluster_ops != NULL)
7640 md_cluster_ops = ops;
7641 md_cluster_mod = module;
7643 spin_unlock(&pers_lock);
7646 EXPORT_SYMBOL(register_md_cluster_operations);
7648 int unregister_md_cluster_operations(void)
7650 spin_lock(&pers_lock);
7651 md_cluster_ops = NULL;
7652 spin_unlock(&pers_lock);
7655 EXPORT_SYMBOL(unregister_md_cluster_operations);
7657 int md_setup_cluster(struct mddev *mddev, int nodes)
7659 if (!md_cluster_ops)
7660 request_module("md-cluster");
7661 spin_lock(&pers_lock);
7662 /* ensure module won't be unloaded */
7663 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
7664 pr_warn("can't find md-cluster module or get it's reference.\n");
7665 spin_unlock(&pers_lock);
7668 spin_unlock(&pers_lock);
7670 return md_cluster_ops->join(mddev, nodes);
7673 void md_cluster_stop(struct mddev *mddev)
7675 if (!md_cluster_ops)
7677 md_cluster_ops->leave(mddev);
7678 module_put(md_cluster_mod);
7681 static int is_mddev_idle(struct mddev *mddev, int init)
7683 struct md_rdev *rdev;
7689 rdev_for_each_rcu(rdev, mddev) {
7690 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
7691 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7692 (int)part_stat_read(&disk->part0, sectors[1]) -
7693 atomic_read(&disk->sync_io);
7694 /* sync IO will cause sync_io to increase before the disk_stats
7695 * as sync_io is counted when a request starts, and
7696 * disk_stats is counted when it completes.
7697 * So resync activity will cause curr_events to be smaller than
7698 * when there was no such activity.
7699 * non-sync IO will cause disk_stat to increase without
7700 * increasing sync_io so curr_events will (eventually)
7701 * be larger than it was before. Once it becomes
7702 * substantially larger, the test below will cause
7703 * the array to appear non-idle, and resync will slow
7705 * If there is a lot of outstanding resync activity when
7706 * we set last_event to curr_events, then all that activity
7707 * completing might cause the array to appear non-idle
7708 * and resync will be slowed down even though there might
7709 * not have been non-resync activity. This will only
7710 * happen once though. 'last_events' will soon reflect
7711 * the state where there is little or no outstanding
7712 * resync requests, and further resync activity will
7713 * always make curr_events less than last_events.
7716 if (init || curr_events - rdev->last_events > 64) {
7717 rdev->last_events = curr_events;
7725 void md_done_sync(struct mddev *mddev, int blocks, int ok)
7727 /* another "blocks" (512byte) blocks have been synced */
7728 atomic_sub(blocks, &mddev->recovery_active);
7729 wake_up(&mddev->recovery_wait);
7731 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7732 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
7733 md_wakeup_thread(mddev->thread);
7734 // stop recovery, signal do_sync ....
7737 EXPORT_SYMBOL(md_done_sync);
7739 /* md_write_start(mddev, bi)
7740 * If we need to update some array metadata (e.g. 'active' flag
7741 * in superblock) before writing, schedule a superblock update
7742 * and wait for it to complete.
7744 void md_write_start(struct mddev *mddev, struct bio *bi)
7747 if (bio_data_dir(bi) != WRITE)
7750 BUG_ON(mddev->ro == 1);
7751 if (mddev->ro == 2) {
7752 /* need to switch to read/write */
7754 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7755 md_wakeup_thread(mddev->thread);
7756 md_wakeup_thread(mddev->sync_thread);
7759 atomic_inc(&mddev->writes_pending);
7760 if (mddev->safemode == 1)
7761 mddev->safemode = 0;
7762 if (mddev->in_sync) {
7763 spin_lock(&mddev->lock);
7764 if (mddev->in_sync) {
7766 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
7767 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
7768 md_wakeup_thread(mddev->thread);
7771 spin_unlock(&mddev->lock);
7774 sysfs_notify_dirent_safe(mddev->sysfs_state);
7775 wait_event(mddev->sb_wait,
7776 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
7778 EXPORT_SYMBOL(md_write_start);
7780 void md_write_end(struct mddev *mddev)
7782 if (atomic_dec_and_test(&mddev->writes_pending)) {
7783 if (mddev->safemode == 2)
7784 md_wakeup_thread(mddev->thread);
7785 else if (mddev->safemode_delay)
7786 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7789 EXPORT_SYMBOL(md_write_end);
7791 /* md_allow_write(mddev)
7792 * Calling this ensures that the array is marked 'active' so that writes
7793 * may proceed without blocking. It is important to call this before
7794 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7795 * Must be called with mddev_lock held.
7797 * In the ->external case MD_SB_CHANGE_PENDING can not be cleared until mddev->lock
7798 * is dropped, so return -EAGAIN after notifying userspace.
7800 int md_allow_write(struct mddev *mddev)
7806 if (!mddev->pers->sync_request)
7809 spin_lock(&mddev->lock);
7810 if (mddev->in_sync) {
7812 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
7813 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
7814 if (mddev->safemode_delay &&
7815 mddev->safemode == 0)
7816 mddev->safemode = 1;
7817 spin_unlock(&mddev->lock);
7818 md_update_sb(mddev, 0);
7819 sysfs_notify_dirent_safe(mddev->sysfs_state);
7821 spin_unlock(&mddev->lock);
7823 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
7828 EXPORT_SYMBOL_GPL(md_allow_write);
7830 #define SYNC_MARKS 10
7831 #define SYNC_MARK_STEP (3*HZ)
7832 #define UPDATE_FREQUENCY (5*60*HZ)
7833 void md_do_sync(struct md_thread *thread)
7835 struct mddev *mddev = thread->mddev;
7836 struct mddev *mddev2;
7837 unsigned int currspeed = 0,
7839 sector_t max_sectors,j, io_sectors, recovery_done;
7840 unsigned long mark[SYNC_MARKS];
7841 unsigned long update_time;
7842 sector_t mark_cnt[SYNC_MARKS];
7844 struct list_head *tmp;
7845 sector_t last_check;
7847 struct md_rdev *rdev;
7848 char *desc, *action = NULL;
7849 struct blk_plug plug;
7852 /* just incase thread restarts... */
7853 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7855 if (mddev->ro) {/* never try to sync a read-only array */
7856 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7860 if (mddev_is_clustered(mddev)) {
7861 ret = md_cluster_ops->resync_start(mddev);
7865 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
7866 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7867 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
7868 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
7869 && ((unsigned long long)mddev->curr_resync_completed
7870 < (unsigned long long)mddev->resync_max_sectors))
7874 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7875 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
7876 desc = "data-check";
7878 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7879 desc = "requested-resync";
7883 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7888 mddev->last_sync_action = action ?: desc;
7890 /* we overload curr_resync somewhat here.
7891 * 0 == not engaged in resync at all
7892 * 2 == checking that there is no conflict with another sync
7893 * 1 == like 2, but have yielded to allow conflicting resync to
7895 * other == active in resync - this many blocks
7897 * Before starting a resync we must have set curr_resync to
7898 * 2, and then checked that every "conflicting" array has curr_resync
7899 * less than ours. When we find one that is the same or higher
7900 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7901 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7902 * This will mean we have to start checking from the beginning again.
7907 int mddev2_minor = -1;
7908 mddev->curr_resync = 2;
7911 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7913 for_each_mddev(mddev2, tmp) {
7914 if (mddev2 == mddev)
7916 if (!mddev->parallel_resync
7917 && mddev2->curr_resync
7918 && match_mddev_units(mddev, mddev2)) {
7920 if (mddev < mddev2 && mddev->curr_resync == 2) {
7921 /* arbitrarily yield */
7922 mddev->curr_resync = 1;
7923 wake_up(&resync_wait);
7925 if (mddev > mddev2 && mddev->curr_resync == 1)
7926 /* no need to wait here, we can wait the next
7927 * time 'round when curr_resync == 2
7930 /* We need to wait 'interruptible' so as not to
7931 * contribute to the load average, and not to
7932 * be caught by 'softlockup'
7934 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
7935 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7936 mddev2->curr_resync >= mddev->curr_resync) {
7937 if (mddev2_minor != mddev2->md_minor) {
7938 mddev2_minor = mddev2->md_minor;
7939 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
7940 desc, mdname(mddev),
7944 if (signal_pending(current))
7945 flush_signals(current);
7947 finish_wait(&resync_wait, &wq);
7950 finish_wait(&resync_wait, &wq);
7953 } while (mddev->curr_resync < 2);
7956 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7957 /* resync follows the size requested by the personality,
7958 * which defaults to physical size, but can be virtual size
7960 max_sectors = mddev->resync_max_sectors;
7961 atomic64_set(&mddev->resync_mismatches, 0);
7962 /* we don't use the checkpoint if there's a bitmap */
7963 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7964 j = mddev->resync_min;
7965 else if (!mddev->bitmap)
7966 j = mddev->recovery_cp;
7968 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7969 max_sectors = mddev->resync_max_sectors;
7971 /* recovery follows the physical size of devices */
7972 max_sectors = mddev->dev_sectors;
7975 rdev_for_each_rcu(rdev, mddev)
7976 if (rdev->raid_disk >= 0 &&
7977 !test_bit(Journal, &rdev->flags) &&
7978 !test_bit(Faulty, &rdev->flags) &&
7979 !test_bit(In_sync, &rdev->flags) &&
7980 rdev->recovery_offset < j)
7981 j = rdev->recovery_offset;
7984 /* If there is a bitmap, we need to make sure all
7985 * writes that started before we added a spare
7986 * complete before we start doing a recovery.
7987 * Otherwise the write might complete and (via
7988 * bitmap_endwrite) set a bit in the bitmap after the
7989 * recovery has checked that bit and skipped that
7992 if (mddev->bitmap) {
7993 mddev->pers->quiesce(mddev, 1);
7994 mddev->pers->quiesce(mddev, 0);
7998 pr_info("md: %s of RAID array %s\n", desc, mdname(mddev));
7999 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev));
8000 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8001 speed_max(mddev), desc);
8003 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
8006 for (m = 0; m < SYNC_MARKS; m++) {
8008 mark_cnt[m] = io_sectors;
8011 mddev->resync_mark = mark[last_mark];
8012 mddev->resync_mark_cnt = mark_cnt[last_mark];
8015 * Tune reconstruction:
8017 window = 32*(PAGE_SIZE/512);
8018 pr_debug("md: using %dk window, over a total of %lluk.\n",
8019 window/2, (unsigned long long)max_sectors/2);
8021 atomic_set(&mddev->recovery_active, 0);
8025 pr_debug("md: resuming %s of %s from checkpoint.\n",
8026 desc, mdname(mddev));
8027 mddev->curr_resync = j;
8029 mddev->curr_resync = 3; /* no longer delayed */
8030 mddev->curr_resync_completed = j;
8031 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8032 md_new_event(mddev);
8033 update_time = jiffies;
8035 blk_start_plug(&plug);
8036 while (j < max_sectors) {
8041 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8042 ((mddev->curr_resync > mddev->curr_resync_completed &&
8043 (mddev->curr_resync - mddev->curr_resync_completed)
8044 > (max_sectors >> 4)) ||
8045 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
8046 (j - mddev->curr_resync_completed)*2
8047 >= mddev->resync_max - mddev->curr_resync_completed ||
8048 mddev->curr_resync_completed > mddev->resync_max
8050 /* time to update curr_resync_completed */
8051 wait_event(mddev->recovery_wait,
8052 atomic_read(&mddev->recovery_active) == 0);
8053 mddev->curr_resync_completed = j;
8054 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8055 j > mddev->recovery_cp)
8056 mddev->recovery_cp = j;
8057 update_time = jiffies;
8058 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8059 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8062 while (j >= mddev->resync_max &&
8063 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8064 /* As this condition is controlled by user-space,
8065 * we can block indefinitely, so use '_interruptible'
8066 * to avoid triggering warnings.
8068 flush_signals(current); /* just in case */
8069 wait_event_interruptible(mddev->recovery_wait,
8070 mddev->resync_max > j
8071 || test_bit(MD_RECOVERY_INTR,
8075 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8078 sectors = mddev->pers->sync_request(mddev, j, &skipped);
8080 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
8084 if (!skipped) { /* actual IO requested */
8085 io_sectors += sectors;
8086 atomic_add(sectors, &mddev->recovery_active);
8089 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8093 if (j > max_sectors)
8094 /* when skipping, extra large numbers can be returned. */
8097 mddev->curr_resync = j;
8098 mddev->curr_mark_cnt = io_sectors;
8099 if (last_check == 0)
8100 /* this is the earliest that rebuild will be
8101 * visible in /proc/mdstat
8103 md_new_event(mddev);
8105 if (last_check + window > io_sectors || j == max_sectors)
8108 last_check = io_sectors;
8110 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8112 int next = (last_mark+1) % SYNC_MARKS;
8114 mddev->resync_mark = mark[next];
8115 mddev->resync_mark_cnt = mark_cnt[next];
8116 mark[next] = jiffies;
8117 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
8121 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8125 * this loop exits only if either when we are slower than
8126 * the 'hard' speed limit, or the system was IO-idle for
8128 * the system might be non-idle CPU-wise, but we only care
8129 * about not overloading the IO subsystem. (things like an
8130 * e2fsck being done on the RAID array should execute fast)
8134 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8135 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
8136 /((jiffies-mddev->resync_mark)/HZ +1) +1;
8138 if (currspeed > speed_min(mddev)) {
8139 if (currspeed > speed_max(mddev)) {
8143 if (!is_mddev_idle(mddev, 0)) {
8145 * Give other IO more of a chance.
8146 * The faster the devices, the less we wait.
8148 wait_event(mddev->recovery_wait,
8149 !atomic_read(&mddev->recovery_active));
8153 pr_info("md: %s: %s %s.\n",mdname(mddev), desc,
8154 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8155 ? "interrupted" : "done");
8157 * this also signals 'finished resyncing' to md_stop
8159 blk_finish_plug(&plug);
8160 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8162 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8163 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8164 mddev->curr_resync > 3) {
8165 mddev->curr_resync_completed = mddev->curr_resync;
8166 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8168 mddev->pers->sync_request(mddev, max_sectors, &skipped);
8170 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
8171 mddev->curr_resync > 3) {
8172 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8173 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8174 if (mddev->curr_resync >= mddev->recovery_cp) {
8175 pr_debug("md: checkpointing %s of %s.\n",
8176 desc, mdname(mddev));
8177 if (test_bit(MD_RECOVERY_ERROR,
8179 mddev->recovery_cp =
8180 mddev->curr_resync_completed;
8182 mddev->recovery_cp =
8186 mddev->recovery_cp = MaxSector;
8188 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8189 mddev->curr_resync = MaxSector;
8191 rdev_for_each_rcu(rdev, mddev)
8192 if (rdev->raid_disk >= 0 &&
8193 mddev->delta_disks >= 0 &&
8194 !test_bit(Journal, &rdev->flags) &&
8195 !test_bit(Faulty, &rdev->flags) &&
8196 !test_bit(In_sync, &rdev->flags) &&
8197 rdev->recovery_offset < mddev->curr_resync)
8198 rdev->recovery_offset = mddev->curr_resync;
8203 /* set CHANGE_PENDING here since maybe another update is needed,
8204 * so other nodes are informed. It should be harmless for normal
8206 set_mask_bits(&mddev->sb_flags, 0,
8207 BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS));
8209 spin_lock(&mddev->lock);
8210 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8211 /* We completed so min/max setting can be forgotten if used. */
8212 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8213 mddev->resync_min = 0;
8214 mddev->resync_max = MaxSector;
8215 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8216 mddev->resync_min = mddev->curr_resync_completed;
8217 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
8218 mddev->curr_resync = 0;
8219 spin_unlock(&mddev->lock);
8221 wake_up(&resync_wait);
8222 md_wakeup_thread(mddev->thread);
8225 EXPORT_SYMBOL_GPL(md_do_sync);
8227 static int remove_and_add_spares(struct mddev *mddev,
8228 struct md_rdev *this)
8230 struct md_rdev *rdev;
8233 bool remove_some = false;
8235 rdev_for_each(rdev, mddev) {
8236 if ((this == NULL || rdev == this) &&
8237 rdev->raid_disk >= 0 &&
8238 !test_bit(Blocked, &rdev->flags) &&
8239 test_bit(Faulty, &rdev->flags) &&
8240 atomic_read(&rdev->nr_pending)==0) {
8241 /* Faulty non-Blocked devices with nr_pending == 0
8242 * never get nr_pending incremented,
8243 * never get Faulty cleared, and never get Blocked set.
8244 * So we can synchronize_rcu now rather than once per device
8247 set_bit(RemoveSynchronized, &rdev->flags);
8253 rdev_for_each(rdev, mddev) {
8254 if ((this == NULL || rdev == this) &&
8255 rdev->raid_disk >= 0 &&
8256 !test_bit(Blocked, &rdev->flags) &&
8257 ((test_bit(RemoveSynchronized, &rdev->flags) ||
8258 (!test_bit(In_sync, &rdev->flags) &&
8259 !test_bit(Journal, &rdev->flags))) &&
8260 atomic_read(&rdev->nr_pending)==0)) {
8261 if (mddev->pers->hot_remove_disk(
8262 mddev, rdev) == 0) {
8263 sysfs_unlink_rdev(mddev, rdev);
8264 rdev->raid_disk = -1;
8268 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
8269 clear_bit(RemoveSynchronized, &rdev->flags);
8272 if (removed && mddev->kobj.sd)
8273 sysfs_notify(&mddev->kobj, NULL, "degraded");
8275 if (this && removed)
8278 rdev_for_each(rdev, mddev) {
8279 if (this && this != rdev)
8281 if (test_bit(Candidate, &rdev->flags))
8283 if (rdev->raid_disk >= 0 &&
8284 !test_bit(In_sync, &rdev->flags) &&
8285 !test_bit(Journal, &rdev->flags) &&
8286 !test_bit(Faulty, &rdev->flags))
8288 if (rdev->raid_disk >= 0)
8290 if (test_bit(Faulty, &rdev->flags))
8292 if (!test_bit(Journal, &rdev->flags)) {
8294 ! (rdev->saved_raid_disk >= 0 &&
8295 !test_bit(Bitmap_sync, &rdev->flags)))
8298 rdev->recovery_offset = 0;
8301 hot_add_disk(mddev, rdev) == 0) {
8302 if (sysfs_link_rdev(mddev, rdev))
8303 /* failure here is OK */;
8304 if (!test_bit(Journal, &rdev->flags))
8306 md_new_event(mddev);
8307 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
8312 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
8316 static void md_start_sync(struct work_struct *ws)
8318 struct mddev *mddev = container_of(ws, struct mddev, del_work);
8320 mddev->sync_thread = md_register_thread(md_do_sync,
8323 if (!mddev->sync_thread) {
8324 pr_warn("%s: could not start resync thread...\n",
8326 /* leave the spares where they are, it shouldn't hurt */
8327 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8328 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8329 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8330 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8331 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
8332 wake_up(&resync_wait);
8333 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8335 if (mddev->sysfs_action)
8336 sysfs_notify_dirent_safe(mddev->sysfs_action);
8338 md_wakeup_thread(mddev->sync_thread);
8339 sysfs_notify_dirent_safe(mddev->sysfs_action);
8340 md_new_event(mddev);
8344 * This routine is regularly called by all per-raid-array threads to
8345 * deal with generic issues like resync and super-block update.
8346 * Raid personalities that don't have a thread (linear/raid0) do not
8347 * need this as they never do any recovery or update the superblock.
8349 * It does not do any resync itself, but rather "forks" off other threads
8350 * to do that as needed.
8351 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8352 * "->recovery" and create a thread at ->sync_thread.
8353 * When the thread finishes it sets MD_RECOVERY_DONE
8354 * and wakeups up this thread which will reap the thread and finish up.
8355 * This thread also removes any faulty devices (with nr_pending == 0).
8357 * The overall approach is:
8358 * 1/ if the superblock needs updating, update it.
8359 * 2/ If a recovery thread is running, don't do anything else.
8360 * 3/ If recovery has finished, clean up, possibly marking spares active.
8361 * 4/ If there are any faulty devices, remove them.
8362 * 5/ If array is degraded, try to add spares devices
8363 * 6/ If array has spares or is not in-sync, start a resync thread.
8365 void md_check_recovery(struct mddev *mddev)
8367 if (mddev->suspended)
8371 bitmap_daemon_work(mddev);
8373 if (signal_pending(current)) {
8374 if (mddev->pers->sync_request && !mddev->external) {
8375 pr_debug("md: %s in immediate safe mode\n",
8377 mddev->safemode = 2;
8379 flush_signals(current);
8382 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8385 (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
8386 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8387 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
8388 test_bit(MD_RELOAD_SB, &mddev->flags) ||
8389 (mddev->external == 0 && mddev->safemode == 1) ||
8390 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
8391 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
8395 if (mddev_trylock(mddev)) {
8399 struct md_rdev *rdev;
8400 if (!mddev->external && mddev->in_sync)
8401 /* 'Blocked' flag not needed as failed devices
8402 * will be recorded if array switched to read/write.
8403 * Leaving it set will prevent the device
8404 * from being removed.
8406 rdev_for_each(rdev, mddev)
8407 clear_bit(Blocked, &rdev->flags);
8408 /* On a read-only array we can:
8409 * - remove failed devices
8410 * - add already-in_sync devices if the array itself
8412 * As we only add devices that are already in-sync,
8413 * we can activate the spares immediately.
8415 remove_and_add_spares(mddev, NULL);
8416 /* There is no thread, but we need to call
8417 * ->spare_active and clear saved_raid_disk
8419 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
8420 md_reap_sync_thread(mddev);
8421 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
8422 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8423 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
8427 if (mddev_is_clustered(mddev)) {
8428 struct md_rdev *rdev;
8429 /* kick the device if another node issued a
8432 rdev_for_each(rdev, mddev) {
8433 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
8434 rdev->raid_disk < 0)
8435 md_kick_rdev_from_array(rdev);
8438 if (test_and_clear_bit(MD_RELOAD_SB, &mddev->flags))
8439 md_reload_sb(mddev, mddev->good_device_nr);
8442 if (!mddev->external) {
8444 spin_lock(&mddev->lock);
8445 if (mddev->safemode &&
8446 !atomic_read(&mddev->writes_pending) &&
8448 mddev->recovery_cp == MaxSector) {
8451 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8453 if (mddev->safemode == 1)
8454 mddev->safemode = 0;
8455 spin_unlock(&mddev->lock);
8457 sysfs_notify_dirent_safe(mddev->sysfs_state);
8460 if (mddev->sb_flags)
8461 md_update_sb(mddev, 0);
8463 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8464 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8465 /* resync/recovery still happening */
8466 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8469 if (mddev->sync_thread) {
8470 md_reap_sync_thread(mddev);
8473 /* Set RUNNING before clearing NEEDED to avoid
8474 * any transients in the value of "sync_action".
8476 mddev->curr_resync_completed = 0;
8477 spin_lock(&mddev->lock);
8478 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
8479 spin_unlock(&mddev->lock);
8480 /* Clear some bits that don't mean anything, but
8483 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8484 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
8486 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8487 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
8489 /* no recovery is running.
8490 * remove any failed drives, then
8491 * add spares if possible.
8492 * Spares are also removed and re-added, to allow
8493 * the personality to fail the re-add.
8496 if (mddev->reshape_position != MaxSector) {
8497 if (mddev->pers->check_reshape == NULL ||
8498 mddev->pers->check_reshape(mddev) != 0)
8499 /* Cannot proceed */
8501 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8502 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
8503 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
8504 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8505 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8506 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8507 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
8508 } else if (mddev->recovery_cp < MaxSector) {
8509 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8510 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
8511 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8512 /* nothing to be done ... */
8515 if (mddev->pers->sync_request) {
8517 /* We are adding a device or devices to an array
8518 * which has the bitmap stored on all devices.
8519 * So make sure all bitmap pages get written
8521 bitmap_write_all(mddev->bitmap);
8523 INIT_WORK(&mddev->del_work, md_start_sync);
8524 queue_work(md_misc_wq, &mddev->del_work);
8528 if (!mddev->sync_thread) {
8529 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
8530 wake_up(&resync_wait);
8531 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8533 if (mddev->sysfs_action)
8534 sysfs_notify_dirent_safe(mddev->sysfs_action);
8537 wake_up(&mddev->sb_wait);
8538 mddev_unlock(mddev);
8541 EXPORT_SYMBOL(md_check_recovery);
8543 void md_reap_sync_thread(struct mddev *mddev)
8545 struct md_rdev *rdev;
8547 /* resync has finished, collect result */
8548 md_unregister_thread(&mddev->sync_thread);
8549 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8550 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8552 /* activate any spares */
8553 if (mddev->pers->spare_active(mddev)) {
8554 sysfs_notify(&mddev->kobj, NULL,
8556 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
8559 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8560 mddev->pers->finish_reshape)
8561 mddev->pers->finish_reshape(mddev);
8563 /* If array is no-longer degraded, then any saved_raid_disk
8564 * information must be scrapped.
8566 if (!mddev->degraded)
8567 rdev_for_each(rdev, mddev)
8568 rdev->saved_raid_disk = -1;
8570 md_update_sb(mddev, 1);
8571 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
8572 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
8574 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
8575 md_cluster_ops->resync_finish(mddev);
8576 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
8577 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
8578 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8579 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8580 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8581 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8582 wake_up(&resync_wait);
8583 /* flag recovery needed just to double check */
8584 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8585 sysfs_notify_dirent_safe(mddev->sysfs_action);
8586 md_new_event(mddev);
8587 if (mddev->event_work.func)
8588 queue_work(md_misc_wq, &mddev->event_work);
8590 EXPORT_SYMBOL(md_reap_sync_thread);
8592 void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
8594 sysfs_notify_dirent_safe(rdev->sysfs_state);
8595 wait_event_timeout(rdev->blocked_wait,
8596 !test_bit(Blocked, &rdev->flags) &&
8597 !test_bit(BlockedBadBlocks, &rdev->flags),
8598 msecs_to_jiffies(5000));
8599 rdev_dec_pending(rdev, mddev);
8601 EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8603 void md_finish_reshape(struct mddev *mddev)
8605 /* called be personality module when reshape completes. */
8606 struct md_rdev *rdev;
8608 rdev_for_each(rdev, mddev) {
8609 if (rdev->data_offset > rdev->new_data_offset)
8610 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8612 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8613 rdev->data_offset = rdev->new_data_offset;
8616 EXPORT_SYMBOL(md_finish_reshape);
8618 /* Bad block management */
8620 /* Returns 1 on success, 0 on failure */
8621 int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8624 struct mddev *mddev = rdev->mddev;
8627 s += rdev->new_data_offset;
8629 s += rdev->data_offset;
8630 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
8632 /* Make sure they get written out promptly */
8633 if (test_bit(ExternalBbl, &rdev->flags))
8634 sysfs_notify(&rdev->kobj, NULL,
8635 "unacknowledged_bad_blocks");
8636 sysfs_notify_dirent_safe(rdev->sysfs_state);
8637 set_mask_bits(&mddev->sb_flags, 0,
8638 BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING));
8639 md_wakeup_thread(rdev->mddev->thread);
8644 EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8646 int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8651 s += rdev->new_data_offset;
8653 s += rdev->data_offset;
8654 rv = badblocks_clear(&rdev->badblocks, s, sectors);
8655 if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags))
8656 sysfs_notify(&rdev->kobj, NULL, "bad_blocks");
8659 EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8661 static int md_notify_reboot(struct notifier_block *this,
8662 unsigned long code, void *x)
8664 struct list_head *tmp;
8665 struct mddev *mddev;
8668 for_each_mddev(mddev, tmp) {
8669 if (mddev_trylock(mddev)) {
8671 __md_stop_writes(mddev);
8672 if (mddev->persistent)
8673 mddev->safemode = 2;
8674 mddev_unlock(mddev);
8679 * certain more exotic SCSI devices are known to be
8680 * volatile wrt too early system reboots. While the
8681 * right place to handle this issue is the given
8682 * driver, we do want to have a safe RAID driver ...
8690 static struct notifier_block md_notifier = {
8691 .notifier_call = md_notify_reboot,
8693 .priority = INT_MAX, /* before any real devices */
8696 static void md_geninit(void)
8698 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
8700 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
8703 static int __init md_init(void)
8707 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
8711 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8715 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8718 if ((ret = register_blkdev(0, "mdp")) < 0)
8722 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
8723 md_probe, NULL, NULL);
8724 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
8725 md_probe, NULL, NULL);
8727 register_reboot_notifier(&md_notifier);
8728 raid_table_header = register_sysctl_table(raid_root_table);
8734 unregister_blkdev(MD_MAJOR, "md");
8736 destroy_workqueue(md_misc_wq);
8738 destroy_workqueue(md_wq);
8743 static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
8745 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
8746 struct md_rdev *rdev2;
8748 char b[BDEVNAME_SIZE];
8750 /* Check for change of roles in the active devices */
8751 rdev_for_each(rdev2, mddev) {
8752 if (test_bit(Faulty, &rdev2->flags))
8755 /* Check if the roles changed */
8756 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
8758 if (test_bit(Candidate, &rdev2->flags)) {
8759 if (role == 0xfffe) {
8760 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
8761 md_kick_rdev_from_array(rdev2);
8765 clear_bit(Candidate, &rdev2->flags);
8768 if (role != rdev2->raid_disk) {
8770 if (rdev2->raid_disk == -1 && role != 0xffff) {
8771 rdev2->saved_raid_disk = role;
8772 ret = remove_and_add_spares(mddev, rdev2);
8773 pr_info("Activated spare: %s\n",
8774 bdevname(rdev2->bdev,b));
8775 /* wakeup mddev->thread here, so array could
8776 * perform resync with the new activated disk */
8777 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8778 md_wakeup_thread(mddev->thread);
8782 * We just want to do the minimum to mark the disk
8783 * as faulty. The recovery is performed by the
8784 * one who initiated the error.
8786 if ((role == 0xfffe) || (role == 0xfffd)) {
8787 md_error(mddev, rdev2);
8788 clear_bit(Blocked, &rdev2->flags);
8793 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
8794 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
8796 /* Finally set the event to be up to date */
8797 mddev->events = le64_to_cpu(sb->events);
8800 static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
8803 struct page *swapout = rdev->sb_page;
8804 struct mdp_superblock_1 *sb;
8806 /* Store the sb page of the rdev in the swapout temporary
8807 * variable in case we err in the future
8809 rdev->sb_page = NULL;
8810 err = alloc_disk_sb(rdev);
8812 ClearPageUptodate(rdev->sb_page);
8813 rdev->sb_loaded = 0;
8814 err = super_types[mddev->major_version].
8815 load_super(rdev, NULL, mddev->minor_version);
8818 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
8819 __func__, __LINE__, rdev->desc_nr, err);
8821 put_page(rdev->sb_page);
8822 rdev->sb_page = swapout;
8823 rdev->sb_loaded = 1;
8827 sb = page_address(rdev->sb_page);
8828 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
8832 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
8833 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
8835 /* The other node finished recovery, call spare_active to set
8836 * device In_sync and mddev->degraded
8838 if (rdev->recovery_offset == MaxSector &&
8839 !test_bit(In_sync, &rdev->flags) &&
8840 mddev->pers->spare_active(mddev))
8841 sysfs_notify(&mddev->kobj, NULL, "degraded");
8847 void md_reload_sb(struct mddev *mddev, int nr)
8849 struct md_rdev *rdev;
8853 rdev_for_each_rcu(rdev, mddev) {
8854 if (rdev->desc_nr == nr)
8858 if (!rdev || rdev->desc_nr != nr) {
8859 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
8863 err = read_rdev(mddev, rdev);
8867 check_sb_changes(mddev, rdev);
8869 /* Read all rdev's to update recovery_offset */
8870 rdev_for_each_rcu(rdev, mddev)
8871 read_rdev(mddev, rdev);
8873 EXPORT_SYMBOL(md_reload_sb);
8878 * Searches all registered partitions for autorun RAID arrays
8882 static DEFINE_MUTEX(detected_devices_mutex);
8883 static LIST_HEAD(all_detected_devices);
8884 struct detected_devices_node {
8885 struct list_head list;
8889 void md_autodetect_dev(dev_t dev)
8891 struct detected_devices_node *node_detected_dev;
8893 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8894 if (node_detected_dev) {
8895 node_detected_dev->dev = dev;
8896 mutex_lock(&detected_devices_mutex);
8897 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8898 mutex_unlock(&detected_devices_mutex);
8902 static void autostart_arrays(int part)
8904 struct md_rdev *rdev;
8905 struct detected_devices_node *node_detected_dev;
8907 int i_scanned, i_passed;
8912 pr_info("md: Autodetecting RAID arrays.\n");
8914 mutex_lock(&detected_devices_mutex);
8915 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8917 node_detected_dev = list_entry(all_detected_devices.next,
8918 struct detected_devices_node, list);
8919 list_del(&node_detected_dev->list);
8920 dev = node_detected_dev->dev;
8921 kfree(node_detected_dev);
8922 mutex_unlock(&detected_devices_mutex);
8923 rdev = md_import_device(dev,0, 90);
8924 mutex_lock(&detected_devices_mutex);
8928 if (test_bit(Faulty, &rdev->flags))
8931 set_bit(AutoDetected, &rdev->flags);
8932 list_add(&rdev->same_set, &pending_raid_disks);
8935 mutex_unlock(&detected_devices_mutex);
8937 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed);
8939 autorun_devices(part);
8942 #endif /* !MODULE */
8944 static __exit void md_exit(void)
8946 struct mddev *mddev;
8947 struct list_head *tmp;
8950 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
8951 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
8953 unregister_blkdev(MD_MAJOR,"md");
8954 unregister_blkdev(mdp_major, "mdp");
8955 unregister_reboot_notifier(&md_notifier);
8956 unregister_sysctl_table(raid_table_header);
8958 /* We cannot unload the modules while some process is
8959 * waiting for us in select() or poll() - wake them up
8962 while (waitqueue_active(&md_event_waiters)) {
8963 /* not safe to leave yet */
8964 wake_up(&md_event_waiters);
8968 remove_proc_entry("mdstat", NULL);
8970 for_each_mddev(mddev, tmp) {
8971 export_array(mddev);
8973 mddev->hold_active = 0;
8975 * for_each_mddev() will call mddev_put() at the end of each
8976 * iteration. As the mddev is now fully clear, this will
8977 * schedule the mddev for destruction by a workqueue, and the
8978 * destroy_workqueue() below will wait for that to complete.
8981 destroy_workqueue(md_misc_wq);
8982 destroy_workqueue(md_wq);
8985 subsys_initcall(md_init);
8986 module_exit(md_exit)
8988 static int get_ro(char *buffer, struct kernel_param *kp)
8990 return sprintf(buffer, "%d", start_readonly);
8992 static int set_ro(const char *val, struct kernel_param *kp)
8994 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
8997 module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8998 module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
8999 module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
9001 MODULE_LICENSE("GPL");
9002 MODULE_DESCRIPTION("MD RAID framework");
9004 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);