1 // SPDX-License-Identifier: GPL-2.0-or-later
3 md.c : Multiple Devices driver for Linux
4 Copyright (C) 1998, 1999, 2000 Ingo Molnar
6 completely rewritten, based on the MD driver code from Marc Zyngier
10 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
11 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
12 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
13 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
14 - kmod support by: Cyrus Durgin
15 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
16 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
18 - lots of fixes and improvements to the RAID1/RAID5 and generic
19 RAID code (such as request based resynchronization):
21 Neil Brown <neilb@cse.unsw.edu.au>.
23 - persistent bitmap code
24 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
27 Errors, Warnings, etc.
29 pr_crit() for error conditions that risk data loss
30 pr_err() for error conditions that are unexpected, like an IO error
31 or internal inconsistency
32 pr_warn() for error conditions that could have been predicated, like
33 adding a device to an array when it has incompatible metadata
34 pr_info() for every interesting, very rare events, like an array starting
35 or stopping, or resync starting or stopping
36 pr_debug() for everything else.
40 #include <linux/sched/mm.h>
41 #include <linux/sched/signal.h>
42 #include <linux/kthread.h>
43 #include <linux/blkdev.h>
44 #include <linux/blk-integrity.h>
45 #include <linux/badblocks.h>
46 #include <linux/sysctl.h>
47 #include <linux/seq_file.h>
49 #include <linux/poll.h>
50 #include <linux/ctype.h>
51 #include <linux/string.h>
52 #include <linux/hdreg.h>
53 #include <linux/proc_fs.h>
54 #include <linux/random.h>
55 #include <linux/major.h>
56 #include <linux/module.h>
57 #include <linux/reboot.h>
58 #include <linux/file.h>
59 #include <linux/compat.h>
60 #include <linux/delay.h>
61 #include <linux/raid/md_p.h>
62 #include <linux/raid/md_u.h>
63 #include <linux/raid/detect.h>
64 #include <linux/slab.h>
65 #include <linux/percpu-refcount.h>
66 #include <linux/part_stat.h>
69 #include "md-bitmap.h"
70 #include "md-cluster.h"
72 static const char *action_name
[NR_SYNC_ACTIONS
] = {
73 [ACTION_RESYNC
] = "resync",
74 [ACTION_RECOVER
] = "recover",
75 [ACTION_CHECK
] = "check",
76 [ACTION_REPAIR
] = "repair",
77 [ACTION_RESHAPE
] = "reshape",
78 [ACTION_FROZEN
] = "frozen",
79 [ACTION_IDLE
] = "idle",
82 /* pers_list is a list of registered personalities protected by pers_lock. */
83 static LIST_HEAD(pers_list
);
84 static DEFINE_SPINLOCK(pers_lock
);
86 static const struct kobj_type md_ktype
;
88 const struct md_cluster_operations
*md_cluster_ops
;
89 EXPORT_SYMBOL(md_cluster_ops
);
90 static struct module
*md_cluster_mod
;
92 static DECLARE_WAIT_QUEUE_HEAD(resync_wait
);
93 static struct workqueue_struct
*md_wq
;
96 * This workqueue is used for sync_work to register new sync_thread, and for
97 * del_work to remove rdev, and for event_work that is only set by dm-raid.
99 * Noted that sync_work will grab reconfig_mutex, hence never flush this
100 * workqueue whith reconfig_mutex grabbed.
102 static struct workqueue_struct
*md_misc_wq
;
103 struct workqueue_struct
*md_bitmap_wq
;
105 static int remove_and_add_spares(struct mddev
*mddev
,
106 struct md_rdev
*this);
107 static void mddev_detach(struct mddev
*mddev
);
108 static void export_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
);
109 static void md_wakeup_thread_directly(struct md_thread __rcu
*thread
);
112 * Default number of read corrections we'll attempt on an rdev
113 * before ejecting it from the array. We divide the read error
114 * count by 2 for every hour elapsed between read errors.
116 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
117 /* Default safemode delay: 200 msec */
118 #define DEFAULT_SAFEMODE_DELAY ((200 * HZ)/1000 +1)
120 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
121 * is 1000 KB/sec, so the extra system load does not show up that much.
122 * Increase it if you want to have more _guaranteed_ speed. Note that
123 * the RAID driver will use the maximum available bandwidth if the IO
124 * subsystem is idle. There is also an 'absolute maximum' reconstruction
125 * speed limit - in case reconstruction slows down your system despite
128 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
129 * or /sys/block/mdX/md/sync_speed_{min,max}
132 static int sysctl_speed_limit_min
= 1000;
133 static int sysctl_speed_limit_max
= 200000;
134 static inline int speed_min(struct mddev
*mddev
)
136 return mddev
->sync_speed_min
?
137 mddev
->sync_speed_min
: sysctl_speed_limit_min
;
140 static inline int speed_max(struct mddev
*mddev
)
142 return mddev
->sync_speed_max
?
143 mddev
->sync_speed_max
: sysctl_speed_limit_max
;
146 static void rdev_uninit_serial(struct md_rdev
*rdev
)
148 if (!test_and_clear_bit(CollisionCheck
, &rdev
->flags
))
151 kvfree(rdev
->serial
);
155 static void rdevs_uninit_serial(struct mddev
*mddev
)
157 struct md_rdev
*rdev
;
159 rdev_for_each(rdev
, mddev
)
160 rdev_uninit_serial(rdev
);
163 static int rdev_init_serial(struct md_rdev
*rdev
)
165 /* serial_nums equals with BARRIER_BUCKETS_NR */
166 int i
, serial_nums
= 1 << ((PAGE_SHIFT
- ilog2(sizeof(atomic_t
))));
167 struct serial_in_rdev
*serial
= NULL
;
169 if (test_bit(CollisionCheck
, &rdev
->flags
))
172 serial
= kvmalloc(sizeof(struct serial_in_rdev
) * serial_nums
,
177 for (i
= 0; i
< serial_nums
; i
++) {
178 struct serial_in_rdev
*serial_tmp
= &serial
[i
];
180 spin_lock_init(&serial_tmp
->serial_lock
);
181 serial_tmp
->serial_rb
= RB_ROOT_CACHED
;
182 init_waitqueue_head(&serial_tmp
->serial_io_wait
);
185 rdev
->serial
= serial
;
186 set_bit(CollisionCheck
, &rdev
->flags
);
191 static int rdevs_init_serial(struct mddev
*mddev
)
193 struct md_rdev
*rdev
;
196 rdev_for_each(rdev
, mddev
) {
197 ret
= rdev_init_serial(rdev
);
202 /* Free all resources if pool is not existed */
203 if (ret
&& !mddev
->serial_info_pool
)
204 rdevs_uninit_serial(mddev
);
210 * rdev needs to enable serial stuffs if it meets the conditions:
211 * 1. it is multi-queue device flaged with writemostly.
212 * 2. the write-behind mode is enabled.
214 static int rdev_need_serial(struct md_rdev
*rdev
)
216 return (rdev
&& rdev
->mddev
->bitmap_info
.max_write_behind
> 0 &&
217 rdev
->bdev
->bd_disk
->queue
->nr_hw_queues
!= 1 &&
218 test_bit(WriteMostly
, &rdev
->flags
));
222 * Init resource for rdev(s), then create serial_info_pool if:
223 * 1. rdev is the first device which return true from rdev_enable_serial.
224 * 2. rdev is NULL, means we want to enable serialization for all rdevs.
226 void mddev_create_serial_pool(struct mddev
*mddev
, struct md_rdev
*rdev
)
230 if (rdev
&& !rdev_need_serial(rdev
) &&
231 !test_bit(CollisionCheck
, &rdev
->flags
))
235 ret
= rdevs_init_serial(mddev
);
237 ret
= rdev_init_serial(rdev
);
241 if (mddev
->serial_info_pool
== NULL
) {
243 * already in memalloc noio context by
246 mddev
->serial_info_pool
=
247 mempool_create_kmalloc_pool(NR_SERIAL_INFOS
,
248 sizeof(struct serial_info
));
249 if (!mddev
->serial_info_pool
) {
250 rdevs_uninit_serial(mddev
);
251 pr_err("can't alloc memory pool for serialization\n");
257 * Free resource from rdev(s), and destroy serial_info_pool under conditions:
258 * 1. rdev is the last device flaged with CollisionCheck.
259 * 2. when bitmap is destroyed while policy is not enabled.
260 * 3. for disable policy, the pool is destroyed only when no rdev needs it.
262 void mddev_destroy_serial_pool(struct mddev
*mddev
, struct md_rdev
*rdev
)
264 if (rdev
&& !test_bit(CollisionCheck
, &rdev
->flags
))
267 if (mddev
->serial_info_pool
) {
268 struct md_rdev
*temp
;
269 int num
= 0; /* used to track if other rdevs need the pool */
271 rdev_for_each(temp
, mddev
) {
273 if (!mddev
->serialize_policy
||
274 !rdev_need_serial(temp
))
275 rdev_uninit_serial(temp
);
278 } else if (temp
!= rdev
&&
279 test_bit(CollisionCheck
, &temp
->flags
))
284 rdev_uninit_serial(rdev
);
287 pr_info("The mempool could be used by other devices\n");
289 mempool_destroy(mddev
->serial_info_pool
);
290 mddev
->serial_info_pool
= NULL
;
295 static struct ctl_table_header
*raid_table_header
;
297 static struct ctl_table raid_table
[] = {
299 .procname
= "speed_limit_min",
300 .data
= &sysctl_speed_limit_min
,
301 .maxlen
= sizeof(int),
302 .mode
= S_IRUGO
|S_IWUSR
,
303 .proc_handler
= proc_dointvec
,
306 .procname
= "speed_limit_max",
307 .data
= &sysctl_speed_limit_max
,
308 .maxlen
= sizeof(int),
309 .mode
= S_IRUGO
|S_IWUSR
,
310 .proc_handler
= proc_dointvec
,
314 static int start_readonly
;
317 * The original mechanism for creating an md device is to create
318 * a device node in /dev and to open it. This causes races with device-close.
319 * The preferred method is to write to the "new_array" module parameter.
320 * This can avoid races.
321 * Setting create_on_open to false disables the original mechanism
322 * so all the races disappear.
324 static bool create_on_open
= true;
327 * We have a system wide 'event count' that is incremented
328 * on any 'interesting' event, and readers of /proc/mdstat
329 * can use 'poll' or 'select' to find out when the event
333 * start array, stop array, error, add device, remove device,
334 * start build, activate spare
336 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters
);
337 static atomic_t md_event_count
;
338 void md_new_event(void)
340 atomic_inc(&md_event_count
);
341 wake_up(&md_event_waiters
);
343 EXPORT_SYMBOL_GPL(md_new_event
);
346 * Enables to iterate over all existing md arrays
347 * all_mddevs_lock protects this list.
349 static LIST_HEAD(all_mddevs
);
350 static DEFINE_SPINLOCK(all_mddevs_lock
);
352 static bool is_md_suspended(struct mddev
*mddev
)
354 return percpu_ref_is_dying(&mddev
->active_io
);
356 /* Rather than calling directly into the personality make_request function,
357 * IO requests come here first so that we can check if the device is
358 * being suspended pending a reconfiguration.
359 * We hold a refcount over the call to ->make_request. By the time that
360 * call has finished, the bio has been linked into some internal structure
361 * and so is visible to ->quiesce(), so we don't need the refcount any more.
363 static bool is_suspended(struct mddev
*mddev
, struct bio
*bio
)
365 if (is_md_suspended(mddev
))
367 if (bio_data_dir(bio
) != WRITE
)
369 if (READ_ONCE(mddev
->suspend_lo
) >= READ_ONCE(mddev
->suspend_hi
))
371 if (bio
->bi_iter
.bi_sector
>= READ_ONCE(mddev
->suspend_hi
))
373 if (bio_end_sector(bio
) < READ_ONCE(mddev
->suspend_lo
))
378 bool md_handle_request(struct mddev
*mddev
, struct bio
*bio
)
381 if (is_suspended(mddev
, bio
)) {
383 /* Bail out if REQ_NOWAIT is set for the bio */
384 if (bio
->bi_opf
& REQ_NOWAIT
) {
385 bio_wouldblock_error(bio
);
389 prepare_to_wait(&mddev
->sb_wait
, &__wait
,
390 TASK_UNINTERRUPTIBLE
);
391 if (!is_suspended(mddev
, bio
))
395 finish_wait(&mddev
->sb_wait
, &__wait
);
397 if (!percpu_ref_tryget_live(&mddev
->active_io
))
398 goto check_suspended
;
400 if (!mddev
->pers
->make_request(mddev
, bio
)) {
401 percpu_ref_put(&mddev
->active_io
);
402 if (!mddev
->gendisk
&& mddev
->pers
->prepare_suspend
)
404 goto check_suspended
;
407 percpu_ref_put(&mddev
->active_io
);
410 EXPORT_SYMBOL(md_handle_request
);
412 static void md_submit_bio(struct bio
*bio
)
414 const int rw
= bio_data_dir(bio
);
415 struct mddev
*mddev
= bio
->bi_bdev
->bd_disk
->private_data
;
417 if (mddev
== NULL
|| mddev
->pers
== NULL
) {
422 if (unlikely(test_bit(MD_BROKEN
, &mddev
->flags
)) && (rw
== WRITE
)) {
427 bio
= bio_split_to_limits(bio
);
431 if (mddev
->ro
== MD_RDONLY
&& unlikely(rw
== WRITE
)) {
432 if (bio_sectors(bio
) != 0)
433 bio
->bi_status
= BLK_STS_IOERR
;
438 /* bio could be mergeable after passing to underlayer */
439 bio
->bi_opf
&= ~REQ_NOMERGE
;
441 md_handle_request(mddev
, bio
);
445 * Make sure no new requests are submitted to the device, and any requests that
446 * have been submitted are completely handled.
448 int mddev_suspend(struct mddev
*mddev
, bool interruptible
)
453 * hold reconfig_mutex to wait for normal io will deadlock, because
454 * other context can't update super_block, and normal io can rely on
455 * updating super_block.
457 lockdep_assert_not_held(&mddev
->reconfig_mutex
);
460 err
= mutex_lock_interruptible(&mddev
->suspend_mutex
);
462 mutex_lock(&mddev
->suspend_mutex
);
466 if (mddev
->suspended
) {
467 WRITE_ONCE(mddev
->suspended
, mddev
->suspended
+ 1);
468 mutex_unlock(&mddev
->suspend_mutex
);
472 percpu_ref_kill(&mddev
->active_io
);
474 err
= wait_event_interruptible(mddev
->sb_wait
,
475 percpu_ref_is_zero(&mddev
->active_io
));
477 wait_event(mddev
->sb_wait
,
478 percpu_ref_is_zero(&mddev
->active_io
));
480 percpu_ref_resurrect(&mddev
->active_io
);
481 mutex_unlock(&mddev
->suspend_mutex
);
486 * For raid456, io might be waiting for reshape to make progress,
487 * allow new reshape to start while waiting for io to be done to
490 WRITE_ONCE(mddev
->suspended
, mddev
->suspended
+ 1);
492 /* restrict memory reclaim I/O during raid array is suspend */
493 mddev
->noio_flag
= memalloc_noio_save();
495 mutex_unlock(&mddev
->suspend_mutex
);
498 EXPORT_SYMBOL_GPL(mddev_suspend
);
500 static void __mddev_resume(struct mddev
*mddev
, bool recovery_needed
)
502 lockdep_assert_not_held(&mddev
->reconfig_mutex
);
504 mutex_lock(&mddev
->suspend_mutex
);
505 WRITE_ONCE(mddev
->suspended
, mddev
->suspended
- 1);
506 if (mddev
->suspended
) {
507 mutex_unlock(&mddev
->suspend_mutex
);
511 /* entred the memalloc scope from mddev_suspend() */
512 memalloc_noio_restore(mddev
->noio_flag
);
514 percpu_ref_resurrect(&mddev
->active_io
);
515 wake_up(&mddev
->sb_wait
);
518 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
519 md_wakeup_thread(mddev
->thread
);
520 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
522 mutex_unlock(&mddev
->suspend_mutex
);
525 void mddev_resume(struct mddev
*mddev
)
527 return __mddev_resume(mddev
, true);
529 EXPORT_SYMBOL_GPL(mddev_resume
);
531 /* sync bdev before setting device to readonly or stopping raid*/
532 static int mddev_set_closing_and_sync_blockdev(struct mddev
*mddev
, int opener_num
)
534 mutex_lock(&mddev
->open_mutex
);
535 if (mddev
->pers
&& atomic_read(&mddev
->openers
) > opener_num
) {
536 mutex_unlock(&mddev
->open_mutex
);
539 if (test_and_set_bit(MD_CLOSING
, &mddev
->flags
)) {
540 mutex_unlock(&mddev
->open_mutex
);
543 mutex_unlock(&mddev
->open_mutex
);
545 sync_blockdev(mddev
->gendisk
->part0
);
550 * The only difference from bio_chain_endio() is that the current
551 * bi_status of bio does not affect the bi_status of parent.
553 static void md_end_flush(struct bio
*bio
)
555 struct bio
*parent
= bio
->bi_private
;
558 * If any flush io error before the power failure,
559 * disk data may be lost.
562 pr_err("md: %pg flush io error %d\n", bio
->bi_bdev
,
563 blk_status_to_errno(bio
->bi_status
));
569 bool md_flush_request(struct mddev
*mddev
, struct bio
*bio
)
571 struct md_rdev
*rdev
;
575 * md_flush_reqeust() should be called under md_handle_request() and
576 * 'active_io' is already grabbed. Hence it's safe to get rdev directly
577 * without rcu protection.
579 WARN_ON(percpu_ref_is_zero(&mddev
->active_io
));
581 rdev_for_each(rdev
, mddev
) {
582 if (rdev
->raid_disk
< 0 || test_bit(Faulty
, &rdev
->flags
))
585 new = bio_alloc_bioset(rdev
->bdev
, 0,
586 REQ_OP_WRITE
| REQ_PREFLUSH
, GFP_NOIO
,
588 new->bi_private
= bio
;
589 new->bi_end_io
= md_end_flush
;
590 bio_inc_remaining(bio
);
594 if (bio_sectors(bio
) == 0) {
599 bio
->bi_opf
&= ~REQ_PREFLUSH
;
602 EXPORT_SYMBOL(md_flush_request
);
604 static inline struct mddev
*mddev_get(struct mddev
*mddev
)
606 lockdep_assert_held(&all_mddevs_lock
);
608 if (test_bit(MD_DELETED
, &mddev
->flags
))
610 atomic_inc(&mddev
->active
);
614 static void mddev_delayed_delete(struct work_struct
*ws
);
616 static void __mddev_put(struct mddev
*mddev
)
618 if (mddev
->raid_disks
|| !list_empty(&mddev
->disks
) ||
619 mddev
->ctime
|| mddev
->hold_active
)
622 /* Array is not configured at all, and not held active, so destroy it */
623 set_bit(MD_DELETED
, &mddev
->flags
);
626 * Call queue_work inside the spinlock so that flush_workqueue() after
627 * mddev_find will succeed in waiting for the work to be done.
629 queue_work(md_misc_wq
, &mddev
->del_work
);
632 void mddev_put(struct mddev
*mddev
)
634 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
638 spin_unlock(&all_mddevs_lock
);
641 static void md_safemode_timeout(struct timer_list
*t
);
642 static void md_start_sync(struct work_struct
*ws
);
644 static void active_io_release(struct percpu_ref
*ref
)
646 struct mddev
*mddev
= container_of(ref
, struct mddev
, active_io
);
648 wake_up(&mddev
->sb_wait
);
651 static void no_op(struct percpu_ref
*r
) {}
653 int mddev_init(struct mddev
*mddev
)
656 if (percpu_ref_init(&mddev
->active_io
, active_io_release
,
657 PERCPU_REF_ALLOW_REINIT
, GFP_KERNEL
))
660 if (percpu_ref_init(&mddev
->writes_pending
, no_op
,
661 PERCPU_REF_ALLOW_REINIT
, GFP_KERNEL
)) {
662 percpu_ref_exit(&mddev
->active_io
);
666 /* We want to start with the refcount at zero */
667 percpu_ref_put(&mddev
->writes_pending
);
669 mutex_init(&mddev
->open_mutex
);
670 mutex_init(&mddev
->reconfig_mutex
);
671 mutex_init(&mddev
->suspend_mutex
);
672 mutex_init(&mddev
->bitmap_info
.mutex
);
673 INIT_LIST_HEAD(&mddev
->disks
);
674 INIT_LIST_HEAD(&mddev
->all_mddevs
);
675 INIT_LIST_HEAD(&mddev
->deleting
);
676 timer_setup(&mddev
->safemode_timer
, md_safemode_timeout
, 0);
677 atomic_set(&mddev
->active
, 1);
678 atomic_set(&mddev
->openers
, 0);
679 atomic_set(&mddev
->sync_seq
, 0);
680 spin_lock_init(&mddev
->lock
);
681 init_waitqueue_head(&mddev
->sb_wait
);
682 init_waitqueue_head(&mddev
->recovery_wait
);
683 mddev
->reshape_position
= MaxSector
;
684 mddev
->reshape_backwards
= 0;
685 mddev
->last_sync_action
= ACTION_IDLE
;
686 mddev
->resync_min
= 0;
687 mddev
->resync_max
= MaxSector
;
688 mddev
->level
= LEVEL_NONE
;
689 mddev_set_bitmap_ops(mddev
);
691 INIT_WORK(&mddev
->sync_work
, md_start_sync
);
692 INIT_WORK(&mddev
->del_work
, mddev_delayed_delete
);
696 EXPORT_SYMBOL_GPL(mddev_init
);
698 void mddev_destroy(struct mddev
*mddev
)
700 percpu_ref_exit(&mddev
->active_io
);
701 percpu_ref_exit(&mddev
->writes_pending
);
703 EXPORT_SYMBOL_GPL(mddev_destroy
);
705 static struct mddev
*mddev_find_locked(dev_t unit
)
709 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
710 if (mddev
->unit
== unit
)
716 /* find an unused unit number */
717 static dev_t
mddev_alloc_unit(void)
719 static int next_minor
= 512;
720 int start
= next_minor
;
725 dev
= MKDEV(MD_MAJOR
, next_minor
);
727 if (next_minor
> MINORMASK
)
729 if (next_minor
== start
)
730 return 0; /* Oh dear, all in use. */
731 is_free
= !mddev_find_locked(dev
);
737 static struct mddev
*mddev_alloc(dev_t unit
)
742 if (unit
&& MAJOR(unit
) != MD_MAJOR
)
743 unit
&= ~((1 << MdpMinorShift
) - 1);
745 new = kzalloc(sizeof(*new), GFP_KERNEL
);
747 return ERR_PTR(-ENOMEM
);
749 error
= mddev_init(new);
753 spin_lock(&all_mddevs_lock
);
756 if (mddev_find_locked(unit
))
757 goto out_destroy_new
;
759 if (MAJOR(unit
) == MD_MAJOR
)
760 new->md_minor
= MINOR(unit
);
762 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
763 new->hold_active
= UNTIL_IOCTL
;
766 new->unit
= mddev_alloc_unit();
768 goto out_destroy_new
;
769 new->md_minor
= MINOR(new->unit
);
770 new->hold_active
= UNTIL_STOP
;
773 list_add(&new->all_mddevs
, &all_mddevs
);
774 spin_unlock(&all_mddevs_lock
);
778 spin_unlock(&all_mddevs_lock
);
782 return ERR_PTR(error
);
785 static void mddev_free(struct mddev
*mddev
)
787 spin_lock(&all_mddevs_lock
);
788 list_del(&mddev
->all_mddevs
);
789 spin_unlock(&all_mddevs_lock
);
791 mddev_destroy(mddev
);
795 static const struct attribute_group md_redundancy_group
;
797 void mddev_unlock(struct mddev
*mddev
)
799 struct md_rdev
*rdev
;
803 if (!list_empty(&mddev
->deleting
))
804 list_splice_init(&mddev
->deleting
, &delete);
806 if (mddev
->to_remove
) {
807 /* These cannot be removed under reconfig_mutex as
808 * an access to the files will try to take reconfig_mutex
809 * while holding the file unremovable, which leads to
811 * So hold set sysfs_active while the remove in happeing,
812 * and anything else which might set ->to_remove or my
813 * otherwise change the sysfs namespace will fail with
814 * -EBUSY if sysfs_active is still set.
815 * We set sysfs_active under reconfig_mutex and elsewhere
816 * test it under the same mutex to ensure its correct value
819 const struct attribute_group
*to_remove
= mddev
->to_remove
;
820 mddev
->to_remove
= NULL
;
821 mddev
->sysfs_active
= 1;
822 mutex_unlock(&mddev
->reconfig_mutex
);
824 if (mddev
->kobj
.sd
) {
825 if (to_remove
!= &md_redundancy_group
)
826 sysfs_remove_group(&mddev
->kobj
, to_remove
);
827 if (mddev
->pers
== NULL
||
828 mddev
->pers
->sync_request
== NULL
) {
829 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
830 if (mddev
->sysfs_action
)
831 sysfs_put(mddev
->sysfs_action
);
832 if (mddev
->sysfs_completed
)
833 sysfs_put(mddev
->sysfs_completed
);
834 if (mddev
->sysfs_degraded
)
835 sysfs_put(mddev
->sysfs_degraded
);
836 mddev
->sysfs_action
= NULL
;
837 mddev
->sysfs_completed
= NULL
;
838 mddev
->sysfs_degraded
= NULL
;
841 mddev
->sysfs_active
= 0;
843 mutex_unlock(&mddev
->reconfig_mutex
);
845 md_wakeup_thread(mddev
->thread
);
846 wake_up(&mddev
->sb_wait
);
848 list_for_each_entry_safe(rdev
, tmp
, &delete, same_set
) {
849 list_del_init(&rdev
->same_set
);
850 kobject_del(&rdev
->kobj
);
851 export_rdev(rdev
, mddev
);
854 EXPORT_SYMBOL_GPL(mddev_unlock
);
856 struct md_rdev
*md_find_rdev_nr_rcu(struct mddev
*mddev
, int nr
)
858 struct md_rdev
*rdev
;
860 rdev_for_each_rcu(rdev
, mddev
)
861 if (rdev
->desc_nr
== nr
)
866 EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu
);
868 static struct md_rdev
*find_rdev(struct mddev
*mddev
, dev_t dev
)
870 struct md_rdev
*rdev
;
872 rdev_for_each(rdev
, mddev
)
873 if (rdev
->bdev
->bd_dev
== dev
)
879 struct md_rdev
*md_find_rdev_rcu(struct mddev
*mddev
, dev_t dev
)
881 struct md_rdev
*rdev
;
883 rdev_for_each_rcu(rdev
, mddev
)
884 if (rdev
->bdev
->bd_dev
== dev
)
889 EXPORT_SYMBOL_GPL(md_find_rdev_rcu
);
891 static struct md_personality
*find_pers(int level
, char *clevel
)
893 struct md_personality
*pers
;
894 list_for_each_entry(pers
, &pers_list
, list
) {
895 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
897 if (strcmp(pers
->name
, clevel
)==0)
903 /* return the offset of the super block in 512byte sectors */
904 static inline sector_t
calc_dev_sboffset(struct md_rdev
*rdev
)
906 return MD_NEW_SIZE_SECTORS(bdev_nr_sectors(rdev
->bdev
));
909 static int alloc_disk_sb(struct md_rdev
*rdev
)
911 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
917 void md_rdev_clear(struct md_rdev
*rdev
)
920 put_page(rdev
->sb_page
);
922 rdev
->sb_page
= NULL
;
927 put_page(rdev
->bb_page
);
928 rdev
->bb_page
= NULL
;
930 badblocks_exit(&rdev
->badblocks
);
932 EXPORT_SYMBOL_GPL(md_rdev_clear
);
934 static void super_written(struct bio
*bio
)
936 struct md_rdev
*rdev
= bio
->bi_private
;
937 struct mddev
*mddev
= rdev
->mddev
;
939 if (bio
->bi_status
) {
940 pr_err("md: %s gets error=%d\n", __func__
,
941 blk_status_to_errno(bio
->bi_status
));
942 md_error(mddev
, rdev
);
943 if (!test_bit(Faulty
, &rdev
->flags
)
944 && (bio
->bi_opf
& MD_FAILFAST
)) {
945 set_bit(MD_SB_NEED_REWRITE
, &mddev
->sb_flags
);
946 set_bit(LastDev
, &rdev
->flags
);
949 clear_bit(LastDev
, &rdev
->flags
);
953 rdev_dec_pending(rdev
, mddev
);
955 if (atomic_dec_and_test(&mddev
->pending_writes
))
956 wake_up(&mddev
->sb_wait
);
959 void md_super_write(struct mddev
*mddev
, struct md_rdev
*rdev
,
960 sector_t sector
, int size
, struct page
*page
)
962 /* write first size bytes of page to sector of rdev
963 * Increment mddev->pending_writes before returning
964 * and decrement it on completion, waking up sb_wait
965 * if zero is reached.
966 * If an error occurred, call md_error
973 if (test_bit(Faulty
, &rdev
->flags
))
976 bio
= bio_alloc_bioset(rdev
->meta_bdev
? rdev
->meta_bdev
: rdev
->bdev
,
978 REQ_OP_WRITE
| REQ_SYNC
| REQ_IDLE
| REQ_META
979 | REQ_PREFLUSH
| REQ_FUA
,
980 GFP_NOIO
, &mddev
->sync_set
);
982 atomic_inc(&rdev
->nr_pending
);
984 bio
->bi_iter
.bi_sector
= sector
;
985 __bio_add_page(bio
, page
, size
, 0);
986 bio
->bi_private
= rdev
;
987 bio
->bi_end_io
= super_written
;
989 if (test_bit(MD_FAILFAST_SUPPORTED
, &mddev
->flags
) &&
990 test_bit(FailFast
, &rdev
->flags
) &&
991 !test_bit(LastDev
, &rdev
->flags
))
992 bio
->bi_opf
|= MD_FAILFAST
;
994 atomic_inc(&mddev
->pending_writes
);
998 int md_super_wait(struct mddev
*mddev
)
1000 /* wait for all superblock writes that were scheduled to complete */
1001 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->pending_writes
)==0);
1002 if (test_and_clear_bit(MD_SB_NEED_REWRITE
, &mddev
->sb_flags
))
1007 int sync_page_io(struct md_rdev
*rdev
, sector_t sector
, int size
,
1008 struct page
*page
, blk_opf_t opf
, bool metadata_op
)
1011 struct bio_vec bvec
;
1013 if (metadata_op
&& rdev
->meta_bdev
)
1014 bio_init(&bio
, rdev
->meta_bdev
, &bvec
, 1, opf
);
1016 bio_init(&bio
, rdev
->bdev
, &bvec
, 1, opf
);
1019 bio
.bi_iter
.bi_sector
= sector
+ rdev
->sb_start
;
1020 else if (rdev
->mddev
->reshape_position
!= MaxSector
&&
1021 (rdev
->mddev
->reshape_backwards
==
1022 (sector
>= rdev
->mddev
->reshape_position
)))
1023 bio
.bi_iter
.bi_sector
= sector
+ rdev
->new_data_offset
;
1025 bio
.bi_iter
.bi_sector
= sector
+ rdev
->data_offset
;
1026 __bio_add_page(&bio
, page
, size
, 0);
1028 submit_bio_wait(&bio
);
1030 return !bio
.bi_status
;
1032 EXPORT_SYMBOL_GPL(sync_page_io
);
1034 static int read_disk_sb(struct md_rdev
*rdev
, int size
)
1036 if (rdev
->sb_loaded
)
1039 if (!sync_page_io(rdev
, 0, size
, rdev
->sb_page
, REQ_OP_READ
, true))
1041 rdev
->sb_loaded
= 1;
1045 pr_err("md: disabled device %pg, could not read superblock.\n",
1050 static int md_uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
1052 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
1053 sb1
->set_uuid1
== sb2
->set_uuid1
&&
1054 sb1
->set_uuid2
== sb2
->set_uuid2
&&
1055 sb1
->set_uuid3
== sb2
->set_uuid3
;
1058 static int md_sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
1061 mdp_super_t
*tmp1
, *tmp2
;
1063 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
1064 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
1066 if (!tmp1
|| !tmp2
) {
1075 * nr_disks is not constant
1080 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
1087 static u32
md_csum_fold(u32 csum
)
1089 csum
= (csum
& 0xffff) + (csum
>> 16);
1090 return (csum
& 0xffff) + (csum
>> 16);
1093 static unsigned int calc_sb_csum(mdp_super_t
*sb
)
1096 u32
*sb32
= (u32
*)sb
;
1098 unsigned int disk_csum
, csum
;
1100 disk_csum
= sb
->sb_csum
;
1103 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
1105 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
1108 /* This used to use csum_partial, which was wrong for several
1109 * reasons including that different results are returned on
1110 * different architectures. It isn't critical that we get exactly
1111 * the same return value as before (we always csum_fold before
1112 * testing, and that removes any differences). However as we
1113 * know that csum_partial always returned a 16bit value on
1114 * alphas, do a fold to maximise conformity to previous behaviour.
1116 sb
->sb_csum
= md_csum_fold(disk_csum
);
1118 sb
->sb_csum
= disk_csum
;
1124 * Handle superblock details.
1125 * We want to be able to handle multiple superblock formats
1126 * so we have a common interface to them all, and an array of
1127 * different handlers.
1128 * We rely on user-space to write the initial superblock, and support
1129 * reading and updating of superblocks.
1130 * Interface methods are:
1131 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
1132 * loads and validates a superblock on dev.
1133 * if refdev != NULL, compare superblocks on both devices
1135 * 0 - dev has a superblock that is compatible with refdev
1136 * 1 - dev has a superblock that is compatible and newer than refdev
1137 * so dev should be used as the refdev in future
1138 * -EINVAL superblock incompatible or invalid
1139 * -othererror e.g. -EIO
1141 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
1142 * Verify that dev is acceptable into mddev.
1143 * The first time, mddev->raid_disks will be 0, and data from
1144 * dev should be merged in. Subsequent calls check that dev
1145 * is new enough. Return 0 or -EINVAL
1147 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
1148 * Update the superblock for rdev with data in mddev
1149 * This does not write to disc.
1155 struct module
*owner
;
1156 int (*load_super
)(struct md_rdev
*rdev
,
1157 struct md_rdev
*refdev
,
1159 int (*validate_super
)(struct mddev
*mddev
,
1160 struct md_rdev
*freshest
,
1161 struct md_rdev
*rdev
);
1162 void (*sync_super
)(struct mddev
*mddev
,
1163 struct md_rdev
*rdev
);
1164 unsigned long long (*rdev_size_change
)(struct md_rdev
*rdev
,
1165 sector_t num_sectors
);
1166 int (*allow_new_offset
)(struct md_rdev
*rdev
,
1167 unsigned long long new_offset
);
1171 * Check that the given mddev has no bitmap.
1173 * This function is called from the run method of all personalities that do not
1174 * support bitmaps. It prints an error message and returns non-zero if mddev
1175 * has a bitmap. Otherwise, it returns 0.
1178 int md_check_no_bitmap(struct mddev
*mddev
)
1180 if (!mddev
->bitmap_info
.file
&& !mddev
->bitmap_info
.offset
)
1182 pr_warn("%s: bitmaps are not supported for %s\n",
1183 mdname(mddev
), mddev
->pers
->name
);
1186 EXPORT_SYMBOL(md_check_no_bitmap
);
1189 * load_super for 0.90.0
1191 static int super_90_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
1195 bool spare_disk
= true;
1198 * Calculate the position of the superblock (512byte sectors),
1199 * it's at the end of the disk.
1201 * It also happens to be a multiple of 4Kb.
1203 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1205 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
1211 sb
= page_address(rdev
->sb_page
);
1213 if (sb
->md_magic
!= MD_SB_MAGIC
) {
1214 pr_warn("md: invalid raid superblock magic on %pg\n",
1219 if (sb
->major_version
!= 0 ||
1220 sb
->minor_version
< 90 ||
1221 sb
->minor_version
> 91) {
1222 pr_warn("Bad version number %d.%d on %pg\n",
1223 sb
->major_version
, sb
->minor_version
, rdev
->bdev
);
1227 if (sb
->raid_disks
<= 0)
1230 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
1231 pr_warn("md: invalid superblock checksum on %pg\n", rdev
->bdev
);
1235 rdev
->preferred_minor
= sb
->md_minor
;
1236 rdev
->data_offset
= 0;
1237 rdev
->new_data_offset
= 0;
1238 rdev
->sb_size
= MD_SB_BYTES
;
1239 rdev
->badblocks
.shift
= -1;
1241 rdev
->desc_nr
= sb
->this_disk
.number
;
1243 /* not spare disk */
1244 if (rdev
->desc_nr
>= 0 && rdev
->desc_nr
< MD_SB_DISKS
&&
1245 sb
->disks
[rdev
->desc_nr
].state
& ((1<<MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
)))
1255 mdp_super_t
*refsb
= page_address(refdev
->sb_page
);
1256 if (!md_uuid_equal(refsb
, sb
)) {
1257 pr_warn("md: %pg has different UUID to %pg\n",
1258 rdev
->bdev
, refdev
->bdev
);
1261 if (!md_sb_equal(refsb
, sb
)) {
1262 pr_warn("md: %pg has same UUID but different superblock to %pg\n",
1263 rdev
->bdev
, refdev
->bdev
);
1267 ev2
= md_event(refsb
);
1269 if (!spare_disk
&& ev1
> ev2
)
1274 rdev
->sectors
= rdev
->sb_start
;
1275 /* Limit to 4TB as metadata cannot record more than that.
1276 * (not needed for Linear and RAID0 as metadata doesn't
1279 if ((u64
)rdev
->sectors
>= (2ULL << 32) && sb
->level
>= 1)
1280 rdev
->sectors
= (sector_t
)(2ULL << 32) - 2;
1282 if (rdev
->sectors
< ((sector_t
)sb
->size
) * 2 && sb
->level
>= 1)
1283 /* "this cannot possibly happen" ... */
1290 static u64
md_bitmap_events_cleared(struct mddev
*mddev
)
1292 struct md_bitmap_stats stats
;
1295 err
= mddev
->bitmap_ops
->get_stats(mddev
->bitmap
, &stats
);
1299 return stats
.events_cleared
;
1303 * validate_super for 0.90.0
1304 * note: we are not using "freshest" for 0.9 superblock
1306 static int super_90_validate(struct mddev
*mddev
, struct md_rdev
*freshest
, struct md_rdev
*rdev
)
1309 mdp_super_t
*sb
= page_address(rdev
->sb_page
);
1310 __u64 ev1
= md_event(sb
);
1312 rdev
->raid_disk
= -1;
1313 clear_bit(Faulty
, &rdev
->flags
);
1314 clear_bit(In_sync
, &rdev
->flags
);
1315 clear_bit(Bitmap_sync
, &rdev
->flags
);
1316 clear_bit(WriteMostly
, &rdev
->flags
);
1318 if (mddev
->raid_disks
== 0) {
1319 mddev
->major_version
= 0;
1320 mddev
->minor_version
= sb
->minor_version
;
1321 mddev
->patch_version
= sb
->patch_version
;
1322 mddev
->external
= 0;
1323 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
1324 mddev
->ctime
= sb
->ctime
;
1325 mddev
->utime
= sb
->utime
;
1326 mddev
->level
= sb
->level
;
1327 mddev
->clevel
[0] = 0;
1328 mddev
->layout
= sb
->layout
;
1329 mddev
->raid_disks
= sb
->raid_disks
;
1330 mddev
->dev_sectors
= ((sector_t
)sb
->size
) * 2;
1331 mddev
->events
= ev1
;
1332 mddev
->bitmap_info
.offset
= 0;
1333 mddev
->bitmap_info
.space
= 0;
1334 /* bitmap can use 60 K after the 4K superblocks */
1335 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
1336 mddev
->bitmap_info
.default_space
= 64*2 - (MD_SB_BYTES
>> 9);
1337 mddev
->reshape_backwards
= 0;
1339 if (mddev
->minor_version
>= 91) {
1340 mddev
->reshape_position
= sb
->reshape_position
;
1341 mddev
->delta_disks
= sb
->delta_disks
;
1342 mddev
->new_level
= sb
->new_level
;
1343 mddev
->new_layout
= sb
->new_layout
;
1344 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
1345 if (mddev
->delta_disks
< 0)
1346 mddev
->reshape_backwards
= 1;
1348 mddev
->reshape_position
= MaxSector
;
1349 mddev
->delta_disks
= 0;
1350 mddev
->new_level
= mddev
->level
;
1351 mddev
->new_layout
= mddev
->layout
;
1352 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1354 if (mddev
->level
== 0)
1357 if (sb
->state
& (1<<MD_SB_CLEAN
))
1358 mddev
->recovery_cp
= MaxSector
;
1360 if (sb
->events_hi
== sb
->cp_events_hi
&&
1361 sb
->events_lo
== sb
->cp_events_lo
) {
1362 mddev
->recovery_cp
= sb
->recovery_cp
;
1364 mddev
->recovery_cp
= 0;
1367 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
1368 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
1369 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
1370 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
1372 mddev
->max_disks
= MD_SB_DISKS
;
1374 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
1375 mddev
->bitmap_info
.file
== NULL
) {
1376 mddev
->bitmap_info
.offset
=
1377 mddev
->bitmap_info
.default_offset
;
1378 mddev
->bitmap_info
.space
=
1379 mddev
->bitmap_info
.default_space
;
1382 } else if (mddev
->pers
== NULL
) {
1383 /* Insist on good event counter while assembling, except
1384 * for spares (which don't need an event count) */
1386 if (sb
->disks
[rdev
->desc_nr
].state
& (
1387 (1<<MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
)))
1388 if (ev1
< mddev
->events
)
1390 } else if (mddev
->bitmap
) {
1391 /* if adding to array with a bitmap, then we can accept an
1392 * older device ... but not too old.
1394 if (ev1
< md_bitmap_events_cleared(mddev
))
1396 if (ev1
< mddev
->events
)
1397 set_bit(Bitmap_sync
, &rdev
->flags
);
1399 if (ev1
< mddev
->events
)
1400 /* just a hot-add of a new device, leave raid_disk at -1 */
1404 desc
= sb
->disks
+ rdev
->desc_nr
;
1406 if (desc
->state
& (1<<MD_DISK_FAULTY
))
1407 set_bit(Faulty
, &rdev
->flags
);
1408 else if (desc
->state
& (1<<MD_DISK_SYNC
)) {
1409 set_bit(In_sync
, &rdev
->flags
);
1410 rdev
->raid_disk
= desc
->raid_disk
;
1411 rdev
->saved_raid_disk
= desc
->raid_disk
;
1412 } else if (desc
->state
& (1<<MD_DISK_ACTIVE
)) {
1413 /* active but not in sync implies recovery up to
1414 * reshape position. We don't know exactly where
1415 * that is, so set to zero for now
1417 if (mddev
->minor_version
>= 91) {
1418 rdev
->recovery_offset
= 0;
1419 rdev
->raid_disk
= desc
->raid_disk
;
1422 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
1423 set_bit(WriteMostly
, &rdev
->flags
);
1424 if (desc
->state
& (1<<MD_DISK_FAILFAST
))
1425 set_bit(FailFast
, &rdev
->flags
);
1430 * sync_super for 0.90.0
1432 static void super_90_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
1435 struct md_rdev
*rdev2
;
1436 int next_spare
= mddev
->raid_disks
;
1438 /* make rdev->sb match mddev data..
1441 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1442 * 3/ any empty disks < next_spare become removed
1444 * disks[0] gets initialised to REMOVED because
1445 * we cannot be sure from other fields if it has
1446 * been initialised or not.
1449 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
1451 rdev
->sb_size
= MD_SB_BYTES
;
1453 sb
= page_address(rdev
->sb_page
);
1455 memset(sb
, 0, sizeof(*sb
));
1457 sb
->md_magic
= MD_SB_MAGIC
;
1458 sb
->major_version
= mddev
->major_version
;
1459 sb
->patch_version
= mddev
->patch_version
;
1460 sb
->gvalid_words
= 0; /* ignored */
1461 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
1462 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
1463 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
1464 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
1466 sb
->ctime
= clamp_t(time64_t
, mddev
->ctime
, 0, U32_MAX
);
1467 sb
->level
= mddev
->level
;
1468 sb
->size
= mddev
->dev_sectors
/ 2;
1469 sb
->raid_disks
= mddev
->raid_disks
;
1470 sb
->md_minor
= mddev
->md_minor
;
1471 sb
->not_persistent
= 0;
1472 sb
->utime
= clamp_t(time64_t
, mddev
->utime
, 0, U32_MAX
);
1474 sb
->events_hi
= (mddev
->events
>>32);
1475 sb
->events_lo
= (u32
)mddev
->events
;
1477 if (mddev
->reshape_position
== MaxSector
)
1478 sb
->minor_version
= 90;
1480 sb
->minor_version
= 91;
1481 sb
->reshape_position
= mddev
->reshape_position
;
1482 sb
->new_level
= mddev
->new_level
;
1483 sb
->delta_disks
= mddev
->delta_disks
;
1484 sb
->new_layout
= mddev
->new_layout
;
1485 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1487 mddev
->minor_version
= sb
->minor_version
;
1490 sb
->recovery_cp
= mddev
->recovery_cp
;
1491 sb
->cp_events_hi
= (mddev
->events
>>32);
1492 sb
->cp_events_lo
= (u32
)mddev
->events
;
1493 if (mddev
->recovery_cp
== MaxSector
)
1494 sb
->state
= (1<< MD_SB_CLEAN
);
1496 sb
->recovery_cp
= 0;
1498 sb
->layout
= mddev
->layout
;
1499 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1501 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
)
1502 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1504 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1505 rdev_for_each(rdev2
, mddev
) {
1508 int is_active
= test_bit(In_sync
, &rdev2
->flags
);
1510 if (rdev2
->raid_disk
>= 0 &&
1511 sb
->minor_version
>= 91)
1512 /* we have nowhere to store the recovery_offset,
1513 * but if it is not below the reshape_position,
1514 * we can piggy-back on that.
1517 if (rdev2
->raid_disk
< 0 ||
1518 test_bit(Faulty
, &rdev2
->flags
))
1521 desc_nr
= rdev2
->raid_disk
;
1523 desc_nr
= next_spare
++;
1524 rdev2
->desc_nr
= desc_nr
;
1525 d
= &sb
->disks
[rdev2
->desc_nr
];
1527 d
->number
= rdev2
->desc_nr
;
1528 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1529 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1531 d
->raid_disk
= rdev2
->raid_disk
;
1533 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1534 if (test_bit(Faulty
, &rdev2
->flags
))
1535 d
->state
= (1<<MD_DISK_FAULTY
);
1536 else if (is_active
) {
1537 d
->state
= (1<<MD_DISK_ACTIVE
);
1538 if (test_bit(In_sync
, &rdev2
->flags
))
1539 d
->state
|= (1<<MD_DISK_SYNC
);
1547 if (test_bit(WriteMostly
, &rdev2
->flags
))
1548 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1549 if (test_bit(FailFast
, &rdev2
->flags
))
1550 d
->state
|= (1<<MD_DISK_FAILFAST
);
1552 /* now set the "removed" and "faulty" bits on any missing devices */
1553 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1554 mdp_disk_t
*d
= &sb
->disks
[i
];
1555 if (d
->state
== 0 && d
->number
== 0) {
1558 d
->state
= (1<<MD_DISK_REMOVED
);
1559 d
->state
|= (1<<MD_DISK_FAULTY
);
1563 sb
->nr_disks
= nr_disks
;
1564 sb
->active_disks
= active
;
1565 sb
->working_disks
= working
;
1566 sb
->failed_disks
= failed
;
1567 sb
->spare_disks
= spare
;
1569 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1570 sb
->sb_csum
= calc_sb_csum(sb
);
1574 * rdev_size_change for 0.90.0
1576 static unsigned long long
1577 super_90_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
1579 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1580 return 0; /* component must fit device */
1581 if (rdev
->mddev
->bitmap_info
.offset
)
1582 return 0; /* can't move bitmap */
1583 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1584 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1585 num_sectors
= rdev
->sb_start
;
1586 /* Limit to 4TB as metadata cannot record more than that.
1587 * 4TB == 2^32 KB, or 2*2^32 sectors.
1589 if ((u64
)num_sectors
>= (2ULL << 32) && rdev
->mddev
->level
>= 1)
1590 num_sectors
= (sector_t
)(2ULL << 32) - 2;
1592 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1594 } while (md_super_wait(rdev
->mddev
) < 0);
1599 super_90_allow_new_offset(struct md_rdev
*rdev
, unsigned long long new_offset
)
1601 /* non-zero offset changes not possible with v0.90 */
1602 return new_offset
== 0;
1606 * version 1 superblock
1609 static __le32
calc_sb_1_csum(struct mdp_superblock_1
*sb
)
1613 unsigned long long newcsum
;
1614 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1615 __le32
*isuper
= (__le32
*)sb
;
1617 disk_csum
= sb
->sb_csum
;
1620 for (; size
>= 4; size
-= 4)
1621 newcsum
+= le32_to_cpu(*isuper
++);
1624 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1626 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1627 sb
->sb_csum
= disk_csum
;
1628 return cpu_to_le32(csum
);
1631 static int super_1_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
1633 struct mdp_superblock_1
*sb
;
1638 bool spare_disk
= true;
1641 * Calculate the position of the superblock in 512byte sectors.
1642 * It is always aligned to a 4K boundary and
1643 * depeding on minor_version, it can be:
1644 * 0: At least 8K, but less than 12K, from end of device
1645 * 1: At start of device
1646 * 2: 4K from start of device.
1648 switch(minor_version
) {
1650 sb_start
= bdev_nr_sectors(rdev
->bdev
) - 8 * 2;
1651 sb_start
&= ~(sector_t
)(4*2-1);
1662 rdev
->sb_start
= sb_start
;
1664 /* superblock is rarely larger than 1K, but it can be larger,
1665 * and it is safe to read 4k, so we do that
1667 ret
= read_disk_sb(rdev
, 4096);
1668 if (ret
) return ret
;
1670 sb
= page_address(rdev
->sb_page
);
1672 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1673 sb
->major_version
!= cpu_to_le32(1) ||
1674 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1675 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1676 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1679 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1680 pr_warn("md: invalid superblock checksum on %pg\n",
1684 if (le64_to_cpu(sb
->data_size
) < 10) {
1685 pr_warn("md: data_size too small on %pg\n",
1691 memcmp(sb
->pad3
, sb
->pad3
+1, sizeof(sb
->pad3
) - sizeof(sb
->pad3
[1])))
1692 /* Some padding is non-zero, might be a new feature */
1695 rdev
->preferred_minor
= 0xffff;
1696 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1697 rdev
->new_data_offset
= rdev
->data_offset
;
1698 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
) &&
1699 (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_NEW_OFFSET
))
1700 rdev
->new_data_offset
+= (s32
)le32_to_cpu(sb
->new_offset
);
1701 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1703 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1704 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1705 if (rdev
->sb_size
& bmask
)
1706 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1709 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1712 && rdev
->new_data_offset
< sb_start
+ (rdev
->sb_size
/512))
1715 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1717 if (!rdev
->bb_page
) {
1718 rdev
->bb_page
= alloc_page(GFP_KERNEL
);
1722 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BAD_BLOCKS
) &&
1723 rdev
->badblocks
.count
== 0) {
1724 /* need to load the bad block list.
1725 * Currently we limit it to one page.
1731 int sectors
= le16_to_cpu(sb
->bblog_size
);
1732 if (sectors
> (PAGE_SIZE
/ 512))
1734 offset
= le32_to_cpu(sb
->bblog_offset
);
1737 bb_sector
= (long long)offset
;
1738 if (!sync_page_io(rdev
, bb_sector
, sectors
<< 9,
1739 rdev
->bb_page
, REQ_OP_READ
, true))
1741 bbp
= (__le64
*)page_address(rdev
->bb_page
);
1742 rdev
->badblocks
.shift
= sb
->bblog_shift
;
1743 for (i
= 0 ; i
< (sectors
<< (9-3)) ; i
++, bbp
++) {
1744 u64 bb
= le64_to_cpu(*bbp
);
1745 int count
= bb
& (0x3ff);
1746 u64 sector
= bb
>> 10;
1747 sector
<<= sb
->bblog_shift
;
1748 count
<<= sb
->bblog_shift
;
1751 if (badblocks_set(&rdev
->badblocks
, sector
, count
, 1))
1754 } else if (sb
->bblog_offset
!= 0)
1755 rdev
->badblocks
.shift
= 0;
1757 if ((le32_to_cpu(sb
->feature_map
) &
1758 (MD_FEATURE_PPL
| MD_FEATURE_MULTIPLE_PPLS
))) {
1759 rdev
->ppl
.offset
= (__s16
)le16_to_cpu(sb
->ppl
.offset
);
1760 rdev
->ppl
.size
= le16_to_cpu(sb
->ppl
.size
);
1761 rdev
->ppl
.sector
= rdev
->sb_start
+ rdev
->ppl
.offset
;
1764 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RAID0_LAYOUT
) &&
1768 /* not spare disk */
1769 if (rdev
->desc_nr
>= 0 && rdev
->desc_nr
< le32_to_cpu(sb
->max_dev
) &&
1770 (le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) < MD_DISK_ROLE_MAX
||
1771 le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) == MD_DISK_ROLE_JOURNAL
))
1781 struct mdp_superblock_1
*refsb
= page_address(refdev
->sb_page
);
1783 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1784 sb
->level
!= refsb
->level
||
1785 sb
->layout
!= refsb
->layout
||
1786 sb
->chunksize
!= refsb
->chunksize
) {
1787 pr_warn("md: %pg has strangely different superblock to %pg\n",
1792 ev1
= le64_to_cpu(sb
->events
);
1793 ev2
= le64_to_cpu(refsb
->events
);
1795 if (!spare_disk
&& ev1
> ev2
)
1801 sectors
= bdev_nr_sectors(rdev
->bdev
) - rdev
->data_offset
;
1803 sectors
= rdev
->sb_start
;
1804 if (sectors
< le64_to_cpu(sb
->data_size
))
1806 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1810 static int super_1_validate(struct mddev
*mddev
, struct md_rdev
*freshest
, struct md_rdev
*rdev
)
1812 struct mdp_superblock_1
*sb
= page_address(rdev
->sb_page
);
1813 __u64 ev1
= le64_to_cpu(sb
->events
);
1816 rdev
->raid_disk
= -1;
1817 clear_bit(Faulty
, &rdev
->flags
);
1818 clear_bit(In_sync
, &rdev
->flags
);
1819 clear_bit(Bitmap_sync
, &rdev
->flags
);
1820 clear_bit(WriteMostly
, &rdev
->flags
);
1822 if (mddev
->raid_disks
== 0) {
1823 mddev
->major_version
= 1;
1824 mddev
->patch_version
= 0;
1825 mddev
->external
= 0;
1826 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1827 mddev
->ctime
= le64_to_cpu(sb
->ctime
);
1828 mddev
->utime
= le64_to_cpu(sb
->utime
);
1829 mddev
->level
= le32_to_cpu(sb
->level
);
1830 mddev
->clevel
[0] = 0;
1831 mddev
->layout
= le32_to_cpu(sb
->layout
);
1832 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1833 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1834 mddev
->events
= ev1
;
1835 mddev
->bitmap_info
.offset
= 0;
1836 mddev
->bitmap_info
.space
= 0;
1837 /* Default location for bitmap is 1K after superblock
1838 * using 3K - total of 4K
1840 mddev
->bitmap_info
.default_offset
= 1024 >> 9;
1841 mddev
->bitmap_info
.default_space
= (4096-1024) >> 9;
1842 mddev
->reshape_backwards
= 0;
1844 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1845 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1847 mddev
->max_disks
= (4096-256)/2;
1849 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1850 mddev
->bitmap_info
.file
== NULL
) {
1851 mddev
->bitmap_info
.offset
=
1852 (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1853 /* Metadata doesn't record how much space is available.
1854 * For 1.0, we assume we can use up to the superblock
1855 * if before, else to 4K beyond superblock.
1856 * For others, assume no change is possible.
1858 if (mddev
->minor_version
> 0)
1859 mddev
->bitmap_info
.space
= 0;
1860 else if (mddev
->bitmap_info
.offset
> 0)
1861 mddev
->bitmap_info
.space
=
1862 8 - mddev
->bitmap_info
.offset
;
1864 mddev
->bitmap_info
.space
=
1865 -mddev
->bitmap_info
.offset
;
1868 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1869 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1870 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1871 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1872 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1873 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1874 if (mddev
->delta_disks
< 0 ||
1875 (mddev
->delta_disks
== 0 &&
1876 (le32_to_cpu(sb
->feature_map
)
1877 & MD_FEATURE_RESHAPE_BACKWARDS
)))
1878 mddev
->reshape_backwards
= 1;
1880 mddev
->reshape_position
= MaxSector
;
1881 mddev
->delta_disks
= 0;
1882 mddev
->new_level
= mddev
->level
;
1883 mddev
->new_layout
= mddev
->layout
;
1884 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1887 if (mddev
->level
== 0 &&
1888 !(le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RAID0_LAYOUT
))
1891 if (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_JOURNAL
)
1892 set_bit(MD_HAS_JOURNAL
, &mddev
->flags
);
1894 if (le32_to_cpu(sb
->feature_map
) &
1895 (MD_FEATURE_PPL
| MD_FEATURE_MULTIPLE_PPLS
)) {
1896 if (le32_to_cpu(sb
->feature_map
) &
1897 (MD_FEATURE_BITMAP_OFFSET
| MD_FEATURE_JOURNAL
))
1899 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_PPL
) &&
1900 (le32_to_cpu(sb
->feature_map
) &
1901 MD_FEATURE_MULTIPLE_PPLS
))
1903 set_bit(MD_HAS_PPL
, &mddev
->flags
);
1905 } else if (mddev
->pers
== NULL
) {
1906 /* Insist of good event counter while assembling, except for
1907 * spares (which don't need an event count).
1908 * Similar to mdadm, we allow event counter difference of 1
1909 * from the freshest device.
1911 if (rdev
->desc_nr
>= 0 &&
1912 rdev
->desc_nr
< le32_to_cpu(sb
->max_dev
) &&
1913 (le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) < MD_DISK_ROLE_MAX
||
1914 le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) == MD_DISK_ROLE_JOURNAL
))
1915 if (ev1
+ 1 < mddev
->events
)
1917 } else if (mddev
->bitmap
) {
1918 /* If adding to array with a bitmap, then we can accept an
1919 * older device, but not too old.
1921 if (ev1
< md_bitmap_events_cleared(mddev
))
1923 if (ev1
< mddev
->events
)
1924 set_bit(Bitmap_sync
, &rdev
->flags
);
1926 if (ev1
< mddev
->events
)
1927 /* just a hot-add of a new device, leave raid_disk at -1 */
1931 if (rdev
->desc_nr
< 0 ||
1932 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1933 role
= MD_DISK_ROLE_SPARE
;
1935 } else if (mddev
->pers
== NULL
&& freshest
&& ev1
< mddev
->events
) {
1937 * If we are assembling, and our event counter is smaller than the
1938 * highest event counter, we cannot trust our superblock about the role.
1939 * It could happen that our rdev was marked as Faulty, and all other
1940 * superblocks were updated with +1 event counter.
1941 * Then, before the next superblock update, which typically happens when
1942 * remove_and_add_spares() removes the device from the array, there was
1943 * a crash or reboot.
1944 * If we allow current rdev without consulting the freshest superblock,
1945 * we could cause data corruption.
1946 * Note that in this case our event counter is smaller by 1 than the
1947 * highest, otherwise, this rdev would not be allowed into array;
1948 * both kernel and mdadm allow event counter difference of 1.
1950 struct mdp_superblock_1
*freshest_sb
= page_address(freshest
->sb_page
);
1951 u32 freshest_max_dev
= le32_to_cpu(freshest_sb
->max_dev
);
1953 if (rdev
->desc_nr
>= freshest_max_dev
) {
1954 /* this is unexpected, better not proceed */
1955 pr_warn("md: %s: rdev[%pg]: desc_nr(%d) >= freshest(%pg)->sb->max_dev(%u)\n",
1956 mdname(mddev
), rdev
->bdev
, rdev
->desc_nr
,
1957 freshest
->bdev
, freshest_max_dev
);
1961 role
= le16_to_cpu(freshest_sb
->dev_roles
[rdev
->desc_nr
]);
1962 pr_debug("md: %s: rdev[%pg]: role=%d(0x%x) according to freshest %pg\n",
1963 mdname(mddev
), rdev
->bdev
, role
, role
, freshest
->bdev
);
1965 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1968 case MD_DISK_ROLE_SPARE
: /* spare */
1970 case MD_DISK_ROLE_FAULTY
: /* faulty */
1971 set_bit(Faulty
, &rdev
->flags
);
1973 case MD_DISK_ROLE_JOURNAL
: /* journal device */
1974 if (!(le32_to_cpu(sb
->feature_map
) & MD_FEATURE_JOURNAL
)) {
1975 /* journal device without journal feature */
1976 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
1979 set_bit(Journal
, &rdev
->flags
);
1980 rdev
->journal_tail
= le64_to_cpu(sb
->journal_tail
);
1981 rdev
->raid_disk
= 0;
1984 rdev
->saved_raid_disk
= role
;
1985 if ((le32_to_cpu(sb
->feature_map
) &
1986 MD_FEATURE_RECOVERY_OFFSET
)) {
1987 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1988 if (!(le32_to_cpu(sb
->feature_map
) &
1989 MD_FEATURE_RECOVERY_BITMAP
))
1990 rdev
->saved_raid_disk
= -1;
1993 * If the array is FROZEN, then the device can't
1994 * be in_sync with rest of array.
1996 if (!test_bit(MD_RECOVERY_FROZEN
,
1998 set_bit(In_sync
, &rdev
->flags
);
2000 rdev
->raid_disk
= role
;
2003 if (sb
->devflags
& WriteMostly1
)
2004 set_bit(WriteMostly
, &rdev
->flags
);
2005 if (sb
->devflags
& FailFast1
)
2006 set_bit(FailFast
, &rdev
->flags
);
2007 if (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_REPLACEMENT
)
2008 set_bit(Replacement
, &rdev
->flags
);
2013 static void super_1_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
2015 struct mdp_superblock_1
*sb
;
2016 struct md_rdev
*rdev2
;
2018 /* make rdev->sb match mddev and rdev data. */
2020 sb
= page_address(rdev
->sb_page
);
2022 sb
->feature_map
= 0;
2024 sb
->recovery_offset
= cpu_to_le64(0);
2025 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
2027 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
2028 sb
->events
= cpu_to_le64(mddev
->events
);
2030 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
2031 else if (test_bit(MD_JOURNAL_CLEAN
, &mddev
->flags
))
2032 sb
->resync_offset
= cpu_to_le64(MaxSector
);
2034 sb
->resync_offset
= cpu_to_le64(0);
2036 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
2038 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
2039 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
2040 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
2041 sb
->level
= cpu_to_le32(mddev
->level
);
2042 sb
->layout
= cpu_to_le32(mddev
->layout
);
2043 if (test_bit(FailFast
, &rdev
->flags
))
2044 sb
->devflags
|= FailFast1
;
2046 sb
->devflags
&= ~FailFast1
;
2048 if (test_bit(WriteMostly
, &rdev
->flags
))
2049 sb
->devflags
|= WriteMostly1
;
2051 sb
->devflags
&= ~WriteMostly1
;
2052 sb
->data_offset
= cpu_to_le64(rdev
->data_offset
);
2053 sb
->data_size
= cpu_to_le64(rdev
->sectors
);
2055 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
) {
2056 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_info
.offset
);
2057 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
2060 if (rdev
->raid_disk
>= 0 && !test_bit(Journal
, &rdev
->flags
) &&
2061 !test_bit(In_sync
, &rdev
->flags
)) {
2063 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
2064 sb
->recovery_offset
=
2065 cpu_to_le64(rdev
->recovery_offset
);
2066 if (rdev
->saved_raid_disk
>= 0 && mddev
->bitmap
)
2068 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP
);
2070 /* Note: recovery_offset and journal_tail share space */
2071 if (test_bit(Journal
, &rdev
->flags
))
2072 sb
->journal_tail
= cpu_to_le64(rdev
->journal_tail
);
2073 if (test_bit(Replacement
, &rdev
->flags
))
2075 cpu_to_le32(MD_FEATURE_REPLACEMENT
);
2077 if (mddev
->reshape_position
!= MaxSector
) {
2078 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
2079 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
2080 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
2081 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
2082 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
2083 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
2084 if (mddev
->delta_disks
== 0 &&
2085 mddev
->reshape_backwards
)
2087 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS
);
2088 if (rdev
->new_data_offset
!= rdev
->data_offset
) {
2090 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET
);
2091 sb
->new_offset
= cpu_to_le32((__u32
)(rdev
->new_data_offset
2092 - rdev
->data_offset
));
2096 if (mddev_is_clustered(mddev
))
2097 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_CLUSTERED
);
2099 if (rdev
->badblocks
.count
== 0)
2100 /* Nothing to do for bad blocks*/ ;
2101 else if (sb
->bblog_offset
== 0)
2102 /* Cannot record bad blocks on this device */
2103 md_error(mddev
, rdev
);
2105 struct badblocks
*bb
= &rdev
->badblocks
;
2106 __le64
*bbp
= (__le64
*)page_address(rdev
->bb_page
);
2108 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_BAD_BLOCKS
);
2113 seq
= read_seqbegin(&bb
->lock
);
2115 memset(bbp
, 0xff, PAGE_SIZE
);
2117 for (i
= 0 ; i
< bb
->count
; i
++) {
2118 u64 internal_bb
= p
[i
];
2119 u64 store_bb
= ((BB_OFFSET(internal_bb
) << 10)
2120 | BB_LEN(internal_bb
));
2121 bbp
[i
] = cpu_to_le64(store_bb
);
2124 if (read_seqretry(&bb
->lock
, seq
))
2127 bb
->sector
= (rdev
->sb_start
+
2128 (int)le32_to_cpu(sb
->bblog_offset
));
2129 bb
->size
= le16_to_cpu(sb
->bblog_size
);
2134 rdev_for_each(rdev2
, mddev
)
2135 if (rdev2
->desc_nr
+1 > max_dev
)
2136 max_dev
= rdev2
->desc_nr
+1;
2138 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
2140 sb
->max_dev
= cpu_to_le32(max_dev
);
2141 rdev
->sb_size
= max_dev
* 2 + 256;
2142 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
2143 if (rdev
->sb_size
& bmask
)
2144 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
2146 max_dev
= le32_to_cpu(sb
->max_dev
);
2148 for (i
=0; i
<max_dev
;i
++)
2149 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_SPARE
);
2151 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
))
2152 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_JOURNAL
);
2154 if (test_bit(MD_HAS_PPL
, &mddev
->flags
)) {
2155 if (test_bit(MD_HAS_MULTIPLE_PPLS
, &mddev
->flags
))
2157 cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS
);
2159 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_PPL
);
2160 sb
->ppl
.offset
= cpu_to_le16(rdev
->ppl
.offset
);
2161 sb
->ppl
.size
= cpu_to_le16(rdev
->ppl
.size
);
2164 rdev_for_each(rdev2
, mddev
) {
2166 if (test_bit(Faulty
, &rdev2
->flags
))
2167 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_FAULTY
);
2168 else if (test_bit(In_sync
, &rdev2
->flags
))
2169 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
2170 else if (test_bit(Journal
, &rdev2
->flags
))
2171 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_JOURNAL
);
2172 else if (rdev2
->raid_disk
>= 0)
2173 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
2175 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_SPARE
);
2178 sb
->sb_csum
= calc_sb_1_csum(sb
);
2181 static sector_t
super_1_choose_bm_space(sector_t dev_size
)
2185 /* if the device is bigger than 8Gig, save 64k for bitmap
2186 * usage, if bigger than 200Gig, save 128k
2188 if (dev_size
< 64*2)
2190 else if (dev_size
- 64*2 >= 200*1024*1024*2)
2192 else if (dev_size
- 4*2 > 8*1024*1024*2)
2199 static unsigned long long
2200 super_1_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
2202 struct mdp_superblock_1
*sb
;
2203 sector_t max_sectors
;
2204 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
2205 return 0; /* component must fit device */
2206 if (rdev
->data_offset
!= rdev
->new_data_offset
)
2207 return 0; /* too confusing */
2208 if (rdev
->sb_start
< rdev
->data_offset
) {
2209 /* minor versions 1 and 2; superblock before data */
2210 max_sectors
= bdev_nr_sectors(rdev
->bdev
) - rdev
->data_offset
;
2211 if (!num_sectors
|| num_sectors
> max_sectors
)
2212 num_sectors
= max_sectors
;
2213 } else if (rdev
->mddev
->bitmap_info
.offset
) {
2214 /* minor version 0 with bitmap we can't move */
2217 /* minor version 0; superblock after data */
2218 sector_t sb_start
, bm_space
;
2219 sector_t dev_size
= bdev_nr_sectors(rdev
->bdev
);
2221 /* 8K is for superblock */
2222 sb_start
= dev_size
- 8*2;
2223 sb_start
&= ~(sector_t
)(4*2 - 1);
2225 bm_space
= super_1_choose_bm_space(dev_size
);
2227 /* Space that can be used to store date needs to decrease
2228 * superblock bitmap space and bad block space(4K)
2230 max_sectors
= sb_start
- bm_space
- 4*2;
2232 if (!num_sectors
|| num_sectors
> max_sectors
)
2233 num_sectors
= max_sectors
;
2234 rdev
->sb_start
= sb_start
;
2236 sb
= page_address(rdev
->sb_page
);
2237 sb
->data_size
= cpu_to_le64(num_sectors
);
2238 sb
->super_offset
= cpu_to_le64(rdev
->sb_start
);
2239 sb
->sb_csum
= calc_sb_1_csum(sb
);
2241 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
2243 } while (md_super_wait(rdev
->mddev
) < 0);
2249 super_1_allow_new_offset(struct md_rdev
*rdev
,
2250 unsigned long long new_offset
)
2252 /* All necessary checks on new >= old have been done */
2253 if (new_offset
>= rdev
->data_offset
)
2256 /* with 1.0 metadata, there is no metadata to tread on
2257 * so we can always move back */
2258 if (rdev
->mddev
->minor_version
== 0)
2261 /* otherwise we must be sure not to step on
2262 * any metadata, so stay:
2263 * 36K beyond start of superblock
2264 * beyond end of badblocks
2265 * beyond write-intent bitmap
2267 if (rdev
->sb_start
+ (32+4)*2 > new_offset
)
2270 if (!rdev
->mddev
->bitmap_info
.file
) {
2271 struct mddev
*mddev
= rdev
->mddev
;
2272 struct md_bitmap_stats stats
;
2275 err
= mddev
->bitmap_ops
->get_stats(mddev
->bitmap
, &stats
);
2276 if (!err
&& rdev
->sb_start
+ mddev
->bitmap_info
.offset
+
2277 stats
.file_pages
* (PAGE_SIZE
>> 9) > new_offset
)
2281 if (rdev
->badblocks
.sector
+ rdev
->badblocks
.size
> new_offset
)
2287 static struct super_type super_types
[] = {
2290 .owner
= THIS_MODULE
,
2291 .load_super
= super_90_load
,
2292 .validate_super
= super_90_validate
,
2293 .sync_super
= super_90_sync
,
2294 .rdev_size_change
= super_90_rdev_size_change
,
2295 .allow_new_offset
= super_90_allow_new_offset
,
2299 .owner
= THIS_MODULE
,
2300 .load_super
= super_1_load
,
2301 .validate_super
= super_1_validate
,
2302 .sync_super
= super_1_sync
,
2303 .rdev_size_change
= super_1_rdev_size_change
,
2304 .allow_new_offset
= super_1_allow_new_offset
,
2308 static void sync_super(struct mddev
*mddev
, struct md_rdev
*rdev
)
2310 if (mddev
->sync_super
) {
2311 mddev
->sync_super(mddev
, rdev
);
2315 BUG_ON(mddev
->major_version
>= ARRAY_SIZE(super_types
));
2317 super_types
[mddev
->major_version
].sync_super(mddev
, rdev
);
2320 static int match_mddev_units(struct mddev
*mddev1
, struct mddev
*mddev2
)
2322 struct md_rdev
*rdev
, *rdev2
;
2325 rdev_for_each_rcu(rdev
, mddev1
) {
2326 if (test_bit(Faulty
, &rdev
->flags
) ||
2327 test_bit(Journal
, &rdev
->flags
) ||
2328 rdev
->raid_disk
== -1)
2330 rdev_for_each_rcu(rdev2
, mddev2
) {
2331 if (test_bit(Faulty
, &rdev2
->flags
) ||
2332 test_bit(Journal
, &rdev2
->flags
) ||
2333 rdev2
->raid_disk
== -1)
2335 if (rdev
->bdev
->bd_disk
== rdev2
->bdev
->bd_disk
) {
2345 static LIST_HEAD(pending_raid_disks
);
2348 * Try to register data integrity profile for an mddev
2350 * This is called when an array is started and after a disk has been kicked
2351 * from the array. It only succeeds if all working and active component devices
2352 * are integrity capable with matching profiles.
2354 int md_integrity_register(struct mddev
*mddev
)
2356 if (list_empty(&mddev
->disks
))
2357 return 0; /* nothing to do */
2358 if (mddev_is_dm(mddev
) || !blk_get_integrity(mddev
->gendisk
))
2359 return 0; /* shouldn't register */
2361 pr_debug("md: data integrity enabled on %s\n", mdname(mddev
));
2362 if (bioset_integrity_create(&mddev
->bio_set
, BIO_POOL_SIZE
) ||
2363 (mddev
->level
!= 1 && mddev
->level
!= 10 &&
2364 bioset_integrity_create(&mddev
->io_clone_set
, BIO_POOL_SIZE
))) {
2366 * No need to handle the failure of bioset_integrity_create,
2367 * because the function is called by md_run() -> pers->run(),
2368 * md_run calls bioset_exit -> bioset_integrity_free in case
2371 pr_err("md: failed to create integrity pool for %s\n",
2377 EXPORT_SYMBOL(md_integrity_register
);
2379 static bool rdev_read_only(struct md_rdev
*rdev
)
2381 return bdev_read_only(rdev
->bdev
) ||
2382 (rdev
->meta_bdev
&& bdev_read_only(rdev
->meta_bdev
));
2385 static int bind_rdev_to_array(struct md_rdev
*rdev
, struct mddev
*mddev
)
2387 char b
[BDEVNAME_SIZE
];
2390 /* prevent duplicates */
2391 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
2394 if (rdev_read_only(rdev
) && mddev
->pers
)
2397 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2398 if (!test_bit(Journal
, &rdev
->flags
) &&
2400 (mddev
->dev_sectors
== 0 || rdev
->sectors
< mddev
->dev_sectors
)) {
2402 /* Cannot change size, so fail
2403 * If mddev->level <= 0, then we don't care
2404 * about aligning sizes (e.g. linear)
2406 if (mddev
->level
> 0)
2409 mddev
->dev_sectors
= rdev
->sectors
;
2412 /* Verify rdev->desc_nr is unique.
2413 * If it is -1, assign a free number, else
2414 * check number is not in use
2417 if (rdev
->desc_nr
< 0) {
2420 choice
= mddev
->raid_disks
;
2421 while (md_find_rdev_nr_rcu(mddev
, choice
))
2423 rdev
->desc_nr
= choice
;
2425 if (md_find_rdev_nr_rcu(mddev
, rdev
->desc_nr
)) {
2431 if (!test_bit(Journal
, &rdev
->flags
) &&
2432 mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
2433 pr_warn("md: %s: array is limited to %d devices\n",
2434 mdname(mddev
), mddev
->max_disks
);
2437 snprintf(b
, sizeof(b
), "%pg", rdev
->bdev
);
2438 strreplace(b
, '/', '!');
2440 rdev
->mddev
= mddev
;
2441 pr_debug("md: bind<%s>\n", b
);
2443 if (mddev
->raid_disks
)
2444 mddev_create_serial_pool(mddev
, rdev
);
2446 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
2449 /* failure here is OK */
2450 err
= sysfs_create_link(&rdev
->kobj
, bdev_kobj(rdev
->bdev
), "block");
2451 rdev
->sysfs_state
= sysfs_get_dirent_safe(rdev
->kobj
.sd
, "state");
2452 rdev
->sysfs_unack_badblocks
=
2453 sysfs_get_dirent_safe(rdev
->kobj
.sd
, "unacknowledged_bad_blocks");
2454 rdev
->sysfs_badblocks
=
2455 sysfs_get_dirent_safe(rdev
->kobj
.sd
, "bad_blocks");
2457 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
2458 bd_link_disk_holder(rdev
->bdev
, mddev
->gendisk
);
2460 /* May as well allow recovery to be retried once */
2461 mddev
->recovery_disabled
++;
2466 pr_warn("md: failed to register dev-%s for %s\n",
2468 mddev_destroy_serial_pool(mddev
, rdev
);
2472 void md_autodetect_dev(dev_t dev
);
2474 /* just for claiming the bdev */
2475 static struct md_rdev claim_rdev
;
2477 static void export_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
2479 pr_debug("md: export_rdev(%pg)\n", rdev
->bdev
);
2480 md_rdev_clear(rdev
);
2482 if (test_bit(AutoDetected
, &rdev
->flags
))
2483 md_autodetect_dev(rdev
->bdev
->bd_dev
);
2485 fput(rdev
->bdev_file
);
2487 kobject_put(&rdev
->kobj
);
2490 static void md_kick_rdev_from_array(struct md_rdev
*rdev
)
2492 struct mddev
*mddev
= rdev
->mddev
;
2494 bd_unlink_disk_holder(rdev
->bdev
, rdev
->mddev
->gendisk
);
2495 list_del_rcu(&rdev
->same_set
);
2496 pr_debug("md: unbind<%pg>\n", rdev
->bdev
);
2497 mddev_destroy_serial_pool(rdev
->mddev
, rdev
);
2498 WRITE_ONCE(rdev
->mddev
, NULL
);
2499 sysfs_remove_link(&rdev
->kobj
, "block");
2500 sysfs_put(rdev
->sysfs_state
);
2501 sysfs_put(rdev
->sysfs_unack_badblocks
);
2502 sysfs_put(rdev
->sysfs_badblocks
);
2503 rdev
->sysfs_state
= NULL
;
2504 rdev
->sysfs_unack_badblocks
= NULL
;
2505 rdev
->sysfs_badblocks
= NULL
;
2506 rdev
->badblocks
.count
= 0;
2511 * kobject_del() will wait for all in progress writers to be done, where
2512 * reconfig_mutex is held, hence it can't be called under
2513 * reconfig_mutex and it's delayed to mddev_unlock().
2515 list_add(&rdev
->same_set
, &mddev
->deleting
);
2518 static void export_array(struct mddev
*mddev
)
2520 struct md_rdev
*rdev
;
2522 while (!list_empty(&mddev
->disks
)) {
2523 rdev
= list_first_entry(&mddev
->disks
, struct md_rdev
,
2525 md_kick_rdev_from_array(rdev
);
2527 mddev
->raid_disks
= 0;
2528 mddev
->major_version
= 0;
2531 static bool set_in_sync(struct mddev
*mddev
)
2533 lockdep_assert_held(&mddev
->lock
);
2534 if (!mddev
->in_sync
) {
2535 mddev
->sync_checkers
++;
2536 spin_unlock(&mddev
->lock
);
2537 percpu_ref_switch_to_atomic_sync(&mddev
->writes_pending
);
2538 spin_lock(&mddev
->lock
);
2539 if (!mddev
->in_sync
&&
2540 percpu_ref_is_zero(&mddev
->writes_pending
)) {
2543 * Ensure ->in_sync is visible before we clear
2547 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
2548 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
2550 if (--mddev
->sync_checkers
== 0)
2551 percpu_ref_switch_to_percpu(&mddev
->writes_pending
);
2553 if (mddev
->safemode
== 1)
2554 mddev
->safemode
= 0;
2555 return mddev
->in_sync
;
2558 static void sync_sbs(struct mddev
*mddev
, int nospares
)
2560 /* Update each superblock (in-memory image), but
2561 * if we are allowed to, skip spares which already
2562 * have the right event counter, or have one earlier
2563 * (which would mean they aren't being marked as dirty
2564 * with the rest of the array)
2566 struct md_rdev
*rdev
;
2567 rdev_for_each(rdev
, mddev
) {
2568 if (rdev
->sb_events
== mddev
->events
||
2570 rdev
->raid_disk
< 0 &&
2571 rdev
->sb_events
+1 == mddev
->events
)) {
2572 /* Don't update this superblock */
2573 rdev
->sb_loaded
= 2;
2575 sync_super(mddev
, rdev
);
2576 rdev
->sb_loaded
= 1;
2581 static bool does_sb_need_changing(struct mddev
*mddev
)
2583 struct md_rdev
*rdev
= NULL
, *iter
;
2584 struct mdp_superblock_1
*sb
;
2587 /* Find a good rdev */
2588 rdev_for_each(iter
, mddev
)
2589 if ((iter
->raid_disk
>= 0) && !test_bit(Faulty
, &iter
->flags
)) {
2594 /* No good device found. */
2598 sb
= page_address(rdev
->sb_page
);
2599 /* Check if a device has become faulty or a spare become active */
2600 rdev_for_each(rdev
, mddev
) {
2601 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
2602 /* Device activated? */
2603 if (role
== MD_DISK_ROLE_SPARE
&& rdev
->raid_disk
>= 0 &&
2604 !test_bit(Faulty
, &rdev
->flags
))
2606 /* Device turned faulty? */
2607 if (test_bit(Faulty
, &rdev
->flags
) && (role
< MD_DISK_ROLE_MAX
))
2611 /* Check if any mddev parameters have changed */
2612 if ((mddev
->dev_sectors
!= le64_to_cpu(sb
->size
)) ||
2613 (mddev
->reshape_position
!= le64_to_cpu(sb
->reshape_position
)) ||
2614 (mddev
->layout
!= le32_to_cpu(sb
->layout
)) ||
2615 (mddev
->raid_disks
!= le32_to_cpu(sb
->raid_disks
)) ||
2616 (mddev
->chunk_sectors
!= le32_to_cpu(sb
->chunksize
)))
2622 void md_update_sb(struct mddev
*mddev
, int force_change
)
2624 struct md_rdev
*rdev
;
2627 int any_badblocks_changed
= 0;
2630 if (!md_is_rdwr(mddev
)) {
2632 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2637 if (mddev_is_clustered(mddev
)) {
2638 if (test_and_clear_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
))
2640 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
))
2642 ret
= md_cluster_ops
->metadata_update_start(mddev
);
2643 /* Has someone else has updated the sb */
2644 if (!does_sb_need_changing(mddev
)) {
2646 md_cluster_ops
->metadata_update_cancel(mddev
);
2647 bit_clear_unless(&mddev
->sb_flags
, BIT(MD_SB_CHANGE_PENDING
),
2648 BIT(MD_SB_CHANGE_DEVS
) |
2649 BIT(MD_SB_CHANGE_CLEAN
));
2655 * First make sure individual recovery_offsets are correct
2656 * curr_resync_completed can only be used during recovery.
2657 * During reshape/resync it might use array-addresses rather
2658 * that device addresses.
2660 rdev_for_each(rdev
, mddev
) {
2661 if (rdev
->raid_disk
>= 0 &&
2662 mddev
->delta_disks
>= 0 &&
2663 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
2664 test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
) &&
2665 !test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
2666 !test_bit(Journal
, &rdev
->flags
) &&
2667 !test_bit(In_sync
, &rdev
->flags
) &&
2668 mddev
->curr_resync_completed
> rdev
->recovery_offset
)
2669 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
2672 if (!mddev
->persistent
) {
2673 clear_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
2674 clear_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2675 if (!mddev
->external
) {
2676 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
2677 rdev_for_each(rdev
, mddev
) {
2678 if (rdev
->badblocks
.changed
) {
2679 rdev
->badblocks
.changed
= 0;
2680 ack_all_badblocks(&rdev
->badblocks
);
2681 md_error(mddev
, rdev
);
2683 clear_bit(Blocked
, &rdev
->flags
);
2684 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2685 wake_up(&rdev
->blocked_wait
);
2688 wake_up(&mddev
->sb_wait
);
2692 spin_lock(&mddev
->lock
);
2694 mddev
->utime
= ktime_get_real_seconds();
2696 if (test_and_clear_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
))
2698 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
))
2699 /* just a clean<-> dirty transition, possibly leave spares alone,
2700 * though if events isn't the right even/odd, we will have to do
2706 if (mddev
->degraded
)
2707 /* If the array is degraded, then skipping spares is both
2708 * dangerous and fairly pointless.
2709 * Dangerous because a device that was removed from the array
2710 * might have a event_count that still looks up-to-date,
2711 * so it can be re-added without a resync.
2712 * Pointless because if there are any spares to skip,
2713 * then a recovery will happen and soon that array won't
2714 * be degraded any more and the spare can go back to sleep then.
2718 sync_req
= mddev
->in_sync
;
2720 /* If this is just a dirty<->clean transition, and the array is clean
2721 * and 'events' is odd, we can roll back to the previous clean state */
2723 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
2724 && mddev
->can_decrease_events
2725 && mddev
->events
!= 1) {
2727 mddev
->can_decrease_events
= 0;
2729 /* otherwise we have to go forward and ... */
2731 mddev
->can_decrease_events
= nospares
;
2735 * This 64-bit counter should never wrap.
2736 * Either we are in around ~1 trillion A.C., assuming
2737 * 1 reboot per second, or we have a bug...
2739 WARN_ON(mddev
->events
== 0);
2741 rdev_for_each(rdev
, mddev
) {
2742 if (rdev
->badblocks
.changed
)
2743 any_badblocks_changed
++;
2744 if (test_bit(Faulty
, &rdev
->flags
))
2745 set_bit(FaultRecorded
, &rdev
->flags
);
2748 sync_sbs(mddev
, nospares
);
2749 spin_unlock(&mddev
->lock
);
2751 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2752 mdname(mddev
), mddev
->in_sync
);
2754 mddev_add_trace_msg(mddev
, "md md_update_sb");
2756 mddev
->bitmap_ops
->update_sb(mddev
->bitmap
);
2757 rdev_for_each(rdev
, mddev
) {
2758 if (rdev
->sb_loaded
!= 1)
2759 continue; /* no noise on spare devices */
2761 if (!test_bit(Faulty
, &rdev
->flags
)) {
2762 md_super_write(mddev
,rdev
,
2763 rdev
->sb_start
, rdev
->sb_size
,
2765 pr_debug("md: (write) %pg's sb offset: %llu\n",
2767 (unsigned long long)rdev
->sb_start
);
2768 rdev
->sb_events
= mddev
->events
;
2769 if (rdev
->badblocks
.size
) {
2770 md_super_write(mddev
, rdev
,
2771 rdev
->badblocks
.sector
,
2772 rdev
->badblocks
.size
<< 9,
2774 rdev
->badblocks
.size
= 0;
2778 pr_debug("md: %pg (skipping faulty)\n",
2781 if (md_super_wait(mddev
) < 0)
2783 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
2785 if (mddev_is_clustered(mddev
) && ret
== 0)
2786 md_cluster_ops
->metadata_update_finish(mddev
);
2788 if (mddev
->in_sync
!= sync_req
||
2789 !bit_clear_unless(&mddev
->sb_flags
, BIT(MD_SB_CHANGE_PENDING
),
2790 BIT(MD_SB_CHANGE_DEVS
) | BIT(MD_SB_CHANGE_CLEAN
)))
2791 /* have to write it out again */
2793 wake_up(&mddev
->sb_wait
);
2794 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2795 sysfs_notify_dirent_safe(mddev
->sysfs_completed
);
2797 rdev_for_each(rdev
, mddev
) {
2798 if (test_and_clear_bit(FaultRecorded
, &rdev
->flags
))
2799 clear_bit(Blocked
, &rdev
->flags
);
2801 if (any_badblocks_changed
)
2802 ack_all_badblocks(&rdev
->badblocks
);
2803 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2804 wake_up(&rdev
->blocked_wait
);
2807 EXPORT_SYMBOL(md_update_sb
);
2809 static int add_bound_rdev(struct md_rdev
*rdev
)
2811 struct mddev
*mddev
= rdev
->mddev
;
2813 bool add_journal
= test_bit(Journal
, &rdev
->flags
);
2815 if (!mddev
->pers
->hot_remove_disk
|| add_journal
) {
2816 /* If there is hot_add_disk but no hot_remove_disk
2817 * then added disks for geometry changes,
2818 * and should be added immediately.
2820 super_types
[mddev
->major_version
].
2821 validate_super(mddev
, NULL
/*freshest*/, rdev
);
2822 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
2824 md_kick_rdev_from_array(rdev
);
2828 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2830 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2831 if (mddev
->degraded
)
2832 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
2833 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2838 /* words written to sysfs files may, or may not, be \n terminated.
2839 * We want to accept with case. For this we use cmd_match.
2841 static int cmd_match(const char *cmd
, const char *str
)
2843 /* See if cmd, written into a sysfs file, matches
2844 * str. They must either be the same, or cmd can
2845 * have a trailing newline
2847 while (*cmd
&& *str
&& *cmd
== *str
) {
2858 struct rdev_sysfs_entry
{
2859 struct attribute attr
;
2860 ssize_t (*show
)(struct md_rdev
*, char *);
2861 ssize_t (*store
)(struct md_rdev
*, const char *, size_t);
2865 state_show(struct md_rdev
*rdev
, char *page
)
2869 unsigned long flags
= READ_ONCE(rdev
->flags
);
2871 if (test_bit(Faulty
, &flags
) ||
2872 (!test_bit(ExternalBbl
, &flags
) &&
2873 rdev
->badblocks
.unacked_exist
))
2874 len
+= sprintf(page
+len
, "faulty%s", sep
);
2875 if (test_bit(In_sync
, &flags
))
2876 len
+= sprintf(page
+len
, "in_sync%s", sep
);
2877 if (test_bit(Journal
, &flags
))
2878 len
+= sprintf(page
+len
, "journal%s", sep
);
2879 if (test_bit(WriteMostly
, &flags
))
2880 len
+= sprintf(page
+len
, "write_mostly%s", sep
);
2881 if (test_bit(Blocked
, &flags
) ||
2882 (rdev
->badblocks
.unacked_exist
2883 && !test_bit(Faulty
, &flags
)))
2884 len
+= sprintf(page
+len
, "blocked%s", sep
);
2885 if (!test_bit(Faulty
, &flags
) &&
2886 !test_bit(Journal
, &flags
) &&
2887 !test_bit(In_sync
, &flags
))
2888 len
+= sprintf(page
+len
, "spare%s", sep
);
2889 if (test_bit(WriteErrorSeen
, &flags
))
2890 len
+= sprintf(page
+len
, "write_error%s", sep
);
2891 if (test_bit(WantReplacement
, &flags
))
2892 len
+= sprintf(page
+len
, "want_replacement%s", sep
);
2893 if (test_bit(Replacement
, &flags
))
2894 len
+= sprintf(page
+len
, "replacement%s", sep
);
2895 if (test_bit(ExternalBbl
, &flags
))
2896 len
+= sprintf(page
+len
, "external_bbl%s", sep
);
2897 if (test_bit(FailFast
, &flags
))
2898 len
+= sprintf(page
+len
, "failfast%s", sep
);
2903 return len
+sprintf(page
+len
, "\n");
2907 state_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2910 * faulty - simulates an error
2911 * remove - disconnects the device
2912 * writemostly - sets write_mostly
2913 * -writemostly - clears write_mostly
2914 * blocked - sets the Blocked flags
2915 * -blocked - clears the Blocked and possibly simulates an error
2916 * insync - sets Insync providing device isn't active
2917 * -insync - clear Insync for a device with a slot assigned,
2918 * so that it gets rebuilt based on bitmap
2919 * write_error - sets WriteErrorSeen
2920 * -write_error - clears WriteErrorSeen
2921 * {,-}failfast - set/clear FailFast
2924 struct mddev
*mddev
= rdev
->mddev
;
2926 bool need_update_sb
= false;
2928 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
2929 md_error(rdev
->mddev
, rdev
);
2931 if (test_bit(MD_BROKEN
, &rdev
->mddev
->flags
))
2935 } else if (cmd_match(buf
, "remove")) {
2936 if (rdev
->mddev
->pers
) {
2937 clear_bit(Blocked
, &rdev
->flags
);
2938 remove_and_add_spares(rdev
->mddev
, rdev
);
2940 if (rdev
->raid_disk
>= 0)
2944 if (mddev_is_clustered(mddev
))
2945 err
= md_cluster_ops
->remove_disk(mddev
, rdev
);
2948 md_kick_rdev_from_array(rdev
);
2950 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2954 } else if (cmd_match(buf
, "writemostly")) {
2955 set_bit(WriteMostly
, &rdev
->flags
);
2956 mddev_create_serial_pool(rdev
->mddev
, rdev
);
2957 need_update_sb
= true;
2959 } else if (cmd_match(buf
, "-writemostly")) {
2960 mddev_destroy_serial_pool(rdev
->mddev
, rdev
);
2961 clear_bit(WriteMostly
, &rdev
->flags
);
2962 need_update_sb
= true;
2964 } else if (cmd_match(buf
, "blocked")) {
2965 set_bit(Blocked
, &rdev
->flags
);
2967 } else if (cmd_match(buf
, "-blocked")) {
2968 if (!test_bit(Faulty
, &rdev
->flags
) &&
2969 !test_bit(ExternalBbl
, &rdev
->flags
) &&
2970 rdev
->badblocks
.unacked_exist
) {
2971 /* metadata handler doesn't understand badblocks,
2972 * so we need to fail the device
2974 md_error(rdev
->mddev
, rdev
);
2976 clear_bit(Blocked
, &rdev
->flags
);
2977 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2978 wake_up(&rdev
->blocked_wait
);
2979 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2982 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
2983 set_bit(In_sync
, &rdev
->flags
);
2985 } else if (cmd_match(buf
, "failfast")) {
2986 set_bit(FailFast
, &rdev
->flags
);
2987 need_update_sb
= true;
2989 } else if (cmd_match(buf
, "-failfast")) {
2990 clear_bit(FailFast
, &rdev
->flags
);
2991 need_update_sb
= true;
2993 } else if (cmd_match(buf
, "-insync") && rdev
->raid_disk
>= 0 &&
2994 !test_bit(Journal
, &rdev
->flags
)) {
2995 if (rdev
->mddev
->pers
== NULL
) {
2996 clear_bit(In_sync
, &rdev
->flags
);
2997 rdev
->saved_raid_disk
= rdev
->raid_disk
;
2998 rdev
->raid_disk
= -1;
3001 } else if (cmd_match(buf
, "write_error")) {
3002 set_bit(WriteErrorSeen
, &rdev
->flags
);
3004 } else if (cmd_match(buf
, "-write_error")) {
3005 clear_bit(WriteErrorSeen
, &rdev
->flags
);
3007 } else if (cmd_match(buf
, "want_replacement")) {
3008 /* Any non-spare device that is not a replacement can
3009 * become want_replacement at any time, but we then need to
3010 * check if recovery is needed.
3012 if (rdev
->raid_disk
>= 0 &&
3013 !test_bit(Journal
, &rdev
->flags
) &&
3014 !test_bit(Replacement
, &rdev
->flags
))
3015 set_bit(WantReplacement
, &rdev
->flags
);
3016 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
3018 } else if (cmd_match(buf
, "-want_replacement")) {
3019 /* Clearing 'want_replacement' is always allowed.
3020 * Once replacements starts it is too late though.
3023 clear_bit(WantReplacement
, &rdev
->flags
);
3024 } else if (cmd_match(buf
, "replacement")) {
3025 /* Can only set a device as a replacement when array has not
3026 * yet been started. Once running, replacement is automatic
3027 * from spares, or by assigning 'slot'.
3029 if (rdev
->mddev
->pers
)
3032 set_bit(Replacement
, &rdev
->flags
);
3035 } else if (cmd_match(buf
, "-replacement")) {
3036 /* Similarly, can only clear Replacement before start */
3037 if (rdev
->mddev
->pers
)
3040 clear_bit(Replacement
, &rdev
->flags
);
3043 } else if (cmd_match(buf
, "re-add")) {
3044 if (!rdev
->mddev
->pers
)
3046 else if (test_bit(Faulty
, &rdev
->flags
) && (rdev
->raid_disk
== -1) &&
3047 rdev
->saved_raid_disk
>= 0) {
3048 /* clear_bit is performed _after_ all the devices
3049 * have their local Faulty bit cleared. If any writes
3050 * happen in the meantime in the local node, they
3051 * will land in the local bitmap, which will be synced
3052 * by this node eventually
3054 if (!mddev_is_clustered(rdev
->mddev
) ||
3055 (err
= md_cluster_ops
->gather_bitmaps(rdev
)) == 0) {
3056 clear_bit(Faulty
, &rdev
->flags
);
3057 err
= add_bound_rdev(rdev
);
3061 } else if (cmd_match(buf
, "external_bbl") && (rdev
->mddev
->external
)) {
3062 set_bit(ExternalBbl
, &rdev
->flags
);
3063 rdev
->badblocks
.shift
= 0;
3065 } else if (cmd_match(buf
, "-external_bbl") && (rdev
->mddev
->external
)) {
3066 clear_bit(ExternalBbl
, &rdev
->flags
);
3070 md_update_sb(mddev
, 1);
3072 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
3073 return err
? err
: len
;
3075 static struct rdev_sysfs_entry rdev_state
=
3076 __ATTR_PREALLOC(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
3079 errors_show(struct md_rdev
*rdev
, char *page
)
3081 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
3085 errors_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3090 rv
= kstrtouint(buf
, 10, &n
);
3093 atomic_set(&rdev
->corrected_errors
, n
);
3096 static struct rdev_sysfs_entry rdev_errors
=
3097 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
3100 slot_show(struct md_rdev
*rdev
, char *page
)
3102 if (test_bit(Journal
, &rdev
->flags
))
3103 return sprintf(page
, "journal\n");
3104 else if (rdev
->raid_disk
< 0)
3105 return sprintf(page
, "none\n");
3107 return sprintf(page
, "%d\n", rdev
->raid_disk
);
3111 slot_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3116 if (test_bit(Journal
, &rdev
->flags
))
3118 if (strncmp(buf
, "none", 4)==0)
3121 err
= kstrtouint(buf
, 10, (unsigned int *)&slot
);
3128 if (rdev
->mddev
->pers
&& slot
== -1) {
3129 /* Setting 'slot' on an active array requires also
3130 * updating the 'rd%d' link, and communicating
3131 * with the personality with ->hot_*_disk.
3132 * For now we only support removing
3133 * failed/spare devices. This normally happens automatically,
3134 * but not when the metadata is externally managed.
3136 if (rdev
->raid_disk
== -1)
3138 /* personality does all needed checks */
3139 if (rdev
->mddev
->pers
->hot_remove_disk
== NULL
)
3141 clear_bit(Blocked
, &rdev
->flags
);
3142 remove_and_add_spares(rdev
->mddev
, rdev
);
3143 if (rdev
->raid_disk
>= 0)
3145 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
3146 } else if (rdev
->mddev
->pers
) {
3147 /* Activating a spare .. or possibly reactivating
3148 * if we ever get bitmaps working here.
3152 if (rdev
->raid_disk
!= -1)
3155 if (test_bit(MD_RECOVERY_RUNNING
, &rdev
->mddev
->recovery
))
3158 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
3161 if (slot
>= rdev
->mddev
->raid_disks
&&
3162 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
3165 rdev
->raid_disk
= slot
;
3166 if (test_bit(In_sync
, &rdev
->flags
))
3167 rdev
->saved_raid_disk
= slot
;
3169 rdev
->saved_raid_disk
= -1;
3170 clear_bit(In_sync
, &rdev
->flags
);
3171 clear_bit(Bitmap_sync
, &rdev
->flags
);
3172 err
= rdev
->mddev
->pers
->hot_add_disk(rdev
->mddev
, rdev
);
3174 rdev
->raid_disk
= -1;
3177 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
3178 /* failure here is OK */;
3179 sysfs_link_rdev(rdev
->mddev
, rdev
);
3180 /* don't wakeup anyone, leave that to userspace. */
3182 if (slot
>= rdev
->mddev
->raid_disks
&&
3183 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
3185 rdev
->raid_disk
= slot
;
3186 /* assume it is working */
3187 clear_bit(Faulty
, &rdev
->flags
);
3188 clear_bit(WriteMostly
, &rdev
->flags
);
3189 set_bit(In_sync
, &rdev
->flags
);
3190 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
3195 static struct rdev_sysfs_entry rdev_slot
=
3196 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
3199 offset_show(struct md_rdev
*rdev
, char *page
)
3201 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
3205 offset_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3207 unsigned long long offset
;
3208 if (kstrtoull(buf
, 10, &offset
) < 0)
3210 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
3212 if (rdev
->sectors
&& rdev
->mddev
->external
)
3213 /* Must set offset before size, so overlap checks
3216 rdev
->data_offset
= offset
;
3217 rdev
->new_data_offset
= offset
;
3221 static struct rdev_sysfs_entry rdev_offset
=
3222 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
3224 static ssize_t
new_offset_show(struct md_rdev
*rdev
, char *page
)
3226 return sprintf(page
, "%llu\n",
3227 (unsigned long long)rdev
->new_data_offset
);
3230 static ssize_t
new_offset_store(struct md_rdev
*rdev
,
3231 const char *buf
, size_t len
)
3233 unsigned long long new_offset
;
3234 struct mddev
*mddev
= rdev
->mddev
;
3236 if (kstrtoull(buf
, 10, &new_offset
) < 0)
3239 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3241 if (new_offset
== rdev
->data_offset
)
3242 /* reset is always permitted */
3244 else if (new_offset
> rdev
->data_offset
) {
3245 /* must not push array size beyond rdev_sectors */
3246 if (new_offset
- rdev
->data_offset
3247 + mddev
->dev_sectors
> rdev
->sectors
)
3250 /* Metadata worries about other space details. */
3252 /* decreasing the offset is inconsistent with a backwards
3255 if (new_offset
< rdev
->data_offset
&&
3256 mddev
->reshape_backwards
)
3258 /* Increasing offset is inconsistent with forwards
3259 * reshape. reshape_direction should be set to
3260 * 'backwards' first.
3262 if (new_offset
> rdev
->data_offset
&&
3263 !mddev
->reshape_backwards
)
3266 if (mddev
->pers
&& mddev
->persistent
&&
3267 !super_types
[mddev
->major_version
]
3268 .allow_new_offset(rdev
, new_offset
))
3270 rdev
->new_data_offset
= new_offset
;
3271 if (new_offset
> rdev
->data_offset
)
3272 mddev
->reshape_backwards
= 1;
3273 else if (new_offset
< rdev
->data_offset
)
3274 mddev
->reshape_backwards
= 0;
3278 static struct rdev_sysfs_entry rdev_new_offset
=
3279 __ATTR(new_offset
, S_IRUGO
|S_IWUSR
, new_offset_show
, new_offset_store
);
3282 rdev_size_show(struct md_rdev
*rdev
, char *page
)
3284 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
3287 static int md_rdevs_overlap(struct md_rdev
*a
, struct md_rdev
*b
)
3289 /* check if two start/length pairs overlap */
3290 if (a
->data_offset
+ a
->sectors
<= b
->data_offset
)
3292 if (b
->data_offset
+ b
->sectors
<= a
->data_offset
)
3297 static bool md_rdev_overlaps(struct md_rdev
*rdev
)
3299 struct mddev
*mddev
;
3300 struct md_rdev
*rdev2
;
3302 spin_lock(&all_mddevs_lock
);
3303 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
) {
3304 if (test_bit(MD_DELETED
, &mddev
->flags
))
3306 rdev_for_each(rdev2
, mddev
) {
3307 if (rdev
!= rdev2
&& rdev
->bdev
== rdev2
->bdev
&&
3308 md_rdevs_overlap(rdev
, rdev2
)) {
3309 spin_unlock(&all_mddevs_lock
);
3314 spin_unlock(&all_mddevs_lock
);
3318 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
3320 unsigned long long blocks
;
3323 if (kstrtoull(buf
, 10, &blocks
) < 0)
3326 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
3327 return -EINVAL
; /* sector conversion overflow */
3330 if (new != blocks
* 2)
3331 return -EINVAL
; /* unsigned long long to sector_t overflow */
3338 rdev_size_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3340 struct mddev
*my_mddev
= rdev
->mddev
;
3341 sector_t oldsectors
= rdev
->sectors
;
3344 if (test_bit(Journal
, &rdev
->flags
))
3346 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
3348 if (rdev
->data_offset
!= rdev
->new_data_offset
)
3349 return -EINVAL
; /* too confusing */
3350 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
3351 if (my_mddev
->persistent
) {
3352 sectors
= super_types
[my_mddev
->major_version
].
3353 rdev_size_change(rdev
, sectors
);
3356 } else if (!sectors
)
3357 sectors
= bdev_nr_sectors(rdev
->bdev
) -
3359 if (!my_mddev
->pers
->resize
)
3360 /* Cannot change size for RAID0 or Linear etc */
3363 if (sectors
< my_mddev
->dev_sectors
)
3364 return -EINVAL
; /* component must fit device */
3366 rdev
->sectors
= sectors
;
3369 * Check that all other rdevs with the same bdev do not overlap. This
3370 * check does not provide a hard guarantee, it just helps avoid
3371 * dangerous mistakes.
3373 if (sectors
> oldsectors
&& my_mddev
->external
&&
3374 md_rdev_overlaps(rdev
)) {
3376 * Someone else could have slipped in a size change here, but
3377 * doing so is just silly. We put oldsectors back because we
3378 * know it is safe, and trust userspace not to race with itself.
3380 rdev
->sectors
= oldsectors
;
3386 static struct rdev_sysfs_entry rdev_size
=
3387 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
3389 static ssize_t
recovery_start_show(struct md_rdev
*rdev
, char *page
)
3391 unsigned long long recovery_start
= rdev
->recovery_offset
;
3393 if (test_bit(In_sync
, &rdev
->flags
) ||
3394 recovery_start
== MaxSector
)
3395 return sprintf(page
, "none\n");
3397 return sprintf(page
, "%llu\n", recovery_start
);
3400 static ssize_t
recovery_start_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3402 unsigned long long recovery_start
;
3404 if (cmd_match(buf
, "none"))
3405 recovery_start
= MaxSector
;
3406 else if (kstrtoull(buf
, 10, &recovery_start
))
3409 if (rdev
->mddev
->pers
&&
3410 rdev
->raid_disk
>= 0)
3413 rdev
->recovery_offset
= recovery_start
;
3414 if (recovery_start
== MaxSector
)
3415 set_bit(In_sync
, &rdev
->flags
);
3417 clear_bit(In_sync
, &rdev
->flags
);
3421 static struct rdev_sysfs_entry rdev_recovery_start
=
3422 __ATTR(recovery_start
, S_IRUGO
|S_IWUSR
, recovery_start_show
, recovery_start_store
);
3424 /* sysfs access to bad-blocks list.
3425 * We present two files.
3426 * 'bad-blocks' lists sector numbers and lengths of ranges that
3427 * are recorded as bad. The list is truncated to fit within
3428 * the one-page limit of sysfs.
3429 * Writing "sector length" to this file adds an acknowledged
3431 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3432 * been acknowledged. Writing to this file adds bad blocks
3433 * without acknowledging them. This is largely for testing.
3435 static ssize_t
bb_show(struct md_rdev
*rdev
, char *page
)
3437 return badblocks_show(&rdev
->badblocks
, page
, 0);
3439 static ssize_t
bb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
3441 int rv
= badblocks_store(&rdev
->badblocks
, page
, len
, 0);
3442 /* Maybe that ack was all we needed */
3443 if (test_and_clear_bit(BlockedBadBlocks
, &rdev
->flags
))
3444 wake_up(&rdev
->blocked_wait
);
3447 static struct rdev_sysfs_entry rdev_bad_blocks
=
3448 __ATTR(bad_blocks
, S_IRUGO
|S_IWUSR
, bb_show
, bb_store
);
3450 static ssize_t
ubb_show(struct md_rdev
*rdev
, char *page
)
3452 return badblocks_show(&rdev
->badblocks
, page
, 1);
3454 static ssize_t
ubb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
3456 return badblocks_store(&rdev
->badblocks
, page
, len
, 1);
3458 static struct rdev_sysfs_entry rdev_unack_bad_blocks
=
3459 __ATTR(unacknowledged_bad_blocks
, S_IRUGO
|S_IWUSR
, ubb_show
, ubb_store
);
3462 ppl_sector_show(struct md_rdev
*rdev
, char *page
)
3464 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->ppl
.sector
);
3468 ppl_sector_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3470 unsigned long long sector
;
3472 if (kstrtoull(buf
, 10, §or
) < 0)
3474 if (sector
!= (sector_t
)sector
)
3477 if (rdev
->mddev
->pers
&& test_bit(MD_HAS_PPL
, &rdev
->mddev
->flags
) &&
3478 rdev
->raid_disk
>= 0)
3481 if (rdev
->mddev
->persistent
) {
3482 if (rdev
->mddev
->major_version
== 0)
3484 if ((sector
> rdev
->sb_start
&&
3485 sector
- rdev
->sb_start
> S16_MAX
) ||
3486 (sector
< rdev
->sb_start
&&
3487 rdev
->sb_start
- sector
> -S16_MIN
))
3489 rdev
->ppl
.offset
= sector
- rdev
->sb_start
;
3490 } else if (!rdev
->mddev
->external
) {
3493 rdev
->ppl
.sector
= sector
;
3497 static struct rdev_sysfs_entry rdev_ppl_sector
=
3498 __ATTR(ppl_sector
, S_IRUGO
|S_IWUSR
, ppl_sector_show
, ppl_sector_store
);
3501 ppl_size_show(struct md_rdev
*rdev
, char *page
)
3503 return sprintf(page
, "%u\n", rdev
->ppl
.size
);
3507 ppl_size_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3511 if (kstrtouint(buf
, 10, &size
) < 0)
3514 if (rdev
->mddev
->pers
&& test_bit(MD_HAS_PPL
, &rdev
->mddev
->flags
) &&
3515 rdev
->raid_disk
>= 0)
3518 if (rdev
->mddev
->persistent
) {
3519 if (rdev
->mddev
->major_version
== 0)
3523 } else if (!rdev
->mddev
->external
) {
3526 rdev
->ppl
.size
= size
;
3530 static struct rdev_sysfs_entry rdev_ppl_size
=
3531 __ATTR(ppl_size
, S_IRUGO
|S_IWUSR
, ppl_size_show
, ppl_size_store
);
3533 static struct attribute
*rdev_default_attrs
[] = {
3538 &rdev_new_offset
.attr
,
3540 &rdev_recovery_start
.attr
,
3541 &rdev_bad_blocks
.attr
,
3542 &rdev_unack_bad_blocks
.attr
,
3543 &rdev_ppl_sector
.attr
,
3544 &rdev_ppl_size
.attr
,
3547 ATTRIBUTE_GROUPS(rdev_default
);
3549 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
3551 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3552 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3558 return entry
->show(rdev
, page
);
3562 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
3563 const char *page
, size_t length
)
3565 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3566 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3567 struct kernfs_node
*kn
= NULL
;
3568 bool suspend
= false;
3570 struct mddev
*mddev
= READ_ONCE(rdev
->mddev
);
3574 if (!capable(CAP_SYS_ADMIN
))
3579 if (entry
->store
== state_store
) {
3580 if (cmd_match(page
, "remove"))
3581 kn
= sysfs_break_active_protection(kobj
, attr
);
3582 if (cmd_match(page
, "remove") || cmd_match(page
, "re-add") ||
3583 cmd_match(page
, "writemostly") ||
3584 cmd_match(page
, "-writemostly"))
3588 rv
= suspend
? mddev_suspend_and_lock(mddev
) : mddev_lock(mddev
);
3590 if (rdev
->mddev
== NULL
)
3593 rv
= entry
->store(rdev
, page
, length
);
3594 suspend
? mddev_unlock_and_resume(mddev
) : mddev_unlock(mddev
);
3598 sysfs_unbreak_active_protection(kn
);
3603 static void rdev_free(struct kobject
*ko
)
3605 struct md_rdev
*rdev
= container_of(ko
, struct md_rdev
, kobj
);
3608 static const struct sysfs_ops rdev_sysfs_ops
= {
3609 .show
= rdev_attr_show
,
3610 .store
= rdev_attr_store
,
3612 static const struct kobj_type rdev_ktype
= {
3613 .release
= rdev_free
,
3614 .sysfs_ops
= &rdev_sysfs_ops
,
3615 .default_groups
= rdev_default_groups
,
3618 int md_rdev_init(struct md_rdev
*rdev
)
3621 rdev
->saved_raid_disk
= -1;
3622 rdev
->raid_disk
= -1;
3624 rdev
->data_offset
= 0;
3625 rdev
->new_data_offset
= 0;
3626 rdev
->sb_events
= 0;
3627 rdev
->last_read_error
= 0;
3628 rdev
->sb_loaded
= 0;
3629 rdev
->bb_page
= NULL
;
3630 atomic_set(&rdev
->nr_pending
, 0);
3631 atomic_set(&rdev
->read_errors
, 0);
3632 atomic_set(&rdev
->corrected_errors
, 0);
3634 INIT_LIST_HEAD(&rdev
->same_set
);
3635 init_waitqueue_head(&rdev
->blocked_wait
);
3637 /* Add space to store bad block list.
3638 * This reserves the space even on arrays where it cannot
3639 * be used - I wonder if that matters
3641 return badblocks_init(&rdev
->badblocks
, 0);
3643 EXPORT_SYMBOL_GPL(md_rdev_init
);
3646 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3648 * mark the device faulty if:
3650 * - the device is nonexistent (zero size)
3651 * - the device has no valid superblock
3653 * a faulty rdev _never_ has rdev->sb set.
3655 static struct md_rdev
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
3657 struct md_rdev
*rdev
;
3661 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
3663 return ERR_PTR(-ENOMEM
);
3665 err
= md_rdev_init(rdev
);
3668 err
= alloc_disk_sb(rdev
);
3670 goto out_clear_rdev
;
3672 rdev
->bdev_file
= bdev_file_open_by_dev(newdev
,
3673 BLK_OPEN_READ
| BLK_OPEN_WRITE
,
3674 super_format
== -2 ? &claim_rdev
: rdev
, NULL
);
3675 if (IS_ERR(rdev
->bdev_file
)) {
3676 pr_warn("md: could not open device unknown-block(%u,%u).\n",
3677 MAJOR(newdev
), MINOR(newdev
));
3678 err
= PTR_ERR(rdev
->bdev_file
);
3679 goto out_clear_rdev
;
3681 rdev
->bdev
= file_bdev(rdev
->bdev_file
);
3683 kobject_init(&rdev
->kobj
, &rdev_ktype
);
3685 size
= bdev_nr_bytes(rdev
->bdev
) >> BLOCK_SIZE_BITS
;
3687 pr_warn("md: %pg has zero or unknown size, marking faulty!\n",
3690 goto out_blkdev_put
;
3693 if (super_format
>= 0) {
3694 err
= super_types
[super_format
].
3695 load_super(rdev
, NULL
, super_minor
);
3696 if (err
== -EINVAL
) {
3697 pr_warn("md: %pg does not have a valid v%d.%d superblock, not importing!\n",
3699 super_format
, super_minor
);
3700 goto out_blkdev_put
;
3703 pr_warn("md: could not read %pg's sb, not importing!\n",
3705 goto out_blkdev_put
;
3712 fput(rdev
->bdev_file
);
3714 md_rdev_clear(rdev
);
3717 return ERR_PTR(err
);
3721 * Check a full RAID array for plausibility
3724 static int analyze_sbs(struct mddev
*mddev
)
3727 struct md_rdev
*rdev
, *freshest
, *tmp
;
3730 rdev_for_each_safe(rdev
, tmp
, mddev
)
3731 switch (super_types
[mddev
->major_version
].
3732 load_super(rdev
, freshest
, mddev
->minor_version
)) {
3739 pr_warn("md: fatal superblock inconsistency in %pg -- removing from array\n",
3741 md_kick_rdev_from_array(rdev
);
3744 /* Cannot find a valid fresh disk */
3746 pr_warn("md: cannot find a valid disk\n");
3750 super_types
[mddev
->major_version
].
3751 validate_super(mddev
, NULL
/*freshest*/, freshest
);
3754 rdev_for_each_safe(rdev
, tmp
, mddev
) {
3755 if (mddev
->max_disks
&&
3756 (rdev
->desc_nr
>= mddev
->max_disks
||
3757 i
> mddev
->max_disks
)) {
3758 pr_warn("md: %s: %pg: only %d devices permitted\n",
3759 mdname(mddev
), rdev
->bdev
,
3761 md_kick_rdev_from_array(rdev
);
3764 if (rdev
!= freshest
) {
3765 if (super_types
[mddev
->major_version
].
3766 validate_super(mddev
, freshest
, rdev
)) {
3767 pr_warn("md: kicking non-fresh %pg from array!\n",
3769 md_kick_rdev_from_array(rdev
);
3773 if (rdev
->raid_disk
>= (mddev
->raid_disks
- min(0, mddev
->delta_disks
)) &&
3774 !test_bit(Journal
, &rdev
->flags
)) {
3775 rdev
->raid_disk
= -1;
3776 clear_bit(In_sync
, &rdev
->flags
);
3783 /* Read a fixed-point number.
3784 * Numbers in sysfs attributes should be in "standard" units where
3785 * possible, so time should be in seconds.
3786 * However we internally use a a much smaller unit such as
3787 * milliseconds or jiffies.
3788 * This function takes a decimal number with a possible fractional
3789 * component, and produces an integer which is the result of
3790 * multiplying that number by 10^'scale'.
3791 * all without any floating-point arithmetic.
3793 int strict_strtoul_scaled(const char *cp
, unsigned long *res
, int scale
)
3795 unsigned long result
= 0;
3797 while (isdigit(*cp
) || (*cp
== '.' && decimals
< 0)) {
3800 else if (decimals
< scale
) {
3803 result
= result
* 10 + value
;
3815 *res
= result
* int_pow(10, scale
- decimals
);
3820 safe_delay_show(struct mddev
*mddev
, char *page
)
3822 unsigned int msec
= ((unsigned long)mddev
->safemode_delay
*1000)/HZ
;
3824 return sprintf(page
, "%u.%03u\n", msec
/1000, msec
%1000);
3827 safe_delay_store(struct mddev
*mddev
, const char *cbuf
, size_t len
)
3831 if (mddev_is_clustered(mddev
)) {
3832 pr_warn("md: Safemode is disabled for clustered mode\n");
3836 if (strict_strtoul_scaled(cbuf
, &msec
, 3) < 0 || msec
> UINT_MAX
/ HZ
)
3839 mddev
->safemode_delay
= 0;
3841 unsigned long old_delay
= mddev
->safemode_delay
;
3842 unsigned long new_delay
= (msec
*HZ
)/1000;
3846 mddev
->safemode_delay
= new_delay
;
3847 if (new_delay
< old_delay
|| old_delay
== 0)
3848 mod_timer(&mddev
->safemode_timer
, jiffies
+1);
3852 static struct md_sysfs_entry md_safe_delay
=
3853 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
3856 level_show(struct mddev
*mddev
, char *page
)
3858 struct md_personality
*p
;
3860 spin_lock(&mddev
->lock
);
3863 ret
= sprintf(page
, "%s\n", p
->name
);
3864 else if (mddev
->clevel
[0])
3865 ret
= sprintf(page
, "%s\n", mddev
->clevel
);
3866 else if (mddev
->level
!= LEVEL_NONE
)
3867 ret
= sprintf(page
, "%d\n", mddev
->level
);
3870 spin_unlock(&mddev
->lock
);
3875 level_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3880 struct md_personality
*pers
, *oldpers
;
3882 void *priv
, *oldpriv
;
3883 struct md_rdev
*rdev
;
3885 if (slen
== 0 || slen
>= sizeof(clevel
))
3888 rv
= mddev_suspend_and_lock(mddev
);
3892 if (mddev
->pers
== NULL
) {
3893 memcpy(mddev
->clevel
, buf
, slen
);
3894 if (mddev
->clevel
[slen
-1] == '\n')
3896 mddev
->clevel
[slen
] = 0;
3897 mddev
->level
= LEVEL_NONE
;
3902 if (!md_is_rdwr(mddev
))
3905 /* request to change the personality. Need to ensure:
3906 * - array is not engaged in resync/recovery/reshape
3907 * - old personality can be suspended
3908 * - new personality will access other array.
3912 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3913 mddev
->reshape_position
!= MaxSector
||
3914 mddev
->sysfs_active
)
3918 if (!mddev
->pers
->quiesce
) {
3919 pr_warn("md: %s: %s does not support online personality change\n",
3920 mdname(mddev
), mddev
->pers
->name
);
3924 /* Now find the new personality */
3925 memcpy(clevel
, buf
, slen
);
3926 if (clevel
[slen
-1] == '\n')
3929 if (kstrtol(clevel
, 10, &level
))
3932 if (request_module("md-%s", clevel
) != 0)
3933 request_module("md-level-%s", clevel
);
3934 spin_lock(&pers_lock
);
3935 pers
= find_pers(level
, clevel
);
3936 if (!pers
|| !try_module_get(pers
->owner
)) {
3937 spin_unlock(&pers_lock
);
3938 pr_warn("md: personality %s not loaded\n", clevel
);
3942 spin_unlock(&pers_lock
);
3944 if (pers
== mddev
->pers
) {
3945 /* Nothing to do! */
3946 module_put(pers
->owner
);
3950 if (!pers
->takeover
) {
3951 module_put(pers
->owner
);
3952 pr_warn("md: %s: %s does not support personality takeover\n",
3953 mdname(mddev
), clevel
);
3958 rdev_for_each(rdev
, mddev
)
3959 rdev
->new_raid_disk
= rdev
->raid_disk
;
3961 /* ->takeover must set new_* and/or delta_disks
3962 * if it succeeds, and may set them when it fails.
3964 priv
= pers
->takeover(mddev
);
3966 mddev
->new_level
= mddev
->level
;
3967 mddev
->new_layout
= mddev
->layout
;
3968 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3969 mddev
->raid_disks
-= mddev
->delta_disks
;
3970 mddev
->delta_disks
= 0;
3971 mddev
->reshape_backwards
= 0;
3972 module_put(pers
->owner
);
3973 pr_warn("md: %s: %s would not accept array\n",
3974 mdname(mddev
), clevel
);
3979 /* Looks like we have a winner */
3980 mddev_detach(mddev
);
3982 spin_lock(&mddev
->lock
);
3983 oldpers
= mddev
->pers
;
3984 oldpriv
= mddev
->private;
3986 mddev
->private = priv
;
3987 strscpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3988 mddev
->level
= mddev
->new_level
;
3989 mddev
->layout
= mddev
->new_layout
;
3990 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
3991 mddev
->delta_disks
= 0;
3992 mddev
->reshape_backwards
= 0;
3993 mddev
->degraded
= 0;
3994 spin_unlock(&mddev
->lock
);
3996 if (oldpers
->sync_request
== NULL
&&
3998 /* We are converting from a no-redundancy array
3999 * to a redundancy array and metadata is managed
4000 * externally so we need to be sure that writes
4001 * won't block due to a need to transition
4003 * until external management is started.
4006 mddev
->safemode_delay
= 0;
4007 mddev
->safemode
= 0;
4010 oldpers
->free(mddev
, oldpriv
);
4012 if (oldpers
->sync_request
== NULL
&&
4013 pers
->sync_request
!= NULL
) {
4014 /* need to add the md_redundancy_group */
4015 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
4016 pr_warn("md: cannot register extra attributes for %s\n",
4018 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, "sync_action");
4019 mddev
->sysfs_completed
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "sync_completed");
4020 mddev
->sysfs_degraded
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "degraded");
4022 if (oldpers
->sync_request
!= NULL
&&
4023 pers
->sync_request
== NULL
) {
4024 /* need to remove the md_redundancy_group */
4025 if (mddev
->to_remove
== NULL
)
4026 mddev
->to_remove
= &md_redundancy_group
;
4029 module_put(oldpers
->owner
);
4031 rdev_for_each(rdev
, mddev
) {
4032 if (rdev
->raid_disk
< 0)
4034 if (rdev
->new_raid_disk
>= mddev
->raid_disks
)
4035 rdev
->new_raid_disk
= -1;
4036 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
4038 sysfs_unlink_rdev(mddev
, rdev
);
4040 rdev_for_each(rdev
, mddev
) {
4041 if (rdev
->raid_disk
< 0)
4043 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
4045 rdev
->raid_disk
= rdev
->new_raid_disk
;
4046 if (rdev
->raid_disk
< 0)
4047 clear_bit(In_sync
, &rdev
->flags
);
4049 if (sysfs_link_rdev(mddev
, rdev
))
4050 pr_warn("md: cannot register rd%d for %s after level change\n",
4051 rdev
->raid_disk
, mdname(mddev
));
4055 if (pers
->sync_request
== NULL
) {
4056 /* this is now an array without redundancy, so
4057 * it must always be in_sync
4060 del_timer_sync(&mddev
->safemode_timer
);
4063 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
4065 md_update_sb(mddev
, 1);
4066 sysfs_notify_dirent_safe(mddev
->sysfs_level
);
4070 mddev_unlock_and_resume(mddev
);
4074 static struct md_sysfs_entry md_level
=
4075 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
4078 new_level_show(struct mddev
*mddev
, char *page
)
4080 return sprintf(page
, "%d\n", mddev
->new_level
);
4084 new_level_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4089 err
= kstrtouint(buf
, 10, &n
);
4092 err
= mddev_lock(mddev
);
4096 mddev
->new_level
= n
;
4097 md_update_sb(mddev
, 1);
4099 mddev_unlock(mddev
);
4102 static struct md_sysfs_entry md_new_level
=
4103 __ATTR(new_level
, 0664, new_level_show
, new_level_store
);
4106 layout_show(struct mddev
*mddev
, char *page
)
4108 /* just a number, not meaningful for all levels */
4109 if (mddev
->reshape_position
!= MaxSector
&&
4110 mddev
->layout
!= mddev
->new_layout
)
4111 return sprintf(page
, "%d (%d)\n",
4112 mddev
->new_layout
, mddev
->layout
);
4113 return sprintf(page
, "%d\n", mddev
->layout
);
4117 layout_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4122 err
= kstrtouint(buf
, 10, &n
);
4125 err
= mddev_lock(mddev
);
4130 if (mddev
->pers
->check_reshape
== NULL
)
4132 else if (!md_is_rdwr(mddev
))
4135 mddev
->new_layout
= n
;
4136 err
= mddev
->pers
->check_reshape(mddev
);
4138 mddev
->new_layout
= mddev
->layout
;
4141 mddev
->new_layout
= n
;
4142 if (mddev
->reshape_position
== MaxSector
)
4145 mddev_unlock(mddev
);
4148 static struct md_sysfs_entry md_layout
=
4149 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
4152 raid_disks_show(struct mddev
*mddev
, char *page
)
4154 if (mddev
->raid_disks
== 0)
4156 if (mddev
->reshape_position
!= MaxSector
&&
4157 mddev
->delta_disks
!= 0)
4158 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
4159 mddev
->raid_disks
- mddev
->delta_disks
);
4160 return sprintf(page
, "%d\n", mddev
->raid_disks
);
4163 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
);
4166 raid_disks_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4171 err
= kstrtouint(buf
, 10, &n
);
4175 err
= mddev_lock(mddev
);
4179 err
= update_raid_disks(mddev
, n
);
4180 else if (mddev
->reshape_position
!= MaxSector
) {
4181 struct md_rdev
*rdev
;
4182 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
4185 rdev_for_each(rdev
, mddev
) {
4187 rdev
->data_offset
< rdev
->new_data_offset
)
4190 rdev
->data_offset
> rdev
->new_data_offset
)
4194 mddev
->delta_disks
= n
- olddisks
;
4195 mddev
->raid_disks
= n
;
4196 mddev
->reshape_backwards
= (mddev
->delta_disks
< 0);
4198 mddev
->raid_disks
= n
;
4200 mddev_unlock(mddev
);
4201 return err
? err
: len
;
4203 static struct md_sysfs_entry md_raid_disks
=
4204 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
4207 uuid_show(struct mddev
*mddev
, char *page
)
4209 return sprintf(page
, "%pU\n", mddev
->uuid
);
4211 static struct md_sysfs_entry md_uuid
=
4212 __ATTR(uuid
, S_IRUGO
, uuid_show
, NULL
);
4215 chunk_size_show(struct mddev
*mddev
, char *page
)
4217 if (mddev
->reshape_position
!= MaxSector
&&
4218 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
4219 return sprintf(page
, "%d (%d)\n",
4220 mddev
->new_chunk_sectors
<< 9,
4221 mddev
->chunk_sectors
<< 9);
4222 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
4226 chunk_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4231 err
= kstrtoul(buf
, 10, &n
);
4235 err
= mddev_lock(mddev
);
4239 if (mddev
->pers
->check_reshape
== NULL
)
4241 else if (!md_is_rdwr(mddev
))
4244 mddev
->new_chunk_sectors
= n
>> 9;
4245 err
= mddev
->pers
->check_reshape(mddev
);
4247 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4250 mddev
->new_chunk_sectors
= n
>> 9;
4251 if (mddev
->reshape_position
== MaxSector
)
4252 mddev
->chunk_sectors
= n
>> 9;
4254 mddev_unlock(mddev
);
4257 static struct md_sysfs_entry md_chunk_size
=
4258 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
4261 resync_start_show(struct mddev
*mddev
, char *page
)
4263 if (mddev
->recovery_cp
== MaxSector
)
4264 return sprintf(page
, "none\n");
4265 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
4269 resync_start_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4271 unsigned long long n
;
4274 if (cmd_match(buf
, "none"))
4277 err
= kstrtoull(buf
, 10, &n
);
4280 if (n
!= (sector_t
)n
)
4284 err
= mddev_lock(mddev
);
4287 if (mddev
->pers
&& !test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
4291 mddev
->recovery_cp
= n
;
4293 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
4295 mddev_unlock(mddev
);
4298 static struct md_sysfs_entry md_resync_start
=
4299 __ATTR_PREALLOC(resync_start
, S_IRUGO
|S_IWUSR
,
4300 resync_start_show
, resync_start_store
);
4303 * The array state can be:
4306 * No devices, no size, no level
4307 * Equivalent to STOP_ARRAY ioctl
4309 * May have some settings, but array is not active
4310 * all IO results in error
4311 * When written, doesn't tear down array, but just stops it
4312 * suspended (not supported yet)
4313 * All IO requests will block. The array can be reconfigured.
4314 * Writing this, if accepted, will block until array is quiescent
4316 * no resync can happen. no superblocks get written.
4317 * write requests fail
4319 * like readonly, but behaves like 'clean' on a write request.
4321 * clean - no pending writes, but otherwise active.
4322 * When written to inactive array, starts without resync
4323 * If a write request arrives then
4324 * if metadata is known, mark 'dirty' and switch to 'active'.
4325 * if not known, block and switch to write-pending
4326 * If written to an active array that has pending writes, then fails.
4328 * fully active: IO and resync can be happening.
4329 * When written to inactive array, starts with resync
4332 * clean, but writes are blocked waiting for 'active' to be written.
4335 * like active, but no writes have been seen for a while (100msec).
4338 * Array is failed. It's useful because mounted-arrays aren't stopped
4339 * when array is failed, so this state will at least alert the user that
4340 * something is wrong.
4342 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
4343 write_pending
, active_idle
, broken
, bad_word
};
4344 static char *array_states
[] = {
4345 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
4346 "write-pending", "active-idle", "broken", NULL
};
4348 static int match_word(const char *word
, char **list
)
4351 for (n
=0; list
[n
]; n
++)
4352 if (cmd_match(word
, list
[n
]))
4358 array_state_show(struct mddev
*mddev
, char *page
)
4360 enum array_state st
= inactive
;
4362 if (mddev
->pers
&& !test_bit(MD_NOT_READY
, &mddev
->flags
)) {
4371 spin_lock(&mddev
->lock
);
4372 if (test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
))
4374 else if (mddev
->in_sync
)
4376 else if (mddev
->safemode
)
4380 spin_unlock(&mddev
->lock
);
4383 if (test_bit(MD_BROKEN
, &mddev
->flags
) && st
== clean
)
4386 if (list_empty(&mddev
->disks
) &&
4387 mddev
->raid_disks
== 0 &&
4388 mddev
->dev_sectors
== 0)
4393 return sprintf(page
, "%s\n", array_states
[st
]);
4396 static int do_md_stop(struct mddev
*mddev
, int ro
);
4397 static int md_set_readonly(struct mddev
*mddev
);
4398 static int restart_array(struct mddev
*mddev
);
4401 array_state_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4404 enum array_state st
= match_word(buf
, array_states
);
4406 /* No lock dependent actions */
4408 case suspended
: /* not supported yet */
4409 case write_pending
: /* cannot be set */
4410 case active_idle
: /* cannot be set */
4411 case broken
: /* cannot be set */
4418 if (!mddev
->pers
|| !md_is_rdwr(mddev
))
4420 /* write sysfs will not open mddev and opener should be 0 */
4421 err
= mddev_set_closing_and_sync_blockdev(mddev
, 0);
4429 if (mddev
->pers
&& (st
== active
|| st
== clean
) &&
4430 mddev
->ro
!= MD_RDONLY
) {
4431 /* don't take reconfig_mutex when toggling between
4434 spin_lock(&mddev
->lock
);
4436 restart_array(mddev
);
4437 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
4438 md_wakeup_thread(mddev
->thread
);
4439 wake_up(&mddev
->sb_wait
);
4440 } else /* st == clean */ {
4441 restart_array(mddev
);
4442 if (!set_in_sync(mddev
))
4446 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4447 spin_unlock(&mddev
->lock
);
4450 err
= mddev_lock(mddev
);
4456 /* stop an active array, return 0 otherwise */
4458 err
= do_md_stop(mddev
, 2);
4461 err
= do_md_stop(mddev
, 0);
4465 err
= md_set_readonly(mddev
);
4467 mddev
->ro
= MD_RDONLY
;
4468 set_disk_ro(mddev
->gendisk
, 1);
4469 err
= do_md_run(mddev
);
4474 if (md_is_rdwr(mddev
))
4475 err
= md_set_readonly(mddev
);
4476 else if (mddev
->ro
== MD_RDONLY
)
4477 err
= restart_array(mddev
);
4479 mddev
->ro
= MD_AUTO_READ
;
4480 set_disk_ro(mddev
->gendisk
, 0);
4483 mddev
->ro
= MD_AUTO_READ
;
4484 err
= do_md_run(mddev
);
4489 err
= restart_array(mddev
);
4492 spin_lock(&mddev
->lock
);
4493 if (!set_in_sync(mddev
))
4495 spin_unlock(&mddev
->lock
);
4501 err
= restart_array(mddev
);
4504 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
4505 wake_up(&mddev
->sb_wait
);
4508 mddev
->ro
= MD_RDWR
;
4509 set_disk_ro(mddev
->gendisk
, 0);
4510 err
= do_md_run(mddev
);
4519 if (mddev
->hold_active
== UNTIL_IOCTL
)
4520 mddev
->hold_active
= 0;
4521 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4523 mddev_unlock(mddev
);
4525 if (st
== readonly
|| st
== read_auto
|| st
== inactive
||
4526 (err
&& st
== clear
))
4527 clear_bit(MD_CLOSING
, &mddev
->flags
);
4531 static struct md_sysfs_entry md_array_state
=
4532 __ATTR_PREALLOC(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
4535 max_corrected_read_errors_show(struct mddev
*mddev
, char *page
) {
4536 return sprintf(page
, "%d\n",
4537 atomic_read(&mddev
->max_corr_read_errors
));
4541 max_corrected_read_errors_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4546 rv
= kstrtouint(buf
, 10, &n
);
4551 atomic_set(&mddev
->max_corr_read_errors
, n
);
4555 static struct md_sysfs_entry max_corr_read_errors
=
4556 __ATTR(max_read_errors
, S_IRUGO
|S_IWUSR
, max_corrected_read_errors_show
,
4557 max_corrected_read_errors_store
);
4560 null_show(struct mddev
*mddev
, char *page
)
4566 new_dev_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4568 /* buf must be %d:%d\n? giving major and minor numbers */
4569 /* The new device is added to the array.
4570 * If the array has a persistent superblock, we read the
4571 * superblock to initialise info and check validity.
4572 * Otherwise, only checking done is that in bind_rdev_to_array,
4573 * which mainly checks size.
4576 int major
= simple_strtoul(buf
, &e
, 10);
4579 struct md_rdev
*rdev
;
4582 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
4584 minor
= simple_strtoul(e
+1, &e
, 10);
4585 if (*e
&& *e
!= '\n')
4587 dev
= MKDEV(major
, minor
);
4588 if (major
!= MAJOR(dev
) ||
4589 minor
!= MINOR(dev
))
4592 err
= mddev_suspend_and_lock(mddev
);
4595 if (mddev
->persistent
) {
4596 rdev
= md_import_device(dev
, mddev
->major_version
,
4597 mddev
->minor_version
);
4598 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
4599 struct md_rdev
*rdev0
4600 = list_entry(mddev
->disks
.next
,
4601 struct md_rdev
, same_set
);
4602 err
= super_types
[mddev
->major_version
]
4603 .load_super(rdev
, rdev0
, mddev
->minor_version
);
4607 } else if (mddev
->external
)
4608 rdev
= md_import_device(dev
, -2, -1);
4610 rdev
= md_import_device(dev
, -1, -1);
4613 mddev_unlock_and_resume(mddev
);
4614 return PTR_ERR(rdev
);
4616 err
= bind_rdev_to_array(rdev
, mddev
);
4619 export_rdev(rdev
, mddev
);
4620 mddev_unlock_and_resume(mddev
);
4623 return err
? err
: len
;
4626 static struct md_sysfs_entry md_new_device
=
4627 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
4630 bitmap_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4633 unsigned long chunk
, end_chunk
;
4636 err
= mddev_lock(mddev
);
4641 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4643 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
4647 if (*end
== '-') { /* range */
4649 end_chunk
= simple_strtoul(buf
, &end
, 0);
4654 if (*end
&& !isspace(*end
))
4657 mddev
->bitmap_ops
->dirty_bits(mddev
, chunk
, end_chunk
);
4658 buf
= skip_spaces(end
);
4660 mddev
->bitmap_ops
->unplug(mddev
, true); /* flush the bits to disk */
4662 mddev_unlock(mddev
);
4666 static struct md_sysfs_entry md_bitmap
=
4667 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
4670 size_show(struct mddev
*mddev
, char *page
)
4672 return sprintf(page
, "%llu\n",
4673 (unsigned long long)mddev
->dev_sectors
/ 2);
4676 static int update_size(struct mddev
*mddev
, sector_t num_sectors
);
4679 size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4681 /* If array is inactive, we can reduce the component size, but
4682 * not increase it (except from 0).
4683 * If array is active, we can try an on-line resize
4686 int err
= strict_blocks_to_sectors(buf
, §ors
);
4690 err
= mddev_lock(mddev
);
4694 err
= update_size(mddev
, sectors
);
4696 md_update_sb(mddev
, 1);
4698 if (mddev
->dev_sectors
== 0 ||
4699 mddev
->dev_sectors
> sectors
)
4700 mddev
->dev_sectors
= sectors
;
4704 mddev_unlock(mddev
);
4705 return err
? err
: len
;
4708 static struct md_sysfs_entry md_size
=
4709 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
4711 /* Metadata version.
4713 * 'none' for arrays with no metadata (good luck...)
4714 * 'external' for arrays with externally managed metadata,
4715 * or N.M for internally known formats
4718 metadata_show(struct mddev
*mddev
, char *page
)
4720 if (mddev
->persistent
)
4721 return sprintf(page
, "%d.%d\n",
4722 mddev
->major_version
, mddev
->minor_version
);
4723 else if (mddev
->external
)
4724 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
4726 return sprintf(page
, "none\n");
4730 metadata_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4735 /* Changing the details of 'external' metadata is
4736 * always permitted. Otherwise there must be
4737 * no devices attached to the array.
4740 err
= mddev_lock(mddev
);
4744 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
4746 else if (!list_empty(&mddev
->disks
))
4750 if (cmd_match(buf
, "none")) {
4751 mddev
->persistent
= 0;
4752 mddev
->external
= 0;
4753 mddev
->major_version
= 0;
4754 mddev
->minor_version
= 90;
4757 if (strncmp(buf
, "external:", 9) == 0) {
4758 size_t namelen
= len
-9;
4759 if (namelen
>= sizeof(mddev
->metadata_type
))
4760 namelen
= sizeof(mddev
->metadata_type
)-1;
4761 memcpy(mddev
->metadata_type
, buf
+9, namelen
);
4762 mddev
->metadata_type
[namelen
] = 0;
4763 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
4764 mddev
->metadata_type
[--namelen
] = 0;
4765 mddev
->persistent
= 0;
4766 mddev
->external
= 1;
4767 mddev
->major_version
= 0;
4768 mddev
->minor_version
= 90;
4771 major
= simple_strtoul(buf
, &e
, 10);
4773 if (e
==buf
|| *e
!= '.')
4776 minor
= simple_strtoul(buf
, &e
, 10);
4777 if (e
==buf
|| (*e
&& *e
!= '\n') )
4780 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
4782 mddev
->major_version
= major
;
4783 mddev
->minor_version
= minor
;
4784 mddev
->persistent
= 1;
4785 mddev
->external
= 0;
4788 mddev_unlock(mddev
);
4792 static struct md_sysfs_entry md_metadata
=
4793 __ATTR_PREALLOC(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
4795 enum sync_action
md_sync_action(struct mddev
*mddev
)
4797 unsigned long recovery
= mddev
->recovery
;
4800 * frozen has the highest priority, means running sync_thread will be
4801 * stopped immediately, and no new sync_thread can start.
4803 if (test_bit(MD_RECOVERY_FROZEN
, &recovery
))
4804 return ACTION_FROZEN
;
4807 * read-only array can't register sync_thread, and it can only
4808 * add/remove spares.
4810 if (!md_is_rdwr(mddev
))
4814 * idle means no sync_thread is running, and no new sync_thread is
4817 if (!test_bit(MD_RECOVERY_RUNNING
, &recovery
) &&
4818 !test_bit(MD_RECOVERY_NEEDED
, &recovery
))
4821 if (test_bit(MD_RECOVERY_RESHAPE
, &recovery
) ||
4822 mddev
->reshape_position
!= MaxSector
)
4823 return ACTION_RESHAPE
;
4825 if (test_bit(MD_RECOVERY_RECOVER
, &recovery
))
4826 return ACTION_RECOVER
;
4828 if (test_bit(MD_RECOVERY_SYNC
, &recovery
)) {
4830 * MD_RECOVERY_CHECK must be paired with
4831 * MD_RECOVERY_REQUESTED.
4833 if (test_bit(MD_RECOVERY_CHECK
, &recovery
))
4834 return ACTION_CHECK
;
4835 if (test_bit(MD_RECOVERY_REQUESTED
, &recovery
))
4836 return ACTION_REPAIR
;
4837 return ACTION_RESYNC
;
4841 * MD_RECOVERY_NEEDED or MD_RECOVERY_RUNNING is set, however, no
4842 * sync_action is specified.
4847 enum sync_action
md_sync_action_by_name(const char *page
)
4849 enum sync_action action
;
4851 for (action
= 0; action
< NR_SYNC_ACTIONS
; ++action
) {
4852 if (cmd_match(page
, action_name
[action
]))
4856 return NR_SYNC_ACTIONS
;
4859 const char *md_sync_action_name(enum sync_action action
)
4861 return action_name
[action
];
4865 action_show(struct mddev
*mddev
, char *page
)
4867 enum sync_action action
= md_sync_action(mddev
);
4869 return sprintf(page
, "%s\n", md_sync_action_name(action
));
4873 * stop_sync_thread() - wait for sync_thread to stop if it's running.
4874 * @mddev: the array.
4875 * @locked: if set, reconfig_mutex will still be held after this function
4876 * return; if not set, reconfig_mutex will be released after this
4879 static void stop_sync_thread(struct mddev
*mddev
, bool locked
)
4881 int sync_seq
= atomic_read(&mddev
->sync_seq
);
4883 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
)) {
4885 mddev_unlock(mddev
);
4889 mddev_unlock(mddev
);
4891 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4893 * Thread might be blocked waiting for metadata update which will now
4896 md_wakeup_thread_directly(mddev
->sync_thread
);
4897 if (work_pending(&mddev
->sync_work
))
4898 flush_work(&mddev
->sync_work
);
4900 wait_event(resync_wait
,
4901 !test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
4902 (!test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
) &&
4903 sync_seq
!= atomic_read(&mddev
->sync_seq
)));
4906 mddev_lock_nointr(mddev
);
4909 void md_idle_sync_thread(struct mddev
*mddev
)
4911 lockdep_assert_held(&mddev
->reconfig_mutex
);
4913 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4914 stop_sync_thread(mddev
, true);
4916 EXPORT_SYMBOL_GPL(md_idle_sync_thread
);
4918 void md_frozen_sync_thread(struct mddev
*mddev
)
4920 lockdep_assert_held(&mddev
->reconfig_mutex
);
4922 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4923 stop_sync_thread(mddev
, true);
4925 EXPORT_SYMBOL_GPL(md_frozen_sync_thread
);
4927 void md_unfrozen_sync_thread(struct mddev
*mddev
)
4929 lockdep_assert_held(&mddev
->reconfig_mutex
);
4931 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4932 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4933 md_wakeup_thread(mddev
->thread
);
4934 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4936 EXPORT_SYMBOL_GPL(md_unfrozen_sync_thread
);
4938 static int mddev_start_reshape(struct mddev
*mddev
)
4942 if (mddev
->pers
->start_reshape
== NULL
)
4945 if (mddev
->reshape_position
== MaxSector
||
4946 mddev
->pers
->check_reshape
== NULL
||
4947 mddev
->pers
->check_reshape(mddev
)) {
4948 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4949 ret
= mddev
->pers
->start_reshape(mddev
);
4954 * If reshape is still in progress, and md_check_recovery() can
4955 * continue to reshape, don't restart reshape because data can
4956 * be corrupted for raid456.
4958 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4961 sysfs_notify_dirent_safe(mddev
->sysfs_degraded
);
4966 action_store(struct mddev
*mddev
, const char *page
, size_t len
)
4969 enum sync_action action
;
4971 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
4975 if (work_busy(&mddev
->sync_work
))
4976 flush_work(&mddev
->sync_work
);
4978 ret
= mddev_lock(mddev
);
4982 if (work_busy(&mddev
->sync_work
)) {
4983 mddev_unlock(mddev
);
4987 action
= md_sync_action_by_name(page
);
4989 /* TODO: mdadm rely on "idle" to start sync_thread. */
4990 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
)) {
4993 md_frozen_sync_thread(mddev
);
4997 md_idle_sync_thread(mddev
);
4999 case ACTION_RESHAPE
:
5000 case ACTION_RECOVER
:
5013 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5016 case ACTION_RESHAPE
:
5017 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5018 ret
= mddev_start_reshape(mddev
);
5022 case ACTION_RECOVER
:
5023 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5024 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5027 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
5030 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
5031 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
5035 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5043 if (mddev
->ro
== MD_AUTO_READ
) {
5044 /* A write to sync_action is enough to justify
5045 * canceling read-auto mode
5047 mddev
->ro
= MD_RDWR
;
5048 md_wakeup_thread(mddev
->sync_thread
);
5051 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5052 md_wakeup_thread(mddev
->thread
);
5053 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
5057 mddev_unlock(mddev
);
5061 static struct md_sysfs_entry md_scan_mode
=
5062 __ATTR_PREALLOC(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
5065 last_sync_action_show(struct mddev
*mddev
, char *page
)
5067 return sprintf(page
, "%s\n",
5068 md_sync_action_name(mddev
->last_sync_action
));
5071 static struct md_sysfs_entry md_last_scan_mode
= __ATTR_RO(last_sync_action
);
5074 mismatch_cnt_show(struct mddev
*mddev
, char *page
)
5076 return sprintf(page
, "%llu\n",
5077 (unsigned long long)
5078 atomic64_read(&mddev
->resync_mismatches
));
5081 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
5084 sync_min_show(struct mddev
*mddev
, char *page
)
5086 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
5087 mddev
->sync_speed_min
? "local": "system");
5091 sync_min_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5096 if (strncmp(buf
, "system", 6)==0) {
5099 rv
= kstrtouint(buf
, 10, &min
);
5105 mddev
->sync_speed_min
= min
;
5109 static struct md_sysfs_entry md_sync_min
=
5110 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
5113 sync_max_show(struct mddev
*mddev
, char *page
)
5115 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
5116 mddev
->sync_speed_max
? "local": "system");
5120 sync_max_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5125 if (strncmp(buf
, "system", 6)==0) {
5128 rv
= kstrtouint(buf
, 10, &max
);
5134 mddev
->sync_speed_max
= max
;
5138 static struct md_sysfs_entry md_sync_max
=
5139 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
5142 degraded_show(struct mddev
*mddev
, char *page
)
5144 return sprintf(page
, "%d\n", mddev
->degraded
);
5146 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
5149 sync_force_parallel_show(struct mddev
*mddev
, char *page
)
5151 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
5155 sync_force_parallel_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5159 if (kstrtol(buf
, 10, &n
))
5162 if (n
!= 0 && n
!= 1)
5165 mddev
->parallel_resync
= n
;
5167 if (mddev
->sync_thread
)
5168 wake_up(&resync_wait
);
5173 /* force parallel resync, even with shared block devices */
5174 static struct md_sysfs_entry md_sync_force_parallel
=
5175 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
5176 sync_force_parallel_show
, sync_force_parallel_store
);
5179 sync_speed_show(struct mddev
*mddev
, char *page
)
5181 unsigned long resync
, dt
, db
;
5182 if (mddev
->curr_resync
== MD_RESYNC_NONE
)
5183 return sprintf(page
, "none\n");
5184 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
5185 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
5187 db
= resync
- mddev
->resync_mark_cnt
;
5188 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
5191 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
5194 sync_completed_show(struct mddev
*mddev
, char *page
)
5196 unsigned long long max_sectors
, resync
;
5198 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
5199 return sprintf(page
, "none\n");
5201 if (mddev
->curr_resync
== MD_RESYNC_YIELDED
||
5202 mddev
->curr_resync
== MD_RESYNC_DELAYED
)
5203 return sprintf(page
, "delayed\n");
5205 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
5206 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
5207 max_sectors
= mddev
->resync_max_sectors
;
5209 max_sectors
= mddev
->dev_sectors
;
5211 resync
= mddev
->curr_resync_completed
;
5212 return sprintf(page
, "%llu / %llu\n", resync
, max_sectors
);
5215 static struct md_sysfs_entry md_sync_completed
=
5216 __ATTR_PREALLOC(sync_completed
, S_IRUGO
, sync_completed_show
, NULL
);
5219 min_sync_show(struct mddev
*mddev
, char *page
)
5221 return sprintf(page
, "%llu\n",
5222 (unsigned long long)mddev
->resync_min
);
5225 min_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5227 unsigned long long min
;
5230 if (kstrtoull(buf
, 10, &min
))
5233 spin_lock(&mddev
->lock
);
5235 if (min
> mddev
->resync_max
)
5239 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
5242 /* Round down to multiple of 4K for safety */
5243 mddev
->resync_min
= round_down(min
, 8);
5247 spin_unlock(&mddev
->lock
);
5251 static struct md_sysfs_entry md_min_sync
=
5252 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
5255 max_sync_show(struct mddev
*mddev
, char *page
)
5257 if (mddev
->resync_max
== MaxSector
)
5258 return sprintf(page
, "max\n");
5260 return sprintf(page
, "%llu\n",
5261 (unsigned long long)mddev
->resync_max
);
5264 max_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5267 spin_lock(&mddev
->lock
);
5268 if (strncmp(buf
, "max", 3) == 0)
5269 mddev
->resync_max
= MaxSector
;
5271 unsigned long long max
;
5275 if (kstrtoull(buf
, 10, &max
))
5277 if (max
< mddev
->resync_min
)
5281 if (max
< mddev
->resync_max
&& md_is_rdwr(mddev
) &&
5282 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
5285 /* Must be a multiple of chunk_size */
5286 chunk
= mddev
->chunk_sectors
;
5288 sector_t temp
= max
;
5291 if (sector_div(temp
, chunk
))
5294 mddev
->resync_max
= max
;
5296 wake_up(&mddev
->recovery_wait
);
5299 spin_unlock(&mddev
->lock
);
5303 static struct md_sysfs_entry md_max_sync
=
5304 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
5307 suspend_lo_show(struct mddev
*mddev
, char *page
)
5309 return sprintf(page
, "%llu\n",
5310 (unsigned long long)READ_ONCE(mddev
->suspend_lo
));
5314 suspend_lo_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5316 unsigned long long new;
5319 err
= kstrtoull(buf
, 10, &new);
5322 if (new != (sector_t
)new)
5325 err
= mddev_suspend(mddev
, true);
5329 WRITE_ONCE(mddev
->suspend_lo
, new);
5330 mddev_resume(mddev
);
5334 static struct md_sysfs_entry md_suspend_lo
=
5335 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
5338 suspend_hi_show(struct mddev
*mddev
, char *page
)
5340 return sprintf(page
, "%llu\n",
5341 (unsigned long long)READ_ONCE(mddev
->suspend_hi
));
5345 suspend_hi_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5347 unsigned long long new;
5350 err
= kstrtoull(buf
, 10, &new);
5353 if (new != (sector_t
)new)
5356 err
= mddev_suspend(mddev
, true);
5360 WRITE_ONCE(mddev
->suspend_hi
, new);
5361 mddev_resume(mddev
);
5365 static struct md_sysfs_entry md_suspend_hi
=
5366 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
5369 reshape_position_show(struct mddev
*mddev
, char *page
)
5371 if (mddev
->reshape_position
!= MaxSector
)
5372 return sprintf(page
, "%llu\n",
5373 (unsigned long long)mddev
->reshape_position
);
5374 strcpy(page
, "none\n");
5379 reshape_position_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5381 struct md_rdev
*rdev
;
5382 unsigned long long new;
5385 err
= kstrtoull(buf
, 10, &new);
5388 if (new != (sector_t
)new)
5390 err
= mddev_lock(mddev
);
5396 mddev
->reshape_position
= new;
5397 mddev
->delta_disks
= 0;
5398 mddev
->reshape_backwards
= 0;
5399 mddev
->new_level
= mddev
->level
;
5400 mddev
->new_layout
= mddev
->layout
;
5401 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
5402 rdev_for_each(rdev
, mddev
)
5403 rdev
->new_data_offset
= rdev
->data_offset
;
5406 mddev_unlock(mddev
);
5410 static struct md_sysfs_entry md_reshape_position
=
5411 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
5412 reshape_position_store
);
5415 reshape_direction_show(struct mddev
*mddev
, char *page
)
5417 return sprintf(page
, "%s\n",
5418 mddev
->reshape_backwards
? "backwards" : "forwards");
5422 reshape_direction_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5427 if (cmd_match(buf
, "forwards"))
5429 else if (cmd_match(buf
, "backwards"))
5433 if (mddev
->reshape_backwards
== backwards
)
5436 err
= mddev_lock(mddev
);
5439 /* check if we are allowed to change */
5440 if (mddev
->delta_disks
)
5442 else if (mddev
->persistent
&&
5443 mddev
->major_version
== 0)
5446 mddev
->reshape_backwards
= backwards
;
5447 mddev_unlock(mddev
);
5451 static struct md_sysfs_entry md_reshape_direction
=
5452 __ATTR(reshape_direction
, S_IRUGO
|S_IWUSR
, reshape_direction_show
,
5453 reshape_direction_store
);
5456 array_size_show(struct mddev
*mddev
, char *page
)
5458 if (mddev
->external_size
)
5459 return sprintf(page
, "%llu\n",
5460 (unsigned long long)mddev
->array_sectors
/2);
5462 return sprintf(page
, "default\n");
5466 array_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5471 err
= mddev_lock(mddev
);
5475 /* cluster raid doesn't support change array_sectors */
5476 if (mddev_is_clustered(mddev
)) {
5477 mddev_unlock(mddev
);
5481 if (strncmp(buf
, "default", 7) == 0) {
5483 sectors
= mddev
->pers
->size(mddev
, 0, 0);
5485 sectors
= mddev
->array_sectors
;
5487 mddev
->external_size
= 0;
5489 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
5491 else if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
5494 mddev
->external_size
= 1;
5498 mddev
->array_sectors
= sectors
;
5500 set_capacity_and_notify(mddev
->gendisk
,
5501 mddev
->array_sectors
);
5503 mddev_unlock(mddev
);
5507 static struct md_sysfs_entry md_array_size
=
5508 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
5512 consistency_policy_show(struct mddev
*mddev
, char *page
)
5516 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
)) {
5517 ret
= sprintf(page
, "journal\n");
5518 } else if (test_bit(MD_HAS_PPL
, &mddev
->flags
)) {
5519 ret
= sprintf(page
, "ppl\n");
5520 } else if (mddev
->bitmap
) {
5521 ret
= sprintf(page
, "bitmap\n");
5522 } else if (mddev
->pers
) {
5523 if (mddev
->pers
->sync_request
)
5524 ret
= sprintf(page
, "resync\n");
5526 ret
= sprintf(page
, "none\n");
5528 ret
= sprintf(page
, "unknown\n");
5535 consistency_policy_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5540 if (mddev
->pers
->change_consistency_policy
)
5541 err
= mddev
->pers
->change_consistency_policy(mddev
, buf
);
5544 } else if (mddev
->external
&& strncmp(buf
, "ppl", 3) == 0) {
5545 set_bit(MD_HAS_PPL
, &mddev
->flags
);
5550 return err
? err
: len
;
5553 static struct md_sysfs_entry md_consistency_policy
=
5554 __ATTR(consistency_policy
, S_IRUGO
| S_IWUSR
, consistency_policy_show
,
5555 consistency_policy_store
);
5557 static ssize_t
fail_last_dev_show(struct mddev
*mddev
, char *page
)
5559 return sprintf(page
, "%d\n", mddev
->fail_last_dev
);
5563 * Setting fail_last_dev to true to allow last device to be forcibly removed
5564 * from RAID1/RAID10.
5567 fail_last_dev_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5572 ret
= kstrtobool(buf
, &value
);
5576 if (value
!= mddev
->fail_last_dev
)
5577 mddev
->fail_last_dev
= value
;
5581 static struct md_sysfs_entry md_fail_last_dev
=
5582 __ATTR(fail_last_dev
, S_IRUGO
| S_IWUSR
, fail_last_dev_show
,
5583 fail_last_dev_store
);
5585 static ssize_t
serialize_policy_show(struct mddev
*mddev
, char *page
)
5587 if (mddev
->pers
== NULL
|| (mddev
->pers
->level
!= 1))
5588 return sprintf(page
, "n/a\n");
5590 return sprintf(page
, "%d\n", mddev
->serialize_policy
);
5594 * Setting serialize_policy to true to enforce write IO is not reordered
5598 serialize_policy_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5603 err
= kstrtobool(buf
, &value
);
5607 if (value
== mddev
->serialize_policy
)
5610 err
= mddev_suspend_and_lock(mddev
);
5613 if (mddev
->pers
== NULL
|| (mddev
->pers
->level
!= 1)) {
5614 pr_err("md: serialize_policy is only effective for raid1\n");
5620 mddev_create_serial_pool(mddev
, NULL
);
5622 mddev_destroy_serial_pool(mddev
, NULL
);
5623 mddev
->serialize_policy
= value
;
5625 mddev_unlock_and_resume(mddev
);
5629 static struct md_sysfs_entry md_serialize_policy
=
5630 __ATTR(serialize_policy
, S_IRUGO
| S_IWUSR
, serialize_policy_show
,
5631 serialize_policy_store
);
5634 static struct attribute
*md_default_attrs
[] = {
5638 &md_raid_disks
.attr
,
5640 &md_chunk_size
.attr
,
5642 &md_resync_start
.attr
,
5644 &md_new_device
.attr
,
5645 &md_safe_delay
.attr
,
5646 &md_array_state
.attr
,
5647 &md_reshape_position
.attr
,
5648 &md_reshape_direction
.attr
,
5649 &md_array_size
.attr
,
5650 &max_corr_read_errors
.attr
,
5651 &md_consistency_policy
.attr
,
5652 &md_fail_last_dev
.attr
,
5653 &md_serialize_policy
.attr
,
5657 static const struct attribute_group md_default_group
= {
5658 .attrs
= md_default_attrs
,
5661 static struct attribute
*md_redundancy_attrs
[] = {
5663 &md_last_scan_mode
.attr
,
5664 &md_mismatches
.attr
,
5667 &md_sync_speed
.attr
,
5668 &md_sync_force_parallel
.attr
,
5669 &md_sync_completed
.attr
,
5672 &md_suspend_lo
.attr
,
5673 &md_suspend_hi
.attr
,
5678 static const struct attribute_group md_redundancy_group
= {
5680 .attrs
= md_redundancy_attrs
,
5683 static const struct attribute_group
*md_attr_groups
[] = {
5690 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
5692 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
5693 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
5698 spin_lock(&all_mddevs_lock
);
5699 if (!mddev_get(mddev
)) {
5700 spin_unlock(&all_mddevs_lock
);
5703 spin_unlock(&all_mddevs_lock
);
5705 rv
= entry
->show(mddev
, page
);
5711 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
5712 const char *page
, size_t length
)
5714 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
5715 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
5720 if (!capable(CAP_SYS_ADMIN
))
5722 spin_lock(&all_mddevs_lock
);
5723 if (!mddev_get(mddev
)) {
5724 spin_unlock(&all_mddevs_lock
);
5727 spin_unlock(&all_mddevs_lock
);
5728 rv
= entry
->store(mddev
, page
, length
);
5733 static void md_kobj_release(struct kobject
*ko
)
5735 struct mddev
*mddev
= container_of(ko
, struct mddev
, kobj
);
5737 if (mddev
->sysfs_state
)
5738 sysfs_put(mddev
->sysfs_state
);
5739 if (mddev
->sysfs_level
)
5740 sysfs_put(mddev
->sysfs_level
);
5742 del_gendisk(mddev
->gendisk
);
5743 put_disk(mddev
->gendisk
);
5746 static const struct sysfs_ops md_sysfs_ops
= {
5747 .show
= md_attr_show
,
5748 .store
= md_attr_store
,
5750 static const struct kobj_type md_ktype
= {
5751 .release
= md_kobj_release
,
5752 .sysfs_ops
= &md_sysfs_ops
,
5753 .default_groups
= md_attr_groups
,
5758 /* stack the limit for all rdevs into lim */
5759 int mddev_stack_rdev_limits(struct mddev
*mddev
, struct queue_limits
*lim
,
5762 struct md_rdev
*rdev
;
5764 rdev_for_each(rdev
, mddev
) {
5765 queue_limits_stack_bdev(lim
, rdev
->bdev
, rdev
->data_offset
,
5766 mddev
->gendisk
->disk_name
);
5767 if ((flags
& MDDEV_STACK_INTEGRITY
) &&
5768 !queue_limits_stack_integrity_bdev(lim
, rdev
->bdev
))
5774 EXPORT_SYMBOL_GPL(mddev_stack_rdev_limits
);
5776 /* apply the extra stacking limits from a new rdev into mddev */
5777 int mddev_stack_new_rdev(struct mddev
*mddev
, struct md_rdev
*rdev
)
5779 struct queue_limits lim
;
5781 if (mddev_is_dm(mddev
))
5784 lim
= queue_limits_start_update(mddev
->gendisk
->queue
);
5785 queue_limits_stack_bdev(&lim
, rdev
->bdev
, rdev
->data_offset
,
5786 mddev
->gendisk
->disk_name
);
5788 if (!queue_limits_stack_integrity_bdev(&lim
, rdev
->bdev
)) {
5789 pr_err("%s: incompatible integrity profile for %pg\n",
5790 mdname(mddev
), rdev
->bdev
);
5791 queue_limits_cancel_update(mddev
->gendisk
->queue
);
5795 return queue_limits_commit_update(mddev
->gendisk
->queue
, &lim
);
5797 EXPORT_SYMBOL_GPL(mddev_stack_new_rdev
);
5799 /* update the optimal I/O size after a reshape */
5800 void mddev_update_io_opt(struct mddev
*mddev
, unsigned int nr_stripes
)
5802 struct queue_limits lim
;
5804 if (mddev_is_dm(mddev
))
5807 /* don't bother updating io_opt if we can't suspend the array */
5808 if (mddev_suspend(mddev
, false) < 0)
5810 lim
= queue_limits_start_update(mddev
->gendisk
->queue
);
5811 lim
.io_opt
= lim
.io_min
* nr_stripes
;
5812 queue_limits_commit_update(mddev
->gendisk
->queue
, &lim
);
5813 mddev_resume(mddev
);
5815 EXPORT_SYMBOL_GPL(mddev_update_io_opt
);
5817 static void mddev_delayed_delete(struct work_struct
*ws
)
5819 struct mddev
*mddev
= container_of(ws
, struct mddev
, del_work
);
5821 kobject_put(&mddev
->kobj
);
5824 void md_init_stacking_limits(struct queue_limits
*lim
)
5826 blk_set_stacking_limits(lim
);
5827 lim
->features
= BLK_FEAT_WRITE_CACHE
| BLK_FEAT_FUA
|
5828 BLK_FEAT_IO_STAT
| BLK_FEAT_NOWAIT
;
5830 EXPORT_SYMBOL_GPL(md_init_stacking_limits
);
5832 struct mddev
*md_alloc(dev_t dev
, char *name
)
5835 * If dev is zero, name is the name of a device to allocate with
5836 * an arbitrary minor number. It will be "md_???"
5837 * If dev is non-zero it must be a device number with a MAJOR of
5838 * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then
5839 * the device is being created by opening a node in /dev.
5840 * If "name" is not NULL, the device is being created by
5841 * writing to /sys/module/md_mod/parameters/new_array.
5843 static DEFINE_MUTEX(disks_mutex
);
5844 struct mddev
*mddev
;
5845 struct gendisk
*disk
;
5852 * Wait for any previous instance of this device to be completely
5853 * removed (mddev_delayed_delete).
5855 flush_workqueue(md_misc_wq
);
5857 mutex_lock(&disks_mutex
);
5858 mddev
= mddev_alloc(dev
);
5859 if (IS_ERR(mddev
)) {
5860 error
= PTR_ERR(mddev
);
5864 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
5865 shift
= partitioned
? MdpMinorShift
: 0;
5866 unit
= MINOR(mddev
->unit
) >> shift
;
5869 /* Need to ensure that 'name' is not a duplicate.
5871 struct mddev
*mddev2
;
5872 spin_lock(&all_mddevs_lock
);
5874 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
5875 if (mddev2
->gendisk
&&
5876 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
5877 spin_unlock(&all_mddevs_lock
);
5879 goto out_free_mddev
;
5881 spin_unlock(&all_mddevs_lock
);
5885 * Creating /dev/mdNNN via "newarray", so adjust hold_active.
5887 mddev
->hold_active
= UNTIL_STOP
;
5889 disk
= blk_alloc_disk(NULL
, NUMA_NO_NODE
);
5891 error
= PTR_ERR(disk
);
5892 goto out_free_mddev
;
5895 disk
->major
= MAJOR(mddev
->unit
);
5896 disk
->first_minor
= unit
<< shift
;
5897 disk
->minors
= 1 << shift
;
5899 strcpy(disk
->disk_name
, name
);
5900 else if (partitioned
)
5901 sprintf(disk
->disk_name
, "md_d%d", unit
);
5903 sprintf(disk
->disk_name
, "md%d", unit
);
5904 disk
->fops
= &md_fops
;
5905 disk
->private_data
= mddev
;
5907 disk
->events
|= DISK_EVENT_MEDIA_CHANGE
;
5908 mddev
->gendisk
= disk
;
5909 error
= add_disk(disk
);
5913 kobject_init(&mddev
->kobj
, &md_ktype
);
5914 error
= kobject_add(&mddev
->kobj
, &disk_to_dev(disk
)->kobj
, "%s", "md");
5917 * The disk is already live at this point. Clear the hold flag
5918 * and let mddev_put take care of the deletion, as it isn't any
5919 * different from a normal close on last release now.
5921 mddev
->hold_active
= 0;
5922 mutex_unlock(&disks_mutex
);
5924 return ERR_PTR(error
);
5927 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
5928 mddev
->sysfs_state
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "array_state");
5929 mddev
->sysfs_level
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "level");
5930 mutex_unlock(&disks_mutex
);
5938 mutex_unlock(&disks_mutex
);
5939 return ERR_PTR(error
);
5942 static int md_alloc_and_put(dev_t dev
, char *name
)
5944 struct mddev
*mddev
= md_alloc(dev
, name
);
5947 return PTR_ERR(mddev
);
5952 static void md_probe(dev_t dev
)
5954 if (MAJOR(dev
) == MD_MAJOR
&& MINOR(dev
) >= 512)
5957 md_alloc_and_put(dev
, NULL
);
5960 static int add_named_array(const char *val
, const struct kernel_param
*kp
)
5963 * val must be "md_*" or "mdNNN".
5964 * For "md_*" we allocate an array with a large free minor number, and
5965 * set the name to val. val must not already be an active name.
5966 * For "mdNNN" we allocate an array with the minor number NNN
5967 * which must not already be in use.
5969 int len
= strlen(val
);
5970 char buf
[DISK_NAME_LEN
];
5971 unsigned long devnum
;
5973 while (len
&& val
[len
-1] == '\n')
5975 if (len
>= DISK_NAME_LEN
)
5977 strscpy(buf
, val
, len
+1);
5978 if (strncmp(buf
, "md_", 3) == 0)
5979 return md_alloc_and_put(0, buf
);
5980 if (strncmp(buf
, "md", 2) == 0 &&
5982 kstrtoul(buf
+2, 10, &devnum
) == 0 &&
5983 devnum
<= MINORMASK
)
5984 return md_alloc_and_put(MKDEV(MD_MAJOR
, devnum
), NULL
);
5989 static void md_safemode_timeout(struct timer_list
*t
)
5991 struct mddev
*mddev
= from_timer(mddev
, t
, safemode_timer
);
5993 mddev
->safemode
= 1;
5994 if (mddev
->external
)
5995 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5997 md_wakeup_thread(mddev
->thread
);
6000 static int start_dirty_degraded
;
6002 int md_run(struct mddev
*mddev
)
6005 struct md_rdev
*rdev
;
6006 struct md_personality
*pers
;
6009 if (list_empty(&mddev
->disks
))
6010 /* cannot run an array with no devices.. */
6015 /* Cannot run until previous stop completes properly */
6016 if (mddev
->sysfs_active
)
6020 * Analyze all RAID superblock(s)
6022 if (!mddev
->raid_disks
) {
6023 if (!mddev
->persistent
)
6025 err
= analyze_sbs(mddev
);
6030 if (mddev
->level
!= LEVEL_NONE
)
6031 request_module("md-level-%d", mddev
->level
);
6032 else if (mddev
->clevel
[0])
6033 request_module("md-%s", mddev
->clevel
);
6036 * Drop all container device buffers, from now on
6037 * the only valid external interface is through the md
6040 mddev
->has_superblocks
= false;
6041 rdev_for_each(rdev
, mddev
) {
6042 if (test_bit(Faulty
, &rdev
->flags
))
6044 sync_blockdev(rdev
->bdev
);
6045 invalidate_bdev(rdev
->bdev
);
6046 if (mddev
->ro
!= MD_RDONLY
&& rdev_read_only(rdev
)) {
6047 mddev
->ro
= MD_RDONLY
;
6048 if (!mddev_is_dm(mddev
))
6049 set_disk_ro(mddev
->gendisk
, 1);
6053 mddev
->has_superblocks
= true;
6055 /* perform some consistency tests on the device.
6056 * We don't want the data to overlap the metadata,
6057 * Internal Bitmap issues have been handled elsewhere.
6059 if (rdev
->meta_bdev
) {
6060 /* Nothing to check */;
6061 } else if (rdev
->data_offset
< rdev
->sb_start
) {
6062 if (mddev
->dev_sectors
&&
6063 rdev
->data_offset
+ mddev
->dev_sectors
6065 pr_warn("md: %s: data overlaps metadata\n",
6070 if (rdev
->sb_start
+ rdev
->sb_size
/512
6071 > rdev
->data_offset
) {
6072 pr_warn("md: %s: metadata overlaps data\n",
6077 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
6078 nowait
= nowait
&& bdev_nowait(rdev
->bdev
);
6081 if (!bioset_initialized(&mddev
->bio_set
)) {
6082 err
= bioset_init(&mddev
->bio_set
, BIO_POOL_SIZE
, 0, BIOSET_NEED_BVECS
);
6086 if (!bioset_initialized(&mddev
->sync_set
)) {
6087 err
= bioset_init(&mddev
->sync_set
, BIO_POOL_SIZE
, 0, BIOSET_NEED_BVECS
);
6092 if (!bioset_initialized(&mddev
->io_clone_set
)) {
6093 err
= bioset_init(&mddev
->io_clone_set
, BIO_POOL_SIZE
,
6094 offsetof(struct md_io_clone
, bio_clone
), 0);
6099 spin_lock(&pers_lock
);
6100 pers
= find_pers(mddev
->level
, mddev
->clevel
);
6101 if (!pers
|| !try_module_get(pers
->owner
)) {
6102 spin_unlock(&pers_lock
);
6103 if (mddev
->level
!= LEVEL_NONE
)
6104 pr_warn("md: personality for level %d is not loaded!\n",
6107 pr_warn("md: personality for level %s is not loaded!\n",
6112 spin_unlock(&pers_lock
);
6113 if (mddev
->level
!= pers
->level
) {
6114 mddev
->level
= pers
->level
;
6115 mddev
->new_level
= pers
->level
;
6117 strscpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
6119 if (mddev
->reshape_position
!= MaxSector
&&
6120 pers
->start_reshape
== NULL
) {
6121 /* This personality cannot handle reshaping... */
6122 module_put(pers
->owner
);
6127 if (pers
->sync_request
) {
6128 /* Warn if this is a potentially silly
6131 struct md_rdev
*rdev2
;
6134 rdev_for_each(rdev
, mddev
)
6135 rdev_for_each(rdev2
, mddev
) {
6137 rdev
->bdev
->bd_disk
==
6138 rdev2
->bdev
->bd_disk
) {
6139 pr_warn("%s: WARNING: %pg appears to be on the same physical disk as %pg.\n",
6148 pr_warn("True protection against single-disk failure might be compromised.\n");
6151 /* dm-raid expect sync_thread to be frozen until resume */
6153 mddev
->recovery
= 0;
6155 /* may be over-ridden by personality */
6156 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
6158 mddev
->ok_start_degraded
= start_dirty_degraded
;
6160 if (start_readonly
&& md_is_rdwr(mddev
))
6161 mddev
->ro
= MD_AUTO_READ
; /* read-only, but switch on first write */
6163 err
= pers
->run(mddev
);
6165 pr_warn("md: pers->run() failed ...\n");
6166 else if (pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
6167 WARN_ONCE(!mddev
->external_size
,
6168 "%s: default size too small, but 'external_size' not in effect?\n",
6170 pr_warn("md: invalid array_size %llu > default size %llu\n",
6171 (unsigned long long)mddev
->array_sectors
/ 2,
6172 (unsigned long long)pers
->size(mddev
, 0, 0) / 2);
6175 if (err
== 0 && pers
->sync_request
&&
6176 (mddev
->bitmap_info
.file
|| mddev
->bitmap_info
.offset
)) {
6177 err
= mddev
->bitmap_ops
->create(mddev
, -1);
6179 pr_warn("%s: failed to create bitmap (%d)\n",
6180 mdname(mddev
), err
);
6185 if (mddev
->bitmap_info
.max_write_behind
> 0) {
6186 bool create_pool
= false;
6188 rdev_for_each(rdev
, mddev
) {
6189 if (test_bit(WriteMostly
, &rdev
->flags
) &&
6190 rdev_init_serial(rdev
))
6193 if (create_pool
&& mddev
->serial_info_pool
== NULL
) {
6194 mddev
->serial_info_pool
=
6195 mempool_create_kmalloc_pool(NR_SERIAL_INFOS
,
6196 sizeof(struct serial_info
));
6197 if (!mddev
->serial_info_pool
) {
6204 if (pers
->sync_request
) {
6205 if (mddev
->kobj
.sd
&&
6206 sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
6207 pr_warn("md: cannot register extra attributes for %s\n",
6209 mddev
->sysfs_action
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "sync_action");
6210 mddev
->sysfs_completed
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "sync_completed");
6211 mddev
->sysfs_degraded
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "degraded");
6212 } else if (mddev
->ro
== MD_AUTO_READ
)
6213 mddev
->ro
= MD_RDWR
;
6215 atomic_set(&mddev
->max_corr_read_errors
,
6216 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS
);
6217 mddev
->safemode
= 0;
6218 if (mddev_is_clustered(mddev
))
6219 mddev
->safemode_delay
= 0;
6221 mddev
->safemode_delay
= DEFAULT_SAFEMODE_DELAY
;
6224 spin_lock(&mddev
->lock
);
6226 spin_unlock(&mddev
->lock
);
6227 rdev_for_each(rdev
, mddev
)
6228 if (rdev
->raid_disk
>= 0)
6229 sysfs_link_rdev(mddev
, rdev
); /* failure here is OK */
6231 if (mddev
->degraded
&& md_is_rdwr(mddev
))
6232 /* This ensures that recovering status is reported immediately
6233 * via sysfs - until a lack of spares is confirmed.
6235 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6236 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6238 if (mddev
->sb_flags
)
6239 md_update_sb(mddev
, 0);
6245 mddev_detach(mddev
);
6247 pers
->free(mddev
, mddev
->private);
6248 mddev
->private = NULL
;
6249 module_put(pers
->owner
);
6250 mddev
->bitmap_ops
->destroy(mddev
);
6252 bioset_exit(&mddev
->io_clone_set
);
6254 bioset_exit(&mddev
->sync_set
);
6256 bioset_exit(&mddev
->bio_set
);
6259 EXPORT_SYMBOL_GPL(md_run
);
6261 int do_md_run(struct mddev
*mddev
)
6265 set_bit(MD_NOT_READY
, &mddev
->flags
);
6266 err
= md_run(mddev
);
6270 err
= mddev
->bitmap_ops
->load(mddev
);
6272 mddev
->bitmap_ops
->destroy(mddev
);
6276 if (mddev_is_clustered(mddev
))
6277 md_allow_write(mddev
);
6279 /* run start up tasks that require md_thread */
6282 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
6284 set_capacity_and_notify(mddev
->gendisk
, mddev
->array_sectors
);
6285 clear_bit(MD_NOT_READY
, &mddev
->flags
);
6287 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
6288 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6289 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
6290 sysfs_notify_dirent_safe(mddev
->sysfs_degraded
);
6292 clear_bit(MD_NOT_READY
, &mddev
->flags
);
6296 int md_start(struct mddev
*mddev
)
6300 if (mddev
->pers
->start
) {
6301 set_bit(MD_RECOVERY_WAIT
, &mddev
->recovery
);
6302 ret
= mddev
->pers
->start(mddev
);
6303 clear_bit(MD_RECOVERY_WAIT
, &mddev
->recovery
);
6304 md_wakeup_thread(mddev
->sync_thread
);
6308 EXPORT_SYMBOL_GPL(md_start
);
6310 static int restart_array(struct mddev
*mddev
)
6312 struct gendisk
*disk
= mddev
->gendisk
;
6313 struct md_rdev
*rdev
;
6314 bool has_journal
= false;
6315 bool has_readonly
= false;
6317 /* Complain if it has no devices */
6318 if (list_empty(&mddev
->disks
))
6322 if (md_is_rdwr(mddev
))
6326 rdev_for_each_rcu(rdev
, mddev
) {
6327 if (test_bit(Journal
, &rdev
->flags
) &&
6328 !test_bit(Faulty
, &rdev
->flags
))
6330 if (rdev_read_only(rdev
))
6331 has_readonly
= true;
6334 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
) && !has_journal
)
6335 /* Don't restart rw with journal missing/faulty */
6340 mddev
->safemode
= 0;
6341 mddev
->ro
= MD_RDWR
;
6342 set_disk_ro(disk
, 0);
6343 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev
));
6344 /* Kick recovery or resync if necessary */
6345 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6346 md_wakeup_thread(mddev
->sync_thread
);
6347 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6351 static void md_clean(struct mddev
*mddev
)
6353 mddev
->array_sectors
= 0;
6354 mddev
->external_size
= 0;
6355 mddev
->dev_sectors
= 0;
6356 mddev
->raid_disks
= 0;
6357 mddev
->recovery_cp
= 0;
6358 mddev
->resync_min
= 0;
6359 mddev
->resync_max
= MaxSector
;
6360 mddev
->reshape_position
= MaxSector
;
6361 /* we still need mddev->external in export_rdev, do not clear it yet */
6362 mddev
->persistent
= 0;
6363 mddev
->level
= LEVEL_NONE
;
6364 mddev
->clevel
[0] = 0;
6366 * Don't clear MD_CLOSING, or mddev can be opened again.
6367 * 'hold_active != 0' means mddev is still in the creation
6368 * process and will be used later.
6370 if (mddev
->hold_active
)
6373 mddev
->flags
&= BIT_ULL_MASK(MD_CLOSING
);
6374 mddev
->sb_flags
= 0;
6375 mddev
->ro
= MD_RDWR
;
6376 mddev
->metadata_type
[0] = 0;
6377 mddev
->chunk_sectors
= 0;
6378 mddev
->ctime
= mddev
->utime
= 0;
6380 mddev
->max_disks
= 0;
6382 mddev
->can_decrease_events
= 0;
6383 mddev
->delta_disks
= 0;
6384 mddev
->reshape_backwards
= 0;
6385 mddev
->new_level
= LEVEL_NONE
;
6386 mddev
->new_layout
= 0;
6387 mddev
->new_chunk_sectors
= 0;
6388 mddev
->curr_resync
= MD_RESYNC_NONE
;
6389 atomic64_set(&mddev
->resync_mismatches
, 0);
6390 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
6391 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
6392 mddev
->recovery
= 0;
6395 mddev
->degraded
= 0;
6396 mddev
->safemode
= 0;
6397 mddev
->private = NULL
;
6398 mddev
->cluster_info
= NULL
;
6399 mddev
->bitmap_info
.offset
= 0;
6400 mddev
->bitmap_info
.default_offset
= 0;
6401 mddev
->bitmap_info
.default_space
= 0;
6402 mddev
->bitmap_info
.chunksize
= 0;
6403 mddev
->bitmap_info
.daemon_sleep
= 0;
6404 mddev
->bitmap_info
.max_write_behind
= 0;
6405 mddev
->bitmap_info
.nodes
= 0;
6408 static void __md_stop_writes(struct mddev
*mddev
)
6410 del_timer_sync(&mddev
->safemode_timer
);
6412 if (mddev
->pers
&& mddev
->pers
->quiesce
) {
6413 mddev
->pers
->quiesce(mddev
, 1);
6414 mddev
->pers
->quiesce(mddev
, 0);
6417 mddev
->bitmap_ops
->flush(mddev
);
6419 if (md_is_rdwr(mddev
) &&
6420 ((!mddev
->in_sync
&& !mddev_is_clustered(mddev
)) ||
6422 /* mark array as shutdown cleanly */
6423 if (!mddev_is_clustered(mddev
))
6425 md_update_sb(mddev
, 1);
6427 /* disable policy to guarantee rdevs free resources for serialization */
6428 mddev
->serialize_policy
= 0;
6429 mddev_destroy_serial_pool(mddev
, NULL
);
6432 void md_stop_writes(struct mddev
*mddev
)
6434 mddev_lock_nointr(mddev
);
6435 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
6436 stop_sync_thread(mddev
, true);
6437 __md_stop_writes(mddev
);
6438 mddev_unlock(mddev
);
6440 EXPORT_SYMBOL_GPL(md_stop_writes
);
6442 static void mddev_detach(struct mddev
*mddev
)
6444 mddev
->bitmap_ops
->wait_behind_writes(mddev
);
6445 if (mddev
->pers
&& mddev
->pers
->quiesce
&& !is_md_suspended(mddev
)) {
6446 mddev
->pers
->quiesce(mddev
, 1);
6447 mddev
->pers
->quiesce(mddev
, 0);
6449 md_unregister_thread(mddev
, &mddev
->thread
);
6451 /* the unplug fn references 'conf' */
6452 if (!mddev_is_dm(mddev
))
6453 blk_sync_queue(mddev
->gendisk
->queue
);
6456 static void __md_stop(struct mddev
*mddev
)
6458 struct md_personality
*pers
= mddev
->pers
;
6460 mddev
->bitmap_ops
->destroy(mddev
);
6461 mddev_detach(mddev
);
6462 spin_lock(&mddev
->lock
);
6464 spin_unlock(&mddev
->lock
);
6466 pers
->free(mddev
, mddev
->private);
6467 mddev
->private = NULL
;
6468 if (pers
->sync_request
&& mddev
->to_remove
== NULL
)
6469 mddev
->to_remove
= &md_redundancy_group
;
6470 module_put(pers
->owner
);
6471 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
6473 bioset_exit(&mddev
->bio_set
);
6474 bioset_exit(&mddev
->sync_set
);
6475 bioset_exit(&mddev
->io_clone_set
);
6478 void md_stop(struct mddev
*mddev
)
6480 lockdep_assert_held(&mddev
->reconfig_mutex
);
6482 /* stop the array and free an attached data structures.
6483 * This is called from dm-raid
6485 __md_stop_writes(mddev
);
6489 EXPORT_SYMBOL_GPL(md_stop
);
6491 /* ensure 'mddev->pers' exist before calling md_set_readonly() */
6492 static int md_set_readonly(struct mddev
*mddev
)
6497 if (mddev
->external
&& test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
))
6500 if (!test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
)) {
6502 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
6505 stop_sync_thread(mddev
, false);
6506 wait_event(mddev
->sb_wait
,
6507 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
6508 mddev_lock_nointr(mddev
);
6510 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
)) {
6511 pr_warn("md: %s still in use.\n",mdname(mddev
));
6516 __md_stop_writes(mddev
);
6518 if (mddev
->ro
== MD_RDONLY
) {
6523 mddev
->ro
= MD_RDONLY
;
6524 set_disk_ro(mddev
->gendisk
, 1);
6527 if (!err
|| did_freeze
) {
6528 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
6529 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6530 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6537 * 0 - completely stop and dis-assemble array
6538 * 2 - stop but do not disassemble array
6540 static int do_md_stop(struct mddev
*mddev
, int mode
)
6542 struct gendisk
*disk
= mddev
->gendisk
;
6543 struct md_rdev
*rdev
;
6546 if (!test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
)) {
6548 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
6551 stop_sync_thread(mddev
, true);
6553 if (mddev
->sysfs_active
||
6554 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
)) {
6555 pr_warn("md: %s still in use.\n",mdname(mddev
));
6557 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
6558 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6563 if (!md_is_rdwr(mddev
))
6564 set_disk_ro(disk
, 0);
6566 __md_stop_writes(mddev
);
6569 /* tell userspace to handle 'inactive' */
6570 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6572 rdev_for_each(rdev
, mddev
)
6573 if (rdev
->raid_disk
>= 0)
6574 sysfs_unlink_rdev(mddev
, rdev
);
6576 set_capacity_and_notify(disk
, 0);
6579 if (!md_is_rdwr(mddev
))
6580 mddev
->ro
= MD_RDWR
;
6583 * Free resources if final stop
6586 pr_info("md: %s stopped.\n", mdname(mddev
));
6588 if (mddev
->bitmap_info
.file
) {
6589 struct file
*f
= mddev
->bitmap_info
.file
;
6590 spin_lock(&mddev
->lock
);
6591 mddev
->bitmap_info
.file
= NULL
;
6592 spin_unlock(&mddev
->lock
);
6595 mddev
->bitmap_info
.offset
= 0;
6597 export_array(mddev
);
6600 if (mddev
->hold_active
== UNTIL_STOP
)
6601 mddev
->hold_active
= 0;
6604 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6609 static void autorun_array(struct mddev
*mddev
)
6611 struct md_rdev
*rdev
;
6614 if (list_empty(&mddev
->disks
))
6617 pr_info("md: running: ");
6619 rdev_for_each(rdev
, mddev
) {
6620 pr_cont("<%pg>", rdev
->bdev
);
6624 err
= do_md_run(mddev
);
6626 pr_warn("md: do_md_run() returned %d\n", err
);
6627 do_md_stop(mddev
, 0);
6632 * lets try to run arrays based on all disks that have arrived
6633 * until now. (those are in pending_raid_disks)
6635 * the method: pick the first pending disk, collect all disks with
6636 * the same UUID, remove all from the pending list and put them into
6637 * the 'same_array' list. Then order this list based on superblock
6638 * update time (freshest comes first), kick out 'old' disks and
6639 * compare superblocks. If everything's fine then run it.
6641 * If "unit" is allocated, then bump its reference count
6643 static void autorun_devices(int part
)
6645 struct md_rdev
*rdev0
, *rdev
, *tmp
;
6646 struct mddev
*mddev
;
6648 pr_info("md: autorun ...\n");
6649 while (!list_empty(&pending_raid_disks
)) {
6652 LIST_HEAD(candidates
);
6653 rdev0
= list_entry(pending_raid_disks
.next
,
6654 struct md_rdev
, same_set
);
6656 pr_debug("md: considering %pg ...\n", rdev0
->bdev
);
6657 INIT_LIST_HEAD(&candidates
);
6658 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
6659 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
6660 pr_debug("md: adding %pg ...\n",
6662 list_move(&rdev
->same_set
, &candidates
);
6665 * now we have a set of devices, with all of them having
6666 * mostly sane superblocks. It's time to allocate the
6670 dev
= MKDEV(mdp_major
,
6671 rdev0
->preferred_minor
<< MdpMinorShift
);
6672 unit
= MINOR(dev
) >> MdpMinorShift
;
6674 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
6677 if (rdev0
->preferred_minor
!= unit
) {
6678 pr_warn("md: unit number in %pg is bad: %d\n",
6679 rdev0
->bdev
, rdev0
->preferred_minor
);
6683 mddev
= md_alloc(dev
, NULL
);
6687 if (mddev_suspend_and_lock(mddev
))
6688 pr_warn("md: %s locked, cannot run\n", mdname(mddev
));
6689 else if (mddev
->raid_disks
|| mddev
->major_version
6690 || !list_empty(&mddev
->disks
)) {
6691 pr_warn("md: %s already running, cannot run %pg\n",
6692 mdname(mddev
), rdev0
->bdev
);
6693 mddev_unlock_and_resume(mddev
);
6695 pr_debug("md: created %s\n", mdname(mddev
));
6696 mddev
->persistent
= 1;
6697 rdev_for_each_list(rdev
, tmp
, &candidates
) {
6698 list_del_init(&rdev
->same_set
);
6699 if (bind_rdev_to_array(rdev
, mddev
))
6700 export_rdev(rdev
, mddev
);
6702 autorun_array(mddev
);
6703 mddev_unlock_and_resume(mddev
);
6705 /* on success, candidates will be empty, on error
6708 rdev_for_each_list(rdev
, tmp
, &candidates
) {
6709 list_del_init(&rdev
->same_set
);
6710 export_rdev(rdev
, mddev
);
6714 pr_info("md: ... autorun DONE.\n");
6716 #endif /* !MODULE */
6718 static int get_version(void __user
*arg
)
6722 ver
.major
= MD_MAJOR_VERSION
;
6723 ver
.minor
= MD_MINOR_VERSION
;
6724 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
6726 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
6732 static int get_array_info(struct mddev
*mddev
, void __user
*arg
)
6734 mdu_array_info_t info
;
6735 int nr
,working
,insync
,failed
,spare
;
6736 struct md_rdev
*rdev
;
6738 nr
= working
= insync
= failed
= spare
= 0;
6740 rdev_for_each_rcu(rdev
, mddev
) {
6742 if (test_bit(Faulty
, &rdev
->flags
))
6746 if (test_bit(In_sync
, &rdev
->flags
))
6748 else if (test_bit(Journal
, &rdev
->flags
))
6749 /* TODO: add journal count to md_u.h */
6757 info
.major_version
= mddev
->major_version
;
6758 info
.minor_version
= mddev
->minor_version
;
6759 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
6760 info
.ctime
= clamp_t(time64_t
, mddev
->ctime
, 0, U32_MAX
);
6761 info
.level
= mddev
->level
;
6762 info
.size
= mddev
->dev_sectors
/ 2;
6763 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
6766 info
.raid_disks
= mddev
->raid_disks
;
6767 info
.md_minor
= mddev
->md_minor
;
6768 info
.not_persistent
= !mddev
->persistent
;
6770 info
.utime
= clamp_t(time64_t
, mddev
->utime
, 0, U32_MAX
);
6773 info
.state
= (1<<MD_SB_CLEAN
);
6774 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
6775 info
.state
|= (1<<MD_SB_BITMAP_PRESENT
);
6776 if (mddev_is_clustered(mddev
))
6777 info
.state
|= (1<<MD_SB_CLUSTERED
);
6778 info
.active_disks
= insync
;
6779 info
.working_disks
= working
;
6780 info
.failed_disks
= failed
;
6781 info
.spare_disks
= spare
;
6783 info
.layout
= mddev
->layout
;
6784 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
6786 if (copy_to_user(arg
, &info
, sizeof(info
)))
6792 static int get_bitmap_file(struct mddev
*mddev
, void __user
* arg
)
6794 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
6798 file
= kzalloc(sizeof(*file
), GFP_NOIO
);
6803 spin_lock(&mddev
->lock
);
6804 /* bitmap enabled */
6805 if (mddev
->bitmap_info
.file
) {
6806 ptr
= file_path(mddev
->bitmap_info
.file
, file
->pathname
,
6807 sizeof(file
->pathname
));
6811 memmove(file
->pathname
, ptr
,
6812 sizeof(file
->pathname
)-(ptr
-file
->pathname
));
6814 spin_unlock(&mddev
->lock
);
6817 copy_to_user(arg
, file
, sizeof(*file
)))
6824 static int get_disk_info(struct mddev
*mddev
, void __user
* arg
)
6826 mdu_disk_info_t info
;
6827 struct md_rdev
*rdev
;
6829 if (copy_from_user(&info
, arg
, sizeof(info
)))
6833 rdev
= md_find_rdev_nr_rcu(mddev
, info
.number
);
6835 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
6836 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
6837 info
.raid_disk
= rdev
->raid_disk
;
6839 if (test_bit(Faulty
, &rdev
->flags
))
6840 info
.state
|= (1<<MD_DISK_FAULTY
);
6841 else if (test_bit(In_sync
, &rdev
->flags
)) {
6842 info
.state
|= (1<<MD_DISK_ACTIVE
);
6843 info
.state
|= (1<<MD_DISK_SYNC
);
6845 if (test_bit(Journal
, &rdev
->flags
))
6846 info
.state
|= (1<<MD_DISK_JOURNAL
);
6847 if (test_bit(WriteMostly
, &rdev
->flags
))
6848 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
6849 if (test_bit(FailFast
, &rdev
->flags
))
6850 info
.state
|= (1<<MD_DISK_FAILFAST
);
6852 info
.major
= info
.minor
= 0;
6853 info
.raid_disk
= -1;
6854 info
.state
= (1<<MD_DISK_REMOVED
);
6858 if (copy_to_user(arg
, &info
, sizeof(info
)))
6864 int md_add_new_disk(struct mddev
*mddev
, struct mdu_disk_info_s
*info
)
6866 struct md_rdev
*rdev
;
6867 dev_t dev
= MKDEV(info
->major
,info
->minor
);
6869 if (mddev_is_clustered(mddev
) &&
6870 !(info
->state
& ((1 << MD_DISK_CLUSTER_ADD
) | (1 << MD_DISK_CANDIDATE
)))) {
6871 pr_warn("%s: Cannot add to clustered mddev.\n",
6876 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
6879 if (!mddev
->raid_disks
) {
6881 /* expecting a device which has a superblock */
6882 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
6884 pr_warn("md: md_import_device returned %ld\n",
6886 return PTR_ERR(rdev
);
6888 if (!list_empty(&mddev
->disks
)) {
6889 struct md_rdev
*rdev0
6890 = list_entry(mddev
->disks
.next
,
6891 struct md_rdev
, same_set
);
6892 err
= super_types
[mddev
->major_version
]
6893 .load_super(rdev
, rdev0
, mddev
->minor_version
);
6895 pr_warn("md: %pg has different UUID to %pg\n",
6898 export_rdev(rdev
, mddev
);
6902 err
= bind_rdev_to_array(rdev
, mddev
);
6904 export_rdev(rdev
, mddev
);
6909 * md_add_new_disk can be used once the array is assembled
6910 * to add "hot spares". They must already have a superblock
6915 if (!mddev
->pers
->hot_add_disk
) {
6916 pr_warn("%s: personality does not support diskops!\n",
6920 if (mddev
->persistent
)
6921 rdev
= md_import_device(dev
, mddev
->major_version
,
6922 mddev
->minor_version
);
6924 rdev
= md_import_device(dev
, -1, -1);
6926 pr_warn("md: md_import_device returned %ld\n",
6928 return PTR_ERR(rdev
);
6930 /* set saved_raid_disk if appropriate */
6931 if (!mddev
->persistent
) {
6932 if (info
->state
& (1<<MD_DISK_SYNC
) &&
6933 info
->raid_disk
< mddev
->raid_disks
) {
6934 rdev
->raid_disk
= info
->raid_disk
;
6935 clear_bit(Bitmap_sync
, &rdev
->flags
);
6937 rdev
->raid_disk
= -1;
6938 rdev
->saved_raid_disk
= rdev
->raid_disk
;
6940 super_types
[mddev
->major_version
].
6941 validate_super(mddev
, NULL
/*freshest*/, rdev
);
6942 if ((info
->state
& (1<<MD_DISK_SYNC
)) &&
6943 rdev
->raid_disk
!= info
->raid_disk
) {
6944 /* This was a hot-add request, but events doesn't
6945 * match, so reject it.
6947 export_rdev(rdev
, mddev
);
6951 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
6952 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
6953 set_bit(WriteMostly
, &rdev
->flags
);
6955 clear_bit(WriteMostly
, &rdev
->flags
);
6956 if (info
->state
& (1<<MD_DISK_FAILFAST
))
6957 set_bit(FailFast
, &rdev
->flags
);
6959 clear_bit(FailFast
, &rdev
->flags
);
6961 if (info
->state
& (1<<MD_DISK_JOURNAL
)) {
6962 struct md_rdev
*rdev2
;
6963 bool has_journal
= false;
6965 /* make sure no existing journal disk */
6966 rdev_for_each(rdev2
, mddev
) {
6967 if (test_bit(Journal
, &rdev2
->flags
)) {
6972 if (has_journal
|| mddev
->bitmap
) {
6973 export_rdev(rdev
, mddev
);
6976 set_bit(Journal
, &rdev
->flags
);
6979 * check whether the device shows up in other nodes
6981 if (mddev_is_clustered(mddev
)) {
6982 if (info
->state
& (1 << MD_DISK_CANDIDATE
))
6983 set_bit(Candidate
, &rdev
->flags
);
6984 else if (info
->state
& (1 << MD_DISK_CLUSTER_ADD
)) {
6985 /* --add initiated by this node */
6986 err
= md_cluster_ops
->add_new_disk(mddev
, rdev
);
6988 export_rdev(rdev
, mddev
);
6994 rdev
->raid_disk
= -1;
6995 err
= bind_rdev_to_array(rdev
, mddev
);
6998 export_rdev(rdev
, mddev
);
7000 if (mddev_is_clustered(mddev
)) {
7001 if (info
->state
& (1 << MD_DISK_CANDIDATE
)) {
7003 err
= md_cluster_ops
->new_disk_ack(mddev
,
7006 md_kick_rdev_from_array(rdev
);
7010 md_cluster_ops
->add_new_disk_cancel(mddev
);
7012 err
= add_bound_rdev(rdev
);
7016 err
= add_bound_rdev(rdev
);
7021 /* otherwise, md_add_new_disk is only allowed
7022 * for major_version==0 superblocks
7024 if (mddev
->major_version
!= 0) {
7025 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev
));
7029 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
7031 rdev
= md_import_device(dev
, -1, 0);
7033 pr_warn("md: error, md_import_device() returned %ld\n",
7035 return PTR_ERR(rdev
);
7037 rdev
->desc_nr
= info
->number
;
7038 if (info
->raid_disk
< mddev
->raid_disks
)
7039 rdev
->raid_disk
= info
->raid_disk
;
7041 rdev
->raid_disk
= -1;
7043 if (rdev
->raid_disk
< mddev
->raid_disks
)
7044 if (info
->state
& (1<<MD_DISK_SYNC
))
7045 set_bit(In_sync
, &rdev
->flags
);
7047 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
7048 set_bit(WriteMostly
, &rdev
->flags
);
7049 if (info
->state
& (1<<MD_DISK_FAILFAST
))
7050 set_bit(FailFast
, &rdev
->flags
);
7052 if (!mddev
->persistent
) {
7053 pr_debug("md: nonpersistent superblock ...\n");
7054 rdev
->sb_start
= bdev_nr_sectors(rdev
->bdev
);
7056 rdev
->sb_start
= calc_dev_sboffset(rdev
);
7057 rdev
->sectors
= rdev
->sb_start
;
7059 err
= bind_rdev_to_array(rdev
, mddev
);
7061 export_rdev(rdev
, mddev
);
7069 static int hot_remove_disk(struct mddev
*mddev
, dev_t dev
)
7071 struct md_rdev
*rdev
;
7076 rdev
= find_rdev(mddev
, dev
);
7080 if (rdev
->raid_disk
< 0)
7083 clear_bit(Blocked
, &rdev
->flags
);
7084 remove_and_add_spares(mddev
, rdev
);
7086 if (rdev
->raid_disk
>= 0)
7090 if (mddev_is_clustered(mddev
)) {
7091 if (md_cluster_ops
->remove_disk(mddev
, rdev
))
7095 md_kick_rdev_from_array(rdev
);
7096 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
7098 md_update_sb(mddev
, 1);
7103 pr_debug("md: cannot remove active disk %pg from %s ...\n",
7104 rdev
->bdev
, mdname(mddev
));
7108 static int hot_add_disk(struct mddev
*mddev
, dev_t dev
)
7111 struct md_rdev
*rdev
;
7116 if (mddev
->major_version
!= 0) {
7117 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
7121 if (!mddev
->pers
->hot_add_disk
) {
7122 pr_warn("%s: personality does not support diskops!\n",
7127 rdev
= md_import_device(dev
, -1, 0);
7129 pr_warn("md: error, md_import_device() returned %ld\n",
7134 if (mddev
->persistent
)
7135 rdev
->sb_start
= calc_dev_sboffset(rdev
);
7137 rdev
->sb_start
= bdev_nr_sectors(rdev
->bdev
);
7139 rdev
->sectors
= rdev
->sb_start
;
7141 if (test_bit(Faulty
, &rdev
->flags
)) {
7142 pr_warn("md: can not hot-add faulty %pg disk to %s!\n",
7143 rdev
->bdev
, mdname(mddev
));
7148 clear_bit(In_sync
, &rdev
->flags
);
7150 rdev
->saved_raid_disk
= -1;
7151 err
= bind_rdev_to_array(rdev
, mddev
);
7156 * The rest should better be atomic, we can have disk failures
7157 * noticed in interrupt contexts ...
7160 rdev
->raid_disk
= -1;
7162 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
7164 md_update_sb(mddev
, 1);
7166 * Kick recovery, maybe this spare has to be added to the
7167 * array immediately.
7169 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7174 export_rdev(rdev
, mddev
);
7178 static int set_bitmap_file(struct mddev
*mddev
, int fd
)
7183 if (!mddev
->pers
->quiesce
|| !mddev
->thread
)
7185 if (mddev
->recovery
|| mddev
->sync_thread
)
7187 /* we should be able to change the bitmap.. */
7191 struct inode
*inode
;
7194 if (mddev
->bitmap
|| mddev
->bitmap_info
.file
)
7195 return -EEXIST
; /* cannot add when bitmap is present */
7197 if (!IS_ENABLED(CONFIG_MD_BITMAP_FILE
)) {
7198 pr_warn("%s: bitmap files not supported by this kernel\n",
7202 pr_warn("%s: using deprecated bitmap file support\n",
7208 pr_warn("%s: error: failed to get bitmap file\n",
7213 inode
= f
->f_mapping
->host
;
7214 if (!S_ISREG(inode
->i_mode
)) {
7215 pr_warn("%s: error: bitmap file must be a regular file\n",
7218 } else if (!(f
->f_mode
& FMODE_WRITE
)) {
7219 pr_warn("%s: error: bitmap file must open for write\n",
7222 } else if (atomic_read(&inode
->i_writecount
) != 1) {
7223 pr_warn("%s: error: bitmap file is already in use\n",
7231 mddev
->bitmap_info
.file
= f
;
7232 mddev
->bitmap_info
.offset
= 0; /* file overrides offset */
7233 } else if (mddev
->bitmap
== NULL
)
7234 return -ENOENT
; /* cannot remove what isn't there */
7238 err
= mddev
->bitmap_ops
->create(mddev
, -1);
7240 err
= mddev
->bitmap_ops
->load(mddev
);
7243 mddev
->bitmap_ops
->destroy(mddev
);
7246 } else if (fd
< 0) {
7247 mddev
->bitmap_ops
->destroy(mddev
);
7252 struct file
*f
= mddev
->bitmap_info
.file
;
7254 spin_lock(&mddev
->lock
);
7255 mddev
->bitmap_info
.file
= NULL
;
7256 spin_unlock(&mddev
->lock
);
7265 * md_set_array_info is used two different ways
7266 * The original usage is when creating a new array.
7267 * In this usage, raid_disks is > 0 and it together with
7268 * level, size, not_persistent,layout,chunksize determine the
7269 * shape of the array.
7270 * This will always create an array with a type-0.90.0 superblock.
7271 * The newer usage is when assembling an array.
7272 * In this case raid_disks will be 0, and the major_version field is
7273 * use to determine which style super-blocks are to be found on the devices.
7274 * The minor and patch _version numbers are also kept incase the
7275 * super_block handler wishes to interpret them.
7277 int md_set_array_info(struct mddev
*mddev
, struct mdu_array_info_s
*info
)
7279 if (info
->raid_disks
== 0) {
7280 /* just setting version number for superblock loading */
7281 if (info
->major_version
< 0 ||
7282 info
->major_version
>= ARRAY_SIZE(super_types
) ||
7283 super_types
[info
->major_version
].name
== NULL
) {
7284 /* maybe try to auto-load a module? */
7285 pr_warn("md: superblock version %d not known\n",
7286 info
->major_version
);
7289 mddev
->major_version
= info
->major_version
;
7290 mddev
->minor_version
= info
->minor_version
;
7291 mddev
->patch_version
= info
->patch_version
;
7292 mddev
->persistent
= !info
->not_persistent
;
7293 /* ensure mddev_put doesn't delete this now that there
7294 * is some minimal configuration.
7296 mddev
->ctime
= ktime_get_real_seconds();
7299 mddev
->major_version
= MD_MAJOR_VERSION
;
7300 mddev
->minor_version
= MD_MINOR_VERSION
;
7301 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
7302 mddev
->ctime
= ktime_get_real_seconds();
7304 mddev
->level
= info
->level
;
7305 mddev
->clevel
[0] = 0;
7306 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
7307 mddev
->raid_disks
= info
->raid_disks
;
7308 /* don't set md_minor, it is determined by which /dev/md* was
7311 if (info
->state
& (1<<MD_SB_CLEAN
))
7312 mddev
->recovery_cp
= MaxSector
;
7314 mddev
->recovery_cp
= 0;
7315 mddev
->persistent
= ! info
->not_persistent
;
7316 mddev
->external
= 0;
7318 mddev
->layout
= info
->layout
;
7319 if (mddev
->level
== 0)
7320 /* Cannot trust RAID0 layout info here */
7322 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
7324 if (mddev
->persistent
) {
7325 mddev
->max_disks
= MD_SB_DISKS
;
7327 mddev
->sb_flags
= 0;
7329 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
7331 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
7332 mddev
->bitmap_info
.default_space
= 64*2 - (MD_SB_BYTES
>> 9);
7333 mddev
->bitmap_info
.offset
= 0;
7335 mddev
->reshape_position
= MaxSector
;
7338 * Generate a 128 bit UUID
7340 get_random_bytes(mddev
->uuid
, 16);
7342 mddev
->new_level
= mddev
->level
;
7343 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
7344 mddev
->new_layout
= mddev
->layout
;
7345 mddev
->delta_disks
= 0;
7346 mddev
->reshape_backwards
= 0;
7351 void md_set_array_sectors(struct mddev
*mddev
, sector_t array_sectors
)
7353 lockdep_assert_held(&mddev
->reconfig_mutex
);
7355 if (mddev
->external_size
)
7358 mddev
->array_sectors
= array_sectors
;
7360 EXPORT_SYMBOL(md_set_array_sectors
);
7362 static int update_size(struct mddev
*mddev
, sector_t num_sectors
)
7364 struct md_rdev
*rdev
;
7366 int fit
= (num_sectors
== 0);
7367 sector_t old_dev_sectors
= mddev
->dev_sectors
;
7369 if (mddev
->pers
->resize
== NULL
)
7371 /* The "num_sectors" is the number of sectors of each device that
7372 * is used. This can only make sense for arrays with redundancy.
7373 * linear and raid0 always use whatever space is available. We can only
7374 * consider changing this number if no resync or reconstruction is
7375 * happening, and if the new size is acceptable. It must fit before the
7376 * sb_start or, if that is <data_offset, it must fit before the size
7377 * of each device. If num_sectors is zero, we find the largest size
7380 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
7382 if (!md_is_rdwr(mddev
))
7385 rdev_for_each(rdev
, mddev
) {
7386 sector_t avail
= rdev
->sectors
;
7388 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
7389 num_sectors
= avail
;
7390 if (avail
< num_sectors
)
7393 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
7395 if (mddev_is_clustered(mddev
))
7396 md_cluster_ops
->update_size(mddev
, old_dev_sectors
);
7397 else if (!mddev_is_dm(mddev
))
7398 set_capacity_and_notify(mddev
->gendisk
,
7399 mddev
->array_sectors
);
7404 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
)
7407 struct md_rdev
*rdev
;
7408 /* change the number of raid disks */
7409 if (mddev
->pers
->check_reshape
== NULL
)
7411 if (!md_is_rdwr(mddev
))
7413 if (raid_disks
<= 0 ||
7414 (mddev
->max_disks
&& raid_disks
>= mddev
->max_disks
))
7416 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
7417 test_bit(MD_RESYNCING_REMOTE
, &mddev
->recovery
) ||
7418 mddev
->reshape_position
!= MaxSector
)
7421 rdev_for_each(rdev
, mddev
) {
7422 if (mddev
->raid_disks
< raid_disks
&&
7423 rdev
->data_offset
< rdev
->new_data_offset
)
7425 if (mddev
->raid_disks
> raid_disks
&&
7426 rdev
->data_offset
> rdev
->new_data_offset
)
7430 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
7431 if (mddev
->delta_disks
< 0)
7432 mddev
->reshape_backwards
= 1;
7433 else if (mddev
->delta_disks
> 0)
7434 mddev
->reshape_backwards
= 0;
7436 rv
= mddev
->pers
->check_reshape(mddev
);
7438 mddev
->delta_disks
= 0;
7439 mddev
->reshape_backwards
= 0;
7445 * update_array_info is used to change the configuration of an
7447 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
7448 * fields in the info are checked against the array.
7449 * Any differences that cannot be handled will cause an error.
7450 * Normally, only one change can be managed at a time.
7452 static int update_array_info(struct mddev
*mddev
, mdu_array_info_t
*info
)
7458 /* calculate expected state,ignoring low bits */
7459 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
7460 state
|= (1 << MD_SB_BITMAP_PRESENT
);
7462 if (mddev
->major_version
!= info
->major_version
||
7463 mddev
->minor_version
!= info
->minor_version
||
7464 /* mddev->patch_version != info->patch_version || */
7465 mddev
->ctime
!= info
->ctime
||
7466 mddev
->level
!= info
->level
||
7467 /* mddev->layout != info->layout || */
7468 mddev
->persistent
!= !info
->not_persistent
||
7469 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
7470 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
7471 ((state
^info
->state
) & 0xfffffe00)
7474 /* Check there is only one change */
7475 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
7477 if (mddev
->raid_disks
!= info
->raid_disks
)
7479 if (mddev
->layout
!= info
->layout
)
7481 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
7488 if (mddev
->layout
!= info
->layout
) {
7490 * we don't need to do anything at the md level, the
7491 * personality will take care of it all.
7493 if (mddev
->pers
->check_reshape
== NULL
)
7496 mddev
->new_layout
= info
->layout
;
7497 rv
= mddev
->pers
->check_reshape(mddev
);
7499 mddev
->new_layout
= mddev
->layout
;
7503 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
7504 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
7506 if (mddev
->raid_disks
!= info
->raid_disks
)
7507 rv
= update_raid_disks(mddev
, info
->raid_disks
);
7509 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
7510 if (mddev
->pers
->quiesce
== NULL
|| mddev
->thread
== NULL
) {
7514 if (mddev
->recovery
|| mddev
->sync_thread
) {
7518 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
7519 /* add the bitmap */
7520 if (mddev
->bitmap
) {
7524 if (mddev
->bitmap_info
.default_offset
== 0) {
7528 mddev
->bitmap_info
.offset
=
7529 mddev
->bitmap_info
.default_offset
;
7530 mddev
->bitmap_info
.space
=
7531 mddev
->bitmap_info
.default_space
;
7532 rv
= mddev
->bitmap_ops
->create(mddev
, -1);
7534 rv
= mddev
->bitmap_ops
->load(mddev
);
7537 mddev
->bitmap_ops
->destroy(mddev
);
7539 struct md_bitmap_stats stats
;
7541 rv
= mddev
->bitmap_ops
->get_stats(mddev
->bitmap
, &stats
);
7550 if (mddev
->bitmap_info
.nodes
) {
7551 /* hold PW on all the bitmap lock */
7552 if (md_cluster_ops
->lock_all_bitmaps(mddev
) <= 0) {
7553 pr_warn("md: can't change bitmap to none since the array is in use by more than one node\n");
7555 md_cluster_ops
->unlock_all_bitmaps(mddev
);
7559 mddev
->bitmap_info
.nodes
= 0;
7560 md_cluster_ops
->leave(mddev
);
7561 module_put(md_cluster_mod
);
7562 mddev
->safemode_delay
= DEFAULT_SAFEMODE_DELAY
;
7564 mddev
->bitmap_ops
->destroy(mddev
);
7565 mddev
->bitmap_info
.offset
= 0;
7568 md_update_sb(mddev
, 1);
7574 static int set_disk_faulty(struct mddev
*mddev
, dev_t dev
)
7576 struct md_rdev
*rdev
;
7579 if (mddev
->pers
== NULL
)
7583 rdev
= md_find_rdev_rcu(mddev
, dev
);
7587 md_error(mddev
, rdev
);
7588 if (test_bit(MD_BROKEN
, &mddev
->flags
))
7596 * We have a problem here : there is no easy way to give a CHS
7597 * virtual geometry. We currently pretend that we have a 2 heads
7598 * 4 sectors (with a BIG number of cylinders...). This drives
7599 * dosfs just mad... ;-)
7601 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
7603 struct mddev
*mddev
= bdev
->bd_disk
->private_data
;
7607 geo
->cylinders
= mddev
->array_sectors
/ 8;
7611 static inline int md_ioctl_valid(unsigned int cmd
)
7614 case GET_ARRAY_INFO
:
7619 case GET_BITMAP_FILE
:
7621 case HOT_REMOVE_DISK
:
7622 case RESTART_ARRAY_RW
:
7624 case SET_ARRAY_INFO
:
7625 case SET_BITMAP_FILE
:
7626 case SET_DISK_FAULTY
:
7629 case CLUSTERED_DISK_NACK
:
7630 if (!capable(CAP_SYS_ADMIN
))
7638 static bool md_ioctl_need_suspend(unsigned int cmd
)
7643 case HOT_REMOVE_DISK
:
7644 case SET_BITMAP_FILE
:
7645 case SET_ARRAY_INFO
:
7652 static int __md_set_array_info(struct mddev
*mddev
, void __user
*argp
)
7654 mdu_array_info_t info
;
7658 memset(&info
, 0, sizeof(info
));
7659 else if (copy_from_user(&info
, argp
, sizeof(info
)))
7663 err
= update_array_info(mddev
, &info
);
7665 pr_warn("md: couldn't update array info. %d\n", err
);
7669 if (!list_empty(&mddev
->disks
)) {
7670 pr_warn("md: array %s already has disks!\n", mdname(mddev
));
7674 if (mddev
->raid_disks
) {
7675 pr_warn("md: array %s already initialised!\n", mdname(mddev
));
7679 err
= md_set_array_info(mddev
, &info
);
7681 pr_warn("md: couldn't set array info. %d\n", err
);
7686 static int md_ioctl(struct block_device
*bdev
, blk_mode_t mode
,
7687 unsigned int cmd
, unsigned long arg
)
7690 void __user
*argp
= (void __user
*)arg
;
7691 struct mddev
*mddev
= NULL
;
7693 err
= md_ioctl_valid(cmd
);
7698 * Commands dealing with the RAID driver but not any
7701 if (cmd
== RAID_VERSION
)
7702 return get_version(argp
);
7705 * Commands creating/starting a new array:
7708 mddev
= bdev
->bd_disk
->private_data
;
7710 /* Some actions do not requires the mutex */
7712 case GET_ARRAY_INFO
:
7713 if (!mddev
->raid_disks
&& !mddev
->external
)
7715 return get_array_info(mddev
, argp
);
7718 if (!mddev
->raid_disks
&& !mddev
->external
)
7720 return get_disk_info(mddev
, argp
);
7722 case SET_DISK_FAULTY
:
7723 return set_disk_faulty(mddev
, new_decode_dev(arg
));
7725 case GET_BITMAP_FILE
:
7726 return get_bitmap_file(mddev
, argp
);
7729 if (cmd
== STOP_ARRAY
|| cmd
== STOP_ARRAY_RO
) {
7730 /* Need to flush page cache, and ensure no-one else opens
7733 err
= mddev_set_closing_and_sync_blockdev(mddev
, 1);
7738 if (!md_is_rdwr(mddev
))
7739 flush_work(&mddev
->sync_work
);
7741 err
= md_ioctl_need_suspend(cmd
) ? mddev_suspend_and_lock(mddev
) :
7744 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7749 if (cmd
== SET_ARRAY_INFO
) {
7750 err
= __md_set_array_info(mddev
, argp
);
7755 * Commands querying/configuring an existing array:
7757 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
7758 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
7759 if ((!mddev
->raid_disks
&& !mddev
->external
)
7760 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
7761 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
7762 && cmd
!= GET_BITMAP_FILE
) {
7768 * Commands even a read-only array can execute:
7771 case RESTART_ARRAY_RW
:
7772 err
= restart_array(mddev
);
7776 err
= do_md_stop(mddev
, 0);
7781 err
= md_set_readonly(mddev
);
7784 case HOT_REMOVE_DISK
:
7785 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
7789 /* We can support ADD_NEW_DISK on read-only arrays
7790 * only if we are re-adding a preexisting device.
7791 * So require mddev->pers and MD_DISK_SYNC.
7794 mdu_disk_info_t info
;
7795 if (copy_from_user(&info
, argp
, sizeof(info
)))
7797 else if (!(info
.state
& (1<<MD_DISK_SYNC
)))
7798 /* Need to clear read-only for this */
7801 err
= md_add_new_disk(mddev
, &info
);
7808 * The remaining ioctls are changing the state of the
7809 * superblock, so we do not allow them on read-only arrays.
7811 if (!md_is_rdwr(mddev
) && mddev
->pers
) {
7812 if (mddev
->ro
!= MD_AUTO_READ
) {
7816 mddev
->ro
= MD_RDWR
;
7817 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7818 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7819 /* mddev_unlock will wake thread */
7820 /* If a device failed while we were read-only, we
7821 * need to make sure the metadata is updated now.
7823 if (test_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
)) {
7824 mddev_unlock(mddev
);
7825 wait_event(mddev
->sb_wait
,
7826 !test_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
) &&
7827 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
7828 mddev_lock_nointr(mddev
);
7835 mdu_disk_info_t info
;
7836 if (copy_from_user(&info
, argp
, sizeof(info
)))
7839 err
= md_add_new_disk(mddev
, &info
);
7843 case CLUSTERED_DISK_NACK
:
7844 if (mddev_is_clustered(mddev
))
7845 md_cluster_ops
->new_disk_ack(mddev
, false);
7851 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
7855 err
= do_md_run(mddev
);
7858 case SET_BITMAP_FILE
:
7859 err
= set_bitmap_file(mddev
, (int)arg
);
7868 if (mddev
->hold_active
== UNTIL_IOCTL
&&
7870 mddev
->hold_active
= 0;
7872 md_ioctl_need_suspend(cmd
) ? mddev_unlock_and_resume(mddev
) :
7873 mddev_unlock(mddev
);
7876 if (cmd
== STOP_ARRAY_RO
|| (err
&& cmd
== STOP_ARRAY
))
7877 clear_bit(MD_CLOSING
, &mddev
->flags
);
7880 #ifdef CONFIG_COMPAT
7881 static int md_compat_ioctl(struct block_device
*bdev
, blk_mode_t mode
,
7882 unsigned int cmd
, unsigned long arg
)
7885 case HOT_REMOVE_DISK
:
7887 case SET_DISK_FAULTY
:
7888 case SET_BITMAP_FILE
:
7889 /* These take in integer arg, do not convert */
7892 arg
= (unsigned long)compat_ptr(arg
);
7896 return md_ioctl(bdev
, mode
, cmd
, arg
);
7898 #endif /* CONFIG_COMPAT */
7900 static int md_set_read_only(struct block_device
*bdev
, bool ro
)
7902 struct mddev
*mddev
= bdev
->bd_disk
->private_data
;
7905 err
= mddev_lock(mddev
);
7909 if (!mddev
->raid_disks
&& !mddev
->external
) {
7915 * Transitioning to read-auto need only happen for arrays that call
7916 * md_write_start and which are not ready for writes yet.
7918 if (!ro
&& mddev
->ro
== MD_RDONLY
&& mddev
->pers
) {
7919 err
= restart_array(mddev
);
7922 mddev
->ro
= MD_AUTO_READ
;
7926 mddev_unlock(mddev
);
7930 static int md_open(struct gendisk
*disk
, blk_mode_t mode
)
7932 struct mddev
*mddev
;
7935 spin_lock(&all_mddevs_lock
);
7936 mddev
= mddev_get(disk
->private_data
);
7937 spin_unlock(&all_mddevs_lock
);
7941 err
= mutex_lock_interruptible(&mddev
->open_mutex
);
7946 if (test_bit(MD_CLOSING
, &mddev
->flags
))
7949 atomic_inc(&mddev
->openers
);
7950 mutex_unlock(&mddev
->open_mutex
);
7952 disk_check_media_change(disk
);
7956 mutex_unlock(&mddev
->open_mutex
);
7962 static void md_release(struct gendisk
*disk
)
7964 struct mddev
*mddev
= disk
->private_data
;
7967 atomic_dec(&mddev
->openers
);
7971 static unsigned int md_check_events(struct gendisk
*disk
, unsigned int clearing
)
7973 struct mddev
*mddev
= disk
->private_data
;
7974 unsigned int ret
= 0;
7977 ret
= DISK_EVENT_MEDIA_CHANGE
;
7982 static void md_free_disk(struct gendisk
*disk
)
7984 struct mddev
*mddev
= disk
->private_data
;
7989 const struct block_device_operations md_fops
=
7991 .owner
= THIS_MODULE
,
7992 .submit_bio
= md_submit_bio
,
7994 .release
= md_release
,
7996 #ifdef CONFIG_COMPAT
7997 .compat_ioctl
= md_compat_ioctl
,
7999 .getgeo
= md_getgeo
,
8000 .check_events
= md_check_events
,
8001 .set_read_only
= md_set_read_only
,
8002 .free_disk
= md_free_disk
,
8005 static int md_thread(void *arg
)
8007 struct md_thread
*thread
= arg
;
8010 * md_thread is a 'system-thread', it's priority should be very
8011 * high. We avoid resource deadlocks individually in each
8012 * raid personality. (RAID5 does preallocation) We also use RR and
8013 * the very same RT priority as kswapd, thus we will never get
8014 * into a priority inversion deadlock.
8016 * we definitely have to have equal or higher priority than
8017 * bdflush, otherwise bdflush will deadlock if there are too
8018 * many dirty RAID5 blocks.
8021 allow_signal(SIGKILL
);
8022 while (!kthread_should_stop()) {
8024 /* We need to wait INTERRUPTIBLE so that
8025 * we don't add to the load-average.
8026 * That means we need to be sure no signals are
8029 if (signal_pending(current
))
8030 flush_signals(current
);
8032 wait_event_interruptible_timeout
8034 test_bit(THREAD_WAKEUP
, &thread
->flags
)
8035 || kthread_should_stop() || kthread_should_park(),
8038 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
8039 if (kthread_should_park())
8041 if (!kthread_should_stop())
8042 thread
->run(thread
);
8048 static void md_wakeup_thread_directly(struct md_thread __rcu
*thread
)
8050 struct md_thread
*t
;
8053 t
= rcu_dereference(thread
);
8055 wake_up_process(t
->tsk
);
8059 void md_wakeup_thread(struct md_thread __rcu
*thread
)
8061 struct md_thread
*t
;
8064 t
= rcu_dereference(thread
);
8066 pr_debug("md: waking up MD thread %s.\n", t
->tsk
->comm
);
8067 set_bit(THREAD_WAKEUP
, &t
->flags
);
8068 if (wq_has_sleeper(&t
->wqueue
))
8069 wake_up(&t
->wqueue
);
8073 EXPORT_SYMBOL(md_wakeup_thread
);
8075 struct md_thread
*md_register_thread(void (*run
) (struct md_thread
*),
8076 struct mddev
*mddev
, const char *name
)
8078 struct md_thread
*thread
;
8080 thread
= kzalloc(sizeof(struct md_thread
), GFP_KERNEL
);
8084 init_waitqueue_head(&thread
->wqueue
);
8087 thread
->mddev
= mddev
;
8088 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
8089 thread
->tsk
= kthread_run(md_thread
, thread
,
8091 mdname(thread
->mddev
),
8093 if (IS_ERR(thread
->tsk
)) {
8099 EXPORT_SYMBOL(md_register_thread
);
8101 void md_unregister_thread(struct mddev
*mddev
, struct md_thread __rcu
**threadp
)
8103 struct md_thread
*thread
= rcu_dereference_protected(*threadp
,
8104 lockdep_is_held(&mddev
->reconfig_mutex
));
8109 rcu_assign_pointer(*threadp
, NULL
);
8112 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
8113 kthread_stop(thread
->tsk
);
8116 EXPORT_SYMBOL(md_unregister_thread
);
8118 void md_error(struct mddev
*mddev
, struct md_rdev
*rdev
)
8120 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
8123 if (!mddev
->pers
|| !mddev
->pers
->error_handler
)
8125 mddev
->pers
->error_handler(mddev
, rdev
);
8127 if (mddev
->pers
->level
== 0)
8130 if (mddev
->degraded
&& !test_bit(MD_BROKEN
, &mddev
->flags
))
8131 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8132 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
8133 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8134 if (!test_bit(MD_BROKEN
, &mddev
->flags
)) {
8135 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8136 md_wakeup_thread(mddev
->thread
);
8138 if (mddev
->event_work
.func
)
8139 queue_work(md_misc_wq
, &mddev
->event_work
);
8142 EXPORT_SYMBOL(md_error
);
8144 /* seq_file implementation /proc/mdstat */
8146 static void status_unused(struct seq_file
*seq
)
8149 struct md_rdev
*rdev
;
8151 seq_printf(seq
, "unused devices: ");
8153 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
8155 seq_printf(seq
, "%pg ", rdev
->bdev
);
8158 seq_printf(seq
, "<none>");
8160 seq_printf(seq
, "\n");
8163 static void status_personalities(struct seq_file
*seq
)
8165 struct md_personality
*pers
;
8167 seq_puts(seq
, "Personalities : ");
8168 spin_lock(&pers_lock
);
8169 list_for_each_entry(pers
, &pers_list
, list
)
8170 seq_printf(seq
, "[%s] ", pers
->name
);
8172 spin_unlock(&pers_lock
);
8173 seq_puts(seq
, "\n");
8176 static int status_resync(struct seq_file
*seq
, struct mddev
*mddev
)
8178 sector_t max_sectors
, resync
, res
;
8179 unsigned long dt
, db
= 0;
8180 sector_t rt
, curr_mark_cnt
, resync_mark_cnt
;
8181 int scale
, recovery_active
;
8182 unsigned int per_milli
;
8184 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
8185 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
8186 max_sectors
= mddev
->resync_max_sectors
;
8188 max_sectors
= mddev
->dev_sectors
;
8190 resync
= mddev
->curr_resync
;
8191 if (resync
< MD_RESYNC_ACTIVE
) {
8192 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
8193 /* Still cleaning up */
8194 resync
= max_sectors
;
8195 } else if (resync
> max_sectors
) {
8196 resync
= max_sectors
;
8198 res
= atomic_read(&mddev
->recovery_active
);
8200 * Resync has started, but the subtraction has overflowed or
8201 * yielded one of the special values. Force it to active to
8202 * ensure the status reports an active resync.
8204 if (resync
< res
|| resync
- res
< MD_RESYNC_ACTIVE
)
8205 resync
= MD_RESYNC_ACTIVE
;
8210 if (resync
== MD_RESYNC_NONE
) {
8211 if (test_bit(MD_RESYNCING_REMOTE
, &mddev
->recovery
)) {
8212 struct md_rdev
*rdev
;
8214 rdev_for_each(rdev
, mddev
)
8215 if (rdev
->raid_disk
>= 0 &&
8216 !test_bit(Faulty
, &rdev
->flags
) &&
8217 rdev
->recovery_offset
!= MaxSector
&&
8218 rdev
->recovery_offset
) {
8219 seq_printf(seq
, "\trecover=REMOTE");
8222 if (mddev
->reshape_position
!= MaxSector
)
8223 seq_printf(seq
, "\treshape=REMOTE");
8225 seq_printf(seq
, "\tresync=REMOTE");
8228 if (mddev
->recovery_cp
< MaxSector
) {
8229 seq_printf(seq
, "\tresync=PENDING");
8234 if (resync
< MD_RESYNC_ACTIVE
) {
8235 seq_printf(seq
, "\tresync=DELAYED");
8239 WARN_ON(max_sectors
== 0);
8240 /* Pick 'scale' such that (resync>>scale)*1000 will fit
8241 * in a sector_t, and (max_sectors>>scale) will fit in a
8242 * u32, as those are the requirements for sector_div.
8243 * Thus 'scale' must be at least 10
8246 if (sizeof(sector_t
) > sizeof(unsigned long)) {
8247 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
8250 res
= (resync
>>scale
)*1000;
8251 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
8255 int i
, x
= per_milli
/50, y
= 20-x
;
8256 seq_printf(seq
, "[");
8257 for (i
= 0; i
< x
; i
++)
8258 seq_printf(seq
, "=");
8259 seq_printf(seq
, ">");
8260 for (i
= 0; i
< y
; i
++)
8261 seq_printf(seq
, ".");
8262 seq_printf(seq
, "] ");
8264 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
8265 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
8267 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
8269 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
8270 "resync" : "recovery"))),
8271 per_milli
/10, per_milli
% 10,
8272 (unsigned long long) resync
/2,
8273 (unsigned long long) max_sectors
/2);
8276 * dt: time from mark until now
8277 * db: blocks written from mark until now
8278 * rt: remaining time
8280 * rt is a sector_t, which is always 64bit now. We are keeping
8281 * the original algorithm, but it is not really necessary.
8283 * Original algorithm:
8284 * So we divide before multiply in case it is 32bit and close
8286 * We scale the divisor (db) by 32 to avoid losing precision
8287 * near the end of resync when the number of remaining sectors
8289 * We then divide rt by 32 after multiplying by db to compensate.
8290 * The '+1' avoids division by zero if db is very small.
8292 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
8295 curr_mark_cnt
= mddev
->curr_mark_cnt
;
8296 recovery_active
= atomic_read(&mddev
->recovery_active
);
8297 resync_mark_cnt
= mddev
->resync_mark_cnt
;
8299 if (curr_mark_cnt
>= (recovery_active
+ resync_mark_cnt
))
8300 db
= curr_mark_cnt
- (recovery_active
+ resync_mark_cnt
);
8302 rt
= max_sectors
- resync
; /* number of remaining sectors */
8303 rt
= div64_u64(rt
, db
/32+1);
8307 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
8308 ((unsigned long)rt
% 60)/6);
8310 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
8314 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
8315 __acquires(&all_mddevs_lock
)
8317 seq
->poll_event
= atomic_read(&md_event_count
);
8318 spin_lock(&all_mddevs_lock
);
8320 return seq_list_start_head(&all_mddevs
, *pos
);
8323 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
8325 return seq_list_next(v
, &all_mddevs
, pos
);
8328 static void md_seq_stop(struct seq_file
*seq
, void *v
)
8329 __releases(&all_mddevs_lock
)
8331 spin_unlock(&all_mddevs_lock
);
8334 static void md_bitmap_status(struct seq_file
*seq
, struct mddev
*mddev
)
8336 struct md_bitmap_stats stats
;
8337 unsigned long used_pages
;
8338 unsigned long chunk_kb
;
8341 err
= mddev
->bitmap_ops
->get_stats(mddev
->bitmap
, &stats
);
8345 chunk_kb
= mddev
->bitmap_info
.chunksize
>> 10;
8346 used_pages
= stats
.pages
- stats
.missing_pages
;
8348 seq_printf(seq
, "bitmap: %lu/%lu pages [%luKB], %lu%s chunk",
8349 used_pages
, stats
.pages
, used_pages
<< (PAGE_SHIFT
- 10),
8350 chunk_kb
? chunk_kb
: mddev
->bitmap_info
.chunksize
,
8351 chunk_kb
? "KB" : "B");
8354 seq_puts(seq
, ", file: ");
8355 seq_file_path(seq
, stats
.file
, " \t\n");
8358 seq_putc(seq
, '\n');
8361 static int md_seq_show(struct seq_file
*seq
, void *v
)
8363 struct mddev
*mddev
;
8365 struct md_rdev
*rdev
;
8367 if (v
== &all_mddevs
) {
8368 status_personalities(seq
);
8369 if (list_empty(&all_mddevs
))
8374 mddev
= list_entry(v
, struct mddev
, all_mddevs
);
8375 if (!mddev_get(mddev
))
8378 spin_unlock(&all_mddevs_lock
);
8379 spin_lock(&mddev
->lock
);
8380 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
8381 seq_printf(seq
, "%s : ", mdname(mddev
));
8383 if (test_bit(MD_BROKEN
, &mddev
->flags
))
8384 seq_printf(seq
, "broken");
8386 seq_printf(seq
, "active");
8387 if (mddev
->ro
== MD_RDONLY
)
8388 seq_printf(seq
, " (read-only)");
8389 if (mddev
->ro
== MD_AUTO_READ
)
8390 seq_printf(seq
, " (auto-read-only)");
8391 seq_printf(seq
, " %s", mddev
->pers
->name
);
8393 seq_printf(seq
, "inactive");
8398 rdev_for_each_rcu(rdev
, mddev
) {
8399 seq_printf(seq
, " %pg[%d]", rdev
->bdev
, rdev
->desc_nr
);
8401 if (test_bit(WriteMostly
, &rdev
->flags
))
8402 seq_printf(seq
, "(W)");
8403 if (test_bit(Journal
, &rdev
->flags
))
8404 seq_printf(seq
, "(J)");
8405 if (test_bit(Faulty
, &rdev
->flags
)) {
8406 seq_printf(seq
, "(F)");
8409 if (rdev
->raid_disk
< 0)
8410 seq_printf(seq
, "(S)"); /* spare */
8411 if (test_bit(Replacement
, &rdev
->flags
))
8412 seq_printf(seq
, "(R)");
8413 sectors
+= rdev
->sectors
;
8417 if (!list_empty(&mddev
->disks
)) {
8419 seq_printf(seq
, "\n %llu blocks",
8420 (unsigned long long)
8421 mddev
->array_sectors
/ 2);
8423 seq_printf(seq
, "\n %llu blocks",
8424 (unsigned long long)sectors
/ 2);
8426 if (mddev
->persistent
) {
8427 if (mddev
->major_version
!= 0 ||
8428 mddev
->minor_version
!= 90) {
8429 seq_printf(seq
," super %d.%d",
8430 mddev
->major_version
,
8431 mddev
->minor_version
);
8433 } else if (mddev
->external
)
8434 seq_printf(seq
, " super external:%s",
8435 mddev
->metadata_type
);
8437 seq_printf(seq
, " super non-persistent");
8440 mddev
->pers
->status(seq
, mddev
);
8441 seq_printf(seq
, "\n ");
8442 if (mddev
->pers
->sync_request
) {
8443 if (status_resync(seq
, mddev
))
8444 seq_printf(seq
, "\n ");
8447 seq_printf(seq
, "\n ");
8449 md_bitmap_status(seq
, mddev
);
8451 seq_printf(seq
, "\n");
8453 spin_unlock(&mddev
->lock
);
8454 spin_lock(&all_mddevs_lock
);
8456 if (mddev
== list_last_entry(&all_mddevs
, struct mddev
, all_mddevs
))
8459 if (atomic_dec_and_test(&mddev
->active
))
8465 static const struct seq_operations md_seq_ops
= {
8466 .start
= md_seq_start
,
8467 .next
= md_seq_next
,
8468 .stop
= md_seq_stop
,
8469 .show
= md_seq_show
,
8472 static int md_seq_open(struct inode
*inode
, struct file
*file
)
8474 struct seq_file
*seq
;
8477 error
= seq_open(file
, &md_seq_ops
);
8481 seq
= file
->private_data
;
8482 seq
->poll_event
= atomic_read(&md_event_count
);
8486 static int md_unloading
;
8487 static __poll_t
mdstat_poll(struct file
*filp
, poll_table
*wait
)
8489 struct seq_file
*seq
= filp
->private_data
;
8493 return EPOLLIN
|EPOLLRDNORM
|EPOLLERR
|EPOLLPRI
;
8494 poll_wait(filp
, &md_event_waiters
, wait
);
8496 /* always allow read */
8497 mask
= EPOLLIN
| EPOLLRDNORM
;
8499 if (seq
->poll_event
!= atomic_read(&md_event_count
))
8500 mask
|= EPOLLERR
| EPOLLPRI
;
8504 static const struct proc_ops mdstat_proc_ops
= {
8505 .proc_open
= md_seq_open
,
8506 .proc_read
= seq_read
,
8507 .proc_lseek
= seq_lseek
,
8508 .proc_release
= seq_release
,
8509 .proc_poll
= mdstat_poll
,
8512 int register_md_personality(struct md_personality
*p
)
8514 pr_debug("md: %s personality registered for level %d\n",
8516 spin_lock(&pers_lock
);
8517 list_add_tail(&p
->list
, &pers_list
);
8518 spin_unlock(&pers_lock
);
8521 EXPORT_SYMBOL(register_md_personality
);
8523 int unregister_md_personality(struct md_personality
*p
)
8525 pr_debug("md: %s personality unregistered\n", p
->name
);
8526 spin_lock(&pers_lock
);
8527 list_del_init(&p
->list
);
8528 spin_unlock(&pers_lock
);
8531 EXPORT_SYMBOL(unregister_md_personality
);
8533 int register_md_cluster_operations(const struct md_cluster_operations
*ops
,
8534 struct module
*module
)
8537 spin_lock(&pers_lock
);
8538 if (md_cluster_ops
!= NULL
)
8541 md_cluster_ops
= ops
;
8542 md_cluster_mod
= module
;
8544 spin_unlock(&pers_lock
);
8547 EXPORT_SYMBOL(register_md_cluster_operations
);
8549 int unregister_md_cluster_operations(void)
8551 spin_lock(&pers_lock
);
8552 md_cluster_ops
= NULL
;
8553 spin_unlock(&pers_lock
);
8556 EXPORT_SYMBOL(unregister_md_cluster_operations
);
8558 int md_setup_cluster(struct mddev
*mddev
, int nodes
)
8561 if (!md_cluster_ops
)
8562 request_module("md-cluster");
8563 spin_lock(&pers_lock
);
8564 /* ensure module won't be unloaded */
8565 if (!md_cluster_ops
|| !try_module_get(md_cluster_mod
)) {
8566 pr_warn("can't find md-cluster module or get its reference.\n");
8567 spin_unlock(&pers_lock
);
8570 spin_unlock(&pers_lock
);
8572 ret
= md_cluster_ops
->join(mddev
, nodes
);
8574 mddev
->safemode_delay
= 0;
8578 void md_cluster_stop(struct mddev
*mddev
)
8580 if (!md_cluster_ops
)
8582 md_cluster_ops
->leave(mddev
);
8583 module_put(md_cluster_mod
);
8586 static int is_mddev_idle(struct mddev
*mddev
, int init
)
8588 struct md_rdev
*rdev
;
8594 rdev_for_each_rcu(rdev
, mddev
) {
8595 struct gendisk
*disk
= rdev
->bdev
->bd_disk
;
8597 if (!init
&& !blk_queue_io_stat(disk
->queue
))
8600 curr_events
= (int)part_stat_read_accum(disk
->part0
, sectors
) -
8601 atomic_read(&disk
->sync_io
);
8602 /* sync IO will cause sync_io to increase before the disk_stats
8603 * as sync_io is counted when a request starts, and
8604 * disk_stats is counted when it completes.
8605 * So resync activity will cause curr_events to be smaller than
8606 * when there was no such activity.
8607 * non-sync IO will cause disk_stat to increase without
8608 * increasing sync_io so curr_events will (eventually)
8609 * be larger than it was before. Once it becomes
8610 * substantially larger, the test below will cause
8611 * the array to appear non-idle, and resync will slow
8613 * If there is a lot of outstanding resync activity when
8614 * we set last_event to curr_events, then all that activity
8615 * completing might cause the array to appear non-idle
8616 * and resync will be slowed down even though there might
8617 * not have been non-resync activity. This will only
8618 * happen once though. 'last_events' will soon reflect
8619 * the state where there is little or no outstanding
8620 * resync requests, and further resync activity will
8621 * always make curr_events less than last_events.
8624 if (init
|| curr_events
- rdev
->last_events
> 64) {
8625 rdev
->last_events
= curr_events
;
8633 void md_done_sync(struct mddev
*mddev
, int blocks
, int ok
)
8635 /* another "blocks" (512byte) blocks have been synced */
8636 atomic_sub(blocks
, &mddev
->recovery_active
);
8637 wake_up(&mddev
->recovery_wait
);
8639 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8640 set_bit(MD_RECOVERY_ERROR
, &mddev
->recovery
);
8641 md_wakeup_thread(mddev
->thread
);
8642 // stop recovery, signal do_sync ....
8645 EXPORT_SYMBOL(md_done_sync
);
8647 /* md_write_start(mddev, bi)
8648 * If we need to update some array metadata (e.g. 'active' flag
8649 * in superblock) before writing, schedule a superblock update
8650 * and wait for it to complete.
8651 * A return value of 'false' means that the write wasn't recorded
8652 * and cannot proceed as the array is being suspend.
8654 void md_write_start(struct mddev
*mddev
, struct bio
*bi
)
8658 if (bio_data_dir(bi
) != WRITE
)
8661 BUG_ON(mddev
->ro
== MD_RDONLY
);
8662 if (mddev
->ro
== MD_AUTO_READ
) {
8663 /* need to switch to read/write */
8664 mddev
->ro
= MD_RDWR
;
8665 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8666 md_wakeup_thread(mddev
->thread
);
8667 md_wakeup_thread(mddev
->sync_thread
);
8671 percpu_ref_get(&mddev
->writes_pending
);
8672 smp_mb(); /* Match smp_mb in set_in_sync() */
8673 if (mddev
->safemode
== 1)
8674 mddev
->safemode
= 0;
8675 /* sync_checkers is always 0 when writes_pending is in per-cpu mode */
8676 if (mddev
->in_sync
|| mddev
->sync_checkers
) {
8677 spin_lock(&mddev
->lock
);
8678 if (mddev
->in_sync
) {
8680 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
8681 set_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
8682 md_wakeup_thread(mddev
->thread
);
8685 spin_unlock(&mddev
->lock
);
8689 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
8690 if (!mddev
->has_superblocks
)
8692 wait_event(mddev
->sb_wait
,
8693 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
8695 EXPORT_SYMBOL(md_write_start
);
8697 /* md_write_inc can only be called when md_write_start() has
8698 * already been called at least once of the current request.
8699 * It increments the counter and is useful when a single request
8700 * is split into several parts. Each part causes an increment and
8701 * so needs a matching md_write_end().
8702 * Unlike md_write_start(), it is safe to call md_write_inc() inside
8703 * a spinlocked region.
8705 void md_write_inc(struct mddev
*mddev
, struct bio
*bi
)
8707 if (bio_data_dir(bi
) != WRITE
)
8709 WARN_ON_ONCE(mddev
->in_sync
|| !md_is_rdwr(mddev
));
8710 percpu_ref_get(&mddev
->writes_pending
);
8712 EXPORT_SYMBOL(md_write_inc
);
8714 void md_write_end(struct mddev
*mddev
)
8716 percpu_ref_put(&mddev
->writes_pending
);
8718 if (mddev
->safemode
== 2)
8719 md_wakeup_thread(mddev
->thread
);
8720 else if (mddev
->safemode_delay
)
8721 /* The roundup() ensures this only performs locking once
8722 * every ->safemode_delay jiffies
8724 mod_timer(&mddev
->safemode_timer
,
8725 roundup(jiffies
, mddev
->safemode_delay
) +
8726 mddev
->safemode_delay
);
8729 EXPORT_SYMBOL(md_write_end
);
8731 /* This is used by raid0 and raid10 */
8732 void md_submit_discard_bio(struct mddev
*mddev
, struct md_rdev
*rdev
,
8733 struct bio
*bio
, sector_t start
, sector_t size
)
8735 struct bio
*discard_bio
= NULL
;
8737 if (__blkdev_issue_discard(rdev
->bdev
, start
, size
, GFP_NOIO
,
8738 &discard_bio
) || !discard_bio
)
8741 bio_chain(discard_bio
, bio
);
8742 bio_clone_blkg_association(discard_bio
, bio
);
8743 mddev_trace_remap(mddev
, discard_bio
, bio
->bi_iter
.bi_sector
);
8744 submit_bio_noacct(discard_bio
);
8746 EXPORT_SYMBOL_GPL(md_submit_discard_bio
);
8748 static void md_end_clone_io(struct bio
*bio
)
8750 struct md_io_clone
*md_io_clone
= bio
->bi_private
;
8751 struct bio
*orig_bio
= md_io_clone
->orig_bio
;
8752 struct mddev
*mddev
= md_io_clone
->mddev
;
8754 if (bio
->bi_status
&& !orig_bio
->bi_status
)
8755 orig_bio
->bi_status
= bio
->bi_status
;
8757 if (md_io_clone
->start_time
)
8758 bio_end_io_acct(orig_bio
, md_io_clone
->start_time
);
8761 bio_endio(orig_bio
);
8762 percpu_ref_put(&mddev
->active_io
);
8765 static void md_clone_bio(struct mddev
*mddev
, struct bio
**bio
)
8767 struct block_device
*bdev
= (*bio
)->bi_bdev
;
8768 struct md_io_clone
*md_io_clone
;
8770 bio_alloc_clone(bdev
, *bio
, GFP_NOIO
, &mddev
->io_clone_set
);
8772 md_io_clone
= container_of(clone
, struct md_io_clone
, bio_clone
);
8773 md_io_clone
->orig_bio
= *bio
;
8774 md_io_clone
->mddev
= mddev
;
8775 if (blk_queue_io_stat(bdev
->bd_disk
->queue
))
8776 md_io_clone
->start_time
= bio_start_io_acct(*bio
);
8778 clone
->bi_end_io
= md_end_clone_io
;
8779 clone
->bi_private
= md_io_clone
;
8783 void md_account_bio(struct mddev
*mddev
, struct bio
**bio
)
8785 percpu_ref_get(&mddev
->active_io
);
8786 md_clone_bio(mddev
, bio
);
8788 EXPORT_SYMBOL_GPL(md_account_bio
);
8790 void md_free_cloned_bio(struct bio
*bio
)
8792 struct md_io_clone
*md_io_clone
= bio
->bi_private
;
8793 struct bio
*orig_bio
= md_io_clone
->orig_bio
;
8794 struct mddev
*mddev
= md_io_clone
->mddev
;
8796 if (bio
->bi_status
&& !orig_bio
->bi_status
)
8797 orig_bio
->bi_status
= bio
->bi_status
;
8799 if (md_io_clone
->start_time
)
8800 bio_end_io_acct(orig_bio
, md_io_clone
->start_time
);
8803 percpu_ref_put(&mddev
->active_io
);
8805 EXPORT_SYMBOL_GPL(md_free_cloned_bio
);
8807 /* md_allow_write(mddev)
8808 * Calling this ensures that the array is marked 'active' so that writes
8809 * may proceed without blocking. It is important to call this before
8810 * attempting a GFP_KERNEL allocation while holding the mddev lock.
8811 * Must be called with mddev_lock held.
8813 void md_allow_write(struct mddev
*mddev
)
8817 if (!md_is_rdwr(mddev
))
8819 if (!mddev
->pers
->sync_request
)
8822 spin_lock(&mddev
->lock
);
8823 if (mddev
->in_sync
) {
8825 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
8826 set_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
8827 if (mddev
->safemode_delay
&&
8828 mddev
->safemode
== 0)
8829 mddev
->safemode
= 1;
8830 spin_unlock(&mddev
->lock
);
8831 md_update_sb(mddev
, 0);
8832 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
8833 /* wait for the dirty state to be recorded in the metadata */
8834 wait_event(mddev
->sb_wait
,
8835 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
8837 spin_unlock(&mddev
->lock
);
8839 EXPORT_SYMBOL_GPL(md_allow_write
);
8841 static sector_t
md_sync_max_sectors(struct mddev
*mddev
,
8842 enum sync_action action
)
8848 atomic64_set(&mddev
->resync_mismatches
, 0);
8850 case ACTION_RESHAPE
:
8851 return mddev
->resync_max_sectors
;
8852 case ACTION_RECOVER
:
8853 return mddev
->dev_sectors
;
8859 static sector_t
md_sync_position(struct mddev
*mddev
, enum sync_action action
)
8862 struct md_rdev
*rdev
;
8867 return mddev
->resync_min
;
8870 return mddev
->recovery_cp
;
8872 case ACTION_RESHAPE
:
8874 * If the original node aborts reshaping then we continue the
8875 * reshaping, so set again to avoid restart reshape from the
8878 if (mddev_is_clustered(mddev
) &&
8879 mddev
->reshape_position
!= MaxSector
)
8880 return mddev
->reshape_position
;
8882 case ACTION_RECOVER
:
8885 rdev_for_each_rcu(rdev
, mddev
)
8886 if (rdev
->raid_disk
>= 0 &&
8887 !test_bit(Journal
, &rdev
->flags
) &&
8888 !test_bit(Faulty
, &rdev
->flags
) &&
8889 !test_bit(In_sync
, &rdev
->flags
) &&
8890 rdev
->recovery_offset
< start
)
8891 start
= rdev
->recovery_offset
;
8894 /* If there is a bitmap, we need to make sure all
8895 * writes that started before we added a spare
8896 * complete before we start doing a recovery.
8897 * Otherwise the write might complete and (via
8898 * bitmap_endwrite) set a bit in the bitmap after the
8899 * recovery has checked that bit and skipped that
8902 if (mddev
->bitmap
) {
8903 mddev
->pers
->quiesce(mddev
, 1);
8904 mddev
->pers
->quiesce(mddev
, 0);
8912 #define SYNC_MARKS 10
8913 #define SYNC_MARK_STEP (3*HZ)
8914 #define UPDATE_FREQUENCY (5*60*HZ)
8915 void md_do_sync(struct md_thread
*thread
)
8917 struct mddev
*mddev
= thread
->mddev
;
8918 struct mddev
*mddev2
;
8919 unsigned int currspeed
= 0, window
;
8920 sector_t max_sectors
,j
, io_sectors
, recovery_done
;
8921 unsigned long mark
[SYNC_MARKS
];
8922 unsigned long update_time
;
8923 sector_t mark_cnt
[SYNC_MARKS
];
8925 sector_t last_check
;
8927 struct md_rdev
*rdev
;
8928 enum sync_action action
;
8930 struct blk_plug plug
;
8933 /* just incase thread restarts... */
8934 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
8937 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8940 if (test_bit(MD_RECOVERY_WAIT
, &mddev
->recovery
) ||
8941 !md_is_rdwr(mddev
)) {/* never try to sync a read-only array */
8942 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8946 if (mddev_is_clustered(mddev
)) {
8947 ret
= md_cluster_ops
->resync_start(mddev
);
8951 set_bit(MD_CLUSTER_RESYNC_LOCKED
, &mddev
->flags
);
8952 if (!(test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
8953 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) ||
8954 test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
8955 && ((unsigned long long)mddev
->curr_resync_completed
8956 < (unsigned long long)mddev
->resync_max_sectors
))
8960 action
= md_sync_action(mddev
);
8961 desc
= md_sync_action_name(action
);
8962 mddev
->last_sync_action
= action
;
8965 * Before starting a resync we must have set curr_resync to
8966 * 2, and then checked that every "conflicting" array has curr_resync
8967 * less than ours. When we find one that is the same or higher
8968 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8969 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8970 * This will mean we have to start checking from the beginning again.
8973 if (mddev_is_clustered(mddev
))
8974 md_cluster_ops
->resync_start_notify(mddev
);
8976 int mddev2_minor
= -1;
8977 mddev
->curr_resync
= MD_RESYNC_DELAYED
;
8980 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8982 spin_lock(&all_mddevs_lock
);
8983 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
) {
8984 if (test_bit(MD_DELETED
, &mddev2
->flags
))
8986 if (mddev2
== mddev
)
8988 if (!mddev
->parallel_resync
8989 && mddev2
->curr_resync
8990 && match_mddev_units(mddev
, mddev2
)) {
8992 if (mddev
< mddev2
&&
8993 mddev
->curr_resync
== MD_RESYNC_DELAYED
) {
8994 /* arbitrarily yield */
8995 mddev
->curr_resync
= MD_RESYNC_YIELDED
;
8996 wake_up(&resync_wait
);
8998 if (mddev
> mddev2
&&
8999 mddev
->curr_resync
== MD_RESYNC_YIELDED
)
9000 /* no need to wait here, we can wait the next
9001 * time 'round when curr_resync == 2
9004 /* We need to wait 'interruptible' so as not to
9005 * contribute to the load average, and not to
9006 * be caught by 'softlockup'
9008 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
9009 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
9010 mddev2
->curr_resync
>= mddev
->curr_resync
) {
9011 if (mddev2_minor
!= mddev2
->md_minor
) {
9012 mddev2_minor
= mddev2
->md_minor
;
9013 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
9014 desc
, mdname(mddev
),
9017 spin_unlock(&all_mddevs_lock
);
9019 if (signal_pending(current
))
9020 flush_signals(current
);
9022 finish_wait(&resync_wait
, &wq
);
9025 finish_wait(&resync_wait
, &wq
);
9028 spin_unlock(&all_mddevs_lock
);
9029 } while (mddev
->curr_resync
< MD_RESYNC_DELAYED
);
9031 max_sectors
= md_sync_max_sectors(mddev
, action
);
9032 j
= md_sync_position(mddev
, action
);
9034 pr_info("md: %s of RAID array %s\n", desc
, mdname(mddev
));
9035 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev
));
9036 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
9037 speed_max(mddev
), desc
);
9039 is_mddev_idle(mddev
, 1); /* this initializes IO event counters */
9042 for (m
= 0; m
< SYNC_MARKS
; m
++) {
9044 mark_cnt
[m
] = io_sectors
;
9047 mddev
->resync_mark
= mark
[last_mark
];
9048 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
9051 * Tune reconstruction:
9053 window
= 32 * (PAGE_SIZE
/ 512);
9054 pr_debug("md: using %dk window, over a total of %lluk.\n",
9055 window
/2, (unsigned long long)max_sectors
/2);
9057 atomic_set(&mddev
->recovery_active
, 0);
9060 if (j
>= MD_RESYNC_ACTIVE
) {
9061 pr_debug("md: resuming %s of %s from checkpoint.\n",
9062 desc
, mdname(mddev
));
9063 mddev
->curr_resync
= j
;
9065 mddev
->curr_resync
= MD_RESYNC_ACTIVE
; /* no longer delayed */
9066 mddev
->curr_resync_completed
= j
;
9067 sysfs_notify_dirent_safe(mddev
->sysfs_completed
);
9069 update_time
= jiffies
;
9071 blk_start_plug(&plug
);
9072 while (j
< max_sectors
) {
9077 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
9078 ((mddev
->curr_resync
> mddev
->curr_resync_completed
&&
9079 (mddev
->curr_resync
- mddev
->curr_resync_completed
)
9080 > (max_sectors
>> 4)) ||
9081 time_after_eq(jiffies
, update_time
+ UPDATE_FREQUENCY
) ||
9082 (j
- mddev
->curr_resync_completed
)*2
9083 >= mddev
->resync_max
- mddev
->curr_resync_completed
||
9084 mddev
->curr_resync_completed
> mddev
->resync_max
9086 /* time to update curr_resync_completed */
9087 wait_event(mddev
->recovery_wait
,
9088 atomic_read(&mddev
->recovery_active
) == 0);
9089 mddev
->curr_resync_completed
= j
;
9090 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) &&
9091 j
> mddev
->recovery_cp
)
9092 mddev
->recovery_cp
= j
;
9093 update_time
= jiffies
;
9094 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
9095 sysfs_notify_dirent_safe(mddev
->sysfs_completed
);
9098 while (j
>= mddev
->resync_max
&&
9099 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
9100 /* As this condition is controlled by user-space,
9101 * we can block indefinitely, so use '_interruptible'
9102 * to avoid triggering warnings.
9104 flush_signals(current
); /* just in case */
9105 wait_event_interruptible(mddev
->recovery_wait
,
9106 mddev
->resync_max
> j
9107 || test_bit(MD_RECOVERY_INTR
,
9111 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
9114 sectors
= mddev
->pers
->sync_request(mddev
, j
, max_sectors
,
9117 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
9121 if (!skipped
) { /* actual IO requested */
9122 io_sectors
+= sectors
;
9123 atomic_add(sectors
, &mddev
->recovery_active
);
9126 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
9130 if (j
> max_sectors
)
9131 /* when skipping, extra large numbers can be returned. */
9133 if (j
>= MD_RESYNC_ACTIVE
)
9134 mddev
->curr_resync
= j
;
9135 mddev
->curr_mark_cnt
= io_sectors
;
9136 if (last_check
== 0)
9137 /* this is the earliest that rebuild will be
9138 * visible in /proc/mdstat
9142 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
9145 last_check
= io_sectors
;
9147 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
9149 int next
= (last_mark
+1) % SYNC_MARKS
;
9151 mddev
->resync_mark
= mark
[next
];
9152 mddev
->resync_mark_cnt
= mark_cnt
[next
];
9153 mark
[next
] = jiffies
;
9154 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
9158 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
9162 * this loop exits only if either when we are slower than
9163 * the 'hard' speed limit, or the system was IO-idle for
9165 * the system might be non-idle CPU-wise, but we only care
9166 * about not overloading the IO subsystem. (things like an
9167 * e2fsck being done on the RAID array should execute fast)
9171 recovery_done
= io_sectors
- atomic_read(&mddev
->recovery_active
);
9172 currspeed
= ((unsigned long)(recovery_done
- mddev
->resync_mark_cnt
))/2
9173 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
9175 if (currspeed
> speed_min(mddev
)) {
9176 if (currspeed
> speed_max(mddev
)) {
9180 if (!is_mddev_idle(mddev
, 0)) {
9182 * Give other IO more of a chance.
9183 * The faster the devices, the less we wait.
9185 wait_event(mddev
->recovery_wait
,
9186 !atomic_read(&mddev
->recovery_active
));
9190 pr_info("md: %s: %s %s.\n",mdname(mddev
), desc
,
9191 test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)
9192 ? "interrupted" : "done");
9194 * this also signals 'finished resyncing' to md_stop
9196 blk_finish_plug(&plug
);
9197 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
9199 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
9200 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
9201 mddev
->curr_resync
>= MD_RESYNC_ACTIVE
) {
9202 mddev
->curr_resync_completed
= mddev
->curr_resync
;
9203 sysfs_notify_dirent_safe(mddev
->sysfs_completed
);
9205 mddev
->pers
->sync_request(mddev
, max_sectors
, max_sectors
, &skipped
);
9207 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
9208 mddev
->curr_resync
> MD_RESYNC_ACTIVE
) {
9209 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
9210 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
9211 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
9212 pr_debug("md: checkpointing %s of %s.\n",
9213 desc
, mdname(mddev
));
9214 if (test_bit(MD_RECOVERY_ERROR
,
9216 mddev
->recovery_cp
=
9217 mddev
->curr_resync_completed
;
9219 mddev
->recovery_cp
=
9223 mddev
->recovery_cp
= MaxSector
;
9225 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
9226 mddev
->curr_resync
= MaxSector
;
9227 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
9228 test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
)) {
9230 rdev_for_each_rcu(rdev
, mddev
)
9231 if (rdev
->raid_disk
>= 0 &&
9232 mddev
->delta_disks
>= 0 &&
9233 !test_bit(Journal
, &rdev
->flags
) &&
9234 !test_bit(Faulty
, &rdev
->flags
) &&
9235 !test_bit(In_sync
, &rdev
->flags
) &&
9236 rdev
->recovery_offset
< mddev
->curr_resync
)
9237 rdev
->recovery_offset
= mddev
->curr_resync
;
9243 /* set CHANGE_PENDING here since maybe another update is needed,
9244 * so other nodes are informed. It should be harmless for normal
9246 set_mask_bits(&mddev
->sb_flags
, 0,
9247 BIT(MD_SB_CHANGE_PENDING
) | BIT(MD_SB_CHANGE_DEVS
));
9249 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
9250 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
9251 mddev
->delta_disks
> 0 &&
9252 mddev
->pers
->finish_reshape
&&
9253 mddev
->pers
->size
&&
9254 !mddev_is_dm(mddev
)) {
9255 mddev_lock_nointr(mddev
);
9256 md_set_array_sectors(mddev
, mddev
->pers
->size(mddev
, 0, 0));
9257 mddev_unlock(mddev
);
9258 if (!mddev_is_clustered(mddev
))
9259 set_capacity_and_notify(mddev
->gendisk
,
9260 mddev
->array_sectors
);
9263 spin_lock(&mddev
->lock
);
9264 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
9265 /* We completed so min/max setting can be forgotten if used. */
9266 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
9267 mddev
->resync_min
= 0;
9268 mddev
->resync_max
= MaxSector
;
9269 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
9270 mddev
->resync_min
= mddev
->curr_resync_completed
;
9271 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
9272 mddev
->curr_resync
= MD_RESYNC_NONE
;
9273 spin_unlock(&mddev
->lock
);
9275 wake_up(&resync_wait
);
9276 md_wakeup_thread(mddev
->thread
);
9279 EXPORT_SYMBOL_GPL(md_do_sync
);
9281 static bool rdev_removeable(struct md_rdev
*rdev
)
9283 /* rdev is not used. */
9284 if (rdev
->raid_disk
< 0)
9287 /* There are still inflight io, don't remove this rdev. */
9288 if (atomic_read(&rdev
->nr_pending
))
9292 * An error occurred but has not yet been acknowledged by the metadata
9293 * handler, don't remove this rdev.
9295 if (test_bit(Blocked
, &rdev
->flags
))
9298 /* Fautly rdev is not used, it's safe to remove it. */
9299 if (test_bit(Faulty
, &rdev
->flags
))
9302 /* Journal disk can only be removed if it's faulty. */
9303 if (test_bit(Journal
, &rdev
->flags
))
9307 * 'In_sync' is cleared while 'raid_disk' is valid, which means
9308 * replacement has just become active from pers->spare_active(), and
9309 * then pers->hot_remove_disk() will replace this rdev with replacement.
9311 if (!test_bit(In_sync
, &rdev
->flags
))
9317 static bool rdev_is_spare(struct md_rdev
*rdev
)
9319 return !test_bit(Candidate
, &rdev
->flags
) && rdev
->raid_disk
>= 0 &&
9320 !test_bit(In_sync
, &rdev
->flags
) &&
9321 !test_bit(Journal
, &rdev
->flags
) &&
9322 !test_bit(Faulty
, &rdev
->flags
);
9325 static bool rdev_addable(struct md_rdev
*rdev
)
9327 /* rdev is already used, don't add it again. */
9328 if (test_bit(Candidate
, &rdev
->flags
) || rdev
->raid_disk
>= 0 ||
9329 test_bit(Faulty
, &rdev
->flags
))
9332 /* Allow to add journal disk. */
9333 if (test_bit(Journal
, &rdev
->flags
))
9336 /* Allow to add if array is read-write. */
9337 if (md_is_rdwr(rdev
->mddev
))
9341 * For read-only array, only allow to readd a rdev. And if bitmap is
9342 * used, don't allow to readd a rdev that is too old.
9344 if (rdev
->saved_raid_disk
>= 0 && !test_bit(Bitmap_sync
, &rdev
->flags
))
9350 static bool md_spares_need_change(struct mddev
*mddev
)
9352 struct md_rdev
*rdev
;
9355 rdev_for_each_rcu(rdev
, mddev
) {
9356 if (rdev_removeable(rdev
) || rdev_addable(rdev
)) {
9365 static int remove_and_add_spares(struct mddev
*mddev
,
9366 struct md_rdev
*this)
9368 struct md_rdev
*rdev
;
9372 if (this && test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
9373 /* Mustn't remove devices when resync thread is running */
9376 rdev_for_each(rdev
, mddev
) {
9377 if ((this == NULL
|| rdev
== this) && rdev_removeable(rdev
) &&
9378 !mddev
->pers
->hot_remove_disk(mddev
, rdev
)) {
9379 sysfs_unlink_rdev(mddev
, rdev
);
9380 rdev
->saved_raid_disk
= rdev
->raid_disk
;
9381 rdev
->raid_disk
= -1;
9386 if (removed
&& mddev
->kobj
.sd
)
9387 sysfs_notify_dirent_safe(mddev
->sysfs_degraded
);
9389 if (this && removed
)
9392 rdev_for_each(rdev
, mddev
) {
9393 if (this && this != rdev
)
9395 if (rdev_is_spare(rdev
))
9397 if (!rdev_addable(rdev
))
9399 if (!test_bit(Journal
, &rdev
->flags
))
9400 rdev
->recovery_offset
= 0;
9401 if (mddev
->pers
->hot_add_disk(mddev
, rdev
) == 0) {
9402 /* failure here is OK */
9403 sysfs_link_rdev(mddev
, rdev
);
9404 if (!test_bit(Journal
, &rdev
->flags
))
9407 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
9412 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
9416 static bool md_choose_sync_action(struct mddev
*mddev
, int *spares
)
9418 /* Check if reshape is in progress first. */
9419 if (mddev
->reshape_position
!= MaxSector
) {
9420 if (mddev
->pers
->check_reshape
== NULL
||
9421 mddev
->pers
->check_reshape(mddev
) != 0)
9424 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
9425 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
9430 * Remove any failed drives, then add spares if possible. Spares are
9431 * also removed and re-added, to allow the personality to fail the
9434 *spares
= remove_and_add_spares(mddev
, NULL
);
9436 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
9437 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
9438 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
9440 /* Start new recovery. */
9441 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
9445 /* Check if recovery is in progress. */
9446 if (mddev
->recovery_cp
< MaxSector
) {
9447 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
9448 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
9452 /* Delay to choose resync/check/repair in md_do_sync(). */
9453 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
9456 /* Nothing to be done */
9460 static void md_start_sync(struct work_struct
*ws
)
9462 struct mddev
*mddev
= container_of(ws
, struct mddev
, sync_work
);
9464 bool suspend
= false;
9468 * If reshape is still in progress, spares won't be added or removed
9469 * from conf until reshape is done.
9471 if (mddev
->reshape_position
== MaxSector
&&
9472 md_spares_need_change(mddev
)) {
9474 mddev_suspend(mddev
, false);
9477 mddev_lock_nointr(mddev
);
9478 if (!md_is_rdwr(mddev
)) {
9480 * On a read-only array we can:
9481 * - remove failed devices
9482 * - add already-in_sync devices if the array itself is in-sync.
9483 * As we only add devices that are already in-sync, we can
9484 * activate the spares immediately.
9486 remove_and_add_spares(mddev
, NULL
);
9490 if (!md_choose_sync_action(mddev
, &spares
))
9493 if (!mddev
->pers
->sync_request
)
9497 * We are adding a device or devices to an array which has the bitmap
9498 * stored on all devices. So make sure all bitmap pages get written.
9501 mddev
->bitmap_ops
->write_all(mddev
);
9503 name
= test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) ?
9504 "reshape" : "resync";
9505 rcu_assign_pointer(mddev
->sync_thread
,
9506 md_register_thread(md_do_sync
, mddev
, name
));
9507 if (!mddev
->sync_thread
) {
9508 pr_warn("%s: could not start resync thread...\n",
9510 /* leave the spares where they are, it shouldn't hurt */
9514 mddev_unlock(mddev
);
9516 * md_start_sync was triggered by MD_RECOVERY_NEEDED, so we should
9517 * not set it again. Otherwise, we may cause issue like this one:
9518 * https://bugzilla.kernel.org/show_bug.cgi?id=218200
9519 * Therefore, use __mddev_resume(mddev, false).
9522 __mddev_resume(mddev
, false);
9523 md_wakeup_thread(mddev
->sync_thread
);
9524 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
9529 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
9530 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
9531 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
9532 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
9533 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
9534 mddev_unlock(mddev
);
9536 * md_start_sync was triggered by MD_RECOVERY_NEEDED, so we should
9537 * not set it again. Otherwise, we may cause issue like this one:
9538 * https://bugzilla.kernel.org/show_bug.cgi?id=218200
9539 * Therefore, use __mddev_resume(mddev, false).
9542 __mddev_resume(mddev
, false);
9544 wake_up(&resync_wait
);
9545 if (test_and_clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
) &&
9546 mddev
->sysfs_action
)
9547 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
9550 static void unregister_sync_thread(struct mddev
*mddev
)
9552 if (!test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
9553 /* resync/recovery still happening */
9554 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
9558 if (WARN_ON_ONCE(!mddev
->sync_thread
))
9561 md_reap_sync_thread(mddev
);
9565 * This routine is regularly called by all per-raid-array threads to
9566 * deal with generic issues like resync and super-block update.
9567 * Raid personalities that don't have a thread (linear/raid0) do not
9568 * need this as they never do any recovery or update the superblock.
9570 * It does not do any resync itself, but rather "forks" off other threads
9571 * to do that as needed.
9572 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
9573 * "->recovery" and create a thread at ->sync_thread.
9574 * When the thread finishes it sets MD_RECOVERY_DONE
9575 * and wakeups up this thread which will reap the thread and finish up.
9576 * This thread also removes any faulty devices (with nr_pending == 0).
9578 * The overall approach is:
9579 * 1/ if the superblock needs updating, update it.
9580 * 2/ If a recovery thread is running, don't do anything else.
9581 * 3/ If recovery has finished, clean up, possibly marking spares active.
9582 * 4/ If there are any faulty devices, remove them.
9583 * 5/ If array is degraded, try to add spares devices
9584 * 6/ If array has spares or is not in-sync, start a resync thread.
9586 void md_check_recovery(struct mddev
*mddev
)
9589 mddev
->bitmap_ops
->daemon_work(mddev
);
9591 if (signal_pending(current
)) {
9592 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
9593 pr_debug("md: %s in immediate safe mode\n",
9595 mddev
->safemode
= 2;
9597 flush_signals(current
);
9600 if (!md_is_rdwr(mddev
) &&
9601 !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) &&
9602 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
9605 (mddev
->sb_flags
& ~ (1<<MD_SB_CHANGE_PENDING
)) ||
9606 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
9607 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
9608 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
9609 (mddev
->safemode
== 2
9610 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
9614 if (mddev_trylock(mddev
)) {
9615 bool try_set_sync
= mddev
->safemode
!= 0;
9617 if (!mddev
->external
&& mddev
->safemode
== 1)
9618 mddev
->safemode
= 0;
9620 if (!md_is_rdwr(mddev
)) {
9621 struct md_rdev
*rdev
;
9623 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
)) {
9624 unregister_sync_thread(mddev
);
9628 if (!mddev
->external
&& mddev
->in_sync
)
9630 * 'Blocked' flag not needed as failed devices
9631 * will be recorded if array switched to read/write.
9632 * Leaving it set will prevent the device
9633 * from being removed.
9635 rdev_for_each(rdev
, mddev
)
9636 clear_bit(Blocked
, &rdev
->flags
);
9639 * There is no thread, but we need to call
9640 * ->spare_active and clear saved_raid_disk
9642 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
9643 md_reap_sync_thread(mddev
);
9646 * Let md_start_sync() to remove and add rdevs to the
9649 if (md_spares_need_change(mddev
)) {
9650 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
9651 queue_work(md_misc_wq
, &mddev
->sync_work
);
9654 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
9655 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
9656 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
9661 if (mddev_is_clustered(mddev
)) {
9662 struct md_rdev
*rdev
, *tmp
;
9663 /* kick the device if another node issued a
9666 rdev_for_each_safe(rdev
, tmp
, mddev
) {
9667 if (test_and_clear_bit(ClusterRemove
, &rdev
->flags
) &&
9668 rdev
->raid_disk
< 0)
9669 md_kick_rdev_from_array(rdev
);
9673 if (try_set_sync
&& !mddev
->external
&& !mddev
->in_sync
) {
9674 spin_lock(&mddev
->lock
);
9676 spin_unlock(&mddev
->lock
);
9679 if (mddev
->sb_flags
)
9680 md_update_sb(mddev
, 0);
9683 * Never start a new sync thread if MD_RECOVERY_RUNNING is
9686 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
)) {
9687 unregister_sync_thread(mddev
);
9691 /* Set RUNNING before clearing NEEDED to avoid
9692 * any transients in the value of "sync_action".
9694 mddev
->curr_resync_completed
= 0;
9695 spin_lock(&mddev
->lock
);
9696 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
9697 spin_unlock(&mddev
->lock
);
9698 /* Clear some bits that don't mean anything, but
9701 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
9702 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
9704 if (test_and_clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) &&
9705 !test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
)) {
9706 queue_work(md_misc_wq
, &mddev
->sync_work
);
9708 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
9709 wake_up(&resync_wait
);
9713 wake_up(&mddev
->sb_wait
);
9714 mddev_unlock(mddev
);
9717 EXPORT_SYMBOL(md_check_recovery
);
9719 void md_reap_sync_thread(struct mddev
*mddev
)
9721 struct md_rdev
*rdev
;
9722 sector_t old_dev_sectors
= mddev
->dev_sectors
;
9723 bool is_reshaped
= false;
9725 /* resync has finished, collect result */
9726 md_unregister_thread(mddev
, &mddev
->sync_thread
);
9727 atomic_inc(&mddev
->sync_seq
);
9729 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
9730 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
) &&
9731 mddev
->degraded
!= mddev
->raid_disks
) {
9733 /* activate any spares */
9734 if (mddev
->pers
->spare_active(mddev
)) {
9735 sysfs_notify_dirent_safe(mddev
->sysfs_degraded
);
9736 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
9739 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
9740 mddev
->pers
->finish_reshape
) {
9741 mddev
->pers
->finish_reshape(mddev
);
9742 if (mddev_is_clustered(mddev
))
9746 /* If array is no-longer degraded, then any saved_raid_disk
9747 * information must be scrapped.
9749 if (!mddev
->degraded
)
9750 rdev_for_each(rdev
, mddev
)
9751 rdev
->saved_raid_disk
= -1;
9753 md_update_sb(mddev
, 1);
9754 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
9755 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
9757 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED
, &mddev
->flags
))
9758 md_cluster_ops
->resync_finish(mddev
);
9759 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
9760 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
9761 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
9762 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
9763 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
9764 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
9766 * We call md_cluster_ops->update_size here because sync_size could
9767 * be changed by md_update_sb, and MD_RECOVERY_RESHAPE is cleared,
9768 * so it is time to update size across cluster.
9770 if (mddev_is_clustered(mddev
) && is_reshaped
9771 && !test_bit(MD_CLOSING
, &mddev
->flags
))
9772 md_cluster_ops
->update_size(mddev
, old_dev_sectors
);
9773 /* flag recovery needed just to double check */
9774 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
9775 sysfs_notify_dirent_safe(mddev
->sysfs_completed
);
9776 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
9778 if (mddev
->event_work
.func
)
9779 queue_work(md_misc_wq
, &mddev
->event_work
);
9780 wake_up(&resync_wait
);
9782 EXPORT_SYMBOL(md_reap_sync_thread
);
9784 void md_wait_for_blocked_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
9786 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
9787 wait_event_timeout(rdev
->blocked_wait
, !rdev_blocked(rdev
),
9788 msecs_to_jiffies(5000));
9789 rdev_dec_pending(rdev
, mddev
);
9791 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
9793 void md_finish_reshape(struct mddev
*mddev
)
9795 /* called be personality module when reshape completes. */
9796 struct md_rdev
*rdev
;
9798 rdev_for_each(rdev
, mddev
) {
9799 if (rdev
->data_offset
> rdev
->new_data_offset
)
9800 rdev
->sectors
+= rdev
->data_offset
- rdev
->new_data_offset
;
9802 rdev
->sectors
-= rdev
->new_data_offset
- rdev
->data_offset
;
9803 rdev
->data_offset
= rdev
->new_data_offset
;
9806 EXPORT_SYMBOL(md_finish_reshape
);
9808 /* Bad block management */
9810 /* Returns 1 on success, 0 on failure */
9811 int rdev_set_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
,
9814 struct mddev
*mddev
= rdev
->mddev
;
9818 * Recording new badblocks for faulty rdev will force unnecessary
9819 * super block updating. This is fragile for external management because
9820 * userspace daemon may trying to remove this device and deadlock may
9821 * occur. This will be probably solved in the mdadm, but it is safer to
9824 if (test_bit(Faulty
, &rdev
->flags
))
9828 s
+= rdev
->new_data_offset
;
9830 s
+= rdev
->data_offset
;
9831 rv
= badblocks_set(&rdev
->badblocks
, s
, sectors
, 0);
9833 /* Make sure they get written out promptly */
9834 if (test_bit(ExternalBbl
, &rdev
->flags
))
9835 sysfs_notify_dirent_safe(rdev
->sysfs_unack_badblocks
);
9836 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
9837 set_mask_bits(&mddev
->sb_flags
, 0,
9838 BIT(MD_SB_CHANGE_CLEAN
) | BIT(MD_SB_CHANGE_PENDING
));
9839 md_wakeup_thread(rdev
->mddev
->thread
);
9844 EXPORT_SYMBOL_GPL(rdev_set_badblocks
);
9846 int rdev_clear_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
,
9851 s
+= rdev
->new_data_offset
;
9853 s
+= rdev
->data_offset
;
9854 rv
= badblocks_clear(&rdev
->badblocks
, s
, sectors
);
9855 if ((rv
== 0) && test_bit(ExternalBbl
, &rdev
->flags
))
9856 sysfs_notify_dirent_safe(rdev
->sysfs_badblocks
);
9859 EXPORT_SYMBOL_GPL(rdev_clear_badblocks
);
9861 static int md_notify_reboot(struct notifier_block
*this,
9862 unsigned long code
, void *x
)
9864 struct mddev
*mddev
, *n
;
9867 spin_lock(&all_mddevs_lock
);
9868 list_for_each_entry_safe(mddev
, n
, &all_mddevs
, all_mddevs
) {
9869 if (!mddev_get(mddev
))
9871 spin_unlock(&all_mddevs_lock
);
9872 if (mddev_trylock(mddev
)) {
9874 __md_stop_writes(mddev
);
9875 if (mddev
->persistent
)
9876 mddev
->safemode
= 2;
9877 mddev_unlock(mddev
);
9881 spin_lock(&all_mddevs_lock
);
9883 spin_unlock(&all_mddevs_lock
);
9886 * certain more exotic SCSI devices are known to be
9887 * volatile wrt too early system reboots. While the
9888 * right place to handle this issue is the given
9889 * driver, we do want to have a safe RAID driver ...
9897 static struct notifier_block md_notifier
= {
9898 .notifier_call
= md_notify_reboot
,
9900 .priority
= INT_MAX
, /* before any real devices */
9903 static void md_geninit(void)
9905 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
9907 proc_create("mdstat", S_IRUGO
, NULL
, &mdstat_proc_ops
);
9910 static int __init
md_init(void)
9914 md_wq
= alloc_workqueue("md", WQ_MEM_RECLAIM
, 0);
9918 md_misc_wq
= alloc_workqueue("md_misc", 0, 0);
9922 md_bitmap_wq
= alloc_workqueue("md_bitmap", WQ_MEM_RECLAIM
| WQ_UNBOUND
,
9927 ret
= __register_blkdev(MD_MAJOR
, "md", md_probe
);
9931 ret
= __register_blkdev(0, "mdp", md_probe
);
9936 register_reboot_notifier(&md_notifier
);
9937 raid_table_header
= register_sysctl("dev/raid", raid_table
);
9943 unregister_blkdev(MD_MAJOR
, "md");
9945 destroy_workqueue(md_bitmap_wq
);
9947 destroy_workqueue(md_misc_wq
);
9949 destroy_workqueue(md_wq
);
9954 static void check_sb_changes(struct mddev
*mddev
, struct md_rdev
*rdev
)
9956 struct mdp_superblock_1
*sb
= page_address(rdev
->sb_page
);
9957 struct md_rdev
*rdev2
, *tmp
;
9961 * If size is changed in another node then we need to
9962 * do resize as well.
9964 if (mddev
->dev_sectors
!= le64_to_cpu(sb
->size
)) {
9965 ret
= mddev
->pers
->resize(mddev
, le64_to_cpu(sb
->size
));
9967 pr_info("md-cluster: resize failed\n");
9969 mddev
->bitmap_ops
->update_sb(mddev
->bitmap
);
9972 /* Check for change of roles in the active devices */
9973 rdev_for_each_safe(rdev2
, tmp
, mddev
) {
9974 if (test_bit(Faulty
, &rdev2
->flags
))
9977 /* Check if the roles changed */
9978 role
= le16_to_cpu(sb
->dev_roles
[rdev2
->desc_nr
]);
9980 if (test_bit(Candidate
, &rdev2
->flags
)) {
9981 if (role
== MD_DISK_ROLE_FAULTY
) {
9982 pr_info("md: Removing Candidate device %pg because add failed\n",
9984 md_kick_rdev_from_array(rdev2
);
9988 clear_bit(Candidate
, &rdev2
->flags
);
9991 if (role
!= rdev2
->raid_disk
) {
9993 * got activated except reshape is happening.
9995 if (rdev2
->raid_disk
== -1 && role
!= MD_DISK_ROLE_SPARE
&&
9996 !(le32_to_cpu(sb
->feature_map
) &
9997 MD_FEATURE_RESHAPE_ACTIVE
) &&
9998 !md_cluster_ops
->resync_status_get(mddev
)) {
10000 * -1 to make raid1_add_disk() set conf->fullsync
10001 * to 1. This could avoid skipping sync when the
10002 * remote node is down during resyncing.
10004 if ((le32_to_cpu(sb
->feature_map
)
10005 & MD_FEATURE_RECOVERY_OFFSET
))
10006 rdev2
->saved_raid_disk
= -1;
10008 rdev2
->saved_raid_disk
= role
;
10009 ret
= remove_and_add_spares(mddev
, rdev2
);
10010 pr_info("Activated spare: %pg\n",
10012 /* wakeup mddev->thread here, so array could
10013 * perform resync with the new activated disk */
10014 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
10015 md_wakeup_thread(mddev
->thread
);
10018 * We just want to do the minimum to mark the disk
10019 * as faulty. The recovery is performed by the
10020 * one who initiated the error.
10022 if (role
== MD_DISK_ROLE_FAULTY
||
10023 role
== MD_DISK_ROLE_JOURNAL
) {
10024 md_error(mddev
, rdev2
);
10025 clear_bit(Blocked
, &rdev2
->flags
);
10030 if (mddev
->raid_disks
!= le32_to_cpu(sb
->raid_disks
)) {
10031 ret
= update_raid_disks(mddev
, le32_to_cpu(sb
->raid_disks
));
10033 pr_warn("md: updating array disks failed. %d\n", ret
);
10037 * Since mddev->delta_disks has already updated in update_raid_disks,
10038 * so it is time to check reshape.
10040 if (test_bit(MD_RESYNCING_REMOTE
, &mddev
->recovery
) &&
10041 (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
10043 * reshape is happening in the remote node, we need to
10044 * update reshape_position and call start_reshape.
10046 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
10047 if (mddev
->pers
->update_reshape_pos
)
10048 mddev
->pers
->update_reshape_pos(mddev
);
10049 if (mddev
->pers
->start_reshape
)
10050 mddev
->pers
->start_reshape(mddev
);
10051 } else if (test_bit(MD_RESYNCING_REMOTE
, &mddev
->recovery
) &&
10052 mddev
->reshape_position
!= MaxSector
&&
10053 !(le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
10054 /* reshape is just done in another node. */
10055 mddev
->reshape_position
= MaxSector
;
10056 if (mddev
->pers
->update_reshape_pos
)
10057 mddev
->pers
->update_reshape_pos(mddev
);
10060 /* Finally set the event to be up to date */
10061 mddev
->events
= le64_to_cpu(sb
->events
);
10064 static int read_rdev(struct mddev
*mddev
, struct md_rdev
*rdev
)
10067 struct page
*swapout
= rdev
->sb_page
;
10068 struct mdp_superblock_1
*sb
;
10070 /* Store the sb page of the rdev in the swapout temporary
10071 * variable in case we err in the future
10073 rdev
->sb_page
= NULL
;
10074 err
= alloc_disk_sb(rdev
);
10076 ClearPageUptodate(rdev
->sb_page
);
10077 rdev
->sb_loaded
= 0;
10078 err
= super_types
[mddev
->major_version
].
10079 load_super(rdev
, NULL
, mddev
->minor_version
);
10082 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
10083 __func__
, __LINE__
, rdev
->desc_nr
, err
);
10085 put_page(rdev
->sb_page
);
10086 rdev
->sb_page
= swapout
;
10087 rdev
->sb_loaded
= 1;
10091 sb
= page_address(rdev
->sb_page
);
10092 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
10096 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RECOVERY_OFFSET
))
10097 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
10099 /* The other node finished recovery, call spare_active to set
10100 * device In_sync and mddev->degraded
10102 if (rdev
->recovery_offset
== MaxSector
&&
10103 !test_bit(In_sync
, &rdev
->flags
) &&
10104 mddev
->pers
->spare_active(mddev
))
10105 sysfs_notify_dirent_safe(mddev
->sysfs_degraded
);
10111 void md_reload_sb(struct mddev
*mddev
, int nr
)
10113 struct md_rdev
*rdev
= NULL
, *iter
;
10116 /* Find the rdev */
10117 rdev_for_each_rcu(iter
, mddev
) {
10118 if (iter
->desc_nr
== nr
) {
10125 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__
, __LINE__
, nr
);
10129 err
= read_rdev(mddev
, rdev
);
10133 check_sb_changes(mddev
, rdev
);
10135 /* Read all rdev's to update recovery_offset */
10136 rdev_for_each_rcu(rdev
, mddev
) {
10137 if (!test_bit(Faulty
, &rdev
->flags
))
10138 read_rdev(mddev
, rdev
);
10141 EXPORT_SYMBOL(md_reload_sb
);
10146 * Searches all registered partitions for autorun RAID arrays
10150 static DEFINE_MUTEX(detected_devices_mutex
);
10151 static LIST_HEAD(all_detected_devices
);
10152 struct detected_devices_node
{
10153 struct list_head list
;
10157 void md_autodetect_dev(dev_t dev
)
10159 struct detected_devices_node
*node_detected_dev
;
10161 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
10162 if (node_detected_dev
) {
10163 node_detected_dev
->dev
= dev
;
10164 mutex_lock(&detected_devices_mutex
);
10165 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
10166 mutex_unlock(&detected_devices_mutex
);
10170 void md_autostart_arrays(int part
)
10172 struct md_rdev
*rdev
;
10173 struct detected_devices_node
*node_detected_dev
;
10175 int i_scanned
, i_passed
;
10180 pr_info("md: Autodetecting RAID arrays.\n");
10182 mutex_lock(&detected_devices_mutex
);
10183 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
10185 node_detected_dev
= list_entry(all_detected_devices
.next
,
10186 struct detected_devices_node
, list
);
10187 list_del(&node_detected_dev
->list
);
10188 dev
= node_detected_dev
->dev
;
10189 kfree(node_detected_dev
);
10190 mutex_unlock(&detected_devices_mutex
);
10191 rdev
= md_import_device(dev
,0, 90);
10192 mutex_lock(&detected_devices_mutex
);
10196 if (test_bit(Faulty
, &rdev
->flags
))
10199 set_bit(AutoDetected
, &rdev
->flags
);
10200 list_add(&rdev
->same_set
, &pending_raid_disks
);
10203 mutex_unlock(&detected_devices_mutex
);
10205 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned
, i_passed
);
10207 autorun_devices(part
);
10210 #endif /* !MODULE */
10212 static __exit
void md_exit(void)
10214 struct mddev
*mddev
, *n
;
10217 unregister_blkdev(MD_MAJOR
,"md");
10218 unregister_blkdev(mdp_major
, "mdp");
10219 unregister_reboot_notifier(&md_notifier
);
10220 unregister_sysctl_table(raid_table_header
);
10222 /* We cannot unload the modules while some process is
10223 * waiting for us in select() or poll() - wake them up
10226 while (waitqueue_active(&md_event_waiters
)) {
10227 /* not safe to leave yet */
10228 wake_up(&md_event_waiters
);
10232 remove_proc_entry("mdstat", NULL
);
10234 spin_lock(&all_mddevs_lock
);
10235 list_for_each_entry_safe(mddev
, n
, &all_mddevs
, all_mddevs
) {
10236 if (!mddev_get(mddev
))
10238 spin_unlock(&all_mddevs_lock
);
10239 export_array(mddev
);
10241 mddev
->hold_active
= 0;
10243 * As the mddev is now fully clear, mddev_put will schedule
10244 * the mddev for destruction by a workqueue, and the
10245 * destroy_workqueue() below will wait for that to complete.
10248 spin_lock(&all_mddevs_lock
);
10250 spin_unlock(&all_mddevs_lock
);
10252 destroy_workqueue(md_misc_wq
);
10253 destroy_workqueue(md_bitmap_wq
);
10254 destroy_workqueue(md_wq
);
10257 subsys_initcall(md_init
);
10258 module_exit(md_exit
)
10260 static int get_ro(char *buffer
, const struct kernel_param
*kp
)
10262 return sprintf(buffer
, "%d\n", start_readonly
);
10264 static int set_ro(const char *val
, const struct kernel_param
*kp
)
10266 return kstrtouint(val
, 10, (unsigned int *)&start_readonly
);
10269 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
10270 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
10271 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
10272 module_param(create_on_open
, bool, S_IRUSR
|S_IWUSR
);
10274 MODULE_LICENSE("GPL");
10275 MODULE_DESCRIPTION("MD RAID framework");
10276 MODULE_ALIAS("md");
10277 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);