1 // SPDX-License-Identifier: GPL-2.0-or-later
3 md.c : Multiple Devices driver for Linux
4 Copyright (C) 1998, 1999, 2000 Ingo Molnar
6 completely rewritten, based on the MD driver code from Marc Zyngier
10 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
11 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
12 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
13 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
14 - kmod support by: Cyrus Durgin
15 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
16 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
18 - lots of fixes and improvements to the RAID1/RAID5 and generic
19 RAID code (such as request based resynchronization):
21 Neil Brown <neilb@cse.unsw.edu.au>.
23 - persistent bitmap code
24 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
27 Errors, Warnings, etc.
29 pr_crit() for error conditions that risk data loss
30 pr_err() for error conditions that are unexpected, like an IO error
31 or internal inconsistency
32 pr_warn() for error conditions that could have been predicated, like
33 adding a device to an array when it has incompatible metadata
34 pr_info() for every interesting, very rare events, like an array starting
35 or stopping, or resync starting or stopping
36 pr_debug() for everything else.
40 #include <linux/sched/mm.h>
41 #include <linux/sched/signal.h>
42 #include <linux/kthread.h>
43 #include <linux/blkdev.h>
44 #include <linux/blk-integrity.h>
45 #include <linux/badblocks.h>
46 #include <linux/sysctl.h>
47 #include <linux/seq_file.h>
49 #include <linux/poll.h>
50 #include <linux/ctype.h>
51 #include <linux/string.h>
52 #include <linux/hdreg.h>
53 #include <linux/proc_fs.h>
54 #include <linux/random.h>
55 #include <linux/major.h>
56 #include <linux/module.h>
57 #include <linux/reboot.h>
58 #include <linux/file.h>
59 #include <linux/compat.h>
60 #include <linux/delay.h>
61 #include <linux/raid/md_p.h>
62 #include <linux/raid/md_u.h>
63 #include <linux/raid/detect.h>
64 #include <linux/slab.h>
65 #include <linux/percpu-refcount.h>
66 #include <linux/part_stat.h>
68 #include <trace/events/block.h>
70 #include "md-bitmap.h"
71 #include "md-cluster.h"
73 /* pers_list is a list of registered personalities protected
75 * pers_lock does extra service to protect accesses to
76 * mddev->thread when the mutex cannot be held.
78 static LIST_HEAD(pers_list);
79 static DEFINE_SPINLOCK(pers_lock);
81 static struct kobj_type md_ktype;
83 struct md_cluster_operations *md_cluster_ops;
84 EXPORT_SYMBOL(md_cluster_ops);
85 static struct module *md_cluster_mod;
87 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
88 static struct workqueue_struct *md_wq;
89 static struct workqueue_struct *md_misc_wq;
90 static struct workqueue_struct *md_rdev_misc_wq;
92 static int remove_and_add_spares(struct mddev *mddev,
93 struct md_rdev *this);
94 static void mddev_detach(struct mddev *mddev);
97 * Default number of read corrections we'll attempt on an rdev
98 * before ejecting it from the array. We divide the read error
99 * count by 2 for every hour elapsed between read errors.
101 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
102 /* Default safemode delay: 200 msec */
103 #define DEFAULT_SAFEMODE_DELAY ((200 * HZ)/1000 +1)
105 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
106 * is 1000 KB/sec, so the extra system load does not show up that much.
107 * Increase it if you want to have more _guaranteed_ speed. Note that
108 * the RAID driver will use the maximum available bandwidth if the IO
109 * subsystem is idle. There is also an 'absolute maximum' reconstruction
110 * speed limit - in case reconstruction slows down your system despite
113 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
114 * or /sys/block/mdX/md/sync_speed_{min,max}
117 static int sysctl_speed_limit_min = 1000;
118 static int sysctl_speed_limit_max = 200000;
119 static inline int speed_min(struct mddev *mddev)
121 return mddev->sync_speed_min ?
122 mddev->sync_speed_min : sysctl_speed_limit_min;
125 static inline int speed_max(struct mddev *mddev)
127 return mddev->sync_speed_max ?
128 mddev->sync_speed_max : sysctl_speed_limit_max;
131 static void rdev_uninit_serial(struct md_rdev *rdev)
133 if (!test_and_clear_bit(CollisionCheck, &rdev->flags))
136 kvfree(rdev->serial);
140 static void rdevs_uninit_serial(struct mddev *mddev)
142 struct md_rdev *rdev;
144 rdev_for_each(rdev, mddev)
145 rdev_uninit_serial(rdev);
148 static int rdev_init_serial(struct md_rdev *rdev)
150 /* serial_nums equals with BARRIER_BUCKETS_NR */
151 int i, serial_nums = 1 << ((PAGE_SHIFT - ilog2(sizeof(atomic_t))));
152 struct serial_in_rdev *serial = NULL;
154 if (test_bit(CollisionCheck, &rdev->flags))
157 serial = kvmalloc(sizeof(struct serial_in_rdev) * serial_nums,
162 for (i = 0; i < serial_nums; i++) {
163 struct serial_in_rdev *serial_tmp = &serial[i];
165 spin_lock_init(&serial_tmp->serial_lock);
166 serial_tmp->serial_rb = RB_ROOT_CACHED;
167 init_waitqueue_head(&serial_tmp->serial_io_wait);
170 rdev->serial = serial;
171 set_bit(CollisionCheck, &rdev->flags);
176 static int rdevs_init_serial(struct mddev *mddev)
178 struct md_rdev *rdev;
181 rdev_for_each(rdev, mddev) {
182 ret = rdev_init_serial(rdev);
187 /* Free all resources if pool is not existed */
188 if (ret && !mddev->serial_info_pool)
189 rdevs_uninit_serial(mddev);
195 * rdev needs to enable serial stuffs if it meets the conditions:
196 * 1. it is multi-queue device flaged with writemostly.
197 * 2. the write-behind mode is enabled.
199 static int rdev_need_serial(struct md_rdev *rdev)
201 return (rdev && rdev->mddev->bitmap_info.max_write_behind > 0 &&
202 rdev->bdev->bd_disk->queue->nr_hw_queues != 1 &&
203 test_bit(WriteMostly, &rdev->flags));
207 * Init resource for rdev(s), then create serial_info_pool if:
208 * 1. rdev is the first device which return true from rdev_enable_serial.
209 * 2. rdev is NULL, means we want to enable serialization for all rdevs.
211 void mddev_create_serial_pool(struct mddev *mddev, struct md_rdev *rdev,
216 if (rdev && !rdev_need_serial(rdev) &&
217 !test_bit(CollisionCheck, &rdev->flags))
221 mddev_suspend(mddev);
224 ret = rdevs_init_serial(mddev);
226 ret = rdev_init_serial(rdev);
230 if (mddev->serial_info_pool == NULL) {
232 * already in memalloc noio context by
235 mddev->serial_info_pool =
236 mempool_create_kmalloc_pool(NR_SERIAL_INFOS,
237 sizeof(struct serial_info));
238 if (!mddev->serial_info_pool) {
239 rdevs_uninit_serial(mddev);
240 pr_err("can't alloc memory pool for serialization\n");
250 * Free resource from rdev(s), and destroy serial_info_pool under conditions:
251 * 1. rdev is the last device flaged with CollisionCheck.
252 * 2. when bitmap is destroyed while policy is not enabled.
253 * 3. for disable policy, the pool is destroyed only when no rdev needs it.
255 void mddev_destroy_serial_pool(struct mddev *mddev, struct md_rdev *rdev,
258 if (rdev && !test_bit(CollisionCheck, &rdev->flags))
261 if (mddev->serial_info_pool) {
262 struct md_rdev *temp;
263 int num = 0; /* used to track if other rdevs need the pool */
266 mddev_suspend(mddev);
267 rdev_for_each(temp, mddev) {
269 if (!mddev->serialize_policy ||
270 !rdev_need_serial(temp))
271 rdev_uninit_serial(temp);
274 } else if (temp != rdev &&
275 test_bit(CollisionCheck, &temp->flags))
280 rdev_uninit_serial(rdev);
283 pr_info("The mempool could be used by other devices\n");
285 mempool_destroy(mddev->serial_info_pool);
286 mddev->serial_info_pool = NULL;
293 static struct ctl_table_header *raid_table_header;
295 static struct ctl_table raid_table[] = {
297 .procname = "speed_limit_min",
298 .data = &sysctl_speed_limit_min,
299 .maxlen = sizeof(int),
300 .mode = S_IRUGO|S_IWUSR,
301 .proc_handler = proc_dointvec,
304 .procname = "speed_limit_max",
305 .data = &sysctl_speed_limit_max,
306 .maxlen = sizeof(int),
307 .mode = S_IRUGO|S_IWUSR,
308 .proc_handler = proc_dointvec,
313 static struct ctl_table raid_dir_table[] = {
317 .mode = S_IRUGO|S_IXUGO,
323 static struct ctl_table raid_root_table[] = {
328 .child = raid_dir_table,
333 static int start_readonly;
336 * The original mechanism for creating an md device is to create
337 * a device node in /dev and to open it. This causes races with device-close.
338 * The preferred method is to write to the "new_array" module parameter.
339 * This can avoid races.
340 * Setting create_on_open to false disables the original mechanism
341 * so all the races disappear.
343 static bool create_on_open = true;
346 * We have a system wide 'event count' that is incremented
347 * on any 'interesting' event, and readers of /proc/mdstat
348 * can use 'poll' or 'select' to find out when the event
352 * start array, stop array, error, add device, remove device,
353 * start build, activate spare
355 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
356 static atomic_t md_event_count;
357 void md_new_event(struct mddev *mddev)
359 atomic_inc(&md_event_count);
360 wake_up(&md_event_waiters);
362 EXPORT_SYMBOL_GPL(md_new_event);
365 * Enables to iterate over all existing md arrays
366 * all_mddevs_lock protects this list.
368 static LIST_HEAD(all_mddevs);
369 static DEFINE_SPINLOCK(all_mddevs_lock);
372 * iterates through all used mddevs in the system.
373 * We take care to grab the all_mddevs_lock whenever navigating
374 * the list, and to always hold a refcount when unlocked.
375 * Any code which breaks out of this loop while own
376 * a reference to the current mddev and must mddev_put it.
378 #define for_each_mddev(_mddev,_tmp) \
380 for (({ spin_lock(&all_mddevs_lock); \
381 _tmp = all_mddevs.next; \
383 ({ if (_tmp != &all_mddevs) \
384 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
385 spin_unlock(&all_mddevs_lock); \
386 if (_mddev) mddev_put(_mddev); \
387 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
388 _tmp != &all_mddevs;}); \
389 ({ spin_lock(&all_mddevs_lock); \
390 _tmp = _tmp->next;}) \
393 /* Rather than calling directly into the personality make_request function,
394 * IO requests come here first so that we can check if the device is
395 * being suspended pending a reconfiguration.
396 * We hold a refcount over the call to ->make_request. By the time that
397 * call has finished, the bio has been linked into some internal structure
398 * and so is visible to ->quiesce(), so we don't need the refcount any more.
400 static bool is_suspended(struct mddev *mddev, struct bio *bio)
402 if (mddev->suspended)
404 if (bio_data_dir(bio) != WRITE)
406 if (mddev->suspend_lo >= mddev->suspend_hi)
408 if (bio->bi_iter.bi_sector >= mddev->suspend_hi)
410 if (bio_end_sector(bio) < mddev->suspend_lo)
415 void md_handle_request(struct mddev *mddev, struct bio *bio)
419 if (is_suspended(mddev, bio)) {
422 prepare_to_wait(&mddev->sb_wait, &__wait,
423 TASK_UNINTERRUPTIBLE);
424 if (!is_suspended(mddev, bio))
430 finish_wait(&mddev->sb_wait, &__wait);
432 atomic_inc(&mddev->active_io);
435 if (!mddev->pers->make_request(mddev, bio)) {
436 atomic_dec(&mddev->active_io);
437 wake_up(&mddev->sb_wait);
438 goto check_suspended;
441 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
442 wake_up(&mddev->sb_wait);
444 EXPORT_SYMBOL(md_handle_request);
446 static void md_submit_bio(struct bio *bio)
448 const int rw = bio_data_dir(bio);
449 struct mddev *mddev = bio->bi_bdev->bd_disk->private_data;
451 if (mddev == NULL || mddev->pers == NULL) {
456 if (unlikely(test_bit(MD_BROKEN, &mddev->flags)) && (rw == WRITE)) {
461 blk_queue_split(&bio);
463 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
464 if (bio_sectors(bio) != 0)
465 bio->bi_status = BLK_STS_IOERR;
470 /* bio could be mergeable after passing to underlayer */
471 bio->bi_opf &= ~REQ_NOMERGE;
473 md_handle_request(mddev, bio);
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;
5705 error = kobject_add(&mddev->kobj, &disk_to_dev(disk)->kobj, "%s", "md");
5707 /* This isn't possible, but as kobject_init_and_add is marked
5708 * __must_check, we must do something with the result
5710 pr_debug("md: cannot register %s/md - name in use\n",
5714 if (mddev->kobj.sd &&
5715 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
5716 pr_debug("pointless warning\n");
5718 mutex_unlock(&disks_mutex);
5719 if (!error && mddev->kobj.sd) {
5720 kobject_uevent(&mddev->kobj, KOBJ_ADD);
5721 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
5722 mddev->sysfs_level = sysfs_get_dirent_safe(mddev->kobj.sd, "level");
5728 static void md_probe(dev_t dev)
5730 if (MAJOR(dev) == MD_MAJOR && MINOR(dev) >= 512)
5733 md_alloc(dev, NULL);
5736 static int add_named_array(const char *val, const struct kernel_param *kp)
5739 * val must be "md_*" or "mdNNN".
5740 * For "md_*" we allocate an array with a large free minor number, and
5741 * set the name to val. val must not already be an active name.
5742 * For "mdNNN" we allocate an array with the minor number NNN
5743 * which must not already be in use.
5745 int len = strlen(val);
5746 char buf[DISK_NAME_LEN];
5747 unsigned long devnum;
5749 while (len && val[len-1] == '\n')
5751 if (len >= DISK_NAME_LEN)
5753 strlcpy(buf, val, len+1);
5754 if (strncmp(buf, "md_", 3) == 0)
5755 return md_alloc(0, buf);
5756 if (strncmp(buf, "md", 2) == 0 &&
5758 kstrtoul(buf+2, 10, &devnum) == 0 &&
5759 devnum <= MINORMASK)
5760 return md_alloc(MKDEV(MD_MAJOR, devnum), NULL);
5765 static void md_safemode_timeout(struct timer_list *t)
5767 struct mddev *mddev = from_timer(mddev, t, safemode_timer);
5769 mddev->safemode = 1;
5770 if (mddev->external)
5771 sysfs_notify_dirent_safe(mddev->sysfs_state);
5773 md_wakeup_thread(mddev->thread);
5776 static int start_dirty_degraded;
5778 int md_run(struct mddev *mddev)
5781 struct md_rdev *rdev;
5782 struct md_personality *pers;
5784 if (list_empty(&mddev->disks))
5785 /* cannot run an array with no devices.. */
5790 /* Cannot run until previous stop completes properly */
5791 if (mddev->sysfs_active)
5795 * Analyze all RAID superblock(s)
5797 if (!mddev->raid_disks) {
5798 if (!mddev->persistent)
5800 err = analyze_sbs(mddev);
5805 if (mddev->level != LEVEL_NONE)
5806 request_module("md-level-%d", mddev->level);
5807 else if (mddev->clevel[0])
5808 request_module("md-%s", mddev->clevel);
5811 * Drop all container device buffers, from now on
5812 * the only valid external interface is through the md
5815 mddev->has_superblocks = false;
5816 rdev_for_each(rdev, mddev) {
5817 if (test_bit(Faulty, &rdev->flags))
5819 sync_blockdev(rdev->bdev);
5820 invalidate_bdev(rdev->bdev);
5821 if (mddev->ro != 1 && rdev_read_only(rdev)) {
5824 set_disk_ro(mddev->gendisk, 1);
5828 mddev->has_superblocks = true;
5830 /* perform some consistency tests on the device.
5831 * We don't want the data to overlap the metadata,
5832 * Internal Bitmap issues have been handled elsewhere.
5834 if (rdev->meta_bdev) {
5835 /* Nothing to check */;
5836 } else if (rdev->data_offset < rdev->sb_start) {
5837 if (mddev->dev_sectors &&
5838 rdev->data_offset + mddev->dev_sectors
5840 pr_warn("md: %s: data overlaps metadata\n",
5845 if (rdev->sb_start + rdev->sb_size/512
5846 > rdev->data_offset) {
5847 pr_warn("md: %s: metadata overlaps data\n",
5852 sysfs_notify_dirent_safe(rdev->sysfs_state);
5855 if (!bioset_initialized(&mddev->bio_set)) {
5856 err = bioset_init(&mddev->bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
5860 if (!bioset_initialized(&mddev->sync_set)) {
5861 err = bioset_init(&mddev->sync_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
5865 if (mddev->level != 1 && mddev->level != 10 &&
5866 !bioset_initialized(&mddev->io_acct_set)) {
5867 err = bioset_init(&mddev->io_acct_set, BIO_POOL_SIZE,
5868 offsetof(struct md_io_acct, bio_clone), 0);
5873 spin_lock(&pers_lock);
5874 pers = find_pers(mddev->level, mddev->clevel);
5875 if (!pers || !try_module_get(pers->owner)) {
5876 spin_unlock(&pers_lock);
5877 if (mddev->level != LEVEL_NONE)
5878 pr_warn("md: personality for level %d is not loaded!\n",
5881 pr_warn("md: personality for level %s is not loaded!\n",
5886 spin_unlock(&pers_lock);
5887 if (mddev->level != pers->level) {
5888 mddev->level = pers->level;
5889 mddev->new_level = pers->level;
5891 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
5893 if (mddev->reshape_position != MaxSector &&
5894 pers->start_reshape == NULL) {
5895 /* This personality cannot handle reshaping... */
5896 module_put(pers->owner);
5901 if (pers->sync_request) {
5902 /* Warn if this is a potentially silly
5905 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5906 struct md_rdev *rdev2;
5909 rdev_for_each(rdev, mddev)
5910 rdev_for_each(rdev2, mddev) {
5912 rdev->bdev->bd_disk ==
5913 rdev2->bdev->bd_disk) {
5914 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5916 bdevname(rdev->bdev,b),
5917 bdevname(rdev2->bdev,b2));
5923 pr_warn("True protection against single-disk failure might be compromised.\n");
5926 mddev->recovery = 0;
5927 /* may be over-ridden by personality */
5928 mddev->resync_max_sectors = mddev->dev_sectors;
5930 mddev->ok_start_degraded = start_dirty_degraded;
5932 if (start_readonly && mddev->ro == 0)
5933 mddev->ro = 2; /* read-only, but switch on first write */
5935 err = pers->run(mddev);
5937 pr_warn("md: pers->run() failed ...\n");
5938 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
5939 WARN_ONCE(!mddev->external_size,
5940 "%s: default size too small, but 'external_size' not in effect?\n",
5942 pr_warn("md: invalid array_size %llu > default size %llu\n",
5943 (unsigned long long)mddev->array_sectors / 2,
5944 (unsigned long long)pers->size(mddev, 0, 0) / 2);
5947 if (err == 0 && pers->sync_request &&
5948 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
5949 struct bitmap *bitmap;
5951 bitmap = md_bitmap_create(mddev, -1);
5952 if (IS_ERR(bitmap)) {
5953 err = PTR_ERR(bitmap);
5954 pr_warn("%s: failed to create bitmap (%d)\n",
5955 mdname(mddev), err);
5957 mddev->bitmap = bitmap;
5963 if (mddev->bitmap_info.max_write_behind > 0) {
5964 bool create_pool = false;
5966 rdev_for_each(rdev, mddev) {
5967 if (test_bit(WriteMostly, &rdev->flags) &&
5968 rdev_init_serial(rdev))
5971 if (create_pool && mddev->serial_info_pool == NULL) {
5972 mddev->serial_info_pool =
5973 mempool_create_kmalloc_pool(NR_SERIAL_INFOS,
5974 sizeof(struct serial_info));
5975 if (!mddev->serial_info_pool) {
5985 rdev_for_each(rdev, mddev) {
5986 if (rdev->raid_disk >= 0 &&
5987 !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
5992 if (mddev->degraded)
5995 blk_queue_flag_set(QUEUE_FLAG_NONROT, mddev->queue);
5997 blk_queue_flag_clear(QUEUE_FLAG_NONROT, mddev->queue);
5998 blk_queue_flag_set(QUEUE_FLAG_IO_STAT, mddev->queue);
6000 if (pers->sync_request) {
6001 if (mddev->kobj.sd &&
6002 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
6003 pr_warn("md: cannot register extra attributes for %s\n",
6005 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
6006 mddev->sysfs_completed = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_completed");
6007 mddev->sysfs_degraded = sysfs_get_dirent_safe(mddev->kobj.sd, "degraded");
6008 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
6011 atomic_set(&mddev->max_corr_read_errors,
6012 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
6013 mddev->safemode = 0;
6014 if (mddev_is_clustered(mddev))
6015 mddev->safemode_delay = 0;
6017 mddev->safemode_delay = DEFAULT_SAFEMODE_DELAY;
6020 spin_lock(&mddev->lock);
6022 spin_unlock(&mddev->lock);
6023 rdev_for_each(rdev, mddev)
6024 if (rdev->raid_disk >= 0)
6025 sysfs_link_rdev(mddev, rdev); /* failure here is OK */
6027 if (mddev->degraded && !mddev->ro)
6028 /* This ensures that recovering status is reported immediately
6029 * via sysfs - until a lack of spares is confirmed.
6031 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6032 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6034 if (mddev->sb_flags)
6035 md_update_sb(mddev, 0);
6037 md_new_event(mddev);
6041 mddev_detach(mddev);
6043 pers->free(mddev, mddev->private);
6044 mddev->private = NULL;
6045 module_put(pers->owner);
6046 md_bitmap_destroy(mddev);
6048 if (mddev->level != 1 && mddev->level != 10)
6049 bioset_exit(&mddev->io_acct_set);
6051 bioset_exit(&mddev->sync_set);
6053 bioset_exit(&mddev->bio_set);
6056 EXPORT_SYMBOL_GPL(md_run);
6058 int do_md_run(struct mddev *mddev)
6062 set_bit(MD_NOT_READY, &mddev->flags);
6063 err = md_run(mddev);
6066 err = md_bitmap_load(mddev);
6068 md_bitmap_destroy(mddev);
6072 if (mddev_is_clustered(mddev))
6073 md_allow_write(mddev);
6075 /* run start up tasks that require md_thread */
6078 md_wakeup_thread(mddev->thread);
6079 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
6081 set_capacity_and_notify(mddev->gendisk, mddev->array_sectors);
6082 clear_bit(MD_NOT_READY, &mddev->flags);
6084 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
6085 sysfs_notify_dirent_safe(mddev->sysfs_state);
6086 sysfs_notify_dirent_safe(mddev->sysfs_action);
6087 sysfs_notify_dirent_safe(mddev->sysfs_degraded);
6089 clear_bit(MD_NOT_READY, &mddev->flags);
6093 int md_start(struct mddev *mddev)
6097 if (mddev->pers->start) {
6098 set_bit(MD_RECOVERY_WAIT, &mddev->recovery);
6099 md_wakeup_thread(mddev->thread);
6100 ret = mddev->pers->start(mddev);
6101 clear_bit(MD_RECOVERY_WAIT, &mddev->recovery);
6102 md_wakeup_thread(mddev->sync_thread);
6106 EXPORT_SYMBOL_GPL(md_start);
6108 static int restart_array(struct mddev *mddev)
6110 struct gendisk *disk = mddev->gendisk;
6111 struct md_rdev *rdev;
6112 bool has_journal = false;
6113 bool has_readonly = false;
6115 /* Complain if it has no devices */
6116 if (list_empty(&mddev->disks))
6124 rdev_for_each_rcu(rdev, mddev) {
6125 if (test_bit(Journal, &rdev->flags) &&
6126 !test_bit(Faulty, &rdev->flags))
6128 if (rdev_read_only(rdev))
6129 has_readonly = true;
6132 if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && !has_journal)
6133 /* Don't restart rw with journal missing/faulty */
6138 mddev->safemode = 0;
6140 set_disk_ro(disk, 0);
6141 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev));
6142 /* Kick recovery or resync if necessary */
6143 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6144 md_wakeup_thread(mddev->thread);
6145 md_wakeup_thread(mddev->sync_thread);
6146 sysfs_notify_dirent_safe(mddev->sysfs_state);
6150 static void md_clean(struct mddev *mddev)
6152 mddev->array_sectors = 0;
6153 mddev->external_size = 0;
6154 mddev->dev_sectors = 0;
6155 mddev->raid_disks = 0;
6156 mddev->recovery_cp = 0;
6157 mddev->resync_min = 0;
6158 mddev->resync_max = MaxSector;
6159 mddev->reshape_position = MaxSector;
6160 mddev->external = 0;
6161 mddev->persistent = 0;
6162 mddev->level = LEVEL_NONE;
6163 mddev->clevel[0] = 0;
6165 mddev->sb_flags = 0;
6167 mddev->metadata_type[0] = 0;
6168 mddev->chunk_sectors = 0;
6169 mddev->ctime = mddev->utime = 0;
6171 mddev->max_disks = 0;
6173 mddev->can_decrease_events = 0;
6174 mddev->delta_disks = 0;
6175 mddev->reshape_backwards = 0;
6176 mddev->new_level = LEVEL_NONE;
6177 mddev->new_layout = 0;
6178 mddev->new_chunk_sectors = 0;
6179 mddev->curr_resync = 0;
6180 atomic64_set(&mddev->resync_mismatches, 0);
6181 mddev->suspend_lo = mddev->suspend_hi = 0;
6182 mddev->sync_speed_min = mddev->sync_speed_max = 0;
6183 mddev->recovery = 0;
6186 mddev->degraded = 0;
6187 mddev->safemode = 0;
6188 mddev->private = NULL;
6189 mddev->cluster_info = NULL;
6190 mddev->bitmap_info.offset = 0;
6191 mddev->bitmap_info.default_offset = 0;
6192 mddev->bitmap_info.default_space = 0;
6193 mddev->bitmap_info.chunksize = 0;
6194 mddev->bitmap_info.daemon_sleep = 0;
6195 mddev->bitmap_info.max_write_behind = 0;
6196 mddev->bitmap_info.nodes = 0;
6199 static void __md_stop_writes(struct mddev *mddev)
6201 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6202 if (work_pending(&mddev->del_work))
6203 flush_workqueue(md_misc_wq);
6204 if (mddev->sync_thread) {
6205 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6206 md_reap_sync_thread(mddev);
6209 del_timer_sync(&mddev->safemode_timer);
6211 if (mddev->pers && mddev->pers->quiesce) {
6212 mddev->pers->quiesce(mddev, 1);
6213 mddev->pers->quiesce(mddev, 0);
6215 md_bitmap_flush(mddev);
6217 if (mddev->ro == 0 &&
6218 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
6220 /* mark array as shutdown cleanly */
6221 if (!mddev_is_clustered(mddev))
6223 md_update_sb(mddev, 1);
6225 /* disable policy to guarantee rdevs free resources for serialization */
6226 mddev->serialize_policy = 0;
6227 mddev_destroy_serial_pool(mddev, NULL, true);
6230 void md_stop_writes(struct mddev *mddev)
6232 mddev_lock_nointr(mddev);
6233 __md_stop_writes(mddev);
6234 mddev_unlock(mddev);
6236 EXPORT_SYMBOL_GPL(md_stop_writes);
6238 static void mddev_detach(struct mddev *mddev)
6240 md_bitmap_wait_behind_writes(mddev);
6241 if (mddev->pers && mddev->pers->quiesce && !mddev->suspended) {
6242 mddev->pers->quiesce(mddev, 1);
6243 mddev->pers->quiesce(mddev, 0);
6245 md_unregister_thread(&mddev->thread);
6247 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
6250 static void __md_stop(struct mddev *mddev)
6252 struct md_personality *pers = mddev->pers;
6253 md_bitmap_destroy(mddev);
6254 mddev_detach(mddev);
6255 /* Ensure ->event_work is done */
6256 if (mddev->event_work.func)
6257 flush_workqueue(md_misc_wq);
6258 spin_lock(&mddev->lock);
6260 spin_unlock(&mddev->lock);
6261 pers->free(mddev, mddev->private);
6262 mddev->private = NULL;
6263 if (pers->sync_request && mddev->to_remove == NULL)
6264 mddev->to_remove = &md_redundancy_group;
6265 module_put(pers->owner);
6266 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6269 void md_stop(struct mddev *mddev)
6271 /* stop the array and free an attached data structures.
6272 * This is called from dm-raid
6275 bioset_exit(&mddev->bio_set);
6276 bioset_exit(&mddev->sync_set);
6277 if (mddev->level != 1 && mddev->level != 10)
6278 bioset_exit(&mddev->io_acct_set);
6281 EXPORT_SYMBOL_GPL(md_stop);
6283 static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
6288 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
6290 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6291 md_wakeup_thread(mddev->thread);
6293 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
6294 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6295 if (mddev->sync_thread)
6296 /* Thread might be blocked waiting for metadata update
6297 * which will now never happen */
6298 wake_up_process(mddev->sync_thread->tsk);
6300 if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
6302 mddev_unlock(mddev);
6303 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
6305 wait_event(mddev->sb_wait,
6306 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
6307 mddev_lock_nointr(mddev);
6309 mutex_lock(&mddev->open_mutex);
6310 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
6311 mddev->sync_thread ||
6312 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
6313 pr_warn("md: %s still in use.\n",mdname(mddev));
6315 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6316 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6317 md_wakeup_thread(mddev->thread);
6323 __md_stop_writes(mddev);
6329 set_disk_ro(mddev->gendisk, 1);
6330 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6331 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6332 md_wakeup_thread(mddev->thread);
6333 sysfs_notify_dirent_safe(mddev->sysfs_state);
6337 mutex_unlock(&mddev->open_mutex);
6342 * 0 - completely stop and dis-assemble array
6343 * 2 - stop but do not disassemble array
6345 static int do_md_stop(struct mddev *mddev, int mode,
6346 struct block_device *bdev)
6348 struct gendisk *disk = mddev->gendisk;
6349 struct md_rdev *rdev;
6352 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
6354 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6355 md_wakeup_thread(mddev->thread);
6357 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
6358 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6359 if (mddev->sync_thread)
6360 /* Thread might be blocked waiting for metadata update
6361 * which will now never happen */
6362 wake_up_process(mddev->sync_thread->tsk);
6364 mddev_unlock(mddev);
6365 wait_event(resync_wait, (mddev->sync_thread == NULL &&
6366 !test_bit(MD_RECOVERY_RUNNING,
6367 &mddev->recovery)));
6368 mddev_lock_nointr(mddev);
6370 mutex_lock(&mddev->open_mutex);
6371 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
6372 mddev->sysfs_active ||
6373 mddev->sync_thread ||
6374 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
6375 pr_warn("md: %s still in use.\n",mdname(mddev));
6376 mutex_unlock(&mddev->open_mutex);
6378 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6379 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6380 md_wakeup_thread(mddev->thread);
6386 set_disk_ro(disk, 0);
6388 __md_stop_writes(mddev);
6391 /* tell userspace to handle 'inactive' */
6392 sysfs_notify_dirent_safe(mddev->sysfs_state);
6394 rdev_for_each(rdev, mddev)
6395 if (rdev->raid_disk >= 0)
6396 sysfs_unlink_rdev(mddev, rdev);
6398 set_capacity_and_notify(disk, 0);
6399 mutex_unlock(&mddev->open_mutex);
6405 mutex_unlock(&mddev->open_mutex);
6407 * Free resources if final stop
6410 pr_info("md: %s stopped.\n", mdname(mddev));
6412 if (mddev->bitmap_info.file) {
6413 struct file *f = mddev->bitmap_info.file;
6414 spin_lock(&mddev->lock);
6415 mddev->bitmap_info.file = NULL;
6416 spin_unlock(&mddev->lock);
6419 mddev->bitmap_info.offset = 0;
6421 export_array(mddev);
6424 if (mddev->hold_active == UNTIL_STOP)
6425 mddev->hold_active = 0;
6427 md_new_event(mddev);
6428 sysfs_notify_dirent_safe(mddev->sysfs_state);
6433 static void autorun_array(struct mddev *mddev)
6435 struct md_rdev *rdev;
6438 if (list_empty(&mddev->disks))
6441 pr_info("md: running: ");
6443 rdev_for_each(rdev, mddev) {
6444 char b[BDEVNAME_SIZE];
6445 pr_cont("<%s>", bdevname(rdev->bdev,b));
6449 err = do_md_run(mddev);
6451 pr_warn("md: do_md_run() returned %d\n", err);
6452 do_md_stop(mddev, 0, NULL);
6457 * lets try to run arrays based on all disks that have arrived
6458 * until now. (those are in pending_raid_disks)
6460 * the method: pick the first pending disk, collect all disks with
6461 * the same UUID, remove all from the pending list and put them into
6462 * the 'same_array' list. Then order this list based on superblock
6463 * update time (freshest comes first), kick out 'old' disks and
6464 * compare superblocks. If everything's fine then run it.
6466 * If "unit" is allocated, then bump its reference count
6468 static void autorun_devices(int part)
6470 struct md_rdev *rdev0, *rdev, *tmp;
6471 struct mddev *mddev;
6472 char b[BDEVNAME_SIZE];
6474 pr_info("md: autorun ...\n");
6475 while (!list_empty(&pending_raid_disks)) {
6478 LIST_HEAD(candidates);
6479 rdev0 = list_entry(pending_raid_disks.next,
6480 struct md_rdev, same_set);
6482 pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b));
6483 INIT_LIST_HEAD(&candidates);
6484 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
6485 if (super_90_load(rdev, rdev0, 0) >= 0) {
6486 pr_debug("md: adding %s ...\n",
6487 bdevname(rdev->bdev,b));
6488 list_move(&rdev->same_set, &candidates);
6491 * now we have a set of devices, with all of them having
6492 * mostly sane superblocks. It's time to allocate the
6496 dev = MKDEV(mdp_major,
6497 rdev0->preferred_minor << MdpMinorShift);
6498 unit = MINOR(dev) >> MdpMinorShift;
6500 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
6503 if (rdev0->preferred_minor != unit) {
6504 pr_warn("md: unit number in %s is bad: %d\n",
6505 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
6510 mddev = mddev_find(dev);
6514 if (mddev_lock(mddev))
6515 pr_warn("md: %s locked, cannot run\n", mdname(mddev));
6516 else if (mddev->raid_disks || mddev->major_version
6517 || !list_empty(&mddev->disks)) {
6518 pr_warn("md: %s already running, cannot run %s\n",
6519 mdname(mddev), bdevname(rdev0->bdev,b));
6520 mddev_unlock(mddev);
6522 pr_debug("md: created %s\n", mdname(mddev));
6523 mddev->persistent = 1;
6524 rdev_for_each_list(rdev, tmp, &candidates) {
6525 list_del_init(&rdev->same_set);
6526 if (bind_rdev_to_array(rdev, mddev))
6529 autorun_array(mddev);
6530 mddev_unlock(mddev);
6532 /* on success, candidates will be empty, on error
6535 rdev_for_each_list(rdev, tmp, &candidates) {
6536 list_del_init(&rdev->same_set);
6541 pr_info("md: ... autorun DONE.\n");
6543 #endif /* !MODULE */
6545 static int get_version(void __user *arg)
6549 ver.major = MD_MAJOR_VERSION;
6550 ver.minor = MD_MINOR_VERSION;
6551 ver.patchlevel = MD_PATCHLEVEL_VERSION;
6553 if (copy_to_user(arg, &ver, sizeof(ver)))
6559 static int get_array_info(struct mddev *mddev, void __user *arg)
6561 mdu_array_info_t info;
6562 int nr,working,insync,failed,spare;
6563 struct md_rdev *rdev;
6565 nr = working = insync = failed = spare = 0;
6567 rdev_for_each_rcu(rdev, mddev) {
6569 if (test_bit(Faulty, &rdev->flags))
6573 if (test_bit(In_sync, &rdev->flags))
6575 else if (test_bit(Journal, &rdev->flags))
6576 /* TODO: add journal count to md_u.h */
6584 info.major_version = mddev->major_version;
6585 info.minor_version = mddev->minor_version;
6586 info.patch_version = MD_PATCHLEVEL_VERSION;
6587 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
6588 info.level = mddev->level;
6589 info.size = mddev->dev_sectors / 2;
6590 if (info.size != mddev->dev_sectors / 2) /* overflow */
6593 info.raid_disks = mddev->raid_disks;
6594 info.md_minor = mddev->md_minor;
6595 info.not_persistent= !mddev->persistent;
6597 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
6600 info.state = (1<<MD_SB_CLEAN);
6601 if (mddev->bitmap && mddev->bitmap_info.offset)
6602 info.state |= (1<<MD_SB_BITMAP_PRESENT);
6603 if (mddev_is_clustered(mddev))
6604 info.state |= (1<<MD_SB_CLUSTERED);
6605 info.active_disks = insync;
6606 info.working_disks = working;
6607 info.failed_disks = failed;
6608 info.spare_disks = spare;
6610 info.layout = mddev->layout;
6611 info.chunk_size = mddev->chunk_sectors << 9;
6613 if (copy_to_user(arg, &info, sizeof(info)))
6619 static int get_bitmap_file(struct mddev *mddev, void __user * arg)
6621 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
6625 file = kzalloc(sizeof(*file), GFP_NOIO);
6630 spin_lock(&mddev->lock);
6631 /* bitmap enabled */
6632 if (mddev->bitmap_info.file) {
6633 ptr = file_path(mddev->bitmap_info.file, file->pathname,
6634 sizeof(file->pathname));
6638 memmove(file->pathname, ptr,
6639 sizeof(file->pathname)-(ptr-file->pathname));
6641 spin_unlock(&mddev->lock);
6644 copy_to_user(arg, file, sizeof(*file)))
6651 static int get_disk_info(struct mddev *mddev, void __user * arg)
6653 mdu_disk_info_t info;
6654 struct md_rdev *rdev;
6656 if (copy_from_user(&info, arg, sizeof(info)))
6660 rdev = md_find_rdev_nr_rcu(mddev, info.number);
6662 info.major = MAJOR(rdev->bdev->bd_dev);
6663 info.minor = MINOR(rdev->bdev->bd_dev);
6664 info.raid_disk = rdev->raid_disk;
6666 if (test_bit(Faulty, &rdev->flags))
6667 info.state |= (1<<MD_DISK_FAULTY);
6668 else if (test_bit(In_sync, &rdev->flags)) {
6669 info.state |= (1<<MD_DISK_ACTIVE);
6670 info.state |= (1<<MD_DISK_SYNC);
6672 if (test_bit(Journal, &rdev->flags))
6673 info.state |= (1<<MD_DISK_JOURNAL);
6674 if (test_bit(WriteMostly, &rdev->flags))
6675 info.state |= (1<<MD_DISK_WRITEMOSTLY);
6676 if (test_bit(FailFast, &rdev->flags))
6677 info.state |= (1<<MD_DISK_FAILFAST);
6679 info.major = info.minor = 0;
6680 info.raid_disk = -1;
6681 info.state = (1<<MD_DISK_REMOVED);
6685 if (copy_to_user(arg, &info, sizeof(info)))
6691 int md_add_new_disk(struct mddev *mddev, struct mdu_disk_info_s *info)
6693 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
6694 struct md_rdev *rdev;
6695 dev_t dev = MKDEV(info->major,info->minor);
6697 if (mddev_is_clustered(mddev) &&
6698 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
6699 pr_warn("%s: Cannot add to clustered mddev.\n",
6704 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
6707 if (!mddev->raid_disks) {
6709 /* expecting a device which has a superblock */
6710 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
6712 pr_warn("md: md_import_device returned %ld\n",
6714 return PTR_ERR(rdev);
6716 if (!list_empty(&mddev->disks)) {
6717 struct md_rdev *rdev0
6718 = list_entry(mddev->disks.next,
6719 struct md_rdev, same_set);
6720 err = super_types[mddev->major_version]
6721 .load_super(rdev, rdev0, mddev->minor_version);
6723 pr_warn("md: %s has different UUID to %s\n",
6724 bdevname(rdev->bdev,b),
6725 bdevname(rdev0->bdev,b2));
6730 err = bind_rdev_to_array(rdev, mddev);
6737 * md_add_new_disk can be used once the array is assembled
6738 * to add "hot spares". They must already have a superblock
6743 if (!mddev->pers->hot_add_disk) {
6744 pr_warn("%s: personality does not support diskops!\n",
6748 if (mddev->persistent)
6749 rdev = md_import_device(dev, mddev->major_version,
6750 mddev->minor_version);
6752 rdev = md_import_device(dev, -1, -1);
6754 pr_warn("md: md_import_device returned %ld\n",
6756 return PTR_ERR(rdev);
6758 /* set saved_raid_disk if appropriate */
6759 if (!mddev->persistent) {
6760 if (info->state & (1<<MD_DISK_SYNC) &&
6761 info->raid_disk < mddev->raid_disks) {
6762 rdev->raid_disk = info->raid_disk;
6763 set_bit(In_sync, &rdev->flags);
6764 clear_bit(Bitmap_sync, &rdev->flags);
6766 rdev->raid_disk = -1;
6767 rdev->saved_raid_disk = rdev->raid_disk;
6769 super_types[mddev->major_version].
6770 validate_super(mddev, rdev);
6771 if ((info->state & (1<<MD_DISK_SYNC)) &&
6772 rdev->raid_disk != info->raid_disk) {
6773 /* This was a hot-add request, but events doesn't
6774 * match, so reject it.
6780 clear_bit(In_sync, &rdev->flags); /* just to be sure */
6781 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6782 set_bit(WriteMostly, &rdev->flags);
6784 clear_bit(WriteMostly, &rdev->flags);
6785 if (info->state & (1<<MD_DISK_FAILFAST))
6786 set_bit(FailFast, &rdev->flags);
6788 clear_bit(FailFast, &rdev->flags);
6790 if (info->state & (1<<MD_DISK_JOURNAL)) {
6791 struct md_rdev *rdev2;
6792 bool has_journal = false;
6794 /* make sure no existing journal disk */
6795 rdev_for_each(rdev2, mddev) {
6796 if (test_bit(Journal, &rdev2->flags)) {
6801 if (has_journal || mddev->bitmap) {
6805 set_bit(Journal, &rdev->flags);
6808 * check whether the device shows up in other nodes
6810 if (mddev_is_clustered(mddev)) {
6811 if (info->state & (1 << MD_DISK_CANDIDATE))
6812 set_bit(Candidate, &rdev->flags);
6813 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
6814 /* --add initiated by this node */
6815 err = md_cluster_ops->add_new_disk(mddev, rdev);
6823 rdev->raid_disk = -1;
6824 err = bind_rdev_to_array(rdev, mddev);
6829 if (mddev_is_clustered(mddev)) {
6830 if (info->state & (1 << MD_DISK_CANDIDATE)) {
6832 err = md_cluster_ops->new_disk_ack(mddev,
6835 md_kick_rdev_from_array(rdev);
6839 md_cluster_ops->add_new_disk_cancel(mddev);
6841 err = add_bound_rdev(rdev);
6845 err = add_bound_rdev(rdev);
6850 /* otherwise, md_add_new_disk is only allowed
6851 * for major_version==0 superblocks
6853 if (mddev->major_version != 0) {
6854 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev));
6858 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6860 rdev = md_import_device(dev, -1, 0);
6862 pr_warn("md: error, md_import_device() returned %ld\n",
6864 return PTR_ERR(rdev);
6866 rdev->desc_nr = info->number;
6867 if (info->raid_disk < mddev->raid_disks)
6868 rdev->raid_disk = info->raid_disk;
6870 rdev->raid_disk = -1;
6872 if (rdev->raid_disk < mddev->raid_disks)
6873 if (info->state & (1<<MD_DISK_SYNC))
6874 set_bit(In_sync, &rdev->flags);
6876 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6877 set_bit(WriteMostly, &rdev->flags);
6878 if (info->state & (1<<MD_DISK_FAILFAST))
6879 set_bit(FailFast, &rdev->flags);
6881 if (!mddev->persistent) {
6882 pr_debug("md: nonpersistent superblock ...\n");
6883 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6885 rdev->sb_start = calc_dev_sboffset(rdev);
6886 rdev->sectors = rdev->sb_start;
6888 err = bind_rdev_to_array(rdev, mddev);
6898 static int hot_remove_disk(struct mddev *mddev, dev_t dev)
6900 char b[BDEVNAME_SIZE];
6901 struct md_rdev *rdev;
6906 rdev = find_rdev(mddev, dev);
6910 if (rdev->raid_disk < 0)
6913 clear_bit(Blocked, &rdev->flags);
6914 remove_and_add_spares(mddev, rdev);
6916 if (rdev->raid_disk >= 0)
6920 if (mddev_is_clustered(mddev)) {
6921 if (md_cluster_ops->remove_disk(mddev, rdev))
6925 md_kick_rdev_from_array(rdev);
6926 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
6928 md_wakeup_thread(mddev->thread);
6930 md_update_sb(mddev, 1);
6931 md_new_event(mddev);
6935 pr_debug("md: cannot remove active disk %s from %s ...\n",
6936 bdevname(rdev->bdev,b), mdname(mddev));
6940 static int hot_add_disk(struct mddev *mddev, dev_t dev)
6942 char b[BDEVNAME_SIZE];
6944 struct md_rdev *rdev;
6949 if (mddev->major_version != 0) {
6950 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
6954 if (!mddev->pers->hot_add_disk) {
6955 pr_warn("%s: personality does not support diskops!\n",
6960 rdev = md_import_device(dev, -1, 0);
6962 pr_warn("md: error, md_import_device() returned %ld\n",
6967 if (mddev->persistent)
6968 rdev->sb_start = calc_dev_sboffset(rdev);
6970 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6972 rdev->sectors = rdev->sb_start;
6974 if (test_bit(Faulty, &rdev->flags)) {
6975 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
6976 bdevname(rdev->bdev,b), mdname(mddev));
6981 clear_bit(In_sync, &rdev->flags);
6983 rdev->saved_raid_disk = -1;
6984 err = bind_rdev_to_array(rdev, mddev);
6989 * The rest should better be atomic, we can have disk failures
6990 * noticed in interrupt contexts ...
6993 rdev->raid_disk = -1;
6995 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
6997 md_update_sb(mddev, 1);
6999 * Kick recovery, maybe this spare has to be added to the
7000 * array immediately.
7002 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7003 md_wakeup_thread(mddev->thread);
7004 md_new_event(mddev);
7012 static int set_bitmap_file(struct mddev *mddev, int fd)
7017 if (!mddev->pers->quiesce || !mddev->thread)
7019 if (mddev->recovery || mddev->sync_thread)
7021 /* we should be able to change the bitmap.. */
7025 struct inode *inode;
7028 if (mddev->bitmap || mddev->bitmap_info.file)
7029 return -EEXIST; /* cannot add when bitmap is present */
7033 pr_warn("%s: error: failed to get bitmap file\n",
7038 inode = f->f_mapping->host;
7039 if (!S_ISREG(inode->i_mode)) {
7040 pr_warn("%s: error: bitmap file must be a regular file\n",
7043 } else if (!(f->f_mode & FMODE_WRITE)) {
7044 pr_warn("%s: error: bitmap file must open for write\n",
7047 } else if (atomic_read(&inode->i_writecount) != 1) {
7048 pr_warn("%s: error: bitmap file is already in use\n",
7056 mddev->bitmap_info.file = f;
7057 mddev->bitmap_info.offset = 0; /* file overrides offset */
7058 } else if (mddev->bitmap == NULL)
7059 return -ENOENT; /* cannot remove what isn't there */
7063 struct bitmap *bitmap;
7065 bitmap = md_bitmap_create(mddev, -1);
7066 mddev_suspend(mddev);
7067 if (!IS_ERR(bitmap)) {
7068 mddev->bitmap = bitmap;
7069 err = md_bitmap_load(mddev);
7071 err = PTR_ERR(bitmap);
7073 md_bitmap_destroy(mddev);
7076 mddev_resume(mddev);
7077 } else if (fd < 0) {
7078 mddev_suspend(mddev);
7079 md_bitmap_destroy(mddev);
7080 mddev_resume(mddev);
7084 struct file *f = mddev->bitmap_info.file;
7086 spin_lock(&mddev->lock);
7087 mddev->bitmap_info.file = NULL;
7088 spin_unlock(&mddev->lock);
7097 * md_set_array_info is used two different ways
7098 * The original usage is when creating a new array.
7099 * In this usage, raid_disks is > 0 and it together with
7100 * level, size, not_persistent,layout,chunksize determine the
7101 * shape of the array.
7102 * This will always create an array with a type-0.90.0 superblock.
7103 * The newer usage is when assembling an array.
7104 * In this case raid_disks will be 0, and the major_version field is
7105 * use to determine which style super-blocks are to be found on the devices.
7106 * The minor and patch _version numbers are also kept incase the
7107 * super_block handler wishes to interpret them.
7109 int md_set_array_info(struct mddev *mddev, struct mdu_array_info_s *info)
7111 if (info->raid_disks == 0) {
7112 /* just setting version number for superblock loading */
7113 if (info->major_version < 0 ||
7114 info->major_version >= ARRAY_SIZE(super_types) ||
7115 super_types[info->major_version].name == NULL) {
7116 /* maybe try to auto-load a module? */
7117 pr_warn("md: superblock version %d not known\n",
7118 info->major_version);
7121 mddev->major_version = info->major_version;
7122 mddev->minor_version = info->minor_version;
7123 mddev->patch_version = info->patch_version;
7124 mddev->persistent = !info->not_persistent;
7125 /* ensure mddev_put doesn't delete this now that there
7126 * is some minimal configuration.
7128 mddev->ctime = ktime_get_real_seconds();
7131 mddev->major_version = MD_MAJOR_VERSION;
7132 mddev->minor_version = MD_MINOR_VERSION;
7133 mddev->patch_version = MD_PATCHLEVEL_VERSION;
7134 mddev->ctime = ktime_get_real_seconds();
7136 mddev->level = info->level;
7137 mddev->clevel[0] = 0;
7138 mddev->dev_sectors = 2 * (sector_t)info->size;
7139 mddev->raid_disks = info->raid_disks;
7140 /* don't set md_minor, it is determined by which /dev/md* was
7143 if (info->state & (1<<MD_SB_CLEAN))
7144 mddev->recovery_cp = MaxSector;
7146 mddev->recovery_cp = 0;
7147 mddev->persistent = ! info->not_persistent;
7148 mddev->external = 0;
7150 mddev->layout = info->layout;
7151 if (mddev->level == 0)
7152 /* Cannot trust RAID0 layout info here */
7154 mddev->chunk_sectors = info->chunk_size >> 9;
7156 if (mddev->persistent) {
7157 mddev->max_disks = MD_SB_DISKS;
7159 mddev->sb_flags = 0;
7161 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
7163 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
7164 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
7165 mddev->bitmap_info.offset = 0;
7167 mddev->reshape_position = MaxSector;
7170 * Generate a 128 bit UUID
7172 get_random_bytes(mddev->uuid, 16);
7174 mddev->new_level = mddev->level;
7175 mddev->new_chunk_sectors = mddev->chunk_sectors;
7176 mddev->new_layout = mddev->layout;
7177 mddev->delta_disks = 0;
7178 mddev->reshape_backwards = 0;
7183 void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
7185 lockdep_assert_held(&mddev->reconfig_mutex);
7187 if (mddev->external_size)
7190 mddev->array_sectors = array_sectors;
7192 EXPORT_SYMBOL(md_set_array_sectors);
7194 static int update_size(struct mddev *mddev, sector_t num_sectors)
7196 struct md_rdev *rdev;
7198 int fit = (num_sectors == 0);
7199 sector_t old_dev_sectors = mddev->dev_sectors;
7201 if (mddev->pers->resize == NULL)
7203 /* The "num_sectors" is the number of sectors of each device that
7204 * is used. This can only make sense for arrays with redundancy.
7205 * linear and raid0 always use whatever space is available. We can only
7206 * consider changing this number if no resync or reconstruction is
7207 * happening, and if the new size is acceptable. It must fit before the
7208 * sb_start or, if that is <data_offset, it must fit before the size
7209 * of each device. If num_sectors is zero, we find the largest size
7212 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
7218 rdev_for_each(rdev, mddev) {
7219 sector_t avail = rdev->sectors;
7221 if (fit && (num_sectors == 0 || num_sectors > avail))
7222 num_sectors = avail;
7223 if (avail < num_sectors)
7226 rv = mddev->pers->resize(mddev, num_sectors);
7228 if (mddev_is_clustered(mddev))
7229 md_cluster_ops->update_size(mddev, old_dev_sectors);
7230 else if (mddev->queue) {
7231 set_capacity_and_notify(mddev->gendisk,
7232 mddev->array_sectors);
7238 static int update_raid_disks(struct mddev *mddev, int raid_disks)
7241 struct md_rdev *rdev;
7242 /* change the number of raid disks */
7243 if (mddev->pers->check_reshape == NULL)
7247 if (raid_disks <= 0 ||
7248 (mddev->max_disks && raid_disks >= mddev->max_disks))
7250 if (mddev->sync_thread ||
7251 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
7252 test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) ||
7253 mddev->reshape_position != MaxSector)
7256 rdev_for_each(rdev, mddev) {
7257 if (mddev->raid_disks < raid_disks &&
7258 rdev->data_offset < rdev->new_data_offset)
7260 if (mddev->raid_disks > raid_disks &&
7261 rdev->data_offset > rdev->new_data_offset)
7265 mddev->delta_disks = raid_disks - mddev->raid_disks;
7266 if (mddev->delta_disks < 0)
7267 mddev->reshape_backwards = 1;
7268 else if (mddev->delta_disks > 0)
7269 mddev->reshape_backwards = 0;
7271 rv = mddev->pers->check_reshape(mddev);
7273 mddev->delta_disks = 0;
7274 mddev->reshape_backwards = 0;
7280 * update_array_info is used to change the configuration of an
7282 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
7283 * fields in the info are checked against the array.
7284 * Any differences that cannot be handled will cause an error.
7285 * Normally, only one change can be managed at a time.
7287 static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
7293 /* calculate expected state,ignoring low bits */
7294 if (mddev->bitmap && mddev->bitmap_info.offset)
7295 state |= (1 << MD_SB_BITMAP_PRESENT);
7297 if (mddev->major_version != info->major_version ||
7298 mddev->minor_version != info->minor_version ||
7299 /* mddev->patch_version != info->patch_version || */
7300 mddev->ctime != info->ctime ||
7301 mddev->level != info->level ||
7302 /* mddev->layout != info->layout || */
7303 mddev->persistent != !info->not_persistent ||
7304 mddev->chunk_sectors != info->chunk_size >> 9 ||
7305 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
7306 ((state^info->state) & 0xfffffe00)
7309 /* Check there is only one change */
7310 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
7312 if (mddev->raid_disks != info->raid_disks)
7314 if (mddev->layout != info->layout)
7316 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
7323 if (mddev->layout != info->layout) {
7325 * we don't need to do anything at the md level, the
7326 * personality will take care of it all.
7328 if (mddev->pers->check_reshape == NULL)
7331 mddev->new_layout = info->layout;
7332 rv = mddev->pers->check_reshape(mddev);
7334 mddev->new_layout = mddev->layout;
7338 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
7339 rv = update_size(mddev, (sector_t)info->size * 2);
7341 if (mddev->raid_disks != info->raid_disks)
7342 rv = update_raid_disks(mddev, info->raid_disks);
7344 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
7345 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
7349 if (mddev->recovery || mddev->sync_thread) {
7353 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
7354 struct bitmap *bitmap;
7355 /* add the bitmap */
7356 if (mddev->bitmap) {
7360 if (mddev->bitmap_info.default_offset == 0) {
7364 mddev->bitmap_info.offset =
7365 mddev->bitmap_info.default_offset;
7366 mddev->bitmap_info.space =
7367 mddev->bitmap_info.default_space;
7368 bitmap = md_bitmap_create(mddev, -1);
7369 mddev_suspend(mddev);
7370 if (!IS_ERR(bitmap)) {
7371 mddev->bitmap = bitmap;
7372 rv = md_bitmap_load(mddev);
7374 rv = PTR_ERR(bitmap);
7376 md_bitmap_destroy(mddev);
7377 mddev_resume(mddev);
7379 /* remove the bitmap */
7380 if (!mddev->bitmap) {
7384 if (mddev->bitmap->storage.file) {
7388 if (mddev->bitmap_info.nodes) {
7389 /* hold PW on all the bitmap lock */
7390 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
7391 pr_warn("md: can't change bitmap to none since the array is in use by more than one node\n");
7393 md_cluster_ops->unlock_all_bitmaps(mddev);
7397 mddev->bitmap_info.nodes = 0;
7398 md_cluster_ops->leave(mddev);
7399 module_put(md_cluster_mod);
7400 mddev->safemode_delay = DEFAULT_SAFEMODE_DELAY;
7402 mddev_suspend(mddev);
7403 md_bitmap_destroy(mddev);
7404 mddev_resume(mddev);
7405 mddev->bitmap_info.offset = 0;
7408 md_update_sb(mddev, 1);
7414 static int set_disk_faulty(struct mddev *mddev, dev_t dev)
7416 struct md_rdev *rdev;
7419 if (mddev->pers == NULL)
7423 rdev = md_find_rdev_rcu(mddev, dev);
7427 md_error(mddev, rdev);
7428 if (!test_bit(Faulty, &rdev->flags))
7436 * We have a problem here : there is no easy way to give a CHS
7437 * virtual geometry. We currently pretend that we have a 2 heads
7438 * 4 sectors (with a BIG number of cylinders...). This drives
7439 * dosfs just mad... ;-)
7441 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
7443 struct mddev *mddev = bdev->bd_disk->private_data;
7447 geo->cylinders = mddev->array_sectors / 8;
7451 static inline bool md_ioctl_valid(unsigned int cmd)
7455 case GET_ARRAY_INFO:
7456 case GET_BITMAP_FILE:
7459 case HOT_REMOVE_DISK:
7461 case RESTART_ARRAY_RW:
7463 case SET_ARRAY_INFO:
7464 case SET_BITMAP_FILE:
7465 case SET_DISK_FAULTY:
7468 case CLUSTERED_DISK_NACK:
7475 static int md_ioctl(struct block_device *bdev, fmode_t mode,
7476 unsigned int cmd, unsigned long arg)
7479 void __user *argp = (void __user *)arg;
7480 struct mddev *mddev = NULL;
7481 bool did_set_md_closing = false;
7483 if (!md_ioctl_valid(cmd))
7488 case GET_ARRAY_INFO:
7492 if (!capable(CAP_SYS_ADMIN))
7497 * Commands dealing with the RAID driver but not any
7502 err = get_version(argp);
7508 * Commands creating/starting a new array:
7511 mddev = bdev->bd_disk->private_data;
7518 /* Some actions do not requires the mutex */
7520 case GET_ARRAY_INFO:
7521 if (!mddev->raid_disks && !mddev->external)
7524 err = get_array_info(mddev, argp);
7528 if (!mddev->raid_disks && !mddev->external)
7531 err = get_disk_info(mddev, argp);
7534 case SET_DISK_FAULTY:
7535 err = set_disk_faulty(mddev, new_decode_dev(arg));
7538 case GET_BITMAP_FILE:
7539 err = get_bitmap_file(mddev, argp);
7544 if (cmd == ADD_NEW_DISK || cmd == HOT_ADD_DISK)
7545 flush_rdev_wq(mddev);
7547 if (cmd == HOT_REMOVE_DISK)
7548 /* need to ensure recovery thread has run */
7549 wait_event_interruptible_timeout(mddev->sb_wait,
7550 !test_bit(MD_RECOVERY_NEEDED,
7552 msecs_to_jiffies(5000));
7553 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
7554 /* Need to flush page cache, and ensure no-one else opens
7557 mutex_lock(&mddev->open_mutex);
7558 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
7559 mutex_unlock(&mddev->open_mutex);
7563 if (test_and_set_bit(MD_CLOSING, &mddev->flags)) {
7564 mutex_unlock(&mddev->open_mutex);
7568 did_set_md_closing = true;
7569 mutex_unlock(&mddev->open_mutex);
7570 sync_blockdev(bdev);
7572 err = mddev_lock(mddev);
7574 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7579 if (cmd == SET_ARRAY_INFO) {
7580 mdu_array_info_t info;
7582 memset(&info, 0, sizeof(info));
7583 else if (copy_from_user(&info, argp, sizeof(info))) {
7588 err = update_array_info(mddev, &info);
7590 pr_warn("md: couldn't update array info. %d\n", err);
7595 if (!list_empty(&mddev->disks)) {
7596 pr_warn("md: array %s already has disks!\n", mdname(mddev));
7600 if (mddev->raid_disks) {
7601 pr_warn("md: array %s already initialised!\n", mdname(mddev));
7605 err = md_set_array_info(mddev, &info);
7607 pr_warn("md: couldn't set array info. %d\n", err);
7614 * Commands querying/configuring an existing array:
7616 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
7617 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
7618 if ((!mddev->raid_disks && !mddev->external)
7619 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
7620 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
7621 && cmd != GET_BITMAP_FILE) {
7627 * Commands even a read-only array can execute:
7630 case RESTART_ARRAY_RW:
7631 err = restart_array(mddev);
7635 err = do_md_stop(mddev, 0, bdev);
7639 err = md_set_readonly(mddev, bdev);
7642 case HOT_REMOVE_DISK:
7643 err = hot_remove_disk(mddev, new_decode_dev(arg));
7647 /* We can support ADD_NEW_DISK on read-only arrays
7648 * only if we are re-adding a preexisting device.
7649 * So require mddev->pers and MD_DISK_SYNC.
7652 mdu_disk_info_t info;
7653 if (copy_from_user(&info, argp, sizeof(info)))
7655 else if (!(info.state & (1<<MD_DISK_SYNC)))
7656 /* Need to clear read-only for this */
7659 err = md_add_new_disk(mddev, &info);
7666 * The remaining ioctls are changing the state of the
7667 * superblock, so we do not allow them on read-only arrays.
7669 if (mddev->ro && mddev->pers) {
7670 if (mddev->ro == 2) {
7672 sysfs_notify_dirent_safe(mddev->sysfs_state);
7673 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7674 /* mddev_unlock will wake thread */
7675 /* If a device failed while we were read-only, we
7676 * need to make sure the metadata is updated now.
7678 if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) {
7679 mddev_unlock(mddev);
7680 wait_event(mddev->sb_wait,
7681 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) &&
7682 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
7683 mddev_lock_nointr(mddev);
7694 mdu_disk_info_t info;
7695 if (copy_from_user(&info, argp, sizeof(info)))
7698 err = md_add_new_disk(mddev, &info);
7702 case CLUSTERED_DISK_NACK:
7703 if (mddev_is_clustered(mddev))
7704 md_cluster_ops->new_disk_ack(mddev, false);
7710 err = hot_add_disk(mddev, new_decode_dev(arg));
7714 err = do_md_run(mddev);
7717 case SET_BITMAP_FILE:
7718 err = set_bitmap_file(mddev, (int)arg);
7727 if (mddev->hold_active == UNTIL_IOCTL &&
7729 mddev->hold_active = 0;
7730 mddev_unlock(mddev);
7732 if(did_set_md_closing)
7733 clear_bit(MD_CLOSING, &mddev->flags);
7736 #ifdef CONFIG_COMPAT
7737 static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7738 unsigned int cmd, unsigned long arg)
7741 case HOT_REMOVE_DISK:
7743 case SET_DISK_FAULTY:
7744 case SET_BITMAP_FILE:
7745 /* These take in integer arg, do not convert */
7748 arg = (unsigned long)compat_ptr(arg);
7752 return md_ioctl(bdev, mode, cmd, arg);
7754 #endif /* CONFIG_COMPAT */
7756 static int md_set_read_only(struct block_device *bdev, bool ro)
7758 struct mddev *mddev = bdev->bd_disk->private_data;
7761 err = mddev_lock(mddev);
7765 if (!mddev->raid_disks && !mddev->external) {
7771 * Transitioning to read-auto need only happen for arrays that call
7772 * md_write_start and which are not ready for writes yet.
7774 if (!ro && mddev->ro == 1 && mddev->pers) {
7775 err = restart_array(mddev);
7782 mddev_unlock(mddev);
7786 static int md_open(struct block_device *bdev, fmode_t mode)
7789 * Succeed if we can lock the mddev, which confirms that
7790 * it isn't being stopped right now.
7792 struct mddev *mddev = mddev_find(bdev->bd_dev);
7798 if (mddev->gendisk != bdev->bd_disk) {
7799 /* we are racing with mddev_put which is discarding this
7803 /* Wait until bdev->bd_disk is definitely gone */
7804 if (work_pending(&mddev->del_work))
7805 flush_workqueue(md_misc_wq);
7808 BUG_ON(mddev != bdev->bd_disk->private_data);
7810 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
7813 if (test_bit(MD_CLOSING, &mddev->flags)) {
7814 mutex_unlock(&mddev->open_mutex);
7820 atomic_inc(&mddev->openers);
7821 mutex_unlock(&mddev->open_mutex);
7823 bdev_check_media_change(bdev);
7830 static void md_release(struct gendisk *disk, fmode_t mode)
7832 struct mddev *mddev = disk->private_data;
7835 atomic_dec(&mddev->openers);
7839 static unsigned int md_check_events(struct gendisk *disk, unsigned int clearing)
7841 struct mddev *mddev = disk->private_data;
7842 unsigned int ret = 0;
7845 ret = DISK_EVENT_MEDIA_CHANGE;
7850 const struct block_device_operations md_fops =
7852 .owner = THIS_MODULE,
7853 .submit_bio = md_submit_bio,
7855 .release = md_release,
7857 #ifdef CONFIG_COMPAT
7858 .compat_ioctl = md_compat_ioctl,
7860 .getgeo = md_getgeo,
7861 .check_events = md_check_events,
7862 .set_read_only = md_set_read_only,
7865 static int md_thread(void *arg)
7867 struct md_thread *thread = arg;
7870 * md_thread is a 'system-thread', it's priority should be very
7871 * high. We avoid resource deadlocks individually in each
7872 * raid personality. (RAID5 does preallocation) We also use RR and
7873 * the very same RT priority as kswapd, thus we will never get
7874 * into a priority inversion deadlock.
7876 * we definitely have to have equal or higher priority than
7877 * bdflush, otherwise bdflush will deadlock if there are too
7878 * many dirty RAID5 blocks.
7881 allow_signal(SIGKILL);
7882 while (!kthread_should_stop()) {
7884 /* We need to wait INTERRUPTIBLE so that
7885 * we don't add to the load-average.
7886 * That means we need to be sure no signals are
7889 if (signal_pending(current))
7890 flush_signals(current);
7892 wait_event_interruptible_timeout
7894 test_bit(THREAD_WAKEUP, &thread->flags)
7895 || kthread_should_stop() || kthread_should_park(),
7898 clear_bit(THREAD_WAKEUP, &thread->flags);
7899 if (kthread_should_park())
7901 if (!kthread_should_stop())
7902 thread->run(thread);
7908 void md_wakeup_thread(struct md_thread *thread)
7911 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
7912 set_bit(THREAD_WAKEUP, &thread->flags);
7913 wake_up(&thread->wqueue);
7916 EXPORT_SYMBOL(md_wakeup_thread);
7918 struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7919 struct mddev *mddev, const char *name)
7921 struct md_thread *thread;
7923 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
7927 init_waitqueue_head(&thread->wqueue);
7930 thread->mddev = mddev;
7931 thread->timeout = MAX_SCHEDULE_TIMEOUT;
7932 thread->tsk = kthread_run(md_thread, thread,
7934 mdname(thread->mddev),
7936 if (IS_ERR(thread->tsk)) {
7942 EXPORT_SYMBOL(md_register_thread);
7944 void md_unregister_thread(struct md_thread **threadp)
7946 struct md_thread *thread = *threadp;
7949 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
7950 /* Locking ensures that mddev_unlock does not wake_up a
7951 * non-existent thread
7953 spin_lock(&pers_lock);
7955 spin_unlock(&pers_lock);
7957 kthread_stop(thread->tsk);
7960 EXPORT_SYMBOL(md_unregister_thread);
7962 void md_error(struct mddev *mddev, struct md_rdev *rdev)
7964 if (!rdev || test_bit(Faulty, &rdev->flags))
7967 if (!mddev->pers || !mddev->pers->error_handler)
7969 mddev->pers->error_handler(mddev,rdev);
7970 if (mddev->degraded)
7971 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7972 sysfs_notify_dirent_safe(rdev->sysfs_state);
7973 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7974 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7975 md_wakeup_thread(mddev->thread);
7976 if (mddev->event_work.func)
7977 queue_work(md_misc_wq, &mddev->event_work);
7978 md_new_event(mddev);
7980 EXPORT_SYMBOL(md_error);
7982 /* seq_file implementation /proc/mdstat */
7984 static void status_unused(struct seq_file *seq)
7987 struct md_rdev *rdev;
7989 seq_printf(seq, "unused devices: ");
7991 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
7992 char b[BDEVNAME_SIZE];
7994 seq_printf(seq, "%s ",
7995 bdevname(rdev->bdev,b));
7998 seq_printf(seq, "<none>");
8000 seq_printf(seq, "\n");
8003 static int status_resync(struct seq_file *seq, struct mddev *mddev)
8005 sector_t max_sectors, resync, res;
8006 unsigned long dt, db = 0;
8007 sector_t rt, curr_mark_cnt, resync_mark_cnt;
8008 int scale, recovery_active;
8009 unsigned int per_milli;
8011 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8012 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
8013 max_sectors = mddev->resync_max_sectors;
8015 max_sectors = mddev->dev_sectors;
8017 resync = mddev->curr_resync;
8019 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
8020 /* Still cleaning up */
8021 resync = max_sectors;
8022 } else if (resync > max_sectors)
8023 resync = max_sectors;
8025 resync -= atomic_read(&mddev->recovery_active);
8028 if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery)) {
8029 struct md_rdev *rdev;
8031 rdev_for_each(rdev, mddev)
8032 if (rdev->raid_disk >= 0 &&
8033 !test_bit(Faulty, &rdev->flags) &&
8034 rdev->recovery_offset != MaxSector &&
8035 rdev->recovery_offset) {
8036 seq_printf(seq, "\trecover=REMOTE");
8039 if (mddev->reshape_position != MaxSector)
8040 seq_printf(seq, "\treshape=REMOTE");
8042 seq_printf(seq, "\tresync=REMOTE");
8045 if (mddev->recovery_cp < MaxSector) {
8046 seq_printf(seq, "\tresync=PENDING");
8052 seq_printf(seq, "\tresync=DELAYED");
8056 WARN_ON(max_sectors == 0);
8057 /* Pick 'scale' such that (resync>>scale)*1000 will fit
8058 * in a sector_t, and (max_sectors>>scale) will fit in a
8059 * u32, as those are the requirements for sector_div.
8060 * Thus 'scale' must be at least 10
8063 if (sizeof(sector_t) > sizeof(unsigned long)) {
8064 while ( max_sectors/2 > (1ULL<<(scale+32)))
8067 res = (resync>>scale)*1000;
8068 sector_div(res, (u32)((max_sectors>>scale)+1));
8072 int i, x = per_milli/50, y = 20-x;
8073 seq_printf(seq, "[");
8074 for (i = 0; i < x; i++)
8075 seq_printf(seq, "=");
8076 seq_printf(seq, ">");
8077 for (i = 0; i < y; i++)
8078 seq_printf(seq, ".");
8079 seq_printf(seq, "] ");
8081 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
8082 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
8084 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
8086 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
8087 "resync" : "recovery"))),
8088 per_milli/10, per_milli % 10,
8089 (unsigned long long) resync/2,
8090 (unsigned long long) max_sectors/2);
8093 * dt: time from mark until now
8094 * db: blocks written from mark until now
8095 * rt: remaining time
8097 * rt is a sector_t, which is always 64bit now. We are keeping
8098 * the original algorithm, but it is not really necessary.
8100 * Original algorithm:
8101 * So we divide before multiply in case it is 32bit and close
8103 * We scale the divisor (db) by 32 to avoid losing precision
8104 * near the end of resync when the number of remaining sectors
8106 * We then divide rt by 32 after multiplying by db to compensate.
8107 * The '+1' avoids division by zero if db is very small.
8109 dt = ((jiffies - mddev->resync_mark) / HZ);
8112 curr_mark_cnt = mddev->curr_mark_cnt;
8113 recovery_active = atomic_read(&mddev->recovery_active);
8114 resync_mark_cnt = mddev->resync_mark_cnt;
8116 if (curr_mark_cnt >= (recovery_active + resync_mark_cnt))
8117 db = curr_mark_cnt - (recovery_active + resync_mark_cnt);
8119 rt = max_sectors - resync; /* number of remaining sectors */
8120 rt = div64_u64(rt, db/32+1);
8124 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
8125 ((unsigned long)rt % 60)/6);
8127 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
8131 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
8133 struct list_head *tmp;
8135 struct mddev *mddev;
8147 spin_lock(&all_mddevs_lock);
8148 list_for_each(tmp,&all_mddevs)
8150 mddev = list_entry(tmp, struct mddev, all_mddevs);
8152 spin_unlock(&all_mddevs_lock);
8155 spin_unlock(&all_mddevs_lock);
8157 return (void*)2;/* tail */
8161 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
8163 struct list_head *tmp;
8164 struct mddev *next_mddev, *mddev = v;
8170 spin_lock(&all_mddevs_lock);
8172 tmp = all_mddevs.next;
8174 tmp = mddev->all_mddevs.next;
8175 if (tmp != &all_mddevs)
8176 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
8178 next_mddev = (void*)2;
8181 spin_unlock(&all_mddevs_lock);
8189 static void md_seq_stop(struct seq_file *seq, void *v)
8191 struct mddev *mddev = v;
8193 if (mddev && v != (void*)1 && v != (void*)2)
8197 static int md_seq_show(struct seq_file *seq, void *v)
8199 struct mddev *mddev = v;
8201 struct md_rdev *rdev;
8203 if (v == (void*)1) {
8204 struct md_personality *pers;
8205 seq_printf(seq, "Personalities : ");
8206 spin_lock(&pers_lock);
8207 list_for_each_entry(pers, &pers_list, list)
8208 seq_printf(seq, "[%s] ", pers->name);
8210 spin_unlock(&pers_lock);
8211 seq_printf(seq, "\n");
8212 seq->poll_event = atomic_read(&md_event_count);
8215 if (v == (void*)2) {
8220 spin_lock(&mddev->lock);
8221 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
8222 seq_printf(seq, "%s : %sactive", mdname(mddev),
8223 mddev->pers ? "" : "in");
8226 seq_printf(seq, " (read-only)");
8228 seq_printf(seq, " (auto-read-only)");
8229 seq_printf(seq, " %s", mddev->pers->name);
8234 rdev_for_each_rcu(rdev, mddev) {
8235 char b[BDEVNAME_SIZE];
8236 seq_printf(seq, " %s[%d]",
8237 bdevname(rdev->bdev,b), rdev->desc_nr);
8238 if (test_bit(WriteMostly, &rdev->flags))
8239 seq_printf(seq, "(W)");
8240 if (test_bit(Journal, &rdev->flags))
8241 seq_printf(seq, "(J)");
8242 if (test_bit(Faulty, &rdev->flags)) {
8243 seq_printf(seq, "(F)");
8246 if (rdev->raid_disk < 0)
8247 seq_printf(seq, "(S)"); /* spare */
8248 if (test_bit(Replacement, &rdev->flags))
8249 seq_printf(seq, "(R)");
8250 sectors += rdev->sectors;
8254 if (!list_empty(&mddev->disks)) {
8256 seq_printf(seq, "\n %llu blocks",
8257 (unsigned long long)
8258 mddev->array_sectors / 2);
8260 seq_printf(seq, "\n %llu blocks",
8261 (unsigned long long)sectors / 2);
8263 if (mddev->persistent) {
8264 if (mddev->major_version != 0 ||
8265 mddev->minor_version != 90) {
8266 seq_printf(seq," super %d.%d",
8267 mddev->major_version,
8268 mddev->minor_version);
8270 } else if (mddev->external)
8271 seq_printf(seq, " super external:%s",
8272 mddev->metadata_type);
8274 seq_printf(seq, " super non-persistent");
8277 mddev->pers->status(seq, mddev);
8278 seq_printf(seq, "\n ");
8279 if (mddev->pers->sync_request) {
8280 if (status_resync(seq, mddev))
8281 seq_printf(seq, "\n ");
8284 seq_printf(seq, "\n ");
8286 md_bitmap_status(seq, mddev->bitmap);
8288 seq_printf(seq, "\n");
8290 spin_unlock(&mddev->lock);
8295 static const struct seq_operations md_seq_ops = {
8296 .start = md_seq_start,
8297 .next = md_seq_next,
8298 .stop = md_seq_stop,
8299 .show = md_seq_show,
8302 static int md_seq_open(struct inode *inode, struct file *file)
8304 struct seq_file *seq;
8307 error = seq_open(file, &md_seq_ops);
8311 seq = file->private_data;
8312 seq->poll_event = atomic_read(&md_event_count);
8316 static int md_unloading;
8317 static __poll_t mdstat_poll(struct file *filp, poll_table *wait)
8319 struct seq_file *seq = filp->private_data;
8323 return EPOLLIN|EPOLLRDNORM|EPOLLERR|EPOLLPRI;
8324 poll_wait(filp, &md_event_waiters, wait);
8326 /* always allow read */
8327 mask = EPOLLIN | EPOLLRDNORM;
8329 if (seq->poll_event != atomic_read(&md_event_count))
8330 mask |= EPOLLERR | EPOLLPRI;
8334 static const struct proc_ops mdstat_proc_ops = {
8335 .proc_open = md_seq_open,
8336 .proc_read = seq_read,
8337 .proc_lseek = seq_lseek,
8338 .proc_release = seq_release,
8339 .proc_poll = mdstat_poll,
8342 int register_md_personality(struct md_personality *p)
8344 pr_debug("md: %s personality registered for level %d\n",
8346 spin_lock(&pers_lock);
8347 list_add_tail(&p->list, &pers_list);
8348 spin_unlock(&pers_lock);
8351 EXPORT_SYMBOL(register_md_personality);
8353 int unregister_md_personality(struct md_personality *p)
8355 pr_debug("md: %s personality unregistered\n", p->name);
8356 spin_lock(&pers_lock);
8357 list_del_init(&p->list);
8358 spin_unlock(&pers_lock);
8361 EXPORT_SYMBOL(unregister_md_personality);
8363 int register_md_cluster_operations(struct md_cluster_operations *ops,
8364 struct module *module)
8367 spin_lock(&pers_lock);
8368 if (md_cluster_ops != NULL)
8371 md_cluster_ops = ops;
8372 md_cluster_mod = module;
8374 spin_unlock(&pers_lock);
8377 EXPORT_SYMBOL(register_md_cluster_operations);
8379 int unregister_md_cluster_operations(void)
8381 spin_lock(&pers_lock);
8382 md_cluster_ops = NULL;
8383 spin_unlock(&pers_lock);
8386 EXPORT_SYMBOL(unregister_md_cluster_operations);
8388 int md_setup_cluster(struct mddev *mddev, int nodes)
8391 if (!md_cluster_ops)
8392 request_module("md-cluster");
8393 spin_lock(&pers_lock);
8394 /* ensure module won't be unloaded */
8395 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
8396 pr_warn("can't find md-cluster module or get it's reference.\n");
8397 spin_unlock(&pers_lock);
8400 spin_unlock(&pers_lock);
8402 ret = md_cluster_ops->join(mddev, nodes);
8404 mddev->safemode_delay = 0;
8408 void md_cluster_stop(struct mddev *mddev)
8410 if (!md_cluster_ops)
8412 md_cluster_ops->leave(mddev);
8413 module_put(md_cluster_mod);
8416 static int is_mddev_idle(struct mddev *mddev, int init)
8418 struct md_rdev *rdev;
8424 rdev_for_each_rcu(rdev, mddev) {
8425 struct gendisk *disk = rdev->bdev->bd_disk;
8426 curr_events = (int)part_stat_read_accum(disk->part0, sectors) -
8427 atomic_read(&disk->sync_io);
8428 /* sync IO will cause sync_io to increase before the disk_stats
8429 * as sync_io is counted when a request starts, and
8430 * disk_stats is counted when it completes.
8431 * So resync activity will cause curr_events to be smaller than
8432 * when there was no such activity.
8433 * non-sync IO will cause disk_stat to increase without
8434 * increasing sync_io so curr_events will (eventually)
8435 * be larger than it was before. Once it becomes
8436 * substantially larger, the test below will cause
8437 * the array to appear non-idle, and resync will slow
8439 * If there is a lot of outstanding resync activity when
8440 * we set last_event to curr_events, then all that activity
8441 * completing might cause the array to appear non-idle
8442 * and resync will be slowed down even though there might
8443 * not have been non-resync activity. This will only
8444 * happen once though. 'last_events' will soon reflect
8445 * the state where there is little or no outstanding
8446 * resync requests, and further resync activity will
8447 * always make curr_events less than last_events.
8450 if (init || curr_events - rdev->last_events > 64) {
8451 rdev->last_events = curr_events;
8459 void md_done_sync(struct mddev *mddev, int blocks, int ok)
8461 /* another "blocks" (512byte) blocks have been synced */
8462 atomic_sub(blocks, &mddev->recovery_active);
8463 wake_up(&mddev->recovery_wait);
8465 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
8466 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
8467 md_wakeup_thread(mddev->thread);
8468 // stop recovery, signal do_sync ....
8471 EXPORT_SYMBOL(md_done_sync);
8473 /* md_write_start(mddev, bi)
8474 * If we need to update some array metadata (e.g. 'active' flag
8475 * in superblock) before writing, schedule a superblock update
8476 * and wait for it to complete.
8477 * A return value of 'false' means that the write wasn't recorded
8478 * and cannot proceed as the array is being suspend.
8480 bool md_write_start(struct mddev *mddev, struct bio *bi)
8484 if (bio_data_dir(bi) != WRITE)
8487 BUG_ON(mddev->ro == 1);
8488 if (mddev->ro == 2) {
8489 /* need to switch to read/write */
8491 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8492 md_wakeup_thread(mddev->thread);
8493 md_wakeup_thread(mddev->sync_thread);
8497 percpu_ref_get(&mddev->writes_pending);
8498 smp_mb(); /* Match smp_mb in set_in_sync() */
8499 if (mddev->safemode == 1)
8500 mddev->safemode = 0;
8501 /* sync_checkers is always 0 when writes_pending is in per-cpu mode */
8502 if (mddev->in_sync || mddev->sync_checkers) {
8503 spin_lock(&mddev->lock);
8504 if (mddev->in_sync) {
8506 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8507 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
8508 md_wakeup_thread(mddev->thread);
8511 spin_unlock(&mddev->lock);
8515 sysfs_notify_dirent_safe(mddev->sysfs_state);
8516 if (!mddev->has_superblocks)
8518 wait_event(mddev->sb_wait,
8519 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags) ||
8521 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
8522 percpu_ref_put(&mddev->writes_pending);
8527 EXPORT_SYMBOL(md_write_start);
8529 /* md_write_inc can only be called when md_write_start() has
8530 * already been called at least once of the current request.
8531 * It increments the counter and is useful when a single request
8532 * is split into several parts. Each part causes an increment and
8533 * so needs a matching md_write_end().
8534 * Unlike md_write_start(), it is safe to call md_write_inc() inside
8535 * a spinlocked region.
8537 void md_write_inc(struct mddev *mddev, struct bio *bi)
8539 if (bio_data_dir(bi) != WRITE)
8541 WARN_ON_ONCE(mddev->in_sync || mddev->ro);
8542 percpu_ref_get(&mddev->writes_pending);
8544 EXPORT_SYMBOL(md_write_inc);
8546 void md_write_end(struct mddev *mddev)
8548 percpu_ref_put(&mddev->writes_pending);
8550 if (mddev->safemode == 2)
8551 md_wakeup_thread(mddev->thread);
8552 else if (mddev->safemode_delay)
8553 /* The roundup() ensures this only performs locking once
8554 * every ->safemode_delay jiffies
8556 mod_timer(&mddev->safemode_timer,
8557 roundup(jiffies, mddev->safemode_delay) +
8558 mddev->safemode_delay);
8561 EXPORT_SYMBOL(md_write_end);
8563 /* This is used by raid0 and raid10 */
8564 void md_submit_discard_bio(struct mddev *mddev, struct md_rdev *rdev,
8565 struct bio *bio, sector_t start, sector_t size)
8567 struct bio *discard_bio = NULL;
8569 if (__blkdev_issue_discard(rdev->bdev, start, size, GFP_NOIO, 0,
8570 &discard_bio) || !discard_bio)
8573 bio_chain(discard_bio, bio);
8574 bio_clone_blkg_association(discard_bio, bio);
8576 trace_block_bio_remap(discard_bio,
8577 disk_devt(mddev->gendisk),
8578 bio->bi_iter.bi_sector);
8579 submit_bio_noacct(discard_bio);
8581 EXPORT_SYMBOL_GPL(md_submit_discard_bio);
8583 static void md_end_io_acct(struct bio *bio)
8585 struct md_io_acct *md_io_acct = bio->bi_private;
8586 struct bio *orig_bio = md_io_acct->orig_bio;
8588 orig_bio->bi_status = bio->bi_status;
8590 bio_end_io_acct(orig_bio, md_io_acct->start_time);
8592 bio_endio(orig_bio);
8596 * Used by personalities that don't already clone the bio and thus can't
8597 * easily add the timestamp to their extended bio structure.
8599 void md_account_bio(struct mddev *mddev, struct bio **bio)
8601 struct md_io_acct *md_io_acct;
8604 if (!blk_queue_io_stat((*bio)->bi_bdev->bd_disk->queue))
8607 clone = bio_clone_fast(*bio, GFP_NOIO, &mddev->io_acct_set);
8608 md_io_acct = container_of(clone, struct md_io_acct, bio_clone);
8609 md_io_acct->orig_bio = *bio;
8610 md_io_acct->start_time = bio_start_io_acct(*bio);
8612 clone->bi_end_io = md_end_io_acct;
8613 clone->bi_private = md_io_acct;
8616 EXPORT_SYMBOL_GPL(md_account_bio);
8618 /* md_allow_write(mddev)
8619 * Calling this ensures that the array is marked 'active' so that writes
8620 * may proceed without blocking. It is important to call this before
8621 * attempting a GFP_KERNEL allocation while holding the mddev lock.
8622 * Must be called with mddev_lock held.
8624 void md_allow_write(struct mddev *mddev)
8630 if (!mddev->pers->sync_request)
8633 spin_lock(&mddev->lock);
8634 if (mddev->in_sync) {
8636 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8637 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
8638 if (mddev->safemode_delay &&
8639 mddev->safemode == 0)
8640 mddev->safemode = 1;
8641 spin_unlock(&mddev->lock);
8642 md_update_sb(mddev, 0);
8643 sysfs_notify_dirent_safe(mddev->sysfs_state);
8644 /* wait for the dirty state to be recorded in the metadata */
8645 wait_event(mddev->sb_wait,
8646 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
8648 spin_unlock(&mddev->lock);
8650 EXPORT_SYMBOL_GPL(md_allow_write);
8652 #define SYNC_MARKS 10
8653 #define SYNC_MARK_STEP (3*HZ)
8654 #define UPDATE_FREQUENCY (5*60*HZ)
8655 void md_do_sync(struct md_thread *thread)
8657 struct mddev *mddev = thread->mddev;
8658 struct mddev *mddev2;
8659 unsigned int currspeed = 0, window;
8660 sector_t max_sectors,j, io_sectors, recovery_done;
8661 unsigned long mark[SYNC_MARKS];
8662 unsigned long update_time;
8663 sector_t mark_cnt[SYNC_MARKS];
8665 struct list_head *tmp;
8666 sector_t last_check;
8668 struct md_rdev *rdev;
8669 char *desc, *action = NULL;
8670 struct blk_plug plug;
8673 /* just incase thread restarts... */
8674 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
8675 test_bit(MD_RECOVERY_WAIT, &mddev->recovery))
8677 if (mddev->ro) {/* never try to sync a read-only array */
8678 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
8682 if (mddev_is_clustered(mddev)) {
8683 ret = md_cluster_ops->resync_start(mddev);
8687 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
8688 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8689 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
8690 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
8691 && ((unsigned long long)mddev->curr_resync_completed
8692 < (unsigned long long)mddev->resync_max_sectors))
8696 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8697 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
8698 desc = "data-check";
8700 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8701 desc = "requested-resync";
8705 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
8710 mddev->last_sync_action = action ?: desc;
8712 /* we overload curr_resync somewhat here.
8713 * 0 == not engaged in resync at all
8714 * 2 == checking that there is no conflict with another sync
8715 * 1 == like 2, but have yielded to allow conflicting resync to
8717 * other == active in resync - this many blocks
8719 * Before starting a resync we must have set curr_resync to
8720 * 2, and then checked that every "conflicting" array has curr_resync
8721 * less than ours. When we find one that is the same or higher
8722 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8723 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8724 * This will mean we have to start checking from the beginning again.
8729 int mddev2_minor = -1;
8730 mddev->curr_resync = 2;
8733 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8735 for_each_mddev(mddev2, tmp) {
8736 if (mddev2 == mddev)
8738 if (!mddev->parallel_resync
8739 && mddev2->curr_resync
8740 && match_mddev_units(mddev, mddev2)) {
8742 if (mddev < mddev2 && mddev->curr_resync == 2) {
8743 /* arbitrarily yield */
8744 mddev->curr_resync = 1;
8745 wake_up(&resync_wait);
8747 if (mddev > mddev2 && mddev->curr_resync == 1)
8748 /* no need to wait here, we can wait the next
8749 * time 'round when curr_resync == 2
8752 /* We need to wait 'interruptible' so as not to
8753 * contribute to the load average, and not to
8754 * be caught by 'softlockup'
8756 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
8757 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8758 mddev2->curr_resync >= mddev->curr_resync) {
8759 if (mddev2_minor != mddev2->md_minor) {
8760 mddev2_minor = mddev2->md_minor;
8761 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8762 desc, mdname(mddev),
8766 if (signal_pending(current))
8767 flush_signals(current);
8769 finish_wait(&resync_wait, &wq);
8772 finish_wait(&resync_wait, &wq);
8775 } while (mddev->curr_resync < 2);
8778 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8779 /* resync follows the size requested by the personality,
8780 * which defaults to physical size, but can be virtual size
8782 max_sectors = mddev->resync_max_sectors;
8783 atomic64_set(&mddev->resync_mismatches, 0);
8784 /* we don't use the checkpoint if there's a bitmap */
8785 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8786 j = mddev->resync_min;
8787 else if (!mddev->bitmap)
8788 j = mddev->recovery_cp;
8790 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
8791 max_sectors = mddev->resync_max_sectors;
8793 * If the original node aborts reshaping then we continue the
8794 * reshaping, so set j again to avoid restart reshape from the
8797 if (mddev_is_clustered(mddev) &&
8798 mddev->reshape_position != MaxSector)
8799 j = mddev->reshape_position;
8801 /* recovery follows the physical size of devices */
8802 max_sectors = mddev->dev_sectors;
8805 rdev_for_each_rcu(rdev, mddev)
8806 if (rdev->raid_disk >= 0 &&
8807 !test_bit(Journal, &rdev->flags) &&
8808 !test_bit(Faulty, &rdev->flags) &&
8809 !test_bit(In_sync, &rdev->flags) &&
8810 rdev->recovery_offset < j)
8811 j = rdev->recovery_offset;
8814 /* If there is a bitmap, we need to make sure all
8815 * writes that started before we added a spare
8816 * complete before we start doing a recovery.
8817 * Otherwise the write might complete and (via
8818 * bitmap_endwrite) set a bit in the bitmap after the
8819 * recovery has checked that bit and skipped that
8822 if (mddev->bitmap) {
8823 mddev->pers->quiesce(mddev, 1);
8824 mddev->pers->quiesce(mddev, 0);
8828 pr_info("md: %s of RAID array %s\n", desc, mdname(mddev));
8829 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev));
8830 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8831 speed_max(mddev), desc);
8833 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
8836 for (m = 0; m < SYNC_MARKS; m++) {
8838 mark_cnt[m] = io_sectors;
8841 mddev->resync_mark = mark[last_mark];
8842 mddev->resync_mark_cnt = mark_cnt[last_mark];
8845 * Tune reconstruction:
8847 window = 32 * (PAGE_SIZE / 512);
8848 pr_debug("md: using %dk window, over a total of %lluk.\n",
8849 window/2, (unsigned long long)max_sectors/2);
8851 atomic_set(&mddev->recovery_active, 0);
8855 pr_debug("md: resuming %s of %s from checkpoint.\n",
8856 desc, mdname(mddev));
8857 mddev->curr_resync = j;
8859 mddev->curr_resync = 3; /* no longer delayed */
8860 mddev->curr_resync_completed = j;
8861 sysfs_notify_dirent_safe(mddev->sysfs_completed);
8862 md_new_event(mddev);
8863 update_time = jiffies;
8865 blk_start_plug(&plug);
8866 while (j < max_sectors) {
8871 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8872 ((mddev->curr_resync > mddev->curr_resync_completed &&
8873 (mddev->curr_resync - mddev->curr_resync_completed)
8874 > (max_sectors >> 4)) ||
8875 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
8876 (j - mddev->curr_resync_completed)*2
8877 >= mddev->resync_max - mddev->curr_resync_completed ||
8878 mddev->curr_resync_completed > mddev->resync_max
8880 /* time to update curr_resync_completed */
8881 wait_event(mddev->recovery_wait,
8882 atomic_read(&mddev->recovery_active) == 0);
8883 mddev->curr_resync_completed = j;
8884 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8885 j > mddev->recovery_cp)
8886 mddev->recovery_cp = j;
8887 update_time = jiffies;
8888 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8889 sysfs_notify_dirent_safe(mddev->sysfs_completed);
8892 while (j >= mddev->resync_max &&
8893 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8894 /* As this condition is controlled by user-space,
8895 * we can block indefinitely, so use '_interruptible'
8896 * to avoid triggering warnings.
8898 flush_signals(current); /* just in case */
8899 wait_event_interruptible(mddev->recovery_wait,
8900 mddev->resync_max > j
8901 || test_bit(MD_RECOVERY_INTR,
8905 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8908 sectors = mddev->pers->sync_request(mddev, j, &skipped);
8910 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
8914 if (!skipped) { /* actual IO requested */
8915 io_sectors += sectors;
8916 atomic_add(sectors, &mddev->recovery_active);
8919 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8923 if (j > max_sectors)
8924 /* when skipping, extra large numbers can be returned. */
8927 mddev->curr_resync = j;
8928 mddev->curr_mark_cnt = io_sectors;
8929 if (last_check == 0)
8930 /* this is the earliest that rebuild will be
8931 * visible in /proc/mdstat
8933 md_new_event(mddev);
8935 if (last_check + window > io_sectors || j == max_sectors)
8938 last_check = io_sectors;
8940 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8942 int next = (last_mark+1) % SYNC_MARKS;
8944 mddev->resync_mark = mark[next];
8945 mddev->resync_mark_cnt = mark_cnt[next];
8946 mark[next] = jiffies;
8947 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
8951 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8955 * this loop exits only if either when we are slower than
8956 * the 'hard' speed limit, or the system was IO-idle for
8958 * the system might be non-idle CPU-wise, but we only care
8959 * about not overloading the IO subsystem. (things like an
8960 * e2fsck being done on the RAID array should execute fast)
8964 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8965 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
8966 /((jiffies-mddev->resync_mark)/HZ +1) +1;
8968 if (currspeed > speed_min(mddev)) {
8969 if (currspeed > speed_max(mddev)) {
8973 if (!is_mddev_idle(mddev, 0)) {
8975 * Give other IO more of a chance.
8976 * The faster the devices, the less we wait.
8978 wait_event(mddev->recovery_wait,
8979 !atomic_read(&mddev->recovery_active));
8983 pr_info("md: %s: %s %s.\n",mdname(mddev), desc,
8984 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8985 ? "interrupted" : "done");
8987 * this also signals 'finished resyncing' to md_stop
8989 blk_finish_plug(&plug);
8990 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8992 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8993 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8994 mddev->curr_resync > 3) {
8995 mddev->curr_resync_completed = mddev->curr_resync;
8996 sysfs_notify_dirent_safe(mddev->sysfs_completed);
8998 mddev->pers->sync_request(mddev, max_sectors, &skipped);
9000 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
9001 mddev->curr_resync > 3) {
9002 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
9003 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
9004 if (mddev->curr_resync >= mddev->recovery_cp) {
9005 pr_debug("md: checkpointing %s of %s.\n",
9006 desc, mdname(mddev));
9007 if (test_bit(MD_RECOVERY_ERROR,
9009 mddev->recovery_cp =
9010 mddev->curr_resync_completed;
9012 mddev->recovery_cp =
9016 mddev->recovery_cp = MaxSector;
9018 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
9019 mddev->curr_resync = MaxSector;
9020 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
9021 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) {
9023 rdev_for_each_rcu(rdev, mddev)
9024 if (rdev->raid_disk >= 0 &&
9025 mddev->delta_disks >= 0 &&
9026 !test_bit(Journal, &rdev->flags) &&
9027 !test_bit(Faulty, &rdev->flags) &&
9028 !test_bit(In_sync, &rdev->flags) &&
9029 rdev->recovery_offset < mddev->curr_resync)
9030 rdev->recovery_offset = mddev->curr_resync;
9036 /* set CHANGE_PENDING here since maybe another update is needed,
9037 * so other nodes are informed. It should be harmless for normal
9039 set_mask_bits(&mddev->sb_flags, 0,
9040 BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS));
9042 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
9043 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
9044 mddev->delta_disks > 0 &&
9045 mddev->pers->finish_reshape &&
9046 mddev->pers->size &&
9048 mddev_lock_nointr(mddev);
9049 md_set_array_sectors(mddev, mddev->pers->size(mddev, 0, 0));
9050 mddev_unlock(mddev);
9051 if (!mddev_is_clustered(mddev))
9052 set_capacity_and_notify(mddev->gendisk,
9053 mddev->array_sectors);
9056 spin_lock(&mddev->lock);
9057 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
9058 /* We completed so min/max setting can be forgotten if used. */
9059 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
9060 mddev->resync_min = 0;
9061 mddev->resync_max = MaxSector;
9062 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
9063 mddev->resync_min = mddev->curr_resync_completed;
9064 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
9065 mddev->curr_resync = 0;
9066 spin_unlock(&mddev->lock);
9068 wake_up(&resync_wait);
9069 md_wakeup_thread(mddev->thread);
9072 EXPORT_SYMBOL_GPL(md_do_sync);
9074 static int remove_and_add_spares(struct mddev *mddev,
9075 struct md_rdev *this)
9077 struct md_rdev *rdev;
9080 bool remove_some = false;
9082 if (this && test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
9083 /* Mustn't remove devices when resync thread is running */
9086 rdev_for_each(rdev, mddev) {
9087 if ((this == NULL || rdev == this) &&
9088 rdev->raid_disk >= 0 &&
9089 !test_bit(Blocked, &rdev->flags) &&
9090 test_bit(Faulty, &rdev->flags) &&
9091 atomic_read(&rdev->nr_pending)==0) {
9092 /* Faulty non-Blocked devices with nr_pending == 0
9093 * never get nr_pending incremented,
9094 * never get Faulty cleared, and never get Blocked set.
9095 * So we can synchronize_rcu now rather than once per device
9098 set_bit(RemoveSynchronized, &rdev->flags);
9104 rdev_for_each(rdev, mddev) {
9105 if ((this == NULL || rdev == this) &&
9106 rdev->raid_disk >= 0 &&
9107 !test_bit(Blocked, &rdev->flags) &&
9108 ((test_bit(RemoveSynchronized, &rdev->flags) ||
9109 (!test_bit(In_sync, &rdev->flags) &&
9110 !test_bit(Journal, &rdev->flags))) &&
9111 atomic_read(&rdev->nr_pending)==0)) {
9112 if (mddev->pers->hot_remove_disk(
9113 mddev, rdev) == 0) {
9114 sysfs_unlink_rdev(mddev, rdev);
9115 rdev->saved_raid_disk = rdev->raid_disk;
9116 rdev->raid_disk = -1;
9120 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
9121 clear_bit(RemoveSynchronized, &rdev->flags);
9124 if (removed && mddev->kobj.sd)
9125 sysfs_notify_dirent_safe(mddev->sysfs_degraded);
9127 if (this && removed)
9130 rdev_for_each(rdev, mddev) {
9131 if (this && this != rdev)
9133 if (test_bit(Candidate, &rdev->flags))
9135 if (rdev->raid_disk >= 0 &&
9136 !test_bit(In_sync, &rdev->flags) &&
9137 !test_bit(Journal, &rdev->flags) &&
9138 !test_bit(Faulty, &rdev->flags))
9140 if (rdev->raid_disk >= 0)
9142 if (test_bit(Faulty, &rdev->flags))
9144 if (!test_bit(Journal, &rdev->flags)) {
9146 ! (rdev->saved_raid_disk >= 0 &&
9147 !test_bit(Bitmap_sync, &rdev->flags)))
9150 rdev->recovery_offset = 0;
9152 if (mddev->pers->hot_add_disk(mddev, rdev) == 0) {
9153 /* failure here is OK */
9154 sysfs_link_rdev(mddev, rdev);
9155 if (!test_bit(Journal, &rdev->flags))
9157 md_new_event(mddev);
9158 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
9163 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
9167 static void md_start_sync(struct work_struct *ws)
9169 struct mddev *mddev = container_of(ws, struct mddev, del_work);
9171 mddev->sync_thread = md_register_thread(md_do_sync,
9174 if (!mddev->sync_thread) {
9175 pr_warn("%s: could not start resync thread...\n",
9177 /* leave the spares where they are, it shouldn't hurt */
9178 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9179 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
9180 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
9181 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
9182 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
9183 wake_up(&resync_wait);
9184 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
9186 if (mddev->sysfs_action)
9187 sysfs_notify_dirent_safe(mddev->sysfs_action);
9189 md_wakeup_thread(mddev->sync_thread);
9190 sysfs_notify_dirent_safe(mddev->sysfs_action);
9191 md_new_event(mddev);
9195 * This routine is regularly called by all per-raid-array threads to
9196 * deal with generic issues like resync and super-block update.
9197 * Raid personalities that don't have a thread (linear/raid0) do not
9198 * need this as they never do any recovery or update the superblock.
9200 * It does not do any resync itself, but rather "forks" off other threads
9201 * to do that as needed.
9202 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
9203 * "->recovery" and create a thread at ->sync_thread.
9204 * When the thread finishes it sets MD_RECOVERY_DONE
9205 * and wakeups up this thread which will reap the thread and finish up.
9206 * This thread also removes any faulty devices (with nr_pending == 0).
9208 * The overall approach is:
9209 * 1/ if the superblock needs updating, update it.
9210 * 2/ If a recovery thread is running, don't do anything else.
9211 * 3/ If recovery has finished, clean up, possibly marking spares active.
9212 * 4/ If there are any faulty devices, remove them.
9213 * 5/ If array is degraded, try to add spares devices
9214 * 6/ If array has spares or is not in-sync, start a resync thread.
9216 void md_check_recovery(struct mddev *mddev)
9218 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags) && mddev->sb_flags) {
9219 /* Write superblock - thread that called mddev_suspend()
9220 * holds reconfig_mutex for us.
9222 set_bit(MD_UPDATING_SB, &mddev->flags);
9223 smp_mb__after_atomic();
9224 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags))
9225 md_update_sb(mddev, 0);
9226 clear_bit_unlock(MD_UPDATING_SB, &mddev->flags);
9227 wake_up(&mddev->sb_wait);
9230 if (mddev->suspended)
9234 md_bitmap_daemon_work(mddev);
9236 if (signal_pending(current)) {
9237 if (mddev->pers->sync_request && !mddev->external) {
9238 pr_debug("md: %s in immediate safe mode\n",
9240 mddev->safemode = 2;
9242 flush_signals(current);
9245 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
9248 (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
9249 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
9250 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
9251 (mddev->external == 0 && mddev->safemode == 1) ||
9252 (mddev->safemode == 2
9253 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
9257 if (mddev_trylock(mddev)) {
9259 bool try_set_sync = mddev->safemode != 0;
9261 if (!mddev->external && mddev->safemode == 1)
9262 mddev->safemode = 0;
9265 struct md_rdev *rdev;
9266 if (!mddev->external && mddev->in_sync)
9267 /* 'Blocked' flag not needed as failed devices
9268 * will be recorded if array switched to read/write.
9269 * Leaving it set will prevent the device
9270 * from being removed.
9272 rdev_for_each(rdev, mddev)
9273 clear_bit(Blocked, &rdev->flags);
9274 /* On a read-only array we can:
9275 * - remove failed devices
9276 * - add already-in_sync devices if the array itself
9278 * As we only add devices that are already in-sync,
9279 * we can activate the spares immediately.
9281 remove_and_add_spares(mddev, NULL);
9282 /* There is no thread, but we need to call
9283 * ->spare_active and clear saved_raid_disk
9285 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
9286 md_reap_sync_thread(mddev);
9287 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
9288 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9289 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
9293 if (mddev_is_clustered(mddev)) {
9294 struct md_rdev *rdev, *tmp;
9295 /* kick the device if another node issued a
9298 rdev_for_each_safe(rdev, tmp, mddev) {
9299 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
9300 rdev->raid_disk < 0)
9301 md_kick_rdev_from_array(rdev);
9305 if (try_set_sync && !mddev->external && !mddev->in_sync) {
9306 spin_lock(&mddev->lock);
9308 spin_unlock(&mddev->lock);
9311 if (mddev->sb_flags)
9312 md_update_sb(mddev, 0);
9314 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
9315 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
9316 /* resync/recovery still happening */
9317 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9320 if (mddev->sync_thread) {
9321 md_reap_sync_thread(mddev);
9324 /* Set RUNNING before clearing NEEDED to avoid
9325 * any transients in the value of "sync_action".
9327 mddev->curr_resync_completed = 0;
9328 spin_lock(&mddev->lock);
9329 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
9330 spin_unlock(&mddev->lock);
9331 /* Clear some bits that don't mean anything, but
9334 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
9335 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
9337 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
9338 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
9340 /* no recovery is running.
9341 * remove any failed drives, then
9342 * add spares if possible.
9343 * Spares are also removed and re-added, to allow
9344 * the personality to fail the re-add.
9347 if (mddev->reshape_position != MaxSector) {
9348 if (mddev->pers->check_reshape == NULL ||
9349 mddev->pers->check_reshape(mddev) != 0)
9350 /* Cannot proceed */
9352 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
9353 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
9354 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
9355 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9356 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
9357 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
9358 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
9359 } else if (mddev->recovery_cp < MaxSector) {
9360 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9361 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
9362 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
9363 /* nothing to be done ... */
9366 if (mddev->pers->sync_request) {
9368 /* We are adding a device or devices to an array
9369 * which has the bitmap stored on all devices.
9370 * So make sure all bitmap pages get written
9372 md_bitmap_write_all(mddev->bitmap);
9374 INIT_WORK(&mddev->del_work, md_start_sync);
9375 queue_work(md_misc_wq, &mddev->del_work);
9379 if (!mddev->sync_thread) {
9380 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
9381 wake_up(&resync_wait);
9382 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
9384 if (mddev->sysfs_action)
9385 sysfs_notify_dirent_safe(mddev->sysfs_action);
9388 wake_up(&mddev->sb_wait);
9389 mddev_unlock(mddev);
9392 EXPORT_SYMBOL(md_check_recovery);
9394 void md_reap_sync_thread(struct mddev *mddev)
9396 struct md_rdev *rdev;
9397 sector_t old_dev_sectors = mddev->dev_sectors;
9398 bool is_reshaped = false;
9400 /* resync has finished, collect result */
9401 md_unregister_thread(&mddev->sync_thread);
9402 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
9403 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
9404 mddev->degraded != mddev->raid_disks) {
9406 /* activate any spares */
9407 if (mddev->pers->spare_active(mddev)) {
9408 sysfs_notify_dirent_safe(mddev->sysfs_degraded);
9409 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
9412 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
9413 mddev->pers->finish_reshape) {
9414 mddev->pers->finish_reshape(mddev);
9415 if (mddev_is_clustered(mddev))
9419 /* If array is no-longer degraded, then any saved_raid_disk
9420 * information must be scrapped.
9422 if (!mddev->degraded)
9423 rdev_for_each(rdev, mddev)
9424 rdev->saved_raid_disk = -1;
9426 md_update_sb(mddev, 1);
9427 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
9428 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
9430 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
9431 md_cluster_ops->resync_finish(mddev);
9432 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
9433 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
9434 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9435 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
9436 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
9437 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
9439 * We call md_cluster_ops->update_size here because sync_size could
9440 * be changed by md_update_sb, and MD_RECOVERY_RESHAPE is cleared,
9441 * so it is time to update size across cluster.
9443 if (mddev_is_clustered(mddev) && is_reshaped
9444 && !test_bit(MD_CLOSING, &mddev->flags))
9445 md_cluster_ops->update_size(mddev, old_dev_sectors);
9446 wake_up(&resync_wait);
9447 /* flag recovery needed just to double check */
9448 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9449 sysfs_notify_dirent_safe(mddev->sysfs_action);
9450 md_new_event(mddev);
9451 if (mddev->event_work.func)
9452 queue_work(md_misc_wq, &mddev->event_work);
9454 EXPORT_SYMBOL(md_reap_sync_thread);
9456 void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
9458 sysfs_notify_dirent_safe(rdev->sysfs_state);
9459 wait_event_timeout(rdev->blocked_wait,
9460 !test_bit(Blocked, &rdev->flags) &&
9461 !test_bit(BlockedBadBlocks, &rdev->flags),
9462 msecs_to_jiffies(5000));
9463 rdev_dec_pending(rdev, mddev);
9465 EXPORT_SYMBOL(md_wait_for_blocked_rdev);
9467 void md_finish_reshape(struct mddev *mddev)
9469 /* called be personality module when reshape completes. */
9470 struct md_rdev *rdev;
9472 rdev_for_each(rdev, mddev) {
9473 if (rdev->data_offset > rdev->new_data_offset)
9474 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
9476 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
9477 rdev->data_offset = rdev->new_data_offset;
9480 EXPORT_SYMBOL(md_finish_reshape);
9482 /* Bad block management */
9484 /* Returns 1 on success, 0 on failure */
9485 int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
9488 struct mddev *mddev = rdev->mddev;
9491 s += rdev->new_data_offset;
9493 s += rdev->data_offset;
9494 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
9496 /* Make sure they get written out promptly */
9497 if (test_bit(ExternalBbl, &rdev->flags))
9498 sysfs_notify_dirent_safe(rdev->sysfs_unack_badblocks);
9499 sysfs_notify_dirent_safe(rdev->sysfs_state);
9500 set_mask_bits(&mddev->sb_flags, 0,
9501 BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING));
9502 md_wakeup_thread(rdev->mddev->thread);
9507 EXPORT_SYMBOL_GPL(rdev_set_badblocks);
9509 int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
9514 s += rdev->new_data_offset;
9516 s += rdev->data_offset;
9517 rv = badblocks_clear(&rdev->badblocks, s, sectors);
9518 if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags))
9519 sysfs_notify_dirent_safe(rdev->sysfs_badblocks);
9522 EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
9524 static int md_notify_reboot(struct notifier_block *this,
9525 unsigned long code, void *x)
9527 struct list_head *tmp;
9528 struct mddev *mddev;
9531 for_each_mddev(mddev, tmp) {
9532 if (mddev_trylock(mddev)) {
9534 __md_stop_writes(mddev);
9535 if (mddev->persistent)
9536 mddev->safemode = 2;
9537 mddev_unlock(mddev);
9542 * certain more exotic SCSI devices are known to be
9543 * volatile wrt too early system reboots. While the
9544 * right place to handle this issue is the given
9545 * driver, we do want to have a safe RAID driver ...
9553 static struct notifier_block md_notifier = {
9554 .notifier_call = md_notify_reboot,
9556 .priority = INT_MAX, /* before any real devices */
9559 static void md_geninit(void)
9561 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
9563 proc_create("mdstat", S_IRUGO, NULL, &mdstat_proc_ops);
9566 static int __init md_init(void)
9570 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
9574 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
9578 md_rdev_misc_wq = alloc_workqueue("md_rdev_misc", 0, 0);
9579 if (!md_rdev_misc_wq)
9580 goto err_rdev_misc_wq;
9582 ret = __register_blkdev(MD_MAJOR, "md", md_probe);
9586 ret = __register_blkdev(0, "mdp", md_probe);
9591 register_reboot_notifier(&md_notifier);
9592 raid_table_header = register_sysctl_table(raid_root_table);
9598 unregister_blkdev(MD_MAJOR, "md");
9600 destroy_workqueue(md_rdev_misc_wq);
9602 destroy_workqueue(md_misc_wq);
9604 destroy_workqueue(md_wq);
9609 static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
9611 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
9612 struct md_rdev *rdev2, *tmp;
9614 char b[BDEVNAME_SIZE];
9617 * If size is changed in another node then we need to
9618 * do resize as well.
9620 if (mddev->dev_sectors != le64_to_cpu(sb->size)) {
9621 ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size));
9623 pr_info("md-cluster: resize failed\n");
9625 md_bitmap_update_sb(mddev->bitmap);
9628 /* Check for change of roles in the active devices */
9629 rdev_for_each_safe(rdev2, tmp, mddev) {
9630 if (test_bit(Faulty, &rdev2->flags))
9633 /* Check if the roles changed */
9634 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
9636 if (test_bit(Candidate, &rdev2->flags)) {
9637 if (role == 0xfffe) {
9638 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
9639 md_kick_rdev_from_array(rdev2);
9643 clear_bit(Candidate, &rdev2->flags);
9646 if (role != rdev2->raid_disk) {
9648 * got activated except reshape is happening.
9650 if (rdev2->raid_disk == -1 && role != 0xffff &&
9651 !(le32_to_cpu(sb->feature_map) &
9652 MD_FEATURE_RESHAPE_ACTIVE)) {
9653 rdev2->saved_raid_disk = role;
9654 ret = remove_and_add_spares(mddev, rdev2);
9655 pr_info("Activated spare: %s\n",
9656 bdevname(rdev2->bdev,b));
9657 /* wakeup mddev->thread here, so array could
9658 * perform resync with the new activated disk */
9659 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9660 md_wakeup_thread(mddev->thread);
9663 * We just want to do the minimum to mark the disk
9664 * as faulty. The recovery is performed by the
9665 * one who initiated the error.
9667 if ((role == 0xfffe) || (role == 0xfffd)) {
9668 md_error(mddev, rdev2);
9669 clear_bit(Blocked, &rdev2->flags);
9674 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) {
9675 ret = update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
9677 pr_warn("md: updating array disks failed. %d\n", ret);
9681 * Since mddev->delta_disks has already updated in update_raid_disks,
9682 * so it is time to check reshape.
9684 if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
9685 (le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
9687 * reshape is happening in the remote node, we need to
9688 * update reshape_position and call start_reshape.
9690 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
9691 if (mddev->pers->update_reshape_pos)
9692 mddev->pers->update_reshape_pos(mddev);
9693 if (mddev->pers->start_reshape)
9694 mddev->pers->start_reshape(mddev);
9695 } else if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
9696 mddev->reshape_position != MaxSector &&
9697 !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
9698 /* reshape is just done in another node. */
9699 mddev->reshape_position = MaxSector;
9700 if (mddev->pers->update_reshape_pos)
9701 mddev->pers->update_reshape_pos(mddev);
9704 /* Finally set the event to be up to date */
9705 mddev->events = le64_to_cpu(sb->events);
9708 static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
9711 struct page *swapout = rdev->sb_page;
9712 struct mdp_superblock_1 *sb;
9714 /* Store the sb page of the rdev in the swapout temporary
9715 * variable in case we err in the future
9717 rdev->sb_page = NULL;
9718 err = alloc_disk_sb(rdev);
9720 ClearPageUptodate(rdev->sb_page);
9721 rdev->sb_loaded = 0;
9722 err = super_types[mddev->major_version].
9723 load_super(rdev, NULL, mddev->minor_version);
9726 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9727 __func__, __LINE__, rdev->desc_nr, err);
9729 put_page(rdev->sb_page);
9730 rdev->sb_page = swapout;
9731 rdev->sb_loaded = 1;
9735 sb = page_address(rdev->sb_page);
9736 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9740 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
9741 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
9743 /* The other node finished recovery, call spare_active to set
9744 * device In_sync and mddev->degraded
9746 if (rdev->recovery_offset == MaxSector &&
9747 !test_bit(In_sync, &rdev->flags) &&
9748 mddev->pers->spare_active(mddev))
9749 sysfs_notify_dirent_safe(mddev->sysfs_degraded);
9755 void md_reload_sb(struct mddev *mddev, int nr)
9757 struct md_rdev *rdev;
9761 rdev_for_each_rcu(rdev, mddev) {
9762 if (rdev->desc_nr == nr)
9766 if (!rdev || rdev->desc_nr != nr) {
9767 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
9771 err = read_rdev(mddev, rdev);
9775 check_sb_changes(mddev, rdev);
9777 /* Read all rdev's to update recovery_offset */
9778 rdev_for_each_rcu(rdev, mddev) {
9779 if (!test_bit(Faulty, &rdev->flags))
9780 read_rdev(mddev, rdev);
9783 EXPORT_SYMBOL(md_reload_sb);
9788 * Searches all registered partitions for autorun RAID arrays
9792 static DEFINE_MUTEX(detected_devices_mutex);
9793 static LIST_HEAD(all_detected_devices);
9794 struct detected_devices_node {
9795 struct list_head list;
9799 void md_autodetect_dev(dev_t dev)
9801 struct detected_devices_node *node_detected_dev;
9803 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
9804 if (node_detected_dev) {
9805 node_detected_dev->dev = dev;
9806 mutex_lock(&detected_devices_mutex);
9807 list_add_tail(&node_detected_dev->list, &all_detected_devices);
9808 mutex_unlock(&detected_devices_mutex);
9812 void md_autostart_arrays(int part)
9814 struct md_rdev *rdev;
9815 struct detected_devices_node *node_detected_dev;
9817 int i_scanned, i_passed;
9822 pr_info("md: Autodetecting RAID arrays.\n");
9824 mutex_lock(&detected_devices_mutex);
9825 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
9827 node_detected_dev = list_entry(all_detected_devices.next,
9828 struct detected_devices_node, list);
9829 list_del(&node_detected_dev->list);
9830 dev = node_detected_dev->dev;
9831 kfree(node_detected_dev);
9832 mutex_unlock(&detected_devices_mutex);
9833 rdev = md_import_device(dev,0, 90);
9834 mutex_lock(&detected_devices_mutex);
9838 if (test_bit(Faulty, &rdev->flags))
9841 set_bit(AutoDetected, &rdev->flags);
9842 list_add(&rdev->same_set, &pending_raid_disks);
9845 mutex_unlock(&detected_devices_mutex);
9847 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed);
9849 autorun_devices(part);
9852 #endif /* !MODULE */
9854 static __exit void md_exit(void)
9856 struct mddev *mddev;
9857 struct list_head *tmp;
9860 unregister_blkdev(MD_MAJOR,"md");
9861 unregister_blkdev(mdp_major, "mdp");
9862 unregister_reboot_notifier(&md_notifier);
9863 unregister_sysctl_table(raid_table_header);
9865 /* We cannot unload the modules while some process is
9866 * waiting for us in select() or poll() - wake them up
9869 while (waitqueue_active(&md_event_waiters)) {
9870 /* not safe to leave yet */
9871 wake_up(&md_event_waiters);
9875 remove_proc_entry("mdstat", NULL);
9877 for_each_mddev(mddev, tmp) {
9878 export_array(mddev);
9880 mddev->hold_active = 0;
9882 * for_each_mddev() will call mddev_put() at the end of each
9883 * iteration. As the mddev is now fully clear, this will
9884 * schedule the mddev for destruction by a workqueue, and the
9885 * destroy_workqueue() below will wait for that to complete.
9888 destroy_workqueue(md_rdev_misc_wq);
9889 destroy_workqueue(md_misc_wq);
9890 destroy_workqueue(md_wq);
9893 subsys_initcall(md_init);
9894 module_exit(md_exit)
9896 static int get_ro(char *buffer, const struct kernel_param *kp)
9898 return sprintf(buffer, "%d\n", start_readonly);
9900 static int set_ro(const char *val, const struct kernel_param *kp)
9902 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
9905 module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9906 module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
9907 module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
9908 module_param(create_on_open, bool, S_IRUSR|S_IWUSR);
9910 MODULE_LICENSE("GPL");
9911 MODULE_DESCRIPTION("MD RAID framework");
9913 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);