1 // SPDX-License-Identifier: GPL-2.0-or-later
3 md.c : Multiple Devices driver for Linux
4 Copyright (C) 1998, 1999, 2000 Ingo Molnar
6 completely rewritten, based on the MD driver code from Marc Zyngier
10 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
11 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
12 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
13 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
14 - kmod support by: Cyrus Durgin
15 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
16 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
18 - lots of fixes and improvements to the RAID1/RAID5 and generic
19 RAID code (such as request based resynchronization):
21 Neil Brown <neilb@cse.unsw.edu.au>.
23 - persistent bitmap code
24 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
27 Errors, Warnings, etc.
29 pr_crit() for error conditions that risk data loss
30 pr_err() for error conditions that are unexpected, like an IO error
31 or internal inconsistency
32 pr_warn() for error conditions that could have been predicated, like
33 adding a device to an array when it has incompatible metadata
34 pr_info() for every interesting, very rare events, like an array starting
35 or stopping, or resync starting or stopping
36 pr_debug() for everything else.
40 #include <linux/sched/mm.h>
41 #include <linux/sched/signal.h>
42 #include <linux/kthread.h>
43 #include <linux/blkdev.h>
44 #include <linux/blk-integrity.h>
45 #include <linux/badblocks.h>
46 #include <linux/sysctl.h>
47 #include <linux/seq_file.h>
49 #include <linux/poll.h>
50 #include <linux/ctype.h>
51 #include <linux/string.h>
52 #include <linux/hdreg.h>
53 #include <linux/proc_fs.h>
54 #include <linux/random.h>
55 #include <linux/major.h>
56 #include <linux/module.h>
57 #include <linux/reboot.h>
58 #include <linux/file.h>
59 #include <linux/compat.h>
60 #include <linux/delay.h>
61 #include <linux/raid/md_p.h>
62 #include <linux/raid/md_u.h>
63 #include <linux/raid/detect.h>
64 #include <linux/slab.h>
65 #include <linux/percpu-refcount.h>
66 #include <linux/part_stat.h>
68 #include <trace/events/block.h>
70 #include "md-bitmap.h"
71 #include "md-cluster.h"
73 /* pers_list is a list of registered personalities protected
75 * pers_lock does extra service to protect accesses to
76 * mddev->thread when the mutex cannot be held.
78 static LIST_HEAD(pers_list);
79 static DEFINE_SPINLOCK(pers_lock);
81 static struct kobj_type md_ktype;
83 struct md_cluster_operations *md_cluster_ops;
84 EXPORT_SYMBOL(md_cluster_ops);
85 static struct module *md_cluster_mod;
87 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
88 static struct workqueue_struct *md_wq;
89 static struct workqueue_struct *md_misc_wq;
90 static struct workqueue_struct *md_rdev_misc_wq;
92 static int remove_and_add_spares(struct mddev *mddev,
93 struct md_rdev *this);
94 static void mddev_detach(struct mddev *mddev);
97 * Default number of read corrections we'll attempt on an rdev
98 * before ejecting it from the array. We divide the read error
99 * count by 2 for every hour elapsed between read errors.
101 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
102 /* Default safemode delay: 200 msec */
103 #define DEFAULT_SAFEMODE_DELAY ((200 * HZ)/1000 +1)
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 void rdev_uninit_serial(struct md_rdev *rdev)
133 if (!test_and_clear_bit(CollisionCheck, &rdev->flags))
136 kvfree(rdev->serial);
140 static void rdevs_uninit_serial(struct mddev *mddev)
142 struct md_rdev *rdev;
144 rdev_for_each(rdev, mddev)
145 rdev_uninit_serial(rdev);
148 static int rdev_init_serial(struct md_rdev *rdev)
150 /* serial_nums equals with BARRIER_BUCKETS_NR */
151 int i, serial_nums = 1 << ((PAGE_SHIFT - ilog2(sizeof(atomic_t))));
152 struct serial_in_rdev *serial = NULL;
154 if (test_bit(CollisionCheck, &rdev->flags))
157 serial = kvmalloc(sizeof(struct serial_in_rdev) * serial_nums,
162 for (i = 0; i < serial_nums; i++) {
163 struct serial_in_rdev *serial_tmp = &serial[i];
165 spin_lock_init(&serial_tmp->serial_lock);
166 serial_tmp->serial_rb = RB_ROOT_CACHED;
167 init_waitqueue_head(&serial_tmp->serial_io_wait);
170 rdev->serial = serial;
171 set_bit(CollisionCheck, &rdev->flags);
176 static int rdevs_init_serial(struct mddev *mddev)
178 struct md_rdev *rdev;
181 rdev_for_each(rdev, mddev) {
182 ret = rdev_init_serial(rdev);
187 /* Free all resources if pool is not existed */
188 if (ret && !mddev->serial_info_pool)
189 rdevs_uninit_serial(mddev);
195 * rdev needs to enable serial stuffs if it meets the conditions:
196 * 1. it is multi-queue device flaged with writemostly.
197 * 2. the write-behind mode is enabled.
199 static int rdev_need_serial(struct md_rdev *rdev)
201 return (rdev && rdev->mddev->bitmap_info.max_write_behind > 0 &&
202 rdev->bdev->bd_disk->queue->nr_hw_queues != 1 &&
203 test_bit(WriteMostly, &rdev->flags));
207 * Init resource for rdev(s), then create serial_info_pool if:
208 * 1. rdev is the first device which return true from rdev_enable_serial.
209 * 2. rdev is NULL, means we want to enable serialization for all rdevs.
211 void mddev_create_serial_pool(struct mddev *mddev, struct md_rdev *rdev,
216 if (rdev && !rdev_need_serial(rdev) &&
217 !test_bit(CollisionCheck, &rdev->flags))
221 mddev_suspend(mddev);
224 ret = rdevs_init_serial(mddev);
226 ret = rdev_init_serial(rdev);
230 if (mddev->serial_info_pool == NULL) {
232 * already in memalloc noio context by
235 mddev->serial_info_pool =
236 mempool_create_kmalloc_pool(NR_SERIAL_INFOS,
237 sizeof(struct serial_info));
238 if (!mddev->serial_info_pool) {
239 rdevs_uninit_serial(mddev);
240 pr_err("can't alloc memory pool for serialization\n");
250 * Free resource from rdev(s), and destroy serial_info_pool under conditions:
251 * 1. rdev is the last device flaged with CollisionCheck.
252 * 2. when bitmap is destroyed while policy is not enabled.
253 * 3. for disable policy, the pool is destroyed only when no rdev needs it.
255 void mddev_destroy_serial_pool(struct mddev *mddev, struct md_rdev *rdev,
258 if (rdev && !test_bit(CollisionCheck, &rdev->flags))
261 if (mddev->serial_info_pool) {
262 struct md_rdev *temp;
263 int num = 0; /* used to track if other rdevs need the pool */
266 mddev_suspend(mddev);
267 rdev_for_each(temp, mddev) {
269 if (!mddev->serialize_policy ||
270 !rdev_need_serial(temp))
271 rdev_uninit_serial(temp);
274 } else if (temp != rdev &&
275 test_bit(CollisionCheck, &temp->flags))
280 rdev_uninit_serial(rdev);
283 pr_info("The mempool could be used by other devices\n");
285 mempool_destroy(mddev->serial_info_pool);
286 mddev->serial_info_pool = NULL;
293 static struct ctl_table_header *raid_table_header;
295 static struct ctl_table raid_table[] = {
297 .procname = "speed_limit_min",
298 .data = &sysctl_speed_limit_min,
299 .maxlen = sizeof(int),
300 .mode = S_IRUGO|S_IWUSR,
301 .proc_handler = proc_dointvec,
304 .procname = "speed_limit_max",
305 .data = &sysctl_speed_limit_max,
306 .maxlen = sizeof(int),
307 .mode = S_IRUGO|S_IWUSR,
308 .proc_handler = proc_dointvec,
313 static struct ctl_table raid_dir_table[] = {
317 .mode = S_IRUGO|S_IXUGO,
323 static struct ctl_table raid_root_table[] = {
328 .child = raid_dir_table,
333 static int start_readonly;
336 * The original mechanism for creating an md device is to create
337 * a device node in /dev and to open it. This causes races with device-close.
338 * The preferred method is to write to the "new_array" module parameter.
339 * This can avoid races.
340 * Setting create_on_open to false disables the original mechanism
341 * so all the races disappear.
343 static bool create_on_open = true;
346 * We have a system wide 'event count' that is incremented
347 * on any 'interesting' event, and readers of /proc/mdstat
348 * can use 'poll' or 'select' to find out when the event
352 * start array, stop array, error, add device, remove device,
353 * start build, activate spare
355 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
356 static atomic_t md_event_count;
357 void md_new_event(void)
359 atomic_inc(&md_event_count);
360 wake_up(&md_event_waiters);
362 EXPORT_SYMBOL_GPL(md_new_event);
365 * Enables to iterate over all existing md arrays
366 * all_mddevs_lock protects this list.
368 static LIST_HEAD(all_mddevs);
369 static DEFINE_SPINLOCK(all_mddevs_lock);
371 /* Rather than calling directly into the personality make_request function,
372 * IO requests come here first so that we can check if the device is
373 * being suspended pending a reconfiguration.
374 * We hold a refcount over the call to ->make_request. By the time that
375 * call has finished, the bio has been linked into some internal structure
376 * and so is visible to ->quiesce(), so we don't need the refcount any more.
378 static bool is_suspended(struct mddev *mddev, struct bio *bio)
380 if (mddev->suspended)
382 if (bio_data_dir(bio) != WRITE)
384 if (mddev->suspend_lo >= mddev->suspend_hi)
386 if (bio->bi_iter.bi_sector >= mddev->suspend_hi)
388 if (bio_end_sector(bio) < mddev->suspend_lo)
393 void md_handle_request(struct mddev *mddev, struct bio *bio)
397 if (is_suspended(mddev, bio)) {
399 /* Bail out if REQ_NOWAIT is set for the bio */
400 if (bio->bi_opf & REQ_NOWAIT) {
402 bio_wouldblock_error(bio);
406 prepare_to_wait(&mddev->sb_wait, &__wait,
407 TASK_UNINTERRUPTIBLE);
408 if (!is_suspended(mddev, bio))
414 finish_wait(&mddev->sb_wait, &__wait);
416 atomic_inc(&mddev->active_io);
419 if (!mddev->pers->make_request(mddev, bio)) {
420 atomic_dec(&mddev->active_io);
421 wake_up(&mddev->sb_wait);
422 goto check_suspended;
425 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
426 wake_up(&mddev->sb_wait);
428 EXPORT_SYMBOL(md_handle_request);
430 static void md_submit_bio(struct bio *bio)
432 const int rw = bio_data_dir(bio);
433 struct mddev *mddev = bio->bi_bdev->bd_disk->private_data;
435 if (mddev == NULL || mddev->pers == NULL) {
440 if (unlikely(test_bit(MD_BROKEN, &mddev->flags)) && (rw == WRITE)) {
445 bio = bio_split_to_limits(bio);
447 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
448 if (bio_sectors(bio) != 0)
449 bio->bi_status = BLK_STS_IOERR;
454 /* bio could be mergeable after passing to underlayer */
455 bio->bi_opf &= ~REQ_NOMERGE;
457 md_handle_request(mddev, bio);
460 /* mddev_suspend makes sure no new requests are submitted
461 * to the device, and that any requests that have been submitted
462 * are completely handled.
463 * Once mddev_detach() is called and completes, the module will be
466 void mddev_suspend(struct mddev *mddev)
468 WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk);
469 lockdep_assert_held(&mddev->reconfig_mutex);
470 if (mddev->suspended++)
473 wake_up(&mddev->sb_wait);
474 set_bit(MD_ALLOW_SB_UPDATE, &mddev->flags);
475 smp_mb__after_atomic();
476 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
477 mddev->pers->quiesce(mddev, 1);
478 clear_bit_unlock(MD_ALLOW_SB_UPDATE, &mddev->flags);
479 wait_event(mddev->sb_wait, !test_bit(MD_UPDATING_SB, &mddev->flags));
481 del_timer_sync(&mddev->safemode_timer);
482 /* restrict memory reclaim I/O during raid array is suspend */
483 mddev->noio_flag = memalloc_noio_save();
485 EXPORT_SYMBOL_GPL(mddev_suspend);
487 void mddev_resume(struct mddev *mddev)
489 /* entred the memalloc scope from mddev_suspend() */
490 memalloc_noio_restore(mddev->noio_flag);
491 lockdep_assert_held(&mddev->reconfig_mutex);
492 if (--mddev->suspended)
494 wake_up(&mddev->sb_wait);
495 mddev->pers->quiesce(mddev, 0);
497 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
498 md_wakeup_thread(mddev->thread);
499 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
501 EXPORT_SYMBOL_GPL(mddev_resume);
504 * Generic flush handling for md
507 static void md_end_flush(struct bio *bio)
509 struct md_rdev *rdev = bio->bi_private;
510 struct mddev *mddev = rdev->mddev;
512 rdev_dec_pending(rdev, mddev);
514 if (atomic_dec_and_test(&mddev->flush_pending)) {
515 /* The pre-request flush has finished */
516 queue_work(md_wq, &mddev->flush_work);
521 static void md_submit_flush_data(struct work_struct *ws);
523 static void submit_flushes(struct work_struct *ws)
525 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
526 struct md_rdev *rdev;
528 mddev->start_flush = ktime_get_boottime();
529 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
530 atomic_set(&mddev->flush_pending, 1);
532 rdev_for_each_rcu(rdev, mddev)
533 if (rdev->raid_disk >= 0 &&
534 !test_bit(Faulty, &rdev->flags)) {
535 /* Take two references, one is dropped
536 * when request finishes, one after
537 * we reclaim rcu_read_lock
540 atomic_inc(&rdev->nr_pending);
541 atomic_inc(&rdev->nr_pending);
543 bi = bio_alloc_bioset(rdev->bdev, 0,
544 REQ_OP_WRITE | REQ_PREFLUSH,
545 GFP_NOIO, &mddev->bio_set);
546 bi->bi_end_io = md_end_flush;
547 bi->bi_private = rdev;
548 atomic_inc(&mddev->flush_pending);
551 rdev_dec_pending(rdev, mddev);
554 if (atomic_dec_and_test(&mddev->flush_pending))
555 queue_work(md_wq, &mddev->flush_work);
558 static void md_submit_flush_data(struct work_struct *ws)
560 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
561 struct bio *bio = mddev->flush_bio;
564 * must reset flush_bio before calling into md_handle_request to avoid a
565 * deadlock, because other bios passed md_handle_request suspend check
566 * could wait for this and below md_handle_request could wait for those
567 * bios because of suspend check
569 spin_lock_irq(&mddev->lock);
570 mddev->prev_flush_start = mddev->start_flush;
571 mddev->flush_bio = NULL;
572 spin_unlock_irq(&mddev->lock);
573 wake_up(&mddev->sb_wait);
575 if (bio->bi_iter.bi_size == 0) {
576 /* an empty barrier - all done */
579 bio->bi_opf &= ~REQ_PREFLUSH;
580 md_handle_request(mddev, bio);
585 * Manages consolidation of flushes and submitting any flushes needed for
586 * a bio with REQ_PREFLUSH. Returns true if the bio is finished or is
587 * being finished in another context. Returns false if the flushing is
588 * complete but still needs the I/O portion of the bio to be processed.
590 bool md_flush_request(struct mddev *mddev, struct bio *bio)
592 ktime_t req_start = ktime_get_boottime();
593 spin_lock_irq(&mddev->lock);
594 /* flush requests wait until ongoing flush completes,
595 * hence coalescing all the pending requests.
597 wait_event_lock_irq(mddev->sb_wait,
599 ktime_before(req_start, mddev->prev_flush_start),
601 /* new request after previous flush is completed */
602 if (ktime_after(req_start, mddev->prev_flush_start)) {
603 WARN_ON(mddev->flush_bio);
604 mddev->flush_bio = bio;
607 spin_unlock_irq(&mddev->lock);
610 INIT_WORK(&mddev->flush_work, submit_flushes);
611 queue_work(md_wq, &mddev->flush_work);
613 /* flush was performed for some other bio while we waited. */
614 if (bio->bi_iter.bi_size == 0)
615 /* an empty barrier - all done */
618 bio->bi_opf &= ~REQ_PREFLUSH;
624 EXPORT_SYMBOL(md_flush_request);
626 static inline struct mddev *mddev_get(struct mddev *mddev)
628 lockdep_assert_held(&all_mddevs_lock);
630 if (test_bit(MD_DELETED, &mddev->flags))
632 atomic_inc(&mddev->active);
636 static void mddev_delayed_delete(struct work_struct *ws);
638 void mddev_put(struct mddev *mddev)
640 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
642 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
643 mddev->ctime == 0 && !mddev->hold_active) {
644 /* Array is not configured at all, and not held active,
646 set_bit(MD_DELETED, &mddev->flags);
649 * Call queue_work inside the spinlock so that
650 * flush_workqueue() after mddev_find will succeed in waiting
651 * for the work to be done.
653 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
654 queue_work(md_misc_wq, &mddev->del_work);
656 spin_unlock(&all_mddevs_lock);
659 static void md_safemode_timeout(struct timer_list *t);
661 void mddev_init(struct mddev *mddev)
663 mutex_init(&mddev->open_mutex);
664 mutex_init(&mddev->reconfig_mutex);
665 mutex_init(&mddev->bitmap_info.mutex);
666 INIT_LIST_HEAD(&mddev->disks);
667 INIT_LIST_HEAD(&mddev->all_mddevs);
668 timer_setup(&mddev->safemode_timer, md_safemode_timeout, 0);
669 atomic_set(&mddev->active, 1);
670 atomic_set(&mddev->openers, 0);
671 atomic_set(&mddev->active_io, 0);
672 spin_lock_init(&mddev->lock);
673 atomic_set(&mddev->flush_pending, 0);
674 init_waitqueue_head(&mddev->sb_wait);
675 init_waitqueue_head(&mddev->recovery_wait);
676 mddev->reshape_position = MaxSector;
677 mddev->reshape_backwards = 0;
678 mddev->last_sync_action = "none";
679 mddev->resync_min = 0;
680 mddev->resync_max = MaxSector;
681 mddev->level = LEVEL_NONE;
683 EXPORT_SYMBOL_GPL(mddev_init);
685 static struct mddev *mddev_find_locked(dev_t unit)
689 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
690 if (mddev->unit == unit)
696 /* find an unused unit number */
697 static dev_t mddev_alloc_unit(void)
699 static int next_minor = 512;
700 int start = next_minor;
705 dev = MKDEV(MD_MAJOR, next_minor);
707 if (next_minor > MINORMASK)
709 if (next_minor == start)
710 return 0; /* Oh dear, all in use. */
711 is_free = !mddev_find_locked(dev);
717 static struct mddev *mddev_alloc(dev_t unit)
722 if (unit && MAJOR(unit) != MD_MAJOR)
723 unit &= ~((1 << MdpMinorShift) - 1);
725 new = kzalloc(sizeof(*new), GFP_KERNEL);
727 return ERR_PTR(-ENOMEM);
730 spin_lock(&all_mddevs_lock);
733 if (mddev_find_locked(unit))
736 if (MAJOR(unit) == MD_MAJOR)
737 new->md_minor = MINOR(unit);
739 new->md_minor = MINOR(unit) >> MdpMinorShift;
740 new->hold_active = UNTIL_IOCTL;
743 new->unit = mddev_alloc_unit();
746 new->md_minor = MINOR(new->unit);
747 new->hold_active = UNTIL_STOP;
750 list_add(&new->all_mddevs, &all_mddevs);
751 spin_unlock(&all_mddevs_lock);
754 spin_unlock(&all_mddevs_lock);
756 return ERR_PTR(error);
759 static void mddev_free(struct mddev *mddev)
761 spin_lock(&all_mddevs_lock);
762 list_del(&mddev->all_mddevs);
763 spin_unlock(&all_mddevs_lock);
768 static const struct attribute_group md_redundancy_group;
770 void mddev_unlock(struct mddev *mddev)
772 if (mddev->to_remove) {
773 /* These cannot be removed under reconfig_mutex as
774 * an access to the files will try to take reconfig_mutex
775 * while holding the file unremovable, which leads to
777 * So hold set sysfs_active while the remove in happeing,
778 * and anything else which might set ->to_remove or my
779 * otherwise change the sysfs namespace will fail with
780 * -EBUSY if sysfs_active is still set.
781 * We set sysfs_active under reconfig_mutex and elsewhere
782 * test it under the same mutex to ensure its correct value
785 const struct attribute_group *to_remove = mddev->to_remove;
786 mddev->to_remove = NULL;
787 mddev->sysfs_active = 1;
788 mutex_unlock(&mddev->reconfig_mutex);
790 if (mddev->kobj.sd) {
791 if (to_remove != &md_redundancy_group)
792 sysfs_remove_group(&mddev->kobj, to_remove);
793 if (mddev->pers == NULL ||
794 mddev->pers->sync_request == NULL) {
795 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
796 if (mddev->sysfs_action)
797 sysfs_put(mddev->sysfs_action);
798 if (mddev->sysfs_completed)
799 sysfs_put(mddev->sysfs_completed);
800 if (mddev->sysfs_degraded)
801 sysfs_put(mddev->sysfs_degraded);
802 mddev->sysfs_action = NULL;
803 mddev->sysfs_completed = NULL;
804 mddev->sysfs_degraded = NULL;
807 mddev->sysfs_active = 0;
809 mutex_unlock(&mddev->reconfig_mutex);
811 /* As we've dropped the mutex we need a spinlock to
812 * make sure the thread doesn't disappear
814 spin_lock(&pers_lock);
815 md_wakeup_thread(mddev->thread);
816 wake_up(&mddev->sb_wait);
817 spin_unlock(&pers_lock);
819 EXPORT_SYMBOL_GPL(mddev_unlock);
821 struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
823 struct md_rdev *rdev;
825 rdev_for_each_rcu(rdev, mddev)
826 if (rdev->desc_nr == nr)
831 EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
833 static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
835 struct md_rdev *rdev;
837 rdev_for_each(rdev, mddev)
838 if (rdev->bdev->bd_dev == dev)
844 struct md_rdev *md_find_rdev_rcu(struct mddev *mddev, dev_t dev)
846 struct md_rdev *rdev;
848 rdev_for_each_rcu(rdev, mddev)
849 if (rdev->bdev->bd_dev == dev)
854 EXPORT_SYMBOL_GPL(md_find_rdev_rcu);
856 static struct md_personality *find_pers(int level, char *clevel)
858 struct md_personality *pers;
859 list_for_each_entry(pers, &pers_list, list) {
860 if (level != LEVEL_NONE && pers->level == level)
862 if (strcmp(pers->name, clevel)==0)
868 /* return the offset of the super block in 512byte sectors */
869 static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
871 return MD_NEW_SIZE_SECTORS(bdev_nr_sectors(rdev->bdev));
874 static int alloc_disk_sb(struct md_rdev *rdev)
876 rdev->sb_page = alloc_page(GFP_KERNEL);
882 void md_rdev_clear(struct md_rdev *rdev)
885 put_page(rdev->sb_page);
887 rdev->sb_page = NULL;
892 put_page(rdev->bb_page);
893 rdev->bb_page = NULL;
895 badblocks_exit(&rdev->badblocks);
897 EXPORT_SYMBOL_GPL(md_rdev_clear);
899 static void super_written(struct bio *bio)
901 struct md_rdev *rdev = bio->bi_private;
902 struct mddev *mddev = rdev->mddev;
904 if (bio->bi_status) {
905 pr_err("md: %s gets error=%d\n", __func__,
906 blk_status_to_errno(bio->bi_status));
907 md_error(mddev, rdev);
908 if (!test_bit(Faulty, &rdev->flags)
909 && (bio->bi_opf & MD_FAILFAST)) {
910 set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags);
911 set_bit(LastDev, &rdev->flags);
914 clear_bit(LastDev, &rdev->flags);
916 if (atomic_dec_and_test(&mddev->pending_writes))
917 wake_up(&mddev->sb_wait);
918 rdev_dec_pending(rdev, mddev);
922 void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
923 sector_t sector, int size, struct page *page)
925 /* write first size bytes of page to sector of rdev
926 * Increment mddev->pending_writes before returning
927 * and decrement it on completion, waking up sb_wait
928 * if zero is reached.
929 * If an error occurred, call md_error
936 if (test_bit(Faulty, &rdev->flags))
939 bio = bio_alloc_bioset(rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev,
941 REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA,
942 GFP_NOIO, &mddev->sync_set);
944 atomic_inc(&rdev->nr_pending);
946 bio->bi_iter.bi_sector = sector;
947 bio_add_page(bio, page, size, 0);
948 bio->bi_private = rdev;
949 bio->bi_end_io = super_written;
951 if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) &&
952 test_bit(FailFast, &rdev->flags) &&
953 !test_bit(LastDev, &rdev->flags))
954 bio->bi_opf |= MD_FAILFAST;
956 atomic_inc(&mddev->pending_writes);
960 int md_super_wait(struct mddev *mddev)
962 /* wait for all superblock writes that were scheduled to complete */
963 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
964 if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags))
969 int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
970 struct page *page, blk_opf_t opf, bool metadata_op)
975 if (metadata_op && rdev->meta_bdev)
976 bio_init(&bio, rdev->meta_bdev, &bvec, 1, opf);
978 bio_init(&bio, rdev->bdev, &bvec, 1, opf);
981 bio.bi_iter.bi_sector = sector + rdev->sb_start;
982 else if (rdev->mddev->reshape_position != MaxSector &&
983 (rdev->mddev->reshape_backwards ==
984 (sector >= rdev->mddev->reshape_position)))
985 bio.bi_iter.bi_sector = sector + rdev->new_data_offset;
987 bio.bi_iter.bi_sector = sector + rdev->data_offset;
988 bio_add_page(&bio, page, size, 0);
990 submit_bio_wait(&bio);
992 return !bio.bi_status;
994 EXPORT_SYMBOL_GPL(sync_page_io);
996 static int read_disk_sb(struct md_rdev *rdev, int size)
1001 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, true))
1003 rdev->sb_loaded = 1;
1007 pr_err("md: disabled device %pg, could not read superblock.\n",
1012 static int md_uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
1014 return sb1->set_uuid0 == sb2->set_uuid0 &&
1015 sb1->set_uuid1 == sb2->set_uuid1 &&
1016 sb1->set_uuid2 == sb2->set_uuid2 &&
1017 sb1->set_uuid3 == sb2->set_uuid3;
1020 static int md_sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
1023 mdp_super_t *tmp1, *tmp2;
1025 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
1026 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
1028 if (!tmp1 || !tmp2) {
1037 * nr_disks is not constant
1042 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
1049 static u32 md_csum_fold(u32 csum)
1051 csum = (csum & 0xffff) + (csum >> 16);
1052 return (csum & 0xffff) + (csum >> 16);
1055 static unsigned int calc_sb_csum(mdp_super_t *sb)
1058 u32 *sb32 = (u32*)sb;
1060 unsigned int disk_csum, csum;
1062 disk_csum = sb->sb_csum;
1065 for (i = 0; i < MD_SB_BYTES/4 ; i++)
1067 csum = (newcsum & 0xffffffff) + (newcsum>>32);
1070 /* This used to use csum_partial, which was wrong for several
1071 * reasons including that different results are returned on
1072 * different architectures. It isn't critical that we get exactly
1073 * the same return value as before (we always csum_fold before
1074 * testing, and that removes any differences). However as we
1075 * know that csum_partial always returned a 16bit value on
1076 * alphas, do a fold to maximise conformity to previous behaviour.
1078 sb->sb_csum = md_csum_fold(disk_csum);
1080 sb->sb_csum = disk_csum;
1086 * Handle superblock details.
1087 * We want to be able to handle multiple superblock formats
1088 * so we have a common interface to them all, and an array of
1089 * different handlers.
1090 * We rely on user-space to write the initial superblock, and support
1091 * reading and updating of superblocks.
1092 * Interface methods are:
1093 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
1094 * loads and validates a superblock on dev.
1095 * if refdev != NULL, compare superblocks on both devices
1097 * 0 - dev has a superblock that is compatible with refdev
1098 * 1 - dev has a superblock that is compatible and newer than refdev
1099 * so dev should be used as the refdev in future
1100 * -EINVAL superblock incompatible or invalid
1101 * -othererror e.g. -EIO
1103 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
1104 * Verify that dev is acceptable into mddev.
1105 * The first time, mddev->raid_disks will be 0, and data from
1106 * dev should be merged in. Subsequent calls check that dev
1107 * is new enough. Return 0 or -EINVAL
1109 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
1110 * Update the superblock for rdev with data in mddev
1111 * This does not write to disc.
1117 struct module *owner;
1118 int (*load_super)(struct md_rdev *rdev,
1119 struct md_rdev *refdev,
1121 int (*validate_super)(struct mddev *mddev,
1122 struct md_rdev *rdev);
1123 void (*sync_super)(struct mddev *mddev,
1124 struct md_rdev *rdev);
1125 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
1126 sector_t num_sectors);
1127 int (*allow_new_offset)(struct md_rdev *rdev,
1128 unsigned long long new_offset);
1132 * Check that the given mddev has no bitmap.
1134 * This function is called from the run method of all personalities that do not
1135 * support bitmaps. It prints an error message and returns non-zero if mddev
1136 * has a bitmap. Otherwise, it returns 0.
1139 int md_check_no_bitmap(struct mddev *mddev)
1141 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
1143 pr_warn("%s: bitmaps are not supported for %s\n",
1144 mdname(mddev), mddev->pers->name);
1147 EXPORT_SYMBOL(md_check_no_bitmap);
1150 * load_super for 0.90.0
1152 static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
1156 bool spare_disk = true;
1159 * Calculate the position of the superblock (512byte sectors),
1160 * it's at the end of the disk.
1162 * It also happens to be a multiple of 4Kb.
1164 rdev->sb_start = calc_dev_sboffset(rdev);
1166 ret = read_disk_sb(rdev, MD_SB_BYTES);
1172 sb = page_address(rdev->sb_page);
1174 if (sb->md_magic != MD_SB_MAGIC) {
1175 pr_warn("md: invalid raid superblock magic on %pg\n",
1180 if (sb->major_version != 0 ||
1181 sb->minor_version < 90 ||
1182 sb->minor_version > 91) {
1183 pr_warn("Bad version number %d.%d on %pg\n",
1184 sb->major_version, sb->minor_version, rdev->bdev);
1188 if (sb->raid_disks <= 0)
1191 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
1192 pr_warn("md: invalid superblock checksum on %pg\n", rdev->bdev);
1196 rdev->preferred_minor = sb->md_minor;
1197 rdev->data_offset = 0;
1198 rdev->new_data_offset = 0;
1199 rdev->sb_size = MD_SB_BYTES;
1200 rdev->badblocks.shift = -1;
1202 if (sb->level == LEVEL_MULTIPATH)
1205 rdev->desc_nr = sb->this_disk.number;
1207 /* not spare disk, or LEVEL_MULTIPATH */
1208 if (sb->level == LEVEL_MULTIPATH ||
1209 (rdev->desc_nr >= 0 &&
1210 rdev->desc_nr < MD_SB_DISKS &&
1211 sb->disks[rdev->desc_nr].state &
1212 ((1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))))
1222 mdp_super_t *refsb = page_address(refdev->sb_page);
1223 if (!md_uuid_equal(refsb, sb)) {
1224 pr_warn("md: %pg has different UUID to %pg\n",
1225 rdev->bdev, refdev->bdev);
1228 if (!md_sb_equal(refsb, sb)) {
1229 pr_warn("md: %pg has same UUID but different superblock to %pg\n",
1230 rdev->bdev, refdev->bdev);
1234 ev2 = md_event(refsb);
1236 if (!spare_disk && ev1 > ev2)
1241 rdev->sectors = rdev->sb_start;
1242 /* Limit to 4TB as metadata cannot record more than that.
1243 * (not needed for Linear and RAID0 as metadata doesn't
1246 if ((u64)rdev->sectors >= (2ULL << 32) && sb->level >= 1)
1247 rdev->sectors = (sector_t)(2ULL << 32) - 2;
1249 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
1250 /* "this cannot possibly happen" ... */
1258 * validate_super for 0.90.0
1260 static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
1263 mdp_super_t *sb = page_address(rdev->sb_page);
1264 __u64 ev1 = md_event(sb);
1266 rdev->raid_disk = -1;
1267 clear_bit(Faulty, &rdev->flags);
1268 clear_bit(In_sync, &rdev->flags);
1269 clear_bit(Bitmap_sync, &rdev->flags);
1270 clear_bit(WriteMostly, &rdev->flags);
1272 if (mddev->raid_disks == 0) {
1273 mddev->major_version = 0;
1274 mddev->minor_version = sb->minor_version;
1275 mddev->patch_version = sb->patch_version;
1276 mddev->external = 0;
1277 mddev->chunk_sectors = sb->chunk_size >> 9;
1278 mddev->ctime = sb->ctime;
1279 mddev->utime = sb->utime;
1280 mddev->level = sb->level;
1281 mddev->clevel[0] = 0;
1282 mddev->layout = sb->layout;
1283 mddev->raid_disks = sb->raid_disks;
1284 mddev->dev_sectors = ((sector_t)sb->size) * 2;
1285 mddev->events = ev1;
1286 mddev->bitmap_info.offset = 0;
1287 mddev->bitmap_info.space = 0;
1288 /* bitmap can use 60 K after the 4K superblocks */
1289 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
1290 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
1291 mddev->reshape_backwards = 0;
1293 if (mddev->minor_version >= 91) {
1294 mddev->reshape_position = sb->reshape_position;
1295 mddev->delta_disks = sb->delta_disks;
1296 mddev->new_level = sb->new_level;
1297 mddev->new_layout = sb->new_layout;
1298 mddev->new_chunk_sectors = sb->new_chunk >> 9;
1299 if (mddev->delta_disks < 0)
1300 mddev->reshape_backwards = 1;
1302 mddev->reshape_position = MaxSector;
1303 mddev->delta_disks = 0;
1304 mddev->new_level = mddev->level;
1305 mddev->new_layout = mddev->layout;
1306 mddev->new_chunk_sectors = mddev->chunk_sectors;
1308 if (mddev->level == 0)
1311 if (sb->state & (1<<MD_SB_CLEAN))
1312 mddev->recovery_cp = MaxSector;
1314 if (sb->events_hi == sb->cp_events_hi &&
1315 sb->events_lo == sb->cp_events_lo) {
1316 mddev->recovery_cp = sb->recovery_cp;
1318 mddev->recovery_cp = 0;
1321 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1322 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1323 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1324 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1326 mddev->max_disks = MD_SB_DISKS;
1328 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
1329 mddev->bitmap_info.file == NULL) {
1330 mddev->bitmap_info.offset =
1331 mddev->bitmap_info.default_offset;
1332 mddev->bitmap_info.space =
1333 mddev->bitmap_info.default_space;
1336 } else if (mddev->pers == NULL) {
1337 /* Insist on good event counter while assembling, except
1338 * for spares (which don't need an event count) */
1340 if (sb->disks[rdev->desc_nr].state & (
1341 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1342 if (ev1 < mddev->events)
1344 } else if (mddev->bitmap) {
1345 /* if adding to array with a bitmap, then we can accept an
1346 * older device ... but not too old.
1348 if (ev1 < mddev->bitmap->events_cleared)
1350 if (ev1 < mddev->events)
1351 set_bit(Bitmap_sync, &rdev->flags);
1353 if (ev1 < mddev->events)
1354 /* just a hot-add of a new device, leave raid_disk at -1 */
1358 if (mddev->level != LEVEL_MULTIPATH) {
1359 desc = sb->disks + rdev->desc_nr;
1361 if (desc->state & (1<<MD_DISK_FAULTY))
1362 set_bit(Faulty, &rdev->flags);
1363 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1364 desc->raid_disk < mddev->raid_disks */) {
1365 set_bit(In_sync, &rdev->flags);
1366 rdev->raid_disk = desc->raid_disk;
1367 rdev->saved_raid_disk = desc->raid_disk;
1368 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1369 /* active but not in sync implies recovery up to
1370 * reshape position. We don't know exactly where
1371 * that is, so set to zero for now */
1372 if (mddev->minor_version >= 91) {
1373 rdev->recovery_offset = 0;
1374 rdev->raid_disk = desc->raid_disk;
1377 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1378 set_bit(WriteMostly, &rdev->flags);
1379 if (desc->state & (1<<MD_DISK_FAILFAST))
1380 set_bit(FailFast, &rdev->flags);
1381 } else /* MULTIPATH are always insync */
1382 set_bit(In_sync, &rdev->flags);
1387 * sync_super for 0.90.0
1389 static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
1392 struct md_rdev *rdev2;
1393 int next_spare = mddev->raid_disks;
1395 /* make rdev->sb match mddev data..
1398 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1399 * 3/ any empty disks < next_spare become removed
1401 * disks[0] gets initialised to REMOVED because
1402 * we cannot be sure from other fields if it has
1403 * been initialised or not.
1406 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1408 rdev->sb_size = MD_SB_BYTES;
1410 sb = page_address(rdev->sb_page);
1412 memset(sb, 0, sizeof(*sb));
1414 sb->md_magic = MD_SB_MAGIC;
1415 sb->major_version = mddev->major_version;
1416 sb->patch_version = mddev->patch_version;
1417 sb->gvalid_words = 0; /* ignored */
1418 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1419 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1420 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1421 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1423 sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
1424 sb->level = mddev->level;
1425 sb->size = mddev->dev_sectors / 2;
1426 sb->raid_disks = mddev->raid_disks;
1427 sb->md_minor = mddev->md_minor;
1428 sb->not_persistent = 0;
1429 sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
1431 sb->events_hi = (mddev->events>>32);
1432 sb->events_lo = (u32)mddev->events;
1434 if (mddev->reshape_position == MaxSector)
1435 sb->minor_version = 90;
1437 sb->minor_version = 91;
1438 sb->reshape_position = mddev->reshape_position;
1439 sb->new_level = mddev->new_level;
1440 sb->delta_disks = mddev->delta_disks;
1441 sb->new_layout = mddev->new_layout;
1442 sb->new_chunk = mddev->new_chunk_sectors << 9;
1444 mddev->minor_version = sb->minor_version;
1447 sb->recovery_cp = mddev->recovery_cp;
1448 sb->cp_events_hi = (mddev->events>>32);
1449 sb->cp_events_lo = (u32)mddev->events;
1450 if (mddev->recovery_cp == MaxSector)
1451 sb->state = (1<< MD_SB_CLEAN);
1453 sb->recovery_cp = 0;
1455 sb->layout = mddev->layout;
1456 sb->chunk_size = mddev->chunk_sectors << 9;
1458 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
1459 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1461 sb->disks[0].state = (1<<MD_DISK_REMOVED);
1462 rdev_for_each(rdev2, mddev) {
1465 int is_active = test_bit(In_sync, &rdev2->flags);
1467 if (rdev2->raid_disk >= 0 &&
1468 sb->minor_version >= 91)
1469 /* we have nowhere to store the recovery_offset,
1470 * but if it is not below the reshape_position,
1471 * we can piggy-back on that.
1474 if (rdev2->raid_disk < 0 ||
1475 test_bit(Faulty, &rdev2->flags))
1478 desc_nr = rdev2->raid_disk;
1480 desc_nr = next_spare++;
1481 rdev2->desc_nr = desc_nr;
1482 d = &sb->disks[rdev2->desc_nr];
1484 d->number = rdev2->desc_nr;
1485 d->major = MAJOR(rdev2->bdev->bd_dev);
1486 d->minor = MINOR(rdev2->bdev->bd_dev);
1488 d->raid_disk = rdev2->raid_disk;
1490 d->raid_disk = rdev2->desc_nr; /* compatibility */
1491 if (test_bit(Faulty, &rdev2->flags))
1492 d->state = (1<<MD_DISK_FAULTY);
1493 else if (is_active) {
1494 d->state = (1<<MD_DISK_ACTIVE);
1495 if (test_bit(In_sync, &rdev2->flags))
1496 d->state |= (1<<MD_DISK_SYNC);
1504 if (test_bit(WriteMostly, &rdev2->flags))
1505 d->state |= (1<<MD_DISK_WRITEMOSTLY);
1506 if (test_bit(FailFast, &rdev2->flags))
1507 d->state |= (1<<MD_DISK_FAILFAST);
1509 /* now set the "removed" and "faulty" bits on any missing devices */
1510 for (i=0 ; i < mddev->raid_disks ; i++) {
1511 mdp_disk_t *d = &sb->disks[i];
1512 if (d->state == 0 && d->number == 0) {
1515 d->state = (1<<MD_DISK_REMOVED);
1516 d->state |= (1<<MD_DISK_FAULTY);
1520 sb->nr_disks = nr_disks;
1521 sb->active_disks = active;
1522 sb->working_disks = working;
1523 sb->failed_disks = failed;
1524 sb->spare_disks = spare;
1526 sb->this_disk = sb->disks[rdev->desc_nr];
1527 sb->sb_csum = calc_sb_csum(sb);
1531 * rdev_size_change for 0.90.0
1533 static unsigned long long
1534 super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
1536 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1537 return 0; /* component must fit device */
1538 if (rdev->mddev->bitmap_info.offset)
1539 return 0; /* can't move bitmap */
1540 rdev->sb_start = calc_dev_sboffset(rdev);
1541 if (!num_sectors || num_sectors > rdev->sb_start)
1542 num_sectors = rdev->sb_start;
1543 /* Limit to 4TB as metadata cannot record more than that.
1544 * 4TB == 2^32 KB, or 2*2^32 sectors.
1546 if ((u64)num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
1547 num_sectors = (sector_t)(2ULL << 32) - 2;
1549 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1551 } while (md_super_wait(rdev->mddev) < 0);
1556 super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1558 /* non-zero offset changes not possible with v0.90 */
1559 return new_offset == 0;
1563 * version 1 superblock
1566 static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
1570 unsigned long long newcsum;
1571 int size = 256 + le32_to_cpu(sb->max_dev)*2;
1572 __le32 *isuper = (__le32*)sb;
1574 disk_csum = sb->sb_csum;
1577 for (; size >= 4; size -= 4)
1578 newcsum += le32_to_cpu(*isuper++);
1581 newcsum += le16_to_cpu(*(__le16*) isuper);
1583 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1584 sb->sb_csum = disk_csum;
1585 return cpu_to_le32(csum);
1588 static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
1590 struct mdp_superblock_1 *sb;
1595 bool spare_disk = true;
1598 * Calculate the position of the superblock in 512byte sectors.
1599 * It is always aligned to a 4K boundary and
1600 * depeding on minor_version, it can be:
1601 * 0: At least 8K, but less than 12K, from end of device
1602 * 1: At start of device
1603 * 2: 4K from start of device.
1605 switch(minor_version) {
1607 sb_start = bdev_nr_sectors(rdev->bdev) - 8 * 2;
1608 sb_start &= ~(sector_t)(4*2-1);
1619 rdev->sb_start = sb_start;
1621 /* superblock is rarely larger than 1K, but it can be larger,
1622 * and it is safe to read 4k, so we do that
1624 ret = read_disk_sb(rdev, 4096);
1625 if (ret) return ret;
1627 sb = page_address(rdev->sb_page);
1629 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1630 sb->major_version != cpu_to_le32(1) ||
1631 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1632 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1633 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1636 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1637 pr_warn("md: invalid superblock checksum on %pg\n",
1641 if (le64_to_cpu(sb->data_size) < 10) {
1642 pr_warn("md: data_size too small on %pg\n",
1648 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1649 /* Some padding is non-zero, might be a new feature */
1652 rdev->preferred_minor = 0xffff;
1653 rdev->data_offset = le64_to_cpu(sb->data_offset);
1654 rdev->new_data_offset = rdev->data_offset;
1655 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1656 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1657 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
1658 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1660 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1661 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1662 if (rdev->sb_size & bmask)
1663 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1666 && rdev->data_offset < sb_start + (rdev->sb_size/512))
1669 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1672 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1675 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1677 if (!rdev->bb_page) {
1678 rdev->bb_page = alloc_page(GFP_KERNEL);
1682 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1683 rdev->badblocks.count == 0) {
1684 /* need to load the bad block list.
1685 * Currently we limit it to one page.
1691 int sectors = le16_to_cpu(sb->bblog_size);
1692 if (sectors > (PAGE_SIZE / 512))
1694 offset = le32_to_cpu(sb->bblog_offset);
1697 bb_sector = (long long)offset;
1698 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1699 rdev->bb_page, REQ_OP_READ, true))
1701 bbp = (__le64 *)page_address(rdev->bb_page);
1702 rdev->badblocks.shift = sb->bblog_shift;
1703 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1704 u64 bb = le64_to_cpu(*bbp);
1705 int count = bb & (0x3ff);
1706 u64 sector = bb >> 10;
1707 sector <<= sb->bblog_shift;
1708 count <<= sb->bblog_shift;
1711 if (badblocks_set(&rdev->badblocks, sector, count, 1))
1714 } else if (sb->bblog_offset != 0)
1715 rdev->badblocks.shift = 0;
1717 if ((le32_to_cpu(sb->feature_map) &
1718 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS))) {
1719 rdev->ppl.offset = (__s16)le16_to_cpu(sb->ppl.offset);
1720 rdev->ppl.size = le16_to_cpu(sb->ppl.size);
1721 rdev->ppl.sector = rdev->sb_start + rdev->ppl.offset;
1724 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RAID0_LAYOUT) &&
1728 /* not spare disk, or LEVEL_MULTIPATH */
1729 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH) ||
1730 (rdev->desc_nr >= 0 &&
1731 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1732 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1733 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL)))
1743 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
1745 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1746 sb->level != refsb->level ||
1747 sb->layout != refsb->layout ||
1748 sb->chunksize != refsb->chunksize) {
1749 pr_warn("md: %pg has strangely different superblock to %pg\n",
1754 ev1 = le64_to_cpu(sb->events);
1755 ev2 = le64_to_cpu(refsb->events);
1757 if (!spare_disk && ev1 > ev2)
1763 sectors = bdev_nr_sectors(rdev->bdev) - rdev->data_offset;
1765 sectors = rdev->sb_start;
1766 if (sectors < le64_to_cpu(sb->data_size))
1768 rdev->sectors = le64_to_cpu(sb->data_size);
1772 static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
1774 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
1775 __u64 ev1 = le64_to_cpu(sb->events);
1777 rdev->raid_disk = -1;
1778 clear_bit(Faulty, &rdev->flags);
1779 clear_bit(In_sync, &rdev->flags);
1780 clear_bit(Bitmap_sync, &rdev->flags);
1781 clear_bit(WriteMostly, &rdev->flags);
1783 if (mddev->raid_disks == 0) {
1784 mddev->major_version = 1;
1785 mddev->patch_version = 0;
1786 mddev->external = 0;
1787 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
1788 mddev->ctime = le64_to_cpu(sb->ctime);
1789 mddev->utime = le64_to_cpu(sb->utime);
1790 mddev->level = le32_to_cpu(sb->level);
1791 mddev->clevel[0] = 0;
1792 mddev->layout = le32_to_cpu(sb->layout);
1793 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1794 mddev->dev_sectors = le64_to_cpu(sb->size);
1795 mddev->events = ev1;
1796 mddev->bitmap_info.offset = 0;
1797 mddev->bitmap_info.space = 0;
1798 /* Default location for bitmap is 1K after superblock
1799 * using 3K - total of 4K
1801 mddev->bitmap_info.default_offset = 1024 >> 9;
1802 mddev->bitmap_info.default_space = (4096-1024) >> 9;
1803 mddev->reshape_backwards = 0;
1805 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1806 memcpy(mddev->uuid, sb->set_uuid, 16);
1808 mddev->max_disks = (4096-256)/2;
1810 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1811 mddev->bitmap_info.file == NULL) {
1812 mddev->bitmap_info.offset =
1813 (__s32)le32_to_cpu(sb->bitmap_offset);
1814 /* Metadata doesn't record how much space is available.
1815 * For 1.0, we assume we can use up to the superblock
1816 * if before, else to 4K beyond superblock.
1817 * For others, assume no change is possible.
1819 if (mddev->minor_version > 0)
1820 mddev->bitmap_info.space = 0;
1821 else if (mddev->bitmap_info.offset > 0)
1822 mddev->bitmap_info.space =
1823 8 - mddev->bitmap_info.offset;
1825 mddev->bitmap_info.space =
1826 -mddev->bitmap_info.offset;
1829 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1830 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1831 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1832 mddev->new_level = le32_to_cpu(sb->new_level);
1833 mddev->new_layout = le32_to_cpu(sb->new_layout);
1834 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
1835 if (mddev->delta_disks < 0 ||
1836 (mddev->delta_disks == 0 &&
1837 (le32_to_cpu(sb->feature_map)
1838 & MD_FEATURE_RESHAPE_BACKWARDS)))
1839 mddev->reshape_backwards = 1;
1841 mddev->reshape_position = MaxSector;
1842 mddev->delta_disks = 0;
1843 mddev->new_level = mddev->level;
1844 mddev->new_layout = mddev->layout;
1845 mddev->new_chunk_sectors = mddev->chunk_sectors;
1848 if (mddev->level == 0 &&
1849 !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RAID0_LAYOUT))
1852 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
1853 set_bit(MD_HAS_JOURNAL, &mddev->flags);
1855 if (le32_to_cpu(sb->feature_map) &
1856 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS)) {
1857 if (le32_to_cpu(sb->feature_map) &
1858 (MD_FEATURE_BITMAP_OFFSET | MD_FEATURE_JOURNAL))
1860 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) &&
1861 (le32_to_cpu(sb->feature_map) &
1862 MD_FEATURE_MULTIPLE_PPLS))
1864 set_bit(MD_HAS_PPL, &mddev->flags);
1866 } else if (mddev->pers == NULL) {
1867 /* Insist of good event counter while assembling, except for
1868 * spares (which don't need an event count) */
1870 if (rdev->desc_nr >= 0 &&
1871 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1872 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1873 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
1874 if (ev1 < mddev->events)
1876 } else if (mddev->bitmap) {
1877 /* If adding to array with a bitmap, then we can accept an
1878 * older device, but not too old.
1880 if (ev1 < mddev->bitmap->events_cleared)
1882 if (ev1 < mddev->events)
1883 set_bit(Bitmap_sync, &rdev->flags);
1885 if (ev1 < mddev->events)
1886 /* just a hot-add of a new device, leave raid_disk at -1 */
1889 if (mddev->level != LEVEL_MULTIPATH) {
1891 if (rdev->desc_nr < 0 ||
1892 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1893 role = MD_DISK_ROLE_SPARE;
1896 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1898 case MD_DISK_ROLE_SPARE: /* spare */
1900 case MD_DISK_ROLE_FAULTY: /* faulty */
1901 set_bit(Faulty, &rdev->flags);
1903 case MD_DISK_ROLE_JOURNAL: /* journal device */
1904 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1905 /* journal device without journal feature */
1906 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
1909 set_bit(Journal, &rdev->flags);
1910 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
1911 rdev->raid_disk = 0;
1914 rdev->saved_raid_disk = role;
1915 if ((le32_to_cpu(sb->feature_map) &
1916 MD_FEATURE_RECOVERY_OFFSET)) {
1917 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1918 if (!(le32_to_cpu(sb->feature_map) &
1919 MD_FEATURE_RECOVERY_BITMAP))
1920 rdev->saved_raid_disk = -1;
1923 * If the array is FROZEN, then the device can't
1924 * be in_sync with rest of array.
1926 if (!test_bit(MD_RECOVERY_FROZEN,
1928 set_bit(In_sync, &rdev->flags);
1930 rdev->raid_disk = role;
1933 if (sb->devflags & WriteMostly1)
1934 set_bit(WriteMostly, &rdev->flags);
1935 if (sb->devflags & FailFast1)
1936 set_bit(FailFast, &rdev->flags);
1937 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1938 set_bit(Replacement, &rdev->flags);
1939 } else /* MULTIPATH are always insync */
1940 set_bit(In_sync, &rdev->flags);
1945 static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
1947 struct mdp_superblock_1 *sb;
1948 struct md_rdev *rdev2;
1950 /* make rdev->sb match mddev and rdev data. */
1952 sb = page_address(rdev->sb_page);
1954 sb->feature_map = 0;
1956 sb->recovery_offset = cpu_to_le64(0);
1957 memset(sb->pad3, 0, sizeof(sb->pad3));
1959 sb->utime = cpu_to_le64((__u64)mddev->utime);
1960 sb->events = cpu_to_le64(mddev->events);
1962 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1963 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
1964 sb->resync_offset = cpu_to_le64(MaxSector);
1966 sb->resync_offset = cpu_to_le64(0);
1968 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1970 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1971 sb->size = cpu_to_le64(mddev->dev_sectors);
1972 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
1973 sb->level = cpu_to_le32(mddev->level);
1974 sb->layout = cpu_to_le32(mddev->layout);
1975 if (test_bit(FailFast, &rdev->flags))
1976 sb->devflags |= FailFast1;
1978 sb->devflags &= ~FailFast1;
1980 if (test_bit(WriteMostly, &rdev->flags))
1981 sb->devflags |= WriteMostly1;
1983 sb->devflags &= ~WriteMostly1;
1984 sb->data_offset = cpu_to_le64(rdev->data_offset);
1985 sb->data_size = cpu_to_le64(rdev->sectors);
1987 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1988 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
1989 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1992 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
1993 !test_bit(In_sync, &rdev->flags)) {
1995 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1996 sb->recovery_offset =
1997 cpu_to_le64(rdev->recovery_offset);
1998 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
2000 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
2002 /* Note: recovery_offset and journal_tail share space */
2003 if (test_bit(Journal, &rdev->flags))
2004 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
2005 if (test_bit(Replacement, &rdev->flags))
2007 cpu_to_le32(MD_FEATURE_REPLACEMENT);
2009 if (mddev->reshape_position != MaxSector) {
2010 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
2011 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
2012 sb->new_layout = cpu_to_le32(mddev->new_layout);
2013 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
2014 sb->new_level = cpu_to_le32(mddev->new_level);
2015 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
2016 if (mddev->delta_disks == 0 &&
2017 mddev->reshape_backwards)
2019 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
2020 if (rdev->new_data_offset != rdev->data_offset) {
2022 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
2023 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
2024 - rdev->data_offset));
2028 if (mddev_is_clustered(mddev))
2029 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
2031 if (rdev->badblocks.count == 0)
2032 /* Nothing to do for bad blocks*/ ;
2033 else if (sb->bblog_offset == 0)
2034 /* Cannot record bad blocks on this device */
2035 md_error(mddev, rdev);
2037 struct badblocks *bb = &rdev->badblocks;
2038 __le64 *bbp = (__le64 *)page_address(rdev->bb_page);
2040 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
2045 seq = read_seqbegin(&bb->lock);
2047 memset(bbp, 0xff, PAGE_SIZE);
2049 for (i = 0 ; i < bb->count ; i++) {
2050 u64 internal_bb = p[i];
2051 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
2052 | BB_LEN(internal_bb));
2053 bbp[i] = cpu_to_le64(store_bb);
2056 if (read_seqretry(&bb->lock, seq))
2059 bb->sector = (rdev->sb_start +
2060 (int)le32_to_cpu(sb->bblog_offset));
2061 bb->size = le16_to_cpu(sb->bblog_size);
2066 rdev_for_each(rdev2, mddev)
2067 if (rdev2->desc_nr+1 > max_dev)
2068 max_dev = rdev2->desc_nr+1;
2070 if (max_dev > le32_to_cpu(sb->max_dev)) {
2072 sb->max_dev = cpu_to_le32(max_dev);
2073 rdev->sb_size = max_dev * 2 + 256;
2074 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
2075 if (rdev->sb_size & bmask)
2076 rdev->sb_size = (rdev->sb_size | bmask) + 1;
2078 max_dev = le32_to_cpu(sb->max_dev);
2080 for (i=0; i<max_dev;i++)
2081 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
2083 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
2084 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
2086 if (test_bit(MD_HAS_PPL, &mddev->flags)) {
2087 if (test_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags))
2089 cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS);
2091 sb->feature_map |= cpu_to_le32(MD_FEATURE_PPL);
2092 sb->ppl.offset = cpu_to_le16(rdev->ppl.offset);
2093 sb->ppl.size = cpu_to_le16(rdev->ppl.size);
2096 rdev_for_each(rdev2, mddev) {
2098 if (test_bit(Faulty, &rdev2->flags))
2099 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
2100 else if (test_bit(In_sync, &rdev2->flags))
2101 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
2102 else if (test_bit(Journal, &rdev2->flags))
2103 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
2104 else if (rdev2->raid_disk >= 0)
2105 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
2107 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
2110 sb->sb_csum = calc_sb_1_csum(sb);
2113 static sector_t super_1_choose_bm_space(sector_t dev_size)
2117 /* if the device is bigger than 8Gig, save 64k for bitmap
2118 * usage, if bigger than 200Gig, save 128k
2120 if (dev_size < 64*2)
2122 else if (dev_size - 64*2 >= 200*1024*1024*2)
2124 else if (dev_size - 4*2 > 8*1024*1024*2)
2131 static unsigned long long
2132 super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
2134 struct mdp_superblock_1 *sb;
2135 sector_t max_sectors;
2136 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
2137 return 0; /* component must fit device */
2138 if (rdev->data_offset != rdev->new_data_offset)
2139 return 0; /* too confusing */
2140 if (rdev->sb_start < rdev->data_offset) {
2141 /* minor versions 1 and 2; superblock before data */
2142 max_sectors = bdev_nr_sectors(rdev->bdev) - rdev->data_offset;
2143 if (!num_sectors || num_sectors > max_sectors)
2144 num_sectors = max_sectors;
2145 } else if (rdev->mddev->bitmap_info.offset) {
2146 /* minor version 0 with bitmap we can't move */
2149 /* minor version 0; superblock after data */
2150 sector_t sb_start, bm_space;
2151 sector_t dev_size = bdev_nr_sectors(rdev->bdev);
2153 /* 8K is for superblock */
2154 sb_start = dev_size - 8*2;
2155 sb_start &= ~(sector_t)(4*2 - 1);
2157 bm_space = super_1_choose_bm_space(dev_size);
2159 /* Space that can be used to store date needs to decrease
2160 * superblock bitmap space and bad block space(4K)
2162 max_sectors = sb_start - bm_space - 4*2;
2164 if (!num_sectors || num_sectors > max_sectors)
2165 num_sectors = max_sectors;
2166 rdev->sb_start = sb_start;
2168 sb = page_address(rdev->sb_page);
2169 sb->data_size = cpu_to_le64(num_sectors);
2170 sb->super_offset = cpu_to_le64(rdev->sb_start);
2171 sb->sb_csum = calc_sb_1_csum(sb);
2173 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
2175 } while (md_super_wait(rdev->mddev) < 0);
2181 super_1_allow_new_offset(struct md_rdev *rdev,
2182 unsigned long long new_offset)
2184 /* All necessary checks on new >= old have been done */
2185 struct bitmap *bitmap;
2186 if (new_offset >= rdev->data_offset)
2189 /* with 1.0 metadata, there is no metadata to tread on
2190 * so we can always move back */
2191 if (rdev->mddev->minor_version == 0)
2194 /* otherwise we must be sure not to step on
2195 * any metadata, so stay:
2196 * 36K beyond start of superblock
2197 * beyond end of badblocks
2198 * beyond write-intent bitmap
2200 if (rdev->sb_start + (32+4)*2 > new_offset)
2202 bitmap = rdev->mddev->bitmap;
2203 if (bitmap && !rdev->mddev->bitmap_info.file &&
2204 rdev->sb_start + rdev->mddev->bitmap_info.offset +
2205 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
2207 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
2213 static struct super_type super_types[] = {
2216 .owner = THIS_MODULE,
2217 .load_super = super_90_load,
2218 .validate_super = super_90_validate,
2219 .sync_super = super_90_sync,
2220 .rdev_size_change = super_90_rdev_size_change,
2221 .allow_new_offset = super_90_allow_new_offset,
2225 .owner = THIS_MODULE,
2226 .load_super = super_1_load,
2227 .validate_super = super_1_validate,
2228 .sync_super = super_1_sync,
2229 .rdev_size_change = super_1_rdev_size_change,
2230 .allow_new_offset = super_1_allow_new_offset,
2234 static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
2236 if (mddev->sync_super) {
2237 mddev->sync_super(mddev, rdev);
2241 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
2243 super_types[mddev->major_version].sync_super(mddev, rdev);
2246 static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
2248 struct md_rdev *rdev, *rdev2;
2251 rdev_for_each_rcu(rdev, mddev1) {
2252 if (test_bit(Faulty, &rdev->flags) ||
2253 test_bit(Journal, &rdev->flags) ||
2254 rdev->raid_disk == -1)
2256 rdev_for_each_rcu(rdev2, mddev2) {
2257 if (test_bit(Faulty, &rdev2->flags) ||
2258 test_bit(Journal, &rdev2->flags) ||
2259 rdev2->raid_disk == -1)
2261 if (rdev->bdev->bd_disk == rdev2->bdev->bd_disk) {
2271 static LIST_HEAD(pending_raid_disks);
2274 * Try to register data integrity profile for an mddev
2276 * This is called when an array is started and after a disk has been kicked
2277 * from the array. It only succeeds if all working and active component devices
2278 * are integrity capable with matching profiles.
2280 int md_integrity_register(struct mddev *mddev)
2282 struct md_rdev *rdev, *reference = NULL;
2284 if (list_empty(&mddev->disks))
2285 return 0; /* nothing to do */
2286 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2287 return 0; /* shouldn't register, or already is */
2288 rdev_for_each(rdev, mddev) {
2289 /* skip spares and non-functional disks */
2290 if (test_bit(Faulty, &rdev->flags))
2292 if (rdev->raid_disk < 0)
2295 /* Use the first rdev as the reference */
2299 /* does this rdev's profile match the reference profile? */
2300 if (blk_integrity_compare(reference->bdev->bd_disk,
2301 rdev->bdev->bd_disk) < 0)
2304 if (!reference || !bdev_get_integrity(reference->bdev))
2307 * All component devices are integrity capable and have matching
2308 * profiles, register the common profile for the md device.
2310 blk_integrity_register(mddev->gendisk,
2311 bdev_get_integrity(reference->bdev));
2313 pr_debug("md: data integrity enabled on %s\n", mdname(mddev));
2314 if (bioset_integrity_create(&mddev->bio_set, BIO_POOL_SIZE) ||
2315 (mddev->level != 1 && mddev->level != 10 &&
2316 bioset_integrity_create(&mddev->io_acct_set, BIO_POOL_SIZE))) {
2318 * No need to handle the failure of bioset_integrity_create,
2319 * because the function is called by md_run() -> pers->run(),
2320 * md_run calls bioset_exit -> bioset_integrity_free in case
2323 pr_err("md: failed to create integrity pool for %s\n",
2329 EXPORT_SYMBOL(md_integrity_register);
2332 * Attempt to add an rdev, but only if it is consistent with the current
2335 int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
2337 struct blk_integrity *bi_mddev;
2339 if (!mddev->gendisk)
2342 bi_mddev = blk_get_integrity(mddev->gendisk);
2344 if (!bi_mddev) /* nothing to do */
2347 if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
2348 pr_err("%s: incompatible integrity profile for %pg\n",
2349 mdname(mddev), rdev->bdev);
2355 EXPORT_SYMBOL(md_integrity_add_rdev);
2357 static bool rdev_read_only(struct md_rdev *rdev)
2359 return bdev_read_only(rdev->bdev) ||
2360 (rdev->meta_bdev && bdev_read_only(rdev->meta_bdev));
2363 static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
2365 char b[BDEVNAME_SIZE];
2368 /* prevent duplicates */
2369 if (find_rdev(mddev, rdev->bdev->bd_dev))
2372 if (rdev_read_only(rdev) && mddev->pers)
2375 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2376 if (!test_bit(Journal, &rdev->flags) &&
2378 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
2380 /* Cannot change size, so fail
2381 * If mddev->level <= 0, then we don't care
2382 * about aligning sizes (e.g. linear)
2384 if (mddev->level > 0)
2387 mddev->dev_sectors = rdev->sectors;
2390 /* Verify rdev->desc_nr is unique.
2391 * If it is -1, assign a free number, else
2392 * check number is not in use
2395 if (rdev->desc_nr < 0) {
2398 choice = mddev->raid_disks;
2399 while (md_find_rdev_nr_rcu(mddev, choice))
2401 rdev->desc_nr = choice;
2403 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
2409 if (!test_bit(Journal, &rdev->flags) &&
2410 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2411 pr_warn("md: %s: array is limited to %d devices\n",
2412 mdname(mddev), mddev->max_disks);
2415 snprintf(b, sizeof(b), "%pg", rdev->bdev);
2416 strreplace(b, '/', '!');
2418 rdev->mddev = mddev;
2419 pr_debug("md: bind<%s>\n", b);
2421 if (mddev->raid_disks)
2422 mddev_create_serial_pool(mddev, rdev, false);
2424 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
2427 /* failure here is OK */
2428 err = sysfs_create_link(&rdev->kobj, bdev_kobj(rdev->bdev), "block");
2429 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
2430 rdev->sysfs_unack_badblocks =
2431 sysfs_get_dirent_safe(rdev->kobj.sd, "unacknowledged_bad_blocks");
2432 rdev->sysfs_badblocks =
2433 sysfs_get_dirent_safe(rdev->kobj.sd, "bad_blocks");
2435 list_add_rcu(&rdev->same_set, &mddev->disks);
2436 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
2438 /* May as well allow recovery to be retried once */
2439 mddev->recovery_disabled++;
2444 pr_warn("md: failed to register dev-%s for %s\n",
2449 static void rdev_delayed_delete(struct work_struct *ws)
2451 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
2452 kobject_del(&rdev->kobj);
2453 kobject_put(&rdev->kobj);
2456 static void unbind_rdev_from_array(struct md_rdev *rdev)
2458 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
2459 list_del_rcu(&rdev->same_set);
2460 pr_debug("md: unbind<%pg>\n", rdev->bdev);
2461 mddev_destroy_serial_pool(rdev->mddev, rdev, false);
2463 sysfs_remove_link(&rdev->kobj, "block");
2464 sysfs_put(rdev->sysfs_state);
2465 sysfs_put(rdev->sysfs_unack_badblocks);
2466 sysfs_put(rdev->sysfs_badblocks);
2467 rdev->sysfs_state = NULL;
2468 rdev->sysfs_unack_badblocks = NULL;
2469 rdev->sysfs_badblocks = NULL;
2470 rdev->badblocks.count = 0;
2471 /* We need to delay this, otherwise we can deadlock when
2472 * writing to 'remove' to "dev/state". We also need
2473 * to delay it due to rcu usage.
2476 INIT_WORK(&rdev->del_work, rdev_delayed_delete);
2477 kobject_get(&rdev->kobj);
2478 queue_work(md_rdev_misc_wq, &rdev->del_work);
2482 * prevent the device from being mounted, repartitioned or
2483 * otherwise reused by a RAID array (or any other kernel
2484 * subsystem), by bd_claiming the device.
2486 static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
2489 struct block_device *bdev;
2491 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
2492 shared ? (struct md_rdev *)lock_rdev : rdev);
2494 pr_warn("md: could not open device unknown-block(%u,%u).\n",
2495 MAJOR(dev), MINOR(dev));
2496 return PTR_ERR(bdev);
2502 static void unlock_rdev(struct md_rdev *rdev)
2504 struct block_device *bdev = rdev->bdev;
2506 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
2509 void md_autodetect_dev(dev_t dev);
2511 static void export_rdev(struct md_rdev *rdev)
2513 pr_debug("md: export_rdev(%pg)\n", rdev->bdev);
2514 md_rdev_clear(rdev);
2516 if (test_bit(AutoDetected, &rdev->flags))
2517 md_autodetect_dev(rdev->bdev->bd_dev);
2520 kobject_put(&rdev->kobj);
2523 void md_kick_rdev_from_array(struct md_rdev *rdev)
2525 unbind_rdev_from_array(rdev);
2528 EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
2530 static void export_array(struct mddev *mddev)
2532 struct md_rdev *rdev;
2534 while (!list_empty(&mddev->disks)) {
2535 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2537 md_kick_rdev_from_array(rdev);
2539 mddev->raid_disks = 0;
2540 mddev->major_version = 0;
2543 static bool set_in_sync(struct mddev *mddev)
2545 lockdep_assert_held(&mddev->lock);
2546 if (!mddev->in_sync) {
2547 mddev->sync_checkers++;
2548 spin_unlock(&mddev->lock);
2549 percpu_ref_switch_to_atomic_sync(&mddev->writes_pending);
2550 spin_lock(&mddev->lock);
2551 if (!mddev->in_sync &&
2552 percpu_ref_is_zero(&mddev->writes_pending)) {
2555 * Ensure ->in_sync is visible before we clear
2559 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2560 sysfs_notify_dirent_safe(mddev->sysfs_state);
2562 if (--mddev->sync_checkers == 0)
2563 percpu_ref_switch_to_percpu(&mddev->writes_pending);
2565 if (mddev->safemode == 1)
2566 mddev->safemode = 0;
2567 return mddev->in_sync;
2570 static void sync_sbs(struct mddev *mddev, int nospares)
2572 /* Update each superblock (in-memory image), but
2573 * if we are allowed to, skip spares which already
2574 * have the right event counter, or have one earlier
2575 * (which would mean they aren't being marked as dirty
2576 * with the rest of the array)
2578 struct md_rdev *rdev;
2579 rdev_for_each(rdev, mddev) {
2580 if (rdev->sb_events == mddev->events ||
2582 rdev->raid_disk < 0 &&
2583 rdev->sb_events+1 == mddev->events)) {
2584 /* Don't update this superblock */
2585 rdev->sb_loaded = 2;
2587 sync_super(mddev, rdev);
2588 rdev->sb_loaded = 1;
2593 static bool does_sb_need_changing(struct mddev *mddev)
2595 struct md_rdev *rdev = NULL, *iter;
2596 struct mdp_superblock_1 *sb;
2599 /* Find a good rdev */
2600 rdev_for_each(iter, mddev)
2601 if ((iter->raid_disk >= 0) && !test_bit(Faulty, &iter->flags)) {
2606 /* No good device found. */
2610 sb = page_address(rdev->sb_page);
2611 /* Check if a device has become faulty or a spare become active */
2612 rdev_for_each(rdev, mddev) {
2613 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2614 /* Device activated? */
2615 if (role == MD_DISK_ROLE_SPARE && rdev->raid_disk >= 0 &&
2616 !test_bit(Faulty, &rdev->flags))
2618 /* Device turned faulty? */
2619 if (test_bit(Faulty, &rdev->flags) && (role < MD_DISK_ROLE_MAX))
2623 /* Check if any mddev parameters have changed */
2624 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2625 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
2626 (mddev->layout != le32_to_cpu(sb->layout)) ||
2627 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2628 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2634 void md_update_sb(struct mddev *mddev, int force_change)
2636 struct md_rdev *rdev;
2639 int any_badblocks_changed = 0;
2644 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2649 if (mddev_is_clustered(mddev)) {
2650 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
2652 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
2654 ret = md_cluster_ops->metadata_update_start(mddev);
2655 /* Has someone else has updated the sb */
2656 if (!does_sb_need_changing(mddev)) {
2658 md_cluster_ops->metadata_update_cancel(mddev);
2659 bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2660 BIT(MD_SB_CHANGE_DEVS) |
2661 BIT(MD_SB_CHANGE_CLEAN));
2667 * First make sure individual recovery_offsets are correct
2668 * curr_resync_completed can only be used during recovery.
2669 * During reshape/resync it might use array-addresses rather
2670 * that device addresses.
2672 rdev_for_each(rdev, mddev) {
2673 if (rdev->raid_disk >= 0 &&
2674 mddev->delta_disks >= 0 &&
2675 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
2676 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery) &&
2677 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
2678 !test_bit(Journal, &rdev->flags) &&
2679 !test_bit(In_sync, &rdev->flags) &&
2680 mddev->curr_resync_completed > rdev->recovery_offset)
2681 rdev->recovery_offset = mddev->curr_resync_completed;
2684 if (!mddev->persistent) {
2685 clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2686 clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2687 if (!mddev->external) {
2688 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
2689 rdev_for_each(rdev, mddev) {
2690 if (rdev->badblocks.changed) {
2691 rdev->badblocks.changed = 0;
2692 ack_all_badblocks(&rdev->badblocks);
2693 md_error(mddev, rdev);
2695 clear_bit(Blocked, &rdev->flags);
2696 clear_bit(BlockedBadBlocks, &rdev->flags);
2697 wake_up(&rdev->blocked_wait);
2700 wake_up(&mddev->sb_wait);
2704 spin_lock(&mddev->lock);
2706 mddev->utime = ktime_get_real_seconds();
2708 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
2710 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
2711 /* just a clean<-> dirty transition, possibly leave spares alone,
2712 * though if events isn't the right even/odd, we will have to do
2718 if (mddev->degraded)
2719 /* If the array is degraded, then skipping spares is both
2720 * dangerous and fairly pointless.
2721 * Dangerous because a device that was removed from the array
2722 * might have a event_count that still looks up-to-date,
2723 * so it can be re-added without a resync.
2724 * Pointless because if there are any spares to skip,
2725 * then a recovery will happen and soon that array won't
2726 * be degraded any more and the spare can go back to sleep then.
2730 sync_req = mddev->in_sync;
2732 /* If this is just a dirty<->clean transition, and the array is clean
2733 * and 'events' is odd, we can roll back to the previous clean state */
2735 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
2736 && mddev->can_decrease_events
2737 && mddev->events != 1) {
2739 mddev->can_decrease_events = 0;
2741 /* otherwise we have to go forward and ... */
2743 mddev->can_decrease_events = nospares;
2747 * This 64-bit counter should never wrap.
2748 * Either we are in around ~1 trillion A.C., assuming
2749 * 1 reboot per second, or we have a bug...
2751 WARN_ON(mddev->events == 0);
2753 rdev_for_each(rdev, mddev) {
2754 if (rdev->badblocks.changed)
2755 any_badblocks_changed++;
2756 if (test_bit(Faulty, &rdev->flags))
2757 set_bit(FaultRecorded, &rdev->flags);
2760 sync_sbs(mddev, nospares);
2761 spin_unlock(&mddev->lock);
2763 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2764 mdname(mddev), mddev->in_sync);
2767 blk_add_trace_msg(mddev->queue, "md md_update_sb");
2769 md_bitmap_update_sb(mddev->bitmap);
2770 rdev_for_each(rdev, mddev) {
2771 if (rdev->sb_loaded != 1)
2772 continue; /* no noise on spare devices */
2774 if (!test_bit(Faulty, &rdev->flags)) {
2775 md_super_write(mddev,rdev,
2776 rdev->sb_start, rdev->sb_size,
2778 pr_debug("md: (write) %pg's sb offset: %llu\n",
2780 (unsigned long long)rdev->sb_start);
2781 rdev->sb_events = mddev->events;
2782 if (rdev->badblocks.size) {
2783 md_super_write(mddev, rdev,
2784 rdev->badblocks.sector,
2785 rdev->badblocks.size << 9,
2787 rdev->badblocks.size = 0;
2791 pr_debug("md: %pg (skipping faulty)\n",
2794 if (mddev->level == LEVEL_MULTIPATH)
2795 /* only need to write one superblock... */
2798 if (md_super_wait(mddev) < 0)
2800 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
2802 if (mddev_is_clustered(mddev) && ret == 0)
2803 md_cluster_ops->metadata_update_finish(mddev);
2805 if (mddev->in_sync != sync_req ||
2806 !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2807 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN)))
2808 /* have to write it out again */
2810 wake_up(&mddev->sb_wait);
2811 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2812 sysfs_notify_dirent_safe(mddev->sysfs_completed);
2814 rdev_for_each(rdev, mddev) {
2815 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2816 clear_bit(Blocked, &rdev->flags);
2818 if (any_badblocks_changed)
2819 ack_all_badblocks(&rdev->badblocks);
2820 clear_bit(BlockedBadBlocks, &rdev->flags);
2821 wake_up(&rdev->blocked_wait);
2824 EXPORT_SYMBOL(md_update_sb);
2826 static int add_bound_rdev(struct md_rdev *rdev)
2828 struct mddev *mddev = rdev->mddev;
2830 bool add_journal = test_bit(Journal, &rdev->flags);
2832 if (!mddev->pers->hot_remove_disk || add_journal) {
2833 /* If there is hot_add_disk but no hot_remove_disk
2834 * then added disks for geometry changes,
2835 * and should be added immediately.
2837 super_types[mddev->major_version].
2838 validate_super(mddev, rdev);
2840 mddev_suspend(mddev);
2841 err = mddev->pers->hot_add_disk(mddev, rdev);
2843 mddev_resume(mddev);
2845 md_kick_rdev_from_array(rdev);
2849 sysfs_notify_dirent_safe(rdev->sysfs_state);
2851 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2852 if (mddev->degraded)
2853 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2854 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2856 md_wakeup_thread(mddev->thread);
2860 /* words written to sysfs files may, or may not, be \n terminated.
2861 * We want to accept with case. For this we use cmd_match.
2863 static int cmd_match(const char *cmd, const char *str)
2865 /* See if cmd, written into a sysfs file, matches
2866 * str. They must either be the same, or cmd can
2867 * have a trailing newline
2869 while (*cmd && *str && *cmd == *str) {
2880 struct rdev_sysfs_entry {
2881 struct attribute attr;
2882 ssize_t (*show)(struct md_rdev *, char *);
2883 ssize_t (*store)(struct md_rdev *, const char *, size_t);
2887 state_show(struct md_rdev *rdev, char *page)
2891 unsigned long flags = READ_ONCE(rdev->flags);
2893 if (test_bit(Faulty, &flags) ||
2894 (!test_bit(ExternalBbl, &flags) &&
2895 rdev->badblocks.unacked_exist))
2896 len += sprintf(page+len, "faulty%s", sep);
2897 if (test_bit(In_sync, &flags))
2898 len += sprintf(page+len, "in_sync%s", sep);
2899 if (test_bit(Journal, &flags))
2900 len += sprintf(page+len, "journal%s", sep);
2901 if (test_bit(WriteMostly, &flags))
2902 len += sprintf(page+len, "write_mostly%s", sep);
2903 if (test_bit(Blocked, &flags) ||
2904 (rdev->badblocks.unacked_exist
2905 && !test_bit(Faulty, &flags)))
2906 len += sprintf(page+len, "blocked%s", sep);
2907 if (!test_bit(Faulty, &flags) &&
2908 !test_bit(Journal, &flags) &&
2909 !test_bit(In_sync, &flags))
2910 len += sprintf(page+len, "spare%s", sep);
2911 if (test_bit(WriteErrorSeen, &flags))
2912 len += sprintf(page+len, "write_error%s", sep);
2913 if (test_bit(WantReplacement, &flags))
2914 len += sprintf(page+len, "want_replacement%s", sep);
2915 if (test_bit(Replacement, &flags))
2916 len += sprintf(page+len, "replacement%s", sep);
2917 if (test_bit(ExternalBbl, &flags))
2918 len += sprintf(page+len, "external_bbl%s", sep);
2919 if (test_bit(FailFast, &flags))
2920 len += sprintf(page+len, "failfast%s", sep);
2925 return len+sprintf(page+len, "\n");
2929 state_store(struct md_rdev *rdev, const char *buf, size_t len)
2932 * faulty - simulates an error
2933 * remove - disconnects the device
2934 * writemostly - sets write_mostly
2935 * -writemostly - clears write_mostly
2936 * blocked - sets the Blocked flags
2937 * -blocked - clears the Blocked and possibly simulates an error
2938 * insync - sets Insync providing device isn't active
2939 * -insync - clear Insync for a device with a slot assigned,
2940 * so that it gets rebuilt based on bitmap
2941 * write_error - sets WriteErrorSeen
2942 * -write_error - clears WriteErrorSeen
2943 * {,-}failfast - set/clear FailFast
2946 struct mddev *mddev = rdev->mddev;
2948 bool need_update_sb = false;
2950 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2951 md_error(rdev->mddev, rdev);
2953 if (test_bit(MD_BROKEN, &rdev->mddev->flags))
2957 } else if (cmd_match(buf, "remove")) {
2958 if (rdev->mddev->pers) {
2959 clear_bit(Blocked, &rdev->flags);
2960 remove_and_add_spares(rdev->mddev, rdev);
2962 if (rdev->raid_disk >= 0)
2966 if (mddev_is_clustered(mddev))
2967 err = md_cluster_ops->remove_disk(mddev, rdev);
2970 md_kick_rdev_from_array(rdev);
2972 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2973 md_wakeup_thread(mddev->thread);
2978 } else if (cmd_match(buf, "writemostly")) {
2979 set_bit(WriteMostly, &rdev->flags);
2980 mddev_create_serial_pool(rdev->mddev, rdev, false);
2981 need_update_sb = true;
2983 } else if (cmd_match(buf, "-writemostly")) {
2984 mddev_destroy_serial_pool(rdev->mddev, rdev, false);
2985 clear_bit(WriteMostly, &rdev->flags);
2986 need_update_sb = true;
2988 } else if (cmd_match(buf, "blocked")) {
2989 set_bit(Blocked, &rdev->flags);
2991 } else if (cmd_match(buf, "-blocked")) {
2992 if (!test_bit(Faulty, &rdev->flags) &&
2993 !test_bit(ExternalBbl, &rdev->flags) &&
2994 rdev->badblocks.unacked_exist) {
2995 /* metadata handler doesn't understand badblocks,
2996 * so we need to fail the device
2998 md_error(rdev->mddev, rdev);
3000 clear_bit(Blocked, &rdev->flags);
3001 clear_bit(BlockedBadBlocks, &rdev->flags);
3002 wake_up(&rdev->blocked_wait);
3003 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
3004 md_wakeup_thread(rdev->mddev->thread);
3007 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
3008 set_bit(In_sync, &rdev->flags);
3010 } else if (cmd_match(buf, "failfast")) {
3011 set_bit(FailFast, &rdev->flags);
3012 need_update_sb = true;
3014 } else if (cmd_match(buf, "-failfast")) {
3015 clear_bit(FailFast, &rdev->flags);
3016 need_update_sb = true;
3018 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
3019 !test_bit(Journal, &rdev->flags)) {
3020 if (rdev->mddev->pers == NULL) {
3021 clear_bit(In_sync, &rdev->flags);
3022 rdev->saved_raid_disk = rdev->raid_disk;
3023 rdev->raid_disk = -1;
3026 } else if (cmd_match(buf, "write_error")) {
3027 set_bit(WriteErrorSeen, &rdev->flags);
3029 } else if (cmd_match(buf, "-write_error")) {
3030 clear_bit(WriteErrorSeen, &rdev->flags);
3032 } else if (cmd_match(buf, "want_replacement")) {
3033 /* Any non-spare device that is not a replacement can
3034 * become want_replacement at any time, but we then need to
3035 * check if recovery is needed.
3037 if (rdev->raid_disk >= 0 &&
3038 !test_bit(Journal, &rdev->flags) &&
3039 !test_bit(Replacement, &rdev->flags))
3040 set_bit(WantReplacement, &rdev->flags);
3041 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
3042 md_wakeup_thread(rdev->mddev->thread);
3044 } else if (cmd_match(buf, "-want_replacement")) {
3045 /* Clearing 'want_replacement' is always allowed.
3046 * Once replacements starts it is too late though.
3049 clear_bit(WantReplacement, &rdev->flags);
3050 } else if (cmd_match(buf, "replacement")) {
3051 /* Can only set a device as a replacement when array has not
3052 * yet been started. Once running, replacement is automatic
3053 * from spares, or by assigning 'slot'.
3055 if (rdev->mddev->pers)
3058 set_bit(Replacement, &rdev->flags);
3061 } else if (cmd_match(buf, "-replacement")) {
3062 /* Similarly, can only clear Replacement before start */
3063 if (rdev->mddev->pers)
3066 clear_bit(Replacement, &rdev->flags);
3069 } else if (cmd_match(buf, "re-add")) {
3070 if (!rdev->mddev->pers)
3072 else if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1) &&
3073 rdev->saved_raid_disk >= 0) {
3074 /* clear_bit is performed _after_ all the devices
3075 * have their local Faulty bit cleared. If any writes
3076 * happen in the meantime in the local node, they
3077 * will land in the local bitmap, which will be synced
3078 * by this node eventually
3080 if (!mddev_is_clustered(rdev->mddev) ||
3081 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
3082 clear_bit(Faulty, &rdev->flags);
3083 err = add_bound_rdev(rdev);
3087 } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) {
3088 set_bit(ExternalBbl, &rdev->flags);
3089 rdev->badblocks.shift = 0;
3091 } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) {
3092 clear_bit(ExternalBbl, &rdev->flags);
3096 md_update_sb(mddev, 1);
3098 sysfs_notify_dirent_safe(rdev->sysfs_state);
3099 return err ? err : len;
3101 static struct rdev_sysfs_entry rdev_state =
3102 __ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
3105 errors_show(struct md_rdev *rdev, char *page)
3107 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
3111 errors_store(struct md_rdev *rdev, const char *buf, size_t len)
3116 rv = kstrtouint(buf, 10, &n);
3119 atomic_set(&rdev->corrected_errors, n);
3122 static struct rdev_sysfs_entry rdev_errors =
3123 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
3126 slot_show(struct md_rdev *rdev, char *page)
3128 if (test_bit(Journal, &rdev->flags))
3129 return sprintf(page, "journal\n");
3130 else if (rdev->raid_disk < 0)
3131 return sprintf(page, "none\n");
3133 return sprintf(page, "%d\n", rdev->raid_disk);
3137 slot_store(struct md_rdev *rdev, const char *buf, size_t len)
3142 if (test_bit(Journal, &rdev->flags))
3144 if (strncmp(buf, "none", 4)==0)
3147 err = kstrtouint(buf, 10, (unsigned int *)&slot);
3151 if (rdev->mddev->pers && slot == -1) {
3152 /* Setting 'slot' on an active array requires also
3153 * updating the 'rd%d' link, and communicating
3154 * with the personality with ->hot_*_disk.
3155 * For now we only support removing
3156 * failed/spare devices. This normally happens automatically,
3157 * but not when the metadata is externally managed.
3159 if (rdev->raid_disk == -1)
3161 /* personality does all needed checks */
3162 if (rdev->mddev->pers->hot_remove_disk == NULL)
3164 clear_bit(Blocked, &rdev->flags);
3165 remove_and_add_spares(rdev->mddev, rdev);
3166 if (rdev->raid_disk >= 0)
3168 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
3169 md_wakeup_thread(rdev->mddev->thread);
3170 } else if (rdev->mddev->pers) {
3171 /* Activating a spare .. or possibly reactivating
3172 * if we ever get bitmaps working here.
3176 if (rdev->raid_disk != -1)
3179 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
3182 if (rdev->mddev->pers->hot_add_disk == NULL)
3185 if (slot >= rdev->mddev->raid_disks &&
3186 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
3189 rdev->raid_disk = slot;
3190 if (test_bit(In_sync, &rdev->flags))
3191 rdev->saved_raid_disk = slot;
3193 rdev->saved_raid_disk = -1;
3194 clear_bit(In_sync, &rdev->flags);
3195 clear_bit(Bitmap_sync, &rdev->flags);
3196 err = rdev->mddev->pers->hot_add_disk(rdev->mddev, rdev);
3198 rdev->raid_disk = -1;
3201 sysfs_notify_dirent_safe(rdev->sysfs_state);
3202 /* failure here is OK */;
3203 sysfs_link_rdev(rdev->mddev, rdev);
3204 /* don't wakeup anyone, leave that to userspace. */
3206 if (slot >= rdev->mddev->raid_disks &&
3207 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
3209 rdev->raid_disk = slot;
3210 /* assume it is working */
3211 clear_bit(Faulty, &rdev->flags);
3212 clear_bit(WriteMostly, &rdev->flags);
3213 set_bit(In_sync, &rdev->flags);
3214 sysfs_notify_dirent_safe(rdev->sysfs_state);
3219 static struct rdev_sysfs_entry rdev_slot =
3220 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
3223 offset_show(struct md_rdev *rdev, char *page)
3225 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
3229 offset_store(struct md_rdev *rdev, const char *buf, size_t len)
3231 unsigned long long offset;
3232 if (kstrtoull(buf, 10, &offset) < 0)
3234 if (rdev->mddev->pers && rdev->raid_disk >= 0)
3236 if (rdev->sectors && rdev->mddev->external)
3237 /* Must set offset before size, so overlap checks
3240 rdev->data_offset = offset;
3241 rdev->new_data_offset = offset;
3245 static struct rdev_sysfs_entry rdev_offset =
3246 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
3248 static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
3250 return sprintf(page, "%llu\n",
3251 (unsigned long long)rdev->new_data_offset);
3254 static ssize_t new_offset_store(struct md_rdev *rdev,
3255 const char *buf, size_t len)
3257 unsigned long long new_offset;
3258 struct mddev *mddev = rdev->mddev;
3260 if (kstrtoull(buf, 10, &new_offset) < 0)
3263 if (mddev->sync_thread ||
3264 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
3266 if (new_offset == rdev->data_offset)
3267 /* reset is always permitted */
3269 else if (new_offset > rdev->data_offset) {
3270 /* must not push array size beyond rdev_sectors */
3271 if (new_offset - rdev->data_offset
3272 + mddev->dev_sectors > rdev->sectors)
3275 /* Metadata worries about other space details. */
3277 /* decreasing the offset is inconsistent with a backwards
3280 if (new_offset < rdev->data_offset &&
3281 mddev->reshape_backwards)
3283 /* Increasing offset is inconsistent with forwards
3284 * reshape. reshape_direction should be set to
3285 * 'backwards' first.
3287 if (new_offset > rdev->data_offset &&
3288 !mddev->reshape_backwards)
3291 if (mddev->pers && mddev->persistent &&
3292 !super_types[mddev->major_version]
3293 .allow_new_offset(rdev, new_offset))
3295 rdev->new_data_offset = new_offset;
3296 if (new_offset > rdev->data_offset)
3297 mddev->reshape_backwards = 1;
3298 else if (new_offset < rdev->data_offset)
3299 mddev->reshape_backwards = 0;
3303 static struct rdev_sysfs_entry rdev_new_offset =
3304 __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
3307 rdev_size_show(struct md_rdev *rdev, char *page)
3309 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
3312 static int md_rdevs_overlap(struct md_rdev *a, struct md_rdev *b)
3314 /* check if two start/length pairs overlap */
3315 if (a->data_offset + a->sectors <= b->data_offset)
3317 if (b->data_offset + b->sectors <= a->data_offset)
3322 static bool md_rdev_overlaps(struct md_rdev *rdev)
3324 struct mddev *mddev;
3325 struct md_rdev *rdev2;
3327 spin_lock(&all_mddevs_lock);
3328 list_for_each_entry(mddev, &all_mddevs, all_mddevs) {
3329 if (test_bit(MD_DELETED, &mddev->flags))
3331 rdev_for_each(rdev2, mddev) {
3332 if (rdev != rdev2 && rdev->bdev == rdev2->bdev &&
3333 md_rdevs_overlap(rdev, rdev2)) {
3334 spin_unlock(&all_mddevs_lock);
3339 spin_unlock(&all_mddevs_lock);
3343 static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
3345 unsigned long long blocks;
3348 if (kstrtoull(buf, 10, &blocks) < 0)
3351 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
3352 return -EINVAL; /* sector conversion overflow */
3355 if (new != blocks * 2)
3356 return -EINVAL; /* unsigned long long to sector_t overflow */
3363 rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3365 struct mddev *my_mddev = rdev->mddev;
3366 sector_t oldsectors = rdev->sectors;
3369 if (test_bit(Journal, &rdev->flags))
3371 if (strict_blocks_to_sectors(buf, §ors) < 0)
3373 if (rdev->data_offset != rdev->new_data_offset)
3374 return -EINVAL; /* too confusing */
3375 if (my_mddev->pers && rdev->raid_disk >= 0) {
3376 if (my_mddev->persistent) {
3377 sectors = super_types[my_mddev->major_version].
3378 rdev_size_change(rdev, sectors);
3381 } else if (!sectors)
3382 sectors = bdev_nr_sectors(rdev->bdev) -
3384 if (!my_mddev->pers->resize)
3385 /* Cannot change size for RAID0 or Linear etc */
3388 if (sectors < my_mddev->dev_sectors)
3389 return -EINVAL; /* component must fit device */
3391 rdev->sectors = sectors;
3394 * Check that all other rdevs with the same bdev do not overlap. This
3395 * check does not provide a hard guarantee, it just helps avoid
3396 * dangerous mistakes.
3398 if (sectors > oldsectors && my_mddev->external &&
3399 md_rdev_overlaps(rdev)) {
3401 * Someone else could have slipped in a size change here, but
3402 * doing so is just silly. We put oldsectors back because we
3403 * know it is safe, and trust userspace not to race with itself.
3405 rdev->sectors = oldsectors;
3411 static struct rdev_sysfs_entry rdev_size =
3412 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
3414 static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
3416 unsigned long long recovery_start = rdev->recovery_offset;
3418 if (test_bit(In_sync, &rdev->flags) ||
3419 recovery_start == MaxSector)
3420 return sprintf(page, "none\n");
3422 return sprintf(page, "%llu\n", recovery_start);
3425 static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
3427 unsigned long long recovery_start;
3429 if (cmd_match(buf, "none"))
3430 recovery_start = MaxSector;
3431 else if (kstrtoull(buf, 10, &recovery_start))
3434 if (rdev->mddev->pers &&
3435 rdev->raid_disk >= 0)
3438 rdev->recovery_offset = recovery_start;
3439 if (recovery_start == MaxSector)
3440 set_bit(In_sync, &rdev->flags);
3442 clear_bit(In_sync, &rdev->flags);
3446 static struct rdev_sysfs_entry rdev_recovery_start =
3447 __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3449 /* sysfs access to bad-blocks list.
3450 * We present two files.
3451 * 'bad-blocks' lists sector numbers and lengths of ranges that
3452 * are recorded as bad. The list is truncated to fit within
3453 * the one-page limit of sysfs.
3454 * Writing "sector length" to this file adds an acknowledged
3456 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3457 * been acknowledged. Writing to this file adds bad blocks
3458 * without acknowledging them. This is largely for testing.
3460 static ssize_t bb_show(struct md_rdev *rdev, char *page)
3462 return badblocks_show(&rdev->badblocks, page, 0);
3464 static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
3466 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3467 /* Maybe that ack was all we needed */
3468 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3469 wake_up(&rdev->blocked_wait);
3472 static struct rdev_sysfs_entry rdev_bad_blocks =
3473 __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3475 static ssize_t ubb_show(struct md_rdev *rdev, char *page)
3477 return badblocks_show(&rdev->badblocks, page, 1);
3479 static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
3481 return badblocks_store(&rdev->badblocks, page, len, 1);
3483 static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3484 __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3487 ppl_sector_show(struct md_rdev *rdev, char *page)
3489 return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector);
3493 ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len)
3495 unsigned long long sector;
3497 if (kstrtoull(buf, 10, §or) < 0)
3499 if (sector != (sector_t)sector)
3502 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3503 rdev->raid_disk >= 0)
3506 if (rdev->mddev->persistent) {
3507 if (rdev->mddev->major_version == 0)
3509 if ((sector > rdev->sb_start &&
3510 sector - rdev->sb_start > S16_MAX) ||
3511 (sector < rdev->sb_start &&
3512 rdev->sb_start - sector > -S16_MIN))
3514 rdev->ppl.offset = sector - rdev->sb_start;
3515 } else if (!rdev->mddev->external) {
3518 rdev->ppl.sector = sector;
3522 static struct rdev_sysfs_entry rdev_ppl_sector =
3523 __ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store);
3526 ppl_size_show(struct md_rdev *rdev, char *page)
3528 return sprintf(page, "%u\n", rdev->ppl.size);
3532 ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3536 if (kstrtouint(buf, 10, &size) < 0)
3539 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3540 rdev->raid_disk >= 0)
3543 if (rdev->mddev->persistent) {
3544 if (rdev->mddev->major_version == 0)
3548 } else if (!rdev->mddev->external) {
3551 rdev->ppl.size = size;
3555 static struct rdev_sysfs_entry rdev_ppl_size =
3556 __ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store);
3558 static struct attribute *rdev_default_attrs[] = {
3563 &rdev_new_offset.attr,
3565 &rdev_recovery_start.attr,
3566 &rdev_bad_blocks.attr,
3567 &rdev_unack_bad_blocks.attr,
3568 &rdev_ppl_sector.attr,
3569 &rdev_ppl_size.attr,
3572 ATTRIBUTE_GROUPS(rdev_default);
3574 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3576 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
3577 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
3583 return entry->show(rdev, page);
3587 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3588 const char *page, size_t length)
3590 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
3591 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
3593 struct mddev *mddev = rdev->mddev;
3597 if (!capable(CAP_SYS_ADMIN))
3599 rv = mddev ? mddev_lock(mddev) : -ENODEV;
3601 if (rdev->mddev == NULL)
3604 rv = entry->store(rdev, page, length);
3605 mddev_unlock(mddev);
3610 static void rdev_free(struct kobject *ko)
3612 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
3615 static const struct sysfs_ops rdev_sysfs_ops = {
3616 .show = rdev_attr_show,
3617 .store = rdev_attr_store,
3619 static struct kobj_type rdev_ktype = {
3620 .release = rdev_free,
3621 .sysfs_ops = &rdev_sysfs_ops,
3622 .default_groups = rdev_default_groups,
3625 int md_rdev_init(struct md_rdev *rdev)
3628 rdev->saved_raid_disk = -1;
3629 rdev->raid_disk = -1;
3631 rdev->data_offset = 0;
3632 rdev->new_data_offset = 0;
3633 rdev->sb_events = 0;
3634 rdev->last_read_error = 0;
3635 rdev->sb_loaded = 0;
3636 rdev->bb_page = NULL;
3637 atomic_set(&rdev->nr_pending, 0);
3638 atomic_set(&rdev->read_errors, 0);
3639 atomic_set(&rdev->corrected_errors, 0);
3641 INIT_LIST_HEAD(&rdev->same_set);
3642 init_waitqueue_head(&rdev->blocked_wait);
3644 /* Add space to store bad block list.
3645 * This reserves the space even on arrays where it cannot
3646 * be used - I wonder if that matters
3648 return badblocks_init(&rdev->badblocks, 0);
3650 EXPORT_SYMBOL_GPL(md_rdev_init);
3652 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3654 * mark the device faulty if:
3656 * - the device is nonexistent (zero size)
3657 * - the device has no valid superblock
3659 * a faulty rdev _never_ has rdev->sb set.
3661 static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
3664 struct md_rdev *rdev;
3667 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
3669 return ERR_PTR(-ENOMEM);
3671 err = md_rdev_init(rdev);
3674 err = alloc_disk_sb(rdev);
3678 err = lock_rdev(rdev, newdev, super_format == -2);
3682 kobject_init(&rdev->kobj, &rdev_ktype);
3684 size = bdev_nr_bytes(rdev->bdev) >> BLOCK_SIZE_BITS;
3686 pr_warn("md: %pg has zero or unknown size, marking faulty!\n",
3692 if (super_format >= 0) {
3693 err = super_types[super_format].
3694 load_super(rdev, NULL, super_minor);
3695 if (err == -EINVAL) {
3696 pr_warn("md: %pg does not have a valid v%d.%d superblock, not importing!\n",
3698 super_format, super_minor);
3702 pr_warn("md: could not read %pg's sb, not importing!\n",
3713 md_rdev_clear(rdev);
3715 return ERR_PTR(err);
3719 * Check a full RAID array for plausibility
3722 static int analyze_sbs(struct mddev *mddev)
3725 struct md_rdev *rdev, *freshest, *tmp;
3728 rdev_for_each_safe(rdev, tmp, mddev)
3729 switch (super_types[mddev->major_version].
3730 load_super(rdev, freshest, mddev->minor_version)) {
3737 pr_warn("md: fatal superblock inconsistency in %pg -- removing from array\n",
3739 md_kick_rdev_from_array(rdev);
3742 /* Cannot find a valid fresh disk */
3744 pr_warn("md: cannot find a valid disk\n");
3748 super_types[mddev->major_version].
3749 validate_super(mddev, freshest);
3752 rdev_for_each_safe(rdev, tmp, mddev) {
3753 if (mddev->max_disks &&
3754 (rdev->desc_nr >= mddev->max_disks ||
3755 i > mddev->max_disks)) {
3756 pr_warn("md: %s: %pg: only %d devices permitted\n",
3757 mdname(mddev), rdev->bdev,
3759 md_kick_rdev_from_array(rdev);
3762 if (rdev != freshest) {
3763 if (super_types[mddev->major_version].
3764 validate_super(mddev, rdev)) {
3765 pr_warn("md: kicking non-fresh %pg from array!\n",
3767 md_kick_rdev_from_array(rdev);
3771 if (mddev->level == LEVEL_MULTIPATH) {
3772 rdev->desc_nr = i++;
3773 rdev->raid_disk = rdev->desc_nr;
3774 set_bit(In_sync, &rdev->flags);
3775 } else if (rdev->raid_disk >=
3776 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3777 !test_bit(Journal, &rdev->flags)) {
3778 rdev->raid_disk = -1;
3779 clear_bit(In_sync, &rdev->flags);
3786 /* Read a fixed-point number.
3787 * Numbers in sysfs attributes should be in "standard" units where
3788 * possible, so time should be in seconds.
3789 * However we internally use a a much smaller unit such as
3790 * milliseconds or jiffies.
3791 * This function takes a decimal number with a possible fractional
3792 * component, and produces an integer which is the result of
3793 * multiplying that number by 10^'scale'.
3794 * all without any floating-point arithmetic.
3796 int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3798 unsigned long result = 0;
3800 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3803 else if (decimals < scale) {
3806 result = result * 10 + value;
3818 *res = result * int_pow(10, scale - decimals);
3823 safe_delay_show(struct mddev *mddev, char *page)
3825 int msec = (mddev->safemode_delay*1000)/HZ;
3826 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3829 safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
3833 if (mddev_is_clustered(mddev)) {
3834 pr_warn("md: Safemode is disabled for clustered mode\n");
3838 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
3841 mddev->safemode_delay = 0;
3843 unsigned long old_delay = mddev->safemode_delay;
3844 unsigned long new_delay = (msec*HZ)/1000;
3848 mddev->safemode_delay = new_delay;
3849 if (new_delay < old_delay || old_delay == 0)
3850 mod_timer(&mddev->safemode_timer, jiffies+1);
3854 static struct md_sysfs_entry md_safe_delay =
3855 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
3858 level_show(struct mddev *mddev, char *page)
3860 struct md_personality *p;
3862 spin_lock(&mddev->lock);
3865 ret = sprintf(page, "%s\n", p->name);
3866 else if (mddev->clevel[0])
3867 ret = sprintf(page, "%s\n", mddev->clevel);
3868 else if (mddev->level != LEVEL_NONE)
3869 ret = sprintf(page, "%d\n", mddev->level);
3872 spin_unlock(&mddev->lock);
3877 level_store(struct mddev *mddev, const char *buf, size_t len)
3882 struct md_personality *pers, *oldpers;
3884 void *priv, *oldpriv;
3885 struct md_rdev *rdev;
3887 if (slen == 0 || slen >= sizeof(clevel))
3890 rv = mddev_lock(mddev);
3894 if (mddev->pers == NULL) {
3895 strncpy(mddev->clevel, buf, slen);
3896 if (mddev->clevel[slen-1] == '\n')
3898 mddev->clevel[slen] = 0;
3899 mddev->level = LEVEL_NONE;
3907 /* request to change the personality. Need to ensure:
3908 * - array is not engaged in resync/recovery/reshape
3909 * - old personality can be suspended
3910 * - new personality will access other array.
3914 if (mddev->sync_thread ||
3915 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3916 mddev->reshape_position != MaxSector ||
3917 mddev->sysfs_active)
3921 if (!mddev->pers->quiesce) {
3922 pr_warn("md: %s: %s does not support online personality change\n",
3923 mdname(mddev), mddev->pers->name);
3927 /* Now find the new personality */
3928 strncpy(clevel, buf, slen);
3929 if (clevel[slen-1] == '\n')
3932 if (kstrtol(clevel, 10, &level))
3935 if (request_module("md-%s", clevel) != 0)
3936 request_module("md-level-%s", clevel);
3937 spin_lock(&pers_lock);
3938 pers = find_pers(level, clevel);
3939 if (!pers || !try_module_get(pers->owner)) {
3940 spin_unlock(&pers_lock);
3941 pr_warn("md: personality %s not loaded\n", clevel);
3945 spin_unlock(&pers_lock);
3947 if (pers == mddev->pers) {
3948 /* Nothing to do! */
3949 module_put(pers->owner);
3953 if (!pers->takeover) {
3954 module_put(pers->owner);
3955 pr_warn("md: %s: %s does not support personality takeover\n",
3956 mdname(mddev), clevel);
3961 rdev_for_each(rdev, mddev)
3962 rdev->new_raid_disk = rdev->raid_disk;
3964 /* ->takeover must set new_* and/or delta_disks
3965 * if it succeeds, and may set them when it fails.
3967 priv = pers->takeover(mddev);
3969 mddev->new_level = mddev->level;
3970 mddev->new_layout = mddev->layout;
3971 mddev->new_chunk_sectors = mddev->chunk_sectors;
3972 mddev->raid_disks -= mddev->delta_disks;
3973 mddev->delta_disks = 0;
3974 mddev->reshape_backwards = 0;
3975 module_put(pers->owner);
3976 pr_warn("md: %s: %s would not accept array\n",
3977 mdname(mddev), clevel);
3982 /* Looks like we have a winner */
3983 mddev_suspend(mddev);
3984 mddev_detach(mddev);
3986 spin_lock(&mddev->lock);
3987 oldpers = mddev->pers;
3988 oldpriv = mddev->private;
3990 mddev->private = priv;
3991 strscpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3992 mddev->level = mddev->new_level;
3993 mddev->layout = mddev->new_layout;
3994 mddev->chunk_sectors = mddev->new_chunk_sectors;
3995 mddev->delta_disks = 0;
3996 mddev->reshape_backwards = 0;
3997 mddev->degraded = 0;
3998 spin_unlock(&mddev->lock);
4000 if (oldpers->sync_request == NULL &&
4002 /* We are converting from a no-redundancy array
4003 * to a redundancy array and metadata is managed
4004 * externally so we need to be sure that writes
4005 * won't block due to a need to transition
4007 * until external management is started.
4010 mddev->safemode_delay = 0;
4011 mddev->safemode = 0;
4014 oldpers->free(mddev, oldpriv);
4016 if (oldpers->sync_request == NULL &&
4017 pers->sync_request != NULL) {
4018 /* need to add the md_redundancy_group */
4019 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4020 pr_warn("md: cannot register extra attributes for %s\n",
4022 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
4023 mddev->sysfs_completed = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_completed");
4024 mddev->sysfs_degraded = sysfs_get_dirent_safe(mddev->kobj.sd, "degraded");
4026 if (oldpers->sync_request != NULL &&
4027 pers->sync_request == NULL) {
4028 /* need to remove the md_redundancy_group */
4029 if (mddev->to_remove == NULL)
4030 mddev->to_remove = &md_redundancy_group;
4033 module_put(oldpers->owner);
4035 rdev_for_each(rdev, mddev) {
4036 if (rdev->raid_disk < 0)
4038 if (rdev->new_raid_disk >= mddev->raid_disks)
4039 rdev->new_raid_disk = -1;
4040 if (rdev->new_raid_disk == rdev->raid_disk)
4042 sysfs_unlink_rdev(mddev, rdev);
4044 rdev_for_each(rdev, mddev) {
4045 if (rdev->raid_disk < 0)
4047 if (rdev->new_raid_disk == rdev->raid_disk)
4049 rdev->raid_disk = rdev->new_raid_disk;
4050 if (rdev->raid_disk < 0)
4051 clear_bit(In_sync, &rdev->flags);
4053 if (sysfs_link_rdev(mddev, rdev))
4054 pr_warn("md: cannot register rd%d for %s after level change\n",
4055 rdev->raid_disk, mdname(mddev));
4059 if (pers->sync_request == NULL) {
4060 /* this is now an array without redundancy, so
4061 * it must always be in_sync
4064 del_timer_sync(&mddev->safemode_timer);
4066 blk_set_stacking_limits(&mddev->queue->limits);
4068 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
4069 mddev_resume(mddev);
4071 md_update_sb(mddev, 1);
4072 sysfs_notify_dirent_safe(mddev->sysfs_level);
4076 mddev_unlock(mddev);
4080 static struct md_sysfs_entry md_level =
4081 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
4084 layout_show(struct mddev *mddev, char *page)
4086 /* just a number, not meaningful for all levels */
4087 if (mddev->reshape_position != MaxSector &&
4088 mddev->layout != mddev->new_layout)
4089 return sprintf(page, "%d (%d)\n",
4090 mddev->new_layout, mddev->layout);
4091 return sprintf(page, "%d\n", mddev->layout);
4095 layout_store(struct mddev *mddev, const char *buf, size_t len)
4100 err = kstrtouint(buf, 10, &n);
4103 err = mddev_lock(mddev);
4108 if (mddev->pers->check_reshape == NULL)
4113 mddev->new_layout = n;
4114 err = mddev->pers->check_reshape(mddev);
4116 mddev->new_layout = mddev->layout;
4119 mddev->new_layout = n;
4120 if (mddev->reshape_position == MaxSector)
4123 mddev_unlock(mddev);
4126 static struct md_sysfs_entry md_layout =
4127 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
4130 raid_disks_show(struct mddev *mddev, char *page)
4132 if (mddev->raid_disks == 0)
4134 if (mddev->reshape_position != MaxSector &&
4135 mddev->delta_disks != 0)
4136 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
4137 mddev->raid_disks - mddev->delta_disks);
4138 return sprintf(page, "%d\n", mddev->raid_disks);
4141 static int update_raid_disks(struct mddev *mddev, int raid_disks);
4144 raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
4149 err = kstrtouint(buf, 10, &n);
4153 err = mddev_lock(mddev);
4157 err = update_raid_disks(mddev, n);
4158 else if (mddev->reshape_position != MaxSector) {
4159 struct md_rdev *rdev;
4160 int olddisks = mddev->raid_disks - mddev->delta_disks;
4163 rdev_for_each(rdev, mddev) {
4165 rdev->data_offset < rdev->new_data_offset)
4168 rdev->data_offset > rdev->new_data_offset)
4172 mddev->delta_disks = n - olddisks;
4173 mddev->raid_disks = n;
4174 mddev->reshape_backwards = (mddev->delta_disks < 0);
4176 mddev->raid_disks = n;
4178 mddev_unlock(mddev);
4179 return err ? err : len;
4181 static struct md_sysfs_entry md_raid_disks =
4182 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
4185 uuid_show(struct mddev *mddev, char *page)
4187 return sprintf(page, "%pU\n", mddev->uuid);
4189 static struct md_sysfs_entry md_uuid =
4190 __ATTR(uuid, S_IRUGO, uuid_show, NULL);
4193 chunk_size_show(struct mddev *mddev, char *page)
4195 if (mddev->reshape_position != MaxSector &&
4196 mddev->chunk_sectors != mddev->new_chunk_sectors)
4197 return sprintf(page, "%d (%d)\n",
4198 mddev->new_chunk_sectors << 9,
4199 mddev->chunk_sectors << 9);
4200 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
4204 chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
4209 err = kstrtoul(buf, 10, &n);
4213 err = mddev_lock(mddev);
4217 if (mddev->pers->check_reshape == NULL)
4222 mddev->new_chunk_sectors = n >> 9;
4223 err = mddev->pers->check_reshape(mddev);
4225 mddev->new_chunk_sectors = mddev->chunk_sectors;
4228 mddev->new_chunk_sectors = n >> 9;
4229 if (mddev->reshape_position == MaxSector)
4230 mddev->chunk_sectors = n >> 9;
4232 mddev_unlock(mddev);
4235 static struct md_sysfs_entry md_chunk_size =
4236 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
4239 resync_start_show(struct mddev *mddev, char *page)
4241 if (mddev->recovery_cp == MaxSector)
4242 return sprintf(page, "none\n");
4243 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
4247 resync_start_store(struct mddev *mddev, const char *buf, size_t len)
4249 unsigned long long n;
4252 if (cmd_match(buf, "none"))
4255 err = kstrtoull(buf, 10, &n);
4258 if (n != (sector_t)n)
4262 err = mddev_lock(mddev);
4265 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4269 mddev->recovery_cp = n;
4271 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
4273 mddev_unlock(mddev);
4276 static struct md_sysfs_entry md_resync_start =
4277 __ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
4278 resync_start_show, resync_start_store);
4281 * The array state can be:
4284 * No devices, no size, no level
4285 * Equivalent to STOP_ARRAY ioctl
4287 * May have some settings, but array is not active
4288 * all IO results in error
4289 * When written, doesn't tear down array, but just stops it
4290 * suspended (not supported yet)
4291 * All IO requests will block. The array can be reconfigured.
4292 * Writing this, if accepted, will block until array is quiescent
4294 * no resync can happen. no superblocks get written.
4295 * write requests fail
4297 * like readonly, but behaves like 'clean' on a write request.
4299 * clean - no pending writes, but otherwise active.
4300 * When written to inactive array, starts without resync
4301 * If a write request arrives then
4302 * if metadata is known, mark 'dirty' and switch to 'active'.
4303 * if not known, block and switch to write-pending
4304 * If written to an active array that has pending writes, then fails.
4306 * fully active: IO and resync can be happening.
4307 * When written to inactive array, starts with resync
4310 * clean, but writes are blocked waiting for 'active' to be written.
4313 * like active, but no writes have been seen for a while (100msec).
4316 * Array is failed. It's useful because mounted-arrays aren't stopped
4317 * when array is failed, so this state will at least alert the user that
4318 * something is wrong.
4320 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
4321 write_pending, active_idle, broken, bad_word};
4322 static char *array_states[] = {
4323 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
4324 "write-pending", "active-idle", "broken", NULL };
4326 static int match_word(const char *word, char **list)
4329 for (n=0; list[n]; n++)
4330 if (cmd_match(word, list[n]))
4336 array_state_show(struct mddev *mddev, char *page)
4338 enum array_state st = inactive;
4340 if (mddev->pers && !test_bit(MD_NOT_READY, &mddev->flags)) {
4349 spin_lock(&mddev->lock);
4350 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
4352 else if (mddev->in_sync)
4354 else if (mddev->safemode)
4358 spin_unlock(&mddev->lock);
4361 if (test_bit(MD_BROKEN, &mddev->flags) && st == clean)
4364 if (list_empty(&mddev->disks) &&
4365 mddev->raid_disks == 0 &&
4366 mddev->dev_sectors == 0)
4371 return sprintf(page, "%s\n", array_states[st]);
4374 static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
4375 static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
4376 static int restart_array(struct mddev *mddev);
4379 array_state_store(struct mddev *mddev, const char *buf, size_t len)
4382 enum array_state st = match_word(buf, array_states);
4384 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
4385 /* don't take reconfig_mutex when toggling between
4388 spin_lock(&mddev->lock);
4390 restart_array(mddev);
4391 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
4392 md_wakeup_thread(mddev->thread);
4393 wake_up(&mddev->sb_wait);
4394 } else /* st == clean */ {
4395 restart_array(mddev);
4396 if (!set_in_sync(mddev))
4400 sysfs_notify_dirent_safe(mddev->sysfs_state);
4401 spin_unlock(&mddev->lock);
4404 err = mddev_lock(mddev);
4412 /* stopping an active array */
4413 err = do_md_stop(mddev, 0, NULL);
4416 /* stopping an active array */
4418 err = do_md_stop(mddev, 2, NULL);
4420 err = 0; /* already inactive */
4423 break; /* not supported yet */
4426 err = md_set_readonly(mddev, NULL);
4429 set_disk_ro(mddev->gendisk, 1);
4430 err = do_md_run(mddev);
4436 err = md_set_readonly(mddev, NULL);
4437 else if (mddev->ro == 1)
4438 err = restart_array(mddev);
4441 set_disk_ro(mddev->gendisk, 0);
4445 err = do_md_run(mddev);
4450 err = restart_array(mddev);
4453 spin_lock(&mddev->lock);
4454 if (!set_in_sync(mddev))
4456 spin_unlock(&mddev->lock);
4462 err = restart_array(mddev);
4465 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
4466 wake_up(&mddev->sb_wait);
4470 set_disk_ro(mddev->gendisk, 0);
4471 err = do_md_run(mddev);
4477 /* these cannot be set */
4482 if (mddev->hold_active == UNTIL_IOCTL)
4483 mddev->hold_active = 0;
4484 sysfs_notify_dirent_safe(mddev->sysfs_state);
4486 mddev_unlock(mddev);
4489 static struct md_sysfs_entry md_array_state =
4490 __ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
4493 max_corrected_read_errors_show(struct mddev *mddev, char *page) {
4494 return sprintf(page, "%d\n",
4495 atomic_read(&mddev->max_corr_read_errors));
4499 max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
4504 rv = kstrtouint(buf, 10, &n);
4507 atomic_set(&mddev->max_corr_read_errors, n);
4511 static struct md_sysfs_entry max_corr_read_errors =
4512 __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4513 max_corrected_read_errors_store);
4516 null_show(struct mddev *mddev, char *page)
4521 /* need to ensure rdev_delayed_delete() has completed */
4522 static void flush_rdev_wq(struct mddev *mddev)
4524 struct md_rdev *rdev;
4527 rdev_for_each_rcu(rdev, mddev)
4528 if (work_pending(&rdev->del_work)) {
4529 flush_workqueue(md_rdev_misc_wq);
4536 new_dev_store(struct mddev *mddev, const char *buf, size_t len)
4538 /* buf must be %d:%d\n? giving major and minor numbers */
4539 /* The new device is added to the array.
4540 * If the array has a persistent superblock, we read the
4541 * superblock to initialise info and check validity.
4542 * Otherwise, only checking done is that in bind_rdev_to_array,
4543 * which mainly checks size.
4546 int major = simple_strtoul(buf, &e, 10);
4549 struct md_rdev *rdev;
4552 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4554 minor = simple_strtoul(e+1, &e, 10);
4555 if (*e && *e != '\n')
4557 dev = MKDEV(major, minor);
4558 if (major != MAJOR(dev) ||
4559 minor != MINOR(dev))
4562 flush_rdev_wq(mddev);
4563 err = mddev_lock(mddev);
4566 if (mddev->persistent) {
4567 rdev = md_import_device(dev, mddev->major_version,
4568 mddev->minor_version);
4569 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
4570 struct md_rdev *rdev0
4571 = list_entry(mddev->disks.next,
4572 struct md_rdev, same_set);
4573 err = super_types[mddev->major_version]
4574 .load_super(rdev, rdev0, mddev->minor_version);
4578 } else if (mddev->external)
4579 rdev = md_import_device(dev, -2, -1);
4581 rdev = md_import_device(dev, -1, -1);
4584 mddev_unlock(mddev);
4585 return PTR_ERR(rdev);
4587 err = bind_rdev_to_array(rdev, mddev);
4591 mddev_unlock(mddev);
4594 return err ? err : len;
4597 static struct md_sysfs_entry md_new_device =
4598 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
4601 bitmap_store(struct mddev *mddev, const char *buf, size_t len)
4604 unsigned long chunk, end_chunk;
4607 err = mddev_lock(mddev);
4612 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4614 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4615 if (buf == end) break;
4616 if (*end == '-') { /* range */
4618 end_chunk = simple_strtoul(buf, &end, 0);
4619 if (buf == end) break;
4621 if (*end && !isspace(*end)) break;
4622 md_bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
4623 buf = skip_spaces(end);
4625 md_bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4627 mddev_unlock(mddev);
4631 static struct md_sysfs_entry md_bitmap =
4632 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4635 size_show(struct mddev *mddev, char *page)
4637 return sprintf(page, "%llu\n",
4638 (unsigned long long)mddev->dev_sectors / 2);
4641 static int update_size(struct mddev *mddev, sector_t num_sectors);
4644 size_store(struct mddev *mddev, const char *buf, size_t len)
4646 /* If array is inactive, we can reduce the component size, but
4647 * not increase it (except from 0).
4648 * If array is active, we can try an on-line resize
4651 int err = strict_blocks_to_sectors(buf, §ors);
4655 err = mddev_lock(mddev);
4659 err = update_size(mddev, sectors);
4661 md_update_sb(mddev, 1);
4663 if (mddev->dev_sectors == 0 ||
4664 mddev->dev_sectors > sectors)
4665 mddev->dev_sectors = sectors;
4669 mddev_unlock(mddev);
4670 return err ? err : len;
4673 static struct md_sysfs_entry md_size =
4674 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
4676 /* Metadata version.
4678 * 'none' for arrays with no metadata (good luck...)
4679 * 'external' for arrays with externally managed metadata,
4680 * or N.M for internally known formats
4683 metadata_show(struct mddev *mddev, char *page)
4685 if (mddev->persistent)
4686 return sprintf(page, "%d.%d\n",
4687 mddev->major_version, mddev->minor_version);
4688 else if (mddev->external)
4689 return sprintf(page, "external:%s\n", mddev->metadata_type);
4691 return sprintf(page, "none\n");
4695 metadata_store(struct mddev *mddev, const char *buf, size_t len)
4700 /* Changing the details of 'external' metadata is
4701 * always permitted. Otherwise there must be
4702 * no devices attached to the array.
4705 err = mddev_lock(mddev);
4709 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4711 else if (!list_empty(&mddev->disks))
4715 if (cmd_match(buf, "none")) {
4716 mddev->persistent = 0;
4717 mddev->external = 0;
4718 mddev->major_version = 0;
4719 mddev->minor_version = 90;
4722 if (strncmp(buf, "external:", 9) == 0) {
4723 size_t namelen = len-9;
4724 if (namelen >= sizeof(mddev->metadata_type))
4725 namelen = sizeof(mddev->metadata_type)-1;
4726 strncpy(mddev->metadata_type, buf+9, namelen);
4727 mddev->metadata_type[namelen] = 0;
4728 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4729 mddev->metadata_type[--namelen] = 0;
4730 mddev->persistent = 0;
4731 mddev->external = 1;
4732 mddev->major_version = 0;
4733 mddev->minor_version = 90;
4736 major = simple_strtoul(buf, &e, 10);
4738 if (e==buf || *e != '.')
4741 minor = simple_strtoul(buf, &e, 10);
4742 if (e==buf || (*e && *e != '\n') )
4745 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
4747 mddev->major_version = major;
4748 mddev->minor_version = minor;
4749 mddev->persistent = 1;
4750 mddev->external = 0;
4753 mddev_unlock(mddev);
4757 static struct md_sysfs_entry md_metadata =
4758 __ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
4761 action_show(struct mddev *mddev, char *page)
4763 char *type = "idle";
4764 unsigned long recovery = mddev->recovery;
4765 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
4767 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4768 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4769 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
4771 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4772 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
4774 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
4778 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
4780 else if (mddev->reshape_position != MaxSector)
4783 return sprintf(page, "%s\n", type);
4787 action_store(struct mddev *mddev, const char *page, size_t len)
4789 if (!mddev->pers || !mddev->pers->sync_request)
4793 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
4794 if (cmd_match(page, "frozen"))
4795 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4797 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4798 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4799 mddev_lock(mddev) == 0) {
4800 if (work_pending(&mddev->del_work))
4801 flush_workqueue(md_misc_wq);
4802 if (mddev->sync_thread) {
4803 sector_t save_rp = mddev->reshape_position;
4805 mddev_unlock(mddev);
4806 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4807 md_unregister_thread(&mddev->sync_thread);
4808 mddev_lock_nointr(mddev);
4810 * set RECOVERY_INTR again and restore reshape
4811 * position in case others changed them after
4812 * got lock, eg, reshape_position_store and
4813 * md_check_recovery.
4815 mddev->reshape_position = save_rp;
4816 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4817 md_reap_sync_thread(mddev);
4819 mddev_unlock(mddev);
4821 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4823 else if (cmd_match(page, "resync"))
4824 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4825 else if (cmd_match(page, "recover")) {
4826 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4827 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4828 } else if (cmd_match(page, "reshape")) {
4830 if (mddev->pers->start_reshape == NULL)
4832 err = mddev_lock(mddev);
4834 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4837 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4838 err = mddev->pers->start_reshape(mddev);
4840 mddev_unlock(mddev);
4844 sysfs_notify_dirent_safe(mddev->sysfs_degraded);
4846 if (cmd_match(page, "check"))
4847 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4848 else if (!cmd_match(page, "repair"))
4850 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4851 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4852 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4854 if (mddev->ro == 2) {
4855 /* A write to sync_action is enough to justify
4856 * canceling read-auto mode
4859 md_wakeup_thread(mddev->sync_thread);
4861 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4862 md_wakeup_thread(mddev->thread);
4863 sysfs_notify_dirent_safe(mddev->sysfs_action);
4867 static struct md_sysfs_entry md_scan_mode =
4868 __ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
4871 last_sync_action_show(struct mddev *mddev, char *page)
4873 return sprintf(page, "%s\n", mddev->last_sync_action);
4876 static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4879 mismatch_cnt_show(struct mddev *mddev, char *page)
4881 return sprintf(page, "%llu\n",
4882 (unsigned long long)
4883 atomic64_read(&mddev->resync_mismatches));
4886 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
4889 sync_min_show(struct mddev *mddev, char *page)
4891 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4892 mddev->sync_speed_min ? "local": "system");
4896 sync_min_store(struct mddev *mddev, const char *buf, size_t len)
4901 if (strncmp(buf, "system", 6)==0) {
4904 rv = kstrtouint(buf, 10, &min);
4910 mddev->sync_speed_min = min;
4914 static struct md_sysfs_entry md_sync_min =
4915 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4918 sync_max_show(struct mddev *mddev, char *page)
4920 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4921 mddev->sync_speed_max ? "local": "system");
4925 sync_max_store(struct mddev *mddev, const char *buf, size_t len)
4930 if (strncmp(buf, "system", 6)==0) {
4933 rv = kstrtouint(buf, 10, &max);
4939 mddev->sync_speed_max = max;
4943 static struct md_sysfs_entry md_sync_max =
4944 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4947 degraded_show(struct mddev *mddev, char *page)
4949 return sprintf(page, "%d\n", mddev->degraded);
4951 static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
4954 sync_force_parallel_show(struct mddev *mddev, char *page)
4956 return sprintf(page, "%d\n", mddev->parallel_resync);
4960 sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
4964 if (kstrtol(buf, 10, &n))
4967 if (n != 0 && n != 1)
4970 mddev->parallel_resync = n;
4972 if (mddev->sync_thread)
4973 wake_up(&resync_wait);
4978 /* force parallel resync, even with shared block devices */
4979 static struct md_sysfs_entry md_sync_force_parallel =
4980 __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4981 sync_force_parallel_show, sync_force_parallel_store);
4984 sync_speed_show(struct mddev *mddev, char *page)
4986 unsigned long resync, dt, db;
4987 if (mddev->curr_resync == MD_RESYNC_NONE)
4988 return sprintf(page, "none\n");
4989 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4990 dt = (jiffies - mddev->resync_mark) / HZ;
4992 db = resync - mddev->resync_mark_cnt;
4993 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
4996 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
4999 sync_completed_show(struct mddev *mddev, char *page)
5001 unsigned long long max_sectors, resync;
5003 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
5004 return sprintf(page, "none\n");
5006 if (mddev->curr_resync == MD_RESYNC_YIELDED ||
5007 mddev->curr_resync == MD_RESYNC_DELAYED)
5008 return sprintf(page, "delayed\n");
5010 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
5011 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5012 max_sectors = mddev->resync_max_sectors;
5014 max_sectors = mddev->dev_sectors;
5016 resync = mddev->curr_resync_completed;
5017 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
5020 static struct md_sysfs_entry md_sync_completed =
5021 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
5024 min_sync_show(struct mddev *mddev, char *page)
5026 return sprintf(page, "%llu\n",
5027 (unsigned long long)mddev->resync_min);
5030 min_sync_store(struct mddev *mddev, const char *buf, size_t len)
5032 unsigned long long min;
5035 if (kstrtoull(buf, 10, &min))
5038 spin_lock(&mddev->lock);
5040 if (min > mddev->resync_max)
5044 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
5047 /* Round down to multiple of 4K for safety */
5048 mddev->resync_min = round_down(min, 8);
5052 spin_unlock(&mddev->lock);
5056 static struct md_sysfs_entry md_min_sync =
5057 __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
5060 max_sync_show(struct mddev *mddev, char *page)
5062 if (mddev->resync_max == MaxSector)
5063 return sprintf(page, "max\n");
5065 return sprintf(page, "%llu\n",
5066 (unsigned long long)mddev->resync_max);
5069 max_sync_store(struct mddev *mddev, const char *buf, size_t len)
5072 spin_lock(&mddev->lock);
5073 if (strncmp(buf, "max", 3) == 0)
5074 mddev->resync_max = MaxSector;
5076 unsigned long long max;
5080 if (kstrtoull(buf, 10, &max))
5082 if (max < mddev->resync_min)
5086 if (max < mddev->resync_max &&
5088 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
5091 /* Must be a multiple of chunk_size */
5092 chunk = mddev->chunk_sectors;
5094 sector_t temp = max;
5097 if (sector_div(temp, chunk))
5100 mddev->resync_max = max;
5102 wake_up(&mddev->recovery_wait);
5105 spin_unlock(&mddev->lock);
5109 static struct md_sysfs_entry md_max_sync =
5110 __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
5113 suspend_lo_show(struct mddev *mddev, char *page)
5115 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
5119 suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
5121 unsigned long long new;
5124 err = kstrtoull(buf, 10, &new);
5127 if (new != (sector_t)new)
5130 err = mddev_lock(mddev);
5134 if (mddev->pers == NULL ||
5135 mddev->pers->quiesce == NULL)
5137 mddev_suspend(mddev);
5138 mddev->suspend_lo = new;
5139 mddev_resume(mddev);
5143 mddev_unlock(mddev);
5146 static struct md_sysfs_entry md_suspend_lo =
5147 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
5150 suspend_hi_show(struct mddev *mddev, char *page)
5152 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
5156 suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
5158 unsigned long long new;
5161 err = kstrtoull(buf, 10, &new);
5164 if (new != (sector_t)new)
5167 err = mddev_lock(mddev);
5171 if (mddev->pers == NULL)
5174 mddev_suspend(mddev);
5175 mddev->suspend_hi = new;
5176 mddev_resume(mddev);
5180 mddev_unlock(mddev);
5183 static struct md_sysfs_entry md_suspend_hi =
5184 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
5187 reshape_position_show(struct mddev *mddev, char *page)
5189 if (mddev->reshape_position != MaxSector)
5190 return sprintf(page, "%llu\n",
5191 (unsigned long long)mddev->reshape_position);
5192 strcpy(page, "none\n");
5197 reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
5199 struct md_rdev *rdev;
5200 unsigned long long new;
5203 err = kstrtoull(buf, 10, &new);
5206 if (new != (sector_t)new)
5208 err = mddev_lock(mddev);
5214 mddev->reshape_position = new;
5215 mddev->delta_disks = 0;
5216 mddev->reshape_backwards = 0;
5217 mddev->new_level = mddev->level;
5218 mddev->new_layout = mddev->layout;
5219 mddev->new_chunk_sectors = mddev->chunk_sectors;
5220 rdev_for_each(rdev, mddev)
5221 rdev->new_data_offset = rdev->data_offset;
5224 mddev_unlock(mddev);
5228 static struct md_sysfs_entry md_reshape_position =
5229 __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
5230 reshape_position_store);
5233 reshape_direction_show(struct mddev *mddev, char *page)
5235 return sprintf(page, "%s\n",
5236 mddev->reshape_backwards ? "backwards" : "forwards");
5240 reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
5245 if (cmd_match(buf, "forwards"))
5247 else if (cmd_match(buf, "backwards"))
5251 if (mddev->reshape_backwards == backwards)
5254 err = mddev_lock(mddev);
5257 /* check if we are allowed to change */
5258 if (mddev->delta_disks)
5260 else if (mddev->persistent &&
5261 mddev->major_version == 0)
5264 mddev->reshape_backwards = backwards;
5265 mddev_unlock(mddev);
5269 static struct md_sysfs_entry md_reshape_direction =
5270 __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
5271 reshape_direction_store);
5274 array_size_show(struct mddev *mddev, char *page)
5276 if (mddev->external_size)
5277 return sprintf(page, "%llu\n",
5278 (unsigned long long)mddev->array_sectors/2);
5280 return sprintf(page, "default\n");
5284 array_size_store(struct mddev *mddev, const char *buf, size_t len)
5289 err = mddev_lock(mddev);
5293 /* cluster raid doesn't support change array_sectors */
5294 if (mddev_is_clustered(mddev)) {
5295 mddev_unlock(mddev);
5299 if (strncmp(buf, "default", 7) == 0) {
5301 sectors = mddev->pers->size(mddev, 0, 0);
5303 sectors = mddev->array_sectors;
5305 mddev->external_size = 0;
5307 if (strict_blocks_to_sectors(buf, §ors) < 0)
5309 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
5312 mddev->external_size = 1;
5316 mddev->array_sectors = sectors;
5318 set_capacity_and_notify(mddev->gendisk,
5319 mddev->array_sectors);
5321 mddev_unlock(mddev);
5325 static struct md_sysfs_entry md_array_size =
5326 __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
5330 consistency_policy_show(struct mddev *mddev, char *page)
5334 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5335 ret = sprintf(page, "journal\n");
5336 } else if (test_bit(MD_HAS_PPL, &mddev->flags)) {
5337 ret = sprintf(page, "ppl\n");
5338 } else if (mddev->bitmap) {
5339 ret = sprintf(page, "bitmap\n");
5340 } else if (mddev->pers) {
5341 if (mddev->pers->sync_request)
5342 ret = sprintf(page, "resync\n");
5344 ret = sprintf(page, "none\n");
5346 ret = sprintf(page, "unknown\n");
5353 consistency_policy_store(struct mddev *mddev, const char *buf, size_t len)
5358 if (mddev->pers->change_consistency_policy)
5359 err = mddev->pers->change_consistency_policy(mddev, buf);
5362 } else if (mddev->external && strncmp(buf, "ppl", 3) == 0) {
5363 set_bit(MD_HAS_PPL, &mddev->flags);
5368 return err ? err : len;
5371 static struct md_sysfs_entry md_consistency_policy =
5372 __ATTR(consistency_policy, S_IRUGO | S_IWUSR, consistency_policy_show,
5373 consistency_policy_store);
5375 static ssize_t fail_last_dev_show(struct mddev *mddev, char *page)
5377 return sprintf(page, "%d\n", mddev->fail_last_dev);
5381 * Setting fail_last_dev to true to allow last device to be forcibly removed
5382 * from RAID1/RAID10.
5385 fail_last_dev_store(struct mddev *mddev, const char *buf, size_t len)
5390 ret = kstrtobool(buf, &value);
5394 if (value != mddev->fail_last_dev)
5395 mddev->fail_last_dev = value;
5399 static struct md_sysfs_entry md_fail_last_dev =
5400 __ATTR(fail_last_dev, S_IRUGO | S_IWUSR, fail_last_dev_show,
5401 fail_last_dev_store);
5403 static ssize_t serialize_policy_show(struct mddev *mddev, char *page)
5405 if (mddev->pers == NULL || (mddev->pers->level != 1))
5406 return sprintf(page, "n/a\n");
5408 return sprintf(page, "%d\n", mddev->serialize_policy);
5412 * Setting serialize_policy to true to enforce write IO is not reordered
5416 serialize_policy_store(struct mddev *mddev, const char *buf, size_t len)
5421 err = kstrtobool(buf, &value);
5425 if (value == mddev->serialize_policy)
5428 err = mddev_lock(mddev);
5431 if (mddev->pers == NULL || (mddev->pers->level != 1)) {
5432 pr_err("md: serialize_policy is only effective for raid1\n");
5437 mddev_suspend(mddev);
5439 mddev_create_serial_pool(mddev, NULL, true);
5441 mddev_destroy_serial_pool(mddev, NULL, true);
5442 mddev->serialize_policy = value;
5443 mddev_resume(mddev);
5445 mddev_unlock(mddev);
5449 static struct md_sysfs_entry md_serialize_policy =
5450 __ATTR(serialize_policy, S_IRUGO | S_IWUSR, serialize_policy_show,
5451 serialize_policy_store);
5454 static struct attribute *md_default_attrs[] = {
5457 &md_raid_disks.attr,
5459 &md_chunk_size.attr,
5461 &md_resync_start.attr,
5463 &md_new_device.attr,
5464 &md_safe_delay.attr,
5465 &md_array_state.attr,
5466 &md_reshape_position.attr,
5467 &md_reshape_direction.attr,
5468 &md_array_size.attr,
5469 &max_corr_read_errors.attr,
5470 &md_consistency_policy.attr,
5471 &md_fail_last_dev.attr,
5472 &md_serialize_policy.attr,
5476 static const struct attribute_group md_default_group = {
5477 .attrs = md_default_attrs,
5480 static struct attribute *md_redundancy_attrs[] = {
5482 &md_last_scan_mode.attr,
5483 &md_mismatches.attr,
5486 &md_sync_speed.attr,
5487 &md_sync_force_parallel.attr,
5488 &md_sync_completed.attr,
5491 &md_suspend_lo.attr,
5492 &md_suspend_hi.attr,
5497 static const struct attribute_group md_redundancy_group = {
5499 .attrs = md_redundancy_attrs,
5502 static const struct attribute_group *md_attr_groups[] = {
5509 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
5511 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
5512 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
5517 spin_lock(&all_mddevs_lock);
5518 if (!mddev_get(mddev)) {
5519 spin_unlock(&all_mddevs_lock);
5522 spin_unlock(&all_mddevs_lock);
5524 rv = entry->show(mddev, page);
5530 md_attr_store(struct kobject *kobj, struct attribute *attr,
5531 const char *page, size_t length)
5533 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
5534 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
5539 if (!capable(CAP_SYS_ADMIN))
5541 spin_lock(&all_mddevs_lock);
5542 if (!mddev_get(mddev)) {
5543 spin_unlock(&all_mddevs_lock);
5546 spin_unlock(&all_mddevs_lock);
5547 rv = entry->store(mddev, page, length);
5552 static void md_kobj_release(struct kobject *ko)
5554 struct mddev *mddev = container_of(ko, struct mddev, kobj);
5556 if (mddev->sysfs_state)
5557 sysfs_put(mddev->sysfs_state);
5558 if (mddev->sysfs_level)
5559 sysfs_put(mddev->sysfs_level);
5561 del_gendisk(mddev->gendisk);
5562 put_disk(mddev->gendisk);
5565 static const struct sysfs_ops md_sysfs_ops = {
5566 .show = md_attr_show,
5567 .store = md_attr_store,
5569 static struct kobj_type md_ktype = {
5570 .release = md_kobj_release,
5571 .sysfs_ops = &md_sysfs_ops,
5572 .default_groups = md_attr_groups,
5577 static void mddev_delayed_delete(struct work_struct *ws)
5579 struct mddev *mddev = container_of(ws, struct mddev, del_work);
5581 kobject_put(&mddev->kobj);
5584 static void no_op(struct percpu_ref *r) {}
5586 int mddev_init_writes_pending(struct mddev *mddev)
5588 if (mddev->writes_pending.percpu_count_ptr)
5590 if (percpu_ref_init(&mddev->writes_pending, no_op,
5591 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL) < 0)
5593 /* We want to start with the refcount at zero */
5594 percpu_ref_put(&mddev->writes_pending);
5597 EXPORT_SYMBOL_GPL(mddev_init_writes_pending);
5599 struct mddev *md_alloc(dev_t dev, char *name)
5602 * If dev is zero, name is the name of a device to allocate with
5603 * an arbitrary minor number. It will be "md_???"
5604 * If dev is non-zero it must be a device number with a MAJOR of
5605 * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then
5606 * the device is being created by opening a node in /dev.
5607 * If "name" is not NULL, the device is being created by
5608 * writing to /sys/module/md_mod/parameters/new_array.
5610 static DEFINE_MUTEX(disks_mutex);
5611 struct mddev *mddev;
5612 struct gendisk *disk;
5619 * Wait for any previous instance of this device to be completely
5620 * removed (mddev_delayed_delete).
5622 flush_workqueue(md_misc_wq);
5623 flush_workqueue(md_rdev_misc_wq);
5625 mutex_lock(&disks_mutex);
5626 mddev = mddev_alloc(dev);
5627 if (IS_ERR(mddev)) {
5628 error = PTR_ERR(mddev);
5632 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
5633 shift = partitioned ? MdpMinorShift : 0;
5634 unit = MINOR(mddev->unit) >> shift;
5637 /* Need to ensure that 'name' is not a duplicate.
5639 struct mddev *mddev2;
5640 spin_lock(&all_mddevs_lock);
5642 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5643 if (mddev2->gendisk &&
5644 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5645 spin_unlock(&all_mddevs_lock);
5647 goto out_free_mddev;
5649 spin_unlock(&all_mddevs_lock);
5653 * Creating /dev/mdNNN via "newarray", so adjust hold_active.
5655 mddev->hold_active = UNTIL_STOP;
5658 disk = blk_alloc_disk(NUMA_NO_NODE);
5660 goto out_free_mddev;
5662 disk->major = MAJOR(mddev->unit);
5663 disk->first_minor = unit << shift;
5664 disk->minors = 1 << shift;
5666 strcpy(disk->disk_name, name);
5667 else if (partitioned)
5668 sprintf(disk->disk_name, "md_d%d", unit);
5670 sprintf(disk->disk_name, "md%d", unit);
5671 disk->fops = &md_fops;
5672 disk->private_data = mddev;
5674 mddev->queue = disk->queue;
5675 blk_set_stacking_limits(&mddev->queue->limits);
5676 blk_queue_write_cache(mddev->queue, true, true);
5677 disk->events |= DISK_EVENT_MEDIA_CHANGE;
5678 mddev->gendisk = disk;
5679 error = add_disk(disk);
5683 kobject_init(&mddev->kobj, &md_ktype);
5684 error = kobject_add(&mddev->kobj, &disk_to_dev(disk)->kobj, "%s", "md");
5687 * The disk is already live at this point. Clear the hold flag
5688 * and let mddev_put take care of the deletion, as it isn't any
5689 * different from a normal close on last release now.
5691 mddev->hold_active = 0;
5692 mutex_unlock(&disks_mutex);
5694 return ERR_PTR(error);
5697 kobject_uevent(&mddev->kobj, KOBJ_ADD);
5698 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
5699 mddev->sysfs_level = sysfs_get_dirent_safe(mddev->kobj.sd, "level");
5700 mutex_unlock(&disks_mutex);
5708 mutex_unlock(&disks_mutex);
5709 return ERR_PTR(error);
5712 static int md_alloc_and_put(dev_t dev, char *name)
5714 struct mddev *mddev = md_alloc(dev, name);
5717 return PTR_ERR(mddev);
5722 static void md_probe(dev_t dev)
5724 if (MAJOR(dev) == MD_MAJOR && MINOR(dev) >= 512)
5727 md_alloc_and_put(dev, NULL);
5730 static int add_named_array(const char *val, const struct kernel_param *kp)
5733 * val must be "md_*" or "mdNNN".
5734 * For "md_*" we allocate an array with a large free minor number, and
5735 * set the name to val. val must not already be an active name.
5736 * For "mdNNN" we allocate an array with the minor number NNN
5737 * which must not already be in use.
5739 int len = strlen(val);
5740 char buf[DISK_NAME_LEN];
5741 unsigned long devnum;
5743 while (len && val[len-1] == '\n')
5745 if (len >= DISK_NAME_LEN)
5747 strscpy(buf, val, len+1);
5748 if (strncmp(buf, "md_", 3) == 0)
5749 return md_alloc_and_put(0, buf);
5750 if (strncmp(buf, "md", 2) == 0 &&
5752 kstrtoul(buf+2, 10, &devnum) == 0 &&
5753 devnum <= MINORMASK)
5754 return md_alloc_and_put(MKDEV(MD_MAJOR, devnum), NULL);
5759 static void md_safemode_timeout(struct timer_list *t)
5761 struct mddev *mddev = from_timer(mddev, t, safemode_timer);
5763 mddev->safemode = 1;
5764 if (mddev->external)
5765 sysfs_notify_dirent_safe(mddev->sysfs_state);
5767 md_wakeup_thread(mddev->thread);
5770 static int start_dirty_degraded;
5772 int md_run(struct mddev *mddev)
5775 struct md_rdev *rdev;
5776 struct md_personality *pers;
5779 if (list_empty(&mddev->disks))
5780 /* cannot run an array with no devices.. */
5785 /* Cannot run until previous stop completes properly */
5786 if (mddev->sysfs_active)
5790 * Analyze all RAID superblock(s)
5792 if (!mddev->raid_disks) {
5793 if (!mddev->persistent)
5795 err = analyze_sbs(mddev);
5800 if (mddev->level != LEVEL_NONE)
5801 request_module("md-level-%d", mddev->level);
5802 else if (mddev->clevel[0])
5803 request_module("md-%s", mddev->clevel);
5806 * Drop all container device buffers, from now on
5807 * the only valid external interface is through the md
5810 mddev->has_superblocks = false;
5811 rdev_for_each(rdev, mddev) {
5812 if (test_bit(Faulty, &rdev->flags))
5814 sync_blockdev(rdev->bdev);
5815 invalidate_bdev(rdev->bdev);
5816 if (mddev->ro != 1 && rdev_read_only(rdev)) {
5819 set_disk_ro(mddev->gendisk, 1);
5823 mddev->has_superblocks = true;
5825 /* perform some consistency tests on the device.
5826 * We don't want the data to overlap the metadata,
5827 * Internal Bitmap issues have been handled elsewhere.
5829 if (rdev->meta_bdev) {
5830 /* Nothing to check */;
5831 } else if (rdev->data_offset < rdev->sb_start) {
5832 if (mddev->dev_sectors &&
5833 rdev->data_offset + mddev->dev_sectors
5835 pr_warn("md: %s: data overlaps metadata\n",
5840 if (rdev->sb_start + rdev->sb_size/512
5841 > rdev->data_offset) {
5842 pr_warn("md: %s: metadata overlaps data\n",
5847 sysfs_notify_dirent_safe(rdev->sysfs_state);
5848 nowait = nowait && bdev_nowait(rdev->bdev);
5851 if (!bioset_initialized(&mddev->bio_set)) {
5852 err = bioset_init(&mddev->bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
5856 if (!bioset_initialized(&mddev->sync_set)) {
5857 err = bioset_init(&mddev->sync_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
5862 spin_lock(&pers_lock);
5863 pers = find_pers(mddev->level, mddev->clevel);
5864 if (!pers || !try_module_get(pers->owner)) {
5865 spin_unlock(&pers_lock);
5866 if (mddev->level != LEVEL_NONE)
5867 pr_warn("md: personality for level %d is not loaded!\n",
5870 pr_warn("md: personality for level %s is not loaded!\n",
5875 spin_unlock(&pers_lock);
5876 if (mddev->level != pers->level) {
5877 mddev->level = pers->level;
5878 mddev->new_level = pers->level;
5880 strscpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
5882 if (mddev->reshape_position != MaxSector &&
5883 pers->start_reshape == NULL) {
5884 /* This personality cannot handle reshaping... */
5885 module_put(pers->owner);
5890 if (pers->sync_request) {
5891 /* Warn if this is a potentially silly
5894 struct md_rdev *rdev2;
5897 rdev_for_each(rdev, mddev)
5898 rdev_for_each(rdev2, mddev) {
5900 rdev->bdev->bd_disk ==
5901 rdev2->bdev->bd_disk) {
5902 pr_warn("%s: WARNING: %pg appears to be on the same physical disk as %pg.\n",
5911 pr_warn("True protection against single-disk failure might be compromised.\n");
5914 mddev->recovery = 0;
5915 /* may be over-ridden by personality */
5916 mddev->resync_max_sectors = mddev->dev_sectors;
5918 mddev->ok_start_degraded = start_dirty_degraded;
5920 if (start_readonly && mddev->ro == 0)
5921 mddev->ro = 2; /* read-only, but switch on first write */
5923 err = pers->run(mddev);
5925 pr_warn("md: pers->run() failed ...\n");
5926 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
5927 WARN_ONCE(!mddev->external_size,
5928 "%s: default size too small, but 'external_size' not in effect?\n",
5930 pr_warn("md: invalid array_size %llu > default size %llu\n",
5931 (unsigned long long)mddev->array_sectors / 2,
5932 (unsigned long long)pers->size(mddev, 0, 0) / 2);
5935 if (err == 0 && pers->sync_request &&
5936 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
5937 struct bitmap *bitmap;
5939 bitmap = md_bitmap_create(mddev, -1);
5940 if (IS_ERR(bitmap)) {
5941 err = PTR_ERR(bitmap);
5942 pr_warn("%s: failed to create bitmap (%d)\n",
5943 mdname(mddev), err);
5945 mddev->bitmap = bitmap;
5951 if (mddev->bitmap_info.max_write_behind > 0) {
5952 bool create_pool = false;
5954 rdev_for_each(rdev, mddev) {
5955 if (test_bit(WriteMostly, &rdev->flags) &&
5956 rdev_init_serial(rdev))
5959 if (create_pool && mddev->serial_info_pool == NULL) {
5960 mddev->serial_info_pool =
5961 mempool_create_kmalloc_pool(NR_SERIAL_INFOS,
5962 sizeof(struct serial_info));
5963 if (!mddev->serial_info_pool) {
5973 rdev_for_each(rdev, mddev) {
5974 if (rdev->raid_disk >= 0 && !bdev_nonrot(rdev->bdev)) {
5979 if (mddev->degraded)
5982 blk_queue_flag_set(QUEUE_FLAG_NONROT, mddev->queue);
5984 blk_queue_flag_clear(QUEUE_FLAG_NONROT, mddev->queue);
5985 blk_queue_flag_set(QUEUE_FLAG_IO_STAT, mddev->queue);
5987 /* Set the NOWAIT flags if all underlying devices support it */
5989 blk_queue_flag_set(QUEUE_FLAG_NOWAIT, mddev->queue);
5991 if (pers->sync_request) {
5992 if (mddev->kobj.sd &&
5993 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
5994 pr_warn("md: cannot register extra attributes for %s\n",
5996 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
5997 mddev->sysfs_completed = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_completed");
5998 mddev->sysfs_degraded = sysfs_get_dirent_safe(mddev->kobj.sd, "degraded");
5999 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
6002 atomic_set(&mddev->max_corr_read_errors,
6003 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
6004 mddev->safemode = 0;
6005 if (mddev_is_clustered(mddev))
6006 mddev->safemode_delay = 0;
6008 mddev->safemode_delay = DEFAULT_SAFEMODE_DELAY;
6011 spin_lock(&mddev->lock);
6013 spin_unlock(&mddev->lock);
6014 rdev_for_each(rdev, mddev)
6015 if (rdev->raid_disk >= 0)
6016 sysfs_link_rdev(mddev, rdev); /* failure here is OK */
6018 if (mddev->degraded && !mddev->ro)
6019 /* This ensures that recovering status is reported immediately
6020 * via sysfs - until a lack of spares is confirmed.
6022 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6023 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6025 if (mddev->sb_flags)
6026 md_update_sb(mddev, 0);
6032 mddev_detach(mddev);
6034 pers->free(mddev, mddev->private);
6035 mddev->private = NULL;
6036 module_put(pers->owner);
6037 md_bitmap_destroy(mddev);
6039 bioset_exit(&mddev->sync_set);
6041 bioset_exit(&mddev->bio_set);
6044 EXPORT_SYMBOL_GPL(md_run);
6046 int do_md_run(struct mddev *mddev)
6050 set_bit(MD_NOT_READY, &mddev->flags);
6051 err = md_run(mddev);
6054 err = md_bitmap_load(mddev);
6056 md_bitmap_destroy(mddev);
6060 if (mddev_is_clustered(mddev))
6061 md_allow_write(mddev);
6063 /* run start up tasks that require md_thread */
6066 md_wakeup_thread(mddev->thread);
6067 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
6069 set_capacity_and_notify(mddev->gendisk, mddev->array_sectors);
6070 clear_bit(MD_NOT_READY, &mddev->flags);
6072 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
6073 sysfs_notify_dirent_safe(mddev->sysfs_state);
6074 sysfs_notify_dirent_safe(mddev->sysfs_action);
6075 sysfs_notify_dirent_safe(mddev->sysfs_degraded);
6077 clear_bit(MD_NOT_READY, &mddev->flags);
6081 int md_start(struct mddev *mddev)
6085 if (mddev->pers->start) {
6086 set_bit(MD_RECOVERY_WAIT, &mddev->recovery);
6087 md_wakeup_thread(mddev->thread);
6088 ret = mddev->pers->start(mddev);
6089 clear_bit(MD_RECOVERY_WAIT, &mddev->recovery);
6090 md_wakeup_thread(mddev->sync_thread);
6094 EXPORT_SYMBOL_GPL(md_start);
6096 static int restart_array(struct mddev *mddev)
6098 struct gendisk *disk = mddev->gendisk;
6099 struct md_rdev *rdev;
6100 bool has_journal = false;
6101 bool has_readonly = false;
6103 /* Complain if it has no devices */
6104 if (list_empty(&mddev->disks))
6112 rdev_for_each_rcu(rdev, mddev) {
6113 if (test_bit(Journal, &rdev->flags) &&
6114 !test_bit(Faulty, &rdev->flags))
6116 if (rdev_read_only(rdev))
6117 has_readonly = true;
6120 if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && !has_journal)
6121 /* Don't restart rw with journal missing/faulty */
6126 mddev->safemode = 0;
6128 set_disk_ro(disk, 0);
6129 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev));
6130 /* Kick recovery or resync if necessary */
6131 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6132 md_wakeup_thread(mddev->thread);
6133 md_wakeup_thread(mddev->sync_thread);
6134 sysfs_notify_dirent_safe(mddev->sysfs_state);
6138 static void md_clean(struct mddev *mddev)
6140 mddev->array_sectors = 0;
6141 mddev->external_size = 0;
6142 mddev->dev_sectors = 0;
6143 mddev->raid_disks = 0;
6144 mddev->recovery_cp = 0;
6145 mddev->resync_min = 0;
6146 mddev->resync_max = MaxSector;
6147 mddev->reshape_position = MaxSector;
6148 mddev->external = 0;
6149 mddev->persistent = 0;
6150 mddev->level = LEVEL_NONE;
6151 mddev->clevel[0] = 0;
6153 mddev->sb_flags = 0;
6155 mddev->metadata_type[0] = 0;
6156 mddev->chunk_sectors = 0;
6157 mddev->ctime = mddev->utime = 0;
6159 mddev->max_disks = 0;
6161 mddev->can_decrease_events = 0;
6162 mddev->delta_disks = 0;
6163 mddev->reshape_backwards = 0;
6164 mddev->new_level = LEVEL_NONE;
6165 mddev->new_layout = 0;
6166 mddev->new_chunk_sectors = 0;
6167 mddev->curr_resync = 0;
6168 atomic64_set(&mddev->resync_mismatches, 0);
6169 mddev->suspend_lo = mddev->suspend_hi = 0;
6170 mddev->sync_speed_min = mddev->sync_speed_max = 0;
6171 mddev->recovery = 0;
6174 mddev->degraded = 0;
6175 mddev->safemode = 0;
6176 mddev->private = NULL;
6177 mddev->cluster_info = NULL;
6178 mddev->bitmap_info.offset = 0;
6179 mddev->bitmap_info.default_offset = 0;
6180 mddev->bitmap_info.default_space = 0;
6181 mddev->bitmap_info.chunksize = 0;
6182 mddev->bitmap_info.daemon_sleep = 0;
6183 mddev->bitmap_info.max_write_behind = 0;
6184 mddev->bitmap_info.nodes = 0;
6187 static void __md_stop_writes(struct mddev *mddev)
6189 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6190 if (work_pending(&mddev->del_work))
6191 flush_workqueue(md_misc_wq);
6192 if (mddev->sync_thread) {
6193 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6194 md_unregister_thread(&mddev->sync_thread);
6195 md_reap_sync_thread(mddev);
6198 del_timer_sync(&mddev->safemode_timer);
6200 if (mddev->pers && mddev->pers->quiesce) {
6201 mddev->pers->quiesce(mddev, 1);
6202 mddev->pers->quiesce(mddev, 0);
6204 md_bitmap_flush(mddev);
6206 if (mddev->ro == 0 &&
6207 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
6209 /* mark array as shutdown cleanly */
6210 if (!mddev_is_clustered(mddev))
6212 md_update_sb(mddev, 1);
6214 /* disable policy to guarantee rdevs free resources for serialization */
6215 mddev->serialize_policy = 0;
6216 mddev_destroy_serial_pool(mddev, NULL, true);
6219 void md_stop_writes(struct mddev *mddev)
6221 mddev_lock_nointr(mddev);
6222 __md_stop_writes(mddev);
6223 mddev_unlock(mddev);
6225 EXPORT_SYMBOL_GPL(md_stop_writes);
6227 static void mddev_detach(struct mddev *mddev)
6229 md_bitmap_wait_behind_writes(mddev);
6230 if (mddev->pers && mddev->pers->quiesce && !mddev->suspended) {
6231 mddev->pers->quiesce(mddev, 1);
6232 mddev->pers->quiesce(mddev, 0);
6234 md_unregister_thread(&mddev->thread);
6236 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
6239 static void __md_stop(struct mddev *mddev)
6241 struct md_personality *pers = mddev->pers;
6242 md_bitmap_destroy(mddev);
6243 mddev_detach(mddev);
6244 /* Ensure ->event_work is done */
6245 if (mddev->event_work.func)
6246 flush_workqueue(md_misc_wq);
6247 spin_lock(&mddev->lock);
6249 spin_unlock(&mddev->lock);
6251 pers->free(mddev, mddev->private);
6252 mddev->private = NULL;
6253 if (pers->sync_request && mddev->to_remove == NULL)
6254 mddev->to_remove = &md_redundancy_group;
6255 module_put(pers->owner);
6256 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6259 void md_stop(struct mddev *mddev)
6261 /* stop the array and free an attached data structures.
6262 * This is called from dm-raid
6264 __md_stop_writes(mddev);
6266 bioset_exit(&mddev->bio_set);
6267 bioset_exit(&mddev->sync_set);
6270 EXPORT_SYMBOL_GPL(md_stop);
6272 static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
6277 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
6279 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6280 md_wakeup_thread(mddev->thread);
6282 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
6283 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6284 if (mddev->sync_thread)
6285 /* Thread might be blocked waiting for metadata update
6286 * which will now never happen */
6287 wake_up_process(mddev->sync_thread->tsk);
6289 if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
6291 mddev_unlock(mddev);
6292 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
6294 wait_event(mddev->sb_wait,
6295 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
6296 mddev_lock_nointr(mddev);
6298 mutex_lock(&mddev->open_mutex);
6299 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
6300 mddev->sync_thread ||
6301 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
6302 pr_warn("md: %s still in use.\n",mdname(mddev));
6304 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6305 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6306 md_wakeup_thread(mddev->thread);
6312 __md_stop_writes(mddev);
6318 set_disk_ro(mddev->gendisk, 1);
6319 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6320 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6321 md_wakeup_thread(mddev->thread);
6322 sysfs_notify_dirent_safe(mddev->sysfs_state);
6326 mutex_unlock(&mddev->open_mutex);
6331 * 0 - completely stop and dis-assemble array
6332 * 2 - stop but do not disassemble array
6334 static int do_md_stop(struct mddev *mddev, int mode,
6335 struct block_device *bdev)
6337 struct gendisk *disk = mddev->gendisk;
6338 struct md_rdev *rdev;
6341 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
6343 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6344 md_wakeup_thread(mddev->thread);
6346 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
6347 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6348 if (mddev->sync_thread)
6349 /* Thread might be blocked waiting for metadata update
6350 * which will now never happen */
6351 wake_up_process(mddev->sync_thread->tsk);
6353 mddev_unlock(mddev);
6354 wait_event(resync_wait, (mddev->sync_thread == NULL &&
6355 !test_bit(MD_RECOVERY_RUNNING,
6356 &mddev->recovery)));
6357 mddev_lock_nointr(mddev);
6359 mutex_lock(&mddev->open_mutex);
6360 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
6361 mddev->sysfs_active ||
6362 mddev->sync_thread ||
6363 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
6364 pr_warn("md: %s still in use.\n",mdname(mddev));
6365 mutex_unlock(&mddev->open_mutex);
6367 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6368 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6369 md_wakeup_thread(mddev->thread);
6375 set_disk_ro(disk, 0);
6377 __md_stop_writes(mddev);
6380 /* tell userspace to handle 'inactive' */
6381 sysfs_notify_dirent_safe(mddev->sysfs_state);
6383 rdev_for_each(rdev, mddev)
6384 if (rdev->raid_disk >= 0)
6385 sysfs_unlink_rdev(mddev, rdev);
6387 set_capacity_and_notify(disk, 0);
6388 mutex_unlock(&mddev->open_mutex);
6394 mutex_unlock(&mddev->open_mutex);
6396 * Free resources if final stop
6399 pr_info("md: %s stopped.\n", mdname(mddev));
6401 if (mddev->bitmap_info.file) {
6402 struct file *f = mddev->bitmap_info.file;
6403 spin_lock(&mddev->lock);
6404 mddev->bitmap_info.file = NULL;
6405 spin_unlock(&mddev->lock);
6408 mddev->bitmap_info.offset = 0;
6410 export_array(mddev);
6413 if (mddev->hold_active == UNTIL_STOP)
6414 mddev->hold_active = 0;
6417 sysfs_notify_dirent_safe(mddev->sysfs_state);
6422 static void autorun_array(struct mddev *mddev)
6424 struct md_rdev *rdev;
6427 if (list_empty(&mddev->disks))
6430 pr_info("md: running: ");
6432 rdev_for_each(rdev, mddev) {
6433 pr_cont("<%pg>", rdev->bdev);
6437 err = do_md_run(mddev);
6439 pr_warn("md: do_md_run() returned %d\n", err);
6440 do_md_stop(mddev, 0, NULL);
6445 * lets try to run arrays based on all disks that have arrived
6446 * until now. (those are in pending_raid_disks)
6448 * the method: pick the first pending disk, collect all disks with
6449 * the same UUID, remove all from the pending list and put them into
6450 * the 'same_array' list. Then order this list based on superblock
6451 * update time (freshest comes first), kick out 'old' disks and
6452 * compare superblocks. If everything's fine then run it.
6454 * If "unit" is allocated, then bump its reference count
6456 static void autorun_devices(int part)
6458 struct md_rdev *rdev0, *rdev, *tmp;
6459 struct mddev *mddev;
6461 pr_info("md: autorun ...\n");
6462 while (!list_empty(&pending_raid_disks)) {
6465 LIST_HEAD(candidates);
6466 rdev0 = list_entry(pending_raid_disks.next,
6467 struct md_rdev, same_set);
6469 pr_debug("md: considering %pg ...\n", rdev0->bdev);
6470 INIT_LIST_HEAD(&candidates);
6471 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
6472 if (super_90_load(rdev, rdev0, 0) >= 0) {
6473 pr_debug("md: adding %pg ...\n",
6475 list_move(&rdev->same_set, &candidates);
6478 * now we have a set of devices, with all of them having
6479 * mostly sane superblocks. It's time to allocate the
6483 dev = MKDEV(mdp_major,
6484 rdev0->preferred_minor << MdpMinorShift);
6485 unit = MINOR(dev) >> MdpMinorShift;
6487 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
6490 if (rdev0->preferred_minor != unit) {
6491 pr_warn("md: unit number in %pg is bad: %d\n",
6492 rdev0->bdev, rdev0->preferred_minor);
6496 mddev = md_alloc(dev, NULL);
6500 if (mddev_lock(mddev))
6501 pr_warn("md: %s locked, cannot run\n", mdname(mddev));
6502 else if (mddev->raid_disks || mddev->major_version
6503 || !list_empty(&mddev->disks)) {
6504 pr_warn("md: %s already running, cannot run %pg\n",
6505 mdname(mddev), rdev0->bdev);
6506 mddev_unlock(mddev);
6508 pr_debug("md: created %s\n", mdname(mddev));
6509 mddev->persistent = 1;
6510 rdev_for_each_list(rdev, tmp, &candidates) {
6511 list_del_init(&rdev->same_set);
6512 if (bind_rdev_to_array(rdev, mddev))
6515 autorun_array(mddev);
6516 mddev_unlock(mddev);
6518 /* on success, candidates will be empty, on error
6521 rdev_for_each_list(rdev, tmp, &candidates) {
6522 list_del_init(&rdev->same_set);
6527 pr_info("md: ... autorun DONE.\n");
6529 #endif /* !MODULE */
6531 static int get_version(void __user *arg)
6535 ver.major = MD_MAJOR_VERSION;
6536 ver.minor = MD_MINOR_VERSION;
6537 ver.patchlevel = MD_PATCHLEVEL_VERSION;
6539 if (copy_to_user(arg, &ver, sizeof(ver)))
6545 static int get_array_info(struct mddev *mddev, void __user *arg)
6547 mdu_array_info_t info;
6548 int nr,working,insync,failed,spare;
6549 struct md_rdev *rdev;
6551 nr = working = insync = failed = spare = 0;
6553 rdev_for_each_rcu(rdev, mddev) {
6555 if (test_bit(Faulty, &rdev->flags))
6559 if (test_bit(In_sync, &rdev->flags))
6561 else if (test_bit(Journal, &rdev->flags))
6562 /* TODO: add journal count to md_u.h */
6570 info.major_version = mddev->major_version;
6571 info.minor_version = mddev->minor_version;
6572 info.patch_version = MD_PATCHLEVEL_VERSION;
6573 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
6574 info.level = mddev->level;
6575 info.size = mddev->dev_sectors / 2;
6576 if (info.size != mddev->dev_sectors / 2) /* overflow */
6579 info.raid_disks = mddev->raid_disks;
6580 info.md_minor = mddev->md_minor;
6581 info.not_persistent= !mddev->persistent;
6583 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
6586 info.state = (1<<MD_SB_CLEAN);
6587 if (mddev->bitmap && mddev->bitmap_info.offset)
6588 info.state |= (1<<MD_SB_BITMAP_PRESENT);
6589 if (mddev_is_clustered(mddev))
6590 info.state |= (1<<MD_SB_CLUSTERED);
6591 info.active_disks = insync;
6592 info.working_disks = working;
6593 info.failed_disks = failed;
6594 info.spare_disks = spare;
6596 info.layout = mddev->layout;
6597 info.chunk_size = mddev->chunk_sectors << 9;
6599 if (copy_to_user(arg, &info, sizeof(info)))
6605 static int get_bitmap_file(struct mddev *mddev, void __user * arg)
6607 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
6611 file = kzalloc(sizeof(*file), GFP_NOIO);
6616 spin_lock(&mddev->lock);
6617 /* bitmap enabled */
6618 if (mddev->bitmap_info.file) {
6619 ptr = file_path(mddev->bitmap_info.file, file->pathname,
6620 sizeof(file->pathname));
6624 memmove(file->pathname, ptr,
6625 sizeof(file->pathname)-(ptr-file->pathname));
6627 spin_unlock(&mddev->lock);
6630 copy_to_user(arg, file, sizeof(*file)))
6637 static int get_disk_info(struct mddev *mddev, void __user * arg)
6639 mdu_disk_info_t info;
6640 struct md_rdev *rdev;
6642 if (copy_from_user(&info, arg, sizeof(info)))
6646 rdev = md_find_rdev_nr_rcu(mddev, info.number);
6648 info.major = MAJOR(rdev->bdev->bd_dev);
6649 info.minor = MINOR(rdev->bdev->bd_dev);
6650 info.raid_disk = rdev->raid_disk;
6652 if (test_bit(Faulty, &rdev->flags))
6653 info.state |= (1<<MD_DISK_FAULTY);
6654 else if (test_bit(In_sync, &rdev->flags)) {
6655 info.state |= (1<<MD_DISK_ACTIVE);
6656 info.state |= (1<<MD_DISK_SYNC);
6658 if (test_bit(Journal, &rdev->flags))
6659 info.state |= (1<<MD_DISK_JOURNAL);
6660 if (test_bit(WriteMostly, &rdev->flags))
6661 info.state |= (1<<MD_DISK_WRITEMOSTLY);
6662 if (test_bit(FailFast, &rdev->flags))
6663 info.state |= (1<<MD_DISK_FAILFAST);
6665 info.major = info.minor = 0;
6666 info.raid_disk = -1;
6667 info.state = (1<<MD_DISK_REMOVED);
6671 if (copy_to_user(arg, &info, sizeof(info)))
6677 int md_add_new_disk(struct mddev *mddev, struct mdu_disk_info_s *info)
6679 struct md_rdev *rdev;
6680 dev_t dev = MKDEV(info->major,info->minor);
6682 if (mddev_is_clustered(mddev) &&
6683 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
6684 pr_warn("%s: Cannot add to clustered mddev.\n",
6689 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
6692 if (!mddev->raid_disks) {
6694 /* expecting a device which has a superblock */
6695 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
6697 pr_warn("md: md_import_device returned %ld\n",
6699 return PTR_ERR(rdev);
6701 if (!list_empty(&mddev->disks)) {
6702 struct md_rdev *rdev0
6703 = list_entry(mddev->disks.next,
6704 struct md_rdev, same_set);
6705 err = super_types[mddev->major_version]
6706 .load_super(rdev, rdev0, mddev->minor_version);
6708 pr_warn("md: %pg has different UUID to %pg\n",
6715 err = bind_rdev_to_array(rdev, mddev);
6722 * md_add_new_disk can be used once the array is assembled
6723 * to add "hot spares". They must already have a superblock
6728 if (!mddev->pers->hot_add_disk) {
6729 pr_warn("%s: personality does not support diskops!\n",
6733 if (mddev->persistent)
6734 rdev = md_import_device(dev, mddev->major_version,
6735 mddev->minor_version);
6737 rdev = md_import_device(dev, -1, -1);
6739 pr_warn("md: md_import_device returned %ld\n",
6741 return PTR_ERR(rdev);
6743 /* set saved_raid_disk if appropriate */
6744 if (!mddev->persistent) {
6745 if (info->state & (1<<MD_DISK_SYNC) &&
6746 info->raid_disk < mddev->raid_disks) {
6747 rdev->raid_disk = info->raid_disk;
6748 set_bit(In_sync, &rdev->flags);
6749 clear_bit(Bitmap_sync, &rdev->flags);
6751 rdev->raid_disk = -1;
6752 rdev->saved_raid_disk = rdev->raid_disk;
6754 super_types[mddev->major_version].
6755 validate_super(mddev, rdev);
6756 if ((info->state & (1<<MD_DISK_SYNC)) &&
6757 rdev->raid_disk != info->raid_disk) {
6758 /* This was a hot-add request, but events doesn't
6759 * match, so reject it.
6765 clear_bit(In_sync, &rdev->flags); /* just to be sure */
6766 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6767 set_bit(WriteMostly, &rdev->flags);
6769 clear_bit(WriteMostly, &rdev->flags);
6770 if (info->state & (1<<MD_DISK_FAILFAST))
6771 set_bit(FailFast, &rdev->flags);
6773 clear_bit(FailFast, &rdev->flags);
6775 if (info->state & (1<<MD_DISK_JOURNAL)) {
6776 struct md_rdev *rdev2;
6777 bool has_journal = false;
6779 /* make sure no existing journal disk */
6780 rdev_for_each(rdev2, mddev) {
6781 if (test_bit(Journal, &rdev2->flags)) {
6786 if (has_journal || mddev->bitmap) {
6790 set_bit(Journal, &rdev->flags);
6793 * check whether the device shows up in other nodes
6795 if (mddev_is_clustered(mddev)) {
6796 if (info->state & (1 << MD_DISK_CANDIDATE))
6797 set_bit(Candidate, &rdev->flags);
6798 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
6799 /* --add initiated by this node */
6800 err = md_cluster_ops->add_new_disk(mddev, rdev);
6808 rdev->raid_disk = -1;
6809 err = bind_rdev_to_array(rdev, mddev);
6814 if (mddev_is_clustered(mddev)) {
6815 if (info->state & (1 << MD_DISK_CANDIDATE)) {
6817 err = md_cluster_ops->new_disk_ack(mddev,
6820 md_kick_rdev_from_array(rdev);
6824 md_cluster_ops->add_new_disk_cancel(mddev);
6826 err = add_bound_rdev(rdev);
6830 err = add_bound_rdev(rdev);
6835 /* otherwise, md_add_new_disk is only allowed
6836 * for major_version==0 superblocks
6838 if (mddev->major_version != 0) {
6839 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev));
6843 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6845 rdev = md_import_device(dev, -1, 0);
6847 pr_warn("md: error, md_import_device() returned %ld\n",
6849 return PTR_ERR(rdev);
6851 rdev->desc_nr = info->number;
6852 if (info->raid_disk < mddev->raid_disks)
6853 rdev->raid_disk = info->raid_disk;
6855 rdev->raid_disk = -1;
6857 if (rdev->raid_disk < mddev->raid_disks)
6858 if (info->state & (1<<MD_DISK_SYNC))
6859 set_bit(In_sync, &rdev->flags);
6861 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6862 set_bit(WriteMostly, &rdev->flags);
6863 if (info->state & (1<<MD_DISK_FAILFAST))
6864 set_bit(FailFast, &rdev->flags);
6866 if (!mddev->persistent) {
6867 pr_debug("md: nonpersistent superblock ...\n");
6868 rdev->sb_start = bdev_nr_sectors(rdev->bdev);
6870 rdev->sb_start = calc_dev_sboffset(rdev);
6871 rdev->sectors = rdev->sb_start;
6873 err = bind_rdev_to_array(rdev, mddev);
6883 static int hot_remove_disk(struct mddev *mddev, dev_t dev)
6885 struct md_rdev *rdev;
6890 rdev = find_rdev(mddev, dev);
6894 if (rdev->raid_disk < 0)
6897 clear_bit(Blocked, &rdev->flags);
6898 remove_and_add_spares(mddev, rdev);
6900 if (rdev->raid_disk >= 0)
6904 if (mddev_is_clustered(mddev)) {
6905 if (md_cluster_ops->remove_disk(mddev, rdev))
6909 md_kick_rdev_from_array(rdev);
6910 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
6912 md_wakeup_thread(mddev->thread);
6914 md_update_sb(mddev, 1);
6919 pr_debug("md: cannot remove active disk %pg from %s ...\n",
6920 rdev->bdev, mdname(mddev));
6924 static int hot_add_disk(struct mddev *mddev, dev_t dev)
6927 struct md_rdev *rdev;
6932 if (mddev->major_version != 0) {
6933 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
6937 if (!mddev->pers->hot_add_disk) {
6938 pr_warn("%s: personality does not support diskops!\n",
6943 rdev = md_import_device(dev, -1, 0);
6945 pr_warn("md: error, md_import_device() returned %ld\n",
6950 if (mddev->persistent)
6951 rdev->sb_start = calc_dev_sboffset(rdev);
6953 rdev->sb_start = bdev_nr_sectors(rdev->bdev);
6955 rdev->sectors = rdev->sb_start;
6957 if (test_bit(Faulty, &rdev->flags)) {
6958 pr_warn("md: can not hot-add faulty %pg disk to %s!\n",
6959 rdev->bdev, mdname(mddev));
6964 clear_bit(In_sync, &rdev->flags);
6966 rdev->saved_raid_disk = -1;
6967 err = bind_rdev_to_array(rdev, mddev);
6972 * The rest should better be atomic, we can have disk failures
6973 * noticed in interrupt contexts ...
6976 rdev->raid_disk = -1;
6978 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
6980 md_update_sb(mddev, 1);
6982 * If the new disk does not support REQ_NOWAIT,
6983 * disable on the whole MD.
6985 if (!bdev_nowait(rdev->bdev)) {
6986 pr_info("%s: Disabling nowait because %pg does not support nowait\n",
6987 mdname(mddev), rdev->bdev);
6988 blk_queue_flag_clear(QUEUE_FLAG_NOWAIT, mddev->queue);
6991 * Kick recovery, maybe this spare has to be added to the
6992 * array immediately.
6994 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6995 md_wakeup_thread(mddev->thread);
7004 static int set_bitmap_file(struct mddev *mddev, int fd)
7009 if (!mddev->pers->quiesce || !mddev->thread)
7011 if (mddev->recovery || mddev->sync_thread)
7013 /* we should be able to change the bitmap.. */
7017 struct inode *inode;
7020 if (mddev->bitmap || mddev->bitmap_info.file)
7021 return -EEXIST; /* cannot add when bitmap is present */
7025 pr_warn("%s: error: failed to get bitmap file\n",
7030 inode = f->f_mapping->host;
7031 if (!S_ISREG(inode->i_mode)) {
7032 pr_warn("%s: error: bitmap file must be a regular file\n",
7035 } else if (!(f->f_mode & FMODE_WRITE)) {
7036 pr_warn("%s: error: bitmap file must open for write\n",
7039 } else if (atomic_read(&inode->i_writecount) != 1) {
7040 pr_warn("%s: error: bitmap file is already in use\n",
7048 mddev->bitmap_info.file = f;
7049 mddev->bitmap_info.offset = 0; /* file overrides offset */
7050 } else if (mddev->bitmap == NULL)
7051 return -ENOENT; /* cannot remove what isn't there */
7055 struct bitmap *bitmap;
7057 bitmap = md_bitmap_create(mddev, -1);
7058 mddev_suspend(mddev);
7059 if (!IS_ERR(bitmap)) {
7060 mddev->bitmap = bitmap;
7061 err = md_bitmap_load(mddev);
7063 err = PTR_ERR(bitmap);
7065 md_bitmap_destroy(mddev);
7068 mddev_resume(mddev);
7069 } else if (fd < 0) {
7070 mddev_suspend(mddev);
7071 md_bitmap_destroy(mddev);
7072 mddev_resume(mddev);
7076 struct file *f = mddev->bitmap_info.file;
7078 spin_lock(&mddev->lock);
7079 mddev->bitmap_info.file = NULL;
7080 spin_unlock(&mddev->lock);
7089 * md_set_array_info is used two different ways
7090 * The original usage is when creating a new array.
7091 * In this usage, raid_disks is > 0 and it together with
7092 * level, size, not_persistent,layout,chunksize determine the
7093 * shape of the array.
7094 * This will always create an array with a type-0.90.0 superblock.
7095 * The newer usage is when assembling an array.
7096 * In this case raid_disks will be 0, and the major_version field is
7097 * use to determine which style super-blocks are to be found on the devices.
7098 * The minor and patch _version numbers are also kept incase the
7099 * super_block handler wishes to interpret them.
7101 int md_set_array_info(struct mddev *mddev, struct mdu_array_info_s *info)
7103 if (info->raid_disks == 0) {
7104 /* just setting version number for superblock loading */
7105 if (info->major_version < 0 ||
7106 info->major_version >= ARRAY_SIZE(super_types) ||
7107 super_types[info->major_version].name == NULL) {
7108 /* maybe try to auto-load a module? */
7109 pr_warn("md: superblock version %d not known\n",
7110 info->major_version);
7113 mddev->major_version = info->major_version;
7114 mddev->minor_version = info->minor_version;
7115 mddev->patch_version = info->patch_version;
7116 mddev->persistent = !info->not_persistent;
7117 /* ensure mddev_put doesn't delete this now that there
7118 * is some minimal configuration.
7120 mddev->ctime = ktime_get_real_seconds();
7123 mddev->major_version = MD_MAJOR_VERSION;
7124 mddev->minor_version = MD_MINOR_VERSION;
7125 mddev->patch_version = MD_PATCHLEVEL_VERSION;
7126 mddev->ctime = ktime_get_real_seconds();
7128 mddev->level = info->level;
7129 mddev->clevel[0] = 0;
7130 mddev->dev_sectors = 2 * (sector_t)info->size;
7131 mddev->raid_disks = info->raid_disks;
7132 /* don't set md_minor, it is determined by which /dev/md* was
7135 if (info->state & (1<<MD_SB_CLEAN))
7136 mddev->recovery_cp = MaxSector;
7138 mddev->recovery_cp = 0;
7139 mddev->persistent = ! info->not_persistent;
7140 mddev->external = 0;
7142 mddev->layout = info->layout;
7143 if (mddev->level == 0)
7144 /* Cannot trust RAID0 layout info here */
7146 mddev->chunk_sectors = info->chunk_size >> 9;
7148 if (mddev->persistent) {
7149 mddev->max_disks = MD_SB_DISKS;
7151 mddev->sb_flags = 0;
7153 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
7155 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
7156 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
7157 mddev->bitmap_info.offset = 0;
7159 mddev->reshape_position = MaxSector;
7162 * Generate a 128 bit UUID
7164 get_random_bytes(mddev->uuid, 16);
7166 mddev->new_level = mddev->level;
7167 mddev->new_chunk_sectors = mddev->chunk_sectors;
7168 mddev->new_layout = mddev->layout;
7169 mddev->delta_disks = 0;
7170 mddev->reshape_backwards = 0;
7175 void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
7177 lockdep_assert_held(&mddev->reconfig_mutex);
7179 if (mddev->external_size)
7182 mddev->array_sectors = array_sectors;
7184 EXPORT_SYMBOL(md_set_array_sectors);
7186 static int update_size(struct mddev *mddev, sector_t num_sectors)
7188 struct md_rdev *rdev;
7190 int fit = (num_sectors == 0);
7191 sector_t old_dev_sectors = mddev->dev_sectors;
7193 if (mddev->pers->resize == NULL)
7195 /* The "num_sectors" is the number of sectors of each device that
7196 * is used. This can only make sense for arrays with redundancy.
7197 * linear and raid0 always use whatever space is available. We can only
7198 * consider changing this number if no resync or reconstruction is
7199 * happening, and if the new size is acceptable. It must fit before the
7200 * sb_start or, if that is <data_offset, it must fit before the size
7201 * of each device. If num_sectors is zero, we find the largest size
7204 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
7210 rdev_for_each(rdev, mddev) {
7211 sector_t avail = rdev->sectors;
7213 if (fit && (num_sectors == 0 || num_sectors > avail))
7214 num_sectors = avail;
7215 if (avail < num_sectors)
7218 rv = mddev->pers->resize(mddev, num_sectors);
7220 if (mddev_is_clustered(mddev))
7221 md_cluster_ops->update_size(mddev, old_dev_sectors);
7222 else if (mddev->queue) {
7223 set_capacity_and_notify(mddev->gendisk,
7224 mddev->array_sectors);
7230 static int update_raid_disks(struct mddev *mddev, int raid_disks)
7233 struct md_rdev *rdev;
7234 /* change the number of raid disks */
7235 if (mddev->pers->check_reshape == NULL)
7239 if (raid_disks <= 0 ||
7240 (mddev->max_disks && raid_disks >= mddev->max_disks))
7242 if (mddev->sync_thread ||
7243 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
7244 test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) ||
7245 mddev->reshape_position != MaxSector)
7248 rdev_for_each(rdev, mddev) {
7249 if (mddev->raid_disks < raid_disks &&
7250 rdev->data_offset < rdev->new_data_offset)
7252 if (mddev->raid_disks > raid_disks &&
7253 rdev->data_offset > rdev->new_data_offset)
7257 mddev->delta_disks = raid_disks - mddev->raid_disks;
7258 if (mddev->delta_disks < 0)
7259 mddev->reshape_backwards = 1;
7260 else if (mddev->delta_disks > 0)
7261 mddev->reshape_backwards = 0;
7263 rv = mddev->pers->check_reshape(mddev);
7265 mddev->delta_disks = 0;
7266 mddev->reshape_backwards = 0;
7272 * update_array_info is used to change the configuration of an
7274 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
7275 * fields in the info are checked against the array.
7276 * Any differences that cannot be handled will cause an error.
7277 * Normally, only one change can be managed at a time.
7279 static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
7285 /* calculate expected state,ignoring low bits */
7286 if (mddev->bitmap && mddev->bitmap_info.offset)
7287 state |= (1 << MD_SB_BITMAP_PRESENT);
7289 if (mddev->major_version != info->major_version ||
7290 mddev->minor_version != info->minor_version ||
7291 /* mddev->patch_version != info->patch_version || */
7292 mddev->ctime != info->ctime ||
7293 mddev->level != info->level ||
7294 /* mddev->layout != info->layout || */
7295 mddev->persistent != !info->not_persistent ||
7296 mddev->chunk_sectors != info->chunk_size >> 9 ||
7297 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
7298 ((state^info->state) & 0xfffffe00)
7301 /* Check there is only one change */
7302 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
7304 if (mddev->raid_disks != info->raid_disks)
7306 if (mddev->layout != info->layout)
7308 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
7315 if (mddev->layout != info->layout) {
7317 * we don't need to do anything at the md level, the
7318 * personality will take care of it all.
7320 if (mddev->pers->check_reshape == NULL)
7323 mddev->new_layout = info->layout;
7324 rv = mddev->pers->check_reshape(mddev);
7326 mddev->new_layout = mddev->layout;
7330 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
7331 rv = update_size(mddev, (sector_t)info->size * 2);
7333 if (mddev->raid_disks != info->raid_disks)
7334 rv = update_raid_disks(mddev, info->raid_disks);
7336 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
7337 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
7341 if (mddev->recovery || mddev->sync_thread) {
7345 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
7346 struct bitmap *bitmap;
7347 /* add the bitmap */
7348 if (mddev->bitmap) {
7352 if (mddev->bitmap_info.default_offset == 0) {
7356 mddev->bitmap_info.offset =
7357 mddev->bitmap_info.default_offset;
7358 mddev->bitmap_info.space =
7359 mddev->bitmap_info.default_space;
7360 bitmap = md_bitmap_create(mddev, -1);
7361 mddev_suspend(mddev);
7362 if (!IS_ERR(bitmap)) {
7363 mddev->bitmap = bitmap;
7364 rv = md_bitmap_load(mddev);
7366 rv = PTR_ERR(bitmap);
7368 md_bitmap_destroy(mddev);
7369 mddev_resume(mddev);
7371 /* remove the bitmap */
7372 if (!mddev->bitmap) {
7376 if (mddev->bitmap->storage.file) {
7380 if (mddev->bitmap_info.nodes) {
7381 /* hold PW on all the bitmap lock */
7382 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
7383 pr_warn("md: can't change bitmap to none since the array is in use by more than one node\n");
7385 md_cluster_ops->unlock_all_bitmaps(mddev);
7389 mddev->bitmap_info.nodes = 0;
7390 md_cluster_ops->leave(mddev);
7391 module_put(md_cluster_mod);
7392 mddev->safemode_delay = DEFAULT_SAFEMODE_DELAY;
7394 mddev_suspend(mddev);
7395 md_bitmap_destroy(mddev);
7396 mddev_resume(mddev);
7397 mddev->bitmap_info.offset = 0;
7400 md_update_sb(mddev, 1);
7406 static int set_disk_faulty(struct mddev *mddev, dev_t dev)
7408 struct md_rdev *rdev;
7411 if (mddev->pers == NULL)
7415 rdev = md_find_rdev_rcu(mddev, dev);
7419 md_error(mddev, rdev);
7420 if (test_bit(MD_BROKEN, &mddev->flags))
7428 * We have a problem here : there is no easy way to give a CHS
7429 * virtual geometry. We currently pretend that we have a 2 heads
7430 * 4 sectors (with a BIG number of cylinders...). This drives
7431 * dosfs just mad... ;-)
7433 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
7435 struct mddev *mddev = bdev->bd_disk->private_data;
7439 geo->cylinders = mddev->array_sectors / 8;
7443 static inline bool md_ioctl_valid(unsigned int cmd)
7447 case GET_ARRAY_INFO:
7448 case GET_BITMAP_FILE:
7451 case HOT_REMOVE_DISK:
7453 case RESTART_ARRAY_RW:
7455 case SET_ARRAY_INFO:
7456 case SET_BITMAP_FILE:
7457 case SET_DISK_FAULTY:
7460 case CLUSTERED_DISK_NACK:
7467 static int md_ioctl(struct block_device *bdev, fmode_t mode,
7468 unsigned int cmd, unsigned long arg)
7471 void __user *argp = (void __user *)arg;
7472 struct mddev *mddev = NULL;
7473 bool did_set_md_closing = false;
7475 if (!md_ioctl_valid(cmd))
7480 case GET_ARRAY_INFO:
7484 if (!capable(CAP_SYS_ADMIN))
7489 * Commands dealing with the RAID driver but not any
7494 err = get_version(argp);
7500 * Commands creating/starting a new array:
7503 mddev = bdev->bd_disk->private_data;
7510 /* Some actions do not requires the mutex */
7512 case GET_ARRAY_INFO:
7513 if (!mddev->raid_disks && !mddev->external)
7516 err = get_array_info(mddev, argp);
7520 if (!mddev->raid_disks && !mddev->external)
7523 err = get_disk_info(mddev, argp);
7526 case SET_DISK_FAULTY:
7527 err = set_disk_faulty(mddev, new_decode_dev(arg));
7530 case GET_BITMAP_FILE:
7531 err = get_bitmap_file(mddev, argp);
7536 if (cmd == ADD_NEW_DISK || cmd == HOT_ADD_DISK)
7537 flush_rdev_wq(mddev);
7539 if (cmd == HOT_REMOVE_DISK)
7540 /* need to ensure recovery thread has run */
7541 wait_event_interruptible_timeout(mddev->sb_wait,
7542 !test_bit(MD_RECOVERY_NEEDED,
7544 msecs_to_jiffies(5000));
7545 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
7546 /* Need to flush page cache, and ensure no-one else opens
7549 mutex_lock(&mddev->open_mutex);
7550 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
7551 mutex_unlock(&mddev->open_mutex);
7555 if (test_and_set_bit(MD_CLOSING, &mddev->flags)) {
7556 mutex_unlock(&mddev->open_mutex);
7560 did_set_md_closing = true;
7561 mutex_unlock(&mddev->open_mutex);
7562 sync_blockdev(bdev);
7564 err = mddev_lock(mddev);
7566 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7571 if (cmd == SET_ARRAY_INFO) {
7572 mdu_array_info_t info;
7574 memset(&info, 0, sizeof(info));
7575 else if (copy_from_user(&info, argp, sizeof(info))) {
7580 err = update_array_info(mddev, &info);
7582 pr_warn("md: couldn't update array info. %d\n", err);
7587 if (!list_empty(&mddev->disks)) {
7588 pr_warn("md: array %s already has disks!\n", mdname(mddev));
7592 if (mddev->raid_disks) {
7593 pr_warn("md: array %s already initialised!\n", mdname(mddev));
7597 err = md_set_array_info(mddev, &info);
7599 pr_warn("md: couldn't set array info. %d\n", err);
7606 * Commands querying/configuring an existing array:
7608 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
7609 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
7610 if ((!mddev->raid_disks && !mddev->external)
7611 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
7612 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
7613 && cmd != GET_BITMAP_FILE) {
7619 * Commands even a read-only array can execute:
7622 case RESTART_ARRAY_RW:
7623 err = restart_array(mddev);
7627 err = do_md_stop(mddev, 0, bdev);
7631 err = md_set_readonly(mddev, bdev);
7634 case HOT_REMOVE_DISK:
7635 err = hot_remove_disk(mddev, new_decode_dev(arg));
7639 /* We can support ADD_NEW_DISK on read-only arrays
7640 * only if we are re-adding a preexisting device.
7641 * So require mddev->pers and MD_DISK_SYNC.
7644 mdu_disk_info_t info;
7645 if (copy_from_user(&info, argp, sizeof(info)))
7647 else if (!(info.state & (1<<MD_DISK_SYNC)))
7648 /* Need to clear read-only for this */
7651 err = md_add_new_disk(mddev, &info);
7658 * The remaining ioctls are changing the state of the
7659 * superblock, so we do not allow them on read-only arrays.
7661 if (mddev->ro && mddev->pers) {
7662 if (mddev->ro == 2) {
7664 sysfs_notify_dirent_safe(mddev->sysfs_state);
7665 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7666 /* mddev_unlock will wake thread */
7667 /* If a device failed while we were read-only, we
7668 * need to make sure the metadata is updated now.
7670 if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) {
7671 mddev_unlock(mddev);
7672 wait_event(mddev->sb_wait,
7673 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) &&
7674 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
7675 mddev_lock_nointr(mddev);
7686 mdu_disk_info_t info;
7687 if (copy_from_user(&info, argp, sizeof(info)))
7690 err = md_add_new_disk(mddev, &info);
7694 case CLUSTERED_DISK_NACK:
7695 if (mddev_is_clustered(mddev))
7696 md_cluster_ops->new_disk_ack(mddev, false);
7702 err = hot_add_disk(mddev, new_decode_dev(arg));
7706 err = do_md_run(mddev);
7709 case SET_BITMAP_FILE:
7710 err = set_bitmap_file(mddev, (int)arg);
7719 if (mddev->hold_active == UNTIL_IOCTL &&
7721 mddev->hold_active = 0;
7722 mddev_unlock(mddev);
7724 if(did_set_md_closing)
7725 clear_bit(MD_CLOSING, &mddev->flags);
7728 #ifdef CONFIG_COMPAT
7729 static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7730 unsigned int cmd, unsigned long arg)
7733 case HOT_REMOVE_DISK:
7735 case SET_DISK_FAULTY:
7736 case SET_BITMAP_FILE:
7737 /* These take in integer arg, do not convert */
7740 arg = (unsigned long)compat_ptr(arg);
7744 return md_ioctl(bdev, mode, cmd, arg);
7746 #endif /* CONFIG_COMPAT */
7748 static int md_set_read_only(struct block_device *bdev, bool ro)
7750 struct mddev *mddev = bdev->bd_disk->private_data;
7753 err = mddev_lock(mddev);
7757 if (!mddev->raid_disks && !mddev->external) {
7763 * Transitioning to read-auto need only happen for arrays that call
7764 * md_write_start and which are not ready for writes yet.
7766 if (!ro && mddev->ro == 1 && mddev->pers) {
7767 err = restart_array(mddev);
7774 mddev_unlock(mddev);
7778 static int md_open(struct block_device *bdev, fmode_t mode)
7780 struct mddev *mddev;
7783 spin_lock(&all_mddevs_lock);
7784 mddev = mddev_get(bdev->bd_disk->private_data);
7785 spin_unlock(&all_mddevs_lock);
7789 err = mutex_lock_interruptible(&mddev->open_mutex);
7794 if (test_bit(MD_CLOSING, &mddev->flags))
7797 atomic_inc(&mddev->openers);
7798 mutex_unlock(&mddev->open_mutex);
7800 bdev_check_media_change(bdev);
7804 mutex_unlock(&mddev->open_mutex);
7810 static void md_release(struct gendisk *disk, fmode_t mode)
7812 struct mddev *mddev = disk->private_data;
7815 atomic_dec(&mddev->openers);
7819 static unsigned int md_check_events(struct gendisk *disk, unsigned int clearing)
7821 struct mddev *mddev = disk->private_data;
7822 unsigned int ret = 0;
7825 ret = DISK_EVENT_MEDIA_CHANGE;
7830 static void md_free_disk(struct gendisk *disk)
7832 struct mddev *mddev = disk->private_data;
7834 percpu_ref_exit(&mddev->writes_pending);
7835 bioset_exit(&mddev->bio_set);
7836 bioset_exit(&mddev->sync_set);
7841 const struct block_device_operations md_fops =
7843 .owner = THIS_MODULE,
7844 .submit_bio = md_submit_bio,
7846 .release = md_release,
7848 #ifdef CONFIG_COMPAT
7849 .compat_ioctl = md_compat_ioctl,
7851 .getgeo = md_getgeo,
7852 .check_events = md_check_events,
7853 .set_read_only = md_set_read_only,
7854 .free_disk = md_free_disk,
7857 static int md_thread(void *arg)
7859 struct md_thread *thread = arg;
7862 * md_thread is a 'system-thread', it's priority should be very
7863 * high. We avoid resource deadlocks individually in each
7864 * raid personality. (RAID5 does preallocation) We also use RR and
7865 * the very same RT priority as kswapd, thus we will never get
7866 * into a priority inversion deadlock.
7868 * we definitely have to have equal or higher priority than
7869 * bdflush, otherwise bdflush will deadlock if there are too
7870 * many dirty RAID5 blocks.
7873 allow_signal(SIGKILL);
7874 while (!kthread_should_stop()) {
7876 /* We need to wait INTERRUPTIBLE so that
7877 * we don't add to the load-average.
7878 * That means we need to be sure no signals are
7881 if (signal_pending(current))
7882 flush_signals(current);
7884 wait_event_interruptible_timeout
7886 test_bit(THREAD_WAKEUP, &thread->flags)
7887 || kthread_should_stop() || kthread_should_park(),
7890 clear_bit(THREAD_WAKEUP, &thread->flags);
7891 if (kthread_should_park())
7893 if (!kthread_should_stop())
7894 thread->run(thread);
7900 void md_wakeup_thread(struct md_thread *thread)
7903 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
7904 set_bit(THREAD_WAKEUP, &thread->flags);
7905 wake_up(&thread->wqueue);
7908 EXPORT_SYMBOL(md_wakeup_thread);
7910 struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7911 struct mddev *mddev, const char *name)
7913 struct md_thread *thread;
7915 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
7919 init_waitqueue_head(&thread->wqueue);
7922 thread->mddev = mddev;
7923 thread->timeout = MAX_SCHEDULE_TIMEOUT;
7924 thread->tsk = kthread_run(md_thread, thread,
7926 mdname(thread->mddev),
7928 if (IS_ERR(thread->tsk)) {
7934 EXPORT_SYMBOL(md_register_thread);
7936 void md_unregister_thread(struct md_thread **threadp)
7938 struct md_thread *thread;
7941 * Locking ensures that mddev_unlock does not wake_up a
7942 * non-existent thread
7944 spin_lock(&pers_lock);
7947 spin_unlock(&pers_lock);
7951 spin_unlock(&pers_lock);
7953 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
7954 kthread_stop(thread->tsk);
7957 EXPORT_SYMBOL(md_unregister_thread);
7959 void md_error(struct mddev *mddev, struct md_rdev *rdev)
7961 if (!rdev || test_bit(Faulty, &rdev->flags))
7964 if (!mddev->pers || !mddev->pers->error_handler)
7966 mddev->pers->error_handler(mddev, rdev);
7968 if (mddev->degraded && !test_bit(MD_BROKEN, &mddev->flags))
7969 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7970 sysfs_notify_dirent_safe(rdev->sysfs_state);
7971 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7972 if (!test_bit(MD_BROKEN, &mddev->flags)) {
7973 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7974 md_wakeup_thread(mddev->thread);
7976 if (mddev->event_work.func)
7977 queue_work(md_misc_wq, &mddev->event_work);
7980 EXPORT_SYMBOL(md_error);
7982 /* seq_file implementation /proc/mdstat */
7984 static void status_unused(struct seq_file *seq)
7987 struct md_rdev *rdev;
7989 seq_printf(seq, "unused devices: ");
7991 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
7993 seq_printf(seq, "%pg ", rdev->bdev);
7996 seq_printf(seq, "<none>");
7998 seq_printf(seq, "\n");
8001 static int status_resync(struct seq_file *seq, struct mddev *mddev)
8003 sector_t max_sectors, resync, res;
8004 unsigned long dt, db = 0;
8005 sector_t rt, curr_mark_cnt, resync_mark_cnt;
8006 int scale, recovery_active;
8007 unsigned int per_milli;
8009 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8010 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
8011 max_sectors = mddev->resync_max_sectors;
8013 max_sectors = mddev->dev_sectors;
8015 resync = mddev->curr_resync;
8016 if (resync < MD_RESYNC_ACTIVE) {
8017 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
8018 /* Still cleaning up */
8019 resync = max_sectors;
8020 } else if (resync > max_sectors) {
8021 resync = max_sectors;
8023 resync -= atomic_read(&mddev->recovery_active);
8024 if (resync < MD_RESYNC_ACTIVE) {
8026 * Resync has started, but the subtraction has
8027 * yielded one of the special values. Force it
8028 * to active to ensure the status reports an
8031 resync = MD_RESYNC_ACTIVE;
8035 if (resync == MD_RESYNC_NONE) {
8036 if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery)) {
8037 struct md_rdev *rdev;
8039 rdev_for_each(rdev, mddev)
8040 if (rdev->raid_disk >= 0 &&
8041 !test_bit(Faulty, &rdev->flags) &&
8042 rdev->recovery_offset != MaxSector &&
8043 rdev->recovery_offset) {
8044 seq_printf(seq, "\trecover=REMOTE");
8047 if (mddev->reshape_position != MaxSector)
8048 seq_printf(seq, "\treshape=REMOTE");
8050 seq_printf(seq, "\tresync=REMOTE");
8053 if (mddev->recovery_cp < MaxSector) {
8054 seq_printf(seq, "\tresync=PENDING");
8059 if (resync < MD_RESYNC_ACTIVE) {
8060 seq_printf(seq, "\tresync=DELAYED");
8064 WARN_ON(max_sectors == 0);
8065 /* Pick 'scale' such that (resync>>scale)*1000 will fit
8066 * in a sector_t, and (max_sectors>>scale) will fit in a
8067 * u32, as those are the requirements for sector_div.
8068 * Thus 'scale' must be at least 10
8071 if (sizeof(sector_t) > sizeof(unsigned long)) {
8072 while ( max_sectors/2 > (1ULL<<(scale+32)))
8075 res = (resync>>scale)*1000;
8076 sector_div(res, (u32)((max_sectors>>scale)+1));
8080 int i, x = per_milli/50, y = 20-x;
8081 seq_printf(seq, "[");
8082 for (i = 0; i < x; i++)
8083 seq_printf(seq, "=");
8084 seq_printf(seq, ">");
8085 for (i = 0; i < y; i++)
8086 seq_printf(seq, ".");
8087 seq_printf(seq, "] ");
8089 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
8090 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
8092 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
8094 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
8095 "resync" : "recovery"))),
8096 per_milli/10, per_milli % 10,
8097 (unsigned long long) resync/2,
8098 (unsigned long long) max_sectors/2);
8101 * dt: time from mark until now
8102 * db: blocks written from mark until now
8103 * rt: remaining time
8105 * rt is a sector_t, which is always 64bit now. We are keeping
8106 * the original algorithm, but it is not really necessary.
8108 * Original algorithm:
8109 * So we divide before multiply in case it is 32bit and close
8111 * We scale the divisor (db) by 32 to avoid losing precision
8112 * near the end of resync when the number of remaining sectors
8114 * We then divide rt by 32 after multiplying by db to compensate.
8115 * The '+1' avoids division by zero if db is very small.
8117 dt = ((jiffies - mddev->resync_mark) / HZ);
8120 curr_mark_cnt = mddev->curr_mark_cnt;
8121 recovery_active = atomic_read(&mddev->recovery_active);
8122 resync_mark_cnt = mddev->resync_mark_cnt;
8124 if (curr_mark_cnt >= (recovery_active + resync_mark_cnt))
8125 db = curr_mark_cnt - (recovery_active + resync_mark_cnt);
8127 rt = max_sectors - resync; /* number of remaining sectors */
8128 rt = div64_u64(rt, db/32+1);
8132 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
8133 ((unsigned long)rt % 60)/6);
8135 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
8139 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
8141 struct list_head *tmp;
8143 struct mddev *mddev;
8155 spin_lock(&all_mddevs_lock);
8156 list_for_each(tmp,&all_mddevs)
8158 mddev = list_entry(tmp, struct mddev, all_mddevs);
8159 if (!mddev_get(mddev))
8161 spin_unlock(&all_mddevs_lock);
8164 spin_unlock(&all_mddevs_lock);
8166 return (void*)2;/* tail */
8170 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
8172 struct list_head *tmp;
8173 struct mddev *next_mddev, *mddev = v;
8174 struct mddev *to_put = NULL;
8180 spin_lock(&all_mddevs_lock);
8181 if (v == (void*)1) {
8182 tmp = all_mddevs.next;
8185 tmp = mddev->all_mddevs.next;
8189 if (tmp == &all_mddevs) {
8190 next_mddev = (void*)2;
8194 next_mddev = list_entry(tmp, struct mddev, all_mddevs);
8195 if (mddev_get(next_mddev))
8198 tmp = mddev->all_mddevs.next;
8200 spin_unlock(&all_mddevs_lock);
8208 static void md_seq_stop(struct seq_file *seq, void *v)
8210 struct mddev *mddev = v;
8212 if (mddev && v != (void*)1 && v != (void*)2)
8216 static int md_seq_show(struct seq_file *seq, void *v)
8218 struct mddev *mddev = v;
8220 struct md_rdev *rdev;
8222 if (v == (void*)1) {
8223 struct md_personality *pers;
8224 seq_printf(seq, "Personalities : ");
8225 spin_lock(&pers_lock);
8226 list_for_each_entry(pers, &pers_list, list)
8227 seq_printf(seq, "[%s] ", pers->name);
8229 spin_unlock(&pers_lock);
8230 seq_printf(seq, "\n");
8231 seq->poll_event = atomic_read(&md_event_count);
8234 if (v == (void*)2) {
8239 spin_lock(&mddev->lock);
8240 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
8241 seq_printf(seq, "%s : %sactive", mdname(mddev),
8242 mddev->pers ? "" : "in");
8245 seq_printf(seq, " (read-only)");
8247 seq_printf(seq, " (auto-read-only)");
8248 seq_printf(seq, " %s", mddev->pers->name);
8253 rdev_for_each_rcu(rdev, mddev) {
8254 seq_printf(seq, " %pg[%d]", rdev->bdev, rdev->desc_nr);
8256 if (test_bit(WriteMostly, &rdev->flags))
8257 seq_printf(seq, "(W)");
8258 if (test_bit(Journal, &rdev->flags))
8259 seq_printf(seq, "(J)");
8260 if (test_bit(Faulty, &rdev->flags)) {
8261 seq_printf(seq, "(F)");
8264 if (rdev->raid_disk < 0)
8265 seq_printf(seq, "(S)"); /* spare */
8266 if (test_bit(Replacement, &rdev->flags))
8267 seq_printf(seq, "(R)");
8268 sectors += rdev->sectors;
8272 if (!list_empty(&mddev->disks)) {
8274 seq_printf(seq, "\n %llu blocks",
8275 (unsigned long long)
8276 mddev->array_sectors / 2);
8278 seq_printf(seq, "\n %llu blocks",
8279 (unsigned long long)sectors / 2);
8281 if (mddev->persistent) {
8282 if (mddev->major_version != 0 ||
8283 mddev->minor_version != 90) {
8284 seq_printf(seq," super %d.%d",
8285 mddev->major_version,
8286 mddev->minor_version);
8288 } else if (mddev->external)
8289 seq_printf(seq, " super external:%s",
8290 mddev->metadata_type);
8292 seq_printf(seq, " super non-persistent");
8295 mddev->pers->status(seq, mddev);
8296 seq_printf(seq, "\n ");
8297 if (mddev->pers->sync_request) {
8298 if (status_resync(seq, mddev))
8299 seq_printf(seq, "\n ");
8302 seq_printf(seq, "\n ");
8304 md_bitmap_status(seq, mddev->bitmap);
8306 seq_printf(seq, "\n");
8308 spin_unlock(&mddev->lock);
8313 static const struct seq_operations md_seq_ops = {
8314 .start = md_seq_start,
8315 .next = md_seq_next,
8316 .stop = md_seq_stop,
8317 .show = md_seq_show,
8320 static int md_seq_open(struct inode *inode, struct file *file)
8322 struct seq_file *seq;
8325 error = seq_open(file, &md_seq_ops);
8329 seq = file->private_data;
8330 seq->poll_event = atomic_read(&md_event_count);
8334 static int md_unloading;
8335 static __poll_t mdstat_poll(struct file *filp, poll_table *wait)
8337 struct seq_file *seq = filp->private_data;
8341 return EPOLLIN|EPOLLRDNORM|EPOLLERR|EPOLLPRI;
8342 poll_wait(filp, &md_event_waiters, wait);
8344 /* always allow read */
8345 mask = EPOLLIN | EPOLLRDNORM;
8347 if (seq->poll_event != atomic_read(&md_event_count))
8348 mask |= EPOLLERR | EPOLLPRI;
8352 static const struct proc_ops mdstat_proc_ops = {
8353 .proc_open = md_seq_open,
8354 .proc_read = seq_read,
8355 .proc_lseek = seq_lseek,
8356 .proc_release = seq_release,
8357 .proc_poll = mdstat_poll,
8360 int register_md_personality(struct md_personality *p)
8362 pr_debug("md: %s personality registered for level %d\n",
8364 spin_lock(&pers_lock);
8365 list_add_tail(&p->list, &pers_list);
8366 spin_unlock(&pers_lock);
8369 EXPORT_SYMBOL(register_md_personality);
8371 int unregister_md_personality(struct md_personality *p)
8373 pr_debug("md: %s personality unregistered\n", p->name);
8374 spin_lock(&pers_lock);
8375 list_del_init(&p->list);
8376 spin_unlock(&pers_lock);
8379 EXPORT_SYMBOL(unregister_md_personality);
8381 int register_md_cluster_operations(struct md_cluster_operations *ops,
8382 struct module *module)
8385 spin_lock(&pers_lock);
8386 if (md_cluster_ops != NULL)
8389 md_cluster_ops = ops;
8390 md_cluster_mod = module;
8392 spin_unlock(&pers_lock);
8395 EXPORT_SYMBOL(register_md_cluster_operations);
8397 int unregister_md_cluster_operations(void)
8399 spin_lock(&pers_lock);
8400 md_cluster_ops = NULL;
8401 spin_unlock(&pers_lock);
8404 EXPORT_SYMBOL(unregister_md_cluster_operations);
8406 int md_setup_cluster(struct mddev *mddev, int nodes)
8409 if (!md_cluster_ops)
8410 request_module("md-cluster");
8411 spin_lock(&pers_lock);
8412 /* ensure module won't be unloaded */
8413 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
8414 pr_warn("can't find md-cluster module or get its reference.\n");
8415 spin_unlock(&pers_lock);
8418 spin_unlock(&pers_lock);
8420 ret = md_cluster_ops->join(mddev, nodes);
8422 mddev->safemode_delay = 0;
8426 void md_cluster_stop(struct mddev *mddev)
8428 if (!md_cluster_ops)
8430 md_cluster_ops->leave(mddev);
8431 module_put(md_cluster_mod);
8434 static int is_mddev_idle(struct mddev *mddev, int init)
8436 struct md_rdev *rdev;
8442 rdev_for_each_rcu(rdev, mddev) {
8443 struct gendisk *disk = rdev->bdev->bd_disk;
8444 curr_events = (int)part_stat_read_accum(disk->part0, sectors) -
8445 atomic_read(&disk->sync_io);
8446 /* sync IO will cause sync_io to increase before the disk_stats
8447 * as sync_io is counted when a request starts, and
8448 * disk_stats is counted when it completes.
8449 * So resync activity will cause curr_events to be smaller than
8450 * when there was no such activity.
8451 * non-sync IO will cause disk_stat to increase without
8452 * increasing sync_io so curr_events will (eventually)
8453 * be larger than it was before. Once it becomes
8454 * substantially larger, the test below will cause
8455 * the array to appear non-idle, and resync will slow
8457 * If there is a lot of outstanding resync activity when
8458 * we set last_event to curr_events, then all that activity
8459 * completing might cause the array to appear non-idle
8460 * and resync will be slowed down even though there might
8461 * not have been non-resync activity. This will only
8462 * happen once though. 'last_events' will soon reflect
8463 * the state where there is little or no outstanding
8464 * resync requests, and further resync activity will
8465 * always make curr_events less than last_events.
8468 if (init || curr_events - rdev->last_events > 64) {
8469 rdev->last_events = curr_events;
8477 void md_done_sync(struct mddev *mddev, int blocks, int ok)
8479 /* another "blocks" (512byte) blocks have been synced */
8480 atomic_sub(blocks, &mddev->recovery_active);
8481 wake_up(&mddev->recovery_wait);
8483 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
8484 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
8485 md_wakeup_thread(mddev->thread);
8486 // stop recovery, signal do_sync ....
8489 EXPORT_SYMBOL(md_done_sync);
8491 /* md_write_start(mddev, bi)
8492 * If we need to update some array metadata (e.g. 'active' flag
8493 * in superblock) before writing, schedule a superblock update
8494 * and wait for it to complete.
8495 * A return value of 'false' means that the write wasn't recorded
8496 * and cannot proceed as the array is being suspend.
8498 bool md_write_start(struct mddev *mddev, struct bio *bi)
8502 if (bio_data_dir(bi) != WRITE)
8505 BUG_ON(mddev->ro == 1);
8506 if (mddev->ro == 2) {
8507 /* need to switch to read/write */
8509 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8510 md_wakeup_thread(mddev->thread);
8511 md_wakeup_thread(mddev->sync_thread);
8515 percpu_ref_get(&mddev->writes_pending);
8516 smp_mb(); /* Match smp_mb in set_in_sync() */
8517 if (mddev->safemode == 1)
8518 mddev->safemode = 0;
8519 /* sync_checkers is always 0 when writes_pending is in per-cpu mode */
8520 if (mddev->in_sync || mddev->sync_checkers) {
8521 spin_lock(&mddev->lock);
8522 if (mddev->in_sync) {
8524 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8525 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
8526 md_wakeup_thread(mddev->thread);
8529 spin_unlock(&mddev->lock);
8533 sysfs_notify_dirent_safe(mddev->sysfs_state);
8534 if (!mddev->has_superblocks)
8536 wait_event(mddev->sb_wait,
8537 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags) ||
8539 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
8540 percpu_ref_put(&mddev->writes_pending);
8545 EXPORT_SYMBOL(md_write_start);
8547 /* md_write_inc can only be called when md_write_start() has
8548 * already been called at least once of the current request.
8549 * It increments the counter and is useful when a single request
8550 * is split into several parts. Each part causes an increment and
8551 * so needs a matching md_write_end().
8552 * Unlike md_write_start(), it is safe to call md_write_inc() inside
8553 * a spinlocked region.
8555 void md_write_inc(struct mddev *mddev, struct bio *bi)
8557 if (bio_data_dir(bi) != WRITE)
8559 WARN_ON_ONCE(mddev->in_sync || mddev->ro);
8560 percpu_ref_get(&mddev->writes_pending);
8562 EXPORT_SYMBOL(md_write_inc);
8564 void md_write_end(struct mddev *mddev)
8566 percpu_ref_put(&mddev->writes_pending);
8568 if (mddev->safemode == 2)
8569 md_wakeup_thread(mddev->thread);
8570 else if (mddev->safemode_delay)
8571 /* The roundup() ensures this only performs locking once
8572 * every ->safemode_delay jiffies
8574 mod_timer(&mddev->safemode_timer,
8575 roundup(jiffies, mddev->safemode_delay) +
8576 mddev->safemode_delay);
8579 EXPORT_SYMBOL(md_write_end);
8581 /* This is used by raid0 and raid10 */
8582 void md_submit_discard_bio(struct mddev *mddev, struct md_rdev *rdev,
8583 struct bio *bio, sector_t start, sector_t size)
8585 struct bio *discard_bio = NULL;
8587 if (__blkdev_issue_discard(rdev->bdev, start, size, GFP_NOIO,
8588 &discard_bio) || !discard_bio)
8591 bio_chain(discard_bio, bio);
8592 bio_clone_blkg_association(discard_bio, bio);
8594 trace_block_bio_remap(discard_bio,
8595 disk_devt(mddev->gendisk),
8596 bio->bi_iter.bi_sector);
8597 submit_bio_noacct(discard_bio);
8599 EXPORT_SYMBOL_GPL(md_submit_discard_bio);
8601 int acct_bioset_init(struct mddev *mddev)
8605 if (!bioset_initialized(&mddev->io_acct_set))
8606 err = bioset_init(&mddev->io_acct_set, BIO_POOL_SIZE,
8607 offsetof(struct md_io_acct, bio_clone), 0);
8610 EXPORT_SYMBOL_GPL(acct_bioset_init);
8612 void acct_bioset_exit(struct mddev *mddev)
8614 bioset_exit(&mddev->io_acct_set);
8616 EXPORT_SYMBOL_GPL(acct_bioset_exit);
8618 static void md_end_io_acct(struct bio *bio)
8620 struct md_io_acct *md_io_acct = bio->bi_private;
8621 struct bio *orig_bio = md_io_acct->orig_bio;
8623 orig_bio->bi_status = bio->bi_status;
8625 bio_end_io_acct(orig_bio, md_io_acct->start_time);
8627 bio_endio(orig_bio);
8631 * Used by personalities that don't already clone the bio and thus can't
8632 * easily add the timestamp to their extended bio structure.
8634 void md_account_bio(struct mddev *mddev, struct bio **bio)
8636 struct block_device *bdev = (*bio)->bi_bdev;
8637 struct md_io_acct *md_io_acct;
8640 if (!blk_queue_io_stat(bdev->bd_disk->queue))
8643 clone = bio_alloc_clone(bdev, *bio, GFP_NOIO, &mddev->io_acct_set);
8644 md_io_acct = container_of(clone, struct md_io_acct, bio_clone);
8645 md_io_acct->orig_bio = *bio;
8646 md_io_acct->start_time = bio_start_io_acct(*bio);
8648 clone->bi_end_io = md_end_io_acct;
8649 clone->bi_private = md_io_acct;
8652 EXPORT_SYMBOL_GPL(md_account_bio);
8654 /* md_allow_write(mddev)
8655 * Calling this ensures that the array is marked 'active' so that writes
8656 * may proceed without blocking. It is important to call this before
8657 * attempting a GFP_KERNEL allocation while holding the mddev lock.
8658 * Must be called with mddev_lock held.
8660 void md_allow_write(struct mddev *mddev)
8666 if (!mddev->pers->sync_request)
8669 spin_lock(&mddev->lock);
8670 if (mddev->in_sync) {
8672 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8673 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
8674 if (mddev->safemode_delay &&
8675 mddev->safemode == 0)
8676 mddev->safemode = 1;
8677 spin_unlock(&mddev->lock);
8678 md_update_sb(mddev, 0);
8679 sysfs_notify_dirent_safe(mddev->sysfs_state);
8680 /* wait for the dirty state to be recorded in the metadata */
8681 wait_event(mddev->sb_wait,
8682 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
8684 spin_unlock(&mddev->lock);
8686 EXPORT_SYMBOL_GPL(md_allow_write);
8688 #define SYNC_MARKS 10
8689 #define SYNC_MARK_STEP (3*HZ)
8690 #define UPDATE_FREQUENCY (5*60*HZ)
8691 void md_do_sync(struct md_thread *thread)
8693 struct mddev *mddev = thread->mddev;
8694 struct mddev *mddev2;
8695 unsigned int currspeed = 0, window;
8696 sector_t max_sectors,j, io_sectors, recovery_done;
8697 unsigned long mark[SYNC_MARKS];
8698 unsigned long update_time;
8699 sector_t mark_cnt[SYNC_MARKS];
8701 sector_t last_check;
8703 struct md_rdev *rdev;
8704 char *desc, *action = NULL;
8705 struct blk_plug plug;
8708 /* just incase thread restarts... */
8709 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
8710 test_bit(MD_RECOVERY_WAIT, &mddev->recovery))
8712 if (mddev->ro) {/* never try to sync a read-only array */
8713 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
8717 if (mddev_is_clustered(mddev)) {
8718 ret = md_cluster_ops->resync_start(mddev);
8722 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
8723 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8724 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
8725 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
8726 && ((unsigned long long)mddev->curr_resync_completed
8727 < (unsigned long long)mddev->resync_max_sectors))
8731 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8732 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
8733 desc = "data-check";
8735 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8736 desc = "requested-resync";
8740 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
8745 mddev->last_sync_action = action ?: desc;
8748 * Before starting a resync we must have set curr_resync to
8749 * 2, and then checked that every "conflicting" array has curr_resync
8750 * less than ours. When we find one that is the same or higher
8751 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8752 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8753 * This will mean we have to start checking from the beginning again.
8758 int mddev2_minor = -1;
8759 mddev->curr_resync = MD_RESYNC_DELAYED;
8762 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8764 spin_lock(&all_mddevs_lock);
8765 list_for_each_entry(mddev2, &all_mddevs, all_mddevs) {
8766 if (test_bit(MD_DELETED, &mddev2->flags))
8768 if (mddev2 == mddev)
8770 if (!mddev->parallel_resync
8771 && mddev2->curr_resync
8772 && match_mddev_units(mddev, mddev2)) {
8774 if (mddev < mddev2 &&
8775 mddev->curr_resync == MD_RESYNC_DELAYED) {
8776 /* arbitrarily yield */
8777 mddev->curr_resync = MD_RESYNC_YIELDED;
8778 wake_up(&resync_wait);
8780 if (mddev > mddev2 &&
8781 mddev->curr_resync == MD_RESYNC_YIELDED)
8782 /* no need to wait here, we can wait the next
8783 * time 'round when curr_resync == 2
8786 /* We need to wait 'interruptible' so as not to
8787 * contribute to the load average, and not to
8788 * be caught by 'softlockup'
8790 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
8791 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8792 mddev2->curr_resync >= mddev->curr_resync) {
8793 if (mddev2_minor != mddev2->md_minor) {
8794 mddev2_minor = mddev2->md_minor;
8795 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8796 desc, mdname(mddev),
8799 spin_unlock(&all_mddevs_lock);
8801 if (signal_pending(current))
8802 flush_signals(current);
8804 finish_wait(&resync_wait, &wq);
8807 finish_wait(&resync_wait, &wq);
8810 spin_unlock(&all_mddevs_lock);
8811 } while (mddev->curr_resync < MD_RESYNC_DELAYED);
8814 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8815 /* resync follows the size requested by the personality,
8816 * which defaults to physical size, but can be virtual size
8818 max_sectors = mddev->resync_max_sectors;
8819 atomic64_set(&mddev->resync_mismatches, 0);
8820 /* we don't use the checkpoint if there's a bitmap */
8821 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8822 j = mddev->resync_min;
8823 else if (!mddev->bitmap)
8824 j = mddev->recovery_cp;
8826 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
8827 max_sectors = mddev->resync_max_sectors;
8829 * If the original node aborts reshaping then we continue the
8830 * reshaping, so set j again to avoid restart reshape from the
8833 if (mddev_is_clustered(mddev) &&
8834 mddev->reshape_position != MaxSector)
8835 j = mddev->reshape_position;
8837 /* recovery follows the physical size of devices */
8838 max_sectors = mddev->dev_sectors;
8841 rdev_for_each_rcu(rdev, mddev)
8842 if (rdev->raid_disk >= 0 &&
8843 !test_bit(Journal, &rdev->flags) &&
8844 !test_bit(Faulty, &rdev->flags) &&
8845 !test_bit(In_sync, &rdev->flags) &&
8846 rdev->recovery_offset < j)
8847 j = rdev->recovery_offset;
8850 /* If there is a bitmap, we need to make sure all
8851 * writes that started before we added a spare
8852 * complete before we start doing a recovery.
8853 * Otherwise the write might complete and (via
8854 * bitmap_endwrite) set a bit in the bitmap after the
8855 * recovery has checked that bit and skipped that
8858 if (mddev->bitmap) {
8859 mddev->pers->quiesce(mddev, 1);
8860 mddev->pers->quiesce(mddev, 0);
8864 pr_info("md: %s of RAID array %s\n", desc, mdname(mddev));
8865 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev));
8866 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8867 speed_max(mddev), desc);
8869 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
8872 for (m = 0; m < SYNC_MARKS; m++) {
8874 mark_cnt[m] = io_sectors;
8877 mddev->resync_mark = mark[last_mark];
8878 mddev->resync_mark_cnt = mark_cnt[last_mark];
8881 * Tune reconstruction:
8883 window = 32 * (PAGE_SIZE / 512);
8884 pr_debug("md: using %dk window, over a total of %lluk.\n",
8885 window/2, (unsigned long long)max_sectors/2);
8887 atomic_set(&mddev->recovery_active, 0);
8891 pr_debug("md: resuming %s of %s from checkpoint.\n",
8892 desc, mdname(mddev));
8893 mddev->curr_resync = j;
8895 mddev->curr_resync = MD_RESYNC_ACTIVE; /* no longer delayed */
8896 mddev->curr_resync_completed = j;
8897 sysfs_notify_dirent_safe(mddev->sysfs_completed);
8899 update_time = jiffies;
8901 blk_start_plug(&plug);
8902 while (j < max_sectors) {
8907 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8908 ((mddev->curr_resync > mddev->curr_resync_completed &&
8909 (mddev->curr_resync - mddev->curr_resync_completed)
8910 > (max_sectors >> 4)) ||
8911 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
8912 (j - mddev->curr_resync_completed)*2
8913 >= mddev->resync_max - mddev->curr_resync_completed ||
8914 mddev->curr_resync_completed > mddev->resync_max
8916 /* time to update curr_resync_completed */
8917 wait_event(mddev->recovery_wait,
8918 atomic_read(&mddev->recovery_active) == 0);
8919 mddev->curr_resync_completed = j;
8920 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8921 j > mddev->recovery_cp)
8922 mddev->recovery_cp = j;
8923 update_time = jiffies;
8924 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8925 sysfs_notify_dirent_safe(mddev->sysfs_completed);
8928 while (j >= mddev->resync_max &&
8929 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8930 /* As this condition is controlled by user-space,
8931 * we can block indefinitely, so use '_interruptible'
8932 * to avoid triggering warnings.
8934 flush_signals(current); /* just in case */
8935 wait_event_interruptible(mddev->recovery_wait,
8936 mddev->resync_max > j
8937 || test_bit(MD_RECOVERY_INTR,
8941 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8944 sectors = mddev->pers->sync_request(mddev, j, &skipped);
8946 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
8950 if (!skipped) { /* actual IO requested */
8951 io_sectors += sectors;
8952 atomic_add(sectors, &mddev->recovery_active);
8955 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8959 if (j > max_sectors)
8960 /* when skipping, extra large numbers can be returned. */
8963 mddev->curr_resync = j;
8964 mddev->curr_mark_cnt = io_sectors;
8965 if (last_check == 0)
8966 /* this is the earliest that rebuild will be
8967 * visible in /proc/mdstat
8971 if (last_check + window > io_sectors || j == max_sectors)
8974 last_check = io_sectors;
8976 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8978 int next = (last_mark+1) % SYNC_MARKS;
8980 mddev->resync_mark = mark[next];
8981 mddev->resync_mark_cnt = mark_cnt[next];
8982 mark[next] = jiffies;
8983 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
8987 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8991 * this loop exits only if either when we are slower than
8992 * the 'hard' speed limit, or the system was IO-idle for
8994 * the system might be non-idle CPU-wise, but we only care
8995 * about not overloading the IO subsystem. (things like an
8996 * e2fsck being done on the RAID array should execute fast)
9000 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
9001 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
9002 /((jiffies-mddev->resync_mark)/HZ +1) +1;
9004 if (currspeed > speed_min(mddev)) {
9005 if (currspeed > speed_max(mddev)) {
9009 if (!is_mddev_idle(mddev, 0)) {
9011 * Give other IO more of a chance.
9012 * The faster the devices, the less we wait.
9014 wait_event(mddev->recovery_wait,
9015 !atomic_read(&mddev->recovery_active));
9019 pr_info("md: %s: %s %s.\n",mdname(mddev), desc,
9020 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
9021 ? "interrupted" : "done");
9023 * this also signals 'finished resyncing' to md_stop
9025 blk_finish_plug(&plug);
9026 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
9028 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
9029 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
9030 mddev->curr_resync >= MD_RESYNC_ACTIVE) {
9031 mddev->curr_resync_completed = mddev->curr_resync;
9032 sysfs_notify_dirent_safe(mddev->sysfs_completed);
9034 mddev->pers->sync_request(mddev, max_sectors, &skipped);
9036 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
9037 mddev->curr_resync >= MD_RESYNC_ACTIVE) {
9038 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
9039 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
9040 if (mddev->curr_resync >= mddev->recovery_cp) {
9041 pr_debug("md: checkpointing %s of %s.\n",
9042 desc, mdname(mddev));
9043 if (test_bit(MD_RECOVERY_ERROR,
9045 mddev->recovery_cp =
9046 mddev->curr_resync_completed;
9048 mddev->recovery_cp =
9052 mddev->recovery_cp = MaxSector;
9054 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
9055 mddev->curr_resync = MaxSector;
9056 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
9057 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) {
9059 rdev_for_each_rcu(rdev, mddev)
9060 if (rdev->raid_disk >= 0 &&
9061 mddev->delta_disks >= 0 &&
9062 !test_bit(Journal, &rdev->flags) &&
9063 !test_bit(Faulty, &rdev->flags) &&
9064 !test_bit(In_sync, &rdev->flags) &&
9065 rdev->recovery_offset < mddev->curr_resync)
9066 rdev->recovery_offset = mddev->curr_resync;
9072 /* set CHANGE_PENDING here since maybe another update is needed,
9073 * so other nodes are informed. It should be harmless for normal
9075 set_mask_bits(&mddev->sb_flags, 0,
9076 BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS));
9078 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
9079 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
9080 mddev->delta_disks > 0 &&
9081 mddev->pers->finish_reshape &&
9082 mddev->pers->size &&
9084 mddev_lock_nointr(mddev);
9085 md_set_array_sectors(mddev, mddev->pers->size(mddev, 0, 0));
9086 mddev_unlock(mddev);
9087 if (!mddev_is_clustered(mddev))
9088 set_capacity_and_notify(mddev->gendisk,
9089 mddev->array_sectors);
9092 spin_lock(&mddev->lock);
9093 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
9094 /* We completed so min/max setting can be forgotten if used. */
9095 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
9096 mddev->resync_min = 0;
9097 mddev->resync_max = MaxSector;
9098 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
9099 mddev->resync_min = mddev->curr_resync_completed;
9100 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
9101 mddev->curr_resync = MD_RESYNC_NONE;
9102 spin_unlock(&mddev->lock);
9104 wake_up(&resync_wait);
9105 md_wakeup_thread(mddev->thread);
9108 EXPORT_SYMBOL_GPL(md_do_sync);
9110 static int remove_and_add_spares(struct mddev *mddev,
9111 struct md_rdev *this)
9113 struct md_rdev *rdev;
9116 bool remove_some = false;
9118 if (this && test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
9119 /* Mustn't remove devices when resync thread is running */
9122 rdev_for_each(rdev, mddev) {
9123 if ((this == NULL || rdev == this) &&
9124 rdev->raid_disk >= 0 &&
9125 !test_bit(Blocked, &rdev->flags) &&
9126 test_bit(Faulty, &rdev->flags) &&
9127 atomic_read(&rdev->nr_pending)==0) {
9128 /* Faulty non-Blocked devices with nr_pending == 0
9129 * never get nr_pending incremented,
9130 * never get Faulty cleared, and never get Blocked set.
9131 * So we can synchronize_rcu now rather than once per device
9134 set_bit(RemoveSynchronized, &rdev->flags);
9140 rdev_for_each(rdev, mddev) {
9141 if ((this == NULL || rdev == this) &&
9142 rdev->raid_disk >= 0 &&
9143 !test_bit(Blocked, &rdev->flags) &&
9144 ((test_bit(RemoveSynchronized, &rdev->flags) ||
9145 (!test_bit(In_sync, &rdev->flags) &&
9146 !test_bit(Journal, &rdev->flags))) &&
9147 atomic_read(&rdev->nr_pending)==0)) {
9148 if (mddev->pers->hot_remove_disk(
9149 mddev, rdev) == 0) {
9150 sysfs_unlink_rdev(mddev, rdev);
9151 rdev->saved_raid_disk = rdev->raid_disk;
9152 rdev->raid_disk = -1;
9156 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
9157 clear_bit(RemoveSynchronized, &rdev->flags);
9160 if (removed && mddev->kobj.sd)
9161 sysfs_notify_dirent_safe(mddev->sysfs_degraded);
9163 if (this && removed)
9166 rdev_for_each(rdev, mddev) {
9167 if (this && this != rdev)
9169 if (test_bit(Candidate, &rdev->flags))
9171 if (rdev->raid_disk >= 0 &&
9172 !test_bit(In_sync, &rdev->flags) &&
9173 !test_bit(Journal, &rdev->flags) &&
9174 !test_bit(Faulty, &rdev->flags))
9176 if (rdev->raid_disk >= 0)
9178 if (test_bit(Faulty, &rdev->flags))
9180 if (!test_bit(Journal, &rdev->flags)) {
9182 ! (rdev->saved_raid_disk >= 0 &&
9183 !test_bit(Bitmap_sync, &rdev->flags)))
9186 rdev->recovery_offset = 0;
9188 if (mddev->pers->hot_add_disk(mddev, rdev) == 0) {
9189 /* failure here is OK */
9190 sysfs_link_rdev(mddev, rdev);
9191 if (!test_bit(Journal, &rdev->flags))
9194 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
9199 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
9203 static void md_start_sync(struct work_struct *ws)
9205 struct mddev *mddev = container_of(ws, struct mddev, del_work);
9207 mddev->sync_thread = md_register_thread(md_do_sync,
9210 if (!mddev->sync_thread) {
9211 pr_warn("%s: could not start resync thread...\n",
9213 /* leave the spares where they are, it shouldn't hurt */
9214 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9215 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
9216 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
9217 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
9218 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
9219 wake_up(&resync_wait);
9220 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
9222 if (mddev->sysfs_action)
9223 sysfs_notify_dirent_safe(mddev->sysfs_action);
9225 md_wakeup_thread(mddev->sync_thread);
9226 sysfs_notify_dirent_safe(mddev->sysfs_action);
9231 * This routine is regularly called by all per-raid-array threads to
9232 * deal with generic issues like resync and super-block update.
9233 * Raid personalities that don't have a thread (linear/raid0) do not
9234 * need this as they never do any recovery or update the superblock.
9236 * It does not do any resync itself, but rather "forks" off other threads
9237 * to do that as needed.
9238 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
9239 * "->recovery" and create a thread at ->sync_thread.
9240 * When the thread finishes it sets MD_RECOVERY_DONE
9241 * and wakeups up this thread which will reap the thread and finish up.
9242 * This thread also removes any faulty devices (with nr_pending == 0).
9244 * The overall approach is:
9245 * 1/ if the superblock needs updating, update it.
9246 * 2/ If a recovery thread is running, don't do anything else.
9247 * 3/ If recovery has finished, clean up, possibly marking spares active.
9248 * 4/ If there are any faulty devices, remove them.
9249 * 5/ If array is degraded, try to add spares devices
9250 * 6/ If array has spares or is not in-sync, start a resync thread.
9252 void md_check_recovery(struct mddev *mddev)
9254 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags) && mddev->sb_flags) {
9255 /* Write superblock - thread that called mddev_suspend()
9256 * holds reconfig_mutex for us.
9258 set_bit(MD_UPDATING_SB, &mddev->flags);
9259 smp_mb__after_atomic();
9260 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags))
9261 md_update_sb(mddev, 0);
9262 clear_bit_unlock(MD_UPDATING_SB, &mddev->flags);
9263 wake_up(&mddev->sb_wait);
9266 if (mddev->suspended)
9270 md_bitmap_daemon_work(mddev);
9272 if (signal_pending(current)) {
9273 if (mddev->pers->sync_request && !mddev->external) {
9274 pr_debug("md: %s in immediate safe mode\n",
9276 mddev->safemode = 2;
9278 flush_signals(current);
9281 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
9284 (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
9285 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
9286 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
9287 (mddev->external == 0 && mddev->safemode == 1) ||
9288 (mddev->safemode == 2
9289 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
9293 if (mddev_trylock(mddev)) {
9295 bool try_set_sync = mddev->safemode != 0;
9297 if (!mddev->external && mddev->safemode == 1)
9298 mddev->safemode = 0;
9301 struct md_rdev *rdev;
9302 if (!mddev->external && mddev->in_sync)
9303 /* 'Blocked' flag not needed as failed devices
9304 * will be recorded if array switched to read/write.
9305 * Leaving it set will prevent the device
9306 * from being removed.
9308 rdev_for_each(rdev, mddev)
9309 clear_bit(Blocked, &rdev->flags);
9310 /* On a read-only array we can:
9311 * - remove failed devices
9312 * - add already-in_sync devices if the array itself
9314 * As we only add devices that are already in-sync,
9315 * we can activate the spares immediately.
9317 remove_and_add_spares(mddev, NULL);
9318 /* There is no thread, but we need to call
9319 * ->spare_active and clear saved_raid_disk
9321 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
9322 md_unregister_thread(&mddev->sync_thread);
9323 md_reap_sync_thread(mddev);
9324 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
9325 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9326 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
9330 if (mddev_is_clustered(mddev)) {
9331 struct md_rdev *rdev, *tmp;
9332 /* kick the device if another node issued a
9335 rdev_for_each_safe(rdev, tmp, mddev) {
9336 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
9337 rdev->raid_disk < 0)
9338 md_kick_rdev_from_array(rdev);
9342 if (try_set_sync && !mddev->external && !mddev->in_sync) {
9343 spin_lock(&mddev->lock);
9345 spin_unlock(&mddev->lock);
9348 if (mddev->sb_flags)
9349 md_update_sb(mddev, 0);
9351 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
9352 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
9353 /* resync/recovery still happening */
9354 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9357 if (mddev->sync_thread) {
9358 md_unregister_thread(&mddev->sync_thread);
9359 md_reap_sync_thread(mddev);
9362 /* Set RUNNING before clearing NEEDED to avoid
9363 * any transients in the value of "sync_action".
9365 mddev->curr_resync_completed = 0;
9366 spin_lock(&mddev->lock);
9367 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
9368 spin_unlock(&mddev->lock);
9369 /* Clear some bits that don't mean anything, but
9372 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
9373 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
9375 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
9376 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
9378 /* no recovery is running.
9379 * remove any failed drives, then
9380 * add spares if possible.
9381 * Spares are also removed and re-added, to allow
9382 * the personality to fail the re-add.
9385 if (mddev->reshape_position != MaxSector) {
9386 if (mddev->pers->check_reshape == NULL ||
9387 mddev->pers->check_reshape(mddev) != 0)
9388 /* Cannot proceed */
9390 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
9391 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
9392 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
9393 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9394 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
9395 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
9396 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
9397 } else if (mddev->recovery_cp < MaxSector) {
9398 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9399 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
9400 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
9401 /* nothing to be done ... */
9404 if (mddev->pers->sync_request) {
9406 /* We are adding a device or devices to an array
9407 * which has the bitmap stored on all devices.
9408 * So make sure all bitmap pages get written
9410 md_bitmap_write_all(mddev->bitmap);
9412 INIT_WORK(&mddev->del_work, md_start_sync);
9413 queue_work(md_misc_wq, &mddev->del_work);
9417 if (!mddev->sync_thread) {
9418 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
9419 wake_up(&resync_wait);
9420 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
9422 if (mddev->sysfs_action)
9423 sysfs_notify_dirent_safe(mddev->sysfs_action);
9426 wake_up(&mddev->sb_wait);
9427 mddev_unlock(mddev);
9430 EXPORT_SYMBOL(md_check_recovery);
9432 void md_reap_sync_thread(struct mddev *mddev)
9434 struct md_rdev *rdev;
9435 sector_t old_dev_sectors = mddev->dev_sectors;
9436 bool is_reshaped = false;
9438 /* sync_thread should be unregistered, collect result */
9439 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
9440 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
9441 mddev->degraded != mddev->raid_disks) {
9443 /* activate any spares */
9444 if (mddev->pers->spare_active(mddev)) {
9445 sysfs_notify_dirent_safe(mddev->sysfs_degraded);
9446 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
9449 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
9450 mddev->pers->finish_reshape) {
9451 mddev->pers->finish_reshape(mddev);
9452 if (mddev_is_clustered(mddev))
9456 /* If array is no-longer degraded, then any saved_raid_disk
9457 * information must be scrapped.
9459 if (!mddev->degraded)
9460 rdev_for_each(rdev, mddev)
9461 rdev->saved_raid_disk = -1;
9463 md_update_sb(mddev, 1);
9464 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
9465 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
9467 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
9468 md_cluster_ops->resync_finish(mddev);
9469 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
9470 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
9471 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9472 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
9473 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
9474 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
9476 * We call md_cluster_ops->update_size here because sync_size could
9477 * be changed by md_update_sb, and MD_RECOVERY_RESHAPE is cleared,
9478 * so it is time to update size across cluster.
9480 if (mddev_is_clustered(mddev) && is_reshaped
9481 && !test_bit(MD_CLOSING, &mddev->flags))
9482 md_cluster_ops->update_size(mddev, old_dev_sectors);
9483 wake_up(&resync_wait);
9484 /* flag recovery needed just to double check */
9485 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9486 sysfs_notify_dirent_safe(mddev->sysfs_completed);
9487 sysfs_notify_dirent_safe(mddev->sysfs_action);
9489 if (mddev->event_work.func)
9490 queue_work(md_misc_wq, &mddev->event_work);
9492 EXPORT_SYMBOL(md_reap_sync_thread);
9494 void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
9496 sysfs_notify_dirent_safe(rdev->sysfs_state);
9497 wait_event_timeout(rdev->blocked_wait,
9498 !test_bit(Blocked, &rdev->flags) &&
9499 !test_bit(BlockedBadBlocks, &rdev->flags),
9500 msecs_to_jiffies(5000));
9501 rdev_dec_pending(rdev, mddev);
9503 EXPORT_SYMBOL(md_wait_for_blocked_rdev);
9505 void md_finish_reshape(struct mddev *mddev)
9507 /* called be personality module when reshape completes. */
9508 struct md_rdev *rdev;
9510 rdev_for_each(rdev, mddev) {
9511 if (rdev->data_offset > rdev->new_data_offset)
9512 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
9514 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
9515 rdev->data_offset = rdev->new_data_offset;
9518 EXPORT_SYMBOL(md_finish_reshape);
9520 /* Bad block management */
9522 /* Returns 1 on success, 0 on failure */
9523 int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
9526 struct mddev *mddev = rdev->mddev;
9529 s += rdev->new_data_offset;
9531 s += rdev->data_offset;
9532 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
9534 /* Make sure they get written out promptly */
9535 if (test_bit(ExternalBbl, &rdev->flags))
9536 sysfs_notify_dirent_safe(rdev->sysfs_unack_badblocks);
9537 sysfs_notify_dirent_safe(rdev->sysfs_state);
9538 set_mask_bits(&mddev->sb_flags, 0,
9539 BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING));
9540 md_wakeup_thread(rdev->mddev->thread);
9545 EXPORT_SYMBOL_GPL(rdev_set_badblocks);
9547 int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
9552 s += rdev->new_data_offset;
9554 s += rdev->data_offset;
9555 rv = badblocks_clear(&rdev->badblocks, s, sectors);
9556 if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags))
9557 sysfs_notify_dirent_safe(rdev->sysfs_badblocks);
9560 EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
9562 static int md_notify_reboot(struct notifier_block *this,
9563 unsigned long code, void *x)
9565 struct mddev *mddev, *n;
9568 spin_lock(&all_mddevs_lock);
9569 list_for_each_entry_safe(mddev, n, &all_mddevs, all_mddevs) {
9570 if (!mddev_get(mddev))
9572 spin_unlock(&all_mddevs_lock);
9573 if (mddev_trylock(mddev)) {
9575 __md_stop_writes(mddev);
9576 if (mddev->persistent)
9577 mddev->safemode = 2;
9578 mddev_unlock(mddev);
9582 spin_lock(&all_mddevs_lock);
9584 spin_unlock(&all_mddevs_lock);
9587 * certain more exotic SCSI devices are known to be
9588 * volatile wrt too early system reboots. While the
9589 * right place to handle this issue is the given
9590 * driver, we do want to have a safe RAID driver ...
9598 static struct notifier_block md_notifier = {
9599 .notifier_call = md_notify_reboot,
9601 .priority = INT_MAX, /* before any real devices */
9604 static void md_geninit(void)
9606 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
9608 proc_create("mdstat", S_IRUGO, NULL, &mdstat_proc_ops);
9611 static int __init md_init(void)
9615 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
9619 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
9623 md_rdev_misc_wq = alloc_workqueue("md_rdev_misc", 0, 0);
9624 if (!md_rdev_misc_wq)
9625 goto err_rdev_misc_wq;
9627 ret = __register_blkdev(MD_MAJOR, "md", md_probe);
9631 ret = __register_blkdev(0, "mdp", md_probe);
9636 register_reboot_notifier(&md_notifier);
9637 raid_table_header = register_sysctl_table(raid_root_table);
9643 unregister_blkdev(MD_MAJOR, "md");
9645 destroy_workqueue(md_rdev_misc_wq);
9647 destroy_workqueue(md_misc_wq);
9649 destroy_workqueue(md_wq);
9654 static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
9656 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
9657 struct md_rdev *rdev2, *tmp;
9661 * If size is changed in another node then we need to
9662 * do resize as well.
9664 if (mddev->dev_sectors != le64_to_cpu(sb->size)) {
9665 ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size));
9667 pr_info("md-cluster: resize failed\n");
9669 md_bitmap_update_sb(mddev->bitmap);
9672 /* Check for change of roles in the active devices */
9673 rdev_for_each_safe(rdev2, tmp, mddev) {
9674 if (test_bit(Faulty, &rdev2->flags))
9677 /* Check if the roles changed */
9678 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
9680 if (test_bit(Candidate, &rdev2->flags)) {
9681 if (role == MD_DISK_ROLE_FAULTY) {
9682 pr_info("md: Removing Candidate device %pg because add failed\n",
9684 md_kick_rdev_from_array(rdev2);
9688 clear_bit(Candidate, &rdev2->flags);
9691 if (role != rdev2->raid_disk) {
9693 * got activated except reshape is happening.
9695 if (rdev2->raid_disk == -1 && role != MD_DISK_ROLE_SPARE &&
9696 !(le32_to_cpu(sb->feature_map) &
9697 MD_FEATURE_RESHAPE_ACTIVE)) {
9698 rdev2->saved_raid_disk = role;
9699 ret = remove_and_add_spares(mddev, rdev2);
9700 pr_info("Activated spare: %pg\n",
9702 /* wakeup mddev->thread here, so array could
9703 * perform resync with the new activated disk */
9704 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9705 md_wakeup_thread(mddev->thread);
9708 * We just want to do the minimum to mark the disk
9709 * as faulty. The recovery is performed by the
9710 * one who initiated the error.
9712 if (role == MD_DISK_ROLE_FAULTY ||
9713 role == MD_DISK_ROLE_JOURNAL) {
9714 md_error(mddev, rdev2);
9715 clear_bit(Blocked, &rdev2->flags);
9720 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) {
9721 ret = update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
9723 pr_warn("md: updating array disks failed. %d\n", ret);
9727 * Since mddev->delta_disks has already updated in update_raid_disks,
9728 * so it is time to check reshape.
9730 if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
9731 (le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
9733 * reshape is happening in the remote node, we need to
9734 * update reshape_position and call start_reshape.
9736 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
9737 if (mddev->pers->update_reshape_pos)
9738 mddev->pers->update_reshape_pos(mddev);
9739 if (mddev->pers->start_reshape)
9740 mddev->pers->start_reshape(mddev);
9741 } else if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
9742 mddev->reshape_position != MaxSector &&
9743 !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
9744 /* reshape is just done in another node. */
9745 mddev->reshape_position = MaxSector;
9746 if (mddev->pers->update_reshape_pos)
9747 mddev->pers->update_reshape_pos(mddev);
9750 /* Finally set the event to be up to date */
9751 mddev->events = le64_to_cpu(sb->events);
9754 static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
9757 struct page *swapout = rdev->sb_page;
9758 struct mdp_superblock_1 *sb;
9760 /* Store the sb page of the rdev in the swapout temporary
9761 * variable in case we err in the future
9763 rdev->sb_page = NULL;
9764 err = alloc_disk_sb(rdev);
9766 ClearPageUptodate(rdev->sb_page);
9767 rdev->sb_loaded = 0;
9768 err = super_types[mddev->major_version].
9769 load_super(rdev, NULL, mddev->minor_version);
9772 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9773 __func__, __LINE__, rdev->desc_nr, err);
9775 put_page(rdev->sb_page);
9776 rdev->sb_page = swapout;
9777 rdev->sb_loaded = 1;
9781 sb = page_address(rdev->sb_page);
9782 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9786 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
9787 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
9789 /* The other node finished recovery, call spare_active to set
9790 * device In_sync and mddev->degraded
9792 if (rdev->recovery_offset == MaxSector &&
9793 !test_bit(In_sync, &rdev->flags) &&
9794 mddev->pers->spare_active(mddev))
9795 sysfs_notify_dirent_safe(mddev->sysfs_degraded);
9801 void md_reload_sb(struct mddev *mddev, int nr)
9803 struct md_rdev *rdev = NULL, *iter;
9807 rdev_for_each_rcu(iter, mddev) {
9808 if (iter->desc_nr == nr) {
9815 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
9819 err = read_rdev(mddev, rdev);
9823 check_sb_changes(mddev, rdev);
9825 /* Read all rdev's to update recovery_offset */
9826 rdev_for_each_rcu(rdev, mddev) {
9827 if (!test_bit(Faulty, &rdev->flags))
9828 read_rdev(mddev, rdev);
9831 EXPORT_SYMBOL(md_reload_sb);
9836 * Searches all registered partitions for autorun RAID arrays
9840 static DEFINE_MUTEX(detected_devices_mutex);
9841 static LIST_HEAD(all_detected_devices);
9842 struct detected_devices_node {
9843 struct list_head list;
9847 void md_autodetect_dev(dev_t dev)
9849 struct detected_devices_node *node_detected_dev;
9851 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
9852 if (node_detected_dev) {
9853 node_detected_dev->dev = dev;
9854 mutex_lock(&detected_devices_mutex);
9855 list_add_tail(&node_detected_dev->list, &all_detected_devices);
9856 mutex_unlock(&detected_devices_mutex);
9860 void md_autostart_arrays(int part)
9862 struct md_rdev *rdev;
9863 struct detected_devices_node *node_detected_dev;
9865 int i_scanned, i_passed;
9870 pr_info("md: Autodetecting RAID arrays.\n");
9872 mutex_lock(&detected_devices_mutex);
9873 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
9875 node_detected_dev = list_entry(all_detected_devices.next,
9876 struct detected_devices_node, list);
9877 list_del(&node_detected_dev->list);
9878 dev = node_detected_dev->dev;
9879 kfree(node_detected_dev);
9880 mutex_unlock(&detected_devices_mutex);
9881 rdev = md_import_device(dev,0, 90);
9882 mutex_lock(&detected_devices_mutex);
9886 if (test_bit(Faulty, &rdev->flags))
9889 set_bit(AutoDetected, &rdev->flags);
9890 list_add(&rdev->same_set, &pending_raid_disks);
9893 mutex_unlock(&detected_devices_mutex);
9895 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed);
9897 autorun_devices(part);
9900 #endif /* !MODULE */
9902 static __exit void md_exit(void)
9904 struct mddev *mddev, *n;
9907 unregister_blkdev(MD_MAJOR,"md");
9908 unregister_blkdev(mdp_major, "mdp");
9909 unregister_reboot_notifier(&md_notifier);
9910 unregister_sysctl_table(raid_table_header);
9912 /* We cannot unload the modules while some process is
9913 * waiting for us in select() or poll() - wake them up
9916 while (waitqueue_active(&md_event_waiters)) {
9917 /* not safe to leave yet */
9918 wake_up(&md_event_waiters);
9922 remove_proc_entry("mdstat", NULL);
9924 spin_lock(&all_mddevs_lock);
9925 list_for_each_entry_safe(mddev, n, &all_mddevs, all_mddevs) {
9926 if (!mddev_get(mddev))
9928 spin_unlock(&all_mddevs_lock);
9929 export_array(mddev);
9931 mddev->hold_active = 0;
9933 * As the mddev is now fully clear, mddev_put will schedule
9934 * the mddev for destruction by a workqueue, and the
9935 * destroy_workqueue() below will wait for that to complete.
9938 spin_lock(&all_mddevs_lock);
9940 spin_unlock(&all_mddevs_lock);
9942 destroy_workqueue(md_rdev_misc_wq);
9943 destroy_workqueue(md_misc_wq);
9944 destroy_workqueue(md_wq);
9947 subsys_initcall(md_init);
9948 module_exit(md_exit)
9950 static int get_ro(char *buffer, const struct kernel_param *kp)
9952 return sprintf(buffer, "%d\n", start_readonly);
9954 static int set_ro(const char *val, const struct kernel_param *kp)
9956 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
9959 module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9960 module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
9961 module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
9962 module_param(create_on_open, bool, S_IRUSR|S_IWUSR);
9964 MODULE_LICENSE("GPL");
9965 MODULE_DESCRIPTION("MD RAID framework");
9967 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);