1 // SPDX-License-Identifier: GPL-2.0-or-later
3 md.c : Multiple Devices driver for Linux
4 Copyright (C) 1998, 1999, 2000 Ingo Molnar
6 completely rewritten, based on the MD driver code from Marc Zyngier
10 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
11 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
12 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
13 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
14 - kmod support by: Cyrus Durgin
15 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
16 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
18 - lots of fixes and improvements to the RAID1/RAID5 and generic
19 RAID code (such as request based resynchronization):
21 Neil Brown <neilb@cse.unsw.edu.au>.
23 - persistent bitmap code
24 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
27 Errors, Warnings, etc.
29 pr_crit() for error conditions that risk data loss
30 pr_err() for error conditions that are unexpected, like an IO error
31 or internal inconsistency
32 pr_warn() for error conditions that could have been predicated, like
33 adding a device to an array when it has incompatible metadata
34 pr_info() for every interesting, very rare events, like an array starting
35 or stopping, or resync starting or stopping
36 pr_debug() for everything else.
40 #include <linux/sched/mm.h>
41 #include <linux/sched/signal.h>
42 #include <linux/kthread.h>
43 #include <linux/blkdev.h>
44 #include <linux/badblocks.h>
45 #include <linux/sysctl.h>
46 #include <linux/seq_file.h>
48 #include <linux/poll.h>
49 #include <linux/ctype.h>
50 #include <linux/string.h>
51 #include <linux/hdreg.h>
52 #include <linux/proc_fs.h>
53 #include <linux/random.h>
54 #include <linux/module.h>
55 #include <linux/reboot.h>
56 #include <linux/file.h>
57 #include <linux/compat.h>
58 #include <linux/delay.h>
59 #include <linux/raid/md_p.h>
60 #include <linux/raid/md_u.h>
61 #include <linux/slab.h>
62 #include <linux/percpu-refcount.h>
64 #include <trace/events/block.h>
66 #include "md-bitmap.h"
67 #include "md-cluster.h"
70 static void autostart_arrays(int part);
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;
91 static int remove_and_add_spares(struct mddev *mddev,
92 struct md_rdev *this);
93 static void mddev_detach(struct mddev *mddev);
96 * Default number of read corrections we'll attempt on an rdev
97 * before ejecting it from the array. We divide the read error
98 * count by 2 for every hour elapsed between read errors.
100 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
102 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
103 * is 1000 KB/sec, so the extra system load does not show up that much.
104 * Increase it if you want to have more _guaranteed_ speed. Note that
105 * the RAID driver will use the maximum available bandwidth if the IO
106 * subsystem is idle. There is also an 'absolute maximum' reconstruction
107 * speed limit - in case reconstruction slows down your system despite
110 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
111 * or /sys/block/mdX/md/sync_speed_{min,max}
114 static int sysctl_speed_limit_min = 1000;
115 static int sysctl_speed_limit_max = 200000;
116 static inline int speed_min(struct mddev *mddev)
118 return mddev->sync_speed_min ?
119 mddev->sync_speed_min : sysctl_speed_limit_min;
122 static inline int speed_max(struct mddev *mddev)
124 return mddev->sync_speed_max ?
125 mddev->sync_speed_max : sysctl_speed_limit_max;
128 static int rdev_init_wb(struct md_rdev *rdev)
130 if (rdev->bdev->bd_queue->nr_hw_queues == 1)
133 spin_lock_init(&rdev->wb_list_lock);
134 INIT_LIST_HEAD(&rdev->wb_list);
135 init_waitqueue_head(&rdev->wb_io_wait);
136 set_bit(WBCollisionCheck, &rdev->flags);
142 * Create wb_info_pool if rdev is the first multi-queue device flaged
143 * with writemostly, also write-behind mode is enabled.
145 void mddev_create_wb_pool(struct mddev *mddev, struct md_rdev *rdev,
148 if (mddev->bitmap_info.max_write_behind == 0)
151 if (!test_bit(WriteMostly, &rdev->flags) || !rdev_init_wb(rdev))
154 if (mddev->wb_info_pool == NULL) {
155 unsigned int noio_flag;
158 mddev_suspend(mddev);
159 noio_flag = memalloc_noio_save();
160 mddev->wb_info_pool = mempool_create_kmalloc_pool(NR_WB_INFOS,
161 sizeof(struct wb_info));
162 memalloc_noio_restore(noio_flag);
163 if (!mddev->wb_info_pool)
164 pr_err("can't alloc memory pool for writemostly\n");
169 EXPORT_SYMBOL_GPL(mddev_create_wb_pool);
172 * destroy wb_info_pool if rdev is the last device flaged with WBCollisionCheck.
174 static void mddev_destroy_wb_pool(struct mddev *mddev, struct md_rdev *rdev)
176 if (!test_and_clear_bit(WBCollisionCheck, &rdev->flags))
179 if (mddev->wb_info_pool) {
180 struct md_rdev *temp;
184 * Check if other rdevs need wb_info_pool.
186 rdev_for_each(temp, mddev)
188 test_bit(WBCollisionCheck, &temp->flags))
191 mddev_suspend(rdev->mddev);
192 mempool_destroy(mddev->wb_info_pool);
193 mddev->wb_info_pool = NULL;
194 mddev_resume(rdev->mddev);
199 static struct ctl_table_header *raid_table_header;
201 static struct ctl_table raid_table[] = {
203 .procname = "speed_limit_min",
204 .data = &sysctl_speed_limit_min,
205 .maxlen = sizeof(int),
206 .mode = S_IRUGO|S_IWUSR,
207 .proc_handler = proc_dointvec,
210 .procname = "speed_limit_max",
211 .data = &sysctl_speed_limit_max,
212 .maxlen = sizeof(int),
213 .mode = S_IRUGO|S_IWUSR,
214 .proc_handler = proc_dointvec,
219 static struct ctl_table raid_dir_table[] = {
223 .mode = S_IRUGO|S_IXUGO,
229 static struct ctl_table raid_root_table[] = {
234 .child = raid_dir_table,
239 static const struct block_device_operations md_fops;
241 static int start_readonly;
244 * The original mechanism for creating an md device is to create
245 * a device node in /dev and to open it. This causes races with device-close.
246 * The preferred method is to write to the "new_array" module parameter.
247 * This can avoid races.
248 * Setting create_on_open to false disables the original mechanism
249 * so all the races disappear.
251 static bool create_on_open = true;
253 struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
256 if (!mddev || !bioset_initialized(&mddev->bio_set))
257 return bio_alloc(gfp_mask, nr_iovecs);
259 return bio_alloc_bioset(gfp_mask, nr_iovecs, &mddev->bio_set);
261 EXPORT_SYMBOL_GPL(bio_alloc_mddev);
263 static struct bio *md_bio_alloc_sync(struct mddev *mddev)
265 if (!mddev || !bioset_initialized(&mddev->sync_set))
266 return bio_alloc(GFP_NOIO, 1);
268 return bio_alloc_bioset(GFP_NOIO, 1, &mddev->sync_set);
272 * We have a system wide 'event count' that is incremented
273 * on any 'interesting' event, and readers of /proc/mdstat
274 * can use 'poll' or 'select' to find out when the event
278 * start array, stop array, error, add device, remove device,
279 * start build, activate spare
281 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
282 static atomic_t md_event_count;
283 void md_new_event(struct mddev *mddev)
285 atomic_inc(&md_event_count);
286 wake_up(&md_event_waiters);
288 EXPORT_SYMBOL_GPL(md_new_event);
291 * Enables to iterate over all existing md arrays
292 * all_mddevs_lock protects this list.
294 static LIST_HEAD(all_mddevs);
295 static DEFINE_SPINLOCK(all_mddevs_lock);
298 * iterates through all used mddevs in the system.
299 * We take care to grab the all_mddevs_lock whenever navigating
300 * the list, and to always hold a refcount when unlocked.
301 * Any code which breaks out of this loop while own
302 * a reference to the current mddev and must mddev_put it.
304 #define for_each_mddev(_mddev,_tmp) \
306 for (({ spin_lock(&all_mddevs_lock); \
307 _tmp = all_mddevs.next; \
309 ({ if (_tmp != &all_mddevs) \
310 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
311 spin_unlock(&all_mddevs_lock); \
312 if (_mddev) mddev_put(_mddev); \
313 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
314 _tmp != &all_mddevs;}); \
315 ({ spin_lock(&all_mddevs_lock); \
316 _tmp = _tmp->next;}) \
319 /* Rather than calling directly into the personality make_request function,
320 * IO requests come here first so that we can check if the device is
321 * being suspended pending a reconfiguration.
322 * We hold a refcount over the call to ->make_request. By the time that
323 * call has finished, the bio has been linked into some internal structure
324 * and so is visible to ->quiesce(), so we don't need the refcount any more.
326 static bool is_suspended(struct mddev *mddev, struct bio *bio)
328 if (mddev->suspended)
330 if (bio_data_dir(bio) != WRITE)
332 if (mddev->suspend_lo >= mddev->suspend_hi)
334 if (bio->bi_iter.bi_sector >= mddev->suspend_hi)
336 if (bio_end_sector(bio) < mddev->suspend_lo)
341 void md_handle_request(struct mddev *mddev, struct bio *bio)
345 if (is_suspended(mddev, bio)) {
348 prepare_to_wait(&mddev->sb_wait, &__wait,
349 TASK_UNINTERRUPTIBLE);
350 if (!is_suspended(mddev, bio))
356 finish_wait(&mddev->sb_wait, &__wait);
358 atomic_inc(&mddev->active_io);
361 if (!mddev->pers->make_request(mddev, bio)) {
362 atomic_dec(&mddev->active_io);
363 wake_up(&mddev->sb_wait);
364 goto check_suspended;
367 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
368 wake_up(&mddev->sb_wait);
370 EXPORT_SYMBOL(md_handle_request);
372 static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio)
374 const int rw = bio_data_dir(bio);
375 const int sgrp = op_stat_group(bio_op(bio));
376 struct mddev *mddev = q->queuedata;
377 unsigned int sectors;
379 if (unlikely(test_bit(MD_BROKEN, &mddev->flags)) && (rw == WRITE)) {
381 return BLK_QC_T_NONE;
384 blk_queue_split(q, &bio);
386 if (mddev == NULL || mddev->pers == NULL) {
388 return BLK_QC_T_NONE;
390 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
391 if (bio_sectors(bio) != 0)
392 bio->bi_status = BLK_STS_IOERR;
394 return BLK_QC_T_NONE;
398 * save the sectors now since our bio can
399 * go away inside make_request
401 sectors = bio_sectors(bio);
402 /* bio could be mergeable after passing to underlayer */
403 bio->bi_opf &= ~REQ_NOMERGE;
405 md_handle_request(mddev, bio);
408 part_stat_inc(&mddev->gendisk->part0, ios[sgrp]);
409 part_stat_add(&mddev->gendisk->part0, sectors[sgrp], sectors);
412 return BLK_QC_T_NONE;
415 /* mddev_suspend makes sure no new requests are submitted
416 * to the device, and that any requests that have been submitted
417 * are completely handled.
418 * Once mddev_detach() is called and completes, the module will be
421 void mddev_suspend(struct mddev *mddev)
423 WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk);
424 lockdep_assert_held(&mddev->reconfig_mutex);
425 if (mddev->suspended++)
428 wake_up(&mddev->sb_wait);
429 set_bit(MD_ALLOW_SB_UPDATE, &mddev->flags);
430 smp_mb__after_atomic();
431 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
432 mddev->pers->quiesce(mddev, 1);
433 clear_bit_unlock(MD_ALLOW_SB_UPDATE, &mddev->flags);
434 wait_event(mddev->sb_wait, !test_bit(MD_UPDATING_SB, &mddev->flags));
436 del_timer_sync(&mddev->safemode_timer);
438 EXPORT_SYMBOL_GPL(mddev_suspend);
440 void mddev_resume(struct mddev *mddev)
442 lockdep_assert_held(&mddev->reconfig_mutex);
443 if (--mddev->suspended)
445 wake_up(&mddev->sb_wait);
446 mddev->pers->quiesce(mddev, 0);
448 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
449 md_wakeup_thread(mddev->thread);
450 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
452 EXPORT_SYMBOL_GPL(mddev_resume);
454 int mddev_congested(struct mddev *mddev, int bits)
456 struct md_personality *pers = mddev->pers;
460 if (mddev->suspended)
462 else if (pers && pers->congested)
463 ret = pers->congested(mddev, bits);
467 EXPORT_SYMBOL_GPL(mddev_congested);
468 static int md_congested(void *data, int bits)
470 struct mddev *mddev = data;
471 return mddev_congested(mddev, bits);
475 * Generic flush handling for md
478 static void md_end_flush(struct bio *bio)
480 struct md_rdev *rdev = bio->bi_private;
481 struct mddev *mddev = rdev->mddev;
483 rdev_dec_pending(rdev, mddev);
485 if (atomic_dec_and_test(&mddev->flush_pending)) {
486 /* The pre-request flush has finished */
487 queue_work(md_wq, &mddev->flush_work);
492 static void md_submit_flush_data(struct work_struct *ws);
494 static void submit_flushes(struct work_struct *ws)
496 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
497 struct md_rdev *rdev;
499 mddev->start_flush = ktime_get_boottime();
500 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
501 atomic_set(&mddev->flush_pending, 1);
503 rdev_for_each_rcu(rdev, mddev)
504 if (rdev->raid_disk >= 0 &&
505 !test_bit(Faulty, &rdev->flags)) {
506 /* Take two references, one is dropped
507 * when request finishes, one after
508 * we reclaim rcu_read_lock
511 atomic_inc(&rdev->nr_pending);
512 atomic_inc(&rdev->nr_pending);
514 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
515 bi->bi_end_io = md_end_flush;
516 bi->bi_private = rdev;
517 bio_set_dev(bi, rdev->bdev);
518 bi->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
519 atomic_inc(&mddev->flush_pending);
522 rdev_dec_pending(rdev, mddev);
525 if (atomic_dec_and_test(&mddev->flush_pending))
526 queue_work(md_wq, &mddev->flush_work);
529 static void md_submit_flush_data(struct work_struct *ws)
531 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
532 struct bio *bio = mddev->flush_bio;
535 * must reset flush_bio before calling into md_handle_request to avoid a
536 * deadlock, because other bios passed md_handle_request suspend check
537 * could wait for this and below md_handle_request could wait for those
538 * bios because of suspend check
540 mddev->last_flush = mddev->start_flush;
541 mddev->flush_bio = NULL;
542 wake_up(&mddev->sb_wait);
544 if (bio->bi_iter.bi_size == 0) {
545 /* an empty barrier - all done */
548 bio->bi_opf &= ~REQ_PREFLUSH;
549 md_handle_request(mddev, bio);
553 void md_flush_request(struct mddev *mddev, struct bio *bio)
555 ktime_t start = ktime_get_boottime();
556 spin_lock_irq(&mddev->lock);
557 wait_event_lock_irq(mddev->sb_wait,
559 ktime_after(mddev->last_flush, start),
561 if (!ktime_after(mddev->last_flush, start)) {
562 WARN_ON(mddev->flush_bio);
563 mddev->flush_bio = bio;
566 spin_unlock_irq(&mddev->lock);
569 INIT_WORK(&mddev->flush_work, submit_flushes);
570 queue_work(md_wq, &mddev->flush_work);
572 /* flush was performed for some other bio while we waited. */
573 if (bio->bi_iter.bi_size == 0)
574 /* an empty barrier - all done */
577 bio->bi_opf &= ~REQ_PREFLUSH;
578 mddev->pers->make_request(mddev, bio);
582 EXPORT_SYMBOL(md_flush_request);
584 static inline struct mddev *mddev_get(struct mddev *mddev)
586 atomic_inc(&mddev->active);
590 static void mddev_delayed_delete(struct work_struct *ws);
592 static void mddev_put(struct mddev *mddev)
594 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
596 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
597 mddev->ctime == 0 && !mddev->hold_active) {
598 /* Array is not configured at all, and not held active,
600 list_del_init(&mddev->all_mddevs);
603 * Call queue_work inside the spinlock so that
604 * flush_workqueue() after mddev_find will succeed in waiting
605 * for the work to be done.
607 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
608 queue_work(md_misc_wq, &mddev->del_work);
610 spin_unlock(&all_mddevs_lock);
613 static void md_safemode_timeout(struct timer_list *t);
615 void mddev_init(struct mddev *mddev)
617 kobject_init(&mddev->kobj, &md_ktype);
618 mutex_init(&mddev->open_mutex);
619 mutex_init(&mddev->reconfig_mutex);
620 mutex_init(&mddev->bitmap_info.mutex);
621 INIT_LIST_HEAD(&mddev->disks);
622 INIT_LIST_HEAD(&mddev->all_mddevs);
623 timer_setup(&mddev->safemode_timer, md_safemode_timeout, 0);
624 atomic_set(&mddev->active, 1);
625 atomic_set(&mddev->openers, 0);
626 atomic_set(&mddev->active_io, 0);
627 spin_lock_init(&mddev->lock);
628 atomic_set(&mddev->flush_pending, 0);
629 init_waitqueue_head(&mddev->sb_wait);
630 init_waitqueue_head(&mddev->recovery_wait);
631 mddev->reshape_position = MaxSector;
632 mddev->reshape_backwards = 0;
633 mddev->last_sync_action = "none";
634 mddev->resync_min = 0;
635 mddev->resync_max = MaxSector;
636 mddev->level = LEVEL_NONE;
638 EXPORT_SYMBOL_GPL(mddev_init);
640 static struct mddev *mddev_find(dev_t unit)
642 struct mddev *mddev, *new = NULL;
644 if (unit && MAJOR(unit) != MD_MAJOR)
645 unit &= ~((1<<MdpMinorShift)-1);
648 spin_lock(&all_mddevs_lock);
651 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
652 if (mddev->unit == unit) {
654 spin_unlock(&all_mddevs_lock);
660 list_add(&new->all_mddevs, &all_mddevs);
661 spin_unlock(&all_mddevs_lock);
662 new->hold_active = UNTIL_IOCTL;
666 /* find an unused unit number */
667 static int next_minor = 512;
668 int start = next_minor;
672 dev = MKDEV(MD_MAJOR, next_minor);
674 if (next_minor > MINORMASK)
676 if (next_minor == start) {
677 /* Oh dear, all in use. */
678 spin_unlock(&all_mddevs_lock);
684 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
685 if (mddev->unit == dev) {
691 new->md_minor = MINOR(dev);
692 new->hold_active = UNTIL_STOP;
693 list_add(&new->all_mddevs, &all_mddevs);
694 spin_unlock(&all_mddevs_lock);
697 spin_unlock(&all_mddevs_lock);
699 new = kzalloc(sizeof(*new), GFP_KERNEL);
704 if (MAJOR(unit) == MD_MAJOR)
705 new->md_minor = MINOR(unit);
707 new->md_minor = MINOR(unit) >> MdpMinorShift;
714 static struct attribute_group md_redundancy_group;
716 void mddev_unlock(struct mddev *mddev)
718 if (mddev->to_remove) {
719 /* These cannot be removed under reconfig_mutex as
720 * an access to the files will try to take reconfig_mutex
721 * while holding the file unremovable, which leads to
723 * So hold set sysfs_active while the remove in happeing,
724 * and anything else which might set ->to_remove or my
725 * otherwise change the sysfs namespace will fail with
726 * -EBUSY if sysfs_active is still set.
727 * We set sysfs_active under reconfig_mutex and elsewhere
728 * test it under the same mutex to ensure its correct value
731 struct attribute_group *to_remove = mddev->to_remove;
732 mddev->to_remove = NULL;
733 mddev->sysfs_active = 1;
734 mutex_unlock(&mddev->reconfig_mutex);
736 if (mddev->kobj.sd) {
737 if (to_remove != &md_redundancy_group)
738 sysfs_remove_group(&mddev->kobj, to_remove);
739 if (mddev->pers == NULL ||
740 mddev->pers->sync_request == NULL) {
741 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
742 if (mddev->sysfs_action)
743 sysfs_put(mddev->sysfs_action);
744 mddev->sysfs_action = NULL;
747 mddev->sysfs_active = 0;
749 mutex_unlock(&mddev->reconfig_mutex);
751 /* As we've dropped the mutex we need a spinlock to
752 * make sure the thread doesn't disappear
754 spin_lock(&pers_lock);
755 md_wakeup_thread(mddev->thread);
756 wake_up(&mddev->sb_wait);
757 spin_unlock(&pers_lock);
759 EXPORT_SYMBOL_GPL(mddev_unlock);
761 struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
763 struct md_rdev *rdev;
765 rdev_for_each_rcu(rdev, mddev)
766 if (rdev->desc_nr == nr)
771 EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
773 static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
775 struct md_rdev *rdev;
777 rdev_for_each(rdev, mddev)
778 if (rdev->bdev->bd_dev == dev)
784 struct md_rdev *md_find_rdev_rcu(struct mddev *mddev, dev_t dev)
786 struct md_rdev *rdev;
788 rdev_for_each_rcu(rdev, mddev)
789 if (rdev->bdev->bd_dev == dev)
794 EXPORT_SYMBOL_GPL(md_find_rdev_rcu);
796 static struct md_personality *find_pers(int level, char *clevel)
798 struct md_personality *pers;
799 list_for_each_entry(pers, &pers_list, list) {
800 if (level != LEVEL_NONE && pers->level == level)
802 if (strcmp(pers->name, clevel)==0)
808 /* return the offset of the super block in 512byte sectors */
809 static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
811 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
812 return MD_NEW_SIZE_SECTORS(num_sectors);
815 static int alloc_disk_sb(struct md_rdev *rdev)
817 rdev->sb_page = alloc_page(GFP_KERNEL);
823 void md_rdev_clear(struct md_rdev *rdev)
826 put_page(rdev->sb_page);
828 rdev->sb_page = NULL;
833 put_page(rdev->bb_page);
834 rdev->bb_page = NULL;
836 badblocks_exit(&rdev->badblocks);
838 EXPORT_SYMBOL_GPL(md_rdev_clear);
840 static void super_written(struct bio *bio)
842 struct md_rdev *rdev = bio->bi_private;
843 struct mddev *mddev = rdev->mddev;
845 if (bio->bi_status) {
846 pr_err("md: super_written gets error=%d\n", bio->bi_status);
847 md_error(mddev, rdev);
848 if (!test_bit(Faulty, &rdev->flags)
849 && (bio->bi_opf & MD_FAILFAST)) {
850 set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags);
851 set_bit(LastDev, &rdev->flags);
854 clear_bit(LastDev, &rdev->flags);
856 if (atomic_dec_and_test(&mddev->pending_writes))
857 wake_up(&mddev->sb_wait);
858 rdev_dec_pending(rdev, mddev);
862 void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
863 sector_t sector, int size, struct page *page)
865 /* write first size bytes of page to sector of rdev
866 * Increment mddev->pending_writes before returning
867 * and decrement it on completion, waking up sb_wait
868 * if zero is reached.
869 * If an error occurred, call md_error
877 if (test_bit(Faulty, &rdev->flags))
880 bio = md_bio_alloc_sync(mddev);
882 atomic_inc(&rdev->nr_pending);
884 bio_set_dev(bio, rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev);
885 bio->bi_iter.bi_sector = sector;
886 bio_add_page(bio, page, size, 0);
887 bio->bi_private = rdev;
888 bio->bi_end_io = super_written;
890 if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) &&
891 test_bit(FailFast, &rdev->flags) &&
892 !test_bit(LastDev, &rdev->flags))
894 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA | ff;
896 atomic_inc(&mddev->pending_writes);
900 int md_super_wait(struct mddev *mddev)
902 /* wait for all superblock writes that were scheduled to complete */
903 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
904 if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags))
909 int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
910 struct page *page, int op, int op_flags, bool metadata_op)
912 struct bio *bio = md_bio_alloc_sync(rdev->mddev);
915 if (metadata_op && rdev->meta_bdev)
916 bio_set_dev(bio, rdev->meta_bdev);
918 bio_set_dev(bio, rdev->bdev);
919 bio_set_op_attrs(bio, op, op_flags);
921 bio->bi_iter.bi_sector = sector + rdev->sb_start;
922 else if (rdev->mddev->reshape_position != MaxSector &&
923 (rdev->mddev->reshape_backwards ==
924 (sector >= rdev->mddev->reshape_position)))
925 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
927 bio->bi_iter.bi_sector = sector + rdev->data_offset;
928 bio_add_page(bio, page, size, 0);
930 submit_bio_wait(bio);
932 ret = !bio->bi_status;
936 EXPORT_SYMBOL_GPL(sync_page_io);
938 static int read_disk_sb(struct md_rdev *rdev, int size)
940 char b[BDEVNAME_SIZE];
945 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true))
951 pr_err("md: disabled device %s, could not read superblock.\n",
952 bdevname(rdev->bdev,b));
956 static int md_uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
958 return sb1->set_uuid0 == sb2->set_uuid0 &&
959 sb1->set_uuid1 == sb2->set_uuid1 &&
960 sb1->set_uuid2 == sb2->set_uuid2 &&
961 sb1->set_uuid3 == sb2->set_uuid3;
964 static int md_sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
967 mdp_super_t *tmp1, *tmp2;
969 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
970 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
972 if (!tmp1 || !tmp2) {
981 * nr_disks is not constant
986 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
993 static u32 md_csum_fold(u32 csum)
995 csum = (csum & 0xffff) + (csum >> 16);
996 return (csum & 0xffff) + (csum >> 16);
999 static unsigned int calc_sb_csum(mdp_super_t *sb)
1002 u32 *sb32 = (u32*)sb;
1004 unsigned int disk_csum, csum;
1006 disk_csum = sb->sb_csum;
1009 for (i = 0; i < MD_SB_BYTES/4 ; i++)
1011 csum = (newcsum & 0xffffffff) + (newcsum>>32);
1014 /* This used to use csum_partial, which was wrong for several
1015 * reasons including that different results are returned on
1016 * different architectures. It isn't critical that we get exactly
1017 * the same return value as before (we always csum_fold before
1018 * testing, and that removes any differences). However as we
1019 * know that csum_partial always returned a 16bit value on
1020 * alphas, do a fold to maximise conformity to previous behaviour.
1022 sb->sb_csum = md_csum_fold(disk_csum);
1024 sb->sb_csum = disk_csum;
1030 * Handle superblock details.
1031 * We want to be able to handle multiple superblock formats
1032 * so we have a common interface to them all, and an array of
1033 * different handlers.
1034 * We rely on user-space to write the initial superblock, and support
1035 * reading and updating of superblocks.
1036 * Interface methods are:
1037 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
1038 * loads and validates a superblock on dev.
1039 * if refdev != NULL, compare superblocks on both devices
1041 * 0 - dev has a superblock that is compatible with refdev
1042 * 1 - dev has a superblock that is compatible and newer than refdev
1043 * so dev should be used as the refdev in future
1044 * -EINVAL superblock incompatible or invalid
1045 * -othererror e.g. -EIO
1047 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
1048 * Verify that dev is acceptable into mddev.
1049 * The first time, mddev->raid_disks will be 0, and data from
1050 * dev should be merged in. Subsequent calls check that dev
1051 * is new enough. Return 0 or -EINVAL
1053 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
1054 * Update the superblock for rdev with data in mddev
1055 * This does not write to disc.
1061 struct module *owner;
1062 int (*load_super)(struct md_rdev *rdev,
1063 struct md_rdev *refdev,
1065 int (*validate_super)(struct mddev *mddev,
1066 struct md_rdev *rdev);
1067 void (*sync_super)(struct mddev *mddev,
1068 struct md_rdev *rdev);
1069 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
1070 sector_t num_sectors);
1071 int (*allow_new_offset)(struct md_rdev *rdev,
1072 unsigned long long new_offset);
1076 * Check that the given mddev has no bitmap.
1078 * This function is called from the run method of all personalities that do not
1079 * support bitmaps. It prints an error message and returns non-zero if mddev
1080 * has a bitmap. Otherwise, it returns 0.
1083 int md_check_no_bitmap(struct mddev *mddev)
1085 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
1087 pr_warn("%s: bitmaps are not supported for %s\n",
1088 mdname(mddev), mddev->pers->name);
1091 EXPORT_SYMBOL(md_check_no_bitmap);
1094 * load_super for 0.90.0
1096 static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
1098 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1103 * Calculate the position of the superblock (512byte sectors),
1104 * it's at the end of the disk.
1106 * It also happens to be a multiple of 4Kb.
1108 rdev->sb_start = calc_dev_sboffset(rdev);
1110 ret = read_disk_sb(rdev, MD_SB_BYTES);
1116 bdevname(rdev->bdev, b);
1117 sb = page_address(rdev->sb_page);
1119 if (sb->md_magic != MD_SB_MAGIC) {
1120 pr_warn("md: invalid raid superblock magic on %s\n", b);
1124 if (sb->major_version != 0 ||
1125 sb->minor_version < 90 ||
1126 sb->minor_version > 91) {
1127 pr_warn("Bad version number %d.%d on %s\n",
1128 sb->major_version, sb->minor_version, b);
1132 if (sb->raid_disks <= 0)
1135 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
1136 pr_warn("md: invalid superblock checksum on %s\n", b);
1140 rdev->preferred_minor = sb->md_minor;
1141 rdev->data_offset = 0;
1142 rdev->new_data_offset = 0;
1143 rdev->sb_size = MD_SB_BYTES;
1144 rdev->badblocks.shift = -1;
1146 if (sb->level == LEVEL_MULTIPATH)
1149 rdev->desc_nr = sb->this_disk.number;
1155 mdp_super_t *refsb = page_address(refdev->sb_page);
1156 if (!md_uuid_equal(refsb, sb)) {
1157 pr_warn("md: %s has different UUID to %s\n",
1158 b, bdevname(refdev->bdev,b2));
1161 if (!md_sb_equal(refsb, sb)) {
1162 pr_warn("md: %s has same UUID but different superblock to %s\n",
1163 b, bdevname(refdev->bdev, b2));
1167 ev2 = md_event(refsb);
1173 rdev->sectors = rdev->sb_start;
1174 /* Limit to 4TB as metadata cannot record more than that.
1175 * (not needed for Linear and RAID0 as metadata doesn't
1178 if ((u64)rdev->sectors >= (2ULL << 32) && sb->level >= 1)
1179 rdev->sectors = (sector_t)(2ULL << 32) - 2;
1181 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
1182 /* "this cannot possibly happen" ... */
1190 * validate_super for 0.90.0
1192 static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
1195 mdp_super_t *sb = page_address(rdev->sb_page);
1196 __u64 ev1 = md_event(sb);
1198 rdev->raid_disk = -1;
1199 clear_bit(Faulty, &rdev->flags);
1200 clear_bit(In_sync, &rdev->flags);
1201 clear_bit(Bitmap_sync, &rdev->flags);
1202 clear_bit(WriteMostly, &rdev->flags);
1204 if (mddev->raid_disks == 0) {
1205 mddev->major_version = 0;
1206 mddev->minor_version = sb->minor_version;
1207 mddev->patch_version = sb->patch_version;
1208 mddev->external = 0;
1209 mddev->chunk_sectors = sb->chunk_size >> 9;
1210 mddev->ctime = sb->ctime;
1211 mddev->utime = sb->utime;
1212 mddev->level = sb->level;
1213 mddev->clevel[0] = 0;
1214 mddev->layout = sb->layout;
1215 mddev->raid_disks = sb->raid_disks;
1216 mddev->dev_sectors = ((sector_t)sb->size) * 2;
1217 mddev->events = ev1;
1218 mddev->bitmap_info.offset = 0;
1219 mddev->bitmap_info.space = 0;
1220 /* bitmap can use 60 K after the 4K superblocks */
1221 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
1222 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
1223 mddev->reshape_backwards = 0;
1225 if (mddev->minor_version >= 91) {
1226 mddev->reshape_position = sb->reshape_position;
1227 mddev->delta_disks = sb->delta_disks;
1228 mddev->new_level = sb->new_level;
1229 mddev->new_layout = sb->new_layout;
1230 mddev->new_chunk_sectors = sb->new_chunk >> 9;
1231 if (mddev->delta_disks < 0)
1232 mddev->reshape_backwards = 1;
1234 mddev->reshape_position = MaxSector;
1235 mddev->delta_disks = 0;
1236 mddev->new_level = mddev->level;
1237 mddev->new_layout = mddev->layout;
1238 mddev->new_chunk_sectors = mddev->chunk_sectors;
1240 if (mddev->level == 0)
1243 if (sb->state & (1<<MD_SB_CLEAN))
1244 mddev->recovery_cp = MaxSector;
1246 if (sb->events_hi == sb->cp_events_hi &&
1247 sb->events_lo == sb->cp_events_lo) {
1248 mddev->recovery_cp = sb->recovery_cp;
1250 mddev->recovery_cp = 0;
1253 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1254 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1255 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1256 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1258 mddev->max_disks = MD_SB_DISKS;
1260 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
1261 mddev->bitmap_info.file == NULL) {
1262 mddev->bitmap_info.offset =
1263 mddev->bitmap_info.default_offset;
1264 mddev->bitmap_info.space =
1265 mddev->bitmap_info.default_space;
1268 } else if (mddev->pers == NULL) {
1269 /* Insist on good event counter while assembling, except
1270 * for spares (which don't need an event count) */
1272 if (sb->disks[rdev->desc_nr].state & (
1273 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1274 if (ev1 < mddev->events)
1276 } else if (mddev->bitmap) {
1277 /* if adding to array with a bitmap, then we can accept an
1278 * older device ... but not too old.
1280 if (ev1 < mddev->bitmap->events_cleared)
1282 if (ev1 < mddev->events)
1283 set_bit(Bitmap_sync, &rdev->flags);
1285 if (ev1 < mddev->events)
1286 /* just a hot-add of a new device, leave raid_disk at -1 */
1290 if (mddev->level != LEVEL_MULTIPATH) {
1291 desc = sb->disks + rdev->desc_nr;
1293 if (desc->state & (1<<MD_DISK_FAULTY))
1294 set_bit(Faulty, &rdev->flags);
1295 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1296 desc->raid_disk < mddev->raid_disks */) {
1297 set_bit(In_sync, &rdev->flags);
1298 rdev->raid_disk = desc->raid_disk;
1299 rdev->saved_raid_disk = desc->raid_disk;
1300 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1301 /* active but not in sync implies recovery up to
1302 * reshape position. We don't know exactly where
1303 * that is, so set to zero for now */
1304 if (mddev->minor_version >= 91) {
1305 rdev->recovery_offset = 0;
1306 rdev->raid_disk = desc->raid_disk;
1309 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1310 set_bit(WriteMostly, &rdev->flags);
1311 if (desc->state & (1<<MD_DISK_FAILFAST))
1312 set_bit(FailFast, &rdev->flags);
1313 } else /* MULTIPATH are always insync */
1314 set_bit(In_sync, &rdev->flags);
1319 * sync_super for 0.90.0
1321 static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
1324 struct md_rdev *rdev2;
1325 int next_spare = mddev->raid_disks;
1327 /* make rdev->sb match mddev data..
1330 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1331 * 3/ any empty disks < next_spare become removed
1333 * disks[0] gets initialised to REMOVED because
1334 * we cannot be sure from other fields if it has
1335 * been initialised or not.
1338 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1340 rdev->sb_size = MD_SB_BYTES;
1342 sb = page_address(rdev->sb_page);
1344 memset(sb, 0, sizeof(*sb));
1346 sb->md_magic = MD_SB_MAGIC;
1347 sb->major_version = mddev->major_version;
1348 sb->patch_version = mddev->patch_version;
1349 sb->gvalid_words = 0; /* ignored */
1350 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1351 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1352 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1353 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1355 sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
1356 sb->level = mddev->level;
1357 sb->size = mddev->dev_sectors / 2;
1358 sb->raid_disks = mddev->raid_disks;
1359 sb->md_minor = mddev->md_minor;
1360 sb->not_persistent = 0;
1361 sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
1363 sb->events_hi = (mddev->events>>32);
1364 sb->events_lo = (u32)mddev->events;
1366 if (mddev->reshape_position == MaxSector)
1367 sb->minor_version = 90;
1369 sb->minor_version = 91;
1370 sb->reshape_position = mddev->reshape_position;
1371 sb->new_level = mddev->new_level;
1372 sb->delta_disks = mddev->delta_disks;
1373 sb->new_layout = mddev->new_layout;
1374 sb->new_chunk = mddev->new_chunk_sectors << 9;
1376 mddev->minor_version = sb->minor_version;
1379 sb->recovery_cp = mddev->recovery_cp;
1380 sb->cp_events_hi = (mddev->events>>32);
1381 sb->cp_events_lo = (u32)mddev->events;
1382 if (mddev->recovery_cp == MaxSector)
1383 sb->state = (1<< MD_SB_CLEAN);
1385 sb->recovery_cp = 0;
1387 sb->layout = mddev->layout;
1388 sb->chunk_size = mddev->chunk_sectors << 9;
1390 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
1391 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1393 sb->disks[0].state = (1<<MD_DISK_REMOVED);
1394 rdev_for_each(rdev2, mddev) {
1397 int is_active = test_bit(In_sync, &rdev2->flags);
1399 if (rdev2->raid_disk >= 0 &&
1400 sb->minor_version >= 91)
1401 /* we have nowhere to store the recovery_offset,
1402 * but if it is not below the reshape_position,
1403 * we can piggy-back on that.
1406 if (rdev2->raid_disk < 0 ||
1407 test_bit(Faulty, &rdev2->flags))
1410 desc_nr = rdev2->raid_disk;
1412 desc_nr = next_spare++;
1413 rdev2->desc_nr = desc_nr;
1414 d = &sb->disks[rdev2->desc_nr];
1416 d->number = rdev2->desc_nr;
1417 d->major = MAJOR(rdev2->bdev->bd_dev);
1418 d->minor = MINOR(rdev2->bdev->bd_dev);
1420 d->raid_disk = rdev2->raid_disk;
1422 d->raid_disk = rdev2->desc_nr; /* compatibility */
1423 if (test_bit(Faulty, &rdev2->flags))
1424 d->state = (1<<MD_DISK_FAULTY);
1425 else if (is_active) {
1426 d->state = (1<<MD_DISK_ACTIVE);
1427 if (test_bit(In_sync, &rdev2->flags))
1428 d->state |= (1<<MD_DISK_SYNC);
1436 if (test_bit(WriteMostly, &rdev2->flags))
1437 d->state |= (1<<MD_DISK_WRITEMOSTLY);
1438 if (test_bit(FailFast, &rdev2->flags))
1439 d->state |= (1<<MD_DISK_FAILFAST);
1441 /* now set the "removed" and "faulty" bits on any missing devices */
1442 for (i=0 ; i < mddev->raid_disks ; i++) {
1443 mdp_disk_t *d = &sb->disks[i];
1444 if (d->state == 0 && d->number == 0) {
1447 d->state = (1<<MD_DISK_REMOVED);
1448 d->state |= (1<<MD_DISK_FAULTY);
1452 sb->nr_disks = nr_disks;
1453 sb->active_disks = active;
1454 sb->working_disks = working;
1455 sb->failed_disks = failed;
1456 sb->spare_disks = spare;
1458 sb->this_disk = sb->disks[rdev->desc_nr];
1459 sb->sb_csum = calc_sb_csum(sb);
1463 * rdev_size_change for 0.90.0
1465 static unsigned long long
1466 super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
1468 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1469 return 0; /* component must fit device */
1470 if (rdev->mddev->bitmap_info.offset)
1471 return 0; /* can't move bitmap */
1472 rdev->sb_start = calc_dev_sboffset(rdev);
1473 if (!num_sectors || num_sectors > rdev->sb_start)
1474 num_sectors = rdev->sb_start;
1475 /* Limit to 4TB as metadata cannot record more than that.
1476 * 4TB == 2^32 KB, or 2*2^32 sectors.
1478 if ((u64)num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
1479 num_sectors = (sector_t)(2ULL << 32) - 2;
1481 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1483 } while (md_super_wait(rdev->mddev) < 0);
1488 super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1490 /* non-zero offset changes not possible with v0.90 */
1491 return new_offset == 0;
1495 * version 1 superblock
1498 static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
1502 unsigned long long newcsum;
1503 int size = 256 + le32_to_cpu(sb->max_dev)*2;
1504 __le32 *isuper = (__le32*)sb;
1506 disk_csum = sb->sb_csum;
1509 for (; size >= 4; size -= 4)
1510 newcsum += le32_to_cpu(*isuper++);
1513 newcsum += le16_to_cpu(*(__le16*) isuper);
1515 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1516 sb->sb_csum = disk_csum;
1517 return cpu_to_le32(csum);
1520 static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
1522 struct mdp_superblock_1 *sb;
1526 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1530 * Calculate the position of the superblock in 512byte sectors.
1531 * It is always aligned to a 4K boundary and
1532 * depeding on minor_version, it can be:
1533 * 0: At least 8K, but less than 12K, from end of device
1534 * 1: At start of device
1535 * 2: 4K from start of device.
1537 switch(minor_version) {
1539 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
1541 sb_start &= ~(sector_t)(4*2-1);
1552 rdev->sb_start = sb_start;
1554 /* superblock is rarely larger than 1K, but it can be larger,
1555 * and it is safe to read 4k, so we do that
1557 ret = read_disk_sb(rdev, 4096);
1558 if (ret) return ret;
1560 sb = page_address(rdev->sb_page);
1562 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1563 sb->major_version != cpu_to_le32(1) ||
1564 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1565 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1566 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1569 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1570 pr_warn("md: invalid superblock checksum on %s\n",
1571 bdevname(rdev->bdev,b));
1574 if (le64_to_cpu(sb->data_size) < 10) {
1575 pr_warn("md: data_size too small on %s\n",
1576 bdevname(rdev->bdev,b));
1581 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1582 /* Some padding is non-zero, might be a new feature */
1585 rdev->preferred_minor = 0xffff;
1586 rdev->data_offset = le64_to_cpu(sb->data_offset);
1587 rdev->new_data_offset = rdev->data_offset;
1588 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1589 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1590 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
1591 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1593 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1594 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1595 if (rdev->sb_size & bmask)
1596 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1599 && rdev->data_offset < sb_start + (rdev->sb_size/512))
1602 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1605 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1608 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1610 if (!rdev->bb_page) {
1611 rdev->bb_page = alloc_page(GFP_KERNEL);
1615 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1616 rdev->badblocks.count == 0) {
1617 /* need to load the bad block list.
1618 * Currently we limit it to one page.
1624 int sectors = le16_to_cpu(sb->bblog_size);
1625 if (sectors > (PAGE_SIZE / 512))
1627 offset = le32_to_cpu(sb->bblog_offset);
1630 bb_sector = (long long)offset;
1631 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1632 rdev->bb_page, REQ_OP_READ, 0, true))
1634 bbp = (__le64 *)page_address(rdev->bb_page);
1635 rdev->badblocks.shift = sb->bblog_shift;
1636 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1637 u64 bb = le64_to_cpu(*bbp);
1638 int count = bb & (0x3ff);
1639 u64 sector = bb >> 10;
1640 sector <<= sb->bblog_shift;
1641 count <<= sb->bblog_shift;
1644 if (badblocks_set(&rdev->badblocks, sector, count, 1))
1647 } else if (sb->bblog_offset != 0)
1648 rdev->badblocks.shift = 0;
1650 if ((le32_to_cpu(sb->feature_map) &
1651 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS))) {
1652 rdev->ppl.offset = (__s16)le16_to_cpu(sb->ppl.offset);
1653 rdev->ppl.size = le16_to_cpu(sb->ppl.size);
1654 rdev->ppl.sector = rdev->sb_start + rdev->ppl.offset;
1657 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RAID0_LAYOUT) &&
1665 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
1667 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1668 sb->level != refsb->level ||
1669 sb->layout != refsb->layout ||
1670 sb->chunksize != refsb->chunksize) {
1671 pr_warn("md: %s has strangely different superblock to %s\n",
1672 bdevname(rdev->bdev,b),
1673 bdevname(refdev->bdev,b2));
1676 ev1 = le64_to_cpu(sb->events);
1677 ev2 = le64_to_cpu(refsb->events);
1684 if (minor_version) {
1685 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1686 sectors -= rdev->data_offset;
1688 sectors = rdev->sb_start;
1689 if (sectors < le64_to_cpu(sb->data_size))
1691 rdev->sectors = le64_to_cpu(sb->data_size);
1695 static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
1697 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
1698 __u64 ev1 = le64_to_cpu(sb->events);
1700 rdev->raid_disk = -1;
1701 clear_bit(Faulty, &rdev->flags);
1702 clear_bit(In_sync, &rdev->flags);
1703 clear_bit(Bitmap_sync, &rdev->flags);
1704 clear_bit(WriteMostly, &rdev->flags);
1706 if (mddev->raid_disks == 0) {
1707 mddev->major_version = 1;
1708 mddev->patch_version = 0;
1709 mddev->external = 0;
1710 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
1711 mddev->ctime = le64_to_cpu(sb->ctime);
1712 mddev->utime = le64_to_cpu(sb->utime);
1713 mddev->level = le32_to_cpu(sb->level);
1714 mddev->clevel[0] = 0;
1715 mddev->layout = le32_to_cpu(sb->layout);
1716 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1717 mddev->dev_sectors = le64_to_cpu(sb->size);
1718 mddev->events = ev1;
1719 mddev->bitmap_info.offset = 0;
1720 mddev->bitmap_info.space = 0;
1721 /* Default location for bitmap is 1K after superblock
1722 * using 3K - total of 4K
1724 mddev->bitmap_info.default_offset = 1024 >> 9;
1725 mddev->bitmap_info.default_space = (4096-1024) >> 9;
1726 mddev->reshape_backwards = 0;
1728 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1729 memcpy(mddev->uuid, sb->set_uuid, 16);
1731 mddev->max_disks = (4096-256)/2;
1733 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1734 mddev->bitmap_info.file == NULL) {
1735 mddev->bitmap_info.offset =
1736 (__s32)le32_to_cpu(sb->bitmap_offset);
1737 /* Metadata doesn't record how much space is available.
1738 * For 1.0, we assume we can use up to the superblock
1739 * if before, else to 4K beyond superblock.
1740 * For others, assume no change is possible.
1742 if (mddev->minor_version > 0)
1743 mddev->bitmap_info.space = 0;
1744 else if (mddev->bitmap_info.offset > 0)
1745 mddev->bitmap_info.space =
1746 8 - mddev->bitmap_info.offset;
1748 mddev->bitmap_info.space =
1749 -mddev->bitmap_info.offset;
1752 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1753 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1754 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1755 mddev->new_level = le32_to_cpu(sb->new_level);
1756 mddev->new_layout = le32_to_cpu(sb->new_layout);
1757 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
1758 if (mddev->delta_disks < 0 ||
1759 (mddev->delta_disks == 0 &&
1760 (le32_to_cpu(sb->feature_map)
1761 & MD_FEATURE_RESHAPE_BACKWARDS)))
1762 mddev->reshape_backwards = 1;
1764 mddev->reshape_position = MaxSector;
1765 mddev->delta_disks = 0;
1766 mddev->new_level = mddev->level;
1767 mddev->new_layout = mddev->layout;
1768 mddev->new_chunk_sectors = mddev->chunk_sectors;
1771 if (mddev->level == 0 &&
1772 !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RAID0_LAYOUT))
1775 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
1776 set_bit(MD_HAS_JOURNAL, &mddev->flags);
1778 if (le32_to_cpu(sb->feature_map) &
1779 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS)) {
1780 if (le32_to_cpu(sb->feature_map) &
1781 (MD_FEATURE_BITMAP_OFFSET | MD_FEATURE_JOURNAL))
1783 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) &&
1784 (le32_to_cpu(sb->feature_map) &
1785 MD_FEATURE_MULTIPLE_PPLS))
1787 set_bit(MD_HAS_PPL, &mddev->flags);
1789 } else if (mddev->pers == NULL) {
1790 /* Insist of good event counter while assembling, except for
1791 * spares (which don't need an event count) */
1793 if (rdev->desc_nr >= 0 &&
1794 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1795 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1796 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
1797 if (ev1 < mddev->events)
1799 } else if (mddev->bitmap) {
1800 /* If adding to array with a bitmap, then we can accept an
1801 * older device, but not too old.
1803 if (ev1 < mddev->bitmap->events_cleared)
1805 if (ev1 < mddev->events)
1806 set_bit(Bitmap_sync, &rdev->flags);
1808 if (ev1 < mddev->events)
1809 /* just a hot-add of a new device, leave raid_disk at -1 */
1812 if (mddev->level != LEVEL_MULTIPATH) {
1814 if (rdev->desc_nr < 0 ||
1815 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1816 role = MD_DISK_ROLE_SPARE;
1819 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1821 case MD_DISK_ROLE_SPARE: /* spare */
1823 case MD_DISK_ROLE_FAULTY: /* faulty */
1824 set_bit(Faulty, &rdev->flags);
1826 case MD_DISK_ROLE_JOURNAL: /* journal device */
1827 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1828 /* journal device without journal feature */
1829 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
1832 set_bit(Journal, &rdev->flags);
1833 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
1834 rdev->raid_disk = 0;
1837 rdev->saved_raid_disk = role;
1838 if ((le32_to_cpu(sb->feature_map) &
1839 MD_FEATURE_RECOVERY_OFFSET)) {
1840 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1841 if (!(le32_to_cpu(sb->feature_map) &
1842 MD_FEATURE_RECOVERY_BITMAP))
1843 rdev->saved_raid_disk = -1;
1846 * If the array is FROZEN, then the device can't
1847 * be in_sync with rest of array.
1849 if (!test_bit(MD_RECOVERY_FROZEN,
1851 set_bit(In_sync, &rdev->flags);
1853 rdev->raid_disk = role;
1856 if (sb->devflags & WriteMostly1)
1857 set_bit(WriteMostly, &rdev->flags);
1858 if (sb->devflags & FailFast1)
1859 set_bit(FailFast, &rdev->flags);
1860 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1861 set_bit(Replacement, &rdev->flags);
1862 } else /* MULTIPATH are always insync */
1863 set_bit(In_sync, &rdev->flags);
1868 static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
1870 struct mdp_superblock_1 *sb;
1871 struct md_rdev *rdev2;
1873 /* make rdev->sb match mddev and rdev data. */
1875 sb = page_address(rdev->sb_page);
1877 sb->feature_map = 0;
1879 sb->recovery_offset = cpu_to_le64(0);
1880 memset(sb->pad3, 0, sizeof(sb->pad3));
1882 sb->utime = cpu_to_le64((__u64)mddev->utime);
1883 sb->events = cpu_to_le64(mddev->events);
1885 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1886 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
1887 sb->resync_offset = cpu_to_le64(MaxSector);
1889 sb->resync_offset = cpu_to_le64(0);
1891 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1893 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1894 sb->size = cpu_to_le64(mddev->dev_sectors);
1895 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
1896 sb->level = cpu_to_le32(mddev->level);
1897 sb->layout = cpu_to_le32(mddev->layout);
1898 if (test_bit(FailFast, &rdev->flags))
1899 sb->devflags |= FailFast1;
1901 sb->devflags &= ~FailFast1;
1903 if (test_bit(WriteMostly, &rdev->flags))
1904 sb->devflags |= WriteMostly1;
1906 sb->devflags &= ~WriteMostly1;
1907 sb->data_offset = cpu_to_le64(rdev->data_offset);
1908 sb->data_size = cpu_to_le64(rdev->sectors);
1910 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1911 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
1912 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1915 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
1916 !test_bit(In_sync, &rdev->flags)) {
1918 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1919 sb->recovery_offset =
1920 cpu_to_le64(rdev->recovery_offset);
1921 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1923 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
1925 /* Note: recovery_offset and journal_tail share space */
1926 if (test_bit(Journal, &rdev->flags))
1927 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
1928 if (test_bit(Replacement, &rdev->flags))
1930 cpu_to_le32(MD_FEATURE_REPLACEMENT);
1932 if (mddev->reshape_position != MaxSector) {
1933 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1934 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1935 sb->new_layout = cpu_to_le32(mddev->new_layout);
1936 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1937 sb->new_level = cpu_to_le32(mddev->new_level);
1938 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
1939 if (mddev->delta_disks == 0 &&
1940 mddev->reshape_backwards)
1942 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
1943 if (rdev->new_data_offset != rdev->data_offset) {
1945 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1946 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1947 - rdev->data_offset));
1951 if (mddev_is_clustered(mddev))
1952 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
1954 if (rdev->badblocks.count == 0)
1955 /* Nothing to do for bad blocks*/ ;
1956 else if (sb->bblog_offset == 0)
1957 /* Cannot record bad blocks on this device */
1958 md_error(mddev, rdev);
1960 struct badblocks *bb = &rdev->badblocks;
1961 __le64 *bbp = (__le64 *)page_address(rdev->bb_page);
1963 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1968 seq = read_seqbegin(&bb->lock);
1970 memset(bbp, 0xff, PAGE_SIZE);
1972 for (i = 0 ; i < bb->count ; i++) {
1973 u64 internal_bb = p[i];
1974 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1975 | BB_LEN(internal_bb));
1976 bbp[i] = cpu_to_le64(store_bb);
1979 if (read_seqretry(&bb->lock, seq))
1982 bb->sector = (rdev->sb_start +
1983 (int)le32_to_cpu(sb->bblog_offset));
1984 bb->size = le16_to_cpu(sb->bblog_size);
1989 rdev_for_each(rdev2, mddev)
1990 if (rdev2->desc_nr+1 > max_dev)
1991 max_dev = rdev2->desc_nr+1;
1993 if (max_dev > le32_to_cpu(sb->max_dev)) {
1995 sb->max_dev = cpu_to_le32(max_dev);
1996 rdev->sb_size = max_dev * 2 + 256;
1997 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1998 if (rdev->sb_size & bmask)
1999 rdev->sb_size = (rdev->sb_size | bmask) + 1;
2001 max_dev = le32_to_cpu(sb->max_dev);
2003 for (i=0; i<max_dev;i++)
2004 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
2006 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
2007 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
2009 if (test_bit(MD_HAS_PPL, &mddev->flags)) {
2010 if (test_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags))
2012 cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS);
2014 sb->feature_map |= cpu_to_le32(MD_FEATURE_PPL);
2015 sb->ppl.offset = cpu_to_le16(rdev->ppl.offset);
2016 sb->ppl.size = cpu_to_le16(rdev->ppl.size);
2019 rdev_for_each(rdev2, mddev) {
2021 if (test_bit(Faulty, &rdev2->flags))
2022 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
2023 else if (test_bit(In_sync, &rdev2->flags))
2024 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
2025 else if (test_bit(Journal, &rdev2->flags))
2026 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
2027 else if (rdev2->raid_disk >= 0)
2028 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
2030 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
2033 sb->sb_csum = calc_sb_1_csum(sb);
2036 static unsigned long long
2037 super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
2039 struct mdp_superblock_1 *sb;
2040 sector_t max_sectors;
2041 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
2042 return 0; /* component must fit device */
2043 if (rdev->data_offset != rdev->new_data_offset)
2044 return 0; /* too confusing */
2045 if (rdev->sb_start < rdev->data_offset) {
2046 /* minor versions 1 and 2; superblock before data */
2047 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
2048 max_sectors -= rdev->data_offset;
2049 if (!num_sectors || num_sectors > max_sectors)
2050 num_sectors = max_sectors;
2051 } else if (rdev->mddev->bitmap_info.offset) {
2052 /* minor version 0 with bitmap we can't move */
2055 /* minor version 0; superblock after data */
2057 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
2058 sb_start &= ~(sector_t)(4*2 - 1);
2059 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
2060 if (!num_sectors || num_sectors > max_sectors)
2061 num_sectors = max_sectors;
2062 rdev->sb_start = sb_start;
2064 sb = page_address(rdev->sb_page);
2065 sb->data_size = cpu_to_le64(num_sectors);
2066 sb->super_offset = cpu_to_le64(rdev->sb_start);
2067 sb->sb_csum = calc_sb_1_csum(sb);
2069 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
2071 } while (md_super_wait(rdev->mddev) < 0);
2077 super_1_allow_new_offset(struct md_rdev *rdev,
2078 unsigned long long new_offset)
2080 /* All necessary checks on new >= old have been done */
2081 struct bitmap *bitmap;
2082 if (new_offset >= rdev->data_offset)
2085 /* with 1.0 metadata, there is no metadata to tread on
2086 * so we can always move back */
2087 if (rdev->mddev->minor_version == 0)
2090 /* otherwise we must be sure not to step on
2091 * any metadata, so stay:
2092 * 36K beyond start of superblock
2093 * beyond end of badblocks
2094 * beyond write-intent bitmap
2096 if (rdev->sb_start + (32+4)*2 > new_offset)
2098 bitmap = rdev->mddev->bitmap;
2099 if (bitmap && !rdev->mddev->bitmap_info.file &&
2100 rdev->sb_start + rdev->mddev->bitmap_info.offset +
2101 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
2103 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
2109 static struct super_type super_types[] = {
2112 .owner = THIS_MODULE,
2113 .load_super = super_90_load,
2114 .validate_super = super_90_validate,
2115 .sync_super = super_90_sync,
2116 .rdev_size_change = super_90_rdev_size_change,
2117 .allow_new_offset = super_90_allow_new_offset,
2121 .owner = THIS_MODULE,
2122 .load_super = super_1_load,
2123 .validate_super = super_1_validate,
2124 .sync_super = super_1_sync,
2125 .rdev_size_change = super_1_rdev_size_change,
2126 .allow_new_offset = super_1_allow_new_offset,
2130 static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
2132 if (mddev->sync_super) {
2133 mddev->sync_super(mddev, rdev);
2137 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
2139 super_types[mddev->major_version].sync_super(mddev, rdev);
2142 static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
2144 struct md_rdev *rdev, *rdev2;
2147 rdev_for_each_rcu(rdev, mddev1) {
2148 if (test_bit(Faulty, &rdev->flags) ||
2149 test_bit(Journal, &rdev->flags) ||
2150 rdev->raid_disk == -1)
2152 rdev_for_each_rcu(rdev2, mddev2) {
2153 if (test_bit(Faulty, &rdev2->flags) ||
2154 test_bit(Journal, &rdev2->flags) ||
2155 rdev2->raid_disk == -1)
2157 if (rdev->bdev->bd_contains ==
2158 rdev2->bdev->bd_contains) {
2168 static LIST_HEAD(pending_raid_disks);
2171 * Try to register data integrity profile for an mddev
2173 * This is called when an array is started and after a disk has been kicked
2174 * from the array. It only succeeds if all working and active component devices
2175 * are integrity capable with matching profiles.
2177 int md_integrity_register(struct mddev *mddev)
2179 struct md_rdev *rdev, *reference = NULL;
2181 if (list_empty(&mddev->disks))
2182 return 0; /* nothing to do */
2183 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2184 return 0; /* shouldn't register, or already is */
2185 rdev_for_each(rdev, mddev) {
2186 /* skip spares and non-functional disks */
2187 if (test_bit(Faulty, &rdev->flags))
2189 if (rdev->raid_disk < 0)
2192 /* Use the first rdev as the reference */
2196 /* does this rdev's profile match the reference profile? */
2197 if (blk_integrity_compare(reference->bdev->bd_disk,
2198 rdev->bdev->bd_disk) < 0)
2201 if (!reference || !bdev_get_integrity(reference->bdev))
2204 * All component devices are integrity capable and have matching
2205 * profiles, register the common profile for the md device.
2207 blk_integrity_register(mddev->gendisk,
2208 bdev_get_integrity(reference->bdev));
2210 pr_debug("md: data integrity enabled on %s\n", mdname(mddev));
2211 if (bioset_integrity_create(&mddev->bio_set, BIO_POOL_SIZE)) {
2212 pr_err("md: failed to create integrity pool for %s\n",
2218 EXPORT_SYMBOL(md_integrity_register);
2221 * Attempt to add an rdev, but only if it is consistent with the current
2224 int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
2226 struct blk_integrity *bi_mddev;
2227 char name[BDEVNAME_SIZE];
2229 if (!mddev->gendisk)
2232 bi_mddev = blk_get_integrity(mddev->gendisk);
2234 if (!bi_mddev) /* nothing to do */
2237 if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
2238 pr_err("%s: incompatible integrity profile for %s\n",
2239 mdname(mddev), bdevname(rdev->bdev, name));
2245 EXPORT_SYMBOL(md_integrity_add_rdev);
2247 static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
2249 char b[BDEVNAME_SIZE];
2253 /* prevent duplicates */
2254 if (find_rdev(mddev, rdev->bdev->bd_dev))
2257 if ((bdev_read_only(rdev->bdev) || bdev_read_only(rdev->meta_bdev)) &&
2261 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2262 if (!test_bit(Journal, &rdev->flags) &&
2264 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
2266 /* Cannot change size, so fail
2267 * If mddev->level <= 0, then we don't care
2268 * about aligning sizes (e.g. linear)
2270 if (mddev->level > 0)
2273 mddev->dev_sectors = rdev->sectors;
2276 /* Verify rdev->desc_nr is unique.
2277 * If it is -1, assign a free number, else
2278 * check number is not in use
2281 if (rdev->desc_nr < 0) {
2284 choice = mddev->raid_disks;
2285 while (md_find_rdev_nr_rcu(mddev, choice))
2287 rdev->desc_nr = choice;
2289 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
2295 if (!test_bit(Journal, &rdev->flags) &&
2296 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2297 pr_warn("md: %s: array is limited to %d devices\n",
2298 mdname(mddev), mddev->max_disks);
2301 bdevname(rdev->bdev,b);
2302 strreplace(b, '/', '!');
2304 rdev->mddev = mddev;
2305 pr_debug("md: bind<%s>\n", b);
2307 if (mddev->raid_disks)
2308 mddev_create_wb_pool(mddev, rdev, false);
2310 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
2313 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
2314 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2315 /* failure here is OK */;
2316 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
2318 list_add_rcu(&rdev->same_set, &mddev->disks);
2319 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
2321 /* May as well allow recovery to be retried once */
2322 mddev->recovery_disabled++;
2327 pr_warn("md: failed to register dev-%s for %s\n",
2332 static void md_delayed_delete(struct work_struct *ws)
2334 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
2335 kobject_del(&rdev->kobj);
2336 kobject_put(&rdev->kobj);
2339 static void unbind_rdev_from_array(struct md_rdev *rdev)
2341 char b[BDEVNAME_SIZE];
2343 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
2344 list_del_rcu(&rdev->same_set);
2345 pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b));
2346 mddev_destroy_wb_pool(rdev->mddev, rdev);
2348 sysfs_remove_link(&rdev->kobj, "block");
2349 sysfs_put(rdev->sysfs_state);
2350 rdev->sysfs_state = NULL;
2351 rdev->badblocks.count = 0;
2352 /* We need to delay this, otherwise we can deadlock when
2353 * writing to 'remove' to "dev/state". We also need
2354 * to delay it due to rcu usage.
2357 INIT_WORK(&rdev->del_work, md_delayed_delete);
2358 kobject_get(&rdev->kobj);
2359 queue_work(md_misc_wq, &rdev->del_work);
2363 * prevent the device from being mounted, repartitioned or
2364 * otherwise reused by a RAID array (or any other kernel
2365 * subsystem), by bd_claiming the device.
2367 static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
2370 struct block_device *bdev;
2371 char b[BDEVNAME_SIZE];
2373 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
2374 shared ? (struct md_rdev *)lock_rdev : rdev);
2376 pr_warn("md: could not open %s.\n", __bdevname(dev, b));
2377 return PTR_ERR(bdev);
2383 static void unlock_rdev(struct md_rdev *rdev)
2385 struct block_device *bdev = rdev->bdev;
2387 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
2390 void md_autodetect_dev(dev_t dev);
2392 static void export_rdev(struct md_rdev *rdev)
2394 char b[BDEVNAME_SIZE];
2396 pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b));
2397 md_rdev_clear(rdev);
2399 if (test_bit(AutoDetected, &rdev->flags))
2400 md_autodetect_dev(rdev->bdev->bd_dev);
2403 kobject_put(&rdev->kobj);
2406 void md_kick_rdev_from_array(struct md_rdev *rdev)
2408 unbind_rdev_from_array(rdev);
2411 EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
2413 static void export_array(struct mddev *mddev)
2415 struct md_rdev *rdev;
2417 while (!list_empty(&mddev->disks)) {
2418 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2420 md_kick_rdev_from_array(rdev);
2422 mddev->raid_disks = 0;
2423 mddev->major_version = 0;
2426 static bool set_in_sync(struct mddev *mddev)
2428 lockdep_assert_held(&mddev->lock);
2429 if (!mddev->in_sync) {
2430 mddev->sync_checkers++;
2431 spin_unlock(&mddev->lock);
2432 percpu_ref_switch_to_atomic_sync(&mddev->writes_pending);
2433 spin_lock(&mddev->lock);
2434 if (!mddev->in_sync &&
2435 percpu_ref_is_zero(&mddev->writes_pending)) {
2438 * Ensure ->in_sync is visible before we clear
2442 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2443 sysfs_notify_dirent_safe(mddev->sysfs_state);
2445 if (--mddev->sync_checkers == 0)
2446 percpu_ref_switch_to_percpu(&mddev->writes_pending);
2448 if (mddev->safemode == 1)
2449 mddev->safemode = 0;
2450 return mddev->in_sync;
2453 static void sync_sbs(struct mddev *mddev, int nospares)
2455 /* Update each superblock (in-memory image), but
2456 * if we are allowed to, skip spares which already
2457 * have the right event counter, or have one earlier
2458 * (which would mean they aren't being marked as dirty
2459 * with the rest of the array)
2461 struct md_rdev *rdev;
2462 rdev_for_each(rdev, mddev) {
2463 if (rdev->sb_events == mddev->events ||
2465 rdev->raid_disk < 0 &&
2466 rdev->sb_events+1 == mddev->events)) {
2467 /* Don't update this superblock */
2468 rdev->sb_loaded = 2;
2470 sync_super(mddev, rdev);
2471 rdev->sb_loaded = 1;
2476 static bool does_sb_need_changing(struct mddev *mddev)
2478 struct md_rdev *rdev;
2479 struct mdp_superblock_1 *sb;
2482 /* Find a good rdev */
2483 rdev_for_each(rdev, mddev)
2484 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2487 /* No good device found. */
2491 sb = page_address(rdev->sb_page);
2492 /* Check if a device has become faulty or a spare become active */
2493 rdev_for_each(rdev, mddev) {
2494 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2495 /* Device activated? */
2496 if (role == 0xffff && rdev->raid_disk >=0 &&
2497 !test_bit(Faulty, &rdev->flags))
2499 /* Device turned faulty? */
2500 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2504 /* Check if any mddev parameters have changed */
2505 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2506 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
2507 (mddev->layout != le32_to_cpu(sb->layout)) ||
2508 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2509 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2515 void md_update_sb(struct mddev *mddev, int force_change)
2517 struct md_rdev *rdev;
2520 int any_badblocks_changed = 0;
2525 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2530 if (mddev_is_clustered(mddev)) {
2531 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
2533 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
2535 ret = md_cluster_ops->metadata_update_start(mddev);
2536 /* Has someone else has updated the sb */
2537 if (!does_sb_need_changing(mddev)) {
2539 md_cluster_ops->metadata_update_cancel(mddev);
2540 bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2541 BIT(MD_SB_CHANGE_DEVS) |
2542 BIT(MD_SB_CHANGE_CLEAN));
2548 * First make sure individual recovery_offsets are correct
2549 * curr_resync_completed can only be used during recovery.
2550 * During reshape/resync it might use array-addresses rather
2551 * that device addresses.
2553 rdev_for_each(rdev, mddev) {
2554 if (rdev->raid_disk >= 0 &&
2555 mddev->delta_disks >= 0 &&
2556 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
2557 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery) &&
2558 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
2559 !test_bit(Journal, &rdev->flags) &&
2560 !test_bit(In_sync, &rdev->flags) &&
2561 mddev->curr_resync_completed > rdev->recovery_offset)
2562 rdev->recovery_offset = mddev->curr_resync_completed;
2565 if (!mddev->persistent) {
2566 clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2567 clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2568 if (!mddev->external) {
2569 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
2570 rdev_for_each(rdev, mddev) {
2571 if (rdev->badblocks.changed) {
2572 rdev->badblocks.changed = 0;
2573 ack_all_badblocks(&rdev->badblocks);
2574 md_error(mddev, rdev);
2576 clear_bit(Blocked, &rdev->flags);
2577 clear_bit(BlockedBadBlocks, &rdev->flags);
2578 wake_up(&rdev->blocked_wait);
2581 wake_up(&mddev->sb_wait);
2585 spin_lock(&mddev->lock);
2587 mddev->utime = ktime_get_real_seconds();
2589 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
2591 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
2592 /* just a clean<-> dirty transition, possibly leave spares alone,
2593 * though if events isn't the right even/odd, we will have to do
2599 if (mddev->degraded)
2600 /* If the array is degraded, then skipping spares is both
2601 * dangerous and fairly pointless.
2602 * Dangerous because a device that was removed from the array
2603 * might have a event_count that still looks up-to-date,
2604 * so it can be re-added without a resync.
2605 * Pointless because if there are any spares to skip,
2606 * then a recovery will happen and soon that array won't
2607 * be degraded any more and the spare can go back to sleep then.
2611 sync_req = mddev->in_sync;
2613 /* If this is just a dirty<->clean transition, and the array is clean
2614 * and 'events' is odd, we can roll back to the previous clean state */
2616 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
2617 && mddev->can_decrease_events
2618 && mddev->events != 1) {
2620 mddev->can_decrease_events = 0;
2622 /* otherwise we have to go forward and ... */
2624 mddev->can_decrease_events = nospares;
2628 * This 64-bit counter should never wrap.
2629 * Either we are in around ~1 trillion A.C., assuming
2630 * 1 reboot per second, or we have a bug...
2632 WARN_ON(mddev->events == 0);
2634 rdev_for_each(rdev, mddev) {
2635 if (rdev->badblocks.changed)
2636 any_badblocks_changed++;
2637 if (test_bit(Faulty, &rdev->flags))
2638 set_bit(FaultRecorded, &rdev->flags);
2641 sync_sbs(mddev, nospares);
2642 spin_unlock(&mddev->lock);
2644 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2645 mdname(mddev), mddev->in_sync);
2648 blk_add_trace_msg(mddev->queue, "md md_update_sb");
2650 md_bitmap_update_sb(mddev->bitmap);
2651 rdev_for_each(rdev, mddev) {
2652 char b[BDEVNAME_SIZE];
2654 if (rdev->sb_loaded != 1)
2655 continue; /* no noise on spare devices */
2657 if (!test_bit(Faulty, &rdev->flags)) {
2658 md_super_write(mddev,rdev,
2659 rdev->sb_start, rdev->sb_size,
2661 pr_debug("md: (write) %s's sb offset: %llu\n",
2662 bdevname(rdev->bdev, b),
2663 (unsigned long long)rdev->sb_start);
2664 rdev->sb_events = mddev->events;
2665 if (rdev->badblocks.size) {
2666 md_super_write(mddev, rdev,
2667 rdev->badblocks.sector,
2668 rdev->badblocks.size << 9,
2670 rdev->badblocks.size = 0;
2674 pr_debug("md: %s (skipping faulty)\n",
2675 bdevname(rdev->bdev, b));
2677 if (mddev->level == LEVEL_MULTIPATH)
2678 /* only need to write one superblock... */
2681 if (md_super_wait(mddev) < 0)
2683 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
2685 if (mddev_is_clustered(mddev) && ret == 0)
2686 md_cluster_ops->metadata_update_finish(mddev);
2688 if (mddev->in_sync != sync_req ||
2689 !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2690 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN)))
2691 /* have to write it out again */
2693 wake_up(&mddev->sb_wait);
2694 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2695 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
2697 rdev_for_each(rdev, mddev) {
2698 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2699 clear_bit(Blocked, &rdev->flags);
2701 if (any_badblocks_changed)
2702 ack_all_badblocks(&rdev->badblocks);
2703 clear_bit(BlockedBadBlocks, &rdev->flags);
2704 wake_up(&rdev->blocked_wait);
2707 EXPORT_SYMBOL(md_update_sb);
2709 static int add_bound_rdev(struct md_rdev *rdev)
2711 struct mddev *mddev = rdev->mddev;
2713 bool add_journal = test_bit(Journal, &rdev->flags);
2715 if (!mddev->pers->hot_remove_disk || add_journal) {
2716 /* If there is hot_add_disk but no hot_remove_disk
2717 * then added disks for geometry changes,
2718 * and should be added immediately.
2720 super_types[mddev->major_version].
2721 validate_super(mddev, rdev);
2723 mddev_suspend(mddev);
2724 err = mddev->pers->hot_add_disk(mddev, rdev);
2726 mddev_resume(mddev);
2728 md_kick_rdev_from_array(rdev);
2732 sysfs_notify_dirent_safe(rdev->sysfs_state);
2734 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2735 if (mddev->degraded)
2736 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2737 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2738 md_new_event(mddev);
2739 md_wakeup_thread(mddev->thread);
2743 /* words written to sysfs files may, or may not, be \n terminated.
2744 * We want to accept with case. For this we use cmd_match.
2746 static int cmd_match(const char *cmd, const char *str)
2748 /* See if cmd, written into a sysfs file, matches
2749 * str. They must either be the same, or cmd can
2750 * have a trailing newline
2752 while (*cmd && *str && *cmd == *str) {
2763 struct rdev_sysfs_entry {
2764 struct attribute attr;
2765 ssize_t (*show)(struct md_rdev *, char *);
2766 ssize_t (*store)(struct md_rdev *, const char *, size_t);
2770 state_show(struct md_rdev *rdev, char *page)
2774 unsigned long flags = READ_ONCE(rdev->flags);
2776 if (test_bit(Faulty, &flags) ||
2777 (!test_bit(ExternalBbl, &flags) &&
2778 rdev->badblocks.unacked_exist))
2779 len += sprintf(page+len, "faulty%s", sep);
2780 if (test_bit(In_sync, &flags))
2781 len += sprintf(page+len, "in_sync%s", sep);
2782 if (test_bit(Journal, &flags))
2783 len += sprintf(page+len, "journal%s", sep);
2784 if (test_bit(WriteMostly, &flags))
2785 len += sprintf(page+len, "write_mostly%s", sep);
2786 if (test_bit(Blocked, &flags) ||
2787 (rdev->badblocks.unacked_exist
2788 && !test_bit(Faulty, &flags)))
2789 len += sprintf(page+len, "blocked%s", sep);
2790 if (!test_bit(Faulty, &flags) &&
2791 !test_bit(Journal, &flags) &&
2792 !test_bit(In_sync, &flags))
2793 len += sprintf(page+len, "spare%s", sep);
2794 if (test_bit(WriteErrorSeen, &flags))
2795 len += sprintf(page+len, "write_error%s", sep);
2796 if (test_bit(WantReplacement, &flags))
2797 len += sprintf(page+len, "want_replacement%s", sep);
2798 if (test_bit(Replacement, &flags))
2799 len += sprintf(page+len, "replacement%s", sep);
2800 if (test_bit(ExternalBbl, &flags))
2801 len += sprintf(page+len, "external_bbl%s", sep);
2802 if (test_bit(FailFast, &flags))
2803 len += sprintf(page+len, "failfast%s", sep);
2808 return len+sprintf(page+len, "\n");
2812 state_store(struct md_rdev *rdev, const char *buf, size_t len)
2815 * faulty - simulates an error
2816 * remove - disconnects the device
2817 * writemostly - sets write_mostly
2818 * -writemostly - clears write_mostly
2819 * blocked - sets the Blocked flags
2820 * -blocked - clears the Blocked and possibly simulates an error
2821 * insync - sets Insync providing device isn't active
2822 * -insync - clear Insync for a device with a slot assigned,
2823 * so that it gets rebuilt based on bitmap
2824 * write_error - sets WriteErrorSeen
2825 * -write_error - clears WriteErrorSeen
2826 * {,-}failfast - set/clear FailFast
2829 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2830 md_error(rdev->mddev, rdev);
2831 if (test_bit(Faulty, &rdev->flags))
2835 } else if (cmd_match(buf, "remove")) {
2836 if (rdev->mddev->pers) {
2837 clear_bit(Blocked, &rdev->flags);
2838 remove_and_add_spares(rdev->mddev, rdev);
2840 if (rdev->raid_disk >= 0)
2843 struct mddev *mddev = rdev->mddev;
2845 if (mddev_is_clustered(mddev))
2846 err = md_cluster_ops->remove_disk(mddev, rdev);
2849 md_kick_rdev_from_array(rdev);
2851 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2852 md_wakeup_thread(mddev->thread);
2854 md_new_event(mddev);
2857 } else if (cmd_match(buf, "writemostly")) {
2858 set_bit(WriteMostly, &rdev->flags);
2859 mddev_create_wb_pool(rdev->mddev, rdev, false);
2861 } else if (cmd_match(buf, "-writemostly")) {
2862 mddev_destroy_wb_pool(rdev->mddev, rdev);
2863 clear_bit(WriteMostly, &rdev->flags);
2865 } else if (cmd_match(buf, "blocked")) {
2866 set_bit(Blocked, &rdev->flags);
2868 } else if (cmd_match(buf, "-blocked")) {
2869 if (!test_bit(Faulty, &rdev->flags) &&
2870 !test_bit(ExternalBbl, &rdev->flags) &&
2871 rdev->badblocks.unacked_exist) {
2872 /* metadata handler doesn't understand badblocks,
2873 * so we need to fail the device
2875 md_error(rdev->mddev, rdev);
2877 clear_bit(Blocked, &rdev->flags);
2878 clear_bit(BlockedBadBlocks, &rdev->flags);
2879 wake_up(&rdev->blocked_wait);
2880 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2881 md_wakeup_thread(rdev->mddev->thread);
2884 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2885 set_bit(In_sync, &rdev->flags);
2887 } else if (cmd_match(buf, "failfast")) {
2888 set_bit(FailFast, &rdev->flags);
2890 } else if (cmd_match(buf, "-failfast")) {
2891 clear_bit(FailFast, &rdev->flags);
2893 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
2894 !test_bit(Journal, &rdev->flags)) {
2895 if (rdev->mddev->pers == NULL) {
2896 clear_bit(In_sync, &rdev->flags);
2897 rdev->saved_raid_disk = rdev->raid_disk;
2898 rdev->raid_disk = -1;
2901 } else if (cmd_match(buf, "write_error")) {
2902 set_bit(WriteErrorSeen, &rdev->flags);
2904 } else if (cmd_match(buf, "-write_error")) {
2905 clear_bit(WriteErrorSeen, &rdev->flags);
2907 } else if (cmd_match(buf, "want_replacement")) {
2908 /* Any non-spare device that is not a replacement can
2909 * become want_replacement at any time, but we then need to
2910 * check if recovery is needed.
2912 if (rdev->raid_disk >= 0 &&
2913 !test_bit(Journal, &rdev->flags) &&
2914 !test_bit(Replacement, &rdev->flags))
2915 set_bit(WantReplacement, &rdev->flags);
2916 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2917 md_wakeup_thread(rdev->mddev->thread);
2919 } else if (cmd_match(buf, "-want_replacement")) {
2920 /* Clearing 'want_replacement' is always allowed.
2921 * Once replacements starts it is too late though.
2924 clear_bit(WantReplacement, &rdev->flags);
2925 } else if (cmd_match(buf, "replacement")) {
2926 /* Can only set a device as a replacement when array has not
2927 * yet been started. Once running, replacement is automatic
2928 * from spares, or by assigning 'slot'.
2930 if (rdev->mddev->pers)
2933 set_bit(Replacement, &rdev->flags);
2936 } else if (cmd_match(buf, "-replacement")) {
2937 /* Similarly, can only clear Replacement before start */
2938 if (rdev->mddev->pers)
2941 clear_bit(Replacement, &rdev->flags);
2944 } else if (cmd_match(buf, "re-add")) {
2945 if (!rdev->mddev->pers)
2947 else if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1) &&
2948 rdev->saved_raid_disk >= 0) {
2949 /* clear_bit is performed _after_ all the devices
2950 * have their local Faulty bit cleared. If any writes
2951 * happen in the meantime in the local node, they
2952 * will land in the local bitmap, which will be synced
2953 * by this node eventually
2955 if (!mddev_is_clustered(rdev->mddev) ||
2956 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2957 clear_bit(Faulty, &rdev->flags);
2958 err = add_bound_rdev(rdev);
2962 } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) {
2963 set_bit(ExternalBbl, &rdev->flags);
2964 rdev->badblocks.shift = 0;
2966 } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) {
2967 clear_bit(ExternalBbl, &rdev->flags);
2971 sysfs_notify_dirent_safe(rdev->sysfs_state);
2972 return err ? err : len;
2974 static struct rdev_sysfs_entry rdev_state =
2975 __ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
2978 errors_show(struct md_rdev *rdev, char *page)
2980 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2984 errors_store(struct md_rdev *rdev, const char *buf, size_t len)
2989 rv = kstrtouint(buf, 10, &n);
2992 atomic_set(&rdev->corrected_errors, n);
2995 static struct rdev_sysfs_entry rdev_errors =
2996 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
2999 slot_show(struct md_rdev *rdev, char *page)
3001 if (test_bit(Journal, &rdev->flags))
3002 return sprintf(page, "journal\n");
3003 else if (rdev->raid_disk < 0)
3004 return sprintf(page, "none\n");
3006 return sprintf(page, "%d\n", rdev->raid_disk);
3010 slot_store(struct md_rdev *rdev, const char *buf, size_t len)
3015 if (test_bit(Journal, &rdev->flags))
3017 if (strncmp(buf, "none", 4)==0)
3020 err = kstrtouint(buf, 10, (unsigned int *)&slot);
3024 if (rdev->mddev->pers && slot == -1) {
3025 /* Setting 'slot' on an active array requires also
3026 * updating the 'rd%d' link, and communicating
3027 * with the personality with ->hot_*_disk.
3028 * For now we only support removing
3029 * failed/spare devices. This normally happens automatically,
3030 * but not when the metadata is externally managed.
3032 if (rdev->raid_disk == -1)
3034 /* personality does all needed checks */
3035 if (rdev->mddev->pers->hot_remove_disk == NULL)
3037 clear_bit(Blocked, &rdev->flags);
3038 remove_and_add_spares(rdev->mddev, rdev);
3039 if (rdev->raid_disk >= 0)
3041 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
3042 md_wakeup_thread(rdev->mddev->thread);
3043 } else if (rdev->mddev->pers) {
3044 /* Activating a spare .. or possibly reactivating
3045 * if we ever get bitmaps working here.
3049 if (rdev->raid_disk != -1)
3052 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
3055 if (rdev->mddev->pers->hot_add_disk == NULL)
3058 if (slot >= rdev->mddev->raid_disks &&
3059 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
3062 rdev->raid_disk = slot;
3063 if (test_bit(In_sync, &rdev->flags))
3064 rdev->saved_raid_disk = slot;
3066 rdev->saved_raid_disk = -1;
3067 clear_bit(In_sync, &rdev->flags);
3068 clear_bit(Bitmap_sync, &rdev->flags);
3069 err = rdev->mddev->pers->
3070 hot_add_disk(rdev->mddev, rdev);
3072 rdev->raid_disk = -1;
3075 sysfs_notify_dirent_safe(rdev->sysfs_state);
3076 if (sysfs_link_rdev(rdev->mddev, rdev))
3077 /* failure here is OK */;
3078 /* don't wakeup anyone, leave that to userspace. */
3080 if (slot >= rdev->mddev->raid_disks &&
3081 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
3083 rdev->raid_disk = slot;
3084 /* assume it is working */
3085 clear_bit(Faulty, &rdev->flags);
3086 clear_bit(WriteMostly, &rdev->flags);
3087 set_bit(In_sync, &rdev->flags);
3088 sysfs_notify_dirent_safe(rdev->sysfs_state);
3093 static struct rdev_sysfs_entry rdev_slot =
3094 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
3097 offset_show(struct md_rdev *rdev, char *page)
3099 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
3103 offset_store(struct md_rdev *rdev, const char *buf, size_t len)
3105 unsigned long long offset;
3106 if (kstrtoull(buf, 10, &offset) < 0)
3108 if (rdev->mddev->pers && rdev->raid_disk >= 0)
3110 if (rdev->sectors && rdev->mddev->external)
3111 /* Must set offset before size, so overlap checks
3114 rdev->data_offset = offset;
3115 rdev->new_data_offset = offset;
3119 static struct rdev_sysfs_entry rdev_offset =
3120 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
3122 static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
3124 return sprintf(page, "%llu\n",
3125 (unsigned long long)rdev->new_data_offset);
3128 static ssize_t new_offset_store(struct md_rdev *rdev,
3129 const char *buf, size_t len)
3131 unsigned long long new_offset;
3132 struct mddev *mddev = rdev->mddev;
3134 if (kstrtoull(buf, 10, &new_offset) < 0)
3137 if (mddev->sync_thread ||
3138 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
3140 if (new_offset == rdev->data_offset)
3141 /* reset is always permitted */
3143 else if (new_offset > rdev->data_offset) {
3144 /* must not push array size beyond rdev_sectors */
3145 if (new_offset - rdev->data_offset
3146 + mddev->dev_sectors > rdev->sectors)
3149 /* Metadata worries about other space details. */
3151 /* decreasing the offset is inconsistent with a backwards
3154 if (new_offset < rdev->data_offset &&
3155 mddev->reshape_backwards)
3157 /* Increasing offset is inconsistent with forwards
3158 * reshape. reshape_direction should be set to
3159 * 'backwards' first.
3161 if (new_offset > rdev->data_offset &&
3162 !mddev->reshape_backwards)
3165 if (mddev->pers && mddev->persistent &&
3166 !super_types[mddev->major_version]
3167 .allow_new_offset(rdev, new_offset))
3169 rdev->new_data_offset = new_offset;
3170 if (new_offset > rdev->data_offset)
3171 mddev->reshape_backwards = 1;
3172 else if (new_offset < rdev->data_offset)
3173 mddev->reshape_backwards = 0;
3177 static struct rdev_sysfs_entry rdev_new_offset =
3178 __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
3181 rdev_size_show(struct md_rdev *rdev, char *page)
3183 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
3186 static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
3188 /* check if two start/length pairs overlap */
3196 static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
3198 unsigned long long blocks;
3201 if (kstrtoull(buf, 10, &blocks) < 0)
3204 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
3205 return -EINVAL; /* sector conversion overflow */
3208 if (new != blocks * 2)
3209 return -EINVAL; /* unsigned long long to sector_t overflow */
3216 rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3218 struct mddev *my_mddev = rdev->mddev;
3219 sector_t oldsectors = rdev->sectors;
3222 if (test_bit(Journal, &rdev->flags))
3224 if (strict_blocks_to_sectors(buf, §ors) < 0)
3226 if (rdev->data_offset != rdev->new_data_offset)
3227 return -EINVAL; /* too confusing */
3228 if (my_mddev->pers && rdev->raid_disk >= 0) {
3229 if (my_mddev->persistent) {
3230 sectors = super_types[my_mddev->major_version].
3231 rdev_size_change(rdev, sectors);
3234 } else if (!sectors)
3235 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
3237 if (!my_mddev->pers->resize)
3238 /* Cannot change size for RAID0 or Linear etc */
3241 if (sectors < my_mddev->dev_sectors)
3242 return -EINVAL; /* component must fit device */
3244 rdev->sectors = sectors;
3245 if (sectors > oldsectors && my_mddev->external) {
3246 /* Need to check that all other rdevs with the same
3247 * ->bdev do not overlap. 'rcu' is sufficient to walk
3248 * the rdev lists safely.
3249 * This check does not provide a hard guarantee, it
3250 * just helps avoid dangerous mistakes.
3252 struct mddev *mddev;
3254 struct list_head *tmp;
3257 for_each_mddev(mddev, tmp) {
3258 struct md_rdev *rdev2;
3260 rdev_for_each(rdev2, mddev)
3261 if (rdev->bdev == rdev2->bdev &&
3263 overlaps(rdev->data_offset, rdev->sectors,
3276 /* Someone else could have slipped in a size
3277 * change here, but doing so is just silly.
3278 * We put oldsectors back because we *know* it is
3279 * safe, and trust userspace not to race with
3282 rdev->sectors = oldsectors;
3289 static struct rdev_sysfs_entry rdev_size =
3290 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
3292 static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
3294 unsigned long long recovery_start = rdev->recovery_offset;
3296 if (test_bit(In_sync, &rdev->flags) ||
3297 recovery_start == MaxSector)
3298 return sprintf(page, "none\n");
3300 return sprintf(page, "%llu\n", recovery_start);
3303 static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
3305 unsigned long long recovery_start;
3307 if (cmd_match(buf, "none"))
3308 recovery_start = MaxSector;
3309 else if (kstrtoull(buf, 10, &recovery_start))
3312 if (rdev->mddev->pers &&
3313 rdev->raid_disk >= 0)
3316 rdev->recovery_offset = recovery_start;
3317 if (recovery_start == MaxSector)
3318 set_bit(In_sync, &rdev->flags);
3320 clear_bit(In_sync, &rdev->flags);
3324 static struct rdev_sysfs_entry rdev_recovery_start =
3325 __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3327 /* sysfs access to bad-blocks list.
3328 * We present two files.
3329 * 'bad-blocks' lists sector numbers and lengths of ranges that
3330 * are recorded as bad. The list is truncated to fit within
3331 * the one-page limit of sysfs.
3332 * Writing "sector length" to this file adds an acknowledged
3334 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3335 * been acknowledged. Writing to this file adds bad blocks
3336 * without acknowledging them. This is largely for testing.
3338 static ssize_t bb_show(struct md_rdev *rdev, char *page)
3340 return badblocks_show(&rdev->badblocks, page, 0);
3342 static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
3344 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3345 /* Maybe that ack was all we needed */
3346 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3347 wake_up(&rdev->blocked_wait);
3350 static struct rdev_sysfs_entry rdev_bad_blocks =
3351 __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3353 static ssize_t ubb_show(struct md_rdev *rdev, char *page)
3355 return badblocks_show(&rdev->badblocks, page, 1);
3357 static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
3359 return badblocks_store(&rdev->badblocks, page, len, 1);
3361 static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3362 __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3365 ppl_sector_show(struct md_rdev *rdev, char *page)
3367 return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector);
3371 ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len)
3373 unsigned long long sector;
3375 if (kstrtoull(buf, 10, §or) < 0)
3377 if (sector != (sector_t)sector)
3380 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3381 rdev->raid_disk >= 0)
3384 if (rdev->mddev->persistent) {
3385 if (rdev->mddev->major_version == 0)
3387 if ((sector > rdev->sb_start &&
3388 sector - rdev->sb_start > S16_MAX) ||
3389 (sector < rdev->sb_start &&
3390 rdev->sb_start - sector > -S16_MIN))
3392 rdev->ppl.offset = sector - rdev->sb_start;
3393 } else if (!rdev->mddev->external) {
3396 rdev->ppl.sector = sector;
3400 static struct rdev_sysfs_entry rdev_ppl_sector =
3401 __ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store);
3404 ppl_size_show(struct md_rdev *rdev, char *page)
3406 return sprintf(page, "%u\n", rdev->ppl.size);
3410 ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3414 if (kstrtouint(buf, 10, &size) < 0)
3417 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3418 rdev->raid_disk >= 0)
3421 if (rdev->mddev->persistent) {
3422 if (rdev->mddev->major_version == 0)
3426 } else if (!rdev->mddev->external) {
3429 rdev->ppl.size = size;
3433 static struct rdev_sysfs_entry rdev_ppl_size =
3434 __ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store);
3436 static struct attribute *rdev_default_attrs[] = {
3441 &rdev_new_offset.attr,
3443 &rdev_recovery_start.attr,
3444 &rdev_bad_blocks.attr,
3445 &rdev_unack_bad_blocks.attr,
3446 &rdev_ppl_sector.attr,
3447 &rdev_ppl_size.attr,
3451 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3453 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
3454 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
3460 return entry->show(rdev, page);
3464 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3465 const char *page, size_t length)
3467 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
3468 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
3470 struct mddev *mddev = rdev->mddev;
3474 if (!capable(CAP_SYS_ADMIN))
3476 rv = mddev ? mddev_lock(mddev) : -ENODEV;
3478 if (rdev->mddev == NULL)
3481 rv = entry->store(rdev, page, length);
3482 mddev_unlock(mddev);
3487 static void rdev_free(struct kobject *ko)
3489 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
3492 static const struct sysfs_ops rdev_sysfs_ops = {
3493 .show = rdev_attr_show,
3494 .store = rdev_attr_store,
3496 static struct kobj_type rdev_ktype = {
3497 .release = rdev_free,
3498 .sysfs_ops = &rdev_sysfs_ops,
3499 .default_attrs = rdev_default_attrs,
3502 int md_rdev_init(struct md_rdev *rdev)
3505 rdev->saved_raid_disk = -1;
3506 rdev->raid_disk = -1;
3508 rdev->data_offset = 0;
3509 rdev->new_data_offset = 0;
3510 rdev->sb_events = 0;
3511 rdev->last_read_error = 0;
3512 rdev->sb_loaded = 0;
3513 rdev->bb_page = NULL;
3514 atomic_set(&rdev->nr_pending, 0);
3515 atomic_set(&rdev->read_errors, 0);
3516 atomic_set(&rdev->corrected_errors, 0);
3518 INIT_LIST_HEAD(&rdev->same_set);
3519 init_waitqueue_head(&rdev->blocked_wait);
3521 /* Add space to store bad block list.
3522 * This reserves the space even on arrays where it cannot
3523 * be used - I wonder if that matters
3525 return badblocks_init(&rdev->badblocks, 0);
3527 EXPORT_SYMBOL_GPL(md_rdev_init);
3529 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3531 * mark the device faulty if:
3533 * - the device is nonexistent (zero size)
3534 * - the device has no valid superblock
3536 * a faulty rdev _never_ has rdev->sb set.
3538 static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
3540 char b[BDEVNAME_SIZE];
3542 struct md_rdev *rdev;
3545 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
3547 return ERR_PTR(-ENOMEM);
3549 err = md_rdev_init(rdev);
3552 err = alloc_disk_sb(rdev);
3556 err = lock_rdev(rdev, newdev, super_format == -2);
3560 kobject_init(&rdev->kobj, &rdev_ktype);
3562 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
3564 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
3565 bdevname(rdev->bdev,b));
3570 if (super_format >= 0) {
3571 err = super_types[super_format].
3572 load_super(rdev, NULL, super_minor);
3573 if (err == -EINVAL) {
3574 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
3575 bdevname(rdev->bdev,b),
3576 super_format, super_minor);
3580 pr_warn("md: could not read %s's sb, not importing!\n",
3581 bdevname(rdev->bdev,b));
3591 md_rdev_clear(rdev);
3593 return ERR_PTR(err);
3597 * Check a full RAID array for plausibility
3600 static void analyze_sbs(struct mddev *mddev)
3603 struct md_rdev *rdev, *freshest, *tmp;
3604 char b[BDEVNAME_SIZE];
3607 rdev_for_each_safe(rdev, tmp, mddev)
3608 switch (super_types[mddev->major_version].
3609 load_super(rdev, freshest, mddev->minor_version)) {
3616 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
3617 bdevname(rdev->bdev,b));
3618 md_kick_rdev_from_array(rdev);
3621 super_types[mddev->major_version].
3622 validate_super(mddev, freshest);
3625 rdev_for_each_safe(rdev, tmp, mddev) {
3626 if (mddev->max_disks &&
3627 (rdev->desc_nr >= mddev->max_disks ||
3628 i > mddev->max_disks)) {
3629 pr_warn("md: %s: %s: only %d devices permitted\n",
3630 mdname(mddev), bdevname(rdev->bdev, b),
3632 md_kick_rdev_from_array(rdev);
3635 if (rdev != freshest) {
3636 if (super_types[mddev->major_version].
3637 validate_super(mddev, rdev)) {
3638 pr_warn("md: kicking non-fresh %s from array!\n",
3639 bdevname(rdev->bdev,b));
3640 md_kick_rdev_from_array(rdev);
3644 if (mddev->level == LEVEL_MULTIPATH) {
3645 rdev->desc_nr = i++;
3646 rdev->raid_disk = rdev->desc_nr;
3647 set_bit(In_sync, &rdev->flags);
3648 } else if (rdev->raid_disk >=
3649 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3650 !test_bit(Journal, &rdev->flags)) {
3651 rdev->raid_disk = -1;
3652 clear_bit(In_sync, &rdev->flags);
3657 /* Read a fixed-point number.
3658 * Numbers in sysfs attributes should be in "standard" units where
3659 * possible, so time should be in seconds.
3660 * However we internally use a a much smaller unit such as
3661 * milliseconds or jiffies.
3662 * This function takes a decimal number with a possible fractional
3663 * component, and produces an integer which is the result of
3664 * multiplying that number by 10^'scale'.
3665 * all without any floating-point arithmetic.
3667 int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3669 unsigned long result = 0;
3671 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3674 else if (decimals < scale) {
3677 result = result * 10 + value;
3689 *res = result * int_pow(10, scale - decimals);
3694 safe_delay_show(struct mddev *mddev, char *page)
3696 int msec = (mddev->safemode_delay*1000)/HZ;
3697 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3700 safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
3704 if (mddev_is_clustered(mddev)) {
3705 pr_warn("md: Safemode is disabled for clustered mode\n");
3709 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
3712 mddev->safemode_delay = 0;
3714 unsigned long old_delay = mddev->safemode_delay;
3715 unsigned long new_delay = (msec*HZ)/1000;
3719 mddev->safemode_delay = new_delay;
3720 if (new_delay < old_delay || old_delay == 0)
3721 mod_timer(&mddev->safemode_timer, jiffies+1);
3725 static struct md_sysfs_entry md_safe_delay =
3726 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
3729 level_show(struct mddev *mddev, char *page)
3731 struct md_personality *p;
3733 spin_lock(&mddev->lock);
3736 ret = sprintf(page, "%s\n", p->name);
3737 else if (mddev->clevel[0])
3738 ret = sprintf(page, "%s\n", mddev->clevel);
3739 else if (mddev->level != LEVEL_NONE)
3740 ret = sprintf(page, "%d\n", mddev->level);
3743 spin_unlock(&mddev->lock);
3748 level_store(struct mddev *mddev, const char *buf, size_t len)
3753 struct md_personality *pers, *oldpers;
3755 void *priv, *oldpriv;
3756 struct md_rdev *rdev;
3758 if (slen == 0 || slen >= sizeof(clevel))
3761 rv = mddev_lock(mddev);
3765 if (mddev->pers == NULL) {
3766 strncpy(mddev->clevel, buf, slen);
3767 if (mddev->clevel[slen-1] == '\n')
3769 mddev->clevel[slen] = 0;
3770 mddev->level = LEVEL_NONE;
3778 /* request to change the personality. Need to ensure:
3779 * - array is not engaged in resync/recovery/reshape
3780 * - old personality can be suspended
3781 * - new personality will access other array.
3785 if (mddev->sync_thread ||
3786 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3787 mddev->reshape_position != MaxSector ||
3788 mddev->sysfs_active)
3792 if (!mddev->pers->quiesce) {
3793 pr_warn("md: %s: %s does not support online personality change\n",
3794 mdname(mddev), mddev->pers->name);
3798 /* Now find the new personality */
3799 strncpy(clevel, buf, slen);
3800 if (clevel[slen-1] == '\n')
3803 if (kstrtol(clevel, 10, &level))
3806 if (request_module("md-%s", clevel) != 0)
3807 request_module("md-level-%s", clevel);
3808 spin_lock(&pers_lock);
3809 pers = find_pers(level, clevel);
3810 if (!pers || !try_module_get(pers->owner)) {
3811 spin_unlock(&pers_lock);
3812 pr_warn("md: personality %s not loaded\n", clevel);
3816 spin_unlock(&pers_lock);
3818 if (pers == mddev->pers) {
3819 /* Nothing to do! */
3820 module_put(pers->owner);
3824 if (!pers->takeover) {
3825 module_put(pers->owner);
3826 pr_warn("md: %s: %s does not support personality takeover\n",
3827 mdname(mddev), clevel);
3832 rdev_for_each(rdev, mddev)
3833 rdev->new_raid_disk = rdev->raid_disk;
3835 /* ->takeover must set new_* and/or delta_disks
3836 * if it succeeds, and may set them when it fails.
3838 priv = pers->takeover(mddev);
3840 mddev->new_level = mddev->level;
3841 mddev->new_layout = mddev->layout;
3842 mddev->new_chunk_sectors = mddev->chunk_sectors;
3843 mddev->raid_disks -= mddev->delta_disks;
3844 mddev->delta_disks = 0;
3845 mddev->reshape_backwards = 0;
3846 module_put(pers->owner);
3847 pr_warn("md: %s: %s would not accept array\n",
3848 mdname(mddev), clevel);
3853 /* Looks like we have a winner */
3854 mddev_suspend(mddev);
3855 mddev_detach(mddev);
3857 spin_lock(&mddev->lock);
3858 oldpers = mddev->pers;
3859 oldpriv = mddev->private;
3861 mddev->private = priv;
3862 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3863 mddev->level = mddev->new_level;
3864 mddev->layout = mddev->new_layout;
3865 mddev->chunk_sectors = mddev->new_chunk_sectors;
3866 mddev->delta_disks = 0;
3867 mddev->reshape_backwards = 0;
3868 mddev->degraded = 0;
3869 spin_unlock(&mddev->lock);
3871 if (oldpers->sync_request == NULL &&
3873 /* We are converting from a no-redundancy array
3874 * to a redundancy array and metadata is managed
3875 * externally so we need to be sure that writes
3876 * won't block due to a need to transition
3878 * until external management is started.
3881 mddev->safemode_delay = 0;
3882 mddev->safemode = 0;
3885 oldpers->free(mddev, oldpriv);
3887 if (oldpers->sync_request == NULL &&
3888 pers->sync_request != NULL) {
3889 /* need to add the md_redundancy_group */
3890 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3891 pr_warn("md: cannot register extra attributes for %s\n",
3893 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3895 if (oldpers->sync_request != NULL &&
3896 pers->sync_request == NULL) {
3897 /* need to remove the md_redundancy_group */
3898 if (mddev->to_remove == NULL)
3899 mddev->to_remove = &md_redundancy_group;
3902 module_put(oldpers->owner);
3904 rdev_for_each(rdev, mddev) {
3905 if (rdev->raid_disk < 0)
3907 if (rdev->new_raid_disk >= mddev->raid_disks)
3908 rdev->new_raid_disk = -1;
3909 if (rdev->new_raid_disk == rdev->raid_disk)
3911 sysfs_unlink_rdev(mddev, rdev);
3913 rdev_for_each(rdev, mddev) {
3914 if (rdev->raid_disk < 0)
3916 if (rdev->new_raid_disk == rdev->raid_disk)
3918 rdev->raid_disk = rdev->new_raid_disk;
3919 if (rdev->raid_disk < 0)
3920 clear_bit(In_sync, &rdev->flags);
3922 if (sysfs_link_rdev(mddev, rdev))
3923 pr_warn("md: cannot register rd%d for %s after level change\n",
3924 rdev->raid_disk, mdname(mddev));
3928 if (pers->sync_request == NULL) {
3929 /* this is now an array without redundancy, so
3930 * it must always be in_sync
3933 del_timer_sync(&mddev->safemode_timer);
3935 blk_set_stacking_limits(&mddev->queue->limits);
3937 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
3938 mddev_resume(mddev);
3940 md_update_sb(mddev, 1);
3941 sysfs_notify(&mddev->kobj, NULL, "level");
3942 md_new_event(mddev);
3945 mddev_unlock(mddev);
3949 static struct md_sysfs_entry md_level =
3950 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
3953 layout_show(struct mddev *mddev, char *page)
3955 /* just a number, not meaningful for all levels */
3956 if (mddev->reshape_position != MaxSector &&
3957 mddev->layout != mddev->new_layout)
3958 return sprintf(page, "%d (%d)\n",
3959 mddev->new_layout, mddev->layout);
3960 return sprintf(page, "%d\n", mddev->layout);
3964 layout_store(struct mddev *mddev, const char *buf, size_t len)
3969 err = kstrtouint(buf, 10, &n);
3972 err = mddev_lock(mddev);
3977 if (mddev->pers->check_reshape == NULL)
3982 mddev->new_layout = n;
3983 err = mddev->pers->check_reshape(mddev);
3985 mddev->new_layout = mddev->layout;
3988 mddev->new_layout = n;
3989 if (mddev->reshape_position == MaxSector)
3992 mddev_unlock(mddev);
3995 static struct md_sysfs_entry md_layout =
3996 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
3999 raid_disks_show(struct mddev *mddev, char *page)
4001 if (mddev->raid_disks == 0)
4003 if (mddev->reshape_position != MaxSector &&
4004 mddev->delta_disks != 0)
4005 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
4006 mddev->raid_disks - mddev->delta_disks);
4007 return sprintf(page, "%d\n", mddev->raid_disks);
4010 static int update_raid_disks(struct mddev *mddev, int raid_disks);
4013 raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
4018 err = kstrtouint(buf, 10, &n);
4022 err = mddev_lock(mddev);
4026 err = update_raid_disks(mddev, n);
4027 else if (mddev->reshape_position != MaxSector) {
4028 struct md_rdev *rdev;
4029 int olddisks = mddev->raid_disks - mddev->delta_disks;
4032 rdev_for_each(rdev, mddev) {
4034 rdev->data_offset < rdev->new_data_offset)
4037 rdev->data_offset > rdev->new_data_offset)
4041 mddev->delta_disks = n - olddisks;
4042 mddev->raid_disks = n;
4043 mddev->reshape_backwards = (mddev->delta_disks < 0);
4045 mddev->raid_disks = n;
4047 mddev_unlock(mddev);
4048 return err ? err : len;
4050 static struct md_sysfs_entry md_raid_disks =
4051 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
4054 chunk_size_show(struct mddev *mddev, char *page)
4056 if (mddev->reshape_position != MaxSector &&
4057 mddev->chunk_sectors != mddev->new_chunk_sectors)
4058 return sprintf(page, "%d (%d)\n",
4059 mddev->new_chunk_sectors << 9,
4060 mddev->chunk_sectors << 9);
4061 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
4065 chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
4070 err = kstrtoul(buf, 10, &n);
4074 err = mddev_lock(mddev);
4078 if (mddev->pers->check_reshape == NULL)
4083 mddev->new_chunk_sectors = n >> 9;
4084 err = mddev->pers->check_reshape(mddev);
4086 mddev->new_chunk_sectors = mddev->chunk_sectors;
4089 mddev->new_chunk_sectors = n >> 9;
4090 if (mddev->reshape_position == MaxSector)
4091 mddev->chunk_sectors = n >> 9;
4093 mddev_unlock(mddev);
4096 static struct md_sysfs_entry md_chunk_size =
4097 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
4100 resync_start_show(struct mddev *mddev, char *page)
4102 if (mddev->recovery_cp == MaxSector)
4103 return sprintf(page, "none\n");
4104 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
4108 resync_start_store(struct mddev *mddev, const char *buf, size_t len)
4110 unsigned long long n;
4113 if (cmd_match(buf, "none"))
4116 err = kstrtoull(buf, 10, &n);
4119 if (n != (sector_t)n)
4123 err = mddev_lock(mddev);
4126 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4130 mddev->recovery_cp = n;
4132 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
4134 mddev_unlock(mddev);
4137 static struct md_sysfs_entry md_resync_start =
4138 __ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
4139 resync_start_show, resync_start_store);
4142 * The array state can be:
4145 * No devices, no size, no level
4146 * Equivalent to STOP_ARRAY ioctl
4148 * May have some settings, but array is not active
4149 * all IO results in error
4150 * When written, doesn't tear down array, but just stops it
4151 * suspended (not supported yet)
4152 * All IO requests will block. The array can be reconfigured.
4153 * Writing this, if accepted, will block until array is quiescent
4155 * no resync can happen. no superblocks get written.
4156 * write requests fail
4158 * like readonly, but behaves like 'clean' on a write request.
4160 * clean - no pending writes, but otherwise active.
4161 * When written to inactive array, starts without resync
4162 * If a write request arrives then
4163 * if metadata is known, mark 'dirty' and switch to 'active'.
4164 * if not known, block and switch to write-pending
4165 * If written to an active array that has pending writes, then fails.
4167 * fully active: IO and resync can be happening.
4168 * When written to inactive array, starts with resync
4171 * clean, but writes are blocked waiting for 'active' to be written.
4174 * like active, but no writes have been seen for a while (100msec).
4177 * RAID0/LINEAR-only: same as clean, but array is missing a member.
4178 * It's useful because RAID0/LINEAR mounted-arrays aren't stopped
4179 * when a member is gone, so this state will at least alert the
4180 * user that something is wrong.
4182 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
4183 write_pending, active_idle, broken, bad_word};
4184 static char *array_states[] = {
4185 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
4186 "write-pending", "active-idle", "broken", NULL };
4188 static int match_word(const char *word, char **list)
4191 for (n=0; list[n]; n++)
4192 if (cmd_match(word, list[n]))
4198 array_state_show(struct mddev *mddev, char *page)
4200 enum array_state st = inactive;
4202 if (mddev->pers && !test_bit(MD_NOT_READY, &mddev->flags)) {
4211 spin_lock(&mddev->lock);
4212 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
4214 else if (mddev->in_sync)
4216 else if (mddev->safemode)
4220 spin_unlock(&mddev->lock);
4223 if (test_bit(MD_BROKEN, &mddev->flags) && st == clean)
4226 if (list_empty(&mddev->disks) &&
4227 mddev->raid_disks == 0 &&
4228 mddev->dev_sectors == 0)
4233 return sprintf(page, "%s\n", array_states[st]);
4236 static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
4237 static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
4238 static int do_md_run(struct mddev *mddev);
4239 static int restart_array(struct mddev *mddev);
4242 array_state_store(struct mddev *mddev, const char *buf, size_t len)
4245 enum array_state st = match_word(buf, array_states);
4247 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
4248 /* don't take reconfig_mutex when toggling between
4251 spin_lock(&mddev->lock);
4253 restart_array(mddev);
4254 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
4255 md_wakeup_thread(mddev->thread);
4256 wake_up(&mddev->sb_wait);
4257 } else /* st == clean */ {
4258 restart_array(mddev);
4259 if (!set_in_sync(mddev))
4263 sysfs_notify_dirent_safe(mddev->sysfs_state);
4264 spin_unlock(&mddev->lock);
4267 err = mddev_lock(mddev);
4275 /* stopping an active array */
4276 err = do_md_stop(mddev, 0, NULL);
4279 /* stopping an active array */
4281 err = do_md_stop(mddev, 2, NULL);
4283 err = 0; /* already inactive */
4286 break; /* not supported yet */
4289 err = md_set_readonly(mddev, NULL);
4292 set_disk_ro(mddev->gendisk, 1);
4293 err = do_md_run(mddev);
4299 err = md_set_readonly(mddev, NULL);
4300 else if (mddev->ro == 1)
4301 err = restart_array(mddev);
4304 set_disk_ro(mddev->gendisk, 0);
4308 err = do_md_run(mddev);
4313 err = restart_array(mddev);
4316 spin_lock(&mddev->lock);
4317 if (!set_in_sync(mddev))
4319 spin_unlock(&mddev->lock);
4325 err = restart_array(mddev);
4328 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
4329 wake_up(&mddev->sb_wait);
4333 set_disk_ro(mddev->gendisk, 0);
4334 err = do_md_run(mddev);
4340 /* these cannot be set */
4345 if (mddev->hold_active == UNTIL_IOCTL)
4346 mddev->hold_active = 0;
4347 sysfs_notify_dirent_safe(mddev->sysfs_state);
4349 mddev_unlock(mddev);
4352 static struct md_sysfs_entry md_array_state =
4353 __ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
4356 max_corrected_read_errors_show(struct mddev *mddev, char *page) {
4357 return sprintf(page, "%d\n",
4358 atomic_read(&mddev->max_corr_read_errors));
4362 max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
4367 rv = kstrtouint(buf, 10, &n);
4370 atomic_set(&mddev->max_corr_read_errors, n);
4374 static struct md_sysfs_entry max_corr_read_errors =
4375 __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4376 max_corrected_read_errors_store);
4379 null_show(struct mddev *mddev, char *page)
4385 new_dev_store(struct mddev *mddev, const char *buf, size_t len)
4387 /* buf must be %d:%d\n? giving major and minor numbers */
4388 /* The new device is added to the array.
4389 * If the array has a persistent superblock, we read the
4390 * superblock to initialise info and check validity.
4391 * Otherwise, only checking done is that in bind_rdev_to_array,
4392 * which mainly checks size.
4395 int major = simple_strtoul(buf, &e, 10);
4398 struct md_rdev *rdev;
4401 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4403 minor = simple_strtoul(e+1, &e, 10);
4404 if (*e && *e != '\n')
4406 dev = MKDEV(major, minor);
4407 if (major != MAJOR(dev) ||
4408 minor != MINOR(dev))
4411 flush_workqueue(md_misc_wq);
4413 err = mddev_lock(mddev);
4416 if (mddev->persistent) {
4417 rdev = md_import_device(dev, mddev->major_version,
4418 mddev->minor_version);
4419 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
4420 struct md_rdev *rdev0
4421 = list_entry(mddev->disks.next,
4422 struct md_rdev, same_set);
4423 err = super_types[mddev->major_version]
4424 .load_super(rdev, rdev0, mddev->minor_version);
4428 } else if (mddev->external)
4429 rdev = md_import_device(dev, -2, -1);
4431 rdev = md_import_device(dev, -1, -1);
4434 mddev_unlock(mddev);
4435 return PTR_ERR(rdev);
4437 err = bind_rdev_to_array(rdev, mddev);
4441 mddev_unlock(mddev);
4443 md_new_event(mddev);
4444 return err ? err : len;
4447 static struct md_sysfs_entry md_new_device =
4448 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
4451 bitmap_store(struct mddev *mddev, const char *buf, size_t len)
4454 unsigned long chunk, end_chunk;
4457 err = mddev_lock(mddev);
4462 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4464 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4465 if (buf == end) break;
4466 if (*end == '-') { /* range */
4468 end_chunk = simple_strtoul(buf, &end, 0);
4469 if (buf == end) break;
4471 if (*end && !isspace(*end)) break;
4472 md_bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
4473 buf = skip_spaces(end);
4475 md_bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4477 mddev_unlock(mddev);
4481 static struct md_sysfs_entry md_bitmap =
4482 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4485 size_show(struct mddev *mddev, char *page)
4487 return sprintf(page, "%llu\n",
4488 (unsigned long long)mddev->dev_sectors / 2);
4491 static int update_size(struct mddev *mddev, sector_t num_sectors);
4494 size_store(struct mddev *mddev, const char *buf, size_t len)
4496 /* If array is inactive, we can reduce the component size, but
4497 * not increase it (except from 0).
4498 * If array is active, we can try an on-line resize
4501 int err = strict_blocks_to_sectors(buf, §ors);
4505 err = mddev_lock(mddev);
4509 err = update_size(mddev, sectors);
4511 md_update_sb(mddev, 1);
4513 if (mddev->dev_sectors == 0 ||
4514 mddev->dev_sectors > sectors)
4515 mddev->dev_sectors = sectors;
4519 mddev_unlock(mddev);
4520 return err ? err : len;
4523 static struct md_sysfs_entry md_size =
4524 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
4526 /* Metadata version.
4528 * 'none' for arrays with no metadata (good luck...)
4529 * 'external' for arrays with externally managed metadata,
4530 * or N.M for internally known formats
4533 metadata_show(struct mddev *mddev, char *page)
4535 if (mddev->persistent)
4536 return sprintf(page, "%d.%d\n",
4537 mddev->major_version, mddev->minor_version);
4538 else if (mddev->external)
4539 return sprintf(page, "external:%s\n", mddev->metadata_type);
4541 return sprintf(page, "none\n");
4545 metadata_store(struct mddev *mddev, const char *buf, size_t len)
4550 /* Changing the details of 'external' metadata is
4551 * always permitted. Otherwise there must be
4552 * no devices attached to the array.
4555 err = mddev_lock(mddev);
4559 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4561 else if (!list_empty(&mddev->disks))
4565 if (cmd_match(buf, "none")) {
4566 mddev->persistent = 0;
4567 mddev->external = 0;
4568 mddev->major_version = 0;
4569 mddev->minor_version = 90;
4572 if (strncmp(buf, "external:", 9) == 0) {
4573 size_t namelen = len-9;
4574 if (namelen >= sizeof(mddev->metadata_type))
4575 namelen = sizeof(mddev->metadata_type)-1;
4576 strncpy(mddev->metadata_type, buf+9, namelen);
4577 mddev->metadata_type[namelen] = 0;
4578 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4579 mddev->metadata_type[--namelen] = 0;
4580 mddev->persistent = 0;
4581 mddev->external = 1;
4582 mddev->major_version = 0;
4583 mddev->minor_version = 90;
4586 major = simple_strtoul(buf, &e, 10);
4588 if (e==buf || *e != '.')
4591 minor = simple_strtoul(buf, &e, 10);
4592 if (e==buf || (*e && *e != '\n') )
4595 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
4597 mddev->major_version = major;
4598 mddev->minor_version = minor;
4599 mddev->persistent = 1;
4600 mddev->external = 0;
4603 mddev_unlock(mddev);
4607 static struct md_sysfs_entry md_metadata =
4608 __ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
4611 action_show(struct mddev *mddev, char *page)
4613 char *type = "idle";
4614 unsigned long recovery = mddev->recovery;
4615 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
4617 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4618 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4619 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
4621 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4622 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
4624 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
4628 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
4630 else if (mddev->reshape_position != MaxSector)
4633 return sprintf(page, "%s\n", type);
4637 action_store(struct mddev *mddev, const char *page, size_t len)
4639 if (!mddev->pers || !mddev->pers->sync_request)
4643 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
4644 if (cmd_match(page, "frozen"))
4645 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4647 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4648 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4649 mddev_lock(mddev) == 0) {
4650 flush_workqueue(md_misc_wq);
4651 if (mddev->sync_thread) {
4652 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4653 md_reap_sync_thread(mddev);
4655 mddev_unlock(mddev);
4657 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4659 else if (cmd_match(page, "resync"))
4660 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4661 else if (cmd_match(page, "recover")) {
4662 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4663 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4664 } else if (cmd_match(page, "reshape")) {
4666 if (mddev->pers->start_reshape == NULL)
4668 err = mddev_lock(mddev);
4670 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4673 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4674 err = mddev->pers->start_reshape(mddev);
4676 mddev_unlock(mddev);
4680 sysfs_notify(&mddev->kobj, NULL, "degraded");
4682 if (cmd_match(page, "check"))
4683 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4684 else if (!cmd_match(page, "repair"))
4686 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4687 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4688 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4690 if (mddev->ro == 2) {
4691 /* A write to sync_action is enough to justify
4692 * canceling read-auto mode
4695 md_wakeup_thread(mddev->sync_thread);
4697 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4698 md_wakeup_thread(mddev->thread);
4699 sysfs_notify_dirent_safe(mddev->sysfs_action);
4703 static struct md_sysfs_entry md_scan_mode =
4704 __ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
4707 last_sync_action_show(struct mddev *mddev, char *page)
4709 return sprintf(page, "%s\n", mddev->last_sync_action);
4712 static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4715 mismatch_cnt_show(struct mddev *mddev, char *page)
4717 return sprintf(page, "%llu\n",
4718 (unsigned long long)
4719 atomic64_read(&mddev->resync_mismatches));
4722 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
4725 sync_min_show(struct mddev *mddev, char *page)
4727 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4728 mddev->sync_speed_min ? "local": "system");
4732 sync_min_store(struct mddev *mddev, const char *buf, size_t len)
4737 if (strncmp(buf, "system", 6)==0) {
4740 rv = kstrtouint(buf, 10, &min);
4746 mddev->sync_speed_min = min;
4750 static struct md_sysfs_entry md_sync_min =
4751 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4754 sync_max_show(struct mddev *mddev, char *page)
4756 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4757 mddev->sync_speed_max ? "local": "system");
4761 sync_max_store(struct mddev *mddev, const char *buf, size_t len)
4766 if (strncmp(buf, "system", 6)==0) {
4769 rv = kstrtouint(buf, 10, &max);
4775 mddev->sync_speed_max = max;
4779 static struct md_sysfs_entry md_sync_max =
4780 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4783 degraded_show(struct mddev *mddev, char *page)
4785 return sprintf(page, "%d\n", mddev->degraded);
4787 static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
4790 sync_force_parallel_show(struct mddev *mddev, char *page)
4792 return sprintf(page, "%d\n", mddev->parallel_resync);
4796 sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
4800 if (kstrtol(buf, 10, &n))
4803 if (n != 0 && n != 1)
4806 mddev->parallel_resync = n;
4808 if (mddev->sync_thread)
4809 wake_up(&resync_wait);
4814 /* force parallel resync, even with shared block devices */
4815 static struct md_sysfs_entry md_sync_force_parallel =
4816 __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4817 sync_force_parallel_show, sync_force_parallel_store);
4820 sync_speed_show(struct mddev *mddev, char *page)
4822 unsigned long resync, dt, db;
4823 if (mddev->curr_resync == 0)
4824 return sprintf(page, "none\n");
4825 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4826 dt = (jiffies - mddev->resync_mark) / HZ;
4828 db = resync - mddev->resync_mark_cnt;
4829 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
4832 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
4835 sync_completed_show(struct mddev *mddev, char *page)
4837 unsigned long long max_sectors, resync;
4839 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4840 return sprintf(page, "none\n");
4842 if (mddev->curr_resync == 1 ||
4843 mddev->curr_resync == 2)
4844 return sprintf(page, "delayed\n");
4846 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4847 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4848 max_sectors = mddev->resync_max_sectors;
4850 max_sectors = mddev->dev_sectors;
4852 resync = mddev->curr_resync_completed;
4853 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
4856 static struct md_sysfs_entry md_sync_completed =
4857 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
4860 min_sync_show(struct mddev *mddev, char *page)
4862 return sprintf(page, "%llu\n",
4863 (unsigned long long)mddev->resync_min);
4866 min_sync_store(struct mddev *mddev, const char *buf, size_t len)
4868 unsigned long long min;
4871 if (kstrtoull(buf, 10, &min))
4874 spin_lock(&mddev->lock);
4876 if (min > mddev->resync_max)
4880 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4883 /* Round down to multiple of 4K for safety */
4884 mddev->resync_min = round_down(min, 8);
4888 spin_unlock(&mddev->lock);
4892 static struct md_sysfs_entry md_min_sync =
4893 __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4896 max_sync_show(struct mddev *mddev, char *page)
4898 if (mddev->resync_max == MaxSector)
4899 return sprintf(page, "max\n");
4901 return sprintf(page, "%llu\n",
4902 (unsigned long long)mddev->resync_max);
4905 max_sync_store(struct mddev *mddev, const char *buf, size_t len)
4908 spin_lock(&mddev->lock);
4909 if (strncmp(buf, "max", 3) == 0)
4910 mddev->resync_max = MaxSector;
4912 unsigned long long max;
4916 if (kstrtoull(buf, 10, &max))
4918 if (max < mddev->resync_min)
4922 if (max < mddev->resync_max &&
4924 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4927 /* Must be a multiple of chunk_size */
4928 chunk = mddev->chunk_sectors;
4930 sector_t temp = max;
4933 if (sector_div(temp, chunk))
4936 mddev->resync_max = max;
4938 wake_up(&mddev->recovery_wait);
4941 spin_unlock(&mddev->lock);
4945 static struct md_sysfs_entry md_max_sync =
4946 __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4949 suspend_lo_show(struct mddev *mddev, char *page)
4951 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4955 suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
4957 unsigned long long new;
4960 err = kstrtoull(buf, 10, &new);
4963 if (new != (sector_t)new)
4966 err = mddev_lock(mddev);
4970 if (mddev->pers == NULL ||
4971 mddev->pers->quiesce == NULL)
4973 mddev_suspend(mddev);
4974 mddev->suspend_lo = new;
4975 mddev_resume(mddev);
4979 mddev_unlock(mddev);
4982 static struct md_sysfs_entry md_suspend_lo =
4983 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4986 suspend_hi_show(struct mddev *mddev, char *page)
4988 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4992 suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
4994 unsigned long long new;
4997 err = kstrtoull(buf, 10, &new);
5000 if (new != (sector_t)new)
5003 err = mddev_lock(mddev);
5007 if (mddev->pers == NULL)
5010 mddev_suspend(mddev);
5011 mddev->suspend_hi = new;
5012 mddev_resume(mddev);
5016 mddev_unlock(mddev);
5019 static struct md_sysfs_entry md_suspend_hi =
5020 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
5023 reshape_position_show(struct mddev *mddev, char *page)
5025 if (mddev->reshape_position != MaxSector)
5026 return sprintf(page, "%llu\n",
5027 (unsigned long long)mddev->reshape_position);
5028 strcpy(page, "none\n");
5033 reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
5035 struct md_rdev *rdev;
5036 unsigned long long new;
5039 err = kstrtoull(buf, 10, &new);
5042 if (new != (sector_t)new)
5044 err = mddev_lock(mddev);
5050 mddev->reshape_position = new;
5051 mddev->delta_disks = 0;
5052 mddev->reshape_backwards = 0;
5053 mddev->new_level = mddev->level;
5054 mddev->new_layout = mddev->layout;
5055 mddev->new_chunk_sectors = mddev->chunk_sectors;
5056 rdev_for_each(rdev, mddev)
5057 rdev->new_data_offset = rdev->data_offset;
5060 mddev_unlock(mddev);
5064 static struct md_sysfs_entry md_reshape_position =
5065 __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
5066 reshape_position_store);
5069 reshape_direction_show(struct mddev *mddev, char *page)
5071 return sprintf(page, "%s\n",
5072 mddev->reshape_backwards ? "backwards" : "forwards");
5076 reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
5081 if (cmd_match(buf, "forwards"))
5083 else if (cmd_match(buf, "backwards"))
5087 if (mddev->reshape_backwards == backwards)
5090 err = mddev_lock(mddev);
5093 /* check if we are allowed to change */
5094 if (mddev->delta_disks)
5096 else if (mddev->persistent &&
5097 mddev->major_version == 0)
5100 mddev->reshape_backwards = backwards;
5101 mddev_unlock(mddev);
5105 static struct md_sysfs_entry md_reshape_direction =
5106 __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
5107 reshape_direction_store);
5110 array_size_show(struct mddev *mddev, char *page)
5112 if (mddev->external_size)
5113 return sprintf(page, "%llu\n",
5114 (unsigned long long)mddev->array_sectors/2);
5116 return sprintf(page, "default\n");
5120 array_size_store(struct mddev *mddev, const char *buf, size_t len)
5125 err = mddev_lock(mddev);
5129 /* cluster raid doesn't support change array_sectors */
5130 if (mddev_is_clustered(mddev)) {
5131 mddev_unlock(mddev);
5135 if (strncmp(buf, "default", 7) == 0) {
5137 sectors = mddev->pers->size(mddev, 0, 0);
5139 sectors = mddev->array_sectors;
5141 mddev->external_size = 0;
5143 if (strict_blocks_to_sectors(buf, §ors) < 0)
5145 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
5148 mddev->external_size = 1;
5152 mddev->array_sectors = sectors;
5154 set_capacity(mddev->gendisk, mddev->array_sectors);
5155 revalidate_disk(mddev->gendisk);
5158 mddev_unlock(mddev);
5162 static struct md_sysfs_entry md_array_size =
5163 __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
5167 consistency_policy_show(struct mddev *mddev, char *page)
5171 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5172 ret = sprintf(page, "journal\n");
5173 } else if (test_bit(MD_HAS_PPL, &mddev->flags)) {
5174 ret = sprintf(page, "ppl\n");
5175 } else if (mddev->bitmap) {
5176 ret = sprintf(page, "bitmap\n");
5177 } else if (mddev->pers) {
5178 if (mddev->pers->sync_request)
5179 ret = sprintf(page, "resync\n");
5181 ret = sprintf(page, "none\n");
5183 ret = sprintf(page, "unknown\n");
5190 consistency_policy_store(struct mddev *mddev, const char *buf, size_t len)
5195 if (mddev->pers->change_consistency_policy)
5196 err = mddev->pers->change_consistency_policy(mddev, buf);
5199 } else if (mddev->external && strncmp(buf, "ppl", 3) == 0) {
5200 set_bit(MD_HAS_PPL, &mddev->flags);
5205 return err ? err : len;
5208 static struct md_sysfs_entry md_consistency_policy =
5209 __ATTR(consistency_policy, S_IRUGO | S_IWUSR, consistency_policy_show,
5210 consistency_policy_store);
5212 static ssize_t fail_last_dev_show(struct mddev *mddev, char *page)
5214 return sprintf(page, "%d\n", mddev->fail_last_dev);
5218 * Setting fail_last_dev to true to allow last device to be forcibly removed
5219 * from RAID1/RAID10.
5222 fail_last_dev_store(struct mddev *mddev, const char *buf, size_t len)
5227 ret = kstrtobool(buf, &value);
5231 if (value != mddev->fail_last_dev)
5232 mddev->fail_last_dev = value;
5236 static struct md_sysfs_entry md_fail_last_dev =
5237 __ATTR(fail_last_dev, S_IRUGO | S_IWUSR, fail_last_dev_show,
5238 fail_last_dev_store);
5240 static struct attribute *md_default_attrs[] = {
5243 &md_raid_disks.attr,
5244 &md_chunk_size.attr,
5246 &md_resync_start.attr,
5248 &md_new_device.attr,
5249 &md_safe_delay.attr,
5250 &md_array_state.attr,
5251 &md_reshape_position.attr,
5252 &md_reshape_direction.attr,
5253 &md_array_size.attr,
5254 &max_corr_read_errors.attr,
5255 &md_consistency_policy.attr,
5256 &md_fail_last_dev.attr,
5260 static struct attribute *md_redundancy_attrs[] = {
5262 &md_last_scan_mode.attr,
5263 &md_mismatches.attr,
5266 &md_sync_speed.attr,
5267 &md_sync_force_parallel.attr,
5268 &md_sync_completed.attr,
5271 &md_suspend_lo.attr,
5272 &md_suspend_hi.attr,
5277 static struct attribute_group md_redundancy_group = {
5279 .attrs = md_redundancy_attrs,
5283 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
5285 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
5286 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
5291 spin_lock(&all_mddevs_lock);
5292 if (list_empty(&mddev->all_mddevs)) {
5293 spin_unlock(&all_mddevs_lock);
5297 spin_unlock(&all_mddevs_lock);
5299 rv = entry->show(mddev, page);
5305 md_attr_store(struct kobject *kobj, struct attribute *attr,
5306 const char *page, size_t length)
5308 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
5309 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
5314 if (!capable(CAP_SYS_ADMIN))
5316 spin_lock(&all_mddevs_lock);
5317 if (list_empty(&mddev->all_mddevs)) {
5318 spin_unlock(&all_mddevs_lock);
5322 spin_unlock(&all_mddevs_lock);
5323 rv = entry->store(mddev, page, length);
5328 static void md_free(struct kobject *ko)
5330 struct mddev *mddev = container_of(ko, struct mddev, kobj);
5332 if (mddev->sysfs_state)
5333 sysfs_put(mddev->sysfs_state);
5336 del_gendisk(mddev->gendisk);
5338 blk_cleanup_queue(mddev->queue);
5340 put_disk(mddev->gendisk);
5341 percpu_ref_exit(&mddev->writes_pending);
5343 bioset_exit(&mddev->bio_set);
5344 bioset_exit(&mddev->sync_set);
5348 static const struct sysfs_ops md_sysfs_ops = {
5349 .show = md_attr_show,
5350 .store = md_attr_store,
5352 static struct kobj_type md_ktype = {
5354 .sysfs_ops = &md_sysfs_ops,
5355 .default_attrs = md_default_attrs,
5360 static void mddev_delayed_delete(struct work_struct *ws)
5362 struct mddev *mddev = container_of(ws, struct mddev, del_work);
5364 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
5365 kobject_del(&mddev->kobj);
5366 kobject_put(&mddev->kobj);
5369 static void no_op(struct percpu_ref *r) {}
5371 int mddev_init_writes_pending(struct mddev *mddev)
5373 if (mddev->writes_pending.percpu_count_ptr)
5375 if (percpu_ref_init(&mddev->writes_pending, no_op,
5376 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL) < 0)
5378 /* We want to start with the refcount at zero */
5379 percpu_ref_put(&mddev->writes_pending);
5382 EXPORT_SYMBOL_GPL(mddev_init_writes_pending);
5384 static int md_alloc(dev_t dev, char *name)
5387 * If dev is zero, name is the name of a device to allocate with
5388 * an arbitrary minor number. It will be "md_???"
5389 * If dev is non-zero it must be a device number with a MAJOR of
5390 * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then
5391 * the device is being created by opening a node in /dev.
5392 * If "name" is not NULL, the device is being created by
5393 * writing to /sys/module/md_mod/parameters/new_array.
5395 static DEFINE_MUTEX(disks_mutex);
5396 struct mddev *mddev = mddev_find(dev);
5397 struct gendisk *disk;
5406 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
5407 shift = partitioned ? MdpMinorShift : 0;
5408 unit = MINOR(mddev->unit) >> shift;
5410 /* wait for any previous instance of this device to be
5411 * completely removed (mddev_delayed_delete).
5413 flush_workqueue(md_misc_wq);
5415 mutex_lock(&disks_mutex);
5421 /* Need to ensure that 'name' is not a duplicate.
5423 struct mddev *mddev2;
5424 spin_lock(&all_mddevs_lock);
5426 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5427 if (mddev2->gendisk &&
5428 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5429 spin_unlock(&all_mddevs_lock);
5432 spin_unlock(&all_mddevs_lock);
5436 * Creating /dev/mdNNN via "newarray", so adjust hold_active.
5438 mddev->hold_active = UNTIL_STOP;
5441 mddev->queue = blk_alloc_queue(GFP_KERNEL);
5444 mddev->queue->queuedata = mddev;
5446 blk_queue_make_request(mddev->queue, md_make_request);
5447 blk_set_stacking_limits(&mddev->queue->limits);
5449 disk = alloc_disk(1 << shift);
5451 blk_cleanup_queue(mddev->queue);
5452 mddev->queue = NULL;
5455 disk->major = MAJOR(mddev->unit);
5456 disk->first_minor = unit << shift;
5458 strcpy(disk->disk_name, name);
5459 else if (partitioned)
5460 sprintf(disk->disk_name, "md_d%d", unit);
5462 sprintf(disk->disk_name, "md%d", unit);
5463 disk->fops = &md_fops;
5464 disk->private_data = mddev;
5465 disk->queue = mddev->queue;
5466 blk_queue_write_cache(mddev->queue, true, true);
5467 /* Allow extended partitions. This makes the
5468 * 'mdp' device redundant, but we can't really
5471 disk->flags |= GENHD_FL_EXT_DEVT;
5472 mddev->gendisk = disk;
5473 /* As soon as we call add_disk(), another thread could get
5474 * through to md_open, so make sure it doesn't get too far
5476 mutex_lock(&mddev->open_mutex);
5479 error = kobject_add(&mddev->kobj, &disk_to_dev(disk)->kobj, "%s", "md");
5481 /* This isn't possible, but as kobject_init_and_add is marked
5482 * __must_check, we must do something with the result
5484 pr_debug("md: cannot register %s/md - name in use\n",
5488 if (mddev->kobj.sd &&
5489 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
5490 pr_debug("pointless warning\n");
5491 mutex_unlock(&mddev->open_mutex);
5493 mutex_unlock(&disks_mutex);
5494 if (!error && mddev->kobj.sd) {
5495 kobject_uevent(&mddev->kobj, KOBJ_ADD);
5496 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
5502 static struct kobject *md_probe(dev_t dev, int *part, void *data)
5505 md_alloc(dev, NULL);
5509 static int add_named_array(const char *val, const struct kernel_param *kp)
5512 * val must be "md_*" or "mdNNN".
5513 * For "md_*" we allocate an array with a large free minor number, and
5514 * set the name to val. val must not already be an active name.
5515 * For "mdNNN" we allocate an array with the minor number NNN
5516 * which must not already be in use.
5518 int len = strlen(val);
5519 char buf[DISK_NAME_LEN];
5520 unsigned long devnum;
5522 while (len && val[len-1] == '\n')
5524 if (len >= DISK_NAME_LEN)
5526 strlcpy(buf, val, len+1);
5527 if (strncmp(buf, "md_", 3) == 0)
5528 return md_alloc(0, buf);
5529 if (strncmp(buf, "md", 2) == 0 &&
5531 kstrtoul(buf+2, 10, &devnum) == 0 &&
5532 devnum <= MINORMASK)
5533 return md_alloc(MKDEV(MD_MAJOR, devnum), NULL);
5538 static void md_safemode_timeout(struct timer_list *t)
5540 struct mddev *mddev = from_timer(mddev, t, safemode_timer);
5542 mddev->safemode = 1;
5543 if (mddev->external)
5544 sysfs_notify_dirent_safe(mddev->sysfs_state);
5546 md_wakeup_thread(mddev->thread);
5549 static int start_dirty_degraded;
5551 int md_run(struct mddev *mddev)
5554 struct md_rdev *rdev;
5555 struct md_personality *pers;
5557 if (list_empty(&mddev->disks))
5558 /* cannot run an array with no devices.. */
5563 /* Cannot run until previous stop completes properly */
5564 if (mddev->sysfs_active)
5568 * Analyze all RAID superblock(s)
5570 if (!mddev->raid_disks) {
5571 if (!mddev->persistent)
5576 if (mddev->level != LEVEL_NONE)
5577 request_module("md-level-%d", mddev->level);
5578 else if (mddev->clevel[0])
5579 request_module("md-%s", mddev->clevel);
5582 * Drop all container device buffers, from now on
5583 * the only valid external interface is through the md
5586 mddev->has_superblocks = false;
5587 rdev_for_each(rdev, mddev) {
5588 if (test_bit(Faulty, &rdev->flags))
5590 sync_blockdev(rdev->bdev);
5591 invalidate_bdev(rdev->bdev);
5592 if (mddev->ro != 1 &&
5593 (bdev_read_only(rdev->bdev) ||
5594 bdev_read_only(rdev->meta_bdev))) {
5597 set_disk_ro(mddev->gendisk, 1);
5601 mddev->has_superblocks = true;
5603 /* perform some consistency tests on the device.
5604 * We don't want the data to overlap the metadata,
5605 * Internal Bitmap issues have been handled elsewhere.
5607 if (rdev->meta_bdev) {
5608 /* Nothing to check */;
5609 } else if (rdev->data_offset < rdev->sb_start) {
5610 if (mddev->dev_sectors &&
5611 rdev->data_offset + mddev->dev_sectors
5613 pr_warn("md: %s: data overlaps metadata\n",
5618 if (rdev->sb_start + rdev->sb_size/512
5619 > rdev->data_offset) {
5620 pr_warn("md: %s: metadata overlaps data\n",
5625 sysfs_notify_dirent_safe(rdev->sysfs_state);
5628 if (!bioset_initialized(&mddev->bio_set)) {
5629 err = bioset_init(&mddev->bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
5633 if (!bioset_initialized(&mddev->sync_set)) {
5634 err = bioset_init(&mddev->sync_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
5639 spin_lock(&pers_lock);
5640 pers = find_pers(mddev->level, mddev->clevel);
5641 if (!pers || !try_module_get(pers->owner)) {
5642 spin_unlock(&pers_lock);
5643 if (mddev->level != LEVEL_NONE)
5644 pr_warn("md: personality for level %d is not loaded!\n",
5647 pr_warn("md: personality for level %s is not loaded!\n",
5652 spin_unlock(&pers_lock);
5653 if (mddev->level != pers->level) {
5654 mddev->level = pers->level;
5655 mddev->new_level = pers->level;
5657 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
5659 if (mddev->reshape_position != MaxSector &&
5660 pers->start_reshape == NULL) {
5661 /* This personality cannot handle reshaping... */
5662 module_put(pers->owner);
5667 if (pers->sync_request) {
5668 /* Warn if this is a potentially silly
5671 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5672 struct md_rdev *rdev2;
5675 rdev_for_each(rdev, mddev)
5676 rdev_for_each(rdev2, mddev) {
5678 rdev->bdev->bd_contains ==
5679 rdev2->bdev->bd_contains) {
5680 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5682 bdevname(rdev->bdev,b),
5683 bdevname(rdev2->bdev,b2));
5689 pr_warn("True protection against single-disk failure might be compromised.\n");
5692 mddev->recovery = 0;
5693 /* may be over-ridden by personality */
5694 mddev->resync_max_sectors = mddev->dev_sectors;
5696 mddev->ok_start_degraded = start_dirty_degraded;
5698 if (start_readonly && mddev->ro == 0)
5699 mddev->ro = 2; /* read-only, but switch on first write */
5701 err = pers->run(mddev);
5703 pr_warn("md: pers->run() failed ...\n");
5704 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
5705 WARN_ONCE(!mddev->external_size,
5706 "%s: default size too small, but 'external_size' not in effect?\n",
5708 pr_warn("md: invalid array_size %llu > default size %llu\n",
5709 (unsigned long long)mddev->array_sectors / 2,
5710 (unsigned long long)pers->size(mddev, 0, 0) / 2);
5713 if (err == 0 && pers->sync_request &&
5714 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
5715 struct bitmap *bitmap;
5717 bitmap = md_bitmap_create(mddev, -1);
5718 if (IS_ERR(bitmap)) {
5719 err = PTR_ERR(bitmap);
5720 pr_warn("%s: failed to create bitmap (%d)\n",
5721 mdname(mddev), err);
5723 mddev->bitmap = bitmap;
5729 if (mddev->bitmap_info.max_write_behind > 0) {
5730 bool creat_pool = false;
5732 rdev_for_each(rdev, mddev) {
5733 if (test_bit(WriteMostly, &rdev->flags) &&
5737 if (creat_pool && mddev->wb_info_pool == NULL) {
5738 mddev->wb_info_pool =
5739 mempool_create_kmalloc_pool(NR_WB_INFOS,
5740 sizeof(struct wb_info));
5741 if (!mddev->wb_info_pool) {
5751 rdev_for_each(rdev, mddev) {
5752 if (rdev->raid_disk >= 0 &&
5753 !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
5758 if (mddev->degraded)
5761 blk_queue_flag_set(QUEUE_FLAG_NONROT, mddev->queue);
5763 blk_queue_flag_clear(QUEUE_FLAG_NONROT, mddev->queue);
5764 mddev->queue->backing_dev_info->congested_data = mddev;
5765 mddev->queue->backing_dev_info->congested_fn = md_congested;
5767 if (pers->sync_request) {
5768 if (mddev->kobj.sd &&
5769 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
5770 pr_warn("md: cannot register extra attributes for %s\n",
5772 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
5773 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
5776 atomic_set(&mddev->max_corr_read_errors,
5777 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
5778 mddev->safemode = 0;
5779 if (mddev_is_clustered(mddev))
5780 mddev->safemode_delay = 0;
5782 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
5785 spin_lock(&mddev->lock);
5787 spin_unlock(&mddev->lock);
5788 rdev_for_each(rdev, mddev)
5789 if (rdev->raid_disk >= 0)
5790 sysfs_link_rdev(mddev, rdev); /* failure here is OK */
5792 if (mddev->degraded && !mddev->ro)
5793 /* This ensures that recovering status is reported immediately
5794 * via sysfs - until a lack of spares is confirmed.
5796 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
5797 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5799 if (mddev->sb_flags)
5800 md_update_sb(mddev, 0);
5802 md_new_event(mddev);
5806 mddev_detach(mddev);
5808 pers->free(mddev, mddev->private);
5809 mddev->private = NULL;
5810 module_put(pers->owner);
5811 md_bitmap_destroy(mddev);
5813 bioset_exit(&mddev->bio_set);
5814 bioset_exit(&mddev->sync_set);
5817 EXPORT_SYMBOL_GPL(md_run);
5819 static int do_md_run(struct mddev *mddev)
5823 set_bit(MD_NOT_READY, &mddev->flags);
5824 err = md_run(mddev);
5827 err = md_bitmap_load(mddev);
5829 md_bitmap_destroy(mddev);
5833 if (mddev_is_clustered(mddev))
5834 md_allow_write(mddev);
5836 /* run start up tasks that require md_thread */
5839 md_wakeup_thread(mddev->thread);
5840 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5842 set_capacity(mddev->gendisk, mddev->array_sectors);
5843 revalidate_disk(mddev->gendisk);
5844 clear_bit(MD_NOT_READY, &mddev->flags);
5846 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5847 sysfs_notify_dirent_safe(mddev->sysfs_state);
5848 sysfs_notify_dirent_safe(mddev->sysfs_action);
5849 sysfs_notify(&mddev->kobj, NULL, "degraded");
5851 clear_bit(MD_NOT_READY, &mddev->flags);
5855 int md_start(struct mddev *mddev)
5859 if (mddev->pers->start) {
5860 set_bit(MD_RECOVERY_WAIT, &mddev->recovery);
5861 md_wakeup_thread(mddev->thread);
5862 ret = mddev->pers->start(mddev);
5863 clear_bit(MD_RECOVERY_WAIT, &mddev->recovery);
5864 md_wakeup_thread(mddev->sync_thread);
5868 EXPORT_SYMBOL_GPL(md_start);
5870 static int restart_array(struct mddev *mddev)
5872 struct gendisk *disk = mddev->gendisk;
5873 struct md_rdev *rdev;
5874 bool has_journal = false;
5875 bool has_readonly = false;
5877 /* Complain if it has no devices */
5878 if (list_empty(&mddev->disks))
5886 rdev_for_each_rcu(rdev, mddev) {
5887 if (test_bit(Journal, &rdev->flags) &&
5888 !test_bit(Faulty, &rdev->flags))
5890 if (bdev_read_only(rdev->bdev))
5891 has_readonly = true;
5894 if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && !has_journal)
5895 /* Don't restart rw with journal missing/faulty */
5900 mddev->safemode = 0;
5902 set_disk_ro(disk, 0);
5903 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev));
5904 /* Kick recovery or resync if necessary */
5905 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5906 md_wakeup_thread(mddev->thread);
5907 md_wakeup_thread(mddev->sync_thread);
5908 sysfs_notify_dirent_safe(mddev->sysfs_state);
5912 static void md_clean(struct mddev *mddev)
5914 mddev->array_sectors = 0;
5915 mddev->external_size = 0;
5916 mddev->dev_sectors = 0;
5917 mddev->raid_disks = 0;
5918 mddev->recovery_cp = 0;
5919 mddev->resync_min = 0;
5920 mddev->resync_max = MaxSector;
5921 mddev->reshape_position = MaxSector;
5922 mddev->external = 0;
5923 mddev->persistent = 0;
5924 mddev->level = LEVEL_NONE;
5925 mddev->clevel[0] = 0;
5927 mddev->sb_flags = 0;
5929 mddev->metadata_type[0] = 0;
5930 mddev->chunk_sectors = 0;
5931 mddev->ctime = mddev->utime = 0;
5933 mddev->max_disks = 0;
5935 mddev->can_decrease_events = 0;
5936 mddev->delta_disks = 0;
5937 mddev->reshape_backwards = 0;
5938 mddev->new_level = LEVEL_NONE;
5939 mddev->new_layout = 0;
5940 mddev->new_chunk_sectors = 0;
5941 mddev->curr_resync = 0;
5942 atomic64_set(&mddev->resync_mismatches, 0);
5943 mddev->suspend_lo = mddev->suspend_hi = 0;
5944 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5945 mddev->recovery = 0;
5948 mddev->degraded = 0;
5949 mddev->safemode = 0;
5950 mddev->private = NULL;
5951 mddev->cluster_info = NULL;
5952 mddev->bitmap_info.offset = 0;
5953 mddev->bitmap_info.default_offset = 0;
5954 mddev->bitmap_info.default_space = 0;
5955 mddev->bitmap_info.chunksize = 0;
5956 mddev->bitmap_info.daemon_sleep = 0;
5957 mddev->bitmap_info.max_write_behind = 0;
5958 mddev->bitmap_info.nodes = 0;
5961 static void __md_stop_writes(struct mddev *mddev)
5963 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5964 flush_workqueue(md_misc_wq);
5965 if (mddev->sync_thread) {
5966 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5967 md_reap_sync_thread(mddev);
5970 del_timer_sync(&mddev->safemode_timer);
5972 if (mddev->pers && mddev->pers->quiesce) {
5973 mddev->pers->quiesce(mddev, 1);
5974 mddev->pers->quiesce(mddev, 0);
5976 md_bitmap_flush(mddev);
5978 if (mddev->ro == 0 &&
5979 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
5981 /* mark array as shutdown cleanly */
5982 if (!mddev_is_clustered(mddev))
5984 md_update_sb(mddev, 1);
5986 mempool_destroy(mddev->wb_info_pool);
5987 mddev->wb_info_pool = NULL;
5990 void md_stop_writes(struct mddev *mddev)
5992 mddev_lock_nointr(mddev);
5993 __md_stop_writes(mddev);
5994 mddev_unlock(mddev);
5996 EXPORT_SYMBOL_GPL(md_stop_writes);
5998 static void mddev_detach(struct mddev *mddev)
6000 md_bitmap_wait_behind_writes(mddev);
6001 if (mddev->pers && mddev->pers->quiesce) {
6002 mddev->pers->quiesce(mddev, 1);
6003 mddev->pers->quiesce(mddev, 0);
6005 md_unregister_thread(&mddev->thread);
6007 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
6010 static void __md_stop(struct mddev *mddev)
6012 struct md_personality *pers = mddev->pers;
6013 md_bitmap_destroy(mddev);
6014 mddev_detach(mddev);
6015 /* Ensure ->event_work is done */
6016 flush_workqueue(md_misc_wq);
6017 spin_lock(&mddev->lock);
6019 spin_unlock(&mddev->lock);
6020 pers->free(mddev, mddev->private);
6021 mddev->private = NULL;
6022 if (pers->sync_request && mddev->to_remove == NULL)
6023 mddev->to_remove = &md_redundancy_group;
6024 module_put(pers->owner);
6025 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6028 void md_stop(struct mddev *mddev)
6030 /* stop the array and free an attached data structures.
6031 * This is called from dm-raid
6034 bioset_exit(&mddev->bio_set);
6035 bioset_exit(&mddev->sync_set);
6038 EXPORT_SYMBOL_GPL(md_stop);
6040 static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
6045 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
6047 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6048 md_wakeup_thread(mddev->thread);
6050 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
6051 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6052 if (mddev->sync_thread)
6053 /* Thread might be blocked waiting for metadata update
6054 * which will now never happen */
6055 wake_up_process(mddev->sync_thread->tsk);
6057 if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
6059 mddev_unlock(mddev);
6060 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
6062 wait_event(mddev->sb_wait,
6063 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
6064 mddev_lock_nointr(mddev);
6066 mutex_lock(&mddev->open_mutex);
6067 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
6068 mddev->sync_thread ||
6069 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
6070 pr_warn("md: %s still in use.\n",mdname(mddev));
6072 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6073 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6074 md_wakeup_thread(mddev->thread);
6080 __md_stop_writes(mddev);
6086 set_disk_ro(mddev->gendisk, 1);
6087 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6088 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6089 md_wakeup_thread(mddev->thread);
6090 sysfs_notify_dirent_safe(mddev->sysfs_state);
6094 mutex_unlock(&mddev->open_mutex);
6099 * 0 - completely stop and dis-assemble array
6100 * 2 - stop but do not disassemble array
6102 static int do_md_stop(struct mddev *mddev, int mode,
6103 struct block_device *bdev)
6105 struct gendisk *disk = mddev->gendisk;
6106 struct md_rdev *rdev;
6109 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
6111 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6112 md_wakeup_thread(mddev->thread);
6114 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
6115 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6116 if (mddev->sync_thread)
6117 /* Thread might be blocked waiting for metadata update
6118 * which will now never happen */
6119 wake_up_process(mddev->sync_thread->tsk);
6121 mddev_unlock(mddev);
6122 wait_event(resync_wait, (mddev->sync_thread == NULL &&
6123 !test_bit(MD_RECOVERY_RUNNING,
6124 &mddev->recovery)));
6125 mddev_lock_nointr(mddev);
6127 mutex_lock(&mddev->open_mutex);
6128 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
6129 mddev->sysfs_active ||
6130 mddev->sync_thread ||
6131 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
6132 pr_warn("md: %s still in use.\n",mdname(mddev));
6133 mutex_unlock(&mddev->open_mutex);
6135 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6136 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6137 md_wakeup_thread(mddev->thread);
6143 set_disk_ro(disk, 0);
6145 __md_stop_writes(mddev);
6147 mddev->queue->backing_dev_info->congested_fn = NULL;
6149 /* tell userspace to handle 'inactive' */
6150 sysfs_notify_dirent_safe(mddev->sysfs_state);
6152 rdev_for_each(rdev, mddev)
6153 if (rdev->raid_disk >= 0)
6154 sysfs_unlink_rdev(mddev, rdev);
6156 set_capacity(disk, 0);
6157 mutex_unlock(&mddev->open_mutex);
6159 revalidate_disk(disk);
6164 mutex_unlock(&mddev->open_mutex);
6166 * Free resources if final stop
6169 pr_info("md: %s stopped.\n", mdname(mddev));
6171 if (mddev->bitmap_info.file) {
6172 struct file *f = mddev->bitmap_info.file;
6173 spin_lock(&mddev->lock);
6174 mddev->bitmap_info.file = NULL;
6175 spin_unlock(&mddev->lock);
6178 mddev->bitmap_info.offset = 0;
6180 export_array(mddev);
6183 if (mddev->hold_active == UNTIL_STOP)
6184 mddev->hold_active = 0;
6186 md_new_event(mddev);
6187 sysfs_notify_dirent_safe(mddev->sysfs_state);
6192 static void autorun_array(struct mddev *mddev)
6194 struct md_rdev *rdev;
6197 if (list_empty(&mddev->disks))
6200 pr_info("md: running: ");
6202 rdev_for_each(rdev, mddev) {
6203 char b[BDEVNAME_SIZE];
6204 pr_cont("<%s>", bdevname(rdev->bdev,b));
6208 err = do_md_run(mddev);
6210 pr_warn("md: do_md_run() returned %d\n", err);
6211 do_md_stop(mddev, 0, NULL);
6216 * lets try to run arrays based on all disks that have arrived
6217 * until now. (those are in pending_raid_disks)
6219 * the method: pick the first pending disk, collect all disks with
6220 * the same UUID, remove all from the pending list and put them into
6221 * the 'same_array' list. Then order this list based on superblock
6222 * update time (freshest comes first), kick out 'old' disks and
6223 * compare superblocks. If everything's fine then run it.
6225 * If "unit" is allocated, then bump its reference count
6227 static void autorun_devices(int part)
6229 struct md_rdev *rdev0, *rdev, *tmp;
6230 struct mddev *mddev;
6231 char b[BDEVNAME_SIZE];
6233 pr_info("md: autorun ...\n");
6234 while (!list_empty(&pending_raid_disks)) {
6237 LIST_HEAD(candidates);
6238 rdev0 = list_entry(pending_raid_disks.next,
6239 struct md_rdev, same_set);
6241 pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b));
6242 INIT_LIST_HEAD(&candidates);
6243 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
6244 if (super_90_load(rdev, rdev0, 0) >= 0) {
6245 pr_debug("md: adding %s ...\n",
6246 bdevname(rdev->bdev,b));
6247 list_move(&rdev->same_set, &candidates);
6250 * now we have a set of devices, with all of them having
6251 * mostly sane superblocks. It's time to allocate the
6255 dev = MKDEV(mdp_major,
6256 rdev0->preferred_minor << MdpMinorShift);
6257 unit = MINOR(dev) >> MdpMinorShift;
6259 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
6262 if (rdev0->preferred_minor != unit) {
6263 pr_warn("md: unit number in %s is bad: %d\n",
6264 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
6268 md_probe(dev, NULL, NULL);
6269 mddev = mddev_find(dev);
6270 if (!mddev || !mddev->gendisk) {
6275 if (mddev_lock(mddev))
6276 pr_warn("md: %s locked, cannot run\n", mdname(mddev));
6277 else if (mddev->raid_disks || mddev->major_version
6278 || !list_empty(&mddev->disks)) {
6279 pr_warn("md: %s already running, cannot run %s\n",
6280 mdname(mddev), bdevname(rdev0->bdev,b));
6281 mddev_unlock(mddev);
6283 pr_debug("md: created %s\n", mdname(mddev));
6284 mddev->persistent = 1;
6285 rdev_for_each_list(rdev, tmp, &candidates) {
6286 list_del_init(&rdev->same_set);
6287 if (bind_rdev_to_array(rdev, mddev))
6290 autorun_array(mddev);
6291 mddev_unlock(mddev);
6293 /* on success, candidates will be empty, on error
6296 rdev_for_each_list(rdev, tmp, &candidates) {
6297 list_del_init(&rdev->same_set);
6302 pr_info("md: ... autorun DONE.\n");
6304 #endif /* !MODULE */
6306 static int get_version(void __user *arg)
6310 ver.major = MD_MAJOR_VERSION;
6311 ver.minor = MD_MINOR_VERSION;
6312 ver.patchlevel = MD_PATCHLEVEL_VERSION;
6314 if (copy_to_user(arg, &ver, sizeof(ver)))
6320 static int get_array_info(struct mddev *mddev, void __user *arg)
6322 mdu_array_info_t info;
6323 int nr,working,insync,failed,spare;
6324 struct md_rdev *rdev;
6326 nr = working = insync = failed = spare = 0;
6328 rdev_for_each_rcu(rdev, mddev) {
6330 if (test_bit(Faulty, &rdev->flags))
6334 if (test_bit(In_sync, &rdev->flags))
6336 else if (test_bit(Journal, &rdev->flags))
6337 /* TODO: add journal count to md_u.h */
6345 info.major_version = mddev->major_version;
6346 info.minor_version = mddev->minor_version;
6347 info.patch_version = MD_PATCHLEVEL_VERSION;
6348 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
6349 info.level = mddev->level;
6350 info.size = mddev->dev_sectors / 2;
6351 if (info.size != mddev->dev_sectors / 2) /* overflow */
6354 info.raid_disks = mddev->raid_disks;
6355 info.md_minor = mddev->md_minor;
6356 info.not_persistent= !mddev->persistent;
6358 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
6361 info.state = (1<<MD_SB_CLEAN);
6362 if (mddev->bitmap && mddev->bitmap_info.offset)
6363 info.state |= (1<<MD_SB_BITMAP_PRESENT);
6364 if (mddev_is_clustered(mddev))
6365 info.state |= (1<<MD_SB_CLUSTERED);
6366 info.active_disks = insync;
6367 info.working_disks = working;
6368 info.failed_disks = failed;
6369 info.spare_disks = spare;
6371 info.layout = mddev->layout;
6372 info.chunk_size = mddev->chunk_sectors << 9;
6374 if (copy_to_user(arg, &info, sizeof(info)))
6380 static int get_bitmap_file(struct mddev *mddev, void __user * arg)
6382 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
6386 file = kzalloc(sizeof(*file), GFP_NOIO);
6391 spin_lock(&mddev->lock);
6392 /* bitmap enabled */
6393 if (mddev->bitmap_info.file) {
6394 ptr = file_path(mddev->bitmap_info.file, file->pathname,
6395 sizeof(file->pathname));
6399 memmove(file->pathname, ptr,
6400 sizeof(file->pathname)-(ptr-file->pathname));
6402 spin_unlock(&mddev->lock);
6405 copy_to_user(arg, file, sizeof(*file)))
6412 static int get_disk_info(struct mddev *mddev, void __user * arg)
6414 mdu_disk_info_t info;
6415 struct md_rdev *rdev;
6417 if (copy_from_user(&info, arg, sizeof(info)))
6421 rdev = md_find_rdev_nr_rcu(mddev, info.number);
6423 info.major = MAJOR(rdev->bdev->bd_dev);
6424 info.minor = MINOR(rdev->bdev->bd_dev);
6425 info.raid_disk = rdev->raid_disk;
6427 if (test_bit(Faulty, &rdev->flags))
6428 info.state |= (1<<MD_DISK_FAULTY);
6429 else if (test_bit(In_sync, &rdev->flags)) {
6430 info.state |= (1<<MD_DISK_ACTIVE);
6431 info.state |= (1<<MD_DISK_SYNC);
6433 if (test_bit(Journal, &rdev->flags))
6434 info.state |= (1<<MD_DISK_JOURNAL);
6435 if (test_bit(WriteMostly, &rdev->flags))
6436 info.state |= (1<<MD_DISK_WRITEMOSTLY);
6437 if (test_bit(FailFast, &rdev->flags))
6438 info.state |= (1<<MD_DISK_FAILFAST);
6440 info.major = info.minor = 0;
6441 info.raid_disk = -1;
6442 info.state = (1<<MD_DISK_REMOVED);
6446 if (copy_to_user(arg, &info, sizeof(info)))
6452 static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
6454 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
6455 struct md_rdev *rdev;
6456 dev_t dev = MKDEV(info->major,info->minor);
6458 if (mddev_is_clustered(mddev) &&
6459 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
6460 pr_warn("%s: Cannot add to clustered mddev.\n",
6465 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
6468 if (!mddev->raid_disks) {
6470 /* expecting a device which has a superblock */
6471 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
6473 pr_warn("md: md_import_device returned %ld\n",
6475 return PTR_ERR(rdev);
6477 if (!list_empty(&mddev->disks)) {
6478 struct md_rdev *rdev0
6479 = list_entry(mddev->disks.next,
6480 struct md_rdev, same_set);
6481 err = super_types[mddev->major_version]
6482 .load_super(rdev, rdev0, mddev->minor_version);
6484 pr_warn("md: %s has different UUID to %s\n",
6485 bdevname(rdev->bdev,b),
6486 bdevname(rdev0->bdev,b2));
6491 err = bind_rdev_to_array(rdev, mddev);
6498 * add_new_disk can be used once the array is assembled
6499 * to add "hot spares". They must already have a superblock
6504 if (!mddev->pers->hot_add_disk) {
6505 pr_warn("%s: personality does not support diskops!\n",
6509 if (mddev->persistent)
6510 rdev = md_import_device(dev, mddev->major_version,
6511 mddev->minor_version);
6513 rdev = md_import_device(dev, -1, -1);
6515 pr_warn("md: md_import_device returned %ld\n",
6517 return PTR_ERR(rdev);
6519 /* set saved_raid_disk if appropriate */
6520 if (!mddev->persistent) {
6521 if (info->state & (1<<MD_DISK_SYNC) &&
6522 info->raid_disk < mddev->raid_disks) {
6523 rdev->raid_disk = info->raid_disk;
6524 set_bit(In_sync, &rdev->flags);
6525 clear_bit(Bitmap_sync, &rdev->flags);
6527 rdev->raid_disk = -1;
6528 rdev->saved_raid_disk = rdev->raid_disk;
6530 super_types[mddev->major_version].
6531 validate_super(mddev, rdev);
6532 if ((info->state & (1<<MD_DISK_SYNC)) &&
6533 rdev->raid_disk != info->raid_disk) {
6534 /* This was a hot-add request, but events doesn't
6535 * match, so reject it.
6541 clear_bit(In_sync, &rdev->flags); /* just to be sure */
6542 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6543 set_bit(WriteMostly, &rdev->flags);
6545 clear_bit(WriteMostly, &rdev->flags);
6546 if (info->state & (1<<MD_DISK_FAILFAST))
6547 set_bit(FailFast, &rdev->flags);
6549 clear_bit(FailFast, &rdev->flags);
6551 if (info->state & (1<<MD_DISK_JOURNAL)) {
6552 struct md_rdev *rdev2;
6553 bool has_journal = false;
6555 /* make sure no existing journal disk */
6556 rdev_for_each(rdev2, mddev) {
6557 if (test_bit(Journal, &rdev2->flags)) {
6562 if (has_journal || mddev->bitmap) {
6566 set_bit(Journal, &rdev->flags);
6569 * check whether the device shows up in other nodes
6571 if (mddev_is_clustered(mddev)) {
6572 if (info->state & (1 << MD_DISK_CANDIDATE))
6573 set_bit(Candidate, &rdev->flags);
6574 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
6575 /* --add initiated by this node */
6576 err = md_cluster_ops->add_new_disk(mddev, rdev);
6584 rdev->raid_disk = -1;
6585 err = bind_rdev_to_array(rdev, mddev);
6590 if (mddev_is_clustered(mddev)) {
6591 if (info->state & (1 << MD_DISK_CANDIDATE)) {
6593 err = md_cluster_ops->new_disk_ack(mddev,
6596 md_kick_rdev_from_array(rdev);
6600 md_cluster_ops->add_new_disk_cancel(mddev);
6602 err = add_bound_rdev(rdev);
6606 err = add_bound_rdev(rdev);
6611 /* otherwise, add_new_disk is only allowed
6612 * for major_version==0 superblocks
6614 if (mddev->major_version != 0) {
6615 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev));
6619 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6621 rdev = md_import_device(dev, -1, 0);
6623 pr_warn("md: error, md_import_device() returned %ld\n",
6625 return PTR_ERR(rdev);
6627 rdev->desc_nr = info->number;
6628 if (info->raid_disk < mddev->raid_disks)
6629 rdev->raid_disk = info->raid_disk;
6631 rdev->raid_disk = -1;
6633 if (rdev->raid_disk < mddev->raid_disks)
6634 if (info->state & (1<<MD_DISK_SYNC))
6635 set_bit(In_sync, &rdev->flags);
6637 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6638 set_bit(WriteMostly, &rdev->flags);
6639 if (info->state & (1<<MD_DISK_FAILFAST))
6640 set_bit(FailFast, &rdev->flags);
6642 if (!mddev->persistent) {
6643 pr_debug("md: nonpersistent superblock ...\n");
6644 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6646 rdev->sb_start = calc_dev_sboffset(rdev);
6647 rdev->sectors = rdev->sb_start;
6649 err = bind_rdev_to_array(rdev, mddev);
6659 static int hot_remove_disk(struct mddev *mddev, dev_t dev)
6661 char b[BDEVNAME_SIZE];
6662 struct md_rdev *rdev;
6667 rdev = find_rdev(mddev, dev);
6671 if (rdev->raid_disk < 0)
6674 clear_bit(Blocked, &rdev->flags);
6675 remove_and_add_spares(mddev, rdev);
6677 if (rdev->raid_disk >= 0)
6681 if (mddev_is_clustered(mddev))
6682 md_cluster_ops->remove_disk(mddev, rdev);
6684 md_kick_rdev_from_array(rdev);
6685 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
6687 md_wakeup_thread(mddev->thread);
6689 md_update_sb(mddev, 1);
6690 md_new_event(mddev);
6694 pr_debug("md: cannot remove active disk %s from %s ...\n",
6695 bdevname(rdev->bdev,b), mdname(mddev));
6699 static int hot_add_disk(struct mddev *mddev, dev_t dev)
6701 char b[BDEVNAME_SIZE];
6703 struct md_rdev *rdev;
6708 if (mddev->major_version != 0) {
6709 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
6713 if (!mddev->pers->hot_add_disk) {
6714 pr_warn("%s: personality does not support diskops!\n",
6719 rdev = md_import_device(dev, -1, 0);
6721 pr_warn("md: error, md_import_device() returned %ld\n",
6726 if (mddev->persistent)
6727 rdev->sb_start = calc_dev_sboffset(rdev);
6729 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6731 rdev->sectors = rdev->sb_start;
6733 if (test_bit(Faulty, &rdev->flags)) {
6734 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
6735 bdevname(rdev->bdev,b), mdname(mddev));
6740 clear_bit(In_sync, &rdev->flags);
6742 rdev->saved_raid_disk = -1;
6743 err = bind_rdev_to_array(rdev, mddev);
6748 * The rest should better be atomic, we can have disk failures
6749 * noticed in interrupt contexts ...
6752 rdev->raid_disk = -1;
6754 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
6756 md_update_sb(mddev, 1);
6758 * Kick recovery, maybe this spare has to be added to the
6759 * array immediately.
6761 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6762 md_wakeup_thread(mddev->thread);
6763 md_new_event(mddev);
6771 static int set_bitmap_file(struct mddev *mddev, int fd)
6776 if (!mddev->pers->quiesce || !mddev->thread)
6778 if (mddev->recovery || mddev->sync_thread)
6780 /* we should be able to change the bitmap.. */
6784 struct inode *inode;
6787 if (mddev->bitmap || mddev->bitmap_info.file)
6788 return -EEXIST; /* cannot add when bitmap is present */
6792 pr_warn("%s: error: failed to get bitmap file\n",
6797 inode = f->f_mapping->host;
6798 if (!S_ISREG(inode->i_mode)) {
6799 pr_warn("%s: error: bitmap file must be a regular file\n",
6802 } else if (!(f->f_mode & FMODE_WRITE)) {
6803 pr_warn("%s: error: bitmap file must open for write\n",
6806 } else if (atomic_read(&inode->i_writecount) != 1) {
6807 pr_warn("%s: error: bitmap file is already in use\n",
6815 mddev->bitmap_info.file = f;
6816 mddev->bitmap_info.offset = 0; /* file overrides offset */
6817 } else if (mddev->bitmap == NULL)
6818 return -ENOENT; /* cannot remove what isn't there */
6822 struct bitmap *bitmap;
6824 bitmap = md_bitmap_create(mddev, -1);
6825 mddev_suspend(mddev);
6826 if (!IS_ERR(bitmap)) {
6827 mddev->bitmap = bitmap;
6828 err = md_bitmap_load(mddev);
6830 err = PTR_ERR(bitmap);
6832 md_bitmap_destroy(mddev);
6835 mddev_resume(mddev);
6836 } else if (fd < 0) {
6837 mddev_suspend(mddev);
6838 md_bitmap_destroy(mddev);
6839 mddev_resume(mddev);
6843 struct file *f = mddev->bitmap_info.file;
6845 spin_lock(&mddev->lock);
6846 mddev->bitmap_info.file = NULL;
6847 spin_unlock(&mddev->lock);
6856 * set_array_info is used two different ways
6857 * The original usage is when creating a new array.
6858 * In this usage, raid_disks is > 0 and it together with
6859 * level, size, not_persistent,layout,chunksize determine the
6860 * shape of the array.
6861 * This will always create an array with a type-0.90.0 superblock.
6862 * The newer usage is when assembling an array.
6863 * In this case raid_disks will be 0, and the major_version field is
6864 * use to determine which style super-blocks are to be found on the devices.
6865 * The minor and patch _version numbers are also kept incase the
6866 * super_block handler wishes to interpret them.
6868 static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
6871 if (info->raid_disks == 0) {
6872 /* just setting version number for superblock loading */
6873 if (info->major_version < 0 ||
6874 info->major_version >= ARRAY_SIZE(super_types) ||
6875 super_types[info->major_version].name == NULL) {
6876 /* maybe try to auto-load a module? */
6877 pr_warn("md: superblock version %d not known\n",
6878 info->major_version);
6881 mddev->major_version = info->major_version;
6882 mddev->minor_version = info->minor_version;
6883 mddev->patch_version = info->patch_version;
6884 mddev->persistent = !info->not_persistent;
6885 /* ensure mddev_put doesn't delete this now that there
6886 * is some minimal configuration.
6888 mddev->ctime = ktime_get_real_seconds();
6891 mddev->major_version = MD_MAJOR_VERSION;
6892 mddev->minor_version = MD_MINOR_VERSION;
6893 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6894 mddev->ctime = ktime_get_real_seconds();
6896 mddev->level = info->level;
6897 mddev->clevel[0] = 0;
6898 mddev->dev_sectors = 2 * (sector_t)info->size;
6899 mddev->raid_disks = info->raid_disks;
6900 /* don't set md_minor, it is determined by which /dev/md* was
6903 if (info->state & (1<<MD_SB_CLEAN))
6904 mddev->recovery_cp = MaxSector;
6906 mddev->recovery_cp = 0;
6907 mddev->persistent = ! info->not_persistent;
6908 mddev->external = 0;
6910 mddev->layout = info->layout;
6911 if (mddev->level == 0)
6912 /* Cannot trust RAID0 layout info here */
6914 mddev->chunk_sectors = info->chunk_size >> 9;
6916 if (mddev->persistent) {
6917 mddev->max_disks = MD_SB_DISKS;
6919 mddev->sb_flags = 0;
6921 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
6923 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
6924 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
6925 mddev->bitmap_info.offset = 0;
6927 mddev->reshape_position = MaxSector;
6930 * Generate a 128 bit UUID
6932 get_random_bytes(mddev->uuid, 16);
6934 mddev->new_level = mddev->level;
6935 mddev->new_chunk_sectors = mddev->chunk_sectors;
6936 mddev->new_layout = mddev->layout;
6937 mddev->delta_disks = 0;
6938 mddev->reshape_backwards = 0;
6943 void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
6945 lockdep_assert_held(&mddev->reconfig_mutex);
6947 if (mddev->external_size)
6950 mddev->array_sectors = array_sectors;
6952 EXPORT_SYMBOL(md_set_array_sectors);
6954 static int update_size(struct mddev *mddev, sector_t num_sectors)
6956 struct md_rdev *rdev;
6958 int fit = (num_sectors == 0);
6959 sector_t old_dev_sectors = mddev->dev_sectors;
6961 if (mddev->pers->resize == NULL)
6963 /* The "num_sectors" is the number of sectors of each device that
6964 * is used. This can only make sense for arrays with redundancy.
6965 * linear and raid0 always use whatever space is available. We can only
6966 * consider changing this number if no resync or reconstruction is
6967 * happening, and if the new size is acceptable. It must fit before the
6968 * sb_start or, if that is <data_offset, it must fit before the size
6969 * of each device. If num_sectors is zero, we find the largest size
6972 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6978 rdev_for_each(rdev, mddev) {
6979 sector_t avail = rdev->sectors;
6981 if (fit && (num_sectors == 0 || num_sectors > avail))
6982 num_sectors = avail;
6983 if (avail < num_sectors)
6986 rv = mddev->pers->resize(mddev, num_sectors);
6988 if (mddev_is_clustered(mddev))
6989 md_cluster_ops->update_size(mddev, old_dev_sectors);
6990 else if (mddev->queue) {
6991 set_capacity(mddev->gendisk, mddev->array_sectors);
6992 revalidate_disk(mddev->gendisk);
6998 static int update_raid_disks(struct mddev *mddev, int raid_disks)
7001 struct md_rdev *rdev;
7002 /* change the number of raid disks */
7003 if (mddev->pers->check_reshape == NULL)
7007 if (raid_disks <= 0 ||
7008 (mddev->max_disks && raid_disks >= mddev->max_disks))
7010 if (mddev->sync_thread ||
7011 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
7012 mddev->reshape_position != MaxSector)
7015 rdev_for_each(rdev, mddev) {
7016 if (mddev->raid_disks < raid_disks &&
7017 rdev->data_offset < rdev->new_data_offset)
7019 if (mddev->raid_disks > raid_disks &&
7020 rdev->data_offset > rdev->new_data_offset)
7024 mddev->delta_disks = raid_disks - mddev->raid_disks;
7025 if (mddev->delta_disks < 0)
7026 mddev->reshape_backwards = 1;
7027 else if (mddev->delta_disks > 0)
7028 mddev->reshape_backwards = 0;
7030 rv = mddev->pers->check_reshape(mddev);
7032 mddev->delta_disks = 0;
7033 mddev->reshape_backwards = 0;
7039 * update_array_info is used to change the configuration of an
7041 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
7042 * fields in the info are checked against the array.
7043 * Any differences that cannot be handled will cause an error.
7044 * Normally, only one change can be managed at a time.
7046 static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
7052 /* calculate expected state,ignoring low bits */
7053 if (mddev->bitmap && mddev->bitmap_info.offset)
7054 state |= (1 << MD_SB_BITMAP_PRESENT);
7056 if (mddev->major_version != info->major_version ||
7057 mddev->minor_version != info->minor_version ||
7058 /* mddev->patch_version != info->patch_version || */
7059 mddev->ctime != info->ctime ||
7060 mddev->level != info->level ||
7061 /* mddev->layout != info->layout || */
7062 mddev->persistent != !info->not_persistent ||
7063 mddev->chunk_sectors != info->chunk_size >> 9 ||
7064 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
7065 ((state^info->state) & 0xfffffe00)
7068 /* Check there is only one change */
7069 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
7071 if (mddev->raid_disks != info->raid_disks)
7073 if (mddev->layout != info->layout)
7075 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
7082 if (mddev->layout != info->layout) {
7084 * we don't need to do anything at the md level, the
7085 * personality will take care of it all.
7087 if (mddev->pers->check_reshape == NULL)
7090 mddev->new_layout = info->layout;
7091 rv = mddev->pers->check_reshape(mddev);
7093 mddev->new_layout = mddev->layout;
7097 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
7098 rv = update_size(mddev, (sector_t)info->size * 2);
7100 if (mddev->raid_disks != info->raid_disks)
7101 rv = update_raid_disks(mddev, info->raid_disks);
7103 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
7104 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
7108 if (mddev->recovery || mddev->sync_thread) {
7112 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
7113 struct bitmap *bitmap;
7114 /* add the bitmap */
7115 if (mddev->bitmap) {
7119 if (mddev->bitmap_info.default_offset == 0) {
7123 mddev->bitmap_info.offset =
7124 mddev->bitmap_info.default_offset;
7125 mddev->bitmap_info.space =
7126 mddev->bitmap_info.default_space;
7127 bitmap = md_bitmap_create(mddev, -1);
7128 mddev_suspend(mddev);
7129 if (!IS_ERR(bitmap)) {
7130 mddev->bitmap = bitmap;
7131 rv = md_bitmap_load(mddev);
7133 rv = PTR_ERR(bitmap);
7135 md_bitmap_destroy(mddev);
7136 mddev_resume(mddev);
7138 /* remove the bitmap */
7139 if (!mddev->bitmap) {
7143 if (mddev->bitmap->storage.file) {
7147 if (mddev->bitmap_info.nodes) {
7148 /* hold PW on all the bitmap lock */
7149 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
7150 pr_warn("md: can't change bitmap to none since the array is in use by more than one node\n");
7152 md_cluster_ops->unlock_all_bitmaps(mddev);
7156 mddev->bitmap_info.nodes = 0;
7157 md_cluster_ops->leave(mddev);
7159 mddev_suspend(mddev);
7160 md_bitmap_destroy(mddev);
7161 mddev_resume(mddev);
7162 mddev->bitmap_info.offset = 0;
7165 md_update_sb(mddev, 1);
7171 static int set_disk_faulty(struct mddev *mddev, dev_t dev)
7173 struct md_rdev *rdev;
7176 if (mddev->pers == NULL)
7180 rdev = md_find_rdev_rcu(mddev, dev);
7184 md_error(mddev, rdev);
7185 if (!test_bit(Faulty, &rdev->flags))
7193 * We have a problem here : there is no easy way to give a CHS
7194 * virtual geometry. We currently pretend that we have a 2 heads
7195 * 4 sectors (with a BIG number of cylinders...). This drives
7196 * dosfs just mad... ;-)
7198 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
7200 struct mddev *mddev = bdev->bd_disk->private_data;
7204 geo->cylinders = mddev->array_sectors / 8;
7208 static inline bool md_ioctl_valid(unsigned int cmd)
7213 case GET_ARRAY_INFO:
7214 case GET_BITMAP_FILE:
7217 case HOT_REMOVE_DISK:
7220 case RESTART_ARRAY_RW:
7222 case SET_ARRAY_INFO:
7223 case SET_BITMAP_FILE:
7224 case SET_DISK_FAULTY:
7227 case CLUSTERED_DISK_NACK:
7234 static int md_ioctl(struct block_device *bdev, fmode_t mode,
7235 unsigned int cmd, unsigned long arg)
7238 void __user *argp = (void __user *)arg;
7239 struct mddev *mddev = NULL;
7241 bool did_set_md_closing = false;
7243 if (!md_ioctl_valid(cmd))
7248 case GET_ARRAY_INFO:
7252 if (!capable(CAP_SYS_ADMIN))
7257 * Commands dealing with the RAID driver but not any
7262 err = get_version(argp);
7268 autostart_arrays(arg);
7275 * Commands creating/starting a new array:
7278 mddev = bdev->bd_disk->private_data;
7285 /* Some actions do not requires the mutex */
7287 case GET_ARRAY_INFO:
7288 if (!mddev->raid_disks && !mddev->external)
7291 err = get_array_info(mddev, argp);
7295 if (!mddev->raid_disks && !mddev->external)
7298 err = get_disk_info(mddev, argp);
7301 case SET_DISK_FAULTY:
7302 err = set_disk_faulty(mddev, new_decode_dev(arg));
7305 case GET_BITMAP_FILE:
7306 err = get_bitmap_file(mddev, argp);
7311 if (cmd == ADD_NEW_DISK)
7312 /* need to ensure md_delayed_delete() has completed */
7313 flush_workqueue(md_misc_wq);
7315 if (cmd == HOT_REMOVE_DISK)
7316 /* need to ensure recovery thread has run */
7317 wait_event_interruptible_timeout(mddev->sb_wait,
7318 !test_bit(MD_RECOVERY_NEEDED,
7320 msecs_to_jiffies(5000));
7321 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
7322 /* Need to flush page cache, and ensure no-one else opens
7325 mutex_lock(&mddev->open_mutex);
7326 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
7327 mutex_unlock(&mddev->open_mutex);
7331 WARN_ON_ONCE(test_bit(MD_CLOSING, &mddev->flags));
7332 set_bit(MD_CLOSING, &mddev->flags);
7333 did_set_md_closing = true;
7334 mutex_unlock(&mddev->open_mutex);
7335 sync_blockdev(bdev);
7337 err = mddev_lock(mddev);
7339 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7344 if (cmd == SET_ARRAY_INFO) {
7345 mdu_array_info_t info;
7347 memset(&info, 0, sizeof(info));
7348 else if (copy_from_user(&info, argp, sizeof(info))) {
7353 err = update_array_info(mddev, &info);
7355 pr_warn("md: couldn't update array info. %d\n", err);
7360 if (!list_empty(&mddev->disks)) {
7361 pr_warn("md: array %s already has disks!\n", mdname(mddev));
7365 if (mddev->raid_disks) {
7366 pr_warn("md: array %s already initialised!\n", mdname(mddev));
7370 err = set_array_info(mddev, &info);
7372 pr_warn("md: couldn't set array info. %d\n", err);
7379 * Commands querying/configuring an existing array:
7381 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
7382 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
7383 if ((!mddev->raid_disks && !mddev->external)
7384 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
7385 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
7386 && cmd != GET_BITMAP_FILE) {
7392 * Commands even a read-only array can execute:
7395 case RESTART_ARRAY_RW:
7396 err = restart_array(mddev);
7400 err = do_md_stop(mddev, 0, bdev);
7404 err = md_set_readonly(mddev, bdev);
7407 case HOT_REMOVE_DISK:
7408 err = hot_remove_disk(mddev, new_decode_dev(arg));
7412 /* We can support ADD_NEW_DISK on read-only arrays
7413 * only if we are re-adding a preexisting device.
7414 * So require mddev->pers and MD_DISK_SYNC.
7417 mdu_disk_info_t info;
7418 if (copy_from_user(&info, argp, sizeof(info)))
7420 else if (!(info.state & (1<<MD_DISK_SYNC)))
7421 /* Need to clear read-only for this */
7424 err = add_new_disk(mddev, &info);
7430 if (get_user(ro, (int __user *)(arg))) {
7436 /* if the bdev is going readonly the value of mddev->ro
7437 * does not matter, no writes are coming
7442 /* are we are already prepared for writes? */
7446 /* transitioning to readauto need only happen for
7447 * arrays that call md_write_start
7450 err = restart_array(mddev);
7453 set_disk_ro(mddev->gendisk, 0);
7460 * The remaining ioctls are changing the state of the
7461 * superblock, so we do not allow them on read-only arrays.
7463 if (mddev->ro && mddev->pers) {
7464 if (mddev->ro == 2) {
7466 sysfs_notify_dirent_safe(mddev->sysfs_state);
7467 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7468 /* mddev_unlock will wake thread */
7469 /* If a device failed while we were read-only, we
7470 * need to make sure the metadata is updated now.
7472 if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) {
7473 mddev_unlock(mddev);
7474 wait_event(mddev->sb_wait,
7475 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) &&
7476 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
7477 mddev_lock_nointr(mddev);
7488 mdu_disk_info_t info;
7489 if (copy_from_user(&info, argp, sizeof(info)))
7492 err = add_new_disk(mddev, &info);
7496 case CLUSTERED_DISK_NACK:
7497 if (mddev_is_clustered(mddev))
7498 md_cluster_ops->new_disk_ack(mddev, false);
7504 err = hot_add_disk(mddev, new_decode_dev(arg));
7508 err = do_md_run(mddev);
7511 case SET_BITMAP_FILE:
7512 err = set_bitmap_file(mddev, (int)arg);
7521 if (mddev->hold_active == UNTIL_IOCTL &&
7523 mddev->hold_active = 0;
7524 mddev_unlock(mddev);
7526 if(did_set_md_closing)
7527 clear_bit(MD_CLOSING, &mddev->flags);
7530 #ifdef CONFIG_COMPAT
7531 static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7532 unsigned int cmd, unsigned long arg)
7535 case HOT_REMOVE_DISK:
7537 case SET_DISK_FAULTY:
7538 case SET_BITMAP_FILE:
7539 /* These take in integer arg, do not convert */
7542 arg = (unsigned long)compat_ptr(arg);
7546 return md_ioctl(bdev, mode, cmd, arg);
7548 #endif /* CONFIG_COMPAT */
7550 static int md_open(struct block_device *bdev, fmode_t mode)
7553 * Succeed if we can lock the mddev, which confirms that
7554 * it isn't being stopped right now.
7556 struct mddev *mddev = mddev_find(bdev->bd_dev);
7562 if (mddev->gendisk != bdev->bd_disk) {
7563 /* we are racing with mddev_put which is discarding this
7567 /* Wait until bdev->bd_disk is definitely gone */
7568 flush_workqueue(md_misc_wq);
7569 /* Then retry the open from the top */
7570 return -ERESTARTSYS;
7572 BUG_ON(mddev != bdev->bd_disk->private_data);
7574 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
7577 if (test_bit(MD_CLOSING, &mddev->flags)) {
7578 mutex_unlock(&mddev->open_mutex);
7584 atomic_inc(&mddev->openers);
7585 mutex_unlock(&mddev->open_mutex);
7587 check_disk_change(bdev);
7594 static void md_release(struct gendisk *disk, fmode_t mode)
7596 struct mddev *mddev = disk->private_data;
7599 atomic_dec(&mddev->openers);
7603 static int md_media_changed(struct gendisk *disk)
7605 struct mddev *mddev = disk->private_data;
7607 return mddev->changed;
7610 static int md_revalidate(struct gendisk *disk)
7612 struct mddev *mddev = disk->private_data;
7617 static const struct block_device_operations md_fops =
7619 .owner = THIS_MODULE,
7621 .release = md_release,
7623 #ifdef CONFIG_COMPAT
7624 .compat_ioctl = md_compat_ioctl,
7626 .getgeo = md_getgeo,
7627 .media_changed = md_media_changed,
7628 .revalidate_disk= md_revalidate,
7631 static int md_thread(void *arg)
7633 struct md_thread *thread = arg;
7636 * md_thread is a 'system-thread', it's priority should be very
7637 * high. We avoid resource deadlocks individually in each
7638 * raid personality. (RAID5 does preallocation) We also use RR and
7639 * the very same RT priority as kswapd, thus we will never get
7640 * into a priority inversion deadlock.
7642 * we definitely have to have equal or higher priority than
7643 * bdflush, otherwise bdflush will deadlock if there are too
7644 * many dirty RAID5 blocks.
7647 allow_signal(SIGKILL);
7648 while (!kthread_should_stop()) {
7650 /* We need to wait INTERRUPTIBLE so that
7651 * we don't add to the load-average.
7652 * That means we need to be sure no signals are
7655 if (signal_pending(current))
7656 flush_signals(current);
7658 wait_event_interruptible_timeout
7660 test_bit(THREAD_WAKEUP, &thread->flags)
7661 || kthread_should_stop() || kthread_should_park(),
7664 clear_bit(THREAD_WAKEUP, &thread->flags);
7665 if (kthread_should_park())
7667 if (!kthread_should_stop())
7668 thread->run(thread);
7674 void md_wakeup_thread(struct md_thread *thread)
7677 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
7678 set_bit(THREAD_WAKEUP, &thread->flags);
7679 wake_up(&thread->wqueue);
7682 EXPORT_SYMBOL(md_wakeup_thread);
7684 struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7685 struct mddev *mddev, const char *name)
7687 struct md_thread *thread;
7689 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
7693 init_waitqueue_head(&thread->wqueue);
7696 thread->mddev = mddev;
7697 thread->timeout = MAX_SCHEDULE_TIMEOUT;
7698 thread->tsk = kthread_run(md_thread, thread,
7700 mdname(thread->mddev),
7702 if (IS_ERR(thread->tsk)) {
7708 EXPORT_SYMBOL(md_register_thread);
7710 void md_unregister_thread(struct md_thread **threadp)
7712 struct md_thread *thread = *threadp;
7715 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
7716 /* Locking ensures that mddev_unlock does not wake_up a
7717 * non-existent thread
7719 spin_lock(&pers_lock);
7721 spin_unlock(&pers_lock);
7723 kthread_stop(thread->tsk);
7726 EXPORT_SYMBOL(md_unregister_thread);
7728 void md_error(struct mddev *mddev, struct md_rdev *rdev)
7730 if (!rdev || test_bit(Faulty, &rdev->flags))
7733 if (!mddev->pers || !mddev->pers->error_handler)
7735 mddev->pers->error_handler(mddev,rdev);
7736 if (mddev->degraded)
7737 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7738 sysfs_notify_dirent_safe(rdev->sysfs_state);
7739 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7740 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7741 md_wakeup_thread(mddev->thread);
7742 if (mddev->event_work.func)
7743 queue_work(md_misc_wq, &mddev->event_work);
7744 md_new_event(mddev);
7746 EXPORT_SYMBOL(md_error);
7748 /* seq_file implementation /proc/mdstat */
7750 static void status_unused(struct seq_file *seq)
7753 struct md_rdev *rdev;
7755 seq_printf(seq, "unused devices: ");
7757 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
7758 char b[BDEVNAME_SIZE];
7760 seq_printf(seq, "%s ",
7761 bdevname(rdev->bdev,b));
7764 seq_printf(seq, "<none>");
7766 seq_printf(seq, "\n");
7769 static int status_resync(struct seq_file *seq, struct mddev *mddev)
7771 sector_t max_sectors, resync, res;
7772 unsigned long dt, db = 0;
7773 sector_t rt, curr_mark_cnt, resync_mark_cnt;
7774 int scale, recovery_active;
7775 unsigned int per_milli;
7777 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7778 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7779 max_sectors = mddev->resync_max_sectors;
7781 max_sectors = mddev->dev_sectors;
7783 resync = mddev->curr_resync;
7785 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7786 /* Still cleaning up */
7787 resync = max_sectors;
7788 } else if (resync > max_sectors)
7789 resync = max_sectors;
7791 resync -= atomic_read(&mddev->recovery_active);
7794 if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery)) {
7795 struct md_rdev *rdev;
7797 rdev_for_each(rdev, mddev)
7798 if (rdev->raid_disk >= 0 &&
7799 !test_bit(Faulty, &rdev->flags) &&
7800 rdev->recovery_offset != MaxSector &&
7801 rdev->recovery_offset) {
7802 seq_printf(seq, "\trecover=REMOTE");
7805 if (mddev->reshape_position != MaxSector)
7806 seq_printf(seq, "\treshape=REMOTE");
7808 seq_printf(seq, "\tresync=REMOTE");
7811 if (mddev->recovery_cp < MaxSector) {
7812 seq_printf(seq, "\tresync=PENDING");
7818 seq_printf(seq, "\tresync=DELAYED");
7822 WARN_ON(max_sectors == 0);
7823 /* Pick 'scale' such that (resync>>scale)*1000 will fit
7824 * in a sector_t, and (max_sectors>>scale) will fit in a
7825 * u32, as those are the requirements for sector_div.
7826 * Thus 'scale' must be at least 10
7829 if (sizeof(sector_t) > sizeof(unsigned long)) {
7830 while ( max_sectors/2 > (1ULL<<(scale+32)))
7833 res = (resync>>scale)*1000;
7834 sector_div(res, (u32)((max_sectors>>scale)+1));
7838 int i, x = per_milli/50, y = 20-x;
7839 seq_printf(seq, "[");
7840 for (i = 0; i < x; i++)
7841 seq_printf(seq, "=");
7842 seq_printf(seq, ">");
7843 for (i = 0; i < y; i++)
7844 seq_printf(seq, ".");
7845 seq_printf(seq, "] ");
7847 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
7848 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7850 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7852 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7853 "resync" : "recovery"))),
7854 per_milli/10, per_milli % 10,
7855 (unsigned long long) resync/2,
7856 (unsigned long long) max_sectors/2);
7859 * dt: time from mark until now
7860 * db: blocks written from mark until now
7861 * rt: remaining time
7863 * rt is a sector_t, which is always 64bit now. We are keeping
7864 * the original algorithm, but it is not really necessary.
7866 * Original algorithm:
7867 * So we divide before multiply in case it is 32bit and close
7869 * We scale the divisor (db) by 32 to avoid losing precision
7870 * near the end of resync when the number of remaining sectors
7872 * We then divide rt by 32 after multiplying by db to compensate.
7873 * The '+1' avoids division by zero if db is very small.
7875 dt = ((jiffies - mddev->resync_mark) / HZ);
7878 curr_mark_cnt = mddev->curr_mark_cnt;
7879 recovery_active = atomic_read(&mddev->recovery_active);
7880 resync_mark_cnt = mddev->resync_mark_cnt;
7882 if (curr_mark_cnt >= (recovery_active + resync_mark_cnt))
7883 db = curr_mark_cnt - (recovery_active + resync_mark_cnt);
7885 rt = max_sectors - resync; /* number of remaining sectors */
7886 rt = div64_u64(rt, db/32+1);
7890 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7891 ((unsigned long)rt % 60)/6);
7893 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
7897 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7899 struct list_head *tmp;
7901 struct mddev *mddev;
7909 spin_lock(&all_mddevs_lock);
7910 list_for_each(tmp,&all_mddevs)
7912 mddev = list_entry(tmp, struct mddev, all_mddevs);
7914 spin_unlock(&all_mddevs_lock);
7917 spin_unlock(&all_mddevs_lock);
7919 return (void*)2;/* tail */
7923 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7925 struct list_head *tmp;
7926 struct mddev *next_mddev, *mddev = v;
7932 spin_lock(&all_mddevs_lock);
7934 tmp = all_mddevs.next;
7936 tmp = mddev->all_mddevs.next;
7937 if (tmp != &all_mddevs)
7938 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
7940 next_mddev = (void*)2;
7943 spin_unlock(&all_mddevs_lock);
7951 static void md_seq_stop(struct seq_file *seq, void *v)
7953 struct mddev *mddev = v;
7955 if (mddev && v != (void*)1 && v != (void*)2)
7959 static int md_seq_show(struct seq_file *seq, void *v)
7961 struct mddev *mddev = v;
7963 struct md_rdev *rdev;
7965 if (v == (void*)1) {
7966 struct md_personality *pers;
7967 seq_printf(seq, "Personalities : ");
7968 spin_lock(&pers_lock);
7969 list_for_each_entry(pers, &pers_list, list)
7970 seq_printf(seq, "[%s] ", pers->name);
7972 spin_unlock(&pers_lock);
7973 seq_printf(seq, "\n");
7974 seq->poll_event = atomic_read(&md_event_count);
7977 if (v == (void*)2) {
7982 spin_lock(&mddev->lock);
7983 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7984 seq_printf(seq, "%s : %sactive", mdname(mddev),
7985 mddev->pers ? "" : "in");
7988 seq_printf(seq, " (read-only)");
7990 seq_printf(seq, " (auto-read-only)");
7991 seq_printf(seq, " %s", mddev->pers->name);
7996 rdev_for_each_rcu(rdev, mddev) {
7997 char b[BDEVNAME_SIZE];
7998 seq_printf(seq, " %s[%d]",
7999 bdevname(rdev->bdev,b), rdev->desc_nr);
8000 if (test_bit(WriteMostly, &rdev->flags))
8001 seq_printf(seq, "(W)");
8002 if (test_bit(Journal, &rdev->flags))
8003 seq_printf(seq, "(J)");
8004 if (test_bit(Faulty, &rdev->flags)) {
8005 seq_printf(seq, "(F)");
8008 if (rdev->raid_disk < 0)
8009 seq_printf(seq, "(S)"); /* spare */
8010 if (test_bit(Replacement, &rdev->flags))
8011 seq_printf(seq, "(R)");
8012 sectors += rdev->sectors;
8016 if (!list_empty(&mddev->disks)) {
8018 seq_printf(seq, "\n %llu blocks",
8019 (unsigned long long)
8020 mddev->array_sectors / 2);
8022 seq_printf(seq, "\n %llu blocks",
8023 (unsigned long long)sectors / 2);
8025 if (mddev->persistent) {
8026 if (mddev->major_version != 0 ||
8027 mddev->minor_version != 90) {
8028 seq_printf(seq," super %d.%d",
8029 mddev->major_version,
8030 mddev->minor_version);
8032 } else if (mddev->external)
8033 seq_printf(seq, " super external:%s",
8034 mddev->metadata_type);
8036 seq_printf(seq, " super non-persistent");
8039 mddev->pers->status(seq, mddev);
8040 seq_printf(seq, "\n ");
8041 if (mddev->pers->sync_request) {
8042 if (status_resync(seq, mddev))
8043 seq_printf(seq, "\n ");
8046 seq_printf(seq, "\n ");
8048 md_bitmap_status(seq, mddev->bitmap);
8050 seq_printf(seq, "\n");
8052 spin_unlock(&mddev->lock);
8057 static const struct seq_operations md_seq_ops = {
8058 .start = md_seq_start,
8059 .next = md_seq_next,
8060 .stop = md_seq_stop,
8061 .show = md_seq_show,
8064 static int md_seq_open(struct inode *inode, struct file *file)
8066 struct seq_file *seq;
8069 error = seq_open(file, &md_seq_ops);
8073 seq = file->private_data;
8074 seq->poll_event = atomic_read(&md_event_count);
8078 static int md_unloading;
8079 static __poll_t mdstat_poll(struct file *filp, poll_table *wait)
8081 struct seq_file *seq = filp->private_data;
8085 return EPOLLIN|EPOLLRDNORM|EPOLLERR|EPOLLPRI;
8086 poll_wait(filp, &md_event_waiters, wait);
8088 /* always allow read */
8089 mask = EPOLLIN | EPOLLRDNORM;
8091 if (seq->poll_event != atomic_read(&md_event_count))
8092 mask |= EPOLLERR | EPOLLPRI;
8096 static const struct file_operations md_seq_fops = {
8097 .owner = THIS_MODULE,
8098 .open = md_seq_open,
8100 .llseek = seq_lseek,
8101 .release = seq_release,
8102 .poll = mdstat_poll,
8105 int register_md_personality(struct md_personality *p)
8107 pr_debug("md: %s personality registered for level %d\n",
8109 spin_lock(&pers_lock);
8110 list_add_tail(&p->list, &pers_list);
8111 spin_unlock(&pers_lock);
8114 EXPORT_SYMBOL(register_md_personality);
8116 int unregister_md_personality(struct md_personality *p)
8118 pr_debug("md: %s personality unregistered\n", p->name);
8119 spin_lock(&pers_lock);
8120 list_del_init(&p->list);
8121 spin_unlock(&pers_lock);
8124 EXPORT_SYMBOL(unregister_md_personality);
8126 int register_md_cluster_operations(struct md_cluster_operations *ops,
8127 struct module *module)
8130 spin_lock(&pers_lock);
8131 if (md_cluster_ops != NULL)
8134 md_cluster_ops = ops;
8135 md_cluster_mod = module;
8137 spin_unlock(&pers_lock);
8140 EXPORT_SYMBOL(register_md_cluster_operations);
8142 int unregister_md_cluster_operations(void)
8144 spin_lock(&pers_lock);
8145 md_cluster_ops = NULL;
8146 spin_unlock(&pers_lock);
8149 EXPORT_SYMBOL(unregister_md_cluster_operations);
8151 int md_setup_cluster(struct mddev *mddev, int nodes)
8153 if (!md_cluster_ops)
8154 request_module("md-cluster");
8155 spin_lock(&pers_lock);
8156 /* ensure module won't be unloaded */
8157 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
8158 pr_warn("can't find md-cluster module or get it's reference.\n");
8159 spin_unlock(&pers_lock);
8162 spin_unlock(&pers_lock);
8164 return md_cluster_ops->join(mddev, nodes);
8167 void md_cluster_stop(struct mddev *mddev)
8169 if (!md_cluster_ops)
8171 md_cluster_ops->leave(mddev);
8172 module_put(md_cluster_mod);
8175 static int is_mddev_idle(struct mddev *mddev, int init)
8177 struct md_rdev *rdev;
8183 rdev_for_each_rcu(rdev, mddev) {
8184 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
8185 curr_events = (int)part_stat_read_accum(&disk->part0, sectors) -
8186 atomic_read(&disk->sync_io);
8187 /* sync IO will cause sync_io to increase before the disk_stats
8188 * as sync_io is counted when a request starts, and
8189 * disk_stats is counted when it completes.
8190 * So resync activity will cause curr_events to be smaller than
8191 * when there was no such activity.
8192 * non-sync IO will cause disk_stat to increase without
8193 * increasing sync_io so curr_events will (eventually)
8194 * be larger than it was before. Once it becomes
8195 * substantially larger, the test below will cause
8196 * the array to appear non-idle, and resync will slow
8198 * If there is a lot of outstanding resync activity when
8199 * we set last_event to curr_events, then all that activity
8200 * completing might cause the array to appear non-idle
8201 * and resync will be slowed down even though there might
8202 * not have been non-resync activity. This will only
8203 * happen once though. 'last_events' will soon reflect
8204 * the state where there is little or no outstanding
8205 * resync requests, and further resync activity will
8206 * always make curr_events less than last_events.
8209 if (init || curr_events - rdev->last_events > 64) {
8210 rdev->last_events = curr_events;
8218 void md_done_sync(struct mddev *mddev, int blocks, int ok)
8220 /* another "blocks" (512byte) blocks have been synced */
8221 atomic_sub(blocks, &mddev->recovery_active);
8222 wake_up(&mddev->recovery_wait);
8224 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
8225 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
8226 md_wakeup_thread(mddev->thread);
8227 // stop recovery, signal do_sync ....
8230 EXPORT_SYMBOL(md_done_sync);
8232 /* md_write_start(mddev, bi)
8233 * If we need to update some array metadata (e.g. 'active' flag
8234 * in superblock) before writing, schedule a superblock update
8235 * and wait for it to complete.
8236 * A return value of 'false' means that the write wasn't recorded
8237 * and cannot proceed as the array is being suspend.
8239 bool md_write_start(struct mddev *mddev, struct bio *bi)
8243 if (bio_data_dir(bi) != WRITE)
8246 BUG_ON(mddev->ro == 1);
8247 if (mddev->ro == 2) {
8248 /* need to switch to read/write */
8250 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8251 md_wakeup_thread(mddev->thread);
8252 md_wakeup_thread(mddev->sync_thread);
8256 percpu_ref_get(&mddev->writes_pending);
8257 smp_mb(); /* Match smp_mb in set_in_sync() */
8258 if (mddev->safemode == 1)
8259 mddev->safemode = 0;
8260 /* sync_checkers is always 0 when writes_pending is in per-cpu mode */
8261 if (mddev->in_sync || mddev->sync_checkers) {
8262 spin_lock(&mddev->lock);
8263 if (mddev->in_sync) {
8265 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8266 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
8267 md_wakeup_thread(mddev->thread);
8270 spin_unlock(&mddev->lock);
8274 sysfs_notify_dirent_safe(mddev->sysfs_state);
8275 if (!mddev->has_superblocks)
8277 wait_event(mddev->sb_wait,
8278 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags) ||
8280 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
8281 percpu_ref_put(&mddev->writes_pending);
8286 EXPORT_SYMBOL(md_write_start);
8288 /* md_write_inc can only be called when md_write_start() has
8289 * already been called at least once of the current request.
8290 * It increments the counter and is useful when a single request
8291 * is split into several parts. Each part causes an increment and
8292 * so needs a matching md_write_end().
8293 * Unlike md_write_start(), it is safe to call md_write_inc() inside
8294 * a spinlocked region.
8296 void md_write_inc(struct mddev *mddev, struct bio *bi)
8298 if (bio_data_dir(bi) != WRITE)
8300 WARN_ON_ONCE(mddev->in_sync || mddev->ro);
8301 percpu_ref_get(&mddev->writes_pending);
8303 EXPORT_SYMBOL(md_write_inc);
8305 void md_write_end(struct mddev *mddev)
8307 percpu_ref_put(&mddev->writes_pending);
8309 if (mddev->safemode == 2)
8310 md_wakeup_thread(mddev->thread);
8311 else if (mddev->safemode_delay)
8312 /* The roundup() ensures this only performs locking once
8313 * every ->safemode_delay jiffies
8315 mod_timer(&mddev->safemode_timer,
8316 roundup(jiffies, mddev->safemode_delay) +
8317 mddev->safemode_delay);
8320 EXPORT_SYMBOL(md_write_end);
8322 /* md_allow_write(mddev)
8323 * Calling this ensures that the array is marked 'active' so that writes
8324 * may proceed without blocking. It is important to call this before
8325 * attempting a GFP_KERNEL allocation while holding the mddev lock.
8326 * Must be called with mddev_lock held.
8328 void md_allow_write(struct mddev *mddev)
8334 if (!mddev->pers->sync_request)
8337 spin_lock(&mddev->lock);
8338 if (mddev->in_sync) {
8340 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8341 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
8342 if (mddev->safemode_delay &&
8343 mddev->safemode == 0)
8344 mddev->safemode = 1;
8345 spin_unlock(&mddev->lock);
8346 md_update_sb(mddev, 0);
8347 sysfs_notify_dirent_safe(mddev->sysfs_state);
8348 /* wait for the dirty state to be recorded in the metadata */
8349 wait_event(mddev->sb_wait,
8350 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
8352 spin_unlock(&mddev->lock);
8354 EXPORT_SYMBOL_GPL(md_allow_write);
8356 #define SYNC_MARKS 10
8357 #define SYNC_MARK_STEP (3*HZ)
8358 #define UPDATE_FREQUENCY (5*60*HZ)
8359 void md_do_sync(struct md_thread *thread)
8361 struct mddev *mddev = thread->mddev;
8362 struct mddev *mddev2;
8363 unsigned int currspeed = 0, window;
8364 sector_t max_sectors,j, io_sectors, recovery_done;
8365 unsigned long mark[SYNC_MARKS];
8366 unsigned long update_time;
8367 sector_t mark_cnt[SYNC_MARKS];
8369 struct list_head *tmp;
8370 sector_t last_check;
8372 struct md_rdev *rdev;
8373 char *desc, *action = NULL;
8374 struct blk_plug plug;
8377 /* just incase thread restarts... */
8378 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
8379 test_bit(MD_RECOVERY_WAIT, &mddev->recovery))
8381 if (mddev->ro) {/* never try to sync a read-only array */
8382 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
8386 if (mddev_is_clustered(mddev)) {
8387 ret = md_cluster_ops->resync_start(mddev);
8391 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
8392 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8393 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
8394 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
8395 && ((unsigned long long)mddev->curr_resync_completed
8396 < (unsigned long long)mddev->resync_max_sectors))
8400 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8401 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
8402 desc = "data-check";
8404 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8405 desc = "requested-resync";
8409 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
8414 mddev->last_sync_action = action ?: desc;
8416 /* we overload curr_resync somewhat here.
8417 * 0 == not engaged in resync at all
8418 * 2 == checking that there is no conflict with another sync
8419 * 1 == like 2, but have yielded to allow conflicting resync to
8421 * other == active in resync - this many blocks
8423 * Before starting a resync we must have set curr_resync to
8424 * 2, and then checked that every "conflicting" array has curr_resync
8425 * less than ours. When we find one that is the same or higher
8426 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8427 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8428 * This will mean we have to start checking from the beginning again.
8433 int mddev2_minor = -1;
8434 mddev->curr_resync = 2;
8437 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8439 for_each_mddev(mddev2, tmp) {
8440 if (mddev2 == mddev)
8442 if (!mddev->parallel_resync
8443 && mddev2->curr_resync
8444 && match_mddev_units(mddev, mddev2)) {
8446 if (mddev < mddev2 && mddev->curr_resync == 2) {
8447 /* arbitrarily yield */
8448 mddev->curr_resync = 1;
8449 wake_up(&resync_wait);
8451 if (mddev > mddev2 && mddev->curr_resync == 1)
8452 /* no need to wait here, we can wait the next
8453 * time 'round when curr_resync == 2
8456 /* We need to wait 'interruptible' so as not to
8457 * contribute to the load average, and not to
8458 * be caught by 'softlockup'
8460 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
8461 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8462 mddev2->curr_resync >= mddev->curr_resync) {
8463 if (mddev2_minor != mddev2->md_minor) {
8464 mddev2_minor = mddev2->md_minor;
8465 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8466 desc, mdname(mddev),
8470 if (signal_pending(current))
8471 flush_signals(current);
8473 finish_wait(&resync_wait, &wq);
8476 finish_wait(&resync_wait, &wq);
8479 } while (mddev->curr_resync < 2);
8482 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8483 /* resync follows the size requested by the personality,
8484 * which defaults to physical size, but can be virtual size
8486 max_sectors = mddev->resync_max_sectors;
8487 atomic64_set(&mddev->resync_mismatches, 0);
8488 /* we don't use the checkpoint if there's a bitmap */
8489 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8490 j = mddev->resync_min;
8491 else if (!mddev->bitmap)
8492 j = mddev->recovery_cp;
8494 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
8495 max_sectors = mddev->resync_max_sectors;
8497 * If the original node aborts reshaping then we continue the
8498 * reshaping, so set j again to avoid restart reshape from the
8501 if (mddev_is_clustered(mddev) &&
8502 mddev->reshape_position != MaxSector)
8503 j = mddev->reshape_position;
8505 /* recovery follows the physical size of devices */
8506 max_sectors = mddev->dev_sectors;
8509 rdev_for_each_rcu(rdev, mddev)
8510 if (rdev->raid_disk >= 0 &&
8511 !test_bit(Journal, &rdev->flags) &&
8512 !test_bit(Faulty, &rdev->flags) &&
8513 !test_bit(In_sync, &rdev->flags) &&
8514 rdev->recovery_offset < j)
8515 j = rdev->recovery_offset;
8518 /* If there is a bitmap, we need to make sure all
8519 * writes that started before we added a spare
8520 * complete before we start doing a recovery.
8521 * Otherwise the write might complete and (via
8522 * bitmap_endwrite) set a bit in the bitmap after the
8523 * recovery has checked that bit and skipped that
8526 if (mddev->bitmap) {
8527 mddev->pers->quiesce(mddev, 1);
8528 mddev->pers->quiesce(mddev, 0);
8532 pr_info("md: %s of RAID array %s\n", desc, mdname(mddev));
8533 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev));
8534 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8535 speed_max(mddev), desc);
8537 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
8540 for (m = 0; m < SYNC_MARKS; m++) {
8542 mark_cnt[m] = io_sectors;
8545 mddev->resync_mark = mark[last_mark];
8546 mddev->resync_mark_cnt = mark_cnt[last_mark];
8549 * Tune reconstruction:
8551 window = 32 * (PAGE_SIZE / 512);
8552 pr_debug("md: using %dk window, over a total of %lluk.\n",
8553 window/2, (unsigned long long)max_sectors/2);
8555 atomic_set(&mddev->recovery_active, 0);
8559 pr_debug("md: resuming %s of %s from checkpoint.\n",
8560 desc, mdname(mddev));
8561 mddev->curr_resync = j;
8563 mddev->curr_resync = 3; /* no longer delayed */
8564 mddev->curr_resync_completed = j;
8565 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8566 md_new_event(mddev);
8567 update_time = jiffies;
8569 blk_start_plug(&plug);
8570 while (j < max_sectors) {
8575 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8576 ((mddev->curr_resync > mddev->curr_resync_completed &&
8577 (mddev->curr_resync - mddev->curr_resync_completed)
8578 > (max_sectors >> 4)) ||
8579 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
8580 (j - mddev->curr_resync_completed)*2
8581 >= mddev->resync_max - mddev->curr_resync_completed ||
8582 mddev->curr_resync_completed > mddev->resync_max
8584 /* time to update curr_resync_completed */
8585 wait_event(mddev->recovery_wait,
8586 atomic_read(&mddev->recovery_active) == 0);
8587 mddev->curr_resync_completed = j;
8588 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8589 j > mddev->recovery_cp)
8590 mddev->recovery_cp = j;
8591 update_time = jiffies;
8592 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8593 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8596 while (j >= mddev->resync_max &&
8597 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8598 /* As this condition is controlled by user-space,
8599 * we can block indefinitely, so use '_interruptible'
8600 * to avoid triggering warnings.
8602 flush_signals(current); /* just in case */
8603 wait_event_interruptible(mddev->recovery_wait,
8604 mddev->resync_max > j
8605 || test_bit(MD_RECOVERY_INTR,
8609 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8612 sectors = mddev->pers->sync_request(mddev, j, &skipped);
8614 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
8618 if (!skipped) { /* actual IO requested */
8619 io_sectors += sectors;
8620 atomic_add(sectors, &mddev->recovery_active);
8623 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8627 if (j > max_sectors)
8628 /* when skipping, extra large numbers can be returned. */
8631 mddev->curr_resync = j;
8632 mddev->curr_mark_cnt = io_sectors;
8633 if (last_check == 0)
8634 /* this is the earliest that rebuild will be
8635 * visible in /proc/mdstat
8637 md_new_event(mddev);
8639 if (last_check + window > io_sectors || j == max_sectors)
8642 last_check = io_sectors;
8644 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8646 int next = (last_mark+1) % SYNC_MARKS;
8648 mddev->resync_mark = mark[next];
8649 mddev->resync_mark_cnt = mark_cnt[next];
8650 mark[next] = jiffies;
8651 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
8655 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8659 * this loop exits only if either when we are slower than
8660 * the 'hard' speed limit, or the system was IO-idle for
8662 * the system might be non-idle CPU-wise, but we only care
8663 * about not overloading the IO subsystem. (things like an
8664 * e2fsck being done on the RAID array should execute fast)
8668 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8669 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
8670 /((jiffies-mddev->resync_mark)/HZ +1) +1;
8672 if (currspeed > speed_min(mddev)) {
8673 if (currspeed > speed_max(mddev)) {
8677 if (!is_mddev_idle(mddev, 0)) {
8679 * Give other IO more of a chance.
8680 * The faster the devices, the less we wait.
8682 wait_event(mddev->recovery_wait,
8683 !atomic_read(&mddev->recovery_active));
8687 pr_info("md: %s: %s %s.\n",mdname(mddev), desc,
8688 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8689 ? "interrupted" : "done");
8691 * this also signals 'finished resyncing' to md_stop
8693 blk_finish_plug(&plug);
8694 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8696 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8697 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8698 mddev->curr_resync > 3) {
8699 mddev->curr_resync_completed = mddev->curr_resync;
8700 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8702 mddev->pers->sync_request(mddev, max_sectors, &skipped);
8704 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
8705 mddev->curr_resync > 3) {
8706 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8707 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8708 if (mddev->curr_resync >= mddev->recovery_cp) {
8709 pr_debug("md: checkpointing %s of %s.\n",
8710 desc, mdname(mddev));
8711 if (test_bit(MD_RECOVERY_ERROR,
8713 mddev->recovery_cp =
8714 mddev->curr_resync_completed;
8716 mddev->recovery_cp =
8720 mddev->recovery_cp = MaxSector;
8722 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8723 mddev->curr_resync = MaxSector;
8724 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8725 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) {
8727 rdev_for_each_rcu(rdev, mddev)
8728 if (rdev->raid_disk >= 0 &&
8729 mddev->delta_disks >= 0 &&
8730 !test_bit(Journal, &rdev->flags) &&
8731 !test_bit(Faulty, &rdev->flags) &&
8732 !test_bit(In_sync, &rdev->flags) &&
8733 rdev->recovery_offset < mddev->curr_resync)
8734 rdev->recovery_offset = mddev->curr_resync;
8740 /* set CHANGE_PENDING here since maybe another update is needed,
8741 * so other nodes are informed. It should be harmless for normal
8743 set_mask_bits(&mddev->sb_flags, 0,
8744 BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS));
8746 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8747 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8748 mddev->delta_disks > 0 &&
8749 mddev->pers->finish_reshape &&
8750 mddev->pers->size &&
8752 mddev_lock_nointr(mddev);
8753 md_set_array_sectors(mddev, mddev->pers->size(mddev, 0, 0));
8754 mddev_unlock(mddev);
8755 if (!mddev_is_clustered(mddev)) {
8756 set_capacity(mddev->gendisk, mddev->array_sectors);
8757 revalidate_disk(mddev->gendisk);
8761 spin_lock(&mddev->lock);
8762 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8763 /* We completed so min/max setting can be forgotten if used. */
8764 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8765 mddev->resync_min = 0;
8766 mddev->resync_max = MaxSector;
8767 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8768 mddev->resync_min = mddev->curr_resync_completed;
8769 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
8770 mddev->curr_resync = 0;
8771 spin_unlock(&mddev->lock);
8773 wake_up(&resync_wait);
8774 md_wakeup_thread(mddev->thread);
8777 EXPORT_SYMBOL_GPL(md_do_sync);
8779 static int remove_and_add_spares(struct mddev *mddev,
8780 struct md_rdev *this)
8782 struct md_rdev *rdev;
8785 bool remove_some = false;
8787 if (this && test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
8788 /* Mustn't remove devices when resync thread is running */
8791 rdev_for_each(rdev, mddev) {
8792 if ((this == NULL || rdev == this) &&
8793 rdev->raid_disk >= 0 &&
8794 !test_bit(Blocked, &rdev->flags) &&
8795 test_bit(Faulty, &rdev->flags) &&
8796 atomic_read(&rdev->nr_pending)==0) {
8797 /* Faulty non-Blocked devices with nr_pending == 0
8798 * never get nr_pending incremented,
8799 * never get Faulty cleared, and never get Blocked set.
8800 * So we can synchronize_rcu now rather than once per device
8803 set_bit(RemoveSynchronized, &rdev->flags);
8809 rdev_for_each(rdev, mddev) {
8810 if ((this == NULL || rdev == this) &&
8811 rdev->raid_disk >= 0 &&
8812 !test_bit(Blocked, &rdev->flags) &&
8813 ((test_bit(RemoveSynchronized, &rdev->flags) ||
8814 (!test_bit(In_sync, &rdev->flags) &&
8815 !test_bit(Journal, &rdev->flags))) &&
8816 atomic_read(&rdev->nr_pending)==0)) {
8817 if (mddev->pers->hot_remove_disk(
8818 mddev, rdev) == 0) {
8819 sysfs_unlink_rdev(mddev, rdev);
8820 rdev->saved_raid_disk = rdev->raid_disk;
8821 rdev->raid_disk = -1;
8825 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
8826 clear_bit(RemoveSynchronized, &rdev->flags);
8829 if (removed && mddev->kobj.sd)
8830 sysfs_notify(&mddev->kobj, NULL, "degraded");
8832 if (this && removed)
8835 rdev_for_each(rdev, mddev) {
8836 if (this && this != rdev)
8838 if (test_bit(Candidate, &rdev->flags))
8840 if (rdev->raid_disk >= 0 &&
8841 !test_bit(In_sync, &rdev->flags) &&
8842 !test_bit(Journal, &rdev->flags) &&
8843 !test_bit(Faulty, &rdev->flags))
8845 if (rdev->raid_disk >= 0)
8847 if (test_bit(Faulty, &rdev->flags))
8849 if (!test_bit(Journal, &rdev->flags)) {
8851 ! (rdev->saved_raid_disk >= 0 &&
8852 !test_bit(Bitmap_sync, &rdev->flags)))
8855 rdev->recovery_offset = 0;
8858 hot_add_disk(mddev, rdev) == 0) {
8859 if (sysfs_link_rdev(mddev, rdev))
8860 /* failure here is OK */;
8861 if (!test_bit(Journal, &rdev->flags))
8863 md_new_event(mddev);
8864 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
8869 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
8873 static void md_start_sync(struct work_struct *ws)
8875 struct mddev *mddev = container_of(ws, struct mddev, del_work);
8877 mddev->sync_thread = md_register_thread(md_do_sync,
8880 if (!mddev->sync_thread) {
8881 pr_warn("%s: could not start resync thread...\n",
8883 /* leave the spares where they are, it shouldn't hurt */
8884 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8885 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8886 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8887 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8888 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
8889 wake_up(&resync_wait);
8890 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8892 if (mddev->sysfs_action)
8893 sysfs_notify_dirent_safe(mddev->sysfs_action);
8895 md_wakeup_thread(mddev->sync_thread);
8896 sysfs_notify_dirent_safe(mddev->sysfs_action);
8897 md_new_event(mddev);
8901 * This routine is regularly called by all per-raid-array threads to
8902 * deal with generic issues like resync and super-block update.
8903 * Raid personalities that don't have a thread (linear/raid0) do not
8904 * need this as they never do any recovery or update the superblock.
8906 * It does not do any resync itself, but rather "forks" off other threads
8907 * to do that as needed.
8908 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8909 * "->recovery" and create a thread at ->sync_thread.
8910 * When the thread finishes it sets MD_RECOVERY_DONE
8911 * and wakeups up this thread which will reap the thread and finish up.
8912 * This thread also removes any faulty devices (with nr_pending == 0).
8914 * The overall approach is:
8915 * 1/ if the superblock needs updating, update it.
8916 * 2/ If a recovery thread is running, don't do anything else.
8917 * 3/ If recovery has finished, clean up, possibly marking spares active.
8918 * 4/ If there are any faulty devices, remove them.
8919 * 5/ If array is degraded, try to add spares devices
8920 * 6/ If array has spares or is not in-sync, start a resync thread.
8922 void md_check_recovery(struct mddev *mddev)
8924 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags) && mddev->sb_flags) {
8925 /* Write superblock - thread that called mddev_suspend()
8926 * holds reconfig_mutex for us.
8928 set_bit(MD_UPDATING_SB, &mddev->flags);
8929 smp_mb__after_atomic();
8930 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags))
8931 md_update_sb(mddev, 0);
8932 clear_bit_unlock(MD_UPDATING_SB, &mddev->flags);
8933 wake_up(&mddev->sb_wait);
8936 if (mddev->suspended)
8940 md_bitmap_daemon_work(mddev);
8942 if (signal_pending(current)) {
8943 if (mddev->pers->sync_request && !mddev->external) {
8944 pr_debug("md: %s in immediate safe mode\n",
8946 mddev->safemode = 2;
8948 flush_signals(current);
8951 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8954 (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
8955 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8956 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
8957 (mddev->external == 0 && mddev->safemode == 1) ||
8958 (mddev->safemode == 2
8959 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
8963 if (mddev_trylock(mddev)) {
8965 bool try_set_sync = mddev->safemode != 0;
8967 if (!mddev->external && mddev->safemode == 1)
8968 mddev->safemode = 0;
8971 struct md_rdev *rdev;
8972 if (!mddev->external && mddev->in_sync)
8973 /* 'Blocked' flag not needed as failed devices
8974 * will be recorded if array switched to read/write.
8975 * Leaving it set will prevent the device
8976 * from being removed.
8978 rdev_for_each(rdev, mddev)
8979 clear_bit(Blocked, &rdev->flags);
8980 /* On a read-only array we can:
8981 * - remove failed devices
8982 * - add already-in_sync devices if the array itself
8984 * As we only add devices that are already in-sync,
8985 * we can activate the spares immediately.
8987 remove_and_add_spares(mddev, NULL);
8988 /* There is no thread, but we need to call
8989 * ->spare_active and clear saved_raid_disk
8991 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
8992 md_reap_sync_thread(mddev);
8993 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
8994 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8995 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
8999 if (mddev_is_clustered(mddev)) {
9000 struct md_rdev *rdev;
9001 /* kick the device if another node issued a
9004 rdev_for_each(rdev, mddev) {
9005 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
9006 rdev->raid_disk < 0)
9007 md_kick_rdev_from_array(rdev);
9011 if (try_set_sync && !mddev->external && !mddev->in_sync) {
9012 spin_lock(&mddev->lock);
9014 spin_unlock(&mddev->lock);
9017 if (mddev->sb_flags)
9018 md_update_sb(mddev, 0);
9020 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
9021 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
9022 /* resync/recovery still happening */
9023 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9026 if (mddev->sync_thread) {
9027 md_reap_sync_thread(mddev);
9030 /* Set RUNNING before clearing NEEDED to avoid
9031 * any transients in the value of "sync_action".
9033 mddev->curr_resync_completed = 0;
9034 spin_lock(&mddev->lock);
9035 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
9036 spin_unlock(&mddev->lock);
9037 /* Clear some bits that don't mean anything, but
9040 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
9041 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
9043 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
9044 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
9046 /* no recovery is running.
9047 * remove any failed drives, then
9048 * add spares if possible.
9049 * Spares are also removed and re-added, to allow
9050 * the personality to fail the re-add.
9053 if (mddev->reshape_position != MaxSector) {
9054 if (mddev->pers->check_reshape == NULL ||
9055 mddev->pers->check_reshape(mddev) != 0)
9056 /* Cannot proceed */
9058 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
9059 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
9060 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
9061 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9062 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
9063 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
9064 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
9065 } else if (mddev->recovery_cp < MaxSector) {
9066 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9067 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
9068 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
9069 /* nothing to be done ... */
9072 if (mddev->pers->sync_request) {
9074 /* We are adding a device or devices to an array
9075 * which has the bitmap stored on all devices.
9076 * So make sure all bitmap pages get written
9078 md_bitmap_write_all(mddev->bitmap);
9080 INIT_WORK(&mddev->del_work, md_start_sync);
9081 queue_work(md_misc_wq, &mddev->del_work);
9085 if (!mddev->sync_thread) {
9086 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
9087 wake_up(&resync_wait);
9088 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
9090 if (mddev->sysfs_action)
9091 sysfs_notify_dirent_safe(mddev->sysfs_action);
9094 wake_up(&mddev->sb_wait);
9095 mddev_unlock(mddev);
9098 EXPORT_SYMBOL(md_check_recovery);
9100 void md_reap_sync_thread(struct mddev *mddev)
9102 struct md_rdev *rdev;
9103 sector_t old_dev_sectors = mddev->dev_sectors;
9104 bool is_reshaped = false;
9106 /* resync has finished, collect result */
9107 md_unregister_thread(&mddev->sync_thread);
9108 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
9109 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
9110 mddev->degraded != mddev->raid_disks) {
9112 /* activate any spares */
9113 if (mddev->pers->spare_active(mddev)) {
9114 sysfs_notify(&mddev->kobj, NULL,
9116 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
9119 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
9120 mddev->pers->finish_reshape) {
9121 mddev->pers->finish_reshape(mddev);
9122 if (mddev_is_clustered(mddev))
9126 /* If array is no-longer degraded, then any saved_raid_disk
9127 * information must be scrapped.
9129 if (!mddev->degraded)
9130 rdev_for_each(rdev, mddev)
9131 rdev->saved_raid_disk = -1;
9133 md_update_sb(mddev, 1);
9134 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
9135 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
9137 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
9138 md_cluster_ops->resync_finish(mddev);
9139 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
9140 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
9141 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9142 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
9143 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
9144 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
9146 * We call md_cluster_ops->update_size here because sync_size could
9147 * be changed by md_update_sb, and MD_RECOVERY_RESHAPE is cleared,
9148 * so it is time to update size across cluster.
9150 if (mddev_is_clustered(mddev) && is_reshaped
9151 && !test_bit(MD_CLOSING, &mddev->flags))
9152 md_cluster_ops->update_size(mddev, old_dev_sectors);
9153 wake_up(&resync_wait);
9154 /* flag recovery needed just to double check */
9155 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9156 sysfs_notify_dirent_safe(mddev->sysfs_action);
9157 md_new_event(mddev);
9158 if (mddev->event_work.func)
9159 queue_work(md_misc_wq, &mddev->event_work);
9161 EXPORT_SYMBOL(md_reap_sync_thread);
9163 void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
9165 sysfs_notify_dirent_safe(rdev->sysfs_state);
9166 wait_event_timeout(rdev->blocked_wait,
9167 !test_bit(Blocked, &rdev->flags) &&
9168 !test_bit(BlockedBadBlocks, &rdev->flags),
9169 msecs_to_jiffies(5000));
9170 rdev_dec_pending(rdev, mddev);
9172 EXPORT_SYMBOL(md_wait_for_blocked_rdev);
9174 void md_finish_reshape(struct mddev *mddev)
9176 /* called be personality module when reshape completes. */
9177 struct md_rdev *rdev;
9179 rdev_for_each(rdev, mddev) {
9180 if (rdev->data_offset > rdev->new_data_offset)
9181 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
9183 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
9184 rdev->data_offset = rdev->new_data_offset;
9187 EXPORT_SYMBOL(md_finish_reshape);
9189 /* Bad block management */
9191 /* Returns 1 on success, 0 on failure */
9192 int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
9195 struct mddev *mddev = rdev->mddev;
9198 s += rdev->new_data_offset;
9200 s += rdev->data_offset;
9201 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
9203 /* Make sure they get written out promptly */
9204 if (test_bit(ExternalBbl, &rdev->flags))
9205 sysfs_notify(&rdev->kobj, NULL,
9206 "unacknowledged_bad_blocks");
9207 sysfs_notify_dirent_safe(rdev->sysfs_state);
9208 set_mask_bits(&mddev->sb_flags, 0,
9209 BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING));
9210 md_wakeup_thread(rdev->mddev->thread);
9215 EXPORT_SYMBOL_GPL(rdev_set_badblocks);
9217 int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
9222 s += rdev->new_data_offset;
9224 s += rdev->data_offset;
9225 rv = badblocks_clear(&rdev->badblocks, s, sectors);
9226 if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags))
9227 sysfs_notify(&rdev->kobj, NULL, "bad_blocks");
9230 EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
9232 static int md_notify_reboot(struct notifier_block *this,
9233 unsigned long code, void *x)
9235 struct list_head *tmp;
9236 struct mddev *mddev;
9239 for_each_mddev(mddev, tmp) {
9240 if (mddev_trylock(mddev)) {
9242 __md_stop_writes(mddev);
9243 if (mddev->persistent)
9244 mddev->safemode = 2;
9245 mddev_unlock(mddev);
9250 * certain more exotic SCSI devices are known to be
9251 * volatile wrt too early system reboots. While the
9252 * right place to handle this issue is the given
9253 * driver, we do want to have a safe RAID driver ...
9261 static struct notifier_block md_notifier = {
9262 .notifier_call = md_notify_reboot,
9264 .priority = INT_MAX, /* before any real devices */
9267 static void md_geninit(void)
9269 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
9271 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
9274 static int __init md_init(void)
9278 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
9282 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
9286 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
9289 if ((ret = register_blkdev(0, "mdp")) < 0)
9293 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
9294 md_probe, NULL, NULL);
9295 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
9296 md_probe, NULL, NULL);
9298 register_reboot_notifier(&md_notifier);
9299 raid_table_header = register_sysctl_table(raid_root_table);
9305 unregister_blkdev(MD_MAJOR, "md");
9307 destroy_workqueue(md_misc_wq);
9309 destroy_workqueue(md_wq);
9314 static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
9316 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
9317 struct md_rdev *rdev2;
9319 char b[BDEVNAME_SIZE];
9322 * If size is changed in another node then we need to
9323 * do resize as well.
9325 if (mddev->dev_sectors != le64_to_cpu(sb->size)) {
9326 ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size));
9328 pr_info("md-cluster: resize failed\n");
9330 md_bitmap_update_sb(mddev->bitmap);
9333 /* Check for change of roles in the active devices */
9334 rdev_for_each(rdev2, mddev) {
9335 if (test_bit(Faulty, &rdev2->flags))
9338 /* Check if the roles changed */
9339 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
9341 if (test_bit(Candidate, &rdev2->flags)) {
9342 if (role == 0xfffe) {
9343 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
9344 md_kick_rdev_from_array(rdev2);
9348 clear_bit(Candidate, &rdev2->flags);
9351 if (role != rdev2->raid_disk) {
9353 * got activated except reshape is happening.
9355 if (rdev2->raid_disk == -1 && role != 0xffff &&
9356 !(le32_to_cpu(sb->feature_map) &
9357 MD_FEATURE_RESHAPE_ACTIVE)) {
9358 rdev2->saved_raid_disk = role;
9359 ret = remove_and_add_spares(mddev, rdev2);
9360 pr_info("Activated spare: %s\n",
9361 bdevname(rdev2->bdev,b));
9362 /* wakeup mddev->thread here, so array could
9363 * perform resync with the new activated disk */
9364 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9365 md_wakeup_thread(mddev->thread);
9368 * We just want to do the minimum to mark the disk
9369 * as faulty. The recovery is performed by the
9370 * one who initiated the error.
9372 if ((role == 0xfffe) || (role == 0xfffd)) {
9373 md_error(mddev, rdev2);
9374 clear_bit(Blocked, &rdev2->flags);
9379 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
9380 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
9383 * Since mddev->delta_disks has already updated in update_raid_disks,
9384 * so it is time to check reshape.
9386 if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
9387 (le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
9389 * reshape is happening in the remote node, we need to
9390 * update reshape_position and call start_reshape.
9392 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
9393 if (mddev->pers->update_reshape_pos)
9394 mddev->pers->update_reshape_pos(mddev);
9395 if (mddev->pers->start_reshape)
9396 mddev->pers->start_reshape(mddev);
9397 } else if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
9398 mddev->reshape_position != MaxSector &&
9399 !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
9400 /* reshape is just done in another node. */
9401 mddev->reshape_position = MaxSector;
9402 if (mddev->pers->update_reshape_pos)
9403 mddev->pers->update_reshape_pos(mddev);
9406 /* Finally set the event to be up to date */
9407 mddev->events = le64_to_cpu(sb->events);
9410 static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
9413 struct page *swapout = rdev->sb_page;
9414 struct mdp_superblock_1 *sb;
9416 /* Store the sb page of the rdev in the swapout temporary
9417 * variable in case we err in the future
9419 rdev->sb_page = NULL;
9420 err = alloc_disk_sb(rdev);
9422 ClearPageUptodate(rdev->sb_page);
9423 rdev->sb_loaded = 0;
9424 err = super_types[mddev->major_version].
9425 load_super(rdev, NULL, mddev->minor_version);
9428 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9429 __func__, __LINE__, rdev->desc_nr, err);
9431 put_page(rdev->sb_page);
9432 rdev->sb_page = swapout;
9433 rdev->sb_loaded = 1;
9437 sb = page_address(rdev->sb_page);
9438 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9442 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
9443 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
9445 /* The other node finished recovery, call spare_active to set
9446 * device In_sync and mddev->degraded
9448 if (rdev->recovery_offset == MaxSector &&
9449 !test_bit(In_sync, &rdev->flags) &&
9450 mddev->pers->spare_active(mddev))
9451 sysfs_notify(&mddev->kobj, NULL, "degraded");
9457 void md_reload_sb(struct mddev *mddev, int nr)
9459 struct md_rdev *rdev;
9463 rdev_for_each_rcu(rdev, mddev) {
9464 if (rdev->desc_nr == nr)
9468 if (!rdev || rdev->desc_nr != nr) {
9469 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
9473 err = read_rdev(mddev, rdev);
9477 check_sb_changes(mddev, rdev);
9479 /* Read all rdev's to update recovery_offset */
9480 rdev_for_each_rcu(rdev, mddev) {
9481 if (!test_bit(Faulty, &rdev->flags))
9482 read_rdev(mddev, rdev);
9485 EXPORT_SYMBOL(md_reload_sb);
9490 * Searches all registered partitions for autorun RAID arrays
9494 static DEFINE_MUTEX(detected_devices_mutex);
9495 static LIST_HEAD(all_detected_devices);
9496 struct detected_devices_node {
9497 struct list_head list;
9501 void md_autodetect_dev(dev_t dev)
9503 struct detected_devices_node *node_detected_dev;
9505 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
9506 if (node_detected_dev) {
9507 node_detected_dev->dev = dev;
9508 mutex_lock(&detected_devices_mutex);
9509 list_add_tail(&node_detected_dev->list, &all_detected_devices);
9510 mutex_unlock(&detected_devices_mutex);
9514 static void autostart_arrays(int part)
9516 struct md_rdev *rdev;
9517 struct detected_devices_node *node_detected_dev;
9519 int i_scanned, i_passed;
9524 pr_info("md: Autodetecting RAID arrays.\n");
9526 mutex_lock(&detected_devices_mutex);
9527 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
9529 node_detected_dev = list_entry(all_detected_devices.next,
9530 struct detected_devices_node, list);
9531 list_del(&node_detected_dev->list);
9532 dev = node_detected_dev->dev;
9533 kfree(node_detected_dev);
9534 mutex_unlock(&detected_devices_mutex);
9535 rdev = md_import_device(dev,0, 90);
9536 mutex_lock(&detected_devices_mutex);
9540 if (test_bit(Faulty, &rdev->flags))
9543 set_bit(AutoDetected, &rdev->flags);
9544 list_add(&rdev->same_set, &pending_raid_disks);
9547 mutex_unlock(&detected_devices_mutex);
9549 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed);
9551 autorun_devices(part);
9554 #endif /* !MODULE */
9556 static __exit void md_exit(void)
9558 struct mddev *mddev;
9559 struct list_head *tmp;
9562 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
9563 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
9565 unregister_blkdev(MD_MAJOR,"md");
9566 unregister_blkdev(mdp_major, "mdp");
9567 unregister_reboot_notifier(&md_notifier);
9568 unregister_sysctl_table(raid_table_header);
9570 /* We cannot unload the modules while some process is
9571 * waiting for us in select() or poll() - wake them up
9574 while (waitqueue_active(&md_event_waiters)) {
9575 /* not safe to leave yet */
9576 wake_up(&md_event_waiters);
9580 remove_proc_entry("mdstat", NULL);
9582 for_each_mddev(mddev, tmp) {
9583 export_array(mddev);
9585 mddev->hold_active = 0;
9587 * for_each_mddev() will call mddev_put() at the end of each
9588 * iteration. As the mddev is now fully clear, this will
9589 * schedule the mddev for destruction by a workqueue, and the
9590 * destroy_workqueue() below will wait for that to complete.
9593 destroy_workqueue(md_misc_wq);
9594 destroy_workqueue(md_wq);
9597 subsys_initcall(md_init);
9598 module_exit(md_exit)
9600 static int get_ro(char *buffer, const struct kernel_param *kp)
9602 return sprintf(buffer, "%d", start_readonly);
9604 static int set_ro(const char *val, const struct kernel_param *kp)
9606 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
9609 module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9610 module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
9611 module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
9612 module_param(create_on_open, bool, S_IRUSR|S_IWUSR);
9614 MODULE_LICENSE("GPL");
9615 MODULE_DESCRIPTION("MD RAID framework");
9617 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);