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/badblocks.h>
45 #include <linux/sysctl.h>
46 #include <linux/seq_file.h>
48 #include <linux/poll.h>
49 #include <linux/ctype.h>
50 #include <linux/string.h>
51 #include <linux/hdreg.h>
52 #include <linux/proc_fs.h>
53 #include <linux/random.h>
54 #include <linux/module.h>
55 #include <linux/reboot.h>
56 #include <linux/file.h>
57 #include <linux/compat.h>
58 #include <linux/delay.h>
59 #include <linux/raid/md_p.h>
60 #include <linux/raid/md_u.h>
61 #include <linux/raid/detect.h>
62 #include <linux/slab.h>
63 #include <linux/percpu-refcount.h>
64 #include <linux/part_stat.h>
66 #include <trace/events/block.h>
68 #include "md-bitmap.h"
69 #include "md-cluster.h"
71 /* pers_list is a list of registered personalities protected
73 * pers_lock does extra service to protect accesses to
74 * mddev->thread when the mutex cannot be held.
76 static LIST_HEAD(pers_list);
77 static DEFINE_SPINLOCK(pers_lock);
79 static struct kobj_type md_ktype;
81 struct md_cluster_operations *md_cluster_ops;
82 EXPORT_SYMBOL(md_cluster_ops);
83 static struct module *md_cluster_mod;
85 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
86 static struct workqueue_struct *md_wq;
87 static struct workqueue_struct *md_misc_wq;
88 static struct workqueue_struct *md_rdev_misc_wq;
90 static int remove_and_add_spares(struct mddev *mddev,
91 struct md_rdev *this);
92 static void mddev_detach(struct mddev *mddev);
95 * Default number of read corrections we'll attempt on an rdev
96 * before ejecting it from the array. We divide the read error
97 * count by 2 for every hour elapsed between read errors.
99 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
100 /* Default safemode delay: 200 msec */
101 #define DEFAULT_SAFEMODE_DELAY ((200 * HZ)/1000 +1)
103 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
104 * is 1000 KB/sec, so the extra system load does not show up that much.
105 * Increase it if you want to have more _guaranteed_ speed. Note that
106 * the RAID driver will use the maximum available bandwidth if the IO
107 * subsystem is idle. There is also an 'absolute maximum' reconstruction
108 * speed limit - in case reconstruction slows down your system despite
111 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
112 * or /sys/block/mdX/md/sync_speed_{min,max}
115 static int sysctl_speed_limit_min = 1000;
116 static int sysctl_speed_limit_max = 200000;
117 static inline int speed_min(struct mddev *mddev)
119 return mddev->sync_speed_min ?
120 mddev->sync_speed_min : sysctl_speed_limit_min;
123 static inline int speed_max(struct mddev *mddev)
125 return mddev->sync_speed_max ?
126 mddev->sync_speed_max : sysctl_speed_limit_max;
129 static void rdev_uninit_serial(struct md_rdev *rdev)
131 if (!test_and_clear_bit(CollisionCheck, &rdev->flags))
134 kvfree(rdev->serial);
138 static void rdevs_uninit_serial(struct mddev *mddev)
140 struct md_rdev *rdev;
142 rdev_for_each(rdev, mddev)
143 rdev_uninit_serial(rdev);
146 static int rdev_init_serial(struct md_rdev *rdev)
148 /* serial_nums equals with BARRIER_BUCKETS_NR */
149 int i, serial_nums = 1 << ((PAGE_SHIFT - ilog2(sizeof(atomic_t))));
150 struct serial_in_rdev *serial = NULL;
152 if (test_bit(CollisionCheck, &rdev->flags))
155 serial = kvmalloc(sizeof(struct serial_in_rdev) * serial_nums,
160 for (i = 0; i < serial_nums; i++) {
161 struct serial_in_rdev *serial_tmp = &serial[i];
163 spin_lock_init(&serial_tmp->serial_lock);
164 serial_tmp->serial_rb = RB_ROOT_CACHED;
165 init_waitqueue_head(&serial_tmp->serial_io_wait);
168 rdev->serial = serial;
169 set_bit(CollisionCheck, &rdev->flags);
174 static int rdevs_init_serial(struct mddev *mddev)
176 struct md_rdev *rdev;
179 rdev_for_each(rdev, mddev) {
180 ret = rdev_init_serial(rdev);
185 /* Free all resources if pool is not existed */
186 if (ret && !mddev->serial_info_pool)
187 rdevs_uninit_serial(mddev);
193 * rdev needs to enable serial stuffs if it meets the conditions:
194 * 1. it is multi-queue device flaged with writemostly.
195 * 2. the write-behind mode is enabled.
197 static int rdev_need_serial(struct md_rdev *rdev)
199 return (rdev && rdev->mddev->bitmap_info.max_write_behind > 0 &&
200 rdev->bdev->bd_disk->queue->nr_hw_queues != 1 &&
201 test_bit(WriteMostly, &rdev->flags));
205 * Init resource for rdev(s), then create serial_info_pool if:
206 * 1. rdev is the first device which return true from rdev_enable_serial.
207 * 2. rdev is NULL, means we want to enable serialization for all rdevs.
209 void mddev_create_serial_pool(struct mddev *mddev, struct md_rdev *rdev,
214 if (rdev && !rdev_need_serial(rdev) &&
215 !test_bit(CollisionCheck, &rdev->flags))
219 mddev_suspend(mddev);
222 ret = rdevs_init_serial(mddev);
224 ret = rdev_init_serial(rdev);
228 if (mddev->serial_info_pool == NULL) {
230 * already in memalloc noio context by
233 mddev->serial_info_pool =
234 mempool_create_kmalloc_pool(NR_SERIAL_INFOS,
235 sizeof(struct serial_info));
236 if (!mddev->serial_info_pool) {
237 rdevs_uninit_serial(mddev);
238 pr_err("can't alloc memory pool for serialization\n");
248 * Free resource from rdev(s), and destroy serial_info_pool under conditions:
249 * 1. rdev is the last device flaged with CollisionCheck.
250 * 2. when bitmap is destroyed while policy is not enabled.
251 * 3. for disable policy, the pool is destroyed only when no rdev needs it.
253 void mddev_destroy_serial_pool(struct mddev *mddev, struct md_rdev *rdev,
256 if (rdev && !test_bit(CollisionCheck, &rdev->flags))
259 if (mddev->serial_info_pool) {
260 struct md_rdev *temp;
261 int num = 0; /* used to track if other rdevs need the pool */
264 mddev_suspend(mddev);
265 rdev_for_each(temp, mddev) {
267 if (!mddev->serialize_policy ||
268 !rdev_need_serial(temp))
269 rdev_uninit_serial(temp);
272 } else if (temp != rdev &&
273 test_bit(CollisionCheck, &temp->flags))
278 rdev_uninit_serial(rdev);
281 pr_info("The mempool could be used by other devices\n");
283 mempool_destroy(mddev->serial_info_pool);
284 mddev->serial_info_pool = NULL;
291 static struct ctl_table_header *raid_table_header;
293 static struct ctl_table raid_table[] = {
295 .procname = "speed_limit_min",
296 .data = &sysctl_speed_limit_min,
297 .maxlen = sizeof(int),
298 .mode = S_IRUGO|S_IWUSR,
299 .proc_handler = proc_dointvec,
302 .procname = "speed_limit_max",
303 .data = &sysctl_speed_limit_max,
304 .maxlen = sizeof(int),
305 .mode = S_IRUGO|S_IWUSR,
306 .proc_handler = proc_dointvec,
311 static struct ctl_table raid_dir_table[] = {
315 .mode = S_IRUGO|S_IXUGO,
321 static struct ctl_table raid_root_table[] = {
326 .child = raid_dir_table,
331 static int start_readonly;
334 * The original mechanism for creating an md device is to create
335 * a device node in /dev and to open it. This causes races with device-close.
336 * The preferred method is to write to the "new_array" module parameter.
337 * This can avoid races.
338 * Setting create_on_open to false disables the original mechanism
339 * so all the races disappear.
341 static bool create_on_open = true;
344 * We have a system wide 'event count' that is incremented
345 * on any 'interesting' event, and readers of /proc/mdstat
346 * can use 'poll' or 'select' to find out when the event
350 * start array, stop array, error, add device, remove device,
351 * start build, activate spare
353 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
354 static atomic_t md_event_count;
355 void md_new_event(struct mddev *mddev)
357 atomic_inc(&md_event_count);
358 wake_up(&md_event_waiters);
360 EXPORT_SYMBOL_GPL(md_new_event);
363 * Enables to iterate over all existing md arrays
364 * all_mddevs_lock protects this list.
366 static LIST_HEAD(all_mddevs);
367 static DEFINE_SPINLOCK(all_mddevs_lock);
370 * iterates through all used mddevs in the system.
371 * We take care to grab the all_mddevs_lock whenever navigating
372 * the list, and to always hold a refcount when unlocked.
373 * Any code which breaks out of this loop while own
374 * a reference to the current mddev and must mddev_put it.
376 #define for_each_mddev(_mddev,_tmp) \
378 for (({ spin_lock(&all_mddevs_lock); \
379 _tmp = all_mddevs.next; \
381 ({ if (_tmp != &all_mddevs) \
382 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
383 spin_unlock(&all_mddevs_lock); \
384 if (_mddev) mddev_put(_mddev); \
385 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
386 _tmp != &all_mddevs;}); \
387 ({ spin_lock(&all_mddevs_lock); \
388 _tmp = _tmp->next;}) \
391 /* Rather than calling directly into the personality make_request function,
392 * IO requests come here first so that we can check if the device is
393 * being suspended pending a reconfiguration.
394 * We hold a refcount over the call to ->make_request. By the time that
395 * call has finished, the bio has been linked into some internal structure
396 * and so is visible to ->quiesce(), so we don't need the refcount any more.
398 static bool is_suspended(struct mddev *mddev, struct bio *bio)
400 if (mddev->suspended)
402 if (bio_data_dir(bio) != WRITE)
404 if (mddev->suspend_lo >= mddev->suspend_hi)
406 if (bio->bi_iter.bi_sector >= mddev->suspend_hi)
408 if (bio_end_sector(bio) < mddev->suspend_lo)
413 void md_handle_request(struct mddev *mddev, struct bio *bio)
417 if (is_suspended(mddev, bio)) {
420 prepare_to_wait(&mddev->sb_wait, &__wait,
421 TASK_UNINTERRUPTIBLE);
422 if (!is_suspended(mddev, bio))
428 finish_wait(&mddev->sb_wait, &__wait);
430 atomic_inc(&mddev->active_io);
433 if (!mddev->pers->make_request(mddev, bio)) {
434 atomic_dec(&mddev->active_io);
435 wake_up(&mddev->sb_wait);
436 goto check_suspended;
439 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
440 wake_up(&mddev->sb_wait);
442 EXPORT_SYMBOL(md_handle_request);
444 static blk_qc_t md_submit_bio(struct bio *bio)
446 const int rw = bio_data_dir(bio);
447 struct mddev *mddev = bio->bi_bdev->bd_disk->private_data;
449 if (mddev == NULL || mddev->pers == NULL) {
451 return BLK_QC_T_NONE;
454 if (unlikely(test_bit(MD_BROKEN, &mddev->flags)) && (rw == WRITE)) {
456 return BLK_QC_T_NONE;
459 blk_queue_split(&bio);
461 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
462 if (bio_sectors(bio) != 0)
463 bio->bi_status = BLK_STS_IOERR;
465 return BLK_QC_T_NONE;
468 /* bio could be mergeable after passing to underlayer */
469 bio->bi_opf &= ~REQ_NOMERGE;
471 md_handle_request(mddev, bio);
473 return BLK_QC_T_NONE;
476 /* mddev_suspend makes sure no new requests are submitted
477 * to the device, and that any requests that have been submitted
478 * are completely handled.
479 * Once mddev_detach() is called and completes, the module will be
482 void mddev_suspend(struct mddev *mddev)
484 WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk);
485 lockdep_assert_held(&mddev->reconfig_mutex);
486 if (mddev->suspended++)
489 wake_up(&mddev->sb_wait);
490 set_bit(MD_ALLOW_SB_UPDATE, &mddev->flags);
491 smp_mb__after_atomic();
492 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
493 mddev->pers->quiesce(mddev, 1);
494 clear_bit_unlock(MD_ALLOW_SB_UPDATE, &mddev->flags);
495 wait_event(mddev->sb_wait, !test_bit(MD_UPDATING_SB, &mddev->flags));
497 del_timer_sync(&mddev->safemode_timer);
498 /* restrict memory reclaim I/O during raid array is suspend */
499 mddev->noio_flag = memalloc_noio_save();
501 EXPORT_SYMBOL_GPL(mddev_suspend);
503 void mddev_resume(struct mddev *mddev)
505 /* entred the memalloc scope from mddev_suspend() */
506 memalloc_noio_restore(mddev->noio_flag);
507 lockdep_assert_held(&mddev->reconfig_mutex);
508 if (--mddev->suspended)
510 wake_up(&mddev->sb_wait);
511 mddev->pers->quiesce(mddev, 0);
513 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
514 md_wakeup_thread(mddev->thread);
515 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
517 EXPORT_SYMBOL_GPL(mddev_resume);
520 * Generic flush handling for md
523 static void md_end_flush(struct bio *bio)
525 struct md_rdev *rdev = bio->bi_private;
526 struct mddev *mddev = rdev->mddev;
528 rdev_dec_pending(rdev, mddev);
530 if (atomic_dec_and_test(&mddev->flush_pending)) {
531 /* The pre-request flush has finished */
532 queue_work(md_wq, &mddev->flush_work);
537 static void md_submit_flush_data(struct work_struct *ws);
539 static void submit_flushes(struct work_struct *ws)
541 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
542 struct md_rdev *rdev;
544 mddev->start_flush = ktime_get_boottime();
545 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
546 atomic_set(&mddev->flush_pending, 1);
548 rdev_for_each_rcu(rdev, mddev)
549 if (rdev->raid_disk >= 0 &&
550 !test_bit(Faulty, &rdev->flags)) {
551 /* Take two references, one is dropped
552 * when request finishes, one after
553 * we reclaim rcu_read_lock
556 atomic_inc(&rdev->nr_pending);
557 atomic_inc(&rdev->nr_pending);
559 bi = bio_alloc_bioset(GFP_NOIO, 0, &mddev->bio_set);
560 bi->bi_end_io = md_end_flush;
561 bi->bi_private = rdev;
562 bio_set_dev(bi, rdev->bdev);
563 bi->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
564 atomic_inc(&mddev->flush_pending);
567 rdev_dec_pending(rdev, mddev);
570 if (atomic_dec_and_test(&mddev->flush_pending))
571 queue_work(md_wq, &mddev->flush_work);
574 static void md_submit_flush_data(struct work_struct *ws)
576 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
577 struct bio *bio = mddev->flush_bio;
580 * must reset flush_bio before calling into md_handle_request to avoid a
581 * deadlock, because other bios passed md_handle_request suspend check
582 * could wait for this and below md_handle_request could wait for those
583 * bios because of suspend check
585 spin_lock_irq(&mddev->lock);
586 mddev->prev_flush_start = mddev->start_flush;
587 mddev->flush_bio = NULL;
588 spin_unlock_irq(&mddev->lock);
589 wake_up(&mddev->sb_wait);
591 if (bio->bi_iter.bi_size == 0) {
592 /* an empty barrier - all done */
595 bio->bi_opf &= ~REQ_PREFLUSH;
596 md_handle_request(mddev, bio);
601 * Manages consolidation of flushes and submitting any flushes needed for
602 * a bio with REQ_PREFLUSH. Returns true if the bio is finished or is
603 * being finished in another context. Returns false if the flushing is
604 * complete but still needs the I/O portion of the bio to be processed.
606 bool md_flush_request(struct mddev *mddev, struct bio *bio)
608 ktime_t req_start = ktime_get_boottime();
609 spin_lock_irq(&mddev->lock);
610 /* flush requests wait until ongoing flush completes,
611 * hence coalescing all the pending requests.
613 wait_event_lock_irq(mddev->sb_wait,
615 ktime_before(req_start, mddev->prev_flush_start),
617 /* new request after previous flush is completed */
618 if (ktime_after(req_start, mddev->prev_flush_start)) {
619 WARN_ON(mddev->flush_bio);
620 mddev->flush_bio = bio;
623 spin_unlock_irq(&mddev->lock);
626 INIT_WORK(&mddev->flush_work, submit_flushes);
627 queue_work(md_wq, &mddev->flush_work);
629 /* flush was performed for some other bio while we waited. */
630 if (bio->bi_iter.bi_size == 0)
631 /* an empty barrier - all done */
634 bio->bi_opf &= ~REQ_PREFLUSH;
640 EXPORT_SYMBOL(md_flush_request);
642 static inline struct mddev *mddev_get(struct mddev *mddev)
644 atomic_inc(&mddev->active);
648 static void mddev_delayed_delete(struct work_struct *ws);
650 static void mddev_put(struct mddev *mddev)
652 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
654 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
655 mddev->ctime == 0 && !mddev->hold_active) {
656 /* Array is not configured at all, and not held active,
658 list_del_init(&mddev->all_mddevs);
661 * Call queue_work inside the spinlock so that
662 * flush_workqueue() after mddev_find will succeed in waiting
663 * for the work to be done.
665 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
666 queue_work(md_misc_wq, &mddev->del_work);
668 spin_unlock(&all_mddevs_lock);
671 static void md_safemode_timeout(struct timer_list *t);
673 void mddev_init(struct mddev *mddev)
675 kobject_init(&mddev->kobj, &md_ktype);
676 mutex_init(&mddev->open_mutex);
677 mutex_init(&mddev->reconfig_mutex);
678 mutex_init(&mddev->bitmap_info.mutex);
679 INIT_LIST_HEAD(&mddev->disks);
680 INIT_LIST_HEAD(&mddev->all_mddevs);
681 timer_setup(&mddev->safemode_timer, md_safemode_timeout, 0);
682 atomic_set(&mddev->active, 1);
683 atomic_set(&mddev->openers, 0);
684 atomic_set(&mddev->active_io, 0);
685 spin_lock_init(&mddev->lock);
686 atomic_set(&mddev->flush_pending, 0);
687 init_waitqueue_head(&mddev->sb_wait);
688 init_waitqueue_head(&mddev->recovery_wait);
689 mddev->reshape_position = MaxSector;
690 mddev->reshape_backwards = 0;
691 mddev->last_sync_action = "none";
692 mddev->resync_min = 0;
693 mddev->resync_max = MaxSector;
694 mddev->level = LEVEL_NONE;
696 EXPORT_SYMBOL_GPL(mddev_init);
698 static struct mddev *mddev_find_locked(dev_t unit)
702 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
703 if (mddev->unit == unit)
709 /* find an unused unit number */
710 static dev_t mddev_alloc_unit(void)
712 static int next_minor = 512;
713 int start = next_minor;
718 dev = MKDEV(MD_MAJOR, next_minor);
720 if (next_minor > MINORMASK)
722 if (next_minor == start)
723 return 0; /* Oh dear, all in use. */
724 is_free = !mddev_find_locked(dev);
730 static struct mddev *mddev_find(dev_t unit)
734 if (MAJOR(unit) != MD_MAJOR)
735 unit &= ~((1 << MdpMinorShift) - 1);
737 spin_lock(&all_mddevs_lock);
738 mddev = mddev_find_locked(unit);
741 spin_unlock(&all_mddevs_lock);
746 static struct mddev *mddev_alloc(dev_t unit)
751 if (unit && MAJOR(unit) != MD_MAJOR)
752 unit &= ~((1 << MdpMinorShift) - 1);
754 new = kzalloc(sizeof(*new), GFP_KERNEL);
756 return ERR_PTR(-ENOMEM);
759 spin_lock(&all_mddevs_lock);
762 if (mddev_find_locked(unit))
765 if (MAJOR(unit) == MD_MAJOR)
766 new->md_minor = MINOR(unit);
768 new->md_minor = MINOR(unit) >> MdpMinorShift;
769 new->hold_active = UNTIL_IOCTL;
772 new->unit = mddev_alloc_unit();
775 new->md_minor = MINOR(new->unit);
776 new->hold_active = UNTIL_STOP;
779 list_add(&new->all_mddevs, &all_mddevs);
780 spin_unlock(&all_mddevs_lock);
783 spin_unlock(&all_mddevs_lock);
785 return ERR_PTR(error);
788 static const struct attribute_group md_redundancy_group;
790 void mddev_unlock(struct mddev *mddev)
792 if (mddev->to_remove) {
793 /* These cannot be removed under reconfig_mutex as
794 * an access to the files will try to take reconfig_mutex
795 * while holding the file unremovable, which leads to
797 * So hold set sysfs_active while the remove in happeing,
798 * and anything else which might set ->to_remove or my
799 * otherwise change the sysfs namespace will fail with
800 * -EBUSY if sysfs_active is still set.
801 * We set sysfs_active under reconfig_mutex and elsewhere
802 * test it under the same mutex to ensure its correct value
805 const struct attribute_group *to_remove = mddev->to_remove;
806 mddev->to_remove = NULL;
807 mddev->sysfs_active = 1;
808 mutex_unlock(&mddev->reconfig_mutex);
810 if (mddev->kobj.sd) {
811 if (to_remove != &md_redundancy_group)
812 sysfs_remove_group(&mddev->kobj, to_remove);
813 if (mddev->pers == NULL ||
814 mddev->pers->sync_request == NULL) {
815 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
816 if (mddev->sysfs_action)
817 sysfs_put(mddev->sysfs_action);
818 if (mddev->sysfs_completed)
819 sysfs_put(mddev->sysfs_completed);
820 if (mddev->sysfs_degraded)
821 sysfs_put(mddev->sysfs_degraded);
822 mddev->sysfs_action = NULL;
823 mddev->sysfs_completed = NULL;
824 mddev->sysfs_degraded = NULL;
827 mddev->sysfs_active = 0;
829 mutex_unlock(&mddev->reconfig_mutex);
831 /* As we've dropped the mutex we need a spinlock to
832 * make sure the thread doesn't disappear
834 spin_lock(&pers_lock);
835 md_wakeup_thread(mddev->thread);
836 wake_up(&mddev->sb_wait);
837 spin_unlock(&pers_lock);
839 EXPORT_SYMBOL_GPL(mddev_unlock);
841 struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
843 struct md_rdev *rdev;
845 rdev_for_each_rcu(rdev, mddev)
846 if (rdev->desc_nr == nr)
851 EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
853 static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
855 struct md_rdev *rdev;
857 rdev_for_each(rdev, mddev)
858 if (rdev->bdev->bd_dev == dev)
864 struct md_rdev *md_find_rdev_rcu(struct mddev *mddev, dev_t dev)
866 struct md_rdev *rdev;
868 rdev_for_each_rcu(rdev, mddev)
869 if (rdev->bdev->bd_dev == dev)
874 EXPORT_SYMBOL_GPL(md_find_rdev_rcu);
876 static struct md_personality *find_pers(int level, char *clevel)
878 struct md_personality *pers;
879 list_for_each_entry(pers, &pers_list, list) {
880 if (level != LEVEL_NONE && pers->level == level)
882 if (strcmp(pers->name, clevel)==0)
888 /* return the offset of the super block in 512byte sectors */
889 static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
891 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
892 return MD_NEW_SIZE_SECTORS(num_sectors);
895 static int alloc_disk_sb(struct md_rdev *rdev)
897 rdev->sb_page = alloc_page(GFP_KERNEL);
903 void md_rdev_clear(struct md_rdev *rdev)
906 put_page(rdev->sb_page);
908 rdev->sb_page = NULL;
913 put_page(rdev->bb_page);
914 rdev->bb_page = NULL;
916 badblocks_exit(&rdev->badblocks);
918 EXPORT_SYMBOL_GPL(md_rdev_clear);
920 static void super_written(struct bio *bio)
922 struct md_rdev *rdev = bio->bi_private;
923 struct mddev *mddev = rdev->mddev;
925 if (bio->bi_status) {
926 pr_err("md: %s gets error=%d\n", __func__,
927 blk_status_to_errno(bio->bi_status));
928 md_error(mddev, rdev);
929 if (!test_bit(Faulty, &rdev->flags)
930 && (bio->bi_opf & MD_FAILFAST)) {
931 set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags);
932 set_bit(LastDev, &rdev->flags);
935 clear_bit(LastDev, &rdev->flags);
937 if (atomic_dec_and_test(&mddev->pending_writes))
938 wake_up(&mddev->sb_wait);
939 rdev_dec_pending(rdev, mddev);
943 void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
944 sector_t sector, int size, struct page *page)
946 /* write first size bytes of page to sector of rdev
947 * Increment mddev->pending_writes before returning
948 * and decrement it on completion, waking up sb_wait
949 * if zero is reached.
950 * If an error occurred, call md_error
958 if (test_bit(Faulty, &rdev->flags))
961 bio = bio_alloc_bioset(GFP_NOIO, 1, &mddev->sync_set);
963 atomic_inc(&rdev->nr_pending);
965 bio_set_dev(bio, rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev);
966 bio->bi_iter.bi_sector = sector;
967 bio_add_page(bio, page, size, 0);
968 bio->bi_private = rdev;
969 bio->bi_end_io = super_written;
971 if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) &&
972 test_bit(FailFast, &rdev->flags) &&
973 !test_bit(LastDev, &rdev->flags))
975 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA | ff;
977 atomic_inc(&mddev->pending_writes);
981 int md_super_wait(struct mddev *mddev)
983 /* wait for all superblock writes that were scheduled to complete */
984 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
985 if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags))
990 int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
991 struct page *page, int op, int op_flags, bool metadata_op)
996 bio_init(&bio, &bvec, 1);
998 if (metadata_op && rdev->meta_bdev)
999 bio_set_dev(&bio, rdev->meta_bdev);
1001 bio_set_dev(&bio, rdev->bdev);
1002 bio.bi_opf = op | op_flags;
1004 bio.bi_iter.bi_sector = sector + rdev->sb_start;
1005 else if (rdev->mddev->reshape_position != MaxSector &&
1006 (rdev->mddev->reshape_backwards ==
1007 (sector >= rdev->mddev->reshape_position)))
1008 bio.bi_iter.bi_sector = sector + rdev->new_data_offset;
1010 bio.bi_iter.bi_sector = sector + rdev->data_offset;
1011 bio_add_page(&bio, page, size, 0);
1013 submit_bio_wait(&bio);
1015 return !bio.bi_status;
1017 EXPORT_SYMBOL_GPL(sync_page_io);
1019 static int read_disk_sb(struct md_rdev *rdev, int size)
1021 char b[BDEVNAME_SIZE];
1023 if (rdev->sb_loaded)
1026 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true))
1028 rdev->sb_loaded = 1;
1032 pr_err("md: disabled device %s, could not read superblock.\n",
1033 bdevname(rdev->bdev,b));
1037 static int md_uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
1039 return sb1->set_uuid0 == sb2->set_uuid0 &&
1040 sb1->set_uuid1 == sb2->set_uuid1 &&
1041 sb1->set_uuid2 == sb2->set_uuid2 &&
1042 sb1->set_uuid3 == sb2->set_uuid3;
1045 static int md_sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
1048 mdp_super_t *tmp1, *tmp2;
1050 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
1051 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
1053 if (!tmp1 || !tmp2) {
1062 * nr_disks is not constant
1067 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
1074 static u32 md_csum_fold(u32 csum)
1076 csum = (csum & 0xffff) + (csum >> 16);
1077 return (csum & 0xffff) + (csum >> 16);
1080 static unsigned int calc_sb_csum(mdp_super_t *sb)
1083 u32 *sb32 = (u32*)sb;
1085 unsigned int disk_csum, csum;
1087 disk_csum = sb->sb_csum;
1090 for (i = 0; i < MD_SB_BYTES/4 ; i++)
1092 csum = (newcsum & 0xffffffff) + (newcsum>>32);
1095 /* This used to use csum_partial, which was wrong for several
1096 * reasons including that different results are returned on
1097 * different architectures. It isn't critical that we get exactly
1098 * the same return value as before (we always csum_fold before
1099 * testing, and that removes any differences). However as we
1100 * know that csum_partial always returned a 16bit value on
1101 * alphas, do a fold to maximise conformity to previous behaviour.
1103 sb->sb_csum = md_csum_fold(disk_csum);
1105 sb->sb_csum = disk_csum;
1111 * Handle superblock details.
1112 * We want to be able to handle multiple superblock formats
1113 * so we have a common interface to them all, and an array of
1114 * different handlers.
1115 * We rely on user-space to write the initial superblock, and support
1116 * reading and updating of superblocks.
1117 * Interface methods are:
1118 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
1119 * loads and validates a superblock on dev.
1120 * if refdev != NULL, compare superblocks on both devices
1122 * 0 - dev has a superblock that is compatible with refdev
1123 * 1 - dev has a superblock that is compatible and newer than refdev
1124 * so dev should be used as the refdev in future
1125 * -EINVAL superblock incompatible or invalid
1126 * -othererror e.g. -EIO
1128 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
1129 * Verify that dev is acceptable into mddev.
1130 * The first time, mddev->raid_disks will be 0, and data from
1131 * dev should be merged in. Subsequent calls check that dev
1132 * is new enough. Return 0 or -EINVAL
1134 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
1135 * Update the superblock for rdev with data in mddev
1136 * This does not write to disc.
1142 struct module *owner;
1143 int (*load_super)(struct md_rdev *rdev,
1144 struct md_rdev *refdev,
1146 int (*validate_super)(struct mddev *mddev,
1147 struct md_rdev *rdev);
1148 void (*sync_super)(struct mddev *mddev,
1149 struct md_rdev *rdev);
1150 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
1151 sector_t num_sectors);
1152 int (*allow_new_offset)(struct md_rdev *rdev,
1153 unsigned long long new_offset);
1157 * Check that the given mddev has no bitmap.
1159 * This function is called from the run method of all personalities that do not
1160 * support bitmaps. It prints an error message and returns non-zero if mddev
1161 * has a bitmap. Otherwise, it returns 0.
1164 int md_check_no_bitmap(struct mddev *mddev)
1166 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
1168 pr_warn("%s: bitmaps are not supported for %s\n",
1169 mdname(mddev), mddev->pers->name);
1172 EXPORT_SYMBOL(md_check_no_bitmap);
1175 * load_super for 0.90.0
1177 static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
1179 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1182 bool spare_disk = true;
1185 * Calculate the position of the superblock (512byte sectors),
1186 * it's at the end of the disk.
1188 * It also happens to be a multiple of 4Kb.
1190 rdev->sb_start = calc_dev_sboffset(rdev);
1192 ret = read_disk_sb(rdev, MD_SB_BYTES);
1198 bdevname(rdev->bdev, b);
1199 sb = page_address(rdev->sb_page);
1201 if (sb->md_magic != MD_SB_MAGIC) {
1202 pr_warn("md: invalid raid superblock magic on %s\n", b);
1206 if (sb->major_version != 0 ||
1207 sb->minor_version < 90 ||
1208 sb->minor_version > 91) {
1209 pr_warn("Bad version number %d.%d on %s\n",
1210 sb->major_version, sb->minor_version, b);
1214 if (sb->raid_disks <= 0)
1217 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
1218 pr_warn("md: invalid superblock checksum on %s\n", b);
1222 rdev->preferred_minor = sb->md_minor;
1223 rdev->data_offset = 0;
1224 rdev->new_data_offset = 0;
1225 rdev->sb_size = MD_SB_BYTES;
1226 rdev->badblocks.shift = -1;
1228 if (sb->level == LEVEL_MULTIPATH)
1231 rdev->desc_nr = sb->this_disk.number;
1233 /* not spare disk, or LEVEL_MULTIPATH */
1234 if (sb->level == LEVEL_MULTIPATH ||
1235 (rdev->desc_nr >= 0 &&
1236 rdev->desc_nr < MD_SB_DISKS &&
1237 sb->disks[rdev->desc_nr].state &
1238 ((1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))))
1248 mdp_super_t *refsb = page_address(refdev->sb_page);
1249 if (!md_uuid_equal(refsb, sb)) {
1250 pr_warn("md: %s has different UUID to %s\n",
1251 b, bdevname(refdev->bdev,b2));
1254 if (!md_sb_equal(refsb, sb)) {
1255 pr_warn("md: %s has same UUID but different superblock to %s\n",
1256 b, bdevname(refdev->bdev, b2));
1260 ev2 = md_event(refsb);
1262 if (!spare_disk && ev1 > ev2)
1267 rdev->sectors = rdev->sb_start;
1268 /* Limit to 4TB as metadata cannot record more than that.
1269 * (not needed for Linear and RAID0 as metadata doesn't
1272 if ((u64)rdev->sectors >= (2ULL << 32) && sb->level >= 1)
1273 rdev->sectors = (sector_t)(2ULL << 32) - 2;
1275 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
1276 /* "this cannot possibly happen" ... */
1284 * validate_super for 0.90.0
1286 static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
1289 mdp_super_t *sb = page_address(rdev->sb_page);
1290 __u64 ev1 = md_event(sb);
1292 rdev->raid_disk = -1;
1293 clear_bit(Faulty, &rdev->flags);
1294 clear_bit(In_sync, &rdev->flags);
1295 clear_bit(Bitmap_sync, &rdev->flags);
1296 clear_bit(WriteMostly, &rdev->flags);
1298 if (mddev->raid_disks == 0) {
1299 mddev->major_version = 0;
1300 mddev->minor_version = sb->minor_version;
1301 mddev->patch_version = sb->patch_version;
1302 mddev->external = 0;
1303 mddev->chunk_sectors = sb->chunk_size >> 9;
1304 mddev->ctime = sb->ctime;
1305 mddev->utime = sb->utime;
1306 mddev->level = sb->level;
1307 mddev->clevel[0] = 0;
1308 mddev->layout = sb->layout;
1309 mddev->raid_disks = sb->raid_disks;
1310 mddev->dev_sectors = ((sector_t)sb->size) * 2;
1311 mddev->events = ev1;
1312 mddev->bitmap_info.offset = 0;
1313 mddev->bitmap_info.space = 0;
1314 /* bitmap can use 60 K after the 4K superblocks */
1315 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
1316 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
1317 mddev->reshape_backwards = 0;
1319 if (mddev->minor_version >= 91) {
1320 mddev->reshape_position = sb->reshape_position;
1321 mddev->delta_disks = sb->delta_disks;
1322 mddev->new_level = sb->new_level;
1323 mddev->new_layout = sb->new_layout;
1324 mddev->new_chunk_sectors = sb->new_chunk >> 9;
1325 if (mddev->delta_disks < 0)
1326 mddev->reshape_backwards = 1;
1328 mddev->reshape_position = MaxSector;
1329 mddev->delta_disks = 0;
1330 mddev->new_level = mddev->level;
1331 mddev->new_layout = mddev->layout;
1332 mddev->new_chunk_sectors = mddev->chunk_sectors;
1334 if (mddev->level == 0)
1337 if (sb->state & (1<<MD_SB_CLEAN))
1338 mddev->recovery_cp = MaxSector;
1340 if (sb->events_hi == sb->cp_events_hi &&
1341 sb->events_lo == sb->cp_events_lo) {
1342 mddev->recovery_cp = sb->recovery_cp;
1344 mddev->recovery_cp = 0;
1347 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1348 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1349 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1350 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1352 mddev->max_disks = MD_SB_DISKS;
1354 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
1355 mddev->bitmap_info.file == NULL) {
1356 mddev->bitmap_info.offset =
1357 mddev->bitmap_info.default_offset;
1358 mddev->bitmap_info.space =
1359 mddev->bitmap_info.default_space;
1362 } else if (mddev->pers == NULL) {
1363 /* Insist on good event counter while assembling, except
1364 * for spares (which don't need an event count) */
1366 if (sb->disks[rdev->desc_nr].state & (
1367 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1368 if (ev1 < mddev->events)
1370 } else if (mddev->bitmap) {
1371 /* if adding to array with a bitmap, then we can accept an
1372 * older device ... but not too old.
1374 if (ev1 < mddev->bitmap->events_cleared)
1376 if (ev1 < mddev->events)
1377 set_bit(Bitmap_sync, &rdev->flags);
1379 if (ev1 < mddev->events)
1380 /* just a hot-add of a new device, leave raid_disk at -1 */
1384 if (mddev->level != LEVEL_MULTIPATH) {
1385 desc = sb->disks + rdev->desc_nr;
1387 if (desc->state & (1<<MD_DISK_FAULTY))
1388 set_bit(Faulty, &rdev->flags);
1389 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1390 desc->raid_disk < mddev->raid_disks */) {
1391 set_bit(In_sync, &rdev->flags);
1392 rdev->raid_disk = desc->raid_disk;
1393 rdev->saved_raid_disk = desc->raid_disk;
1394 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1395 /* active but not in sync implies recovery up to
1396 * reshape position. We don't know exactly where
1397 * that is, so set to zero for now */
1398 if (mddev->minor_version >= 91) {
1399 rdev->recovery_offset = 0;
1400 rdev->raid_disk = desc->raid_disk;
1403 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1404 set_bit(WriteMostly, &rdev->flags);
1405 if (desc->state & (1<<MD_DISK_FAILFAST))
1406 set_bit(FailFast, &rdev->flags);
1407 } else /* MULTIPATH are always insync */
1408 set_bit(In_sync, &rdev->flags);
1413 * sync_super for 0.90.0
1415 static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
1418 struct md_rdev *rdev2;
1419 int next_spare = mddev->raid_disks;
1421 /* make rdev->sb match mddev data..
1424 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1425 * 3/ any empty disks < next_spare become removed
1427 * disks[0] gets initialised to REMOVED because
1428 * we cannot be sure from other fields if it has
1429 * been initialised or not.
1432 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1434 rdev->sb_size = MD_SB_BYTES;
1436 sb = page_address(rdev->sb_page);
1438 memset(sb, 0, sizeof(*sb));
1440 sb->md_magic = MD_SB_MAGIC;
1441 sb->major_version = mddev->major_version;
1442 sb->patch_version = mddev->patch_version;
1443 sb->gvalid_words = 0; /* ignored */
1444 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1445 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1446 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1447 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1449 sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
1450 sb->level = mddev->level;
1451 sb->size = mddev->dev_sectors / 2;
1452 sb->raid_disks = mddev->raid_disks;
1453 sb->md_minor = mddev->md_minor;
1454 sb->not_persistent = 0;
1455 sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
1457 sb->events_hi = (mddev->events>>32);
1458 sb->events_lo = (u32)mddev->events;
1460 if (mddev->reshape_position == MaxSector)
1461 sb->minor_version = 90;
1463 sb->minor_version = 91;
1464 sb->reshape_position = mddev->reshape_position;
1465 sb->new_level = mddev->new_level;
1466 sb->delta_disks = mddev->delta_disks;
1467 sb->new_layout = mddev->new_layout;
1468 sb->new_chunk = mddev->new_chunk_sectors << 9;
1470 mddev->minor_version = sb->minor_version;
1473 sb->recovery_cp = mddev->recovery_cp;
1474 sb->cp_events_hi = (mddev->events>>32);
1475 sb->cp_events_lo = (u32)mddev->events;
1476 if (mddev->recovery_cp == MaxSector)
1477 sb->state = (1<< MD_SB_CLEAN);
1479 sb->recovery_cp = 0;
1481 sb->layout = mddev->layout;
1482 sb->chunk_size = mddev->chunk_sectors << 9;
1484 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
1485 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1487 sb->disks[0].state = (1<<MD_DISK_REMOVED);
1488 rdev_for_each(rdev2, mddev) {
1491 int is_active = test_bit(In_sync, &rdev2->flags);
1493 if (rdev2->raid_disk >= 0 &&
1494 sb->minor_version >= 91)
1495 /* we have nowhere to store the recovery_offset,
1496 * but if it is not below the reshape_position,
1497 * we can piggy-back on that.
1500 if (rdev2->raid_disk < 0 ||
1501 test_bit(Faulty, &rdev2->flags))
1504 desc_nr = rdev2->raid_disk;
1506 desc_nr = next_spare++;
1507 rdev2->desc_nr = desc_nr;
1508 d = &sb->disks[rdev2->desc_nr];
1510 d->number = rdev2->desc_nr;
1511 d->major = MAJOR(rdev2->bdev->bd_dev);
1512 d->minor = MINOR(rdev2->bdev->bd_dev);
1514 d->raid_disk = rdev2->raid_disk;
1516 d->raid_disk = rdev2->desc_nr; /* compatibility */
1517 if (test_bit(Faulty, &rdev2->flags))
1518 d->state = (1<<MD_DISK_FAULTY);
1519 else if (is_active) {
1520 d->state = (1<<MD_DISK_ACTIVE);
1521 if (test_bit(In_sync, &rdev2->flags))
1522 d->state |= (1<<MD_DISK_SYNC);
1530 if (test_bit(WriteMostly, &rdev2->flags))
1531 d->state |= (1<<MD_DISK_WRITEMOSTLY);
1532 if (test_bit(FailFast, &rdev2->flags))
1533 d->state |= (1<<MD_DISK_FAILFAST);
1535 /* now set the "removed" and "faulty" bits on any missing devices */
1536 for (i=0 ; i < mddev->raid_disks ; i++) {
1537 mdp_disk_t *d = &sb->disks[i];
1538 if (d->state == 0 && d->number == 0) {
1541 d->state = (1<<MD_DISK_REMOVED);
1542 d->state |= (1<<MD_DISK_FAULTY);
1546 sb->nr_disks = nr_disks;
1547 sb->active_disks = active;
1548 sb->working_disks = working;
1549 sb->failed_disks = failed;
1550 sb->spare_disks = spare;
1552 sb->this_disk = sb->disks[rdev->desc_nr];
1553 sb->sb_csum = calc_sb_csum(sb);
1557 * rdev_size_change for 0.90.0
1559 static unsigned long long
1560 super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
1562 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1563 return 0; /* component must fit device */
1564 if (rdev->mddev->bitmap_info.offset)
1565 return 0; /* can't move bitmap */
1566 rdev->sb_start = calc_dev_sboffset(rdev);
1567 if (!num_sectors || num_sectors > rdev->sb_start)
1568 num_sectors = rdev->sb_start;
1569 /* Limit to 4TB as metadata cannot record more than that.
1570 * 4TB == 2^32 KB, or 2*2^32 sectors.
1572 if ((u64)num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
1573 num_sectors = (sector_t)(2ULL << 32) - 2;
1575 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1577 } while (md_super_wait(rdev->mddev) < 0);
1582 super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1584 /* non-zero offset changes not possible with v0.90 */
1585 return new_offset == 0;
1589 * version 1 superblock
1592 static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
1596 unsigned long long newcsum;
1597 int size = 256 + le32_to_cpu(sb->max_dev)*2;
1598 __le32 *isuper = (__le32*)sb;
1600 disk_csum = sb->sb_csum;
1603 for (; size >= 4; size -= 4)
1604 newcsum += le32_to_cpu(*isuper++);
1607 newcsum += le16_to_cpu(*(__le16*) isuper);
1609 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1610 sb->sb_csum = disk_csum;
1611 return cpu_to_le32(csum);
1614 static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
1616 struct mdp_superblock_1 *sb;
1620 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1622 bool spare_disk = true;
1625 * Calculate the position of the superblock in 512byte sectors.
1626 * It is always aligned to a 4K boundary and
1627 * depeding on minor_version, it can be:
1628 * 0: At least 8K, but less than 12K, from end of device
1629 * 1: At start of device
1630 * 2: 4K from start of device.
1632 switch(minor_version) {
1634 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
1636 sb_start &= ~(sector_t)(4*2-1);
1647 rdev->sb_start = sb_start;
1649 /* superblock is rarely larger than 1K, but it can be larger,
1650 * and it is safe to read 4k, so we do that
1652 ret = read_disk_sb(rdev, 4096);
1653 if (ret) return ret;
1655 sb = page_address(rdev->sb_page);
1657 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1658 sb->major_version != cpu_to_le32(1) ||
1659 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1660 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1661 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1664 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1665 pr_warn("md: invalid superblock checksum on %s\n",
1666 bdevname(rdev->bdev,b));
1669 if (le64_to_cpu(sb->data_size) < 10) {
1670 pr_warn("md: data_size too small on %s\n",
1671 bdevname(rdev->bdev,b));
1676 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1677 /* Some padding is non-zero, might be a new feature */
1680 rdev->preferred_minor = 0xffff;
1681 rdev->data_offset = le64_to_cpu(sb->data_offset);
1682 rdev->new_data_offset = rdev->data_offset;
1683 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1684 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1685 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
1686 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1688 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1689 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1690 if (rdev->sb_size & bmask)
1691 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1694 && rdev->data_offset < sb_start + (rdev->sb_size/512))
1697 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1700 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1703 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1705 if (!rdev->bb_page) {
1706 rdev->bb_page = alloc_page(GFP_KERNEL);
1710 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1711 rdev->badblocks.count == 0) {
1712 /* need to load the bad block list.
1713 * Currently we limit it to one page.
1719 int sectors = le16_to_cpu(sb->bblog_size);
1720 if (sectors > (PAGE_SIZE / 512))
1722 offset = le32_to_cpu(sb->bblog_offset);
1725 bb_sector = (long long)offset;
1726 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1727 rdev->bb_page, REQ_OP_READ, 0, true))
1729 bbp = (__le64 *)page_address(rdev->bb_page);
1730 rdev->badblocks.shift = sb->bblog_shift;
1731 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1732 u64 bb = le64_to_cpu(*bbp);
1733 int count = bb & (0x3ff);
1734 u64 sector = bb >> 10;
1735 sector <<= sb->bblog_shift;
1736 count <<= sb->bblog_shift;
1739 if (badblocks_set(&rdev->badblocks, sector, count, 1))
1742 } else if (sb->bblog_offset != 0)
1743 rdev->badblocks.shift = 0;
1745 if ((le32_to_cpu(sb->feature_map) &
1746 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS))) {
1747 rdev->ppl.offset = (__s16)le16_to_cpu(sb->ppl.offset);
1748 rdev->ppl.size = le16_to_cpu(sb->ppl.size);
1749 rdev->ppl.sector = rdev->sb_start + rdev->ppl.offset;
1752 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RAID0_LAYOUT) &&
1756 /* not spare disk, or LEVEL_MULTIPATH */
1757 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH) ||
1758 (rdev->desc_nr >= 0 &&
1759 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1760 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1761 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL)))
1771 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
1773 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1774 sb->level != refsb->level ||
1775 sb->layout != refsb->layout ||
1776 sb->chunksize != refsb->chunksize) {
1777 pr_warn("md: %s has strangely different superblock to %s\n",
1778 bdevname(rdev->bdev,b),
1779 bdevname(refdev->bdev,b2));
1782 ev1 = le64_to_cpu(sb->events);
1783 ev2 = le64_to_cpu(refsb->events);
1785 if (!spare_disk && ev1 > ev2)
1790 if (minor_version) {
1791 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1792 sectors -= rdev->data_offset;
1794 sectors = rdev->sb_start;
1795 if (sectors < le64_to_cpu(sb->data_size))
1797 rdev->sectors = le64_to_cpu(sb->data_size);
1801 static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
1803 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
1804 __u64 ev1 = le64_to_cpu(sb->events);
1806 rdev->raid_disk = -1;
1807 clear_bit(Faulty, &rdev->flags);
1808 clear_bit(In_sync, &rdev->flags);
1809 clear_bit(Bitmap_sync, &rdev->flags);
1810 clear_bit(WriteMostly, &rdev->flags);
1812 if (mddev->raid_disks == 0) {
1813 mddev->major_version = 1;
1814 mddev->patch_version = 0;
1815 mddev->external = 0;
1816 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
1817 mddev->ctime = le64_to_cpu(sb->ctime);
1818 mddev->utime = le64_to_cpu(sb->utime);
1819 mddev->level = le32_to_cpu(sb->level);
1820 mddev->clevel[0] = 0;
1821 mddev->layout = le32_to_cpu(sb->layout);
1822 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1823 mddev->dev_sectors = le64_to_cpu(sb->size);
1824 mddev->events = ev1;
1825 mddev->bitmap_info.offset = 0;
1826 mddev->bitmap_info.space = 0;
1827 /* Default location for bitmap is 1K after superblock
1828 * using 3K - total of 4K
1830 mddev->bitmap_info.default_offset = 1024 >> 9;
1831 mddev->bitmap_info.default_space = (4096-1024) >> 9;
1832 mddev->reshape_backwards = 0;
1834 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1835 memcpy(mddev->uuid, sb->set_uuid, 16);
1837 mddev->max_disks = (4096-256)/2;
1839 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1840 mddev->bitmap_info.file == NULL) {
1841 mddev->bitmap_info.offset =
1842 (__s32)le32_to_cpu(sb->bitmap_offset);
1843 /* Metadata doesn't record how much space is available.
1844 * For 1.0, we assume we can use up to the superblock
1845 * if before, else to 4K beyond superblock.
1846 * For others, assume no change is possible.
1848 if (mddev->minor_version > 0)
1849 mddev->bitmap_info.space = 0;
1850 else if (mddev->bitmap_info.offset > 0)
1851 mddev->bitmap_info.space =
1852 8 - mddev->bitmap_info.offset;
1854 mddev->bitmap_info.space =
1855 -mddev->bitmap_info.offset;
1858 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1859 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1860 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1861 mddev->new_level = le32_to_cpu(sb->new_level);
1862 mddev->new_layout = le32_to_cpu(sb->new_layout);
1863 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
1864 if (mddev->delta_disks < 0 ||
1865 (mddev->delta_disks == 0 &&
1866 (le32_to_cpu(sb->feature_map)
1867 & MD_FEATURE_RESHAPE_BACKWARDS)))
1868 mddev->reshape_backwards = 1;
1870 mddev->reshape_position = MaxSector;
1871 mddev->delta_disks = 0;
1872 mddev->new_level = mddev->level;
1873 mddev->new_layout = mddev->layout;
1874 mddev->new_chunk_sectors = mddev->chunk_sectors;
1877 if (mddev->level == 0 &&
1878 !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RAID0_LAYOUT))
1881 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
1882 set_bit(MD_HAS_JOURNAL, &mddev->flags);
1884 if (le32_to_cpu(sb->feature_map) &
1885 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS)) {
1886 if (le32_to_cpu(sb->feature_map) &
1887 (MD_FEATURE_BITMAP_OFFSET | MD_FEATURE_JOURNAL))
1889 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) &&
1890 (le32_to_cpu(sb->feature_map) &
1891 MD_FEATURE_MULTIPLE_PPLS))
1893 set_bit(MD_HAS_PPL, &mddev->flags);
1895 } else if (mddev->pers == NULL) {
1896 /* Insist of good event counter while assembling, except for
1897 * spares (which don't need an event count) */
1899 if (rdev->desc_nr >= 0 &&
1900 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1901 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1902 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
1903 if (ev1 < mddev->events)
1905 } else if (mddev->bitmap) {
1906 /* If adding to array with a bitmap, then we can accept an
1907 * older device, but not too old.
1909 if (ev1 < mddev->bitmap->events_cleared)
1911 if (ev1 < mddev->events)
1912 set_bit(Bitmap_sync, &rdev->flags);
1914 if (ev1 < mddev->events)
1915 /* just a hot-add of a new device, leave raid_disk at -1 */
1918 if (mddev->level != LEVEL_MULTIPATH) {
1920 if (rdev->desc_nr < 0 ||
1921 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1922 role = MD_DISK_ROLE_SPARE;
1925 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1927 case MD_DISK_ROLE_SPARE: /* spare */
1929 case MD_DISK_ROLE_FAULTY: /* faulty */
1930 set_bit(Faulty, &rdev->flags);
1932 case MD_DISK_ROLE_JOURNAL: /* journal device */
1933 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1934 /* journal device without journal feature */
1935 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
1938 set_bit(Journal, &rdev->flags);
1939 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
1940 rdev->raid_disk = 0;
1943 rdev->saved_raid_disk = role;
1944 if ((le32_to_cpu(sb->feature_map) &
1945 MD_FEATURE_RECOVERY_OFFSET)) {
1946 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1947 if (!(le32_to_cpu(sb->feature_map) &
1948 MD_FEATURE_RECOVERY_BITMAP))
1949 rdev->saved_raid_disk = -1;
1952 * If the array is FROZEN, then the device can't
1953 * be in_sync with rest of array.
1955 if (!test_bit(MD_RECOVERY_FROZEN,
1957 set_bit(In_sync, &rdev->flags);
1959 rdev->raid_disk = role;
1962 if (sb->devflags & WriteMostly1)
1963 set_bit(WriteMostly, &rdev->flags);
1964 if (sb->devflags & FailFast1)
1965 set_bit(FailFast, &rdev->flags);
1966 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1967 set_bit(Replacement, &rdev->flags);
1968 } else /* MULTIPATH are always insync */
1969 set_bit(In_sync, &rdev->flags);
1974 static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
1976 struct mdp_superblock_1 *sb;
1977 struct md_rdev *rdev2;
1979 /* make rdev->sb match mddev and rdev data. */
1981 sb = page_address(rdev->sb_page);
1983 sb->feature_map = 0;
1985 sb->recovery_offset = cpu_to_le64(0);
1986 memset(sb->pad3, 0, sizeof(sb->pad3));
1988 sb->utime = cpu_to_le64((__u64)mddev->utime);
1989 sb->events = cpu_to_le64(mddev->events);
1991 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1992 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
1993 sb->resync_offset = cpu_to_le64(MaxSector);
1995 sb->resync_offset = cpu_to_le64(0);
1997 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1999 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
2000 sb->size = cpu_to_le64(mddev->dev_sectors);
2001 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
2002 sb->level = cpu_to_le32(mddev->level);
2003 sb->layout = cpu_to_le32(mddev->layout);
2004 if (test_bit(FailFast, &rdev->flags))
2005 sb->devflags |= FailFast1;
2007 sb->devflags &= ~FailFast1;
2009 if (test_bit(WriteMostly, &rdev->flags))
2010 sb->devflags |= WriteMostly1;
2012 sb->devflags &= ~WriteMostly1;
2013 sb->data_offset = cpu_to_le64(rdev->data_offset);
2014 sb->data_size = cpu_to_le64(rdev->sectors);
2016 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
2017 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
2018 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
2021 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
2022 !test_bit(In_sync, &rdev->flags)) {
2024 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
2025 sb->recovery_offset =
2026 cpu_to_le64(rdev->recovery_offset);
2027 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
2029 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
2031 /* Note: recovery_offset and journal_tail share space */
2032 if (test_bit(Journal, &rdev->flags))
2033 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
2034 if (test_bit(Replacement, &rdev->flags))
2036 cpu_to_le32(MD_FEATURE_REPLACEMENT);
2038 if (mddev->reshape_position != MaxSector) {
2039 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
2040 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
2041 sb->new_layout = cpu_to_le32(mddev->new_layout);
2042 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
2043 sb->new_level = cpu_to_le32(mddev->new_level);
2044 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
2045 if (mddev->delta_disks == 0 &&
2046 mddev->reshape_backwards)
2048 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
2049 if (rdev->new_data_offset != rdev->data_offset) {
2051 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
2052 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
2053 - rdev->data_offset));
2057 if (mddev_is_clustered(mddev))
2058 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
2060 if (rdev->badblocks.count == 0)
2061 /* Nothing to do for bad blocks*/ ;
2062 else if (sb->bblog_offset == 0)
2063 /* Cannot record bad blocks on this device */
2064 md_error(mddev, rdev);
2066 struct badblocks *bb = &rdev->badblocks;
2067 __le64 *bbp = (__le64 *)page_address(rdev->bb_page);
2069 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
2074 seq = read_seqbegin(&bb->lock);
2076 memset(bbp, 0xff, PAGE_SIZE);
2078 for (i = 0 ; i < bb->count ; i++) {
2079 u64 internal_bb = p[i];
2080 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
2081 | BB_LEN(internal_bb));
2082 bbp[i] = cpu_to_le64(store_bb);
2085 if (read_seqretry(&bb->lock, seq))
2088 bb->sector = (rdev->sb_start +
2089 (int)le32_to_cpu(sb->bblog_offset));
2090 bb->size = le16_to_cpu(sb->bblog_size);
2095 rdev_for_each(rdev2, mddev)
2096 if (rdev2->desc_nr+1 > max_dev)
2097 max_dev = rdev2->desc_nr+1;
2099 if (max_dev > le32_to_cpu(sb->max_dev)) {
2101 sb->max_dev = cpu_to_le32(max_dev);
2102 rdev->sb_size = max_dev * 2 + 256;
2103 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
2104 if (rdev->sb_size & bmask)
2105 rdev->sb_size = (rdev->sb_size | bmask) + 1;
2107 max_dev = le32_to_cpu(sb->max_dev);
2109 for (i=0; i<max_dev;i++)
2110 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
2112 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
2113 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
2115 if (test_bit(MD_HAS_PPL, &mddev->flags)) {
2116 if (test_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags))
2118 cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS);
2120 sb->feature_map |= cpu_to_le32(MD_FEATURE_PPL);
2121 sb->ppl.offset = cpu_to_le16(rdev->ppl.offset);
2122 sb->ppl.size = cpu_to_le16(rdev->ppl.size);
2125 rdev_for_each(rdev2, mddev) {
2127 if (test_bit(Faulty, &rdev2->flags))
2128 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
2129 else if (test_bit(In_sync, &rdev2->flags))
2130 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
2131 else if (test_bit(Journal, &rdev2->flags))
2132 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
2133 else if (rdev2->raid_disk >= 0)
2134 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
2136 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
2139 sb->sb_csum = calc_sb_1_csum(sb);
2142 static sector_t super_1_choose_bm_space(sector_t dev_size)
2146 /* if the device is bigger than 8Gig, save 64k for bitmap
2147 * usage, if bigger than 200Gig, save 128k
2149 if (dev_size < 64*2)
2151 else if (dev_size - 64*2 >= 200*1024*1024*2)
2153 else if (dev_size - 4*2 > 8*1024*1024*2)
2160 static unsigned long long
2161 super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
2163 struct mdp_superblock_1 *sb;
2164 sector_t max_sectors;
2165 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
2166 return 0; /* component must fit device */
2167 if (rdev->data_offset != rdev->new_data_offset)
2168 return 0; /* too confusing */
2169 if (rdev->sb_start < rdev->data_offset) {
2170 /* minor versions 1 and 2; superblock before data */
2171 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
2172 max_sectors -= rdev->data_offset;
2173 if (!num_sectors || num_sectors > max_sectors)
2174 num_sectors = max_sectors;
2175 } else if (rdev->mddev->bitmap_info.offset) {
2176 /* minor version 0 with bitmap we can't move */
2179 /* minor version 0; superblock after data */
2180 sector_t sb_start, bm_space;
2181 sector_t dev_size = i_size_read(rdev->bdev->bd_inode) >> 9;
2183 /* 8K is for superblock */
2184 sb_start = dev_size - 8*2;
2185 sb_start &= ~(sector_t)(4*2 - 1);
2187 bm_space = super_1_choose_bm_space(dev_size);
2189 /* Space that can be used to store date needs to decrease
2190 * superblock bitmap space and bad block space(4K)
2192 max_sectors = sb_start - bm_space - 4*2;
2194 if (!num_sectors || num_sectors > max_sectors)
2195 num_sectors = max_sectors;
2197 sb = page_address(rdev->sb_page);
2198 sb->data_size = cpu_to_le64(num_sectors);
2199 sb->super_offset = cpu_to_le64(rdev->sb_start);
2200 sb->sb_csum = calc_sb_1_csum(sb);
2202 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
2204 } while (md_super_wait(rdev->mddev) < 0);
2210 super_1_allow_new_offset(struct md_rdev *rdev,
2211 unsigned long long new_offset)
2213 /* All necessary checks on new >= old have been done */
2214 struct bitmap *bitmap;
2215 if (new_offset >= rdev->data_offset)
2218 /* with 1.0 metadata, there is no metadata to tread on
2219 * so we can always move back */
2220 if (rdev->mddev->minor_version == 0)
2223 /* otherwise we must be sure not to step on
2224 * any metadata, so stay:
2225 * 36K beyond start of superblock
2226 * beyond end of badblocks
2227 * beyond write-intent bitmap
2229 if (rdev->sb_start + (32+4)*2 > new_offset)
2231 bitmap = rdev->mddev->bitmap;
2232 if (bitmap && !rdev->mddev->bitmap_info.file &&
2233 rdev->sb_start + rdev->mddev->bitmap_info.offset +
2234 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
2236 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
2242 static struct super_type super_types[] = {
2245 .owner = THIS_MODULE,
2246 .load_super = super_90_load,
2247 .validate_super = super_90_validate,
2248 .sync_super = super_90_sync,
2249 .rdev_size_change = super_90_rdev_size_change,
2250 .allow_new_offset = super_90_allow_new_offset,
2254 .owner = THIS_MODULE,
2255 .load_super = super_1_load,
2256 .validate_super = super_1_validate,
2257 .sync_super = super_1_sync,
2258 .rdev_size_change = super_1_rdev_size_change,
2259 .allow_new_offset = super_1_allow_new_offset,
2263 static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
2265 if (mddev->sync_super) {
2266 mddev->sync_super(mddev, rdev);
2270 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
2272 super_types[mddev->major_version].sync_super(mddev, rdev);
2275 static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
2277 struct md_rdev *rdev, *rdev2;
2280 rdev_for_each_rcu(rdev, mddev1) {
2281 if (test_bit(Faulty, &rdev->flags) ||
2282 test_bit(Journal, &rdev->flags) ||
2283 rdev->raid_disk == -1)
2285 rdev_for_each_rcu(rdev2, mddev2) {
2286 if (test_bit(Faulty, &rdev2->flags) ||
2287 test_bit(Journal, &rdev2->flags) ||
2288 rdev2->raid_disk == -1)
2290 if (rdev->bdev->bd_disk == rdev2->bdev->bd_disk) {
2300 static LIST_HEAD(pending_raid_disks);
2303 * Try to register data integrity profile for an mddev
2305 * This is called when an array is started and after a disk has been kicked
2306 * from the array. It only succeeds if all working and active component devices
2307 * are integrity capable with matching profiles.
2309 int md_integrity_register(struct mddev *mddev)
2311 struct md_rdev *rdev, *reference = NULL;
2313 if (list_empty(&mddev->disks))
2314 return 0; /* nothing to do */
2315 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2316 return 0; /* shouldn't register, or already is */
2317 rdev_for_each(rdev, mddev) {
2318 /* skip spares and non-functional disks */
2319 if (test_bit(Faulty, &rdev->flags))
2321 if (rdev->raid_disk < 0)
2324 /* Use the first rdev as the reference */
2328 /* does this rdev's profile match the reference profile? */
2329 if (blk_integrity_compare(reference->bdev->bd_disk,
2330 rdev->bdev->bd_disk) < 0)
2333 if (!reference || !bdev_get_integrity(reference->bdev))
2336 * All component devices are integrity capable and have matching
2337 * profiles, register the common profile for the md device.
2339 blk_integrity_register(mddev->gendisk,
2340 bdev_get_integrity(reference->bdev));
2342 pr_debug("md: data integrity enabled on %s\n", mdname(mddev));
2343 if (bioset_integrity_create(&mddev->bio_set, BIO_POOL_SIZE) ||
2344 (mddev->level != 1 && mddev->level != 10 &&
2345 bioset_integrity_create(&mddev->io_acct_set, BIO_POOL_SIZE))) {
2347 * No need to handle the failure of bioset_integrity_create,
2348 * because the function is called by md_run() -> pers->run(),
2349 * md_run calls bioset_exit -> bioset_integrity_free in case
2352 pr_err("md: failed to create integrity pool for %s\n",
2358 EXPORT_SYMBOL(md_integrity_register);
2361 * Attempt to add an rdev, but only if it is consistent with the current
2364 int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
2366 struct blk_integrity *bi_mddev;
2367 char name[BDEVNAME_SIZE];
2369 if (!mddev->gendisk)
2372 bi_mddev = blk_get_integrity(mddev->gendisk);
2374 if (!bi_mddev) /* nothing to do */
2377 if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
2378 pr_err("%s: incompatible integrity profile for %s\n",
2379 mdname(mddev), bdevname(rdev->bdev, name));
2385 EXPORT_SYMBOL(md_integrity_add_rdev);
2387 static bool rdev_read_only(struct md_rdev *rdev)
2389 return bdev_read_only(rdev->bdev) ||
2390 (rdev->meta_bdev && bdev_read_only(rdev->meta_bdev));
2393 static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
2395 char b[BDEVNAME_SIZE];
2398 /* prevent duplicates */
2399 if (find_rdev(mddev, rdev->bdev->bd_dev))
2402 if (rdev_read_only(rdev) && mddev->pers)
2405 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2406 if (!test_bit(Journal, &rdev->flags) &&
2408 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
2410 /* Cannot change size, so fail
2411 * If mddev->level <= 0, then we don't care
2412 * about aligning sizes (e.g. linear)
2414 if (mddev->level > 0)
2417 mddev->dev_sectors = rdev->sectors;
2420 /* Verify rdev->desc_nr is unique.
2421 * If it is -1, assign a free number, else
2422 * check number is not in use
2425 if (rdev->desc_nr < 0) {
2428 choice = mddev->raid_disks;
2429 while (md_find_rdev_nr_rcu(mddev, choice))
2431 rdev->desc_nr = choice;
2433 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
2439 if (!test_bit(Journal, &rdev->flags) &&
2440 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2441 pr_warn("md: %s: array is limited to %d devices\n",
2442 mdname(mddev), mddev->max_disks);
2445 bdevname(rdev->bdev,b);
2446 strreplace(b, '/', '!');
2448 rdev->mddev = mddev;
2449 pr_debug("md: bind<%s>\n", b);
2451 if (mddev->raid_disks)
2452 mddev_create_serial_pool(mddev, rdev, false);
2454 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
2457 /* failure here is OK */
2458 err = sysfs_create_link(&rdev->kobj, bdev_kobj(rdev->bdev), "block");
2459 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
2460 rdev->sysfs_unack_badblocks =
2461 sysfs_get_dirent_safe(rdev->kobj.sd, "unacknowledged_bad_blocks");
2462 rdev->sysfs_badblocks =
2463 sysfs_get_dirent_safe(rdev->kobj.sd, "bad_blocks");
2465 list_add_rcu(&rdev->same_set, &mddev->disks);
2466 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
2468 /* May as well allow recovery to be retried once */
2469 mddev->recovery_disabled++;
2474 pr_warn("md: failed to register dev-%s for %s\n",
2479 static void rdev_delayed_delete(struct work_struct *ws)
2481 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
2482 kobject_del(&rdev->kobj);
2483 kobject_put(&rdev->kobj);
2486 static void unbind_rdev_from_array(struct md_rdev *rdev)
2488 char b[BDEVNAME_SIZE];
2490 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
2491 list_del_rcu(&rdev->same_set);
2492 pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b));
2493 mddev_destroy_serial_pool(rdev->mddev, rdev, false);
2495 sysfs_remove_link(&rdev->kobj, "block");
2496 sysfs_put(rdev->sysfs_state);
2497 sysfs_put(rdev->sysfs_unack_badblocks);
2498 sysfs_put(rdev->sysfs_badblocks);
2499 rdev->sysfs_state = NULL;
2500 rdev->sysfs_unack_badblocks = NULL;
2501 rdev->sysfs_badblocks = NULL;
2502 rdev->badblocks.count = 0;
2503 /* We need to delay this, otherwise we can deadlock when
2504 * writing to 'remove' to "dev/state". We also need
2505 * to delay it due to rcu usage.
2508 INIT_WORK(&rdev->del_work, rdev_delayed_delete);
2509 kobject_get(&rdev->kobj);
2510 queue_work(md_rdev_misc_wq, &rdev->del_work);
2514 * prevent the device from being mounted, repartitioned or
2515 * otherwise reused by a RAID array (or any other kernel
2516 * subsystem), by bd_claiming the device.
2518 static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
2521 struct block_device *bdev;
2523 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
2524 shared ? (struct md_rdev *)lock_rdev : rdev);
2526 pr_warn("md: could not open device unknown-block(%u,%u).\n",
2527 MAJOR(dev), MINOR(dev));
2528 return PTR_ERR(bdev);
2534 static void unlock_rdev(struct md_rdev *rdev)
2536 struct block_device *bdev = rdev->bdev;
2538 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
2541 void md_autodetect_dev(dev_t dev);
2543 static void export_rdev(struct md_rdev *rdev)
2545 char b[BDEVNAME_SIZE];
2547 pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b));
2548 md_rdev_clear(rdev);
2550 if (test_bit(AutoDetected, &rdev->flags))
2551 md_autodetect_dev(rdev->bdev->bd_dev);
2554 kobject_put(&rdev->kobj);
2557 void md_kick_rdev_from_array(struct md_rdev *rdev)
2559 unbind_rdev_from_array(rdev);
2562 EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
2564 static void export_array(struct mddev *mddev)
2566 struct md_rdev *rdev;
2568 while (!list_empty(&mddev->disks)) {
2569 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2571 md_kick_rdev_from_array(rdev);
2573 mddev->raid_disks = 0;
2574 mddev->major_version = 0;
2577 static bool set_in_sync(struct mddev *mddev)
2579 lockdep_assert_held(&mddev->lock);
2580 if (!mddev->in_sync) {
2581 mddev->sync_checkers++;
2582 spin_unlock(&mddev->lock);
2583 percpu_ref_switch_to_atomic_sync(&mddev->writes_pending);
2584 spin_lock(&mddev->lock);
2585 if (!mddev->in_sync &&
2586 percpu_ref_is_zero(&mddev->writes_pending)) {
2589 * Ensure ->in_sync is visible before we clear
2593 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2594 sysfs_notify_dirent_safe(mddev->sysfs_state);
2596 if (--mddev->sync_checkers == 0)
2597 percpu_ref_switch_to_percpu(&mddev->writes_pending);
2599 if (mddev->safemode == 1)
2600 mddev->safemode = 0;
2601 return mddev->in_sync;
2604 static void sync_sbs(struct mddev *mddev, int nospares)
2606 /* Update each superblock (in-memory image), but
2607 * if we are allowed to, skip spares which already
2608 * have the right event counter, or have one earlier
2609 * (which would mean they aren't being marked as dirty
2610 * with the rest of the array)
2612 struct md_rdev *rdev;
2613 rdev_for_each(rdev, mddev) {
2614 if (rdev->sb_events == mddev->events ||
2616 rdev->raid_disk < 0 &&
2617 rdev->sb_events+1 == mddev->events)) {
2618 /* Don't update this superblock */
2619 rdev->sb_loaded = 2;
2621 sync_super(mddev, rdev);
2622 rdev->sb_loaded = 1;
2627 static bool does_sb_need_changing(struct mddev *mddev)
2629 struct md_rdev *rdev;
2630 struct mdp_superblock_1 *sb;
2633 /* Find a good rdev */
2634 rdev_for_each(rdev, mddev)
2635 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2638 /* No good device found. */
2642 sb = page_address(rdev->sb_page);
2643 /* Check if a device has become faulty or a spare become active */
2644 rdev_for_each(rdev, mddev) {
2645 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2646 /* Device activated? */
2647 if (role == 0xffff && rdev->raid_disk >=0 &&
2648 !test_bit(Faulty, &rdev->flags))
2650 /* Device turned faulty? */
2651 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2655 /* Check if any mddev parameters have changed */
2656 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2657 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
2658 (mddev->layout != le32_to_cpu(sb->layout)) ||
2659 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2660 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2666 void md_update_sb(struct mddev *mddev, int force_change)
2668 struct md_rdev *rdev;
2671 int any_badblocks_changed = 0;
2676 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2681 if (mddev_is_clustered(mddev)) {
2682 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
2684 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
2686 ret = md_cluster_ops->metadata_update_start(mddev);
2687 /* Has someone else has updated the sb */
2688 if (!does_sb_need_changing(mddev)) {
2690 md_cluster_ops->metadata_update_cancel(mddev);
2691 bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2692 BIT(MD_SB_CHANGE_DEVS) |
2693 BIT(MD_SB_CHANGE_CLEAN));
2699 * First make sure individual recovery_offsets are correct
2700 * curr_resync_completed can only be used during recovery.
2701 * During reshape/resync it might use array-addresses rather
2702 * that device addresses.
2704 rdev_for_each(rdev, mddev) {
2705 if (rdev->raid_disk >= 0 &&
2706 mddev->delta_disks >= 0 &&
2707 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
2708 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery) &&
2709 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
2710 !test_bit(Journal, &rdev->flags) &&
2711 !test_bit(In_sync, &rdev->flags) &&
2712 mddev->curr_resync_completed > rdev->recovery_offset)
2713 rdev->recovery_offset = mddev->curr_resync_completed;
2716 if (!mddev->persistent) {
2717 clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2718 clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2719 if (!mddev->external) {
2720 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
2721 rdev_for_each(rdev, mddev) {
2722 if (rdev->badblocks.changed) {
2723 rdev->badblocks.changed = 0;
2724 ack_all_badblocks(&rdev->badblocks);
2725 md_error(mddev, rdev);
2727 clear_bit(Blocked, &rdev->flags);
2728 clear_bit(BlockedBadBlocks, &rdev->flags);
2729 wake_up(&rdev->blocked_wait);
2732 wake_up(&mddev->sb_wait);
2736 spin_lock(&mddev->lock);
2738 mddev->utime = ktime_get_real_seconds();
2740 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
2742 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
2743 /* just a clean<-> dirty transition, possibly leave spares alone,
2744 * though if events isn't the right even/odd, we will have to do
2750 if (mddev->degraded)
2751 /* If the array is degraded, then skipping spares is both
2752 * dangerous and fairly pointless.
2753 * Dangerous because a device that was removed from the array
2754 * might have a event_count that still looks up-to-date,
2755 * so it can be re-added without a resync.
2756 * Pointless because if there are any spares to skip,
2757 * then a recovery will happen and soon that array won't
2758 * be degraded any more and the spare can go back to sleep then.
2762 sync_req = mddev->in_sync;
2764 /* If this is just a dirty<->clean transition, and the array is clean
2765 * and 'events' is odd, we can roll back to the previous clean state */
2767 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
2768 && mddev->can_decrease_events
2769 && mddev->events != 1) {
2771 mddev->can_decrease_events = 0;
2773 /* otherwise we have to go forward and ... */
2775 mddev->can_decrease_events = nospares;
2779 * This 64-bit counter should never wrap.
2780 * Either we are in around ~1 trillion A.C., assuming
2781 * 1 reboot per second, or we have a bug...
2783 WARN_ON(mddev->events == 0);
2785 rdev_for_each(rdev, mddev) {
2786 if (rdev->badblocks.changed)
2787 any_badblocks_changed++;
2788 if (test_bit(Faulty, &rdev->flags))
2789 set_bit(FaultRecorded, &rdev->flags);
2792 sync_sbs(mddev, nospares);
2793 spin_unlock(&mddev->lock);
2795 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2796 mdname(mddev), mddev->in_sync);
2799 blk_add_trace_msg(mddev->queue, "md md_update_sb");
2801 md_bitmap_update_sb(mddev->bitmap);
2802 rdev_for_each(rdev, mddev) {
2803 char b[BDEVNAME_SIZE];
2805 if (rdev->sb_loaded != 1)
2806 continue; /* no noise on spare devices */
2808 if (!test_bit(Faulty, &rdev->flags)) {
2809 md_super_write(mddev,rdev,
2810 rdev->sb_start, rdev->sb_size,
2812 pr_debug("md: (write) %s's sb offset: %llu\n",
2813 bdevname(rdev->bdev, b),
2814 (unsigned long long)rdev->sb_start);
2815 rdev->sb_events = mddev->events;
2816 if (rdev->badblocks.size) {
2817 md_super_write(mddev, rdev,
2818 rdev->badblocks.sector,
2819 rdev->badblocks.size << 9,
2821 rdev->badblocks.size = 0;
2825 pr_debug("md: %s (skipping faulty)\n",
2826 bdevname(rdev->bdev, b));
2828 if (mddev->level == LEVEL_MULTIPATH)
2829 /* only need to write one superblock... */
2832 if (md_super_wait(mddev) < 0)
2834 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
2836 if (mddev_is_clustered(mddev) && ret == 0)
2837 md_cluster_ops->metadata_update_finish(mddev);
2839 if (mddev->in_sync != sync_req ||
2840 !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2841 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN)))
2842 /* have to write it out again */
2844 wake_up(&mddev->sb_wait);
2845 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2846 sysfs_notify_dirent_safe(mddev->sysfs_completed);
2848 rdev_for_each(rdev, mddev) {
2849 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2850 clear_bit(Blocked, &rdev->flags);
2852 if (any_badblocks_changed)
2853 ack_all_badblocks(&rdev->badblocks);
2854 clear_bit(BlockedBadBlocks, &rdev->flags);
2855 wake_up(&rdev->blocked_wait);
2858 EXPORT_SYMBOL(md_update_sb);
2860 static int add_bound_rdev(struct md_rdev *rdev)
2862 struct mddev *mddev = rdev->mddev;
2864 bool add_journal = test_bit(Journal, &rdev->flags);
2866 if (!mddev->pers->hot_remove_disk || add_journal) {
2867 /* If there is hot_add_disk but no hot_remove_disk
2868 * then added disks for geometry changes,
2869 * and should be added immediately.
2871 super_types[mddev->major_version].
2872 validate_super(mddev, rdev);
2874 mddev_suspend(mddev);
2875 err = mddev->pers->hot_add_disk(mddev, rdev);
2877 mddev_resume(mddev);
2879 md_kick_rdev_from_array(rdev);
2883 sysfs_notify_dirent_safe(rdev->sysfs_state);
2885 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2886 if (mddev->degraded)
2887 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2888 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2889 md_new_event(mddev);
2890 md_wakeup_thread(mddev->thread);
2894 /* words written to sysfs files may, or may not, be \n terminated.
2895 * We want to accept with case. For this we use cmd_match.
2897 static int cmd_match(const char *cmd, const char *str)
2899 /* See if cmd, written into a sysfs file, matches
2900 * str. They must either be the same, or cmd can
2901 * have a trailing newline
2903 while (*cmd && *str && *cmd == *str) {
2914 struct rdev_sysfs_entry {
2915 struct attribute attr;
2916 ssize_t (*show)(struct md_rdev *, char *);
2917 ssize_t (*store)(struct md_rdev *, const char *, size_t);
2921 state_show(struct md_rdev *rdev, char *page)
2925 unsigned long flags = READ_ONCE(rdev->flags);
2927 if (test_bit(Faulty, &flags) ||
2928 (!test_bit(ExternalBbl, &flags) &&
2929 rdev->badblocks.unacked_exist))
2930 len += sprintf(page+len, "faulty%s", sep);
2931 if (test_bit(In_sync, &flags))
2932 len += sprintf(page+len, "in_sync%s", sep);
2933 if (test_bit(Journal, &flags))
2934 len += sprintf(page+len, "journal%s", sep);
2935 if (test_bit(WriteMostly, &flags))
2936 len += sprintf(page+len, "write_mostly%s", sep);
2937 if (test_bit(Blocked, &flags) ||
2938 (rdev->badblocks.unacked_exist
2939 && !test_bit(Faulty, &flags)))
2940 len += sprintf(page+len, "blocked%s", sep);
2941 if (!test_bit(Faulty, &flags) &&
2942 !test_bit(Journal, &flags) &&
2943 !test_bit(In_sync, &flags))
2944 len += sprintf(page+len, "spare%s", sep);
2945 if (test_bit(WriteErrorSeen, &flags))
2946 len += sprintf(page+len, "write_error%s", sep);
2947 if (test_bit(WantReplacement, &flags))
2948 len += sprintf(page+len, "want_replacement%s", sep);
2949 if (test_bit(Replacement, &flags))
2950 len += sprintf(page+len, "replacement%s", sep);
2951 if (test_bit(ExternalBbl, &flags))
2952 len += sprintf(page+len, "external_bbl%s", sep);
2953 if (test_bit(FailFast, &flags))
2954 len += sprintf(page+len, "failfast%s", sep);
2959 return len+sprintf(page+len, "\n");
2963 state_store(struct md_rdev *rdev, const char *buf, size_t len)
2966 * faulty - simulates an error
2967 * remove - disconnects the device
2968 * writemostly - sets write_mostly
2969 * -writemostly - clears write_mostly
2970 * blocked - sets the Blocked flags
2971 * -blocked - clears the Blocked and possibly simulates an error
2972 * insync - sets Insync providing device isn't active
2973 * -insync - clear Insync for a device with a slot assigned,
2974 * so that it gets rebuilt based on bitmap
2975 * write_error - sets WriteErrorSeen
2976 * -write_error - clears WriteErrorSeen
2977 * {,-}failfast - set/clear FailFast
2980 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2981 md_error(rdev->mddev, rdev);
2982 if (test_bit(Faulty, &rdev->flags))
2986 } else if (cmd_match(buf, "remove")) {
2987 if (rdev->mddev->pers) {
2988 clear_bit(Blocked, &rdev->flags);
2989 remove_and_add_spares(rdev->mddev, rdev);
2991 if (rdev->raid_disk >= 0)
2994 struct mddev *mddev = rdev->mddev;
2996 if (mddev_is_clustered(mddev))
2997 err = md_cluster_ops->remove_disk(mddev, rdev);
3000 md_kick_rdev_from_array(rdev);
3002 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
3003 md_wakeup_thread(mddev->thread);
3005 md_new_event(mddev);
3008 } else if (cmd_match(buf, "writemostly")) {
3009 set_bit(WriteMostly, &rdev->flags);
3010 mddev_create_serial_pool(rdev->mddev, rdev, false);
3012 } else if (cmd_match(buf, "-writemostly")) {
3013 mddev_destroy_serial_pool(rdev->mddev, rdev, false);
3014 clear_bit(WriteMostly, &rdev->flags);
3016 } else if (cmd_match(buf, "blocked")) {
3017 set_bit(Blocked, &rdev->flags);
3019 } else if (cmd_match(buf, "-blocked")) {
3020 if (!test_bit(Faulty, &rdev->flags) &&
3021 !test_bit(ExternalBbl, &rdev->flags) &&
3022 rdev->badblocks.unacked_exist) {
3023 /* metadata handler doesn't understand badblocks,
3024 * so we need to fail the device
3026 md_error(rdev->mddev, rdev);
3028 clear_bit(Blocked, &rdev->flags);
3029 clear_bit(BlockedBadBlocks, &rdev->flags);
3030 wake_up(&rdev->blocked_wait);
3031 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
3032 md_wakeup_thread(rdev->mddev->thread);
3035 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
3036 set_bit(In_sync, &rdev->flags);
3038 } else if (cmd_match(buf, "failfast")) {
3039 set_bit(FailFast, &rdev->flags);
3041 } else if (cmd_match(buf, "-failfast")) {
3042 clear_bit(FailFast, &rdev->flags);
3044 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
3045 !test_bit(Journal, &rdev->flags)) {
3046 if (rdev->mddev->pers == NULL) {
3047 clear_bit(In_sync, &rdev->flags);
3048 rdev->saved_raid_disk = rdev->raid_disk;
3049 rdev->raid_disk = -1;
3052 } else if (cmd_match(buf, "write_error")) {
3053 set_bit(WriteErrorSeen, &rdev->flags);
3055 } else if (cmd_match(buf, "-write_error")) {
3056 clear_bit(WriteErrorSeen, &rdev->flags);
3058 } else if (cmd_match(buf, "want_replacement")) {
3059 /* Any non-spare device that is not a replacement can
3060 * become want_replacement at any time, but we then need to
3061 * check if recovery is needed.
3063 if (rdev->raid_disk >= 0 &&
3064 !test_bit(Journal, &rdev->flags) &&
3065 !test_bit(Replacement, &rdev->flags))
3066 set_bit(WantReplacement, &rdev->flags);
3067 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
3068 md_wakeup_thread(rdev->mddev->thread);
3070 } else if (cmd_match(buf, "-want_replacement")) {
3071 /* Clearing 'want_replacement' is always allowed.
3072 * Once replacements starts it is too late though.
3075 clear_bit(WantReplacement, &rdev->flags);
3076 } else if (cmd_match(buf, "replacement")) {
3077 /* Can only set a device as a replacement when array has not
3078 * yet been started. Once running, replacement is automatic
3079 * from spares, or by assigning 'slot'.
3081 if (rdev->mddev->pers)
3084 set_bit(Replacement, &rdev->flags);
3087 } else if (cmd_match(buf, "-replacement")) {
3088 /* Similarly, can only clear Replacement before start */
3089 if (rdev->mddev->pers)
3092 clear_bit(Replacement, &rdev->flags);
3095 } else if (cmd_match(buf, "re-add")) {
3096 if (!rdev->mddev->pers)
3098 else if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1) &&
3099 rdev->saved_raid_disk >= 0) {
3100 /* clear_bit is performed _after_ all the devices
3101 * have their local Faulty bit cleared. If any writes
3102 * happen in the meantime in the local node, they
3103 * will land in the local bitmap, which will be synced
3104 * by this node eventually
3106 if (!mddev_is_clustered(rdev->mddev) ||
3107 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
3108 clear_bit(Faulty, &rdev->flags);
3109 err = add_bound_rdev(rdev);
3113 } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) {
3114 set_bit(ExternalBbl, &rdev->flags);
3115 rdev->badblocks.shift = 0;
3117 } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) {
3118 clear_bit(ExternalBbl, &rdev->flags);
3122 sysfs_notify_dirent_safe(rdev->sysfs_state);
3123 return err ? err : len;
3125 static struct rdev_sysfs_entry rdev_state =
3126 __ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
3129 errors_show(struct md_rdev *rdev, char *page)
3131 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
3135 errors_store(struct md_rdev *rdev, const char *buf, size_t len)
3140 rv = kstrtouint(buf, 10, &n);
3143 atomic_set(&rdev->corrected_errors, n);
3146 static struct rdev_sysfs_entry rdev_errors =
3147 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
3150 slot_show(struct md_rdev *rdev, char *page)
3152 if (test_bit(Journal, &rdev->flags))
3153 return sprintf(page, "journal\n");
3154 else if (rdev->raid_disk < 0)
3155 return sprintf(page, "none\n");
3157 return sprintf(page, "%d\n", rdev->raid_disk);
3161 slot_store(struct md_rdev *rdev, const char *buf, size_t len)
3166 if (test_bit(Journal, &rdev->flags))
3168 if (strncmp(buf, "none", 4)==0)
3171 err = kstrtouint(buf, 10, (unsigned int *)&slot);
3175 if (rdev->mddev->pers && slot == -1) {
3176 /* Setting 'slot' on an active array requires also
3177 * updating the 'rd%d' link, and communicating
3178 * with the personality with ->hot_*_disk.
3179 * For now we only support removing
3180 * failed/spare devices. This normally happens automatically,
3181 * but not when the metadata is externally managed.
3183 if (rdev->raid_disk == -1)
3185 /* personality does all needed checks */
3186 if (rdev->mddev->pers->hot_remove_disk == NULL)
3188 clear_bit(Blocked, &rdev->flags);
3189 remove_and_add_spares(rdev->mddev, rdev);
3190 if (rdev->raid_disk >= 0)
3192 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
3193 md_wakeup_thread(rdev->mddev->thread);
3194 } else if (rdev->mddev->pers) {
3195 /* Activating a spare .. or possibly reactivating
3196 * if we ever get bitmaps working here.
3200 if (rdev->raid_disk != -1)
3203 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
3206 if (rdev->mddev->pers->hot_add_disk == NULL)
3209 if (slot >= rdev->mddev->raid_disks &&
3210 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
3213 rdev->raid_disk = slot;
3214 if (test_bit(In_sync, &rdev->flags))
3215 rdev->saved_raid_disk = slot;
3217 rdev->saved_raid_disk = -1;
3218 clear_bit(In_sync, &rdev->flags);
3219 clear_bit(Bitmap_sync, &rdev->flags);
3220 err = rdev->mddev->pers->hot_add_disk(rdev->mddev, rdev);
3222 rdev->raid_disk = -1;
3225 sysfs_notify_dirent_safe(rdev->sysfs_state);
3226 /* failure here is OK */;
3227 sysfs_link_rdev(rdev->mddev, rdev);
3228 /* don't wakeup anyone, leave that to userspace. */
3230 if (slot >= rdev->mddev->raid_disks &&
3231 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
3233 rdev->raid_disk = slot;
3234 /* assume it is working */
3235 clear_bit(Faulty, &rdev->flags);
3236 clear_bit(WriteMostly, &rdev->flags);
3237 set_bit(In_sync, &rdev->flags);
3238 sysfs_notify_dirent_safe(rdev->sysfs_state);
3243 static struct rdev_sysfs_entry rdev_slot =
3244 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
3247 offset_show(struct md_rdev *rdev, char *page)
3249 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
3253 offset_store(struct md_rdev *rdev, const char *buf, size_t len)
3255 unsigned long long offset;
3256 if (kstrtoull(buf, 10, &offset) < 0)
3258 if (rdev->mddev->pers && rdev->raid_disk >= 0)
3260 if (rdev->sectors && rdev->mddev->external)
3261 /* Must set offset before size, so overlap checks
3264 rdev->data_offset = offset;
3265 rdev->new_data_offset = offset;
3269 static struct rdev_sysfs_entry rdev_offset =
3270 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
3272 static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
3274 return sprintf(page, "%llu\n",
3275 (unsigned long long)rdev->new_data_offset);
3278 static ssize_t new_offset_store(struct md_rdev *rdev,
3279 const char *buf, size_t len)
3281 unsigned long long new_offset;
3282 struct mddev *mddev = rdev->mddev;
3284 if (kstrtoull(buf, 10, &new_offset) < 0)
3287 if (mddev->sync_thread ||
3288 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
3290 if (new_offset == rdev->data_offset)
3291 /* reset is always permitted */
3293 else if (new_offset > rdev->data_offset) {
3294 /* must not push array size beyond rdev_sectors */
3295 if (new_offset - rdev->data_offset
3296 + mddev->dev_sectors > rdev->sectors)
3299 /* Metadata worries about other space details. */
3301 /* decreasing the offset is inconsistent with a backwards
3304 if (new_offset < rdev->data_offset &&
3305 mddev->reshape_backwards)
3307 /* Increasing offset is inconsistent with forwards
3308 * reshape. reshape_direction should be set to
3309 * 'backwards' first.
3311 if (new_offset > rdev->data_offset &&
3312 !mddev->reshape_backwards)
3315 if (mddev->pers && mddev->persistent &&
3316 !super_types[mddev->major_version]
3317 .allow_new_offset(rdev, new_offset))
3319 rdev->new_data_offset = new_offset;
3320 if (new_offset > rdev->data_offset)
3321 mddev->reshape_backwards = 1;
3322 else if (new_offset < rdev->data_offset)
3323 mddev->reshape_backwards = 0;
3327 static struct rdev_sysfs_entry rdev_new_offset =
3328 __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
3331 rdev_size_show(struct md_rdev *rdev, char *page)
3333 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
3336 static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
3338 /* check if two start/length pairs overlap */
3346 static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
3348 unsigned long long blocks;
3351 if (kstrtoull(buf, 10, &blocks) < 0)
3354 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
3355 return -EINVAL; /* sector conversion overflow */
3358 if (new != blocks * 2)
3359 return -EINVAL; /* unsigned long long to sector_t overflow */
3366 rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3368 struct mddev *my_mddev = rdev->mddev;
3369 sector_t oldsectors = rdev->sectors;
3372 if (test_bit(Journal, &rdev->flags))
3374 if (strict_blocks_to_sectors(buf, §ors) < 0)
3376 if (rdev->data_offset != rdev->new_data_offset)
3377 return -EINVAL; /* too confusing */
3378 if (my_mddev->pers && rdev->raid_disk >= 0) {
3379 if (my_mddev->persistent) {
3380 sectors = super_types[my_mddev->major_version].
3381 rdev_size_change(rdev, sectors);
3384 } else if (!sectors)
3385 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
3387 if (!my_mddev->pers->resize)
3388 /* Cannot change size for RAID0 or Linear etc */
3391 if (sectors < my_mddev->dev_sectors)
3392 return -EINVAL; /* component must fit device */
3394 rdev->sectors = sectors;
3395 if (sectors > oldsectors && my_mddev->external) {
3396 /* Need to check that all other rdevs with the same
3397 * ->bdev do not overlap. 'rcu' is sufficient to walk
3398 * the rdev lists safely.
3399 * This check does not provide a hard guarantee, it
3400 * just helps avoid dangerous mistakes.
3402 struct mddev *mddev;
3404 struct list_head *tmp;
3407 for_each_mddev(mddev, tmp) {
3408 struct md_rdev *rdev2;
3410 rdev_for_each(rdev2, mddev)
3411 if (rdev->bdev == rdev2->bdev &&
3413 overlaps(rdev->data_offset, rdev->sectors,
3426 /* Someone else could have slipped in a size
3427 * change here, but doing so is just silly.
3428 * We put oldsectors back because we *know* it is
3429 * safe, and trust userspace not to race with
3432 rdev->sectors = oldsectors;
3439 static struct rdev_sysfs_entry rdev_size =
3440 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
3442 static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
3444 unsigned long long recovery_start = rdev->recovery_offset;
3446 if (test_bit(In_sync, &rdev->flags) ||
3447 recovery_start == MaxSector)
3448 return sprintf(page, "none\n");
3450 return sprintf(page, "%llu\n", recovery_start);
3453 static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
3455 unsigned long long recovery_start;
3457 if (cmd_match(buf, "none"))
3458 recovery_start = MaxSector;
3459 else if (kstrtoull(buf, 10, &recovery_start))
3462 if (rdev->mddev->pers &&
3463 rdev->raid_disk >= 0)
3466 rdev->recovery_offset = recovery_start;
3467 if (recovery_start == MaxSector)
3468 set_bit(In_sync, &rdev->flags);
3470 clear_bit(In_sync, &rdev->flags);
3474 static struct rdev_sysfs_entry rdev_recovery_start =
3475 __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3477 /* sysfs access to bad-blocks list.
3478 * We present two files.
3479 * 'bad-blocks' lists sector numbers and lengths of ranges that
3480 * are recorded as bad. The list is truncated to fit within
3481 * the one-page limit of sysfs.
3482 * Writing "sector length" to this file adds an acknowledged
3484 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3485 * been acknowledged. Writing to this file adds bad blocks
3486 * without acknowledging them. This is largely for testing.
3488 static ssize_t bb_show(struct md_rdev *rdev, char *page)
3490 return badblocks_show(&rdev->badblocks, page, 0);
3492 static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
3494 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3495 /* Maybe that ack was all we needed */
3496 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3497 wake_up(&rdev->blocked_wait);
3500 static struct rdev_sysfs_entry rdev_bad_blocks =
3501 __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3503 static ssize_t ubb_show(struct md_rdev *rdev, char *page)
3505 return badblocks_show(&rdev->badblocks, page, 1);
3507 static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
3509 return badblocks_store(&rdev->badblocks, page, len, 1);
3511 static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3512 __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3515 ppl_sector_show(struct md_rdev *rdev, char *page)
3517 return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector);
3521 ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len)
3523 unsigned long long sector;
3525 if (kstrtoull(buf, 10, §or) < 0)
3527 if (sector != (sector_t)sector)
3530 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3531 rdev->raid_disk >= 0)
3534 if (rdev->mddev->persistent) {
3535 if (rdev->mddev->major_version == 0)
3537 if ((sector > rdev->sb_start &&
3538 sector - rdev->sb_start > S16_MAX) ||
3539 (sector < rdev->sb_start &&
3540 rdev->sb_start - sector > -S16_MIN))
3542 rdev->ppl.offset = sector - rdev->sb_start;
3543 } else if (!rdev->mddev->external) {
3546 rdev->ppl.sector = sector;
3550 static struct rdev_sysfs_entry rdev_ppl_sector =
3551 __ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store);
3554 ppl_size_show(struct md_rdev *rdev, char *page)
3556 return sprintf(page, "%u\n", rdev->ppl.size);
3560 ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3564 if (kstrtouint(buf, 10, &size) < 0)
3567 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3568 rdev->raid_disk >= 0)
3571 if (rdev->mddev->persistent) {
3572 if (rdev->mddev->major_version == 0)
3576 } else if (!rdev->mddev->external) {
3579 rdev->ppl.size = size;
3583 static struct rdev_sysfs_entry rdev_ppl_size =
3584 __ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store);
3586 static struct attribute *rdev_default_attrs[] = {
3591 &rdev_new_offset.attr,
3593 &rdev_recovery_start.attr,
3594 &rdev_bad_blocks.attr,
3595 &rdev_unack_bad_blocks.attr,
3596 &rdev_ppl_sector.attr,
3597 &rdev_ppl_size.attr,
3601 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3603 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
3604 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
3610 return entry->show(rdev, page);
3614 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3615 const char *page, size_t length)
3617 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
3618 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
3620 struct mddev *mddev = rdev->mddev;
3624 if (!capable(CAP_SYS_ADMIN))
3626 rv = mddev ? mddev_lock(mddev) : -ENODEV;
3628 if (rdev->mddev == NULL)
3631 rv = entry->store(rdev, page, length);
3632 mddev_unlock(mddev);
3637 static void rdev_free(struct kobject *ko)
3639 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
3642 static const struct sysfs_ops rdev_sysfs_ops = {
3643 .show = rdev_attr_show,
3644 .store = rdev_attr_store,
3646 static struct kobj_type rdev_ktype = {
3647 .release = rdev_free,
3648 .sysfs_ops = &rdev_sysfs_ops,
3649 .default_attrs = rdev_default_attrs,
3652 int md_rdev_init(struct md_rdev *rdev)
3655 rdev->saved_raid_disk = -1;
3656 rdev->raid_disk = -1;
3658 rdev->data_offset = 0;
3659 rdev->new_data_offset = 0;
3660 rdev->sb_events = 0;
3661 rdev->last_read_error = 0;
3662 rdev->sb_loaded = 0;
3663 rdev->bb_page = NULL;
3664 atomic_set(&rdev->nr_pending, 0);
3665 atomic_set(&rdev->read_errors, 0);
3666 atomic_set(&rdev->corrected_errors, 0);
3668 INIT_LIST_HEAD(&rdev->same_set);
3669 init_waitqueue_head(&rdev->blocked_wait);
3671 /* Add space to store bad block list.
3672 * This reserves the space even on arrays where it cannot
3673 * be used - I wonder if that matters
3675 return badblocks_init(&rdev->badblocks, 0);
3677 EXPORT_SYMBOL_GPL(md_rdev_init);
3679 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3681 * mark the device faulty if:
3683 * - the device is nonexistent (zero size)
3684 * - the device has no valid superblock
3686 * a faulty rdev _never_ has rdev->sb set.
3688 static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
3690 char b[BDEVNAME_SIZE];
3692 struct md_rdev *rdev;
3695 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
3697 return ERR_PTR(-ENOMEM);
3699 err = md_rdev_init(rdev);
3702 err = alloc_disk_sb(rdev);
3706 err = lock_rdev(rdev, newdev, super_format == -2);
3710 kobject_init(&rdev->kobj, &rdev_ktype);
3712 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
3714 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
3715 bdevname(rdev->bdev,b));
3720 if (super_format >= 0) {
3721 err = super_types[super_format].
3722 load_super(rdev, NULL, super_minor);
3723 if (err == -EINVAL) {
3724 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
3725 bdevname(rdev->bdev,b),
3726 super_format, super_minor);
3730 pr_warn("md: could not read %s's sb, not importing!\n",
3731 bdevname(rdev->bdev,b));
3741 md_rdev_clear(rdev);
3743 return ERR_PTR(err);
3747 * Check a full RAID array for plausibility
3750 static int analyze_sbs(struct mddev *mddev)
3753 struct md_rdev *rdev, *freshest, *tmp;
3754 char b[BDEVNAME_SIZE];
3757 rdev_for_each_safe(rdev, tmp, mddev)
3758 switch (super_types[mddev->major_version].
3759 load_super(rdev, freshest, mddev->minor_version)) {
3766 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
3767 bdevname(rdev->bdev,b));
3768 md_kick_rdev_from_array(rdev);
3771 /* Cannot find a valid fresh disk */
3773 pr_warn("md: cannot find a valid disk\n");
3777 super_types[mddev->major_version].
3778 validate_super(mddev, freshest);
3781 rdev_for_each_safe(rdev, tmp, mddev) {
3782 if (mddev->max_disks &&
3783 (rdev->desc_nr >= mddev->max_disks ||
3784 i > mddev->max_disks)) {
3785 pr_warn("md: %s: %s: only %d devices permitted\n",
3786 mdname(mddev), bdevname(rdev->bdev, b),
3788 md_kick_rdev_from_array(rdev);
3791 if (rdev != freshest) {
3792 if (super_types[mddev->major_version].
3793 validate_super(mddev, rdev)) {
3794 pr_warn("md: kicking non-fresh %s from array!\n",
3795 bdevname(rdev->bdev,b));
3796 md_kick_rdev_from_array(rdev);
3800 if (mddev->level == LEVEL_MULTIPATH) {
3801 rdev->desc_nr = i++;
3802 rdev->raid_disk = rdev->desc_nr;
3803 set_bit(In_sync, &rdev->flags);
3804 } else if (rdev->raid_disk >=
3805 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3806 !test_bit(Journal, &rdev->flags)) {
3807 rdev->raid_disk = -1;
3808 clear_bit(In_sync, &rdev->flags);
3815 /* Read a fixed-point number.
3816 * Numbers in sysfs attributes should be in "standard" units where
3817 * possible, so time should be in seconds.
3818 * However we internally use a a much smaller unit such as
3819 * milliseconds or jiffies.
3820 * This function takes a decimal number with a possible fractional
3821 * component, and produces an integer which is the result of
3822 * multiplying that number by 10^'scale'.
3823 * all without any floating-point arithmetic.
3825 int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3827 unsigned long result = 0;
3829 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3832 else if (decimals < scale) {
3835 result = result * 10 + value;
3847 *res = result * int_pow(10, scale - decimals);
3852 safe_delay_show(struct mddev *mddev, char *page)
3854 int msec = (mddev->safemode_delay*1000)/HZ;
3855 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3858 safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
3862 if (mddev_is_clustered(mddev)) {
3863 pr_warn("md: Safemode is disabled for clustered mode\n");
3867 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
3870 mddev->safemode_delay = 0;
3872 unsigned long old_delay = mddev->safemode_delay;
3873 unsigned long new_delay = (msec*HZ)/1000;
3877 mddev->safemode_delay = new_delay;
3878 if (new_delay < old_delay || old_delay == 0)
3879 mod_timer(&mddev->safemode_timer, jiffies+1);
3883 static struct md_sysfs_entry md_safe_delay =
3884 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
3887 level_show(struct mddev *mddev, char *page)
3889 struct md_personality *p;
3891 spin_lock(&mddev->lock);
3894 ret = sprintf(page, "%s\n", p->name);
3895 else if (mddev->clevel[0])
3896 ret = sprintf(page, "%s\n", mddev->clevel);
3897 else if (mddev->level != LEVEL_NONE)
3898 ret = sprintf(page, "%d\n", mddev->level);
3901 spin_unlock(&mddev->lock);
3906 level_store(struct mddev *mddev, const char *buf, size_t len)
3911 struct md_personality *pers, *oldpers;
3913 void *priv, *oldpriv;
3914 struct md_rdev *rdev;
3916 if (slen == 0 || slen >= sizeof(clevel))
3919 rv = mddev_lock(mddev);
3923 if (mddev->pers == NULL) {
3924 strncpy(mddev->clevel, buf, slen);
3925 if (mddev->clevel[slen-1] == '\n')
3927 mddev->clevel[slen] = 0;
3928 mddev->level = LEVEL_NONE;
3936 /* request to change the personality. Need to ensure:
3937 * - array is not engaged in resync/recovery/reshape
3938 * - old personality can be suspended
3939 * - new personality will access other array.
3943 if (mddev->sync_thread ||
3944 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3945 mddev->reshape_position != MaxSector ||
3946 mddev->sysfs_active)
3950 if (!mddev->pers->quiesce) {
3951 pr_warn("md: %s: %s does not support online personality change\n",
3952 mdname(mddev), mddev->pers->name);
3956 /* Now find the new personality */
3957 strncpy(clevel, buf, slen);
3958 if (clevel[slen-1] == '\n')
3961 if (kstrtol(clevel, 10, &level))
3964 if (request_module("md-%s", clevel) != 0)
3965 request_module("md-level-%s", clevel);
3966 spin_lock(&pers_lock);
3967 pers = find_pers(level, clevel);
3968 if (!pers || !try_module_get(pers->owner)) {
3969 spin_unlock(&pers_lock);
3970 pr_warn("md: personality %s not loaded\n", clevel);
3974 spin_unlock(&pers_lock);
3976 if (pers == mddev->pers) {
3977 /* Nothing to do! */
3978 module_put(pers->owner);
3982 if (!pers->takeover) {
3983 module_put(pers->owner);
3984 pr_warn("md: %s: %s does not support personality takeover\n",
3985 mdname(mddev), clevel);
3990 rdev_for_each(rdev, mddev)
3991 rdev->new_raid_disk = rdev->raid_disk;
3993 /* ->takeover must set new_* and/or delta_disks
3994 * if it succeeds, and may set them when it fails.
3996 priv = pers->takeover(mddev);
3998 mddev->new_level = mddev->level;
3999 mddev->new_layout = mddev->layout;
4000 mddev->new_chunk_sectors = mddev->chunk_sectors;
4001 mddev->raid_disks -= mddev->delta_disks;
4002 mddev->delta_disks = 0;
4003 mddev->reshape_backwards = 0;
4004 module_put(pers->owner);
4005 pr_warn("md: %s: %s would not accept array\n",
4006 mdname(mddev), clevel);
4011 /* Looks like we have a winner */
4012 mddev_suspend(mddev);
4013 mddev_detach(mddev);
4015 spin_lock(&mddev->lock);
4016 oldpers = mddev->pers;
4017 oldpriv = mddev->private;
4019 mddev->private = priv;
4020 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
4021 mddev->level = mddev->new_level;
4022 mddev->layout = mddev->new_layout;
4023 mddev->chunk_sectors = mddev->new_chunk_sectors;
4024 mddev->delta_disks = 0;
4025 mddev->reshape_backwards = 0;
4026 mddev->degraded = 0;
4027 spin_unlock(&mddev->lock);
4029 if (oldpers->sync_request == NULL &&
4031 /* We are converting from a no-redundancy array
4032 * to a redundancy array and metadata is managed
4033 * externally so we need to be sure that writes
4034 * won't block due to a need to transition
4036 * until external management is started.
4039 mddev->safemode_delay = 0;
4040 mddev->safemode = 0;
4043 oldpers->free(mddev, oldpriv);
4045 if (oldpers->sync_request == NULL &&
4046 pers->sync_request != NULL) {
4047 /* need to add the md_redundancy_group */
4048 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4049 pr_warn("md: cannot register extra attributes for %s\n",
4051 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
4052 mddev->sysfs_completed = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_completed");
4053 mddev->sysfs_degraded = sysfs_get_dirent_safe(mddev->kobj.sd, "degraded");
4055 if (oldpers->sync_request != NULL &&
4056 pers->sync_request == NULL) {
4057 /* need to remove the md_redundancy_group */
4058 if (mddev->to_remove == NULL)
4059 mddev->to_remove = &md_redundancy_group;
4062 module_put(oldpers->owner);
4064 rdev_for_each(rdev, mddev) {
4065 if (rdev->raid_disk < 0)
4067 if (rdev->new_raid_disk >= mddev->raid_disks)
4068 rdev->new_raid_disk = -1;
4069 if (rdev->new_raid_disk == rdev->raid_disk)
4071 sysfs_unlink_rdev(mddev, rdev);
4073 rdev_for_each(rdev, mddev) {
4074 if (rdev->raid_disk < 0)
4076 if (rdev->new_raid_disk == rdev->raid_disk)
4078 rdev->raid_disk = rdev->new_raid_disk;
4079 if (rdev->raid_disk < 0)
4080 clear_bit(In_sync, &rdev->flags);
4082 if (sysfs_link_rdev(mddev, rdev))
4083 pr_warn("md: cannot register rd%d for %s after level change\n",
4084 rdev->raid_disk, mdname(mddev));
4088 if (pers->sync_request == NULL) {
4089 /* this is now an array without redundancy, so
4090 * it must always be in_sync
4093 del_timer_sync(&mddev->safemode_timer);
4095 blk_set_stacking_limits(&mddev->queue->limits);
4097 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
4098 mddev_resume(mddev);
4100 md_update_sb(mddev, 1);
4101 sysfs_notify_dirent_safe(mddev->sysfs_level);
4102 md_new_event(mddev);
4105 mddev_unlock(mddev);
4109 static struct md_sysfs_entry md_level =
4110 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
4113 layout_show(struct mddev *mddev, char *page)
4115 /* just a number, not meaningful for all levels */
4116 if (mddev->reshape_position != MaxSector &&
4117 mddev->layout != mddev->new_layout)
4118 return sprintf(page, "%d (%d)\n",
4119 mddev->new_layout, mddev->layout);
4120 return sprintf(page, "%d\n", mddev->layout);
4124 layout_store(struct mddev *mddev, const char *buf, size_t len)
4129 err = kstrtouint(buf, 10, &n);
4132 err = mddev_lock(mddev);
4137 if (mddev->pers->check_reshape == NULL)
4142 mddev->new_layout = n;
4143 err = mddev->pers->check_reshape(mddev);
4145 mddev->new_layout = mddev->layout;
4148 mddev->new_layout = n;
4149 if (mddev->reshape_position == MaxSector)
4152 mddev_unlock(mddev);
4155 static struct md_sysfs_entry md_layout =
4156 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
4159 raid_disks_show(struct mddev *mddev, char *page)
4161 if (mddev->raid_disks == 0)
4163 if (mddev->reshape_position != MaxSector &&
4164 mddev->delta_disks != 0)
4165 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
4166 mddev->raid_disks - mddev->delta_disks);
4167 return sprintf(page, "%d\n", mddev->raid_disks);
4170 static int update_raid_disks(struct mddev *mddev, int raid_disks);
4173 raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
4178 err = kstrtouint(buf, 10, &n);
4182 err = mddev_lock(mddev);
4186 err = update_raid_disks(mddev, n);
4187 else if (mddev->reshape_position != MaxSector) {
4188 struct md_rdev *rdev;
4189 int olddisks = mddev->raid_disks - mddev->delta_disks;
4192 rdev_for_each(rdev, mddev) {
4194 rdev->data_offset < rdev->new_data_offset)
4197 rdev->data_offset > rdev->new_data_offset)
4201 mddev->delta_disks = n - olddisks;
4202 mddev->raid_disks = n;
4203 mddev->reshape_backwards = (mddev->delta_disks < 0);
4205 mddev->raid_disks = n;
4207 mddev_unlock(mddev);
4208 return err ? err : len;
4210 static struct md_sysfs_entry md_raid_disks =
4211 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
4214 uuid_show(struct mddev *mddev, char *page)
4216 return sprintf(page, "%pU\n", mddev->uuid);
4218 static struct md_sysfs_entry md_uuid =
4219 __ATTR(uuid, S_IRUGO, uuid_show, NULL);
4222 chunk_size_show(struct mddev *mddev, char *page)
4224 if (mddev->reshape_position != MaxSector &&
4225 mddev->chunk_sectors != mddev->new_chunk_sectors)
4226 return sprintf(page, "%d (%d)\n",
4227 mddev->new_chunk_sectors << 9,
4228 mddev->chunk_sectors << 9);
4229 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
4233 chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
4238 err = kstrtoul(buf, 10, &n);
4242 err = mddev_lock(mddev);
4246 if (mddev->pers->check_reshape == NULL)
4251 mddev->new_chunk_sectors = n >> 9;
4252 err = mddev->pers->check_reshape(mddev);
4254 mddev->new_chunk_sectors = mddev->chunk_sectors;
4257 mddev->new_chunk_sectors = n >> 9;
4258 if (mddev->reshape_position == MaxSector)
4259 mddev->chunk_sectors = n >> 9;
4261 mddev_unlock(mddev);
4264 static struct md_sysfs_entry md_chunk_size =
4265 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
4268 resync_start_show(struct mddev *mddev, char *page)
4270 if (mddev->recovery_cp == MaxSector)
4271 return sprintf(page, "none\n");
4272 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
4276 resync_start_store(struct mddev *mddev, const char *buf, size_t len)
4278 unsigned long long n;
4281 if (cmd_match(buf, "none"))
4284 err = kstrtoull(buf, 10, &n);
4287 if (n != (sector_t)n)
4291 err = mddev_lock(mddev);
4294 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4298 mddev->recovery_cp = n;
4300 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
4302 mddev_unlock(mddev);
4305 static struct md_sysfs_entry md_resync_start =
4306 __ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
4307 resync_start_show, resync_start_store);
4310 * The array state can be:
4313 * No devices, no size, no level
4314 * Equivalent to STOP_ARRAY ioctl
4316 * May have some settings, but array is not active
4317 * all IO results in error
4318 * When written, doesn't tear down array, but just stops it
4319 * suspended (not supported yet)
4320 * All IO requests will block. The array can be reconfigured.
4321 * Writing this, if accepted, will block until array is quiescent
4323 * no resync can happen. no superblocks get written.
4324 * write requests fail
4326 * like readonly, but behaves like 'clean' on a write request.
4328 * clean - no pending writes, but otherwise active.
4329 * When written to inactive array, starts without resync
4330 * If a write request arrives then
4331 * if metadata is known, mark 'dirty' and switch to 'active'.
4332 * if not known, block and switch to write-pending
4333 * If written to an active array that has pending writes, then fails.
4335 * fully active: IO and resync can be happening.
4336 * When written to inactive array, starts with resync
4339 * clean, but writes are blocked waiting for 'active' to be written.
4342 * like active, but no writes have been seen for a while (100msec).
4345 * RAID0/LINEAR-only: same as clean, but array is missing a member.
4346 * It's useful because RAID0/LINEAR mounted-arrays aren't stopped
4347 * when a member is gone, so this state will at least alert the
4348 * user that something is wrong.
4350 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
4351 write_pending, active_idle, broken, bad_word};
4352 static char *array_states[] = {
4353 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
4354 "write-pending", "active-idle", "broken", NULL };
4356 static int match_word(const char *word, char **list)
4359 for (n=0; list[n]; n++)
4360 if (cmd_match(word, list[n]))
4366 array_state_show(struct mddev *mddev, char *page)
4368 enum array_state st = inactive;
4370 if (mddev->pers && !test_bit(MD_NOT_READY, &mddev->flags)) {
4379 spin_lock(&mddev->lock);
4380 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
4382 else if (mddev->in_sync)
4384 else if (mddev->safemode)
4388 spin_unlock(&mddev->lock);
4391 if (test_bit(MD_BROKEN, &mddev->flags) && st == clean)
4394 if (list_empty(&mddev->disks) &&
4395 mddev->raid_disks == 0 &&
4396 mddev->dev_sectors == 0)
4401 return sprintf(page, "%s\n", array_states[st]);
4404 static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
4405 static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
4406 static int restart_array(struct mddev *mddev);
4409 array_state_store(struct mddev *mddev, const char *buf, size_t len)
4412 enum array_state st = match_word(buf, array_states);
4414 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
4415 /* don't take reconfig_mutex when toggling between
4418 spin_lock(&mddev->lock);
4420 restart_array(mddev);
4421 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
4422 md_wakeup_thread(mddev->thread);
4423 wake_up(&mddev->sb_wait);
4424 } else /* st == clean */ {
4425 restart_array(mddev);
4426 if (!set_in_sync(mddev))
4430 sysfs_notify_dirent_safe(mddev->sysfs_state);
4431 spin_unlock(&mddev->lock);
4434 err = mddev_lock(mddev);
4442 /* stopping an active array */
4443 err = do_md_stop(mddev, 0, NULL);
4446 /* stopping an active array */
4448 err = do_md_stop(mddev, 2, NULL);
4450 err = 0; /* already inactive */
4453 break; /* not supported yet */
4456 err = md_set_readonly(mddev, NULL);
4459 set_disk_ro(mddev->gendisk, 1);
4460 err = do_md_run(mddev);
4466 err = md_set_readonly(mddev, NULL);
4467 else if (mddev->ro == 1)
4468 err = restart_array(mddev);
4471 set_disk_ro(mddev->gendisk, 0);
4475 err = do_md_run(mddev);
4480 err = restart_array(mddev);
4483 spin_lock(&mddev->lock);
4484 if (!set_in_sync(mddev))
4486 spin_unlock(&mddev->lock);
4492 err = restart_array(mddev);
4495 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
4496 wake_up(&mddev->sb_wait);
4500 set_disk_ro(mddev->gendisk, 0);
4501 err = do_md_run(mddev);
4507 /* these cannot be set */
4512 if (mddev->hold_active == UNTIL_IOCTL)
4513 mddev->hold_active = 0;
4514 sysfs_notify_dirent_safe(mddev->sysfs_state);
4516 mddev_unlock(mddev);
4519 static struct md_sysfs_entry md_array_state =
4520 __ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
4523 max_corrected_read_errors_show(struct mddev *mddev, char *page) {
4524 return sprintf(page, "%d\n",
4525 atomic_read(&mddev->max_corr_read_errors));
4529 max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
4534 rv = kstrtouint(buf, 10, &n);
4537 atomic_set(&mddev->max_corr_read_errors, n);
4541 static struct md_sysfs_entry max_corr_read_errors =
4542 __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4543 max_corrected_read_errors_store);
4546 null_show(struct mddev *mddev, char *page)
4551 /* need to ensure rdev_delayed_delete() has completed */
4552 static void flush_rdev_wq(struct mddev *mddev)
4554 struct md_rdev *rdev;
4557 rdev_for_each_rcu(rdev, mddev)
4558 if (work_pending(&rdev->del_work)) {
4559 flush_workqueue(md_rdev_misc_wq);
4566 new_dev_store(struct mddev *mddev, const char *buf, size_t len)
4568 /* buf must be %d:%d\n? giving major and minor numbers */
4569 /* The new device is added to the array.
4570 * If the array has a persistent superblock, we read the
4571 * superblock to initialise info and check validity.
4572 * Otherwise, only checking done is that in bind_rdev_to_array,
4573 * which mainly checks size.
4576 int major = simple_strtoul(buf, &e, 10);
4579 struct md_rdev *rdev;
4582 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4584 minor = simple_strtoul(e+1, &e, 10);
4585 if (*e && *e != '\n')
4587 dev = MKDEV(major, minor);
4588 if (major != MAJOR(dev) ||
4589 minor != MINOR(dev))
4592 flush_rdev_wq(mddev);
4593 err = mddev_lock(mddev);
4596 if (mddev->persistent) {
4597 rdev = md_import_device(dev, mddev->major_version,
4598 mddev->minor_version);
4599 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
4600 struct md_rdev *rdev0
4601 = list_entry(mddev->disks.next,
4602 struct md_rdev, same_set);
4603 err = super_types[mddev->major_version]
4604 .load_super(rdev, rdev0, mddev->minor_version);
4608 } else if (mddev->external)
4609 rdev = md_import_device(dev, -2, -1);
4611 rdev = md_import_device(dev, -1, -1);
4614 mddev_unlock(mddev);
4615 return PTR_ERR(rdev);
4617 err = bind_rdev_to_array(rdev, mddev);
4621 mddev_unlock(mddev);
4623 md_new_event(mddev);
4624 return err ? err : len;
4627 static struct md_sysfs_entry md_new_device =
4628 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
4631 bitmap_store(struct mddev *mddev, const char *buf, size_t len)
4634 unsigned long chunk, end_chunk;
4637 err = mddev_lock(mddev);
4642 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4644 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4645 if (buf == end) break;
4646 if (*end == '-') { /* range */
4648 end_chunk = simple_strtoul(buf, &end, 0);
4649 if (buf == end) break;
4651 if (*end && !isspace(*end)) break;
4652 md_bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
4653 buf = skip_spaces(end);
4655 md_bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4657 mddev_unlock(mddev);
4661 static struct md_sysfs_entry md_bitmap =
4662 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4665 size_show(struct mddev *mddev, char *page)
4667 return sprintf(page, "%llu\n",
4668 (unsigned long long)mddev->dev_sectors / 2);
4671 static int update_size(struct mddev *mddev, sector_t num_sectors);
4674 size_store(struct mddev *mddev, const char *buf, size_t len)
4676 /* If array is inactive, we can reduce the component size, but
4677 * not increase it (except from 0).
4678 * If array is active, we can try an on-line resize
4681 int err = strict_blocks_to_sectors(buf, §ors);
4685 err = mddev_lock(mddev);
4689 err = update_size(mddev, sectors);
4691 md_update_sb(mddev, 1);
4693 if (mddev->dev_sectors == 0 ||
4694 mddev->dev_sectors > sectors)
4695 mddev->dev_sectors = sectors;
4699 mddev_unlock(mddev);
4700 return err ? err : len;
4703 static struct md_sysfs_entry md_size =
4704 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
4706 /* Metadata version.
4708 * 'none' for arrays with no metadata (good luck...)
4709 * 'external' for arrays with externally managed metadata,
4710 * or N.M for internally known formats
4713 metadata_show(struct mddev *mddev, char *page)
4715 if (mddev->persistent)
4716 return sprintf(page, "%d.%d\n",
4717 mddev->major_version, mddev->minor_version);
4718 else if (mddev->external)
4719 return sprintf(page, "external:%s\n", mddev->metadata_type);
4721 return sprintf(page, "none\n");
4725 metadata_store(struct mddev *mddev, const char *buf, size_t len)
4730 /* Changing the details of 'external' metadata is
4731 * always permitted. Otherwise there must be
4732 * no devices attached to the array.
4735 err = mddev_lock(mddev);
4739 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4741 else if (!list_empty(&mddev->disks))
4745 if (cmd_match(buf, "none")) {
4746 mddev->persistent = 0;
4747 mddev->external = 0;
4748 mddev->major_version = 0;
4749 mddev->minor_version = 90;
4752 if (strncmp(buf, "external:", 9) == 0) {
4753 size_t namelen = len-9;
4754 if (namelen >= sizeof(mddev->metadata_type))
4755 namelen = sizeof(mddev->metadata_type)-1;
4756 strncpy(mddev->metadata_type, buf+9, namelen);
4757 mddev->metadata_type[namelen] = 0;
4758 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4759 mddev->metadata_type[--namelen] = 0;
4760 mddev->persistent = 0;
4761 mddev->external = 1;
4762 mddev->major_version = 0;
4763 mddev->minor_version = 90;
4766 major = simple_strtoul(buf, &e, 10);
4768 if (e==buf || *e != '.')
4771 minor = simple_strtoul(buf, &e, 10);
4772 if (e==buf || (*e && *e != '\n') )
4775 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
4777 mddev->major_version = major;
4778 mddev->minor_version = minor;
4779 mddev->persistent = 1;
4780 mddev->external = 0;
4783 mddev_unlock(mddev);
4787 static struct md_sysfs_entry md_metadata =
4788 __ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
4791 action_show(struct mddev *mddev, char *page)
4793 char *type = "idle";
4794 unsigned long recovery = mddev->recovery;
4795 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
4797 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4798 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4799 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
4801 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4802 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
4804 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
4808 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
4810 else if (mddev->reshape_position != MaxSector)
4813 return sprintf(page, "%s\n", type);
4817 action_store(struct mddev *mddev, const char *page, size_t len)
4819 if (!mddev->pers || !mddev->pers->sync_request)
4823 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
4824 if (cmd_match(page, "frozen"))
4825 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4827 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4828 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4829 mddev_lock(mddev) == 0) {
4830 if (work_pending(&mddev->del_work))
4831 flush_workqueue(md_misc_wq);
4832 if (mddev->sync_thread) {
4833 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4834 md_reap_sync_thread(mddev);
4836 mddev_unlock(mddev);
4838 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4840 else if (cmd_match(page, "resync"))
4841 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4842 else if (cmd_match(page, "recover")) {
4843 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4844 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4845 } else if (cmd_match(page, "reshape")) {
4847 if (mddev->pers->start_reshape == NULL)
4849 err = mddev_lock(mddev);
4851 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4854 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4855 err = mddev->pers->start_reshape(mddev);
4857 mddev_unlock(mddev);
4861 sysfs_notify_dirent_safe(mddev->sysfs_degraded);
4863 if (cmd_match(page, "check"))
4864 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4865 else if (!cmd_match(page, "repair"))
4867 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4868 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4869 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4871 if (mddev->ro == 2) {
4872 /* A write to sync_action is enough to justify
4873 * canceling read-auto mode
4876 md_wakeup_thread(mddev->sync_thread);
4878 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4879 md_wakeup_thread(mddev->thread);
4880 sysfs_notify_dirent_safe(mddev->sysfs_action);
4884 static struct md_sysfs_entry md_scan_mode =
4885 __ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
4888 last_sync_action_show(struct mddev *mddev, char *page)
4890 return sprintf(page, "%s\n", mddev->last_sync_action);
4893 static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4896 mismatch_cnt_show(struct mddev *mddev, char *page)
4898 return sprintf(page, "%llu\n",
4899 (unsigned long long)
4900 atomic64_read(&mddev->resync_mismatches));
4903 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
4906 sync_min_show(struct mddev *mddev, char *page)
4908 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4909 mddev->sync_speed_min ? "local": "system");
4913 sync_min_store(struct mddev *mddev, const char *buf, size_t len)
4918 if (strncmp(buf, "system", 6)==0) {
4921 rv = kstrtouint(buf, 10, &min);
4927 mddev->sync_speed_min = min;
4931 static struct md_sysfs_entry md_sync_min =
4932 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4935 sync_max_show(struct mddev *mddev, char *page)
4937 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4938 mddev->sync_speed_max ? "local": "system");
4942 sync_max_store(struct mddev *mddev, const char *buf, size_t len)
4947 if (strncmp(buf, "system", 6)==0) {
4950 rv = kstrtouint(buf, 10, &max);
4956 mddev->sync_speed_max = max;
4960 static struct md_sysfs_entry md_sync_max =
4961 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4964 degraded_show(struct mddev *mddev, char *page)
4966 return sprintf(page, "%d\n", mddev->degraded);
4968 static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
4971 sync_force_parallel_show(struct mddev *mddev, char *page)
4973 return sprintf(page, "%d\n", mddev->parallel_resync);
4977 sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
4981 if (kstrtol(buf, 10, &n))
4984 if (n != 0 && n != 1)
4987 mddev->parallel_resync = n;
4989 if (mddev->sync_thread)
4990 wake_up(&resync_wait);
4995 /* force parallel resync, even with shared block devices */
4996 static struct md_sysfs_entry md_sync_force_parallel =
4997 __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4998 sync_force_parallel_show, sync_force_parallel_store);
5001 sync_speed_show(struct mddev *mddev, char *page)
5003 unsigned long resync, dt, db;
5004 if (mddev->curr_resync == 0)
5005 return sprintf(page, "none\n");
5006 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
5007 dt = (jiffies - mddev->resync_mark) / HZ;
5009 db = resync - mddev->resync_mark_cnt;
5010 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
5013 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
5016 sync_completed_show(struct mddev *mddev, char *page)
5018 unsigned long long max_sectors, resync;
5020 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
5021 return sprintf(page, "none\n");
5023 if (mddev->curr_resync == 1 ||
5024 mddev->curr_resync == 2)
5025 return sprintf(page, "delayed\n");
5027 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
5028 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5029 max_sectors = mddev->resync_max_sectors;
5031 max_sectors = mddev->dev_sectors;
5033 resync = mddev->curr_resync_completed;
5034 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
5037 static struct md_sysfs_entry md_sync_completed =
5038 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
5041 min_sync_show(struct mddev *mddev, char *page)
5043 return sprintf(page, "%llu\n",
5044 (unsigned long long)mddev->resync_min);
5047 min_sync_store(struct mddev *mddev, const char *buf, size_t len)
5049 unsigned long long min;
5052 if (kstrtoull(buf, 10, &min))
5055 spin_lock(&mddev->lock);
5057 if (min > mddev->resync_max)
5061 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
5064 /* Round down to multiple of 4K for safety */
5065 mddev->resync_min = round_down(min, 8);
5069 spin_unlock(&mddev->lock);
5073 static struct md_sysfs_entry md_min_sync =
5074 __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
5077 max_sync_show(struct mddev *mddev, char *page)
5079 if (mddev->resync_max == MaxSector)
5080 return sprintf(page, "max\n");
5082 return sprintf(page, "%llu\n",
5083 (unsigned long long)mddev->resync_max);
5086 max_sync_store(struct mddev *mddev, const char *buf, size_t len)
5089 spin_lock(&mddev->lock);
5090 if (strncmp(buf, "max", 3) == 0)
5091 mddev->resync_max = MaxSector;
5093 unsigned long long max;
5097 if (kstrtoull(buf, 10, &max))
5099 if (max < mddev->resync_min)
5103 if (max < mddev->resync_max &&
5105 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
5108 /* Must be a multiple of chunk_size */
5109 chunk = mddev->chunk_sectors;
5111 sector_t temp = max;
5114 if (sector_div(temp, chunk))
5117 mddev->resync_max = max;
5119 wake_up(&mddev->recovery_wait);
5122 spin_unlock(&mddev->lock);
5126 static struct md_sysfs_entry md_max_sync =
5127 __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
5130 suspend_lo_show(struct mddev *mddev, char *page)
5132 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
5136 suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
5138 unsigned long long new;
5141 err = kstrtoull(buf, 10, &new);
5144 if (new != (sector_t)new)
5147 err = mddev_lock(mddev);
5151 if (mddev->pers == NULL ||
5152 mddev->pers->quiesce == NULL)
5154 mddev_suspend(mddev);
5155 mddev->suspend_lo = new;
5156 mddev_resume(mddev);
5160 mddev_unlock(mddev);
5163 static struct md_sysfs_entry md_suspend_lo =
5164 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
5167 suspend_hi_show(struct mddev *mddev, char *page)
5169 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
5173 suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
5175 unsigned long long new;
5178 err = kstrtoull(buf, 10, &new);
5181 if (new != (sector_t)new)
5184 err = mddev_lock(mddev);
5188 if (mddev->pers == NULL)
5191 mddev_suspend(mddev);
5192 mddev->suspend_hi = new;
5193 mddev_resume(mddev);
5197 mddev_unlock(mddev);
5200 static struct md_sysfs_entry md_suspend_hi =
5201 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
5204 reshape_position_show(struct mddev *mddev, char *page)
5206 if (mddev->reshape_position != MaxSector)
5207 return sprintf(page, "%llu\n",
5208 (unsigned long long)mddev->reshape_position);
5209 strcpy(page, "none\n");
5214 reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
5216 struct md_rdev *rdev;
5217 unsigned long long new;
5220 err = kstrtoull(buf, 10, &new);
5223 if (new != (sector_t)new)
5225 err = mddev_lock(mddev);
5231 mddev->reshape_position = new;
5232 mddev->delta_disks = 0;
5233 mddev->reshape_backwards = 0;
5234 mddev->new_level = mddev->level;
5235 mddev->new_layout = mddev->layout;
5236 mddev->new_chunk_sectors = mddev->chunk_sectors;
5237 rdev_for_each(rdev, mddev)
5238 rdev->new_data_offset = rdev->data_offset;
5241 mddev_unlock(mddev);
5245 static struct md_sysfs_entry md_reshape_position =
5246 __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
5247 reshape_position_store);
5250 reshape_direction_show(struct mddev *mddev, char *page)
5252 return sprintf(page, "%s\n",
5253 mddev->reshape_backwards ? "backwards" : "forwards");
5257 reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
5262 if (cmd_match(buf, "forwards"))
5264 else if (cmd_match(buf, "backwards"))
5268 if (mddev->reshape_backwards == backwards)
5271 err = mddev_lock(mddev);
5274 /* check if we are allowed to change */
5275 if (mddev->delta_disks)
5277 else if (mddev->persistent &&
5278 mddev->major_version == 0)
5281 mddev->reshape_backwards = backwards;
5282 mddev_unlock(mddev);
5286 static struct md_sysfs_entry md_reshape_direction =
5287 __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
5288 reshape_direction_store);
5291 array_size_show(struct mddev *mddev, char *page)
5293 if (mddev->external_size)
5294 return sprintf(page, "%llu\n",
5295 (unsigned long long)mddev->array_sectors/2);
5297 return sprintf(page, "default\n");
5301 array_size_store(struct mddev *mddev, const char *buf, size_t len)
5306 err = mddev_lock(mddev);
5310 /* cluster raid doesn't support change array_sectors */
5311 if (mddev_is_clustered(mddev)) {
5312 mddev_unlock(mddev);
5316 if (strncmp(buf, "default", 7) == 0) {
5318 sectors = mddev->pers->size(mddev, 0, 0);
5320 sectors = mddev->array_sectors;
5322 mddev->external_size = 0;
5324 if (strict_blocks_to_sectors(buf, §ors) < 0)
5326 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
5329 mddev->external_size = 1;
5333 mddev->array_sectors = sectors;
5335 set_capacity_and_notify(mddev->gendisk,
5336 mddev->array_sectors);
5338 mddev_unlock(mddev);
5342 static struct md_sysfs_entry md_array_size =
5343 __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
5347 consistency_policy_show(struct mddev *mddev, char *page)
5351 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5352 ret = sprintf(page, "journal\n");
5353 } else if (test_bit(MD_HAS_PPL, &mddev->flags)) {
5354 ret = sprintf(page, "ppl\n");
5355 } else if (mddev->bitmap) {
5356 ret = sprintf(page, "bitmap\n");
5357 } else if (mddev->pers) {
5358 if (mddev->pers->sync_request)
5359 ret = sprintf(page, "resync\n");
5361 ret = sprintf(page, "none\n");
5363 ret = sprintf(page, "unknown\n");
5370 consistency_policy_store(struct mddev *mddev, const char *buf, size_t len)
5375 if (mddev->pers->change_consistency_policy)
5376 err = mddev->pers->change_consistency_policy(mddev, buf);
5379 } else if (mddev->external && strncmp(buf, "ppl", 3) == 0) {
5380 set_bit(MD_HAS_PPL, &mddev->flags);
5385 return err ? err : len;
5388 static struct md_sysfs_entry md_consistency_policy =
5389 __ATTR(consistency_policy, S_IRUGO | S_IWUSR, consistency_policy_show,
5390 consistency_policy_store);
5392 static ssize_t fail_last_dev_show(struct mddev *mddev, char *page)
5394 return sprintf(page, "%d\n", mddev->fail_last_dev);
5398 * Setting fail_last_dev to true to allow last device to be forcibly removed
5399 * from RAID1/RAID10.
5402 fail_last_dev_store(struct mddev *mddev, const char *buf, size_t len)
5407 ret = kstrtobool(buf, &value);
5411 if (value != mddev->fail_last_dev)
5412 mddev->fail_last_dev = value;
5416 static struct md_sysfs_entry md_fail_last_dev =
5417 __ATTR(fail_last_dev, S_IRUGO | S_IWUSR, fail_last_dev_show,
5418 fail_last_dev_store);
5420 static ssize_t serialize_policy_show(struct mddev *mddev, char *page)
5422 if (mddev->pers == NULL || (mddev->pers->level != 1))
5423 return sprintf(page, "n/a\n");
5425 return sprintf(page, "%d\n", mddev->serialize_policy);
5429 * Setting serialize_policy to true to enforce write IO is not reordered
5433 serialize_policy_store(struct mddev *mddev, const char *buf, size_t len)
5438 err = kstrtobool(buf, &value);
5442 if (value == mddev->serialize_policy)
5445 err = mddev_lock(mddev);
5448 if (mddev->pers == NULL || (mddev->pers->level != 1)) {
5449 pr_err("md: serialize_policy is only effective for raid1\n");
5454 mddev_suspend(mddev);
5456 mddev_create_serial_pool(mddev, NULL, true);
5458 mddev_destroy_serial_pool(mddev, NULL, true);
5459 mddev->serialize_policy = value;
5460 mddev_resume(mddev);
5462 mddev_unlock(mddev);
5466 static struct md_sysfs_entry md_serialize_policy =
5467 __ATTR(serialize_policy, S_IRUGO | S_IWUSR, serialize_policy_show,
5468 serialize_policy_store);
5471 static struct attribute *md_default_attrs[] = {
5474 &md_raid_disks.attr,
5476 &md_chunk_size.attr,
5478 &md_resync_start.attr,
5480 &md_new_device.attr,
5481 &md_safe_delay.attr,
5482 &md_array_state.attr,
5483 &md_reshape_position.attr,
5484 &md_reshape_direction.attr,
5485 &md_array_size.attr,
5486 &max_corr_read_errors.attr,
5487 &md_consistency_policy.attr,
5488 &md_fail_last_dev.attr,
5489 &md_serialize_policy.attr,
5493 static struct attribute *md_redundancy_attrs[] = {
5495 &md_last_scan_mode.attr,
5496 &md_mismatches.attr,
5499 &md_sync_speed.attr,
5500 &md_sync_force_parallel.attr,
5501 &md_sync_completed.attr,
5504 &md_suspend_lo.attr,
5505 &md_suspend_hi.attr,
5510 static const struct attribute_group md_redundancy_group = {
5512 .attrs = md_redundancy_attrs,
5516 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
5518 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
5519 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
5524 spin_lock(&all_mddevs_lock);
5525 if (list_empty(&mddev->all_mddevs)) {
5526 spin_unlock(&all_mddevs_lock);
5530 spin_unlock(&all_mddevs_lock);
5532 rv = entry->show(mddev, page);
5538 md_attr_store(struct kobject *kobj, struct attribute *attr,
5539 const char *page, size_t length)
5541 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
5542 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
5547 if (!capable(CAP_SYS_ADMIN))
5549 spin_lock(&all_mddevs_lock);
5550 if (list_empty(&mddev->all_mddevs)) {
5551 spin_unlock(&all_mddevs_lock);
5555 spin_unlock(&all_mddevs_lock);
5556 rv = entry->store(mddev, page, length);
5561 static void md_free(struct kobject *ko)
5563 struct mddev *mddev = container_of(ko, struct mddev, kobj);
5565 if (mddev->sysfs_state)
5566 sysfs_put(mddev->sysfs_state);
5567 if (mddev->sysfs_level)
5568 sysfs_put(mddev->sysfs_level);
5570 if (mddev->gendisk) {
5571 del_gendisk(mddev->gendisk);
5572 blk_cleanup_disk(mddev->gendisk);
5574 percpu_ref_exit(&mddev->writes_pending);
5576 bioset_exit(&mddev->bio_set);
5577 bioset_exit(&mddev->sync_set);
5578 if (mddev->level != 1 && mddev->level != 10)
5579 bioset_exit(&mddev->io_acct_set);
5583 static const struct sysfs_ops md_sysfs_ops = {
5584 .show = md_attr_show,
5585 .store = md_attr_store,
5587 static struct kobj_type md_ktype = {
5589 .sysfs_ops = &md_sysfs_ops,
5590 .default_attrs = md_default_attrs,
5595 static void mddev_delayed_delete(struct work_struct *ws)
5597 struct mddev *mddev = container_of(ws, struct mddev, del_work);
5599 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
5600 kobject_del(&mddev->kobj);
5601 kobject_put(&mddev->kobj);
5604 static void no_op(struct percpu_ref *r) {}
5606 int mddev_init_writes_pending(struct mddev *mddev)
5608 if (mddev->writes_pending.percpu_count_ptr)
5610 if (percpu_ref_init(&mddev->writes_pending, no_op,
5611 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL) < 0)
5613 /* We want to start with the refcount at zero */
5614 percpu_ref_put(&mddev->writes_pending);
5617 EXPORT_SYMBOL_GPL(mddev_init_writes_pending);
5619 static int md_alloc(dev_t dev, char *name)
5622 * If dev is zero, name is the name of a device to allocate with
5623 * an arbitrary minor number. It will be "md_???"
5624 * If dev is non-zero it must be a device number with a MAJOR of
5625 * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then
5626 * the device is being created by opening a node in /dev.
5627 * If "name" is not NULL, the device is being created by
5628 * writing to /sys/module/md_mod/parameters/new_array.
5630 static DEFINE_MUTEX(disks_mutex);
5631 struct mddev *mddev;
5632 struct gendisk *disk;
5639 * Wait for any previous instance of this device to be completely
5640 * removed (mddev_delayed_delete).
5642 flush_workqueue(md_misc_wq);
5644 mutex_lock(&disks_mutex);
5645 mddev = mddev_alloc(dev);
5646 if (IS_ERR(mddev)) {
5647 mutex_unlock(&disks_mutex);
5648 return PTR_ERR(mddev);
5651 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
5652 shift = partitioned ? MdpMinorShift : 0;
5653 unit = MINOR(mddev->unit) >> shift;
5656 /* Need to ensure that 'name' is not a duplicate.
5658 struct mddev *mddev2;
5659 spin_lock(&all_mddevs_lock);
5661 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5662 if (mddev2->gendisk &&
5663 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5664 spin_unlock(&all_mddevs_lock);
5668 spin_unlock(&all_mddevs_lock);
5672 * Creating /dev/mdNNN via "newarray", so adjust hold_active.
5674 mddev->hold_active = UNTIL_STOP;
5677 disk = blk_alloc_disk(NUMA_NO_NODE);
5681 disk->major = MAJOR(mddev->unit);
5682 disk->first_minor = unit << shift;
5683 disk->minors = 1 << shift;
5685 strcpy(disk->disk_name, name);
5686 else if (partitioned)
5687 sprintf(disk->disk_name, "md_d%d", unit);
5689 sprintf(disk->disk_name, "md%d", unit);
5690 disk->fops = &md_fops;
5691 disk->private_data = mddev;
5693 mddev->queue = disk->queue;
5694 blk_set_stacking_limits(&mddev->queue->limits);
5695 blk_queue_write_cache(mddev->queue, true, true);
5696 /* Allow extended partitions. This makes the
5697 * 'mdp' device redundant, but we can't really
5700 disk->flags |= GENHD_FL_EXT_DEVT;
5701 disk->events |= DISK_EVENT_MEDIA_CHANGE;
5702 mddev->gendisk = disk;
5703 /* As soon as we call add_disk(), another thread could get
5704 * through to md_open, so make sure it doesn't get too far
5706 mutex_lock(&mddev->open_mutex);
5709 error = kobject_add(&mddev->kobj, &disk_to_dev(disk)->kobj, "%s", "md");
5711 /* This isn't possible, but as kobject_init_and_add is marked
5712 * __must_check, we must do something with the result
5714 pr_debug("md: cannot register %s/md - name in use\n",
5718 if (mddev->kobj.sd &&
5719 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
5720 pr_debug("pointless warning\n");
5721 mutex_unlock(&mddev->open_mutex);
5723 mutex_unlock(&disks_mutex);
5724 if (!error && mddev->kobj.sd) {
5725 kobject_uevent(&mddev->kobj, KOBJ_ADD);
5726 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
5727 mddev->sysfs_level = sysfs_get_dirent_safe(mddev->kobj.sd, "level");
5733 static void md_probe(dev_t dev)
5735 if (MAJOR(dev) == MD_MAJOR && MINOR(dev) >= 512)
5738 md_alloc(dev, NULL);
5741 static int add_named_array(const char *val, const struct kernel_param *kp)
5744 * val must be "md_*" or "mdNNN".
5745 * For "md_*" we allocate an array with a large free minor number, and
5746 * set the name to val. val must not already be an active name.
5747 * For "mdNNN" we allocate an array with the minor number NNN
5748 * which must not already be in use.
5750 int len = strlen(val);
5751 char buf[DISK_NAME_LEN];
5752 unsigned long devnum;
5754 while (len && val[len-1] == '\n')
5756 if (len >= DISK_NAME_LEN)
5758 strlcpy(buf, val, len+1);
5759 if (strncmp(buf, "md_", 3) == 0)
5760 return md_alloc(0, buf);
5761 if (strncmp(buf, "md", 2) == 0 &&
5763 kstrtoul(buf+2, 10, &devnum) == 0 &&
5764 devnum <= MINORMASK)
5765 return md_alloc(MKDEV(MD_MAJOR, devnum), NULL);
5770 static void md_safemode_timeout(struct timer_list *t)
5772 struct mddev *mddev = from_timer(mddev, t, safemode_timer);
5774 mddev->safemode = 1;
5775 if (mddev->external)
5776 sysfs_notify_dirent_safe(mddev->sysfs_state);
5778 md_wakeup_thread(mddev->thread);
5781 static int start_dirty_degraded;
5783 int md_run(struct mddev *mddev)
5786 struct md_rdev *rdev;
5787 struct md_personality *pers;
5789 if (list_empty(&mddev->disks))
5790 /* cannot run an array with no devices.. */
5795 /* Cannot run until previous stop completes properly */
5796 if (mddev->sysfs_active)
5800 * Analyze all RAID superblock(s)
5802 if (!mddev->raid_disks) {
5803 if (!mddev->persistent)
5805 err = analyze_sbs(mddev);
5810 if (mddev->level != LEVEL_NONE)
5811 request_module("md-level-%d", mddev->level);
5812 else if (mddev->clevel[0])
5813 request_module("md-%s", mddev->clevel);
5816 * Drop all container device buffers, from now on
5817 * the only valid external interface is through the md
5820 mddev->has_superblocks = false;
5821 rdev_for_each(rdev, mddev) {
5822 if (test_bit(Faulty, &rdev->flags))
5824 sync_blockdev(rdev->bdev);
5825 invalidate_bdev(rdev->bdev);
5826 if (mddev->ro != 1 && rdev_read_only(rdev)) {
5829 set_disk_ro(mddev->gendisk, 1);
5833 mddev->has_superblocks = true;
5835 /* perform some consistency tests on the device.
5836 * We don't want the data to overlap the metadata,
5837 * Internal Bitmap issues have been handled elsewhere.
5839 if (rdev->meta_bdev) {
5840 /* Nothing to check */;
5841 } else if (rdev->data_offset < rdev->sb_start) {
5842 if (mddev->dev_sectors &&
5843 rdev->data_offset + mddev->dev_sectors
5845 pr_warn("md: %s: data overlaps metadata\n",
5850 if (rdev->sb_start + rdev->sb_size/512
5851 > rdev->data_offset) {
5852 pr_warn("md: %s: metadata overlaps data\n",
5857 sysfs_notify_dirent_safe(rdev->sysfs_state);
5860 if (!bioset_initialized(&mddev->bio_set)) {
5861 err = bioset_init(&mddev->bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
5865 if (!bioset_initialized(&mddev->sync_set)) {
5866 err = bioset_init(&mddev->sync_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
5870 if (mddev->level != 1 && mddev->level != 10 &&
5871 !bioset_initialized(&mddev->io_acct_set)) {
5872 err = bioset_init(&mddev->io_acct_set, BIO_POOL_SIZE,
5873 offsetof(struct md_io_acct, bio_clone), 0);
5878 spin_lock(&pers_lock);
5879 pers = find_pers(mddev->level, mddev->clevel);
5880 if (!pers || !try_module_get(pers->owner)) {
5881 spin_unlock(&pers_lock);
5882 if (mddev->level != LEVEL_NONE)
5883 pr_warn("md: personality for level %d is not loaded!\n",
5886 pr_warn("md: personality for level %s is not loaded!\n",
5891 spin_unlock(&pers_lock);
5892 if (mddev->level != pers->level) {
5893 mddev->level = pers->level;
5894 mddev->new_level = pers->level;
5896 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
5898 if (mddev->reshape_position != MaxSector &&
5899 pers->start_reshape == NULL) {
5900 /* This personality cannot handle reshaping... */
5901 module_put(pers->owner);
5906 if (pers->sync_request) {
5907 /* Warn if this is a potentially silly
5910 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5911 struct md_rdev *rdev2;
5914 rdev_for_each(rdev, mddev)
5915 rdev_for_each(rdev2, mddev) {
5917 rdev->bdev->bd_disk ==
5918 rdev2->bdev->bd_disk) {
5919 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5921 bdevname(rdev->bdev,b),
5922 bdevname(rdev2->bdev,b2));
5928 pr_warn("True protection against single-disk failure might be compromised.\n");
5931 mddev->recovery = 0;
5932 /* may be over-ridden by personality */
5933 mddev->resync_max_sectors = mddev->dev_sectors;
5935 mddev->ok_start_degraded = start_dirty_degraded;
5937 if (start_readonly && mddev->ro == 0)
5938 mddev->ro = 2; /* read-only, but switch on first write */
5940 err = pers->run(mddev);
5942 pr_warn("md: pers->run() failed ...\n");
5943 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
5944 WARN_ONCE(!mddev->external_size,
5945 "%s: default size too small, but 'external_size' not in effect?\n",
5947 pr_warn("md: invalid array_size %llu > default size %llu\n",
5948 (unsigned long long)mddev->array_sectors / 2,
5949 (unsigned long long)pers->size(mddev, 0, 0) / 2);
5952 if (err == 0 && pers->sync_request &&
5953 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
5954 struct bitmap *bitmap;
5956 bitmap = md_bitmap_create(mddev, -1);
5957 if (IS_ERR(bitmap)) {
5958 err = PTR_ERR(bitmap);
5959 pr_warn("%s: failed to create bitmap (%d)\n",
5960 mdname(mddev), err);
5962 mddev->bitmap = bitmap;
5968 if (mddev->bitmap_info.max_write_behind > 0) {
5969 bool create_pool = false;
5971 rdev_for_each(rdev, mddev) {
5972 if (test_bit(WriteMostly, &rdev->flags) &&
5973 rdev_init_serial(rdev))
5976 if (create_pool && mddev->serial_info_pool == NULL) {
5977 mddev->serial_info_pool =
5978 mempool_create_kmalloc_pool(NR_SERIAL_INFOS,
5979 sizeof(struct serial_info));
5980 if (!mddev->serial_info_pool) {
5990 rdev_for_each(rdev, mddev) {
5991 if (rdev->raid_disk >= 0 &&
5992 !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
5997 if (mddev->degraded)
6000 blk_queue_flag_set(QUEUE_FLAG_NONROT, mddev->queue);
6002 blk_queue_flag_clear(QUEUE_FLAG_NONROT, mddev->queue);
6003 blk_queue_flag_set(QUEUE_FLAG_IO_STAT, mddev->queue);
6005 if (pers->sync_request) {
6006 if (mddev->kobj.sd &&
6007 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
6008 pr_warn("md: cannot register extra attributes for %s\n",
6010 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
6011 mddev->sysfs_completed = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_completed");
6012 mddev->sysfs_degraded = sysfs_get_dirent_safe(mddev->kobj.sd, "degraded");
6013 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
6016 atomic_set(&mddev->max_corr_read_errors,
6017 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
6018 mddev->safemode = 0;
6019 if (mddev_is_clustered(mddev))
6020 mddev->safemode_delay = 0;
6022 mddev->safemode_delay = DEFAULT_SAFEMODE_DELAY;
6025 spin_lock(&mddev->lock);
6027 spin_unlock(&mddev->lock);
6028 rdev_for_each(rdev, mddev)
6029 if (rdev->raid_disk >= 0)
6030 sysfs_link_rdev(mddev, rdev); /* failure here is OK */
6032 if (mddev->degraded && !mddev->ro)
6033 /* This ensures that recovering status is reported immediately
6034 * via sysfs - until a lack of spares is confirmed.
6036 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6037 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6039 if (mddev->sb_flags)
6040 md_update_sb(mddev, 0);
6042 md_new_event(mddev);
6046 mddev_detach(mddev);
6048 pers->free(mddev, mddev->private);
6049 mddev->private = NULL;
6050 module_put(pers->owner);
6051 md_bitmap_destroy(mddev);
6053 if (mddev->level != 1 && mddev->level != 10)
6054 bioset_exit(&mddev->io_acct_set);
6056 bioset_exit(&mddev->sync_set);
6058 bioset_exit(&mddev->bio_set);
6061 EXPORT_SYMBOL_GPL(md_run);
6063 int do_md_run(struct mddev *mddev)
6067 set_bit(MD_NOT_READY, &mddev->flags);
6068 err = md_run(mddev);
6071 err = md_bitmap_load(mddev);
6073 md_bitmap_destroy(mddev);
6077 if (mddev_is_clustered(mddev))
6078 md_allow_write(mddev);
6080 /* run start up tasks that require md_thread */
6083 md_wakeup_thread(mddev->thread);
6084 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
6086 set_capacity_and_notify(mddev->gendisk, mddev->array_sectors);
6087 clear_bit(MD_NOT_READY, &mddev->flags);
6089 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
6090 sysfs_notify_dirent_safe(mddev->sysfs_state);
6091 sysfs_notify_dirent_safe(mddev->sysfs_action);
6092 sysfs_notify_dirent_safe(mddev->sysfs_degraded);
6094 clear_bit(MD_NOT_READY, &mddev->flags);
6098 int md_start(struct mddev *mddev)
6102 if (mddev->pers->start) {
6103 set_bit(MD_RECOVERY_WAIT, &mddev->recovery);
6104 md_wakeup_thread(mddev->thread);
6105 ret = mddev->pers->start(mddev);
6106 clear_bit(MD_RECOVERY_WAIT, &mddev->recovery);
6107 md_wakeup_thread(mddev->sync_thread);
6111 EXPORT_SYMBOL_GPL(md_start);
6113 static int restart_array(struct mddev *mddev)
6115 struct gendisk *disk = mddev->gendisk;
6116 struct md_rdev *rdev;
6117 bool has_journal = false;
6118 bool has_readonly = false;
6120 /* Complain if it has no devices */
6121 if (list_empty(&mddev->disks))
6129 rdev_for_each_rcu(rdev, mddev) {
6130 if (test_bit(Journal, &rdev->flags) &&
6131 !test_bit(Faulty, &rdev->flags))
6133 if (rdev_read_only(rdev))
6134 has_readonly = true;
6137 if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && !has_journal)
6138 /* Don't restart rw with journal missing/faulty */
6143 mddev->safemode = 0;
6145 set_disk_ro(disk, 0);
6146 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev));
6147 /* Kick recovery or resync if necessary */
6148 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6149 md_wakeup_thread(mddev->thread);
6150 md_wakeup_thread(mddev->sync_thread);
6151 sysfs_notify_dirent_safe(mddev->sysfs_state);
6155 static void md_clean(struct mddev *mddev)
6157 mddev->array_sectors = 0;
6158 mddev->external_size = 0;
6159 mddev->dev_sectors = 0;
6160 mddev->raid_disks = 0;
6161 mddev->recovery_cp = 0;
6162 mddev->resync_min = 0;
6163 mddev->resync_max = MaxSector;
6164 mddev->reshape_position = MaxSector;
6165 mddev->external = 0;
6166 mddev->persistent = 0;
6167 mddev->level = LEVEL_NONE;
6168 mddev->clevel[0] = 0;
6170 mddev->sb_flags = 0;
6172 mddev->metadata_type[0] = 0;
6173 mddev->chunk_sectors = 0;
6174 mddev->ctime = mddev->utime = 0;
6176 mddev->max_disks = 0;
6178 mddev->can_decrease_events = 0;
6179 mddev->delta_disks = 0;
6180 mddev->reshape_backwards = 0;
6181 mddev->new_level = LEVEL_NONE;
6182 mddev->new_layout = 0;
6183 mddev->new_chunk_sectors = 0;
6184 mddev->curr_resync = 0;
6185 atomic64_set(&mddev->resync_mismatches, 0);
6186 mddev->suspend_lo = mddev->suspend_hi = 0;
6187 mddev->sync_speed_min = mddev->sync_speed_max = 0;
6188 mddev->recovery = 0;
6191 mddev->degraded = 0;
6192 mddev->safemode = 0;
6193 mddev->private = NULL;
6194 mddev->cluster_info = NULL;
6195 mddev->bitmap_info.offset = 0;
6196 mddev->bitmap_info.default_offset = 0;
6197 mddev->bitmap_info.default_space = 0;
6198 mddev->bitmap_info.chunksize = 0;
6199 mddev->bitmap_info.daemon_sleep = 0;
6200 mddev->bitmap_info.max_write_behind = 0;
6201 mddev->bitmap_info.nodes = 0;
6204 static void __md_stop_writes(struct mddev *mddev)
6206 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6207 if (work_pending(&mddev->del_work))
6208 flush_workqueue(md_misc_wq);
6209 if (mddev->sync_thread) {
6210 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6211 md_reap_sync_thread(mddev);
6214 del_timer_sync(&mddev->safemode_timer);
6216 if (mddev->pers && mddev->pers->quiesce) {
6217 mddev->pers->quiesce(mddev, 1);
6218 mddev->pers->quiesce(mddev, 0);
6220 md_bitmap_flush(mddev);
6222 if (mddev->ro == 0 &&
6223 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
6225 /* mark array as shutdown cleanly */
6226 if (!mddev_is_clustered(mddev))
6228 md_update_sb(mddev, 1);
6230 /* disable policy to guarantee rdevs free resources for serialization */
6231 mddev->serialize_policy = 0;
6232 mddev_destroy_serial_pool(mddev, NULL, true);
6235 void md_stop_writes(struct mddev *mddev)
6237 mddev_lock_nointr(mddev);
6238 __md_stop_writes(mddev);
6239 mddev_unlock(mddev);
6241 EXPORT_SYMBOL_GPL(md_stop_writes);
6243 static void mddev_detach(struct mddev *mddev)
6245 md_bitmap_wait_behind_writes(mddev);
6246 if (mddev->pers && mddev->pers->quiesce && !mddev->suspended) {
6247 mddev->pers->quiesce(mddev, 1);
6248 mddev->pers->quiesce(mddev, 0);
6250 md_unregister_thread(&mddev->thread);
6252 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
6255 static void __md_stop(struct mddev *mddev)
6257 struct md_personality *pers = mddev->pers;
6258 md_bitmap_destroy(mddev);
6259 mddev_detach(mddev);
6260 /* Ensure ->event_work is done */
6261 if (mddev->event_work.func)
6262 flush_workqueue(md_misc_wq);
6263 spin_lock(&mddev->lock);
6265 spin_unlock(&mddev->lock);
6266 pers->free(mddev, mddev->private);
6267 mddev->private = NULL;
6268 if (pers->sync_request && mddev->to_remove == NULL)
6269 mddev->to_remove = &md_redundancy_group;
6270 module_put(pers->owner);
6271 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6274 void md_stop(struct mddev *mddev)
6276 /* stop the array and free an attached data structures.
6277 * This is called from dm-raid
6280 bioset_exit(&mddev->bio_set);
6281 bioset_exit(&mddev->sync_set);
6282 if (mddev->level != 1 && mddev->level != 10)
6283 bioset_exit(&mddev->io_acct_set);
6286 EXPORT_SYMBOL_GPL(md_stop);
6288 static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
6293 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
6295 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6296 md_wakeup_thread(mddev->thread);
6298 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
6299 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6300 if (mddev->sync_thread)
6301 /* Thread might be blocked waiting for metadata update
6302 * which will now never happen */
6303 wake_up_process(mddev->sync_thread->tsk);
6305 if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
6307 mddev_unlock(mddev);
6308 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
6310 wait_event(mddev->sb_wait,
6311 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
6312 mddev_lock_nointr(mddev);
6314 mutex_lock(&mddev->open_mutex);
6315 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
6316 mddev->sync_thread ||
6317 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
6318 pr_warn("md: %s still in use.\n",mdname(mddev));
6320 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6321 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6322 md_wakeup_thread(mddev->thread);
6328 __md_stop_writes(mddev);
6334 set_disk_ro(mddev->gendisk, 1);
6335 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6336 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6337 md_wakeup_thread(mddev->thread);
6338 sysfs_notify_dirent_safe(mddev->sysfs_state);
6342 mutex_unlock(&mddev->open_mutex);
6347 * 0 - completely stop and dis-assemble array
6348 * 2 - stop but do not disassemble array
6350 static int do_md_stop(struct mddev *mddev, int mode,
6351 struct block_device *bdev)
6353 struct gendisk *disk = mddev->gendisk;
6354 struct md_rdev *rdev;
6357 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
6359 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6360 md_wakeup_thread(mddev->thread);
6362 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
6363 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6364 if (mddev->sync_thread)
6365 /* Thread might be blocked waiting for metadata update
6366 * which will now never happen */
6367 wake_up_process(mddev->sync_thread->tsk);
6369 mddev_unlock(mddev);
6370 wait_event(resync_wait, (mddev->sync_thread == NULL &&
6371 !test_bit(MD_RECOVERY_RUNNING,
6372 &mddev->recovery)));
6373 mddev_lock_nointr(mddev);
6375 mutex_lock(&mddev->open_mutex);
6376 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
6377 mddev->sysfs_active ||
6378 mddev->sync_thread ||
6379 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
6380 pr_warn("md: %s still in use.\n",mdname(mddev));
6381 mutex_unlock(&mddev->open_mutex);
6383 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6384 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6385 md_wakeup_thread(mddev->thread);
6391 set_disk_ro(disk, 0);
6393 __md_stop_writes(mddev);
6396 /* tell userspace to handle 'inactive' */
6397 sysfs_notify_dirent_safe(mddev->sysfs_state);
6399 rdev_for_each(rdev, mddev)
6400 if (rdev->raid_disk >= 0)
6401 sysfs_unlink_rdev(mddev, rdev);
6403 set_capacity_and_notify(disk, 0);
6404 mutex_unlock(&mddev->open_mutex);
6410 mutex_unlock(&mddev->open_mutex);
6412 * Free resources if final stop
6415 pr_info("md: %s stopped.\n", mdname(mddev));
6417 if (mddev->bitmap_info.file) {
6418 struct file *f = mddev->bitmap_info.file;
6419 spin_lock(&mddev->lock);
6420 mddev->bitmap_info.file = NULL;
6421 spin_unlock(&mddev->lock);
6424 mddev->bitmap_info.offset = 0;
6426 export_array(mddev);
6429 if (mddev->hold_active == UNTIL_STOP)
6430 mddev->hold_active = 0;
6432 md_new_event(mddev);
6433 sysfs_notify_dirent_safe(mddev->sysfs_state);
6438 static void autorun_array(struct mddev *mddev)
6440 struct md_rdev *rdev;
6443 if (list_empty(&mddev->disks))
6446 pr_info("md: running: ");
6448 rdev_for_each(rdev, mddev) {
6449 char b[BDEVNAME_SIZE];
6450 pr_cont("<%s>", bdevname(rdev->bdev,b));
6454 err = do_md_run(mddev);
6456 pr_warn("md: do_md_run() returned %d\n", err);
6457 do_md_stop(mddev, 0, NULL);
6462 * lets try to run arrays based on all disks that have arrived
6463 * until now. (those are in pending_raid_disks)
6465 * the method: pick the first pending disk, collect all disks with
6466 * the same UUID, remove all from the pending list and put them into
6467 * the 'same_array' list. Then order this list based on superblock
6468 * update time (freshest comes first), kick out 'old' disks and
6469 * compare superblocks. If everything's fine then run it.
6471 * If "unit" is allocated, then bump its reference count
6473 static void autorun_devices(int part)
6475 struct md_rdev *rdev0, *rdev, *tmp;
6476 struct mddev *mddev;
6477 char b[BDEVNAME_SIZE];
6479 pr_info("md: autorun ...\n");
6480 while (!list_empty(&pending_raid_disks)) {
6483 LIST_HEAD(candidates);
6484 rdev0 = list_entry(pending_raid_disks.next,
6485 struct md_rdev, same_set);
6487 pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b));
6488 INIT_LIST_HEAD(&candidates);
6489 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
6490 if (super_90_load(rdev, rdev0, 0) >= 0) {
6491 pr_debug("md: adding %s ...\n",
6492 bdevname(rdev->bdev,b));
6493 list_move(&rdev->same_set, &candidates);
6496 * now we have a set of devices, with all of them having
6497 * mostly sane superblocks. It's time to allocate the
6501 dev = MKDEV(mdp_major,
6502 rdev0->preferred_minor << MdpMinorShift);
6503 unit = MINOR(dev) >> MdpMinorShift;
6505 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
6508 if (rdev0->preferred_minor != unit) {
6509 pr_warn("md: unit number in %s is bad: %d\n",
6510 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
6515 mddev = mddev_find(dev);
6519 if (mddev_lock(mddev))
6520 pr_warn("md: %s locked, cannot run\n", mdname(mddev));
6521 else if (mddev->raid_disks || mddev->major_version
6522 || !list_empty(&mddev->disks)) {
6523 pr_warn("md: %s already running, cannot run %s\n",
6524 mdname(mddev), bdevname(rdev0->bdev,b));
6525 mddev_unlock(mddev);
6527 pr_debug("md: created %s\n", mdname(mddev));
6528 mddev->persistent = 1;
6529 rdev_for_each_list(rdev, tmp, &candidates) {
6530 list_del_init(&rdev->same_set);
6531 if (bind_rdev_to_array(rdev, mddev))
6534 autorun_array(mddev);
6535 mddev_unlock(mddev);
6537 /* on success, candidates will be empty, on error
6540 rdev_for_each_list(rdev, tmp, &candidates) {
6541 list_del_init(&rdev->same_set);
6546 pr_info("md: ... autorun DONE.\n");
6548 #endif /* !MODULE */
6550 static int get_version(void __user *arg)
6554 ver.major = MD_MAJOR_VERSION;
6555 ver.minor = MD_MINOR_VERSION;
6556 ver.patchlevel = MD_PATCHLEVEL_VERSION;
6558 if (copy_to_user(arg, &ver, sizeof(ver)))
6564 static int get_array_info(struct mddev *mddev, void __user *arg)
6566 mdu_array_info_t info;
6567 int nr,working,insync,failed,spare;
6568 struct md_rdev *rdev;
6570 nr = working = insync = failed = spare = 0;
6572 rdev_for_each_rcu(rdev, mddev) {
6574 if (test_bit(Faulty, &rdev->flags))
6578 if (test_bit(In_sync, &rdev->flags))
6580 else if (test_bit(Journal, &rdev->flags))
6581 /* TODO: add journal count to md_u.h */
6589 info.major_version = mddev->major_version;
6590 info.minor_version = mddev->minor_version;
6591 info.patch_version = MD_PATCHLEVEL_VERSION;
6592 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
6593 info.level = mddev->level;
6594 info.size = mddev->dev_sectors / 2;
6595 if (info.size != mddev->dev_sectors / 2) /* overflow */
6598 info.raid_disks = mddev->raid_disks;
6599 info.md_minor = mddev->md_minor;
6600 info.not_persistent= !mddev->persistent;
6602 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
6605 info.state = (1<<MD_SB_CLEAN);
6606 if (mddev->bitmap && mddev->bitmap_info.offset)
6607 info.state |= (1<<MD_SB_BITMAP_PRESENT);
6608 if (mddev_is_clustered(mddev))
6609 info.state |= (1<<MD_SB_CLUSTERED);
6610 info.active_disks = insync;
6611 info.working_disks = working;
6612 info.failed_disks = failed;
6613 info.spare_disks = spare;
6615 info.layout = mddev->layout;
6616 info.chunk_size = mddev->chunk_sectors << 9;
6618 if (copy_to_user(arg, &info, sizeof(info)))
6624 static int get_bitmap_file(struct mddev *mddev, void __user * arg)
6626 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
6630 file = kzalloc(sizeof(*file), GFP_NOIO);
6635 spin_lock(&mddev->lock);
6636 /* bitmap enabled */
6637 if (mddev->bitmap_info.file) {
6638 ptr = file_path(mddev->bitmap_info.file, file->pathname,
6639 sizeof(file->pathname));
6643 memmove(file->pathname, ptr,
6644 sizeof(file->pathname)-(ptr-file->pathname));
6646 spin_unlock(&mddev->lock);
6649 copy_to_user(arg, file, sizeof(*file)))
6656 static int get_disk_info(struct mddev *mddev, void __user * arg)
6658 mdu_disk_info_t info;
6659 struct md_rdev *rdev;
6661 if (copy_from_user(&info, arg, sizeof(info)))
6665 rdev = md_find_rdev_nr_rcu(mddev, info.number);
6667 info.major = MAJOR(rdev->bdev->bd_dev);
6668 info.minor = MINOR(rdev->bdev->bd_dev);
6669 info.raid_disk = rdev->raid_disk;
6671 if (test_bit(Faulty, &rdev->flags))
6672 info.state |= (1<<MD_DISK_FAULTY);
6673 else if (test_bit(In_sync, &rdev->flags)) {
6674 info.state |= (1<<MD_DISK_ACTIVE);
6675 info.state |= (1<<MD_DISK_SYNC);
6677 if (test_bit(Journal, &rdev->flags))
6678 info.state |= (1<<MD_DISK_JOURNAL);
6679 if (test_bit(WriteMostly, &rdev->flags))
6680 info.state |= (1<<MD_DISK_WRITEMOSTLY);
6681 if (test_bit(FailFast, &rdev->flags))
6682 info.state |= (1<<MD_DISK_FAILFAST);
6684 info.major = info.minor = 0;
6685 info.raid_disk = -1;
6686 info.state = (1<<MD_DISK_REMOVED);
6690 if (copy_to_user(arg, &info, sizeof(info)))
6696 int md_add_new_disk(struct mddev *mddev, struct mdu_disk_info_s *info)
6698 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
6699 struct md_rdev *rdev;
6700 dev_t dev = MKDEV(info->major,info->minor);
6702 if (mddev_is_clustered(mddev) &&
6703 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
6704 pr_warn("%s: Cannot add to clustered mddev.\n",
6709 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
6712 if (!mddev->raid_disks) {
6714 /* expecting a device which has a superblock */
6715 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
6717 pr_warn("md: md_import_device returned %ld\n",
6719 return PTR_ERR(rdev);
6721 if (!list_empty(&mddev->disks)) {
6722 struct md_rdev *rdev0
6723 = list_entry(mddev->disks.next,
6724 struct md_rdev, same_set);
6725 err = super_types[mddev->major_version]
6726 .load_super(rdev, rdev0, mddev->minor_version);
6728 pr_warn("md: %s has different UUID to %s\n",
6729 bdevname(rdev->bdev,b),
6730 bdevname(rdev0->bdev,b2));
6735 err = bind_rdev_to_array(rdev, mddev);
6742 * md_add_new_disk can be used once the array is assembled
6743 * to add "hot spares". They must already have a superblock
6748 if (!mddev->pers->hot_add_disk) {
6749 pr_warn("%s: personality does not support diskops!\n",
6753 if (mddev->persistent)
6754 rdev = md_import_device(dev, mddev->major_version,
6755 mddev->minor_version);
6757 rdev = md_import_device(dev, -1, -1);
6759 pr_warn("md: md_import_device returned %ld\n",
6761 return PTR_ERR(rdev);
6763 /* set saved_raid_disk if appropriate */
6764 if (!mddev->persistent) {
6765 if (info->state & (1<<MD_DISK_SYNC) &&
6766 info->raid_disk < mddev->raid_disks) {
6767 rdev->raid_disk = info->raid_disk;
6768 set_bit(In_sync, &rdev->flags);
6769 clear_bit(Bitmap_sync, &rdev->flags);
6771 rdev->raid_disk = -1;
6772 rdev->saved_raid_disk = rdev->raid_disk;
6774 super_types[mddev->major_version].
6775 validate_super(mddev, rdev);
6776 if ((info->state & (1<<MD_DISK_SYNC)) &&
6777 rdev->raid_disk != info->raid_disk) {
6778 /* This was a hot-add request, but events doesn't
6779 * match, so reject it.
6785 clear_bit(In_sync, &rdev->flags); /* just to be sure */
6786 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6787 set_bit(WriteMostly, &rdev->flags);
6789 clear_bit(WriteMostly, &rdev->flags);
6790 if (info->state & (1<<MD_DISK_FAILFAST))
6791 set_bit(FailFast, &rdev->flags);
6793 clear_bit(FailFast, &rdev->flags);
6795 if (info->state & (1<<MD_DISK_JOURNAL)) {
6796 struct md_rdev *rdev2;
6797 bool has_journal = false;
6799 /* make sure no existing journal disk */
6800 rdev_for_each(rdev2, mddev) {
6801 if (test_bit(Journal, &rdev2->flags)) {
6806 if (has_journal || mddev->bitmap) {
6810 set_bit(Journal, &rdev->flags);
6813 * check whether the device shows up in other nodes
6815 if (mddev_is_clustered(mddev)) {
6816 if (info->state & (1 << MD_DISK_CANDIDATE))
6817 set_bit(Candidate, &rdev->flags);
6818 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
6819 /* --add initiated by this node */
6820 err = md_cluster_ops->add_new_disk(mddev, rdev);
6828 rdev->raid_disk = -1;
6829 err = bind_rdev_to_array(rdev, mddev);
6834 if (mddev_is_clustered(mddev)) {
6835 if (info->state & (1 << MD_DISK_CANDIDATE)) {
6837 err = md_cluster_ops->new_disk_ack(mddev,
6840 md_kick_rdev_from_array(rdev);
6844 md_cluster_ops->add_new_disk_cancel(mddev);
6846 err = add_bound_rdev(rdev);
6850 err = add_bound_rdev(rdev);
6855 /* otherwise, md_add_new_disk is only allowed
6856 * for major_version==0 superblocks
6858 if (mddev->major_version != 0) {
6859 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev));
6863 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6865 rdev = md_import_device(dev, -1, 0);
6867 pr_warn("md: error, md_import_device() returned %ld\n",
6869 return PTR_ERR(rdev);
6871 rdev->desc_nr = info->number;
6872 if (info->raid_disk < mddev->raid_disks)
6873 rdev->raid_disk = info->raid_disk;
6875 rdev->raid_disk = -1;
6877 if (rdev->raid_disk < mddev->raid_disks)
6878 if (info->state & (1<<MD_DISK_SYNC))
6879 set_bit(In_sync, &rdev->flags);
6881 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6882 set_bit(WriteMostly, &rdev->flags);
6883 if (info->state & (1<<MD_DISK_FAILFAST))
6884 set_bit(FailFast, &rdev->flags);
6886 if (!mddev->persistent) {
6887 pr_debug("md: nonpersistent superblock ...\n");
6888 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6890 rdev->sb_start = calc_dev_sboffset(rdev);
6891 rdev->sectors = rdev->sb_start;
6893 err = bind_rdev_to_array(rdev, mddev);
6903 static int hot_remove_disk(struct mddev *mddev, dev_t dev)
6905 char b[BDEVNAME_SIZE];
6906 struct md_rdev *rdev;
6911 rdev = find_rdev(mddev, dev);
6915 if (rdev->raid_disk < 0)
6918 clear_bit(Blocked, &rdev->flags);
6919 remove_and_add_spares(mddev, rdev);
6921 if (rdev->raid_disk >= 0)
6925 if (mddev_is_clustered(mddev)) {
6926 if (md_cluster_ops->remove_disk(mddev, rdev))
6930 md_kick_rdev_from_array(rdev);
6931 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
6933 md_wakeup_thread(mddev->thread);
6935 md_update_sb(mddev, 1);
6936 md_new_event(mddev);
6940 pr_debug("md: cannot remove active disk %s from %s ...\n",
6941 bdevname(rdev->bdev,b), mdname(mddev));
6945 static int hot_add_disk(struct mddev *mddev, dev_t dev)
6947 char b[BDEVNAME_SIZE];
6949 struct md_rdev *rdev;
6954 if (mddev->major_version != 0) {
6955 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
6959 if (!mddev->pers->hot_add_disk) {
6960 pr_warn("%s: personality does not support diskops!\n",
6965 rdev = md_import_device(dev, -1, 0);
6967 pr_warn("md: error, md_import_device() returned %ld\n",
6972 if (mddev->persistent)
6973 rdev->sb_start = calc_dev_sboffset(rdev);
6975 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6977 rdev->sectors = rdev->sb_start;
6979 if (test_bit(Faulty, &rdev->flags)) {
6980 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
6981 bdevname(rdev->bdev,b), mdname(mddev));
6986 clear_bit(In_sync, &rdev->flags);
6988 rdev->saved_raid_disk = -1;
6989 err = bind_rdev_to_array(rdev, mddev);
6994 * The rest should better be atomic, we can have disk failures
6995 * noticed in interrupt contexts ...
6998 rdev->raid_disk = -1;
7000 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
7002 md_update_sb(mddev, 1);
7004 * Kick recovery, maybe this spare has to be added to the
7005 * array immediately.
7007 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7008 md_wakeup_thread(mddev->thread);
7009 md_new_event(mddev);
7017 static int set_bitmap_file(struct mddev *mddev, int fd)
7022 if (!mddev->pers->quiesce || !mddev->thread)
7024 if (mddev->recovery || mddev->sync_thread)
7026 /* we should be able to change the bitmap.. */
7030 struct inode *inode;
7033 if (mddev->bitmap || mddev->bitmap_info.file)
7034 return -EEXIST; /* cannot add when bitmap is present */
7038 pr_warn("%s: error: failed to get bitmap file\n",
7043 inode = f->f_mapping->host;
7044 if (!S_ISREG(inode->i_mode)) {
7045 pr_warn("%s: error: bitmap file must be a regular file\n",
7048 } else if (!(f->f_mode & FMODE_WRITE)) {
7049 pr_warn("%s: error: bitmap file must open for write\n",
7052 } else if (atomic_read(&inode->i_writecount) != 1) {
7053 pr_warn("%s: error: bitmap file is already in use\n",
7061 mddev->bitmap_info.file = f;
7062 mddev->bitmap_info.offset = 0; /* file overrides offset */
7063 } else if (mddev->bitmap == NULL)
7064 return -ENOENT; /* cannot remove what isn't there */
7068 struct bitmap *bitmap;
7070 bitmap = md_bitmap_create(mddev, -1);
7071 mddev_suspend(mddev);
7072 if (!IS_ERR(bitmap)) {
7073 mddev->bitmap = bitmap;
7074 err = md_bitmap_load(mddev);
7076 err = PTR_ERR(bitmap);
7078 md_bitmap_destroy(mddev);
7081 mddev_resume(mddev);
7082 } else if (fd < 0) {
7083 mddev_suspend(mddev);
7084 md_bitmap_destroy(mddev);
7085 mddev_resume(mddev);
7089 struct file *f = mddev->bitmap_info.file;
7091 spin_lock(&mddev->lock);
7092 mddev->bitmap_info.file = NULL;
7093 spin_unlock(&mddev->lock);
7102 * md_set_array_info is used two different ways
7103 * The original usage is when creating a new array.
7104 * In this usage, raid_disks is > 0 and it together with
7105 * level, size, not_persistent,layout,chunksize determine the
7106 * shape of the array.
7107 * This will always create an array with a type-0.90.0 superblock.
7108 * The newer usage is when assembling an array.
7109 * In this case raid_disks will be 0, and the major_version field is
7110 * use to determine which style super-blocks are to be found on the devices.
7111 * The minor and patch _version numbers are also kept incase the
7112 * super_block handler wishes to interpret them.
7114 int md_set_array_info(struct mddev *mddev, struct mdu_array_info_s *info)
7116 if (info->raid_disks == 0) {
7117 /* just setting version number for superblock loading */
7118 if (info->major_version < 0 ||
7119 info->major_version >= ARRAY_SIZE(super_types) ||
7120 super_types[info->major_version].name == NULL) {
7121 /* maybe try to auto-load a module? */
7122 pr_warn("md: superblock version %d not known\n",
7123 info->major_version);
7126 mddev->major_version = info->major_version;
7127 mddev->minor_version = info->minor_version;
7128 mddev->patch_version = info->patch_version;
7129 mddev->persistent = !info->not_persistent;
7130 /* ensure mddev_put doesn't delete this now that there
7131 * is some minimal configuration.
7133 mddev->ctime = ktime_get_real_seconds();
7136 mddev->major_version = MD_MAJOR_VERSION;
7137 mddev->minor_version = MD_MINOR_VERSION;
7138 mddev->patch_version = MD_PATCHLEVEL_VERSION;
7139 mddev->ctime = ktime_get_real_seconds();
7141 mddev->level = info->level;
7142 mddev->clevel[0] = 0;
7143 mddev->dev_sectors = 2 * (sector_t)info->size;
7144 mddev->raid_disks = info->raid_disks;
7145 /* don't set md_minor, it is determined by which /dev/md* was
7148 if (info->state & (1<<MD_SB_CLEAN))
7149 mddev->recovery_cp = MaxSector;
7151 mddev->recovery_cp = 0;
7152 mddev->persistent = ! info->not_persistent;
7153 mddev->external = 0;
7155 mddev->layout = info->layout;
7156 if (mddev->level == 0)
7157 /* Cannot trust RAID0 layout info here */
7159 mddev->chunk_sectors = info->chunk_size >> 9;
7161 if (mddev->persistent) {
7162 mddev->max_disks = MD_SB_DISKS;
7164 mddev->sb_flags = 0;
7166 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
7168 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
7169 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
7170 mddev->bitmap_info.offset = 0;
7172 mddev->reshape_position = MaxSector;
7175 * Generate a 128 bit UUID
7177 get_random_bytes(mddev->uuid, 16);
7179 mddev->new_level = mddev->level;
7180 mddev->new_chunk_sectors = mddev->chunk_sectors;
7181 mddev->new_layout = mddev->layout;
7182 mddev->delta_disks = 0;
7183 mddev->reshape_backwards = 0;
7188 void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
7190 lockdep_assert_held(&mddev->reconfig_mutex);
7192 if (mddev->external_size)
7195 mddev->array_sectors = array_sectors;
7197 EXPORT_SYMBOL(md_set_array_sectors);
7199 static int update_size(struct mddev *mddev, sector_t num_sectors)
7201 struct md_rdev *rdev;
7203 int fit = (num_sectors == 0);
7204 sector_t old_dev_sectors = mddev->dev_sectors;
7206 if (mddev->pers->resize == NULL)
7208 /* The "num_sectors" is the number of sectors of each device that
7209 * is used. This can only make sense for arrays with redundancy.
7210 * linear and raid0 always use whatever space is available. We can only
7211 * consider changing this number if no resync or reconstruction is
7212 * happening, and if the new size is acceptable. It must fit before the
7213 * sb_start or, if that is <data_offset, it must fit before the size
7214 * of each device. If num_sectors is zero, we find the largest size
7217 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
7223 rdev_for_each(rdev, mddev) {
7224 sector_t avail = rdev->sectors;
7226 if (fit && (num_sectors == 0 || num_sectors > avail))
7227 num_sectors = avail;
7228 if (avail < num_sectors)
7231 rv = mddev->pers->resize(mddev, num_sectors);
7233 if (mddev_is_clustered(mddev))
7234 md_cluster_ops->update_size(mddev, old_dev_sectors);
7235 else if (mddev->queue) {
7236 set_capacity_and_notify(mddev->gendisk,
7237 mddev->array_sectors);
7243 static int update_raid_disks(struct mddev *mddev, int raid_disks)
7246 struct md_rdev *rdev;
7247 /* change the number of raid disks */
7248 if (mddev->pers->check_reshape == NULL)
7252 if (raid_disks <= 0 ||
7253 (mddev->max_disks && raid_disks >= mddev->max_disks))
7255 if (mddev->sync_thread ||
7256 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
7257 test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) ||
7258 mddev->reshape_position != MaxSector)
7261 rdev_for_each(rdev, mddev) {
7262 if (mddev->raid_disks < raid_disks &&
7263 rdev->data_offset < rdev->new_data_offset)
7265 if (mddev->raid_disks > raid_disks &&
7266 rdev->data_offset > rdev->new_data_offset)
7270 mddev->delta_disks = raid_disks - mddev->raid_disks;
7271 if (mddev->delta_disks < 0)
7272 mddev->reshape_backwards = 1;
7273 else if (mddev->delta_disks > 0)
7274 mddev->reshape_backwards = 0;
7276 rv = mddev->pers->check_reshape(mddev);
7278 mddev->delta_disks = 0;
7279 mddev->reshape_backwards = 0;
7285 * update_array_info is used to change the configuration of an
7287 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
7288 * fields in the info are checked against the array.
7289 * Any differences that cannot be handled will cause an error.
7290 * Normally, only one change can be managed at a time.
7292 static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
7298 /* calculate expected state,ignoring low bits */
7299 if (mddev->bitmap && mddev->bitmap_info.offset)
7300 state |= (1 << MD_SB_BITMAP_PRESENT);
7302 if (mddev->major_version != info->major_version ||
7303 mddev->minor_version != info->minor_version ||
7304 /* mddev->patch_version != info->patch_version || */
7305 mddev->ctime != info->ctime ||
7306 mddev->level != info->level ||
7307 /* mddev->layout != info->layout || */
7308 mddev->persistent != !info->not_persistent ||
7309 mddev->chunk_sectors != info->chunk_size >> 9 ||
7310 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
7311 ((state^info->state) & 0xfffffe00)
7314 /* Check there is only one change */
7315 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
7317 if (mddev->raid_disks != info->raid_disks)
7319 if (mddev->layout != info->layout)
7321 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
7328 if (mddev->layout != info->layout) {
7330 * we don't need to do anything at the md level, the
7331 * personality will take care of it all.
7333 if (mddev->pers->check_reshape == NULL)
7336 mddev->new_layout = info->layout;
7337 rv = mddev->pers->check_reshape(mddev);
7339 mddev->new_layout = mddev->layout;
7343 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
7344 rv = update_size(mddev, (sector_t)info->size * 2);
7346 if (mddev->raid_disks != info->raid_disks)
7347 rv = update_raid_disks(mddev, info->raid_disks);
7349 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
7350 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
7354 if (mddev->recovery || mddev->sync_thread) {
7358 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
7359 struct bitmap *bitmap;
7360 /* add the bitmap */
7361 if (mddev->bitmap) {
7365 if (mddev->bitmap_info.default_offset == 0) {
7369 mddev->bitmap_info.offset =
7370 mddev->bitmap_info.default_offset;
7371 mddev->bitmap_info.space =
7372 mddev->bitmap_info.default_space;
7373 bitmap = md_bitmap_create(mddev, -1);
7374 mddev_suspend(mddev);
7375 if (!IS_ERR(bitmap)) {
7376 mddev->bitmap = bitmap;
7377 rv = md_bitmap_load(mddev);
7379 rv = PTR_ERR(bitmap);
7381 md_bitmap_destroy(mddev);
7382 mddev_resume(mddev);
7384 /* remove the bitmap */
7385 if (!mddev->bitmap) {
7389 if (mddev->bitmap->storage.file) {
7393 if (mddev->bitmap_info.nodes) {
7394 /* hold PW on all the bitmap lock */
7395 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
7396 pr_warn("md: can't change bitmap to none since the array is in use by more than one node\n");
7398 md_cluster_ops->unlock_all_bitmaps(mddev);
7402 mddev->bitmap_info.nodes = 0;
7403 md_cluster_ops->leave(mddev);
7404 module_put(md_cluster_mod);
7405 mddev->safemode_delay = DEFAULT_SAFEMODE_DELAY;
7407 mddev_suspend(mddev);
7408 md_bitmap_destroy(mddev);
7409 mddev_resume(mddev);
7410 mddev->bitmap_info.offset = 0;
7413 md_update_sb(mddev, 1);
7419 static int set_disk_faulty(struct mddev *mddev, dev_t dev)
7421 struct md_rdev *rdev;
7424 if (mddev->pers == NULL)
7428 rdev = md_find_rdev_rcu(mddev, dev);
7432 md_error(mddev, rdev);
7433 if (!test_bit(Faulty, &rdev->flags))
7441 * We have a problem here : there is no easy way to give a CHS
7442 * virtual geometry. We currently pretend that we have a 2 heads
7443 * 4 sectors (with a BIG number of cylinders...). This drives
7444 * dosfs just mad... ;-)
7446 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
7448 struct mddev *mddev = bdev->bd_disk->private_data;
7452 geo->cylinders = mddev->array_sectors / 8;
7456 static inline bool md_ioctl_valid(unsigned int cmd)
7460 case GET_ARRAY_INFO:
7461 case GET_BITMAP_FILE:
7464 case HOT_REMOVE_DISK:
7466 case RESTART_ARRAY_RW:
7468 case SET_ARRAY_INFO:
7469 case SET_BITMAP_FILE:
7470 case SET_DISK_FAULTY:
7473 case CLUSTERED_DISK_NACK:
7480 static int md_ioctl(struct block_device *bdev, fmode_t mode,
7481 unsigned int cmd, unsigned long arg)
7484 void __user *argp = (void __user *)arg;
7485 struct mddev *mddev = NULL;
7486 bool did_set_md_closing = false;
7488 if (!md_ioctl_valid(cmd))
7493 case GET_ARRAY_INFO:
7497 if (!capable(CAP_SYS_ADMIN))
7502 * Commands dealing with the RAID driver but not any
7507 err = get_version(argp);
7513 * Commands creating/starting a new array:
7516 mddev = bdev->bd_disk->private_data;
7523 /* Some actions do not requires the mutex */
7525 case GET_ARRAY_INFO:
7526 if (!mddev->raid_disks && !mddev->external)
7529 err = get_array_info(mddev, argp);
7533 if (!mddev->raid_disks && !mddev->external)
7536 err = get_disk_info(mddev, argp);
7539 case SET_DISK_FAULTY:
7540 err = set_disk_faulty(mddev, new_decode_dev(arg));
7543 case GET_BITMAP_FILE:
7544 err = get_bitmap_file(mddev, argp);
7549 if (cmd == ADD_NEW_DISK || cmd == HOT_ADD_DISK)
7550 flush_rdev_wq(mddev);
7552 if (cmd == HOT_REMOVE_DISK)
7553 /* need to ensure recovery thread has run */
7554 wait_event_interruptible_timeout(mddev->sb_wait,
7555 !test_bit(MD_RECOVERY_NEEDED,
7557 msecs_to_jiffies(5000));
7558 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
7559 /* Need to flush page cache, and ensure no-one else opens
7562 mutex_lock(&mddev->open_mutex);
7563 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
7564 mutex_unlock(&mddev->open_mutex);
7568 if (test_and_set_bit(MD_CLOSING, &mddev->flags)) {
7569 mutex_unlock(&mddev->open_mutex);
7573 did_set_md_closing = true;
7574 mutex_unlock(&mddev->open_mutex);
7575 sync_blockdev(bdev);
7577 err = mddev_lock(mddev);
7579 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7584 if (cmd == SET_ARRAY_INFO) {
7585 mdu_array_info_t info;
7587 memset(&info, 0, sizeof(info));
7588 else if (copy_from_user(&info, argp, sizeof(info))) {
7593 err = update_array_info(mddev, &info);
7595 pr_warn("md: couldn't update array info. %d\n", err);
7600 if (!list_empty(&mddev->disks)) {
7601 pr_warn("md: array %s already has disks!\n", mdname(mddev));
7605 if (mddev->raid_disks) {
7606 pr_warn("md: array %s already initialised!\n", mdname(mddev));
7610 err = md_set_array_info(mddev, &info);
7612 pr_warn("md: couldn't set array info. %d\n", err);
7619 * Commands querying/configuring an existing array:
7621 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
7622 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
7623 if ((!mddev->raid_disks && !mddev->external)
7624 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
7625 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
7626 && cmd != GET_BITMAP_FILE) {
7632 * Commands even a read-only array can execute:
7635 case RESTART_ARRAY_RW:
7636 err = restart_array(mddev);
7640 err = do_md_stop(mddev, 0, bdev);
7644 err = md_set_readonly(mddev, bdev);
7647 case HOT_REMOVE_DISK:
7648 err = hot_remove_disk(mddev, new_decode_dev(arg));
7652 /* We can support ADD_NEW_DISK on read-only arrays
7653 * only if we are re-adding a preexisting device.
7654 * So require mddev->pers and MD_DISK_SYNC.
7657 mdu_disk_info_t info;
7658 if (copy_from_user(&info, argp, sizeof(info)))
7660 else if (!(info.state & (1<<MD_DISK_SYNC)))
7661 /* Need to clear read-only for this */
7664 err = md_add_new_disk(mddev, &info);
7671 * The remaining ioctls are changing the state of the
7672 * superblock, so we do not allow them on read-only arrays.
7674 if (mddev->ro && mddev->pers) {
7675 if (mddev->ro == 2) {
7677 sysfs_notify_dirent_safe(mddev->sysfs_state);
7678 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7679 /* mddev_unlock will wake thread */
7680 /* If a device failed while we were read-only, we
7681 * need to make sure the metadata is updated now.
7683 if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) {
7684 mddev_unlock(mddev);
7685 wait_event(mddev->sb_wait,
7686 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) &&
7687 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
7688 mddev_lock_nointr(mddev);
7699 mdu_disk_info_t info;
7700 if (copy_from_user(&info, argp, sizeof(info)))
7703 err = md_add_new_disk(mddev, &info);
7707 case CLUSTERED_DISK_NACK:
7708 if (mddev_is_clustered(mddev))
7709 md_cluster_ops->new_disk_ack(mddev, false);
7715 err = hot_add_disk(mddev, new_decode_dev(arg));
7719 err = do_md_run(mddev);
7722 case SET_BITMAP_FILE:
7723 err = set_bitmap_file(mddev, (int)arg);
7732 if (mddev->hold_active == UNTIL_IOCTL &&
7734 mddev->hold_active = 0;
7735 mddev_unlock(mddev);
7737 if(did_set_md_closing)
7738 clear_bit(MD_CLOSING, &mddev->flags);
7741 #ifdef CONFIG_COMPAT
7742 static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7743 unsigned int cmd, unsigned long arg)
7746 case HOT_REMOVE_DISK:
7748 case SET_DISK_FAULTY:
7749 case SET_BITMAP_FILE:
7750 /* These take in integer arg, do not convert */
7753 arg = (unsigned long)compat_ptr(arg);
7757 return md_ioctl(bdev, mode, cmd, arg);
7759 #endif /* CONFIG_COMPAT */
7761 static int md_set_read_only(struct block_device *bdev, bool ro)
7763 struct mddev *mddev = bdev->bd_disk->private_data;
7766 err = mddev_lock(mddev);
7770 if (!mddev->raid_disks && !mddev->external) {
7776 * Transitioning to read-auto need only happen for arrays that call
7777 * md_write_start and which are not ready for writes yet.
7779 if (!ro && mddev->ro == 1 && mddev->pers) {
7780 err = restart_array(mddev);
7787 mddev_unlock(mddev);
7791 static int md_open(struct block_device *bdev, fmode_t mode)
7794 * Succeed if we can lock the mddev, which confirms that
7795 * it isn't being stopped right now.
7797 struct mddev *mddev = mddev_find(bdev->bd_dev);
7803 if (mddev->gendisk != bdev->bd_disk) {
7804 /* we are racing with mddev_put which is discarding this
7808 /* Wait until bdev->bd_disk is definitely gone */
7809 if (work_pending(&mddev->del_work))
7810 flush_workqueue(md_misc_wq);
7813 BUG_ON(mddev != bdev->bd_disk->private_data);
7815 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
7818 if (test_bit(MD_CLOSING, &mddev->flags)) {
7819 mutex_unlock(&mddev->open_mutex);
7825 atomic_inc(&mddev->openers);
7826 mutex_unlock(&mddev->open_mutex);
7828 bdev_check_media_change(bdev);
7835 static void md_release(struct gendisk *disk, fmode_t mode)
7837 struct mddev *mddev = disk->private_data;
7840 atomic_dec(&mddev->openers);
7844 static unsigned int md_check_events(struct gendisk *disk, unsigned int clearing)
7846 struct mddev *mddev = disk->private_data;
7847 unsigned int ret = 0;
7850 ret = DISK_EVENT_MEDIA_CHANGE;
7855 const struct block_device_operations md_fops =
7857 .owner = THIS_MODULE,
7858 .submit_bio = md_submit_bio,
7860 .release = md_release,
7862 #ifdef CONFIG_COMPAT
7863 .compat_ioctl = md_compat_ioctl,
7865 .getgeo = md_getgeo,
7866 .check_events = md_check_events,
7867 .set_read_only = md_set_read_only,
7870 static int md_thread(void *arg)
7872 struct md_thread *thread = arg;
7875 * md_thread is a 'system-thread', it's priority should be very
7876 * high. We avoid resource deadlocks individually in each
7877 * raid personality. (RAID5 does preallocation) We also use RR and
7878 * the very same RT priority as kswapd, thus we will never get
7879 * into a priority inversion deadlock.
7881 * we definitely have to have equal or higher priority than
7882 * bdflush, otherwise bdflush will deadlock if there are too
7883 * many dirty RAID5 blocks.
7886 allow_signal(SIGKILL);
7887 while (!kthread_should_stop()) {
7889 /* We need to wait INTERRUPTIBLE so that
7890 * we don't add to the load-average.
7891 * That means we need to be sure no signals are
7894 if (signal_pending(current))
7895 flush_signals(current);
7897 wait_event_interruptible_timeout
7899 test_bit(THREAD_WAKEUP, &thread->flags)
7900 || kthread_should_stop() || kthread_should_park(),
7903 clear_bit(THREAD_WAKEUP, &thread->flags);
7904 if (kthread_should_park())
7906 if (!kthread_should_stop())
7907 thread->run(thread);
7913 void md_wakeup_thread(struct md_thread *thread)
7916 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
7917 set_bit(THREAD_WAKEUP, &thread->flags);
7918 wake_up(&thread->wqueue);
7921 EXPORT_SYMBOL(md_wakeup_thread);
7923 struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7924 struct mddev *mddev, const char *name)
7926 struct md_thread *thread;
7928 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
7932 init_waitqueue_head(&thread->wqueue);
7935 thread->mddev = mddev;
7936 thread->timeout = MAX_SCHEDULE_TIMEOUT;
7937 thread->tsk = kthread_run(md_thread, thread,
7939 mdname(thread->mddev),
7941 if (IS_ERR(thread->tsk)) {
7947 EXPORT_SYMBOL(md_register_thread);
7949 void md_unregister_thread(struct md_thread **threadp)
7951 struct md_thread *thread = *threadp;
7954 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
7955 /* Locking ensures that mddev_unlock does not wake_up a
7956 * non-existent thread
7958 spin_lock(&pers_lock);
7960 spin_unlock(&pers_lock);
7962 kthread_stop(thread->tsk);
7965 EXPORT_SYMBOL(md_unregister_thread);
7967 void md_error(struct mddev *mddev, struct md_rdev *rdev)
7969 if (!rdev || test_bit(Faulty, &rdev->flags))
7972 if (!mddev->pers || !mddev->pers->error_handler)
7974 mddev->pers->error_handler(mddev,rdev);
7975 if (mddev->degraded)
7976 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7977 sysfs_notify_dirent_safe(rdev->sysfs_state);
7978 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7979 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7980 md_wakeup_thread(mddev->thread);
7981 if (mddev->event_work.func)
7982 queue_work(md_misc_wq, &mddev->event_work);
7983 md_new_event(mddev);
7985 EXPORT_SYMBOL(md_error);
7987 /* seq_file implementation /proc/mdstat */
7989 static void status_unused(struct seq_file *seq)
7992 struct md_rdev *rdev;
7994 seq_printf(seq, "unused devices: ");
7996 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
7997 char b[BDEVNAME_SIZE];
7999 seq_printf(seq, "%s ",
8000 bdevname(rdev->bdev,b));
8003 seq_printf(seq, "<none>");
8005 seq_printf(seq, "\n");
8008 static int status_resync(struct seq_file *seq, struct mddev *mddev)
8010 sector_t max_sectors, resync, res;
8011 unsigned long dt, db = 0;
8012 sector_t rt, curr_mark_cnt, resync_mark_cnt;
8013 int scale, recovery_active;
8014 unsigned int per_milli;
8016 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8017 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
8018 max_sectors = mddev->resync_max_sectors;
8020 max_sectors = mddev->dev_sectors;
8022 resync = mddev->curr_resync;
8024 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
8025 /* Still cleaning up */
8026 resync = max_sectors;
8027 } else if (resync > max_sectors)
8028 resync = max_sectors;
8030 resync -= atomic_read(&mddev->recovery_active);
8033 if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery)) {
8034 struct md_rdev *rdev;
8036 rdev_for_each(rdev, mddev)
8037 if (rdev->raid_disk >= 0 &&
8038 !test_bit(Faulty, &rdev->flags) &&
8039 rdev->recovery_offset != MaxSector &&
8040 rdev->recovery_offset) {
8041 seq_printf(seq, "\trecover=REMOTE");
8044 if (mddev->reshape_position != MaxSector)
8045 seq_printf(seq, "\treshape=REMOTE");
8047 seq_printf(seq, "\tresync=REMOTE");
8050 if (mddev->recovery_cp < MaxSector) {
8051 seq_printf(seq, "\tresync=PENDING");
8057 seq_printf(seq, "\tresync=DELAYED");
8061 WARN_ON(max_sectors == 0);
8062 /* Pick 'scale' such that (resync>>scale)*1000 will fit
8063 * in a sector_t, and (max_sectors>>scale) will fit in a
8064 * u32, as those are the requirements for sector_div.
8065 * Thus 'scale' must be at least 10
8068 if (sizeof(sector_t) > sizeof(unsigned long)) {
8069 while ( max_sectors/2 > (1ULL<<(scale+32)))
8072 res = (resync>>scale)*1000;
8073 sector_div(res, (u32)((max_sectors>>scale)+1));
8077 int i, x = per_milli/50, y = 20-x;
8078 seq_printf(seq, "[");
8079 for (i = 0; i < x; i++)
8080 seq_printf(seq, "=");
8081 seq_printf(seq, ">");
8082 for (i = 0; i < y; i++)
8083 seq_printf(seq, ".");
8084 seq_printf(seq, "] ");
8086 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
8087 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
8089 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
8091 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
8092 "resync" : "recovery"))),
8093 per_milli/10, per_milli % 10,
8094 (unsigned long long) resync/2,
8095 (unsigned long long) max_sectors/2);
8098 * dt: time from mark until now
8099 * db: blocks written from mark until now
8100 * rt: remaining time
8102 * rt is a sector_t, which is always 64bit now. We are keeping
8103 * the original algorithm, but it is not really necessary.
8105 * Original algorithm:
8106 * So we divide before multiply in case it is 32bit and close
8108 * We scale the divisor (db) by 32 to avoid losing precision
8109 * near the end of resync when the number of remaining sectors
8111 * We then divide rt by 32 after multiplying by db to compensate.
8112 * The '+1' avoids division by zero if db is very small.
8114 dt = ((jiffies - mddev->resync_mark) / HZ);
8117 curr_mark_cnt = mddev->curr_mark_cnt;
8118 recovery_active = atomic_read(&mddev->recovery_active);
8119 resync_mark_cnt = mddev->resync_mark_cnt;
8121 if (curr_mark_cnt >= (recovery_active + resync_mark_cnt))
8122 db = curr_mark_cnt - (recovery_active + resync_mark_cnt);
8124 rt = max_sectors - resync; /* number of remaining sectors */
8125 rt = div64_u64(rt, db/32+1);
8129 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
8130 ((unsigned long)rt % 60)/6);
8132 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
8136 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
8138 struct list_head *tmp;
8140 struct mddev *mddev;
8152 spin_lock(&all_mddevs_lock);
8153 list_for_each(tmp,&all_mddevs)
8155 mddev = list_entry(tmp, struct mddev, all_mddevs);
8157 spin_unlock(&all_mddevs_lock);
8160 spin_unlock(&all_mddevs_lock);
8162 return (void*)2;/* tail */
8166 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
8168 struct list_head *tmp;
8169 struct mddev *next_mddev, *mddev = v;
8175 spin_lock(&all_mddevs_lock);
8177 tmp = all_mddevs.next;
8179 tmp = mddev->all_mddevs.next;
8180 if (tmp != &all_mddevs)
8181 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
8183 next_mddev = (void*)2;
8186 spin_unlock(&all_mddevs_lock);
8194 static void md_seq_stop(struct seq_file *seq, void *v)
8196 struct mddev *mddev = v;
8198 if (mddev && v != (void*)1 && v != (void*)2)
8202 static int md_seq_show(struct seq_file *seq, void *v)
8204 struct mddev *mddev = v;
8206 struct md_rdev *rdev;
8208 if (v == (void*)1) {
8209 struct md_personality *pers;
8210 seq_printf(seq, "Personalities : ");
8211 spin_lock(&pers_lock);
8212 list_for_each_entry(pers, &pers_list, list)
8213 seq_printf(seq, "[%s] ", pers->name);
8215 spin_unlock(&pers_lock);
8216 seq_printf(seq, "\n");
8217 seq->poll_event = atomic_read(&md_event_count);
8220 if (v == (void*)2) {
8225 spin_lock(&mddev->lock);
8226 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
8227 seq_printf(seq, "%s : %sactive", mdname(mddev),
8228 mddev->pers ? "" : "in");
8231 seq_printf(seq, " (read-only)");
8233 seq_printf(seq, " (auto-read-only)");
8234 seq_printf(seq, " %s", mddev->pers->name);
8239 rdev_for_each_rcu(rdev, mddev) {
8240 char b[BDEVNAME_SIZE];
8241 seq_printf(seq, " %s[%d]",
8242 bdevname(rdev->bdev,b), rdev->desc_nr);
8243 if (test_bit(WriteMostly, &rdev->flags))
8244 seq_printf(seq, "(W)");
8245 if (test_bit(Journal, &rdev->flags))
8246 seq_printf(seq, "(J)");
8247 if (test_bit(Faulty, &rdev->flags)) {
8248 seq_printf(seq, "(F)");
8251 if (rdev->raid_disk < 0)
8252 seq_printf(seq, "(S)"); /* spare */
8253 if (test_bit(Replacement, &rdev->flags))
8254 seq_printf(seq, "(R)");
8255 sectors += rdev->sectors;
8259 if (!list_empty(&mddev->disks)) {
8261 seq_printf(seq, "\n %llu blocks",
8262 (unsigned long long)
8263 mddev->array_sectors / 2);
8265 seq_printf(seq, "\n %llu blocks",
8266 (unsigned long long)sectors / 2);
8268 if (mddev->persistent) {
8269 if (mddev->major_version != 0 ||
8270 mddev->minor_version != 90) {
8271 seq_printf(seq," super %d.%d",
8272 mddev->major_version,
8273 mddev->minor_version);
8275 } else if (mddev->external)
8276 seq_printf(seq, " super external:%s",
8277 mddev->metadata_type);
8279 seq_printf(seq, " super non-persistent");
8282 mddev->pers->status(seq, mddev);
8283 seq_printf(seq, "\n ");
8284 if (mddev->pers->sync_request) {
8285 if (status_resync(seq, mddev))
8286 seq_printf(seq, "\n ");
8289 seq_printf(seq, "\n ");
8291 md_bitmap_status(seq, mddev->bitmap);
8293 seq_printf(seq, "\n");
8295 spin_unlock(&mddev->lock);
8300 static const struct seq_operations md_seq_ops = {
8301 .start = md_seq_start,
8302 .next = md_seq_next,
8303 .stop = md_seq_stop,
8304 .show = md_seq_show,
8307 static int md_seq_open(struct inode *inode, struct file *file)
8309 struct seq_file *seq;
8312 error = seq_open(file, &md_seq_ops);
8316 seq = file->private_data;
8317 seq->poll_event = atomic_read(&md_event_count);
8321 static int md_unloading;
8322 static __poll_t mdstat_poll(struct file *filp, poll_table *wait)
8324 struct seq_file *seq = filp->private_data;
8328 return EPOLLIN|EPOLLRDNORM|EPOLLERR|EPOLLPRI;
8329 poll_wait(filp, &md_event_waiters, wait);
8331 /* always allow read */
8332 mask = EPOLLIN | EPOLLRDNORM;
8334 if (seq->poll_event != atomic_read(&md_event_count))
8335 mask |= EPOLLERR | EPOLLPRI;
8339 static const struct proc_ops mdstat_proc_ops = {
8340 .proc_open = md_seq_open,
8341 .proc_read = seq_read,
8342 .proc_lseek = seq_lseek,
8343 .proc_release = seq_release,
8344 .proc_poll = mdstat_poll,
8347 int register_md_personality(struct md_personality *p)
8349 pr_debug("md: %s personality registered for level %d\n",
8351 spin_lock(&pers_lock);
8352 list_add_tail(&p->list, &pers_list);
8353 spin_unlock(&pers_lock);
8356 EXPORT_SYMBOL(register_md_personality);
8358 int unregister_md_personality(struct md_personality *p)
8360 pr_debug("md: %s personality unregistered\n", p->name);
8361 spin_lock(&pers_lock);
8362 list_del_init(&p->list);
8363 spin_unlock(&pers_lock);
8366 EXPORT_SYMBOL(unregister_md_personality);
8368 int register_md_cluster_operations(struct md_cluster_operations *ops,
8369 struct module *module)
8372 spin_lock(&pers_lock);
8373 if (md_cluster_ops != NULL)
8376 md_cluster_ops = ops;
8377 md_cluster_mod = module;
8379 spin_unlock(&pers_lock);
8382 EXPORT_SYMBOL(register_md_cluster_operations);
8384 int unregister_md_cluster_operations(void)
8386 spin_lock(&pers_lock);
8387 md_cluster_ops = NULL;
8388 spin_unlock(&pers_lock);
8391 EXPORT_SYMBOL(unregister_md_cluster_operations);
8393 int md_setup_cluster(struct mddev *mddev, int nodes)
8396 if (!md_cluster_ops)
8397 request_module("md-cluster");
8398 spin_lock(&pers_lock);
8399 /* ensure module won't be unloaded */
8400 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
8401 pr_warn("can't find md-cluster module or get it's reference.\n");
8402 spin_unlock(&pers_lock);
8405 spin_unlock(&pers_lock);
8407 ret = md_cluster_ops->join(mddev, nodes);
8409 mddev->safemode_delay = 0;
8413 void md_cluster_stop(struct mddev *mddev)
8415 if (!md_cluster_ops)
8417 md_cluster_ops->leave(mddev);
8418 module_put(md_cluster_mod);
8421 static int is_mddev_idle(struct mddev *mddev, int init)
8423 struct md_rdev *rdev;
8429 rdev_for_each_rcu(rdev, mddev) {
8430 struct gendisk *disk = rdev->bdev->bd_disk;
8431 curr_events = (int)part_stat_read_accum(disk->part0, sectors) -
8432 atomic_read(&disk->sync_io);
8433 /* sync IO will cause sync_io to increase before the disk_stats
8434 * as sync_io is counted when a request starts, and
8435 * disk_stats is counted when it completes.
8436 * So resync activity will cause curr_events to be smaller than
8437 * when there was no such activity.
8438 * non-sync IO will cause disk_stat to increase without
8439 * increasing sync_io so curr_events will (eventually)
8440 * be larger than it was before. Once it becomes
8441 * substantially larger, the test below will cause
8442 * the array to appear non-idle, and resync will slow
8444 * If there is a lot of outstanding resync activity when
8445 * we set last_event to curr_events, then all that activity
8446 * completing might cause the array to appear non-idle
8447 * and resync will be slowed down even though there might
8448 * not have been non-resync activity. This will only
8449 * happen once though. 'last_events' will soon reflect
8450 * the state where there is little or no outstanding
8451 * resync requests, and further resync activity will
8452 * always make curr_events less than last_events.
8455 if (init || curr_events - rdev->last_events > 64) {
8456 rdev->last_events = curr_events;
8464 void md_done_sync(struct mddev *mddev, int blocks, int ok)
8466 /* another "blocks" (512byte) blocks have been synced */
8467 atomic_sub(blocks, &mddev->recovery_active);
8468 wake_up(&mddev->recovery_wait);
8470 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
8471 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
8472 md_wakeup_thread(mddev->thread);
8473 // stop recovery, signal do_sync ....
8476 EXPORT_SYMBOL(md_done_sync);
8478 /* md_write_start(mddev, bi)
8479 * If we need to update some array metadata (e.g. 'active' flag
8480 * in superblock) before writing, schedule a superblock update
8481 * and wait for it to complete.
8482 * A return value of 'false' means that the write wasn't recorded
8483 * and cannot proceed as the array is being suspend.
8485 bool md_write_start(struct mddev *mddev, struct bio *bi)
8489 if (bio_data_dir(bi) != WRITE)
8492 BUG_ON(mddev->ro == 1);
8493 if (mddev->ro == 2) {
8494 /* need to switch to read/write */
8496 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8497 md_wakeup_thread(mddev->thread);
8498 md_wakeup_thread(mddev->sync_thread);
8502 percpu_ref_get(&mddev->writes_pending);
8503 smp_mb(); /* Match smp_mb in set_in_sync() */
8504 if (mddev->safemode == 1)
8505 mddev->safemode = 0;
8506 /* sync_checkers is always 0 when writes_pending is in per-cpu mode */
8507 if (mddev->in_sync || mddev->sync_checkers) {
8508 spin_lock(&mddev->lock);
8509 if (mddev->in_sync) {
8511 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8512 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
8513 md_wakeup_thread(mddev->thread);
8516 spin_unlock(&mddev->lock);
8520 sysfs_notify_dirent_safe(mddev->sysfs_state);
8521 if (!mddev->has_superblocks)
8523 wait_event(mddev->sb_wait,
8524 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags) ||
8526 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
8527 percpu_ref_put(&mddev->writes_pending);
8532 EXPORT_SYMBOL(md_write_start);
8534 /* md_write_inc can only be called when md_write_start() has
8535 * already been called at least once of the current request.
8536 * It increments the counter and is useful when a single request
8537 * is split into several parts. Each part causes an increment and
8538 * so needs a matching md_write_end().
8539 * Unlike md_write_start(), it is safe to call md_write_inc() inside
8540 * a spinlocked region.
8542 void md_write_inc(struct mddev *mddev, struct bio *bi)
8544 if (bio_data_dir(bi) != WRITE)
8546 WARN_ON_ONCE(mddev->in_sync || mddev->ro);
8547 percpu_ref_get(&mddev->writes_pending);
8549 EXPORT_SYMBOL(md_write_inc);
8551 void md_write_end(struct mddev *mddev)
8553 percpu_ref_put(&mddev->writes_pending);
8555 if (mddev->safemode == 2)
8556 md_wakeup_thread(mddev->thread);
8557 else if (mddev->safemode_delay)
8558 /* The roundup() ensures this only performs locking once
8559 * every ->safemode_delay jiffies
8561 mod_timer(&mddev->safemode_timer,
8562 roundup(jiffies, mddev->safemode_delay) +
8563 mddev->safemode_delay);
8566 EXPORT_SYMBOL(md_write_end);
8568 /* This is used by raid0 and raid10 */
8569 void md_submit_discard_bio(struct mddev *mddev, struct md_rdev *rdev,
8570 struct bio *bio, sector_t start, sector_t size)
8572 struct bio *discard_bio = NULL;
8574 if (__blkdev_issue_discard(rdev->bdev, start, size, GFP_NOIO, 0,
8575 &discard_bio) || !discard_bio)
8578 bio_chain(discard_bio, bio);
8579 bio_clone_blkg_association(discard_bio, bio);
8581 trace_block_bio_remap(discard_bio,
8582 disk_devt(mddev->gendisk),
8583 bio->bi_iter.bi_sector);
8584 submit_bio_noacct(discard_bio);
8586 EXPORT_SYMBOL_GPL(md_submit_discard_bio);
8588 static void md_end_io_acct(struct bio *bio)
8590 struct md_io_acct *md_io_acct = bio->bi_private;
8591 struct bio *orig_bio = md_io_acct->orig_bio;
8593 orig_bio->bi_status = bio->bi_status;
8595 bio_end_io_acct(orig_bio, md_io_acct->start_time);
8597 bio_endio(orig_bio);
8601 * Used by personalities that don't already clone the bio and thus can't
8602 * easily add the timestamp to their extended bio structure.
8604 void md_account_bio(struct mddev *mddev, struct bio **bio)
8606 struct md_io_acct *md_io_acct;
8609 if (!blk_queue_io_stat((*bio)->bi_bdev->bd_disk->queue))
8612 clone = bio_clone_fast(*bio, GFP_NOIO, &mddev->io_acct_set);
8613 md_io_acct = container_of(clone, struct md_io_acct, bio_clone);
8614 md_io_acct->orig_bio = *bio;
8615 md_io_acct->start_time = bio_start_io_acct(*bio);
8617 clone->bi_end_io = md_end_io_acct;
8618 clone->bi_private = md_io_acct;
8621 EXPORT_SYMBOL_GPL(md_account_bio);
8623 /* md_allow_write(mddev)
8624 * Calling this ensures that the array is marked 'active' so that writes
8625 * may proceed without blocking. It is important to call this before
8626 * attempting a GFP_KERNEL allocation while holding the mddev lock.
8627 * Must be called with mddev_lock held.
8629 void md_allow_write(struct mddev *mddev)
8635 if (!mddev->pers->sync_request)
8638 spin_lock(&mddev->lock);
8639 if (mddev->in_sync) {
8641 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8642 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
8643 if (mddev->safemode_delay &&
8644 mddev->safemode == 0)
8645 mddev->safemode = 1;
8646 spin_unlock(&mddev->lock);
8647 md_update_sb(mddev, 0);
8648 sysfs_notify_dirent_safe(mddev->sysfs_state);
8649 /* wait for the dirty state to be recorded in the metadata */
8650 wait_event(mddev->sb_wait,
8651 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
8653 spin_unlock(&mddev->lock);
8655 EXPORT_SYMBOL_GPL(md_allow_write);
8657 #define SYNC_MARKS 10
8658 #define SYNC_MARK_STEP (3*HZ)
8659 #define UPDATE_FREQUENCY (5*60*HZ)
8660 void md_do_sync(struct md_thread *thread)
8662 struct mddev *mddev = thread->mddev;
8663 struct mddev *mddev2;
8664 unsigned int currspeed = 0, window;
8665 sector_t max_sectors,j, io_sectors, recovery_done;
8666 unsigned long mark[SYNC_MARKS];
8667 unsigned long update_time;
8668 sector_t mark_cnt[SYNC_MARKS];
8670 struct list_head *tmp;
8671 sector_t last_check;
8673 struct md_rdev *rdev;
8674 char *desc, *action = NULL;
8675 struct blk_plug plug;
8678 /* just incase thread restarts... */
8679 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
8680 test_bit(MD_RECOVERY_WAIT, &mddev->recovery))
8682 if (mddev->ro) {/* never try to sync a read-only array */
8683 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
8687 if (mddev_is_clustered(mddev)) {
8688 ret = md_cluster_ops->resync_start(mddev);
8692 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
8693 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8694 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
8695 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
8696 && ((unsigned long long)mddev->curr_resync_completed
8697 < (unsigned long long)mddev->resync_max_sectors))
8701 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8702 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
8703 desc = "data-check";
8705 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8706 desc = "requested-resync";
8710 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
8715 mddev->last_sync_action = action ?: desc;
8717 /* we overload curr_resync somewhat here.
8718 * 0 == not engaged in resync at all
8719 * 2 == checking that there is no conflict with another sync
8720 * 1 == like 2, but have yielded to allow conflicting resync to
8722 * other == active in resync - this many blocks
8724 * Before starting a resync we must have set curr_resync to
8725 * 2, and then checked that every "conflicting" array has curr_resync
8726 * less than ours. When we find one that is the same or higher
8727 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8728 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8729 * This will mean we have to start checking from the beginning again.
8734 int mddev2_minor = -1;
8735 mddev->curr_resync = 2;
8738 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8740 for_each_mddev(mddev2, tmp) {
8741 if (mddev2 == mddev)
8743 if (!mddev->parallel_resync
8744 && mddev2->curr_resync
8745 && match_mddev_units(mddev, mddev2)) {
8747 if (mddev < mddev2 && mddev->curr_resync == 2) {
8748 /* arbitrarily yield */
8749 mddev->curr_resync = 1;
8750 wake_up(&resync_wait);
8752 if (mddev > mddev2 && mddev->curr_resync == 1)
8753 /* no need to wait here, we can wait the next
8754 * time 'round when curr_resync == 2
8757 /* We need to wait 'interruptible' so as not to
8758 * contribute to the load average, and not to
8759 * be caught by 'softlockup'
8761 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
8762 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8763 mddev2->curr_resync >= mddev->curr_resync) {
8764 if (mddev2_minor != mddev2->md_minor) {
8765 mddev2_minor = mddev2->md_minor;
8766 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8767 desc, mdname(mddev),
8771 if (signal_pending(current))
8772 flush_signals(current);
8774 finish_wait(&resync_wait, &wq);
8777 finish_wait(&resync_wait, &wq);
8780 } while (mddev->curr_resync < 2);
8783 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8784 /* resync follows the size requested by the personality,
8785 * which defaults to physical size, but can be virtual size
8787 max_sectors = mddev->resync_max_sectors;
8788 atomic64_set(&mddev->resync_mismatches, 0);
8789 /* we don't use the checkpoint if there's a bitmap */
8790 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8791 j = mddev->resync_min;
8792 else if (!mddev->bitmap)
8793 j = mddev->recovery_cp;
8795 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
8796 max_sectors = mddev->resync_max_sectors;
8798 * If the original node aborts reshaping then we continue the
8799 * reshaping, so set j again to avoid restart reshape from the
8802 if (mddev_is_clustered(mddev) &&
8803 mddev->reshape_position != MaxSector)
8804 j = mddev->reshape_position;
8806 /* recovery follows the physical size of devices */
8807 max_sectors = mddev->dev_sectors;
8810 rdev_for_each_rcu(rdev, mddev)
8811 if (rdev->raid_disk >= 0 &&
8812 !test_bit(Journal, &rdev->flags) &&
8813 !test_bit(Faulty, &rdev->flags) &&
8814 !test_bit(In_sync, &rdev->flags) &&
8815 rdev->recovery_offset < j)
8816 j = rdev->recovery_offset;
8819 /* If there is a bitmap, we need to make sure all
8820 * writes that started before we added a spare
8821 * complete before we start doing a recovery.
8822 * Otherwise the write might complete and (via
8823 * bitmap_endwrite) set a bit in the bitmap after the
8824 * recovery has checked that bit and skipped that
8827 if (mddev->bitmap) {
8828 mddev->pers->quiesce(mddev, 1);
8829 mddev->pers->quiesce(mddev, 0);
8833 pr_info("md: %s of RAID array %s\n", desc, mdname(mddev));
8834 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev));
8835 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8836 speed_max(mddev), desc);
8838 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
8841 for (m = 0; m < SYNC_MARKS; m++) {
8843 mark_cnt[m] = io_sectors;
8846 mddev->resync_mark = mark[last_mark];
8847 mddev->resync_mark_cnt = mark_cnt[last_mark];
8850 * Tune reconstruction:
8852 window = 32 * (PAGE_SIZE / 512);
8853 pr_debug("md: using %dk window, over a total of %lluk.\n",
8854 window/2, (unsigned long long)max_sectors/2);
8856 atomic_set(&mddev->recovery_active, 0);
8860 pr_debug("md: resuming %s of %s from checkpoint.\n",
8861 desc, mdname(mddev));
8862 mddev->curr_resync = j;
8864 mddev->curr_resync = 3; /* no longer delayed */
8865 mddev->curr_resync_completed = j;
8866 sysfs_notify_dirent_safe(mddev->sysfs_completed);
8867 md_new_event(mddev);
8868 update_time = jiffies;
8870 blk_start_plug(&plug);
8871 while (j < max_sectors) {
8876 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8877 ((mddev->curr_resync > mddev->curr_resync_completed &&
8878 (mddev->curr_resync - mddev->curr_resync_completed)
8879 > (max_sectors >> 4)) ||
8880 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
8881 (j - mddev->curr_resync_completed)*2
8882 >= mddev->resync_max - mddev->curr_resync_completed ||
8883 mddev->curr_resync_completed > mddev->resync_max
8885 /* time to update curr_resync_completed */
8886 wait_event(mddev->recovery_wait,
8887 atomic_read(&mddev->recovery_active) == 0);
8888 mddev->curr_resync_completed = j;
8889 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8890 j > mddev->recovery_cp)
8891 mddev->recovery_cp = j;
8892 update_time = jiffies;
8893 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8894 sysfs_notify_dirent_safe(mddev->sysfs_completed);
8897 while (j >= mddev->resync_max &&
8898 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8899 /* As this condition is controlled by user-space,
8900 * we can block indefinitely, so use '_interruptible'
8901 * to avoid triggering warnings.
8903 flush_signals(current); /* just in case */
8904 wait_event_interruptible(mddev->recovery_wait,
8905 mddev->resync_max > j
8906 || test_bit(MD_RECOVERY_INTR,
8910 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8913 sectors = mddev->pers->sync_request(mddev, j, &skipped);
8915 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
8919 if (!skipped) { /* actual IO requested */
8920 io_sectors += sectors;
8921 atomic_add(sectors, &mddev->recovery_active);
8924 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8928 if (j > max_sectors)
8929 /* when skipping, extra large numbers can be returned. */
8932 mddev->curr_resync = j;
8933 mddev->curr_mark_cnt = io_sectors;
8934 if (last_check == 0)
8935 /* this is the earliest that rebuild will be
8936 * visible in /proc/mdstat
8938 md_new_event(mddev);
8940 if (last_check + window > io_sectors || j == max_sectors)
8943 last_check = io_sectors;
8945 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8947 int next = (last_mark+1) % SYNC_MARKS;
8949 mddev->resync_mark = mark[next];
8950 mddev->resync_mark_cnt = mark_cnt[next];
8951 mark[next] = jiffies;
8952 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
8956 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8960 * this loop exits only if either when we are slower than
8961 * the 'hard' speed limit, or the system was IO-idle for
8963 * the system might be non-idle CPU-wise, but we only care
8964 * about not overloading the IO subsystem. (things like an
8965 * e2fsck being done on the RAID array should execute fast)
8969 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8970 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
8971 /((jiffies-mddev->resync_mark)/HZ +1) +1;
8973 if (currspeed > speed_min(mddev)) {
8974 if (currspeed > speed_max(mddev)) {
8978 if (!is_mddev_idle(mddev, 0)) {
8980 * Give other IO more of a chance.
8981 * The faster the devices, the less we wait.
8983 wait_event(mddev->recovery_wait,
8984 !atomic_read(&mddev->recovery_active));
8988 pr_info("md: %s: %s %s.\n",mdname(mddev), desc,
8989 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8990 ? "interrupted" : "done");
8992 * this also signals 'finished resyncing' to md_stop
8994 blk_finish_plug(&plug);
8995 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8997 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8998 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8999 mddev->curr_resync > 3) {
9000 mddev->curr_resync_completed = mddev->curr_resync;
9001 sysfs_notify_dirent_safe(mddev->sysfs_completed);
9003 mddev->pers->sync_request(mddev, max_sectors, &skipped);
9005 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
9006 mddev->curr_resync > 3) {
9007 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
9008 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
9009 if (mddev->curr_resync >= mddev->recovery_cp) {
9010 pr_debug("md: checkpointing %s of %s.\n",
9011 desc, mdname(mddev));
9012 if (test_bit(MD_RECOVERY_ERROR,
9014 mddev->recovery_cp =
9015 mddev->curr_resync_completed;
9017 mddev->recovery_cp =
9021 mddev->recovery_cp = MaxSector;
9023 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
9024 mddev->curr_resync = MaxSector;
9025 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
9026 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) {
9028 rdev_for_each_rcu(rdev, mddev)
9029 if (rdev->raid_disk >= 0 &&
9030 mddev->delta_disks >= 0 &&
9031 !test_bit(Journal, &rdev->flags) &&
9032 !test_bit(Faulty, &rdev->flags) &&
9033 !test_bit(In_sync, &rdev->flags) &&
9034 rdev->recovery_offset < mddev->curr_resync)
9035 rdev->recovery_offset = mddev->curr_resync;
9041 /* set CHANGE_PENDING here since maybe another update is needed,
9042 * so other nodes are informed. It should be harmless for normal
9044 set_mask_bits(&mddev->sb_flags, 0,
9045 BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS));
9047 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
9048 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
9049 mddev->delta_disks > 0 &&
9050 mddev->pers->finish_reshape &&
9051 mddev->pers->size &&
9053 mddev_lock_nointr(mddev);
9054 md_set_array_sectors(mddev, mddev->pers->size(mddev, 0, 0));
9055 mddev_unlock(mddev);
9056 if (!mddev_is_clustered(mddev))
9057 set_capacity_and_notify(mddev->gendisk,
9058 mddev->array_sectors);
9061 spin_lock(&mddev->lock);
9062 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
9063 /* We completed so min/max setting can be forgotten if used. */
9064 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
9065 mddev->resync_min = 0;
9066 mddev->resync_max = MaxSector;
9067 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
9068 mddev->resync_min = mddev->curr_resync_completed;
9069 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
9070 mddev->curr_resync = 0;
9071 spin_unlock(&mddev->lock);
9073 wake_up(&resync_wait);
9074 md_wakeup_thread(mddev->thread);
9077 EXPORT_SYMBOL_GPL(md_do_sync);
9079 static int remove_and_add_spares(struct mddev *mddev,
9080 struct md_rdev *this)
9082 struct md_rdev *rdev;
9085 bool remove_some = false;
9087 if (this && test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
9088 /* Mustn't remove devices when resync thread is running */
9091 rdev_for_each(rdev, mddev) {
9092 if ((this == NULL || rdev == this) &&
9093 rdev->raid_disk >= 0 &&
9094 !test_bit(Blocked, &rdev->flags) &&
9095 test_bit(Faulty, &rdev->flags) &&
9096 atomic_read(&rdev->nr_pending)==0) {
9097 /* Faulty non-Blocked devices with nr_pending == 0
9098 * never get nr_pending incremented,
9099 * never get Faulty cleared, and never get Blocked set.
9100 * So we can synchronize_rcu now rather than once per device
9103 set_bit(RemoveSynchronized, &rdev->flags);
9109 rdev_for_each(rdev, mddev) {
9110 if ((this == NULL || rdev == this) &&
9111 rdev->raid_disk >= 0 &&
9112 !test_bit(Blocked, &rdev->flags) &&
9113 ((test_bit(RemoveSynchronized, &rdev->flags) ||
9114 (!test_bit(In_sync, &rdev->flags) &&
9115 !test_bit(Journal, &rdev->flags))) &&
9116 atomic_read(&rdev->nr_pending)==0)) {
9117 if (mddev->pers->hot_remove_disk(
9118 mddev, rdev) == 0) {
9119 sysfs_unlink_rdev(mddev, rdev);
9120 rdev->saved_raid_disk = rdev->raid_disk;
9121 rdev->raid_disk = -1;
9125 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
9126 clear_bit(RemoveSynchronized, &rdev->flags);
9129 if (removed && mddev->kobj.sd)
9130 sysfs_notify_dirent_safe(mddev->sysfs_degraded);
9132 if (this && removed)
9135 rdev_for_each(rdev, mddev) {
9136 if (this && this != rdev)
9138 if (test_bit(Candidate, &rdev->flags))
9140 if (rdev->raid_disk >= 0 &&
9141 !test_bit(In_sync, &rdev->flags) &&
9142 !test_bit(Journal, &rdev->flags) &&
9143 !test_bit(Faulty, &rdev->flags))
9145 if (rdev->raid_disk >= 0)
9147 if (test_bit(Faulty, &rdev->flags))
9149 if (!test_bit(Journal, &rdev->flags)) {
9151 ! (rdev->saved_raid_disk >= 0 &&
9152 !test_bit(Bitmap_sync, &rdev->flags)))
9155 rdev->recovery_offset = 0;
9157 if (mddev->pers->hot_add_disk(mddev, rdev) == 0) {
9158 /* failure here is OK */
9159 sysfs_link_rdev(mddev, rdev);
9160 if (!test_bit(Journal, &rdev->flags))
9162 md_new_event(mddev);
9163 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
9168 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
9172 static void md_start_sync(struct work_struct *ws)
9174 struct mddev *mddev = container_of(ws, struct mddev, del_work);
9176 mddev->sync_thread = md_register_thread(md_do_sync,
9179 if (!mddev->sync_thread) {
9180 pr_warn("%s: could not start resync thread...\n",
9182 /* leave the spares where they are, it shouldn't hurt */
9183 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9184 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
9185 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
9186 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
9187 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
9188 wake_up(&resync_wait);
9189 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
9191 if (mddev->sysfs_action)
9192 sysfs_notify_dirent_safe(mddev->sysfs_action);
9194 md_wakeup_thread(mddev->sync_thread);
9195 sysfs_notify_dirent_safe(mddev->sysfs_action);
9196 md_new_event(mddev);
9200 * This routine is regularly called by all per-raid-array threads to
9201 * deal with generic issues like resync and super-block update.
9202 * Raid personalities that don't have a thread (linear/raid0) do not
9203 * need this as they never do any recovery or update the superblock.
9205 * It does not do any resync itself, but rather "forks" off other threads
9206 * to do that as needed.
9207 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
9208 * "->recovery" and create a thread at ->sync_thread.
9209 * When the thread finishes it sets MD_RECOVERY_DONE
9210 * and wakeups up this thread which will reap the thread and finish up.
9211 * This thread also removes any faulty devices (with nr_pending == 0).
9213 * The overall approach is:
9214 * 1/ if the superblock needs updating, update it.
9215 * 2/ If a recovery thread is running, don't do anything else.
9216 * 3/ If recovery has finished, clean up, possibly marking spares active.
9217 * 4/ If there are any faulty devices, remove them.
9218 * 5/ If array is degraded, try to add spares devices
9219 * 6/ If array has spares or is not in-sync, start a resync thread.
9221 void md_check_recovery(struct mddev *mddev)
9223 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags) && mddev->sb_flags) {
9224 /* Write superblock - thread that called mddev_suspend()
9225 * holds reconfig_mutex for us.
9227 set_bit(MD_UPDATING_SB, &mddev->flags);
9228 smp_mb__after_atomic();
9229 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags))
9230 md_update_sb(mddev, 0);
9231 clear_bit_unlock(MD_UPDATING_SB, &mddev->flags);
9232 wake_up(&mddev->sb_wait);
9235 if (mddev->suspended)
9239 md_bitmap_daemon_work(mddev);
9241 if (signal_pending(current)) {
9242 if (mddev->pers->sync_request && !mddev->external) {
9243 pr_debug("md: %s in immediate safe mode\n",
9245 mddev->safemode = 2;
9247 flush_signals(current);
9250 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
9253 (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
9254 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
9255 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
9256 (mddev->external == 0 && mddev->safemode == 1) ||
9257 (mddev->safemode == 2
9258 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
9262 if (mddev_trylock(mddev)) {
9264 bool try_set_sync = mddev->safemode != 0;
9266 if (!mddev->external && mddev->safemode == 1)
9267 mddev->safemode = 0;
9270 struct md_rdev *rdev;
9271 if (!mddev->external && mddev->in_sync)
9272 /* 'Blocked' flag not needed as failed devices
9273 * will be recorded if array switched to read/write.
9274 * Leaving it set will prevent the device
9275 * from being removed.
9277 rdev_for_each(rdev, mddev)
9278 clear_bit(Blocked, &rdev->flags);
9279 /* On a read-only array we can:
9280 * - remove failed devices
9281 * - add already-in_sync devices if the array itself
9283 * As we only add devices that are already in-sync,
9284 * we can activate the spares immediately.
9286 remove_and_add_spares(mddev, NULL);
9287 /* There is no thread, but we need to call
9288 * ->spare_active and clear saved_raid_disk
9290 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
9291 md_reap_sync_thread(mddev);
9292 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
9293 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9294 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
9298 if (mddev_is_clustered(mddev)) {
9299 struct md_rdev *rdev, *tmp;
9300 /* kick the device if another node issued a
9303 rdev_for_each_safe(rdev, tmp, mddev) {
9304 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
9305 rdev->raid_disk < 0)
9306 md_kick_rdev_from_array(rdev);
9310 if (try_set_sync && !mddev->external && !mddev->in_sync) {
9311 spin_lock(&mddev->lock);
9313 spin_unlock(&mddev->lock);
9316 if (mddev->sb_flags)
9317 md_update_sb(mddev, 0);
9319 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
9320 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
9321 /* resync/recovery still happening */
9322 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9325 if (mddev->sync_thread) {
9326 md_reap_sync_thread(mddev);
9329 /* Set RUNNING before clearing NEEDED to avoid
9330 * any transients in the value of "sync_action".
9332 mddev->curr_resync_completed = 0;
9333 spin_lock(&mddev->lock);
9334 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
9335 spin_unlock(&mddev->lock);
9336 /* Clear some bits that don't mean anything, but
9339 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
9340 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
9342 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
9343 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
9345 /* no recovery is running.
9346 * remove any failed drives, then
9347 * add spares if possible.
9348 * Spares are also removed and re-added, to allow
9349 * the personality to fail the re-add.
9352 if (mddev->reshape_position != MaxSector) {
9353 if (mddev->pers->check_reshape == NULL ||
9354 mddev->pers->check_reshape(mddev) != 0)
9355 /* Cannot proceed */
9357 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
9358 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
9359 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
9360 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9361 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
9362 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
9363 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
9364 } else if (mddev->recovery_cp < MaxSector) {
9365 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9366 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
9367 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
9368 /* nothing to be done ... */
9371 if (mddev->pers->sync_request) {
9373 /* We are adding a device or devices to an array
9374 * which has the bitmap stored on all devices.
9375 * So make sure all bitmap pages get written
9377 md_bitmap_write_all(mddev->bitmap);
9379 INIT_WORK(&mddev->del_work, md_start_sync);
9380 queue_work(md_misc_wq, &mddev->del_work);
9384 if (!mddev->sync_thread) {
9385 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
9386 wake_up(&resync_wait);
9387 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
9389 if (mddev->sysfs_action)
9390 sysfs_notify_dirent_safe(mddev->sysfs_action);
9393 wake_up(&mddev->sb_wait);
9394 mddev_unlock(mddev);
9397 EXPORT_SYMBOL(md_check_recovery);
9399 void md_reap_sync_thread(struct mddev *mddev)
9401 struct md_rdev *rdev;
9402 sector_t old_dev_sectors = mddev->dev_sectors;
9403 bool is_reshaped = false;
9405 /* resync has finished, collect result */
9406 md_unregister_thread(&mddev->sync_thread);
9407 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
9408 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
9409 mddev->degraded != mddev->raid_disks) {
9411 /* activate any spares */
9412 if (mddev->pers->spare_active(mddev)) {
9413 sysfs_notify_dirent_safe(mddev->sysfs_degraded);
9414 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
9417 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
9418 mddev->pers->finish_reshape) {
9419 mddev->pers->finish_reshape(mddev);
9420 if (mddev_is_clustered(mddev))
9424 /* If array is no-longer degraded, then any saved_raid_disk
9425 * information must be scrapped.
9427 if (!mddev->degraded)
9428 rdev_for_each(rdev, mddev)
9429 rdev->saved_raid_disk = -1;
9431 md_update_sb(mddev, 1);
9432 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
9433 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
9435 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
9436 md_cluster_ops->resync_finish(mddev);
9437 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
9438 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
9439 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9440 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
9441 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
9442 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
9444 * We call md_cluster_ops->update_size here because sync_size could
9445 * be changed by md_update_sb, and MD_RECOVERY_RESHAPE is cleared,
9446 * so it is time to update size across cluster.
9448 if (mddev_is_clustered(mddev) && is_reshaped
9449 && !test_bit(MD_CLOSING, &mddev->flags))
9450 md_cluster_ops->update_size(mddev, old_dev_sectors);
9451 wake_up(&resync_wait);
9452 /* flag recovery needed just to double check */
9453 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9454 sysfs_notify_dirent_safe(mddev->sysfs_action);
9455 md_new_event(mddev);
9456 if (mddev->event_work.func)
9457 queue_work(md_misc_wq, &mddev->event_work);
9459 EXPORT_SYMBOL(md_reap_sync_thread);
9461 void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
9463 sysfs_notify_dirent_safe(rdev->sysfs_state);
9464 wait_event_timeout(rdev->blocked_wait,
9465 !test_bit(Blocked, &rdev->flags) &&
9466 !test_bit(BlockedBadBlocks, &rdev->flags),
9467 msecs_to_jiffies(5000));
9468 rdev_dec_pending(rdev, mddev);
9470 EXPORT_SYMBOL(md_wait_for_blocked_rdev);
9472 void md_finish_reshape(struct mddev *mddev)
9474 /* called be personality module when reshape completes. */
9475 struct md_rdev *rdev;
9477 rdev_for_each(rdev, mddev) {
9478 if (rdev->data_offset > rdev->new_data_offset)
9479 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
9481 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
9482 rdev->data_offset = rdev->new_data_offset;
9485 EXPORT_SYMBOL(md_finish_reshape);
9487 /* Bad block management */
9489 /* Returns 1 on success, 0 on failure */
9490 int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
9493 struct mddev *mddev = rdev->mddev;
9496 s += rdev->new_data_offset;
9498 s += rdev->data_offset;
9499 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
9501 /* Make sure they get written out promptly */
9502 if (test_bit(ExternalBbl, &rdev->flags))
9503 sysfs_notify_dirent_safe(rdev->sysfs_unack_badblocks);
9504 sysfs_notify_dirent_safe(rdev->sysfs_state);
9505 set_mask_bits(&mddev->sb_flags, 0,
9506 BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING));
9507 md_wakeup_thread(rdev->mddev->thread);
9512 EXPORT_SYMBOL_GPL(rdev_set_badblocks);
9514 int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
9519 s += rdev->new_data_offset;
9521 s += rdev->data_offset;
9522 rv = badblocks_clear(&rdev->badblocks, s, sectors);
9523 if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags))
9524 sysfs_notify_dirent_safe(rdev->sysfs_badblocks);
9527 EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
9529 static int md_notify_reboot(struct notifier_block *this,
9530 unsigned long code, void *x)
9532 struct list_head *tmp;
9533 struct mddev *mddev;
9536 for_each_mddev(mddev, tmp) {
9537 if (mddev_trylock(mddev)) {
9539 __md_stop_writes(mddev);
9540 if (mddev->persistent)
9541 mddev->safemode = 2;
9542 mddev_unlock(mddev);
9547 * certain more exotic SCSI devices are known to be
9548 * volatile wrt too early system reboots. While the
9549 * right place to handle this issue is the given
9550 * driver, we do want to have a safe RAID driver ...
9558 static struct notifier_block md_notifier = {
9559 .notifier_call = md_notify_reboot,
9561 .priority = INT_MAX, /* before any real devices */
9564 static void md_geninit(void)
9566 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
9568 proc_create("mdstat", S_IRUGO, NULL, &mdstat_proc_ops);
9571 static int __init md_init(void)
9575 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
9579 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
9583 md_rdev_misc_wq = alloc_workqueue("md_rdev_misc", 0, 0);
9584 if (!md_rdev_misc_wq)
9585 goto err_rdev_misc_wq;
9587 ret = __register_blkdev(MD_MAJOR, "md", md_probe);
9591 ret = __register_blkdev(0, "mdp", md_probe);
9596 register_reboot_notifier(&md_notifier);
9597 raid_table_header = register_sysctl_table(raid_root_table);
9603 unregister_blkdev(MD_MAJOR, "md");
9605 destroy_workqueue(md_rdev_misc_wq);
9607 destroy_workqueue(md_misc_wq);
9609 destroy_workqueue(md_wq);
9614 static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
9616 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
9617 struct md_rdev *rdev2, *tmp;
9619 char b[BDEVNAME_SIZE];
9622 * If size is changed in another node then we need to
9623 * do resize as well.
9625 if (mddev->dev_sectors != le64_to_cpu(sb->size)) {
9626 ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size));
9628 pr_info("md-cluster: resize failed\n");
9630 md_bitmap_update_sb(mddev->bitmap);
9633 /* Check for change of roles in the active devices */
9634 rdev_for_each_safe(rdev2, tmp, mddev) {
9635 if (test_bit(Faulty, &rdev2->flags))
9638 /* Check if the roles changed */
9639 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
9641 if (test_bit(Candidate, &rdev2->flags)) {
9642 if (role == 0xfffe) {
9643 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
9644 md_kick_rdev_from_array(rdev2);
9648 clear_bit(Candidate, &rdev2->flags);
9651 if (role != rdev2->raid_disk) {
9653 * got activated except reshape is happening.
9655 if (rdev2->raid_disk == -1 && role != 0xffff &&
9656 !(le32_to_cpu(sb->feature_map) &
9657 MD_FEATURE_RESHAPE_ACTIVE)) {
9658 rdev2->saved_raid_disk = role;
9659 ret = remove_and_add_spares(mddev, rdev2);
9660 pr_info("Activated spare: %s\n",
9661 bdevname(rdev2->bdev,b));
9662 /* wakeup mddev->thread here, so array could
9663 * perform resync with the new activated disk */
9664 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9665 md_wakeup_thread(mddev->thread);
9668 * We just want to do the minimum to mark the disk
9669 * as faulty. The recovery is performed by the
9670 * one who initiated the error.
9672 if ((role == 0xfffe) || (role == 0xfffd)) {
9673 md_error(mddev, rdev2);
9674 clear_bit(Blocked, &rdev2->flags);
9679 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) {
9680 ret = update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
9682 pr_warn("md: updating array disks failed. %d\n", ret);
9686 * Since mddev->delta_disks has already updated in update_raid_disks,
9687 * so it is time to check reshape.
9689 if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
9690 (le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
9692 * reshape is happening in the remote node, we need to
9693 * update reshape_position and call start_reshape.
9695 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
9696 if (mddev->pers->update_reshape_pos)
9697 mddev->pers->update_reshape_pos(mddev);
9698 if (mddev->pers->start_reshape)
9699 mddev->pers->start_reshape(mddev);
9700 } else if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
9701 mddev->reshape_position != MaxSector &&
9702 !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
9703 /* reshape is just done in another node. */
9704 mddev->reshape_position = MaxSector;
9705 if (mddev->pers->update_reshape_pos)
9706 mddev->pers->update_reshape_pos(mddev);
9709 /* Finally set the event to be up to date */
9710 mddev->events = le64_to_cpu(sb->events);
9713 static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
9716 struct page *swapout = rdev->sb_page;
9717 struct mdp_superblock_1 *sb;
9719 /* Store the sb page of the rdev in the swapout temporary
9720 * variable in case we err in the future
9722 rdev->sb_page = NULL;
9723 err = alloc_disk_sb(rdev);
9725 ClearPageUptodate(rdev->sb_page);
9726 rdev->sb_loaded = 0;
9727 err = super_types[mddev->major_version].
9728 load_super(rdev, NULL, mddev->minor_version);
9731 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9732 __func__, __LINE__, rdev->desc_nr, err);
9734 put_page(rdev->sb_page);
9735 rdev->sb_page = swapout;
9736 rdev->sb_loaded = 1;
9740 sb = page_address(rdev->sb_page);
9741 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9745 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
9746 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
9748 /* The other node finished recovery, call spare_active to set
9749 * device In_sync and mddev->degraded
9751 if (rdev->recovery_offset == MaxSector &&
9752 !test_bit(In_sync, &rdev->flags) &&
9753 mddev->pers->spare_active(mddev))
9754 sysfs_notify_dirent_safe(mddev->sysfs_degraded);
9760 void md_reload_sb(struct mddev *mddev, int nr)
9762 struct md_rdev *rdev;
9766 rdev_for_each_rcu(rdev, mddev) {
9767 if (rdev->desc_nr == nr)
9771 if (!rdev || rdev->desc_nr != nr) {
9772 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
9776 err = read_rdev(mddev, rdev);
9780 check_sb_changes(mddev, rdev);
9782 /* Read all rdev's to update recovery_offset */
9783 rdev_for_each_rcu(rdev, mddev) {
9784 if (!test_bit(Faulty, &rdev->flags))
9785 read_rdev(mddev, rdev);
9788 EXPORT_SYMBOL(md_reload_sb);
9793 * Searches all registered partitions for autorun RAID arrays
9797 static DEFINE_MUTEX(detected_devices_mutex);
9798 static LIST_HEAD(all_detected_devices);
9799 struct detected_devices_node {
9800 struct list_head list;
9804 void md_autodetect_dev(dev_t dev)
9806 struct detected_devices_node *node_detected_dev;
9808 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
9809 if (node_detected_dev) {
9810 node_detected_dev->dev = dev;
9811 mutex_lock(&detected_devices_mutex);
9812 list_add_tail(&node_detected_dev->list, &all_detected_devices);
9813 mutex_unlock(&detected_devices_mutex);
9817 void md_autostart_arrays(int part)
9819 struct md_rdev *rdev;
9820 struct detected_devices_node *node_detected_dev;
9822 int i_scanned, i_passed;
9827 pr_info("md: Autodetecting RAID arrays.\n");
9829 mutex_lock(&detected_devices_mutex);
9830 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
9832 node_detected_dev = list_entry(all_detected_devices.next,
9833 struct detected_devices_node, list);
9834 list_del(&node_detected_dev->list);
9835 dev = node_detected_dev->dev;
9836 kfree(node_detected_dev);
9837 mutex_unlock(&detected_devices_mutex);
9838 rdev = md_import_device(dev,0, 90);
9839 mutex_lock(&detected_devices_mutex);
9843 if (test_bit(Faulty, &rdev->flags))
9846 set_bit(AutoDetected, &rdev->flags);
9847 list_add(&rdev->same_set, &pending_raid_disks);
9850 mutex_unlock(&detected_devices_mutex);
9852 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed);
9854 autorun_devices(part);
9857 #endif /* !MODULE */
9859 static __exit void md_exit(void)
9861 struct mddev *mddev;
9862 struct list_head *tmp;
9865 unregister_blkdev(MD_MAJOR,"md");
9866 unregister_blkdev(mdp_major, "mdp");
9867 unregister_reboot_notifier(&md_notifier);
9868 unregister_sysctl_table(raid_table_header);
9870 /* We cannot unload the modules while some process is
9871 * waiting for us in select() or poll() - wake them up
9874 while (waitqueue_active(&md_event_waiters)) {
9875 /* not safe to leave yet */
9876 wake_up(&md_event_waiters);
9880 remove_proc_entry("mdstat", NULL);
9882 for_each_mddev(mddev, tmp) {
9883 export_array(mddev);
9885 mddev->hold_active = 0;
9887 * for_each_mddev() will call mddev_put() at the end of each
9888 * iteration. As the mddev is now fully clear, this will
9889 * schedule the mddev for destruction by a workqueue, and the
9890 * destroy_workqueue() below will wait for that to complete.
9893 destroy_workqueue(md_rdev_misc_wq);
9894 destroy_workqueue(md_misc_wq);
9895 destroy_workqueue(md_wq);
9898 subsys_initcall(md_init);
9899 module_exit(md_exit)
9901 static int get_ro(char *buffer, const struct kernel_param *kp)
9903 return sprintf(buffer, "%d\n", start_readonly);
9905 static int set_ro(const char *val, const struct kernel_param *kp)
9907 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
9910 module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9911 module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
9912 module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
9913 module_param(create_on_open, bool, S_IRUSR|S_IWUSR);
9915 MODULE_LICENSE("GPL");
9916 MODULE_DESCRIPTION("MD RAID framework");
9918 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);