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 void rdev_uninit_serial(struct md_rdev
*rdev
)
130 if (!test_and_clear_bit(CollisionCheck
, &rdev
->flags
))
133 kvfree(rdev
->serial
);
137 static void rdevs_uninit_serial(struct mddev
*mddev
)
139 struct md_rdev
*rdev
;
141 rdev_for_each(rdev
, mddev
)
142 rdev_uninit_serial(rdev
);
145 static int rdev_init_serial(struct md_rdev
*rdev
)
147 /* serial_nums equals with BARRIER_BUCKETS_NR */
148 int i
, serial_nums
= 1 << ((PAGE_SHIFT
- ilog2(sizeof(atomic_t
))));
149 struct serial_in_rdev
*serial
= NULL
;
151 if (test_bit(CollisionCheck
, &rdev
->flags
))
154 serial
= kvmalloc(sizeof(struct serial_in_rdev
) * serial_nums
,
159 for (i
= 0; i
< serial_nums
; i
++) {
160 struct serial_in_rdev
*serial_tmp
= &serial
[i
];
162 spin_lock_init(&serial_tmp
->serial_lock
);
163 serial_tmp
->serial_rb
= RB_ROOT_CACHED
;
164 init_waitqueue_head(&serial_tmp
->serial_io_wait
);
167 rdev
->serial
= serial
;
168 set_bit(CollisionCheck
, &rdev
->flags
);
173 static int rdevs_init_serial(struct mddev
*mddev
)
175 struct md_rdev
*rdev
;
178 rdev_for_each(rdev
, mddev
) {
179 ret
= rdev_init_serial(rdev
);
184 /* Free all resources if pool is not existed */
185 if (ret
&& !mddev
->serial_info_pool
)
186 rdevs_uninit_serial(mddev
);
192 * rdev needs to enable serial stuffs if it meets the conditions:
193 * 1. it is multi-queue device flaged with writemostly.
194 * 2. the write-behind mode is enabled.
196 static int rdev_need_serial(struct md_rdev
*rdev
)
198 return (rdev
&& rdev
->mddev
->bitmap_info
.max_write_behind
> 0 &&
199 rdev
->bdev
->bd_queue
->nr_hw_queues
!= 1 &&
200 test_bit(WriteMostly
, &rdev
->flags
));
204 * Init resource for rdev(s), then create serial_info_pool if:
205 * 1. rdev is the first device which return true from rdev_enable_serial.
206 * 2. rdev is NULL, means we want to enable serialization for all rdevs.
208 void mddev_create_serial_pool(struct mddev
*mddev
, struct md_rdev
*rdev
,
213 if (rdev
&& !rdev_need_serial(rdev
) &&
214 !test_bit(CollisionCheck
, &rdev
->flags
))
218 mddev_suspend(mddev
);
221 ret
= rdevs_init_serial(mddev
);
223 ret
= rdev_init_serial(rdev
);
227 if (mddev
->serial_info_pool
== NULL
) {
228 unsigned int noio_flag
;
230 noio_flag
= memalloc_noio_save();
231 mddev
->serial_info_pool
=
232 mempool_create_kmalloc_pool(NR_SERIAL_INFOS
,
233 sizeof(struct serial_info
));
234 memalloc_noio_restore(noio_flag
);
235 if (!mddev
->serial_info_pool
) {
236 rdevs_uninit_serial(mddev
);
237 pr_err("can't alloc memory pool for serialization\n");
247 * Free resource from rdev(s), and destroy serial_info_pool under conditions:
248 * 1. rdev is the last device flaged with CollisionCheck.
249 * 2. when bitmap is destroyed while policy is not enabled.
250 * 3. for disable policy, the pool is destroyed only when no rdev needs it.
252 void mddev_destroy_serial_pool(struct mddev
*mddev
, struct md_rdev
*rdev
,
255 if (rdev
&& !test_bit(CollisionCheck
, &rdev
->flags
))
258 if (mddev
->serial_info_pool
) {
259 struct md_rdev
*temp
;
260 int num
= 0; /* used to track if other rdevs need the pool */
263 mddev_suspend(mddev
);
264 rdev_for_each(temp
, mddev
) {
266 if (!mddev
->serialize_policy
||
267 !rdev_need_serial(temp
))
268 rdev_uninit_serial(temp
);
271 } else if (temp
!= rdev
&&
272 test_bit(CollisionCheck
, &temp
->flags
))
277 rdev_uninit_serial(rdev
);
280 pr_info("The mempool could be used by other devices\n");
282 mempool_destroy(mddev
->serial_info_pool
);
283 mddev
->serial_info_pool
= NULL
;
290 static struct ctl_table_header
*raid_table_header
;
292 static struct ctl_table raid_table
[] = {
294 .procname
= "speed_limit_min",
295 .data
= &sysctl_speed_limit_min
,
296 .maxlen
= sizeof(int),
297 .mode
= S_IRUGO
|S_IWUSR
,
298 .proc_handler
= proc_dointvec
,
301 .procname
= "speed_limit_max",
302 .data
= &sysctl_speed_limit_max
,
303 .maxlen
= sizeof(int),
304 .mode
= S_IRUGO
|S_IWUSR
,
305 .proc_handler
= proc_dointvec
,
310 static struct ctl_table raid_dir_table
[] = {
314 .mode
= S_IRUGO
|S_IXUGO
,
320 static struct ctl_table raid_root_table
[] = {
325 .child
= raid_dir_table
,
330 static const struct block_device_operations md_fops
;
332 static int start_readonly
;
335 * The original mechanism for creating an md device is to create
336 * a device node in /dev and to open it. This causes races with device-close.
337 * The preferred method is to write to the "new_array" module parameter.
338 * This can avoid races.
339 * Setting create_on_open to false disables the original mechanism
340 * so all the races disappear.
342 static bool create_on_open
= true;
344 struct bio
*bio_alloc_mddev(gfp_t gfp_mask
, int nr_iovecs
,
347 if (!mddev
|| !bioset_initialized(&mddev
->bio_set
))
348 return bio_alloc(gfp_mask
, nr_iovecs
);
350 return bio_alloc_bioset(gfp_mask
, nr_iovecs
, &mddev
->bio_set
);
352 EXPORT_SYMBOL_GPL(bio_alloc_mddev
);
354 static struct bio
*md_bio_alloc_sync(struct mddev
*mddev
)
356 if (!mddev
|| !bioset_initialized(&mddev
->sync_set
))
357 return bio_alloc(GFP_NOIO
, 1);
359 return bio_alloc_bioset(GFP_NOIO
, 1, &mddev
->sync_set
);
363 * We have a system wide 'event count' that is incremented
364 * on any 'interesting' event, and readers of /proc/mdstat
365 * can use 'poll' or 'select' to find out when the event
369 * start array, stop array, error, add device, remove device,
370 * start build, activate spare
372 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters
);
373 static atomic_t md_event_count
;
374 void md_new_event(struct mddev
*mddev
)
376 atomic_inc(&md_event_count
);
377 wake_up(&md_event_waiters
);
379 EXPORT_SYMBOL_GPL(md_new_event
);
382 * Enables to iterate over all existing md arrays
383 * all_mddevs_lock protects this list.
385 static LIST_HEAD(all_mddevs
);
386 static DEFINE_SPINLOCK(all_mddevs_lock
);
389 * iterates through all used mddevs in the system.
390 * We take care to grab the all_mddevs_lock whenever navigating
391 * the list, and to always hold a refcount when unlocked.
392 * Any code which breaks out of this loop while own
393 * a reference to the current mddev and must mddev_put it.
395 #define for_each_mddev(_mddev,_tmp) \
397 for (({ spin_lock(&all_mddevs_lock); \
398 _tmp = all_mddevs.next; \
400 ({ if (_tmp != &all_mddevs) \
401 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
402 spin_unlock(&all_mddevs_lock); \
403 if (_mddev) mddev_put(_mddev); \
404 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
405 _tmp != &all_mddevs;}); \
406 ({ spin_lock(&all_mddevs_lock); \
407 _tmp = _tmp->next;}) \
410 /* Rather than calling directly into the personality make_request function,
411 * IO requests come here first so that we can check if the device is
412 * being suspended pending a reconfiguration.
413 * We hold a refcount over the call to ->make_request. By the time that
414 * call has finished, the bio has been linked into some internal structure
415 * and so is visible to ->quiesce(), so we don't need the refcount any more.
417 static bool is_suspended(struct mddev
*mddev
, struct bio
*bio
)
419 if (mddev
->suspended
)
421 if (bio_data_dir(bio
) != WRITE
)
423 if (mddev
->suspend_lo
>= mddev
->suspend_hi
)
425 if (bio
->bi_iter
.bi_sector
>= mddev
->suspend_hi
)
427 if (bio_end_sector(bio
) < mddev
->suspend_lo
)
432 void md_handle_request(struct mddev
*mddev
, struct bio
*bio
)
436 if (is_suspended(mddev
, bio
)) {
439 prepare_to_wait(&mddev
->sb_wait
, &__wait
,
440 TASK_UNINTERRUPTIBLE
);
441 if (!is_suspended(mddev
, bio
))
447 finish_wait(&mddev
->sb_wait
, &__wait
);
449 atomic_inc(&mddev
->active_io
);
452 if (!mddev
->pers
->make_request(mddev
, bio
)) {
453 atomic_dec(&mddev
->active_io
);
454 wake_up(&mddev
->sb_wait
);
455 goto check_suspended
;
458 if (atomic_dec_and_test(&mddev
->active_io
) && mddev
->suspended
)
459 wake_up(&mddev
->sb_wait
);
461 EXPORT_SYMBOL(md_handle_request
);
463 static blk_qc_t
md_make_request(struct request_queue
*q
, struct bio
*bio
)
465 const int rw
= bio_data_dir(bio
);
466 const int sgrp
= op_stat_group(bio_op(bio
));
467 struct mddev
*mddev
= q
->queuedata
;
468 unsigned int sectors
;
470 if (unlikely(test_bit(MD_BROKEN
, &mddev
->flags
)) && (rw
== WRITE
)) {
472 return BLK_QC_T_NONE
;
475 blk_queue_split(q
, &bio
);
477 if (mddev
== NULL
|| mddev
->pers
== NULL
) {
479 return BLK_QC_T_NONE
;
481 if (mddev
->ro
== 1 && unlikely(rw
== WRITE
)) {
482 if (bio_sectors(bio
) != 0)
483 bio
->bi_status
= BLK_STS_IOERR
;
485 return BLK_QC_T_NONE
;
489 * save the sectors now since our bio can
490 * go away inside make_request
492 sectors
= bio_sectors(bio
);
493 /* bio could be mergeable after passing to underlayer */
494 bio
->bi_opf
&= ~REQ_NOMERGE
;
496 md_handle_request(mddev
, bio
);
499 part_stat_inc(&mddev
->gendisk
->part0
, ios
[sgrp
]);
500 part_stat_add(&mddev
->gendisk
->part0
, sectors
[sgrp
], sectors
);
503 return BLK_QC_T_NONE
;
506 /* mddev_suspend makes sure no new requests are submitted
507 * to the device, and that any requests that have been submitted
508 * are completely handled.
509 * Once mddev_detach() is called and completes, the module will be
512 void mddev_suspend(struct mddev
*mddev
)
514 WARN_ON_ONCE(mddev
->thread
&& current
== mddev
->thread
->tsk
);
515 lockdep_assert_held(&mddev
->reconfig_mutex
);
516 if (mddev
->suspended
++)
519 wake_up(&mddev
->sb_wait
);
520 set_bit(MD_ALLOW_SB_UPDATE
, &mddev
->flags
);
521 smp_mb__after_atomic();
522 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->active_io
) == 0);
523 mddev
->pers
->quiesce(mddev
, 1);
524 clear_bit_unlock(MD_ALLOW_SB_UPDATE
, &mddev
->flags
);
525 wait_event(mddev
->sb_wait
, !test_bit(MD_UPDATING_SB
, &mddev
->flags
));
527 del_timer_sync(&mddev
->safemode_timer
);
529 EXPORT_SYMBOL_GPL(mddev_suspend
);
531 void mddev_resume(struct mddev
*mddev
)
533 lockdep_assert_held(&mddev
->reconfig_mutex
);
534 if (--mddev
->suspended
)
536 wake_up(&mddev
->sb_wait
);
537 mddev
->pers
->quiesce(mddev
, 0);
539 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
540 md_wakeup_thread(mddev
->thread
);
541 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
543 EXPORT_SYMBOL_GPL(mddev_resume
);
545 int mddev_congested(struct mddev
*mddev
, int bits
)
547 struct md_personality
*pers
= mddev
->pers
;
551 if (mddev
->suspended
)
553 else if (pers
&& pers
->congested
)
554 ret
= pers
->congested(mddev
, bits
);
558 EXPORT_SYMBOL_GPL(mddev_congested
);
559 static int md_congested(void *data
, int bits
)
561 struct mddev
*mddev
= data
;
562 return mddev_congested(mddev
, bits
);
566 * Generic flush handling for md
569 static void md_end_flush(struct bio
*bio
)
571 struct md_rdev
*rdev
= bio
->bi_private
;
572 struct mddev
*mddev
= rdev
->mddev
;
574 rdev_dec_pending(rdev
, mddev
);
576 if (atomic_dec_and_test(&mddev
->flush_pending
)) {
577 /* The pre-request flush has finished */
578 queue_work(md_wq
, &mddev
->flush_work
);
583 static void md_submit_flush_data(struct work_struct
*ws
);
585 static void submit_flushes(struct work_struct
*ws
)
587 struct mddev
*mddev
= container_of(ws
, struct mddev
, flush_work
);
588 struct md_rdev
*rdev
;
590 mddev
->start_flush
= ktime_get_boottime();
591 INIT_WORK(&mddev
->flush_work
, md_submit_flush_data
);
592 atomic_set(&mddev
->flush_pending
, 1);
594 rdev_for_each_rcu(rdev
, mddev
)
595 if (rdev
->raid_disk
>= 0 &&
596 !test_bit(Faulty
, &rdev
->flags
)) {
597 /* Take two references, one is dropped
598 * when request finishes, one after
599 * we reclaim rcu_read_lock
602 atomic_inc(&rdev
->nr_pending
);
603 atomic_inc(&rdev
->nr_pending
);
605 bi
= bio_alloc_mddev(GFP_NOIO
, 0, mddev
);
606 bi
->bi_end_io
= md_end_flush
;
607 bi
->bi_private
= rdev
;
608 bio_set_dev(bi
, rdev
->bdev
);
609 bi
->bi_opf
= REQ_OP_WRITE
| REQ_PREFLUSH
;
610 atomic_inc(&mddev
->flush_pending
);
613 rdev_dec_pending(rdev
, mddev
);
616 if (atomic_dec_and_test(&mddev
->flush_pending
))
617 queue_work(md_wq
, &mddev
->flush_work
);
620 static void md_submit_flush_data(struct work_struct
*ws
)
622 struct mddev
*mddev
= container_of(ws
, struct mddev
, flush_work
);
623 struct bio
*bio
= mddev
->flush_bio
;
626 * must reset flush_bio before calling into md_handle_request to avoid a
627 * deadlock, because other bios passed md_handle_request suspend check
628 * could wait for this and below md_handle_request could wait for those
629 * bios because of suspend check
631 mddev
->last_flush
= mddev
->start_flush
;
632 mddev
->flush_bio
= NULL
;
633 wake_up(&mddev
->sb_wait
);
635 if (bio
->bi_iter
.bi_size
== 0) {
636 /* an empty barrier - all done */
639 bio
->bi_opf
&= ~REQ_PREFLUSH
;
640 md_handle_request(mddev
, bio
);
645 * Manages consolidation of flushes and submitting any flushes needed for
646 * a bio with REQ_PREFLUSH. Returns true if the bio is finished or is
647 * being finished in another context. Returns false if the flushing is
648 * complete but still needs the I/O portion of the bio to be processed.
650 bool md_flush_request(struct mddev
*mddev
, struct bio
*bio
)
652 ktime_t start
= ktime_get_boottime();
653 spin_lock_irq(&mddev
->lock
);
654 wait_event_lock_irq(mddev
->sb_wait
,
656 ktime_after(mddev
->last_flush
, start
),
658 if (!ktime_after(mddev
->last_flush
, start
)) {
659 WARN_ON(mddev
->flush_bio
);
660 mddev
->flush_bio
= bio
;
663 spin_unlock_irq(&mddev
->lock
);
666 INIT_WORK(&mddev
->flush_work
, submit_flushes
);
667 queue_work(md_wq
, &mddev
->flush_work
);
669 /* flush was performed for some other bio while we waited. */
670 if (bio
->bi_iter
.bi_size
== 0)
671 /* an empty barrier - all done */
674 bio
->bi_opf
&= ~REQ_PREFLUSH
;
680 EXPORT_SYMBOL(md_flush_request
);
682 static inline struct mddev
*mddev_get(struct mddev
*mddev
)
684 atomic_inc(&mddev
->active
);
688 static void mddev_delayed_delete(struct work_struct
*ws
);
690 static void mddev_put(struct mddev
*mddev
)
692 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
694 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
) &&
695 mddev
->ctime
== 0 && !mddev
->hold_active
) {
696 /* Array is not configured at all, and not held active,
698 list_del_init(&mddev
->all_mddevs
);
701 * Call queue_work inside the spinlock so that
702 * flush_workqueue() after mddev_find will succeed in waiting
703 * for the work to be done.
705 INIT_WORK(&mddev
->del_work
, mddev_delayed_delete
);
706 queue_work(md_misc_wq
, &mddev
->del_work
);
708 spin_unlock(&all_mddevs_lock
);
711 static void md_safemode_timeout(struct timer_list
*t
);
713 void mddev_init(struct mddev
*mddev
)
715 kobject_init(&mddev
->kobj
, &md_ktype
);
716 mutex_init(&mddev
->open_mutex
);
717 mutex_init(&mddev
->reconfig_mutex
);
718 mutex_init(&mddev
->bitmap_info
.mutex
);
719 INIT_LIST_HEAD(&mddev
->disks
);
720 INIT_LIST_HEAD(&mddev
->all_mddevs
);
721 timer_setup(&mddev
->safemode_timer
, md_safemode_timeout
, 0);
722 atomic_set(&mddev
->active
, 1);
723 atomic_set(&mddev
->openers
, 0);
724 atomic_set(&mddev
->active_io
, 0);
725 spin_lock_init(&mddev
->lock
);
726 atomic_set(&mddev
->flush_pending
, 0);
727 init_waitqueue_head(&mddev
->sb_wait
);
728 init_waitqueue_head(&mddev
->recovery_wait
);
729 mddev
->reshape_position
= MaxSector
;
730 mddev
->reshape_backwards
= 0;
731 mddev
->last_sync_action
= "none";
732 mddev
->resync_min
= 0;
733 mddev
->resync_max
= MaxSector
;
734 mddev
->level
= LEVEL_NONE
;
736 EXPORT_SYMBOL_GPL(mddev_init
);
738 static struct mddev
*mddev_find(dev_t unit
)
740 struct mddev
*mddev
, *new = NULL
;
742 if (unit
&& MAJOR(unit
) != MD_MAJOR
)
743 unit
&= ~((1<<MdpMinorShift
)-1);
746 spin_lock(&all_mddevs_lock
);
749 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
750 if (mddev
->unit
== unit
) {
752 spin_unlock(&all_mddevs_lock
);
758 list_add(&new->all_mddevs
, &all_mddevs
);
759 spin_unlock(&all_mddevs_lock
);
760 new->hold_active
= UNTIL_IOCTL
;
764 /* find an unused unit number */
765 static int next_minor
= 512;
766 int start
= next_minor
;
770 dev
= MKDEV(MD_MAJOR
, next_minor
);
772 if (next_minor
> MINORMASK
)
774 if (next_minor
== start
) {
775 /* Oh dear, all in use. */
776 spin_unlock(&all_mddevs_lock
);
782 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
783 if (mddev
->unit
== dev
) {
789 new->md_minor
= MINOR(dev
);
790 new->hold_active
= UNTIL_STOP
;
791 list_add(&new->all_mddevs
, &all_mddevs
);
792 spin_unlock(&all_mddevs_lock
);
795 spin_unlock(&all_mddevs_lock
);
797 new = kzalloc(sizeof(*new), GFP_KERNEL
);
802 if (MAJOR(unit
) == MD_MAJOR
)
803 new->md_minor
= MINOR(unit
);
805 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
812 static struct attribute_group md_redundancy_group
;
814 void mddev_unlock(struct mddev
*mddev
)
816 if (mddev
->to_remove
) {
817 /* These cannot be removed under reconfig_mutex as
818 * an access to the files will try to take reconfig_mutex
819 * while holding the file unremovable, which leads to
821 * So hold set sysfs_active while the remove in happeing,
822 * and anything else which might set ->to_remove or my
823 * otherwise change the sysfs namespace will fail with
824 * -EBUSY if sysfs_active is still set.
825 * We set sysfs_active under reconfig_mutex and elsewhere
826 * test it under the same mutex to ensure its correct value
829 struct attribute_group
*to_remove
= mddev
->to_remove
;
830 mddev
->to_remove
= NULL
;
831 mddev
->sysfs_active
= 1;
832 mutex_unlock(&mddev
->reconfig_mutex
);
834 if (mddev
->kobj
.sd
) {
835 if (to_remove
!= &md_redundancy_group
)
836 sysfs_remove_group(&mddev
->kobj
, to_remove
);
837 if (mddev
->pers
== NULL
||
838 mddev
->pers
->sync_request
== NULL
) {
839 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
840 if (mddev
->sysfs_action
)
841 sysfs_put(mddev
->sysfs_action
);
842 mddev
->sysfs_action
= NULL
;
845 mddev
->sysfs_active
= 0;
847 mutex_unlock(&mddev
->reconfig_mutex
);
849 /* As we've dropped the mutex we need a spinlock to
850 * make sure the thread doesn't disappear
852 spin_lock(&pers_lock
);
853 md_wakeup_thread(mddev
->thread
);
854 wake_up(&mddev
->sb_wait
);
855 spin_unlock(&pers_lock
);
857 EXPORT_SYMBOL_GPL(mddev_unlock
);
859 struct md_rdev
*md_find_rdev_nr_rcu(struct mddev
*mddev
, int nr
)
861 struct md_rdev
*rdev
;
863 rdev_for_each_rcu(rdev
, mddev
)
864 if (rdev
->desc_nr
== nr
)
869 EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu
);
871 static struct md_rdev
*find_rdev(struct mddev
*mddev
, dev_t dev
)
873 struct md_rdev
*rdev
;
875 rdev_for_each(rdev
, mddev
)
876 if (rdev
->bdev
->bd_dev
== dev
)
882 struct md_rdev
*md_find_rdev_rcu(struct mddev
*mddev
, dev_t dev
)
884 struct md_rdev
*rdev
;
886 rdev_for_each_rcu(rdev
, mddev
)
887 if (rdev
->bdev
->bd_dev
== dev
)
892 EXPORT_SYMBOL_GPL(md_find_rdev_rcu
);
894 static struct md_personality
*find_pers(int level
, char *clevel
)
896 struct md_personality
*pers
;
897 list_for_each_entry(pers
, &pers_list
, list
) {
898 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
900 if (strcmp(pers
->name
, clevel
)==0)
906 /* return the offset of the super block in 512byte sectors */
907 static inline sector_t
calc_dev_sboffset(struct md_rdev
*rdev
)
909 sector_t num_sectors
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
910 return MD_NEW_SIZE_SECTORS(num_sectors
);
913 static int alloc_disk_sb(struct md_rdev
*rdev
)
915 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
921 void md_rdev_clear(struct md_rdev
*rdev
)
924 put_page(rdev
->sb_page
);
926 rdev
->sb_page
= NULL
;
931 put_page(rdev
->bb_page
);
932 rdev
->bb_page
= NULL
;
934 badblocks_exit(&rdev
->badblocks
);
936 EXPORT_SYMBOL_GPL(md_rdev_clear
);
938 static void super_written(struct bio
*bio
)
940 struct md_rdev
*rdev
= bio
->bi_private
;
941 struct mddev
*mddev
= rdev
->mddev
;
943 if (bio
->bi_status
) {
944 pr_err("md: super_written gets error=%d\n", bio
->bi_status
);
945 md_error(mddev
, rdev
);
946 if (!test_bit(Faulty
, &rdev
->flags
)
947 && (bio
->bi_opf
& MD_FAILFAST
)) {
948 set_bit(MD_SB_NEED_REWRITE
, &mddev
->sb_flags
);
949 set_bit(LastDev
, &rdev
->flags
);
952 clear_bit(LastDev
, &rdev
->flags
);
954 if (atomic_dec_and_test(&mddev
->pending_writes
))
955 wake_up(&mddev
->sb_wait
);
956 rdev_dec_pending(rdev
, mddev
);
960 void md_super_write(struct mddev
*mddev
, struct md_rdev
*rdev
,
961 sector_t sector
, int size
, struct page
*page
)
963 /* write first size bytes of page to sector of rdev
964 * Increment mddev->pending_writes before returning
965 * and decrement it on completion, waking up sb_wait
966 * if zero is reached.
967 * If an error occurred, call md_error
975 if (test_bit(Faulty
, &rdev
->flags
))
978 bio
= md_bio_alloc_sync(mddev
);
980 atomic_inc(&rdev
->nr_pending
);
982 bio_set_dev(bio
, rdev
->meta_bdev
? rdev
->meta_bdev
: rdev
->bdev
);
983 bio
->bi_iter
.bi_sector
= sector
;
984 bio_add_page(bio
, page
, size
, 0);
985 bio
->bi_private
= rdev
;
986 bio
->bi_end_io
= super_written
;
988 if (test_bit(MD_FAILFAST_SUPPORTED
, &mddev
->flags
) &&
989 test_bit(FailFast
, &rdev
->flags
) &&
990 !test_bit(LastDev
, &rdev
->flags
))
992 bio
->bi_opf
= REQ_OP_WRITE
| REQ_SYNC
| REQ_PREFLUSH
| REQ_FUA
| ff
;
994 atomic_inc(&mddev
->pending_writes
);
998 int md_super_wait(struct mddev
*mddev
)
1000 /* wait for all superblock writes that were scheduled to complete */
1001 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->pending_writes
)==0);
1002 if (test_and_clear_bit(MD_SB_NEED_REWRITE
, &mddev
->sb_flags
))
1007 int sync_page_io(struct md_rdev
*rdev
, sector_t sector
, int size
,
1008 struct page
*page
, int op
, int op_flags
, bool metadata_op
)
1010 struct bio
*bio
= md_bio_alloc_sync(rdev
->mddev
);
1013 if (metadata_op
&& rdev
->meta_bdev
)
1014 bio_set_dev(bio
, rdev
->meta_bdev
);
1016 bio_set_dev(bio
, rdev
->bdev
);
1017 bio_set_op_attrs(bio
, op
, op_flags
);
1019 bio
->bi_iter
.bi_sector
= sector
+ rdev
->sb_start
;
1020 else if (rdev
->mddev
->reshape_position
!= MaxSector
&&
1021 (rdev
->mddev
->reshape_backwards
==
1022 (sector
>= rdev
->mddev
->reshape_position
)))
1023 bio
->bi_iter
.bi_sector
= sector
+ rdev
->new_data_offset
;
1025 bio
->bi_iter
.bi_sector
= sector
+ rdev
->data_offset
;
1026 bio_add_page(bio
, page
, size
, 0);
1028 submit_bio_wait(bio
);
1030 ret
= !bio
->bi_status
;
1034 EXPORT_SYMBOL_GPL(sync_page_io
);
1036 static int read_disk_sb(struct md_rdev
*rdev
, int size
)
1038 char b
[BDEVNAME_SIZE
];
1040 if (rdev
->sb_loaded
)
1043 if (!sync_page_io(rdev
, 0, size
, rdev
->sb_page
, REQ_OP_READ
, 0, true))
1045 rdev
->sb_loaded
= 1;
1049 pr_err("md: disabled device %s, could not read superblock.\n",
1050 bdevname(rdev
->bdev
,b
));
1054 static int md_uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
1056 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
1057 sb1
->set_uuid1
== sb2
->set_uuid1
&&
1058 sb1
->set_uuid2
== sb2
->set_uuid2
&&
1059 sb1
->set_uuid3
== sb2
->set_uuid3
;
1062 static int md_sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
1065 mdp_super_t
*tmp1
, *tmp2
;
1067 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
1068 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
1070 if (!tmp1
|| !tmp2
) {
1079 * nr_disks is not constant
1084 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
1091 static u32
md_csum_fold(u32 csum
)
1093 csum
= (csum
& 0xffff) + (csum
>> 16);
1094 return (csum
& 0xffff) + (csum
>> 16);
1097 static unsigned int calc_sb_csum(mdp_super_t
*sb
)
1100 u32
*sb32
= (u32
*)sb
;
1102 unsigned int disk_csum
, csum
;
1104 disk_csum
= sb
->sb_csum
;
1107 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
1109 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
1112 /* This used to use csum_partial, which was wrong for several
1113 * reasons including that different results are returned on
1114 * different architectures. It isn't critical that we get exactly
1115 * the same return value as before (we always csum_fold before
1116 * testing, and that removes any differences). However as we
1117 * know that csum_partial always returned a 16bit value on
1118 * alphas, do a fold to maximise conformity to previous behaviour.
1120 sb
->sb_csum
= md_csum_fold(disk_csum
);
1122 sb
->sb_csum
= disk_csum
;
1128 * Handle superblock details.
1129 * We want to be able to handle multiple superblock formats
1130 * so we have a common interface to them all, and an array of
1131 * different handlers.
1132 * We rely on user-space to write the initial superblock, and support
1133 * reading and updating of superblocks.
1134 * Interface methods are:
1135 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
1136 * loads and validates a superblock on dev.
1137 * if refdev != NULL, compare superblocks on both devices
1139 * 0 - dev has a superblock that is compatible with refdev
1140 * 1 - dev has a superblock that is compatible and newer than refdev
1141 * so dev should be used as the refdev in future
1142 * -EINVAL superblock incompatible or invalid
1143 * -othererror e.g. -EIO
1145 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
1146 * Verify that dev is acceptable into mddev.
1147 * The first time, mddev->raid_disks will be 0, and data from
1148 * dev should be merged in. Subsequent calls check that dev
1149 * is new enough. Return 0 or -EINVAL
1151 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
1152 * Update the superblock for rdev with data in mddev
1153 * This does not write to disc.
1159 struct module
*owner
;
1160 int (*load_super
)(struct md_rdev
*rdev
,
1161 struct md_rdev
*refdev
,
1163 int (*validate_super
)(struct mddev
*mddev
,
1164 struct md_rdev
*rdev
);
1165 void (*sync_super
)(struct mddev
*mddev
,
1166 struct md_rdev
*rdev
);
1167 unsigned long long (*rdev_size_change
)(struct md_rdev
*rdev
,
1168 sector_t num_sectors
);
1169 int (*allow_new_offset
)(struct md_rdev
*rdev
,
1170 unsigned long long new_offset
);
1174 * Check that the given mddev has no bitmap.
1176 * This function is called from the run method of all personalities that do not
1177 * support bitmaps. It prints an error message and returns non-zero if mddev
1178 * has a bitmap. Otherwise, it returns 0.
1181 int md_check_no_bitmap(struct mddev
*mddev
)
1183 if (!mddev
->bitmap_info
.file
&& !mddev
->bitmap_info
.offset
)
1185 pr_warn("%s: bitmaps are not supported for %s\n",
1186 mdname(mddev
), mddev
->pers
->name
);
1189 EXPORT_SYMBOL(md_check_no_bitmap
);
1192 * load_super for 0.90.0
1194 static int super_90_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
1196 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1199 bool spare_disk
= true;
1202 * Calculate the position of the superblock (512byte sectors),
1203 * it's at the end of the disk.
1205 * It also happens to be a multiple of 4Kb.
1207 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1209 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
1215 bdevname(rdev
->bdev
, b
);
1216 sb
= page_address(rdev
->sb_page
);
1218 if (sb
->md_magic
!= MD_SB_MAGIC
) {
1219 pr_warn("md: invalid raid superblock magic on %s\n", b
);
1223 if (sb
->major_version
!= 0 ||
1224 sb
->minor_version
< 90 ||
1225 sb
->minor_version
> 91) {
1226 pr_warn("Bad version number %d.%d on %s\n",
1227 sb
->major_version
, sb
->minor_version
, b
);
1231 if (sb
->raid_disks
<= 0)
1234 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
1235 pr_warn("md: invalid superblock checksum on %s\n", b
);
1239 rdev
->preferred_minor
= sb
->md_minor
;
1240 rdev
->data_offset
= 0;
1241 rdev
->new_data_offset
= 0;
1242 rdev
->sb_size
= MD_SB_BYTES
;
1243 rdev
->badblocks
.shift
= -1;
1245 if (sb
->level
== LEVEL_MULTIPATH
)
1248 rdev
->desc_nr
= sb
->this_disk
.number
;
1250 /* not spare disk, or LEVEL_MULTIPATH */
1251 if (sb
->level
== LEVEL_MULTIPATH
||
1252 (rdev
->desc_nr
>= 0 &&
1253 rdev
->desc_nr
< MD_SB_DISKS
&&
1254 sb
->disks
[rdev
->desc_nr
].state
&
1255 ((1<<MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
))))
1265 mdp_super_t
*refsb
= page_address(refdev
->sb_page
);
1266 if (!md_uuid_equal(refsb
, sb
)) {
1267 pr_warn("md: %s has different UUID to %s\n",
1268 b
, bdevname(refdev
->bdev
,b2
));
1271 if (!md_sb_equal(refsb
, sb
)) {
1272 pr_warn("md: %s has same UUID but different superblock to %s\n",
1273 b
, bdevname(refdev
->bdev
, b2
));
1277 ev2
= md_event(refsb
);
1279 if (!spare_disk
&& ev1
> ev2
)
1284 rdev
->sectors
= rdev
->sb_start
;
1285 /* Limit to 4TB as metadata cannot record more than that.
1286 * (not needed for Linear and RAID0 as metadata doesn't
1289 if ((u64
)rdev
->sectors
>= (2ULL << 32) && sb
->level
>= 1)
1290 rdev
->sectors
= (sector_t
)(2ULL << 32) - 2;
1292 if (rdev
->sectors
< ((sector_t
)sb
->size
) * 2 && sb
->level
>= 1)
1293 /* "this cannot possibly happen" ... */
1301 * validate_super for 0.90.0
1303 static int super_90_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
1306 mdp_super_t
*sb
= page_address(rdev
->sb_page
);
1307 __u64 ev1
= md_event(sb
);
1309 rdev
->raid_disk
= -1;
1310 clear_bit(Faulty
, &rdev
->flags
);
1311 clear_bit(In_sync
, &rdev
->flags
);
1312 clear_bit(Bitmap_sync
, &rdev
->flags
);
1313 clear_bit(WriteMostly
, &rdev
->flags
);
1315 if (mddev
->raid_disks
== 0) {
1316 mddev
->major_version
= 0;
1317 mddev
->minor_version
= sb
->minor_version
;
1318 mddev
->patch_version
= sb
->patch_version
;
1319 mddev
->external
= 0;
1320 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
1321 mddev
->ctime
= sb
->ctime
;
1322 mddev
->utime
= sb
->utime
;
1323 mddev
->level
= sb
->level
;
1324 mddev
->clevel
[0] = 0;
1325 mddev
->layout
= sb
->layout
;
1326 mddev
->raid_disks
= sb
->raid_disks
;
1327 mddev
->dev_sectors
= ((sector_t
)sb
->size
) * 2;
1328 mddev
->events
= ev1
;
1329 mddev
->bitmap_info
.offset
= 0;
1330 mddev
->bitmap_info
.space
= 0;
1331 /* bitmap can use 60 K after the 4K superblocks */
1332 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
1333 mddev
->bitmap_info
.default_space
= 64*2 - (MD_SB_BYTES
>> 9);
1334 mddev
->reshape_backwards
= 0;
1336 if (mddev
->minor_version
>= 91) {
1337 mddev
->reshape_position
= sb
->reshape_position
;
1338 mddev
->delta_disks
= sb
->delta_disks
;
1339 mddev
->new_level
= sb
->new_level
;
1340 mddev
->new_layout
= sb
->new_layout
;
1341 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
1342 if (mddev
->delta_disks
< 0)
1343 mddev
->reshape_backwards
= 1;
1345 mddev
->reshape_position
= MaxSector
;
1346 mddev
->delta_disks
= 0;
1347 mddev
->new_level
= mddev
->level
;
1348 mddev
->new_layout
= mddev
->layout
;
1349 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1351 if (mddev
->level
== 0)
1354 if (sb
->state
& (1<<MD_SB_CLEAN
))
1355 mddev
->recovery_cp
= MaxSector
;
1357 if (sb
->events_hi
== sb
->cp_events_hi
&&
1358 sb
->events_lo
== sb
->cp_events_lo
) {
1359 mddev
->recovery_cp
= sb
->recovery_cp
;
1361 mddev
->recovery_cp
= 0;
1364 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
1365 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
1366 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
1367 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
1369 mddev
->max_disks
= MD_SB_DISKS
;
1371 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
1372 mddev
->bitmap_info
.file
== NULL
) {
1373 mddev
->bitmap_info
.offset
=
1374 mddev
->bitmap_info
.default_offset
;
1375 mddev
->bitmap_info
.space
=
1376 mddev
->bitmap_info
.default_space
;
1379 } else if (mddev
->pers
== NULL
) {
1380 /* Insist on good event counter while assembling, except
1381 * for spares (which don't need an event count) */
1383 if (sb
->disks
[rdev
->desc_nr
].state
& (
1384 (1<<MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
)))
1385 if (ev1
< mddev
->events
)
1387 } else if (mddev
->bitmap
) {
1388 /* if adding to array with a bitmap, then we can accept an
1389 * older device ... but not too old.
1391 if (ev1
< mddev
->bitmap
->events_cleared
)
1393 if (ev1
< mddev
->events
)
1394 set_bit(Bitmap_sync
, &rdev
->flags
);
1396 if (ev1
< mddev
->events
)
1397 /* just a hot-add of a new device, leave raid_disk at -1 */
1401 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1402 desc
= sb
->disks
+ rdev
->desc_nr
;
1404 if (desc
->state
& (1<<MD_DISK_FAULTY
))
1405 set_bit(Faulty
, &rdev
->flags
);
1406 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
1407 desc->raid_disk < mddev->raid_disks */) {
1408 set_bit(In_sync
, &rdev
->flags
);
1409 rdev
->raid_disk
= desc
->raid_disk
;
1410 rdev
->saved_raid_disk
= desc
->raid_disk
;
1411 } else if (desc
->state
& (1<<MD_DISK_ACTIVE
)) {
1412 /* active but not in sync implies recovery up to
1413 * reshape position. We don't know exactly where
1414 * that is, so set to zero for now */
1415 if (mddev
->minor_version
>= 91) {
1416 rdev
->recovery_offset
= 0;
1417 rdev
->raid_disk
= desc
->raid_disk
;
1420 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
1421 set_bit(WriteMostly
, &rdev
->flags
);
1422 if (desc
->state
& (1<<MD_DISK_FAILFAST
))
1423 set_bit(FailFast
, &rdev
->flags
);
1424 } else /* MULTIPATH are always insync */
1425 set_bit(In_sync
, &rdev
->flags
);
1430 * sync_super for 0.90.0
1432 static void super_90_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
1435 struct md_rdev
*rdev2
;
1436 int next_spare
= mddev
->raid_disks
;
1438 /* make rdev->sb match mddev data..
1441 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1442 * 3/ any empty disks < next_spare become removed
1444 * disks[0] gets initialised to REMOVED because
1445 * we cannot be sure from other fields if it has
1446 * been initialised or not.
1449 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
1451 rdev
->sb_size
= MD_SB_BYTES
;
1453 sb
= page_address(rdev
->sb_page
);
1455 memset(sb
, 0, sizeof(*sb
));
1457 sb
->md_magic
= MD_SB_MAGIC
;
1458 sb
->major_version
= mddev
->major_version
;
1459 sb
->patch_version
= mddev
->patch_version
;
1460 sb
->gvalid_words
= 0; /* ignored */
1461 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
1462 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
1463 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
1464 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
1466 sb
->ctime
= clamp_t(time64_t
, mddev
->ctime
, 0, U32_MAX
);
1467 sb
->level
= mddev
->level
;
1468 sb
->size
= mddev
->dev_sectors
/ 2;
1469 sb
->raid_disks
= mddev
->raid_disks
;
1470 sb
->md_minor
= mddev
->md_minor
;
1471 sb
->not_persistent
= 0;
1472 sb
->utime
= clamp_t(time64_t
, mddev
->utime
, 0, U32_MAX
);
1474 sb
->events_hi
= (mddev
->events
>>32);
1475 sb
->events_lo
= (u32
)mddev
->events
;
1477 if (mddev
->reshape_position
== MaxSector
)
1478 sb
->minor_version
= 90;
1480 sb
->minor_version
= 91;
1481 sb
->reshape_position
= mddev
->reshape_position
;
1482 sb
->new_level
= mddev
->new_level
;
1483 sb
->delta_disks
= mddev
->delta_disks
;
1484 sb
->new_layout
= mddev
->new_layout
;
1485 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1487 mddev
->minor_version
= sb
->minor_version
;
1490 sb
->recovery_cp
= mddev
->recovery_cp
;
1491 sb
->cp_events_hi
= (mddev
->events
>>32);
1492 sb
->cp_events_lo
= (u32
)mddev
->events
;
1493 if (mddev
->recovery_cp
== MaxSector
)
1494 sb
->state
= (1<< MD_SB_CLEAN
);
1496 sb
->recovery_cp
= 0;
1498 sb
->layout
= mddev
->layout
;
1499 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1501 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
)
1502 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1504 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1505 rdev_for_each(rdev2
, mddev
) {
1508 int is_active
= test_bit(In_sync
, &rdev2
->flags
);
1510 if (rdev2
->raid_disk
>= 0 &&
1511 sb
->minor_version
>= 91)
1512 /* we have nowhere to store the recovery_offset,
1513 * but if it is not below the reshape_position,
1514 * we can piggy-back on that.
1517 if (rdev2
->raid_disk
< 0 ||
1518 test_bit(Faulty
, &rdev2
->flags
))
1521 desc_nr
= rdev2
->raid_disk
;
1523 desc_nr
= next_spare
++;
1524 rdev2
->desc_nr
= desc_nr
;
1525 d
= &sb
->disks
[rdev2
->desc_nr
];
1527 d
->number
= rdev2
->desc_nr
;
1528 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1529 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1531 d
->raid_disk
= rdev2
->raid_disk
;
1533 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1534 if (test_bit(Faulty
, &rdev2
->flags
))
1535 d
->state
= (1<<MD_DISK_FAULTY
);
1536 else if (is_active
) {
1537 d
->state
= (1<<MD_DISK_ACTIVE
);
1538 if (test_bit(In_sync
, &rdev2
->flags
))
1539 d
->state
|= (1<<MD_DISK_SYNC
);
1547 if (test_bit(WriteMostly
, &rdev2
->flags
))
1548 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1549 if (test_bit(FailFast
, &rdev2
->flags
))
1550 d
->state
|= (1<<MD_DISK_FAILFAST
);
1552 /* now set the "removed" and "faulty" bits on any missing devices */
1553 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1554 mdp_disk_t
*d
= &sb
->disks
[i
];
1555 if (d
->state
== 0 && d
->number
== 0) {
1558 d
->state
= (1<<MD_DISK_REMOVED
);
1559 d
->state
|= (1<<MD_DISK_FAULTY
);
1563 sb
->nr_disks
= nr_disks
;
1564 sb
->active_disks
= active
;
1565 sb
->working_disks
= working
;
1566 sb
->failed_disks
= failed
;
1567 sb
->spare_disks
= spare
;
1569 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1570 sb
->sb_csum
= calc_sb_csum(sb
);
1574 * rdev_size_change for 0.90.0
1576 static unsigned long long
1577 super_90_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
1579 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1580 return 0; /* component must fit device */
1581 if (rdev
->mddev
->bitmap_info
.offset
)
1582 return 0; /* can't move bitmap */
1583 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1584 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1585 num_sectors
= rdev
->sb_start
;
1586 /* Limit to 4TB as metadata cannot record more than that.
1587 * 4TB == 2^32 KB, or 2*2^32 sectors.
1589 if ((u64
)num_sectors
>= (2ULL << 32) && rdev
->mddev
->level
>= 1)
1590 num_sectors
= (sector_t
)(2ULL << 32) - 2;
1592 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1594 } while (md_super_wait(rdev
->mddev
) < 0);
1599 super_90_allow_new_offset(struct md_rdev
*rdev
, unsigned long long new_offset
)
1601 /* non-zero offset changes not possible with v0.90 */
1602 return new_offset
== 0;
1606 * version 1 superblock
1609 static __le32
calc_sb_1_csum(struct mdp_superblock_1
*sb
)
1613 unsigned long long newcsum
;
1614 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1615 __le32
*isuper
= (__le32
*)sb
;
1617 disk_csum
= sb
->sb_csum
;
1620 for (; size
>= 4; size
-= 4)
1621 newcsum
+= le32_to_cpu(*isuper
++);
1624 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1626 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1627 sb
->sb_csum
= disk_csum
;
1628 return cpu_to_le32(csum
);
1631 static int super_1_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
1633 struct mdp_superblock_1
*sb
;
1637 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1639 bool spare_disk
= true;
1642 * Calculate the position of the superblock in 512byte sectors.
1643 * It is always aligned to a 4K boundary and
1644 * depeding on minor_version, it can be:
1645 * 0: At least 8K, but less than 12K, from end of device
1646 * 1: At start of device
1647 * 2: 4K from start of device.
1649 switch(minor_version
) {
1651 sb_start
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1653 sb_start
&= ~(sector_t
)(4*2-1);
1664 rdev
->sb_start
= sb_start
;
1666 /* superblock is rarely larger than 1K, but it can be larger,
1667 * and it is safe to read 4k, so we do that
1669 ret
= read_disk_sb(rdev
, 4096);
1670 if (ret
) return ret
;
1672 sb
= page_address(rdev
->sb_page
);
1674 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1675 sb
->major_version
!= cpu_to_le32(1) ||
1676 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1677 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1678 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1681 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1682 pr_warn("md: invalid superblock checksum on %s\n",
1683 bdevname(rdev
->bdev
,b
));
1686 if (le64_to_cpu(sb
->data_size
) < 10) {
1687 pr_warn("md: data_size too small on %s\n",
1688 bdevname(rdev
->bdev
,b
));
1693 memcmp(sb
->pad3
, sb
->pad3
+1, sizeof(sb
->pad3
) - sizeof(sb
->pad3
[1])))
1694 /* Some padding is non-zero, might be a new feature */
1697 rdev
->preferred_minor
= 0xffff;
1698 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1699 rdev
->new_data_offset
= rdev
->data_offset
;
1700 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
) &&
1701 (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_NEW_OFFSET
))
1702 rdev
->new_data_offset
+= (s32
)le32_to_cpu(sb
->new_offset
);
1703 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1705 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1706 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1707 if (rdev
->sb_size
& bmask
)
1708 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1711 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1714 && rdev
->new_data_offset
< sb_start
+ (rdev
->sb_size
/512))
1717 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1720 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1722 if (!rdev
->bb_page
) {
1723 rdev
->bb_page
= alloc_page(GFP_KERNEL
);
1727 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BAD_BLOCKS
) &&
1728 rdev
->badblocks
.count
== 0) {
1729 /* need to load the bad block list.
1730 * Currently we limit it to one page.
1736 int sectors
= le16_to_cpu(sb
->bblog_size
);
1737 if (sectors
> (PAGE_SIZE
/ 512))
1739 offset
= le32_to_cpu(sb
->bblog_offset
);
1742 bb_sector
= (long long)offset
;
1743 if (!sync_page_io(rdev
, bb_sector
, sectors
<< 9,
1744 rdev
->bb_page
, REQ_OP_READ
, 0, true))
1746 bbp
= (__le64
*)page_address(rdev
->bb_page
);
1747 rdev
->badblocks
.shift
= sb
->bblog_shift
;
1748 for (i
= 0 ; i
< (sectors
<< (9-3)) ; i
++, bbp
++) {
1749 u64 bb
= le64_to_cpu(*bbp
);
1750 int count
= bb
& (0x3ff);
1751 u64 sector
= bb
>> 10;
1752 sector
<<= sb
->bblog_shift
;
1753 count
<<= sb
->bblog_shift
;
1756 if (badblocks_set(&rdev
->badblocks
, sector
, count
, 1))
1759 } else if (sb
->bblog_offset
!= 0)
1760 rdev
->badblocks
.shift
= 0;
1762 if ((le32_to_cpu(sb
->feature_map
) &
1763 (MD_FEATURE_PPL
| MD_FEATURE_MULTIPLE_PPLS
))) {
1764 rdev
->ppl
.offset
= (__s16
)le16_to_cpu(sb
->ppl
.offset
);
1765 rdev
->ppl
.size
= le16_to_cpu(sb
->ppl
.size
);
1766 rdev
->ppl
.sector
= rdev
->sb_start
+ rdev
->ppl
.offset
;
1769 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RAID0_LAYOUT
) &&
1773 /* not spare disk, or LEVEL_MULTIPATH */
1774 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
) ||
1775 (rdev
->desc_nr
>= 0 &&
1776 rdev
->desc_nr
< le32_to_cpu(sb
->max_dev
) &&
1777 (le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) < MD_DISK_ROLE_MAX
||
1778 le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) == MD_DISK_ROLE_JOURNAL
)))
1788 struct mdp_superblock_1
*refsb
= page_address(refdev
->sb_page
);
1790 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1791 sb
->level
!= refsb
->level
||
1792 sb
->layout
!= refsb
->layout
||
1793 sb
->chunksize
!= refsb
->chunksize
) {
1794 pr_warn("md: %s has strangely different superblock to %s\n",
1795 bdevname(rdev
->bdev
,b
),
1796 bdevname(refdev
->bdev
,b2
));
1799 ev1
= le64_to_cpu(sb
->events
);
1800 ev2
= le64_to_cpu(refsb
->events
);
1802 if (!spare_disk
&& ev1
> ev2
)
1807 if (minor_version
) {
1808 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9);
1809 sectors
-= rdev
->data_offset
;
1811 sectors
= rdev
->sb_start
;
1812 if (sectors
< le64_to_cpu(sb
->data_size
))
1814 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1818 static int super_1_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
1820 struct mdp_superblock_1
*sb
= page_address(rdev
->sb_page
);
1821 __u64 ev1
= le64_to_cpu(sb
->events
);
1823 rdev
->raid_disk
= -1;
1824 clear_bit(Faulty
, &rdev
->flags
);
1825 clear_bit(In_sync
, &rdev
->flags
);
1826 clear_bit(Bitmap_sync
, &rdev
->flags
);
1827 clear_bit(WriteMostly
, &rdev
->flags
);
1829 if (mddev
->raid_disks
== 0) {
1830 mddev
->major_version
= 1;
1831 mddev
->patch_version
= 0;
1832 mddev
->external
= 0;
1833 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1834 mddev
->ctime
= le64_to_cpu(sb
->ctime
);
1835 mddev
->utime
= le64_to_cpu(sb
->utime
);
1836 mddev
->level
= le32_to_cpu(sb
->level
);
1837 mddev
->clevel
[0] = 0;
1838 mddev
->layout
= le32_to_cpu(sb
->layout
);
1839 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1840 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1841 mddev
->events
= ev1
;
1842 mddev
->bitmap_info
.offset
= 0;
1843 mddev
->bitmap_info
.space
= 0;
1844 /* Default location for bitmap is 1K after superblock
1845 * using 3K - total of 4K
1847 mddev
->bitmap_info
.default_offset
= 1024 >> 9;
1848 mddev
->bitmap_info
.default_space
= (4096-1024) >> 9;
1849 mddev
->reshape_backwards
= 0;
1851 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1852 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1854 mddev
->max_disks
= (4096-256)/2;
1856 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1857 mddev
->bitmap_info
.file
== NULL
) {
1858 mddev
->bitmap_info
.offset
=
1859 (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1860 /* Metadata doesn't record how much space is available.
1861 * For 1.0, we assume we can use up to the superblock
1862 * if before, else to 4K beyond superblock.
1863 * For others, assume no change is possible.
1865 if (mddev
->minor_version
> 0)
1866 mddev
->bitmap_info
.space
= 0;
1867 else if (mddev
->bitmap_info
.offset
> 0)
1868 mddev
->bitmap_info
.space
=
1869 8 - mddev
->bitmap_info
.offset
;
1871 mddev
->bitmap_info
.space
=
1872 -mddev
->bitmap_info
.offset
;
1875 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1876 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1877 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1878 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1879 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1880 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1881 if (mddev
->delta_disks
< 0 ||
1882 (mddev
->delta_disks
== 0 &&
1883 (le32_to_cpu(sb
->feature_map
)
1884 & MD_FEATURE_RESHAPE_BACKWARDS
)))
1885 mddev
->reshape_backwards
= 1;
1887 mddev
->reshape_position
= MaxSector
;
1888 mddev
->delta_disks
= 0;
1889 mddev
->new_level
= mddev
->level
;
1890 mddev
->new_layout
= mddev
->layout
;
1891 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1894 if (mddev
->level
== 0 &&
1895 !(le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RAID0_LAYOUT
))
1898 if (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_JOURNAL
)
1899 set_bit(MD_HAS_JOURNAL
, &mddev
->flags
);
1901 if (le32_to_cpu(sb
->feature_map
) &
1902 (MD_FEATURE_PPL
| MD_FEATURE_MULTIPLE_PPLS
)) {
1903 if (le32_to_cpu(sb
->feature_map
) &
1904 (MD_FEATURE_BITMAP_OFFSET
| MD_FEATURE_JOURNAL
))
1906 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_PPL
) &&
1907 (le32_to_cpu(sb
->feature_map
) &
1908 MD_FEATURE_MULTIPLE_PPLS
))
1910 set_bit(MD_HAS_PPL
, &mddev
->flags
);
1912 } else if (mddev
->pers
== NULL
) {
1913 /* Insist of good event counter while assembling, except for
1914 * spares (which don't need an event count) */
1916 if (rdev
->desc_nr
>= 0 &&
1917 rdev
->desc_nr
< le32_to_cpu(sb
->max_dev
) &&
1918 (le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) < MD_DISK_ROLE_MAX
||
1919 le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) == MD_DISK_ROLE_JOURNAL
))
1920 if (ev1
< mddev
->events
)
1922 } else if (mddev
->bitmap
) {
1923 /* If adding to array with a bitmap, then we can accept an
1924 * older device, but not too old.
1926 if (ev1
< mddev
->bitmap
->events_cleared
)
1928 if (ev1
< mddev
->events
)
1929 set_bit(Bitmap_sync
, &rdev
->flags
);
1931 if (ev1
< mddev
->events
)
1932 /* just a hot-add of a new device, leave raid_disk at -1 */
1935 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1937 if (rdev
->desc_nr
< 0 ||
1938 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1939 role
= MD_DISK_ROLE_SPARE
;
1942 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1944 case MD_DISK_ROLE_SPARE
: /* spare */
1946 case MD_DISK_ROLE_FAULTY
: /* faulty */
1947 set_bit(Faulty
, &rdev
->flags
);
1949 case MD_DISK_ROLE_JOURNAL
: /* journal device */
1950 if (!(le32_to_cpu(sb
->feature_map
) & MD_FEATURE_JOURNAL
)) {
1951 /* journal device without journal feature */
1952 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
1955 set_bit(Journal
, &rdev
->flags
);
1956 rdev
->journal_tail
= le64_to_cpu(sb
->journal_tail
);
1957 rdev
->raid_disk
= 0;
1960 rdev
->saved_raid_disk
= role
;
1961 if ((le32_to_cpu(sb
->feature_map
) &
1962 MD_FEATURE_RECOVERY_OFFSET
)) {
1963 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1964 if (!(le32_to_cpu(sb
->feature_map
) &
1965 MD_FEATURE_RECOVERY_BITMAP
))
1966 rdev
->saved_raid_disk
= -1;
1969 * If the array is FROZEN, then the device can't
1970 * be in_sync with rest of array.
1972 if (!test_bit(MD_RECOVERY_FROZEN
,
1974 set_bit(In_sync
, &rdev
->flags
);
1976 rdev
->raid_disk
= role
;
1979 if (sb
->devflags
& WriteMostly1
)
1980 set_bit(WriteMostly
, &rdev
->flags
);
1981 if (sb
->devflags
& FailFast1
)
1982 set_bit(FailFast
, &rdev
->flags
);
1983 if (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_REPLACEMENT
)
1984 set_bit(Replacement
, &rdev
->flags
);
1985 } else /* MULTIPATH are always insync */
1986 set_bit(In_sync
, &rdev
->flags
);
1991 static void super_1_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
1993 struct mdp_superblock_1
*sb
;
1994 struct md_rdev
*rdev2
;
1996 /* make rdev->sb match mddev and rdev data. */
1998 sb
= page_address(rdev
->sb_page
);
2000 sb
->feature_map
= 0;
2002 sb
->recovery_offset
= cpu_to_le64(0);
2003 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
2005 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
2006 sb
->events
= cpu_to_le64(mddev
->events
);
2008 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
2009 else if (test_bit(MD_JOURNAL_CLEAN
, &mddev
->flags
))
2010 sb
->resync_offset
= cpu_to_le64(MaxSector
);
2012 sb
->resync_offset
= cpu_to_le64(0);
2014 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
2016 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
2017 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
2018 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
2019 sb
->level
= cpu_to_le32(mddev
->level
);
2020 sb
->layout
= cpu_to_le32(mddev
->layout
);
2021 if (test_bit(FailFast
, &rdev
->flags
))
2022 sb
->devflags
|= FailFast1
;
2024 sb
->devflags
&= ~FailFast1
;
2026 if (test_bit(WriteMostly
, &rdev
->flags
))
2027 sb
->devflags
|= WriteMostly1
;
2029 sb
->devflags
&= ~WriteMostly1
;
2030 sb
->data_offset
= cpu_to_le64(rdev
->data_offset
);
2031 sb
->data_size
= cpu_to_le64(rdev
->sectors
);
2033 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
) {
2034 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_info
.offset
);
2035 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
2038 if (rdev
->raid_disk
>= 0 && !test_bit(Journal
, &rdev
->flags
) &&
2039 !test_bit(In_sync
, &rdev
->flags
)) {
2041 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
2042 sb
->recovery_offset
=
2043 cpu_to_le64(rdev
->recovery_offset
);
2044 if (rdev
->saved_raid_disk
>= 0 && mddev
->bitmap
)
2046 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP
);
2048 /* Note: recovery_offset and journal_tail share space */
2049 if (test_bit(Journal
, &rdev
->flags
))
2050 sb
->journal_tail
= cpu_to_le64(rdev
->journal_tail
);
2051 if (test_bit(Replacement
, &rdev
->flags
))
2053 cpu_to_le32(MD_FEATURE_REPLACEMENT
);
2055 if (mddev
->reshape_position
!= MaxSector
) {
2056 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
2057 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
2058 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
2059 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
2060 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
2061 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
2062 if (mddev
->delta_disks
== 0 &&
2063 mddev
->reshape_backwards
)
2065 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS
);
2066 if (rdev
->new_data_offset
!= rdev
->data_offset
) {
2068 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET
);
2069 sb
->new_offset
= cpu_to_le32((__u32
)(rdev
->new_data_offset
2070 - rdev
->data_offset
));
2074 if (mddev_is_clustered(mddev
))
2075 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_CLUSTERED
);
2077 if (rdev
->badblocks
.count
== 0)
2078 /* Nothing to do for bad blocks*/ ;
2079 else if (sb
->bblog_offset
== 0)
2080 /* Cannot record bad blocks on this device */
2081 md_error(mddev
, rdev
);
2083 struct badblocks
*bb
= &rdev
->badblocks
;
2084 __le64
*bbp
= (__le64
*)page_address(rdev
->bb_page
);
2086 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_BAD_BLOCKS
);
2091 seq
= read_seqbegin(&bb
->lock
);
2093 memset(bbp
, 0xff, PAGE_SIZE
);
2095 for (i
= 0 ; i
< bb
->count
; i
++) {
2096 u64 internal_bb
= p
[i
];
2097 u64 store_bb
= ((BB_OFFSET(internal_bb
) << 10)
2098 | BB_LEN(internal_bb
));
2099 bbp
[i
] = cpu_to_le64(store_bb
);
2102 if (read_seqretry(&bb
->lock
, seq
))
2105 bb
->sector
= (rdev
->sb_start
+
2106 (int)le32_to_cpu(sb
->bblog_offset
));
2107 bb
->size
= le16_to_cpu(sb
->bblog_size
);
2112 rdev_for_each(rdev2
, mddev
)
2113 if (rdev2
->desc_nr
+1 > max_dev
)
2114 max_dev
= rdev2
->desc_nr
+1;
2116 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
2118 sb
->max_dev
= cpu_to_le32(max_dev
);
2119 rdev
->sb_size
= max_dev
* 2 + 256;
2120 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
2121 if (rdev
->sb_size
& bmask
)
2122 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
2124 max_dev
= le32_to_cpu(sb
->max_dev
);
2126 for (i
=0; i
<max_dev
;i
++)
2127 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_SPARE
);
2129 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
))
2130 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_JOURNAL
);
2132 if (test_bit(MD_HAS_PPL
, &mddev
->flags
)) {
2133 if (test_bit(MD_HAS_MULTIPLE_PPLS
, &mddev
->flags
))
2135 cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS
);
2137 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_PPL
);
2138 sb
->ppl
.offset
= cpu_to_le16(rdev
->ppl
.offset
);
2139 sb
->ppl
.size
= cpu_to_le16(rdev
->ppl
.size
);
2142 rdev_for_each(rdev2
, mddev
) {
2144 if (test_bit(Faulty
, &rdev2
->flags
))
2145 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_FAULTY
);
2146 else if (test_bit(In_sync
, &rdev2
->flags
))
2147 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
2148 else if (test_bit(Journal
, &rdev2
->flags
))
2149 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_JOURNAL
);
2150 else if (rdev2
->raid_disk
>= 0)
2151 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
2153 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_SPARE
);
2156 sb
->sb_csum
= calc_sb_1_csum(sb
);
2159 static unsigned long long
2160 super_1_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
2162 struct mdp_superblock_1
*sb
;
2163 sector_t max_sectors
;
2164 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
2165 return 0; /* component must fit device */
2166 if (rdev
->data_offset
!= rdev
->new_data_offset
)
2167 return 0; /* too confusing */
2168 if (rdev
->sb_start
< rdev
->data_offset
) {
2169 /* minor versions 1 and 2; superblock before data */
2170 max_sectors
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
2171 max_sectors
-= rdev
->data_offset
;
2172 if (!num_sectors
|| num_sectors
> max_sectors
)
2173 num_sectors
= max_sectors
;
2174 } else if (rdev
->mddev
->bitmap_info
.offset
) {
2175 /* minor version 0 with bitmap we can't move */
2178 /* minor version 0; superblock after data */
2180 sb_start
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) - 8*2;
2181 sb_start
&= ~(sector_t
)(4*2 - 1);
2182 max_sectors
= rdev
->sectors
+ sb_start
- rdev
->sb_start
;
2183 if (!num_sectors
|| num_sectors
> max_sectors
)
2184 num_sectors
= max_sectors
;
2185 rdev
->sb_start
= sb_start
;
2187 sb
= page_address(rdev
->sb_page
);
2188 sb
->data_size
= cpu_to_le64(num_sectors
);
2189 sb
->super_offset
= cpu_to_le64(rdev
->sb_start
);
2190 sb
->sb_csum
= calc_sb_1_csum(sb
);
2192 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
2194 } while (md_super_wait(rdev
->mddev
) < 0);
2200 super_1_allow_new_offset(struct md_rdev
*rdev
,
2201 unsigned long long new_offset
)
2203 /* All necessary checks on new >= old have been done */
2204 struct bitmap
*bitmap
;
2205 if (new_offset
>= rdev
->data_offset
)
2208 /* with 1.0 metadata, there is no metadata to tread on
2209 * so we can always move back */
2210 if (rdev
->mddev
->minor_version
== 0)
2213 /* otherwise we must be sure not to step on
2214 * any metadata, so stay:
2215 * 36K beyond start of superblock
2216 * beyond end of badblocks
2217 * beyond write-intent bitmap
2219 if (rdev
->sb_start
+ (32+4)*2 > new_offset
)
2221 bitmap
= rdev
->mddev
->bitmap
;
2222 if (bitmap
&& !rdev
->mddev
->bitmap_info
.file
&&
2223 rdev
->sb_start
+ rdev
->mddev
->bitmap_info
.offset
+
2224 bitmap
->storage
.file_pages
* (PAGE_SIZE
>>9) > new_offset
)
2226 if (rdev
->badblocks
.sector
+ rdev
->badblocks
.size
> new_offset
)
2232 static struct super_type super_types
[] = {
2235 .owner
= THIS_MODULE
,
2236 .load_super
= super_90_load
,
2237 .validate_super
= super_90_validate
,
2238 .sync_super
= super_90_sync
,
2239 .rdev_size_change
= super_90_rdev_size_change
,
2240 .allow_new_offset
= super_90_allow_new_offset
,
2244 .owner
= THIS_MODULE
,
2245 .load_super
= super_1_load
,
2246 .validate_super
= super_1_validate
,
2247 .sync_super
= super_1_sync
,
2248 .rdev_size_change
= super_1_rdev_size_change
,
2249 .allow_new_offset
= super_1_allow_new_offset
,
2253 static void sync_super(struct mddev
*mddev
, struct md_rdev
*rdev
)
2255 if (mddev
->sync_super
) {
2256 mddev
->sync_super(mddev
, rdev
);
2260 BUG_ON(mddev
->major_version
>= ARRAY_SIZE(super_types
));
2262 super_types
[mddev
->major_version
].sync_super(mddev
, rdev
);
2265 static int match_mddev_units(struct mddev
*mddev1
, struct mddev
*mddev2
)
2267 struct md_rdev
*rdev
, *rdev2
;
2270 rdev_for_each_rcu(rdev
, mddev1
) {
2271 if (test_bit(Faulty
, &rdev
->flags
) ||
2272 test_bit(Journal
, &rdev
->flags
) ||
2273 rdev
->raid_disk
== -1)
2275 rdev_for_each_rcu(rdev2
, mddev2
) {
2276 if (test_bit(Faulty
, &rdev2
->flags
) ||
2277 test_bit(Journal
, &rdev2
->flags
) ||
2278 rdev2
->raid_disk
== -1)
2280 if (rdev
->bdev
->bd_contains
==
2281 rdev2
->bdev
->bd_contains
) {
2291 static LIST_HEAD(pending_raid_disks
);
2294 * Try to register data integrity profile for an mddev
2296 * This is called when an array is started and after a disk has been kicked
2297 * from the array. It only succeeds if all working and active component devices
2298 * are integrity capable with matching profiles.
2300 int md_integrity_register(struct mddev
*mddev
)
2302 struct md_rdev
*rdev
, *reference
= NULL
;
2304 if (list_empty(&mddev
->disks
))
2305 return 0; /* nothing to do */
2306 if (!mddev
->gendisk
|| blk_get_integrity(mddev
->gendisk
))
2307 return 0; /* shouldn't register, or already is */
2308 rdev_for_each(rdev
, mddev
) {
2309 /* skip spares and non-functional disks */
2310 if (test_bit(Faulty
, &rdev
->flags
))
2312 if (rdev
->raid_disk
< 0)
2315 /* Use the first rdev as the reference */
2319 /* does this rdev's profile match the reference profile? */
2320 if (blk_integrity_compare(reference
->bdev
->bd_disk
,
2321 rdev
->bdev
->bd_disk
) < 0)
2324 if (!reference
|| !bdev_get_integrity(reference
->bdev
))
2327 * All component devices are integrity capable and have matching
2328 * profiles, register the common profile for the md device.
2330 blk_integrity_register(mddev
->gendisk
,
2331 bdev_get_integrity(reference
->bdev
));
2333 pr_debug("md: data integrity enabled on %s\n", mdname(mddev
));
2334 if (bioset_integrity_create(&mddev
->bio_set
, BIO_POOL_SIZE
)) {
2335 pr_err("md: failed to create integrity pool for %s\n",
2341 EXPORT_SYMBOL(md_integrity_register
);
2344 * Attempt to add an rdev, but only if it is consistent with the current
2347 int md_integrity_add_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
2349 struct blk_integrity
*bi_mddev
;
2350 char name
[BDEVNAME_SIZE
];
2352 if (!mddev
->gendisk
)
2355 bi_mddev
= blk_get_integrity(mddev
->gendisk
);
2357 if (!bi_mddev
) /* nothing to do */
2360 if (blk_integrity_compare(mddev
->gendisk
, rdev
->bdev
->bd_disk
) != 0) {
2361 pr_err("%s: incompatible integrity profile for %s\n",
2362 mdname(mddev
), bdevname(rdev
->bdev
, name
));
2368 EXPORT_SYMBOL(md_integrity_add_rdev
);
2370 static int bind_rdev_to_array(struct md_rdev
*rdev
, struct mddev
*mddev
)
2372 char b
[BDEVNAME_SIZE
];
2376 /* prevent duplicates */
2377 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
2380 if ((bdev_read_only(rdev
->bdev
) || bdev_read_only(rdev
->meta_bdev
)) &&
2384 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2385 if (!test_bit(Journal
, &rdev
->flags
) &&
2387 (mddev
->dev_sectors
== 0 || rdev
->sectors
< mddev
->dev_sectors
)) {
2389 /* Cannot change size, so fail
2390 * If mddev->level <= 0, then we don't care
2391 * about aligning sizes (e.g. linear)
2393 if (mddev
->level
> 0)
2396 mddev
->dev_sectors
= rdev
->sectors
;
2399 /* Verify rdev->desc_nr is unique.
2400 * If it is -1, assign a free number, else
2401 * check number is not in use
2404 if (rdev
->desc_nr
< 0) {
2407 choice
= mddev
->raid_disks
;
2408 while (md_find_rdev_nr_rcu(mddev
, choice
))
2410 rdev
->desc_nr
= choice
;
2412 if (md_find_rdev_nr_rcu(mddev
, rdev
->desc_nr
)) {
2418 if (!test_bit(Journal
, &rdev
->flags
) &&
2419 mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
2420 pr_warn("md: %s: array is limited to %d devices\n",
2421 mdname(mddev
), mddev
->max_disks
);
2424 bdevname(rdev
->bdev
,b
);
2425 strreplace(b
, '/', '!');
2427 rdev
->mddev
= mddev
;
2428 pr_debug("md: bind<%s>\n", b
);
2430 if (mddev
->raid_disks
)
2431 mddev_create_serial_pool(mddev
, rdev
, false);
2433 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
2436 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
2437 if (sysfs_create_link(&rdev
->kobj
, ko
, "block"))
2438 /* failure here is OK */;
2439 rdev
->sysfs_state
= sysfs_get_dirent_safe(rdev
->kobj
.sd
, "state");
2441 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
2442 bd_link_disk_holder(rdev
->bdev
, mddev
->gendisk
);
2444 /* May as well allow recovery to be retried once */
2445 mddev
->recovery_disabled
++;
2450 pr_warn("md: failed to register dev-%s for %s\n",
2455 static void md_delayed_delete(struct work_struct
*ws
)
2457 struct md_rdev
*rdev
= container_of(ws
, struct md_rdev
, del_work
);
2458 kobject_del(&rdev
->kobj
);
2459 kobject_put(&rdev
->kobj
);
2462 static void unbind_rdev_from_array(struct md_rdev
*rdev
)
2464 char b
[BDEVNAME_SIZE
];
2466 bd_unlink_disk_holder(rdev
->bdev
, rdev
->mddev
->gendisk
);
2467 list_del_rcu(&rdev
->same_set
);
2468 pr_debug("md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
2469 mddev_destroy_serial_pool(rdev
->mddev
, rdev
, false);
2471 sysfs_remove_link(&rdev
->kobj
, "block");
2472 sysfs_put(rdev
->sysfs_state
);
2473 rdev
->sysfs_state
= NULL
;
2474 rdev
->badblocks
.count
= 0;
2475 /* We need to delay this, otherwise we can deadlock when
2476 * writing to 'remove' to "dev/state". We also need
2477 * to delay it due to rcu usage.
2480 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
2481 kobject_get(&rdev
->kobj
);
2482 queue_work(md_misc_wq
, &rdev
->del_work
);
2486 * prevent the device from being mounted, repartitioned or
2487 * otherwise reused by a RAID array (or any other kernel
2488 * subsystem), by bd_claiming the device.
2490 static int lock_rdev(struct md_rdev
*rdev
, dev_t dev
, int shared
)
2493 struct block_device
*bdev
;
2494 char b
[BDEVNAME_SIZE
];
2496 bdev
= blkdev_get_by_dev(dev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
,
2497 shared
? (struct md_rdev
*)lock_rdev
: rdev
);
2499 pr_warn("md: could not open %s.\n", __bdevname(dev
, b
));
2500 return PTR_ERR(bdev
);
2506 static void unlock_rdev(struct md_rdev
*rdev
)
2508 struct block_device
*bdev
= rdev
->bdev
;
2510 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
2513 void md_autodetect_dev(dev_t dev
);
2515 static void export_rdev(struct md_rdev
*rdev
)
2517 char b
[BDEVNAME_SIZE
];
2519 pr_debug("md: export_rdev(%s)\n", bdevname(rdev
->bdev
,b
));
2520 md_rdev_clear(rdev
);
2522 if (test_bit(AutoDetected
, &rdev
->flags
))
2523 md_autodetect_dev(rdev
->bdev
->bd_dev
);
2526 kobject_put(&rdev
->kobj
);
2529 void md_kick_rdev_from_array(struct md_rdev
*rdev
)
2531 unbind_rdev_from_array(rdev
);
2534 EXPORT_SYMBOL_GPL(md_kick_rdev_from_array
);
2536 static void export_array(struct mddev
*mddev
)
2538 struct md_rdev
*rdev
;
2540 while (!list_empty(&mddev
->disks
)) {
2541 rdev
= list_first_entry(&mddev
->disks
, struct md_rdev
,
2543 md_kick_rdev_from_array(rdev
);
2545 mddev
->raid_disks
= 0;
2546 mddev
->major_version
= 0;
2549 static bool set_in_sync(struct mddev
*mddev
)
2551 lockdep_assert_held(&mddev
->lock
);
2552 if (!mddev
->in_sync
) {
2553 mddev
->sync_checkers
++;
2554 spin_unlock(&mddev
->lock
);
2555 percpu_ref_switch_to_atomic_sync(&mddev
->writes_pending
);
2556 spin_lock(&mddev
->lock
);
2557 if (!mddev
->in_sync
&&
2558 percpu_ref_is_zero(&mddev
->writes_pending
)) {
2561 * Ensure ->in_sync is visible before we clear
2565 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
2566 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
2568 if (--mddev
->sync_checkers
== 0)
2569 percpu_ref_switch_to_percpu(&mddev
->writes_pending
);
2571 if (mddev
->safemode
== 1)
2572 mddev
->safemode
= 0;
2573 return mddev
->in_sync
;
2576 static void sync_sbs(struct mddev
*mddev
, int nospares
)
2578 /* Update each superblock (in-memory image), but
2579 * if we are allowed to, skip spares which already
2580 * have the right event counter, or have one earlier
2581 * (which would mean they aren't being marked as dirty
2582 * with the rest of the array)
2584 struct md_rdev
*rdev
;
2585 rdev_for_each(rdev
, mddev
) {
2586 if (rdev
->sb_events
== mddev
->events
||
2588 rdev
->raid_disk
< 0 &&
2589 rdev
->sb_events
+1 == mddev
->events
)) {
2590 /* Don't update this superblock */
2591 rdev
->sb_loaded
= 2;
2593 sync_super(mddev
, rdev
);
2594 rdev
->sb_loaded
= 1;
2599 static bool does_sb_need_changing(struct mddev
*mddev
)
2601 struct md_rdev
*rdev
;
2602 struct mdp_superblock_1
*sb
;
2605 /* Find a good rdev */
2606 rdev_for_each(rdev
, mddev
)
2607 if ((rdev
->raid_disk
>= 0) && !test_bit(Faulty
, &rdev
->flags
))
2610 /* No good device found. */
2614 sb
= page_address(rdev
->sb_page
);
2615 /* Check if a device has become faulty or a spare become active */
2616 rdev_for_each(rdev
, mddev
) {
2617 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
2618 /* Device activated? */
2619 if (role
== 0xffff && rdev
->raid_disk
>=0 &&
2620 !test_bit(Faulty
, &rdev
->flags
))
2622 /* Device turned faulty? */
2623 if (test_bit(Faulty
, &rdev
->flags
) && (role
< 0xfffd))
2627 /* Check if any mddev parameters have changed */
2628 if ((mddev
->dev_sectors
!= le64_to_cpu(sb
->size
)) ||
2629 (mddev
->reshape_position
!= le64_to_cpu(sb
->reshape_position
)) ||
2630 (mddev
->layout
!= le32_to_cpu(sb
->layout
)) ||
2631 (mddev
->raid_disks
!= le32_to_cpu(sb
->raid_disks
)) ||
2632 (mddev
->chunk_sectors
!= le32_to_cpu(sb
->chunksize
)))
2638 void md_update_sb(struct mddev
*mddev
, int force_change
)
2640 struct md_rdev
*rdev
;
2643 int any_badblocks_changed
= 0;
2648 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2653 if (mddev_is_clustered(mddev
)) {
2654 if (test_and_clear_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
))
2656 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
))
2658 ret
= md_cluster_ops
->metadata_update_start(mddev
);
2659 /* Has someone else has updated the sb */
2660 if (!does_sb_need_changing(mddev
)) {
2662 md_cluster_ops
->metadata_update_cancel(mddev
);
2663 bit_clear_unless(&mddev
->sb_flags
, BIT(MD_SB_CHANGE_PENDING
),
2664 BIT(MD_SB_CHANGE_DEVS
) |
2665 BIT(MD_SB_CHANGE_CLEAN
));
2671 * First make sure individual recovery_offsets are correct
2672 * curr_resync_completed can only be used during recovery.
2673 * During reshape/resync it might use array-addresses rather
2674 * that device addresses.
2676 rdev_for_each(rdev
, mddev
) {
2677 if (rdev
->raid_disk
>= 0 &&
2678 mddev
->delta_disks
>= 0 &&
2679 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
2680 test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
) &&
2681 !test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
2682 !test_bit(Journal
, &rdev
->flags
) &&
2683 !test_bit(In_sync
, &rdev
->flags
) &&
2684 mddev
->curr_resync_completed
> rdev
->recovery_offset
)
2685 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
2688 if (!mddev
->persistent
) {
2689 clear_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
2690 clear_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2691 if (!mddev
->external
) {
2692 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
2693 rdev_for_each(rdev
, mddev
) {
2694 if (rdev
->badblocks
.changed
) {
2695 rdev
->badblocks
.changed
= 0;
2696 ack_all_badblocks(&rdev
->badblocks
);
2697 md_error(mddev
, rdev
);
2699 clear_bit(Blocked
, &rdev
->flags
);
2700 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2701 wake_up(&rdev
->blocked_wait
);
2704 wake_up(&mddev
->sb_wait
);
2708 spin_lock(&mddev
->lock
);
2710 mddev
->utime
= ktime_get_real_seconds();
2712 if (test_and_clear_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
))
2714 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
))
2715 /* just a clean<-> dirty transition, possibly leave spares alone,
2716 * though if events isn't the right even/odd, we will have to do
2722 if (mddev
->degraded
)
2723 /* If the array is degraded, then skipping spares is both
2724 * dangerous and fairly pointless.
2725 * Dangerous because a device that was removed from the array
2726 * might have a event_count that still looks up-to-date,
2727 * so it can be re-added without a resync.
2728 * Pointless because if there are any spares to skip,
2729 * then a recovery will happen and soon that array won't
2730 * be degraded any more and the spare can go back to sleep then.
2734 sync_req
= mddev
->in_sync
;
2736 /* If this is just a dirty<->clean transition, and the array is clean
2737 * and 'events' is odd, we can roll back to the previous clean state */
2739 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
2740 && mddev
->can_decrease_events
2741 && mddev
->events
!= 1) {
2743 mddev
->can_decrease_events
= 0;
2745 /* otherwise we have to go forward and ... */
2747 mddev
->can_decrease_events
= nospares
;
2751 * This 64-bit counter should never wrap.
2752 * Either we are in around ~1 trillion A.C., assuming
2753 * 1 reboot per second, or we have a bug...
2755 WARN_ON(mddev
->events
== 0);
2757 rdev_for_each(rdev
, mddev
) {
2758 if (rdev
->badblocks
.changed
)
2759 any_badblocks_changed
++;
2760 if (test_bit(Faulty
, &rdev
->flags
))
2761 set_bit(FaultRecorded
, &rdev
->flags
);
2764 sync_sbs(mddev
, nospares
);
2765 spin_unlock(&mddev
->lock
);
2767 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2768 mdname(mddev
), mddev
->in_sync
);
2771 blk_add_trace_msg(mddev
->queue
, "md md_update_sb");
2773 md_bitmap_update_sb(mddev
->bitmap
);
2774 rdev_for_each(rdev
, mddev
) {
2775 char b
[BDEVNAME_SIZE
];
2777 if (rdev
->sb_loaded
!= 1)
2778 continue; /* no noise on spare devices */
2780 if (!test_bit(Faulty
, &rdev
->flags
)) {
2781 md_super_write(mddev
,rdev
,
2782 rdev
->sb_start
, rdev
->sb_size
,
2784 pr_debug("md: (write) %s's sb offset: %llu\n",
2785 bdevname(rdev
->bdev
, b
),
2786 (unsigned long long)rdev
->sb_start
);
2787 rdev
->sb_events
= mddev
->events
;
2788 if (rdev
->badblocks
.size
) {
2789 md_super_write(mddev
, rdev
,
2790 rdev
->badblocks
.sector
,
2791 rdev
->badblocks
.size
<< 9,
2793 rdev
->badblocks
.size
= 0;
2797 pr_debug("md: %s (skipping faulty)\n",
2798 bdevname(rdev
->bdev
, b
));
2800 if (mddev
->level
== LEVEL_MULTIPATH
)
2801 /* only need to write one superblock... */
2804 if (md_super_wait(mddev
) < 0)
2806 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
2808 if (mddev_is_clustered(mddev
) && ret
== 0)
2809 md_cluster_ops
->metadata_update_finish(mddev
);
2811 if (mddev
->in_sync
!= sync_req
||
2812 !bit_clear_unless(&mddev
->sb_flags
, BIT(MD_SB_CHANGE_PENDING
),
2813 BIT(MD_SB_CHANGE_DEVS
) | BIT(MD_SB_CHANGE_CLEAN
)))
2814 /* have to write it out again */
2816 wake_up(&mddev
->sb_wait
);
2817 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2818 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
2820 rdev_for_each(rdev
, mddev
) {
2821 if (test_and_clear_bit(FaultRecorded
, &rdev
->flags
))
2822 clear_bit(Blocked
, &rdev
->flags
);
2824 if (any_badblocks_changed
)
2825 ack_all_badblocks(&rdev
->badblocks
);
2826 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2827 wake_up(&rdev
->blocked_wait
);
2830 EXPORT_SYMBOL(md_update_sb
);
2832 static int add_bound_rdev(struct md_rdev
*rdev
)
2834 struct mddev
*mddev
= rdev
->mddev
;
2836 bool add_journal
= test_bit(Journal
, &rdev
->flags
);
2838 if (!mddev
->pers
->hot_remove_disk
|| add_journal
) {
2839 /* If there is hot_add_disk but no hot_remove_disk
2840 * then added disks for geometry changes,
2841 * and should be added immediately.
2843 super_types
[mddev
->major_version
].
2844 validate_super(mddev
, rdev
);
2846 mddev_suspend(mddev
);
2847 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
2849 mddev_resume(mddev
);
2851 md_kick_rdev_from_array(rdev
);
2855 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2857 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2858 if (mddev
->degraded
)
2859 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
2860 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2861 md_new_event(mddev
);
2862 md_wakeup_thread(mddev
->thread
);
2866 /* words written to sysfs files may, or may not, be \n terminated.
2867 * We want to accept with case. For this we use cmd_match.
2869 static int cmd_match(const char *cmd
, const char *str
)
2871 /* See if cmd, written into a sysfs file, matches
2872 * str. They must either be the same, or cmd can
2873 * have a trailing newline
2875 while (*cmd
&& *str
&& *cmd
== *str
) {
2886 struct rdev_sysfs_entry
{
2887 struct attribute attr
;
2888 ssize_t (*show
)(struct md_rdev
*, char *);
2889 ssize_t (*store
)(struct md_rdev
*, const char *, size_t);
2893 state_show(struct md_rdev
*rdev
, char *page
)
2897 unsigned long flags
= READ_ONCE(rdev
->flags
);
2899 if (test_bit(Faulty
, &flags
) ||
2900 (!test_bit(ExternalBbl
, &flags
) &&
2901 rdev
->badblocks
.unacked_exist
))
2902 len
+= sprintf(page
+len
, "faulty%s", sep
);
2903 if (test_bit(In_sync
, &flags
))
2904 len
+= sprintf(page
+len
, "in_sync%s", sep
);
2905 if (test_bit(Journal
, &flags
))
2906 len
+= sprintf(page
+len
, "journal%s", sep
);
2907 if (test_bit(WriteMostly
, &flags
))
2908 len
+= sprintf(page
+len
, "write_mostly%s", sep
);
2909 if (test_bit(Blocked
, &flags
) ||
2910 (rdev
->badblocks
.unacked_exist
2911 && !test_bit(Faulty
, &flags
)))
2912 len
+= sprintf(page
+len
, "blocked%s", sep
);
2913 if (!test_bit(Faulty
, &flags
) &&
2914 !test_bit(Journal
, &flags
) &&
2915 !test_bit(In_sync
, &flags
))
2916 len
+= sprintf(page
+len
, "spare%s", sep
);
2917 if (test_bit(WriteErrorSeen
, &flags
))
2918 len
+= sprintf(page
+len
, "write_error%s", sep
);
2919 if (test_bit(WantReplacement
, &flags
))
2920 len
+= sprintf(page
+len
, "want_replacement%s", sep
);
2921 if (test_bit(Replacement
, &flags
))
2922 len
+= sprintf(page
+len
, "replacement%s", sep
);
2923 if (test_bit(ExternalBbl
, &flags
))
2924 len
+= sprintf(page
+len
, "external_bbl%s", sep
);
2925 if (test_bit(FailFast
, &flags
))
2926 len
+= sprintf(page
+len
, "failfast%s", sep
);
2931 return len
+sprintf(page
+len
, "\n");
2935 state_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2938 * faulty - simulates an error
2939 * remove - disconnects the device
2940 * writemostly - sets write_mostly
2941 * -writemostly - clears write_mostly
2942 * blocked - sets the Blocked flags
2943 * -blocked - clears the Blocked and possibly simulates an error
2944 * insync - sets Insync providing device isn't active
2945 * -insync - clear Insync for a device with a slot assigned,
2946 * so that it gets rebuilt based on bitmap
2947 * write_error - sets WriteErrorSeen
2948 * -write_error - clears WriteErrorSeen
2949 * {,-}failfast - set/clear FailFast
2952 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
2953 md_error(rdev
->mddev
, rdev
);
2954 if (test_bit(Faulty
, &rdev
->flags
))
2958 } else if (cmd_match(buf
, "remove")) {
2959 if (rdev
->mddev
->pers
) {
2960 clear_bit(Blocked
, &rdev
->flags
);
2961 remove_and_add_spares(rdev
->mddev
, rdev
);
2963 if (rdev
->raid_disk
>= 0)
2966 struct mddev
*mddev
= rdev
->mddev
;
2968 if (mddev_is_clustered(mddev
))
2969 err
= md_cluster_ops
->remove_disk(mddev
, rdev
);
2972 md_kick_rdev_from_array(rdev
);
2974 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2975 md_wakeup_thread(mddev
->thread
);
2977 md_new_event(mddev
);
2980 } else if (cmd_match(buf
, "writemostly")) {
2981 set_bit(WriteMostly
, &rdev
->flags
);
2982 mddev_create_serial_pool(rdev
->mddev
, rdev
, false);
2984 } else if (cmd_match(buf
, "-writemostly")) {
2985 mddev_destroy_serial_pool(rdev
->mddev
, rdev
, false);
2986 clear_bit(WriteMostly
, &rdev
->flags
);
2988 } else if (cmd_match(buf
, "blocked")) {
2989 set_bit(Blocked
, &rdev
->flags
);
2991 } else if (cmd_match(buf
, "-blocked")) {
2992 if (!test_bit(Faulty
, &rdev
->flags
) &&
2993 !test_bit(ExternalBbl
, &rdev
->flags
) &&
2994 rdev
->badblocks
.unacked_exist
) {
2995 /* metadata handler doesn't understand badblocks,
2996 * so we need to fail the device
2998 md_error(rdev
->mddev
, rdev
);
3000 clear_bit(Blocked
, &rdev
->flags
);
3001 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
3002 wake_up(&rdev
->blocked_wait
);
3003 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
3004 md_wakeup_thread(rdev
->mddev
->thread
);
3007 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
3008 set_bit(In_sync
, &rdev
->flags
);
3010 } else if (cmd_match(buf
, "failfast")) {
3011 set_bit(FailFast
, &rdev
->flags
);
3013 } else if (cmd_match(buf
, "-failfast")) {
3014 clear_bit(FailFast
, &rdev
->flags
);
3016 } else if (cmd_match(buf
, "-insync") && rdev
->raid_disk
>= 0 &&
3017 !test_bit(Journal
, &rdev
->flags
)) {
3018 if (rdev
->mddev
->pers
== NULL
) {
3019 clear_bit(In_sync
, &rdev
->flags
);
3020 rdev
->saved_raid_disk
= rdev
->raid_disk
;
3021 rdev
->raid_disk
= -1;
3024 } else if (cmd_match(buf
, "write_error")) {
3025 set_bit(WriteErrorSeen
, &rdev
->flags
);
3027 } else if (cmd_match(buf
, "-write_error")) {
3028 clear_bit(WriteErrorSeen
, &rdev
->flags
);
3030 } else if (cmd_match(buf
, "want_replacement")) {
3031 /* Any non-spare device that is not a replacement can
3032 * become want_replacement at any time, but we then need to
3033 * check if recovery is needed.
3035 if (rdev
->raid_disk
>= 0 &&
3036 !test_bit(Journal
, &rdev
->flags
) &&
3037 !test_bit(Replacement
, &rdev
->flags
))
3038 set_bit(WantReplacement
, &rdev
->flags
);
3039 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
3040 md_wakeup_thread(rdev
->mddev
->thread
);
3042 } else if (cmd_match(buf
, "-want_replacement")) {
3043 /* Clearing 'want_replacement' is always allowed.
3044 * Once replacements starts it is too late though.
3047 clear_bit(WantReplacement
, &rdev
->flags
);
3048 } else if (cmd_match(buf
, "replacement")) {
3049 /* Can only set a device as a replacement when array has not
3050 * yet been started. Once running, replacement is automatic
3051 * from spares, or by assigning 'slot'.
3053 if (rdev
->mddev
->pers
)
3056 set_bit(Replacement
, &rdev
->flags
);
3059 } else if (cmd_match(buf
, "-replacement")) {
3060 /* Similarly, can only clear Replacement before start */
3061 if (rdev
->mddev
->pers
)
3064 clear_bit(Replacement
, &rdev
->flags
);
3067 } else if (cmd_match(buf
, "re-add")) {
3068 if (!rdev
->mddev
->pers
)
3070 else if (test_bit(Faulty
, &rdev
->flags
) && (rdev
->raid_disk
== -1) &&
3071 rdev
->saved_raid_disk
>= 0) {
3072 /* clear_bit is performed _after_ all the devices
3073 * have their local Faulty bit cleared. If any writes
3074 * happen in the meantime in the local node, they
3075 * will land in the local bitmap, which will be synced
3076 * by this node eventually
3078 if (!mddev_is_clustered(rdev
->mddev
) ||
3079 (err
= md_cluster_ops
->gather_bitmaps(rdev
)) == 0) {
3080 clear_bit(Faulty
, &rdev
->flags
);
3081 err
= add_bound_rdev(rdev
);
3085 } else if (cmd_match(buf
, "external_bbl") && (rdev
->mddev
->external
)) {
3086 set_bit(ExternalBbl
, &rdev
->flags
);
3087 rdev
->badblocks
.shift
= 0;
3089 } else if (cmd_match(buf
, "-external_bbl") && (rdev
->mddev
->external
)) {
3090 clear_bit(ExternalBbl
, &rdev
->flags
);
3094 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
3095 return err
? err
: len
;
3097 static struct rdev_sysfs_entry rdev_state
=
3098 __ATTR_PREALLOC(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
3101 errors_show(struct md_rdev
*rdev
, char *page
)
3103 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
3107 errors_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3112 rv
= kstrtouint(buf
, 10, &n
);
3115 atomic_set(&rdev
->corrected_errors
, n
);
3118 static struct rdev_sysfs_entry rdev_errors
=
3119 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
3122 slot_show(struct md_rdev
*rdev
, char *page
)
3124 if (test_bit(Journal
, &rdev
->flags
))
3125 return sprintf(page
, "journal\n");
3126 else if (rdev
->raid_disk
< 0)
3127 return sprintf(page
, "none\n");
3129 return sprintf(page
, "%d\n", rdev
->raid_disk
);
3133 slot_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3138 if (test_bit(Journal
, &rdev
->flags
))
3140 if (strncmp(buf
, "none", 4)==0)
3143 err
= kstrtouint(buf
, 10, (unsigned int *)&slot
);
3147 if (rdev
->mddev
->pers
&& slot
== -1) {
3148 /* Setting 'slot' on an active array requires also
3149 * updating the 'rd%d' link, and communicating
3150 * with the personality with ->hot_*_disk.
3151 * For now we only support removing
3152 * failed/spare devices. This normally happens automatically,
3153 * but not when the metadata is externally managed.
3155 if (rdev
->raid_disk
== -1)
3157 /* personality does all needed checks */
3158 if (rdev
->mddev
->pers
->hot_remove_disk
== NULL
)
3160 clear_bit(Blocked
, &rdev
->flags
);
3161 remove_and_add_spares(rdev
->mddev
, rdev
);
3162 if (rdev
->raid_disk
>= 0)
3164 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
3165 md_wakeup_thread(rdev
->mddev
->thread
);
3166 } else if (rdev
->mddev
->pers
) {
3167 /* Activating a spare .. or possibly reactivating
3168 * if we ever get bitmaps working here.
3172 if (rdev
->raid_disk
!= -1)
3175 if (test_bit(MD_RECOVERY_RUNNING
, &rdev
->mddev
->recovery
))
3178 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
3181 if (slot
>= rdev
->mddev
->raid_disks
&&
3182 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
3185 rdev
->raid_disk
= slot
;
3186 if (test_bit(In_sync
, &rdev
->flags
))
3187 rdev
->saved_raid_disk
= slot
;
3189 rdev
->saved_raid_disk
= -1;
3190 clear_bit(In_sync
, &rdev
->flags
);
3191 clear_bit(Bitmap_sync
, &rdev
->flags
);
3192 err
= rdev
->mddev
->pers
->
3193 hot_add_disk(rdev
->mddev
, rdev
);
3195 rdev
->raid_disk
= -1;
3198 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
3199 if (sysfs_link_rdev(rdev
->mddev
, rdev
))
3200 /* failure here is OK */;
3201 /* don't wakeup anyone, leave that to userspace. */
3203 if (slot
>= rdev
->mddev
->raid_disks
&&
3204 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
3206 rdev
->raid_disk
= slot
;
3207 /* assume it is working */
3208 clear_bit(Faulty
, &rdev
->flags
);
3209 clear_bit(WriteMostly
, &rdev
->flags
);
3210 set_bit(In_sync
, &rdev
->flags
);
3211 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
3216 static struct rdev_sysfs_entry rdev_slot
=
3217 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
3220 offset_show(struct md_rdev
*rdev
, char *page
)
3222 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
3226 offset_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3228 unsigned long long offset
;
3229 if (kstrtoull(buf
, 10, &offset
) < 0)
3231 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
3233 if (rdev
->sectors
&& rdev
->mddev
->external
)
3234 /* Must set offset before size, so overlap checks
3237 rdev
->data_offset
= offset
;
3238 rdev
->new_data_offset
= offset
;
3242 static struct rdev_sysfs_entry rdev_offset
=
3243 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
3245 static ssize_t
new_offset_show(struct md_rdev
*rdev
, char *page
)
3247 return sprintf(page
, "%llu\n",
3248 (unsigned long long)rdev
->new_data_offset
);
3251 static ssize_t
new_offset_store(struct md_rdev
*rdev
,
3252 const char *buf
, size_t len
)
3254 unsigned long long new_offset
;
3255 struct mddev
*mddev
= rdev
->mddev
;
3257 if (kstrtoull(buf
, 10, &new_offset
) < 0)
3260 if (mddev
->sync_thread
||
3261 test_bit(MD_RECOVERY_RUNNING
,&mddev
->recovery
))
3263 if (new_offset
== rdev
->data_offset
)
3264 /* reset is always permitted */
3266 else if (new_offset
> rdev
->data_offset
) {
3267 /* must not push array size beyond rdev_sectors */
3268 if (new_offset
- rdev
->data_offset
3269 + mddev
->dev_sectors
> rdev
->sectors
)
3272 /* Metadata worries about other space details. */
3274 /* decreasing the offset is inconsistent with a backwards
3277 if (new_offset
< rdev
->data_offset
&&
3278 mddev
->reshape_backwards
)
3280 /* Increasing offset is inconsistent with forwards
3281 * reshape. reshape_direction should be set to
3282 * 'backwards' first.
3284 if (new_offset
> rdev
->data_offset
&&
3285 !mddev
->reshape_backwards
)
3288 if (mddev
->pers
&& mddev
->persistent
&&
3289 !super_types
[mddev
->major_version
]
3290 .allow_new_offset(rdev
, new_offset
))
3292 rdev
->new_data_offset
= new_offset
;
3293 if (new_offset
> rdev
->data_offset
)
3294 mddev
->reshape_backwards
= 1;
3295 else if (new_offset
< rdev
->data_offset
)
3296 mddev
->reshape_backwards
= 0;
3300 static struct rdev_sysfs_entry rdev_new_offset
=
3301 __ATTR(new_offset
, S_IRUGO
|S_IWUSR
, new_offset_show
, new_offset_store
);
3304 rdev_size_show(struct md_rdev
*rdev
, char *page
)
3306 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
3309 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
3311 /* check if two start/length pairs overlap */
3319 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
3321 unsigned long long blocks
;
3324 if (kstrtoull(buf
, 10, &blocks
) < 0)
3327 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
3328 return -EINVAL
; /* sector conversion overflow */
3331 if (new != blocks
* 2)
3332 return -EINVAL
; /* unsigned long long to sector_t overflow */
3339 rdev_size_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3341 struct mddev
*my_mddev
= rdev
->mddev
;
3342 sector_t oldsectors
= rdev
->sectors
;
3345 if (test_bit(Journal
, &rdev
->flags
))
3347 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
3349 if (rdev
->data_offset
!= rdev
->new_data_offset
)
3350 return -EINVAL
; /* too confusing */
3351 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
3352 if (my_mddev
->persistent
) {
3353 sectors
= super_types
[my_mddev
->major_version
].
3354 rdev_size_change(rdev
, sectors
);
3357 } else if (!sectors
)
3358 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
3360 if (!my_mddev
->pers
->resize
)
3361 /* Cannot change size for RAID0 or Linear etc */
3364 if (sectors
< my_mddev
->dev_sectors
)
3365 return -EINVAL
; /* component must fit device */
3367 rdev
->sectors
= sectors
;
3368 if (sectors
> oldsectors
&& my_mddev
->external
) {
3369 /* Need to check that all other rdevs with the same
3370 * ->bdev do not overlap. 'rcu' is sufficient to walk
3371 * the rdev lists safely.
3372 * This check does not provide a hard guarantee, it
3373 * just helps avoid dangerous mistakes.
3375 struct mddev
*mddev
;
3377 struct list_head
*tmp
;
3380 for_each_mddev(mddev
, tmp
) {
3381 struct md_rdev
*rdev2
;
3383 rdev_for_each(rdev2
, mddev
)
3384 if (rdev
->bdev
== rdev2
->bdev
&&
3386 overlaps(rdev
->data_offset
, rdev
->sectors
,
3399 /* Someone else could have slipped in a size
3400 * change here, but doing so is just silly.
3401 * We put oldsectors back because we *know* it is
3402 * safe, and trust userspace not to race with
3405 rdev
->sectors
= oldsectors
;
3412 static struct rdev_sysfs_entry rdev_size
=
3413 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
3415 static ssize_t
recovery_start_show(struct md_rdev
*rdev
, char *page
)
3417 unsigned long long recovery_start
= rdev
->recovery_offset
;
3419 if (test_bit(In_sync
, &rdev
->flags
) ||
3420 recovery_start
== MaxSector
)
3421 return sprintf(page
, "none\n");
3423 return sprintf(page
, "%llu\n", recovery_start
);
3426 static ssize_t
recovery_start_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3428 unsigned long long recovery_start
;
3430 if (cmd_match(buf
, "none"))
3431 recovery_start
= MaxSector
;
3432 else if (kstrtoull(buf
, 10, &recovery_start
))
3435 if (rdev
->mddev
->pers
&&
3436 rdev
->raid_disk
>= 0)
3439 rdev
->recovery_offset
= recovery_start
;
3440 if (recovery_start
== MaxSector
)
3441 set_bit(In_sync
, &rdev
->flags
);
3443 clear_bit(In_sync
, &rdev
->flags
);
3447 static struct rdev_sysfs_entry rdev_recovery_start
=
3448 __ATTR(recovery_start
, S_IRUGO
|S_IWUSR
, recovery_start_show
, recovery_start_store
);
3450 /* sysfs access to bad-blocks list.
3451 * We present two files.
3452 * 'bad-blocks' lists sector numbers and lengths of ranges that
3453 * are recorded as bad. The list is truncated to fit within
3454 * the one-page limit of sysfs.
3455 * Writing "sector length" to this file adds an acknowledged
3457 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3458 * been acknowledged. Writing to this file adds bad blocks
3459 * without acknowledging them. This is largely for testing.
3461 static ssize_t
bb_show(struct md_rdev
*rdev
, char *page
)
3463 return badblocks_show(&rdev
->badblocks
, page
, 0);
3465 static ssize_t
bb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
3467 int rv
= badblocks_store(&rdev
->badblocks
, page
, len
, 0);
3468 /* Maybe that ack was all we needed */
3469 if (test_and_clear_bit(BlockedBadBlocks
, &rdev
->flags
))
3470 wake_up(&rdev
->blocked_wait
);
3473 static struct rdev_sysfs_entry rdev_bad_blocks
=
3474 __ATTR(bad_blocks
, S_IRUGO
|S_IWUSR
, bb_show
, bb_store
);
3476 static ssize_t
ubb_show(struct md_rdev
*rdev
, char *page
)
3478 return badblocks_show(&rdev
->badblocks
, page
, 1);
3480 static ssize_t
ubb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
3482 return badblocks_store(&rdev
->badblocks
, page
, len
, 1);
3484 static struct rdev_sysfs_entry rdev_unack_bad_blocks
=
3485 __ATTR(unacknowledged_bad_blocks
, S_IRUGO
|S_IWUSR
, ubb_show
, ubb_store
);
3488 ppl_sector_show(struct md_rdev
*rdev
, char *page
)
3490 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->ppl
.sector
);
3494 ppl_sector_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3496 unsigned long long sector
;
3498 if (kstrtoull(buf
, 10, §or
) < 0)
3500 if (sector
!= (sector_t
)sector
)
3503 if (rdev
->mddev
->pers
&& test_bit(MD_HAS_PPL
, &rdev
->mddev
->flags
) &&
3504 rdev
->raid_disk
>= 0)
3507 if (rdev
->mddev
->persistent
) {
3508 if (rdev
->mddev
->major_version
== 0)
3510 if ((sector
> rdev
->sb_start
&&
3511 sector
- rdev
->sb_start
> S16_MAX
) ||
3512 (sector
< rdev
->sb_start
&&
3513 rdev
->sb_start
- sector
> -S16_MIN
))
3515 rdev
->ppl
.offset
= sector
- rdev
->sb_start
;
3516 } else if (!rdev
->mddev
->external
) {
3519 rdev
->ppl
.sector
= sector
;
3523 static struct rdev_sysfs_entry rdev_ppl_sector
=
3524 __ATTR(ppl_sector
, S_IRUGO
|S_IWUSR
, ppl_sector_show
, ppl_sector_store
);
3527 ppl_size_show(struct md_rdev
*rdev
, char *page
)
3529 return sprintf(page
, "%u\n", rdev
->ppl
.size
);
3533 ppl_size_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3537 if (kstrtouint(buf
, 10, &size
) < 0)
3540 if (rdev
->mddev
->pers
&& test_bit(MD_HAS_PPL
, &rdev
->mddev
->flags
) &&
3541 rdev
->raid_disk
>= 0)
3544 if (rdev
->mddev
->persistent
) {
3545 if (rdev
->mddev
->major_version
== 0)
3549 } else if (!rdev
->mddev
->external
) {
3552 rdev
->ppl
.size
= size
;
3556 static struct rdev_sysfs_entry rdev_ppl_size
=
3557 __ATTR(ppl_size
, S_IRUGO
|S_IWUSR
, ppl_size_show
, ppl_size_store
);
3559 static struct attribute
*rdev_default_attrs
[] = {
3564 &rdev_new_offset
.attr
,
3566 &rdev_recovery_start
.attr
,
3567 &rdev_bad_blocks
.attr
,
3568 &rdev_unack_bad_blocks
.attr
,
3569 &rdev_ppl_sector
.attr
,
3570 &rdev_ppl_size
.attr
,
3574 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
3576 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3577 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3583 return entry
->show(rdev
, page
);
3587 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
3588 const char *page
, size_t length
)
3590 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3591 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3593 struct mddev
*mddev
= rdev
->mddev
;
3597 if (!capable(CAP_SYS_ADMIN
))
3599 rv
= mddev
? mddev_lock(mddev
) : -ENODEV
;
3601 if (rdev
->mddev
== NULL
)
3604 rv
= entry
->store(rdev
, page
, length
);
3605 mddev_unlock(mddev
);
3610 static void rdev_free(struct kobject
*ko
)
3612 struct md_rdev
*rdev
= container_of(ko
, struct md_rdev
, kobj
);
3615 static const struct sysfs_ops rdev_sysfs_ops
= {
3616 .show
= rdev_attr_show
,
3617 .store
= rdev_attr_store
,
3619 static struct kobj_type rdev_ktype
= {
3620 .release
= rdev_free
,
3621 .sysfs_ops
= &rdev_sysfs_ops
,
3622 .default_attrs
= rdev_default_attrs
,
3625 int md_rdev_init(struct md_rdev
*rdev
)
3628 rdev
->saved_raid_disk
= -1;
3629 rdev
->raid_disk
= -1;
3631 rdev
->data_offset
= 0;
3632 rdev
->new_data_offset
= 0;
3633 rdev
->sb_events
= 0;
3634 rdev
->last_read_error
= 0;
3635 rdev
->sb_loaded
= 0;
3636 rdev
->bb_page
= NULL
;
3637 atomic_set(&rdev
->nr_pending
, 0);
3638 atomic_set(&rdev
->read_errors
, 0);
3639 atomic_set(&rdev
->corrected_errors
, 0);
3641 INIT_LIST_HEAD(&rdev
->same_set
);
3642 init_waitqueue_head(&rdev
->blocked_wait
);
3644 /* Add space to store bad block list.
3645 * This reserves the space even on arrays where it cannot
3646 * be used - I wonder if that matters
3648 return badblocks_init(&rdev
->badblocks
, 0);
3650 EXPORT_SYMBOL_GPL(md_rdev_init
);
3652 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3654 * mark the device faulty if:
3656 * - the device is nonexistent (zero size)
3657 * - the device has no valid superblock
3659 * a faulty rdev _never_ has rdev->sb set.
3661 static struct md_rdev
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
3663 char b
[BDEVNAME_SIZE
];
3665 struct md_rdev
*rdev
;
3668 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
3670 return ERR_PTR(-ENOMEM
);
3672 err
= md_rdev_init(rdev
);
3675 err
= alloc_disk_sb(rdev
);
3679 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
3683 kobject_init(&rdev
->kobj
, &rdev_ktype
);
3685 size
= i_size_read(rdev
->bdev
->bd_inode
) >> BLOCK_SIZE_BITS
;
3687 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
3688 bdevname(rdev
->bdev
,b
));
3693 if (super_format
>= 0) {
3694 err
= super_types
[super_format
].
3695 load_super(rdev
, NULL
, super_minor
);
3696 if (err
== -EINVAL
) {
3697 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
3698 bdevname(rdev
->bdev
,b
),
3699 super_format
, super_minor
);
3703 pr_warn("md: could not read %s's sb, not importing!\n",
3704 bdevname(rdev
->bdev
,b
));
3714 md_rdev_clear(rdev
);
3716 return ERR_PTR(err
);
3720 * Check a full RAID array for plausibility
3723 static int analyze_sbs(struct mddev
*mddev
)
3726 struct md_rdev
*rdev
, *freshest
, *tmp
;
3727 char b
[BDEVNAME_SIZE
];
3730 rdev_for_each_safe(rdev
, tmp
, mddev
)
3731 switch (super_types
[mddev
->major_version
].
3732 load_super(rdev
, freshest
, mddev
->minor_version
)) {
3739 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
3740 bdevname(rdev
->bdev
,b
));
3741 md_kick_rdev_from_array(rdev
);
3744 /* Cannot find a valid fresh disk */
3746 pr_warn("md: cannot find a valid disk\n");
3750 super_types
[mddev
->major_version
].
3751 validate_super(mddev
, freshest
);
3754 rdev_for_each_safe(rdev
, tmp
, mddev
) {
3755 if (mddev
->max_disks
&&
3756 (rdev
->desc_nr
>= mddev
->max_disks
||
3757 i
> mddev
->max_disks
)) {
3758 pr_warn("md: %s: %s: only %d devices permitted\n",
3759 mdname(mddev
), bdevname(rdev
->bdev
, b
),
3761 md_kick_rdev_from_array(rdev
);
3764 if (rdev
!= freshest
) {
3765 if (super_types
[mddev
->major_version
].
3766 validate_super(mddev
, rdev
)) {
3767 pr_warn("md: kicking non-fresh %s from array!\n",
3768 bdevname(rdev
->bdev
,b
));
3769 md_kick_rdev_from_array(rdev
);
3773 if (mddev
->level
== LEVEL_MULTIPATH
) {
3774 rdev
->desc_nr
= i
++;
3775 rdev
->raid_disk
= rdev
->desc_nr
;
3776 set_bit(In_sync
, &rdev
->flags
);
3777 } else if (rdev
->raid_disk
>=
3778 (mddev
->raid_disks
- min(0, mddev
->delta_disks
)) &&
3779 !test_bit(Journal
, &rdev
->flags
)) {
3780 rdev
->raid_disk
= -1;
3781 clear_bit(In_sync
, &rdev
->flags
);
3788 /* Read a fixed-point number.
3789 * Numbers in sysfs attributes should be in "standard" units where
3790 * possible, so time should be in seconds.
3791 * However we internally use a a much smaller unit such as
3792 * milliseconds or jiffies.
3793 * This function takes a decimal number with a possible fractional
3794 * component, and produces an integer which is the result of
3795 * multiplying that number by 10^'scale'.
3796 * all without any floating-point arithmetic.
3798 int strict_strtoul_scaled(const char *cp
, unsigned long *res
, int scale
)
3800 unsigned long result
= 0;
3802 while (isdigit(*cp
) || (*cp
== '.' && decimals
< 0)) {
3805 else if (decimals
< scale
) {
3808 result
= result
* 10 + value
;
3820 *res
= result
* int_pow(10, scale
- decimals
);
3825 safe_delay_show(struct mddev
*mddev
, char *page
)
3827 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
3828 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
3831 safe_delay_store(struct mddev
*mddev
, const char *cbuf
, size_t len
)
3835 if (mddev_is_clustered(mddev
)) {
3836 pr_warn("md: Safemode is disabled for clustered mode\n");
3840 if (strict_strtoul_scaled(cbuf
, &msec
, 3) < 0)
3843 mddev
->safemode_delay
= 0;
3845 unsigned long old_delay
= mddev
->safemode_delay
;
3846 unsigned long new_delay
= (msec
*HZ
)/1000;
3850 mddev
->safemode_delay
= new_delay
;
3851 if (new_delay
< old_delay
|| old_delay
== 0)
3852 mod_timer(&mddev
->safemode_timer
, jiffies
+1);
3856 static struct md_sysfs_entry md_safe_delay
=
3857 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
3860 level_show(struct mddev
*mddev
, char *page
)
3862 struct md_personality
*p
;
3864 spin_lock(&mddev
->lock
);
3867 ret
= sprintf(page
, "%s\n", p
->name
);
3868 else if (mddev
->clevel
[0])
3869 ret
= sprintf(page
, "%s\n", mddev
->clevel
);
3870 else if (mddev
->level
!= LEVEL_NONE
)
3871 ret
= sprintf(page
, "%d\n", mddev
->level
);
3874 spin_unlock(&mddev
->lock
);
3879 level_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3884 struct md_personality
*pers
, *oldpers
;
3886 void *priv
, *oldpriv
;
3887 struct md_rdev
*rdev
;
3889 if (slen
== 0 || slen
>= sizeof(clevel
))
3892 rv
= mddev_lock(mddev
);
3896 if (mddev
->pers
== NULL
) {
3897 strncpy(mddev
->clevel
, buf
, slen
);
3898 if (mddev
->clevel
[slen
-1] == '\n')
3900 mddev
->clevel
[slen
] = 0;
3901 mddev
->level
= LEVEL_NONE
;
3909 /* request to change the personality. Need to ensure:
3910 * - array is not engaged in resync/recovery/reshape
3911 * - old personality can be suspended
3912 * - new personality will access other array.
3916 if (mddev
->sync_thread
||
3917 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3918 mddev
->reshape_position
!= MaxSector
||
3919 mddev
->sysfs_active
)
3923 if (!mddev
->pers
->quiesce
) {
3924 pr_warn("md: %s: %s does not support online personality change\n",
3925 mdname(mddev
), mddev
->pers
->name
);
3929 /* Now find the new personality */
3930 strncpy(clevel
, buf
, slen
);
3931 if (clevel
[slen
-1] == '\n')
3934 if (kstrtol(clevel
, 10, &level
))
3937 if (request_module("md-%s", clevel
) != 0)
3938 request_module("md-level-%s", clevel
);
3939 spin_lock(&pers_lock
);
3940 pers
= find_pers(level
, clevel
);
3941 if (!pers
|| !try_module_get(pers
->owner
)) {
3942 spin_unlock(&pers_lock
);
3943 pr_warn("md: personality %s not loaded\n", clevel
);
3947 spin_unlock(&pers_lock
);
3949 if (pers
== mddev
->pers
) {
3950 /* Nothing to do! */
3951 module_put(pers
->owner
);
3955 if (!pers
->takeover
) {
3956 module_put(pers
->owner
);
3957 pr_warn("md: %s: %s does not support personality takeover\n",
3958 mdname(mddev
), clevel
);
3963 rdev_for_each(rdev
, mddev
)
3964 rdev
->new_raid_disk
= rdev
->raid_disk
;
3966 /* ->takeover must set new_* and/or delta_disks
3967 * if it succeeds, and may set them when it fails.
3969 priv
= pers
->takeover(mddev
);
3971 mddev
->new_level
= mddev
->level
;
3972 mddev
->new_layout
= mddev
->layout
;
3973 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3974 mddev
->raid_disks
-= mddev
->delta_disks
;
3975 mddev
->delta_disks
= 0;
3976 mddev
->reshape_backwards
= 0;
3977 module_put(pers
->owner
);
3978 pr_warn("md: %s: %s would not accept array\n",
3979 mdname(mddev
), clevel
);
3984 /* Looks like we have a winner */
3985 mddev_suspend(mddev
);
3986 mddev_detach(mddev
);
3988 spin_lock(&mddev
->lock
);
3989 oldpers
= mddev
->pers
;
3990 oldpriv
= mddev
->private;
3992 mddev
->private = priv
;
3993 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3994 mddev
->level
= mddev
->new_level
;
3995 mddev
->layout
= mddev
->new_layout
;
3996 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
3997 mddev
->delta_disks
= 0;
3998 mddev
->reshape_backwards
= 0;
3999 mddev
->degraded
= 0;
4000 spin_unlock(&mddev
->lock
);
4002 if (oldpers
->sync_request
== NULL
&&
4004 /* We are converting from a no-redundancy array
4005 * to a redundancy array and metadata is managed
4006 * externally so we need to be sure that writes
4007 * won't block due to a need to transition
4009 * until external management is started.
4012 mddev
->safemode_delay
= 0;
4013 mddev
->safemode
= 0;
4016 oldpers
->free(mddev
, oldpriv
);
4018 if (oldpers
->sync_request
== NULL
&&
4019 pers
->sync_request
!= NULL
) {
4020 /* need to add the md_redundancy_group */
4021 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
4022 pr_warn("md: cannot register extra attributes for %s\n",
4024 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, "sync_action");
4026 if (oldpers
->sync_request
!= NULL
&&
4027 pers
->sync_request
== NULL
) {
4028 /* need to remove the md_redundancy_group */
4029 if (mddev
->to_remove
== NULL
)
4030 mddev
->to_remove
= &md_redundancy_group
;
4033 module_put(oldpers
->owner
);
4035 rdev_for_each(rdev
, mddev
) {
4036 if (rdev
->raid_disk
< 0)
4038 if (rdev
->new_raid_disk
>= mddev
->raid_disks
)
4039 rdev
->new_raid_disk
= -1;
4040 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
4042 sysfs_unlink_rdev(mddev
, rdev
);
4044 rdev_for_each(rdev
, mddev
) {
4045 if (rdev
->raid_disk
< 0)
4047 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
4049 rdev
->raid_disk
= rdev
->new_raid_disk
;
4050 if (rdev
->raid_disk
< 0)
4051 clear_bit(In_sync
, &rdev
->flags
);
4053 if (sysfs_link_rdev(mddev
, rdev
))
4054 pr_warn("md: cannot register rd%d for %s after level change\n",
4055 rdev
->raid_disk
, mdname(mddev
));
4059 if (pers
->sync_request
== NULL
) {
4060 /* this is now an array without redundancy, so
4061 * it must always be in_sync
4064 del_timer_sync(&mddev
->safemode_timer
);
4066 blk_set_stacking_limits(&mddev
->queue
->limits
);
4068 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
4069 mddev_resume(mddev
);
4071 md_update_sb(mddev
, 1);
4072 sysfs_notify(&mddev
->kobj
, NULL
, "level");
4073 md_new_event(mddev
);
4076 mddev_unlock(mddev
);
4080 static struct md_sysfs_entry md_level
=
4081 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
4084 layout_show(struct mddev
*mddev
, char *page
)
4086 /* just a number, not meaningful for all levels */
4087 if (mddev
->reshape_position
!= MaxSector
&&
4088 mddev
->layout
!= mddev
->new_layout
)
4089 return sprintf(page
, "%d (%d)\n",
4090 mddev
->new_layout
, mddev
->layout
);
4091 return sprintf(page
, "%d\n", mddev
->layout
);
4095 layout_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4100 err
= kstrtouint(buf
, 10, &n
);
4103 err
= mddev_lock(mddev
);
4108 if (mddev
->pers
->check_reshape
== NULL
)
4113 mddev
->new_layout
= n
;
4114 err
= mddev
->pers
->check_reshape(mddev
);
4116 mddev
->new_layout
= mddev
->layout
;
4119 mddev
->new_layout
= n
;
4120 if (mddev
->reshape_position
== MaxSector
)
4123 mddev_unlock(mddev
);
4126 static struct md_sysfs_entry md_layout
=
4127 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
4130 raid_disks_show(struct mddev
*mddev
, char *page
)
4132 if (mddev
->raid_disks
== 0)
4134 if (mddev
->reshape_position
!= MaxSector
&&
4135 mddev
->delta_disks
!= 0)
4136 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
4137 mddev
->raid_disks
- mddev
->delta_disks
);
4138 return sprintf(page
, "%d\n", mddev
->raid_disks
);
4141 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
);
4144 raid_disks_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4149 err
= kstrtouint(buf
, 10, &n
);
4153 err
= mddev_lock(mddev
);
4157 err
= update_raid_disks(mddev
, n
);
4158 else if (mddev
->reshape_position
!= MaxSector
) {
4159 struct md_rdev
*rdev
;
4160 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
4163 rdev_for_each(rdev
, mddev
) {
4165 rdev
->data_offset
< rdev
->new_data_offset
)
4168 rdev
->data_offset
> rdev
->new_data_offset
)
4172 mddev
->delta_disks
= n
- olddisks
;
4173 mddev
->raid_disks
= n
;
4174 mddev
->reshape_backwards
= (mddev
->delta_disks
< 0);
4176 mddev
->raid_disks
= n
;
4178 mddev_unlock(mddev
);
4179 return err
? err
: len
;
4181 static struct md_sysfs_entry md_raid_disks
=
4182 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
4185 chunk_size_show(struct mddev
*mddev
, char *page
)
4187 if (mddev
->reshape_position
!= MaxSector
&&
4188 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
4189 return sprintf(page
, "%d (%d)\n",
4190 mddev
->new_chunk_sectors
<< 9,
4191 mddev
->chunk_sectors
<< 9);
4192 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
4196 chunk_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4201 err
= kstrtoul(buf
, 10, &n
);
4205 err
= mddev_lock(mddev
);
4209 if (mddev
->pers
->check_reshape
== NULL
)
4214 mddev
->new_chunk_sectors
= n
>> 9;
4215 err
= mddev
->pers
->check_reshape(mddev
);
4217 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4220 mddev
->new_chunk_sectors
= n
>> 9;
4221 if (mddev
->reshape_position
== MaxSector
)
4222 mddev
->chunk_sectors
= n
>> 9;
4224 mddev_unlock(mddev
);
4227 static struct md_sysfs_entry md_chunk_size
=
4228 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
4231 resync_start_show(struct mddev
*mddev
, char *page
)
4233 if (mddev
->recovery_cp
== MaxSector
)
4234 return sprintf(page
, "none\n");
4235 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
4239 resync_start_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4241 unsigned long long n
;
4244 if (cmd_match(buf
, "none"))
4247 err
= kstrtoull(buf
, 10, &n
);
4250 if (n
!= (sector_t
)n
)
4254 err
= mddev_lock(mddev
);
4257 if (mddev
->pers
&& !test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
4261 mddev
->recovery_cp
= n
;
4263 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
4265 mddev_unlock(mddev
);
4268 static struct md_sysfs_entry md_resync_start
=
4269 __ATTR_PREALLOC(resync_start
, S_IRUGO
|S_IWUSR
,
4270 resync_start_show
, resync_start_store
);
4273 * The array state can be:
4276 * No devices, no size, no level
4277 * Equivalent to STOP_ARRAY ioctl
4279 * May have some settings, but array is not active
4280 * all IO results in error
4281 * When written, doesn't tear down array, but just stops it
4282 * suspended (not supported yet)
4283 * All IO requests will block. The array can be reconfigured.
4284 * Writing this, if accepted, will block until array is quiescent
4286 * no resync can happen. no superblocks get written.
4287 * write requests fail
4289 * like readonly, but behaves like 'clean' on a write request.
4291 * clean - no pending writes, but otherwise active.
4292 * When written to inactive array, starts without resync
4293 * If a write request arrives then
4294 * if metadata is known, mark 'dirty' and switch to 'active'.
4295 * if not known, block and switch to write-pending
4296 * If written to an active array that has pending writes, then fails.
4298 * fully active: IO and resync can be happening.
4299 * When written to inactive array, starts with resync
4302 * clean, but writes are blocked waiting for 'active' to be written.
4305 * like active, but no writes have been seen for a while (100msec).
4308 * RAID0/LINEAR-only: same as clean, but array is missing a member.
4309 * It's useful because RAID0/LINEAR mounted-arrays aren't stopped
4310 * when a member is gone, so this state will at least alert the
4311 * user that something is wrong.
4313 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
4314 write_pending
, active_idle
, broken
, bad_word
};
4315 static char *array_states
[] = {
4316 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
4317 "write-pending", "active-idle", "broken", NULL
};
4319 static int match_word(const char *word
, char **list
)
4322 for (n
=0; list
[n
]; n
++)
4323 if (cmd_match(word
, list
[n
]))
4329 array_state_show(struct mddev
*mddev
, char *page
)
4331 enum array_state st
= inactive
;
4333 if (mddev
->pers
&& !test_bit(MD_NOT_READY
, &mddev
->flags
)) {
4342 spin_lock(&mddev
->lock
);
4343 if (test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
))
4345 else if (mddev
->in_sync
)
4347 else if (mddev
->safemode
)
4351 spin_unlock(&mddev
->lock
);
4354 if (test_bit(MD_BROKEN
, &mddev
->flags
) && st
== clean
)
4357 if (list_empty(&mddev
->disks
) &&
4358 mddev
->raid_disks
== 0 &&
4359 mddev
->dev_sectors
== 0)
4364 return sprintf(page
, "%s\n", array_states
[st
]);
4367 static int do_md_stop(struct mddev
*mddev
, int ro
, struct block_device
*bdev
);
4368 static int md_set_readonly(struct mddev
*mddev
, struct block_device
*bdev
);
4369 static int do_md_run(struct mddev
*mddev
);
4370 static int restart_array(struct mddev
*mddev
);
4373 array_state_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4376 enum array_state st
= match_word(buf
, array_states
);
4378 if (mddev
->pers
&& (st
== active
|| st
== clean
) && mddev
->ro
!= 1) {
4379 /* don't take reconfig_mutex when toggling between
4382 spin_lock(&mddev
->lock
);
4384 restart_array(mddev
);
4385 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
4386 md_wakeup_thread(mddev
->thread
);
4387 wake_up(&mddev
->sb_wait
);
4388 } else /* st == clean */ {
4389 restart_array(mddev
);
4390 if (!set_in_sync(mddev
))
4394 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4395 spin_unlock(&mddev
->lock
);
4398 err
= mddev_lock(mddev
);
4406 /* stopping an active array */
4407 err
= do_md_stop(mddev
, 0, NULL
);
4410 /* stopping an active array */
4412 err
= do_md_stop(mddev
, 2, NULL
);
4414 err
= 0; /* already inactive */
4417 break; /* not supported yet */
4420 err
= md_set_readonly(mddev
, NULL
);
4423 set_disk_ro(mddev
->gendisk
, 1);
4424 err
= do_md_run(mddev
);
4430 err
= md_set_readonly(mddev
, NULL
);
4431 else if (mddev
->ro
== 1)
4432 err
= restart_array(mddev
);
4435 set_disk_ro(mddev
->gendisk
, 0);
4439 err
= do_md_run(mddev
);
4444 err
= restart_array(mddev
);
4447 spin_lock(&mddev
->lock
);
4448 if (!set_in_sync(mddev
))
4450 spin_unlock(&mddev
->lock
);
4456 err
= restart_array(mddev
);
4459 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
4460 wake_up(&mddev
->sb_wait
);
4464 set_disk_ro(mddev
->gendisk
, 0);
4465 err
= do_md_run(mddev
);
4471 /* these cannot be set */
4476 if (mddev
->hold_active
== UNTIL_IOCTL
)
4477 mddev
->hold_active
= 0;
4478 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4480 mddev_unlock(mddev
);
4483 static struct md_sysfs_entry md_array_state
=
4484 __ATTR_PREALLOC(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
4487 max_corrected_read_errors_show(struct mddev
*mddev
, char *page
) {
4488 return sprintf(page
, "%d\n",
4489 atomic_read(&mddev
->max_corr_read_errors
));
4493 max_corrected_read_errors_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4498 rv
= kstrtouint(buf
, 10, &n
);
4501 atomic_set(&mddev
->max_corr_read_errors
, n
);
4505 static struct md_sysfs_entry max_corr_read_errors
=
4506 __ATTR(max_read_errors
, S_IRUGO
|S_IWUSR
, max_corrected_read_errors_show
,
4507 max_corrected_read_errors_store
);
4510 null_show(struct mddev
*mddev
, char *page
)
4516 new_dev_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4518 /* buf must be %d:%d\n? giving major and minor numbers */
4519 /* The new device is added to the array.
4520 * If the array has a persistent superblock, we read the
4521 * superblock to initialise info and check validity.
4522 * Otherwise, only checking done is that in bind_rdev_to_array,
4523 * which mainly checks size.
4526 int major
= simple_strtoul(buf
, &e
, 10);
4529 struct md_rdev
*rdev
;
4532 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
4534 minor
= simple_strtoul(e
+1, &e
, 10);
4535 if (*e
&& *e
!= '\n')
4537 dev
= MKDEV(major
, minor
);
4538 if (major
!= MAJOR(dev
) ||
4539 minor
!= MINOR(dev
))
4542 flush_workqueue(md_misc_wq
);
4544 err
= mddev_lock(mddev
);
4547 if (mddev
->persistent
) {
4548 rdev
= md_import_device(dev
, mddev
->major_version
,
4549 mddev
->minor_version
);
4550 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
4551 struct md_rdev
*rdev0
4552 = list_entry(mddev
->disks
.next
,
4553 struct md_rdev
, same_set
);
4554 err
= super_types
[mddev
->major_version
]
4555 .load_super(rdev
, rdev0
, mddev
->minor_version
);
4559 } else if (mddev
->external
)
4560 rdev
= md_import_device(dev
, -2, -1);
4562 rdev
= md_import_device(dev
, -1, -1);
4565 mddev_unlock(mddev
);
4566 return PTR_ERR(rdev
);
4568 err
= bind_rdev_to_array(rdev
, mddev
);
4572 mddev_unlock(mddev
);
4574 md_new_event(mddev
);
4575 return err
? err
: len
;
4578 static struct md_sysfs_entry md_new_device
=
4579 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
4582 bitmap_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4585 unsigned long chunk
, end_chunk
;
4588 err
= mddev_lock(mddev
);
4593 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4595 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
4596 if (buf
== end
) break;
4597 if (*end
== '-') { /* range */
4599 end_chunk
= simple_strtoul(buf
, &end
, 0);
4600 if (buf
== end
) break;
4602 if (*end
&& !isspace(*end
)) break;
4603 md_bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
4604 buf
= skip_spaces(end
);
4606 md_bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
4608 mddev_unlock(mddev
);
4612 static struct md_sysfs_entry md_bitmap
=
4613 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
4616 size_show(struct mddev
*mddev
, char *page
)
4618 return sprintf(page
, "%llu\n",
4619 (unsigned long long)mddev
->dev_sectors
/ 2);
4622 static int update_size(struct mddev
*mddev
, sector_t num_sectors
);
4625 size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4627 /* If array is inactive, we can reduce the component size, but
4628 * not increase it (except from 0).
4629 * If array is active, we can try an on-line resize
4632 int err
= strict_blocks_to_sectors(buf
, §ors
);
4636 err
= mddev_lock(mddev
);
4640 err
= update_size(mddev
, sectors
);
4642 md_update_sb(mddev
, 1);
4644 if (mddev
->dev_sectors
== 0 ||
4645 mddev
->dev_sectors
> sectors
)
4646 mddev
->dev_sectors
= sectors
;
4650 mddev_unlock(mddev
);
4651 return err
? err
: len
;
4654 static struct md_sysfs_entry md_size
=
4655 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
4657 /* Metadata version.
4659 * 'none' for arrays with no metadata (good luck...)
4660 * 'external' for arrays with externally managed metadata,
4661 * or N.M for internally known formats
4664 metadata_show(struct mddev
*mddev
, char *page
)
4666 if (mddev
->persistent
)
4667 return sprintf(page
, "%d.%d\n",
4668 mddev
->major_version
, mddev
->minor_version
);
4669 else if (mddev
->external
)
4670 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
4672 return sprintf(page
, "none\n");
4676 metadata_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4681 /* Changing the details of 'external' metadata is
4682 * always permitted. Otherwise there must be
4683 * no devices attached to the array.
4686 err
= mddev_lock(mddev
);
4690 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
4692 else if (!list_empty(&mddev
->disks
))
4696 if (cmd_match(buf
, "none")) {
4697 mddev
->persistent
= 0;
4698 mddev
->external
= 0;
4699 mddev
->major_version
= 0;
4700 mddev
->minor_version
= 90;
4703 if (strncmp(buf
, "external:", 9) == 0) {
4704 size_t namelen
= len
-9;
4705 if (namelen
>= sizeof(mddev
->metadata_type
))
4706 namelen
= sizeof(mddev
->metadata_type
)-1;
4707 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
4708 mddev
->metadata_type
[namelen
] = 0;
4709 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
4710 mddev
->metadata_type
[--namelen
] = 0;
4711 mddev
->persistent
= 0;
4712 mddev
->external
= 1;
4713 mddev
->major_version
= 0;
4714 mddev
->minor_version
= 90;
4717 major
= simple_strtoul(buf
, &e
, 10);
4719 if (e
==buf
|| *e
!= '.')
4722 minor
= simple_strtoul(buf
, &e
, 10);
4723 if (e
==buf
|| (*e
&& *e
!= '\n') )
4726 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
4728 mddev
->major_version
= major
;
4729 mddev
->minor_version
= minor
;
4730 mddev
->persistent
= 1;
4731 mddev
->external
= 0;
4734 mddev_unlock(mddev
);
4738 static struct md_sysfs_entry md_metadata
=
4739 __ATTR_PREALLOC(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
4742 action_show(struct mddev
*mddev
, char *page
)
4744 char *type
= "idle";
4745 unsigned long recovery
= mddev
->recovery
;
4746 if (test_bit(MD_RECOVERY_FROZEN
, &recovery
))
4748 else if (test_bit(MD_RECOVERY_RUNNING
, &recovery
) ||
4749 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &recovery
))) {
4750 if (test_bit(MD_RECOVERY_RESHAPE
, &recovery
))
4752 else if (test_bit(MD_RECOVERY_SYNC
, &recovery
)) {
4753 if (!test_bit(MD_RECOVERY_REQUESTED
, &recovery
))
4755 else if (test_bit(MD_RECOVERY_CHECK
, &recovery
))
4759 } else if (test_bit(MD_RECOVERY_RECOVER
, &recovery
))
4761 else if (mddev
->reshape_position
!= MaxSector
)
4764 return sprintf(page
, "%s\n", type
);
4768 action_store(struct mddev
*mddev
, const char *page
, size_t len
)
4770 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
4774 if (cmd_match(page
, "idle") || cmd_match(page
, "frozen")) {
4775 if (cmd_match(page
, "frozen"))
4776 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4778 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4779 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
4780 mddev_lock(mddev
) == 0) {
4781 flush_workqueue(md_misc_wq
);
4782 if (mddev
->sync_thread
) {
4783 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4784 md_reap_sync_thread(mddev
);
4786 mddev_unlock(mddev
);
4788 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4790 else if (cmd_match(page
, "resync"))
4791 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4792 else if (cmd_match(page
, "recover")) {
4793 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4794 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
4795 } else if (cmd_match(page
, "reshape")) {
4797 if (mddev
->pers
->start_reshape
== NULL
)
4799 err
= mddev_lock(mddev
);
4801 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4804 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4805 err
= mddev
->pers
->start_reshape(mddev
);
4807 mddev_unlock(mddev
);
4811 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4813 if (cmd_match(page
, "check"))
4814 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
4815 else if (!cmd_match(page
, "repair"))
4817 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4818 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
4819 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
4821 if (mddev
->ro
== 2) {
4822 /* A write to sync_action is enough to justify
4823 * canceling read-auto mode
4826 md_wakeup_thread(mddev
->sync_thread
);
4828 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4829 md_wakeup_thread(mddev
->thread
);
4830 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4834 static struct md_sysfs_entry md_scan_mode
=
4835 __ATTR_PREALLOC(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
4838 last_sync_action_show(struct mddev
*mddev
, char *page
)
4840 return sprintf(page
, "%s\n", mddev
->last_sync_action
);
4843 static struct md_sysfs_entry md_last_scan_mode
= __ATTR_RO(last_sync_action
);
4846 mismatch_cnt_show(struct mddev
*mddev
, char *page
)
4848 return sprintf(page
, "%llu\n",
4849 (unsigned long long)
4850 atomic64_read(&mddev
->resync_mismatches
));
4853 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
4856 sync_min_show(struct mddev
*mddev
, char *page
)
4858 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
4859 mddev
->sync_speed_min
? "local": "system");
4863 sync_min_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4868 if (strncmp(buf
, "system", 6)==0) {
4871 rv
= kstrtouint(buf
, 10, &min
);
4877 mddev
->sync_speed_min
= min
;
4881 static struct md_sysfs_entry md_sync_min
=
4882 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
4885 sync_max_show(struct mddev
*mddev
, char *page
)
4887 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
4888 mddev
->sync_speed_max
? "local": "system");
4892 sync_max_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4897 if (strncmp(buf
, "system", 6)==0) {
4900 rv
= kstrtouint(buf
, 10, &max
);
4906 mddev
->sync_speed_max
= max
;
4910 static struct md_sysfs_entry md_sync_max
=
4911 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
4914 degraded_show(struct mddev
*mddev
, char *page
)
4916 return sprintf(page
, "%d\n", mddev
->degraded
);
4918 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
4921 sync_force_parallel_show(struct mddev
*mddev
, char *page
)
4923 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
4927 sync_force_parallel_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4931 if (kstrtol(buf
, 10, &n
))
4934 if (n
!= 0 && n
!= 1)
4937 mddev
->parallel_resync
= n
;
4939 if (mddev
->sync_thread
)
4940 wake_up(&resync_wait
);
4945 /* force parallel resync, even with shared block devices */
4946 static struct md_sysfs_entry md_sync_force_parallel
=
4947 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
4948 sync_force_parallel_show
, sync_force_parallel_store
);
4951 sync_speed_show(struct mddev
*mddev
, char *page
)
4953 unsigned long resync
, dt
, db
;
4954 if (mddev
->curr_resync
== 0)
4955 return sprintf(page
, "none\n");
4956 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
4957 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
4959 db
= resync
- mddev
->resync_mark_cnt
;
4960 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
4963 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
4966 sync_completed_show(struct mddev
*mddev
, char *page
)
4968 unsigned long long max_sectors
, resync
;
4970 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4971 return sprintf(page
, "none\n");
4973 if (mddev
->curr_resync
== 1 ||
4974 mddev
->curr_resync
== 2)
4975 return sprintf(page
, "delayed\n");
4977 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
4978 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
4979 max_sectors
= mddev
->resync_max_sectors
;
4981 max_sectors
= mddev
->dev_sectors
;
4983 resync
= mddev
->curr_resync_completed
;
4984 return sprintf(page
, "%llu / %llu\n", resync
, max_sectors
);
4987 static struct md_sysfs_entry md_sync_completed
=
4988 __ATTR_PREALLOC(sync_completed
, S_IRUGO
, sync_completed_show
, NULL
);
4991 min_sync_show(struct mddev
*mddev
, char *page
)
4993 return sprintf(page
, "%llu\n",
4994 (unsigned long long)mddev
->resync_min
);
4997 min_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4999 unsigned long long min
;
5002 if (kstrtoull(buf
, 10, &min
))
5005 spin_lock(&mddev
->lock
);
5007 if (min
> mddev
->resync_max
)
5011 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
5014 /* Round down to multiple of 4K for safety */
5015 mddev
->resync_min
= round_down(min
, 8);
5019 spin_unlock(&mddev
->lock
);
5023 static struct md_sysfs_entry md_min_sync
=
5024 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
5027 max_sync_show(struct mddev
*mddev
, char *page
)
5029 if (mddev
->resync_max
== MaxSector
)
5030 return sprintf(page
, "max\n");
5032 return sprintf(page
, "%llu\n",
5033 (unsigned long long)mddev
->resync_max
);
5036 max_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5039 spin_lock(&mddev
->lock
);
5040 if (strncmp(buf
, "max", 3) == 0)
5041 mddev
->resync_max
= MaxSector
;
5043 unsigned long long max
;
5047 if (kstrtoull(buf
, 10, &max
))
5049 if (max
< mddev
->resync_min
)
5053 if (max
< mddev
->resync_max
&&
5055 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
5058 /* Must be a multiple of chunk_size */
5059 chunk
= mddev
->chunk_sectors
;
5061 sector_t temp
= max
;
5064 if (sector_div(temp
, chunk
))
5067 mddev
->resync_max
= max
;
5069 wake_up(&mddev
->recovery_wait
);
5072 spin_unlock(&mddev
->lock
);
5076 static struct md_sysfs_entry md_max_sync
=
5077 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
5080 suspend_lo_show(struct mddev
*mddev
, char *page
)
5082 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
5086 suspend_lo_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5088 unsigned long long new;
5091 err
= kstrtoull(buf
, 10, &new);
5094 if (new != (sector_t
)new)
5097 err
= mddev_lock(mddev
);
5101 if (mddev
->pers
== NULL
||
5102 mddev
->pers
->quiesce
== NULL
)
5104 mddev_suspend(mddev
);
5105 mddev
->suspend_lo
= new;
5106 mddev_resume(mddev
);
5110 mddev_unlock(mddev
);
5113 static struct md_sysfs_entry md_suspend_lo
=
5114 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
5117 suspend_hi_show(struct mddev
*mddev
, char *page
)
5119 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
5123 suspend_hi_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5125 unsigned long long new;
5128 err
= kstrtoull(buf
, 10, &new);
5131 if (new != (sector_t
)new)
5134 err
= mddev_lock(mddev
);
5138 if (mddev
->pers
== NULL
)
5141 mddev_suspend(mddev
);
5142 mddev
->suspend_hi
= new;
5143 mddev_resume(mddev
);
5147 mddev_unlock(mddev
);
5150 static struct md_sysfs_entry md_suspend_hi
=
5151 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
5154 reshape_position_show(struct mddev
*mddev
, char *page
)
5156 if (mddev
->reshape_position
!= MaxSector
)
5157 return sprintf(page
, "%llu\n",
5158 (unsigned long long)mddev
->reshape_position
);
5159 strcpy(page
, "none\n");
5164 reshape_position_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5166 struct md_rdev
*rdev
;
5167 unsigned long long new;
5170 err
= kstrtoull(buf
, 10, &new);
5173 if (new != (sector_t
)new)
5175 err
= mddev_lock(mddev
);
5181 mddev
->reshape_position
= new;
5182 mddev
->delta_disks
= 0;
5183 mddev
->reshape_backwards
= 0;
5184 mddev
->new_level
= mddev
->level
;
5185 mddev
->new_layout
= mddev
->layout
;
5186 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
5187 rdev_for_each(rdev
, mddev
)
5188 rdev
->new_data_offset
= rdev
->data_offset
;
5191 mddev_unlock(mddev
);
5195 static struct md_sysfs_entry md_reshape_position
=
5196 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
5197 reshape_position_store
);
5200 reshape_direction_show(struct mddev
*mddev
, char *page
)
5202 return sprintf(page
, "%s\n",
5203 mddev
->reshape_backwards
? "backwards" : "forwards");
5207 reshape_direction_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5212 if (cmd_match(buf
, "forwards"))
5214 else if (cmd_match(buf
, "backwards"))
5218 if (mddev
->reshape_backwards
== backwards
)
5221 err
= mddev_lock(mddev
);
5224 /* check if we are allowed to change */
5225 if (mddev
->delta_disks
)
5227 else if (mddev
->persistent
&&
5228 mddev
->major_version
== 0)
5231 mddev
->reshape_backwards
= backwards
;
5232 mddev_unlock(mddev
);
5236 static struct md_sysfs_entry md_reshape_direction
=
5237 __ATTR(reshape_direction
, S_IRUGO
|S_IWUSR
, reshape_direction_show
,
5238 reshape_direction_store
);
5241 array_size_show(struct mddev
*mddev
, char *page
)
5243 if (mddev
->external_size
)
5244 return sprintf(page
, "%llu\n",
5245 (unsigned long long)mddev
->array_sectors
/2);
5247 return sprintf(page
, "default\n");
5251 array_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5256 err
= mddev_lock(mddev
);
5260 /* cluster raid doesn't support change array_sectors */
5261 if (mddev_is_clustered(mddev
)) {
5262 mddev_unlock(mddev
);
5266 if (strncmp(buf
, "default", 7) == 0) {
5268 sectors
= mddev
->pers
->size(mddev
, 0, 0);
5270 sectors
= mddev
->array_sectors
;
5272 mddev
->external_size
= 0;
5274 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
5276 else if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
5279 mddev
->external_size
= 1;
5283 mddev
->array_sectors
= sectors
;
5285 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
5286 revalidate_disk(mddev
->gendisk
);
5289 mddev_unlock(mddev
);
5293 static struct md_sysfs_entry md_array_size
=
5294 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
5298 consistency_policy_show(struct mddev
*mddev
, char *page
)
5302 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
)) {
5303 ret
= sprintf(page
, "journal\n");
5304 } else if (test_bit(MD_HAS_PPL
, &mddev
->flags
)) {
5305 ret
= sprintf(page
, "ppl\n");
5306 } else if (mddev
->bitmap
) {
5307 ret
= sprintf(page
, "bitmap\n");
5308 } else if (mddev
->pers
) {
5309 if (mddev
->pers
->sync_request
)
5310 ret
= sprintf(page
, "resync\n");
5312 ret
= sprintf(page
, "none\n");
5314 ret
= sprintf(page
, "unknown\n");
5321 consistency_policy_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5326 if (mddev
->pers
->change_consistency_policy
)
5327 err
= mddev
->pers
->change_consistency_policy(mddev
, buf
);
5330 } else if (mddev
->external
&& strncmp(buf
, "ppl", 3) == 0) {
5331 set_bit(MD_HAS_PPL
, &mddev
->flags
);
5336 return err
? err
: len
;
5339 static struct md_sysfs_entry md_consistency_policy
=
5340 __ATTR(consistency_policy
, S_IRUGO
| S_IWUSR
, consistency_policy_show
,
5341 consistency_policy_store
);
5343 static ssize_t
fail_last_dev_show(struct mddev
*mddev
, char *page
)
5345 return sprintf(page
, "%d\n", mddev
->fail_last_dev
);
5349 * Setting fail_last_dev to true to allow last device to be forcibly removed
5350 * from RAID1/RAID10.
5353 fail_last_dev_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5358 ret
= kstrtobool(buf
, &value
);
5362 if (value
!= mddev
->fail_last_dev
)
5363 mddev
->fail_last_dev
= value
;
5367 static struct md_sysfs_entry md_fail_last_dev
=
5368 __ATTR(fail_last_dev
, S_IRUGO
| S_IWUSR
, fail_last_dev_show
,
5369 fail_last_dev_store
);
5371 static ssize_t
serialize_policy_show(struct mddev
*mddev
, char *page
)
5373 if (mddev
->pers
== NULL
|| (mddev
->pers
->level
!= 1))
5374 return sprintf(page
, "n/a\n");
5376 return sprintf(page
, "%d\n", mddev
->serialize_policy
);
5380 * Setting serialize_policy to true to enforce write IO is not reordered
5384 serialize_policy_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5389 err
= kstrtobool(buf
, &value
);
5393 if (value
== mddev
->serialize_policy
)
5396 err
= mddev_lock(mddev
);
5399 if (mddev
->pers
== NULL
|| (mddev
->pers
->level
!= 1)) {
5400 pr_err("md: serialize_policy is only effective for raid1\n");
5405 mddev_suspend(mddev
);
5407 mddev_create_serial_pool(mddev
, NULL
, true);
5409 mddev_destroy_serial_pool(mddev
, NULL
, true);
5410 mddev
->serialize_policy
= value
;
5411 mddev_resume(mddev
);
5413 mddev_unlock(mddev
);
5417 static struct md_sysfs_entry md_serialize_policy
=
5418 __ATTR(serialize_policy
, S_IRUGO
| S_IWUSR
, serialize_policy_show
,
5419 serialize_policy_store
);
5422 static struct attribute
*md_default_attrs
[] = {
5425 &md_raid_disks
.attr
,
5426 &md_chunk_size
.attr
,
5428 &md_resync_start
.attr
,
5430 &md_new_device
.attr
,
5431 &md_safe_delay
.attr
,
5432 &md_array_state
.attr
,
5433 &md_reshape_position
.attr
,
5434 &md_reshape_direction
.attr
,
5435 &md_array_size
.attr
,
5436 &max_corr_read_errors
.attr
,
5437 &md_consistency_policy
.attr
,
5438 &md_fail_last_dev
.attr
,
5439 &md_serialize_policy
.attr
,
5443 static struct attribute
*md_redundancy_attrs
[] = {
5445 &md_last_scan_mode
.attr
,
5446 &md_mismatches
.attr
,
5449 &md_sync_speed
.attr
,
5450 &md_sync_force_parallel
.attr
,
5451 &md_sync_completed
.attr
,
5454 &md_suspend_lo
.attr
,
5455 &md_suspend_hi
.attr
,
5460 static struct attribute_group md_redundancy_group
= {
5462 .attrs
= md_redundancy_attrs
,
5466 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
5468 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
5469 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
5474 spin_lock(&all_mddevs_lock
);
5475 if (list_empty(&mddev
->all_mddevs
)) {
5476 spin_unlock(&all_mddevs_lock
);
5480 spin_unlock(&all_mddevs_lock
);
5482 rv
= entry
->show(mddev
, page
);
5488 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
5489 const char *page
, size_t length
)
5491 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
5492 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
5497 if (!capable(CAP_SYS_ADMIN
))
5499 spin_lock(&all_mddevs_lock
);
5500 if (list_empty(&mddev
->all_mddevs
)) {
5501 spin_unlock(&all_mddevs_lock
);
5505 spin_unlock(&all_mddevs_lock
);
5506 rv
= entry
->store(mddev
, page
, length
);
5511 static void md_free(struct kobject
*ko
)
5513 struct mddev
*mddev
= container_of(ko
, struct mddev
, kobj
);
5515 if (mddev
->sysfs_state
)
5516 sysfs_put(mddev
->sysfs_state
);
5519 del_gendisk(mddev
->gendisk
);
5521 blk_cleanup_queue(mddev
->queue
);
5523 put_disk(mddev
->gendisk
);
5524 percpu_ref_exit(&mddev
->writes_pending
);
5526 bioset_exit(&mddev
->bio_set
);
5527 bioset_exit(&mddev
->sync_set
);
5531 static const struct sysfs_ops md_sysfs_ops
= {
5532 .show
= md_attr_show
,
5533 .store
= md_attr_store
,
5535 static struct kobj_type md_ktype
= {
5537 .sysfs_ops
= &md_sysfs_ops
,
5538 .default_attrs
= md_default_attrs
,
5543 static void mddev_delayed_delete(struct work_struct
*ws
)
5545 struct mddev
*mddev
= container_of(ws
, struct mddev
, del_work
);
5547 sysfs_remove_group(&mddev
->kobj
, &md_bitmap_group
);
5548 kobject_del(&mddev
->kobj
);
5549 kobject_put(&mddev
->kobj
);
5552 static void no_op(struct percpu_ref
*r
) {}
5554 int mddev_init_writes_pending(struct mddev
*mddev
)
5556 if (mddev
->writes_pending
.percpu_count_ptr
)
5558 if (percpu_ref_init(&mddev
->writes_pending
, no_op
,
5559 PERCPU_REF_ALLOW_REINIT
, GFP_KERNEL
) < 0)
5561 /* We want to start with the refcount at zero */
5562 percpu_ref_put(&mddev
->writes_pending
);
5565 EXPORT_SYMBOL_GPL(mddev_init_writes_pending
);
5567 static int md_alloc(dev_t dev
, char *name
)
5570 * If dev is zero, name is the name of a device to allocate with
5571 * an arbitrary minor number. It will be "md_???"
5572 * If dev is non-zero it must be a device number with a MAJOR of
5573 * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then
5574 * the device is being created by opening a node in /dev.
5575 * If "name" is not NULL, the device is being created by
5576 * writing to /sys/module/md_mod/parameters/new_array.
5578 static DEFINE_MUTEX(disks_mutex
);
5579 struct mddev
*mddev
= mddev_find(dev
);
5580 struct gendisk
*disk
;
5589 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
5590 shift
= partitioned
? MdpMinorShift
: 0;
5591 unit
= MINOR(mddev
->unit
) >> shift
;
5593 /* wait for any previous instance of this device to be
5594 * completely removed (mddev_delayed_delete).
5596 flush_workqueue(md_misc_wq
);
5598 mutex_lock(&disks_mutex
);
5604 /* Need to ensure that 'name' is not a duplicate.
5606 struct mddev
*mddev2
;
5607 spin_lock(&all_mddevs_lock
);
5609 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
5610 if (mddev2
->gendisk
&&
5611 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
5612 spin_unlock(&all_mddevs_lock
);
5615 spin_unlock(&all_mddevs_lock
);
5619 * Creating /dev/mdNNN via "newarray", so adjust hold_active.
5621 mddev
->hold_active
= UNTIL_STOP
;
5624 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
5627 mddev
->queue
->queuedata
= mddev
;
5629 blk_queue_make_request(mddev
->queue
, md_make_request
);
5630 blk_set_stacking_limits(&mddev
->queue
->limits
);
5632 disk
= alloc_disk(1 << shift
);
5634 blk_cleanup_queue(mddev
->queue
);
5635 mddev
->queue
= NULL
;
5638 disk
->major
= MAJOR(mddev
->unit
);
5639 disk
->first_minor
= unit
<< shift
;
5641 strcpy(disk
->disk_name
, name
);
5642 else if (partitioned
)
5643 sprintf(disk
->disk_name
, "md_d%d", unit
);
5645 sprintf(disk
->disk_name
, "md%d", unit
);
5646 disk
->fops
= &md_fops
;
5647 disk
->private_data
= mddev
;
5648 disk
->queue
= mddev
->queue
;
5649 blk_queue_write_cache(mddev
->queue
, true, true);
5650 /* Allow extended partitions. This makes the
5651 * 'mdp' device redundant, but we can't really
5654 disk
->flags
|= GENHD_FL_EXT_DEVT
;
5655 mddev
->gendisk
= disk
;
5656 /* As soon as we call add_disk(), another thread could get
5657 * through to md_open, so make sure it doesn't get too far
5659 mutex_lock(&mddev
->open_mutex
);
5662 error
= kobject_add(&mddev
->kobj
, &disk_to_dev(disk
)->kobj
, "%s", "md");
5664 /* This isn't possible, but as kobject_init_and_add is marked
5665 * __must_check, we must do something with the result
5667 pr_debug("md: cannot register %s/md - name in use\n",
5671 if (mddev
->kobj
.sd
&&
5672 sysfs_create_group(&mddev
->kobj
, &md_bitmap_group
))
5673 pr_debug("pointless warning\n");
5674 mutex_unlock(&mddev
->open_mutex
);
5676 mutex_unlock(&disks_mutex
);
5677 if (!error
&& mddev
->kobj
.sd
) {
5678 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
5679 mddev
->sysfs_state
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "array_state");
5685 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
5688 md_alloc(dev
, NULL
);
5692 static int add_named_array(const char *val
, const struct kernel_param
*kp
)
5695 * val must be "md_*" or "mdNNN".
5696 * For "md_*" we allocate an array with a large free minor number, and
5697 * set the name to val. val must not already be an active name.
5698 * For "mdNNN" we allocate an array with the minor number NNN
5699 * which must not already be in use.
5701 int len
= strlen(val
);
5702 char buf
[DISK_NAME_LEN
];
5703 unsigned long devnum
;
5705 while (len
&& val
[len
-1] == '\n')
5707 if (len
>= DISK_NAME_LEN
)
5709 strlcpy(buf
, val
, len
+1);
5710 if (strncmp(buf
, "md_", 3) == 0)
5711 return md_alloc(0, buf
);
5712 if (strncmp(buf
, "md", 2) == 0 &&
5714 kstrtoul(buf
+2, 10, &devnum
) == 0 &&
5715 devnum
<= MINORMASK
)
5716 return md_alloc(MKDEV(MD_MAJOR
, devnum
), NULL
);
5721 static void md_safemode_timeout(struct timer_list
*t
)
5723 struct mddev
*mddev
= from_timer(mddev
, t
, safemode_timer
);
5725 mddev
->safemode
= 1;
5726 if (mddev
->external
)
5727 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5729 md_wakeup_thread(mddev
->thread
);
5732 static int start_dirty_degraded
;
5734 int md_run(struct mddev
*mddev
)
5737 struct md_rdev
*rdev
;
5738 struct md_personality
*pers
;
5740 if (list_empty(&mddev
->disks
))
5741 /* cannot run an array with no devices.. */
5746 /* Cannot run until previous stop completes properly */
5747 if (mddev
->sysfs_active
)
5751 * Analyze all RAID superblock(s)
5753 if (!mddev
->raid_disks
) {
5754 if (!mddev
->persistent
)
5756 err
= analyze_sbs(mddev
);
5761 if (mddev
->level
!= LEVEL_NONE
)
5762 request_module("md-level-%d", mddev
->level
);
5763 else if (mddev
->clevel
[0])
5764 request_module("md-%s", mddev
->clevel
);
5767 * Drop all container device buffers, from now on
5768 * the only valid external interface is through the md
5771 mddev
->has_superblocks
= false;
5772 rdev_for_each(rdev
, mddev
) {
5773 if (test_bit(Faulty
, &rdev
->flags
))
5775 sync_blockdev(rdev
->bdev
);
5776 invalidate_bdev(rdev
->bdev
);
5777 if (mddev
->ro
!= 1 &&
5778 (bdev_read_only(rdev
->bdev
) ||
5779 bdev_read_only(rdev
->meta_bdev
))) {
5782 set_disk_ro(mddev
->gendisk
, 1);
5786 mddev
->has_superblocks
= true;
5788 /* perform some consistency tests on the device.
5789 * We don't want the data to overlap the metadata,
5790 * Internal Bitmap issues have been handled elsewhere.
5792 if (rdev
->meta_bdev
) {
5793 /* Nothing to check */;
5794 } else if (rdev
->data_offset
< rdev
->sb_start
) {
5795 if (mddev
->dev_sectors
&&
5796 rdev
->data_offset
+ mddev
->dev_sectors
5798 pr_warn("md: %s: data overlaps metadata\n",
5803 if (rdev
->sb_start
+ rdev
->sb_size
/512
5804 > rdev
->data_offset
) {
5805 pr_warn("md: %s: metadata overlaps data\n",
5810 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
5813 if (!bioset_initialized(&mddev
->bio_set
)) {
5814 err
= bioset_init(&mddev
->bio_set
, BIO_POOL_SIZE
, 0, BIOSET_NEED_BVECS
);
5818 if (!bioset_initialized(&mddev
->sync_set
)) {
5819 err
= bioset_init(&mddev
->sync_set
, BIO_POOL_SIZE
, 0, BIOSET_NEED_BVECS
);
5824 spin_lock(&pers_lock
);
5825 pers
= find_pers(mddev
->level
, mddev
->clevel
);
5826 if (!pers
|| !try_module_get(pers
->owner
)) {
5827 spin_unlock(&pers_lock
);
5828 if (mddev
->level
!= LEVEL_NONE
)
5829 pr_warn("md: personality for level %d is not loaded!\n",
5832 pr_warn("md: personality for level %s is not loaded!\n",
5837 spin_unlock(&pers_lock
);
5838 if (mddev
->level
!= pers
->level
) {
5839 mddev
->level
= pers
->level
;
5840 mddev
->new_level
= pers
->level
;
5842 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
5844 if (mddev
->reshape_position
!= MaxSector
&&
5845 pers
->start_reshape
== NULL
) {
5846 /* This personality cannot handle reshaping... */
5847 module_put(pers
->owner
);
5852 if (pers
->sync_request
) {
5853 /* Warn if this is a potentially silly
5856 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
5857 struct md_rdev
*rdev2
;
5860 rdev_for_each(rdev
, mddev
)
5861 rdev_for_each(rdev2
, mddev
) {
5863 rdev
->bdev
->bd_contains
==
5864 rdev2
->bdev
->bd_contains
) {
5865 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5867 bdevname(rdev
->bdev
,b
),
5868 bdevname(rdev2
->bdev
,b2
));
5874 pr_warn("True protection against single-disk failure might be compromised.\n");
5877 mddev
->recovery
= 0;
5878 /* may be over-ridden by personality */
5879 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
5881 mddev
->ok_start_degraded
= start_dirty_degraded
;
5883 if (start_readonly
&& mddev
->ro
== 0)
5884 mddev
->ro
= 2; /* read-only, but switch on first write */
5886 err
= pers
->run(mddev
);
5888 pr_warn("md: pers->run() failed ...\n");
5889 else if (pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
5890 WARN_ONCE(!mddev
->external_size
,
5891 "%s: default size too small, but 'external_size' not in effect?\n",
5893 pr_warn("md: invalid array_size %llu > default size %llu\n",
5894 (unsigned long long)mddev
->array_sectors
/ 2,
5895 (unsigned long long)pers
->size(mddev
, 0, 0) / 2);
5898 if (err
== 0 && pers
->sync_request
&&
5899 (mddev
->bitmap_info
.file
|| mddev
->bitmap_info
.offset
)) {
5900 struct bitmap
*bitmap
;
5902 bitmap
= md_bitmap_create(mddev
, -1);
5903 if (IS_ERR(bitmap
)) {
5904 err
= PTR_ERR(bitmap
);
5905 pr_warn("%s: failed to create bitmap (%d)\n",
5906 mdname(mddev
), err
);
5908 mddev
->bitmap
= bitmap
;
5914 if (mddev
->bitmap_info
.max_write_behind
> 0) {
5915 bool create_pool
= false;
5917 rdev_for_each(rdev
, mddev
) {
5918 if (test_bit(WriteMostly
, &rdev
->flags
) &&
5919 rdev_init_serial(rdev
))
5922 if (create_pool
&& mddev
->serial_info_pool
== NULL
) {
5923 mddev
->serial_info_pool
=
5924 mempool_create_kmalloc_pool(NR_SERIAL_INFOS
,
5925 sizeof(struct serial_info
));
5926 if (!mddev
->serial_info_pool
) {
5936 rdev_for_each(rdev
, mddev
) {
5937 if (rdev
->raid_disk
>= 0 &&
5938 !blk_queue_nonrot(bdev_get_queue(rdev
->bdev
))) {
5943 if (mddev
->degraded
)
5946 blk_queue_flag_set(QUEUE_FLAG_NONROT
, mddev
->queue
);
5948 blk_queue_flag_clear(QUEUE_FLAG_NONROT
, mddev
->queue
);
5949 mddev
->queue
->backing_dev_info
->congested_data
= mddev
;
5950 mddev
->queue
->backing_dev_info
->congested_fn
= md_congested
;
5952 if (pers
->sync_request
) {
5953 if (mddev
->kobj
.sd
&&
5954 sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
5955 pr_warn("md: cannot register extra attributes for %s\n",
5957 mddev
->sysfs_action
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "sync_action");
5958 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
5961 atomic_set(&mddev
->max_corr_read_errors
,
5962 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS
);
5963 mddev
->safemode
= 0;
5964 if (mddev_is_clustered(mddev
))
5965 mddev
->safemode_delay
= 0;
5967 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
5970 spin_lock(&mddev
->lock
);
5972 spin_unlock(&mddev
->lock
);
5973 rdev_for_each(rdev
, mddev
)
5974 if (rdev
->raid_disk
>= 0)
5975 sysfs_link_rdev(mddev
, rdev
); /* failure here is OK */
5977 if (mddev
->degraded
&& !mddev
->ro
)
5978 /* This ensures that recovering status is reported immediately
5979 * via sysfs - until a lack of spares is confirmed.
5981 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5982 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5984 if (mddev
->sb_flags
)
5985 md_update_sb(mddev
, 0);
5987 md_new_event(mddev
);
5991 mddev_detach(mddev
);
5993 pers
->free(mddev
, mddev
->private);
5994 mddev
->private = NULL
;
5995 module_put(pers
->owner
);
5996 md_bitmap_destroy(mddev
);
5998 bioset_exit(&mddev
->bio_set
);
5999 bioset_exit(&mddev
->sync_set
);
6002 EXPORT_SYMBOL_GPL(md_run
);
6004 static int do_md_run(struct mddev
*mddev
)
6008 set_bit(MD_NOT_READY
, &mddev
->flags
);
6009 err
= md_run(mddev
);
6012 err
= md_bitmap_load(mddev
);
6014 md_bitmap_destroy(mddev
);
6018 if (mddev_is_clustered(mddev
))
6019 md_allow_write(mddev
);
6021 /* run start up tasks that require md_thread */
6024 md_wakeup_thread(mddev
->thread
);
6025 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
6027 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
6028 revalidate_disk(mddev
->gendisk
);
6029 clear_bit(MD_NOT_READY
, &mddev
->flags
);
6031 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
6032 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6033 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
6034 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
6036 clear_bit(MD_NOT_READY
, &mddev
->flags
);
6040 int md_start(struct mddev
*mddev
)
6044 if (mddev
->pers
->start
) {
6045 set_bit(MD_RECOVERY_WAIT
, &mddev
->recovery
);
6046 md_wakeup_thread(mddev
->thread
);
6047 ret
= mddev
->pers
->start(mddev
);
6048 clear_bit(MD_RECOVERY_WAIT
, &mddev
->recovery
);
6049 md_wakeup_thread(mddev
->sync_thread
);
6053 EXPORT_SYMBOL_GPL(md_start
);
6055 static int restart_array(struct mddev
*mddev
)
6057 struct gendisk
*disk
= mddev
->gendisk
;
6058 struct md_rdev
*rdev
;
6059 bool has_journal
= false;
6060 bool has_readonly
= false;
6062 /* Complain if it has no devices */
6063 if (list_empty(&mddev
->disks
))
6071 rdev_for_each_rcu(rdev
, mddev
) {
6072 if (test_bit(Journal
, &rdev
->flags
) &&
6073 !test_bit(Faulty
, &rdev
->flags
))
6075 if (bdev_read_only(rdev
->bdev
))
6076 has_readonly
= true;
6079 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
) && !has_journal
)
6080 /* Don't restart rw with journal missing/faulty */
6085 mddev
->safemode
= 0;
6087 set_disk_ro(disk
, 0);
6088 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev
));
6089 /* Kick recovery or resync if necessary */
6090 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6091 md_wakeup_thread(mddev
->thread
);
6092 md_wakeup_thread(mddev
->sync_thread
);
6093 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6097 static void md_clean(struct mddev
*mddev
)
6099 mddev
->array_sectors
= 0;
6100 mddev
->external_size
= 0;
6101 mddev
->dev_sectors
= 0;
6102 mddev
->raid_disks
= 0;
6103 mddev
->recovery_cp
= 0;
6104 mddev
->resync_min
= 0;
6105 mddev
->resync_max
= MaxSector
;
6106 mddev
->reshape_position
= MaxSector
;
6107 mddev
->external
= 0;
6108 mddev
->persistent
= 0;
6109 mddev
->level
= LEVEL_NONE
;
6110 mddev
->clevel
[0] = 0;
6112 mddev
->sb_flags
= 0;
6114 mddev
->metadata_type
[0] = 0;
6115 mddev
->chunk_sectors
= 0;
6116 mddev
->ctime
= mddev
->utime
= 0;
6118 mddev
->max_disks
= 0;
6120 mddev
->can_decrease_events
= 0;
6121 mddev
->delta_disks
= 0;
6122 mddev
->reshape_backwards
= 0;
6123 mddev
->new_level
= LEVEL_NONE
;
6124 mddev
->new_layout
= 0;
6125 mddev
->new_chunk_sectors
= 0;
6126 mddev
->curr_resync
= 0;
6127 atomic64_set(&mddev
->resync_mismatches
, 0);
6128 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
6129 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
6130 mddev
->recovery
= 0;
6133 mddev
->degraded
= 0;
6134 mddev
->safemode
= 0;
6135 mddev
->private = NULL
;
6136 mddev
->cluster_info
= NULL
;
6137 mddev
->bitmap_info
.offset
= 0;
6138 mddev
->bitmap_info
.default_offset
= 0;
6139 mddev
->bitmap_info
.default_space
= 0;
6140 mddev
->bitmap_info
.chunksize
= 0;
6141 mddev
->bitmap_info
.daemon_sleep
= 0;
6142 mddev
->bitmap_info
.max_write_behind
= 0;
6143 mddev
->bitmap_info
.nodes
= 0;
6146 static void __md_stop_writes(struct mddev
*mddev
)
6148 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
6149 flush_workqueue(md_misc_wq
);
6150 if (mddev
->sync_thread
) {
6151 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6152 md_reap_sync_thread(mddev
);
6155 del_timer_sync(&mddev
->safemode_timer
);
6157 if (mddev
->pers
&& mddev
->pers
->quiesce
) {
6158 mddev
->pers
->quiesce(mddev
, 1);
6159 mddev
->pers
->quiesce(mddev
, 0);
6161 md_bitmap_flush(mddev
);
6163 if (mddev
->ro
== 0 &&
6164 ((!mddev
->in_sync
&& !mddev_is_clustered(mddev
)) ||
6166 /* mark array as shutdown cleanly */
6167 if (!mddev_is_clustered(mddev
))
6169 md_update_sb(mddev
, 1);
6171 /* disable policy to guarantee rdevs free resources for serialization */
6172 mddev
->serialize_policy
= 0;
6173 mddev_destroy_serial_pool(mddev
, NULL
, true);
6176 void md_stop_writes(struct mddev
*mddev
)
6178 mddev_lock_nointr(mddev
);
6179 __md_stop_writes(mddev
);
6180 mddev_unlock(mddev
);
6182 EXPORT_SYMBOL_GPL(md_stop_writes
);
6184 static void mddev_detach(struct mddev
*mddev
)
6186 md_bitmap_wait_behind_writes(mddev
);
6187 if (mddev
->pers
&& mddev
->pers
->quiesce
&& !mddev
->suspended
) {
6188 mddev
->pers
->quiesce(mddev
, 1);
6189 mddev
->pers
->quiesce(mddev
, 0);
6191 md_unregister_thread(&mddev
->thread
);
6193 blk_sync_queue(mddev
->queue
); /* the unplug fn references 'conf'*/
6196 static void __md_stop(struct mddev
*mddev
)
6198 struct md_personality
*pers
= mddev
->pers
;
6199 md_bitmap_destroy(mddev
);
6200 mddev_detach(mddev
);
6201 /* Ensure ->event_work is done */
6202 flush_workqueue(md_misc_wq
);
6203 spin_lock(&mddev
->lock
);
6205 spin_unlock(&mddev
->lock
);
6206 pers
->free(mddev
, mddev
->private);
6207 mddev
->private = NULL
;
6208 if (pers
->sync_request
&& mddev
->to_remove
== NULL
)
6209 mddev
->to_remove
= &md_redundancy_group
;
6210 module_put(pers
->owner
);
6211 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
6214 void md_stop(struct mddev
*mddev
)
6216 /* stop the array and free an attached data structures.
6217 * This is called from dm-raid
6220 bioset_exit(&mddev
->bio_set
);
6221 bioset_exit(&mddev
->sync_set
);
6224 EXPORT_SYMBOL_GPL(md_stop
);
6226 static int md_set_readonly(struct mddev
*mddev
, struct block_device
*bdev
)
6231 if (!test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
)) {
6233 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
6234 md_wakeup_thread(mddev
->thread
);
6236 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
6237 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6238 if (mddev
->sync_thread
)
6239 /* Thread might be blocked waiting for metadata update
6240 * which will now never happen */
6241 wake_up_process(mddev
->sync_thread
->tsk
);
6243 if (mddev
->external
&& test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
))
6245 mddev_unlock(mddev
);
6246 wait_event(resync_wait
, !test_bit(MD_RECOVERY_RUNNING
,
6248 wait_event(mddev
->sb_wait
,
6249 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
6250 mddev_lock_nointr(mddev
);
6252 mutex_lock(&mddev
->open_mutex
);
6253 if ((mddev
->pers
&& atomic_read(&mddev
->openers
) > !!bdev
) ||
6254 mddev
->sync_thread
||
6255 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
)) {
6256 pr_warn("md: %s still in use.\n",mdname(mddev
));
6258 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
6259 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6260 md_wakeup_thread(mddev
->thread
);
6266 __md_stop_writes(mddev
);
6272 set_disk_ro(mddev
->gendisk
, 1);
6273 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
6274 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6275 md_wakeup_thread(mddev
->thread
);
6276 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6280 mutex_unlock(&mddev
->open_mutex
);
6285 * 0 - completely stop and dis-assemble array
6286 * 2 - stop but do not disassemble array
6288 static int do_md_stop(struct mddev
*mddev
, int mode
,
6289 struct block_device
*bdev
)
6291 struct gendisk
*disk
= mddev
->gendisk
;
6292 struct md_rdev
*rdev
;
6295 if (!test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
)) {
6297 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
6298 md_wakeup_thread(mddev
->thread
);
6300 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
6301 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6302 if (mddev
->sync_thread
)
6303 /* Thread might be blocked waiting for metadata update
6304 * which will now never happen */
6305 wake_up_process(mddev
->sync_thread
->tsk
);
6307 mddev_unlock(mddev
);
6308 wait_event(resync_wait
, (mddev
->sync_thread
== NULL
&&
6309 !test_bit(MD_RECOVERY_RUNNING
,
6310 &mddev
->recovery
)));
6311 mddev_lock_nointr(mddev
);
6313 mutex_lock(&mddev
->open_mutex
);
6314 if ((mddev
->pers
&& atomic_read(&mddev
->openers
) > !!bdev
) ||
6315 mddev
->sysfs_active
||
6316 mddev
->sync_thread
||
6317 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
)) {
6318 pr_warn("md: %s still in use.\n",mdname(mddev
));
6319 mutex_unlock(&mddev
->open_mutex
);
6321 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
6322 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6323 md_wakeup_thread(mddev
->thread
);
6329 set_disk_ro(disk
, 0);
6331 __md_stop_writes(mddev
);
6333 mddev
->queue
->backing_dev_info
->congested_fn
= NULL
;
6335 /* tell userspace to handle 'inactive' */
6336 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6338 rdev_for_each(rdev
, mddev
)
6339 if (rdev
->raid_disk
>= 0)
6340 sysfs_unlink_rdev(mddev
, rdev
);
6342 set_capacity(disk
, 0);
6343 mutex_unlock(&mddev
->open_mutex
);
6345 revalidate_disk(disk
);
6350 mutex_unlock(&mddev
->open_mutex
);
6352 * Free resources if final stop
6355 pr_info("md: %s stopped.\n", mdname(mddev
));
6357 if (mddev
->bitmap_info
.file
) {
6358 struct file
*f
= mddev
->bitmap_info
.file
;
6359 spin_lock(&mddev
->lock
);
6360 mddev
->bitmap_info
.file
= NULL
;
6361 spin_unlock(&mddev
->lock
);
6364 mddev
->bitmap_info
.offset
= 0;
6366 export_array(mddev
);
6369 if (mddev
->hold_active
== UNTIL_STOP
)
6370 mddev
->hold_active
= 0;
6372 md_new_event(mddev
);
6373 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6378 static void autorun_array(struct mddev
*mddev
)
6380 struct md_rdev
*rdev
;
6383 if (list_empty(&mddev
->disks
))
6386 pr_info("md: running: ");
6388 rdev_for_each(rdev
, mddev
) {
6389 char b
[BDEVNAME_SIZE
];
6390 pr_cont("<%s>", bdevname(rdev
->bdev
,b
));
6394 err
= do_md_run(mddev
);
6396 pr_warn("md: do_md_run() returned %d\n", err
);
6397 do_md_stop(mddev
, 0, NULL
);
6402 * lets try to run arrays based on all disks that have arrived
6403 * until now. (those are in pending_raid_disks)
6405 * the method: pick the first pending disk, collect all disks with
6406 * the same UUID, remove all from the pending list and put them into
6407 * the 'same_array' list. Then order this list based on superblock
6408 * update time (freshest comes first), kick out 'old' disks and
6409 * compare superblocks. If everything's fine then run it.
6411 * If "unit" is allocated, then bump its reference count
6413 static void autorun_devices(int part
)
6415 struct md_rdev
*rdev0
, *rdev
, *tmp
;
6416 struct mddev
*mddev
;
6417 char b
[BDEVNAME_SIZE
];
6419 pr_info("md: autorun ...\n");
6420 while (!list_empty(&pending_raid_disks
)) {
6423 LIST_HEAD(candidates
);
6424 rdev0
= list_entry(pending_raid_disks
.next
,
6425 struct md_rdev
, same_set
);
6427 pr_debug("md: considering %s ...\n", bdevname(rdev0
->bdev
,b
));
6428 INIT_LIST_HEAD(&candidates
);
6429 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
6430 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
6431 pr_debug("md: adding %s ...\n",
6432 bdevname(rdev
->bdev
,b
));
6433 list_move(&rdev
->same_set
, &candidates
);
6436 * now we have a set of devices, with all of them having
6437 * mostly sane superblocks. It's time to allocate the
6441 dev
= MKDEV(mdp_major
,
6442 rdev0
->preferred_minor
<< MdpMinorShift
);
6443 unit
= MINOR(dev
) >> MdpMinorShift
;
6445 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
6448 if (rdev0
->preferred_minor
!= unit
) {
6449 pr_warn("md: unit number in %s is bad: %d\n",
6450 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
6454 md_probe(dev
, NULL
, NULL
);
6455 mddev
= mddev_find(dev
);
6456 if (!mddev
|| !mddev
->gendisk
) {
6461 if (mddev_lock(mddev
))
6462 pr_warn("md: %s locked, cannot run\n", mdname(mddev
));
6463 else if (mddev
->raid_disks
|| mddev
->major_version
6464 || !list_empty(&mddev
->disks
)) {
6465 pr_warn("md: %s already running, cannot run %s\n",
6466 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
6467 mddev_unlock(mddev
);
6469 pr_debug("md: created %s\n", mdname(mddev
));
6470 mddev
->persistent
= 1;
6471 rdev_for_each_list(rdev
, tmp
, &candidates
) {
6472 list_del_init(&rdev
->same_set
);
6473 if (bind_rdev_to_array(rdev
, mddev
))
6476 autorun_array(mddev
);
6477 mddev_unlock(mddev
);
6479 /* on success, candidates will be empty, on error
6482 rdev_for_each_list(rdev
, tmp
, &candidates
) {
6483 list_del_init(&rdev
->same_set
);
6488 pr_info("md: ... autorun DONE.\n");
6490 #endif /* !MODULE */
6492 static int get_version(void __user
*arg
)
6496 ver
.major
= MD_MAJOR_VERSION
;
6497 ver
.minor
= MD_MINOR_VERSION
;
6498 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
6500 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
6506 static int get_array_info(struct mddev
*mddev
, void __user
*arg
)
6508 mdu_array_info_t info
;
6509 int nr
,working
,insync
,failed
,spare
;
6510 struct md_rdev
*rdev
;
6512 nr
= working
= insync
= failed
= spare
= 0;
6514 rdev_for_each_rcu(rdev
, mddev
) {
6516 if (test_bit(Faulty
, &rdev
->flags
))
6520 if (test_bit(In_sync
, &rdev
->flags
))
6522 else if (test_bit(Journal
, &rdev
->flags
))
6523 /* TODO: add journal count to md_u.h */
6531 info
.major_version
= mddev
->major_version
;
6532 info
.minor_version
= mddev
->minor_version
;
6533 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
6534 info
.ctime
= clamp_t(time64_t
, mddev
->ctime
, 0, U32_MAX
);
6535 info
.level
= mddev
->level
;
6536 info
.size
= mddev
->dev_sectors
/ 2;
6537 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
6540 info
.raid_disks
= mddev
->raid_disks
;
6541 info
.md_minor
= mddev
->md_minor
;
6542 info
.not_persistent
= !mddev
->persistent
;
6544 info
.utime
= clamp_t(time64_t
, mddev
->utime
, 0, U32_MAX
);
6547 info
.state
= (1<<MD_SB_CLEAN
);
6548 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
6549 info
.state
|= (1<<MD_SB_BITMAP_PRESENT
);
6550 if (mddev_is_clustered(mddev
))
6551 info
.state
|= (1<<MD_SB_CLUSTERED
);
6552 info
.active_disks
= insync
;
6553 info
.working_disks
= working
;
6554 info
.failed_disks
= failed
;
6555 info
.spare_disks
= spare
;
6557 info
.layout
= mddev
->layout
;
6558 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
6560 if (copy_to_user(arg
, &info
, sizeof(info
)))
6566 static int get_bitmap_file(struct mddev
*mddev
, void __user
* arg
)
6568 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
6572 file
= kzalloc(sizeof(*file
), GFP_NOIO
);
6577 spin_lock(&mddev
->lock
);
6578 /* bitmap enabled */
6579 if (mddev
->bitmap_info
.file
) {
6580 ptr
= file_path(mddev
->bitmap_info
.file
, file
->pathname
,
6581 sizeof(file
->pathname
));
6585 memmove(file
->pathname
, ptr
,
6586 sizeof(file
->pathname
)-(ptr
-file
->pathname
));
6588 spin_unlock(&mddev
->lock
);
6591 copy_to_user(arg
, file
, sizeof(*file
)))
6598 static int get_disk_info(struct mddev
*mddev
, void __user
* arg
)
6600 mdu_disk_info_t info
;
6601 struct md_rdev
*rdev
;
6603 if (copy_from_user(&info
, arg
, sizeof(info
)))
6607 rdev
= md_find_rdev_nr_rcu(mddev
, info
.number
);
6609 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
6610 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
6611 info
.raid_disk
= rdev
->raid_disk
;
6613 if (test_bit(Faulty
, &rdev
->flags
))
6614 info
.state
|= (1<<MD_DISK_FAULTY
);
6615 else if (test_bit(In_sync
, &rdev
->flags
)) {
6616 info
.state
|= (1<<MD_DISK_ACTIVE
);
6617 info
.state
|= (1<<MD_DISK_SYNC
);
6619 if (test_bit(Journal
, &rdev
->flags
))
6620 info
.state
|= (1<<MD_DISK_JOURNAL
);
6621 if (test_bit(WriteMostly
, &rdev
->flags
))
6622 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
6623 if (test_bit(FailFast
, &rdev
->flags
))
6624 info
.state
|= (1<<MD_DISK_FAILFAST
);
6626 info
.major
= info
.minor
= 0;
6627 info
.raid_disk
= -1;
6628 info
.state
= (1<<MD_DISK_REMOVED
);
6632 if (copy_to_user(arg
, &info
, sizeof(info
)))
6638 static int add_new_disk(struct mddev
*mddev
, mdu_disk_info_t
*info
)
6640 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
6641 struct md_rdev
*rdev
;
6642 dev_t dev
= MKDEV(info
->major
,info
->minor
);
6644 if (mddev_is_clustered(mddev
) &&
6645 !(info
->state
& ((1 << MD_DISK_CLUSTER_ADD
) | (1 << MD_DISK_CANDIDATE
)))) {
6646 pr_warn("%s: Cannot add to clustered mddev.\n",
6651 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
6654 if (!mddev
->raid_disks
) {
6656 /* expecting a device which has a superblock */
6657 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
6659 pr_warn("md: md_import_device returned %ld\n",
6661 return PTR_ERR(rdev
);
6663 if (!list_empty(&mddev
->disks
)) {
6664 struct md_rdev
*rdev0
6665 = list_entry(mddev
->disks
.next
,
6666 struct md_rdev
, same_set
);
6667 err
= super_types
[mddev
->major_version
]
6668 .load_super(rdev
, rdev0
, mddev
->minor_version
);
6670 pr_warn("md: %s has different UUID to %s\n",
6671 bdevname(rdev
->bdev
,b
),
6672 bdevname(rdev0
->bdev
,b2
));
6677 err
= bind_rdev_to_array(rdev
, mddev
);
6684 * add_new_disk can be used once the array is assembled
6685 * to add "hot spares". They must already have a superblock
6690 if (!mddev
->pers
->hot_add_disk
) {
6691 pr_warn("%s: personality does not support diskops!\n",
6695 if (mddev
->persistent
)
6696 rdev
= md_import_device(dev
, mddev
->major_version
,
6697 mddev
->minor_version
);
6699 rdev
= md_import_device(dev
, -1, -1);
6701 pr_warn("md: md_import_device returned %ld\n",
6703 return PTR_ERR(rdev
);
6705 /* set saved_raid_disk if appropriate */
6706 if (!mddev
->persistent
) {
6707 if (info
->state
& (1<<MD_DISK_SYNC
) &&
6708 info
->raid_disk
< mddev
->raid_disks
) {
6709 rdev
->raid_disk
= info
->raid_disk
;
6710 set_bit(In_sync
, &rdev
->flags
);
6711 clear_bit(Bitmap_sync
, &rdev
->flags
);
6713 rdev
->raid_disk
= -1;
6714 rdev
->saved_raid_disk
= rdev
->raid_disk
;
6716 super_types
[mddev
->major_version
].
6717 validate_super(mddev
, rdev
);
6718 if ((info
->state
& (1<<MD_DISK_SYNC
)) &&
6719 rdev
->raid_disk
!= info
->raid_disk
) {
6720 /* This was a hot-add request, but events doesn't
6721 * match, so reject it.
6727 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
6728 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
6729 set_bit(WriteMostly
, &rdev
->flags
);
6731 clear_bit(WriteMostly
, &rdev
->flags
);
6732 if (info
->state
& (1<<MD_DISK_FAILFAST
))
6733 set_bit(FailFast
, &rdev
->flags
);
6735 clear_bit(FailFast
, &rdev
->flags
);
6737 if (info
->state
& (1<<MD_DISK_JOURNAL
)) {
6738 struct md_rdev
*rdev2
;
6739 bool has_journal
= false;
6741 /* make sure no existing journal disk */
6742 rdev_for_each(rdev2
, mddev
) {
6743 if (test_bit(Journal
, &rdev2
->flags
)) {
6748 if (has_journal
|| mddev
->bitmap
) {
6752 set_bit(Journal
, &rdev
->flags
);
6755 * check whether the device shows up in other nodes
6757 if (mddev_is_clustered(mddev
)) {
6758 if (info
->state
& (1 << MD_DISK_CANDIDATE
))
6759 set_bit(Candidate
, &rdev
->flags
);
6760 else if (info
->state
& (1 << MD_DISK_CLUSTER_ADD
)) {
6761 /* --add initiated by this node */
6762 err
= md_cluster_ops
->add_new_disk(mddev
, rdev
);
6770 rdev
->raid_disk
= -1;
6771 err
= bind_rdev_to_array(rdev
, mddev
);
6776 if (mddev_is_clustered(mddev
)) {
6777 if (info
->state
& (1 << MD_DISK_CANDIDATE
)) {
6779 err
= md_cluster_ops
->new_disk_ack(mddev
,
6782 md_kick_rdev_from_array(rdev
);
6786 md_cluster_ops
->add_new_disk_cancel(mddev
);
6788 err
= add_bound_rdev(rdev
);
6792 err
= add_bound_rdev(rdev
);
6797 /* otherwise, add_new_disk is only allowed
6798 * for major_version==0 superblocks
6800 if (mddev
->major_version
!= 0) {
6801 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev
));
6805 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
6807 rdev
= md_import_device(dev
, -1, 0);
6809 pr_warn("md: error, md_import_device() returned %ld\n",
6811 return PTR_ERR(rdev
);
6813 rdev
->desc_nr
= info
->number
;
6814 if (info
->raid_disk
< mddev
->raid_disks
)
6815 rdev
->raid_disk
= info
->raid_disk
;
6817 rdev
->raid_disk
= -1;
6819 if (rdev
->raid_disk
< mddev
->raid_disks
)
6820 if (info
->state
& (1<<MD_DISK_SYNC
))
6821 set_bit(In_sync
, &rdev
->flags
);
6823 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
6824 set_bit(WriteMostly
, &rdev
->flags
);
6825 if (info
->state
& (1<<MD_DISK_FAILFAST
))
6826 set_bit(FailFast
, &rdev
->flags
);
6828 if (!mddev
->persistent
) {
6829 pr_debug("md: nonpersistent superblock ...\n");
6830 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
6832 rdev
->sb_start
= calc_dev_sboffset(rdev
);
6833 rdev
->sectors
= rdev
->sb_start
;
6835 err
= bind_rdev_to_array(rdev
, mddev
);
6845 static int hot_remove_disk(struct mddev
*mddev
, dev_t dev
)
6847 char b
[BDEVNAME_SIZE
];
6848 struct md_rdev
*rdev
;
6853 rdev
= find_rdev(mddev
, dev
);
6857 if (rdev
->raid_disk
< 0)
6860 clear_bit(Blocked
, &rdev
->flags
);
6861 remove_and_add_spares(mddev
, rdev
);
6863 if (rdev
->raid_disk
>= 0)
6867 if (mddev_is_clustered(mddev
))
6868 md_cluster_ops
->remove_disk(mddev
, rdev
);
6870 md_kick_rdev_from_array(rdev
);
6871 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
6873 md_wakeup_thread(mddev
->thread
);
6875 md_update_sb(mddev
, 1);
6876 md_new_event(mddev
);
6880 pr_debug("md: cannot remove active disk %s from %s ...\n",
6881 bdevname(rdev
->bdev
,b
), mdname(mddev
));
6885 static int hot_add_disk(struct mddev
*mddev
, dev_t dev
)
6887 char b
[BDEVNAME_SIZE
];
6889 struct md_rdev
*rdev
;
6894 if (mddev
->major_version
!= 0) {
6895 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
6899 if (!mddev
->pers
->hot_add_disk
) {
6900 pr_warn("%s: personality does not support diskops!\n",
6905 rdev
= md_import_device(dev
, -1, 0);
6907 pr_warn("md: error, md_import_device() returned %ld\n",
6912 if (mddev
->persistent
)
6913 rdev
->sb_start
= calc_dev_sboffset(rdev
);
6915 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
6917 rdev
->sectors
= rdev
->sb_start
;
6919 if (test_bit(Faulty
, &rdev
->flags
)) {
6920 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
6921 bdevname(rdev
->bdev
,b
), mdname(mddev
));
6926 clear_bit(In_sync
, &rdev
->flags
);
6928 rdev
->saved_raid_disk
= -1;
6929 err
= bind_rdev_to_array(rdev
, mddev
);
6934 * The rest should better be atomic, we can have disk failures
6935 * noticed in interrupt contexts ...
6938 rdev
->raid_disk
= -1;
6940 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
6942 md_update_sb(mddev
, 1);
6944 * Kick recovery, maybe this spare has to be added to the
6945 * array immediately.
6947 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6948 md_wakeup_thread(mddev
->thread
);
6949 md_new_event(mddev
);
6957 static int set_bitmap_file(struct mddev
*mddev
, int fd
)
6962 if (!mddev
->pers
->quiesce
|| !mddev
->thread
)
6964 if (mddev
->recovery
|| mddev
->sync_thread
)
6966 /* we should be able to change the bitmap.. */
6970 struct inode
*inode
;
6973 if (mddev
->bitmap
|| mddev
->bitmap_info
.file
)
6974 return -EEXIST
; /* cannot add when bitmap is present */
6978 pr_warn("%s: error: failed to get bitmap file\n",
6983 inode
= f
->f_mapping
->host
;
6984 if (!S_ISREG(inode
->i_mode
)) {
6985 pr_warn("%s: error: bitmap file must be a regular file\n",
6988 } else if (!(f
->f_mode
& FMODE_WRITE
)) {
6989 pr_warn("%s: error: bitmap file must open for write\n",
6992 } else if (atomic_read(&inode
->i_writecount
) != 1) {
6993 pr_warn("%s: error: bitmap file is already in use\n",
7001 mddev
->bitmap_info
.file
= f
;
7002 mddev
->bitmap_info
.offset
= 0; /* file overrides offset */
7003 } else if (mddev
->bitmap
== NULL
)
7004 return -ENOENT
; /* cannot remove what isn't there */
7008 struct bitmap
*bitmap
;
7010 bitmap
= md_bitmap_create(mddev
, -1);
7011 mddev_suspend(mddev
);
7012 if (!IS_ERR(bitmap
)) {
7013 mddev
->bitmap
= bitmap
;
7014 err
= md_bitmap_load(mddev
);
7016 err
= PTR_ERR(bitmap
);
7018 md_bitmap_destroy(mddev
);
7021 mddev_resume(mddev
);
7022 } else if (fd
< 0) {
7023 mddev_suspend(mddev
);
7024 md_bitmap_destroy(mddev
);
7025 mddev_resume(mddev
);
7029 struct file
*f
= mddev
->bitmap_info
.file
;
7031 spin_lock(&mddev
->lock
);
7032 mddev
->bitmap_info
.file
= NULL
;
7033 spin_unlock(&mddev
->lock
);
7042 * set_array_info is used two different ways
7043 * The original usage is when creating a new array.
7044 * In this usage, raid_disks is > 0 and it together with
7045 * level, size, not_persistent,layout,chunksize determine the
7046 * shape of the array.
7047 * This will always create an array with a type-0.90.0 superblock.
7048 * The newer usage is when assembling an array.
7049 * In this case raid_disks will be 0, and the major_version field is
7050 * use to determine which style super-blocks are to be found on the devices.
7051 * The minor and patch _version numbers are also kept incase the
7052 * super_block handler wishes to interpret them.
7054 static int set_array_info(struct mddev
*mddev
, mdu_array_info_t
*info
)
7057 if (info
->raid_disks
== 0) {
7058 /* just setting version number for superblock loading */
7059 if (info
->major_version
< 0 ||
7060 info
->major_version
>= ARRAY_SIZE(super_types
) ||
7061 super_types
[info
->major_version
].name
== NULL
) {
7062 /* maybe try to auto-load a module? */
7063 pr_warn("md: superblock version %d not known\n",
7064 info
->major_version
);
7067 mddev
->major_version
= info
->major_version
;
7068 mddev
->minor_version
= info
->minor_version
;
7069 mddev
->patch_version
= info
->patch_version
;
7070 mddev
->persistent
= !info
->not_persistent
;
7071 /* ensure mddev_put doesn't delete this now that there
7072 * is some minimal configuration.
7074 mddev
->ctime
= ktime_get_real_seconds();
7077 mddev
->major_version
= MD_MAJOR_VERSION
;
7078 mddev
->minor_version
= MD_MINOR_VERSION
;
7079 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
7080 mddev
->ctime
= ktime_get_real_seconds();
7082 mddev
->level
= info
->level
;
7083 mddev
->clevel
[0] = 0;
7084 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
7085 mddev
->raid_disks
= info
->raid_disks
;
7086 /* don't set md_minor, it is determined by which /dev/md* was
7089 if (info
->state
& (1<<MD_SB_CLEAN
))
7090 mddev
->recovery_cp
= MaxSector
;
7092 mddev
->recovery_cp
= 0;
7093 mddev
->persistent
= ! info
->not_persistent
;
7094 mddev
->external
= 0;
7096 mddev
->layout
= info
->layout
;
7097 if (mddev
->level
== 0)
7098 /* Cannot trust RAID0 layout info here */
7100 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
7102 if (mddev
->persistent
) {
7103 mddev
->max_disks
= MD_SB_DISKS
;
7105 mddev
->sb_flags
= 0;
7107 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
7109 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
7110 mddev
->bitmap_info
.default_space
= 64*2 - (MD_SB_BYTES
>> 9);
7111 mddev
->bitmap_info
.offset
= 0;
7113 mddev
->reshape_position
= MaxSector
;
7116 * Generate a 128 bit UUID
7118 get_random_bytes(mddev
->uuid
, 16);
7120 mddev
->new_level
= mddev
->level
;
7121 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
7122 mddev
->new_layout
= mddev
->layout
;
7123 mddev
->delta_disks
= 0;
7124 mddev
->reshape_backwards
= 0;
7129 void md_set_array_sectors(struct mddev
*mddev
, sector_t array_sectors
)
7131 lockdep_assert_held(&mddev
->reconfig_mutex
);
7133 if (mddev
->external_size
)
7136 mddev
->array_sectors
= array_sectors
;
7138 EXPORT_SYMBOL(md_set_array_sectors
);
7140 static int update_size(struct mddev
*mddev
, sector_t num_sectors
)
7142 struct md_rdev
*rdev
;
7144 int fit
= (num_sectors
== 0);
7145 sector_t old_dev_sectors
= mddev
->dev_sectors
;
7147 if (mddev
->pers
->resize
== NULL
)
7149 /* The "num_sectors" is the number of sectors of each device that
7150 * is used. This can only make sense for arrays with redundancy.
7151 * linear and raid0 always use whatever space is available. We can only
7152 * consider changing this number if no resync or reconstruction is
7153 * happening, and if the new size is acceptable. It must fit before the
7154 * sb_start or, if that is <data_offset, it must fit before the size
7155 * of each device. If num_sectors is zero, we find the largest size
7158 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
7164 rdev_for_each(rdev
, mddev
) {
7165 sector_t avail
= rdev
->sectors
;
7167 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
7168 num_sectors
= avail
;
7169 if (avail
< num_sectors
)
7172 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
7174 if (mddev_is_clustered(mddev
))
7175 md_cluster_ops
->update_size(mddev
, old_dev_sectors
);
7176 else if (mddev
->queue
) {
7177 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
7178 revalidate_disk(mddev
->gendisk
);
7184 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
)
7187 struct md_rdev
*rdev
;
7188 /* change the number of raid disks */
7189 if (mddev
->pers
->check_reshape
== NULL
)
7193 if (raid_disks
<= 0 ||
7194 (mddev
->max_disks
&& raid_disks
>= mddev
->max_disks
))
7196 if (mddev
->sync_thread
||
7197 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
7198 mddev
->reshape_position
!= MaxSector
)
7201 rdev_for_each(rdev
, mddev
) {
7202 if (mddev
->raid_disks
< raid_disks
&&
7203 rdev
->data_offset
< rdev
->new_data_offset
)
7205 if (mddev
->raid_disks
> raid_disks
&&
7206 rdev
->data_offset
> rdev
->new_data_offset
)
7210 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
7211 if (mddev
->delta_disks
< 0)
7212 mddev
->reshape_backwards
= 1;
7213 else if (mddev
->delta_disks
> 0)
7214 mddev
->reshape_backwards
= 0;
7216 rv
= mddev
->pers
->check_reshape(mddev
);
7218 mddev
->delta_disks
= 0;
7219 mddev
->reshape_backwards
= 0;
7225 * update_array_info is used to change the configuration of an
7227 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
7228 * fields in the info are checked against the array.
7229 * Any differences that cannot be handled will cause an error.
7230 * Normally, only one change can be managed at a time.
7232 static int update_array_info(struct mddev
*mddev
, mdu_array_info_t
*info
)
7238 /* calculate expected state,ignoring low bits */
7239 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
7240 state
|= (1 << MD_SB_BITMAP_PRESENT
);
7242 if (mddev
->major_version
!= info
->major_version
||
7243 mddev
->minor_version
!= info
->minor_version
||
7244 /* mddev->patch_version != info->patch_version || */
7245 mddev
->ctime
!= info
->ctime
||
7246 mddev
->level
!= info
->level
||
7247 /* mddev->layout != info->layout || */
7248 mddev
->persistent
!= !info
->not_persistent
||
7249 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
7250 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
7251 ((state
^info
->state
) & 0xfffffe00)
7254 /* Check there is only one change */
7255 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
7257 if (mddev
->raid_disks
!= info
->raid_disks
)
7259 if (mddev
->layout
!= info
->layout
)
7261 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
7268 if (mddev
->layout
!= info
->layout
) {
7270 * we don't need to do anything at the md level, the
7271 * personality will take care of it all.
7273 if (mddev
->pers
->check_reshape
== NULL
)
7276 mddev
->new_layout
= info
->layout
;
7277 rv
= mddev
->pers
->check_reshape(mddev
);
7279 mddev
->new_layout
= mddev
->layout
;
7283 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
7284 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
7286 if (mddev
->raid_disks
!= info
->raid_disks
)
7287 rv
= update_raid_disks(mddev
, info
->raid_disks
);
7289 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
7290 if (mddev
->pers
->quiesce
== NULL
|| mddev
->thread
== NULL
) {
7294 if (mddev
->recovery
|| mddev
->sync_thread
) {
7298 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
7299 struct bitmap
*bitmap
;
7300 /* add the bitmap */
7301 if (mddev
->bitmap
) {
7305 if (mddev
->bitmap_info
.default_offset
== 0) {
7309 mddev
->bitmap_info
.offset
=
7310 mddev
->bitmap_info
.default_offset
;
7311 mddev
->bitmap_info
.space
=
7312 mddev
->bitmap_info
.default_space
;
7313 bitmap
= md_bitmap_create(mddev
, -1);
7314 mddev_suspend(mddev
);
7315 if (!IS_ERR(bitmap
)) {
7316 mddev
->bitmap
= bitmap
;
7317 rv
= md_bitmap_load(mddev
);
7319 rv
= PTR_ERR(bitmap
);
7321 md_bitmap_destroy(mddev
);
7322 mddev_resume(mddev
);
7324 /* remove the bitmap */
7325 if (!mddev
->bitmap
) {
7329 if (mddev
->bitmap
->storage
.file
) {
7333 if (mddev
->bitmap_info
.nodes
) {
7334 /* hold PW on all the bitmap lock */
7335 if (md_cluster_ops
->lock_all_bitmaps(mddev
) <= 0) {
7336 pr_warn("md: can't change bitmap to none since the array is in use by more than one node\n");
7338 md_cluster_ops
->unlock_all_bitmaps(mddev
);
7342 mddev
->bitmap_info
.nodes
= 0;
7343 md_cluster_ops
->leave(mddev
);
7345 mddev_suspend(mddev
);
7346 md_bitmap_destroy(mddev
);
7347 mddev_resume(mddev
);
7348 mddev
->bitmap_info
.offset
= 0;
7351 md_update_sb(mddev
, 1);
7357 static int set_disk_faulty(struct mddev
*mddev
, dev_t dev
)
7359 struct md_rdev
*rdev
;
7362 if (mddev
->pers
== NULL
)
7366 rdev
= md_find_rdev_rcu(mddev
, dev
);
7370 md_error(mddev
, rdev
);
7371 if (!test_bit(Faulty
, &rdev
->flags
))
7379 * We have a problem here : there is no easy way to give a CHS
7380 * virtual geometry. We currently pretend that we have a 2 heads
7381 * 4 sectors (with a BIG number of cylinders...). This drives
7382 * dosfs just mad... ;-)
7384 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
7386 struct mddev
*mddev
= bdev
->bd_disk
->private_data
;
7390 geo
->cylinders
= mddev
->array_sectors
/ 8;
7394 static inline bool md_ioctl_valid(unsigned int cmd
)
7399 case GET_ARRAY_INFO
:
7400 case GET_BITMAP_FILE
:
7403 case HOT_REMOVE_DISK
:
7406 case RESTART_ARRAY_RW
:
7408 case SET_ARRAY_INFO
:
7409 case SET_BITMAP_FILE
:
7410 case SET_DISK_FAULTY
:
7413 case CLUSTERED_DISK_NACK
:
7420 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
7421 unsigned int cmd
, unsigned long arg
)
7424 void __user
*argp
= (void __user
*)arg
;
7425 struct mddev
*mddev
= NULL
;
7427 bool did_set_md_closing
= false;
7429 if (!md_ioctl_valid(cmd
))
7434 case GET_ARRAY_INFO
:
7438 if (!capable(CAP_SYS_ADMIN
))
7443 * Commands dealing with the RAID driver but not any
7448 err
= get_version(argp
);
7454 autostart_arrays(arg
);
7461 * Commands creating/starting a new array:
7464 mddev
= bdev
->bd_disk
->private_data
;
7471 /* Some actions do not requires the mutex */
7473 case GET_ARRAY_INFO
:
7474 if (!mddev
->raid_disks
&& !mddev
->external
)
7477 err
= get_array_info(mddev
, argp
);
7481 if (!mddev
->raid_disks
&& !mddev
->external
)
7484 err
= get_disk_info(mddev
, argp
);
7487 case SET_DISK_FAULTY
:
7488 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
7491 case GET_BITMAP_FILE
:
7492 err
= get_bitmap_file(mddev
, argp
);
7497 if (cmd
== ADD_NEW_DISK
)
7498 /* need to ensure md_delayed_delete() has completed */
7499 flush_workqueue(md_misc_wq
);
7501 if (cmd
== HOT_REMOVE_DISK
)
7502 /* need to ensure recovery thread has run */
7503 wait_event_interruptible_timeout(mddev
->sb_wait
,
7504 !test_bit(MD_RECOVERY_NEEDED
,
7506 msecs_to_jiffies(5000));
7507 if (cmd
== STOP_ARRAY
|| cmd
== STOP_ARRAY_RO
) {
7508 /* Need to flush page cache, and ensure no-one else opens
7511 mutex_lock(&mddev
->open_mutex
);
7512 if (mddev
->pers
&& atomic_read(&mddev
->openers
) > 1) {
7513 mutex_unlock(&mddev
->open_mutex
);
7517 WARN_ON_ONCE(test_bit(MD_CLOSING
, &mddev
->flags
));
7518 set_bit(MD_CLOSING
, &mddev
->flags
);
7519 did_set_md_closing
= true;
7520 mutex_unlock(&mddev
->open_mutex
);
7521 sync_blockdev(bdev
);
7523 err
= mddev_lock(mddev
);
7525 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7530 if (cmd
== SET_ARRAY_INFO
) {
7531 mdu_array_info_t info
;
7533 memset(&info
, 0, sizeof(info
));
7534 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
7539 err
= update_array_info(mddev
, &info
);
7541 pr_warn("md: couldn't update array info. %d\n", err
);
7546 if (!list_empty(&mddev
->disks
)) {
7547 pr_warn("md: array %s already has disks!\n", mdname(mddev
));
7551 if (mddev
->raid_disks
) {
7552 pr_warn("md: array %s already initialised!\n", mdname(mddev
));
7556 err
= set_array_info(mddev
, &info
);
7558 pr_warn("md: couldn't set array info. %d\n", err
);
7565 * Commands querying/configuring an existing array:
7567 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
7568 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
7569 if ((!mddev
->raid_disks
&& !mddev
->external
)
7570 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
7571 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
7572 && cmd
!= GET_BITMAP_FILE
) {
7578 * Commands even a read-only array can execute:
7581 case RESTART_ARRAY_RW
:
7582 err
= restart_array(mddev
);
7586 err
= do_md_stop(mddev
, 0, bdev
);
7590 err
= md_set_readonly(mddev
, bdev
);
7593 case HOT_REMOVE_DISK
:
7594 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
7598 /* We can support ADD_NEW_DISK on read-only arrays
7599 * only if we are re-adding a preexisting device.
7600 * So require mddev->pers and MD_DISK_SYNC.
7603 mdu_disk_info_t info
;
7604 if (copy_from_user(&info
, argp
, sizeof(info
)))
7606 else if (!(info
.state
& (1<<MD_DISK_SYNC
)))
7607 /* Need to clear read-only for this */
7610 err
= add_new_disk(mddev
, &info
);
7616 if (get_user(ro
, (int __user
*)(arg
))) {
7622 /* if the bdev is going readonly the value of mddev->ro
7623 * does not matter, no writes are coming
7628 /* are we are already prepared for writes? */
7632 /* transitioning to readauto need only happen for
7633 * arrays that call md_write_start
7636 err
= restart_array(mddev
);
7639 set_disk_ro(mddev
->gendisk
, 0);
7646 * The remaining ioctls are changing the state of the
7647 * superblock, so we do not allow them on read-only arrays.
7649 if (mddev
->ro
&& mddev
->pers
) {
7650 if (mddev
->ro
== 2) {
7652 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7653 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7654 /* mddev_unlock will wake thread */
7655 /* If a device failed while we were read-only, we
7656 * need to make sure the metadata is updated now.
7658 if (test_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
)) {
7659 mddev_unlock(mddev
);
7660 wait_event(mddev
->sb_wait
,
7661 !test_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
) &&
7662 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
7663 mddev_lock_nointr(mddev
);
7674 mdu_disk_info_t info
;
7675 if (copy_from_user(&info
, argp
, sizeof(info
)))
7678 err
= add_new_disk(mddev
, &info
);
7682 case CLUSTERED_DISK_NACK
:
7683 if (mddev_is_clustered(mddev
))
7684 md_cluster_ops
->new_disk_ack(mddev
, false);
7690 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
7694 err
= do_md_run(mddev
);
7697 case SET_BITMAP_FILE
:
7698 err
= set_bitmap_file(mddev
, (int)arg
);
7707 if (mddev
->hold_active
== UNTIL_IOCTL
&&
7709 mddev
->hold_active
= 0;
7710 mddev_unlock(mddev
);
7712 if(did_set_md_closing
)
7713 clear_bit(MD_CLOSING
, &mddev
->flags
);
7716 #ifdef CONFIG_COMPAT
7717 static int md_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
7718 unsigned int cmd
, unsigned long arg
)
7721 case HOT_REMOVE_DISK
:
7723 case SET_DISK_FAULTY
:
7724 case SET_BITMAP_FILE
:
7725 /* These take in integer arg, do not convert */
7728 arg
= (unsigned long)compat_ptr(arg
);
7732 return md_ioctl(bdev
, mode
, cmd
, arg
);
7734 #endif /* CONFIG_COMPAT */
7736 static int md_open(struct block_device
*bdev
, fmode_t mode
)
7739 * Succeed if we can lock the mddev, which confirms that
7740 * it isn't being stopped right now.
7742 struct mddev
*mddev
= mddev_find(bdev
->bd_dev
);
7748 if (mddev
->gendisk
!= bdev
->bd_disk
) {
7749 /* we are racing with mddev_put which is discarding this
7753 /* Wait until bdev->bd_disk is definitely gone */
7754 flush_workqueue(md_misc_wq
);
7755 /* Then retry the open from the top */
7756 return -ERESTARTSYS
;
7758 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
7760 if ((err
= mutex_lock_interruptible(&mddev
->open_mutex
)))
7763 if (test_bit(MD_CLOSING
, &mddev
->flags
)) {
7764 mutex_unlock(&mddev
->open_mutex
);
7770 atomic_inc(&mddev
->openers
);
7771 mutex_unlock(&mddev
->open_mutex
);
7773 check_disk_change(bdev
);
7780 static void md_release(struct gendisk
*disk
, fmode_t mode
)
7782 struct mddev
*mddev
= disk
->private_data
;
7785 atomic_dec(&mddev
->openers
);
7789 static int md_media_changed(struct gendisk
*disk
)
7791 struct mddev
*mddev
= disk
->private_data
;
7793 return mddev
->changed
;
7796 static int md_revalidate(struct gendisk
*disk
)
7798 struct mddev
*mddev
= disk
->private_data
;
7803 static const struct block_device_operations md_fops
=
7805 .owner
= THIS_MODULE
,
7807 .release
= md_release
,
7809 #ifdef CONFIG_COMPAT
7810 .compat_ioctl
= md_compat_ioctl
,
7812 .getgeo
= md_getgeo
,
7813 .media_changed
= md_media_changed
,
7814 .revalidate_disk
= md_revalidate
,
7817 static int md_thread(void *arg
)
7819 struct md_thread
*thread
= arg
;
7822 * md_thread is a 'system-thread', it's priority should be very
7823 * high. We avoid resource deadlocks individually in each
7824 * raid personality. (RAID5 does preallocation) We also use RR and
7825 * the very same RT priority as kswapd, thus we will never get
7826 * into a priority inversion deadlock.
7828 * we definitely have to have equal or higher priority than
7829 * bdflush, otherwise bdflush will deadlock if there are too
7830 * many dirty RAID5 blocks.
7833 allow_signal(SIGKILL
);
7834 while (!kthread_should_stop()) {
7836 /* We need to wait INTERRUPTIBLE so that
7837 * we don't add to the load-average.
7838 * That means we need to be sure no signals are
7841 if (signal_pending(current
))
7842 flush_signals(current
);
7844 wait_event_interruptible_timeout
7846 test_bit(THREAD_WAKEUP
, &thread
->flags
)
7847 || kthread_should_stop() || kthread_should_park(),
7850 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
7851 if (kthread_should_park())
7853 if (!kthread_should_stop())
7854 thread
->run(thread
);
7860 void md_wakeup_thread(struct md_thread
*thread
)
7863 pr_debug("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
7864 set_bit(THREAD_WAKEUP
, &thread
->flags
);
7865 wake_up(&thread
->wqueue
);
7868 EXPORT_SYMBOL(md_wakeup_thread
);
7870 struct md_thread
*md_register_thread(void (*run
) (struct md_thread
*),
7871 struct mddev
*mddev
, const char *name
)
7873 struct md_thread
*thread
;
7875 thread
= kzalloc(sizeof(struct md_thread
), GFP_KERNEL
);
7879 init_waitqueue_head(&thread
->wqueue
);
7882 thread
->mddev
= mddev
;
7883 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
7884 thread
->tsk
= kthread_run(md_thread
, thread
,
7886 mdname(thread
->mddev
),
7888 if (IS_ERR(thread
->tsk
)) {
7894 EXPORT_SYMBOL(md_register_thread
);
7896 void md_unregister_thread(struct md_thread
**threadp
)
7898 struct md_thread
*thread
= *threadp
;
7901 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
7902 /* Locking ensures that mddev_unlock does not wake_up a
7903 * non-existent thread
7905 spin_lock(&pers_lock
);
7907 spin_unlock(&pers_lock
);
7909 kthread_stop(thread
->tsk
);
7912 EXPORT_SYMBOL(md_unregister_thread
);
7914 void md_error(struct mddev
*mddev
, struct md_rdev
*rdev
)
7916 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
7919 if (!mddev
->pers
|| !mddev
->pers
->error_handler
)
7921 mddev
->pers
->error_handler(mddev
,rdev
);
7922 if (mddev
->degraded
)
7923 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7924 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
7925 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7926 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7927 md_wakeup_thread(mddev
->thread
);
7928 if (mddev
->event_work
.func
)
7929 queue_work(md_misc_wq
, &mddev
->event_work
);
7930 md_new_event(mddev
);
7932 EXPORT_SYMBOL(md_error
);
7934 /* seq_file implementation /proc/mdstat */
7936 static void status_unused(struct seq_file
*seq
)
7939 struct md_rdev
*rdev
;
7941 seq_printf(seq
, "unused devices: ");
7943 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
7944 char b
[BDEVNAME_SIZE
];
7946 seq_printf(seq
, "%s ",
7947 bdevname(rdev
->bdev
,b
));
7950 seq_printf(seq
, "<none>");
7952 seq_printf(seq
, "\n");
7955 static int status_resync(struct seq_file
*seq
, struct mddev
*mddev
)
7957 sector_t max_sectors
, resync
, res
;
7958 unsigned long dt
, db
= 0;
7959 sector_t rt
, curr_mark_cnt
, resync_mark_cnt
;
7960 int scale
, recovery_active
;
7961 unsigned int per_milli
;
7963 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
7964 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
7965 max_sectors
= mddev
->resync_max_sectors
;
7967 max_sectors
= mddev
->dev_sectors
;
7969 resync
= mddev
->curr_resync
;
7971 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
7972 /* Still cleaning up */
7973 resync
= max_sectors
;
7974 } else if (resync
> max_sectors
)
7975 resync
= max_sectors
;
7977 resync
-= atomic_read(&mddev
->recovery_active
);
7980 if (test_bit(MD_RESYNCING_REMOTE
, &mddev
->recovery
)) {
7981 struct md_rdev
*rdev
;
7983 rdev_for_each(rdev
, mddev
)
7984 if (rdev
->raid_disk
>= 0 &&
7985 !test_bit(Faulty
, &rdev
->flags
) &&
7986 rdev
->recovery_offset
!= MaxSector
&&
7987 rdev
->recovery_offset
) {
7988 seq_printf(seq
, "\trecover=REMOTE");
7991 if (mddev
->reshape_position
!= MaxSector
)
7992 seq_printf(seq
, "\treshape=REMOTE");
7994 seq_printf(seq
, "\tresync=REMOTE");
7997 if (mddev
->recovery_cp
< MaxSector
) {
7998 seq_printf(seq
, "\tresync=PENDING");
8004 seq_printf(seq
, "\tresync=DELAYED");
8008 WARN_ON(max_sectors
== 0);
8009 /* Pick 'scale' such that (resync>>scale)*1000 will fit
8010 * in a sector_t, and (max_sectors>>scale) will fit in a
8011 * u32, as those are the requirements for sector_div.
8012 * Thus 'scale' must be at least 10
8015 if (sizeof(sector_t
) > sizeof(unsigned long)) {
8016 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
8019 res
= (resync
>>scale
)*1000;
8020 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
8024 int i
, x
= per_milli
/50, y
= 20-x
;
8025 seq_printf(seq
, "[");
8026 for (i
= 0; i
< x
; i
++)
8027 seq_printf(seq
, "=");
8028 seq_printf(seq
, ">");
8029 for (i
= 0; i
< y
; i
++)
8030 seq_printf(seq
, ".");
8031 seq_printf(seq
, "] ");
8033 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
8034 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
8036 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
8038 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
8039 "resync" : "recovery"))),
8040 per_milli
/10, per_milli
% 10,
8041 (unsigned long long) resync
/2,
8042 (unsigned long long) max_sectors
/2);
8045 * dt: time from mark until now
8046 * db: blocks written from mark until now
8047 * rt: remaining time
8049 * rt is a sector_t, which is always 64bit now. We are keeping
8050 * the original algorithm, but it is not really necessary.
8052 * Original algorithm:
8053 * So we divide before multiply in case it is 32bit and close
8055 * We scale the divisor (db) by 32 to avoid losing precision
8056 * near the end of resync when the number of remaining sectors
8058 * We then divide rt by 32 after multiplying by db to compensate.
8059 * The '+1' avoids division by zero if db is very small.
8061 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
8064 curr_mark_cnt
= mddev
->curr_mark_cnt
;
8065 recovery_active
= atomic_read(&mddev
->recovery_active
);
8066 resync_mark_cnt
= mddev
->resync_mark_cnt
;
8068 if (curr_mark_cnt
>= (recovery_active
+ resync_mark_cnt
))
8069 db
= curr_mark_cnt
- (recovery_active
+ resync_mark_cnt
);
8071 rt
= max_sectors
- resync
; /* number of remaining sectors */
8072 rt
= div64_u64(rt
, db
/32+1);
8076 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
8077 ((unsigned long)rt
% 60)/6);
8079 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
8083 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
8085 struct list_head
*tmp
;
8087 struct mddev
*mddev
;
8095 spin_lock(&all_mddevs_lock
);
8096 list_for_each(tmp
,&all_mddevs
)
8098 mddev
= list_entry(tmp
, struct mddev
, all_mddevs
);
8100 spin_unlock(&all_mddevs_lock
);
8103 spin_unlock(&all_mddevs_lock
);
8105 return (void*)2;/* tail */
8109 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
8111 struct list_head
*tmp
;
8112 struct mddev
*next_mddev
, *mddev
= v
;
8118 spin_lock(&all_mddevs_lock
);
8120 tmp
= all_mddevs
.next
;
8122 tmp
= mddev
->all_mddevs
.next
;
8123 if (tmp
!= &all_mddevs
)
8124 next_mddev
= mddev_get(list_entry(tmp
,struct mddev
,all_mddevs
));
8126 next_mddev
= (void*)2;
8129 spin_unlock(&all_mddevs_lock
);
8137 static void md_seq_stop(struct seq_file
*seq
, void *v
)
8139 struct mddev
*mddev
= v
;
8141 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
8145 static int md_seq_show(struct seq_file
*seq
, void *v
)
8147 struct mddev
*mddev
= v
;
8149 struct md_rdev
*rdev
;
8151 if (v
== (void*)1) {
8152 struct md_personality
*pers
;
8153 seq_printf(seq
, "Personalities : ");
8154 spin_lock(&pers_lock
);
8155 list_for_each_entry(pers
, &pers_list
, list
)
8156 seq_printf(seq
, "[%s] ", pers
->name
);
8158 spin_unlock(&pers_lock
);
8159 seq_printf(seq
, "\n");
8160 seq
->poll_event
= atomic_read(&md_event_count
);
8163 if (v
== (void*)2) {
8168 spin_lock(&mddev
->lock
);
8169 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
8170 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
8171 mddev
->pers
? "" : "in");
8174 seq_printf(seq
, " (read-only)");
8176 seq_printf(seq
, " (auto-read-only)");
8177 seq_printf(seq
, " %s", mddev
->pers
->name
);
8182 rdev_for_each_rcu(rdev
, mddev
) {
8183 char b
[BDEVNAME_SIZE
];
8184 seq_printf(seq
, " %s[%d]",
8185 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
8186 if (test_bit(WriteMostly
, &rdev
->flags
))
8187 seq_printf(seq
, "(W)");
8188 if (test_bit(Journal
, &rdev
->flags
))
8189 seq_printf(seq
, "(J)");
8190 if (test_bit(Faulty
, &rdev
->flags
)) {
8191 seq_printf(seq
, "(F)");
8194 if (rdev
->raid_disk
< 0)
8195 seq_printf(seq
, "(S)"); /* spare */
8196 if (test_bit(Replacement
, &rdev
->flags
))
8197 seq_printf(seq
, "(R)");
8198 sectors
+= rdev
->sectors
;
8202 if (!list_empty(&mddev
->disks
)) {
8204 seq_printf(seq
, "\n %llu blocks",
8205 (unsigned long long)
8206 mddev
->array_sectors
/ 2);
8208 seq_printf(seq
, "\n %llu blocks",
8209 (unsigned long long)sectors
/ 2);
8211 if (mddev
->persistent
) {
8212 if (mddev
->major_version
!= 0 ||
8213 mddev
->minor_version
!= 90) {
8214 seq_printf(seq
," super %d.%d",
8215 mddev
->major_version
,
8216 mddev
->minor_version
);
8218 } else if (mddev
->external
)
8219 seq_printf(seq
, " super external:%s",
8220 mddev
->metadata_type
);
8222 seq_printf(seq
, " super non-persistent");
8225 mddev
->pers
->status(seq
, mddev
);
8226 seq_printf(seq
, "\n ");
8227 if (mddev
->pers
->sync_request
) {
8228 if (status_resync(seq
, mddev
))
8229 seq_printf(seq
, "\n ");
8232 seq_printf(seq
, "\n ");
8234 md_bitmap_status(seq
, mddev
->bitmap
);
8236 seq_printf(seq
, "\n");
8238 spin_unlock(&mddev
->lock
);
8243 static const struct seq_operations md_seq_ops
= {
8244 .start
= md_seq_start
,
8245 .next
= md_seq_next
,
8246 .stop
= md_seq_stop
,
8247 .show
= md_seq_show
,
8250 static int md_seq_open(struct inode
*inode
, struct file
*file
)
8252 struct seq_file
*seq
;
8255 error
= seq_open(file
, &md_seq_ops
);
8259 seq
= file
->private_data
;
8260 seq
->poll_event
= atomic_read(&md_event_count
);
8264 static int md_unloading
;
8265 static __poll_t
mdstat_poll(struct file
*filp
, poll_table
*wait
)
8267 struct seq_file
*seq
= filp
->private_data
;
8271 return EPOLLIN
|EPOLLRDNORM
|EPOLLERR
|EPOLLPRI
;
8272 poll_wait(filp
, &md_event_waiters
, wait
);
8274 /* always allow read */
8275 mask
= EPOLLIN
| EPOLLRDNORM
;
8277 if (seq
->poll_event
!= atomic_read(&md_event_count
))
8278 mask
|= EPOLLERR
| EPOLLPRI
;
8282 static const struct proc_ops mdstat_proc_ops
= {
8283 .proc_open
= md_seq_open
,
8284 .proc_read
= seq_read
,
8285 .proc_lseek
= seq_lseek
,
8286 .proc_release
= seq_release
,
8287 .proc_poll
= mdstat_poll
,
8290 int register_md_personality(struct md_personality
*p
)
8292 pr_debug("md: %s personality registered for level %d\n",
8294 spin_lock(&pers_lock
);
8295 list_add_tail(&p
->list
, &pers_list
);
8296 spin_unlock(&pers_lock
);
8299 EXPORT_SYMBOL(register_md_personality
);
8301 int unregister_md_personality(struct md_personality
*p
)
8303 pr_debug("md: %s personality unregistered\n", p
->name
);
8304 spin_lock(&pers_lock
);
8305 list_del_init(&p
->list
);
8306 spin_unlock(&pers_lock
);
8309 EXPORT_SYMBOL(unregister_md_personality
);
8311 int register_md_cluster_operations(struct md_cluster_operations
*ops
,
8312 struct module
*module
)
8315 spin_lock(&pers_lock
);
8316 if (md_cluster_ops
!= NULL
)
8319 md_cluster_ops
= ops
;
8320 md_cluster_mod
= module
;
8322 spin_unlock(&pers_lock
);
8325 EXPORT_SYMBOL(register_md_cluster_operations
);
8327 int unregister_md_cluster_operations(void)
8329 spin_lock(&pers_lock
);
8330 md_cluster_ops
= NULL
;
8331 spin_unlock(&pers_lock
);
8334 EXPORT_SYMBOL(unregister_md_cluster_operations
);
8336 int md_setup_cluster(struct mddev
*mddev
, int nodes
)
8338 if (!md_cluster_ops
)
8339 request_module("md-cluster");
8340 spin_lock(&pers_lock
);
8341 /* ensure module won't be unloaded */
8342 if (!md_cluster_ops
|| !try_module_get(md_cluster_mod
)) {
8343 pr_warn("can't find md-cluster module or get it's reference.\n");
8344 spin_unlock(&pers_lock
);
8347 spin_unlock(&pers_lock
);
8349 return md_cluster_ops
->join(mddev
, nodes
);
8352 void md_cluster_stop(struct mddev
*mddev
)
8354 if (!md_cluster_ops
)
8356 md_cluster_ops
->leave(mddev
);
8357 module_put(md_cluster_mod
);
8360 static int is_mddev_idle(struct mddev
*mddev
, int init
)
8362 struct md_rdev
*rdev
;
8368 rdev_for_each_rcu(rdev
, mddev
) {
8369 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
8370 curr_events
= (int)part_stat_read_accum(&disk
->part0
, sectors
) -
8371 atomic_read(&disk
->sync_io
);
8372 /* sync IO will cause sync_io to increase before the disk_stats
8373 * as sync_io is counted when a request starts, and
8374 * disk_stats is counted when it completes.
8375 * So resync activity will cause curr_events to be smaller than
8376 * when there was no such activity.
8377 * non-sync IO will cause disk_stat to increase without
8378 * increasing sync_io so curr_events will (eventually)
8379 * be larger than it was before. Once it becomes
8380 * substantially larger, the test below will cause
8381 * the array to appear non-idle, and resync will slow
8383 * If there is a lot of outstanding resync activity when
8384 * we set last_event to curr_events, then all that activity
8385 * completing might cause the array to appear non-idle
8386 * and resync will be slowed down even though there might
8387 * not have been non-resync activity. This will only
8388 * happen once though. 'last_events' will soon reflect
8389 * the state where there is little or no outstanding
8390 * resync requests, and further resync activity will
8391 * always make curr_events less than last_events.
8394 if (init
|| curr_events
- rdev
->last_events
> 64) {
8395 rdev
->last_events
= curr_events
;
8403 void md_done_sync(struct mddev
*mddev
, int blocks
, int ok
)
8405 /* another "blocks" (512byte) blocks have been synced */
8406 atomic_sub(blocks
, &mddev
->recovery_active
);
8407 wake_up(&mddev
->recovery_wait
);
8409 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8410 set_bit(MD_RECOVERY_ERROR
, &mddev
->recovery
);
8411 md_wakeup_thread(mddev
->thread
);
8412 // stop recovery, signal do_sync ....
8415 EXPORT_SYMBOL(md_done_sync
);
8417 /* md_write_start(mddev, bi)
8418 * If we need to update some array metadata (e.g. 'active' flag
8419 * in superblock) before writing, schedule a superblock update
8420 * and wait for it to complete.
8421 * A return value of 'false' means that the write wasn't recorded
8422 * and cannot proceed as the array is being suspend.
8424 bool md_write_start(struct mddev
*mddev
, struct bio
*bi
)
8428 if (bio_data_dir(bi
) != WRITE
)
8431 BUG_ON(mddev
->ro
== 1);
8432 if (mddev
->ro
== 2) {
8433 /* need to switch to read/write */
8435 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8436 md_wakeup_thread(mddev
->thread
);
8437 md_wakeup_thread(mddev
->sync_thread
);
8441 percpu_ref_get(&mddev
->writes_pending
);
8442 smp_mb(); /* Match smp_mb in set_in_sync() */
8443 if (mddev
->safemode
== 1)
8444 mddev
->safemode
= 0;
8445 /* sync_checkers is always 0 when writes_pending is in per-cpu mode */
8446 if (mddev
->in_sync
|| mddev
->sync_checkers
) {
8447 spin_lock(&mddev
->lock
);
8448 if (mddev
->in_sync
) {
8450 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
8451 set_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
8452 md_wakeup_thread(mddev
->thread
);
8455 spin_unlock(&mddev
->lock
);
8459 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
8460 if (!mddev
->has_superblocks
)
8462 wait_event(mddev
->sb_wait
,
8463 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
) ||
8465 if (test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
)) {
8466 percpu_ref_put(&mddev
->writes_pending
);
8471 EXPORT_SYMBOL(md_write_start
);
8473 /* md_write_inc can only be called when md_write_start() has
8474 * already been called at least once of the current request.
8475 * It increments the counter and is useful when a single request
8476 * is split into several parts. Each part causes an increment and
8477 * so needs a matching md_write_end().
8478 * Unlike md_write_start(), it is safe to call md_write_inc() inside
8479 * a spinlocked region.
8481 void md_write_inc(struct mddev
*mddev
, struct bio
*bi
)
8483 if (bio_data_dir(bi
) != WRITE
)
8485 WARN_ON_ONCE(mddev
->in_sync
|| mddev
->ro
);
8486 percpu_ref_get(&mddev
->writes_pending
);
8488 EXPORT_SYMBOL(md_write_inc
);
8490 void md_write_end(struct mddev
*mddev
)
8492 percpu_ref_put(&mddev
->writes_pending
);
8494 if (mddev
->safemode
== 2)
8495 md_wakeup_thread(mddev
->thread
);
8496 else if (mddev
->safemode_delay
)
8497 /* The roundup() ensures this only performs locking once
8498 * every ->safemode_delay jiffies
8500 mod_timer(&mddev
->safemode_timer
,
8501 roundup(jiffies
, mddev
->safemode_delay
) +
8502 mddev
->safemode_delay
);
8505 EXPORT_SYMBOL(md_write_end
);
8507 /* md_allow_write(mddev)
8508 * Calling this ensures that the array is marked 'active' so that writes
8509 * may proceed without blocking. It is important to call this before
8510 * attempting a GFP_KERNEL allocation while holding the mddev lock.
8511 * Must be called with mddev_lock held.
8513 void md_allow_write(struct mddev
*mddev
)
8519 if (!mddev
->pers
->sync_request
)
8522 spin_lock(&mddev
->lock
);
8523 if (mddev
->in_sync
) {
8525 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
8526 set_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
8527 if (mddev
->safemode_delay
&&
8528 mddev
->safemode
== 0)
8529 mddev
->safemode
= 1;
8530 spin_unlock(&mddev
->lock
);
8531 md_update_sb(mddev
, 0);
8532 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
8533 /* wait for the dirty state to be recorded in the metadata */
8534 wait_event(mddev
->sb_wait
,
8535 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
8537 spin_unlock(&mddev
->lock
);
8539 EXPORT_SYMBOL_GPL(md_allow_write
);
8541 #define SYNC_MARKS 10
8542 #define SYNC_MARK_STEP (3*HZ)
8543 #define UPDATE_FREQUENCY (5*60*HZ)
8544 void md_do_sync(struct md_thread
*thread
)
8546 struct mddev
*mddev
= thread
->mddev
;
8547 struct mddev
*mddev2
;
8548 unsigned int currspeed
= 0, window
;
8549 sector_t max_sectors
,j
, io_sectors
, recovery_done
;
8550 unsigned long mark
[SYNC_MARKS
];
8551 unsigned long update_time
;
8552 sector_t mark_cnt
[SYNC_MARKS
];
8554 struct list_head
*tmp
;
8555 sector_t last_check
;
8557 struct md_rdev
*rdev
;
8558 char *desc
, *action
= NULL
;
8559 struct blk_plug plug
;
8562 /* just incase thread restarts... */
8563 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
8564 test_bit(MD_RECOVERY_WAIT
, &mddev
->recovery
))
8566 if (mddev
->ro
) {/* never try to sync a read-only array */
8567 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8571 if (mddev_is_clustered(mddev
)) {
8572 ret
= md_cluster_ops
->resync_start(mddev
);
8576 set_bit(MD_CLUSTER_RESYNC_LOCKED
, &mddev
->flags
);
8577 if (!(test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
8578 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) ||
8579 test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
8580 && ((unsigned long long)mddev
->curr_resync_completed
8581 < (unsigned long long)mddev
->resync_max_sectors
))
8585 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
8586 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)) {
8587 desc
= "data-check";
8589 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
8590 desc
= "requested-resync";
8594 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
8599 mddev
->last_sync_action
= action
?: desc
;
8601 /* we overload curr_resync somewhat here.
8602 * 0 == not engaged in resync at all
8603 * 2 == checking that there is no conflict with another sync
8604 * 1 == like 2, but have yielded to allow conflicting resync to
8606 * other == active in resync - this many blocks
8608 * Before starting a resync we must have set curr_resync to
8609 * 2, and then checked that every "conflicting" array has curr_resync
8610 * less than ours. When we find one that is the same or higher
8611 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8612 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8613 * This will mean we have to start checking from the beginning again.
8618 int mddev2_minor
= -1;
8619 mddev
->curr_resync
= 2;
8622 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8624 for_each_mddev(mddev2
, tmp
) {
8625 if (mddev2
== mddev
)
8627 if (!mddev
->parallel_resync
8628 && mddev2
->curr_resync
8629 && match_mddev_units(mddev
, mddev2
)) {
8631 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
8632 /* arbitrarily yield */
8633 mddev
->curr_resync
= 1;
8634 wake_up(&resync_wait
);
8636 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
8637 /* no need to wait here, we can wait the next
8638 * time 'round when curr_resync == 2
8641 /* We need to wait 'interruptible' so as not to
8642 * contribute to the load average, and not to
8643 * be caught by 'softlockup'
8645 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
8646 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
8647 mddev2
->curr_resync
>= mddev
->curr_resync
) {
8648 if (mddev2_minor
!= mddev2
->md_minor
) {
8649 mddev2_minor
= mddev2
->md_minor
;
8650 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8651 desc
, mdname(mddev
),
8655 if (signal_pending(current
))
8656 flush_signals(current
);
8658 finish_wait(&resync_wait
, &wq
);
8661 finish_wait(&resync_wait
, &wq
);
8664 } while (mddev
->curr_resync
< 2);
8667 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
8668 /* resync follows the size requested by the personality,
8669 * which defaults to physical size, but can be virtual size
8671 max_sectors
= mddev
->resync_max_sectors
;
8672 atomic64_set(&mddev
->resync_mismatches
, 0);
8673 /* we don't use the checkpoint if there's a bitmap */
8674 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
8675 j
= mddev
->resync_min
;
8676 else if (!mddev
->bitmap
)
8677 j
= mddev
->recovery_cp
;
8679 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)) {
8680 max_sectors
= mddev
->resync_max_sectors
;
8682 * If the original node aborts reshaping then we continue the
8683 * reshaping, so set j again to avoid restart reshape from the
8686 if (mddev_is_clustered(mddev
) &&
8687 mddev
->reshape_position
!= MaxSector
)
8688 j
= mddev
->reshape_position
;
8690 /* recovery follows the physical size of devices */
8691 max_sectors
= mddev
->dev_sectors
;
8694 rdev_for_each_rcu(rdev
, mddev
)
8695 if (rdev
->raid_disk
>= 0 &&
8696 !test_bit(Journal
, &rdev
->flags
) &&
8697 !test_bit(Faulty
, &rdev
->flags
) &&
8698 !test_bit(In_sync
, &rdev
->flags
) &&
8699 rdev
->recovery_offset
< j
)
8700 j
= rdev
->recovery_offset
;
8703 /* If there is a bitmap, we need to make sure all
8704 * writes that started before we added a spare
8705 * complete before we start doing a recovery.
8706 * Otherwise the write might complete and (via
8707 * bitmap_endwrite) set a bit in the bitmap after the
8708 * recovery has checked that bit and skipped that
8711 if (mddev
->bitmap
) {
8712 mddev
->pers
->quiesce(mddev
, 1);
8713 mddev
->pers
->quiesce(mddev
, 0);
8717 pr_info("md: %s of RAID array %s\n", desc
, mdname(mddev
));
8718 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev
));
8719 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8720 speed_max(mddev
), desc
);
8722 is_mddev_idle(mddev
, 1); /* this initializes IO event counters */
8725 for (m
= 0; m
< SYNC_MARKS
; m
++) {
8727 mark_cnt
[m
] = io_sectors
;
8730 mddev
->resync_mark
= mark
[last_mark
];
8731 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
8734 * Tune reconstruction:
8736 window
= 32 * (PAGE_SIZE
/ 512);
8737 pr_debug("md: using %dk window, over a total of %lluk.\n",
8738 window
/2, (unsigned long long)max_sectors
/2);
8740 atomic_set(&mddev
->recovery_active
, 0);
8744 pr_debug("md: resuming %s of %s from checkpoint.\n",
8745 desc
, mdname(mddev
));
8746 mddev
->curr_resync
= j
;
8748 mddev
->curr_resync
= 3; /* no longer delayed */
8749 mddev
->curr_resync_completed
= j
;
8750 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
8751 md_new_event(mddev
);
8752 update_time
= jiffies
;
8754 blk_start_plug(&plug
);
8755 while (j
< max_sectors
) {
8760 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8761 ((mddev
->curr_resync
> mddev
->curr_resync_completed
&&
8762 (mddev
->curr_resync
- mddev
->curr_resync_completed
)
8763 > (max_sectors
>> 4)) ||
8764 time_after_eq(jiffies
, update_time
+ UPDATE_FREQUENCY
) ||
8765 (j
- mddev
->curr_resync_completed
)*2
8766 >= mddev
->resync_max
- mddev
->curr_resync_completed
||
8767 mddev
->curr_resync_completed
> mddev
->resync_max
8769 /* time to update curr_resync_completed */
8770 wait_event(mddev
->recovery_wait
,
8771 atomic_read(&mddev
->recovery_active
) == 0);
8772 mddev
->curr_resync_completed
= j
;
8773 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) &&
8774 j
> mddev
->recovery_cp
)
8775 mddev
->recovery_cp
= j
;
8776 update_time
= jiffies
;
8777 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
8778 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
8781 while (j
>= mddev
->resync_max
&&
8782 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
8783 /* As this condition is controlled by user-space,
8784 * we can block indefinitely, so use '_interruptible'
8785 * to avoid triggering warnings.
8787 flush_signals(current
); /* just in case */
8788 wait_event_interruptible(mddev
->recovery_wait
,
8789 mddev
->resync_max
> j
8790 || test_bit(MD_RECOVERY_INTR
,
8794 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8797 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
);
8799 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8803 if (!skipped
) { /* actual IO requested */
8804 io_sectors
+= sectors
;
8805 atomic_add(sectors
, &mddev
->recovery_active
);
8808 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8812 if (j
> max_sectors
)
8813 /* when skipping, extra large numbers can be returned. */
8816 mddev
->curr_resync
= j
;
8817 mddev
->curr_mark_cnt
= io_sectors
;
8818 if (last_check
== 0)
8819 /* this is the earliest that rebuild will be
8820 * visible in /proc/mdstat
8822 md_new_event(mddev
);
8824 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
8827 last_check
= io_sectors
;
8829 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
8831 int next
= (last_mark
+1) % SYNC_MARKS
;
8833 mddev
->resync_mark
= mark
[next
];
8834 mddev
->resync_mark_cnt
= mark_cnt
[next
];
8835 mark
[next
] = jiffies
;
8836 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
8840 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8844 * this loop exits only if either when we are slower than
8845 * the 'hard' speed limit, or the system was IO-idle for
8847 * the system might be non-idle CPU-wise, but we only care
8848 * about not overloading the IO subsystem. (things like an
8849 * e2fsck being done on the RAID array should execute fast)
8853 recovery_done
= io_sectors
- atomic_read(&mddev
->recovery_active
);
8854 currspeed
= ((unsigned long)(recovery_done
- mddev
->resync_mark_cnt
))/2
8855 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
8857 if (currspeed
> speed_min(mddev
)) {
8858 if (currspeed
> speed_max(mddev
)) {
8862 if (!is_mddev_idle(mddev
, 0)) {
8864 * Give other IO more of a chance.
8865 * The faster the devices, the less we wait.
8867 wait_event(mddev
->recovery_wait
,
8868 !atomic_read(&mddev
->recovery_active
));
8872 pr_info("md: %s: %s %s.\n",mdname(mddev
), desc
,
8873 test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)
8874 ? "interrupted" : "done");
8876 * this also signals 'finished resyncing' to md_stop
8878 blk_finish_plug(&plug
);
8879 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
8881 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8882 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
8883 mddev
->curr_resync
> 3) {
8884 mddev
->curr_resync_completed
= mddev
->curr_resync
;
8885 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
8887 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
);
8889 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
8890 mddev
->curr_resync
> 3) {
8891 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
8892 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
8893 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
8894 pr_debug("md: checkpointing %s of %s.\n",
8895 desc
, mdname(mddev
));
8896 if (test_bit(MD_RECOVERY_ERROR
,
8898 mddev
->recovery_cp
=
8899 mddev
->curr_resync_completed
;
8901 mddev
->recovery_cp
=
8905 mddev
->recovery_cp
= MaxSector
;
8907 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8908 mddev
->curr_resync
= MaxSector
;
8909 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8910 test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
)) {
8912 rdev_for_each_rcu(rdev
, mddev
)
8913 if (rdev
->raid_disk
>= 0 &&
8914 mddev
->delta_disks
>= 0 &&
8915 !test_bit(Journal
, &rdev
->flags
) &&
8916 !test_bit(Faulty
, &rdev
->flags
) &&
8917 !test_bit(In_sync
, &rdev
->flags
) &&
8918 rdev
->recovery_offset
< mddev
->curr_resync
)
8919 rdev
->recovery_offset
= mddev
->curr_resync
;
8925 /* set CHANGE_PENDING here since maybe another update is needed,
8926 * so other nodes are informed. It should be harmless for normal
8928 set_mask_bits(&mddev
->sb_flags
, 0,
8929 BIT(MD_SB_CHANGE_PENDING
) | BIT(MD_SB_CHANGE_DEVS
));
8931 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8932 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
8933 mddev
->delta_disks
> 0 &&
8934 mddev
->pers
->finish_reshape
&&
8935 mddev
->pers
->size
&&
8937 mddev_lock_nointr(mddev
);
8938 md_set_array_sectors(mddev
, mddev
->pers
->size(mddev
, 0, 0));
8939 mddev_unlock(mddev
);
8940 if (!mddev_is_clustered(mddev
)) {
8941 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
8942 revalidate_disk(mddev
->gendisk
);
8946 spin_lock(&mddev
->lock
);
8947 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
8948 /* We completed so min/max setting can be forgotten if used. */
8949 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
8950 mddev
->resync_min
= 0;
8951 mddev
->resync_max
= MaxSector
;
8952 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
8953 mddev
->resync_min
= mddev
->curr_resync_completed
;
8954 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
8955 mddev
->curr_resync
= 0;
8956 spin_unlock(&mddev
->lock
);
8958 wake_up(&resync_wait
);
8959 md_wakeup_thread(mddev
->thread
);
8962 EXPORT_SYMBOL_GPL(md_do_sync
);
8964 static int remove_and_add_spares(struct mddev
*mddev
,
8965 struct md_rdev
*this)
8967 struct md_rdev
*rdev
;
8970 bool remove_some
= false;
8972 if (this && test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
8973 /* Mustn't remove devices when resync thread is running */
8976 rdev_for_each(rdev
, mddev
) {
8977 if ((this == NULL
|| rdev
== this) &&
8978 rdev
->raid_disk
>= 0 &&
8979 !test_bit(Blocked
, &rdev
->flags
) &&
8980 test_bit(Faulty
, &rdev
->flags
) &&
8981 atomic_read(&rdev
->nr_pending
)==0) {
8982 /* Faulty non-Blocked devices with nr_pending == 0
8983 * never get nr_pending incremented,
8984 * never get Faulty cleared, and never get Blocked set.
8985 * So we can synchronize_rcu now rather than once per device
8988 set_bit(RemoveSynchronized
, &rdev
->flags
);
8994 rdev_for_each(rdev
, mddev
) {
8995 if ((this == NULL
|| rdev
== this) &&
8996 rdev
->raid_disk
>= 0 &&
8997 !test_bit(Blocked
, &rdev
->flags
) &&
8998 ((test_bit(RemoveSynchronized
, &rdev
->flags
) ||
8999 (!test_bit(In_sync
, &rdev
->flags
) &&
9000 !test_bit(Journal
, &rdev
->flags
))) &&
9001 atomic_read(&rdev
->nr_pending
)==0)) {
9002 if (mddev
->pers
->hot_remove_disk(
9003 mddev
, rdev
) == 0) {
9004 sysfs_unlink_rdev(mddev
, rdev
);
9005 rdev
->saved_raid_disk
= rdev
->raid_disk
;
9006 rdev
->raid_disk
= -1;
9010 if (remove_some
&& test_bit(RemoveSynchronized
, &rdev
->flags
))
9011 clear_bit(RemoveSynchronized
, &rdev
->flags
);
9014 if (removed
&& mddev
->kobj
.sd
)
9015 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
9017 if (this && removed
)
9020 rdev_for_each(rdev
, mddev
) {
9021 if (this && this != rdev
)
9023 if (test_bit(Candidate
, &rdev
->flags
))
9025 if (rdev
->raid_disk
>= 0 &&
9026 !test_bit(In_sync
, &rdev
->flags
) &&
9027 !test_bit(Journal
, &rdev
->flags
) &&
9028 !test_bit(Faulty
, &rdev
->flags
))
9030 if (rdev
->raid_disk
>= 0)
9032 if (test_bit(Faulty
, &rdev
->flags
))
9034 if (!test_bit(Journal
, &rdev
->flags
)) {
9036 ! (rdev
->saved_raid_disk
>= 0 &&
9037 !test_bit(Bitmap_sync
, &rdev
->flags
)))
9040 rdev
->recovery_offset
= 0;
9043 hot_add_disk(mddev
, rdev
) == 0) {
9044 if (sysfs_link_rdev(mddev
, rdev
))
9045 /* failure here is OK */;
9046 if (!test_bit(Journal
, &rdev
->flags
))
9048 md_new_event(mddev
);
9049 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
9054 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
9058 static void md_start_sync(struct work_struct
*ws
)
9060 struct mddev
*mddev
= container_of(ws
, struct mddev
, del_work
);
9062 mddev
->sync_thread
= md_register_thread(md_do_sync
,
9065 if (!mddev
->sync_thread
) {
9066 pr_warn("%s: could not start resync thread...\n",
9068 /* leave the spares where they are, it shouldn't hurt */
9069 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
9070 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
9071 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
9072 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
9073 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
9074 wake_up(&resync_wait
);
9075 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
9077 if (mddev
->sysfs_action
)
9078 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
9080 md_wakeup_thread(mddev
->sync_thread
);
9081 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
9082 md_new_event(mddev
);
9086 * This routine is regularly called by all per-raid-array threads to
9087 * deal with generic issues like resync and super-block update.
9088 * Raid personalities that don't have a thread (linear/raid0) do not
9089 * need this as they never do any recovery or update the superblock.
9091 * It does not do any resync itself, but rather "forks" off other threads
9092 * to do that as needed.
9093 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
9094 * "->recovery" and create a thread at ->sync_thread.
9095 * When the thread finishes it sets MD_RECOVERY_DONE
9096 * and wakeups up this thread which will reap the thread and finish up.
9097 * This thread also removes any faulty devices (with nr_pending == 0).
9099 * The overall approach is:
9100 * 1/ if the superblock needs updating, update it.
9101 * 2/ If a recovery thread is running, don't do anything else.
9102 * 3/ If recovery has finished, clean up, possibly marking spares active.
9103 * 4/ If there are any faulty devices, remove them.
9104 * 5/ If array is degraded, try to add spares devices
9105 * 6/ If array has spares or is not in-sync, start a resync thread.
9107 void md_check_recovery(struct mddev
*mddev
)
9109 if (test_bit(MD_ALLOW_SB_UPDATE
, &mddev
->flags
) && mddev
->sb_flags
) {
9110 /* Write superblock - thread that called mddev_suspend()
9111 * holds reconfig_mutex for us.
9113 set_bit(MD_UPDATING_SB
, &mddev
->flags
);
9114 smp_mb__after_atomic();
9115 if (test_bit(MD_ALLOW_SB_UPDATE
, &mddev
->flags
))
9116 md_update_sb(mddev
, 0);
9117 clear_bit_unlock(MD_UPDATING_SB
, &mddev
->flags
);
9118 wake_up(&mddev
->sb_wait
);
9121 if (mddev
->suspended
)
9125 md_bitmap_daemon_work(mddev
);
9127 if (signal_pending(current
)) {
9128 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
9129 pr_debug("md: %s in immediate safe mode\n",
9131 mddev
->safemode
= 2;
9133 flush_signals(current
);
9136 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
9139 (mddev
->sb_flags
& ~ (1<<MD_SB_CHANGE_PENDING
)) ||
9140 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
9141 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
9142 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
9143 (mddev
->safemode
== 2
9144 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
9148 if (mddev_trylock(mddev
)) {
9150 bool try_set_sync
= mddev
->safemode
!= 0;
9152 if (!mddev
->external
&& mddev
->safemode
== 1)
9153 mddev
->safemode
= 0;
9156 struct md_rdev
*rdev
;
9157 if (!mddev
->external
&& mddev
->in_sync
)
9158 /* 'Blocked' flag not needed as failed devices
9159 * will be recorded if array switched to read/write.
9160 * Leaving it set will prevent the device
9161 * from being removed.
9163 rdev_for_each(rdev
, mddev
)
9164 clear_bit(Blocked
, &rdev
->flags
);
9165 /* On a read-only array we can:
9166 * - remove failed devices
9167 * - add already-in_sync devices if the array itself
9169 * As we only add devices that are already in-sync,
9170 * we can activate the spares immediately.
9172 remove_and_add_spares(mddev
, NULL
);
9173 /* There is no thread, but we need to call
9174 * ->spare_active and clear saved_raid_disk
9176 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
9177 md_reap_sync_thread(mddev
);
9178 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
9179 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
9180 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
9184 if (mddev_is_clustered(mddev
)) {
9185 struct md_rdev
*rdev
;
9186 /* kick the device if another node issued a
9189 rdev_for_each(rdev
, mddev
) {
9190 if (test_and_clear_bit(ClusterRemove
, &rdev
->flags
) &&
9191 rdev
->raid_disk
< 0)
9192 md_kick_rdev_from_array(rdev
);
9196 if (try_set_sync
&& !mddev
->external
&& !mddev
->in_sync
) {
9197 spin_lock(&mddev
->lock
);
9199 spin_unlock(&mddev
->lock
);
9202 if (mddev
->sb_flags
)
9203 md_update_sb(mddev
, 0);
9205 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
9206 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
9207 /* resync/recovery still happening */
9208 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
9211 if (mddev
->sync_thread
) {
9212 md_reap_sync_thread(mddev
);
9215 /* Set RUNNING before clearing NEEDED to avoid
9216 * any transients in the value of "sync_action".
9218 mddev
->curr_resync_completed
= 0;
9219 spin_lock(&mddev
->lock
);
9220 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
9221 spin_unlock(&mddev
->lock
);
9222 /* Clear some bits that don't mean anything, but
9225 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
9226 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
9228 if (!test_and_clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
9229 test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
9231 /* no recovery is running.
9232 * remove any failed drives, then
9233 * add spares if possible.
9234 * Spares are also removed and re-added, to allow
9235 * the personality to fail the re-add.
9238 if (mddev
->reshape_position
!= MaxSector
) {
9239 if (mddev
->pers
->check_reshape
== NULL
||
9240 mddev
->pers
->check_reshape(mddev
) != 0)
9241 /* Cannot proceed */
9243 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
9244 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
9245 } else if ((spares
= remove_and_add_spares(mddev
, NULL
))) {
9246 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
9247 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
9248 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
9249 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
9250 } else if (mddev
->recovery_cp
< MaxSector
) {
9251 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
9252 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
9253 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
9254 /* nothing to be done ... */
9257 if (mddev
->pers
->sync_request
) {
9259 /* We are adding a device or devices to an array
9260 * which has the bitmap stored on all devices.
9261 * So make sure all bitmap pages get written
9263 md_bitmap_write_all(mddev
->bitmap
);
9265 INIT_WORK(&mddev
->del_work
, md_start_sync
);
9266 queue_work(md_misc_wq
, &mddev
->del_work
);
9270 if (!mddev
->sync_thread
) {
9271 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
9272 wake_up(&resync_wait
);
9273 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
9275 if (mddev
->sysfs_action
)
9276 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
9279 wake_up(&mddev
->sb_wait
);
9280 mddev_unlock(mddev
);
9283 EXPORT_SYMBOL(md_check_recovery
);
9285 void md_reap_sync_thread(struct mddev
*mddev
)
9287 struct md_rdev
*rdev
;
9288 sector_t old_dev_sectors
= mddev
->dev_sectors
;
9289 bool is_reshaped
= false;
9291 /* resync has finished, collect result */
9292 md_unregister_thread(&mddev
->sync_thread
);
9293 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
9294 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
) &&
9295 mddev
->degraded
!= mddev
->raid_disks
) {
9297 /* activate any spares */
9298 if (mddev
->pers
->spare_active(mddev
)) {
9299 sysfs_notify(&mddev
->kobj
, NULL
,
9301 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
9304 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
9305 mddev
->pers
->finish_reshape
) {
9306 mddev
->pers
->finish_reshape(mddev
);
9307 if (mddev_is_clustered(mddev
))
9311 /* If array is no-longer degraded, then any saved_raid_disk
9312 * information must be scrapped.
9314 if (!mddev
->degraded
)
9315 rdev_for_each(rdev
, mddev
)
9316 rdev
->saved_raid_disk
= -1;
9318 md_update_sb(mddev
, 1);
9319 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
9320 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
9322 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED
, &mddev
->flags
))
9323 md_cluster_ops
->resync_finish(mddev
);
9324 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
9325 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
9326 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
9327 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
9328 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
9329 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
9331 * We call md_cluster_ops->update_size here because sync_size could
9332 * be changed by md_update_sb, and MD_RECOVERY_RESHAPE is cleared,
9333 * so it is time to update size across cluster.
9335 if (mddev_is_clustered(mddev
) && is_reshaped
9336 && !test_bit(MD_CLOSING
, &mddev
->flags
))
9337 md_cluster_ops
->update_size(mddev
, old_dev_sectors
);
9338 wake_up(&resync_wait
);
9339 /* flag recovery needed just to double check */
9340 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
9341 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
9342 md_new_event(mddev
);
9343 if (mddev
->event_work
.func
)
9344 queue_work(md_misc_wq
, &mddev
->event_work
);
9346 EXPORT_SYMBOL(md_reap_sync_thread
);
9348 void md_wait_for_blocked_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
9350 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
9351 wait_event_timeout(rdev
->blocked_wait
,
9352 !test_bit(Blocked
, &rdev
->flags
) &&
9353 !test_bit(BlockedBadBlocks
, &rdev
->flags
),
9354 msecs_to_jiffies(5000));
9355 rdev_dec_pending(rdev
, mddev
);
9357 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
9359 void md_finish_reshape(struct mddev
*mddev
)
9361 /* called be personality module when reshape completes. */
9362 struct md_rdev
*rdev
;
9364 rdev_for_each(rdev
, mddev
) {
9365 if (rdev
->data_offset
> rdev
->new_data_offset
)
9366 rdev
->sectors
+= rdev
->data_offset
- rdev
->new_data_offset
;
9368 rdev
->sectors
-= rdev
->new_data_offset
- rdev
->data_offset
;
9369 rdev
->data_offset
= rdev
->new_data_offset
;
9372 EXPORT_SYMBOL(md_finish_reshape
);
9374 /* Bad block management */
9376 /* Returns 1 on success, 0 on failure */
9377 int rdev_set_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
,
9380 struct mddev
*mddev
= rdev
->mddev
;
9383 s
+= rdev
->new_data_offset
;
9385 s
+= rdev
->data_offset
;
9386 rv
= badblocks_set(&rdev
->badblocks
, s
, sectors
, 0);
9388 /* Make sure they get written out promptly */
9389 if (test_bit(ExternalBbl
, &rdev
->flags
))
9390 sysfs_notify(&rdev
->kobj
, NULL
,
9391 "unacknowledged_bad_blocks");
9392 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
9393 set_mask_bits(&mddev
->sb_flags
, 0,
9394 BIT(MD_SB_CHANGE_CLEAN
) | BIT(MD_SB_CHANGE_PENDING
));
9395 md_wakeup_thread(rdev
->mddev
->thread
);
9400 EXPORT_SYMBOL_GPL(rdev_set_badblocks
);
9402 int rdev_clear_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
,
9407 s
+= rdev
->new_data_offset
;
9409 s
+= rdev
->data_offset
;
9410 rv
= badblocks_clear(&rdev
->badblocks
, s
, sectors
);
9411 if ((rv
== 0) && test_bit(ExternalBbl
, &rdev
->flags
))
9412 sysfs_notify(&rdev
->kobj
, NULL
, "bad_blocks");
9415 EXPORT_SYMBOL_GPL(rdev_clear_badblocks
);
9417 static int md_notify_reboot(struct notifier_block
*this,
9418 unsigned long code
, void *x
)
9420 struct list_head
*tmp
;
9421 struct mddev
*mddev
;
9424 for_each_mddev(mddev
, tmp
) {
9425 if (mddev_trylock(mddev
)) {
9427 __md_stop_writes(mddev
);
9428 if (mddev
->persistent
)
9429 mddev
->safemode
= 2;
9430 mddev_unlock(mddev
);
9435 * certain more exotic SCSI devices are known to be
9436 * volatile wrt too early system reboots. While the
9437 * right place to handle this issue is the given
9438 * driver, we do want to have a safe RAID driver ...
9446 static struct notifier_block md_notifier
= {
9447 .notifier_call
= md_notify_reboot
,
9449 .priority
= INT_MAX
, /* before any real devices */
9452 static void md_geninit(void)
9454 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
9456 proc_create("mdstat", S_IRUGO
, NULL
, &mdstat_proc_ops
);
9459 static int __init
md_init(void)
9463 md_wq
= alloc_workqueue("md", WQ_MEM_RECLAIM
, 0);
9467 md_misc_wq
= alloc_workqueue("md_misc", 0, 0);
9471 if ((ret
= register_blkdev(MD_MAJOR
, "md")) < 0)
9474 if ((ret
= register_blkdev(0, "mdp")) < 0)
9478 blk_register_region(MKDEV(MD_MAJOR
, 0), 512, THIS_MODULE
,
9479 md_probe
, NULL
, NULL
);
9480 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
9481 md_probe
, NULL
, NULL
);
9483 register_reboot_notifier(&md_notifier
);
9484 raid_table_header
= register_sysctl_table(raid_root_table
);
9490 unregister_blkdev(MD_MAJOR
, "md");
9492 destroy_workqueue(md_misc_wq
);
9494 destroy_workqueue(md_wq
);
9499 static void check_sb_changes(struct mddev
*mddev
, struct md_rdev
*rdev
)
9501 struct mdp_superblock_1
*sb
= page_address(rdev
->sb_page
);
9502 struct md_rdev
*rdev2
;
9504 char b
[BDEVNAME_SIZE
];
9507 * If size is changed in another node then we need to
9508 * do resize as well.
9510 if (mddev
->dev_sectors
!= le64_to_cpu(sb
->size
)) {
9511 ret
= mddev
->pers
->resize(mddev
, le64_to_cpu(sb
->size
));
9513 pr_info("md-cluster: resize failed\n");
9515 md_bitmap_update_sb(mddev
->bitmap
);
9518 /* Check for change of roles in the active devices */
9519 rdev_for_each(rdev2
, mddev
) {
9520 if (test_bit(Faulty
, &rdev2
->flags
))
9523 /* Check if the roles changed */
9524 role
= le16_to_cpu(sb
->dev_roles
[rdev2
->desc_nr
]);
9526 if (test_bit(Candidate
, &rdev2
->flags
)) {
9527 if (role
== 0xfffe) {
9528 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2
->bdev
,b
));
9529 md_kick_rdev_from_array(rdev2
);
9533 clear_bit(Candidate
, &rdev2
->flags
);
9536 if (role
!= rdev2
->raid_disk
) {
9538 * got activated except reshape is happening.
9540 if (rdev2
->raid_disk
== -1 && role
!= 0xffff &&
9541 !(le32_to_cpu(sb
->feature_map
) &
9542 MD_FEATURE_RESHAPE_ACTIVE
)) {
9543 rdev2
->saved_raid_disk
= role
;
9544 ret
= remove_and_add_spares(mddev
, rdev2
);
9545 pr_info("Activated spare: %s\n",
9546 bdevname(rdev2
->bdev
,b
));
9547 /* wakeup mddev->thread here, so array could
9548 * perform resync with the new activated disk */
9549 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
9550 md_wakeup_thread(mddev
->thread
);
9553 * We just want to do the minimum to mark the disk
9554 * as faulty. The recovery is performed by the
9555 * one who initiated the error.
9557 if ((role
== 0xfffe) || (role
== 0xfffd)) {
9558 md_error(mddev
, rdev2
);
9559 clear_bit(Blocked
, &rdev2
->flags
);
9564 if (mddev
->raid_disks
!= le32_to_cpu(sb
->raid_disks
))
9565 update_raid_disks(mddev
, le32_to_cpu(sb
->raid_disks
));
9568 * Since mddev->delta_disks has already updated in update_raid_disks,
9569 * so it is time to check reshape.
9571 if (test_bit(MD_RESYNCING_REMOTE
, &mddev
->recovery
) &&
9572 (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
9574 * reshape is happening in the remote node, we need to
9575 * update reshape_position and call start_reshape.
9577 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
9578 if (mddev
->pers
->update_reshape_pos
)
9579 mddev
->pers
->update_reshape_pos(mddev
);
9580 if (mddev
->pers
->start_reshape
)
9581 mddev
->pers
->start_reshape(mddev
);
9582 } else if (test_bit(MD_RESYNCING_REMOTE
, &mddev
->recovery
) &&
9583 mddev
->reshape_position
!= MaxSector
&&
9584 !(le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
9585 /* reshape is just done in another node. */
9586 mddev
->reshape_position
= MaxSector
;
9587 if (mddev
->pers
->update_reshape_pos
)
9588 mddev
->pers
->update_reshape_pos(mddev
);
9591 /* Finally set the event to be up to date */
9592 mddev
->events
= le64_to_cpu(sb
->events
);
9595 static int read_rdev(struct mddev
*mddev
, struct md_rdev
*rdev
)
9598 struct page
*swapout
= rdev
->sb_page
;
9599 struct mdp_superblock_1
*sb
;
9601 /* Store the sb page of the rdev in the swapout temporary
9602 * variable in case we err in the future
9604 rdev
->sb_page
= NULL
;
9605 err
= alloc_disk_sb(rdev
);
9607 ClearPageUptodate(rdev
->sb_page
);
9608 rdev
->sb_loaded
= 0;
9609 err
= super_types
[mddev
->major_version
].
9610 load_super(rdev
, NULL
, mddev
->minor_version
);
9613 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9614 __func__
, __LINE__
, rdev
->desc_nr
, err
);
9616 put_page(rdev
->sb_page
);
9617 rdev
->sb_page
= swapout
;
9618 rdev
->sb_loaded
= 1;
9622 sb
= page_address(rdev
->sb_page
);
9623 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9627 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RECOVERY_OFFSET
))
9628 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
9630 /* The other node finished recovery, call spare_active to set
9631 * device In_sync and mddev->degraded
9633 if (rdev
->recovery_offset
== MaxSector
&&
9634 !test_bit(In_sync
, &rdev
->flags
) &&
9635 mddev
->pers
->spare_active(mddev
))
9636 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
9642 void md_reload_sb(struct mddev
*mddev
, int nr
)
9644 struct md_rdev
*rdev
;
9648 rdev_for_each_rcu(rdev
, mddev
) {
9649 if (rdev
->desc_nr
== nr
)
9653 if (!rdev
|| rdev
->desc_nr
!= nr
) {
9654 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__
, __LINE__
, nr
);
9658 err
= read_rdev(mddev
, rdev
);
9662 check_sb_changes(mddev
, rdev
);
9664 /* Read all rdev's to update recovery_offset */
9665 rdev_for_each_rcu(rdev
, mddev
) {
9666 if (!test_bit(Faulty
, &rdev
->flags
))
9667 read_rdev(mddev
, rdev
);
9670 EXPORT_SYMBOL(md_reload_sb
);
9675 * Searches all registered partitions for autorun RAID arrays
9679 static DEFINE_MUTEX(detected_devices_mutex
);
9680 static LIST_HEAD(all_detected_devices
);
9681 struct detected_devices_node
{
9682 struct list_head list
;
9686 void md_autodetect_dev(dev_t dev
)
9688 struct detected_devices_node
*node_detected_dev
;
9690 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
9691 if (node_detected_dev
) {
9692 node_detected_dev
->dev
= dev
;
9693 mutex_lock(&detected_devices_mutex
);
9694 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
9695 mutex_unlock(&detected_devices_mutex
);
9699 static void autostart_arrays(int part
)
9701 struct md_rdev
*rdev
;
9702 struct detected_devices_node
*node_detected_dev
;
9704 int i_scanned
, i_passed
;
9709 pr_info("md: Autodetecting RAID arrays.\n");
9711 mutex_lock(&detected_devices_mutex
);
9712 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
9714 node_detected_dev
= list_entry(all_detected_devices
.next
,
9715 struct detected_devices_node
, list
);
9716 list_del(&node_detected_dev
->list
);
9717 dev
= node_detected_dev
->dev
;
9718 kfree(node_detected_dev
);
9719 mutex_unlock(&detected_devices_mutex
);
9720 rdev
= md_import_device(dev
,0, 90);
9721 mutex_lock(&detected_devices_mutex
);
9725 if (test_bit(Faulty
, &rdev
->flags
))
9728 set_bit(AutoDetected
, &rdev
->flags
);
9729 list_add(&rdev
->same_set
, &pending_raid_disks
);
9732 mutex_unlock(&detected_devices_mutex
);
9734 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned
, i_passed
);
9736 autorun_devices(part
);
9739 #endif /* !MODULE */
9741 static __exit
void md_exit(void)
9743 struct mddev
*mddev
;
9744 struct list_head
*tmp
;
9747 blk_unregister_region(MKDEV(MD_MAJOR
,0), 512);
9748 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
9750 unregister_blkdev(MD_MAJOR
,"md");
9751 unregister_blkdev(mdp_major
, "mdp");
9752 unregister_reboot_notifier(&md_notifier
);
9753 unregister_sysctl_table(raid_table_header
);
9755 /* We cannot unload the modules while some process is
9756 * waiting for us in select() or poll() - wake them up
9759 while (waitqueue_active(&md_event_waiters
)) {
9760 /* not safe to leave yet */
9761 wake_up(&md_event_waiters
);
9765 remove_proc_entry("mdstat", NULL
);
9767 for_each_mddev(mddev
, tmp
) {
9768 export_array(mddev
);
9770 mddev
->hold_active
= 0;
9772 * for_each_mddev() will call mddev_put() at the end of each
9773 * iteration. As the mddev is now fully clear, this will
9774 * schedule the mddev for destruction by a workqueue, and the
9775 * destroy_workqueue() below will wait for that to complete.
9778 destroy_workqueue(md_misc_wq
);
9779 destroy_workqueue(md_wq
);
9782 subsys_initcall(md_init
);
9783 module_exit(md_exit
)
9785 static int get_ro(char *buffer
, const struct kernel_param
*kp
)
9787 return sprintf(buffer
, "%d", start_readonly
);
9789 static int set_ro(const char *val
, const struct kernel_param
*kp
)
9791 return kstrtouint(val
, 10, (unsigned int *)&start_readonly
);
9794 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
9795 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
9796 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
9797 module_param(create_on_open
, bool, S_IRUSR
|S_IWUSR
);
9799 MODULE_LICENSE("GPL");
9800 MODULE_DESCRIPTION("MD RAID framework");
9802 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);