1 // SPDX-License-Identifier: GPL-2.0-or-later
3 md.c : Multiple Devices driver for Linux
4 Copyright (C) 1998, 1999, 2000 Ingo Molnar
6 completely rewritten, based on the MD driver code from Marc Zyngier
10 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
11 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
12 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
13 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
14 - kmod support by: Cyrus Durgin
15 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
16 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
18 - lots of fixes and improvements to the RAID1/RAID5 and generic
19 RAID code (such as request based resynchronization):
21 Neil Brown <neilb@cse.unsw.edu.au>.
23 - persistent bitmap code
24 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
27 Errors, Warnings, etc.
29 pr_crit() for error conditions that risk data loss
30 pr_err() for error conditions that are unexpected, like an IO error
31 or internal inconsistency
32 pr_warn() for error conditions that could have been predicated, like
33 adding a device to an array when it has incompatible metadata
34 pr_info() for every interesting, very rare events, like an array starting
35 or stopping, or resync starting or stopping
36 pr_debug() for everything else.
40 #include <linux/sched/mm.h>
41 #include <linux/sched/signal.h>
42 #include <linux/kthread.h>
43 #include <linux/blkdev.h>
44 #include <linux/badblocks.h>
45 #include <linux/sysctl.h>
46 #include <linux/seq_file.h>
48 #include <linux/poll.h>
49 #include <linux/ctype.h>
50 #include <linux/string.h>
51 #include <linux/hdreg.h>
52 #include <linux/proc_fs.h>
53 #include <linux/random.h>
54 #include <linux/module.h>
55 #include <linux/reboot.h>
56 #include <linux/file.h>
57 #include <linux/compat.h>
58 #include <linux/delay.h>
59 #include <linux/raid/md_p.h>
60 #include <linux/raid/md_u.h>
61 #include <linux/slab.h>
62 #include <linux/percpu-refcount.h>
64 #include <trace/events/block.h>
66 #include "md-bitmap.h"
67 #include "md-cluster.h"
70 static void autostart_arrays(int part
);
73 /* pers_list is a list of registered personalities protected
75 * pers_lock does extra service to protect accesses to
76 * mddev->thread when the mutex cannot be held.
78 static LIST_HEAD(pers_list
);
79 static DEFINE_SPINLOCK(pers_lock
);
81 static struct kobj_type md_ktype
;
83 struct md_cluster_operations
*md_cluster_ops
;
84 EXPORT_SYMBOL(md_cluster_ops
);
85 static struct module
*md_cluster_mod
;
87 static DECLARE_WAIT_QUEUE_HEAD(resync_wait
);
88 static struct workqueue_struct
*md_wq
;
89 static struct workqueue_struct
*md_misc_wq
;
91 static int remove_and_add_spares(struct mddev
*mddev
,
92 struct md_rdev
*this);
93 static void mddev_detach(struct mddev
*mddev
);
96 * Default number of read corrections we'll attempt on an rdev
97 * before ejecting it from the array. We divide the read error
98 * count by 2 for every hour elapsed between read errors.
100 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
102 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
103 * is 1000 KB/sec, so the extra system load does not show up that much.
104 * Increase it if you want to have more _guaranteed_ speed. Note that
105 * the RAID driver will use the maximum available bandwidth if the IO
106 * subsystem is idle. There is also an 'absolute maximum' reconstruction
107 * speed limit - in case reconstruction slows down your system despite
110 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
111 * or /sys/block/mdX/md/sync_speed_{min,max}
114 static int sysctl_speed_limit_min
= 1000;
115 static int sysctl_speed_limit_max
= 200000;
116 static inline int speed_min(struct mddev
*mddev
)
118 return mddev
->sync_speed_min
?
119 mddev
->sync_speed_min
: sysctl_speed_limit_min
;
122 static inline int speed_max(struct mddev
*mddev
)
124 return mddev
->sync_speed_max
?
125 mddev
->sync_speed_max
: sysctl_speed_limit_max
;
128 static int rdev_init_wb(struct md_rdev
*rdev
)
130 if (rdev
->bdev
->bd_queue
->nr_hw_queues
== 1)
133 spin_lock_init(&rdev
->wb_list_lock
);
134 INIT_LIST_HEAD(&rdev
->wb_list
);
135 init_waitqueue_head(&rdev
->wb_io_wait
);
136 set_bit(WBCollisionCheck
, &rdev
->flags
);
142 * Create wb_info_pool if rdev is the first multi-queue device flaged
143 * with writemostly, also write-behind mode is enabled.
145 void mddev_create_wb_pool(struct mddev
*mddev
, struct md_rdev
*rdev
,
148 if (mddev
->bitmap_info
.max_write_behind
== 0)
151 if (!test_bit(WriteMostly
, &rdev
->flags
) || !rdev_init_wb(rdev
))
154 if (mddev
->wb_info_pool
== NULL
) {
155 unsigned int noio_flag
;
158 mddev_suspend(mddev
);
159 noio_flag
= memalloc_noio_save();
160 mddev
->wb_info_pool
= mempool_create_kmalloc_pool(NR_WB_INFOS
,
161 sizeof(struct wb_info
));
162 memalloc_noio_restore(noio_flag
);
163 if (!mddev
->wb_info_pool
)
164 pr_err("can't alloc memory pool for writemostly\n");
169 EXPORT_SYMBOL_GPL(mddev_create_wb_pool
);
172 * destroy wb_info_pool if rdev is the last device flaged with WBCollisionCheck.
174 static void mddev_destroy_wb_pool(struct mddev
*mddev
, struct md_rdev
*rdev
)
176 if (!test_and_clear_bit(WBCollisionCheck
, &rdev
->flags
))
179 if (mddev
->wb_info_pool
) {
180 struct md_rdev
*temp
;
184 * Check if other rdevs need wb_info_pool.
186 rdev_for_each(temp
, mddev
)
188 test_bit(WBCollisionCheck
, &temp
->flags
))
191 mddev_suspend(rdev
->mddev
);
192 mempool_destroy(mddev
->wb_info_pool
);
193 mddev
->wb_info_pool
= NULL
;
194 mddev_resume(rdev
->mddev
);
199 static struct ctl_table_header
*raid_table_header
;
201 static struct ctl_table raid_table
[] = {
203 .procname
= "speed_limit_min",
204 .data
= &sysctl_speed_limit_min
,
205 .maxlen
= sizeof(int),
206 .mode
= S_IRUGO
|S_IWUSR
,
207 .proc_handler
= proc_dointvec
,
210 .procname
= "speed_limit_max",
211 .data
= &sysctl_speed_limit_max
,
212 .maxlen
= sizeof(int),
213 .mode
= S_IRUGO
|S_IWUSR
,
214 .proc_handler
= proc_dointvec
,
219 static struct ctl_table raid_dir_table
[] = {
223 .mode
= S_IRUGO
|S_IXUGO
,
229 static struct ctl_table raid_root_table
[] = {
234 .child
= raid_dir_table
,
239 static const struct block_device_operations md_fops
;
241 static int start_readonly
;
244 * The original mechanism for creating an md device is to create
245 * a device node in /dev and to open it. This causes races with device-close.
246 * The preferred method is to write to the "new_array" module parameter.
247 * This can avoid races.
248 * Setting create_on_open to false disables the original mechanism
249 * so all the races disappear.
251 static bool create_on_open
= true;
253 struct bio
*bio_alloc_mddev(gfp_t gfp_mask
, int nr_iovecs
,
256 if (!mddev
|| !bioset_initialized(&mddev
->bio_set
))
257 return bio_alloc(gfp_mask
, nr_iovecs
);
259 return bio_alloc_bioset(gfp_mask
, nr_iovecs
, &mddev
->bio_set
);
261 EXPORT_SYMBOL_GPL(bio_alloc_mddev
);
263 static struct bio
*md_bio_alloc_sync(struct mddev
*mddev
)
265 if (!mddev
|| !bioset_initialized(&mddev
->sync_set
))
266 return bio_alloc(GFP_NOIO
, 1);
268 return bio_alloc_bioset(GFP_NOIO
, 1, &mddev
->sync_set
);
272 * We have a system wide 'event count' that is incremented
273 * on any 'interesting' event, and readers of /proc/mdstat
274 * can use 'poll' or 'select' to find out when the event
278 * start array, stop array, error, add device, remove device,
279 * start build, activate spare
281 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters
);
282 static atomic_t md_event_count
;
283 void md_new_event(struct mddev
*mddev
)
285 atomic_inc(&md_event_count
);
286 wake_up(&md_event_waiters
);
288 EXPORT_SYMBOL_GPL(md_new_event
);
291 * Enables to iterate over all existing md arrays
292 * all_mddevs_lock protects this list.
294 static LIST_HEAD(all_mddevs
);
295 static DEFINE_SPINLOCK(all_mddevs_lock
);
298 * iterates through all used mddevs in the system.
299 * We take care to grab the all_mddevs_lock whenever navigating
300 * the list, and to always hold a refcount when unlocked.
301 * Any code which breaks out of this loop while own
302 * a reference to the current mddev and must mddev_put it.
304 #define for_each_mddev(_mddev,_tmp) \
306 for (({ spin_lock(&all_mddevs_lock); \
307 _tmp = all_mddevs.next; \
309 ({ if (_tmp != &all_mddevs) \
310 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
311 spin_unlock(&all_mddevs_lock); \
312 if (_mddev) mddev_put(_mddev); \
313 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
314 _tmp != &all_mddevs;}); \
315 ({ spin_lock(&all_mddevs_lock); \
316 _tmp = _tmp->next;}) \
319 /* Rather than calling directly into the personality make_request function,
320 * IO requests come here first so that we can check if the device is
321 * being suspended pending a reconfiguration.
322 * We hold a refcount over the call to ->make_request. By the time that
323 * call has finished, the bio has been linked into some internal structure
324 * and so is visible to ->quiesce(), so we don't need the refcount any more.
326 static bool is_suspended(struct mddev
*mddev
, struct bio
*bio
)
328 if (mddev
->suspended
)
330 if (bio_data_dir(bio
) != WRITE
)
332 if (mddev
->suspend_lo
>= mddev
->suspend_hi
)
334 if (bio
->bi_iter
.bi_sector
>= mddev
->suspend_hi
)
336 if (bio_end_sector(bio
) < mddev
->suspend_lo
)
341 void md_handle_request(struct mddev
*mddev
, struct bio
*bio
)
345 if (is_suspended(mddev
, bio
)) {
348 prepare_to_wait(&mddev
->sb_wait
, &__wait
,
349 TASK_UNINTERRUPTIBLE
);
350 if (!is_suspended(mddev
, bio
))
356 finish_wait(&mddev
->sb_wait
, &__wait
);
358 atomic_inc(&mddev
->active_io
);
361 if (!mddev
->pers
->make_request(mddev
, bio
)) {
362 atomic_dec(&mddev
->active_io
);
363 wake_up(&mddev
->sb_wait
);
364 goto check_suspended
;
367 if (atomic_dec_and_test(&mddev
->active_io
) && mddev
->suspended
)
368 wake_up(&mddev
->sb_wait
);
370 EXPORT_SYMBOL(md_handle_request
);
372 static blk_qc_t
md_make_request(struct request_queue
*q
, struct bio
*bio
)
374 const int rw
= bio_data_dir(bio
);
375 const int sgrp
= op_stat_group(bio_op(bio
));
376 struct mddev
*mddev
= q
->queuedata
;
377 unsigned int sectors
;
379 if (unlikely(test_bit(MD_BROKEN
, &mddev
->flags
)) && (rw
== WRITE
)) {
381 return BLK_QC_T_NONE
;
384 blk_queue_split(q
, &bio
);
386 if (mddev
== NULL
|| mddev
->pers
== NULL
) {
388 return BLK_QC_T_NONE
;
390 if (mddev
->ro
== 1 && unlikely(rw
== WRITE
)) {
391 if (bio_sectors(bio
) != 0)
392 bio
->bi_status
= BLK_STS_IOERR
;
394 return BLK_QC_T_NONE
;
398 * save the sectors now since our bio can
399 * go away inside make_request
401 sectors
= bio_sectors(bio
);
402 /* bio could be mergeable after passing to underlayer */
403 bio
->bi_opf
&= ~REQ_NOMERGE
;
405 md_handle_request(mddev
, bio
);
408 part_stat_inc(&mddev
->gendisk
->part0
, ios
[sgrp
]);
409 part_stat_add(&mddev
->gendisk
->part0
, sectors
[sgrp
], sectors
);
412 return BLK_QC_T_NONE
;
415 /* mddev_suspend makes sure no new requests are submitted
416 * to the device, and that any requests that have been submitted
417 * are completely handled.
418 * Once mddev_detach() is called and completes, the module will be
421 void mddev_suspend(struct mddev
*mddev
)
423 WARN_ON_ONCE(mddev
->thread
&& current
== mddev
->thread
->tsk
);
424 lockdep_assert_held(&mddev
->reconfig_mutex
);
425 if (mddev
->suspended
++)
428 wake_up(&mddev
->sb_wait
);
429 set_bit(MD_ALLOW_SB_UPDATE
, &mddev
->flags
);
430 smp_mb__after_atomic();
431 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->active_io
) == 0);
432 mddev
->pers
->quiesce(mddev
, 1);
433 clear_bit_unlock(MD_ALLOW_SB_UPDATE
, &mddev
->flags
);
434 wait_event(mddev
->sb_wait
, !test_bit(MD_UPDATING_SB
, &mddev
->flags
));
436 del_timer_sync(&mddev
->safemode_timer
);
438 EXPORT_SYMBOL_GPL(mddev_suspend
);
440 void mddev_resume(struct mddev
*mddev
)
442 lockdep_assert_held(&mddev
->reconfig_mutex
);
443 if (--mddev
->suspended
)
445 wake_up(&mddev
->sb_wait
);
446 mddev
->pers
->quiesce(mddev
, 0);
448 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
449 md_wakeup_thread(mddev
->thread
);
450 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
452 EXPORT_SYMBOL_GPL(mddev_resume
);
454 int mddev_congested(struct mddev
*mddev
, int bits
)
456 struct md_personality
*pers
= mddev
->pers
;
460 if (mddev
->suspended
)
462 else if (pers
&& pers
->congested
)
463 ret
= pers
->congested(mddev
, bits
);
467 EXPORT_SYMBOL_GPL(mddev_congested
);
468 static int md_congested(void *data
, int bits
)
470 struct mddev
*mddev
= data
;
471 return mddev_congested(mddev
, bits
);
475 * Generic flush handling for md
478 static void md_end_flush(struct bio
*bio
)
480 struct md_rdev
*rdev
= bio
->bi_private
;
481 struct mddev
*mddev
= rdev
->mddev
;
483 rdev_dec_pending(rdev
, mddev
);
485 if (atomic_dec_and_test(&mddev
->flush_pending
)) {
486 /* The pre-request flush has finished */
487 queue_work(md_wq
, &mddev
->flush_work
);
492 static void md_submit_flush_data(struct work_struct
*ws
);
494 static void submit_flushes(struct work_struct
*ws
)
496 struct mddev
*mddev
= container_of(ws
, struct mddev
, flush_work
);
497 struct md_rdev
*rdev
;
499 mddev
->start_flush
= ktime_get_boottime();
500 INIT_WORK(&mddev
->flush_work
, md_submit_flush_data
);
501 atomic_set(&mddev
->flush_pending
, 1);
503 rdev_for_each_rcu(rdev
, mddev
)
504 if (rdev
->raid_disk
>= 0 &&
505 !test_bit(Faulty
, &rdev
->flags
)) {
506 /* Take two references, one is dropped
507 * when request finishes, one after
508 * we reclaim rcu_read_lock
511 atomic_inc(&rdev
->nr_pending
);
512 atomic_inc(&rdev
->nr_pending
);
514 bi
= bio_alloc_mddev(GFP_NOIO
, 0, mddev
);
515 bi
->bi_end_io
= md_end_flush
;
516 bi
->bi_private
= rdev
;
517 bio_set_dev(bi
, rdev
->bdev
);
518 bi
->bi_opf
= REQ_OP_WRITE
| REQ_PREFLUSH
;
519 atomic_inc(&mddev
->flush_pending
);
522 rdev_dec_pending(rdev
, mddev
);
525 if (atomic_dec_and_test(&mddev
->flush_pending
))
526 queue_work(md_wq
, &mddev
->flush_work
);
529 static void md_submit_flush_data(struct work_struct
*ws
)
531 struct mddev
*mddev
= container_of(ws
, struct mddev
, flush_work
);
532 struct bio
*bio
= mddev
->flush_bio
;
535 * must reset flush_bio before calling into md_handle_request to avoid a
536 * deadlock, because other bios passed md_handle_request suspend check
537 * could wait for this and below md_handle_request could wait for those
538 * bios because of suspend check
540 mddev
->last_flush
= mddev
->start_flush
;
541 mddev
->flush_bio
= NULL
;
542 wake_up(&mddev
->sb_wait
);
544 if (bio
->bi_iter
.bi_size
== 0) {
545 /* an empty barrier - all done */
548 bio
->bi_opf
&= ~REQ_PREFLUSH
;
549 md_handle_request(mddev
, bio
);
554 * Manages consolidation of flushes and submitting any flushes needed for
555 * a bio with REQ_PREFLUSH. Returns true if the bio is finished or is
556 * being finished in another context. Returns false if the flushing is
557 * complete but still needs the I/O portion of the bio to be processed.
559 bool md_flush_request(struct mddev
*mddev
, struct bio
*bio
)
561 ktime_t start
= ktime_get_boottime();
562 spin_lock_irq(&mddev
->lock
);
563 wait_event_lock_irq(mddev
->sb_wait
,
565 ktime_after(mddev
->last_flush
, start
),
567 if (!ktime_after(mddev
->last_flush
, start
)) {
568 WARN_ON(mddev
->flush_bio
);
569 mddev
->flush_bio
= bio
;
572 spin_unlock_irq(&mddev
->lock
);
575 INIT_WORK(&mddev
->flush_work
, submit_flushes
);
576 queue_work(md_wq
, &mddev
->flush_work
);
578 /* flush was performed for some other bio while we waited. */
579 if (bio
->bi_iter
.bi_size
== 0)
580 /* an empty barrier - all done */
583 bio
->bi_opf
&= ~REQ_PREFLUSH
;
589 EXPORT_SYMBOL(md_flush_request
);
591 static inline struct mddev
*mddev_get(struct mddev
*mddev
)
593 atomic_inc(&mddev
->active
);
597 static void mddev_delayed_delete(struct work_struct
*ws
);
599 static void mddev_put(struct mddev
*mddev
)
601 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
603 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
) &&
604 mddev
->ctime
== 0 && !mddev
->hold_active
) {
605 /* Array is not configured at all, and not held active,
607 list_del_init(&mddev
->all_mddevs
);
610 * Call queue_work inside the spinlock so that
611 * flush_workqueue() after mddev_find will succeed in waiting
612 * for the work to be done.
614 INIT_WORK(&mddev
->del_work
, mddev_delayed_delete
);
615 queue_work(md_misc_wq
, &mddev
->del_work
);
617 spin_unlock(&all_mddevs_lock
);
620 static void md_safemode_timeout(struct timer_list
*t
);
622 void mddev_init(struct mddev
*mddev
)
624 kobject_init(&mddev
->kobj
, &md_ktype
);
625 mutex_init(&mddev
->open_mutex
);
626 mutex_init(&mddev
->reconfig_mutex
);
627 mutex_init(&mddev
->bitmap_info
.mutex
);
628 INIT_LIST_HEAD(&mddev
->disks
);
629 INIT_LIST_HEAD(&mddev
->all_mddevs
);
630 timer_setup(&mddev
->safemode_timer
, md_safemode_timeout
, 0);
631 atomic_set(&mddev
->active
, 1);
632 atomic_set(&mddev
->openers
, 0);
633 atomic_set(&mddev
->active_io
, 0);
634 spin_lock_init(&mddev
->lock
);
635 atomic_set(&mddev
->flush_pending
, 0);
636 init_waitqueue_head(&mddev
->sb_wait
);
637 init_waitqueue_head(&mddev
->recovery_wait
);
638 mddev
->reshape_position
= MaxSector
;
639 mddev
->reshape_backwards
= 0;
640 mddev
->last_sync_action
= "none";
641 mddev
->resync_min
= 0;
642 mddev
->resync_max
= MaxSector
;
643 mddev
->level
= LEVEL_NONE
;
645 EXPORT_SYMBOL_GPL(mddev_init
);
647 static struct mddev
*mddev_find(dev_t unit
)
649 struct mddev
*mddev
, *new = NULL
;
651 if (unit
&& MAJOR(unit
) != MD_MAJOR
)
652 unit
&= ~((1<<MdpMinorShift
)-1);
655 spin_lock(&all_mddevs_lock
);
658 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
659 if (mddev
->unit
== unit
) {
661 spin_unlock(&all_mddevs_lock
);
667 list_add(&new->all_mddevs
, &all_mddevs
);
668 spin_unlock(&all_mddevs_lock
);
669 new->hold_active
= UNTIL_IOCTL
;
673 /* find an unused unit number */
674 static int next_minor
= 512;
675 int start
= next_minor
;
679 dev
= MKDEV(MD_MAJOR
, next_minor
);
681 if (next_minor
> MINORMASK
)
683 if (next_minor
== start
) {
684 /* Oh dear, all in use. */
685 spin_unlock(&all_mddevs_lock
);
691 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
692 if (mddev
->unit
== dev
) {
698 new->md_minor
= MINOR(dev
);
699 new->hold_active
= UNTIL_STOP
;
700 list_add(&new->all_mddevs
, &all_mddevs
);
701 spin_unlock(&all_mddevs_lock
);
704 spin_unlock(&all_mddevs_lock
);
706 new = kzalloc(sizeof(*new), GFP_KERNEL
);
711 if (MAJOR(unit
) == MD_MAJOR
)
712 new->md_minor
= MINOR(unit
);
714 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
721 static struct attribute_group md_redundancy_group
;
723 void mddev_unlock(struct mddev
*mddev
)
725 if (mddev
->to_remove
) {
726 /* These cannot be removed under reconfig_mutex as
727 * an access to the files will try to take reconfig_mutex
728 * while holding the file unremovable, which leads to
730 * So hold set sysfs_active while the remove in happeing,
731 * and anything else which might set ->to_remove or my
732 * otherwise change the sysfs namespace will fail with
733 * -EBUSY if sysfs_active is still set.
734 * We set sysfs_active under reconfig_mutex and elsewhere
735 * test it under the same mutex to ensure its correct value
738 struct attribute_group
*to_remove
= mddev
->to_remove
;
739 mddev
->to_remove
= NULL
;
740 mddev
->sysfs_active
= 1;
741 mutex_unlock(&mddev
->reconfig_mutex
);
743 if (mddev
->kobj
.sd
) {
744 if (to_remove
!= &md_redundancy_group
)
745 sysfs_remove_group(&mddev
->kobj
, to_remove
);
746 if (mddev
->pers
== NULL
||
747 mddev
->pers
->sync_request
== NULL
) {
748 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
749 if (mddev
->sysfs_action
)
750 sysfs_put(mddev
->sysfs_action
);
751 mddev
->sysfs_action
= NULL
;
754 mddev
->sysfs_active
= 0;
756 mutex_unlock(&mddev
->reconfig_mutex
);
758 /* As we've dropped the mutex we need a spinlock to
759 * make sure the thread doesn't disappear
761 spin_lock(&pers_lock
);
762 md_wakeup_thread(mddev
->thread
);
763 wake_up(&mddev
->sb_wait
);
764 spin_unlock(&pers_lock
);
766 EXPORT_SYMBOL_GPL(mddev_unlock
);
768 struct md_rdev
*md_find_rdev_nr_rcu(struct mddev
*mddev
, int nr
)
770 struct md_rdev
*rdev
;
772 rdev_for_each_rcu(rdev
, mddev
)
773 if (rdev
->desc_nr
== nr
)
778 EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu
);
780 static struct md_rdev
*find_rdev(struct mddev
*mddev
, dev_t dev
)
782 struct md_rdev
*rdev
;
784 rdev_for_each(rdev
, mddev
)
785 if (rdev
->bdev
->bd_dev
== dev
)
791 struct md_rdev
*md_find_rdev_rcu(struct mddev
*mddev
, dev_t dev
)
793 struct md_rdev
*rdev
;
795 rdev_for_each_rcu(rdev
, mddev
)
796 if (rdev
->bdev
->bd_dev
== dev
)
801 EXPORT_SYMBOL_GPL(md_find_rdev_rcu
);
803 static struct md_personality
*find_pers(int level
, char *clevel
)
805 struct md_personality
*pers
;
806 list_for_each_entry(pers
, &pers_list
, list
) {
807 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
809 if (strcmp(pers
->name
, clevel
)==0)
815 /* return the offset of the super block in 512byte sectors */
816 static inline sector_t
calc_dev_sboffset(struct md_rdev
*rdev
)
818 sector_t num_sectors
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
819 return MD_NEW_SIZE_SECTORS(num_sectors
);
822 static int alloc_disk_sb(struct md_rdev
*rdev
)
824 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
830 void md_rdev_clear(struct md_rdev
*rdev
)
833 put_page(rdev
->sb_page
);
835 rdev
->sb_page
= NULL
;
840 put_page(rdev
->bb_page
);
841 rdev
->bb_page
= NULL
;
843 badblocks_exit(&rdev
->badblocks
);
845 EXPORT_SYMBOL_GPL(md_rdev_clear
);
847 static void super_written(struct bio
*bio
)
849 struct md_rdev
*rdev
= bio
->bi_private
;
850 struct mddev
*mddev
= rdev
->mddev
;
852 if (bio
->bi_status
) {
853 pr_err("md: super_written gets error=%d\n", bio
->bi_status
);
854 md_error(mddev
, rdev
);
855 if (!test_bit(Faulty
, &rdev
->flags
)
856 && (bio
->bi_opf
& MD_FAILFAST
)) {
857 set_bit(MD_SB_NEED_REWRITE
, &mddev
->sb_flags
);
858 set_bit(LastDev
, &rdev
->flags
);
861 clear_bit(LastDev
, &rdev
->flags
);
863 if (atomic_dec_and_test(&mddev
->pending_writes
))
864 wake_up(&mddev
->sb_wait
);
865 rdev_dec_pending(rdev
, mddev
);
869 void md_super_write(struct mddev
*mddev
, struct md_rdev
*rdev
,
870 sector_t sector
, int size
, struct page
*page
)
872 /* write first size bytes of page to sector of rdev
873 * Increment mddev->pending_writes before returning
874 * and decrement it on completion, waking up sb_wait
875 * if zero is reached.
876 * If an error occurred, call md_error
884 if (test_bit(Faulty
, &rdev
->flags
))
887 bio
= md_bio_alloc_sync(mddev
);
889 atomic_inc(&rdev
->nr_pending
);
891 bio_set_dev(bio
, rdev
->meta_bdev
? rdev
->meta_bdev
: rdev
->bdev
);
892 bio
->bi_iter
.bi_sector
= sector
;
893 bio_add_page(bio
, page
, size
, 0);
894 bio
->bi_private
= rdev
;
895 bio
->bi_end_io
= super_written
;
897 if (test_bit(MD_FAILFAST_SUPPORTED
, &mddev
->flags
) &&
898 test_bit(FailFast
, &rdev
->flags
) &&
899 !test_bit(LastDev
, &rdev
->flags
))
901 bio
->bi_opf
= REQ_OP_WRITE
| REQ_SYNC
| REQ_PREFLUSH
| REQ_FUA
| ff
;
903 atomic_inc(&mddev
->pending_writes
);
907 int md_super_wait(struct mddev
*mddev
)
909 /* wait for all superblock writes that were scheduled to complete */
910 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->pending_writes
)==0);
911 if (test_and_clear_bit(MD_SB_NEED_REWRITE
, &mddev
->sb_flags
))
916 int sync_page_io(struct md_rdev
*rdev
, sector_t sector
, int size
,
917 struct page
*page
, int op
, int op_flags
, bool metadata_op
)
919 struct bio
*bio
= md_bio_alloc_sync(rdev
->mddev
);
922 if (metadata_op
&& rdev
->meta_bdev
)
923 bio_set_dev(bio
, rdev
->meta_bdev
);
925 bio_set_dev(bio
, rdev
->bdev
);
926 bio_set_op_attrs(bio
, op
, op_flags
);
928 bio
->bi_iter
.bi_sector
= sector
+ rdev
->sb_start
;
929 else if (rdev
->mddev
->reshape_position
!= MaxSector
&&
930 (rdev
->mddev
->reshape_backwards
==
931 (sector
>= rdev
->mddev
->reshape_position
)))
932 bio
->bi_iter
.bi_sector
= sector
+ rdev
->new_data_offset
;
934 bio
->bi_iter
.bi_sector
= sector
+ rdev
->data_offset
;
935 bio_add_page(bio
, page
, size
, 0);
937 submit_bio_wait(bio
);
939 ret
= !bio
->bi_status
;
943 EXPORT_SYMBOL_GPL(sync_page_io
);
945 static int read_disk_sb(struct md_rdev
*rdev
, int size
)
947 char b
[BDEVNAME_SIZE
];
952 if (!sync_page_io(rdev
, 0, size
, rdev
->sb_page
, REQ_OP_READ
, 0, true))
958 pr_err("md: disabled device %s, could not read superblock.\n",
959 bdevname(rdev
->bdev
,b
));
963 static int md_uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
965 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
966 sb1
->set_uuid1
== sb2
->set_uuid1
&&
967 sb1
->set_uuid2
== sb2
->set_uuid2
&&
968 sb1
->set_uuid3
== sb2
->set_uuid3
;
971 static int md_sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
974 mdp_super_t
*tmp1
, *tmp2
;
976 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
977 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
979 if (!tmp1
|| !tmp2
) {
988 * nr_disks is not constant
993 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
1000 static u32
md_csum_fold(u32 csum
)
1002 csum
= (csum
& 0xffff) + (csum
>> 16);
1003 return (csum
& 0xffff) + (csum
>> 16);
1006 static unsigned int calc_sb_csum(mdp_super_t
*sb
)
1009 u32
*sb32
= (u32
*)sb
;
1011 unsigned int disk_csum
, csum
;
1013 disk_csum
= sb
->sb_csum
;
1016 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
1018 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
1021 /* This used to use csum_partial, which was wrong for several
1022 * reasons including that different results are returned on
1023 * different architectures. It isn't critical that we get exactly
1024 * the same return value as before (we always csum_fold before
1025 * testing, and that removes any differences). However as we
1026 * know that csum_partial always returned a 16bit value on
1027 * alphas, do a fold to maximise conformity to previous behaviour.
1029 sb
->sb_csum
= md_csum_fold(disk_csum
);
1031 sb
->sb_csum
= disk_csum
;
1037 * Handle superblock details.
1038 * We want to be able to handle multiple superblock formats
1039 * so we have a common interface to them all, and an array of
1040 * different handlers.
1041 * We rely on user-space to write the initial superblock, and support
1042 * reading and updating of superblocks.
1043 * Interface methods are:
1044 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
1045 * loads and validates a superblock on dev.
1046 * if refdev != NULL, compare superblocks on both devices
1048 * 0 - dev has a superblock that is compatible with refdev
1049 * 1 - dev has a superblock that is compatible and newer than refdev
1050 * so dev should be used as the refdev in future
1051 * -EINVAL superblock incompatible or invalid
1052 * -othererror e.g. -EIO
1054 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
1055 * Verify that dev is acceptable into mddev.
1056 * The first time, mddev->raid_disks will be 0, and data from
1057 * dev should be merged in. Subsequent calls check that dev
1058 * is new enough. Return 0 or -EINVAL
1060 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
1061 * Update the superblock for rdev with data in mddev
1062 * This does not write to disc.
1068 struct module
*owner
;
1069 int (*load_super
)(struct md_rdev
*rdev
,
1070 struct md_rdev
*refdev
,
1072 int (*validate_super
)(struct mddev
*mddev
,
1073 struct md_rdev
*rdev
);
1074 void (*sync_super
)(struct mddev
*mddev
,
1075 struct md_rdev
*rdev
);
1076 unsigned long long (*rdev_size_change
)(struct md_rdev
*rdev
,
1077 sector_t num_sectors
);
1078 int (*allow_new_offset
)(struct md_rdev
*rdev
,
1079 unsigned long long new_offset
);
1083 * Check that the given mddev has no bitmap.
1085 * This function is called from the run method of all personalities that do not
1086 * support bitmaps. It prints an error message and returns non-zero if mddev
1087 * has a bitmap. Otherwise, it returns 0.
1090 int md_check_no_bitmap(struct mddev
*mddev
)
1092 if (!mddev
->bitmap_info
.file
&& !mddev
->bitmap_info
.offset
)
1094 pr_warn("%s: bitmaps are not supported for %s\n",
1095 mdname(mddev
), mddev
->pers
->name
);
1098 EXPORT_SYMBOL(md_check_no_bitmap
);
1101 * load_super for 0.90.0
1103 static int super_90_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
1105 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1108 bool spare_disk
= true;
1111 * Calculate the position of the superblock (512byte sectors),
1112 * it's at the end of the disk.
1114 * It also happens to be a multiple of 4Kb.
1116 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1118 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
1124 bdevname(rdev
->bdev
, b
);
1125 sb
= page_address(rdev
->sb_page
);
1127 if (sb
->md_magic
!= MD_SB_MAGIC
) {
1128 pr_warn("md: invalid raid superblock magic on %s\n", b
);
1132 if (sb
->major_version
!= 0 ||
1133 sb
->minor_version
< 90 ||
1134 sb
->minor_version
> 91) {
1135 pr_warn("Bad version number %d.%d on %s\n",
1136 sb
->major_version
, sb
->minor_version
, b
);
1140 if (sb
->raid_disks
<= 0)
1143 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
1144 pr_warn("md: invalid superblock checksum on %s\n", b
);
1148 rdev
->preferred_minor
= sb
->md_minor
;
1149 rdev
->data_offset
= 0;
1150 rdev
->new_data_offset
= 0;
1151 rdev
->sb_size
= MD_SB_BYTES
;
1152 rdev
->badblocks
.shift
= -1;
1154 if (sb
->level
== LEVEL_MULTIPATH
)
1157 rdev
->desc_nr
= sb
->this_disk
.number
;
1159 /* not spare disk, or LEVEL_MULTIPATH */
1160 if (sb
->level
== LEVEL_MULTIPATH
||
1161 (rdev
->desc_nr
>= 0 &&
1162 rdev
->desc_nr
< MD_SB_DISKS
&&
1163 sb
->disks
[rdev
->desc_nr
].state
&
1164 ((1<<MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
))))
1174 mdp_super_t
*refsb
= page_address(refdev
->sb_page
);
1175 if (!md_uuid_equal(refsb
, sb
)) {
1176 pr_warn("md: %s has different UUID to %s\n",
1177 b
, bdevname(refdev
->bdev
,b2
));
1180 if (!md_sb_equal(refsb
, sb
)) {
1181 pr_warn("md: %s has same UUID but different superblock to %s\n",
1182 b
, bdevname(refdev
->bdev
, b2
));
1186 ev2
= md_event(refsb
);
1188 if (!spare_disk
&& ev1
> ev2
)
1193 rdev
->sectors
= rdev
->sb_start
;
1194 /* Limit to 4TB as metadata cannot record more than that.
1195 * (not needed for Linear and RAID0 as metadata doesn't
1198 if ((u64
)rdev
->sectors
>= (2ULL << 32) && sb
->level
>= 1)
1199 rdev
->sectors
= (sector_t
)(2ULL << 32) - 2;
1201 if (rdev
->sectors
< ((sector_t
)sb
->size
) * 2 && sb
->level
>= 1)
1202 /* "this cannot possibly happen" ... */
1210 * validate_super for 0.90.0
1212 static int super_90_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
1215 mdp_super_t
*sb
= page_address(rdev
->sb_page
);
1216 __u64 ev1
= md_event(sb
);
1218 rdev
->raid_disk
= -1;
1219 clear_bit(Faulty
, &rdev
->flags
);
1220 clear_bit(In_sync
, &rdev
->flags
);
1221 clear_bit(Bitmap_sync
, &rdev
->flags
);
1222 clear_bit(WriteMostly
, &rdev
->flags
);
1224 if (mddev
->raid_disks
== 0) {
1225 mddev
->major_version
= 0;
1226 mddev
->minor_version
= sb
->minor_version
;
1227 mddev
->patch_version
= sb
->patch_version
;
1228 mddev
->external
= 0;
1229 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
1230 mddev
->ctime
= sb
->ctime
;
1231 mddev
->utime
= sb
->utime
;
1232 mddev
->level
= sb
->level
;
1233 mddev
->clevel
[0] = 0;
1234 mddev
->layout
= sb
->layout
;
1235 mddev
->raid_disks
= sb
->raid_disks
;
1236 mddev
->dev_sectors
= ((sector_t
)sb
->size
) * 2;
1237 mddev
->events
= ev1
;
1238 mddev
->bitmap_info
.offset
= 0;
1239 mddev
->bitmap_info
.space
= 0;
1240 /* bitmap can use 60 K after the 4K superblocks */
1241 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
1242 mddev
->bitmap_info
.default_space
= 64*2 - (MD_SB_BYTES
>> 9);
1243 mddev
->reshape_backwards
= 0;
1245 if (mddev
->minor_version
>= 91) {
1246 mddev
->reshape_position
= sb
->reshape_position
;
1247 mddev
->delta_disks
= sb
->delta_disks
;
1248 mddev
->new_level
= sb
->new_level
;
1249 mddev
->new_layout
= sb
->new_layout
;
1250 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
1251 if (mddev
->delta_disks
< 0)
1252 mddev
->reshape_backwards
= 1;
1254 mddev
->reshape_position
= MaxSector
;
1255 mddev
->delta_disks
= 0;
1256 mddev
->new_level
= mddev
->level
;
1257 mddev
->new_layout
= mddev
->layout
;
1258 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1260 if (mddev
->level
== 0)
1263 if (sb
->state
& (1<<MD_SB_CLEAN
))
1264 mddev
->recovery_cp
= MaxSector
;
1266 if (sb
->events_hi
== sb
->cp_events_hi
&&
1267 sb
->events_lo
== sb
->cp_events_lo
) {
1268 mddev
->recovery_cp
= sb
->recovery_cp
;
1270 mddev
->recovery_cp
= 0;
1273 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
1274 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
1275 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
1276 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
1278 mddev
->max_disks
= MD_SB_DISKS
;
1280 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
1281 mddev
->bitmap_info
.file
== NULL
) {
1282 mddev
->bitmap_info
.offset
=
1283 mddev
->bitmap_info
.default_offset
;
1284 mddev
->bitmap_info
.space
=
1285 mddev
->bitmap_info
.default_space
;
1288 } else if (mddev
->pers
== NULL
) {
1289 /* Insist on good event counter while assembling, except
1290 * for spares (which don't need an event count) */
1292 if (sb
->disks
[rdev
->desc_nr
].state
& (
1293 (1<<MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
)))
1294 if (ev1
< mddev
->events
)
1296 } else if (mddev
->bitmap
) {
1297 /* if adding to array with a bitmap, then we can accept an
1298 * older device ... but not too old.
1300 if (ev1
< mddev
->bitmap
->events_cleared
)
1302 if (ev1
< mddev
->events
)
1303 set_bit(Bitmap_sync
, &rdev
->flags
);
1305 if (ev1
< mddev
->events
)
1306 /* just a hot-add of a new device, leave raid_disk at -1 */
1310 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1311 desc
= sb
->disks
+ rdev
->desc_nr
;
1313 if (desc
->state
& (1<<MD_DISK_FAULTY
))
1314 set_bit(Faulty
, &rdev
->flags
);
1315 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
1316 desc->raid_disk < mddev->raid_disks */) {
1317 set_bit(In_sync
, &rdev
->flags
);
1318 rdev
->raid_disk
= desc
->raid_disk
;
1319 rdev
->saved_raid_disk
= desc
->raid_disk
;
1320 } else if (desc
->state
& (1<<MD_DISK_ACTIVE
)) {
1321 /* active but not in sync implies recovery up to
1322 * reshape position. We don't know exactly where
1323 * that is, so set to zero for now */
1324 if (mddev
->minor_version
>= 91) {
1325 rdev
->recovery_offset
= 0;
1326 rdev
->raid_disk
= desc
->raid_disk
;
1329 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
1330 set_bit(WriteMostly
, &rdev
->flags
);
1331 if (desc
->state
& (1<<MD_DISK_FAILFAST
))
1332 set_bit(FailFast
, &rdev
->flags
);
1333 } else /* MULTIPATH are always insync */
1334 set_bit(In_sync
, &rdev
->flags
);
1339 * sync_super for 0.90.0
1341 static void super_90_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
1344 struct md_rdev
*rdev2
;
1345 int next_spare
= mddev
->raid_disks
;
1347 /* make rdev->sb match mddev data..
1350 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1351 * 3/ any empty disks < next_spare become removed
1353 * disks[0] gets initialised to REMOVED because
1354 * we cannot be sure from other fields if it has
1355 * been initialised or not.
1358 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
1360 rdev
->sb_size
= MD_SB_BYTES
;
1362 sb
= page_address(rdev
->sb_page
);
1364 memset(sb
, 0, sizeof(*sb
));
1366 sb
->md_magic
= MD_SB_MAGIC
;
1367 sb
->major_version
= mddev
->major_version
;
1368 sb
->patch_version
= mddev
->patch_version
;
1369 sb
->gvalid_words
= 0; /* ignored */
1370 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
1371 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
1372 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
1373 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
1375 sb
->ctime
= clamp_t(time64_t
, mddev
->ctime
, 0, U32_MAX
);
1376 sb
->level
= mddev
->level
;
1377 sb
->size
= mddev
->dev_sectors
/ 2;
1378 sb
->raid_disks
= mddev
->raid_disks
;
1379 sb
->md_minor
= mddev
->md_minor
;
1380 sb
->not_persistent
= 0;
1381 sb
->utime
= clamp_t(time64_t
, mddev
->utime
, 0, U32_MAX
);
1383 sb
->events_hi
= (mddev
->events
>>32);
1384 sb
->events_lo
= (u32
)mddev
->events
;
1386 if (mddev
->reshape_position
== MaxSector
)
1387 sb
->minor_version
= 90;
1389 sb
->minor_version
= 91;
1390 sb
->reshape_position
= mddev
->reshape_position
;
1391 sb
->new_level
= mddev
->new_level
;
1392 sb
->delta_disks
= mddev
->delta_disks
;
1393 sb
->new_layout
= mddev
->new_layout
;
1394 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1396 mddev
->minor_version
= sb
->minor_version
;
1399 sb
->recovery_cp
= mddev
->recovery_cp
;
1400 sb
->cp_events_hi
= (mddev
->events
>>32);
1401 sb
->cp_events_lo
= (u32
)mddev
->events
;
1402 if (mddev
->recovery_cp
== MaxSector
)
1403 sb
->state
= (1<< MD_SB_CLEAN
);
1405 sb
->recovery_cp
= 0;
1407 sb
->layout
= mddev
->layout
;
1408 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1410 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
)
1411 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1413 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1414 rdev_for_each(rdev2
, mddev
) {
1417 int is_active
= test_bit(In_sync
, &rdev2
->flags
);
1419 if (rdev2
->raid_disk
>= 0 &&
1420 sb
->minor_version
>= 91)
1421 /* we have nowhere to store the recovery_offset,
1422 * but if it is not below the reshape_position,
1423 * we can piggy-back on that.
1426 if (rdev2
->raid_disk
< 0 ||
1427 test_bit(Faulty
, &rdev2
->flags
))
1430 desc_nr
= rdev2
->raid_disk
;
1432 desc_nr
= next_spare
++;
1433 rdev2
->desc_nr
= desc_nr
;
1434 d
= &sb
->disks
[rdev2
->desc_nr
];
1436 d
->number
= rdev2
->desc_nr
;
1437 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1438 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1440 d
->raid_disk
= rdev2
->raid_disk
;
1442 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1443 if (test_bit(Faulty
, &rdev2
->flags
))
1444 d
->state
= (1<<MD_DISK_FAULTY
);
1445 else if (is_active
) {
1446 d
->state
= (1<<MD_DISK_ACTIVE
);
1447 if (test_bit(In_sync
, &rdev2
->flags
))
1448 d
->state
|= (1<<MD_DISK_SYNC
);
1456 if (test_bit(WriteMostly
, &rdev2
->flags
))
1457 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1458 if (test_bit(FailFast
, &rdev2
->flags
))
1459 d
->state
|= (1<<MD_DISK_FAILFAST
);
1461 /* now set the "removed" and "faulty" bits on any missing devices */
1462 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1463 mdp_disk_t
*d
= &sb
->disks
[i
];
1464 if (d
->state
== 0 && d
->number
== 0) {
1467 d
->state
= (1<<MD_DISK_REMOVED
);
1468 d
->state
|= (1<<MD_DISK_FAULTY
);
1472 sb
->nr_disks
= nr_disks
;
1473 sb
->active_disks
= active
;
1474 sb
->working_disks
= working
;
1475 sb
->failed_disks
= failed
;
1476 sb
->spare_disks
= spare
;
1478 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1479 sb
->sb_csum
= calc_sb_csum(sb
);
1483 * rdev_size_change for 0.90.0
1485 static unsigned long long
1486 super_90_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
1488 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1489 return 0; /* component must fit device */
1490 if (rdev
->mddev
->bitmap_info
.offset
)
1491 return 0; /* can't move bitmap */
1492 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1493 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1494 num_sectors
= rdev
->sb_start
;
1495 /* Limit to 4TB as metadata cannot record more than that.
1496 * 4TB == 2^32 KB, or 2*2^32 sectors.
1498 if ((u64
)num_sectors
>= (2ULL << 32) && rdev
->mddev
->level
>= 1)
1499 num_sectors
= (sector_t
)(2ULL << 32) - 2;
1501 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1503 } while (md_super_wait(rdev
->mddev
) < 0);
1508 super_90_allow_new_offset(struct md_rdev
*rdev
, unsigned long long new_offset
)
1510 /* non-zero offset changes not possible with v0.90 */
1511 return new_offset
== 0;
1515 * version 1 superblock
1518 static __le32
calc_sb_1_csum(struct mdp_superblock_1
*sb
)
1522 unsigned long long newcsum
;
1523 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1524 __le32
*isuper
= (__le32
*)sb
;
1526 disk_csum
= sb
->sb_csum
;
1529 for (; size
>= 4; size
-= 4)
1530 newcsum
+= le32_to_cpu(*isuper
++);
1533 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1535 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1536 sb
->sb_csum
= disk_csum
;
1537 return cpu_to_le32(csum
);
1540 static int super_1_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
1542 struct mdp_superblock_1
*sb
;
1546 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1548 bool spare_disk
= true;
1551 * Calculate the position of the superblock in 512byte sectors.
1552 * It is always aligned to a 4K boundary and
1553 * depeding on minor_version, it can be:
1554 * 0: At least 8K, but less than 12K, from end of device
1555 * 1: At start of device
1556 * 2: 4K from start of device.
1558 switch(minor_version
) {
1560 sb_start
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1562 sb_start
&= ~(sector_t
)(4*2-1);
1573 rdev
->sb_start
= sb_start
;
1575 /* superblock is rarely larger than 1K, but it can be larger,
1576 * and it is safe to read 4k, so we do that
1578 ret
= read_disk_sb(rdev
, 4096);
1579 if (ret
) return ret
;
1581 sb
= page_address(rdev
->sb_page
);
1583 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1584 sb
->major_version
!= cpu_to_le32(1) ||
1585 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1586 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1587 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1590 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1591 pr_warn("md: invalid superblock checksum on %s\n",
1592 bdevname(rdev
->bdev
,b
));
1595 if (le64_to_cpu(sb
->data_size
) < 10) {
1596 pr_warn("md: data_size too small on %s\n",
1597 bdevname(rdev
->bdev
,b
));
1602 memcmp(sb
->pad3
, sb
->pad3
+1, sizeof(sb
->pad3
) - sizeof(sb
->pad3
[1])))
1603 /* Some padding is non-zero, might be a new feature */
1606 rdev
->preferred_minor
= 0xffff;
1607 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1608 rdev
->new_data_offset
= rdev
->data_offset
;
1609 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
) &&
1610 (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_NEW_OFFSET
))
1611 rdev
->new_data_offset
+= (s32
)le32_to_cpu(sb
->new_offset
);
1612 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1614 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1615 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1616 if (rdev
->sb_size
& bmask
)
1617 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1620 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1623 && rdev
->new_data_offset
< sb_start
+ (rdev
->sb_size
/512))
1626 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1629 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1631 if (!rdev
->bb_page
) {
1632 rdev
->bb_page
= alloc_page(GFP_KERNEL
);
1636 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BAD_BLOCKS
) &&
1637 rdev
->badblocks
.count
== 0) {
1638 /* need to load the bad block list.
1639 * Currently we limit it to one page.
1645 int sectors
= le16_to_cpu(sb
->bblog_size
);
1646 if (sectors
> (PAGE_SIZE
/ 512))
1648 offset
= le32_to_cpu(sb
->bblog_offset
);
1651 bb_sector
= (long long)offset
;
1652 if (!sync_page_io(rdev
, bb_sector
, sectors
<< 9,
1653 rdev
->bb_page
, REQ_OP_READ
, 0, true))
1655 bbp
= (__le64
*)page_address(rdev
->bb_page
);
1656 rdev
->badblocks
.shift
= sb
->bblog_shift
;
1657 for (i
= 0 ; i
< (sectors
<< (9-3)) ; i
++, bbp
++) {
1658 u64 bb
= le64_to_cpu(*bbp
);
1659 int count
= bb
& (0x3ff);
1660 u64 sector
= bb
>> 10;
1661 sector
<<= sb
->bblog_shift
;
1662 count
<<= sb
->bblog_shift
;
1665 if (badblocks_set(&rdev
->badblocks
, sector
, count
, 1))
1668 } else if (sb
->bblog_offset
!= 0)
1669 rdev
->badblocks
.shift
= 0;
1671 if ((le32_to_cpu(sb
->feature_map
) &
1672 (MD_FEATURE_PPL
| MD_FEATURE_MULTIPLE_PPLS
))) {
1673 rdev
->ppl
.offset
= (__s16
)le16_to_cpu(sb
->ppl
.offset
);
1674 rdev
->ppl
.size
= le16_to_cpu(sb
->ppl
.size
);
1675 rdev
->ppl
.sector
= rdev
->sb_start
+ rdev
->ppl
.offset
;
1678 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RAID0_LAYOUT
) &&
1682 /* not spare disk, or LEVEL_MULTIPATH */
1683 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
) ||
1684 (rdev
->desc_nr
>= 0 &&
1685 rdev
->desc_nr
< le32_to_cpu(sb
->max_dev
) &&
1686 (le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) < MD_DISK_ROLE_MAX
||
1687 le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) == MD_DISK_ROLE_JOURNAL
)))
1697 struct mdp_superblock_1
*refsb
= page_address(refdev
->sb_page
);
1699 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1700 sb
->level
!= refsb
->level
||
1701 sb
->layout
!= refsb
->layout
||
1702 sb
->chunksize
!= refsb
->chunksize
) {
1703 pr_warn("md: %s has strangely different superblock to %s\n",
1704 bdevname(rdev
->bdev
,b
),
1705 bdevname(refdev
->bdev
,b2
));
1708 ev1
= le64_to_cpu(sb
->events
);
1709 ev2
= le64_to_cpu(refsb
->events
);
1711 if (!spare_disk
&& ev1
> ev2
)
1716 if (minor_version
) {
1717 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9);
1718 sectors
-= rdev
->data_offset
;
1720 sectors
= rdev
->sb_start
;
1721 if (sectors
< le64_to_cpu(sb
->data_size
))
1723 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1727 static int super_1_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
1729 struct mdp_superblock_1
*sb
= page_address(rdev
->sb_page
);
1730 __u64 ev1
= le64_to_cpu(sb
->events
);
1732 rdev
->raid_disk
= -1;
1733 clear_bit(Faulty
, &rdev
->flags
);
1734 clear_bit(In_sync
, &rdev
->flags
);
1735 clear_bit(Bitmap_sync
, &rdev
->flags
);
1736 clear_bit(WriteMostly
, &rdev
->flags
);
1738 if (mddev
->raid_disks
== 0) {
1739 mddev
->major_version
= 1;
1740 mddev
->patch_version
= 0;
1741 mddev
->external
= 0;
1742 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1743 mddev
->ctime
= le64_to_cpu(sb
->ctime
);
1744 mddev
->utime
= le64_to_cpu(sb
->utime
);
1745 mddev
->level
= le32_to_cpu(sb
->level
);
1746 mddev
->clevel
[0] = 0;
1747 mddev
->layout
= le32_to_cpu(sb
->layout
);
1748 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1749 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1750 mddev
->events
= ev1
;
1751 mddev
->bitmap_info
.offset
= 0;
1752 mddev
->bitmap_info
.space
= 0;
1753 /* Default location for bitmap is 1K after superblock
1754 * using 3K - total of 4K
1756 mddev
->bitmap_info
.default_offset
= 1024 >> 9;
1757 mddev
->bitmap_info
.default_space
= (4096-1024) >> 9;
1758 mddev
->reshape_backwards
= 0;
1760 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1761 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1763 mddev
->max_disks
= (4096-256)/2;
1765 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1766 mddev
->bitmap_info
.file
== NULL
) {
1767 mddev
->bitmap_info
.offset
=
1768 (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1769 /* Metadata doesn't record how much space is available.
1770 * For 1.0, we assume we can use up to the superblock
1771 * if before, else to 4K beyond superblock.
1772 * For others, assume no change is possible.
1774 if (mddev
->minor_version
> 0)
1775 mddev
->bitmap_info
.space
= 0;
1776 else if (mddev
->bitmap_info
.offset
> 0)
1777 mddev
->bitmap_info
.space
=
1778 8 - mddev
->bitmap_info
.offset
;
1780 mddev
->bitmap_info
.space
=
1781 -mddev
->bitmap_info
.offset
;
1784 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1785 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1786 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1787 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1788 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1789 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1790 if (mddev
->delta_disks
< 0 ||
1791 (mddev
->delta_disks
== 0 &&
1792 (le32_to_cpu(sb
->feature_map
)
1793 & MD_FEATURE_RESHAPE_BACKWARDS
)))
1794 mddev
->reshape_backwards
= 1;
1796 mddev
->reshape_position
= MaxSector
;
1797 mddev
->delta_disks
= 0;
1798 mddev
->new_level
= mddev
->level
;
1799 mddev
->new_layout
= mddev
->layout
;
1800 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1803 if (mddev
->level
== 0 &&
1804 !(le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RAID0_LAYOUT
))
1807 if (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_JOURNAL
)
1808 set_bit(MD_HAS_JOURNAL
, &mddev
->flags
);
1810 if (le32_to_cpu(sb
->feature_map
) &
1811 (MD_FEATURE_PPL
| MD_FEATURE_MULTIPLE_PPLS
)) {
1812 if (le32_to_cpu(sb
->feature_map
) &
1813 (MD_FEATURE_BITMAP_OFFSET
| MD_FEATURE_JOURNAL
))
1815 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_PPL
) &&
1816 (le32_to_cpu(sb
->feature_map
) &
1817 MD_FEATURE_MULTIPLE_PPLS
))
1819 set_bit(MD_HAS_PPL
, &mddev
->flags
);
1821 } else if (mddev
->pers
== NULL
) {
1822 /* Insist of good event counter while assembling, except for
1823 * spares (which don't need an event count) */
1825 if (rdev
->desc_nr
>= 0 &&
1826 rdev
->desc_nr
< le32_to_cpu(sb
->max_dev
) &&
1827 (le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) < MD_DISK_ROLE_MAX
||
1828 le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) == MD_DISK_ROLE_JOURNAL
))
1829 if (ev1
< mddev
->events
)
1831 } else if (mddev
->bitmap
) {
1832 /* If adding to array with a bitmap, then we can accept an
1833 * older device, but not too old.
1835 if (ev1
< mddev
->bitmap
->events_cleared
)
1837 if (ev1
< mddev
->events
)
1838 set_bit(Bitmap_sync
, &rdev
->flags
);
1840 if (ev1
< mddev
->events
)
1841 /* just a hot-add of a new device, leave raid_disk at -1 */
1844 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1846 if (rdev
->desc_nr
< 0 ||
1847 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1848 role
= MD_DISK_ROLE_SPARE
;
1851 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1853 case MD_DISK_ROLE_SPARE
: /* spare */
1855 case MD_DISK_ROLE_FAULTY
: /* faulty */
1856 set_bit(Faulty
, &rdev
->flags
);
1858 case MD_DISK_ROLE_JOURNAL
: /* journal device */
1859 if (!(le32_to_cpu(sb
->feature_map
) & MD_FEATURE_JOURNAL
)) {
1860 /* journal device without journal feature */
1861 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
1864 set_bit(Journal
, &rdev
->flags
);
1865 rdev
->journal_tail
= le64_to_cpu(sb
->journal_tail
);
1866 rdev
->raid_disk
= 0;
1869 rdev
->saved_raid_disk
= role
;
1870 if ((le32_to_cpu(sb
->feature_map
) &
1871 MD_FEATURE_RECOVERY_OFFSET
)) {
1872 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1873 if (!(le32_to_cpu(sb
->feature_map
) &
1874 MD_FEATURE_RECOVERY_BITMAP
))
1875 rdev
->saved_raid_disk
= -1;
1878 * If the array is FROZEN, then the device can't
1879 * be in_sync with rest of array.
1881 if (!test_bit(MD_RECOVERY_FROZEN
,
1883 set_bit(In_sync
, &rdev
->flags
);
1885 rdev
->raid_disk
= role
;
1888 if (sb
->devflags
& WriteMostly1
)
1889 set_bit(WriteMostly
, &rdev
->flags
);
1890 if (sb
->devflags
& FailFast1
)
1891 set_bit(FailFast
, &rdev
->flags
);
1892 if (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_REPLACEMENT
)
1893 set_bit(Replacement
, &rdev
->flags
);
1894 } else /* MULTIPATH are always insync */
1895 set_bit(In_sync
, &rdev
->flags
);
1900 static void super_1_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
1902 struct mdp_superblock_1
*sb
;
1903 struct md_rdev
*rdev2
;
1905 /* make rdev->sb match mddev and rdev data. */
1907 sb
= page_address(rdev
->sb_page
);
1909 sb
->feature_map
= 0;
1911 sb
->recovery_offset
= cpu_to_le64(0);
1912 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1914 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1915 sb
->events
= cpu_to_le64(mddev
->events
);
1917 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1918 else if (test_bit(MD_JOURNAL_CLEAN
, &mddev
->flags
))
1919 sb
->resync_offset
= cpu_to_le64(MaxSector
);
1921 sb
->resync_offset
= cpu_to_le64(0);
1923 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1925 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1926 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
1927 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
1928 sb
->level
= cpu_to_le32(mddev
->level
);
1929 sb
->layout
= cpu_to_le32(mddev
->layout
);
1930 if (test_bit(FailFast
, &rdev
->flags
))
1931 sb
->devflags
|= FailFast1
;
1933 sb
->devflags
&= ~FailFast1
;
1935 if (test_bit(WriteMostly
, &rdev
->flags
))
1936 sb
->devflags
|= WriteMostly1
;
1938 sb
->devflags
&= ~WriteMostly1
;
1939 sb
->data_offset
= cpu_to_le64(rdev
->data_offset
);
1940 sb
->data_size
= cpu_to_le64(rdev
->sectors
);
1942 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
) {
1943 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_info
.offset
);
1944 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1947 if (rdev
->raid_disk
>= 0 && !test_bit(Journal
, &rdev
->flags
) &&
1948 !test_bit(In_sync
, &rdev
->flags
)) {
1950 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1951 sb
->recovery_offset
=
1952 cpu_to_le64(rdev
->recovery_offset
);
1953 if (rdev
->saved_raid_disk
>= 0 && mddev
->bitmap
)
1955 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP
);
1957 /* Note: recovery_offset and journal_tail share space */
1958 if (test_bit(Journal
, &rdev
->flags
))
1959 sb
->journal_tail
= cpu_to_le64(rdev
->journal_tail
);
1960 if (test_bit(Replacement
, &rdev
->flags
))
1962 cpu_to_le32(MD_FEATURE_REPLACEMENT
);
1964 if (mddev
->reshape_position
!= MaxSector
) {
1965 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1966 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1967 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1968 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1969 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1970 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
1971 if (mddev
->delta_disks
== 0 &&
1972 mddev
->reshape_backwards
)
1974 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS
);
1975 if (rdev
->new_data_offset
!= rdev
->data_offset
) {
1977 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET
);
1978 sb
->new_offset
= cpu_to_le32((__u32
)(rdev
->new_data_offset
1979 - rdev
->data_offset
));
1983 if (mddev_is_clustered(mddev
))
1984 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_CLUSTERED
);
1986 if (rdev
->badblocks
.count
== 0)
1987 /* Nothing to do for bad blocks*/ ;
1988 else if (sb
->bblog_offset
== 0)
1989 /* Cannot record bad blocks on this device */
1990 md_error(mddev
, rdev
);
1992 struct badblocks
*bb
= &rdev
->badblocks
;
1993 __le64
*bbp
= (__le64
*)page_address(rdev
->bb_page
);
1995 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_BAD_BLOCKS
);
2000 seq
= read_seqbegin(&bb
->lock
);
2002 memset(bbp
, 0xff, PAGE_SIZE
);
2004 for (i
= 0 ; i
< bb
->count
; i
++) {
2005 u64 internal_bb
= p
[i
];
2006 u64 store_bb
= ((BB_OFFSET(internal_bb
) << 10)
2007 | BB_LEN(internal_bb
));
2008 bbp
[i
] = cpu_to_le64(store_bb
);
2011 if (read_seqretry(&bb
->lock
, seq
))
2014 bb
->sector
= (rdev
->sb_start
+
2015 (int)le32_to_cpu(sb
->bblog_offset
));
2016 bb
->size
= le16_to_cpu(sb
->bblog_size
);
2021 rdev_for_each(rdev2
, mddev
)
2022 if (rdev2
->desc_nr
+1 > max_dev
)
2023 max_dev
= rdev2
->desc_nr
+1;
2025 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
2027 sb
->max_dev
= cpu_to_le32(max_dev
);
2028 rdev
->sb_size
= max_dev
* 2 + 256;
2029 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
2030 if (rdev
->sb_size
& bmask
)
2031 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
2033 max_dev
= le32_to_cpu(sb
->max_dev
);
2035 for (i
=0; i
<max_dev
;i
++)
2036 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_SPARE
);
2038 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
))
2039 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_JOURNAL
);
2041 if (test_bit(MD_HAS_PPL
, &mddev
->flags
)) {
2042 if (test_bit(MD_HAS_MULTIPLE_PPLS
, &mddev
->flags
))
2044 cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS
);
2046 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_PPL
);
2047 sb
->ppl
.offset
= cpu_to_le16(rdev
->ppl
.offset
);
2048 sb
->ppl
.size
= cpu_to_le16(rdev
->ppl
.size
);
2051 rdev_for_each(rdev2
, mddev
) {
2053 if (test_bit(Faulty
, &rdev2
->flags
))
2054 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_FAULTY
);
2055 else if (test_bit(In_sync
, &rdev2
->flags
))
2056 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
2057 else if (test_bit(Journal
, &rdev2
->flags
))
2058 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_JOURNAL
);
2059 else if (rdev2
->raid_disk
>= 0)
2060 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
2062 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_SPARE
);
2065 sb
->sb_csum
= calc_sb_1_csum(sb
);
2068 static unsigned long long
2069 super_1_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
2071 struct mdp_superblock_1
*sb
;
2072 sector_t max_sectors
;
2073 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
2074 return 0; /* component must fit device */
2075 if (rdev
->data_offset
!= rdev
->new_data_offset
)
2076 return 0; /* too confusing */
2077 if (rdev
->sb_start
< rdev
->data_offset
) {
2078 /* minor versions 1 and 2; superblock before data */
2079 max_sectors
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
2080 max_sectors
-= rdev
->data_offset
;
2081 if (!num_sectors
|| num_sectors
> max_sectors
)
2082 num_sectors
= max_sectors
;
2083 } else if (rdev
->mddev
->bitmap_info
.offset
) {
2084 /* minor version 0 with bitmap we can't move */
2087 /* minor version 0; superblock after data */
2089 sb_start
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) - 8*2;
2090 sb_start
&= ~(sector_t
)(4*2 - 1);
2091 max_sectors
= rdev
->sectors
+ sb_start
- rdev
->sb_start
;
2092 if (!num_sectors
|| num_sectors
> max_sectors
)
2093 num_sectors
= max_sectors
;
2094 rdev
->sb_start
= sb_start
;
2096 sb
= page_address(rdev
->sb_page
);
2097 sb
->data_size
= cpu_to_le64(num_sectors
);
2098 sb
->super_offset
= cpu_to_le64(rdev
->sb_start
);
2099 sb
->sb_csum
= calc_sb_1_csum(sb
);
2101 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
2103 } while (md_super_wait(rdev
->mddev
) < 0);
2109 super_1_allow_new_offset(struct md_rdev
*rdev
,
2110 unsigned long long new_offset
)
2112 /* All necessary checks on new >= old have been done */
2113 struct bitmap
*bitmap
;
2114 if (new_offset
>= rdev
->data_offset
)
2117 /* with 1.0 metadata, there is no metadata to tread on
2118 * so we can always move back */
2119 if (rdev
->mddev
->minor_version
== 0)
2122 /* otherwise we must be sure not to step on
2123 * any metadata, so stay:
2124 * 36K beyond start of superblock
2125 * beyond end of badblocks
2126 * beyond write-intent bitmap
2128 if (rdev
->sb_start
+ (32+4)*2 > new_offset
)
2130 bitmap
= rdev
->mddev
->bitmap
;
2131 if (bitmap
&& !rdev
->mddev
->bitmap_info
.file
&&
2132 rdev
->sb_start
+ rdev
->mddev
->bitmap_info
.offset
+
2133 bitmap
->storage
.file_pages
* (PAGE_SIZE
>>9) > new_offset
)
2135 if (rdev
->badblocks
.sector
+ rdev
->badblocks
.size
> new_offset
)
2141 static struct super_type super_types
[] = {
2144 .owner
= THIS_MODULE
,
2145 .load_super
= super_90_load
,
2146 .validate_super
= super_90_validate
,
2147 .sync_super
= super_90_sync
,
2148 .rdev_size_change
= super_90_rdev_size_change
,
2149 .allow_new_offset
= super_90_allow_new_offset
,
2153 .owner
= THIS_MODULE
,
2154 .load_super
= super_1_load
,
2155 .validate_super
= super_1_validate
,
2156 .sync_super
= super_1_sync
,
2157 .rdev_size_change
= super_1_rdev_size_change
,
2158 .allow_new_offset
= super_1_allow_new_offset
,
2162 static void sync_super(struct mddev
*mddev
, struct md_rdev
*rdev
)
2164 if (mddev
->sync_super
) {
2165 mddev
->sync_super(mddev
, rdev
);
2169 BUG_ON(mddev
->major_version
>= ARRAY_SIZE(super_types
));
2171 super_types
[mddev
->major_version
].sync_super(mddev
, rdev
);
2174 static int match_mddev_units(struct mddev
*mddev1
, struct mddev
*mddev2
)
2176 struct md_rdev
*rdev
, *rdev2
;
2179 rdev_for_each_rcu(rdev
, mddev1
) {
2180 if (test_bit(Faulty
, &rdev
->flags
) ||
2181 test_bit(Journal
, &rdev
->flags
) ||
2182 rdev
->raid_disk
== -1)
2184 rdev_for_each_rcu(rdev2
, mddev2
) {
2185 if (test_bit(Faulty
, &rdev2
->flags
) ||
2186 test_bit(Journal
, &rdev2
->flags
) ||
2187 rdev2
->raid_disk
== -1)
2189 if (rdev
->bdev
->bd_contains
==
2190 rdev2
->bdev
->bd_contains
) {
2200 static LIST_HEAD(pending_raid_disks
);
2203 * Try to register data integrity profile for an mddev
2205 * This is called when an array is started and after a disk has been kicked
2206 * from the array. It only succeeds if all working and active component devices
2207 * are integrity capable with matching profiles.
2209 int md_integrity_register(struct mddev
*mddev
)
2211 struct md_rdev
*rdev
, *reference
= NULL
;
2213 if (list_empty(&mddev
->disks
))
2214 return 0; /* nothing to do */
2215 if (!mddev
->gendisk
|| blk_get_integrity(mddev
->gendisk
))
2216 return 0; /* shouldn't register, or already is */
2217 rdev_for_each(rdev
, mddev
) {
2218 /* skip spares and non-functional disks */
2219 if (test_bit(Faulty
, &rdev
->flags
))
2221 if (rdev
->raid_disk
< 0)
2224 /* Use the first rdev as the reference */
2228 /* does this rdev's profile match the reference profile? */
2229 if (blk_integrity_compare(reference
->bdev
->bd_disk
,
2230 rdev
->bdev
->bd_disk
) < 0)
2233 if (!reference
|| !bdev_get_integrity(reference
->bdev
))
2236 * All component devices are integrity capable and have matching
2237 * profiles, register the common profile for the md device.
2239 blk_integrity_register(mddev
->gendisk
,
2240 bdev_get_integrity(reference
->bdev
));
2242 pr_debug("md: data integrity enabled on %s\n", mdname(mddev
));
2243 if (bioset_integrity_create(&mddev
->bio_set
, BIO_POOL_SIZE
)) {
2244 pr_err("md: failed to create integrity pool for %s\n",
2250 EXPORT_SYMBOL(md_integrity_register
);
2253 * Attempt to add an rdev, but only if it is consistent with the current
2256 int md_integrity_add_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
2258 struct blk_integrity
*bi_mddev
;
2259 char name
[BDEVNAME_SIZE
];
2261 if (!mddev
->gendisk
)
2264 bi_mddev
= blk_get_integrity(mddev
->gendisk
);
2266 if (!bi_mddev
) /* nothing to do */
2269 if (blk_integrity_compare(mddev
->gendisk
, rdev
->bdev
->bd_disk
) != 0) {
2270 pr_err("%s: incompatible integrity profile for %s\n",
2271 mdname(mddev
), bdevname(rdev
->bdev
, name
));
2277 EXPORT_SYMBOL(md_integrity_add_rdev
);
2279 static int bind_rdev_to_array(struct md_rdev
*rdev
, struct mddev
*mddev
)
2281 char b
[BDEVNAME_SIZE
];
2285 /* prevent duplicates */
2286 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
2289 if ((bdev_read_only(rdev
->bdev
) || bdev_read_only(rdev
->meta_bdev
)) &&
2293 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2294 if (!test_bit(Journal
, &rdev
->flags
) &&
2296 (mddev
->dev_sectors
== 0 || rdev
->sectors
< mddev
->dev_sectors
)) {
2298 /* Cannot change size, so fail
2299 * If mddev->level <= 0, then we don't care
2300 * about aligning sizes (e.g. linear)
2302 if (mddev
->level
> 0)
2305 mddev
->dev_sectors
= rdev
->sectors
;
2308 /* Verify rdev->desc_nr is unique.
2309 * If it is -1, assign a free number, else
2310 * check number is not in use
2313 if (rdev
->desc_nr
< 0) {
2316 choice
= mddev
->raid_disks
;
2317 while (md_find_rdev_nr_rcu(mddev
, choice
))
2319 rdev
->desc_nr
= choice
;
2321 if (md_find_rdev_nr_rcu(mddev
, rdev
->desc_nr
)) {
2327 if (!test_bit(Journal
, &rdev
->flags
) &&
2328 mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
2329 pr_warn("md: %s: array is limited to %d devices\n",
2330 mdname(mddev
), mddev
->max_disks
);
2333 bdevname(rdev
->bdev
,b
);
2334 strreplace(b
, '/', '!');
2336 rdev
->mddev
= mddev
;
2337 pr_debug("md: bind<%s>\n", b
);
2339 if (mddev
->raid_disks
)
2340 mddev_create_wb_pool(mddev
, rdev
, false);
2342 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
2345 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
2346 if (sysfs_create_link(&rdev
->kobj
, ko
, "block"))
2347 /* failure here is OK */;
2348 rdev
->sysfs_state
= sysfs_get_dirent_safe(rdev
->kobj
.sd
, "state");
2350 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
2351 bd_link_disk_holder(rdev
->bdev
, mddev
->gendisk
);
2353 /* May as well allow recovery to be retried once */
2354 mddev
->recovery_disabled
++;
2359 pr_warn("md: failed to register dev-%s for %s\n",
2364 static void md_delayed_delete(struct work_struct
*ws
)
2366 struct md_rdev
*rdev
= container_of(ws
, struct md_rdev
, del_work
);
2367 kobject_del(&rdev
->kobj
);
2368 kobject_put(&rdev
->kobj
);
2371 static void unbind_rdev_from_array(struct md_rdev
*rdev
)
2373 char b
[BDEVNAME_SIZE
];
2375 bd_unlink_disk_holder(rdev
->bdev
, rdev
->mddev
->gendisk
);
2376 list_del_rcu(&rdev
->same_set
);
2377 pr_debug("md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
2378 mddev_destroy_wb_pool(rdev
->mddev
, rdev
);
2380 sysfs_remove_link(&rdev
->kobj
, "block");
2381 sysfs_put(rdev
->sysfs_state
);
2382 rdev
->sysfs_state
= NULL
;
2383 rdev
->badblocks
.count
= 0;
2384 /* We need to delay this, otherwise we can deadlock when
2385 * writing to 'remove' to "dev/state". We also need
2386 * to delay it due to rcu usage.
2389 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
2390 kobject_get(&rdev
->kobj
);
2391 queue_work(md_misc_wq
, &rdev
->del_work
);
2395 * prevent the device from being mounted, repartitioned or
2396 * otherwise reused by a RAID array (or any other kernel
2397 * subsystem), by bd_claiming the device.
2399 static int lock_rdev(struct md_rdev
*rdev
, dev_t dev
, int shared
)
2402 struct block_device
*bdev
;
2403 char b
[BDEVNAME_SIZE
];
2405 bdev
= blkdev_get_by_dev(dev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
,
2406 shared
? (struct md_rdev
*)lock_rdev
: rdev
);
2408 pr_warn("md: could not open %s.\n", __bdevname(dev
, b
));
2409 return PTR_ERR(bdev
);
2415 static void unlock_rdev(struct md_rdev
*rdev
)
2417 struct block_device
*bdev
= rdev
->bdev
;
2419 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
2422 void md_autodetect_dev(dev_t dev
);
2424 static void export_rdev(struct md_rdev
*rdev
)
2426 char b
[BDEVNAME_SIZE
];
2428 pr_debug("md: export_rdev(%s)\n", bdevname(rdev
->bdev
,b
));
2429 md_rdev_clear(rdev
);
2431 if (test_bit(AutoDetected
, &rdev
->flags
))
2432 md_autodetect_dev(rdev
->bdev
->bd_dev
);
2435 kobject_put(&rdev
->kobj
);
2438 void md_kick_rdev_from_array(struct md_rdev
*rdev
)
2440 unbind_rdev_from_array(rdev
);
2443 EXPORT_SYMBOL_GPL(md_kick_rdev_from_array
);
2445 static void export_array(struct mddev
*mddev
)
2447 struct md_rdev
*rdev
;
2449 while (!list_empty(&mddev
->disks
)) {
2450 rdev
= list_first_entry(&mddev
->disks
, struct md_rdev
,
2452 md_kick_rdev_from_array(rdev
);
2454 mddev
->raid_disks
= 0;
2455 mddev
->major_version
= 0;
2458 static bool set_in_sync(struct mddev
*mddev
)
2460 lockdep_assert_held(&mddev
->lock
);
2461 if (!mddev
->in_sync
) {
2462 mddev
->sync_checkers
++;
2463 spin_unlock(&mddev
->lock
);
2464 percpu_ref_switch_to_atomic_sync(&mddev
->writes_pending
);
2465 spin_lock(&mddev
->lock
);
2466 if (!mddev
->in_sync
&&
2467 percpu_ref_is_zero(&mddev
->writes_pending
)) {
2470 * Ensure ->in_sync is visible before we clear
2474 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
2475 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
2477 if (--mddev
->sync_checkers
== 0)
2478 percpu_ref_switch_to_percpu(&mddev
->writes_pending
);
2480 if (mddev
->safemode
== 1)
2481 mddev
->safemode
= 0;
2482 return mddev
->in_sync
;
2485 static void sync_sbs(struct mddev
*mddev
, int nospares
)
2487 /* Update each superblock (in-memory image), but
2488 * if we are allowed to, skip spares which already
2489 * have the right event counter, or have one earlier
2490 * (which would mean they aren't being marked as dirty
2491 * with the rest of the array)
2493 struct md_rdev
*rdev
;
2494 rdev_for_each(rdev
, mddev
) {
2495 if (rdev
->sb_events
== mddev
->events
||
2497 rdev
->raid_disk
< 0 &&
2498 rdev
->sb_events
+1 == mddev
->events
)) {
2499 /* Don't update this superblock */
2500 rdev
->sb_loaded
= 2;
2502 sync_super(mddev
, rdev
);
2503 rdev
->sb_loaded
= 1;
2508 static bool does_sb_need_changing(struct mddev
*mddev
)
2510 struct md_rdev
*rdev
;
2511 struct mdp_superblock_1
*sb
;
2514 /* Find a good rdev */
2515 rdev_for_each(rdev
, mddev
)
2516 if ((rdev
->raid_disk
>= 0) && !test_bit(Faulty
, &rdev
->flags
))
2519 /* No good device found. */
2523 sb
= page_address(rdev
->sb_page
);
2524 /* Check if a device has become faulty or a spare become active */
2525 rdev_for_each(rdev
, mddev
) {
2526 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
2527 /* Device activated? */
2528 if (role
== 0xffff && rdev
->raid_disk
>=0 &&
2529 !test_bit(Faulty
, &rdev
->flags
))
2531 /* Device turned faulty? */
2532 if (test_bit(Faulty
, &rdev
->flags
) && (role
< 0xfffd))
2536 /* Check if any mddev parameters have changed */
2537 if ((mddev
->dev_sectors
!= le64_to_cpu(sb
->size
)) ||
2538 (mddev
->reshape_position
!= le64_to_cpu(sb
->reshape_position
)) ||
2539 (mddev
->layout
!= le32_to_cpu(sb
->layout
)) ||
2540 (mddev
->raid_disks
!= le32_to_cpu(sb
->raid_disks
)) ||
2541 (mddev
->chunk_sectors
!= le32_to_cpu(sb
->chunksize
)))
2547 void md_update_sb(struct mddev
*mddev
, int force_change
)
2549 struct md_rdev
*rdev
;
2552 int any_badblocks_changed
= 0;
2557 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2562 if (mddev_is_clustered(mddev
)) {
2563 if (test_and_clear_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
))
2565 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
))
2567 ret
= md_cluster_ops
->metadata_update_start(mddev
);
2568 /* Has someone else has updated the sb */
2569 if (!does_sb_need_changing(mddev
)) {
2571 md_cluster_ops
->metadata_update_cancel(mddev
);
2572 bit_clear_unless(&mddev
->sb_flags
, BIT(MD_SB_CHANGE_PENDING
),
2573 BIT(MD_SB_CHANGE_DEVS
) |
2574 BIT(MD_SB_CHANGE_CLEAN
));
2580 * First make sure individual recovery_offsets are correct
2581 * curr_resync_completed can only be used during recovery.
2582 * During reshape/resync it might use array-addresses rather
2583 * that device addresses.
2585 rdev_for_each(rdev
, mddev
) {
2586 if (rdev
->raid_disk
>= 0 &&
2587 mddev
->delta_disks
>= 0 &&
2588 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
2589 test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
) &&
2590 !test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
2591 !test_bit(Journal
, &rdev
->flags
) &&
2592 !test_bit(In_sync
, &rdev
->flags
) &&
2593 mddev
->curr_resync_completed
> rdev
->recovery_offset
)
2594 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
2597 if (!mddev
->persistent
) {
2598 clear_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
2599 clear_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2600 if (!mddev
->external
) {
2601 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
2602 rdev_for_each(rdev
, mddev
) {
2603 if (rdev
->badblocks
.changed
) {
2604 rdev
->badblocks
.changed
= 0;
2605 ack_all_badblocks(&rdev
->badblocks
);
2606 md_error(mddev
, rdev
);
2608 clear_bit(Blocked
, &rdev
->flags
);
2609 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2610 wake_up(&rdev
->blocked_wait
);
2613 wake_up(&mddev
->sb_wait
);
2617 spin_lock(&mddev
->lock
);
2619 mddev
->utime
= ktime_get_real_seconds();
2621 if (test_and_clear_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
))
2623 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
))
2624 /* just a clean<-> dirty transition, possibly leave spares alone,
2625 * though if events isn't the right even/odd, we will have to do
2631 if (mddev
->degraded
)
2632 /* If the array is degraded, then skipping spares is both
2633 * dangerous and fairly pointless.
2634 * Dangerous because a device that was removed from the array
2635 * might have a event_count that still looks up-to-date,
2636 * so it can be re-added without a resync.
2637 * Pointless because if there are any spares to skip,
2638 * then a recovery will happen and soon that array won't
2639 * be degraded any more and the spare can go back to sleep then.
2643 sync_req
= mddev
->in_sync
;
2645 /* If this is just a dirty<->clean transition, and the array is clean
2646 * and 'events' is odd, we can roll back to the previous clean state */
2648 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
2649 && mddev
->can_decrease_events
2650 && mddev
->events
!= 1) {
2652 mddev
->can_decrease_events
= 0;
2654 /* otherwise we have to go forward and ... */
2656 mddev
->can_decrease_events
= nospares
;
2660 * This 64-bit counter should never wrap.
2661 * Either we are in around ~1 trillion A.C., assuming
2662 * 1 reboot per second, or we have a bug...
2664 WARN_ON(mddev
->events
== 0);
2666 rdev_for_each(rdev
, mddev
) {
2667 if (rdev
->badblocks
.changed
)
2668 any_badblocks_changed
++;
2669 if (test_bit(Faulty
, &rdev
->flags
))
2670 set_bit(FaultRecorded
, &rdev
->flags
);
2673 sync_sbs(mddev
, nospares
);
2674 spin_unlock(&mddev
->lock
);
2676 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2677 mdname(mddev
), mddev
->in_sync
);
2680 blk_add_trace_msg(mddev
->queue
, "md md_update_sb");
2682 md_bitmap_update_sb(mddev
->bitmap
);
2683 rdev_for_each(rdev
, mddev
) {
2684 char b
[BDEVNAME_SIZE
];
2686 if (rdev
->sb_loaded
!= 1)
2687 continue; /* no noise on spare devices */
2689 if (!test_bit(Faulty
, &rdev
->flags
)) {
2690 md_super_write(mddev
,rdev
,
2691 rdev
->sb_start
, rdev
->sb_size
,
2693 pr_debug("md: (write) %s's sb offset: %llu\n",
2694 bdevname(rdev
->bdev
, b
),
2695 (unsigned long long)rdev
->sb_start
);
2696 rdev
->sb_events
= mddev
->events
;
2697 if (rdev
->badblocks
.size
) {
2698 md_super_write(mddev
, rdev
,
2699 rdev
->badblocks
.sector
,
2700 rdev
->badblocks
.size
<< 9,
2702 rdev
->badblocks
.size
= 0;
2706 pr_debug("md: %s (skipping faulty)\n",
2707 bdevname(rdev
->bdev
, b
));
2709 if (mddev
->level
== LEVEL_MULTIPATH
)
2710 /* only need to write one superblock... */
2713 if (md_super_wait(mddev
) < 0)
2715 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
2717 if (mddev_is_clustered(mddev
) && ret
== 0)
2718 md_cluster_ops
->metadata_update_finish(mddev
);
2720 if (mddev
->in_sync
!= sync_req
||
2721 !bit_clear_unless(&mddev
->sb_flags
, BIT(MD_SB_CHANGE_PENDING
),
2722 BIT(MD_SB_CHANGE_DEVS
) | BIT(MD_SB_CHANGE_CLEAN
)))
2723 /* have to write it out again */
2725 wake_up(&mddev
->sb_wait
);
2726 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2727 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
2729 rdev_for_each(rdev
, mddev
) {
2730 if (test_and_clear_bit(FaultRecorded
, &rdev
->flags
))
2731 clear_bit(Blocked
, &rdev
->flags
);
2733 if (any_badblocks_changed
)
2734 ack_all_badblocks(&rdev
->badblocks
);
2735 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2736 wake_up(&rdev
->blocked_wait
);
2739 EXPORT_SYMBOL(md_update_sb
);
2741 static int add_bound_rdev(struct md_rdev
*rdev
)
2743 struct mddev
*mddev
= rdev
->mddev
;
2745 bool add_journal
= test_bit(Journal
, &rdev
->flags
);
2747 if (!mddev
->pers
->hot_remove_disk
|| add_journal
) {
2748 /* If there is hot_add_disk but no hot_remove_disk
2749 * then added disks for geometry changes,
2750 * and should be added immediately.
2752 super_types
[mddev
->major_version
].
2753 validate_super(mddev
, rdev
);
2755 mddev_suspend(mddev
);
2756 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
2758 mddev_resume(mddev
);
2760 md_kick_rdev_from_array(rdev
);
2764 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2766 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2767 if (mddev
->degraded
)
2768 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
2769 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2770 md_new_event(mddev
);
2771 md_wakeup_thread(mddev
->thread
);
2775 /* words written to sysfs files may, or may not, be \n terminated.
2776 * We want to accept with case. For this we use cmd_match.
2778 static int cmd_match(const char *cmd
, const char *str
)
2780 /* See if cmd, written into a sysfs file, matches
2781 * str. They must either be the same, or cmd can
2782 * have a trailing newline
2784 while (*cmd
&& *str
&& *cmd
== *str
) {
2795 struct rdev_sysfs_entry
{
2796 struct attribute attr
;
2797 ssize_t (*show
)(struct md_rdev
*, char *);
2798 ssize_t (*store
)(struct md_rdev
*, const char *, size_t);
2802 state_show(struct md_rdev
*rdev
, char *page
)
2806 unsigned long flags
= READ_ONCE(rdev
->flags
);
2808 if (test_bit(Faulty
, &flags
) ||
2809 (!test_bit(ExternalBbl
, &flags
) &&
2810 rdev
->badblocks
.unacked_exist
))
2811 len
+= sprintf(page
+len
, "faulty%s", sep
);
2812 if (test_bit(In_sync
, &flags
))
2813 len
+= sprintf(page
+len
, "in_sync%s", sep
);
2814 if (test_bit(Journal
, &flags
))
2815 len
+= sprintf(page
+len
, "journal%s", sep
);
2816 if (test_bit(WriteMostly
, &flags
))
2817 len
+= sprintf(page
+len
, "write_mostly%s", sep
);
2818 if (test_bit(Blocked
, &flags
) ||
2819 (rdev
->badblocks
.unacked_exist
2820 && !test_bit(Faulty
, &flags
)))
2821 len
+= sprintf(page
+len
, "blocked%s", sep
);
2822 if (!test_bit(Faulty
, &flags
) &&
2823 !test_bit(Journal
, &flags
) &&
2824 !test_bit(In_sync
, &flags
))
2825 len
+= sprintf(page
+len
, "spare%s", sep
);
2826 if (test_bit(WriteErrorSeen
, &flags
))
2827 len
+= sprintf(page
+len
, "write_error%s", sep
);
2828 if (test_bit(WantReplacement
, &flags
))
2829 len
+= sprintf(page
+len
, "want_replacement%s", sep
);
2830 if (test_bit(Replacement
, &flags
))
2831 len
+= sprintf(page
+len
, "replacement%s", sep
);
2832 if (test_bit(ExternalBbl
, &flags
))
2833 len
+= sprintf(page
+len
, "external_bbl%s", sep
);
2834 if (test_bit(FailFast
, &flags
))
2835 len
+= sprintf(page
+len
, "failfast%s", sep
);
2840 return len
+sprintf(page
+len
, "\n");
2844 state_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2847 * faulty - simulates an error
2848 * remove - disconnects the device
2849 * writemostly - sets write_mostly
2850 * -writemostly - clears write_mostly
2851 * blocked - sets the Blocked flags
2852 * -blocked - clears the Blocked and possibly simulates an error
2853 * insync - sets Insync providing device isn't active
2854 * -insync - clear Insync for a device with a slot assigned,
2855 * so that it gets rebuilt based on bitmap
2856 * write_error - sets WriteErrorSeen
2857 * -write_error - clears WriteErrorSeen
2858 * {,-}failfast - set/clear FailFast
2861 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
2862 md_error(rdev
->mddev
, rdev
);
2863 if (test_bit(Faulty
, &rdev
->flags
))
2867 } else if (cmd_match(buf
, "remove")) {
2868 if (rdev
->mddev
->pers
) {
2869 clear_bit(Blocked
, &rdev
->flags
);
2870 remove_and_add_spares(rdev
->mddev
, rdev
);
2872 if (rdev
->raid_disk
>= 0)
2875 struct mddev
*mddev
= rdev
->mddev
;
2877 if (mddev_is_clustered(mddev
))
2878 err
= md_cluster_ops
->remove_disk(mddev
, rdev
);
2881 md_kick_rdev_from_array(rdev
);
2883 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2884 md_wakeup_thread(mddev
->thread
);
2886 md_new_event(mddev
);
2889 } else if (cmd_match(buf
, "writemostly")) {
2890 set_bit(WriteMostly
, &rdev
->flags
);
2891 mddev_create_wb_pool(rdev
->mddev
, rdev
, false);
2893 } else if (cmd_match(buf
, "-writemostly")) {
2894 mddev_destroy_wb_pool(rdev
->mddev
, rdev
);
2895 clear_bit(WriteMostly
, &rdev
->flags
);
2897 } else if (cmd_match(buf
, "blocked")) {
2898 set_bit(Blocked
, &rdev
->flags
);
2900 } else if (cmd_match(buf
, "-blocked")) {
2901 if (!test_bit(Faulty
, &rdev
->flags
) &&
2902 !test_bit(ExternalBbl
, &rdev
->flags
) &&
2903 rdev
->badblocks
.unacked_exist
) {
2904 /* metadata handler doesn't understand badblocks,
2905 * so we need to fail the device
2907 md_error(rdev
->mddev
, rdev
);
2909 clear_bit(Blocked
, &rdev
->flags
);
2910 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2911 wake_up(&rdev
->blocked_wait
);
2912 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2913 md_wakeup_thread(rdev
->mddev
->thread
);
2916 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
2917 set_bit(In_sync
, &rdev
->flags
);
2919 } else if (cmd_match(buf
, "failfast")) {
2920 set_bit(FailFast
, &rdev
->flags
);
2922 } else if (cmd_match(buf
, "-failfast")) {
2923 clear_bit(FailFast
, &rdev
->flags
);
2925 } else if (cmd_match(buf
, "-insync") && rdev
->raid_disk
>= 0 &&
2926 !test_bit(Journal
, &rdev
->flags
)) {
2927 if (rdev
->mddev
->pers
== NULL
) {
2928 clear_bit(In_sync
, &rdev
->flags
);
2929 rdev
->saved_raid_disk
= rdev
->raid_disk
;
2930 rdev
->raid_disk
= -1;
2933 } else if (cmd_match(buf
, "write_error")) {
2934 set_bit(WriteErrorSeen
, &rdev
->flags
);
2936 } else if (cmd_match(buf
, "-write_error")) {
2937 clear_bit(WriteErrorSeen
, &rdev
->flags
);
2939 } else if (cmd_match(buf
, "want_replacement")) {
2940 /* Any non-spare device that is not a replacement can
2941 * become want_replacement at any time, but we then need to
2942 * check if recovery is needed.
2944 if (rdev
->raid_disk
>= 0 &&
2945 !test_bit(Journal
, &rdev
->flags
) &&
2946 !test_bit(Replacement
, &rdev
->flags
))
2947 set_bit(WantReplacement
, &rdev
->flags
);
2948 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2949 md_wakeup_thread(rdev
->mddev
->thread
);
2951 } else if (cmd_match(buf
, "-want_replacement")) {
2952 /* Clearing 'want_replacement' is always allowed.
2953 * Once replacements starts it is too late though.
2956 clear_bit(WantReplacement
, &rdev
->flags
);
2957 } else if (cmd_match(buf
, "replacement")) {
2958 /* Can only set a device as a replacement when array has not
2959 * yet been started. Once running, replacement is automatic
2960 * from spares, or by assigning 'slot'.
2962 if (rdev
->mddev
->pers
)
2965 set_bit(Replacement
, &rdev
->flags
);
2968 } else if (cmd_match(buf
, "-replacement")) {
2969 /* Similarly, can only clear Replacement before start */
2970 if (rdev
->mddev
->pers
)
2973 clear_bit(Replacement
, &rdev
->flags
);
2976 } else if (cmd_match(buf
, "re-add")) {
2977 if (!rdev
->mddev
->pers
)
2979 else if (test_bit(Faulty
, &rdev
->flags
) && (rdev
->raid_disk
== -1) &&
2980 rdev
->saved_raid_disk
>= 0) {
2981 /* clear_bit is performed _after_ all the devices
2982 * have their local Faulty bit cleared. If any writes
2983 * happen in the meantime in the local node, they
2984 * will land in the local bitmap, which will be synced
2985 * by this node eventually
2987 if (!mddev_is_clustered(rdev
->mddev
) ||
2988 (err
= md_cluster_ops
->gather_bitmaps(rdev
)) == 0) {
2989 clear_bit(Faulty
, &rdev
->flags
);
2990 err
= add_bound_rdev(rdev
);
2994 } else if (cmd_match(buf
, "external_bbl") && (rdev
->mddev
->external
)) {
2995 set_bit(ExternalBbl
, &rdev
->flags
);
2996 rdev
->badblocks
.shift
= 0;
2998 } else if (cmd_match(buf
, "-external_bbl") && (rdev
->mddev
->external
)) {
2999 clear_bit(ExternalBbl
, &rdev
->flags
);
3003 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
3004 return err
? err
: len
;
3006 static struct rdev_sysfs_entry rdev_state
=
3007 __ATTR_PREALLOC(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
3010 errors_show(struct md_rdev
*rdev
, char *page
)
3012 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
3016 errors_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3021 rv
= kstrtouint(buf
, 10, &n
);
3024 atomic_set(&rdev
->corrected_errors
, n
);
3027 static struct rdev_sysfs_entry rdev_errors
=
3028 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
3031 slot_show(struct md_rdev
*rdev
, char *page
)
3033 if (test_bit(Journal
, &rdev
->flags
))
3034 return sprintf(page
, "journal\n");
3035 else if (rdev
->raid_disk
< 0)
3036 return sprintf(page
, "none\n");
3038 return sprintf(page
, "%d\n", rdev
->raid_disk
);
3042 slot_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3047 if (test_bit(Journal
, &rdev
->flags
))
3049 if (strncmp(buf
, "none", 4)==0)
3052 err
= kstrtouint(buf
, 10, (unsigned int *)&slot
);
3056 if (rdev
->mddev
->pers
&& slot
== -1) {
3057 /* Setting 'slot' on an active array requires also
3058 * updating the 'rd%d' link, and communicating
3059 * with the personality with ->hot_*_disk.
3060 * For now we only support removing
3061 * failed/spare devices. This normally happens automatically,
3062 * but not when the metadata is externally managed.
3064 if (rdev
->raid_disk
== -1)
3066 /* personality does all needed checks */
3067 if (rdev
->mddev
->pers
->hot_remove_disk
== NULL
)
3069 clear_bit(Blocked
, &rdev
->flags
);
3070 remove_and_add_spares(rdev
->mddev
, rdev
);
3071 if (rdev
->raid_disk
>= 0)
3073 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
3074 md_wakeup_thread(rdev
->mddev
->thread
);
3075 } else if (rdev
->mddev
->pers
) {
3076 /* Activating a spare .. or possibly reactivating
3077 * if we ever get bitmaps working here.
3081 if (rdev
->raid_disk
!= -1)
3084 if (test_bit(MD_RECOVERY_RUNNING
, &rdev
->mddev
->recovery
))
3087 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
3090 if (slot
>= rdev
->mddev
->raid_disks
&&
3091 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
3094 rdev
->raid_disk
= slot
;
3095 if (test_bit(In_sync
, &rdev
->flags
))
3096 rdev
->saved_raid_disk
= slot
;
3098 rdev
->saved_raid_disk
= -1;
3099 clear_bit(In_sync
, &rdev
->flags
);
3100 clear_bit(Bitmap_sync
, &rdev
->flags
);
3101 err
= rdev
->mddev
->pers
->
3102 hot_add_disk(rdev
->mddev
, rdev
);
3104 rdev
->raid_disk
= -1;
3107 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
3108 if (sysfs_link_rdev(rdev
->mddev
, rdev
))
3109 /* failure here is OK */;
3110 /* don't wakeup anyone, leave that to userspace. */
3112 if (slot
>= rdev
->mddev
->raid_disks
&&
3113 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
3115 rdev
->raid_disk
= slot
;
3116 /* assume it is working */
3117 clear_bit(Faulty
, &rdev
->flags
);
3118 clear_bit(WriteMostly
, &rdev
->flags
);
3119 set_bit(In_sync
, &rdev
->flags
);
3120 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
3125 static struct rdev_sysfs_entry rdev_slot
=
3126 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
3129 offset_show(struct md_rdev
*rdev
, char *page
)
3131 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
3135 offset_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3137 unsigned long long offset
;
3138 if (kstrtoull(buf
, 10, &offset
) < 0)
3140 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
3142 if (rdev
->sectors
&& rdev
->mddev
->external
)
3143 /* Must set offset before size, so overlap checks
3146 rdev
->data_offset
= offset
;
3147 rdev
->new_data_offset
= offset
;
3151 static struct rdev_sysfs_entry rdev_offset
=
3152 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
3154 static ssize_t
new_offset_show(struct md_rdev
*rdev
, char *page
)
3156 return sprintf(page
, "%llu\n",
3157 (unsigned long long)rdev
->new_data_offset
);
3160 static ssize_t
new_offset_store(struct md_rdev
*rdev
,
3161 const char *buf
, size_t len
)
3163 unsigned long long new_offset
;
3164 struct mddev
*mddev
= rdev
->mddev
;
3166 if (kstrtoull(buf
, 10, &new_offset
) < 0)
3169 if (mddev
->sync_thread
||
3170 test_bit(MD_RECOVERY_RUNNING
,&mddev
->recovery
))
3172 if (new_offset
== rdev
->data_offset
)
3173 /* reset is always permitted */
3175 else if (new_offset
> rdev
->data_offset
) {
3176 /* must not push array size beyond rdev_sectors */
3177 if (new_offset
- rdev
->data_offset
3178 + mddev
->dev_sectors
> rdev
->sectors
)
3181 /* Metadata worries about other space details. */
3183 /* decreasing the offset is inconsistent with a backwards
3186 if (new_offset
< rdev
->data_offset
&&
3187 mddev
->reshape_backwards
)
3189 /* Increasing offset is inconsistent with forwards
3190 * reshape. reshape_direction should be set to
3191 * 'backwards' first.
3193 if (new_offset
> rdev
->data_offset
&&
3194 !mddev
->reshape_backwards
)
3197 if (mddev
->pers
&& mddev
->persistent
&&
3198 !super_types
[mddev
->major_version
]
3199 .allow_new_offset(rdev
, new_offset
))
3201 rdev
->new_data_offset
= new_offset
;
3202 if (new_offset
> rdev
->data_offset
)
3203 mddev
->reshape_backwards
= 1;
3204 else if (new_offset
< rdev
->data_offset
)
3205 mddev
->reshape_backwards
= 0;
3209 static struct rdev_sysfs_entry rdev_new_offset
=
3210 __ATTR(new_offset
, S_IRUGO
|S_IWUSR
, new_offset_show
, new_offset_store
);
3213 rdev_size_show(struct md_rdev
*rdev
, char *page
)
3215 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
3218 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
3220 /* check if two start/length pairs overlap */
3228 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
3230 unsigned long long blocks
;
3233 if (kstrtoull(buf
, 10, &blocks
) < 0)
3236 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
3237 return -EINVAL
; /* sector conversion overflow */
3240 if (new != blocks
* 2)
3241 return -EINVAL
; /* unsigned long long to sector_t overflow */
3248 rdev_size_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3250 struct mddev
*my_mddev
= rdev
->mddev
;
3251 sector_t oldsectors
= rdev
->sectors
;
3254 if (test_bit(Journal
, &rdev
->flags
))
3256 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
3258 if (rdev
->data_offset
!= rdev
->new_data_offset
)
3259 return -EINVAL
; /* too confusing */
3260 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
3261 if (my_mddev
->persistent
) {
3262 sectors
= super_types
[my_mddev
->major_version
].
3263 rdev_size_change(rdev
, sectors
);
3266 } else if (!sectors
)
3267 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
3269 if (!my_mddev
->pers
->resize
)
3270 /* Cannot change size for RAID0 or Linear etc */
3273 if (sectors
< my_mddev
->dev_sectors
)
3274 return -EINVAL
; /* component must fit device */
3276 rdev
->sectors
= sectors
;
3277 if (sectors
> oldsectors
&& my_mddev
->external
) {
3278 /* Need to check that all other rdevs with the same
3279 * ->bdev do not overlap. 'rcu' is sufficient to walk
3280 * the rdev lists safely.
3281 * This check does not provide a hard guarantee, it
3282 * just helps avoid dangerous mistakes.
3284 struct mddev
*mddev
;
3286 struct list_head
*tmp
;
3289 for_each_mddev(mddev
, tmp
) {
3290 struct md_rdev
*rdev2
;
3292 rdev_for_each(rdev2
, mddev
)
3293 if (rdev
->bdev
== rdev2
->bdev
&&
3295 overlaps(rdev
->data_offset
, rdev
->sectors
,
3308 /* Someone else could have slipped in a size
3309 * change here, but doing so is just silly.
3310 * We put oldsectors back because we *know* it is
3311 * safe, and trust userspace not to race with
3314 rdev
->sectors
= oldsectors
;
3321 static struct rdev_sysfs_entry rdev_size
=
3322 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
3324 static ssize_t
recovery_start_show(struct md_rdev
*rdev
, char *page
)
3326 unsigned long long recovery_start
= rdev
->recovery_offset
;
3328 if (test_bit(In_sync
, &rdev
->flags
) ||
3329 recovery_start
== MaxSector
)
3330 return sprintf(page
, "none\n");
3332 return sprintf(page
, "%llu\n", recovery_start
);
3335 static ssize_t
recovery_start_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3337 unsigned long long recovery_start
;
3339 if (cmd_match(buf
, "none"))
3340 recovery_start
= MaxSector
;
3341 else if (kstrtoull(buf
, 10, &recovery_start
))
3344 if (rdev
->mddev
->pers
&&
3345 rdev
->raid_disk
>= 0)
3348 rdev
->recovery_offset
= recovery_start
;
3349 if (recovery_start
== MaxSector
)
3350 set_bit(In_sync
, &rdev
->flags
);
3352 clear_bit(In_sync
, &rdev
->flags
);
3356 static struct rdev_sysfs_entry rdev_recovery_start
=
3357 __ATTR(recovery_start
, S_IRUGO
|S_IWUSR
, recovery_start_show
, recovery_start_store
);
3359 /* sysfs access to bad-blocks list.
3360 * We present two files.
3361 * 'bad-blocks' lists sector numbers and lengths of ranges that
3362 * are recorded as bad. The list is truncated to fit within
3363 * the one-page limit of sysfs.
3364 * Writing "sector length" to this file adds an acknowledged
3366 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3367 * been acknowledged. Writing to this file adds bad blocks
3368 * without acknowledging them. This is largely for testing.
3370 static ssize_t
bb_show(struct md_rdev
*rdev
, char *page
)
3372 return badblocks_show(&rdev
->badblocks
, page
, 0);
3374 static ssize_t
bb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
3376 int rv
= badblocks_store(&rdev
->badblocks
, page
, len
, 0);
3377 /* Maybe that ack was all we needed */
3378 if (test_and_clear_bit(BlockedBadBlocks
, &rdev
->flags
))
3379 wake_up(&rdev
->blocked_wait
);
3382 static struct rdev_sysfs_entry rdev_bad_blocks
=
3383 __ATTR(bad_blocks
, S_IRUGO
|S_IWUSR
, bb_show
, bb_store
);
3385 static ssize_t
ubb_show(struct md_rdev
*rdev
, char *page
)
3387 return badblocks_show(&rdev
->badblocks
, page
, 1);
3389 static ssize_t
ubb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
3391 return badblocks_store(&rdev
->badblocks
, page
, len
, 1);
3393 static struct rdev_sysfs_entry rdev_unack_bad_blocks
=
3394 __ATTR(unacknowledged_bad_blocks
, S_IRUGO
|S_IWUSR
, ubb_show
, ubb_store
);
3397 ppl_sector_show(struct md_rdev
*rdev
, char *page
)
3399 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->ppl
.sector
);
3403 ppl_sector_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3405 unsigned long long sector
;
3407 if (kstrtoull(buf
, 10, §or
) < 0)
3409 if (sector
!= (sector_t
)sector
)
3412 if (rdev
->mddev
->pers
&& test_bit(MD_HAS_PPL
, &rdev
->mddev
->flags
) &&
3413 rdev
->raid_disk
>= 0)
3416 if (rdev
->mddev
->persistent
) {
3417 if (rdev
->mddev
->major_version
== 0)
3419 if ((sector
> rdev
->sb_start
&&
3420 sector
- rdev
->sb_start
> S16_MAX
) ||
3421 (sector
< rdev
->sb_start
&&
3422 rdev
->sb_start
- sector
> -S16_MIN
))
3424 rdev
->ppl
.offset
= sector
- rdev
->sb_start
;
3425 } else if (!rdev
->mddev
->external
) {
3428 rdev
->ppl
.sector
= sector
;
3432 static struct rdev_sysfs_entry rdev_ppl_sector
=
3433 __ATTR(ppl_sector
, S_IRUGO
|S_IWUSR
, ppl_sector_show
, ppl_sector_store
);
3436 ppl_size_show(struct md_rdev
*rdev
, char *page
)
3438 return sprintf(page
, "%u\n", rdev
->ppl
.size
);
3442 ppl_size_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3446 if (kstrtouint(buf
, 10, &size
) < 0)
3449 if (rdev
->mddev
->pers
&& test_bit(MD_HAS_PPL
, &rdev
->mddev
->flags
) &&
3450 rdev
->raid_disk
>= 0)
3453 if (rdev
->mddev
->persistent
) {
3454 if (rdev
->mddev
->major_version
== 0)
3458 } else if (!rdev
->mddev
->external
) {
3461 rdev
->ppl
.size
= size
;
3465 static struct rdev_sysfs_entry rdev_ppl_size
=
3466 __ATTR(ppl_size
, S_IRUGO
|S_IWUSR
, ppl_size_show
, ppl_size_store
);
3468 static struct attribute
*rdev_default_attrs
[] = {
3473 &rdev_new_offset
.attr
,
3475 &rdev_recovery_start
.attr
,
3476 &rdev_bad_blocks
.attr
,
3477 &rdev_unack_bad_blocks
.attr
,
3478 &rdev_ppl_sector
.attr
,
3479 &rdev_ppl_size
.attr
,
3483 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
3485 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3486 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3492 return entry
->show(rdev
, page
);
3496 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
3497 const char *page
, size_t length
)
3499 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3500 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3502 struct mddev
*mddev
= rdev
->mddev
;
3506 if (!capable(CAP_SYS_ADMIN
))
3508 rv
= mddev
? mddev_lock(mddev
) : -ENODEV
;
3510 if (rdev
->mddev
== NULL
)
3513 rv
= entry
->store(rdev
, page
, length
);
3514 mddev_unlock(mddev
);
3519 static void rdev_free(struct kobject
*ko
)
3521 struct md_rdev
*rdev
= container_of(ko
, struct md_rdev
, kobj
);
3524 static const struct sysfs_ops rdev_sysfs_ops
= {
3525 .show
= rdev_attr_show
,
3526 .store
= rdev_attr_store
,
3528 static struct kobj_type rdev_ktype
= {
3529 .release
= rdev_free
,
3530 .sysfs_ops
= &rdev_sysfs_ops
,
3531 .default_attrs
= rdev_default_attrs
,
3534 int md_rdev_init(struct md_rdev
*rdev
)
3537 rdev
->saved_raid_disk
= -1;
3538 rdev
->raid_disk
= -1;
3540 rdev
->data_offset
= 0;
3541 rdev
->new_data_offset
= 0;
3542 rdev
->sb_events
= 0;
3543 rdev
->last_read_error
= 0;
3544 rdev
->sb_loaded
= 0;
3545 rdev
->bb_page
= NULL
;
3546 atomic_set(&rdev
->nr_pending
, 0);
3547 atomic_set(&rdev
->read_errors
, 0);
3548 atomic_set(&rdev
->corrected_errors
, 0);
3550 INIT_LIST_HEAD(&rdev
->same_set
);
3551 init_waitqueue_head(&rdev
->blocked_wait
);
3553 /* Add space to store bad block list.
3554 * This reserves the space even on arrays where it cannot
3555 * be used - I wonder if that matters
3557 return badblocks_init(&rdev
->badblocks
, 0);
3559 EXPORT_SYMBOL_GPL(md_rdev_init
);
3561 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3563 * mark the device faulty if:
3565 * - the device is nonexistent (zero size)
3566 * - the device has no valid superblock
3568 * a faulty rdev _never_ has rdev->sb set.
3570 static struct md_rdev
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
3572 char b
[BDEVNAME_SIZE
];
3574 struct md_rdev
*rdev
;
3577 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
3579 return ERR_PTR(-ENOMEM
);
3581 err
= md_rdev_init(rdev
);
3584 err
= alloc_disk_sb(rdev
);
3588 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
3592 kobject_init(&rdev
->kobj
, &rdev_ktype
);
3594 size
= i_size_read(rdev
->bdev
->bd_inode
) >> BLOCK_SIZE_BITS
;
3596 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
3597 bdevname(rdev
->bdev
,b
));
3602 if (super_format
>= 0) {
3603 err
= super_types
[super_format
].
3604 load_super(rdev
, NULL
, super_minor
);
3605 if (err
== -EINVAL
) {
3606 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
3607 bdevname(rdev
->bdev
,b
),
3608 super_format
, super_minor
);
3612 pr_warn("md: could not read %s's sb, not importing!\n",
3613 bdevname(rdev
->bdev
,b
));
3623 md_rdev_clear(rdev
);
3625 return ERR_PTR(err
);
3629 * Check a full RAID array for plausibility
3632 static int analyze_sbs(struct mddev
*mddev
)
3635 struct md_rdev
*rdev
, *freshest
, *tmp
;
3636 char b
[BDEVNAME_SIZE
];
3639 rdev_for_each_safe(rdev
, tmp
, mddev
)
3640 switch (super_types
[mddev
->major_version
].
3641 load_super(rdev
, freshest
, mddev
->minor_version
)) {
3648 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
3649 bdevname(rdev
->bdev
,b
));
3650 md_kick_rdev_from_array(rdev
);
3653 /* Cannot find a valid fresh disk */
3655 pr_warn("md: cannot find a valid disk\n");
3659 super_types
[mddev
->major_version
].
3660 validate_super(mddev
, freshest
);
3663 rdev_for_each_safe(rdev
, tmp
, mddev
) {
3664 if (mddev
->max_disks
&&
3665 (rdev
->desc_nr
>= mddev
->max_disks
||
3666 i
> mddev
->max_disks
)) {
3667 pr_warn("md: %s: %s: only %d devices permitted\n",
3668 mdname(mddev
), bdevname(rdev
->bdev
, b
),
3670 md_kick_rdev_from_array(rdev
);
3673 if (rdev
!= freshest
) {
3674 if (super_types
[mddev
->major_version
].
3675 validate_super(mddev
, rdev
)) {
3676 pr_warn("md: kicking non-fresh %s from array!\n",
3677 bdevname(rdev
->bdev
,b
));
3678 md_kick_rdev_from_array(rdev
);
3682 if (mddev
->level
== LEVEL_MULTIPATH
) {
3683 rdev
->desc_nr
= i
++;
3684 rdev
->raid_disk
= rdev
->desc_nr
;
3685 set_bit(In_sync
, &rdev
->flags
);
3686 } else if (rdev
->raid_disk
>=
3687 (mddev
->raid_disks
- min(0, mddev
->delta_disks
)) &&
3688 !test_bit(Journal
, &rdev
->flags
)) {
3689 rdev
->raid_disk
= -1;
3690 clear_bit(In_sync
, &rdev
->flags
);
3697 /* Read a fixed-point number.
3698 * Numbers in sysfs attributes should be in "standard" units where
3699 * possible, so time should be in seconds.
3700 * However we internally use a a much smaller unit such as
3701 * milliseconds or jiffies.
3702 * This function takes a decimal number with a possible fractional
3703 * component, and produces an integer which is the result of
3704 * multiplying that number by 10^'scale'.
3705 * all without any floating-point arithmetic.
3707 int strict_strtoul_scaled(const char *cp
, unsigned long *res
, int scale
)
3709 unsigned long result
= 0;
3711 while (isdigit(*cp
) || (*cp
== '.' && decimals
< 0)) {
3714 else if (decimals
< scale
) {
3717 result
= result
* 10 + value
;
3729 *res
= result
* int_pow(10, scale
- decimals
);
3734 safe_delay_show(struct mddev
*mddev
, char *page
)
3736 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
3737 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
3740 safe_delay_store(struct mddev
*mddev
, const char *cbuf
, size_t len
)
3744 if (mddev_is_clustered(mddev
)) {
3745 pr_warn("md: Safemode is disabled for clustered mode\n");
3749 if (strict_strtoul_scaled(cbuf
, &msec
, 3) < 0)
3752 mddev
->safemode_delay
= 0;
3754 unsigned long old_delay
= mddev
->safemode_delay
;
3755 unsigned long new_delay
= (msec
*HZ
)/1000;
3759 mddev
->safemode_delay
= new_delay
;
3760 if (new_delay
< old_delay
|| old_delay
== 0)
3761 mod_timer(&mddev
->safemode_timer
, jiffies
+1);
3765 static struct md_sysfs_entry md_safe_delay
=
3766 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
3769 level_show(struct mddev
*mddev
, char *page
)
3771 struct md_personality
*p
;
3773 spin_lock(&mddev
->lock
);
3776 ret
= sprintf(page
, "%s\n", p
->name
);
3777 else if (mddev
->clevel
[0])
3778 ret
= sprintf(page
, "%s\n", mddev
->clevel
);
3779 else if (mddev
->level
!= LEVEL_NONE
)
3780 ret
= sprintf(page
, "%d\n", mddev
->level
);
3783 spin_unlock(&mddev
->lock
);
3788 level_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3793 struct md_personality
*pers
, *oldpers
;
3795 void *priv
, *oldpriv
;
3796 struct md_rdev
*rdev
;
3798 if (slen
== 0 || slen
>= sizeof(clevel
))
3801 rv
= mddev_lock(mddev
);
3805 if (mddev
->pers
== NULL
) {
3806 strncpy(mddev
->clevel
, buf
, slen
);
3807 if (mddev
->clevel
[slen
-1] == '\n')
3809 mddev
->clevel
[slen
] = 0;
3810 mddev
->level
= LEVEL_NONE
;
3818 /* request to change the personality. Need to ensure:
3819 * - array is not engaged in resync/recovery/reshape
3820 * - old personality can be suspended
3821 * - new personality will access other array.
3825 if (mddev
->sync_thread
||
3826 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3827 mddev
->reshape_position
!= MaxSector
||
3828 mddev
->sysfs_active
)
3832 if (!mddev
->pers
->quiesce
) {
3833 pr_warn("md: %s: %s does not support online personality change\n",
3834 mdname(mddev
), mddev
->pers
->name
);
3838 /* Now find the new personality */
3839 strncpy(clevel
, buf
, slen
);
3840 if (clevel
[slen
-1] == '\n')
3843 if (kstrtol(clevel
, 10, &level
))
3846 if (request_module("md-%s", clevel
) != 0)
3847 request_module("md-level-%s", clevel
);
3848 spin_lock(&pers_lock
);
3849 pers
= find_pers(level
, clevel
);
3850 if (!pers
|| !try_module_get(pers
->owner
)) {
3851 spin_unlock(&pers_lock
);
3852 pr_warn("md: personality %s not loaded\n", clevel
);
3856 spin_unlock(&pers_lock
);
3858 if (pers
== mddev
->pers
) {
3859 /* Nothing to do! */
3860 module_put(pers
->owner
);
3864 if (!pers
->takeover
) {
3865 module_put(pers
->owner
);
3866 pr_warn("md: %s: %s does not support personality takeover\n",
3867 mdname(mddev
), clevel
);
3872 rdev_for_each(rdev
, mddev
)
3873 rdev
->new_raid_disk
= rdev
->raid_disk
;
3875 /* ->takeover must set new_* and/or delta_disks
3876 * if it succeeds, and may set them when it fails.
3878 priv
= pers
->takeover(mddev
);
3880 mddev
->new_level
= mddev
->level
;
3881 mddev
->new_layout
= mddev
->layout
;
3882 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3883 mddev
->raid_disks
-= mddev
->delta_disks
;
3884 mddev
->delta_disks
= 0;
3885 mddev
->reshape_backwards
= 0;
3886 module_put(pers
->owner
);
3887 pr_warn("md: %s: %s would not accept array\n",
3888 mdname(mddev
), clevel
);
3893 /* Looks like we have a winner */
3894 mddev_suspend(mddev
);
3895 mddev_detach(mddev
);
3897 spin_lock(&mddev
->lock
);
3898 oldpers
= mddev
->pers
;
3899 oldpriv
= mddev
->private;
3901 mddev
->private = priv
;
3902 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3903 mddev
->level
= mddev
->new_level
;
3904 mddev
->layout
= mddev
->new_layout
;
3905 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
3906 mddev
->delta_disks
= 0;
3907 mddev
->reshape_backwards
= 0;
3908 mddev
->degraded
= 0;
3909 spin_unlock(&mddev
->lock
);
3911 if (oldpers
->sync_request
== NULL
&&
3913 /* We are converting from a no-redundancy array
3914 * to a redundancy array and metadata is managed
3915 * externally so we need to be sure that writes
3916 * won't block due to a need to transition
3918 * until external management is started.
3921 mddev
->safemode_delay
= 0;
3922 mddev
->safemode
= 0;
3925 oldpers
->free(mddev
, oldpriv
);
3927 if (oldpers
->sync_request
== NULL
&&
3928 pers
->sync_request
!= NULL
) {
3929 /* need to add the md_redundancy_group */
3930 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
3931 pr_warn("md: cannot register extra attributes for %s\n",
3933 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, "sync_action");
3935 if (oldpers
->sync_request
!= NULL
&&
3936 pers
->sync_request
== NULL
) {
3937 /* need to remove the md_redundancy_group */
3938 if (mddev
->to_remove
== NULL
)
3939 mddev
->to_remove
= &md_redundancy_group
;
3942 module_put(oldpers
->owner
);
3944 rdev_for_each(rdev
, mddev
) {
3945 if (rdev
->raid_disk
< 0)
3947 if (rdev
->new_raid_disk
>= mddev
->raid_disks
)
3948 rdev
->new_raid_disk
= -1;
3949 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3951 sysfs_unlink_rdev(mddev
, rdev
);
3953 rdev_for_each(rdev
, mddev
) {
3954 if (rdev
->raid_disk
< 0)
3956 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3958 rdev
->raid_disk
= rdev
->new_raid_disk
;
3959 if (rdev
->raid_disk
< 0)
3960 clear_bit(In_sync
, &rdev
->flags
);
3962 if (sysfs_link_rdev(mddev
, rdev
))
3963 pr_warn("md: cannot register rd%d for %s after level change\n",
3964 rdev
->raid_disk
, mdname(mddev
));
3968 if (pers
->sync_request
== NULL
) {
3969 /* this is now an array without redundancy, so
3970 * it must always be in_sync
3973 del_timer_sync(&mddev
->safemode_timer
);
3975 blk_set_stacking_limits(&mddev
->queue
->limits
);
3977 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
3978 mddev_resume(mddev
);
3980 md_update_sb(mddev
, 1);
3981 sysfs_notify(&mddev
->kobj
, NULL
, "level");
3982 md_new_event(mddev
);
3985 mddev_unlock(mddev
);
3989 static struct md_sysfs_entry md_level
=
3990 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
3993 layout_show(struct mddev
*mddev
, char *page
)
3995 /* just a number, not meaningful for all levels */
3996 if (mddev
->reshape_position
!= MaxSector
&&
3997 mddev
->layout
!= mddev
->new_layout
)
3998 return sprintf(page
, "%d (%d)\n",
3999 mddev
->new_layout
, mddev
->layout
);
4000 return sprintf(page
, "%d\n", mddev
->layout
);
4004 layout_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4009 err
= kstrtouint(buf
, 10, &n
);
4012 err
= mddev_lock(mddev
);
4017 if (mddev
->pers
->check_reshape
== NULL
)
4022 mddev
->new_layout
= n
;
4023 err
= mddev
->pers
->check_reshape(mddev
);
4025 mddev
->new_layout
= mddev
->layout
;
4028 mddev
->new_layout
= n
;
4029 if (mddev
->reshape_position
== MaxSector
)
4032 mddev_unlock(mddev
);
4035 static struct md_sysfs_entry md_layout
=
4036 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
4039 raid_disks_show(struct mddev
*mddev
, char *page
)
4041 if (mddev
->raid_disks
== 0)
4043 if (mddev
->reshape_position
!= MaxSector
&&
4044 mddev
->delta_disks
!= 0)
4045 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
4046 mddev
->raid_disks
- mddev
->delta_disks
);
4047 return sprintf(page
, "%d\n", mddev
->raid_disks
);
4050 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
);
4053 raid_disks_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4058 err
= kstrtouint(buf
, 10, &n
);
4062 err
= mddev_lock(mddev
);
4066 err
= update_raid_disks(mddev
, n
);
4067 else if (mddev
->reshape_position
!= MaxSector
) {
4068 struct md_rdev
*rdev
;
4069 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
4072 rdev_for_each(rdev
, mddev
) {
4074 rdev
->data_offset
< rdev
->new_data_offset
)
4077 rdev
->data_offset
> rdev
->new_data_offset
)
4081 mddev
->delta_disks
= n
- olddisks
;
4082 mddev
->raid_disks
= n
;
4083 mddev
->reshape_backwards
= (mddev
->delta_disks
< 0);
4085 mddev
->raid_disks
= n
;
4087 mddev_unlock(mddev
);
4088 return err
? err
: len
;
4090 static struct md_sysfs_entry md_raid_disks
=
4091 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
4094 chunk_size_show(struct mddev
*mddev
, char *page
)
4096 if (mddev
->reshape_position
!= MaxSector
&&
4097 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
4098 return sprintf(page
, "%d (%d)\n",
4099 mddev
->new_chunk_sectors
<< 9,
4100 mddev
->chunk_sectors
<< 9);
4101 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
4105 chunk_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4110 err
= kstrtoul(buf
, 10, &n
);
4114 err
= mddev_lock(mddev
);
4118 if (mddev
->pers
->check_reshape
== NULL
)
4123 mddev
->new_chunk_sectors
= n
>> 9;
4124 err
= mddev
->pers
->check_reshape(mddev
);
4126 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4129 mddev
->new_chunk_sectors
= n
>> 9;
4130 if (mddev
->reshape_position
== MaxSector
)
4131 mddev
->chunk_sectors
= n
>> 9;
4133 mddev_unlock(mddev
);
4136 static struct md_sysfs_entry md_chunk_size
=
4137 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
4140 resync_start_show(struct mddev
*mddev
, char *page
)
4142 if (mddev
->recovery_cp
== MaxSector
)
4143 return sprintf(page
, "none\n");
4144 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
4148 resync_start_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4150 unsigned long long n
;
4153 if (cmd_match(buf
, "none"))
4156 err
= kstrtoull(buf
, 10, &n
);
4159 if (n
!= (sector_t
)n
)
4163 err
= mddev_lock(mddev
);
4166 if (mddev
->pers
&& !test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
4170 mddev
->recovery_cp
= n
;
4172 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
4174 mddev_unlock(mddev
);
4177 static struct md_sysfs_entry md_resync_start
=
4178 __ATTR_PREALLOC(resync_start
, S_IRUGO
|S_IWUSR
,
4179 resync_start_show
, resync_start_store
);
4182 * The array state can be:
4185 * No devices, no size, no level
4186 * Equivalent to STOP_ARRAY ioctl
4188 * May have some settings, but array is not active
4189 * all IO results in error
4190 * When written, doesn't tear down array, but just stops it
4191 * suspended (not supported yet)
4192 * All IO requests will block. The array can be reconfigured.
4193 * Writing this, if accepted, will block until array is quiescent
4195 * no resync can happen. no superblocks get written.
4196 * write requests fail
4198 * like readonly, but behaves like 'clean' on a write request.
4200 * clean - no pending writes, but otherwise active.
4201 * When written to inactive array, starts without resync
4202 * If a write request arrives then
4203 * if metadata is known, mark 'dirty' and switch to 'active'.
4204 * if not known, block and switch to write-pending
4205 * If written to an active array that has pending writes, then fails.
4207 * fully active: IO and resync can be happening.
4208 * When written to inactive array, starts with resync
4211 * clean, but writes are blocked waiting for 'active' to be written.
4214 * like active, but no writes have been seen for a while (100msec).
4217 * RAID0/LINEAR-only: same as clean, but array is missing a member.
4218 * It's useful because RAID0/LINEAR mounted-arrays aren't stopped
4219 * when a member is gone, so this state will at least alert the
4220 * user that something is wrong.
4222 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
4223 write_pending
, active_idle
, broken
, bad_word
};
4224 static char *array_states
[] = {
4225 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
4226 "write-pending", "active-idle", "broken", NULL
};
4228 static int match_word(const char *word
, char **list
)
4231 for (n
=0; list
[n
]; n
++)
4232 if (cmd_match(word
, list
[n
]))
4238 array_state_show(struct mddev
*mddev
, char *page
)
4240 enum array_state st
= inactive
;
4242 if (mddev
->pers
&& !test_bit(MD_NOT_READY
, &mddev
->flags
)) {
4251 spin_lock(&mddev
->lock
);
4252 if (test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
))
4254 else if (mddev
->in_sync
)
4256 else if (mddev
->safemode
)
4260 spin_unlock(&mddev
->lock
);
4263 if (test_bit(MD_BROKEN
, &mddev
->flags
) && st
== clean
)
4266 if (list_empty(&mddev
->disks
) &&
4267 mddev
->raid_disks
== 0 &&
4268 mddev
->dev_sectors
== 0)
4273 return sprintf(page
, "%s\n", array_states
[st
]);
4276 static int do_md_stop(struct mddev
*mddev
, int ro
, struct block_device
*bdev
);
4277 static int md_set_readonly(struct mddev
*mddev
, struct block_device
*bdev
);
4278 static int do_md_run(struct mddev
*mddev
);
4279 static int restart_array(struct mddev
*mddev
);
4282 array_state_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4285 enum array_state st
= match_word(buf
, array_states
);
4287 if (mddev
->pers
&& (st
== active
|| st
== clean
) && mddev
->ro
!= 1) {
4288 /* don't take reconfig_mutex when toggling between
4291 spin_lock(&mddev
->lock
);
4293 restart_array(mddev
);
4294 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
4295 md_wakeup_thread(mddev
->thread
);
4296 wake_up(&mddev
->sb_wait
);
4297 } else /* st == clean */ {
4298 restart_array(mddev
);
4299 if (!set_in_sync(mddev
))
4303 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4304 spin_unlock(&mddev
->lock
);
4307 err
= mddev_lock(mddev
);
4315 /* stopping an active array */
4316 err
= do_md_stop(mddev
, 0, NULL
);
4319 /* stopping an active array */
4321 err
= do_md_stop(mddev
, 2, NULL
);
4323 err
= 0; /* already inactive */
4326 break; /* not supported yet */
4329 err
= md_set_readonly(mddev
, NULL
);
4332 set_disk_ro(mddev
->gendisk
, 1);
4333 err
= do_md_run(mddev
);
4339 err
= md_set_readonly(mddev
, NULL
);
4340 else if (mddev
->ro
== 1)
4341 err
= restart_array(mddev
);
4344 set_disk_ro(mddev
->gendisk
, 0);
4348 err
= do_md_run(mddev
);
4353 err
= restart_array(mddev
);
4356 spin_lock(&mddev
->lock
);
4357 if (!set_in_sync(mddev
))
4359 spin_unlock(&mddev
->lock
);
4365 err
= restart_array(mddev
);
4368 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
4369 wake_up(&mddev
->sb_wait
);
4373 set_disk_ro(mddev
->gendisk
, 0);
4374 err
= do_md_run(mddev
);
4380 /* these cannot be set */
4385 if (mddev
->hold_active
== UNTIL_IOCTL
)
4386 mddev
->hold_active
= 0;
4387 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4389 mddev_unlock(mddev
);
4392 static struct md_sysfs_entry md_array_state
=
4393 __ATTR_PREALLOC(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
4396 max_corrected_read_errors_show(struct mddev
*mddev
, char *page
) {
4397 return sprintf(page
, "%d\n",
4398 atomic_read(&mddev
->max_corr_read_errors
));
4402 max_corrected_read_errors_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4407 rv
= kstrtouint(buf
, 10, &n
);
4410 atomic_set(&mddev
->max_corr_read_errors
, n
);
4414 static struct md_sysfs_entry max_corr_read_errors
=
4415 __ATTR(max_read_errors
, S_IRUGO
|S_IWUSR
, max_corrected_read_errors_show
,
4416 max_corrected_read_errors_store
);
4419 null_show(struct mddev
*mddev
, char *page
)
4425 new_dev_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4427 /* buf must be %d:%d\n? giving major and minor numbers */
4428 /* The new device is added to the array.
4429 * If the array has a persistent superblock, we read the
4430 * superblock to initialise info and check validity.
4431 * Otherwise, only checking done is that in bind_rdev_to_array,
4432 * which mainly checks size.
4435 int major
= simple_strtoul(buf
, &e
, 10);
4438 struct md_rdev
*rdev
;
4441 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
4443 minor
= simple_strtoul(e
+1, &e
, 10);
4444 if (*e
&& *e
!= '\n')
4446 dev
= MKDEV(major
, minor
);
4447 if (major
!= MAJOR(dev
) ||
4448 minor
!= MINOR(dev
))
4451 flush_workqueue(md_misc_wq
);
4453 err
= mddev_lock(mddev
);
4456 if (mddev
->persistent
) {
4457 rdev
= md_import_device(dev
, mddev
->major_version
,
4458 mddev
->minor_version
);
4459 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
4460 struct md_rdev
*rdev0
4461 = list_entry(mddev
->disks
.next
,
4462 struct md_rdev
, same_set
);
4463 err
= super_types
[mddev
->major_version
]
4464 .load_super(rdev
, rdev0
, mddev
->minor_version
);
4468 } else if (mddev
->external
)
4469 rdev
= md_import_device(dev
, -2, -1);
4471 rdev
= md_import_device(dev
, -1, -1);
4474 mddev_unlock(mddev
);
4475 return PTR_ERR(rdev
);
4477 err
= bind_rdev_to_array(rdev
, mddev
);
4481 mddev_unlock(mddev
);
4483 md_new_event(mddev
);
4484 return err
? err
: len
;
4487 static struct md_sysfs_entry md_new_device
=
4488 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
4491 bitmap_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4494 unsigned long chunk
, end_chunk
;
4497 err
= mddev_lock(mddev
);
4502 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4504 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
4505 if (buf
== end
) break;
4506 if (*end
== '-') { /* range */
4508 end_chunk
= simple_strtoul(buf
, &end
, 0);
4509 if (buf
== end
) break;
4511 if (*end
&& !isspace(*end
)) break;
4512 md_bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
4513 buf
= skip_spaces(end
);
4515 md_bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
4517 mddev_unlock(mddev
);
4521 static struct md_sysfs_entry md_bitmap
=
4522 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
4525 size_show(struct mddev
*mddev
, char *page
)
4527 return sprintf(page
, "%llu\n",
4528 (unsigned long long)mddev
->dev_sectors
/ 2);
4531 static int update_size(struct mddev
*mddev
, sector_t num_sectors
);
4534 size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4536 /* If array is inactive, we can reduce the component size, but
4537 * not increase it (except from 0).
4538 * If array is active, we can try an on-line resize
4541 int err
= strict_blocks_to_sectors(buf
, §ors
);
4545 err
= mddev_lock(mddev
);
4549 err
= update_size(mddev
, sectors
);
4551 md_update_sb(mddev
, 1);
4553 if (mddev
->dev_sectors
== 0 ||
4554 mddev
->dev_sectors
> sectors
)
4555 mddev
->dev_sectors
= sectors
;
4559 mddev_unlock(mddev
);
4560 return err
? err
: len
;
4563 static struct md_sysfs_entry md_size
=
4564 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
4566 /* Metadata version.
4568 * 'none' for arrays with no metadata (good luck...)
4569 * 'external' for arrays with externally managed metadata,
4570 * or N.M for internally known formats
4573 metadata_show(struct mddev
*mddev
, char *page
)
4575 if (mddev
->persistent
)
4576 return sprintf(page
, "%d.%d\n",
4577 mddev
->major_version
, mddev
->minor_version
);
4578 else if (mddev
->external
)
4579 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
4581 return sprintf(page
, "none\n");
4585 metadata_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4590 /* Changing the details of 'external' metadata is
4591 * always permitted. Otherwise there must be
4592 * no devices attached to the array.
4595 err
= mddev_lock(mddev
);
4599 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
4601 else if (!list_empty(&mddev
->disks
))
4605 if (cmd_match(buf
, "none")) {
4606 mddev
->persistent
= 0;
4607 mddev
->external
= 0;
4608 mddev
->major_version
= 0;
4609 mddev
->minor_version
= 90;
4612 if (strncmp(buf
, "external:", 9) == 0) {
4613 size_t namelen
= len
-9;
4614 if (namelen
>= sizeof(mddev
->metadata_type
))
4615 namelen
= sizeof(mddev
->metadata_type
)-1;
4616 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
4617 mddev
->metadata_type
[namelen
] = 0;
4618 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
4619 mddev
->metadata_type
[--namelen
] = 0;
4620 mddev
->persistent
= 0;
4621 mddev
->external
= 1;
4622 mddev
->major_version
= 0;
4623 mddev
->minor_version
= 90;
4626 major
= simple_strtoul(buf
, &e
, 10);
4628 if (e
==buf
|| *e
!= '.')
4631 minor
= simple_strtoul(buf
, &e
, 10);
4632 if (e
==buf
|| (*e
&& *e
!= '\n') )
4635 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
4637 mddev
->major_version
= major
;
4638 mddev
->minor_version
= minor
;
4639 mddev
->persistent
= 1;
4640 mddev
->external
= 0;
4643 mddev_unlock(mddev
);
4647 static struct md_sysfs_entry md_metadata
=
4648 __ATTR_PREALLOC(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
4651 action_show(struct mddev
*mddev
, char *page
)
4653 char *type
= "idle";
4654 unsigned long recovery
= mddev
->recovery
;
4655 if (test_bit(MD_RECOVERY_FROZEN
, &recovery
))
4657 else if (test_bit(MD_RECOVERY_RUNNING
, &recovery
) ||
4658 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &recovery
))) {
4659 if (test_bit(MD_RECOVERY_RESHAPE
, &recovery
))
4661 else if (test_bit(MD_RECOVERY_SYNC
, &recovery
)) {
4662 if (!test_bit(MD_RECOVERY_REQUESTED
, &recovery
))
4664 else if (test_bit(MD_RECOVERY_CHECK
, &recovery
))
4668 } else if (test_bit(MD_RECOVERY_RECOVER
, &recovery
))
4670 else if (mddev
->reshape_position
!= MaxSector
)
4673 return sprintf(page
, "%s\n", type
);
4677 action_store(struct mddev
*mddev
, const char *page
, size_t len
)
4679 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
4683 if (cmd_match(page
, "idle") || cmd_match(page
, "frozen")) {
4684 if (cmd_match(page
, "frozen"))
4685 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4687 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4688 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
4689 mddev_lock(mddev
) == 0) {
4690 flush_workqueue(md_misc_wq
);
4691 if (mddev
->sync_thread
) {
4692 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4693 md_reap_sync_thread(mddev
);
4695 mddev_unlock(mddev
);
4697 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4699 else if (cmd_match(page
, "resync"))
4700 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4701 else if (cmd_match(page
, "recover")) {
4702 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4703 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
4704 } else if (cmd_match(page
, "reshape")) {
4706 if (mddev
->pers
->start_reshape
== NULL
)
4708 err
= mddev_lock(mddev
);
4710 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4713 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4714 err
= mddev
->pers
->start_reshape(mddev
);
4716 mddev_unlock(mddev
);
4720 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4722 if (cmd_match(page
, "check"))
4723 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
4724 else if (!cmd_match(page
, "repair"))
4726 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4727 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
4728 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
4730 if (mddev
->ro
== 2) {
4731 /* A write to sync_action is enough to justify
4732 * canceling read-auto mode
4735 md_wakeup_thread(mddev
->sync_thread
);
4737 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4738 md_wakeup_thread(mddev
->thread
);
4739 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4743 static struct md_sysfs_entry md_scan_mode
=
4744 __ATTR_PREALLOC(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
4747 last_sync_action_show(struct mddev
*mddev
, char *page
)
4749 return sprintf(page
, "%s\n", mddev
->last_sync_action
);
4752 static struct md_sysfs_entry md_last_scan_mode
= __ATTR_RO(last_sync_action
);
4755 mismatch_cnt_show(struct mddev
*mddev
, char *page
)
4757 return sprintf(page
, "%llu\n",
4758 (unsigned long long)
4759 atomic64_read(&mddev
->resync_mismatches
));
4762 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
4765 sync_min_show(struct mddev
*mddev
, char *page
)
4767 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
4768 mddev
->sync_speed_min
? "local": "system");
4772 sync_min_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4777 if (strncmp(buf
, "system", 6)==0) {
4780 rv
= kstrtouint(buf
, 10, &min
);
4786 mddev
->sync_speed_min
= min
;
4790 static struct md_sysfs_entry md_sync_min
=
4791 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
4794 sync_max_show(struct mddev
*mddev
, char *page
)
4796 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
4797 mddev
->sync_speed_max
? "local": "system");
4801 sync_max_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4806 if (strncmp(buf
, "system", 6)==0) {
4809 rv
= kstrtouint(buf
, 10, &max
);
4815 mddev
->sync_speed_max
= max
;
4819 static struct md_sysfs_entry md_sync_max
=
4820 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
4823 degraded_show(struct mddev
*mddev
, char *page
)
4825 return sprintf(page
, "%d\n", mddev
->degraded
);
4827 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
4830 sync_force_parallel_show(struct mddev
*mddev
, char *page
)
4832 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
4836 sync_force_parallel_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4840 if (kstrtol(buf
, 10, &n
))
4843 if (n
!= 0 && n
!= 1)
4846 mddev
->parallel_resync
= n
;
4848 if (mddev
->sync_thread
)
4849 wake_up(&resync_wait
);
4854 /* force parallel resync, even with shared block devices */
4855 static struct md_sysfs_entry md_sync_force_parallel
=
4856 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
4857 sync_force_parallel_show
, sync_force_parallel_store
);
4860 sync_speed_show(struct mddev
*mddev
, char *page
)
4862 unsigned long resync
, dt
, db
;
4863 if (mddev
->curr_resync
== 0)
4864 return sprintf(page
, "none\n");
4865 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
4866 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
4868 db
= resync
- mddev
->resync_mark_cnt
;
4869 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
4872 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
4875 sync_completed_show(struct mddev
*mddev
, char *page
)
4877 unsigned long long max_sectors
, resync
;
4879 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4880 return sprintf(page
, "none\n");
4882 if (mddev
->curr_resync
== 1 ||
4883 mddev
->curr_resync
== 2)
4884 return sprintf(page
, "delayed\n");
4886 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
4887 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
4888 max_sectors
= mddev
->resync_max_sectors
;
4890 max_sectors
= mddev
->dev_sectors
;
4892 resync
= mddev
->curr_resync_completed
;
4893 return sprintf(page
, "%llu / %llu\n", resync
, max_sectors
);
4896 static struct md_sysfs_entry md_sync_completed
=
4897 __ATTR_PREALLOC(sync_completed
, S_IRUGO
, sync_completed_show
, NULL
);
4900 min_sync_show(struct mddev
*mddev
, char *page
)
4902 return sprintf(page
, "%llu\n",
4903 (unsigned long long)mddev
->resync_min
);
4906 min_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4908 unsigned long long min
;
4911 if (kstrtoull(buf
, 10, &min
))
4914 spin_lock(&mddev
->lock
);
4916 if (min
> mddev
->resync_max
)
4920 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4923 /* Round down to multiple of 4K for safety */
4924 mddev
->resync_min
= round_down(min
, 8);
4928 spin_unlock(&mddev
->lock
);
4932 static struct md_sysfs_entry md_min_sync
=
4933 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
4936 max_sync_show(struct mddev
*mddev
, char *page
)
4938 if (mddev
->resync_max
== MaxSector
)
4939 return sprintf(page
, "max\n");
4941 return sprintf(page
, "%llu\n",
4942 (unsigned long long)mddev
->resync_max
);
4945 max_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4948 spin_lock(&mddev
->lock
);
4949 if (strncmp(buf
, "max", 3) == 0)
4950 mddev
->resync_max
= MaxSector
;
4952 unsigned long long max
;
4956 if (kstrtoull(buf
, 10, &max
))
4958 if (max
< mddev
->resync_min
)
4962 if (max
< mddev
->resync_max
&&
4964 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4967 /* Must be a multiple of chunk_size */
4968 chunk
= mddev
->chunk_sectors
;
4970 sector_t temp
= max
;
4973 if (sector_div(temp
, chunk
))
4976 mddev
->resync_max
= max
;
4978 wake_up(&mddev
->recovery_wait
);
4981 spin_unlock(&mddev
->lock
);
4985 static struct md_sysfs_entry md_max_sync
=
4986 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
4989 suspend_lo_show(struct mddev
*mddev
, char *page
)
4991 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
4995 suspend_lo_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4997 unsigned long long new;
5000 err
= kstrtoull(buf
, 10, &new);
5003 if (new != (sector_t
)new)
5006 err
= mddev_lock(mddev
);
5010 if (mddev
->pers
== NULL
||
5011 mddev
->pers
->quiesce
== NULL
)
5013 mddev_suspend(mddev
);
5014 mddev
->suspend_lo
= new;
5015 mddev_resume(mddev
);
5019 mddev_unlock(mddev
);
5022 static struct md_sysfs_entry md_suspend_lo
=
5023 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
5026 suspend_hi_show(struct mddev
*mddev
, char *page
)
5028 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
5032 suspend_hi_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5034 unsigned long long new;
5037 err
= kstrtoull(buf
, 10, &new);
5040 if (new != (sector_t
)new)
5043 err
= mddev_lock(mddev
);
5047 if (mddev
->pers
== NULL
)
5050 mddev_suspend(mddev
);
5051 mddev
->suspend_hi
= new;
5052 mddev_resume(mddev
);
5056 mddev_unlock(mddev
);
5059 static struct md_sysfs_entry md_suspend_hi
=
5060 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
5063 reshape_position_show(struct mddev
*mddev
, char *page
)
5065 if (mddev
->reshape_position
!= MaxSector
)
5066 return sprintf(page
, "%llu\n",
5067 (unsigned long long)mddev
->reshape_position
);
5068 strcpy(page
, "none\n");
5073 reshape_position_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5075 struct md_rdev
*rdev
;
5076 unsigned long long new;
5079 err
= kstrtoull(buf
, 10, &new);
5082 if (new != (sector_t
)new)
5084 err
= mddev_lock(mddev
);
5090 mddev
->reshape_position
= new;
5091 mddev
->delta_disks
= 0;
5092 mddev
->reshape_backwards
= 0;
5093 mddev
->new_level
= mddev
->level
;
5094 mddev
->new_layout
= mddev
->layout
;
5095 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
5096 rdev_for_each(rdev
, mddev
)
5097 rdev
->new_data_offset
= rdev
->data_offset
;
5100 mddev_unlock(mddev
);
5104 static struct md_sysfs_entry md_reshape_position
=
5105 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
5106 reshape_position_store
);
5109 reshape_direction_show(struct mddev
*mddev
, char *page
)
5111 return sprintf(page
, "%s\n",
5112 mddev
->reshape_backwards
? "backwards" : "forwards");
5116 reshape_direction_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5121 if (cmd_match(buf
, "forwards"))
5123 else if (cmd_match(buf
, "backwards"))
5127 if (mddev
->reshape_backwards
== backwards
)
5130 err
= mddev_lock(mddev
);
5133 /* check if we are allowed to change */
5134 if (mddev
->delta_disks
)
5136 else if (mddev
->persistent
&&
5137 mddev
->major_version
== 0)
5140 mddev
->reshape_backwards
= backwards
;
5141 mddev_unlock(mddev
);
5145 static struct md_sysfs_entry md_reshape_direction
=
5146 __ATTR(reshape_direction
, S_IRUGO
|S_IWUSR
, reshape_direction_show
,
5147 reshape_direction_store
);
5150 array_size_show(struct mddev
*mddev
, char *page
)
5152 if (mddev
->external_size
)
5153 return sprintf(page
, "%llu\n",
5154 (unsigned long long)mddev
->array_sectors
/2);
5156 return sprintf(page
, "default\n");
5160 array_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5165 err
= mddev_lock(mddev
);
5169 /* cluster raid doesn't support change array_sectors */
5170 if (mddev_is_clustered(mddev
)) {
5171 mddev_unlock(mddev
);
5175 if (strncmp(buf
, "default", 7) == 0) {
5177 sectors
= mddev
->pers
->size(mddev
, 0, 0);
5179 sectors
= mddev
->array_sectors
;
5181 mddev
->external_size
= 0;
5183 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
5185 else if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
5188 mddev
->external_size
= 1;
5192 mddev
->array_sectors
= sectors
;
5194 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
5195 revalidate_disk(mddev
->gendisk
);
5198 mddev_unlock(mddev
);
5202 static struct md_sysfs_entry md_array_size
=
5203 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
5207 consistency_policy_show(struct mddev
*mddev
, char *page
)
5211 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
)) {
5212 ret
= sprintf(page
, "journal\n");
5213 } else if (test_bit(MD_HAS_PPL
, &mddev
->flags
)) {
5214 ret
= sprintf(page
, "ppl\n");
5215 } else if (mddev
->bitmap
) {
5216 ret
= sprintf(page
, "bitmap\n");
5217 } else if (mddev
->pers
) {
5218 if (mddev
->pers
->sync_request
)
5219 ret
= sprintf(page
, "resync\n");
5221 ret
= sprintf(page
, "none\n");
5223 ret
= sprintf(page
, "unknown\n");
5230 consistency_policy_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5235 if (mddev
->pers
->change_consistency_policy
)
5236 err
= mddev
->pers
->change_consistency_policy(mddev
, buf
);
5239 } else if (mddev
->external
&& strncmp(buf
, "ppl", 3) == 0) {
5240 set_bit(MD_HAS_PPL
, &mddev
->flags
);
5245 return err
? err
: len
;
5248 static struct md_sysfs_entry md_consistency_policy
=
5249 __ATTR(consistency_policy
, S_IRUGO
| S_IWUSR
, consistency_policy_show
,
5250 consistency_policy_store
);
5252 static ssize_t
fail_last_dev_show(struct mddev
*mddev
, char *page
)
5254 return sprintf(page
, "%d\n", mddev
->fail_last_dev
);
5258 * Setting fail_last_dev to true to allow last device to be forcibly removed
5259 * from RAID1/RAID10.
5262 fail_last_dev_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5267 ret
= kstrtobool(buf
, &value
);
5271 if (value
!= mddev
->fail_last_dev
)
5272 mddev
->fail_last_dev
= value
;
5276 static struct md_sysfs_entry md_fail_last_dev
=
5277 __ATTR(fail_last_dev
, S_IRUGO
| S_IWUSR
, fail_last_dev_show
,
5278 fail_last_dev_store
);
5280 static struct attribute
*md_default_attrs
[] = {
5283 &md_raid_disks
.attr
,
5284 &md_chunk_size
.attr
,
5286 &md_resync_start
.attr
,
5288 &md_new_device
.attr
,
5289 &md_safe_delay
.attr
,
5290 &md_array_state
.attr
,
5291 &md_reshape_position
.attr
,
5292 &md_reshape_direction
.attr
,
5293 &md_array_size
.attr
,
5294 &max_corr_read_errors
.attr
,
5295 &md_consistency_policy
.attr
,
5296 &md_fail_last_dev
.attr
,
5300 static struct attribute
*md_redundancy_attrs
[] = {
5302 &md_last_scan_mode
.attr
,
5303 &md_mismatches
.attr
,
5306 &md_sync_speed
.attr
,
5307 &md_sync_force_parallel
.attr
,
5308 &md_sync_completed
.attr
,
5311 &md_suspend_lo
.attr
,
5312 &md_suspend_hi
.attr
,
5317 static struct attribute_group md_redundancy_group
= {
5319 .attrs
= md_redundancy_attrs
,
5323 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
5325 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
5326 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
5331 spin_lock(&all_mddevs_lock
);
5332 if (list_empty(&mddev
->all_mddevs
)) {
5333 spin_unlock(&all_mddevs_lock
);
5337 spin_unlock(&all_mddevs_lock
);
5339 rv
= entry
->show(mddev
, page
);
5345 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
5346 const char *page
, size_t length
)
5348 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
5349 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
5354 if (!capable(CAP_SYS_ADMIN
))
5356 spin_lock(&all_mddevs_lock
);
5357 if (list_empty(&mddev
->all_mddevs
)) {
5358 spin_unlock(&all_mddevs_lock
);
5362 spin_unlock(&all_mddevs_lock
);
5363 rv
= entry
->store(mddev
, page
, length
);
5368 static void md_free(struct kobject
*ko
)
5370 struct mddev
*mddev
= container_of(ko
, struct mddev
, kobj
);
5372 if (mddev
->sysfs_state
)
5373 sysfs_put(mddev
->sysfs_state
);
5376 del_gendisk(mddev
->gendisk
);
5378 blk_cleanup_queue(mddev
->queue
);
5380 put_disk(mddev
->gendisk
);
5381 percpu_ref_exit(&mddev
->writes_pending
);
5383 bioset_exit(&mddev
->bio_set
);
5384 bioset_exit(&mddev
->sync_set
);
5388 static const struct sysfs_ops md_sysfs_ops
= {
5389 .show
= md_attr_show
,
5390 .store
= md_attr_store
,
5392 static struct kobj_type md_ktype
= {
5394 .sysfs_ops
= &md_sysfs_ops
,
5395 .default_attrs
= md_default_attrs
,
5400 static void mddev_delayed_delete(struct work_struct
*ws
)
5402 struct mddev
*mddev
= container_of(ws
, struct mddev
, del_work
);
5404 sysfs_remove_group(&mddev
->kobj
, &md_bitmap_group
);
5405 kobject_del(&mddev
->kobj
);
5406 kobject_put(&mddev
->kobj
);
5409 static void no_op(struct percpu_ref
*r
) {}
5411 int mddev_init_writes_pending(struct mddev
*mddev
)
5413 if (mddev
->writes_pending
.percpu_count_ptr
)
5415 if (percpu_ref_init(&mddev
->writes_pending
, no_op
,
5416 PERCPU_REF_ALLOW_REINIT
, GFP_KERNEL
) < 0)
5418 /* We want to start with the refcount at zero */
5419 percpu_ref_put(&mddev
->writes_pending
);
5422 EXPORT_SYMBOL_GPL(mddev_init_writes_pending
);
5424 static int md_alloc(dev_t dev
, char *name
)
5427 * If dev is zero, name is the name of a device to allocate with
5428 * an arbitrary minor number. It will be "md_???"
5429 * If dev is non-zero it must be a device number with a MAJOR of
5430 * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then
5431 * the device is being created by opening a node in /dev.
5432 * If "name" is not NULL, the device is being created by
5433 * writing to /sys/module/md_mod/parameters/new_array.
5435 static DEFINE_MUTEX(disks_mutex
);
5436 struct mddev
*mddev
= mddev_find(dev
);
5437 struct gendisk
*disk
;
5446 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
5447 shift
= partitioned
? MdpMinorShift
: 0;
5448 unit
= MINOR(mddev
->unit
) >> shift
;
5450 /* wait for any previous instance of this device to be
5451 * completely removed (mddev_delayed_delete).
5453 flush_workqueue(md_misc_wq
);
5455 mutex_lock(&disks_mutex
);
5461 /* Need to ensure that 'name' is not a duplicate.
5463 struct mddev
*mddev2
;
5464 spin_lock(&all_mddevs_lock
);
5466 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
5467 if (mddev2
->gendisk
&&
5468 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
5469 spin_unlock(&all_mddevs_lock
);
5472 spin_unlock(&all_mddevs_lock
);
5476 * Creating /dev/mdNNN via "newarray", so adjust hold_active.
5478 mddev
->hold_active
= UNTIL_STOP
;
5481 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
5484 mddev
->queue
->queuedata
= mddev
;
5486 blk_queue_make_request(mddev
->queue
, md_make_request
);
5487 blk_set_stacking_limits(&mddev
->queue
->limits
);
5489 disk
= alloc_disk(1 << shift
);
5491 blk_cleanup_queue(mddev
->queue
);
5492 mddev
->queue
= NULL
;
5495 disk
->major
= MAJOR(mddev
->unit
);
5496 disk
->first_minor
= unit
<< shift
;
5498 strcpy(disk
->disk_name
, name
);
5499 else if (partitioned
)
5500 sprintf(disk
->disk_name
, "md_d%d", unit
);
5502 sprintf(disk
->disk_name
, "md%d", unit
);
5503 disk
->fops
= &md_fops
;
5504 disk
->private_data
= mddev
;
5505 disk
->queue
= mddev
->queue
;
5506 blk_queue_write_cache(mddev
->queue
, true, true);
5507 /* Allow extended partitions. This makes the
5508 * 'mdp' device redundant, but we can't really
5511 disk
->flags
|= GENHD_FL_EXT_DEVT
;
5512 mddev
->gendisk
= disk
;
5513 /* As soon as we call add_disk(), another thread could get
5514 * through to md_open, so make sure it doesn't get too far
5516 mutex_lock(&mddev
->open_mutex
);
5519 error
= kobject_add(&mddev
->kobj
, &disk_to_dev(disk
)->kobj
, "%s", "md");
5521 /* This isn't possible, but as kobject_init_and_add is marked
5522 * __must_check, we must do something with the result
5524 pr_debug("md: cannot register %s/md - name in use\n",
5528 if (mddev
->kobj
.sd
&&
5529 sysfs_create_group(&mddev
->kobj
, &md_bitmap_group
))
5530 pr_debug("pointless warning\n");
5531 mutex_unlock(&mddev
->open_mutex
);
5533 mutex_unlock(&disks_mutex
);
5534 if (!error
&& mddev
->kobj
.sd
) {
5535 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
5536 mddev
->sysfs_state
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "array_state");
5542 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
5545 md_alloc(dev
, NULL
);
5549 static int add_named_array(const char *val
, const struct kernel_param
*kp
)
5552 * val must be "md_*" or "mdNNN".
5553 * For "md_*" we allocate an array with a large free minor number, and
5554 * set the name to val. val must not already be an active name.
5555 * For "mdNNN" we allocate an array with the minor number NNN
5556 * which must not already be in use.
5558 int len
= strlen(val
);
5559 char buf
[DISK_NAME_LEN
];
5560 unsigned long devnum
;
5562 while (len
&& val
[len
-1] == '\n')
5564 if (len
>= DISK_NAME_LEN
)
5566 strlcpy(buf
, val
, len
+1);
5567 if (strncmp(buf
, "md_", 3) == 0)
5568 return md_alloc(0, buf
);
5569 if (strncmp(buf
, "md", 2) == 0 &&
5571 kstrtoul(buf
+2, 10, &devnum
) == 0 &&
5572 devnum
<= MINORMASK
)
5573 return md_alloc(MKDEV(MD_MAJOR
, devnum
), NULL
);
5578 static void md_safemode_timeout(struct timer_list
*t
)
5580 struct mddev
*mddev
= from_timer(mddev
, t
, safemode_timer
);
5582 mddev
->safemode
= 1;
5583 if (mddev
->external
)
5584 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5586 md_wakeup_thread(mddev
->thread
);
5589 static int start_dirty_degraded
;
5591 int md_run(struct mddev
*mddev
)
5594 struct md_rdev
*rdev
;
5595 struct md_personality
*pers
;
5597 if (list_empty(&mddev
->disks
))
5598 /* cannot run an array with no devices.. */
5603 /* Cannot run until previous stop completes properly */
5604 if (mddev
->sysfs_active
)
5608 * Analyze all RAID superblock(s)
5610 if (!mddev
->raid_disks
) {
5611 if (!mddev
->persistent
)
5613 err
= analyze_sbs(mddev
);
5618 if (mddev
->level
!= LEVEL_NONE
)
5619 request_module("md-level-%d", mddev
->level
);
5620 else if (mddev
->clevel
[0])
5621 request_module("md-%s", mddev
->clevel
);
5624 * Drop all container device buffers, from now on
5625 * the only valid external interface is through the md
5628 mddev
->has_superblocks
= false;
5629 rdev_for_each(rdev
, mddev
) {
5630 if (test_bit(Faulty
, &rdev
->flags
))
5632 sync_blockdev(rdev
->bdev
);
5633 invalidate_bdev(rdev
->bdev
);
5634 if (mddev
->ro
!= 1 &&
5635 (bdev_read_only(rdev
->bdev
) ||
5636 bdev_read_only(rdev
->meta_bdev
))) {
5639 set_disk_ro(mddev
->gendisk
, 1);
5643 mddev
->has_superblocks
= true;
5645 /* perform some consistency tests on the device.
5646 * We don't want the data to overlap the metadata,
5647 * Internal Bitmap issues have been handled elsewhere.
5649 if (rdev
->meta_bdev
) {
5650 /* Nothing to check */;
5651 } else if (rdev
->data_offset
< rdev
->sb_start
) {
5652 if (mddev
->dev_sectors
&&
5653 rdev
->data_offset
+ mddev
->dev_sectors
5655 pr_warn("md: %s: data overlaps metadata\n",
5660 if (rdev
->sb_start
+ rdev
->sb_size
/512
5661 > rdev
->data_offset
) {
5662 pr_warn("md: %s: metadata overlaps data\n",
5667 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
5670 if (!bioset_initialized(&mddev
->bio_set
)) {
5671 err
= bioset_init(&mddev
->bio_set
, BIO_POOL_SIZE
, 0, BIOSET_NEED_BVECS
);
5675 if (!bioset_initialized(&mddev
->sync_set
)) {
5676 err
= bioset_init(&mddev
->sync_set
, BIO_POOL_SIZE
, 0, BIOSET_NEED_BVECS
);
5681 spin_lock(&pers_lock
);
5682 pers
= find_pers(mddev
->level
, mddev
->clevel
);
5683 if (!pers
|| !try_module_get(pers
->owner
)) {
5684 spin_unlock(&pers_lock
);
5685 if (mddev
->level
!= LEVEL_NONE
)
5686 pr_warn("md: personality for level %d is not loaded!\n",
5689 pr_warn("md: personality for level %s is not loaded!\n",
5694 spin_unlock(&pers_lock
);
5695 if (mddev
->level
!= pers
->level
) {
5696 mddev
->level
= pers
->level
;
5697 mddev
->new_level
= pers
->level
;
5699 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
5701 if (mddev
->reshape_position
!= MaxSector
&&
5702 pers
->start_reshape
== NULL
) {
5703 /* This personality cannot handle reshaping... */
5704 module_put(pers
->owner
);
5709 if (pers
->sync_request
) {
5710 /* Warn if this is a potentially silly
5713 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
5714 struct md_rdev
*rdev2
;
5717 rdev_for_each(rdev
, mddev
)
5718 rdev_for_each(rdev2
, mddev
) {
5720 rdev
->bdev
->bd_contains
==
5721 rdev2
->bdev
->bd_contains
) {
5722 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5724 bdevname(rdev
->bdev
,b
),
5725 bdevname(rdev2
->bdev
,b2
));
5731 pr_warn("True protection against single-disk failure might be compromised.\n");
5734 mddev
->recovery
= 0;
5735 /* may be over-ridden by personality */
5736 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
5738 mddev
->ok_start_degraded
= start_dirty_degraded
;
5740 if (start_readonly
&& mddev
->ro
== 0)
5741 mddev
->ro
= 2; /* read-only, but switch on first write */
5743 err
= pers
->run(mddev
);
5745 pr_warn("md: pers->run() failed ...\n");
5746 else if (pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
5747 WARN_ONCE(!mddev
->external_size
,
5748 "%s: default size too small, but 'external_size' not in effect?\n",
5750 pr_warn("md: invalid array_size %llu > default size %llu\n",
5751 (unsigned long long)mddev
->array_sectors
/ 2,
5752 (unsigned long long)pers
->size(mddev
, 0, 0) / 2);
5755 if (err
== 0 && pers
->sync_request
&&
5756 (mddev
->bitmap_info
.file
|| mddev
->bitmap_info
.offset
)) {
5757 struct bitmap
*bitmap
;
5759 bitmap
= md_bitmap_create(mddev
, -1);
5760 if (IS_ERR(bitmap
)) {
5761 err
= PTR_ERR(bitmap
);
5762 pr_warn("%s: failed to create bitmap (%d)\n",
5763 mdname(mddev
), err
);
5765 mddev
->bitmap
= bitmap
;
5771 if (mddev
->bitmap_info
.max_write_behind
> 0) {
5772 bool creat_pool
= false;
5774 rdev_for_each(rdev
, mddev
) {
5775 if (test_bit(WriteMostly
, &rdev
->flags
) &&
5779 if (creat_pool
&& mddev
->wb_info_pool
== NULL
) {
5780 mddev
->wb_info_pool
=
5781 mempool_create_kmalloc_pool(NR_WB_INFOS
,
5782 sizeof(struct wb_info
));
5783 if (!mddev
->wb_info_pool
) {
5793 rdev_for_each(rdev
, mddev
) {
5794 if (rdev
->raid_disk
>= 0 &&
5795 !blk_queue_nonrot(bdev_get_queue(rdev
->bdev
))) {
5800 if (mddev
->degraded
)
5803 blk_queue_flag_set(QUEUE_FLAG_NONROT
, mddev
->queue
);
5805 blk_queue_flag_clear(QUEUE_FLAG_NONROT
, mddev
->queue
);
5806 mddev
->queue
->backing_dev_info
->congested_data
= mddev
;
5807 mddev
->queue
->backing_dev_info
->congested_fn
= md_congested
;
5809 if (pers
->sync_request
) {
5810 if (mddev
->kobj
.sd
&&
5811 sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
5812 pr_warn("md: cannot register extra attributes for %s\n",
5814 mddev
->sysfs_action
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "sync_action");
5815 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
5818 atomic_set(&mddev
->max_corr_read_errors
,
5819 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS
);
5820 mddev
->safemode
= 0;
5821 if (mddev_is_clustered(mddev
))
5822 mddev
->safemode_delay
= 0;
5824 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
5827 spin_lock(&mddev
->lock
);
5829 spin_unlock(&mddev
->lock
);
5830 rdev_for_each(rdev
, mddev
)
5831 if (rdev
->raid_disk
>= 0)
5832 sysfs_link_rdev(mddev
, rdev
); /* failure here is OK */
5834 if (mddev
->degraded
&& !mddev
->ro
)
5835 /* This ensures that recovering status is reported immediately
5836 * via sysfs - until a lack of spares is confirmed.
5838 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5839 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5841 if (mddev
->sb_flags
)
5842 md_update_sb(mddev
, 0);
5844 md_new_event(mddev
);
5848 mddev_detach(mddev
);
5850 pers
->free(mddev
, mddev
->private);
5851 mddev
->private = NULL
;
5852 module_put(pers
->owner
);
5853 md_bitmap_destroy(mddev
);
5855 bioset_exit(&mddev
->bio_set
);
5856 bioset_exit(&mddev
->sync_set
);
5859 EXPORT_SYMBOL_GPL(md_run
);
5861 static int do_md_run(struct mddev
*mddev
)
5865 set_bit(MD_NOT_READY
, &mddev
->flags
);
5866 err
= md_run(mddev
);
5869 err
= md_bitmap_load(mddev
);
5871 md_bitmap_destroy(mddev
);
5875 if (mddev_is_clustered(mddev
))
5876 md_allow_write(mddev
);
5878 /* run start up tasks that require md_thread */
5881 md_wakeup_thread(mddev
->thread
);
5882 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
5884 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
5885 revalidate_disk(mddev
->gendisk
);
5886 clear_bit(MD_NOT_READY
, &mddev
->flags
);
5888 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
5889 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5890 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
5891 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
5893 clear_bit(MD_NOT_READY
, &mddev
->flags
);
5897 int md_start(struct mddev
*mddev
)
5901 if (mddev
->pers
->start
) {
5902 set_bit(MD_RECOVERY_WAIT
, &mddev
->recovery
);
5903 md_wakeup_thread(mddev
->thread
);
5904 ret
= mddev
->pers
->start(mddev
);
5905 clear_bit(MD_RECOVERY_WAIT
, &mddev
->recovery
);
5906 md_wakeup_thread(mddev
->sync_thread
);
5910 EXPORT_SYMBOL_GPL(md_start
);
5912 static int restart_array(struct mddev
*mddev
)
5914 struct gendisk
*disk
= mddev
->gendisk
;
5915 struct md_rdev
*rdev
;
5916 bool has_journal
= false;
5917 bool has_readonly
= false;
5919 /* Complain if it has no devices */
5920 if (list_empty(&mddev
->disks
))
5928 rdev_for_each_rcu(rdev
, mddev
) {
5929 if (test_bit(Journal
, &rdev
->flags
) &&
5930 !test_bit(Faulty
, &rdev
->flags
))
5932 if (bdev_read_only(rdev
->bdev
))
5933 has_readonly
= true;
5936 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
) && !has_journal
)
5937 /* Don't restart rw with journal missing/faulty */
5942 mddev
->safemode
= 0;
5944 set_disk_ro(disk
, 0);
5945 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev
));
5946 /* Kick recovery or resync if necessary */
5947 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5948 md_wakeup_thread(mddev
->thread
);
5949 md_wakeup_thread(mddev
->sync_thread
);
5950 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5954 static void md_clean(struct mddev
*mddev
)
5956 mddev
->array_sectors
= 0;
5957 mddev
->external_size
= 0;
5958 mddev
->dev_sectors
= 0;
5959 mddev
->raid_disks
= 0;
5960 mddev
->recovery_cp
= 0;
5961 mddev
->resync_min
= 0;
5962 mddev
->resync_max
= MaxSector
;
5963 mddev
->reshape_position
= MaxSector
;
5964 mddev
->external
= 0;
5965 mddev
->persistent
= 0;
5966 mddev
->level
= LEVEL_NONE
;
5967 mddev
->clevel
[0] = 0;
5969 mddev
->sb_flags
= 0;
5971 mddev
->metadata_type
[0] = 0;
5972 mddev
->chunk_sectors
= 0;
5973 mddev
->ctime
= mddev
->utime
= 0;
5975 mddev
->max_disks
= 0;
5977 mddev
->can_decrease_events
= 0;
5978 mddev
->delta_disks
= 0;
5979 mddev
->reshape_backwards
= 0;
5980 mddev
->new_level
= LEVEL_NONE
;
5981 mddev
->new_layout
= 0;
5982 mddev
->new_chunk_sectors
= 0;
5983 mddev
->curr_resync
= 0;
5984 atomic64_set(&mddev
->resync_mismatches
, 0);
5985 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
5986 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
5987 mddev
->recovery
= 0;
5990 mddev
->degraded
= 0;
5991 mddev
->safemode
= 0;
5992 mddev
->private = NULL
;
5993 mddev
->cluster_info
= NULL
;
5994 mddev
->bitmap_info
.offset
= 0;
5995 mddev
->bitmap_info
.default_offset
= 0;
5996 mddev
->bitmap_info
.default_space
= 0;
5997 mddev
->bitmap_info
.chunksize
= 0;
5998 mddev
->bitmap_info
.daemon_sleep
= 0;
5999 mddev
->bitmap_info
.max_write_behind
= 0;
6000 mddev
->bitmap_info
.nodes
= 0;
6003 static void __md_stop_writes(struct mddev
*mddev
)
6005 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
6006 flush_workqueue(md_misc_wq
);
6007 if (mddev
->sync_thread
) {
6008 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6009 md_reap_sync_thread(mddev
);
6012 del_timer_sync(&mddev
->safemode_timer
);
6014 if (mddev
->pers
&& mddev
->pers
->quiesce
) {
6015 mddev
->pers
->quiesce(mddev
, 1);
6016 mddev
->pers
->quiesce(mddev
, 0);
6018 md_bitmap_flush(mddev
);
6020 if (mddev
->ro
== 0 &&
6021 ((!mddev
->in_sync
&& !mddev_is_clustered(mddev
)) ||
6023 /* mark array as shutdown cleanly */
6024 if (!mddev_is_clustered(mddev
))
6026 md_update_sb(mddev
, 1);
6028 mempool_destroy(mddev
->wb_info_pool
);
6029 mddev
->wb_info_pool
= NULL
;
6032 void md_stop_writes(struct mddev
*mddev
)
6034 mddev_lock_nointr(mddev
);
6035 __md_stop_writes(mddev
);
6036 mddev_unlock(mddev
);
6038 EXPORT_SYMBOL_GPL(md_stop_writes
);
6040 static void mddev_detach(struct mddev
*mddev
)
6042 md_bitmap_wait_behind_writes(mddev
);
6043 if (mddev
->pers
&& mddev
->pers
->quiesce
) {
6044 mddev
->pers
->quiesce(mddev
, 1);
6045 mddev
->pers
->quiesce(mddev
, 0);
6047 md_unregister_thread(&mddev
->thread
);
6049 blk_sync_queue(mddev
->queue
); /* the unplug fn references 'conf'*/
6052 static void __md_stop(struct mddev
*mddev
)
6054 struct md_personality
*pers
= mddev
->pers
;
6055 md_bitmap_destroy(mddev
);
6056 mddev_detach(mddev
);
6057 /* Ensure ->event_work is done */
6058 flush_workqueue(md_misc_wq
);
6059 spin_lock(&mddev
->lock
);
6061 spin_unlock(&mddev
->lock
);
6062 pers
->free(mddev
, mddev
->private);
6063 mddev
->private = NULL
;
6064 if (pers
->sync_request
&& mddev
->to_remove
== NULL
)
6065 mddev
->to_remove
= &md_redundancy_group
;
6066 module_put(pers
->owner
);
6067 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
6070 void md_stop(struct mddev
*mddev
)
6072 /* stop the array and free an attached data structures.
6073 * This is called from dm-raid
6076 bioset_exit(&mddev
->bio_set
);
6077 bioset_exit(&mddev
->sync_set
);
6080 EXPORT_SYMBOL_GPL(md_stop
);
6082 static int md_set_readonly(struct mddev
*mddev
, struct block_device
*bdev
)
6087 if (!test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
)) {
6089 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
6090 md_wakeup_thread(mddev
->thread
);
6092 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
6093 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6094 if (mddev
->sync_thread
)
6095 /* Thread might be blocked waiting for metadata update
6096 * which will now never happen */
6097 wake_up_process(mddev
->sync_thread
->tsk
);
6099 if (mddev
->external
&& test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
))
6101 mddev_unlock(mddev
);
6102 wait_event(resync_wait
, !test_bit(MD_RECOVERY_RUNNING
,
6104 wait_event(mddev
->sb_wait
,
6105 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
6106 mddev_lock_nointr(mddev
);
6108 mutex_lock(&mddev
->open_mutex
);
6109 if ((mddev
->pers
&& atomic_read(&mddev
->openers
) > !!bdev
) ||
6110 mddev
->sync_thread
||
6111 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
)) {
6112 pr_warn("md: %s still in use.\n",mdname(mddev
));
6114 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
6115 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6116 md_wakeup_thread(mddev
->thread
);
6122 __md_stop_writes(mddev
);
6128 set_disk_ro(mddev
->gendisk
, 1);
6129 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
6130 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6131 md_wakeup_thread(mddev
->thread
);
6132 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6136 mutex_unlock(&mddev
->open_mutex
);
6141 * 0 - completely stop and dis-assemble array
6142 * 2 - stop but do not disassemble array
6144 static int do_md_stop(struct mddev
*mddev
, int mode
,
6145 struct block_device
*bdev
)
6147 struct gendisk
*disk
= mddev
->gendisk
;
6148 struct md_rdev
*rdev
;
6151 if (!test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
)) {
6153 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
6154 md_wakeup_thread(mddev
->thread
);
6156 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
6157 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6158 if (mddev
->sync_thread
)
6159 /* Thread might be blocked waiting for metadata update
6160 * which will now never happen */
6161 wake_up_process(mddev
->sync_thread
->tsk
);
6163 mddev_unlock(mddev
);
6164 wait_event(resync_wait
, (mddev
->sync_thread
== NULL
&&
6165 !test_bit(MD_RECOVERY_RUNNING
,
6166 &mddev
->recovery
)));
6167 mddev_lock_nointr(mddev
);
6169 mutex_lock(&mddev
->open_mutex
);
6170 if ((mddev
->pers
&& atomic_read(&mddev
->openers
) > !!bdev
) ||
6171 mddev
->sysfs_active
||
6172 mddev
->sync_thread
||
6173 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
)) {
6174 pr_warn("md: %s still in use.\n",mdname(mddev
));
6175 mutex_unlock(&mddev
->open_mutex
);
6177 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
6178 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6179 md_wakeup_thread(mddev
->thread
);
6185 set_disk_ro(disk
, 0);
6187 __md_stop_writes(mddev
);
6189 mddev
->queue
->backing_dev_info
->congested_fn
= NULL
;
6191 /* tell userspace to handle 'inactive' */
6192 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6194 rdev_for_each(rdev
, mddev
)
6195 if (rdev
->raid_disk
>= 0)
6196 sysfs_unlink_rdev(mddev
, rdev
);
6198 set_capacity(disk
, 0);
6199 mutex_unlock(&mddev
->open_mutex
);
6201 revalidate_disk(disk
);
6206 mutex_unlock(&mddev
->open_mutex
);
6208 * Free resources if final stop
6211 pr_info("md: %s stopped.\n", mdname(mddev
));
6213 if (mddev
->bitmap_info
.file
) {
6214 struct file
*f
= mddev
->bitmap_info
.file
;
6215 spin_lock(&mddev
->lock
);
6216 mddev
->bitmap_info
.file
= NULL
;
6217 spin_unlock(&mddev
->lock
);
6220 mddev
->bitmap_info
.offset
= 0;
6222 export_array(mddev
);
6225 if (mddev
->hold_active
== UNTIL_STOP
)
6226 mddev
->hold_active
= 0;
6228 md_new_event(mddev
);
6229 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6234 static void autorun_array(struct mddev
*mddev
)
6236 struct md_rdev
*rdev
;
6239 if (list_empty(&mddev
->disks
))
6242 pr_info("md: running: ");
6244 rdev_for_each(rdev
, mddev
) {
6245 char b
[BDEVNAME_SIZE
];
6246 pr_cont("<%s>", bdevname(rdev
->bdev
,b
));
6250 err
= do_md_run(mddev
);
6252 pr_warn("md: do_md_run() returned %d\n", err
);
6253 do_md_stop(mddev
, 0, NULL
);
6258 * lets try to run arrays based on all disks that have arrived
6259 * until now. (those are in pending_raid_disks)
6261 * the method: pick the first pending disk, collect all disks with
6262 * the same UUID, remove all from the pending list and put them into
6263 * the 'same_array' list. Then order this list based on superblock
6264 * update time (freshest comes first), kick out 'old' disks and
6265 * compare superblocks. If everything's fine then run it.
6267 * If "unit" is allocated, then bump its reference count
6269 static void autorun_devices(int part
)
6271 struct md_rdev
*rdev0
, *rdev
, *tmp
;
6272 struct mddev
*mddev
;
6273 char b
[BDEVNAME_SIZE
];
6275 pr_info("md: autorun ...\n");
6276 while (!list_empty(&pending_raid_disks
)) {
6279 LIST_HEAD(candidates
);
6280 rdev0
= list_entry(pending_raid_disks
.next
,
6281 struct md_rdev
, same_set
);
6283 pr_debug("md: considering %s ...\n", bdevname(rdev0
->bdev
,b
));
6284 INIT_LIST_HEAD(&candidates
);
6285 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
6286 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
6287 pr_debug("md: adding %s ...\n",
6288 bdevname(rdev
->bdev
,b
));
6289 list_move(&rdev
->same_set
, &candidates
);
6292 * now we have a set of devices, with all of them having
6293 * mostly sane superblocks. It's time to allocate the
6297 dev
= MKDEV(mdp_major
,
6298 rdev0
->preferred_minor
<< MdpMinorShift
);
6299 unit
= MINOR(dev
) >> MdpMinorShift
;
6301 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
6304 if (rdev0
->preferred_minor
!= unit
) {
6305 pr_warn("md: unit number in %s is bad: %d\n",
6306 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
6310 md_probe(dev
, NULL
, NULL
);
6311 mddev
= mddev_find(dev
);
6312 if (!mddev
|| !mddev
->gendisk
) {
6317 if (mddev_lock(mddev
))
6318 pr_warn("md: %s locked, cannot run\n", mdname(mddev
));
6319 else if (mddev
->raid_disks
|| mddev
->major_version
6320 || !list_empty(&mddev
->disks
)) {
6321 pr_warn("md: %s already running, cannot run %s\n",
6322 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
6323 mddev_unlock(mddev
);
6325 pr_debug("md: created %s\n", mdname(mddev
));
6326 mddev
->persistent
= 1;
6327 rdev_for_each_list(rdev
, tmp
, &candidates
) {
6328 list_del_init(&rdev
->same_set
);
6329 if (bind_rdev_to_array(rdev
, mddev
))
6332 autorun_array(mddev
);
6333 mddev_unlock(mddev
);
6335 /* on success, candidates will be empty, on error
6338 rdev_for_each_list(rdev
, tmp
, &candidates
) {
6339 list_del_init(&rdev
->same_set
);
6344 pr_info("md: ... autorun DONE.\n");
6346 #endif /* !MODULE */
6348 static int get_version(void __user
*arg
)
6352 ver
.major
= MD_MAJOR_VERSION
;
6353 ver
.minor
= MD_MINOR_VERSION
;
6354 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
6356 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
6362 static int get_array_info(struct mddev
*mddev
, void __user
*arg
)
6364 mdu_array_info_t info
;
6365 int nr
,working
,insync
,failed
,spare
;
6366 struct md_rdev
*rdev
;
6368 nr
= working
= insync
= failed
= spare
= 0;
6370 rdev_for_each_rcu(rdev
, mddev
) {
6372 if (test_bit(Faulty
, &rdev
->flags
))
6376 if (test_bit(In_sync
, &rdev
->flags
))
6378 else if (test_bit(Journal
, &rdev
->flags
))
6379 /* TODO: add journal count to md_u.h */
6387 info
.major_version
= mddev
->major_version
;
6388 info
.minor_version
= mddev
->minor_version
;
6389 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
6390 info
.ctime
= clamp_t(time64_t
, mddev
->ctime
, 0, U32_MAX
);
6391 info
.level
= mddev
->level
;
6392 info
.size
= mddev
->dev_sectors
/ 2;
6393 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
6396 info
.raid_disks
= mddev
->raid_disks
;
6397 info
.md_minor
= mddev
->md_minor
;
6398 info
.not_persistent
= !mddev
->persistent
;
6400 info
.utime
= clamp_t(time64_t
, mddev
->utime
, 0, U32_MAX
);
6403 info
.state
= (1<<MD_SB_CLEAN
);
6404 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
6405 info
.state
|= (1<<MD_SB_BITMAP_PRESENT
);
6406 if (mddev_is_clustered(mddev
))
6407 info
.state
|= (1<<MD_SB_CLUSTERED
);
6408 info
.active_disks
= insync
;
6409 info
.working_disks
= working
;
6410 info
.failed_disks
= failed
;
6411 info
.spare_disks
= spare
;
6413 info
.layout
= mddev
->layout
;
6414 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
6416 if (copy_to_user(arg
, &info
, sizeof(info
)))
6422 static int get_bitmap_file(struct mddev
*mddev
, void __user
* arg
)
6424 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
6428 file
= kzalloc(sizeof(*file
), GFP_NOIO
);
6433 spin_lock(&mddev
->lock
);
6434 /* bitmap enabled */
6435 if (mddev
->bitmap_info
.file
) {
6436 ptr
= file_path(mddev
->bitmap_info
.file
, file
->pathname
,
6437 sizeof(file
->pathname
));
6441 memmove(file
->pathname
, ptr
,
6442 sizeof(file
->pathname
)-(ptr
-file
->pathname
));
6444 spin_unlock(&mddev
->lock
);
6447 copy_to_user(arg
, file
, sizeof(*file
)))
6454 static int get_disk_info(struct mddev
*mddev
, void __user
* arg
)
6456 mdu_disk_info_t info
;
6457 struct md_rdev
*rdev
;
6459 if (copy_from_user(&info
, arg
, sizeof(info
)))
6463 rdev
= md_find_rdev_nr_rcu(mddev
, info
.number
);
6465 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
6466 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
6467 info
.raid_disk
= rdev
->raid_disk
;
6469 if (test_bit(Faulty
, &rdev
->flags
))
6470 info
.state
|= (1<<MD_DISK_FAULTY
);
6471 else if (test_bit(In_sync
, &rdev
->flags
)) {
6472 info
.state
|= (1<<MD_DISK_ACTIVE
);
6473 info
.state
|= (1<<MD_DISK_SYNC
);
6475 if (test_bit(Journal
, &rdev
->flags
))
6476 info
.state
|= (1<<MD_DISK_JOURNAL
);
6477 if (test_bit(WriteMostly
, &rdev
->flags
))
6478 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
6479 if (test_bit(FailFast
, &rdev
->flags
))
6480 info
.state
|= (1<<MD_DISK_FAILFAST
);
6482 info
.major
= info
.minor
= 0;
6483 info
.raid_disk
= -1;
6484 info
.state
= (1<<MD_DISK_REMOVED
);
6488 if (copy_to_user(arg
, &info
, sizeof(info
)))
6494 static int add_new_disk(struct mddev
*mddev
, mdu_disk_info_t
*info
)
6496 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
6497 struct md_rdev
*rdev
;
6498 dev_t dev
= MKDEV(info
->major
,info
->minor
);
6500 if (mddev_is_clustered(mddev
) &&
6501 !(info
->state
& ((1 << MD_DISK_CLUSTER_ADD
) | (1 << MD_DISK_CANDIDATE
)))) {
6502 pr_warn("%s: Cannot add to clustered mddev.\n",
6507 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
6510 if (!mddev
->raid_disks
) {
6512 /* expecting a device which has a superblock */
6513 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
6515 pr_warn("md: md_import_device returned %ld\n",
6517 return PTR_ERR(rdev
);
6519 if (!list_empty(&mddev
->disks
)) {
6520 struct md_rdev
*rdev0
6521 = list_entry(mddev
->disks
.next
,
6522 struct md_rdev
, same_set
);
6523 err
= super_types
[mddev
->major_version
]
6524 .load_super(rdev
, rdev0
, mddev
->minor_version
);
6526 pr_warn("md: %s has different UUID to %s\n",
6527 bdevname(rdev
->bdev
,b
),
6528 bdevname(rdev0
->bdev
,b2
));
6533 err
= bind_rdev_to_array(rdev
, mddev
);
6540 * add_new_disk can be used once the array is assembled
6541 * to add "hot spares". They must already have a superblock
6546 if (!mddev
->pers
->hot_add_disk
) {
6547 pr_warn("%s: personality does not support diskops!\n",
6551 if (mddev
->persistent
)
6552 rdev
= md_import_device(dev
, mddev
->major_version
,
6553 mddev
->minor_version
);
6555 rdev
= md_import_device(dev
, -1, -1);
6557 pr_warn("md: md_import_device returned %ld\n",
6559 return PTR_ERR(rdev
);
6561 /* set saved_raid_disk if appropriate */
6562 if (!mddev
->persistent
) {
6563 if (info
->state
& (1<<MD_DISK_SYNC
) &&
6564 info
->raid_disk
< mddev
->raid_disks
) {
6565 rdev
->raid_disk
= info
->raid_disk
;
6566 set_bit(In_sync
, &rdev
->flags
);
6567 clear_bit(Bitmap_sync
, &rdev
->flags
);
6569 rdev
->raid_disk
= -1;
6570 rdev
->saved_raid_disk
= rdev
->raid_disk
;
6572 super_types
[mddev
->major_version
].
6573 validate_super(mddev
, rdev
);
6574 if ((info
->state
& (1<<MD_DISK_SYNC
)) &&
6575 rdev
->raid_disk
!= info
->raid_disk
) {
6576 /* This was a hot-add request, but events doesn't
6577 * match, so reject it.
6583 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
6584 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
6585 set_bit(WriteMostly
, &rdev
->flags
);
6587 clear_bit(WriteMostly
, &rdev
->flags
);
6588 if (info
->state
& (1<<MD_DISK_FAILFAST
))
6589 set_bit(FailFast
, &rdev
->flags
);
6591 clear_bit(FailFast
, &rdev
->flags
);
6593 if (info
->state
& (1<<MD_DISK_JOURNAL
)) {
6594 struct md_rdev
*rdev2
;
6595 bool has_journal
= false;
6597 /* make sure no existing journal disk */
6598 rdev_for_each(rdev2
, mddev
) {
6599 if (test_bit(Journal
, &rdev2
->flags
)) {
6604 if (has_journal
|| mddev
->bitmap
) {
6608 set_bit(Journal
, &rdev
->flags
);
6611 * check whether the device shows up in other nodes
6613 if (mddev_is_clustered(mddev
)) {
6614 if (info
->state
& (1 << MD_DISK_CANDIDATE
))
6615 set_bit(Candidate
, &rdev
->flags
);
6616 else if (info
->state
& (1 << MD_DISK_CLUSTER_ADD
)) {
6617 /* --add initiated by this node */
6618 err
= md_cluster_ops
->add_new_disk(mddev
, rdev
);
6626 rdev
->raid_disk
= -1;
6627 err
= bind_rdev_to_array(rdev
, mddev
);
6632 if (mddev_is_clustered(mddev
)) {
6633 if (info
->state
& (1 << MD_DISK_CANDIDATE
)) {
6635 err
= md_cluster_ops
->new_disk_ack(mddev
,
6638 md_kick_rdev_from_array(rdev
);
6642 md_cluster_ops
->add_new_disk_cancel(mddev
);
6644 err
= add_bound_rdev(rdev
);
6648 err
= add_bound_rdev(rdev
);
6653 /* otherwise, add_new_disk is only allowed
6654 * for major_version==0 superblocks
6656 if (mddev
->major_version
!= 0) {
6657 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev
));
6661 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
6663 rdev
= md_import_device(dev
, -1, 0);
6665 pr_warn("md: error, md_import_device() returned %ld\n",
6667 return PTR_ERR(rdev
);
6669 rdev
->desc_nr
= info
->number
;
6670 if (info
->raid_disk
< mddev
->raid_disks
)
6671 rdev
->raid_disk
= info
->raid_disk
;
6673 rdev
->raid_disk
= -1;
6675 if (rdev
->raid_disk
< mddev
->raid_disks
)
6676 if (info
->state
& (1<<MD_DISK_SYNC
))
6677 set_bit(In_sync
, &rdev
->flags
);
6679 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
6680 set_bit(WriteMostly
, &rdev
->flags
);
6681 if (info
->state
& (1<<MD_DISK_FAILFAST
))
6682 set_bit(FailFast
, &rdev
->flags
);
6684 if (!mddev
->persistent
) {
6685 pr_debug("md: nonpersistent superblock ...\n");
6686 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
6688 rdev
->sb_start
= calc_dev_sboffset(rdev
);
6689 rdev
->sectors
= rdev
->sb_start
;
6691 err
= bind_rdev_to_array(rdev
, mddev
);
6701 static int hot_remove_disk(struct mddev
*mddev
, dev_t dev
)
6703 char b
[BDEVNAME_SIZE
];
6704 struct md_rdev
*rdev
;
6709 rdev
= find_rdev(mddev
, dev
);
6713 if (rdev
->raid_disk
< 0)
6716 clear_bit(Blocked
, &rdev
->flags
);
6717 remove_and_add_spares(mddev
, rdev
);
6719 if (rdev
->raid_disk
>= 0)
6723 if (mddev_is_clustered(mddev
))
6724 md_cluster_ops
->remove_disk(mddev
, rdev
);
6726 md_kick_rdev_from_array(rdev
);
6727 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
6729 md_wakeup_thread(mddev
->thread
);
6731 md_update_sb(mddev
, 1);
6732 md_new_event(mddev
);
6736 pr_debug("md: cannot remove active disk %s from %s ...\n",
6737 bdevname(rdev
->bdev
,b
), mdname(mddev
));
6741 static int hot_add_disk(struct mddev
*mddev
, dev_t dev
)
6743 char b
[BDEVNAME_SIZE
];
6745 struct md_rdev
*rdev
;
6750 if (mddev
->major_version
!= 0) {
6751 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
6755 if (!mddev
->pers
->hot_add_disk
) {
6756 pr_warn("%s: personality does not support diskops!\n",
6761 rdev
= md_import_device(dev
, -1, 0);
6763 pr_warn("md: error, md_import_device() returned %ld\n",
6768 if (mddev
->persistent
)
6769 rdev
->sb_start
= calc_dev_sboffset(rdev
);
6771 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
6773 rdev
->sectors
= rdev
->sb_start
;
6775 if (test_bit(Faulty
, &rdev
->flags
)) {
6776 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
6777 bdevname(rdev
->bdev
,b
), mdname(mddev
));
6782 clear_bit(In_sync
, &rdev
->flags
);
6784 rdev
->saved_raid_disk
= -1;
6785 err
= bind_rdev_to_array(rdev
, mddev
);
6790 * The rest should better be atomic, we can have disk failures
6791 * noticed in interrupt contexts ...
6794 rdev
->raid_disk
= -1;
6796 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
6798 md_update_sb(mddev
, 1);
6800 * Kick recovery, maybe this spare has to be added to the
6801 * array immediately.
6803 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6804 md_wakeup_thread(mddev
->thread
);
6805 md_new_event(mddev
);
6813 static int set_bitmap_file(struct mddev
*mddev
, int fd
)
6818 if (!mddev
->pers
->quiesce
|| !mddev
->thread
)
6820 if (mddev
->recovery
|| mddev
->sync_thread
)
6822 /* we should be able to change the bitmap.. */
6826 struct inode
*inode
;
6829 if (mddev
->bitmap
|| mddev
->bitmap_info
.file
)
6830 return -EEXIST
; /* cannot add when bitmap is present */
6834 pr_warn("%s: error: failed to get bitmap file\n",
6839 inode
= f
->f_mapping
->host
;
6840 if (!S_ISREG(inode
->i_mode
)) {
6841 pr_warn("%s: error: bitmap file must be a regular file\n",
6844 } else if (!(f
->f_mode
& FMODE_WRITE
)) {
6845 pr_warn("%s: error: bitmap file must open for write\n",
6848 } else if (atomic_read(&inode
->i_writecount
) != 1) {
6849 pr_warn("%s: error: bitmap file is already in use\n",
6857 mddev
->bitmap_info
.file
= f
;
6858 mddev
->bitmap_info
.offset
= 0; /* file overrides offset */
6859 } else if (mddev
->bitmap
== NULL
)
6860 return -ENOENT
; /* cannot remove what isn't there */
6864 struct bitmap
*bitmap
;
6866 bitmap
= md_bitmap_create(mddev
, -1);
6867 mddev_suspend(mddev
);
6868 if (!IS_ERR(bitmap
)) {
6869 mddev
->bitmap
= bitmap
;
6870 err
= md_bitmap_load(mddev
);
6872 err
= PTR_ERR(bitmap
);
6874 md_bitmap_destroy(mddev
);
6877 mddev_resume(mddev
);
6878 } else if (fd
< 0) {
6879 mddev_suspend(mddev
);
6880 md_bitmap_destroy(mddev
);
6881 mddev_resume(mddev
);
6885 struct file
*f
= mddev
->bitmap_info
.file
;
6887 spin_lock(&mddev
->lock
);
6888 mddev
->bitmap_info
.file
= NULL
;
6889 spin_unlock(&mddev
->lock
);
6898 * set_array_info is used two different ways
6899 * The original usage is when creating a new array.
6900 * In this usage, raid_disks is > 0 and it together with
6901 * level, size, not_persistent,layout,chunksize determine the
6902 * shape of the array.
6903 * This will always create an array with a type-0.90.0 superblock.
6904 * The newer usage is when assembling an array.
6905 * In this case raid_disks will be 0, and the major_version field is
6906 * use to determine which style super-blocks are to be found on the devices.
6907 * The minor and patch _version numbers are also kept incase the
6908 * super_block handler wishes to interpret them.
6910 static int set_array_info(struct mddev
*mddev
, mdu_array_info_t
*info
)
6913 if (info
->raid_disks
== 0) {
6914 /* just setting version number for superblock loading */
6915 if (info
->major_version
< 0 ||
6916 info
->major_version
>= ARRAY_SIZE(super_types
) ||
6917 super_types
[info
->major_version
].name
== NULL
) {
6918 /* maybe try to auto-load a module? */
6919 pr_warn("md: superblock version %d not known\n",
6920 info
->major_version
);
6923 mddev
->major_version
= info
->major_version
;
6924 mddev
->minor_version
= info
->minor_version
;
6925 mddev
->patch_version
= info
->patch_version
;
6926 mddev
->persistent
= !info
->not_persistent
;
6927 /* ensure mddev_put doesn't delete this now that there
6928 * is some minimal configuration.
6930 mddev
->ctime
= ktime_get_real_seconds();
6933 mddev
->major_version
= MD_MAJOR_VERSION
;
6934 mddev
->minor_version
= MD_MINOR_VERSION
;
6935 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
6936 mddev
->ctime
= ktime_get_real_seconds();
6938 mddev
->level
= info
->level
;
6939 mddev
->clevel
[0] = 0;
6940 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
6941 mddev
->raid_disks
= info
->raid_disks
;
6942 /* don't set md_minor, it is determined by which /dev/md* was
6945 if (info
->state
& (1<<MD_SB_CLEAN
))
6946 mddev
->recovery_cp
= MaxSector
;
6948 mddev
->recovery_cp
= 0;
6949 mddev
->persistent
= ! info
->not_persistent
;
6950 mddev
->external
= 0;
6952 mddev
->layout
= info
->layout
;
6953 if (mddev
->level
== 0)
6954 /* Cannot trust RAID0 layout info here */
6956 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
6958 if (mddev
->persistent
) {
6959 mddev
->max_disks
= MD_SB_DISKS
;
6961 mddev
->sb_flags
= 0;
6963 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
6965 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
6966 mddev
->bitmap_info
.default_space
= 64*2 - (MD_SB_BYTES
>> 9);
6967 mddev
->bitmap_info
.offset
= 0;
6969 mddev
->reshape_position
= MaxSector
;
6972 * Generate a 128 bit UUID
6974 get_random_bytes(mddev
->uuid
, 16);
6976 mddev
->new_level
= mddev
->level
;
6977 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
6978 mddev
->new_layout
= mddev
->layout
;
6979 mddev
->delta_disks
= 0;
6980 mddev
->reshape_backwards
= 0;
6985 void md_set_array_sectors(struct mddev
*mddev
, sector_t array_sectors
)
6987 lockdep_assert_held(&mddev
->reconfig_mutex
);
6989 if (mddev
->external_size
)
6992 mddev
->array_sectors
= array_sectors
;
6994 EXPORT_SYMBOL(md_set_array_sectors
);
6996 static int update_size(struct mddev
*mddev
, sector_t num_sectors
)
6998 struct md_rdev
*rdev
;
7000 int fit
= (num_sectors
== 0);
7001 sector_t old_dev_sectors
= mddev
->dev_sectors
;
7003 if (mddev
->pers
->resize
== NULL
)
7005 /* The "num_sectors" is the number of sectors of each device that
7006 * is used. This can only make sense for arrays with redundancy.
7007 * linear and raid0 always use whatever space is available. We can only
7008 * consider changing this number if no resync or reconstruction is
7009 * happening, and if the new size is acceptable. It must fit before the
7010 * sb_start or, if that is <data_offset, it must fit before the size
7011 * of each device. If num_sectors is zero, we find the largest size
7014 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
7020 rdev_for_each(rdev
, mddev
) {
7021 sector_t avail
= rdev
->sectors
;
7023 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
7024 num_sectors
= avail
;
7025 if (avail
< num_sectors
)
7028 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
7030 if (mddev_is_clustered(mddev
))
7031 md_cluster_ops
->update_size(mddev
, old_dev_sectors
);
7032 else if (mddev
->queue
) {
7033 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
7034 revalidate_disk(mddev
->gendisk
);
7040 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
)
7043 struct md_rdev
*rdev
;
7044 /* change the number of raid disks */
7045 if (mddev
->pers
->check_reshape
== NULL
)
7049 if (raid_disks
<= 0 ||
7050 (mddev
->max_disks
&& raid_disks
>= mddev
->max_disks
))
7052 if (mddev
->sync_thread
||
7053 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
7054 mddev
->reshape_position
!= MaxSector
)
7057 rdev_for_each(rdev
, mddev
) {
7058 if (mddev
->raid_disks
< raid_disks
&&
7059 rdev
->data_offset
< rdev
->new_data_offset
)
7061 if (mddev
->raid_disks
> raid_disks
&&
7062 rdev
->data_offset
> rdev
->new_data_offset
)
7066 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
7067 if (mddev
->delta_disks
< 0)
7068 mddev
->reshape_backwards
= 1;
7069 else if (mddev
->delta_disks
> 0)
7070 mddev
->reshape_backwards
= 0;
7072 rv
= mddev
->pers
->check_reshape(mddev
);
7074 mddev
->delta_disks
= 0;
7075 mddev
->reshape_backwards
= 0;
7081 * update_array_info is used to change the configuration of an
7083 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
7084 * fields in the info are checked against the array.
7085 * Any differences that cannot be handled will cause an error.
7086 * Normally, only one change can be managed at a time.
7088 static int update_array_info(struct mddev
*mddev
, mdu_array_info_t
*info
)
7094 /* calculate expected state,ignoring low bits */
7095 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
7096 state
|= (1 << MD_SB_BITMAP_PRESENT
);
7098 if (mddev
->major_version
!= info
->major_version
||
7099 mddev
->minor_version
!= info
->minor_version
||
7100 /* mddev->patch_version != info->patch_version || */
7101 mddev
->ctime
!= info
->ctime
||
7102 mddev
->level
!= info
->level
||
7103 /* mddev->layout != info->layout || */
7104 mddev
->persistent
!= !info
->not_persistent
||
7105 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
7106 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
7107 ((state
^info
->state
) & 0xfffffe00)
7110 /* Check there is only one change */
7111 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
7113 if (mddev
->raid_disks
!= info
->raid_disks
)
7115 if (mddev
->layout
!= info
->layout
)
7117 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
7124 if (mddev
->layout
!= info
->layout
) {
7126 * we don't need to do anything at the md level, the
7127 * personality will take care of it all.
7129 if (mddev
->pers
->check_reshape
== NULL
)
7132 mddev
->new_layout
= info
->layout
;
7133 rv
= mddev
->pers
->check_reshape(mddev
);
7135 mddev
->new_layout
= mddev
->layout
;
7139 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
7140 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
7142 if (mddev
->raid_disks
!= info
->raid_disks
)
7143 rv
= update_raid_disks(mddev
, info
->raid_disks
);
7145 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
7146 if (mddev
->pers
->quiesce
== NULL
|| mddev
->thread
== NULL
) {
7150 if (mddev
->recovery
|| mddev
->sync_thread
) {
7154 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
7155 struct bitmap
*bitmap
;
7156 /* add the bitmap */
7157 if (mddev
->bitmap
) {
7161 if (mddev
->bitmap_info
.default_offset
== 0) {
7165 mddev
->bitmap_info
.offset
=
7166 mddev
->bitmap_info
.default_offset
;
7167 mddev
->bitmap_info
.space
=
7168 mddev
->bitmap_info
.default_space
;
7169 bitmap
= md_bitmap_create(mddev
, -1);
7170 mddev_suspend(mddev
);
7171 if (!IS_ERR(bitmap
)) {
7172 mddev
->bitmap
= bitmap
;
7173 rv
= md_bitmap_load(mddev
);
7175 rv
= PTR_ERR(bitmap
);
7177 md_bitmap_destroy(mddev
);
7178 mddev_resume(mddev
);
7180 /* remove the bitmap */
7181 if (!mddev
->bitmap
) {
7185 if (mddev
->bitmap
->storage
.file
) {
7189 if (mddev
->bitmap_info
.nodes
) {
7190 /* hold PW on all the bitmap lock */
7191 if (md_cluster_ops
->lock_all_bitmaps(mddev
) <= 0) {
7192 pr_warn("md: can't change bitmap to none since the array is in use by more than one node\n");
7194 md_cluster_ops
->unlock_all_bitmaps(mddev
);
7198 mddev
->bitmap_info
.nodes
= 0;
7199 md_cluster_ops
->leave(mddev
);
7201 mddev_suspend(mddev
);
7202 md_bitmap_destroy(mddev
);
7203 mddev_resume(mddev
);
7204 mddev
->bitmap_info
.offset
= 0;
7207 md_update_sb(mddev
, 1);
7213 static int set_disk_faulty(struct mddev
*mddev
, dev_t dev
)
7215 struct md_rdev
*rdev
;
7218 if (mddev
->pers
== NULL
)
7222 rdev
= md_find_rdev_rcu(mddev
, dev
);
7226 md_error(mddev
, rdev
);
7227 if (!test_bit(Faulty
, &rdev
->flags
))
7235 * We have a problem here : there is no easy way to give a CHS
7236 * virtual geometry. We currently pretend that we have a 2 heads
7237 * 4 sectors (with a BIG number of cylinders...). This drives
7238 * dosfs just mad... ;-)
7240 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
7242 struct mddev
*mddev
= bdev
->bd_disk
->private_data
;
7246 geo
->cylinders
= mddev
->array_sectors
/ 8;
7250 static inline bool md_ioctl_valid(unsigned int cmd
)
7255 case GET_ARRAY_INFO
:
7256 case GET_BITMAP_FILE
:
7259 case HOT_REMOVE_DISK
:
7262 case RESTART_ARRAY_RW
:
7264 case SET_ARRAY_INFO
:
7265 case SET_BITMAP_FILE
:
7266 case SET_DISK_FAULTY
:
7269 case CLUSTERED_DISK_NACK
:
7276 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
7277 unsigned int cmd
, unsigned long arg
)
7280 void __user
*argp
= (void __user
*)arg
;
7281 struct mddev
*mddev
= NULL
;
7283 bool did_set_md_closing
= false;
7285 if (!md_ioctl_valid(cmd
))
7290 case GET_ARRAY_INFO
:
7294 if (!capable(CAP_SYS_ADMIN
))
7299 * Commands dealing with the RAID driver but not any
7304 err
= get_version(argp
);
7310 autostart_arrays(arg
);
7317 * Commands creating/starting a new array:
7320 mddev
= bdev
->bd_disk
->private_data
;
7327 /* Some actions do not requires the mutex */
7329 case GET_ARRAY_INFO
:
7330 if (!mddev
->raid_disks
&& !mddev
->external
)
7333 err
= get_array_info(mddev
, argp
);
7337 if (!mddev
->raid_disks
&& !mddev
->external
)
7340 err
= get_disk_info(mddev
, argp
);
7343 case SET_DISK_FAULTY
:
7344 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
7347 case GET_BITMAP_FILE
:
7348 err
= get_bitmap_file(mddev
, argp
);
7353 if (cmd
== ADD_NEW_DISK
)
7354 /* need to ensure md_delayed_delete() has completed */
7355 flush_workqueue(md_misc_wq
);
7357 if (cmd
== HOT_REMOVE_DISK
)
7358 /* need to ensure recovery thread has run */
7359 wait_event_interruptible_timeout(mddev
->sb_wait
,
7360 !test_bit(MD_RECOVERY_NEEDED
,
7362 msecs_to_jiffies(5000));
7363 if (cmd
== STOP_ARRAY
|| cmd
== STOP_ARRAY_RO
) {
7364 /* Need to flush page cache, and ensure no-one else opens
7367 mutex_lock(&mddev
->open_mutex
);
7368 if (mddev
->pers
&& atomic_read(&mddev
->openers
) > 1) {
7369 mutex_unlock(&mddev
->open_mutex
);
7373 WARN_ON_ONCE(test_bit(MD_CLOSING
, &mddev
->flags
));
7374 set_bit(MD_CLOSING
, &mddev
->flags
);
7375 did_set_md_closing
= true;
7376 mutex_unlock(&mddev
->open_mutex
);
7377 sync_blockdev(bdev
);
7379 err
= mddev_lock(mddev
);
7381 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7386 if (cmd
== SET_ARRAY_INFO
) {
7387 mdu_array_info_t info
;
7389 memset(&info
, 0, sizeof(info
));
7390 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
7395 err
= update_array_info(mddev
, &info
);
7397 pr_warn("md: couldn't update array info. %d\n", err
);
7402 if (!list_empty(&mddev
->disks
)) {
7403 pr_warn("md: array %s already has disks!\n", mdname(mddev
));
7407 if (mddev
->raid_disks
) {
7408 pr_warn("md: array %s already initialised!\n", mdname(mddev
));
7412 err
= set_array_info(mddev
, &info
);
7414 pr_warn("md: couldn't set array info. %d\n", err
);
7421 * Commands querying/configuring an existing array:
7423 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
7424 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
7425 if ((!mddev
->raid_disks
&& !mddev
->external
)
7426 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
7427 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
7428 && cmd
!= GET_BITMAP_FILE
) {
7434 * Commands even a read-only array can execute:
7437 case RESTART_ARRAY_RW
:
7438 err
= restart_array(mddev
);
7442 err
= do_md_stop(mddev
, 0, bdev
);
7446 err
= md_set_readonly(mddev
, bdev
);
7449 case HOT_REMOVE_DISK
:
7450 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
7454 /* We can support ADD_NEW_DISK on read-only arrays
7455 * only if we are re-adding a preexisting device.
7456 * So require mddev->pers and MD_DISK_SYNC.
7459 mdu_disk_info_t info
;
7460 if (copy_from_user(&info
, argp
, sizeof(info
)))
7462 else if (!(info
.state
& (1<<MD_DISK_SYNC
)))
7463 /* Need to clear read-only for this */
7466 err
= add_new_disk(mddev
, &info
);
7472 if (get_user(ro
, (int __user
*)(arg
))) {
7478 /* if the bdev is going readonly the value of mddev->ro
7479 * does not matter, no writes are coming
7484 /* are we are already prepared for writes? */
7488 /* transitioning to readauto need only happen for
7489 * arrays that call md_write_start
7492 err
= restart_array(mddev
);
7495 set_disk_ro(mddev
->gendisk
, 0);
7502 * The remaining ioctls are changing the state of the
7503 * superblock, so we do not allow them on read-only arrays.
7505 if (mddev
->ro
&& mddev
->pers
) {
7506 if (mddev
->ro
== 2) {
7508 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7509 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7510 /* mddev_unlock will wake thread */
7511 /* If a device failed while we were read-only, we
7512 * need to make sure the metadata is updated now.
7514 if (test_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
)) {
7515 mddev_unlock(mddev
);
7516 wait_event(mddev
->sb_wait
,
7517 !test_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
) &&
7518 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
7519 mddev_lock_nointr(mddev
);
7530 mdu_disk_info_t info
;
7531 if (copy_from_user(&info
, argp
, sizeof(info
)))
7534 err
= add_new_disk(mddev
, &info
);
7538 case CLUSTERED_DISK_NACK
:
7539 if (mddev_is_clustered(mddev
))
7540 md_cluster_ops
->new_disk_ack(mddev
, false);
7546 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
7550 err
= do_md_run(mddev
);
7553 case SET_BITMAP_FILE
:
7554 err
= set_bitmap_file(mddev
, (int)arg
);
7563 if (mddev
->hold_active
== UNTIL_IOCTL
&&
7565 mddev
->hold_active
= 0;
7566 mddev_unlock(mddev
);
7568 if(did_set_md_closing
)
7569 clear_bit(MD_CLOSING
, &mddev
->flags
);
7572 #ifdef CONFIG_COMPAT
7573 static int md_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
7574 unsigned int cmd
, unsigned long arg
)
7577 case HOT_REMOVE_DISK
:
7579 case SET_DISK_FAULTY
:
7580 case SET_BITMAP_FILE
:
7581 /* These take in integer arg, do not convert */
7584 arg
= (unsigned long)compat_ptr(arg
);
7588 return md_ioctl(bdev
, mode
, cmd
, arg
);
7590 #endif /* CONFIG_COMPAT */
7592 static int md_open(struct block_device
*bdev
, fmode_t mode
)
7595 * Succeed if we can lock the mddev, which confirms that
7596 * it isn't being stopped right now.
7598 struct mddev
*mddev
= mddev_find(bdev
->bd_dev
);
7604 if (mddev
->gendisk
!= bdev
->bd_disk
) {
7605 /* we are racing with mddev_put which is discarding this
7609 /* Wait until bdev->bd_disk is definitely gone */
7610 flush_workqueue(md_misc_wq
);
7611 /* Then retry the open from the top */
7612 return -ERESTARTSYS
;
7614 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
7616 if ((err
= mutex_lock_interruptible(&mddev
->open_mutex
)))
7619 if (test_bit(MD_CLOSING
, &mddev
->flags
)) {
7620 mutex_unlock(&mddev
->open_mutex
);
7626 atomic_inc(&mddev
->openers
);
7627 mutex_unlock(&mddev
->open_mutex
);
7629 check_disk_change(bdev
);
7636 static void md_release(struct gendisk
*disk
, fmode_t mode
)
7638 struct mddev
*mddev
= disk
->private_data
;
7641 atomic_dec(&mddev
->openers
);
7645 static int md_media_changed(struct gendisk
*disk
)
7647 struct mddev
*mddev
= disk
->private_data
;
7649 return mddev
->changed
;
7652 static int md_revalidate(struct gendisk
*disk
)
7654 struct mddev
*mddev
= disk
->private_data
;
7659 static const struct block_device_operations md_fops
=
7661 .owner
= THIS_MODULE
,
7663 .release
= md_release
,
7665 #ifdef CONFIG_COMPAT
7666 .compat_ioctl
= md_compat_ioctl
,
7668 .getgeo
= md_getgeo
,
7669 .media_changed
= md_media_changed
,
7670 .revalidate_disk
= md_revalidate
,
7673 static int md_thread(void *arg
)
7675 struct md_thread
*thread
= arg
;
7678 * md_thread is a 'system-thread', it's priority should be very
7679 * high. We avoid resource deadlocks individually in each
7680 * raid personality. (RAID5 does preallocation) We also use RR and
7681 * the very same RT priority as kswapd, thus we will never get
7682 * into a priority inversion deadlock.
7684 * we definitely have to have equal or higher priority than
7685 * bdflush, otherwise bdflush will deadlock if there are too
7686 * many dirty RAID5 blocks.
7689 allow_signal(SIGKILL
);
7690 while (!kthread_should_stop()) {
7692 /* We need to wait INTERRUPTIBLE so that
7693 * we don't add to the load-average.
7694 * That means we need to be sure no signals are
7697 if (signal_pending(current
))
7698 flush_signals(current
);
7700 wait_event_interruptible_timeout
7702 test_bit(THREAD_WAKEUP
, &thread
->flags
)
7703 || kthread_should_stop() || kthread_should_park(),
7706 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
7707 if (kthread_should_park())
7709 if (!kthread_should_stop())
7710 thread
->run(thread
);
7716 void md_wakeup_thread(struct md_thread
*thread
)
7719 pr_debug("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
7720 set_bit(THREAD_WAKEUP
, &thread
->flags
);
7721 wake_up(&thread
->wqueue
);
7724 EXPORT_SYMBOL(md_wakeup_thread
);
7726 struct md_thread
*md_register_thread(void (*run
) (struct md_thread
*),
7727 struct mddev
*mddev
, const char *name
)
7729 struct md_thread
*thread
;
7731 thread
= kzalloc(sizeof(struct md_thread
), GFP_KERNEL
);
7735 init_waitqueue_head(&thread
->wqueue
);
7738 thread
->mddev
= mddev
;
7739 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
7740 thread
->tsk
= kthread_run(md_thread
, thread
,
7742 mdname(thread
->mddev
),
7744 if (IS_ERR(thread
->tsk
)) {
7750 EXPORT_SYMBOL(md_register_thread
);
7752 void md_unregister_thread(struct md_thread
**threadp
)
7754 struct md_thread
*thread
= *threadp
;
7757 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
7758 /* Locking ensures that mddev_unlock does not wake_up a
7759 * non-existent thread
7761 spin_lock(&pers_lock
);
7763 spin_unlock(&pers_lock
);
7765 kthread_stop(thread
->tsk
);
7768 EXPORT_SYMBOL(md_unregister_thread
);
7770 void md_error(struct mddev
*mddev
, struct md_rdev
*rdev
)
7772 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
7775 if (!mddev
->pers
|| !mddev
->pers
->error_handler
)
7777 mddev
->pers
->error_handler(mddev
,rdev
);
7778 if (mddev
->degraded
)
7779 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7780 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
7781 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7782 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7783 md_wakeup_thread(mddev
->thread
);
7784 if (mddev
->event_work
.func
)
7785 queue_work(md_misc_wq
, &mddev
->event_work
);
7786 md_new_event(mddev
);
7788 EXPORT_SYMBOL(md_error
);
7790 /* seq_file implementation /proc/mdstat */
7792 static void status_unused(struct seq_file
*seq
)
7795 struct md_rdev
*rdev
;
7797 seq_printf(seq
, "unused devices: ");
7799 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
7800 char b
[BDEVNAME_SIZE
];
7802 seq_printf(seq
, "%s ",
7803 bdevname(rdev
->bdev
,b
));
7806 seq_printf(seq
, "<none>");
7808 seq_printf(seq
, "\n");
7811 static int status_resync(struct seq_file
*seq
, struct mddev
*mddev
)
7813 sector_t max_sectors
, resync
, res
;
7814 unsigned long dt
, db
= 0;
7815 sector_t rt
, curr_mark_cnt
, resync_mark_cnt
;
7816 int scale
, recovery_active
;
7817 unsigned int per_milli
;
7819 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
7820 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
7821 max_sectors
= mddev
->resync_max_sectors
;
7823 max_sectors
= mddev
->dev_sectors
;
7825 resync
= mddev
->curr_resync
;
7827 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
7828 /* Still cleaning up */
7829 resync
= max_sectors
;
7830 } else if (resync
> max_sectors
)
7831 resync
= max_sectors
;
7833 resync
-= atomic_read(&mddev
->recovery_active
);
7836 if (test_bit(MD_RESYNCING_REMOTE
, &mddev
->recovery
)) {
7837 struct md_rdev
*rdev
;
7839 rdev_for_each(rdev
, mddev
)
7840 if (rdev
->raid_disk
>= 0 &&
7841 !test_bit(Faulty
, &rdev
->flags
) &&
7842 rdev
->recovery_offset
!= MaxSector
&&
7843 rdev
->recovery_offset
) {
7844 seq_printf(seq
, "\trecover=REMOTE");
7847 if (mddev
->reshape_position
!= MaxSector
)
7848 seq_printf(seq
, "\treshape=REMOTE");
7850 seq_printf(seq
, "\tresync=REMOTE");
7853 if (mddev
->recovery_cp
< MaxSector
) {
7854 seq_printf(seq
, "\tresync=PENDING");
7860 seq_printf(seq
, "\tresync=DELAYED");
7864 WARN_ON(max_sectors
== 0);
7865 /* Pick 'scale' such that (resync>>scale)*1000 will fit
7866 * in a sector_t, and (max_sectors>>scale) will fit in a
7867 * u32, as those are the requirements for sector_div.
7868 * Thus 'scale' must be at least 10
7871 if (sizeof(sector_t
) > sizeof(unsigned long)) {
7872 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
7875 res
= (resync
>>scale
)*1000;
7876 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
7880 int i
, x
= per_milli
/50, y
= 20-x
;
7881 seq_printf(seq
, "[");
7882 for (i
= 0; i
< x
; i
++)
7883 seq_printf(seq
, "=");
7884 seq_printf(seq
, ">");
7885 for (i
= 0; i
< y
; i
++)
7886 seq_printf(seq
, ".");
7887 seq_printf(seq
, "] ");
7889 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
7890 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
7892 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
7894 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
7895 "resync" : "recovery"))),
7896 per_milli
/10, per_milli
% 10,
7897 (unsigned long long) resync
/2,
7898 (unsigned long long) max_sectors
/2);
7901 * dt: time from mark until now
7902 * db: blocks written from mark until now
7903 * rt: remaining time
7905 * rt is a sector_t, which is always 64bit now. We are keeping
7906 * the original algorithm, but it is not really necessary.
7908 * Original algorithm:
7909 * So we divide before multiply in case it is 32bit and close
7911 * We scale the divisor (db) by 32 to avoid losing precision
7912 * near the end of resync when the number of remaining sectors
7914 * We then divide rt by 32 after multiplying by db to compensate.
7915 * The '+1' avoids division by zero if db is very small.
7917 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
7920 curr_mark_cnt
= mddev
->curr_mark_cnt
;
7921 recovery_active
= atomic_read(&mddev
->recovery_active
);
7922 resync_mark_cnt
= mddev
->resync_mark_cnt
;
7924 if (curr_mark_cnt
>= (recovery_active
+ resync_mark_cnt
))
7925 db
= curr_mark_cnt
- (recovery_active
+ resync_mark_cnt
);
7927 rt
= max_sectors
- resync
; /* number of remaining sectors */
7928 rt
= div64_u64(rt
, db
/32+1);
7932 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
7933 ((unsigned long)rt
% 60)/6);
7935 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
7939 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
7941 struct list_head
*tmp
;
7943 struct mddev
*mddev
;
7951 spin_lock(&all_mddevs_lock
);
7952 list_for_each(tmp
,&all_mddevs
)
7954 mddev
= list_entry(tmp
, struct mddev
, all_mddevs
);
7956 spin_unlock(&all_mddevs_lock
);
7959 spin_unlock(&all_mddevs_lock
);
7961 return (void*)2;/* tail */
7965 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
7967 struct list_head
*tmp
;
7968 struct mddev
*next_mddev
, *mddev
= v
;
7974 spin_lock(&all_mddevs_lock
);
7976 tmp
= all_mddevs
.next
;
7978 tmp
= mddev
->all_mddevs
.next
;
7979 if (tmp
!= &all_mddevs
)
7980 next_mddev
= mddev_get(list_entry(tmp
,struct mddev
,all_mddevs
));
7982 next_mddev
= (void*)2;
7985 spin_unlock(&all_mddevs_lock
);
7993 static void md_seq_stop(struct seq_file
*seq
, void *v
)
7995 struct mddev
*mddev
= v
;
7997 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
8001 static int md_seq_show(struct seq_file
*seq
, void *v
)
8003 struct mddev
*mddev
= v
;
8005 struct md_rdev
*rdev
;
8007 if (v
== (void*)1) {
8008 struct md_personality
*pers
;
8009 seq_printf(seq
, "Personalities : ");
8010 spin_lock(&pers_lock
);
8011 list_for_each_entry(pers
, &pers_list
, list
)
8012 seq_printf(seq
, "[%s] ", pers
->name
);
8014 spin_unlock(&pers_lock
);
8015 seq_printf(seq
, "\n");
8016 seq
->poll_event
= atomic_read(&md_event_count
);
8019 if (v
== (void*)2) {
8024 spin_lock(&mddev
->lock
);
8025 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
8026 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
8027 mddev
->pers
? "" : "in");
8030 seq_printf(seq
, " (read-only)");
8032 seq_printf(seq
, " (auto-read-only)");
8033 seq_printf(seq
, " %s", mddev
->pers
->name
);
8038 rdev_for_each_rcu(rdev
, mddev
) {
8039 char b
[BDEVNAME_SIZE
];
8040 seq_printf(seq
, " %s[%d]",
8041 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
8042 if (test_bit(WriteMostly
, &rdev
->flags
))
8043 seq_printf(seq
, "(W)");
8044 if (test_bit(Journal
, &rdev
->flags
))
8045 seq_printf(seq
, "(J)");
8046 if (test_bit(Faulty
, &rdev
->flags
)) {
8047 seq_printf(seq
, "(F)");
8050 if (rdev
->raid_disk
< 0)
8051 seq_printf(seq
, "(S)"); /* spare */
8052 if (test_bit(Replacement
, &rdev
->flags
))
8053 seq_printf(seq
, "(R)");
8054 sectors
+= rdev
->sectors
;
8058 if (!list_empty(&mddev
->disks
)) {
8060 seq_printf(seq
, "\n %llu blocks",
8061 (unsigned long long)
8062 mddev
->array_sectors
/ 2);
8064 seq_printf(seq
, "\n %llu blocks",
8065 (unsigned long long)sectors
/ 2);
8067 if (mddev
->persistent
) {
8068 if (mddev
->major_version
!= 0 ||
8069 mddev
->minor_version
!= 90) {
8070 seq_printf(seq
," super %d.%d",
8071 mddev
->major_version
,
8072 mddev
->minor_version
);
8074 } else if (mddev
->external
)
8075 seq_printf(seq
, " super external:%s",
8076 mddev
->metadata_type
);
8078 seq_printf(seq
, " super non-persistent");
8081 mddev
->pers
->status(seq
, mddev
);
8082 seq_printf(seq
, "\n ");
8083 if (mddev
->pers
->sync_request
) {
8084 if (status_resync(seq
, mddev
))
8085 seq_printf(seq
, "\n ");
8088 seq_printf(seq
, "\n ");
8090 md_bitmap_status(seq
, mddev
->bitmap
);
8092 seq_printf(seq
, "\n");
8094 spin_unlock(&mddev
->lock
);
8099 static const struct seq_operations md_seq_ops
= {
8100 .start
= md_seq_start
,
8101 .next
= md_seq_next
,
8102 .stop
= md_seq_stop
,
8103 .show
= md_seq_show
,
8106 static int md_seq_open(struct inode
*inode
, struct file
*file
)
8108 struct seq_file
*seq
;
8111 error
= seq_open(file
, &md_seq_ops
);
8115 seq
= file
->private_data
;
8116 seq
->poll_event
= atomic_read(&md_event_count
);
8120 static int md_unloading
;
8121 static __poll_t
mdstat_poll(struct file
*filp
, poll_table
*wait
)
8123 struct seq_file
*seq
= filp
->private_data
;
8127 return EPOLLIN
|EPOLLRDNORM
|EPOLLERR
|EPOLLPRI
;
8128 poll_wait(filp
, &md_event_waiters
, wait
);
8130 /* always allow read */
8131 mask
= EPOLLIN
| EPOLLRDNORM
;
8133 if (seq
->poll_event
!= atomic_read(&md_event_count
))
8134 mask
|= EPOLLERR
| EPOLLPRI
;
8138 static const struct file_operations md_seq_fops
= {
8139 .owner
= THIS_MODULE
,
8140 .open
= md_seq_open
,
8142 .llseek
= seq_lseek
,
8143 .release
= seq_release
,
8144 .poll
= mdstat_poll
,
8147 int register_md_personality(struct md_personality
*p
)
8149 pr_debug("md: %s personality registered for level %d\n",
8151 spin_lock(&pers_lock
);
8152 list_add_tail(&p
->list
, &pers_list
);
8153 spin_unlock(&pers_lock
);
8156 EXPORT_SYMBOL(register_md_personality
);
8158 int unregister_md_personality(struct md_personality
*p
)
8160 pr_debug("md: %s personality unregistered\n", p
->name
);
8161 spin_lock(&pers_lock
);
8162 list_del_init(&p
->list
);
8163 spin_unlock(&pers_lock
);
8166 EXPORT_SYMBOL(unregister_md_personality
);
8168 int register_md_cluster_operations(struct md_cluster_operations
*ops
,
8169 struct module
*module
)
8172 spin_lock(&pers_lock
);
8173 if (md_cluster_ops
!= NULL
)
8176 md_cluster_ops
= ops
;
8177 md_cluster_mod
= module
;
8179 spin_unlock(&pers_lock
);
8182 EXPORT_SYMBOL(register_md_cluster_operations
);
8184 int unregister_md_cluster_operations(void)
8186 spin_lock(&pers_lock
);
8187 md_cluster_ops
= NULL
;
8188 spin_unlock(&pers_lock
);
8191 EXPORT_SYMBOL(unregister_md_cluster_operations
);
8193 int md_setup_cluster(struct mddev
*mddev
, int nodes
)
8195 if (!md_cluster_ops
)
8196 request_module("md-cluster");
8197 spin_lock(&pers_lock
);
8198 /* ensure module won't be unloaded */
8199 if (!md_cluster_ops
|| !try_module_get(md_cluster_mod
)) {
8200 pr_warn("can't find md-cluster module or get it's reference.\n");
8201 spin_unlock(&pers_lock
);
8204 spin_unlock(&pers_lock
);
8206 return md_cluster_ops
->join(mddev
, nodes
);
8209 void md_cluster_stop(struct mddev
*mddev
)
8211 if (!md_cluster_ops
)
8213 md_cluster_ops
->leave(mddev
);
8214 module_put(md_cluster_mod
);
8217 static int is_mddev_idle(struct mddev
*mddev
, int init
)
8219 struct md_rdev
*rdev
;
8225 rdev_for_each_rcu(rdev
, mddev
) {
8226 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
8227 curr_events
= (int)part_stat_read_accum(&disk
->part0
, sectors
) -
8228 atomic_read(&disk
->sync_io
);
8229 /* sync IO will cause sync_io to increase before the disk_stats
8230 * as sync_io is counted when a request starts, and
8231 * disk_stats is counted when it completes.
8232 * So resync activity will cause curr_events to be smaller than
8233 * when there was no such activity.
8234 * non-sync IO will cause disk_stat to increase without
8235 * increasing sync_io so curr_events will (eventually)
8236 * be larger than it was before. Once it becomes
8237 * substantially larger, the test below will cause
8238 * the array to appear non-idle, and resync will slow
8240 * If there is a lot of outstanding resync activity when
8241 * we set last_event to curr_events, then all that activity
8242 * completing might cause the array to appear non-idle
8243 * and resync will be slowed down even though there might
8244 * not have been non-resync activity. This will only
8245 * happen once though. 'last_events' will soon reflect
8246 * the state where there is little or no outstanding
8247 * resync requests, and further resync activity will
8248 * always make curr_events less than last_events.
8251 if (init
|| curr_events
- rdev
->last_events
> 64) {
8252 rdev
->last_events
= curr_events
;
8260 void md_done_sync(struct mddev
*mddev
, int blocks
, int ok
)
8262 /* another "blocks" (512byte) blocks have been synced */
8263 atomic_sub(blocks
, &mddev
->recovery_active
);
8264 wake_up(&mddev
->recovery_wait
);
8266 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8267 set_bit(MD_RECOVERY_ERROR
, &mddev
->recovery
);
8268 md_wakeup_thread(mddev
->thread
);
8269 // stop recovery, signal do_sync ....
8272 EXPORT_SYMBOL(md_done_sync
);
8274 /* md_write_start(mddev, bi)
8275 * If we need to update some array metadata (e.g. 'active' flag
8276 * in superblock) before writing, schedule a superblock update
8277 * and wait for it to complete.
8278 * A return value of 'false' means that the write wasn't recorded
8279 * and cannot proceed as the array is being suspend.
8281 bool md_write_start(struct mddev
*mddev
, struct bio
*bi
)
8285 if (bio_data_dir(bi
) != WRITE
)
8288 BUG_ON(mddev
->ro
== 1);
8289 if (mddev
->ro
== 2) {
8290 /* need to switch to read/write */
8292 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8293 md_wakeup_thread(mddev
->thread
);
8294 md_wakeup_thread(mddev
->sync_thread
);
8298 percpu_ref_get(&mddev
->writes_pending
);
8299 smp_mb(); /* Match smp_mb in set_in_sync() */
8300 if (mddev
->safemode
== 1)
8301 mddev
->safemode
= 0;
8302 /* sync_checkers is always 0 when writes_pending is in per-cpu mode */
8303 if (mddev
->in_sync
|| mddev
->sync_checkers
) {
8304 spin_lock(&mddev
->lock
);
8305 if (mddev
->in_sync
) {
8307 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
8308 set_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
8309 md_wakeup_thread(mddev
->thread
);
8312 spin_unlock(&mddev
->lock
);
8316 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
8317 if (!mddev
->has_superblocks
)
8319 wait_event(mddev
->sb_wait
,
8320 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
) ||
8322 if (test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
)) {
8323 percpu_ref_put(&mddev
->writes_pending
);
8328 EXPORT_SYMBOL(md_write_start
);
8330 /* md_write_inc can only be called when md_write_start() has
8331 * already been called at least once of the current request.
8332 * It increments the counter and is useful when a single request
8333 * is split into several parts. Each part causes an increment and
8334 * so needs a matching md_write_end().
8335 * Unlike md_write_start(), it is safe to call md_write_inc() inside
8336 * a spinlocked region.
8338 void md_write_inc(struct mddev
*mddev
, struct bio
*bi
)
8340 if (bio_data_dir(bi
) != WRITE
)
8342 WARN_ON_ONCE(mddev
->in_sync
|| mddev
->ro
);
8343 percpu_ref_get(&mddev
->writes_pending
);
8345 EXPORT_SYMBOL(md_write_inc
);
8347 void md_write_end(struct mddev
*mddev
)
8349 percpu_ref_put(&mddev
->writes_pending
);
8351 if (mddev
->safemode
== 2)
8352 md_wakeup_thread(mddev
->thread
);
8353 else if (mddev
->safemode_delay
)
8354 /* The roundup() ensures this only performs locking once
8355 * every ->safemode_delay jiffies
8357 mod_timer(&mddev
->safemode_timer
,
8358 roundup(jiffies
, mddev
->safemode_delay
) +
8359 mddev
->safemode_delay
);
8362 EXPORT_SYMBOL(md_write_end
);
8364 /* md_allow_write(mddev)
8365 * Calling this ensures that the array is marked 'active' so that writes
8366 * may proceed without blocking. It is important to call this before
8367 * attempting a GFP_KERNEL allocation while holding the mddev lock.
8368 * Must be called with mddev_lock held.
8370 void md_allow_write(struct mddev
*mddev
)
8376 if (!mddev
->pers
->sync_request
)
8379 spin_lock(&mddev
->lock
);
8380 if (mddev
->in_sync
) {
8382 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
8383 set_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
8384 if (mddev
->safemode_delay
&&
8385 mddev
->safemode
== 0)
8386 mddev
->safemode
= 1;
8387 spin_unlock(&mddev
->lock
);
8388 md_update_sb(mddev
, 0);
8389 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
8390 /* wait for the dirty state to be recorded in the metadata */
8391 wait_event(mddev
->sb_wait
,
8392 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
8394 spin_unlock(&mddev
->lock
);
8396 EXPORT_SYMBOL_GPL(md_allow_write
);
8398 #define SYNC_MARKS 10
8399 #define SYNC_MARK_STEP (3*HZ)
8400 #define UPDATE_FREQUENCY (5*60*HZ)
8401 void md_do_sync(struct md_thread
*thread
)
8403 struct mddev
*mddev
= thread
->mddev
;
8404 struct mddev
*mddev2
;
8405 unsigned int currspeed
= 0, window
;
8406 sector_t max_sectors
,j
, io_sectors
, recovery_done
;
8407 unsigned long mark
[SYNC_MARKS
];
8408 unsigned long update_time
;
8409 sector_t mark_cnt
[SYNC_MARKS
];
8411 struct list_head
*tmp
;
8412 sector_t last_check
;
8414 struct md_rdev
*rdev
;
8415 char *desc
, *action
= NULL
;
8416 struct blk_plug plug
;
8419 /* just incase thread restarts... */
8420 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
8421 test_bit(MD_RECOVERY_WAIT
, &mddev
->recovery
))
8423 if (mddev
->ro
) {/* never try to sync a read-only array */
8424 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8428 if (mddev_is_clustered(mddev
)) {
8429 ret
= md_cluster_ops
->resync_start(mddev
);
8433 set_bit(MD_CLUSTER_RESYNC_LOCKED
, &mddev
->flags
);
8434 if (!(test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
8435 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) ||
8436 test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
8437 && ((unsigned long long)mddev
->curr_resync_completed
8438 < (unsigned long long)mddev
->resync_max_sectors
))
8442 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
8443 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)) {
8444 desc
= "data-check";
8446 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
8447 desc
= "requested-resync";
8451 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
8456 mddev
->last_sync_action
= action
?: desc
;
8458 /* we overload curr_resync somewhat here.
8459 * 0 == not engaged in resync at all
8460 * 2 == checking that there is no conflict with another sync
8461 * 1 == like 2, but have yielded to allow conflicting resync to
8463 * other == active in resync - this many blocks
8465 * Before starting a resync we must have set curr_resync to
8466 * 2, and then checked that every "conflicting" array has curr_resync
8467 * less than ours. When we find one that is the same or higher
8468 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8469 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8470 * This will mean we have to start checking from the beginning again.
8475 int mddev2_minor
= -1;
8476 mddev
->curr_resync
= 2;
8479 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8481 for_each_mddev(mddev2
, tmp
) {
8482 if (mddev2
== mddev
)
8484 if (!mddev
->parallel_resync
8485 && mddev2
->curr_resync
8486 && match_mddev_units(mddev
, mddev2
)) {
8488 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
8489 /* arbitrarily yield */
8490 mddev
->curr_resync
= 1;
8491 wake_up(&resync_wait
);
8493 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
8494 /* no need to wait here, we can wait the next
8495 * time 'round when curr_resync == 2
8498 /* We need to wait 'interruptible' so as not to
8499 * contribute to the load average, and not to
8500 * be caught by 'softlockup'
8502 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
8503 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
8504 mddev2
->curr_resync
>= mddev
->curr_resync
) {
8505 if (mddev2_minor
!= mddev2
->md_minor
) {
8506 mddev2_minor
= mddev2
->md_minor
;
8507 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8508 desc
, mdname(mddev
),
8512 if (signal_pending(current
))
8513 flush_signals(current
);
8515 finish_wait(&resync_wait
, &wq
);
8518 finish_wait(&resync_wait
, &wq
);
8521 } while (mddev
->curr_resync
< 2);
8524 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
8525 /* resync follows the size requested by the personality,
8526 * which defaults to physical size, but can be virtual size
8528 max_sectors
= mddev
->resync_max_sectors
;
8529 atomic64_set(&mddev
->resync_mismatches
, 0);
8530 /* we don't use the checkpoint if there's a bitmap */
8531 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
8532 j
= mddev
->resync_min
;
8533 else if (!mddev
->bitmap
)
8534 j
= mddev
->recovery_cp
;
8536 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)) {
8537 max_sectors
= mddev
->resync_max_sectors
;
8539 * If the original node aborts reshaping then we continue the
8540 * reshaping, so set j again to avoid restart reshape from the
8543 if (mddev_is_clustered(mddev
) &&
8544 mddev
->reshape_position
!= MaxSector
)
8545 j
= mddev
->reshape_position
;
8547 /* recovery follows the physical size of devices */
8548 max_sectors
= mddev
->dev_sectors
;
8551 rdev_for_each_rcu(rdev
, mddev
)
8552 if (rdev
->raid_disk
>= 0 &&
8553 !test_bit(Journal
, &rdev
->flags
) &&
8554 !test_bit(Faulty
, &rdev
->flags
) &&
8555 !test_bit(In_sync
, &rdev
->flags
) &&
8556 rdev
->recovery_offset
< j
)
8557 j
= rdev
->recovery_offset
;
8560 /* If there is a bitmap, we need to make sure all
8561 * writes that started before we added a spare
8562 * complete before we start doing a recovery.
8563 * Otherwise the write might complete and (via
8564 * bitmap_endwrite) set a bit in the bitmap after the
8565 * recovery has checked that bit and skipped that
8568 if (mddev
->bitmap
) {
8569 mddev
->pers
->quiesce(mddev
, 1);
8570 mddev
->pers
->quiesce(mddev
, 0);
8574 pr_info("md: %s of RAID array %s\n", desc
, mdname(mddev
));
8575 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev
));
8576 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8577 speed_max(mddev
), desc
);
8579 is_mddev_idle(mddev
, 1); /* this initializes IO event counters */
8582 for (m
= 0; m
< SYNC_MARKS
; m
++) {
8584 mark_cnt
[m
] = io_sectors
;
8587 mddev
->resync_mark
= mark
[last_mark
];
8588 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
8591 * Tune reconstruction:
8593 window
= 32 * (PAGE_SIZE
/ 512);
8594 pr_debug("md: using %dk window, over a total of %lluk.\n",
8595 window
/2, (unsigned long long)max_sectors
/2);
8597 atomic_set(&mddev
->recovery_active
, 0);
8601 pr_debug("md: resuming %s of %s from checkpoint.\n",
8602 desc
, mdname(mddev
));
8603 mddev
->curr_resync
= j
;
8605 mddev
->curr_resync
= 3; /* no longer delayed */
8606 mddev
->curr_resync_completed
= j
;
8607 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
8608 md_new_event(mddev
);
8609 update_time
= jiffies
;
8611 blk_start_plug(&plug
);
8612 while (j
< max_sectors
) {
8617 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8618 ((mddev
->curr_resync
> mddev
->curr_resync_completed
&&
8619 (mddev
->curr_resync
- mddev
->curr_resync_completed
)
8620 > (max_sectors
>> 4)) ||
8621 time_after_eq(jiffies
, update_time
+ UPDATE_FREQUENCY
) ||
8622 (j
- mddev
->curr_resync_completed
)*2
8623 >= mddev
->resync_max
- mddev
->curr_resync_completed
||
8624 mddev
->curr_resync_completed
> mddev
->resync_max
8626 /* time to update curr_resync_completed */
8627 wait_event(mddev
->recovery_wait
,
8628 atomic_read(&mddev
->recovery_active
) == 0);
8629 mddev
->curr_resync_completed
= j
;
8630 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) &&
8631 j
> mddev
->recovery_cp
)
8632 mddev
->recovery_cp
= j
;
8633 update_time
= jiffies
;
8634 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
8635 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
8638 while (j
>= mddev
->resync_max
&&
8639 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
8640 /* As this condition is controlled by user-space,
8641 * we can block indefinitely, so use '_interruptible'
8642 * to avoid triggering warnings.
8644 flush_signals(current
); /* just in case */
8645 wait_event_interruptible(mddev
->recovery_wait
,
8646 mddev
->resync_max
> j
8647 || test_bit(MD_RECOVERY_INTR
,
8651 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8654 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
);
8656 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8660 if (!skipped
) { /* actual IO requested */
8661 io_sectors
+= sectors
;
8662 atomic_add(sectors
, &mddev
->recovery_active
);
8665 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8669 if (j
> max_sectors
)
8670 /* when skipping, extra large numbers can be returned. */
8673 mddev
->curr_resync
= j
;
8674 mddev
->curr_mark_cnt
= io_sectors
;
8675 if (last_check
== 0)
8676 /* this is the earliest that rebuild will be
8677 * visible in /proc/mdstat
8679 md_new_event(mddev
);
8681 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
8684 last_check
= io_sectors
;
8686 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
8688 int next
= (last_mark
+1) % SYNC_MARKS
;
8690 mddev
->resync_mark
= mark
[next
];
8691 mddev
->resync_mark_cnt
= mark_cnt
[next
];
8692 mark
[next
] = jiffies
;
8693 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
8697 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8701 * this loop exits only if either when we are slower than
8702 * the 'hard' speed limit, or the system was IO-idle for
8704 * the system might be non-idle CPU-wise, but we only care
8705 * about not overloading the IO subsystem. (things like an
8706 * e2fsck being done on the RAID array should execute fast)
8710 recovery_done
= io_sectors
- atomic_read(&mddev
->recovery_active
);
8711 currspeed
= ((unsigned long)(recovery_done
- mddev
->resync_mark_cnt
))/2
8712 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
8714 if (currspeed
> speed_min(mddev
)) {
8715 if (currspeed
> speed_max(mddev
)) {
8719 if (!is_mddev_idle(mddev
, 0)) {
8721 * Give other IO more of a chance.
8722 * The faster the devices, the less we wait.
8724 wait_event(mddev
->recovery_wait
,
8725 !atomic_read(&mddev
->recovery_active
));
8729 pr_info("md: %s: %s %s.\n",mdname(mddev
), desc
,
8730 test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)
8731 ? "interrupted" : "done");
8733 * this also signals 'finished resyncing' to md_stop
8735 blk_finish_plug(&plug
);
8736 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
8738 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8739 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
8740 mddev
->curr_resync
> 3) {
8741 mddev
->curr_resync_completed
= mddev
->curr_resync
;
8742 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
8744 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
);
8746 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
8747 mddev
->curr_resync
> 3) {
8748 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
8749 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
8750 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
8751 pr_debug("md: checkpointing %s of %s.\n",
8752 desc
, mdname(mddev
));
8753 if (test_bit(MD_RECOVERY_ERROR
,
8755 mddev
->recovery_cp
=
8756 mddev
->curr_resync_completed
;
8758 mddev
->recovery_cp
=
8762 mddev
->recovery_cp
= MaxSector
;
8764 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8765 mddev
->curr_resync
= MaxSector
;
8766 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8767 test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
)) {
8769 rdev_for_each_rcu(rdev
, mddev
)
8770 if (rdev
->raid_disk
>= 0 &&
8771 mddev
->delta_disks
>= 0 &&
8772 !test_bit(Journal
, &rdev
->flags
) &&
8773 !test_bit(Faulty
, &rdev
->flags
) &&
8774 !test_bit(In_sync
, &rdev
->flags
) &&
8775 rdev
->recovery_offset
< mddev
->curr_resync
)
8776 rdev
->recovery_offset
= mddev
->curr_resync
;
8782 /* set CHANGE_PENDING here since maybe another update is needed,
8783 * so other nodes are informed. It should be harmless for normal
8785 set_mask_bits(&mddev
->sb_flags
, 0,
8786 BIT(MD_SB_CHANGE_PENDING
) | BIT(MD_SB_CHANGE_DEVS
));
8788 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8789 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
8790 mddev
->delta_disks
> 0 &&
8791 mddev
->pers
->finish_reshape
&&
8792 mddev
->pers
->size
&&
8794 mddev_lock_nointr(mddev
);
8795 md_set_array_sectors(mddev
, mddev
->pers
->size(mddev
, 0, 0));
8796 mddev_unlock(mddev
);
8797 if (!mddev_is_clustered(mddev
)) {
8798 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
8799 revalidate_disk(mddev
->gendisk
);
8803 spin_lock(&mddev
->lock
);
8804 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
8805 /* We completed so min/max setting can be forgotten if used. */
8806 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
8807 mddev
->resync_min
= 0;
8808 mddev
->resync_max
= MaxSector
;
8809 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
8810 mddev
->resync_min
= mddev
->curr_resync_completed
;
8811 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
8812 mddev
->curr_resync
= 0;
8813 spin_unlock(&mddev
->lock
);
8815 wake_up(&resync_wait
);
8816 md_wakeup_thread(mddev
->thread
);
8819 EXPORT_SYMBOL_GPL(md_do_sync
);
8821 static int remove_and_add_spares(struct mddev
*mddev
,
8822 struct md_rdev
*this)
8824 struct md_rdev
*rdev
;
8827 bool remove_some
= false;
8829 if (this && test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
8830 /* Mustn't remove devices when resync thread is running */
8833 rdev_for_each(rdev
, mddev
) {
8834 if ((this == NULL
|| rdev
== this) &&
8835 rdev
->raid_disk
>= 0 &&
8836 !test_bit(Blocked
, &rdev
->flags
) &&
8837 test_bit(Faulty
, &rdev
->flags
) &&
8838 atomic_read(&rdev
->nr_pending
)==0) {
8839 /* Faulty non-Blocked devices with nr_pending == 0
8840 * never get nr_pending incremented,
8841 * never get Faulty cleared, and never get Blocked set.
8842 * So we can synchronize_rcu now rather than once per device
8845 set_bit(RemoveSynchronized
, &rdev
->flags
);
8851 rdev_for_each(rdev
, mddev
) {
8852 if ((this == NULL
|| rdev
== this) &&
8853 rdev
->raid_disk
>= 0 &&
8854 !test_bit(Blocked
, &rdev
->flags
) &&
8855 ((test_bit(RemoveSynchronized
, &rdev
->flags
) ||
8856 (!test_bit(In_sync
, &rdev
->flags
) &&
8857 !test_bit(Journal
, &rdev
->flags
))) &&
8858 atomic_read(&rdev
->nr_pending
)==0)) {
8859 if (mddev
->pers
->hot_remove_disk(
8860 mddev
, rdev
) == 0) {
8861 sysfs_unlink_rdev(mddev
, rdev
);
8862 rdev
->saved_raid_disk
= rdev
->raid_disk
;
8863 rdev
->raid_disk
= -1;
8867 if (remove_some
&& test_bit(RemoveSynchronized
, &rdev
->flags
))
8868 clear_bit(RemoveSynchronized
, &rdev
->flags
);
8871 if (removed
&& mddev
->kobj
.sd
)
8872 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
8874 if (this && removed
)
8877 rdev_for_each(rdev
, mddev
) {
8878 if (this && this != rdev
)
8880 if (test_bit(Candidate
, &rdev
->flags
))
8882 if (rdev
->raid_disk
>= 0 &&
8883 !test_bit(In_sync
, &rdev
->flags
) &&
8884 !test_bit(Journal
, &rdev
->flags
) &&
8885 !test_bit(Faulty
, &rdev
->flags
))
8887 if (rdev
->raid_disk
>= 0)
8889 if (test_bit(Faulty
, &rdev
->flags
))
8891 if (!test_bit(Journal
, &rdev
->flags
)) {
8893 ! (rdev
->saved_raid_disk
>= 0 &&
8894 !test_bit(Bitmap_sync
, &rdev
->flags
)))
8897 rdev
->recovery_offset
= 0;
8900 hot_add_disk(mddev
, rdev
) == 0) {
8901 if (sysfs_link_rdev(mddev
, rdev
))
8902 /* failure here is OK */;
8903 if (!test_bit(Journal
, &rdev
->flags
))
8905 md_new_event(mddev
);
8906 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
8911 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
8915 static void md_start_sync(struct work_struct
*ws
)
8917 struct mddev
*mddev
= container_of(ws
, struct mddev
, del_work
);
8919 mddev
->sync_thread
= md_register_thread(md_do_sync
,
8922 if (!mddev
->sync_thread
) {
8923 pr_warn("%s: could not start resync thread...\n",
8925 /* leave the spares where they are, it shouldn't hurt */
8926 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
8927 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
8928 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
8929 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
8930 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
8931 wake_up(&resync_wait
);
8932 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
8934 if (mddev
->sysfs_action
)
8935 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
8937 md_wakeup_thread(mddev
->sync_thread
);
8938 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
8939 md_new_event(mddev
);
8943 * This routine is regularly called by all per-raid-array threads to
8944 * deal with generic issues like resync and super-block update.
8945 * Raid personalities that don't have a thread (linear/raid0) do not
8946 * need this as they never do any recovery or update the superblock.
8948 * It does not do any resync itself, but rather "forks" off other threads
8949 * to do that as needed.
8950 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8951 * "->recovery" and create a thread at ->sync_thread.
8952 * When the thread finishes it sets MD_RECOVERY_DONE
8953 * and wakeups up this thread which will reap the thread and finish up.
8954 * This thread also removes any faulty devices (with nr_pending == 0).
8956 * The overall approach is:
8957 * 1/ if the superblock needs updating, update it.
8958 * 2/ If a recovery thread is running, don't do anything else.
8959 * 3/ If recovery has finished, clean up, possibly marking spares active.
8960 * 4/ If there are any faulty devices, remove them.
8961 * 5/ If array is degraded, try to add spares devices
8962 * 6/ If array has spares or is not in-sync, start a resync thread.
8964 void md_check_recovery(struct mddev
*mddev
)
8966 if (test_bit(MD_ALLOW_SB_UPDATE
, &mddev
->flags
) && mddev
->sb_flags
) {
8967 /* Write superblock - thread that called mddev_suspend()
8968 * holds reconfig_mutex for us.
8970 set_bit(MD_UPDATING_SB
, &mddev
->flags
);
8971 smp_mb__after_atomic();
8972 if (test_bit(MD_ALLOW_SB_UPDATE
, &mddev
->flags
))
8973 md_update_sb(mddev
, 0);
8974 clear_bit_unlock(MD_UPDATING_SB
, &mddev
->flags
);
8975 wake_up(&mddev
->sb_wait
);
8978 if (mddev
->suspended
)
8982 md_bitmap_daemon_work(mddev
);
8984 if (signal_pending(current
)) {
8985 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
8986 pr_debug("md: %s in immediate safe mode\n",
8988 mddev
->safemode
= 2;
8990 flush_signals(current
);
8993 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
8996 (mddev
->sb_flags
& ~ (1<<MD_SB_CHANGE_PENDING
)) ||
8997 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
8998 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
8999 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
9000 (mddev
->safemode
== 2
9001 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
9005 if (mddev_trylock(mddev
)) {
9007 bool try_set_sync
= mddev
->safemode
!= 0;
9009 if (!mddev
->external
&& mddev
->safemode
== 1)
9010 mddev
->safemode
= 0;
9013 struct md_rdev
*rdev
;
9014 if (!mddev
->external
&& mddev
->in_sync
)
9015 /* 'Blocked' flag not needed as failed devices
9016 * will be recorded if array switched to read/write.
9017 * Leaving it set will prevent the device
9018 * from being removed.
9020 rdev_for_each(rdev
, mddev
)
9021 clear_bit(Blocked
, &rdev
->flags
);
9022 /* On a read-only array we can:
9023 * - remove failed devices
9024 * - add already-in_sync devices if the array itself
9026 * As we only add devices that are already in-sync,
9027 * we can activate the spares immediately.
9029 remove_and_add_spares(mddev
, NULL
);
9030 /* There is no thread, but we need to call
9031 * ->spare_active and clear saved_raid_disk
9033 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
9034 md_reap_sync_thread(mddev
);
9035 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
9036 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
9037 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
9041 if (mddev_is_clustered(mddev
)) {
9042 struct md_rdev
*rdev
;
9043 /* kick the device if another node issued a
9046 rdev_for_each(rdev
, mddev
) {
9047 if (test_and_clear_bit(ClusterRemove
, &rdev
->flags
) &&
9048 rdev
->raid_disk
< 0)
9049 md_kick_rdev_from_array(rdev
);
9053 if (try_set_sync
&& !mddev
->external
&& !mddev
->in_sync
) {
9054 spin_lock(&mddev
->lock
);
9056 spin_unlock(&mddev
->lock
);
9059 if (mddev
->sb_flags
)
9060 md_update_sb(mddev
, 0);
9062 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
9063 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
9064 /* resync/recovery still happening */
9065 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
9068 if (mddev
->sync_thread
) {
9069 md_reap_sync_thread(mddev
);
9072 /* Set RUNNING before clearing NEEDED to avoid
9073 * any transients in the value of "sync_action".
9075 mddev
->curr_resync_completed
= 0;
9076 spin_lock(&mddev
->lock
);
9077 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
9078 spin_unlock(&mddev
->lock
);
9079 /* Clear some bits that don't mean anything, but
9082 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
9083 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
9085 if (!test_and_clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
9086 test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
9088 /* no recovery is running.
9089 * remove any failed drives, then
9090 * add spares if possible.
9091 * Spares are also removed and re-added, to allow
9092 * the personality to fail the re-add.
9095 if (mddev
->reshape_position
!= MaxSector
) {
9096 if (mddev
->pers
->check_reshape
== NULL
||
9097 mddev
->pers
->check_reshape(mddev
) != 0)
9098 /* Cannot proceed */
9100 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
9101 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
9102 } else if ((spares
= remove_and_add_spares(mddev
, NULL
))) {
9103 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
9104 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
9105 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
9106 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
9107 } else if (mddev
->recovery_cp
< MaxSector
) {
9108 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
9109 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
9110 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
9111 /* nothing to be done ... */
9114 if (mddev
->pers
->sync_request
) {
9116 /* We are adding a device or devices to an array
9117 * which has the bitmap stored on all devices.
9118 * So make sure all bitmap pages get written
9120 md_bitmap_write_all(mddev
->bitmap
);
9122 INIT_WORK(&mddev
->del_work
, md_start_sync
);
9123 queue_work(md_misc_wq
, &mddev
->del_work
);
9127 if (!mddev
->sync_thread
) {
9128 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
9129 wake_up(&resync_wait
);
9130 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
9132 if (mddev
->sysfs_action
)
9133 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
9136 wake_up(&mddev
->sb_wait
);
9137 mddev_unlock(mddev
);
9140 EXPORT_SYMBOL(md_check_recovery
);
9142 void md_reap_sync_thread(struct mddev
*mddev
)
9144 struct md_rdev
*rdev
;
9145 sector_t old_dev_sectors
= mddev
->dev_sectors
;
9146 bool is_reshaped
= false;
9148 /* resync has finished, collect result */
9149 md_unregister_thread(&mddev
->sync_thread
);
9150 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
9151 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
) &&
9152 mddev
->degraded
!= mddev
->raid_disks
) {
9154 /* activate any spares */
9155 if (mddev
->pers
->spare_active(mddev
)) {
9156 sysfs_notify(&mddev
->kobj
, NULL
,
9158 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
9161 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
9162 mddev
->pers
->finish_reshape
) {
9163 mddev
->pers
->finish_reshape(mddev
);
9164 if (mddev_is_clustered(mddev
))
9168 /* If array is no-longer degraded, then any saved_raid_disk
9169 * information must be scrapped.
9171 if (!mddev
->degraded
)
9172 rdev_for_each(rdev
, mddev
)
9173 rdev
->saved_raid_disk
= -1;
9175 md_update_sb(mddev
, 1);
9176 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
9177 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
9179 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED
, &mddev
->flags
))
9180 md_cluster_ops
->resync_finish(mddev
);
9181 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
9182 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
9183 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
9184 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
9185 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
9186 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
9188 * We call md_cluster_ops->update_size here because sync_size could
9189 * be changed by md_update_sb, and MD_RECOVERY_RESHAPE is cleared,
9190 * so it is time to update size across cluster.
9192 if (mddev_is_clustered(mddev
) && is_reshaped
9193 && !test_bit(MD_CLOSING
, &mddev
->flags
))
9194 md_cluster_ops
->update_size(mddev
, old_dev_sectors
);
9195 wake_up(&resync_wait
);
9196 /* flag recovery needed just to double check */
9197 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
9198 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
9199 md_new_event(mddev
);
9200 if (mddev
->event_work
.func
)
9201 queue_work(md_misc_wq
, &mddev
->event_work
);
9203 EXPORT_SYMBOL(md_reap_sync_thread
);
9205 void md_wait_for_blocked_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
9207 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
9208 wait_event_timeout(rdev
->blocked_wait
,
9209 !test_bit(Blocked
, &rdev
->flags
) &&
9210 !test_bit(BlockedBadBlocks
, &rdev
->flags
),
9211 msecs_to_jiffies(5000));
9212 rdev_dec_pending(rdev
, mddev
);
9214 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
9216 void md_finish_reshape(struct mddev
*mddev
)
9218 /* called be personality module when reshape completes. */
9219 struct md_rdev
*rdev
;
9221 rdev_for_each(rdev
, mddev
) {
9222 if (rdev
->data_offset
> rdev
->new_data_offset
)
9223 rdev
->sectors
+= rdev
->data_offset
- rdev
->new_data_offset
;
9225 rdev
->sectors
-= rdev
->new_data_offset
- rdev
->data_offset
;
9226 rdev
->data_offset
= rdev
->new_data_offset
;
9229 EXPORT_SYMBOL(md_finish_reshape
);
9231 /* Bad block management */
9233 /* Returns 1 on success, 0 on failure */
9234 int rdev_set_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
,
9237 struct mddev
*mddev
= rdev
->mddev
;
9240 s
+= rdev
->new_data_offset
;
9242 s
+= rdev
->data_offset
;
9243 rv
= badblocks_set(&rdev
->badblocks
, s
, sectors
, 0);
9245 /* Make sure they get written out promptly */
9246 if (test_bit(ExternalBbl
, &rdev
->flags
))
9247 sysfs_notify(&rdev
->kobj
, NULL
,
9248 "unacknowledged_bad_blocks");
9249 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
9250 set_mask_bits(&mddev
->sb_flags
, 0,
9251 BIT(MD_SB_CHANGE_CLEAN
) | BIT(MD_SB_CHANGE_PENDING
));
9252 md_wakeup_thread(rdev
->mddev
->thread
);
9257 EXPORT_SYMBOL_GPL(rdev_set_badblocks
);
9259 int rdev_clear_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
,
9264 s
+= rdev
->new_data_offset
;
9266 s
+= rdev
->data_offset
;
9267 rv
= badblocks_clear(&rdev
->badblocks
, s
, sectors
);
9268 if ((rv
== 0) && test_bit(ExternalBbl
, &rdev
->flags
))
9269 sysfs_notify(&rdev
->kobj
, NULL
, "bad_blocks");
9272 EXPORT_SYMBOL_GPL(rdev_clear_badblocks
);
9274 static int md_notify_reboot(struct notifier_block
*this,
9275 unsigned long code
, void *x
)
9277 struct list_head
*tmp
;
9278 struct mddev
*mddev
;
9281 for_each_mddev(mddev
, tmp
) {
9282 if (mddev_trylock(mddev
)) {
9284 __md_stop_writes(mddev
);
9285 if (mddev
->persistent
)
9286 mddev
->safemode
= 2;
9287 mddev_unlock(mddev
);
9292 * certain more exotic SCSI devices are known to be
9293 * volatile wrt too early system reboots. While the
9294 * right place to handle this issue is the given
9295 * driver, we do want to have a safe RAID driver ...
9303 static struct notifier_block md_notifier
= {
9304 .notifier_call
= md_notify_reboot
,
9306 .priority
= INT_MAX
, /* before any real devices */
9309 static void md_geninit(void)
9311 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
9313 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
9316 static int __init
md_init(void)
9320 md_wq
= alloc_workqueue("md", WQ_MEM_RECLAIM
, 0);
9324 md_misc_wq
= alloc_workqueue("md_misc", 0, 0);
9328 if ((ret
= register_blkdev(MD_MAJOR
, "md")) < 0)
9331 if ((ret
= register_blkdev(0, "mdp")) < 0)
9335 blk_register_region(MKDEV(MD_MAJOR
, 0), 512, THIS_MODULE
,
9336 md_probe
, NULL
, NULL
);
9337 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
9338 md_probe
, NULL
, NULL
);
9340 register_reboot_notifier(&md_notifier
);
9341 raid_table_header
= register_sysctl_table(raid_root_table
);
9347 unregister_blkdev(MD_MAJOR
, "md");
9349 destroy_workqueue(md_misc_wq
);
9351 destroy_workqueue(md_wq
);
9356 static void check_sb_changes(struct mddev
*mddev
, struct md_rdev
*rdev
)
9358 struct mdp_superblock_1
*sb
= page_address(rdev
->sb_page
);
9359 struct md_rdev
*rdev2
;
9361 char b
[BDEVNAME_SIZE
];
9364 * If size is changed in another node then we need to
9365 * do resize as well.
9367 if (mddev
->dev_sectors
!= le64_to_cpu(sb
->size
)) {
9368 ret
= mddev
->pers
->resize(mddev
, le64_to_cpu(sb
->size
));
9370 pr_info("md-cluster: resize failed\n");
9372 md_bitmap_update_sb(mddev
->bitmap
);
9375 /* Check for change of roles in the active devices */
9376 rdev_for_each(rdev2
, mddev
) {
9377 if (test_bit(Faulty
, &rdev2
->flags
))
9380 /* Check if the roles changed */
9381 role
= le16_to_cpu(sb
->dev_roles
[rdev2
->desc_nr
]);
9383 if (test_bit(Candidate
, &rdev2
->flags
)) {
9384 if (role
== 0xfffe) {
9385 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2
->bdev
,b
));
9386 md_kick_rdev_from_array(rdev2
);
9390 clear_bit(Candidate
, &rdev2
->flags
);
9393 if (role
!= rdev2
->raid_disk
) {
9395 * got activated except reshape is happening.
9397 if (rdev2
->raid_disk
== -1 && role
!= 0xffff &&
9398 !(le32_to_cpu(sb
->feature_map
) &
9399 MD_FEATURE_RESHAPE_ACTIVE
)) {
9400 rdev2
->saved_raid_disk
= role
;
9401 ret
= remove_and_add_spares(mddev
, rdev2
);
9402 pr_info("Activated spare: %s\n",
9403 bdevname(rdev2
->bdev
,b
));
9404 /* wakeup mddev->thread here, so array could
9405 * perform resync with the new activated disk */
9406 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
9407 md_wakeup_thread(mddev
->thread
);
9410 * We just want to do the minimum to mark the disk
9411 * as faulty. The recovery is performed by the
9412 * one who initiated the error.
9414 if ((role
== 0xfffe) || (role
== 0xfffd)) {
9415 md_error(mddev
, rdev2
);
9416 clear_bit(Blocked
, &rdev2
->flags
);
9421 if (mddev
->raid_disks
!= le32_to_cpu(sb
->raid_disks
))
9422 update_raid_disks(mddev
, le32_to_cpu(sb
->raid_disks
));
9425 * Since mddev->delta_disks has already updated in update_raid_disks,
9426 * so it is time to check reshape.
9428 if (test_bit(MD_RESYNCING_REMOTE
, &mddev
->recovery
) &&
9429 (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
9431 * reshape is happening in the remote node, we need to
9432 * update reshape_position and call start_reshape.
9434 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
9435 if (mddev
->pers
->update_reshape_pos
)
9436 mddev
->pers
->update_reshape_pos(mddev
);
9437 if (mddev
->pers
->start_reshape
)
9438 mddev
->pers
->start_reshape(mddev
);
9439 } else if (test_bit(MD_RESYNCING_REMOTE
, &mddev
->recovery
) &&
9440 mddev
->reshape_position
!= MaxSector
&&
9441 !(le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
9442 /* reshape is just done in another node. */
9443 mddev
->reshape_position
= MaxSector
;
9444 if (mddev
->pers
->update_reshape_pos
)
9445 mddev
->pers
->update_reshape_pos(mddev
);
9448 /* Finally set the event to be up to date */
9449 mddev
->events
= le64_to_cpu(sb
->events
);
9452 static int read_rdev(struct mddev
*mddev
, struct md_rdev
*rdev
)
9455 struct page
*swapout
= rdev
->sb_page
;
9456 struct mdp_superblock_1
*sb
;
9458 /* Store the sb page of the rdev in the swapout temporary
9459 * variable in case we err in the future
9461 rdev
->sb_page
= NULL
;
9462 err
= alloc_disk_sb(rdev
);
9464 ClearPageUptodate(rdev
->sb_page
);
9465 rdev
->sb_loaded
= 0;
9466 err
= super_types
[mddev
->major_version
].
9467 load_super(rdev
, NULL
, mddev
->minor_version
);
9470 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9471 __func__
, __LINE__
, rdev
->desc_nr
, err
);
9473 put_page(rdev
->sb_page
);
9474 rdev
->sb_page
= swapout
;
9475 rdev
->sb_loaded
= 1;
9479 sb
= page_address(rdev
->sb_page
);
9480 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9484 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RECOVERY_OFFSET
))
9485 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
9487 /* The other node finished recovery, call spare_active to set
9488 * device In_sync and mddev->degraded
9490 if (rdev
->recovery_offset
== MaxSector
&&
9491 !test_bit(In_sync
, &rdev
->flags
) &&
9492 mddev
->pers
->spare_active(mddev
))
9493 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
9499 void md_reload_sb(struct mddev
*mddev
, int nr
)
9501 struct md_rdev
*rdev
;
9505 rdev_for_each_rcu(rdev
, mddev
) {
9506 if (rdev
->desc_nr
== nr
)
9510 if (!rdev
|| rdev
->desc_nr
!= nr
) {
9511 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__
, __LINE__
, nr
);
9515 err
= read_rdev(mddev
, rdev
);
9519 check_sb_changes(mddev
, rdev
);
9521 /* Read all rdev's to update recovery_offset */
9522 rdev_for_each_rcu(rdev
, mddev
) {
9523 if (!test_bit(Faulty
, &rdev
->flags
))
9524 read_rdev(mddev
, rdev
);
9527 EXPORT_SYMBOL(md_reload_sb
);
9532 * Searches all registered partitions for autorun RAID arrays
9536 static DEFINE_MUTEX(detected_devices_mutex
);
9537 static LIST_HEAD(all_detected_devices
);
9538 struct detected_devices_node
{
9539 struct list_head list
;
9543 void md_autodetect_dev(dev_t dev
)
9545 struct detected_devices_node
*node_detected_dev
;
9547 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
9548 if (node_detected_dev
) {
9549 node_detected_dev
->dev
= dev
;
9550 mutex_lock(&detected_devices_mutex
);
9551 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
9552 mutex_unlock(&detected_devices_mutex
);
9556 static void autostart_arrays(int part
)
9558 struct md_rdev
*rdev
;
9559 struct detected_devices_node
*node_detected_dev
;
9561 int i_scanned
, i_passed
;
9566 pr_info("md: Autodetecting RAID arrays.\n");
9568 mutex_lock(&detected_devices_mutex
);
9569 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
9571 node_detected_dev
= list_entry(all_detected_devices
.next
,
9572 struct detected_devices_node
, list
);
9573 list_del(&node_detected_dev
->list
);
9574 dev
= node_detected_dev
->dev
;
9575 kfree(node_detected_dev
);
9576 mutex_unlock(&detected_devices_mutex
);
9577 rdev
= md_import_device(dev
,0, 90);
9578 mutex_lock(&detected_devices_mutex
);
9582 if (test_bit(Faulty
, &rdev
->flags
))
9585 set_bit(AutoDetected
, &rdev
->flags
);
9586 list_add(&rdev
->same_set
, &pending_raid_disks
);
9589 mutex_unlock(&detected_devices_mutex
);
9591 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned
, i_passed
);
9593 autorun_devices(part
);
9596 #endif /* !MODULE */
9598 static __exit
void md_exit(void)
9600 struct mddev
*mddev
;
9601 struct list_head
*tmp
;
9604 blk_unregister_region(MKDEV(MD_MAJOR
,0), 512);
9605 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
9607 unregister_blkdev(MD_MAJOR
,"md");
9608 unregister_blkdev(mdp_major
, "mdp");
9609 unregister_reboot_notifier(&md_notifier
);
9610 unregister_sysctl_table(raid_table_header
);
9612 /* We cannot unload the modules while some process is
9613 * waiting for us in select() or poll() - wake them up
9616 while (waitqueue_active(&md_event_waiters
)) {
9617 /* not safe to leave yet */
9618 wake_up(&md_event_waiters
);
9622 remove_proc_entry("mdstat", NULL
);
9624 for_each_mddev(mddev
, tmp
) {
9625 export_array(mddev
);
9627 mddev
->hold_active
= 0;
9629 * for_each_mddev() will call mddev_put() at the end of each
9630 * iteration. As the mddev is now fully clear, this will
9631 * schedule the mddev for destruction by a workqueue, and the
9632 * destroy_workqueue() below will wait for that to complete.
9635 destroy_workqueue(md_misc_wq
);
9636 destroy_workqueue(md_wq
);
9639 subsys_initcall(md_init
);
9640 module_exit(md_exit
)
9642 static int get_ro(char *buffer
, const struct kernel_param
*kp
)
9644 return sprintf(buffer
, "%d", start_readonly
);
9646 static int set_ro(const char *val
, const struct kernel_param
*kp
)
9648 return kstrtouint(val
, 10, (unsigned int *)&start_readonly
);
9651 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
9652 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
9653 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
9654 module_param(create_on_open
, bool, S_IRUSR
|S_IWUSR
);
9656 MODULE_LICENSE("GPL");
9657 MODULE_DESCRIPTION("MD RAID framework");
9659 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);