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/raid/detect.h>
62 #include <linux/slab.h>
63 #include <linux/percpu-refcount.h>
64 #include <linux/part_stat.h>
66 #include <trace/events/block.h>
68 #include "md-bitmap.h"
69 #include "md-cluster.h"
71 /* pers_list is a list of registered personalities protected
73 * pers_lock does extra service to protect accesses to
74 * mddev->thread when the mutex cannot be held.
76 static LIST_HEAD(pers_list
);
77 static DEFINE_SPINLOCK(pers_lock
);
79 static struct kobj_type md_ktype
;
81 struct md_cluster_operations
*md_cluster_ops
;
82 EXPORT_SYMBOL(md_cluster_ops
);
83 static struct module
*md_cluster_mod
;
85 static DECLARE_WAIT_QUEUE_HEAD(resync_wait
);
86 static struct workqueue_struct
*md_wq
;
87 static struct workqueue_struct
*md_misc_wq
;
88 static struct workqueue_struct
*md_rdev_misc_wq
;
90 static int remove_and_add_spares(struct mddev
*mddev
,
91 struct md_rdev
*this);
92 static void mddev_detach(struct mddev
*mddev
);
95 * Default number of read corrections we'll attempt on an rdev
96 * before ejecting it from the array. We divide the read error
97 * count by 2 for every hour elapsed between read errors.
99 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
100 /* Default safemode delay: 200 msec */
101 #define DEFAULT_SAFEMODE_DELAY ((200 * HZ)/1000 +1)
103 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
104 * is 1000 KB/sec, so the extra system load does not show up that much.
105 * Increase it if you want to have more _guaranteed_ speed. Note that
106 * the RAID driver will use the maximum available bandwidth if the IO
107 * subsystem is idle. There is also an 'absolute maximum' reconstruction
108 * speed limit - in case reconstruction slows down your system despite
111 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
112 * or /sys/block/mdX/md/sync_speed_{min,max}
115 static int sysctl_speed_limit_min
= 1000;
116 static int sysctl_speed_limit_max
= 200000;
117 static inline int speed_min(struct mddev
*mddev
)
119 return mddev
->sync_speed_min
?
120 mddev
->sync_speed_min
: sysctl_speed_limit_min
;
123 static inline int speed_max(struct mddev
*mddev
)
125 return mddev
->sync_speed_max
?
126 mddev
->sync_speed_max
: sysctl_speed_limit_max
;
129 static void rdev_uninit_serial(struct md_rdev
*rdev
)
131 if (!test_and_clear_bit(CollisionCheck
, &rdev
->flags
))
134 kvfree(rdev
->serial
);
138 static void rdevs_uninit_serial(struct mddev
*mddev
)
140 struct md_rdev
*rdev
;
142 rdev_for_each(rdev
, mddev
)
143 rdev_uninit_serial(rdev
);
146 static int rdev_init_serial(struct md_rdev
*rdev
)
148 /* serial_nums equals with BARRIER_BUCKETS_NR */
149 int i
, serial_nums
= 1 << ((PAGE_SHIFT
- ilog2(sizeof(atomic_t
))));
150 struct serial_in_rdev
*serial
= NULL
;
152 if (test_bit(CollisionCheck
, &rdev
->flags
))
155 serial
= kvmalloc(sizeof(struct serial_in_rdev
) * serial_nums
,
160 for (i
= 0; i
< serial_nums
; i
++) {
161 struct serial_in_rdev
*serial_tmp
= &serial
[i
];
163 spin_lock_init(&serial_tmp
->serial_lock
);
164 serial_tmp
->serial_rb
= RB_ROOT_CACHED
;
165 init_waitqueue_head(&serial_tmp
->serial_io_wait
);
168 rdev
->serial
= serial
;
169 set_bit(CollisionCheck
, &rdev
->flags
);
174 static int rdevs_init_serial(struct mddev
*mddev
)
176 struct md_rdev
*rdev
;
179 rdev_for_each(rdev
, mddev
) {
180 ret
= rdev_init_serial(rdev
);
185 /* Free all resources if pool is not existed */
186 if (ret
&& !mddev
->serial_info_pool
)
187 rdevs_uninit_serial(mddev
);
193 * rdev needs to enable serial stuffs if it meets the conditions:
194 * 1. it is multi-queue device flaged with writemostly.
195 * 2. the write-behind mode is enabled.
197 static int rdev_need_serial(struct md_rdev
*rdev
)
199 return (rdev
&& rdev
->mddev
->bitmap_info
.max_write_behind
> 0 &&
200 rdev
->bdev
->bd_disk
->queue
->nr_hw_queues
!= 1 &&
201 test_bit(WriteMostly
, &rdev
->flags
));
205 * Init resource for rdev(s), then create serial_info_pool if:
206 * 1. rdev is the first device which return true from rdev_enable_serial.
207 * 2. rdev is NULL, means we want to enable serialization for all rdevs.
209 void mddev_create_serial_pool(struct mddev
*mddev
, struct md_rdev
*rdev
,
214 if (rdev
&& !rdev_need_serial(rdev
) &&
215 !test_bit(CollisionCheck
, &rdev
->flags
))
219 mddev_suspend(mddev
);
222 ret
= rdevs_init_serial(mddev
);
224 ret
= rdev_init_serial(rdev
);
228 if (mddev
->serial_info_pool
== NULL
) {
230 * already in memalloc noio context by
233 mddev
->serial_info_pool
=
234 mempool_create_kmalloc_pool(NR_SERIAL_INFOS
,
235 sizeof(struct serial_info
));
236 if (!mddev
->serial_info_pool
) {
237 rdevs_uninit_serial(mddev
);
238 pr_err("can't alloc memory pool for serialization\n");
248 * Free resource from rdev(s), and destroy serial_info_pool under conditions:
249 * 1. rdev is the last device flaged with CollisionCheck.
250 * 2. when bitmap is destroyed while policy is not enabled.
251 * 3. for disable policy, the pool is destroyed only when no rdev needs it.
253 void mddev_destroy_serial_pool(struct mddev
*mddev
, struct md_rdev
*rdev
,
256 if (rdev
&& !test_bit(CollisionCheck
, &rdev
->flags
))
259 if (mddev
->serial_info_pool
) {
260 struct md_rdev
*temp
;
261 int num
= 0; /* used to track if other rdevs need the pool */
264 mddev_suspend(mddev
);
265 rdev_for_each(temp
, mddev
) {
267 if (!mddev
->serialize_policy
||
268 !rdev_need_serial(temp
))
269 rdev_uninit_serial(temp
);
272 } else if (temp
!= rdev
&&
273 test_bit(CollisionCheck
, &temp
->flags
))
278 rdev_uninit_serial(rdev
);
281 pr_info("The mempool could be used by other devices\n");
283 mempool_destroy(mddev
->serial_info_pool
);
284 mddev
->serial_info_pool
= NULL
;
291 static struct ctl_table_header
*raid_table_header
;
293 static struct ctl_table raid_table
[] = {
295 .procname
= "speed_limit_min",
296 .data
= &sysctl_speed_limit_min
,
297 .maxlen
= sizeof(int),
298 .mode
= S_IRUGO
|S_IWUSR
,
299 .proc_handler
= proc_dointvec
,
302 .procname
= "speed_limit_max",
303 .data
= &sysctl_speed_limit_max
,
304 .maxlen
= sizeof(int),
305 .mode
= S_IRUGO
|S_IWUSR
,
306 .proc_handler
= proc_dointvec
,
311 static struct ctl_table raid_dir_table
[] = {
315 .mode
= S_IRUGO
|S_IXUGO
,
321 static struct ctl_table raid_root_table
[] = {
326 .child
= raid_dir_table
,
331 static int start_readonly
;
334 * The original mechanism for creating an md device is to create
335 * a device node in /dev and to open it. This causes races with device-close.
336 * The preferred method is to write to the "new_array" module parameter.
337 * This can avoid races.
338 * Setting create_on_open to false disables the original mechanism
339 * so all the races disappear.
341 static bool create_on_open
= true;
343 struct bio
*bio_alloc_mddev(gfp_t gfp_mask
, int nr_iovecs
,
346 if (!mddev
|| !bioset_initialized(&mddev
->bio_set
))
347 return bio_alloc(gfp_mask
, nr_iovecs
);
349 return bio_alloc_bioset(gfp_mask
, nr_iovecs
, &mddev
->bio_set
);
351 EXPORT_SYMBOL_GPL(bio_alloc_mddev
);
353 static struct bio
*md_bio_alloc_sync(struct mddev
*mddev
)
355 if (!mddev
|| !bioset_initialized(&mddev
->sync_set
))
356 return bio_alloc(GFP_NOIO
, 1);
358 return bio_alloc_bioset(GFP_NOIO
, 1, &mddev
->sync_set
);
362 * We have a system wide 'event count' that is incremented
363 * on any 'interesting' event, and readers of /proc/mdstat
364 * can use 'poll' or 'select' to find out when the event
368 * start array, stop array, error, add device, remove device,
369 * start build, activate spare
371 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters
);
372 static atomic_t md_event_count
;
373 void md_new_event(struct mddev
*mddev
)
375 atomic_inc(&md_event_count
);
376 wake_up(&md_event_waiters
);
378 EXPORT_SYMBOL_GPL(md_new_event
);
381 * Enables to iterate over all existing md arrays
382 * all_mddevs_lock protects this list.
384 static LIST_HEAD(all_mddevs
);
385 static DEFINE_SPINLOCK(all_mddevs_lock
);
388 * iterates through all used mddevs in the system.
389 * We take care to grab the all_mddevs_lock whenever navigating
390 * the list, and to always hold a refcount when unlocked.
391 * Any code which breaks out of this loop while own
392 * a reference to the current mddev and must mddev_put it.
394 #define for_each_mddev(_mddev,_tmp) \
396 for (({ spin_lock(&all_mddevs_lock); \
397 _tmp = all_mddevs.next; \
399 ({ if (_tmp != &all_mddevs) \
400 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
401 spin_unlock(&all_mddevs_lock); \
402 if (_mddev) mddev_put(_mddev); \
403 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
404 _tmp != &all_mddevs;}); \
405 ({ spin_lock(&all_mddevs_lock); \
406 _tmp = _tmp->next;}) \
409 /* Rather than calling directly into the personality make_request function,
410 * IO requests come here first so that we can check if the device is
411 * being suspended pending a reconfiguration.
412 * We hold a refcount over the call to ->make_request. By the time that
413 * call has finished, the bio has been linked into some internal structure
414 * and so is visible to ->quiesce(), so we don't need the refcount any more.
416 static bool is_suspended(struct mddev
*mddev
, struct bio
*bio
)
418 if (mddev
->suspended
)
420 if (bio_data_dir(bio
) != WRITE
)
422 if (mddev
->suspend_lo
>= mddev
->suspend_hi
)
424 if (bio
->bi_iter
.bi_sector
>= mddev
->suspend_hi
)
426 if (bio_end_sector(bio
) < mddev
->suspend_lo
)
431 void md_handle_request(struct mddev
*mddev
, struct bio
*bio
)
435 if (is_suspended(mddev
, bio
)) {
438 prepare_to_wait(&mddev
->sb_wait
, &__wait
,
439 TASK_UNINTERRUPTIBLE
);
440 if (!is_suspended(mddev
, bio
))
446 finish_wait(&mddev
->sb_wait
, &__wait
);
448 atomic_inc(&mddev
->active_io
);
451 if (!mddev
->pers
->make_request(mddev
, bio
)) {
452 atomic_dec(&mddev
->active_io
);
453 wake_up(&mddev
->sb_wait
);
454 goto check_suspended
;
457 if (atomic_dec_and_test(&mddev
->active_io
) && mddev
->suspended
)
458 wake_up(&mddev
->sb_wait
);
460 EXPORT_SYMBOL(md_handle_request
);
464 bio_end_io_t
*orig_bi_end_io
;
465 void *orig_bi_private
;
466 unsigned long start_time
;
467 struct block_device
*part
;
470 static void md_end_io(struct bio
*bio
)
472 struct md_io
*md_io
= bio
->bi_private
;
473 struct mddev
*mddev
= md_io
->mddev
;
475 part_end_io_acct(md_io
->part
, bio
, md_io
->start_time
);
477 bio
->bi_end_io
= md_io
->orig_bi_end_io
;
478 bio
->bi_private
= md_io
->orig_bi_private
;
480 mempool_free(md_io
, &mddev
->md_io_pool
);
486 static blk_qc_t
md_submit_bio(struct bio
*bio
)
488 const int rw
= bio_data_dir(bio
);
489 struct mddev
*mddev
= bio
->bi_disk
->private_data
;
491 if (mddev
== NULL
|| mddev
->pers
== NULL
) {
493 return BLK_QC_T_NONE
;
496 if (unlikely(test_bit(MD_BROKEN
, &mddev
->flags
)) && (rw
== WRITE
)) {
498 return BLK_QC_T_NONE
;
501 blk_queue_split(&bio
);
503 if (mddev
->ro
== 1 && unlikely(rw
== WRITE
)) {
504 if (bio_sectors(bio
) != 0)
505 bio
->bi_status
= BLK_STS_IOERR
;
507 return BLK_QC_T_NONE
;
510 if (bio
->bi_end_io
!= md_end_io
) {
513 md_io
= mempool_alloc(&mddev
->md_io_pool
, GFP_NOIO
);
514 md_io
->mddev
= mddev
;
515 md_io
->orig_bi_end_io
= bio
->bi_end_io
;
516 md_io
->orig_bi_private
= bio
->bi_private
;
518 bio
->bi_end_io
= md_end_io
;
519 bio
->bi_private
= md_io
;
521 md_io
->start_time
= part_start_io_acct(mddev
->gendisk
,
525 /* bio could be mergeable after passing to underlayer */
526 bio
->bi_opf
&= ~REQ_NOMERGE
;
528 md_handle_request(mddev
, bio
);
530 return BLK_QC_T_NONE
;
533 /* mddev_suspend makes sure no new requests are submitted
534 * to the device, and that any requests that have been submitted
535 * are completely handled.
536 * Once mddev_detach() is called and completes, the module will be
539 void mddev_suspend(struct mddev
*mddev
)
541 WARN_ON_ONCE(mddev
->thread
&& current
== mddev
->thread
->tsk
);
542 lockdep_assert_held(&mddev
->reconfig_mutex
);
543 if (mddev
->suspended
++)
546 wake_up(&mddev
->sb_wait
);
547 set_bit(MD_ALLOW_SB_UPDATE
, &mddev
->flags
);
548 smp_mb__after_atomic();
549 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->active_io
) == 0);
550 mddev
->pers
->quiesce(mddev
, 1);
551 clear_bit_unlock(MD_ALLOW_SB_UPDATE
, &mddev
->flags
);
552 wait_event(mddev
->sb_wait
, !test_bit(MD_UPDATING_SB
, &mddev
->flags
));
554 del_timer_sync(&mddev
->safemode_timer
);
555 /* restrict memory reclaim I/O during raid array is suspend */
556 mddev
->noio_flag
= memalloc_noio_save();
558 EXPORT_SYMBOL_GPL(mddev_suspend
);
560 void mddev_resume(struct mddev
*mddev
)
562 /* entred the memalloc scope from mddev_suspend() */
563 memalloc_noio_restore(mddev
->noio_flag
);
564 lockdep_assert_held(&mddev
->reconfig_mutex
);
565 if (--mddev
->suspended
)
567 wake_up(&mddev
->sb_wait
);
568 mddev
->pers
->quiesce(mddev
, 0);
570 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
571 md_wakeup_thread(mddev
->thread
);
572 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
574 EXPORT_SYMBOL_GPL(mddev_resume
);
577 * Generic flush handling for md
580 static void md_end_flush(struct bio
*bio
)
582 struct md_rdev
*rdev
= bio
->bi_private
;
583 struct mddev
*mddev
= rdev
->mddev
;
585 rdev_dec_pending(rdev
, mddev
);
587 if (atomic_dec_and_test(&mddev
->flush_pending
)) {
588 /* The pre-request flush has finished */
589 queue_work(md_wq
, &mddev
->flush_work
);
594 static void md_submit_flush_data(struct work_struct
*ws
);
596 static void submit_flushes(struct work_struct
*ws
)
598 struct mddev
*mddev
= container_of(ws
, struct mddev
, flush_work
);
599 struct md_rdev
*rdev
;
601 mddev
->start_flush
= ktime_get_boottime();
602 INIT_WORK(&mddev
->flush_work
, md_submit_flush_data
);
603 atomic_set(&mddev
->flush_pending
, 1);
605 rdev_for_each_rcu(rdev
, mddev
)
606 if (rdev
->raid_disk
>= 0 &&
607 !test_bit(Faulty
, &rdev
->flags
)) {
608 /* Take two references, one is dropped
609 * when request finishes, one after
610 * we reclaim rcu_read_lock
613 atomic_inc(&rdev
->nr_pending
);
614 atomic_inc(&rdev
->nr_pending
);
616 bi
= bio_alloc_mddev(GFP_NOIO
, 0, mddev
);
617 bi
->bi_end_io
= md_end_flush
;
618 bi
->bi_private
= rdev
;
619 bio_set_dev(bi
, rdev
->bdev
);
620 bi
->bi_opf
= REQ_OP_WRITE
| REQ_PREFLUSH
;
621 atomic_inc(&mddev
->flush_pending
);
624 rdev_dec_pending(rdev
, mddev
);
627 if (atomic_dec_and_test(&mddev
->flush_pending
))
628 queue_work(md_wq
, &mddev
->flush_work
);
631 static void md_submit_flush_data(struct work_struct
*ws
)
633 struct mddev
*mddev
= container_of(ws
, struct mddev
, flush_work
);
634 struct bio
*bio
= mddev
->flush_bio
;
637 * must reset flush_bio before calling into md_handle_request to avoid a
638 * deadlock, because other bios passed md_handle_request suspend check
639 * could wait for this and below md_handle_request could wait for those
640 * bios because of suspend check
642 mddev
->prev_flush_start
= mddev
->start_flush
;
643 mddev
->flush_bio
= NULL
;
644 wake_up(&mddev
->sb_wait
);
646 if (bio
->bi_iter
.bi_size
== 0) {
647 /* an empty barrier - all done */
650 bio
->bi_opf
&= ~REQ_PREFLUSH
;
651 md_handle_request(mddev
, bio
);
656 * Manages consolidation of flushes and submitting any flushes needed for
657 * a bio with REQ_PREFLUSH. Returns true if the bio is finished or is
658 * being finished in another context. Returns false if the flushing is
659 * complete but still needs the I/O portion of the bio to be processed.
661 bool md_flush_request(struct mddev
*mddev
, struct bio
*bio
)
663 ktime_t req_start
= ktime_get_boottime();
664 spin_lock_irq(&mddev
->lock
);
665 /* flush requests wait until ongoing flush completes,
666 * hence coalescing all the pending requests.
668 wait_event_lock_irq(mddev
->sb_wait
,
670 ktime_before(req_start
, mddev
->prev_flush_start
),
672 /* new request after previous flush is completed */
673 if (ktime_after(req_start
, mddev
->prev_flush_start
)) {
674 WARN_ON(mddev
->flush_bio
);
675 mddev
->flush_bio
= bio
;
678 spin_unlock_irq(&mddev
->lock
);
681 INIT_WORK(&mddev
->flush_work
, submit_flushes
);
682 queue_work(md_wq
, &mddev
->flush_work
);
684 /* flush was performed for some other bio while we waited. */
685 if (bio
->bi_iter
.bi_size
== 0)
686 /* an empty barrier - all done */
689 bio
->bi_opf
&= ~REQ_PREFLUSH
;
695 EXPORT_SYMBOL(md_flush_request
);
697 static inline struct mddev
*mddev_get(struct mddev
*mddev
)
699 atomic_inc(&mddev
->active
);
703 static void mddev_delayed_delete(struct work_struct
*ws
);
705 static void mddev_put(struct mddev
*mddev
)
707 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
709 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
) &&
710 mddev
->ctime
== 0 && !mddev
->hold_active
) {
711 /* Array is not configured at all, and not held active,
713 list_del_init(&mddev
->all_mddevs
);
716 * Call queue_work inside the spinlock so that
717 * flush_workqueue() after mddev_find will succeed in waiting
718 * for the work to be done.
720 INIT_WORK(&mddev
->del_work
, mddev_delayed_delete
);
721 queue_work(md_misc_wq
, &mddev
->del_work
);
723 spin_unlock(&all_mddevs_lock
);
726 static void md_safemode_timeout(struct timer_list
*t
);
728 void mddev_init(struct mddev
*mddev
)
730 kobject_init(&mddev
->kobj
, &md_ktype
);
731 mutex_init(&mddev
->open_mutex
);
732 mutex_init(&mddev
->reconfig_mutex
);
733 mutex_init(&mddev
->bitmap_info
.mutex
);
734 INIT_LIST_HEAD(&mddev
->disks
);
735 INIT_LIST_HEAD(&mddev
->all_mddevs
);
736 timer_setup(&mddev
->safemode_timer
, md_safemode_timeout
, 0);
737 atomic_set(&mddev
->active
, 1);
738 atomic_set(&mddev
->openers
, 0);
739 atomic_set(&mddev
->active_io
, 0);
740 spin_lock_init(&mddev
->lock
);
741 atomic_set(&mddev
->flush_pending
, 0);
742 init_waitqueue_head(&mddev
->sb_wait
);
743 init_waitqueue_head(&mddev
->recovery_wait
);
744 mddev
->reshape_position
= MaxSector
;
745 mddev
->reshape_backwards
= 0;
746 mddev
->last_sync_action
= "none";
747 mddev
->resync_min
= 0;
748 mddev
->resync_max
= MaxSector
;
749 mddev
->level
= LEVEL_NONE
;
751 EXPORT_SYMBOL_GPL(mddev_init
);
753 static struct mddev
*mddev_find(dev_t unit
)
755 struct mddev
*mddev
, *new = NULL
;
757 if (unit
&& MAJOR(unit
) != MD_MAJOR
)
758 unit
&= ~((1<<MdpMinorShift
)-1);
761 spin_lock(&all_mddevs_lock
);
764 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
765 if (mddev
->unit
== unit
) {
767 spin_unlock(&all_mddevs_lock
);
773 list_add(&new->all_mddevs
, &all_mddevs
);
774 spin_unlock(&all_mddevs_lock
);
775 new->hold_active
= UNTIL_IOCTL
;
779 /* find an unused unit number */
780 static int next_minor
= 512;
781 int start
= next_minor
;
785 dev
= MKDEV(MD_MAJOR
, next_minor
);
787 if (next_minor
> MINORMASK
)
789 if (next_minor
== start
) {
790 /* Oh dear, all in use. */
791 spin_unlock(&all_mddevs_lock
);
797 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
798 if (mddev
->unit
== dev
) {
804 new->md_minor
= MINOR(dev
);
805 new->hold_active
= UNTIL_STOP
;
806 list_add(&new->all_mddevs
, &all_mddevs
);
807 spin_unlock(&all_mddevs_lock
);
810 spin_unlock(&all_mddevs_lock
);
812 new = kzalloc(sizeof(*new), GFP_KERNEL
);
817 if (MAJOR(unit
) == MD_MAJOR
)
818 new->md_minor
= MINOR(unit
);
820 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
827 static struct attribute_group md_redundancy_group
;
829 void mddev_unlock(struct mddev
*mddev
)
831 if (mddev
->to_remove
) {
832 /* These cannot be removed under reconfig_mutex as
833 * an access to the files will try to take reconfig_mutex
834 * while holding the file unremovable, which leads to
836 * So hold set sysfs_active while the remove in happeing,
837 * and anything else which might set ->to_remove or my
838 * otherwise change the sysfs namespace will fail with
839 * -EBUSY if sysfs_active is still set.
840 * We set sysfs_active under reconfig_mutex and elsewhere
841 * test it under the same mutex to ensure its correct value
844 struct attribute_group
*to_remove
= mddev
->to_remove
;
845 mddev
->to_remove
= NULL
;
846 mddev
->sysfs_active
= 1;
847 mutex_unlock(&mddev
->reconfig_mutex
);
849 if (mddev
->kobj
.sd
) {
850 if (to_remove
!= &md_redundancy_group
)
851 sysfs_remove_group(&mddev
->kobj
, to_remove
);
852 if (mddev
->pers
== NULL
||
853 mddev
->pers
->sync_request
== NULL
) {
854 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
855 if (mddev
->sysfs_action
)
856 sysfs_put(mddev
->sysfs_action
);
857 if (mddev
->sysfs_completed
)
858 sysfs_put(mddev
->sysfs_completed
);
859 if (mddev
->sysfs_degraded
)
860 sysfs_put(mddev
->sysfs_degraded
);
861 mddev
->sysfs_action
= NULL
;
862 mddev
->sysfs_completed
= NULL
;
863 mddev
->sysfs_degraded
= NULL
;
866 mddev
->sysfs_active
= 0;
868 mutex_unlock(&mddev
->reconfig_mutex
);
870 /* As we've dropped the mutex we need a spinlock to
871 * make sure the thread doesn't disappear
873 spin_lock(&pers_lock
);
874 md_wakeup_thread(mddev
->thread
);
875 wake_up(&mddev
->sb_wait
);
876 spin_unlock(&pers_lock
);
878 EXPORT_SYMBOL_GPL(mddev_unlock
);
880 struct md_rdev
*md_find_rdev_nr_rcu(struct mddev
*mddev
, int nr
)
882 struct md_rdev
*rdev
;
884 rdev_for_each_rcu(rdev
, mddev
)
885 if (rdev
->desc_nr
== nr
)
890 EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu
);
892 static struct md_rdev
*find_rdev(struct mddev
*mddev
, dev_t dev
)
894 struct md_rdev
*rdev
;
896 rdev_for_each(rdev
, mddev
)
897 if (rdev
->bdev
->bd_dev
== dev
)
903 struct md_rdev
*md_find_rdev_rcu(struct mddev
*mddev
, dev_t dev
)
905 struct md_rdev
*rdev
;
907 rdev_for_each_rcu(rdev
, mddev
)
908 if (rdev
->bdev
->bd_dev
== dev
)
913 EXPORT_SYMBOL_GPL(md_find_rdev_rcu
);
915 static struct md_personality
*find_pers(int level
, char *clevel
)
917 struct md_personality
*pers
;
918 list_for_each_entry(pers
, &pers_list
, list
) {
919 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
921 if (strcmp(pers
->name
, clevel
)==0)
927 /* return the offset of the super block in 512byte sectors */
928 static inline sector_t
calc_dev_sboffset(struct md_rdev
*rdev
)
930 sector_t num_sectors
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
931 return MD_NEW_SIZE_SECTORS(num_sectors
);
934 static int alloc_disk_sb(struct md_rdev
*rdev
)
936 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
942 void md_rdev_clear(struct md_rdev
*rdev
)
945 put_page(rdev
->sb_page
);
947 rdev
->sb_page
= NULL
;
952 put_page(rdev
->bb_page
);
953 rdev
->bb_page
= NULL
;
955 badblocks_exit(&rdev
->badblocks
);
957 EXPORT_SYMBOL_GPL(md_rdev_clear
);
959 static void super_written(struct bio
*bio
)
961 struct md_rdev
*rdev
= bio
->bi_private
;
962 struct mddev
*mddev
= rdev
->mddev
;
964 if (bio
->bi_status
) {
965 pr_err("md: %s gets error=%d\n", __func__
,
966 blk_status_to_errno(bio
->bi_status
));
967 md_error(mddev
, rdev
);
968 if (!test_bit(Faulty
, &rdev
->flags
)
969 && (bio
->bi_opf
& MD_FAILFAST
)) {
970 set_bit(MD_SB_NEED_REWRITE
, &mddev
->sb_flags
);
971 set_bit(LastDev
, &rdev
->flags
);
974 clear_bit(LastDev
, &rdev
->flags
);
976 if (atomic_dec_and_test(&mddev
->pending_writes
))
977 wake_up(&mddev
->sb_wait
);
978 rdev_dec_pending(rdev
, mddev
);
982 void md_super_write(struct mddev
*mddev
, struct md_rdev
*rdev
,
983 sector_t sector
, int size
, struct page
*page
)
985 /* write first size bytes of page to sector of rdev
986 * Increment mddev->pending_writes before returning
987 * and decrement it on completion, waking up sb_wait
988 * if zero is reached.
989 * If an error occurred, call md_error
997 if (test_bit(Faulty
, &rdev
->flags
))
1000 bio
= md_bio_alloc_sync(mddev
);
1002 atomic_inc(&rdev
->nr_pending
);
1004 bio_set_dev(bio
, rdev
->meta_bdev
? rdev
->meta_bdev
: rdev
->bdev
);
1005 bio
->bi_iter
.bi_sector
= sector
;
1006 bio_add_page(bio
, page
, size
, 0);
1007 bio
->bi_private
= rdev
;
1008 bio
->bi_end_io
= super_written
;
1010 if (test_bit(MD_FAILFAST_SUPPORTED
, &mddev
->flags
) &&
1011 test_bit(FailFast
, &rdev
->flags
) &&
1012 !test_bit(LastDev
, &rdev
->flags
))
1014 bio
->bi_opf
= REQ_OP_WRITE
| REQ_SYNC
| REQ_PREFLUSH
| REQ_FUA
| ff
;
1016 atomic_inc(&mddev
->pending_writes
);
1020 int md_super_wait(struct mddev
*mddev
)
1022 /* wait for all superblock writes that were scheduled to complete */
1023 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->pending_writes
)==0);
1024 if (test_and_clear_bit(MD_SB_NEED_REWRITE
, &mddev
->sb_flags
))
1029 int sync_page_io(struct md_rdev
*rdev
, sector_t sector
, int size
,
1030 struct page
*page
, int op
, int op_flags
, bool metadata_op
)
1032 struct bio
*bio
= md_bio_alloc_sync(rdev
->mddev
);
1035 if (metadata_op
&& rdev
->meta_bdev
)
1036 bio_set_dev(bio
, rdev
->meta_bdev
);
1038 bio_set_dev(bio
, rdev
->bdev
);
1039 bio_set_op_attrs(bio
, op
, op_flags
);
1041 bio
->bi_iter
.bi_sector
= sector
+ rdev
->sb_start
;
1042 else if (rdev
->mddev
->reshape_position
!= MaxSector
&&
1043 (rdev
->mddev
->reshape_backwards
==
1044 (sector
>= rdev
->mddev
->reshape_position
)))
1045 bio
->bi_iter
.bi_sector
= sector
+ rdev
->new_data_offset
;
1047 bio
->bi_iter
.bi_sector
= sector
+ rdev
->data_offset
;
1048 bio_add_page(bio
, page
, size
, 0);
1050 submit_bio_wait(bio
);
1052 ret
= !bio
->bi_status
;
1056 EXPORT_SYMBOL_GPL(sync_page_io
);
1058 static int read_disk_sb(struct md_rdev
*rdev
, int size
)
1060 char b
[BDEVNAME_SIZE
];
1062 if (rdev
->sb_loaded
)
1065 if (!sync_page_io(rdev
, 0, size
, rdev
->sb_page
, REQ_OP_READ
, 0, true))
1067 rdev
->sb_loaded
= 1;
1071 pr_err("md: disabled device %s, could not read superblock.\n",
1072 bdevname(rdev
->bdev
,b
));
1076 static int md_uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
1078 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
1079 sb1
->set_uuid1
== sb2
->set_uuid1
&&
1080 sb1
->set_uuid2
== sb2
->set_uuid2
&&
1081 sb1
->set_uuid3
== sb2
->set_uuid3
;
1084 static int md_sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
1087 mdp_super_t
*tmp1
, *tmp2
;
1089 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
1090 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
1092 if (!tmp1
|| !tmp2
) {
1101 * nr_disks is not constant
1106 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
1113 static u32
md_csum_fold(u32 csum
)
1115 csum
= (csum
& 0xffff) + (csum
>> 16);
1116 return (csum
& 0xffff) + (csum
>> 16);
1119 static unsigned int calc_sb_csum(mdp_super_t
*sb
)
1122 u32
*sb32
= (u32
*)sb
;
1124 unsigned int disk_csum
, csum
;
1126 disk_csum
= sb
->sb_csum
;
1129 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
1131 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
1134 /* This used to use csum_partial, which was wrong for several
1135 * reasons including that different results are returned on
1136 * different architectures. It isn't critical that we get exactly
1137 * the same return value as before (we always csum_fold before
1138 * testing, and that removes any differences). However as we
1139 * know that csum_partial always returned a 16bit value on
1140 * alphas, do a fold to maximise conformity to previous behaviour.
1142 sb
->sb_csum
= md_csum_fold(disk_csum
);
1144 sb
->sb_csum
= disk_csum
;
1150 * Handle superblock details.
1151 * We want to be able to handle multiple superblock formats
1152 * so we have a common interface to them all, and an array of
1153 * different handlers.
1154 * We rely on user-space to write the initial superblock, and support
1155 * reading and updating of superblocks.
1156 * Interface methods are:
1157 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
1158 * loads and validates a superblock on dev.
1159 * if refdev != NULL, compare superblocks on both devices
1161 * 0 - dev has a superblock that is compatible with refdev
1162 * 1 - dev has a superblock that is compatible and newer than refdev
1163 * so dev should be used as the refdev in future
1164 * -EINVAL superblock incompatible or invalid
1165 * -othererror e.g. -EIO
1167 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
1168 * Verify that dev is acceptable into mddev.
1169 * The first time, mddev->raid_disks will be 0, and data from
1170 * dev should be merged in. Subsequent calls check that dev
1171 * is new enough. Return 0 or -EINVAL
1173 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
1174 * Update the superblock for rdev with data in mddev
1175 * This does not write to disc.
1181 struct module
*owner
;
1182 int (*load_super
)(struct md_rdev
*rdev
,
1183 struct md_rdev
*refdev
,
1185 int (*validate_super
)(struct mddev
*mddev
,
1186 struct md_rdev
*rdev
);
1187 void (*sync_super
)(struct mddev
*mddev
,
1188 struct md_rdev
*rdev
);
1189 unsigned long long (*rdev_size_change
)(struct md_rdev
*rdev
,
1190 sector_t num_sectors
);
1191 int (*allow_new_offset
)(struct md_rdev
*rdev
,
1192 unsigned long long new_offset
);
1196 * Check that the given mddev has no bitmap.
1198 * This function is called from the run method of all personalities that do not
1199 * support bitmaps. It prints an error message and returns non-zero if mddev
1200 * has a bitmap. Otherwise, it returns 0.
1203 int md_check_no_bitmap(struct mddev
*mddev
)
1205 if (!mddev
->bitmap_info
.file
&& !mddev
->bitmap_info
.offset
)
1207 pr_warn("%s: bitmaps are not supported for %s\n",
1208 mdname(mddev
), mddev
->pers
->name
);
1211 EXPORT_SYMBOL(md_check_no_bitmap
);
1214 * load_super for 0.90.0
1216 static int super_90_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
1218 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1221 bool spare_disk
= true;
1224 * Calculate the position of the superblock (512byte sectors),
1225 * it's at the end of the disk.
1227 * It also happens to be a multiple of 4Kb.
1229 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1231 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
1237 bdevname(rdev
->bdev
, b
);
1238 sb
= page_address(rdev
->sb_page
);
1240 if (sb
->md_magic
!= MD_SB_MAGIC
) {
1241 pr_warn("md: invalid raid superblock magic on %s\n", b
);
1245 if (sb
->major_version
!= 0 ||
1246 sb
->minor_version
< 90 ||
1247 sb
->minor_version
> 91) {
1248 pr_warn("Bad version number %d.%d on %s\n",
1249 sb
->major_version
, sb
->minor_version
, b
);
1253 if (sb
->raid_disks
<= 0)
1256 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
1257 pr_warn("md: invalid superblock checksum on %s\n", b
);
1261 rdev
->preferred_minor
= sb
->md_minor
;
1262 rdev
->data_offset
= 0;
1263 rdev
->new_data_offset
= 0;
1264 rdev
->sb_size
= MD_SB_BYTES
;
1265 rdev
->badblocks
.shift
= -1;
1267 if (sb
->level
== LEVEL_MULTIPATH
)
1270 rdev
->desc_nr
= sb
->this_disk
.number
;
1272 /* not spare disk, or LEVEL_MULTIPATH */
1273 if (sb
->level
== LEVEL_MULTIPATH
||
1274 (rdev
->desc_nr
>= 0 &&
1275 rdev
->desc_nr
< MD_SB_DISKS
&&
1276 sb
->disks
[rdev
->desc_nr
].state
&
1277 ((1<<MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
))))
1287 mdp_super_t
*refsb
= page_address(refdev
->sb_page
);
1288 if (!md_uuid_equal(refsb
, sb
)) {
1289 pr_warn("md: %s has different UUID to %s\n",
1290 b
, bdevname(refdev
->bdev
,b2
));
1293 if (!md_sb_equal(refsb
, sb
)) {
1294 pr_warn("md: %s has same UUID but different superblock to %s\n",
1295 b
, bdevname(refdev
->bdev
, b2
));
1299 ev2
= md_event(refsb
);
1301 if (!spare_disk
&& ev1
> ev2
)
1306 rdev
->sectors
= rdev
->sb_start
;
1307 /* Limit to 4TB as metadata cannot record more than that.
1308 * (not needed for Linear and RAID0 as metadata doesn't
1311 if ((u64
)rdev
->sectors
>= (2ULL << 32) && sb
->level
>= 1)
1312 rdev
->sectors
= (sector_t
)(2ULL << 32) - 2;
1314 if (rdev
->sectors
< ((sector_t
)sb
->size
) * 2 && sb
->level
>= 1)
1315 /* "this cannot possibly happen" ... */
1323 * validate_super for 0.90.0
1325 static int super_90_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
1328 mdp_super_t
*sb
= page_address(rdev
->sb_page
);
1329 __u64 ev1
= md_event(sb
);
1331 rdev
->raid_disk
= -1;
1332 clear_bit(Faulty
, &rdev
->flags
);
1333 clear_bit(In_sync
, &rdev
->flags
);
1334 clear_bit(Bitmap_sync
, &rdev
->flags
);
1335 clear_bit(WriteMostly
, &rdev
->flags
);
1337 if (mddev
->raid_disks
== 0) {
1338 mddev
->major_version
= 0;
1339 mddev
->minor_version
= sb
->minor_version
;
1340 mddev
->patch_version
= sb
->patch_version
;
1341 mddev
->external
= 0;
1342 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
1343 mddev
->ctime
= sb
->ctime
;
1344 mddev
->utime
= sb
->utime
;
1345 mddev
->level
= sb
->level
;
1346 mddev
->clevel
[0] = 0;
1347 mddev
->layout
= sb
->layout
;
1348 mddev
->raid_disks
= sb
->raid_disks
;
1349 mddev
->dev_sectors
= ((sector_t
)sb
->size
) * 2;
1350 mddev
->events
= ev1
;
1351 mddev
->bitmap_info
.offset
= 0;
1352 mddev
->bitmap_info
.space
= 0;
1353 /* bitmap can use 60 K after the 4K superblocks */
1354 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
1355 mddev
->bitmap_info
.default_space
= 64*2 - (MD_SB_BYTES
>> 9);
1356 mddev
->reshape_backwards
= 0;
1358 if (mddev
->minor_version
>= 91) {
1359 mddev
->reshape_position
= sb
->reshape_position
;
1360 mddev
->delta_disks
= sb
->delta_disks
;
1361 mddev
->new_level
= sb
->new_level
;
1362 mddev
->new_layout
= sb
->new_layout
;
1363 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
1364 if (mddev
->delta_disks
< 0)
1365 mddev
->reshape_backwards
= 1;
1367 mddev
->reshape_position
= MaxSector
;
1368 mddev
->delta_disks
= 0;
1369 mddev
->new_level
= mddev
->level
;
1370 mddev
->new_layout
= mddev
->layout
;
1371 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1373 if (mddev
->level
== 0)
1376 if (sb
->state
& (1<<MD_SB_CLEAN
))
1377 mddev
->recovery_cp
= MaxSector
;
1379 if (sb
->events_hi
== sb
->cp_events_hi
&&
1380 sb
->events_lo
== sb
->cp_events_lo
) {
1381 mddev
->recovery_cp
= sb
->recovery_cp
;
1383 mddev
->recovery_cp
= 0;
1386 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
1387 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
1388 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
1389 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
1391 mddev
->max_disks
= MD_SB_DISKS
;
1393 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
1394 mddev
->bitmap_info
.file
== NULL
) {
1395 mddev
->bitmap_info
.offset
=
1396 mddev
->bitmap_info
.default_offset
;
1397 mddev
->bitmap_info
.space
=
1398 mddev
->bitmap_info
.default_space
;
1401 } else if (mddev
->pers
== NULL
) {
1402 /* Insist on good event counter while assembling, except
1403 * for spares (which don't need an event count) */
1405 if (sb
->disks
[rdev
->desc_nr
].state
& (
1406 (1<<MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
)))
1407 if (ev1
< mddev
->events
)
1409 } else if (mddev
->bitmap
) {
1410 /* if adding to array with a bitmap, then we can accept an
1411 * older device ... but not too old.
1413 if (ev1
< mddev
->bitmap
->events_cleared
)
1415 if (ev1
< mddev
->events
)
1416 set_bit(Bitmap_sync
, &rdev
->flags
);
1418 if (ev1
< mddev
->events
)
1419 /* just a hot-add of a new device, leave raid_disk at -1 */
1423 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1424 desc
= sb
->disks
+ rdev
->desc_nr
;
1426 if (desc
->state
& (1<<MD_DISK_FAULTY
))
1427 set_bit(Faulty
, &rdev
->flags
);
1428 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
1429 desc->raid_disk < mddev->raid_disks */) {
1430 set_bit(In_sync
, &rdev
->flags
);
1431 rdev
->raid_disk
= desc
->raid_disk
;
1432 rdev
->saved_raid_disk
= desc
->raid_disk
;
1433 } else if (desc
->state
& (1<<MD_DISK_ACTIVE
)) {
1434 /* active but not in sync implies recovery up to
1435 * reshape position. We don't know exactly where
1436 * that is, so set to zero for now */
1437 if (mddev
->minor_version
>= 91) {
1438 rdev
->recovery_offset
= 0;
1439 rdev
->raid_disk
= desc
->raid_disk
;
1442 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
1443 set_bit(WriteMostly
, &rdev
->flags
);
1444 if (desc
->state
& (1<<MD_DISK_FAILFAST
))
1445 set_bit(FailFast
, &rdev
->flags
);
1446 } else /* MULTIPATH are always insync */
1447 set_bit(In_sync
, &rdev
->flags
);
1452 * sync_super for 0.90.0
1454 static void super_90_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
1457 struct md_rdev
*rdev2
;
1458 int next_spare
= mddev
->raid_disks
;
1460 /* make rdev->sb match mddev data..
1463 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1464 * 3/ any empty disks < next_spare become removed
1466 * disks[0] gets initialised to REMOVED because
1467 * we cannot be sure from other fields if it has
1468 * been initialised or not.
1471 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
1473 rdev
->sb_size
= MD_SB_BYTES
;
1475 sb
= page_address(rdev
->sb_page
);
1477 memset(sb
, 0, sizeof(*sb
));
1479 sb
->md_magic
= MD_SB_MAGIC
;
1480 sb
->major_version
= mddev
->major_version
;
1481 sb
->patch_version
= mddev
->patch_version
;
1482 sb
->gvalid_words
= 0; /* ignored */
1483 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
1484 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
1485 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
1486 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
1488 sb
->ctime
= clamp_t(time64_t
, mddev
->ctime
, 0, U32_MAX
);
1489 sb
->level
= mddev
->level
;
1490 sb
->size
= mddev
->dev_sectors
/ 2;
1491 sb
->raid_disks
= mddev
->raid_disks
;
1492 sb
->md_minor
= mddev
->md_minor
;
1493 sb
->not_persistent
= 0;
1494 sb
->utime
= clamp_t(time64_t
, mddev
->utime
, 0, U32_MAX
);
1496 sb
->events_hi
= (mddev
->events
>>32);
1497 sb
->events_lo
= (u32
)mddev
->events
;
1499 if (mddev
->reshape_position
== MaxSector
)
1500 sb
->minor_version
= 90;
1502 sb
->minor_version
= 91;
1503 sb
->reshape_position
= mddev
->reshape_position
;
1504 sb
->new_level
= mddev
->new_level
;
1505 sb
->delta_disks
= mddev
->delta_disks
;
1506 sb
->new_layout
= mddev
->new_layout
;
1507 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1509 mddev
->minor_version
= sb
->minor_version
;
1512 sb
->recovery_cp
= mddev
->recovery_cp
;
1513 sb
->cp_events_hi
= (mddev
->events
>>32);
1514 sb
->cp_events_lo
= (u32
)mddev
->events
;
1515 if (mddev
->recovery_cp
== MaxSector
)
1516 sb
->state
= (1<< MD_SB_CLEAN
);
1518 sb
->recovery_cp
= 0;
1520 sb
->layout
= mddev
->layout
;
1521 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1523 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
)
1524 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1526 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1527 rdev_for_each(rdev2
, mddev
) {
1530 int is_active
= test_bit(In_sync
, &rdev2
->flags
);
1532 if (rdev2
->raid_disk
>= 0 &&
1533 sb
->minor_version
>= 91)
1534 /* we have nowhere to store the recovery_offset,
1535 * but if it is not below the reshape_position,
1536 * we can piggy-back on that.
1539 if (rdev2
->raid_disk
< 0 ||
1540 test_bit(Faulty
, &rdev2
->flags
))
1543 desc_nr
= rdev2
->raid_disk
;
1545 desc_nr
= next_spare
++;
1546 rdev2
->desc_nr
= desc_nr
;
1547 d
= &sb
->disks
[rdev2
->desc_nr
];
1549 d
->number
= rdev2
->desc_nr
;
1550 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1551 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1553 d
->raid_disk
= rdev2
->raid_disk
;
1555 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1556 if (test_bit(Faulty
, &rdev2
->flags
))
1557 d
->state
= (1<<MD_DISK_FAULTY
);
1558 else if (is_active
) {
1559 d
->state
= (1<<MD_DISK_ACTIVE
);
1560 if (test_bit(In_sync
, &rdev2
->flags
))
1561 d
->state
|= (1<<MD_DISK_SYNC
);
1569 if (test_bit(WriteMostly
, &rdev2
->flags
))
1570 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1571 if (test_bit(FailFast
, &rdev2
->flags
))
1572 d
->state
|= (1<<MD_DISK_FAILFAST
);
1574 /* now set the "removed" and "faulty" bits on any missing devices */
1575 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1576 mdp_disk_t
*d
= &sb
->disks
[i
];
1577 if (d
->state
== 0 && d
->number
== 0) {
1580 d
->state
= (1<<MD_DISK_REMOVED
);
1581 d
->state
|= (1<<MD_DISK_FAULTY
);
1585 sb
->nr_disks
= nr_disks
;
1586 sb
->active_disks
= active
;
1587 sb
->working_disks
= working
;
1588 sb
->failed_disks
= failed
;
1589 sb
->spare_disks
= spare
;
1591 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1592 sb
->sb_csum
= calc_sb_csum(sb
);
1596 * rdev_size_change for 0.90.0
1598 static unsigned long long
1599 super_90_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
1601 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1602 return 0; /* component must fit device */
1603 if (rdev
->mddev
->bitmap_info
.offset
)
1604 return 0; /* can't move bitmap */
1605 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1606 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1607 num_sectors
= rdev
->sb_start
;
1608 /* Limit to 4TB as metadata cannot record more than that.
1609 * 4TB == 2^32 KB, or 2*2^32 sectors.
1611 if ((u64
)num_sectors
>= (2ULL << 32) && rdev
->mddev
->level
>= 1)
1612 num_sectors
= (sector_t
)(2ULL << 32) - 2;
1614 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1616 } while (md_super_wait(rdev
->mddev
) < 0);
1621 super_90_allow_new_offset(struct md_rdev
*rdev
, unsigned long long new_offset
)
1623 /* non-zero offset changes not possible with v0.90 */
1624 return new_offset
== 0;
1628 * version 1 superblock
1631 static __le32
calc_sb_1_csum(struct mdp_superblock_1
*sb
)
1635 unsigned long long newcsum
;
1636 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1637 __le32
*isuper
= (__le32
*)sb
;
1639 disk_csum
= sb
->sb_csum
;
1642 for (; size
>= 4; size
-= 4)
1643 newcsum
+= le32_to_cpu(*isuper
++);
1646 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1648 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1649 sb
->sb_csum
= disk_csum
;
1650 return cpu_to_le32(csum
);
1653 static int super_1_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
1655 struct mdp_superblock_1
*sb
;
1659 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1661 bool spare_disk
= true;
1664 * Calculate the position of the superblock in 512byte sectors.
1665 * It is always aligned to a 4K boundary and
1666 * depeding on minor_version, it can be:
1667 * 0: At least 8K, but less than 12K, from end of device
1668 * 1: At start of device
1669 * 2: 4K from start of device.
1671 switch(minor_version
) {
1673 sb_start
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1675 sb_start
&= ~(sector_t
)(4*2-1);
1686 rdev
->sb_start
= sb_start
;
1688 /* superblock is rarely larger than 1K, but it can be larger,
1689 * and it is safe to read 4k, so we do that
1691 ret
= read_disk_sb(rdev
, 4096);
1692 if (ret
) return ret
;
1694 sb
= page_address(rdev
->sb_page
);
1696 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1697 sb
->major_version
!= cpu_to_le32(1) ||
1698 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1699 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1700 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1703 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1704 pr_warn("md: invalid superblock checksum on %s\n",
1705 bdevname(rdev
->bdev
,b
));
1708 if (le64_to_cpu(sb
->data_size
) < 10) {
1709 pr_warn("md: data_size too small on %s\n",
1710 bdevname(rdev
->bdev
,b
));
1715 memcmp(sb
->pad3
, sb
->pad3
+1, sizeof(sb
->pad3
) - sizeof(sb
->pad3
[1])))
1716 /* Some padding is non-zero, might be a new feature */
1719 rdev
->preferred_minor
= 0xffff;
1720 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1721 rdev
->new_data_offset
= rdev
->data_offset
;
1722 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
) &&
1723 (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_NEW_OFFSET
))
1724 rdev
->new_data_offset
+= (s32
)le32_to_cpu(sb
->new_offset
);
1725 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1727 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1728 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1729 if (rdev
->sb_size
& bmask
)
1730 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1733 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1736 && rdev
->new_data_offset
< sb_start
+ (rdev
->sb_size
/512))
1739 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1742 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1744 if (!rdev
->bb_page
) {
1745 rdev
->bb_page
= alloc_page(GFP_KERNEL
);
1749 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BAD_BLOCKS
) &&
1750 rdev
->badblocks
.count
== 0) {
1751 /* need to load the bad block list.
1752 * Currently we limit it to one page.
1758 int sectors
= le16_to_cpu(sb
->bblog_size
);
1759 if (sectors
> (PAGE_SIZE
/ 512))
1761 offset
= le32_to_cpu(sb
->bblog_offset
);
1764 bb_sector
= (long long)offset
;
1765 if (!sync_page_io(rdev
, bb_sector
, sectors
<< 9,
1766 rdev
->bb_page
, REQ_OP_READ
, 0, true))
1768 bbp
= (__le64
*)page_address(rdev
->bb_page
);
1769 rdev
->badblocks
.shift
= sb
->bblog_shift
;
1770 for (i
= 0 ; i
< (sectors
<< (9-3)) ; i
++, bbp
++) {
1771 u64 bb
= le64_to_cpu(*bbp
);
1772 int count
= bb
& (0x3ff);
1773 u64 sector
= bb
>> 10;
1774 sector
<<= sb
->bblog_shift
;
1775 count
<<= sb
->bblog_shift
;
1778 if (badblocks_set(&rdev
->badblocks
, sector
, count
, 1))
1781 } else if (sb
->bblog_offset
!= 0)
1782 rdev
->badblocks
.shift
= 0;
1784 if ((le32_to_cpu(sb
->feature_map
) &
1785 (MD_FEATURE_PPL
| MD_FEATURE_MULTIPLE_PPLS
))) {
1786 rdev
->ppl
.offset
= (__s16
)le16_to_cpu(sb
->ppl
.offset
);
1787 rdev
->ppl
.size
= le16_to_cpu(sb
->ppl
.size
);
1788 rdev
->ppl
.sector
= rdev
->sb_start
+ rdev
->ppl
.offset
;
1791 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RAID0_LAYOUT
) &&
1795 /* not spare disk, or LEVEL_MULTIPATH */
1796 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
) ||
1797 (rdev
->desc_nr
>= 0 &&
1798 rdev
->desc_nr
< le32_to_cpu(sb
->max_dev
) &&
1799 (le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) < MD_DISK_ROLE_MAX
||
1800 le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) == MD_DISK_ROLE_JOURNAL
)))
1810 struct mdp_superblock_1
*refsb
= page_address(refdev
->sb_page
);
1812 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1813 sb
->level
!= refsb
->level
||
1814 sb
->layout
!= refsb
->layout
||
1815 sb
->chunksize
!= refsb
->chunksize
) {
1816 pr_warn("md: %s has strangely different superblock to %s\n",
1817 bdevname(rdev
->bdev
,b
),
1818 bdevname(refdev
->bdev
,b2
));
1821 ev1
= le64_to_cpu(sb
->events
);
1822 ev2
= le64_to_cpu(refsb
->events
);
1824 if (!spare_disk
&& ev1
> ev2
)
1829 if (minor_version
) {
1830 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9);
1831 sectors
-= rdev
->data_offset
;
1833 sectors
= rdev
->sb_start
;
1834 if (sectors
< le64_to_cpu(sb
->data_size
))
1836 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1840 static int super_1_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
1842 struct mdp_superblock_1
*sb
= page_address(rdev
->sb_page
);
1843 __u64 ev1
= le64_to_cpu(sb
->events
);
1845 rdev
->raid_disk
= -1;
1846 clear_bit(Faulty
, &rdev
->flags
);
1847 clear_bit(In_sync
, &rdev
->flags
);
1848 clear_bit(Bitmap_sync
, &rdev
->flags
);
1849 clear_bit(WriteMostly
, &rdev
->flags
);
1851 if (mddev
->raid_disks
== 0) {
1852 mddev
->major_version
= 1;
1853 mddev
->patch_version
= 0;
1854 mddev
->external
= 0;
1855 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1856 mddev
->ctime
= le64_to_cpu(sb
->ctime
);
1857 mddev
->utime
= le64_to_cpu(sb
->utime
);
1858 mddev
->level
= le32_to_cpu(sb
->level
);
1859 mddev
->clevel
[0] = 0;
1860 mddev
->layout
= le32_to_cpu(sb
->layout
);
1861 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1862 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1863 mddev
->events
= ev1
;
1864 mddev
->bitmap_info
.offset
= 0;
1865 mddev
->bitmap_info
.space
= 0;
1866 /* Default location for bitmap is 1K after superblock
1867 * using 3K - total of 4K
1869 mddev
->bitmap_info
.default_offset
= 1024 >> 9;
1870 mddev
->bitmap_info
.default_space
= (4096-1024) >> 9;
1871 mddev
->reshape_backwards
= 0;
1873 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1874 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1876 mddev
->max_disks
= (4096-256)/2;
1878 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1879 mddev
->bitmap_info
.file
== NULL
) {
1880 mddev
->bitmap_info
.offset
=
1881 (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1882 /* Metadata doesn't record how much space is available.
1883 * For 1.0, we assume we can use up to the superblock
1884 * if before, else to 4K beyond superblock.
1885 * For others, assume no change is possible.
1887 if (mddev
->minor_version
> 0)
1888 mddev
->bitmap_info
.space
= 0;
1889 else if (mddev
->bitmap_info
.offset
> 0)
1890 mddev
->bitmap_info
.space
=
1891 8 - mddev
->bitmap_info
.offset
;
1893 mddev
->bitmap_info
.space
=
1894 -mddev
->bitmap_info
.offset
;
1897 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1898 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1899 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1900 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1901 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1902 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1903 if (mddev
->delta_disks
< 0 ||
1904 (mddev
->delta_disks
== 0 &&
1905 (le32_to_cpu(sb
->feature_map
)
1906 & MD_FEATURE_RESHAPE_BACKWARDS
)))
1907 mddev
->reshape_backwards
= 1;
1909 mddev
->reshape_position
= MaxSector
;
1910 mddev
->delta_disks
= 0;
1911 mddev
->new_level
= mddev
->level
;
1912 mddev
->new_layout
= mddev
->layout
;
1913 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1916 if (mddev
->level
== 0 &&
1917 !(le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RAID0_LAYOUT
))
1920 if (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_JOURNAL
)
1921 set_bit(MD_HAS_JOURNAL
, &mddev
->flags
);
1923 if (le32_to_cpu(sb
->feature_map
) &
1924 (MD_FEATURE_PPL
| MD_FEATURE_MULTIPLE_PPLS
)) {
1925 if (le32_to_cpu(sb
->feature_map
) &
1926 (MD_FEATURE_BITMAP_OFFSET
| MD_FEATURE_JOURNAL
))
1928 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_PPL
) &&
1929 (le32_to_cpu(sb
->feature_map
) &
1930 MD_FEATURE_MULTIPLE_PPLS
))
1932 set_bit(MD_HAS_PPL
, &mddev
->flags
);
1934 } else if (mddev
->pers
== NULL
) {
1935 /* Insist of good event counter while assembling, except for
1936 * spares (which don't need an event count) */
1938 if (rdev
->desc_nr
>= 0 &&
1939 rdev
->desc_nr
< le32_to_cpu(sb
->max_dev
) &&
1940 (le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) < MD_DISK_ROLE_MAX
||
1941 le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) == MD_DISK_ROLE_JOURNAL
))
1942 if (ev1
< mddev
->events
)
1944 } else if (mddev
->bitmap
) {
1945 /* If adding to array with a bitmap, then we can accept an
1946 * older device, but not too old.
1948 if (ev1
< mddev
->bitmap
->events_cleared
)
1950 if (ev1
< mddev
->events
)
1951 set_bit(Bitmap_sync
, &rdev
->flags
);
1953 if (ev1
< mddev
->events
)
1954 /* just a hot-add of a new device, leave raid_disk at -1 */
1957 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1959 if (rdev
->desc_nr
< 0 ||
1960 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1961 role
= MD_DISK_ROLE_SPARE
;
1964 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1966 case MD_DISK_ROLE_SPARE
: /* spare */
1968 case MD_DISK_ROLE_FAULTY
: /* faulty */
1969 set_bit(Faulty
, &rdev
->flags
);
1971 case MD_DISK_ROLE_JOURNAL
: /* journal device */
1972 if (!(le32_to_cpu(sb
->feature_map
) & MD_FEATURE_JOURNAL
)) {
1973 /* journal device without journal feature */
1974 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
1977 set_bit(Journal
, &rdev
->flags
);
1978 rdev
->journal_tail
= le64_to_cpu(sb
->journal_tail
);
1979 rdev
->raid_disk
= 0;
1982 rdev
->saved_raid_disk
= role
;
1983 if ((le32_to_cpu(sb
->feature_map
) &
1984 MD_FEATURE_RECOVERY_OFFSET
)) {
1985 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1986 if (!(le32_to_cpu(sb
->feature_map
) &
1987 MD_FEATURE_RECOVERY_BITMAP
))
1988 rdev
->saved_raid_disk
= -1;
1991 * If the array is FROZEN, then the device can't
1992 * be in_sync with rest of array.
1994 if (!test_bit(MD_RECOVERY_FROZEN
,
1996 set_bit(In_sync
, &rdev
->flags
);
1998 rdev
->raid_disk
= role
;
2001 if (sb
->devflags
& WriteMostly1
)
2002 set_bit(WriteMostly
, &rdev
->flags
);
2003 if (sb
->devflags
& FailFast1
)
2004 set_bit(FailFast
, &rdev
->flags
);
2005 if (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_REPLACEMENT
)
2006 set_bit(Replacement
, &rdev
->flags
);
2007 } else /* MULTIPATH are always insync */
2008 set_bit(In_sync
, &rdev
->flags
);
2013 static void super_1_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
2015 struct mdp_superblock_1
*sb
;
2016 struct md_rdev
*rdev2
;
2018 /* make rdev->sb match mddev and rdev data. */
2020 sb
= page_address(rdev
->sb_page
);
2022 sb
->feature_map
= 0;
2024 sb
->recovery_offset
= cpu_to_le64(0);
2025 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
2027 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
2028 sb
->events
= cpu_to_le64(mddev
->events
);
2030 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
2031 else if (test_bit(MD_JOURNAL_CLEAN
, &mddev
->flags
))
2032 sb
->resync_offset
= cpu_to_le64(MaxSector
);
2034 sb
->resync_offset
= cpu_to_le64(0);
2036 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
2038 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
2039 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
2040 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
2041 sb
->level
= cpu_to_le32(mddev
->level
);
2042 sb
->layout
= cpu_to_le32(mddev
->layout
);
2043 if (test_bit(FailFast
, &rdev
->flags
))
2044 sb
->devflags
|= FailFast1
;
2046 sb
->devflags
&= ~FailFast1
;
2048 if (test_bit(WriteMostly
, &rdev
->flags
))
2049 sb
->devflags
|= WriteMostly1
;
2051 sb
->devflags
&= ~WriteMostly1
;
2052 sb
->data_offset
= cpu_to_le64(rdev
->data_offset
);
2053 sb
->data_size
= cpu_to_le64(rdev
->sectors
);
2055 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
) {
2056 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_info
.offset
);
2057 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
2060 if (rdev
->raid_disk
>= 0 && !test_bit(Journal
, &rdev
->flags
) &&
2061 !test_bit(In_sync
, &rdev
->flags
)) {
2063 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
2064 sb
->recovery_offset
=
2065 cpu_to_le64(rdev
->recovery_offset
);
2066 if (rdev
->saved_raid_disk
>= 0 && mddev
->bitmap
)
2068 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP
);
2070 /* Note: recovery_offset and journal_tail share space */
2071 if (test_bit(Journal
, &rdev
->flags
))
2072 sb
->journal_tail
= cpu_to_le64(rdev
->journal_tail
);
2073 if (test_bit(Replacement
, &rdev
->flags
))
2075 cpu_to_le32(MD_FEATURE_REPLACEMENT
);
2077 if (mddev
->reshape_position
!= MaxSector
) {
2078 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
2079 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
2080 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
2081 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
2082 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
2083 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
2084 if (mddev
->delta_disks
== 0 &&
2085 mddev
->reshape_backwards
)
2087 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS
);
2088 if (rdev
->new_data_offset
!= rdev
->data_offset
) {
2090 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET
);
2091 sb
->new_offset
= cpu_to_le32((__u32
)(rdev
->new_data_offset
2092 - rdev
->data_offset
));
2096 if (mddev_is_clustered(mddev
))
2097 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_CLUSTERED
);
2099 if (rdev
->badblocks
.count
== 0)
2100 /* Nothing to do for bad blocks*/ ;
2101 else if (sb
->bblog_offset
== 0)
2102 /* Cannot record bad blocks on this device */
2103 md_error(mddev
, rdev
);
2105 struct badblocks
*bb
= &rdev
->badblocks
;
2106 __le64
*bbp
= (__le64
*)page_address(rdev
->bb_page
);
2108 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_BAD_BLOCKS
);
2113 seq
= read_seqbegin(&bb
->lock
);
2115 memset(bbp
, 0xff, PAGE_SIZE
);
2117 for (i
= 0 ; i
< bb
->count
; i
++) {
2118 u64 internal_bb
= p
[i
];
2119 u64 store_bb
= ((BB_OFFSET(internal_bb
) << 10)
2120 | BB_LEN(internal_bb
));
2121 bbp
[i
] = cpu_to_le64(store_bb
);
2124 if (read_seqretry(&bb
->lock
, seq
))
2127 bb
->sector
= (rdev
->sb_start
+
2128 (int)le32_to_cpu(sb
->bblog_offset
));
2129 bb
->size
= le16_to_cpu(sb
->bblog_size
);
2134 rdev_for_each(rdev2
, mddev
)
2135 if (rdev2
->desc_nr
+1 > max_dev
)
2136 max_dev
= rdev2
->desc_nr
+1;
2138 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
2140 sb
->max_dev
= cpu_to_le32(max_dev
);
2141 rdev
->sb_size
= max_dev
* 2 + 256;
2142 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
2143 if (rdev
->sb_size
& bmask
)
2144 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
2146 max_dev
= le32_to_cpu(sb
->max_dev
);
2148 for (i
=0; i
<max_dev
;i
++)
2149 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_SPARE
);
2151 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
))
2152 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_JOURNAL
);
2154 if (test_bit(MD_HAS_PPL
, &mddev
->flags
)) {
2155 if (test_bit(MD_HAS_MULTIPLE_PPLS
, &mddev
->flags
))
2157 cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS
);
2159 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_PPL
);
2160 sb
->ppl
.offset
= cpu_to_le16(rdev
->ppl
.offset
);
2161 sb
->ppl
.size
= cpu_to_le16(rdev
->ppl
.size
);
2164 rdev_for_each(rdev2
, mddev
) {
2166 if (test_bit(Faulty
, &rdev2
->flags
))
2167 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_FAULTY
);
2168 else if (test_bit(In_sync
, &rdev2
->flags
))
2169 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
2170 else if (test_bit(Journal
, &rdev2
->flags
))
2171 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_JOURNAL
);
2172 else if (rdev2
->raid_disk
>= 0)
2173 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
2175 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_SPARE
);
2178 sb
->sb_csum
= calc_sb_1_csum(sb
);
2181 static sector_t
super_1_choose_bm_space(sector_t dev_size
)
2185 /* if the device is bigger than 8Gig, save 64k for bitmap
2186 * usage, if bigger than 200Gig, save 128k
2188 if (dev_size
< 64*2)
2190 else if (dev_size
- 64*2 >= 200*1024*1024*2)
2192 else if (dev_size
- 4*2 > 8*1024*1024*2)
2199 static unsigned long long
2200 super_1_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
2202 struct mdp_superblock_1
*sb
;
2203 sector_t max_sectors
;
2204 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
2205 return 0; /* component must fit device */
2206 if (rdev
->data_offset
!= rdev
->new_data_offset
)
2207 return 0; /* too confusing */
2208 if (rdev
->sb_start
< rdev
->data_offset
) {
2209 /* minor versions 1 and 2; superblock before data */
2210 max_sectors
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
2211 max_sectors
-= rdev
->data_offset
;
2212 if (!num_sectors
|| num_sectors
> max_sectors
)
2213 num_sectors
= max_sectors
;
2214 } else if (rdev
->mddev
->bitmap_info
.offset
) {
2215 /* minor version 0 with bitmap we can't move */
2218 /* minor version 0; superblock after data */
2219 sector_t sb_start
, bm_space
;
2220 sector_t dev_size
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
2222 /* 8K is for superblock */
2223 sb_start
= dev_size
- 8*2;
2224 sb_start
&= ~(sector_t
)(4*2 - 1);
2226 bm_space
= super_1_choose_bm_space(dev_size
);
2228 /* Space that can be used to store date needs to decrease
2229 * superblock bitmap space and bad block space(4K)
2231 max_sectors
= sb_start
- bm_space
- 4*2;
2233 if (!num_sectors
|| num_sectors
> max_sectors
)
2234 num_sectors
= max_sectors
;
2236 sb
= page_address(rdev
->sb_page
);
2237 sb
->data_size
= cpu_to_le64(num_sectors
);
2238 sb
->super_offset
= cpu_to_le64(rdev
->sb_start
);
2239 sb
->sb_csum
= calc_sb_1_csum(sb
);
2241 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
2243 } while (md_super_wait(rdev
->mddev
) < 0);
2249 super_1_allow_new_offset(struct md_rdev
*rdev
,
2250 unsigned long long new_offset
)
2252 /* All necessary checks on new >= old have been done */
2253 struct bitmap
*bitmap
;
2254 if (new_offset
>= rdev
->data_offset
)
2257 /* with 1.0 metadata, there is no metadata to tread on
2258 * so we can always move back */
2259 if (rdev
->mddev
->minor_version
== 0)
2262 /* otherwise we must be sure not to step on
2263 * any metadata, so stay:
2264 * 36K beyond start of superblock
2265 * beyond end of badblocks
2266 * beyond write-intent bitmap
2268 if (rdev
->sb_start
+ (32+4)*2 > new_offset
)
2270 bitmap
= rdev
->mddev
->bitmap
;
2271 if (bitmap
&& !rdev
->mddev
->bitmap_info
.file
&&
2272 rdev
->sb_start
+ rdev
->mddev
->bitmap_info
.offset
+
2273 bitmap
->storage
.file_pages
* (PAGE_SIZE
>>9) > new_offset
)
2275 if (rdev
->badblocks
.sector
+ rdev
->badblocks
.size
> new_offset
)
2281 static struct super_type super_types
[] = {
2284 .owner
= THIS_MODULE
,
2285 .load_super
= super_90_load
,
2286 .validate_super
= super_90_validate
,
2287 .sync_super
= super_90_sync
,
2288 .rdev_size_change
= super_90_rdev_size_change
,
2289 .allow_new_offset
= super_90_allow_new_offset
,
2293 .owner
= THIS_MODULE
,
2294 .load_super
= super_1_load
,
2295 .validate_super
= super_1_validate
,
2296 .sync_super
= super_1_sync
,
2297 .rdev_size_change
= super_1_rdev_size_change
,
2298 .allow_new_offset
= super_1_allow_new_offset
,
2302 static void sync_super(struct mddev
*mddev
, struct md_rdev
*rdev
)
2304 if (mddev
->sync_super
) {
2305 mddev
->sync_super(mddev
, rdev
);
2309 BUG_ON(mddev
->major_version
>= ARRAY_SIZE(super_types
));
2311 super_types
[mddev
->major_version
].sync_super(mddev
, rdev
);
2314 static int match_mddev_units(struct mddev
*mddev1
, struct mddev
*mddev2
)
2316 struct md_rdev
*rdev
, *rdev2
;
2319 rdev_for_each_rcu(rdev
, mddev1
) {
2320 if (test_bit(Faulty
, &rdev
->flags
) ||
2321 test_bit(Journal
, &rdev
->flags
) ||
2322 rdev
->raid_disk
== -1)
2324 rdev_for_each_rcu(rdev2
, mddev2
) {
2325 if (test_bit(Faulty
, &rdev2
->flags
) ||
2326 test_bit(Journal
, &rdev2
->flags
) ||
2327 rdev2
->raid_disk
== -1)
2329 if (rdev
->bdev
->bd_disk
== rdev2
->bdev
->bd_disk
) {
2339 static LIST_HEAD(pending_raid_disks
);
2342 * Try to register data integrity profile for an mddev
2344 * This is called when an array is started and after a disk has been kicked
2345 * from the array. It only succeeds if all working and active component devices
2346 * are integrity capable with matching profiles.
2348 int md_integrity_register(struct mddev
*mddev
)
2350 struct md_rdev
*rdev
, *reference
= NULL
;
2352 if (list_empty(&mddev
->disks
))
2353 return 0; /* nothing to do */
2354 if (!mddev
->gendisk
|| blk_get_integrity(mddev
->gendisk
))
2355 return 0; /* shouldn't register, or already is */
2356 rdev_for_each(rdev
, mddev
) {
2357 /* skip spares and non-functional disks */
2358 if (test_bit(Faulty
, &rdev
->flags
))
2360 if (rdev
->raid_disk
< 0)
2363 /* Use the first rdev as the reference */
2367 /* does this rdev's profile match the reference profile? */
2368 if (blk_integrity_compare(reference
->bdev
->bd_disk
,
2369 rdev
->bdev
->bd_disk
) < 0)
2372 if (!reference
|| !bdev_get_integrity(reference
->bdev
))
2375 * All component devices are integrity capable and have matching
2376 * profiles, register the common profile for the md device.
2378 blk_integrity_register(mddev
->gendisk
,
2379 bdev_get_integrity(reference
->bdev
));
2381 pr_debug("md: data integrity enabled on %s\n", mdname(mddev
));
2382 if (bioset_integrity_create(&mddev
->bio_set
, BIO_POOL_SIZE
)) {
2383 pr_err("md: failed to create integrity pool for %s\n",
2389 EXPORT_SYMBOL(md_integrity_register
);
2392 * Attempt to add an rdev, but only if it is consistent with the current
2395 int md_integrity_add_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
2397 struct blk_integrity
*bi_mddev
;
2398 char name
[BDEVNAME_SIZE
];
2400 if (!mddev
->gendisk
)
2403 bi_mddev
= blk_get_integrity(mddev
->gendisk
);
2405 if (!bi_mddev
) /* nothing to do */
2408 if (blk_integrity_compare(mddev
->gendisk
, rdev
->bdev
->bd_disk
) != 0) {
2409 pr_err("%s: incompatible integrity profile for %s\n",
2410 mdname(mddev
), bdevname(rdev
->bdev
, name
));
2416 EXPORT_SYMBOL(md_integrity_add_rdev
);
2418 static int bind_rdev_to_array(struct md_rdev
*rdev
, struct mddev
*mddev
)
2420 char b
[BDEVNAME_SIZE
];
2423 /* prevent duplicates */
2424 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
2427 if ((bdev_read_only(rdev
->bdev
) || bdev_read_only(rdev
->meta_bdev
)) &&
2431 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2432 if (!test_bit(Journal
, &rdev
->flags
) &&
2434 (mddev
->dev_sectors
== 0 || rdev
->sectors
< mddev
->dev_sectors
)) {
2436 /* Cannot change size, so fail
2437 * If mddev->level <= 0, then we don't care
2438 * about aligning sizes (e.g. linear)
2440 if (mddev
->level
> 0)
2443 mddev
->dev_sectors
= rdev
->sectors
;
2446 /* Verify rdev->desc_nr is unique.
2447 * If it is -1, assign a free number, else
2448 * check number is not in use
2451 if (rdev
->desc_nr
< 0) {
2454 choice
= mddev
->raid_disks
;
2455 while (md_find_rdev_nr_rcu(mddev
, choice
))
2457 rdev
->desc_nr
= choice
;
2459 if (md_find_rdev_nr_rcu(mddev
, rdev
->desc_nr
)) {
2465 if (!test_bit(Journal
, &rdev
->flags
) &&
2466 mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
2467 pr_warn("md: %s: array is limited to %d devices\n",
2468 mdname(mddev
), mddev
->max_disks
);
2471 bdevname(rdev
->bdev
,b
);
2472 strreplace(b
, '/', '!');
2474 rdev
->mddev
= mddev
;
2475 pr_debug("md: bind<%s>\n", b
);
2477 if (mddev
->raid_disks
)
2478 mddev_create_serial_pool(mddev
, rdev
, false);
2480 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
2483 /* failure here is OK */
2484 err
= sysfs_create_link(&rdev
->kobj
, bdev_kobj(rdev
->bdev
), "block");
2485 rdev
->sysfs_state
= sysfs_get_dirent_safe(rdev
->kobj
.sd
, "state");
2486 rdev
->sysfs_unack_badblocks
=
2487 sysfs_get_dirent_safe(rdev
->kobj
.sd
, "unacknowledged_bad_blocks");
2488 rdev
->sysfs_badblocks
=
2489 sysfs_get_dirent_safe(rdev
->kobj
.sd
, "bad_blocks");
2491 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
2492 bd_link_disk_holder(rdev
->bdev
, mddev
->gendisk
);
2494 /* May as well allow recovery to be retried once */
2495 mddev
->recovery_disabled
++;
2500 pr_warn("md: failed to register dev-%s for %s\n",
2505 static void rdev_delayed_delete(struct work_struct
*ws
)
2507 struct md_rdev
*rdev
= container_of(ws
, struct md_rdev
, del_work
);
2508 kobject_del(&rdev
->kobj
);
2509 kobject_put(&rdev
->kobj
);
2512 static void unbind_rdev_from_array(struct md_rdev
*rdev
)
2514 char b
[BDEVNAME_SIZE
];
2516 bd_unlink_disk_holder(rdev
->bdev
, rdev
->mddev
->gendisk
);
2517 list_del_rcu(&rdev
->same_set
);
2518 pr_debug("md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
2519 mddev_destroy_serial_pool(rdev
->mddev
, rdev
, false);
2521 sysfs_remove_link(&rdev
->kobj
, "block");
2522 sysfs_put(rdev
->sysfs_state
);
2523 sysfs_put(rdev
->sysfs_unack_badblocks
);
2524 sysfs_put(rdev
->sysfs_badblocks
);
2525 rdev
->sysfs_state
= NULL
;
2526 rdev
->sysfs_unack_badblocks
= NULL
;
2527 rdev
->sysfs_badblocks
= NULL
;
2528 rdev
->badblocks
.count
= 0;
2529 /* We need to delay this, otherwise we can deadlock when
2530 * writing to 'remove' to "dev/state". We also need
2531 * to delay it due to rcu usage.
2534 INIT_WORK(&rdev
->del_work
, rdev_delayed_delete
);
2535 kobject_get(&rdev
->kobj
);
2536 queue_work(md_rdev_misc_wq
, &rdev
->del_work
);
2540 * prevent the device from being mounted, repartitioned or
2541 * otherwise reused by a RAID array (or any other kernel
2542 * subsystem), by bd_claiming the device.
2544 static int lock_rdev(struct md_rdev
*rdev
, dev_t dev
, int shared
)
2547 struct block_device
*bdev
;
2549 bdev
= blkdev_get_by_dev(dev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
,
2550 shared
? (struct md_rdev
*)lock_rdev
: rdev
);
2552 pr_warn("md: could not open device unknown-block(%u,%u).\n",
2553 MAJOR(dev
), MINOR(dev
));
2554 return PTR_ERR(bdev
);
2560 static void unlock_rdev(struct md_rdev
*rdev
)
2562 struct block_device
*bdev
= rdev
->bdev
;
2564 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
2567 void md_autodetect_dev(dev_t dev
);
2569 static void export_rdev(struct md_rdev
*rdev
)
2571 char b
[BDEVNAME_SIZE
];
2573 pr_debug("md: export_rdev(%s)\n", bdevname(rdev
->bdev
,b
));
2574 md_rdev_clear(rdev
);
2576 if (test_bit(AutoDetected
, &rdev
->flags
))
2577 md_autodetect_dev(rdev
->bdev
->bd_dev
);
2580 kobject_put(&rdev
->kobj
);
2583 void md_kick_rdev_from_array(struct md_rdev
*rdev
)
2585 unbind_rdev_from_array(rdev
);
2588 EXPORT_SYMBOL_GPL(md_kick_rdev_from_array
);
2590 static void export_array(struct mddev
*mddev
)
2592 struct md_rdev
*rdev
;
2594 while (!list_empty(&mddev
->disks
)) {
2595 rdev
= list_first_entry(&mddev
->disks
, struct md_rdev
,
2597 md_kick_rdev_from_array(rdev
);
2599 mddev
->raid_disks
= 0;
2600 mddev
->major_version
= 0;
2603 static bool set_in_sync(struct mddev
*mddev
)
2605 lockdep_assert_held(&mddev
->lock
);
2606 if (!mddev
->in_sync
) {
2607 mddev
->sync_checkers
++;
2608 spin_unlock(&mddev
->lock
);
2609 percpu_ref_switch_to_atomic_sync(&mddev
->writes_pending
);
2610 spin_lock(&mddev
->lock
);
2611 if (!mddev
->in_sync
&&
2612 percpu_ref_is_zero(&mddev
->writes_pending
)) {
2615 * Ensure ->in_sync is visible before we clear
2619 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
2620 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
2622 if (--mddev
->sync_checkers
== 0)
2623 percpu_ref_switch_to_percpu(&mddev
->writes_pending
);
2625 if (mddev
->safemode
== 1)
2626 mddev
->safemode
= 0;
2627 return mddev
->in_sync
;
2630 static void sync_sbs(struct mddev
*mddev
, int nospares
)
2632 /* Update each superblock (in-memory image), but
2633 * if we are allowed to, skip spares which already
2634 * have the right event counter, or have one earlier
2635 * (which would mean they aren't being marked as dirty
2636 * with the rest of the array)
2638 struct md_rdev
*rdev
;
2639 rdev_for_each(rdev
, mddev
) {
2640 if (rdev
->sb_events
== mddev
->events
||
2642 rdev
->raid_disk
< 0 &&
2643 rdev
->sb_events
+1 == mddev
->events
)) {
2644 /* Don't update this superblock */
2645 rdev
->sb_loaded
= 2;
2647 sync_super(mddev
, rdev
);
2648 rdev
->sb_loaded
= 1;
2653 static bool does_sb_need_changing(struct mddev
*mddev
)
2655 struct md_rdev
*rdev
;
2656 struct mdp_superblock_1
*sb
;
2659 /* Find a good rdev */
2660 rdev_for_each(rdev
, mddev
)
2661 if ((rdev
->raid_disk
>= 0) && !test_bit(Faulty
, &rdev
->flags
))
2664 /* No good device found. */
2668 sb
= page_address(rdev
->sb_page
);
2669 /* Check if a device has become faulty or a spare become active */
2670 rdev_for_each(rdev
, mddev
) {
2671 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
2672 /* Device activated? */
2673 if (role
== 0xffff && rdev
->raid_disk
>=0 &&
2674 !test_bit(Faulty
, &rdev
->flags
))
2676 /* Device turned faulty? */
2677 if (test_bit(Faulty
, &rdev
->flags
) && (role
< 0xfffd))
2681 /* Check if any mddev parameters have changed */
2682 if ((mddev
->dev_sectors
!= le64_to_cpu(sb
->size
)) ||
2683 (mddev
->reshape_position
!= le64_to_cpu(sb
->reshape_position
)) ||
2684 (mddev
->layout
!= le32_to_cpu(sb
->layout
)) ||
2685 (mddev
->raid_disks
!= le32_to_cpu(sb
->raid_disks
)) ||
2686 (mddev
->chunk_sectors
!= le32_to_cpu(sb
->chunksize
)))
2692 void md_update_sb(struct mddev
*mddev
, int force_change
)
2694 struct md_rdev
*rdev
;
2697 int any_badblocks_changed
= 0;
2702 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2707 if (mddev_is_clustered(mddev
)) {
2708 if (test_and_clear_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
))
2710 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
))
2712 ret
= md_cluster_ops
->metadata_update_start(mddev
);
2713 /* Has someone else has updated the sb */
2714 if (!does_sb_need_changing(mddev
)) {
2716 md_cluster_ops
->metadata_update_cancel(mddev
);
2717 bit_clear_unless(&mddev
->sb_flags
, BIT(MD_SB_CHANGE_PENDING
),
2718 BIT(MD_SB_CHANGE_DEVS
) |
2719 BIT(MD_SB_CHANGE_CLEAN
));
2725 * First make sure individual recovery_offsets are correct
2726 * curr_resync_completed can only be used during recovery.
2727 * During reshape/resync it might use array-addresses rather
2728 * that device addresses.
2730 rdev_for_each(rdev
, mddev
) {
2731 if (rdev
->raid_disk
>= 0 &&
2732 mddev
->delta_disks
>= 0 &&
2733 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
2734 test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
) &&
2735 !test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
2736 !test_bit(Journal
, &rdev
->flags
) &&
2737 !test_bit(In_sync
, &rdev
->flags
) &&
2738 mddev
->curr_resync_completed
> rdev
->recovery_offset
)
2739 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
2742 if (!mddev
->persistent
) {
2743 clear_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
2744 clear_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2745 if (!mddev
->external
) {
2746 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
2747 rdev_for_each(rdev
, mddev
) {
2748 if (rdev
->badblocks
.changed
) {
2749 rdev
->badblocks
.changed
= 0;
2750 ack_all_badblocks(&rdev
->badblocks
);
2751 md_error(mddev
, rdev
);
2753 clear_bit(Blocked
, &rdev
->flags
);
2754 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2755 wake_up(&rdev
->blocked_wait
);
2758 wake_up(&mddev
->sb_wait
);
2762 spin_lock(&mddev
->lock
);
2764 mddev
->utime
= ktime_get_real_seconds();
2766 if (test_and_clear_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
))
2768 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
))
2769 /* just a clean<-> dirty transition, possibly leave spares alone,
2770 * though if events isn't the right even/odd, we will have to do
2776 if (mddev
->degraded
)
2777 /* If the array is degraded, then skipping spares is both
2778 * dangerous and fairly pointless.
2779 * Dangerous because a device that was removed from the array
2780 * might have a event_count that still looks up-to-date,
2781 * so it can be re-added without a resync.
2782 * Pointless because if there are any spares to skip,
2783 * then a recovery will happen and soon that array won't
2784 * be degraded any more and the spare can go back to sleep then.
2788 sync_req
= mddev
->in_sync
;
2790 /* If this is just a dirty<->clean transition, and the array is clean
2791 * and 'events' is odd, we can roll back to the previous clean state */
2793 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
2794 && mddev
->can_decrease_events
2795 && mddev
->events
!= 1) {
2797 mddev
->can_decrease_events
= 0;
2799 /* otherwise we have to go forward and ... */
2801 mddev
->can_decrease_events
= nospares
;
2805 * This 64-bit counter should never wrap.
2806 * Either we are in around ~1 trillion A.C., assuming
2807 * 1 reboot per second, or we have a bug...
2809 WARN_ON(mddev
->events
== 0);
2811 rdev_for_each(rdev
, mddev
) {
2812 if (rdev
->badblocks
.changed
)
2813 any_badblocks_changed
++;
2814 if (test_bit(Faulty
, &rdev
->flags
))
2815 set_bit(FaultRecorded
, &rdev
->flags
);
2818 sync_sbs(mddev
, nospares
);
2819 spin_unlock(&mddev
->lock
);
2821 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2822 mdname(mddev
), mddev
->in_sync
);
2825 blk_add_trace_msg(mddev
->queue
, "md md_update_sb");
2827 md_bitmap_update_sb(mddev
->bitmap
);
2828 rdev_for_each(rdev
, mddev
) {
2829 char b
[BDEVNAME_SIZE
];
2831 if (rdev
->sb_loaded
!= 1)
2832 continue; /* no noise on spare devices */
2834 if (!test_bit(Faulty
, &rdev
->flags
)) {
2835 md_super_write(mddev
,rdev
,
2836 rdev
->sb_start
, rdev
->sb_size
,
2838 pr_debug("md: (write) %s's sb offset: %llu\n",
2839 bdevname(rdev
->bdev
, b
),
2840 (unsigned long long)rdev
->sb_start
);
2841 rdev
->sb_events
= mddev
->events
;
2842 if (rdev
->badblocks
.size
) {
2843 md_super_write(mddev
, rdev
,
2844 rdev
->badblocks
.sector
,
2845 rdev
->badblocks
.size
<< 9,
2847 rdev
->badblocks
.size
= 0;
2851 pr_debug("md: %s (skipping faulty)\n",
2852 bdevname(rdev
->bdev
, b
));
2854 if (mddev
->level
== LEVEL_MULTIPATH
)
2855 /* only need to write one superblock... */
2858 if (md_super_wait(mddev
) < 0)
2860 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
2862 if (mddev_is_clustered(mddev
) && ret
== 0)
2863 md_cluster_ops
->metadata_update_finish(mddev
);
2865 if (mddev
->in_sync
!= sync_req
||
2866 !bit_clear_unless(&mddev
->sb_flags
, BIT(MD_SB_CHANGE_PENDING
),
2867 BIT(MD_SB_CHANGE_DEVS
) | BIT(MD_SB_CHANGE_CLEAN
)))
2868 /* have to write it out again */
2870 wake_up(&mddev
->sb_wait
);
2871 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2872 sysfs_notify_dirent_safe(mddev
->sysfs_completed
);
2874 rdev_for_each(rdev
, mddev
) {
2875 if (test_and_clear_bit(FaultRecorded
, &rdev
->flags
))
2876 clear_bit(Blocked
, &rdev
->flags
);
2878 if (any_badblocks_changed
)
2879 ack_all_badblocks(&rdev
->badblocks
);
2880 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2881 wake_up(&rdev
->blocked_wait
);
2884 EXPORT_SYMBOL(md_update_sb
);
2886 static int add_bound_rdev(struct md_rdev
*rdev
)
2888 struct mddev
*mddev
= rdev
->mddev
;
2890 bool add_journal
= test_bit(Journal
, &rdev
->flags
);
2892 if (!mddev
->pers
->hot_remove_disk
|| add_journal
) {
2893 /* If there is hot_add_disk but no hot_remove_disk
2894 * then added disks for geometry changes,
2895 * and should be added immediately.
2897 super_types
[mddev
->major_version
].
2898 validate_super(mddev
, rdev
);
2900 mddev_suspend(mddev
);
2901 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
2903 mddev_resume(mddev
);
2905 md_kick_rdev_from_array(rdev
);
2909 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2911 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2912 if (mddev
->degraded
)
2913 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
2914 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2915 md_new_event(mddev
);
2916 md_wakeup_thread(mddev
->thread
);
2920 /* words written to sysfs files may, or may not, be \n terminated.
2921 * We want to accept with case. For this we use cmd_match.
2923 static int cmd_match(const char *cmd
, const char *str
)
2925 /* See if cmd, written into a sysfs file, matches
2926 * str. They must either be the same, or cmd can
2927 * have a trailing newline
2929 while (*cmd
&& *str
&& *cmd
== *str
) {
2940 struct rdev_sysfs_entry
{
2941 struct attribute attr
;
2942 ssize_t (*show
)(struct md_rdev
*, char *);
2943 ssize_t (*store
)(struct md_rdev
*, const char *, size_t);
2947 state_show(struct md_rdev
*rdev
, char *page
)
2951 unsigned long flags
= READ_ONCE(rdev
->flags
);
2953 if (test_bit(Faulty
, &flags
) ||
2954 (!test_bit(ExternalBbl
, &flags
) &&
2955 rdev
->badblocks
.unacked_exist
))
2956 len
+= sprintf(page
+len
, "faulty%s", sep
);
2957 if (test_bit(In_sync
, &flags
))
2958 len
+= sprintf(page
+len
, "in_sync%s", sep
);
2959 if (test_bit(Journal
, &flags
))
2960 len
+= sprintf(page
+len
, "journal%s", sep
);
2961 if (test_bit(WriteMostly
, &flags
))
2962 len
+= sprintf(page
+len
, "write_mostly%s", sep
);
2963 if (test_bit(Blocked
, &flags
) ||
2964 (rdev
->badblocks
.unacked_exist
2965 && !test_bit(Faulty
, &flags
)))
2966 len
+= sprintf(page
+len
, "blocked%s", sep
);
2967 if (!test_bit(Faulty
, &flags
) &&
2968 !test_bit(Journal
, &flags
) &&
2969 !test_bit(In_sync
, &flags
))
2970 len
+= sprintf(page
+len
, "spare%s", sep
);
2971 if (test_bit(WriteErrorSeen
, &flags
))
2972 len
+= sprintf(page
+len
, "write_error%s", sep
);
2973 if (test_bit(WantReplacement
, &flags
))
2974 len
+= sprintf(page
+len
, "want_replacement%s", sep
);
2975 if (test_bit(Replacement
, &flags
))
2976 len
+= sprintf(page
+len
, "replacement%s", sep
);
2977 if (test_bit(ExternalBbl
, &flags
))
2978 len
+= sprintf(page
+len
, "external_bbl%s", sep
);
2979 if (test_bit(FailFast
, &flags
))
2980 len
+= sprintf(page
+len
, "failfast%s", sep
);
2985 return len
+sprintf(page
+len
, "\n");
2989 state_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2992 * faulty - simulates an error
2993 * remove - disconnects the device
2994 * writemostly - sets write_mostly
2995 * -writemostly - clears write_mostly
2996 * blocked - sets the Blocked flags
2997 * -blocked - clears the Blocked and possibly simulates an error
2998 * insync - sets Insync providing device isn't active
2999 * -insync - clear Insync for a device with a slot assigned,
3000 * so that it gets rebuilt based on bitmap
3001 * write_error - sets WriteErrorSeen
3002 * -write_error - clears WriteErrorSeen
3003 * {,-}failfast - set/clear FailFast
3006 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
3007 md_error(rdev
->mddev
, rdev
);
3008 if (test_bit(Faulty
, &rdev
->flags
))
3012 } else if (cmd_match(buf
, "remove")) {
3013 if (rdev
->mddev
->pers
) {
3014 clear_bit(Blocked
, &rdev
->flags
);
3015 remove_and_add_spares(rdev
->mddev
, rdev
);
3017 if (rdev
->raid_disk
>= 0)
3020 struct mddev
*mddev
= rdev
->mddev
;
3022 if (mddev_is_clustered(mddev
))
3023 err
= md_cluster_ops
->remove_disk(mddev
, rdev
);
3026 md_kick_rdev_from_array(rdev
);
3028 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
3029 md_wakeup_thread(mddev
->thread
);
3031 md_new_event(mddev
);
3034 } else if (cmd_match(buf
, "writemostly")) {
3035 set_bit(WriteMostly
, &rdev
->flags
);
3036 mddev_create_serial_pool(rdev
->mddev
, rdev
, false);
3038 } else if (cmd_match(buf
, "-writemostly")) {
3039 mddev_destroy_serial_pool(rdev
->mddev
, rdev
, false);
3040 clear_bit(WriteMostly
, &rdev
->flags
);
3042 } else if (cmd_match(buf
, "blocked")) {
3043 set_bit(Blocked
, &rdev
->flags
);
3045 } else if (cmd_match(buf
, "-blocked")) {
3046 if (!test_bit(Faulty
, &rdev
->flags
) &&
3047 !test_bit(ExternalBbl
, &rdev
->flags
) &&
3048 rdev
->badblocks
.unacked_exist
) {
3049 /* metadata handler doesn't understand badblocks,
3050 * so we need to fail the device
3052 md_error(rdev
->mddev
, rdev
);
3054 clear_bit(Blocked
, &rdev
->flags
);
3055 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
3056 wake_up(&rdev
->blocked_wait
);
3057 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
3058 md_wakeup_thread(rdev
->mddev
->thread
);
3061 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
3062 set_bit(In_sync
, &rdev
->flags
);
3064 } else if (cmd_match(buf
, "failfast")) {
3065 set_bit(FailFast
, &rdev
->flags
);
3067 } else if (cmd_match(buf
, "-failfast")) {
3068 clear_bit(FailFast
, &rdev
->flags
);
3070 } else if (cmd_match(buf
, "-insync") && rdev
->raid_disk
>= 0 &&
3071 !test_bit(Journal
, &rdev
->flags
)) {
3072 if (rdev
->mddev
->pers
== NULL
) {
3073 clear_bit(In_sync
, &rdev
->flags
);
3074 rdev
->saved_raid_disk
= rdev
->raid_disk
;
3075 rdev
->raid_disk
= -1;
3078 } else if (cmd_match(buf
, "write_error")) {
3079 set_bit(WriteErrorSeen
, &rdev
->flags
);
3081 } else if (cmd_match(buf
, "-write_error")) {
3082 clear_bit(WriteErrorSeen
, &rdev
->flags
);
3084 } else if (cmd_match(buf
, "want_replacement")) {
3085 /* Any non-spare device that is not a replacement can
3086 * become want_replacement at any time, but we then need to
3087 * check if recovery is needed.
3089 if (rdev
->raid_disk
>= 0 &&
3090 !test_bit(Journal
, &rdev
->flags
) &&
3091 !test_bit(Replacement
, &rdev
->flags
))
3092 set_bit(WantReplacement
, &rdev
->flags
);
3093 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
3094 md_wakeup_thread(rdev
->mddev
->thread
);
3096 } else if (cmd_match(buf
, "-want_replacement")) {
3097 /* Clearing 'want_replacement' is always allowed.
3098 * Once replacements starts it is too late though.
3101 clear_bit(WantReplacement
, &rdev
->flags
);
3102 } else if (cmd_match(buf
, "replacement")) {
3103 /* Can only set a device as a replacement when array has not
3104 * yet been started. Once running, replacement is automatic
3105 * from spares, or by assigning 'slot'.
3107 if (rdev
->mddev
->pers
)
3110 set_bit(Replacement
, &rdev
->flags
);
3113 } else if (cmd_match(buf
, "-replacement")) {
3114 /* Similarly, can only clear Replacement before start */
3115 if (rdev
->mddev
->pers
)
3118 clear_bit(Replacement
, &rdev
->flags
);
3121 } else if (cmd_match(buf
, "re-add")) {
3122 if (!rdev
->mddev
->pers
)
3124 else if (test_bit(Faulty
, &rdev
->flags
) && (rdev
->raid_disk
== -1) &&
3125 rdev
->saved_raid_disk
>= 0) {
3126 /* clear_bit is performed _after_ all the devices
3127 * have their local Faulty bit cleared. If any writes
3128 * happen in the meantime in the local node, they
3129 * will land in the local bitmap, which will be synced
3130 * by this node eventually
3132 if (!mddev_is_clustered(rdev
->mddev
) ||
3133 (err
= md_cluster_ops
->gather_bitmaps(rdev
)) == 0) {
3134 clear_bit(Faulty
, &rdev
->flags
);
3135 err
= add_bound_rdev(rdev
);
3139 } else if (cmd_match(buf
, "external_bbl") && (rdev
->mddev
->external
)) {
3140 set_bit(ExternalBbl
, &rdev
->flags
);
3141 rdev
->badblocks
.shift
= 0;
3143 } else if (cmd_match(buf
, "-external_bbl") && (rdev
->mddev
->external
)) {
3144 clear_bit(ExternalBbl
, &rdev
->flags
);
3148 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
3149 return err
? err
: len
;
3151 static struct rdev_sysfs_entry rdev_state
=
3152 __ATTR_PREALLOC(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
3155 errors_show(struct md_rdev
*rdev
, char *page
)
3157 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
3161 errors_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3166 rv
= kstrtouint(buf
, 10, &n
);
3169 atomic_set(&rdev
->corrected_errors
, n
);
3172 static struct rdev_sysfs_entry rdev_errors
=
3173 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
3176 slot_show(struct md_rdev
*rdev
, char *page
)
3178 if (test_bit(Journal
, &rdev
->flags
))
3179 return sprintf(page
, "journal\n");
3180 else if (rdev
->raid_disk
< 0)
3181 return sprintf(page
, "none\n");
3183 return sprintf(page
, "%d\n", rdev
->raid_disk
);
3187 slot_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3192 if (test_bit(Journal
, &rdev
->flags
))
3194 if (strncmp(buf
, "none", 4)==0)
3197 err
= kstrtouint(buf
, 10, (unsigned int *)&slot
);
3201 if (rdev
->mddev
->pers
&& slot
== -1) {
3202 /* Setting 'slot' on an active array requires also
3203 * updating the 'rd%d' link, and communicating
3204 * with the personality with ->hot_*_disk.
3205 * For now we only support removing
3206 * failed/spare devices. This normally happens automatically,
3207 * but not when the metadata is externally managed.
3209 if (rdev
->raid_disk
== -1)
3211 /* personality does all needed checks */
3212 if (rdev
->mddev
->pers
->hot_remove_disk
== NULL
)
3214 clear_bit(Blocked
, &rdev
->flags
);
3215 remove_and_add_spares(rdev
->mddev
, rdev
);
3216 if (rdev
->raid_disk
>= 0)
3218 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
3219 md_wakeup_thread(rdev
->mddev
->thread
);
3220 } else if (rdev
->mddev
->pers
) {
3221 /* Activating a spare .. or possibly reactivating
3222 * if we ever get bitmaps working here.
3226 if (rdev
->raid_disk
!= -1)
3229 if (test_bit(MD_RECOVERY_RUNNING
, &rdev
->mddev
->recovery
))
3232 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
3235 if (slot
>= rdev
->mddev
->raid_disks
&&
3236 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
3239 rdev
->raid_disk
= slot
;
3240 if (test_bit(In_sync
, &rdev
->flags
))
3241 rdev
->saved_raid_disk
= slot
;
3243 rdev
->saved_raid_disk
= -1;
3244 clear_bit(In_sync
, &rdev
->flags
);
3245 clear_bit(Bitmap_sync
, &rdev
->flags
);
3246 err
= rdev
->mddev
->pers
->hot_add_disk(rdev
->mddev
, rdev
);
3248 rdev
->raid_disk
= -1;
3251 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
3252 /* failure here is OK */;
3253 sysfs_link_rdev(rdev
->mddev
, rdev
);
3254 /* don't wakeup anyone, leave that to userspace. */
3256 if (slot
>= rdev
->mddev
->raid_disks
&&
3257 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
3259 rdev
->raid_disk
= slot
;
3260 /* assume it is working */
3261 clear_bit(Faulty
, &rdev
->flags
);
3262 clear_bit(WriteMostly
, &rdev
->flags
);
3263 set_bit(In_sync
, &rdev
->flags
);
3264 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
3269 static struct rdev_sysfs_entry rdev_slot
=
3270 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
3273 offset_show(struct md_rdev
*rdev
, char *page
)
3275 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
3279 offset_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3281 unsigned long long offset
;
3282 if (kstrtoull(buf
, 10, &offset
) < 0)
3284 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
3286 if (rdev
->sectors
&& rdev
->mddev
->external
)
3287 /* Must set offset before size, so overlap checks
3290 rdev
->data_offset
= offset
;
3291 rdev
->new_data_offset
= offset
;
3295 static struct rdev_sysfs_entry rdev_offset
=
3296 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
3298 static ssize_t
new_offset_show(struct md_rdev
*rdev
, char *page
)
3300 return sprintf(page
, "%llu\n",
3301 (unsigned long long)rdev
->new_data_offset
);
3304 static ssize_t
new_offset_store(struct md_rdev
*rdev
,
3305 const char *buf
, size_t len
)
3307 unsigned long long new_offset
;
3308 struct mddev
*mddev
= rdev
->mddev
;
3310 if (kstrtoull(buf
, 10, &new_offset
) < 0)
3313 if (mddev
->sync_thread
||
3314 test_bit(MD_RECOVERY_RUNNING
,&mddev
->recovery
))
3316 if (new_offset
== rdev
->data_offset
)
3317 /* reset is always permitted */
3319 else if (new_offset
> rdev
->data_offset
) {
3320 /* must not push array size beyond rdev_sectors */
3321 if (new_offset
- rdev
->data_offset
3322 + mddev
->dev_sectors
> rdev
->sectors
)
3325 /* Metadata worries about other space details. */
3327 /* decreasing the offset is inconsistent with a backwards
3330 if (new_offset
< rdev
->data_offset
&&
3331 mddev
->reshape_backwards
)
3333 /* Increasing offset is inconsistent with forwards
3334 * reshape. reshape_direction should be set to
3335 * 'backwards' first.
3337 if (new_offset
> rdev
->data_offset
&&
3338 !mddev
->reshape_backwards
)
3341 if (mddev
->pers
&& mddev
->persistent
&&
3342 !super_types
[mddev
->major_version
]
3343 .allow_new_offset(rdev
, new_offset
))
3345 rdev
->new_data_offset
= new_offset
;
3346 if (new_offset
> rdev
->data_offset
)
3347 mddev
->reshape_backwards
= 1;
3348 else if (new_offset
< rdev
->data_offset
)
3349 mddev
->reshape_backwards
= 0;
3353 static struct rdev_sysfs_entry rdev_new_offset
=
3354 __ATTR(new_offset
, S_IRUGO
|S_IWUSR
, new_offset_show
, new_offset_store
);
3357 rdev_size_show(struct md_rdev
*rdev
, char *page
)
3359 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
3362 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
3364 /* check if two start/length pairs overlap */
3372 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
3374 unsigned long long blocks
;
3377 if (kstrtoull(buf
, 10, &blocks
) < 0)
3380 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
3381 return -EINVAL
; /* sector conversion overflow */
3384 if (new != blocks
* 2)
3385 return -EINVAL
; /* unsigned long long to sector_t overflow */
3392 rdev_size_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3394 struct mddev
*my_mddev
= rdev
->mddev
;
3395 sector_t oldsectors
= rdev
->sectors
;
3398 if (test_bit(Journal
, &rdev
->flags
))
3400 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
3402 if (rdev
->data_offset
!= rdev
->new_data_offset
)
3403 return -EINVAL
; /* too confusing */
3404 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
3405 if (my_mddev
->persistent
) {
3406 sectors
= super_types
[my_mddev
->major_version
].
3407 rdev_size_change(rdev
, sectors
);
3410 } else if (!sectors
)
3411 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
3413 if (!my_mddev
->pers
->resize
)
3414 /* Cannot change size for RAID0 or Linear etc */
3417 if (sectors
< my_mddev
->dev_sectors
)
3418 return -EINVAL
; /* component must fit device */
3420 rdev
->sectors
= sectors
;
3421 if (sectors
> oldsectors
&& my_mddev
->external
) {
3422 /* Need to check that all other rdevs with the same
3423 * ->bdev do not overlap. 'rcu' is sufficient to walk
3424 * the rdev lists safely.
3425 * This check does not provide a hard guarantee, it
3426 * just helps avoid dangerous mistakes.
3428 struct mddev
*mddev
;
3430 struct list_head
*tmp
;
3433 for_each_mddev(mddev
, tmp
) {
3434 struct md_rdev
*rdev2
;
3436 rdev_for_each(rdev2
, mddev
)
3437 if (rdev
->bdev
== rdev2
->bdev
&&
3439 overlaps(rdev
->data_offset
, rdev
->sectors
,
3452 /* Someone else could have slipped in a size
3453 * change here, but doing so is just silly.
3454 * We put oldsectors back because we *know* it is
3455 * safe, and trust userspace not to race with
3458 rdev
->sectors
= oldsectors
;
3465 static struct rdev_sysfs_entry rdev_size
=
3466 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
3468 static ssize_t
recovery_start_show(struct md_rdev
*rdev
, char *page
)
3470 unsigned long long recovery_start
= rdev
->recovery_offset
;
3472 if (test_bit(In_sync
, &rdev
->flags
) ||
3473 recovery_start
== MaxSector
)
3474 return sprintf(page
, "none\n");
3476 return sprintf(page
, "%llu\n", recovery_start
);
3479 static ssize_t
recovery_start_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3481 unsigned long long recovery_start
;
3483 if (cmd_match(buf
, "none"))
3484 recovery_start
= MaxSector
;
3485 else if (kstrtoull(buf
, 10, &recovery_start
))
3488 if (rdev
->mddev
->pers
&&
3489 rdev
->raid_disk
>= 0)
3492 rdev
->recovery_offset
= recovery_start
;
3493 if (recovery_start
== MaxSector
)
3494 set_bit(In_sync
, &rdev
->flags
);
3496 clear_bit(In_sync
, &rdev
->flags
);
3500 static struct rdev_sysfs_entry rdev_recovery_start
=
3501 __ATTR(recovery_start
, S_IRUGO
|S_IWUSR
, recovery_start_show
, recovery_start_store
);
3503 /* sysfs access to bad-blocks list.
3504 * We present two files.
3505 * 'bad-blocks' lists sector numbers and lengths of ranges that
3506 * are recorded as bad. The list is truncated to fit within
3507 * the one-page limit of sysfs.
3508 * Writing "sector length" to this file adds an acknowledged
3510 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3511 * been acknowledged. Writing to this file adds bad blocks
3512 * without acknowledging them. This is largely for testing.
3514 static ssize_t
bb_show(struct md_rdev
*rdev
, char *page
)
3516 return badblocks_show(&rdev
->badblocks
, page
, 0);
3518 static ssize_t
bb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
3520 int rv
= badblocks_store(&rdev
->badblocks
, page
, len
, 0);
3521 /* Maybe that ack was all we needed */
3522 if (test_and_clear_bit(BlockedBadBlocks
, &rdev
->flags
))
3523 wake_up(&rdev
->blocked_wait
);
3526 static struct rdev_sysfs_entry rdev_bad_blocks
=
3527 __ATTR(bad_blocks
, S_IRUGO
|S_IWUSR
, bb_show
, bb_store
);
3529 static ssize_t
ubb_show(struct md_rdev
*rdev
, char *page
)
3531 return badblocks_show(&rdev
->badblocks
, page
, 1);
3533 static ssize_t
ubb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
3535 return badblocks_store(&rdev
->badblocks
, page
, len
, 1);
3537 static struct rdev_sysfs_entry rdev_unack_bad_blocks
=
3538 __ATTR(unacknowledged_bad_blocks
, S_IRUGO
|S_IWUSR
, ubb_show
, ubb_store
);
3541 ppl_sector_show(struct md_rdev
*rdev
, char *page
)
3543 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->ppl
.sector
);
3547 ppl_sector_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3549 unsigned long long sector
;
3551 if (kstrtoull(buf
, 10, §or
) < 0)
3553 if (sector
!= (sector_t
)sector
)
3556 if (rdev
->mddev
->pers
&& test_bit(MD_HAS_PPL
, &rdev
->mddev
->flags
) &&
3557 rdev
->raid_disk
>= 0)
3560 if (rdev
->mddev
->persistent
) {
3561 if (rdev
->mddev
->major_version
== 0)
3563 if ((sector
> rdev
->sb_start
&&
3564 sector
- rdev
->sb_start
> S16_MAX
) ||
3565 (sector
< rdev
->sb_start
&&
3566 rdev
->sb_start
- sector
> -S16_MIN
))
3568 rdev
->ppl
.offset
= sector
- rdev
->sb_start
;
3569 } else if (!rdev
->mddev
->external
) {
3572 rdev
->ppl
.sector
= sector
;
3576 static struct rdev_sysfs_entry rdev_ppl_sector
=
3577 __ATTR(ppl_sector
, S_IRUGO
|S_IWUSR
, ppl_sector_show
, ppl_sector_store
);
3580 ppl_size_show(struct md_rdev
*rdev
, char *page
)
3582 return sprintf(page
, "%u\n", rdev
->ppl
.size
);
3586 ppl_size_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3590 if (kstrtouint(buf
, 10, &size
) < 0)
3593 if (rdev
->mddev
->pers
&& test_bit(MD_HAS_PPL
, &rdev
->mddev
->flags
) &&
3594 rdev
->raid_disk
>= 0)
3597 if (rdev
->mddev
->persistent
) {
3598 if (rdev
->mddev
->major_version
== 0)
3602 } else if (!rdev
->mddev
->external
) {
3605 rdev
->ppl
.size
= size
;
3609 static struct rdev_sysfs_entry rdev_ppl_size
=
3610 __ATTR(ppl_size
, S_IRUGO
|S_IWUSR
, ppl_size_show
, ppl_size_store
);
3612 static struct attribute
*rdev_default_attrs
[] = {
3617 &rdev_new_offset
.attr
,
3619 &rdev_recovery_start
.attr
,
3620 &rdev_bad_blocks
.attr
,
3621 &rdev_unack_bad_blocks
.attr
,
3622 &rdev_ppl_sector
.attr
,
3623 &rdev_ppl_size
.attr
,
3627 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
3629 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3630 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3636 return entry
->show(rdev
, page
);
3640 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
3641 const char *page
, size_t length
)
3643 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3644 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3646 struct mddev
*mddev
= rdev
->mddev
;
3650 if (!capable(CAP_SYS_ADMIN
))
3652 rv
= mddev
? mddev_lock(mddev
) : -ENODEV
;
3654 if (rdev
->mddev
== NULL
)
3657 rv
= entry
->store(rdev
, page
, length
);
3658 mddev_unlock(mddev
);
3663 static void rdev_free(struct kobject
*ko
)
3665 struct md_rdev
*rdev
= container_of(ko
, struct md_rdev
, kobj
);
3668 static const struct sysfs_ops rdev_sysfs_ops
= {
3669 .show
= rdev_attr_show
,
3670 .store
= rdev_attr_store
,
3672 static struct kobj_type rdev_ktype
= {
3673 .release
= rdev_free
,
3674 .sysfs_ops
= &rdev_sysfs_ops
,
3675 .default_attrs
= rdev_default_attrs
,
3678 int md_rdev_init(struct md_rdev
*rdev
)
3681 rdev
->saved_raid_disk
= -1;
3682 rdev
->raid_disk
= -1;
3684 rdev
->data_offset
= 0;
3685 rdev
->new_data_offset
= 0;
3686 rdev
->sb_events
= 0;
3687 rdev
->last_read_error
= 0;
3688 rdev
->sb_loaded
= 0;
3689 rdev
->bb_page
= NULL
;
3690 atomic_set(&rdev
->nr_pending
, 0);
3691 atomic_set(&rdev
->read_errors
, 0);
3692 atomic_set(&rdev
->corrected_errors
, 0);
3694 INIT_LIST_HEAD(&rdev
->same_set
);
3695 init_waitqueue_head(&rdev
->blocked_wait
);
3697 /* Add space to store bad block list.
3698 * This reserves the space even on arrays where it cannot
3699 * be used - I wonder if that matters
3701 return badblocks_init(&rdev
->badblocks
, 0);
3703 EXPORT_SYMBOL_GPL(md_rdev_init
);
3705 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3707 * mark the device faulty if:
3709 * - the device is nonexistent (zero size)
3710 * - the device has no valid superblock
3712 * a faulty rdev _never_ has rdev->sb set.
3714 static struct md_rdev
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
3716 char b
[BDEVNAME_SIZE
];
3718 struct md_rdev
*rdev
;
3721 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
3723 return ERR_PTR(-ENOMEM
);
3725 err
= md_rdev_init(rdev
);
3728 err
= alloc_disk_sb(rdev
);
3732 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
3736 kobject_init(&rdev
->kobj
, &rdev_ktype
);
3738 size
= i_size_read(rdev
->bdev
->bd_inode
) >> BLOCK_SIZE_BITS
;
3740 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
3741 bdevname(rdev
->bdev
,b
));
3746 if (super_format
>= 0) {
3747 err
= super_types
[super_format
].
3748 load_super(rdev
, NULL
, super_minor
);
3749 if (err
== -EINVAL
) {
3750 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
3751 bdevname(rdev
->bdev
,b
),
3752 super_format
, super_minor
);
3756 pr_warn("md: could not read %s's sb, not importing!\n",
3757 bdevname(rdev
->bdev
,b
));
3767 md_rdev_clear(rdev
);
3769 return ERR_PTR(err
);
3773 * Check a full RAID array for plausibility
3776 static int analyze_sbs(struct mddev
*mddev
)
3779 struct md_rdev
*rdev
, *freshest
, *tmp
;
3780 char b
[BDEVNAME_SIZE
];
3783 rdev_for_each_safe(rdev
, tmp
, mddev
)
3784 switch (super_types
[mddev
->major_version
].
3785 load_super(rdev
, freshest
, mddev
->minor_version
)) {
3792 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
3793 bdevname(rdev
->bdev
,b
));
3794 md_kick_rdev_from_array(rdev
);
3797 /* Cannot find a valid fresh disk */
3799 pr_warn("md: cannot find a valid disk\n");
3803 super_types
[mddev
->major_version
].
3804 validate_super(mddev
, freshest
);
3807 rdev_for_each_safe(rdev
, tmp
, mddev
) {
3808 if (mddev
->max_disks
&&
3809 (rdev
->desc_nr
>= mddev
->max_disks
||
3810 i
> mddev
->max_disks
)) {
3811 pr_warn("md: %s: %s: only %d devices permitted\n",
3812 mdname(mddev
), bdevname(rdev
->bdev
, b
),
3814 md_kick_rdev_from_array(rdev
);
3817 if (rdev
!= freshest
) {
3818 if (super_types
[mddev
->major_version
].
3819 validate_super(mddev
, rdev
)) {
3820 pr_warn("md: kicking non-fresh %s from array!\n",
3821 bdevname(rdev
->bdev
,b
));
3822 md_kick_rdev_from_array(rdev
);
3826 if (mddev
->level
== LEVEL_MULTIPATH
) {
3827 rdev
->desc_nr
= i
++;
3828 rdev
->raid_disk
= rdev
->desc_nr
;
3829 set_bit(In_sync
, &rdev
->flags
);
3830 } else if (rdev
->raid_disk
>=
3831 (mddev
->raid_disks
- min(0, mddev
->delta_disks
)) &&
3832 !test_bit(Journal
, &rdev
->flags
)) {
3833 rdev
->raid_disk
= -1;
3834 clear_bit(In_sync
, &rdev
->flags
);
3841 /* Read a fixed-point number.
3842 * Numbers in sysfs attributes should be in "standard" units where
3843 * possible, so time should be in seconds.
3844 * However we internally use a a much smaller unit such as
3845 * milliseconds or jiffies.
3846 * This function takes a decimal number with a possible fractional
3847 * component, and produces an integer which is the result of
3848 * multiplying that number by 10^'scale'.
3849 * all without any floating-point arithmetic.
3851 int strict_strtoul_scaled(const char *cp
, unsigned long *res
, int scale
)
3853 unsigned long result
= 0;
3855 while (isdigit(*cp
) || (*cp
== '.' && decimals
< 0)) {
3858 else if (decimals
< scale
) {
3861 result
= result
* 10 + value
;
3873 *res
= result
* int_pow(10, scale
- decimals
);
3878 safe_delay_show(struct mddev
*mddev
, char *page
)
3880 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
3881 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
3884 safe_delay_store(struct mddev
*mddev
, const char *cbuf
, size_t len
)
3888 if (mddev_is_clustered(mddev
)) {
3889 pr_warn("md: Safemode is disabled for clustered mode\n");
3893 if (strict_strtoul_scaled(cbuf
, &msec
, 3) < 0)
3896 mddev
->safemode_delay
= 0;
3898 unsigned long old_delay
= mddev
->safemode_delay
;
3899 unsigned long new_delay
= (msec
*HZ
)/1000;
3903 mddev
->safemode_delay
= new_delay
;
3904 if (new_delay
< old_delay
|| old_delay
== 0)
3905 mod_timer(&mddev
->safemode_timer
, jiffies
+1);
3909 static struct md_sysfs_entry md_safe_delay
=
3910 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
3913 level_show(struct mddev
*mddev
, char *page
)
3915 struct md_personality
*p
;
3917 spin_lock(&mddev
->lock
);
3920 ret
= sprintf(page
, "%s\n", p
->name
);
3921 else if (mddev
->clevel
[0])
3922 ret
= sprintf(page
, "%s\n", mddev
->clevel
);
3923 else if (mddev
->level
!= LEVEL_NONE
)
3924 ret
= sprintf(page
, "%d\n", mddev
->level
);
3927 spin_unlock(&mddev
->lock
);
3932 level_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3937 struct md_personality
*pers
, *oldpers
;
3939 void *priv
, *oldpriv
;
3940 struct md_rdev
*rdev
;
3942 if (slen
== 0 || slen
>= sizeof(clevel
))
3945 rv
= mddev_lock(mddev
);
3949 if (mddev
->pers
== NULL
) {
3950 strncpy(mddev
->clevel
, buf
, slen
);
3951 if (mddev
->clevel
[slen
-1] == '\n')
3953 mddev
->clevel
[slen
] = 0;
3954 mddev
->level
= LEVEL_NONE
;
3962 /* request to change the personality. Need to ensure:
3963 * - array is not engaged in resync/recovery/reshape
3964 * - old personality can be suspended
3965 * - new personality will access other array.
3969 if (mddev
->sync_thread
||
3970 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3971 mddev
->reshape_position
!= MaxSector
||
3972 mddev
->sysfs_active
)
3976 if (!mddev
->pers
->quiesce
) {
3977 pr_warn("md: %s: %s does not support online personality change\n",
3978 mdname(mddev
), mddev
->pers
->name
);
3982 /* Now find the new personality */
3983 strncpy(clevel
, buf
, slen
);
3984 if (clevel
[slen
-1] == '\n')
3987 if (kstrtol(clevel
, 10, &level
))
3990 if (request_module("md-%s", clevel
) != 0)
3991 request_module("md-level-%s", clevel
);
3992 spin_lock(&pers_lock
);
3993 pers
= find_pers(level
, clevel
);
3994 if (!pers
|| !try_module_get(pers
->owner
)) {
3995 spin_unlock(&pers_lock
);
3996 pr_warn("md: personality %s not loaded\n", clevel
);
4000 spin_unlock(&pers_lock
);
4002 if (pers
== mddev
->pers
) {
4003 /* Nothing to do! */
4004 module_put(pers
->owner
);
4008 if (!pers
->takeover
) {
4009 module_put(pers
->owner
);
4010 pr_warn("md: %s: %s does not support personality takeover\n",
4011 mdname(mddev
), clevel
);
4016 rdev_for_each(rdev
, mddev
)
4017 rdev
->new_raid_disk
= rdev
->raid_disk
;
4019 /* ->takeover must set new_* and/or delta_disks
4020 * if it succeeds, and may set them when it fails.
4022 priv
= pers
->takeover(mddev
);
4024 mddev
->new_level
= mddev
->level
;
4025 mddev
->new_layout
= mddev
->layout
;
4026 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4027 mddev
->raid_disks
-= mddev
->delta_disks
;
4028 mddev
->delta_disks
= 0;
4029 mddev
->reshape_backwards
= 0;
4030 module_put(pers
->owner
);
4031 pr_warn("md: %s: %s would not accept array\n",
4032 mdname(mddev
), clevel
);
4037 /* Looks like we have a winner */
4038 mddev_suspend(mddev
);
4039 mddev_detach(mddev
);
4041 spin_lock(&mddev
->lock
);
4042 oldpers
= mddev
->pers
;
4043 oldpriv
= mddev
->private;
4045 mddev
->private = priv
;
4046 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
4047 mddev
->level
= mddev
->new_level
;
4048 mddev
->layout
= mddev
->new_layout
;
4049 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
4050 mddev
->delta_disks
= 0;
4051 mddev
->reshape_backwards
= 0;
4052 mddev
->degraded
= 0;
4053 spin_unlock(&mddev
->lock
);
4055 if (oldpers
->sync_request
== NULL
&&
4057 /* We are converting from a no-redundancy array
4058 * to a redundancy array and metadata is managed
4059 * externally so we need to be sure that writes
4060 * won't block due to a need to transition
4062 * until external management is started.
4065 mddev
->safemode_delay
= 0;
4066 mddev
->safemode
= 0;
4069 oldpers
->free(mddev
, oldpriv
);
4071 if (oldpers
->sync_request
== NULL
&&
4072 pers
->sync_request
!= NULL
) {
4073 /* need to add the md_redundancy_group */
4074 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
4075 pr_warn("md: cannot register extra attributes for %s\n",
4077 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, "sync_action");
4078 mddev
->sysfs_completed
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "sync_completed");
4079 mddev
->sysfs_degraded
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "degraded");
4081 if (oldpers
->sync_request
!= NULL
&&
4082 pers
->sync_request
== NULL
) {
4083 /* need to remove the md_redundancy_group */
4084 if (mddev
->to_remove
== NULL
)
4085 mddev
->to_remove
= &md_redundancy_group
;
4088 module_put(oldpers
->owner
);
4090 rdev_for_each(rdev
, mddev
) {
4091 if (rdev
->raid_disk
< 0)
4093 if (rdev
->new_raid_disk
>= mddev
->raid_disks
)
4094 rdev
->new_raid_disk
= -1;
4095 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
4097 sysfs_unlink_rdev(mddev
, rdev
);
4099 rdev_for_each(rdev
, mddev
) {
4100 if (rdev
->raid_disk
< 0)
4102 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
4104 rdev
->raid_disk
= rdev
->new_raid_disk
;
4105 if (rdev
->raid_disk
< 0)
4106 clear_bit(In_sync
, &rdev
->flags
);
4108 if (sysfs_link_rdev(mddev
, rdev
))
4109 pr_warn("md: cannot register rd%d for %s after level change\n",
4110 rdev
->raid_disk
, mdname(mddev
));
4114 if (pers
->sync_request
== NULL
) {
4115 /* this is now an array without redundancy, so
4116 * it must always be in_sync
4119 del_timer_sync(&mddev
->safemode_timer
);
4121 blk_set_stacking_limits(&mddev
->queue
->limits
);
4123 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
4124 mddev_resume(mddev
);
4126 md_update_sb(mddev
, 1);
4127 sysfs_notify_dirent_safe(mddev
->sysfs_level
);
4128 md_new_event(mddev
);
4131 mddev_unlock(mddev
);
4135 static struct md_sysfs_entry md_level
=
4136 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
4139 layout_show(struct mddev
*mddev
, char *page
)
4141 /* just a number, not meaningful for all levels */
4142 if (mddev
->reshape_position
!= MaxSector
&&
4143 mddev
->layout
!= mddev
->new_layout
)
4144 return sprintf(page
, "%d (%d)\n",
4145 mddev
->new_layout
, mddev
->layout
);
4146 return sprintf(page
, "%d\n", mddev
->layout
);
4150 layout_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4155 err
= kstrtouint(buf
, 10, &n
);
4158 err
= mddev_lock(mddev
);
4163 if (mddev
->pers
->check_reshape
== NULL
)
4168 mddev
->new_layout
= n
;
4169 err
= mddev
->pers
->check_reshape(mddev
);
4171 mddev
->new_layout
= mddev
->layout
;
4174 mddev
->new_layout
= n
;
4175 if (mddev
->reshape_position
== MaxSector
)
4178 mddev_unlock(mddev
);
4181 static struct md_sysfs_entry md_layout
=
4182 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
4185 raid_disks_show(struct mddev
*mddev
, char *page
)
4187 if (mddev
->raid_disks
== 0)
4189 if (mddev
->reshape_position
!= MaxSector
&&
4190 mddev
->delta_disks
!= 0)
4191 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
4192 mddev
->raid_disks
- mddev
->delta_disks
);
4193 return sprintf(page
, "%d\n", mddev
->raid_disks
);
4196 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
);
4199 raid_disks_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4204 err
= kstrtouint(buf
, 10, &n
);
4208 err
= mddev_lock(mddev
);
4212 err
= update_raid_disks(mddev
, n
);
4213 else if (mddev
->reshape_position
!= MaxSector
) {
4214 struct md_rdev
*rdev
;
4215 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
4218 rdev_for_each(rdev
, mddev
) {
4220 rdev
->data_offset
< rdev
->new_data_offset
)
4223 rdev
->data_offset
> rdev
->new_data_offset
)
4227 mddev
->delta_disks
= n
- olddisks
;
4228 mddev
->raid_disks
= n
;
4229 mddev
->reshape_backwards
= (mddev
->delta_disks
< 0);
4231 mddev
->raid_disks
= n
;
4233 mddev_unlock(mddev
);
4234 return err
? err
: len
;
4236 static struct md_sysfs_entry md_raid_disks
=
4237 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
4240 uuid_show(struct mddev
*mddev
, char *page
)
4242 return sprintf(page
, "%pU\n", mddev
->uuid
);
4244 static struct md_sysfs_entry md_uuid
=
4245 __ATTR(uuid
, S_IRUGO
, uuid_show
, NULL
);
4248 chunk_size_show(struct mddev
*mddev
, char *page
)
4250 if (mddev
->reshape_position
!= MaxSector
&&
4251 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
4252 return sprintf(page
, "%d (%d)\n",
4253 mddev
->new_chunk_sectors
<< 9,
4254 mddev
->chunk_sectors
<< 9);
4255 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
4259 chunk_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4264 err
= kstrtoul(buf
, 10, &n
);
4268 err
= mddev_lock(mddev
);
4272 if (mddev
->pers
->check_reshape
== NULL
)
4277 mddev
->new_chunk_sectors
= n
>> 9;
4278 err
= mddev
->pers
->check_reshape(mddev
);
4280 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4283 mddev
->new_chunk_sectors
= n
>> 9;
4284 if (mddev
->reshape_position
== MaxSector
)
4285 mddev
->chunk_sectors
= n
>> 9;
4287 mddev_unlock(mddev
);
4290 static struct md_sysfs_entry md_chunk_size
=
4291 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
4294 resync_start_show(struct mddev
*mddev
, char *page
)
4296 if (mddev
->recovery_cp
== MaxSector
)
4297 return sprintf(page
, "none\n");
4298 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
4302 resync_start_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4304 unsigned long long n
;
4307 if (cmd_match(buf
, "none"))
4310 err
= kstrtoull(buf
, 10, &n
);
4313 if (n
!= (sector_t
)n
)
4317 err
= mddev_lock(mddev
);
4320 if (mddev
->pers
&& !test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
4324 mddev
->recovery_cp
= n
;
4326 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
4328 mddev_unlock(mddev
);
4331 static struct md_sysfs_entry md_resync_start
=
4332 __ATTR_PREALLOC(resync_start
, S_IRUGO
|S_IWUSR
,
4333 resync_start_show
, resync_start_store
);
4336 * The array state can be:
4339 * No devices, no size, no level
4340 * Equivalent to STOP_ARRAY ioctl
4342 * May have some settings, but array is not active
4343 * all IO results in error
4344 * When written, doesn't tear down array, but just stops it
4345 * suspended (not supported yet)
4346 * All IO requests will block. The array can be reconfigured.
4347 * Writing this, if accepted, will block until array is quiescent
4349 * no resync can happen. no superblocks get written.
4350 * write requests fail
4352 * like readonly, but behaves like 'clean' on a write request.
4354 * clean - no pending writes, but otherwise active.
4355 * When written to inactive array, starts without resync
4356 * If a write request arrives then
4357 * if metadata is known, mark 'dirty' and switch to 'active'.
4358 * if not known, block and switch to write-pending
4359 * If written to an active array that has pending writes, then fails.
4361 * fully active: IO and resync can be happening.
4362 * When written to inactive array, starts with resync
4365 * clean, but writes are blocked waiting for 'active' to be written.
4368 * like active, but no writes have been seen for a while (100msec).
4371 * RAID0/LINEAR-only: same as clean, but array is missing a member.
4372 * It's useful because RAID0/LINEAR mounted-arrays aren't stopped
4373 * when a member is gone, so this state will at least alert the
4374 * user that something is wrong.
4376 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
4377 write_pending
, active_idle
, broken
, bad_word
};
4378 static char *array_states
[] = {
4379 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
4380 "write-pending", "active-idle", "broken", NULL
};
4382 static int match_word(const char *word
, char **list
)
4385 for (n
=0; list
[n
]; n
++)
4386 if (cmd_match(word
, list
[n
]))
4392 array_state_show(struct mddev
*mddev
, char *page
)
4394 enum array_state st
= inactive
;
4396 if (mddev
->pers
&& !test_bit(MD_NOT_READY
, &mddev
->flags
)) {
4405 spin_lock(&mddev
->lock
);
4406 if (test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
))
4408 else if (mddev
->in_sync
)
4410 else if (mddev
->safemode
)
4414 spin_unlock(&mddev
->lock
);
4417 if (test_bit(MD_BROKEN
, &mddev
->flags
) && st
== clean
)
4420 if (list_empty(&mddev
->disks
) &&
4421 mddev
->raid_disks
== 0 &&
4422 mddev
->dev_sectors
== 0)
4427 return sprintf(page
, "%s\n", array_states
[st
]);
4430 static int do_md_stop(struct mddev
*mddev
, int ro
, struct block_device
*bdev
);
4431 static int md_set_readonly(struct mddev
*mddev
, struct block_device
*bdev
);
4432 static int restart_array(struct mddev
*mddev
);
4435 array_state_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4438 enum array_state st
= match_word(buf
, array_states
);
4440 if (mddev
->pers
&& (st
== active
|| st
== clean
) && mddev
->ro
!= 1) {
4441 /* don't take reconfig_mutex when toggling between
4444 spin_lock(&mddev
->lock
);
4446 restart_array(mddev
);
4447 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
4448 md_wakeup_thread(mddev
->thread
);
4449 wake_up(&mddev
->sb_wait
);
4450 } else /* st == clean */ {
4451 restart_array(mddev
);
4452 if (!set_in_sync(mddev
))
4456 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4457 spin_unlock(&mddev
->lock
);
4460 err
= mddev_lock(mddev
);
4468 /* stopping an active array */
4469 err
= do_md_stop(mddev
, 0, NULL
);
4472 /* stopping an active array */
4474 err
= do_md_stop(mddev
, 2, NULL
);
4476 err
= 0; /* already inactive */
4479 break; /* not supported yet */
4482 err
= md_set_readonly(mddev
, NULL
);
4485 set_disk_ro(mddev
->gendisk
, 1);
4486 err
= do_md_run(mddev
);
4492 err
= md_set_readonly(mddev
, NULL
);
4493 else if (mddev
->ro
== 1)
4494 err
= restart_array(mddev
);
4497 set_disk_ro(mddev
->gendisk
, 0);
4501 err
= do_md_run(mddev
);
4506 err
= restart_array(mddev
);
4509 spin_lock(&mddev
->lock
);
4510 if (!set_in_sync(mddev
))
4512 spin_unlock(&mddev
->lock
);
4518 err
= restart_array(mddev
);
4521 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
4522 wake_up(&mddev
->sb_wait
);
4526 set_disk_ro(mddev
->gendisk
, 0);
4527 err
= do_md_run(mddev
);
4533 /* these cannot be set */
4538 if (mddev
->hold_active
== UNTIL_IOCTL
)
4539 mddev
->hold_active
= 0;
4540 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4542 mddev_unlock(mddev
);
4545 static struct md_sysfs_entry md_array_state
=
4546 __ATTR_PREALLOC(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
4549 max_corrected_read_errors_show(struct mddev
*mddev
, char *page
) {
4550 return sprintf(page
, "%d\n",
4551 atomic_read(&mddev
->max_corr_read_errors
));
4555 max_corrected_read_errors_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4560 rv
= kstrtouint(buf
, 10, &n
);
4563 atomic_set(&mddev
->max_corr_read_errors
, n
);
4567 static struct md_sysfs_entry max_corr_read_errors
=
4568 __ATTR(max_read_errors
, S_IRUGO
|S_IWUSR
, max_corrected_read_errors_show
,
4569 max_corrected_read_errors_store
);
4572 null_show(struct mddev
*mddev
, char *page
)
4577 /* need to ensure rdev_delayed_delete() has completed */
4578 static void flush_rdev_wq(struct mddev
*mddev
)
4580 struct md_rdev
*rdev
;
4583 rdev_for_each_rcu(rdev
, mddev
)
4584 if (work_pending(&rdev
->del_work
)) {
4585 flush_workqueue(md_rdev_misc_wq
);
4592 new_dev_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4594 /* buf must be %d:%d\n? giving major and minor numbers */
4595 /* The new device is added to the array.
4596 * If the array has a persistent superblock, we read the
4597 * superblock to initialise info and check validity.
4598 * Otherwise, only checking done is that in bind_rdev_to_array,
4599 * which mainly checks size.
4602 int major
= simple_strtoul(buf
, &e
, 10);
4605 struct md_rdev
*rdev
;
4608 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
4610 minor
= simple_strtoul(e
+1, &e
, 10);
4611 if (*e
&& *e
!= '\n')
4613 dev
= MKDEV(major
, minor
);
4614 if (major
!= MAJOR(dev
) ||
4615 minor
!= MINOR(dev
))
4618 flush_rdev_wq(mddev
);
4619 err
= mddev_lock(mddev
);
4622 if (mddev
->persistent
) {
4623 rdev
= md_import_device(dev
, mddev
->major_version
,
4624 mddev
->minor_version
);
4625 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
4626 struct md_rdev
*rdev0
4627 = list_entry(mddev
->disks
.next
,
4628 struct md_rdev
, same_set
);
4629 err
= super_types
[mddev
->major_version
]
4630 .load_super(rdev
, rdev0
, mddev
->minor_version
);
4634 } else if (mddev
->external
)
4635 rdev
= md_import_device(dev
, -2, -1);
4637 rdev
= md_import_device(dev
, -1, -1);
4640 mddev_unlock(mddev
);
4641 return PTR_ERR(rdev
);
4643 err
= bind_rdev_to_array(rdev
, mddev
);
4647 mddev_unlock(mddev
);
4649 md_new_event(mddev
);
4650 return err
? err
: len
;
4653 static struct md_sysfs_entry md_new_device
=
4654 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
4657 bitmap_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4660 unsigned long chunk
, end_chunk
;
4663 err
= mddev_lock(mddev
);
4668 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4670 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
4671 if (buf
== end
) break;
4672 if (*end
== '-') { /* range */
4674 end_chunk
= simple_strtoul(buf
, &end
, 0);
4675 if (buf
== end
) break;
4677 if (*end
&& !isspace(*end
)) break;
4678 md_bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
4679 buf
= skip_spaces(end
);
4681 md_bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
4683 mddev_unlock(mddev
);
4687 static struct md_sysfs_entry md_bitmap
=
4688 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
4691 size_show(struct mddev
*mddev
, char *page
)
4693 return sprintf(page
, "%llu\n",
4694 (unsigned long long)mddev
->dev_sectors
/ 2);
4697 static int update_size(struct mddev
*mddev
, sector_t num_sectors
);
4700 size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4702 /* If array is inactive, we can reduce the component size, but
4703 * not increase it (except from 0).
4704 * If array is active, we can try an on-line resize
4707 int err
= strict_blocks_to_sectors(buf
, §ors
);
4711 err
= mddev_lock(mddev
);
4715 err
= update_size(mddev
, sectors
);
4717 md_update_sb(mddev
, 1);
4719 if (mddev
->dev_sectors
== 0 ||
4720 mddev
->dev_sectors
> sectors
)
4721 mddev
->dev_sectors
= sectors
;
4725 mddev_unlock(mddev
);
4726 return err
? err
: len
;
4729 static struct md_sysfs_entry md_size
=
4730 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
4732 /* Metadata version.
4734 * 'none' for arrays with no metadata (good luck...)
4735 * 'external' for arrays with externally managed metadata,
4736 * or N.M for internally known formats
4739 metadata_show(struct mddev
*mddev
, char *page
)
4741 if (mddev
->persistent
)
4742 return sprintf(page
, "%d.%d\n",
4743 mddev
->major_version
, mddev
->minor_version
);
4744 else if (mddev
->external
)
4745 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
4747 return sprintf(page
, "none\n");
4751 metadata_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4756 /* Changing the details of 'external' metadata is
4757 * always permitted. Otherwise there must be
4758 * no devices attached to the array.
4761 err
= mddev_lock(mddev
);
4765 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
4767 else if (!list_empty(&mddev
->disks
))
4771 if (cmd_match(buf
, "none")) {
4772 mddev
->persistent
= 0;
4773 mddev
->external
= 0;
4774 mddev
->major_version
= 0;
4775 mddev
->minor_version
= 90;
4778 if (strncmp(buf
, "external:", 9) == 0) {
4779 size_t namelen
= len
-9;
4780 if (namelen
>= sizeof(mddev
->metadata_type
))
4781 namelen
= sizeof(mddev
->metadata_type
)-1;
4782 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
4783 mddev
->metadata_type
[namelen
] = 0;
4784 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
4785 mddev
->metadata_type
[--namelen
] = 0;
4786 mddev
->persistent
= 0;
4787 mddev
->external
= 1;
4788 mddev
->major_version
= 0;
4789 mddev
->minor_version
= 90;
4792 major
= simple_strtoul(buf
, &e
, 10);
4794 if (e
==buf
|| *e
!= '.')
4797 minor
= simple_strtoul(buf
, &e
, 10);
4798 if (e
==buf
|| (*e
&& *e
!= '\n') )
4801 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
4803 mddev
->major_version
= major
;
4804 mddev
->minor_version
= minor
;
4805 mddev
->persistent
= 1;
4806 mddev
->external
= 0;
4809 mddev_unlock(mddev
);
4813 static struct md_sysfs_entry md_metadata
=
4814 __ATTR_PREALLOC(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
4817 action_show(struct mddev
*mddev
, char *page
)
4819 char *type
= "idle";
4820 unsigned long recovery
= mddev
->recovery
;
4821 if (test_bit(MD_RECOVERY_FROZEN
, &recovery
))
4823 else if (test_bit(MD_RECOVERY_RUNNING
, &recovery
) ||
4824 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &recovery
))) {
4825 if (test_bit(MD_RECOVERY_RESHAPE
, &recovery
))
4827 else if (test_bit(MD_RECOVERY_SYNC
, &recovery
)) {
4828 if (!test_bit(MD_RECOVERY_REQUESTED
, &recovery
))
4830 else if (test_bit(MD_RECOVERY_CHECK
, &recovery
))
4834 } else if (test_bit(MD_RECOVERY_RECOVER
, &recovery
))
4836 else if (mddev
->reshape_position
!= MaxSector
)
4839 return sprintf(page
, "%s\n", type
);
4843 action_store(struct mddev
*mddev
, const char *page
, size_t len
)
4845 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
4849 if (cmd_match(page
, "idle") || cmd_match(page
, "frozen")) {
4850 if (cmd_match(page
, "frozen"))
4851 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4853 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4854 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
4855 mddev_lock(mddev
) == 0) {
4856 if (work_pending(&mddev
->del_work
))
4857 flush_workqueue(md_misc_wq
);
4858 if (mddev
->sync_thread
) {
4859 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4860 md_reap_sync_thread(mddev
);
4862 mddev_unlock(mddev
);
4864 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4866 else if (cmd_match(page
, "resync"))
4867 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4868 else if (cmd_match(page
, "recover")) {
4869 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4870 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
4871 } else if (cmd_match(page
, "reshape")) {
4873 if (mddev
->pers
->start_reshape
== NULL
)
4875 err
= mddev_lock(mddev
);
4877 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4880 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4881 err
= mddev
->pers
->start_reshape(mddev
);
4883 mddev_unlock(mddev
);
4887 sysfs_notify_dirent_safe(mddev
->sysfs_degraded
);
4889 if (cmd_match(page
, "check"))
4890 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
4891 else if (!cmd_match(page
, "repair"))
4893 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4894 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
4895 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
4897 if (mddev
->ro
== 2) {
4898 /* A write to sync_action is enough to justify
4899 * canceling read-auto mode
4902 md_wakeup_thread(mddev
->sync_thread
);
4904 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4905 md_wakeup_thread(mddev
->thread
);
4906 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4910 static struct md_sysfs_entry md_scan_mode
=
4911 __ATTR_PREALLOC(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
4914 last_sync_action_show(struct mddev
*mddev
, char *page
)
4916 return sprintf(page
, "%s\n", mddev
->last_sync_action
);
4919 static struct md_sysfs_entry md_last_scan_mode
= __ATTR_RO(last_sync_action
);
4922 mismatch_cnt_show(struct mddev
*mddev
, char *page
)
4924 return sprintf(page
, "%llu\n",
4925 (unsigned long long)
4926 atomic64_read(&mddev
->resync_mismatches
));
4929 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
4932 sync_min_show(struct mddev
*mddev
, char *page
)
4934 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
4935 mddev
->sync_speed_min
? "local": "system");
4939 sync_min_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4944 if (strncmp(buf
, "system", 6)==0) {
4947 rv
= kstrtouint(buf
, 10, &min
);
4953 mddev
->sync_speed_min
= min
;
4957 static struct md_sysfs_entry md_sync_min
=
4958 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
4961 sync_max_show(struct mddev
*mddev
, char *page
)
4963 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
4964 mddev
->sync_speed_max
? "local": "system");
4968 sync_max_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4973 if (strncmp(buf
, "system", 6)==0) {
4976 rv
= kstrtouint(buf
, 10, &max
);
4982 mddev
->sync_speed_max
= max
;
4986 static struct md_sysfs_entry md_sync_max
=
4987 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
4990 degraded_show(struct mddev
*mddev
, char *page
)
4992 return sprintf(page
, "%d\n", mddev
->degraded
);
4994 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
4997 sync_force_parallel_show(struct mddev
*mddev
, char *page
)
4999 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
5003 sync_force_parallel_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5007 if (kstrtol(buf
, 10, &n
))
5010 if (n
!= 0 && n
!= 1)
5013 mddev
->parallel_resync
= n
;
5015 if (mddev
->sync_thread
)
5016 wake_up(&resync_wait
);
5021 /* force parallel resync, even with shared block devices */
5022 static struct md_sysfs_entry md_sync_force_parallel
=
5023 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
5024 sync_force_parallel_show
, sync_force_parallel_store
);
5027 sync_speed_show(struct mddev
*mddev
, char *page
)
5029 unsigned long resync
, dt
, db
;
5030 if (mddev
->curr_resync
== 0)
5031 return sprintf(page
, "none\n");
5032 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
5033 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
5035 db
= resync
- mddev
->resync_mark_cnt
;
5036 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
5039 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
5042 sync_completed_show(struct mddev
*mddev
, char *page
)
5044 unsigned long long max_sectors
, resync
;
5046 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
5047 return sprintf(page
, "none\n");
5049 if (mddev
->curr_resync
== 1 ||
5050 mddev
->curr_resync
== 2)
5051 return sprintf(page
, "delayed\n");
5053 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
5054 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
5055 max_sectors
= mddev
->resync_max_sectors
;
5057 max_sectors
= mddev
->dev_sectors
;
5059 resync
= mddev
->curr_resync_completed
;
5060 return sprintf(page
, "%llu / %llu\n", resync
, max_sectors
);
5063 static struct md_sysfs_entry md_sync_completed
=
5064 __ATTR_PREALLOC(sync_completed
, S_IRUGO
, sync_completed_show
, NULL
);
5067 min_sync_show(struct mddev
*mddev
, char *page
)
5069 return sprintf(page
, "%llu\n",
5070 (unsigned long long)mddev
->resync_min
);
5073 min_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5075 unsigned long long min
;
5078 if (kstrtoull(buf
, 10, &min
))
5081 spin_lock(&mddev
->lock
);
5083 if (min
> mddev
->resync_max
)
5087 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
5090 /* Round down to multiple of 4K for safety */
5091 mddev
->resync_min
= round_down(min
, 8);
5095 spin_unlock(&mddev
->lock
);
5099 static struct md_sysfs_entry md_min_sync
=
5100 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
5103 max_sync_show(struct mddev
*mddev
, char *page
)
5105 if (mddev
->resync_max
== MaxSector
)
5106 return sprintf(page
, "max\n");
5108 return sprintf(page
, "%llu\n",
5109 (unsigned long long)mddev
->resync_max
);
5112 max_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5115 spin_lock(&mddev
->lock
);
5116 if (strncmp(buf
, "max", 3) == 0)
5117 mddev
->resync_max
= MaxSector
;
5119 unsigned long long max
;
5123 if (kstrtoull(buf
, 10, &max
))
5125 if (max
< mddev
->resync_min
)
5129 if (max
< mddev
->resync_max
&&
5131 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
5134 /* Must be a multiple of chunk_size */
5135 chunk
= mddev
->chunk_sectors
;
5137 sector_t temp
= max
;
5140 if (sector_div(temp
, chunk
))
5143 mddev
->resync_max
= max
;
5145 wake_up(&mddev
->recovery_wait
);
5148 spin_unlock(&mddev
->lock
);
5152 static struct md_sysfs_entry md_max_sync
=
5153 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
5156 suspend_lo_show(struct mddev
*mddev
, char *page
)
5158 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
5162 suspend_lo_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5164 unsigned long long new;
5167 err
= kstrtoull(buf
, 10, &new);
5170 if (new != (sector_t
)new)
5173 err
= mddev_lock(mddev
);
5177 if (mddev
->pers
== NULL
||
5178 mddev
->pers
->quiesce
== NULL
)
5180 mddev_suspend(mddev
);
5181 mddev
->suspend_lo
= new;
5182 mddev_resume(mddev
);
5186 mddev_unlock(mddev
);
5189 static struct md_sysfs_entry md_suspend_lo
=
5190 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
5193 suspend_hi_show(struct mddev
*mddev
, char *page
)
5195 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
5199 suspend_hi_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5201 unsigned long long new;
5204 err
= kstrtoull(buf
, 10, &new);
5207 if (new != (sector_t
)new)
5210 err
= mddev_lock(mddev
);
5214 if (mddev
->pers
== NULL
)
5217 mddev_suspend(mddev
);
5218 mddev
->suspend_hi
= new;
5219 mddev_resume(mddev
);
5223 mddev_unlock(mddev
);
5226 static struct md_sysfs_entry md_suspend_hi
=
5227 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
5230 reshape_position_show(struct mddev
*mddev
, char *page
)
5232 if (mddev
->reshape_position
!= MaxSector
)
5233 return sprintf(page
, "%llu\n",
5234 (unsigned long long)mddev
->reshape_position
);
5235 strcpy(page
, "none\n");
5240 reshape_position_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5242 struct md_rdev
*rdev
;
5243 unsigned long long new;
5246 err
= kstrtoull(buf
, 10, &new);
5249 if (new != (sector_t
)new)
5251 err
= mddev_lock(mddev
);
5257 mddev
->reshape_position
= new;
5258 mddev
->delta_disks
= 0;
5259 mddev
->reshape_backwards
= 0;
5260 mddev
->new_level
= mddev
->level
;
5261 mddev
->new_layout
= mddev
->layout
;
5262 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
5263 rdev_for_each(rdev
, mddev
)
5264 rdev
->new_data_offset
= rdev
->data_offset
;
5267 mddev_unlock(mddev
);
5271 static struct md_sysfs_entry md_reshape_position
=
5272 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
5273 reshape_position_store
);
5276 reshape_direction_show(struct mddev
*mddev
, char *page
)
5278 return sprintf(page
, "%s\n",
5279 mddev
->reshape_backwards
? "backwards" : "forwards");
5283 reshape_direction_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5288 if (cmd_match(buf
, "forwards"))
5290 else if (cmd_match(buf
, "backwards"))
5294 if (mddev
->reshape_backwards
== backwards
)
5297 err
= mddev_lock(mddev
);
5300 /* check if we are allowed to change */
5301 if (mddev
->delta_disks
)
5303 else if (mddev
->persistent
&&
5304 mddev
->major_version
== 0)
5307 mddev
->reshape_backwards
= backwards
;
5308 mddev_unlock(mddev
);
5312 static struct md_sysfs_entry md_reshape_direction
=
5313 __ATTR(reshape_direction
, S_IRUGO
|S_IWUSR
, reshape_direction_show
,
5314 reshape_direction_store
);
5317 array_size_show(struct mddev
*mddev
, char *page
)
5319 if (mddev
->external_size
)
5320 return sprintf(page
, "%llu\n",
5321 (unsigned long long)mddev
->array_sectors
/2);
5323 return sprintf(page
, "default\n");
5327 array_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5332 err
= mddev_lock(mddev
);
5336 /* cluster raid doesn't support change array_sectors */
5337 if (mddev_is_clustered(mddev
)) {
5338 mddev_unlock(mddev
);
5342 if (strncmp(buf
, "default", 7) == 0) {
5344 sectors
= mddev
->pers
->size(mddev
, 0, 0);
5346 sectors
= mddev
->array_sectors
;
5348 mddev
->external_size
= 0;
5350 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
5352 else if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
5355 mddev
->external_size
= 1;
5359 mddev
->array_sectors
= sectors
;
5361 set_capacity_and_notify(mddev
->gendisk
,
5362 mddev
->array_sectors
);
5364 mddev_unlock(mddev
);
5368 static struct md_sysfs_entry md_array_size
=
5369 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
5373 consistency_policy_show(struct mddev
*mddev
, char *page
)
5377 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
)) {
5378 ret
= sprintf(page
, "journal\n");
5379 } else if (test_bit(MD_HAS_PPL
, &mddev
->flags
)) {
5380 ret
= sprintf(page
, "ppl\n");
5381 } else if (mddev
->bitmap
) {
5382 ret
= sprintf(page
, "bitmap\n");
5383 } else if (mddev
->pers
) {
5384 if (mddev
->pers
->sync_request
)
5385 ret
= sprintf(page
, "resync\n");
5387 ret
= sprintf(page
, "none\n");
5389 ret
= sprintf(page
, "unknown\n");
5396 consistency_policy_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5401 if (mddev
->pers
->change_consistency_policy
)
5402 err
= mddev
->pers
->change_consistency_policy(mddev
, buf
);
5405 } else if (mddev
->external
&& strncmp(buf
, "ppl", 3) == 0) {
5406 set_bit(MD_HAS_PPL
, &mddev
->flags
);
5411 return err
? err
: len
;
5414 static struct md_sysfs_entry md_consistency_policy
=
5415 __ATTR(consistency_policy
, S_IRUGO
| S_IWUSR
, consistency_policy_show
,
5416 consistency_policy_store
);
5418 static ssize_t
fail_last_dev_show(struct mddev
*mddev
, char *page
)
5420 return sprintf(page
, "%d\n", mddev
->fail_last_dev
);
5424 * Setting fail_last_dev to true to allow last device to be forcibly removed
5425 * from RAID1/RAID10.
5428 fail_last_dev_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5433 ret
= kstrtobool(buf
, &value
);
5437 if (value
!= mddev
->fail_last_dev
)
5438 mddev
->fail_last_dev
= value
;
5442 static struct md_sysfs_entry md_fail_last_dev
=
5443 __ATTR(fail_last_dev
, S_IRUGO
| S_IWUSR
, fail_last_dev_show
,
5444 fail_last_dev_store
);
5446 static ssize_t
serialize_policy_show(struct mddev
*mddev
, char *page
)
5448 if (mddev
->pers
== NULL
|| (mddev
->pers
->level
!= 1))
5449 return sprintf(page
, "n/a\n");
5451 return sprintf(page
, "%d\n", mddev
->serialize_policy
);
5455 * Setting serialize_policy to true to enforce write IO is not reordered
5459 serialize_policy_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5464 err
= kstrtobool(buf
, &value
);
5468 if (value
== mddev
->serialize_policy
)
5471 err
= mddev_lock(mddev
);
5474 if (mddev
->pers
== NULL
|| (mddev
->pers
->level
!= 1)) {
5475 pr_err("md: serialize_policy is only effective for raid1\n");
5480 mddev_suspend(mddev
);
5482 mddev_create_serial_pool(mddev
, NULL
, true);
5484 mddev_destroy_serial_pool(mddev
, NULL
, true);
5485 mddev
->serialize_policy
= value
;
5486 mddev_resume(mddev
);
5488 mddev_unlock(mddev
);
5492 static struct md_sysfs_entry md_serialize_policy
=
5493 __ATTR(serialize_policy
, S_IRUGO
| S_IWUSR
, serialize_policy_show
,
5494 serialize_policy_store
);
5497 static struct attribute
*md_default_attrs
[] = {
5500 &md_raid_disks
.attr
,
5502 &md_chunk_size
.attr
,
5504 &md_resync_start
.attr
,
5506 &md_new_device
.attr
,
5507 &md_safe_delay
.attr
,
5508 &md_array_state
.attr
,
5509 &md_reshape_position
.attr
,
5510 &md_reshape_direction
.attr
,
5511 &md_array_size
.attr
,
5512 &max_corr_read_errors
.attr
,
5513 &md_consistency_policy
.attr
,
5514 &md_fail_last_dev
.attr
,
5515 &md_serialize_policy
.attr
,
5519 static struct attribute
*md_redundancy_attrs
[] = {
5521 &md_last_scan_mode
.attr
,
5522 &md_mismatches
.attr
,
5525 &md_sync_speed
.attr
,
5526 &md_sync_force_parallel
.attr
,
5527 &md_sync_completed
.attr
,
5530 &md_suspend_lo
.attr
,
5531 &md_suspend_hi
.attr
,
5536 static struct attribute_group md_redundancy_group
= {
5538 .attrs
= md_redundancy_attrs
,
5542 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
5544 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
5545 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
5550 spin_lock(&all_mddevs_lock
);
5551 if (list_empty(&mddev
->all_mddevs
)) {
5552 spin_unlock(&all_mddevs_lock
);
5556 spin_unlock(&all_mddevs_lock
);
5558 rv
= entry
->show(mddev
, page
);
5564 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
5565 const char *page
, size_t length
)
5567 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
5568 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
5573 if (!capable(CAP_SYS_ADMIN
))
5575 spin_lock(&all_mddevs_lock
);
5576 if (list_empty(&mddev
->all_mddevs
)) {
5577 spin_unlock(&all_mddevs_lock
);
5581 spin_unlock(&all_mddevs_lock
);
5582 rv
= entry
->store(mddev
, page
, length
);
5587 static void md_free(struct kobject
*ko
)
5589 struct mddev
*mddev
= container_of(ko
, struct mddev
, kobj
);
5591 if (mddev
->sysfs_state
)
5592 sysfs_put(mddev
->sysfs_state
);
5593 if (mddev
->sysfs_level
)
5594 sysfs_put(mddev
->sysfs_level
);
5597 del_gendisk(mddev
->gendisk
);
5599 blk_cleanup_queue(mddev
->queue
);
5601 put_disk(mddev
->gendisk
);
5602 percpu_ref_exit(&mddev
->writes_pending
);
5604 bioset_exit(&mddev
->bio_set
);
5605 bioset_exit(&mddev
->sync_set
);
5606 mempool_exit(&mddev
->md_io_pool
);
5610 static const struct sysfs_ops md_sysfs_ops
= {
5611 .show
= md_attr_show
,
5612 .store
= md_attr_store
,
5614 static struct kobj_type md_ktype
= {
5616 .sysfs_ops
= &md_sysfs_ops
,
5617 .default_attrs
= md_default_attrs
,
5622 static void mddev_delayed_delete(struct work_struct
*ws
)
5624 struct mddev
*mddev
= container_of(ws
, struct mddev
, del_work
);
5626 sysfs_remove_group(&mddev
->kobj
, &md_bitmap_group
);
5627 kobject_del(&mddev
->kobj
);
5628 kobject_put(&mddev
->kobj
);
5631 static void no_op(struct percpu_ref
*r
) {}
5633 int mddev_init_writes_pending(struct mddev
*mddev
)
5635 if (mddev
->writes_pending
.percpu_count_ptr
)
5637 if (percpu_ref_init(&mddev
->writes_pending
, no_op
,
5638 PERCPU_REF_ALLOW_REINIT
, GFP_KERNEL
) < 0)
5640 /* We want to start with the refcount at zero */
5641 percpu_ref_put(&mddev
->writes_pending
);
5644 EXPORT_SYMBOL_GPL(mddev_init_writes_pending
);
5646 static int md_alloc(dev_t dev
, char *name
)
5649 * If dev is zero, name is the name of a device to allocate with
5650 * an arbitrary minor number. It will be "md_???"
5651 * If dev is non-zero it must be a device number with a MAJOR of
5652 * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then
5653 * the device is being created by opening a node in /dev.
5654 * If "name" is not NULL, the device is being created by
5655 * writing to /sys/module/md_mod/parameters/new_array.
5657 static DEFINE_MUTEX(disks_mutex
);
5658 struct mddev
*mddev
= mddev_find(dev
);
5659 struct gendisk
*disk
;
5668 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
5669 shift
= partitioned
? MdpMinorShift
: 0;
5670 unit
= MINOR(mddev
->unit
) >> shift
;
5672 /* wait for any previous instance of this device to be
5673 * completely removed (mddev_delayed_delete).
5675 flush_workqueue(md_misc_wq
);
5677 mutex_lock(&disks_mutex
);
5683 /* Need to ensure that 'name' is not a duplicate.
5685 struct mddev
*mddev2
;
5686 spin_lock(&all_mddevs_lock
);
5688 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
5689 if (mddev2
->gendisk
&&
5690 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
5691 spin_unlock(&all_mddevs_lock
);
5694 spin_unlock(&all_mddevs_lock
);
5698 * Creating /dev/mdNNN via "newarray", so adjust hold_active.
5700 mddev
->hold_active
= UNTIL_STOP
;
5702 error
= mempool_init_kmalloc_pool(&mddev
->md_io_pool
, BIO_POOL_SIZE
,
5703 sizeof(struct md_io
));
5708 mddev
->queue
= blk_alloc_queue(NUMA_NO_NODE
);
5712 blk_set_stacking_limits(&mddev
->queue
->limits
);
5714 disk
= alloc_disk(1 << shift
);
5716 blk_cleanup_queue(mddev
->queue
);
5717 mddev
->queue
= NULL
;
5720 disk
->major
= MAJOR(mddev
->unit
);
5721 disk
->first_minor
= unit
<< shift
;
5723 strcpy(disk
->disk_name
, name
);
5724 else if (partitioned
)
5725 sprintf(disk
->disk_name
, "md_d%d", unit
);
5727 sprintf(disk
->disk_name
, "md%d", unit
);
5728 disk
->fops
= &md_fops
;
5729 disk
->private_data
= mddev
;
5730 disk
->queue
= mddev
->queue
;
5731 blk_queue_write_cache(mddev
->queue
, true, true);
5732 /* Allow extended partitions. This makes the
5733 * 'mdp' device redundant, but we can't really
5736 disk
->flags
|= GENHD_FL_EXT_DEVT
;
5737 disk
->events
|= DISK_EVENT_MEDIA_CHANGE
;
5738 mddev
->gendisk
= disk
;
5739 /* As soon as we call add_disk(), another thread could get
5740 * through to md_open, so make sure it doesn't get too far
5742 mutex_lock(&mddev
->open_mutex
);
5745 error
= kobject_add(&mddev
->kobj
, &disk_to_dev(disk
)->kobj
, "%s", "md");
5747 /* This isn't possible, but as kobject_init_and_add is marked
5748 * __must_check, we must do something with the result
5750 pr_debug("md: cannot register %s/md - name in use\n",
5754 if (mddev
->kobj
.sd
&&
5755 sysfs_create_group(&mddev
->kobj
, &md_bitmap_group
))
5756 pr_debug("pointless warning\n");
5757 mutex_unlock(&mddev
->open_mutex
);
5759 mutex_unlock(&disks_mutex
);
5760 if (!error
&& mddev
->kobj
.sd
) {
5761 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
5762 mddev
->sysfs_state
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "array_state");
5763 mddev
->sysfs_level
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "level");
5769 static void md_probe(dev_t dev
)
5771 if (MAJOR(dev
) == MD_MAJOR
&& MINOR(dev
) >= 512)
5774 md_alloc(dev
, NULL
);
5777 static int add_named_array(const char *val
, const struct kernel_param
*kp
)
5780 * val must be "md_*" or "mdNNN".
5781 * For "md_*" we allocate an array with a large free minor number, and
5782 * set the name to val. val must not already be an active name.
5783 * For "mdNNN" we allocate an array with the minor number NNN
5784 * which must not already be in use.
5786 int len
= strlen(val
);
5787 char buf
[DISK_NAME_LEN
];
5788 unsigned long devnum
;
5790 while (len
&& val
[len
-1] == '\n')
5792 if (len
>= DISK_NAME_LEN
)
5794 strlcpy(buf
, val
, len
+1);
5795 if (strncmp(buf
, "md_", 3) == 0)
5796 return md_alloc(0, buf
);
5797 if (strncmp(buf
, "md", 2) == 0 &&
5799 kstrtoul(buf
+2, 10, &devnum
) == 0 &&
5800 devnum
<= MINORMASK
)
5801 return md_alloc(MKDEV(MD_MAJOR
, devnum
), NULL
);
5806 static void md_safemode_timeout(struct timer_list
*t
)
5808 struct mddev
*mddev
= from_timer(mddev
, t
, safemode_timer
);
5810 mddev
->safemode
= 1;
5811 if (mddev
->external
)
5812 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5814 md_wakeup_thread(mddev
->thread
);
5817 static int start_dirty_degraded
;
5819 int md_run(struct mddev
*mddev
)
5822 struct md_rdev
*rdev
;
5823 struct md_personality
*pers
;
5825 if (list_empty(&mddev
->disks
))
5826 /* cannot run an array with no devices.. */
5831 /* Cannot run until previous stop completes properly */
5832 if (mddev
->sysfs_active
)
5836 * Analyze all RAID superblock(s)
5838 if (!mddev
->raid_disks
) {
5839 if (!mddev
->persistent
)
5841 err
= analyze_sbs(mddev
);
5846 if (mddev
->level
!= LEVEL_NONE
)
5847 request_module("md-level-%d", mddev
->level
);
5848 else if (mddev
->clevel
[0])
5849 request_module("md-%s", mddev
->clevel
);
5852 * Drop all container device buffers, from now on
5853 * the only valid external interface is through the md
5856 mddev
->has_superblocks
= false;
5857 rdev_for_each(rdev
, mddev
) {
5858 if (test_bit(Faulty
, &rdev
->flags
))
5860 sync_blockdev(rdev
->bdev
);
5861 invalidate_bdev(rdev
->bdev
);
5862 if (mddev
->ro
!= 1 &&
5863 (bdev_read_only(rdev
->bdev
) ||
5864 bdev_read_only(rdev
->meta_bdev
))) {
5867 set_disk_ro(mddev
->gendisk
, 1);
5871 mddev
->has_superblocks
= true;
5873 /* perform some consistency tests on the device.
5874 * We don't want the data to overlap the metadata,
5875 * Internal Bitmap issues have been handled elsewhere.
5877 if (rdev
->meta_bdev
) {
5878 /* Nothing to check */;
5879 } else if (rdev
->data_offset
< rdev
->sb_start
) {
5880 if (mddev
->dev_sectors
&&
5881 rdev
->data_offset
+ mddev
->dev_sectors
5883 pr_warn("md: %s: data overlaps metadata\n",
5888 if (rdev
->sb_start
+ rdev
->sb_size
/512
5889 > rdev
->data_offset
) {
5890 pr_warn("md: %s: metadata overlaps data\n",
5895 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
5898 if (!bioset_initialized(&mddev
->bio_set
)) {
5899 err
= bioset_init(&mddev
->bio_set
, BIO_POOL_SIZE
, 0, BIOSET_NEED_BVECS
);
5903 if (!bioset_initialized(&mddev
->sync_set
)) {
5904 err
= bioset_init(&mddev
->sync_set
, BIO_POOL_SIZE
, 0, BIOSET_NEED_BVECS
);
5909 spin_lock(&pers_lock
);
5910 pers
= find_pers(mddev
->level
, mddev
->clevel
);
5911 if (!pers
|| !try_module_get(pers
->owner
)) {
5912 spin_unlock(&pers_lock
);
5913 if (mddev
->level
!= LEVEL_NONE
)
5914 pr_warn("md: personality for level %d is not loaded!\n",
5917 pr_warn("md: personality for level %s is not loaded!\n",
5922 spin_unlock(&pers_lock
);
5923 if (mddev
->level
!= pers
->level
) {
5924 mddev
->level
= pers
->level
;
5925 mddev
->new_level
= pers
->level
;
5927 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
5929 if (mddev
->reshape_position
!= MaxSector
&&
5930 pers
->start_reshape
== NULL
) {
5931 /* This personality cannot handle reshaping... */
5932 module_put(pers
->owner
);
5937 if (pers
->sync_request
) {
5938 /* Warn if this is a potentially silly
5941 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
5942 struct md_rdev
*rdev2
;
5945 rdev_for_each(rdev
, mddev
)
5946 rdev_for_each(rdev2
, mddev
) {
5948 rdev
->bdev
->bd_disk
==
5949 rdev2
->bdev
->bd_disk
) {
5950 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5952 bdevname(rdev
->bdev
,b
),
5953 bdevname(rdev2
->bdev
,b2
));
5959 pr_warn("True protection against single-disk failure might be compromised.\n");
5962 mddev
->recovery
= 0;
5963 /* may be over-ridden by personality */
5964 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
5966 mddev
->ok_start_degraded
= start_dirty_degraded
;
5968 if (start_readonly
&& mddev
->ro
== 0)
5969 mddev
->ro
= 2; /* read-only, but switch on first write */
5971 err
= pers
->run(mddev
);
5973 pr_warn("md: pers->run() failed ...\n");
5974 else if (pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
5975 WARN_ONCE(!mddev
->external_size
,
5976 "%s: default size too small, but 'external_size' not in effect?\n",
5978 pr_warn("md: invalid array_size %llu > default size %llu\n",
5979 (unsigned long long)mddev
->array_sectors
/ 2,
5980 (unsigned long long)pers
->size(mddev
, 0, 0) / 2);
5983 if (err
== 0 && pers
->sync_request
&&
5984 (mddev
->bitmap_info
.file
|| mddev
->bitmap_info
.offset
)) {
5985 struct bitmap
*bitmap
;
5987 bitmap
= md_bitmap_create(mddev
, -1);
5988 if (IS_ERR(bitmap
)) {
5989 err
= PTR_ERR(bitmap
);
5990 pr_warn("%s: failed to create bitmap (%d)\n",
5991 mdname(mddev
), err
);
5993 mddev
->bitmap
= bitmap
;
5999 if (mddev
->bitmap_info
.max_write_behind
> 0) {
6000 bool create_pool
= false;
6002 rdev_for_each(rdev
, mddev
) {
6003 if (test_bit(WriteMostly
, &rdev
->flags
) &&
6004 rdev_init_serial(rdev
))
6007 if (create_pool
&& mddev
->serial_info_pool
== NULL
) {
6008 mddev
->serial_info_pool
=
6009 mempool_create_kmalloc_pool(NR_SERIAL_INFOS
,
6010 sizeof(struct serial_info
));
6011 if (!mddev
->serial_info_pool
) {
6021 rdev_for_each(rdev
, mddev
) {
6022 if (rdev
->raid_disk
>= 0 &&
6023 !blk_queue_nonrot(bdev_get_queue(rdev
->bdev
))) {
6028 if (mddev
->degraded
)
6031 blk_queue_flag_set(QUEUE_FLAG_NONROT
, mddev
->queue
);
6033 blk_queue_flag_clear(QUEUE_FLAG_NONROT
, mddev
->queue
);
6035 if (pers
->sync_request
) {
6036 if (mddev
->kobj
.sd
&&
6037 sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
6038 pr_warn("md: cannot register extra attributes for %s\n",
6040 mddev
->sysfs_action
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "sync_action");
6041 mddev
->sysfs_completed
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "sync_completed");
6042 mddev
->sysfs_degraded
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "degraded");
6043 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
6046 atomic_set(&mddev
->max_corr_read_errors
,
6047 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS
);
6048 mddev
->safemode
= 0;
6049 if (mddev_is_clustered(mddev
))
6050 mddev
->safemode_delay
= 0;
6052 mddev
->safemode_delay
= DEFAULT_SAFEMODE_DELAY
;
6055 spin_lock(&mddev
->lock
);
6057 spin_unlock(&mddev
->lock
);
6058 rdev_for_each(rdev
, mddev
)
6059 if (rdev
->raid_disk
>= 0)
6060 sysfs_link_rdev(mddev
, rdev
); /* failure here is OK */
6062 if (mddev
->degraded
&& !mddev
->ro
)
6063 /* This ensures that recovering status is reported immediately
6064 * via sysfs - until a lack of spares is confirmed.
6066 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6067 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6069 if (mddev
->sb_flags
)
6070 md_update_sb(mddev
, 0);
6072 md_new_event(mddev
);
6076 mddev_detach(mddev
);
6078 pers
->free(mddev
, mddev
->private);
6079 mddev
->private = NULL
;
6080 module_put(pers
->owner
);
6081 md_bitmap_destroy(mddev
);
6083 bioset_exit(&mddev
->bio_set
);
6084 bioset_exit(&mddev
->sync_set
);
6087 EXPORT_SYMBOL_GPL(md_run
);
6089 int do_md_run(struct mddev
*mddev
)
6093 set_bit(MD_NOT_READY
, &mddev
->flags
);
6094 err
= md_run(mddev
);
6097 err
= md_bitmap_load(mddev
);
6099 md_bitmap_destroy(mddev
);
6103 if (mddev_is_clustered(mddev
))
6104 md_allow_write(mddev
);
6106 /* run start up tasks that require md_thread */
6109 md_wakeup_thread(mddev
->thread
);
6110 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
6112 set_capacity_and_notify(mddev
->gendisk
, mddev
->array_sectors
);
6113 clear_bit(MD_NOT_READY
, &mddev
->flags
);
6115 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
6116 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6117 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
6118 sysfs_notify_dirent_safe(mddev
->sysfs_degraded
);
6120 clear_bit(MD_NOT_READY
, &mddev
->flags
);
6124 int md_start(struct mddev
*mddev
)
6128 if (mddev
->pers
->start
) {
6129 set_bit(MD_RECOVERY_WAIT
, &mddev
->recovery
);
6130 md_wakeup_thread(mddev
->thread
);
6131 ret
= mddev
->pers
->start(mddev
);
6132 clear_bit(MD_RECOVERY_WAIT
, &mddev
->recovery
);
6133 md_wakeup_thread(mddev
->sync_thread
);
6137 EXPORT_SYMBOL_GPL(md_start
);
6139 static int restart_array(struct mddev
*mddev
)
6141 struct gendisk
*disk
= mddev
->gendisk
;
6142 struct md_rdev
*rdev
;
6143 bool has_journal
= false;
6144 bool has_readonly
= false;
6146 /* Complain if it has no devices */
6147 if (list_empty(&mddev
->disks
))
6155 rdev_for_each_rcu(rdev
, mddev
) {
6156 if (test_bit(Journal
, &rdev
->flags
) &&
6157 !test_bit(Faulty
, &rdev
->flags
))
6159 if (bdev_read_only(rdev
->bdev
))
6160 has_readonly
= true;
6163 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
) && !has_journal
)
6164 /* Don't restart rw with journal missing/faulty */
6169 mddev
->safemode
= 0;
6171 set_disk_ro(disk
, 0);
6172 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev
));
6173 /* Kick recovery or resync if necessary */
6174 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6175 md_wakeup_thread(mddev
->thread
);
6176 md_wakeup_thread(mddev
->sync_thread
);
6177 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6181 static void md_clean(struct mddev
*mddev
)
6183 mddev
->array_sectors
= 0;
6184 mddev
->external_size
= 0;
6185 mddev
->dev_sectors
= 0;
6186 mddev
->raid_disks
= 0;
6187 mddev
->recovery_cp
= 0;
6188 mddev
->resync_min
= 0;
6189 mddev
->resync_max
= MaxSector
;
6190 mddev
->reshape_position
= MaxSector
;
6191 mddev
->external
= 0;
6192 mddev
->persistent
= 0;
6193 mddev
->level
= LEVEL_NONE
;
6194 mddev
->clevel
[0] = 0;
6196 mddev
->sb_flags
= 0;
6198 mddev
->metadata_type
[0] = 0;
6199 mddev
->chunk_sectors
= 0;
6200 mddev
->ctime
= mddev
->utime
= 0;
6202 mddev
->max_disks
= 0;
6204 mddev
->can_decrease_events
= 0;
6205 mddev
->delta_disks
= 0;
6206 mddev
->reshape_backwards
= 0;
6207 mddev
->new_level
= LEVEL_NONE
;
6208 mddev
->new_layout
= 0;
6209 mddev
->new_chunk_sectors
= 0;
6210 mddev
->curr_resync
= 0;
6211 atomic64_set(&mddev
->resync_mismatches
, 0);
6212 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
6213 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
6214 mddev
->recovery
= 0;
6217 mddev
->degraded
= 0;
6218 mddev
->safemode
= 0;
6219 mddev
->private = NULL
;
6220 mddev
->cluster_info
= NULL
;
6221 mddev
->bitmap_info
.offset
= 0;
6222 mddev
->bitmap_info
.default_offset
= 0;
6223 mddev
->bitmap_info
.default_space
= 0;
6224 mddev
->bitmap_info
.chunksize
= 0;
6225 mddev
->bitmap_info
.daemon_sleep
= 0;
6226 mddev
->bitmap_info
.max_write_behind
= 0;
6227 mddev
->bitmap_info
.nodes
= 0;
6230 static void __md_stop_writes(struct mddev
*mddev
)
6232 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
6233 if (work_pending(&mddev
->del_work
))
6234 flush_workqueue(md_misc_wq
);
6235 if (mddev
->sync_thread
) {
6236 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6237 md_reap_sync_thread(mddev
);
6240 del_timer_sync(&mddev
->safemode_timer
);
6242 if (mddev
->pers
&& mddev
->pers
->quiesce
) {
6243 mddev
->pers
->quiesce(mddev
, 1);
6244 mddev
->pers
->quiesce(mddev
, 0);
6246 md_bitmap_flush(mddev
);
6248 if (mddev
->ro
== 0 &&
6249 ((!mddev
->in_sync
&& !mddev_is_clustered(mddev
)) ||
6251 /* mark array as shutdown cleanly */
6252 if (!mddev_is_clustered(mddev
))
6254 md_update_sb(mddev
, 1);
6256 /* disable policy to guarantee rdevs free resources for serialization */
6257 mddev
->serialize_policy
= 0;
6258 mddev_destroy_serial_pool(mddev
, NULL
, true);
6261 void md_stop_writes(struct mddev
*mddev
)
6263 mddev_lock_nointr(mddev
);
6264 __md_stop_writes(mddev
);
6265 mddev_unlock(mddev
);
6267 EXPORT_SYMBOL_GPL(md_stop_writes
);
6269 static void mddev_detach(struct mddev
*mddev
)
6271 md_bitmap_wait_behind_writes(mddev
);
6272 if (mddev
->pers
&& mddev
->pers
->quiesce
&& !mddev
->suspended
) {
6273 mddev
->pers
->quiesce(mddev
, 1);
6274 mddev
->pers
->quiesce(mddev
, 0);
6276 md_unregister_thread(&mddev
->thread
);
6278 blk_sync_queue(mddev
->queue
); /* the unplug fn references 'conf'*/
6281 static void __md_stop(struct mddev
*mddev
)
6283 struct md_personality
*pers
= mddev
->pers
;
6284 md_bitmap_destroy(mddev
);
6285 mddev_detach(mddev
);
6286 /* Ensure ->event_work is done */
6287 if (mddev
->event_work
.func
)
6288 flush_workqueue(md_misc_wq
);
6289 spin_lock(&mddev
->lock
);
6291 spin_unlock(&mddev
->lock
);
6292 pers
->free(mddev
, mddev
->private);
6293 mddev
->private = NULL
;
6294 if (pers
->sync_request
&& mddev
->to_remove
== NULL
)
6295 mddev
->to_remove
= &md_redundancy_group
;
6296 module_put(pers
->owner
);
6297 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
6300 void md_stop(struct mddev
*mddev
)
6302 /* stop the array and free an attached data structures.
6303 * This is called from dm-raid
6306 bioset_exit(&mddev
->bio_set
);
6307 bioset_exit(&mddev
->sync_set
);
6310 EXPORT_SYMBOL_GPL(md_stop
);
6312 static int md_set_readonly(struct mddev
*mddev
, struct block_device
*bdev
)
6317 if (!test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
)) {
6319 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
6320 md_wakeup_thread(mddev
->thread
);
6322 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
6323 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6324 if (mddev
->sync_thread
)
6325 /* Thread might be blocked waiting for metadata update
6326 * which will now never happen */
6327 wake_up_process(mddev
->sync_thread
->tsk
);
6329 if (mddev
->external
&& test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
))
6331 mddev_unlock(mddev
);
6332 wait_event(resync_wait
, !test_bit(MD_RECOVERY_RUNNING
,
6334 wait_event(mddev
->sb_wait
,
6335 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
6336 mddev_lock_nointr(mddev
);
6338 mutex_lock(&mddev
->open_mutex
);
6339 if ((mddev
->pers
&& atomic_read(&mddev
->openers
) > !!bdev
) ||
6340 mddev
->sync_thread
||
6341 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
)) {
6342 pr_warn("md: %s still in use.\n",mdname(mddev
));
6344 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
6345 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6346 md_wakeup_thread(mddev
->thread
);
6352 __md_stop_writes(mddev
);
6358 set_disk_ro(mddev
->gendisk
, 1);
6359 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
6360 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6361 md_wakeup_thread(mddev
->thread
);
6362 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6366 mutex_unlock(&mddev
->open_mutex
);
6371 * 0 - completely stop and dis-assemble array
6372 * 2 - stop but do not disassemble array
6374 static int do_md_stop(struct mddev
*mddev
, int mode
,
6375 struct block_device
*bdev
)
6377 struct gendisk
*disk
= mddev
->gendisk
;
6378 struct md_rdev
*rdev
;
6381 if (!test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
)) {
6383 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
6384 md_wakeup_thread(mddev
->thread
);
6386 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
6387 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6388 if (mddev
->sync_thread
)
6389 /* Thread might be blocked waiting for metadata update
6390 * which will now never happen */
6391 wake_up_process(mddev
->sync_thread
->tsk
);
6393 mddev_unlock(mddev
);
6394 wait_event(resync_wait
, (mddev
->sync_thread
== NULL
&&
6395 !test_bit(MD_RECOVERY_RUNNING
,
6396 &mddev
->recovery
)));
6397 mddev_lock_nointr(mddev
);
6399 mutex_lock(&mddev
->open_mutex
);
6400 if ((mddev
->pers
&& atomic_read(&mddev
->openers
) > !!bdev
) ||
6401 mddev
->sysfs_active
||
6402 mddev
->sync_thread
||
6403 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
)) {
6404 pr_warn("md: %s still in use.\n",mdname(mddev
));
6405 mutex_unlock(&mddev
->open_mutex
);
6407 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
6408 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6409 md_wakeup_thread(mddev
->thread
);
6415 set_disk_ro(disk
, 0);
6417 __md_stop_writes(mddev
);
6420 /* tell userspace to handle 'inactive' */
6421 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6423 rdev_for_each(rdev
, mddev
)
6424 if (rdev
->raid_disk
>= 0)
6425 sysfs_unlink_rdev(mddev
, rdev
);
6427 set_capacity_and_notify(disk
, 0);
6428 mutex_unlock(&mddev
->open_mutex
);
6434 mutex_unlock(&mddev
->open_mutex
);
6436 * Free resources if final stop
6439 pr_info("md: %s stopped.\n", mdname(mddev
));
6441 if (mddev
->bitmap_info
.file
) {
6442 struct file
*f
= mddev
->bitmap_info
.file
;
6443 spin_lock(&mddev
->lock
);
6444 mddev
->bitmap_info
.file
= NULL
;
6445 spin_unlock(&mddev
->lock
);
6448 mddev
->bitmap_info
.offset
= 0;
6450 export_array(mddev
);
6453 if (mddev
->hold_active
== UNTIL_STOP
)
6454 mddev
->hold_active
= 0;
6456 md_new_event(mddev
);
6457 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6462 static void autorun_array(struct mddev
*mddev
)
6464 struct md_rdev
*rdev
;
6467 if (list_empty(&mddev
->disks
))
6470 pr_info("md: running: ");
6472 rdev_for_each(rdev
, mddev
) {
6473 char b
[BDEVNAME_SIZE
];
6474 pr_cont("<%s>", bdevname(rdev
->bdev
,b
));
6478 err
= do_md_run(mddev
);
6480 pr_warn("md: do_md_run() returned %d\n", err
);
6481 do_md_stop(mddev
, 0, NULL
);
6486 * lets try to run arrays based on all disks that have arrived
6487 * until now. (those are in pending_raid_disks)
6489 * the method: pick the first pending disk, collect all disks with
6490 * the same UUID, remove all from the pending list and put them into
6491 * the 'same_array' list. Then order this list based on superblock
6492 * update time (freshest comes first), kick out 'old' disks and
6493 * compare superblocks. If everything's fine then run it.
6495 * If "unit" is allocated, then bump its reference count
6497 static void autorun_devices(int part
)
6499 struct md_rdev
*rdev0
, *rdev
, *tmp
;
6500 struct mddev
*mddev
;
6501 char b
[BDEVNAME_SIZE
];
6503 pr_info("md: autorun ...\n");
6504 while (!list_empty(&pending_raid_disks
)) {
6507 LIST_HEAD(candidates
);
6508 rdev0
= list_entry(pending_raid_disks
.next
,
6509 struct md_rdev
, same_set
);
6511 pr_debug("md: considering %s ...\n", bdevname(rdev0
->bdev
,b
));
6512 INIT_LIST_HEAD(&candidates
);
6513 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
6514 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
6515 pr_debug("md: adding %s ...\n",
6516 bdevname(rdev
->bdev
,b
));
6517 list_move(&rdev
->same_set
, &candidates
);
6520 * now we have a set of devices, with all of them having
6521 * mostly sane superblocks. It's time to allocate the
6525 dev
= MKDEV(mdp_major
,
6526 rdev0
->preferred_minor
<< MdpMinorShift
);
6527 unit
= MINOR(dev
) >> MdpMinorShift
;
6529 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
6532 if (rdev0
->preferred_minor
!= unit
) {
6533 pr_warn("md: unit number in %s is bad: %d\n",
6534 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
6539 mddev
= mddev_find(dev
);
6540 if (!mddev
|| !mddev
->gendisk
) {
6545 if (mddev_lock(mddev
))
6546 pr_warn("md: %s locked, cannot run\n", mdname(mddev
));
6547 else if (mddev
->raid_disks
|| mddev
->major_version
6548 || !list_empty(&mddev
->disks
)) {
6549 pr_warn("md: %s already running, cannot run %s\n",
6550 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
6551 mddev_unlock(mddev
);
6553 pr_debug("md: created %s\n", mdname(mddev
));
6554 mddev
->persistent
= 1;
6555 rdev_for_each_list(rdev
, tmp
, &candidates
) {
6556 list_del_init(&rdev
->same_set
);
6557 if (bind_rdev_to_array(rdev
, mddev
))
6560 autorun_array(mddev
);
6561 mddev_unlock(mddev
);
6563 /* on success, candidates will be empty, on error
6566 rdev_for_each_list(rdev
, tmp
, &candidates
) {
6567 list_del_init(&rdev
->same_set
);
6572 pr_info("md: ... autorun DONE.\n");
6574 #endif /* !MODULE */
6576 static int get_version(void __user
*arg
)
6580 ver
.major
= MD_MAJOR_VERSION
;
6581 ver
.minor
= MD_MINOR_VERSION
;
6582 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
6584 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
6590 static int get_array_info(struct mddev
*mddev
, void __user
*arg
)
6592 mdu_array_info_t info
;
6593 int nr
,working
,insync
,failed
,spare
;
6594 struct md_rdev
*rdev
;
6596 nr
= working
= insync
= failed
= spare
= 0;
6598 rdev_for_each_rcu(rdev
, mddev
) {
6600 if (test_bit(Faulty
, &rdev
->flags
))
6604 if (test_bit(In_sync
, &rdev
->flags
))
6606 else if (test_bit(Journal
, &rdev
->flags
))
6607 /* TODO: add journal count to md_u.h */
6615 info
.major_version
= mddev
->major_version
;
6616 info
.minor_version
= mddev
->minor_version
;
6617 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
6618 info
.ctime
= clamp_t(time64_t
, mddev
->ctime
, 0, U32_MAX
);
6619 info
.level
= mddev
->level
;
6620 info
.size
= mddev
->dev_sectors
/ 2;
6621 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
6624 info
.raid_disks
= mddev
->raid_disks
;
6625 info
.md_minor
= mddev
->md_minor
;
6626 info
.not_persistent
= !mddev
->persistent
;
6628 info
.utime
= clamp_t(time64_t
, mddev
->utime
, 0, U32_MAX
);
6631 info
.state
= (1<<MD_SB_CLEAN
);
6632 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
6633 info
.state
|= (1<<MD_SB_BITMAP_PRESENT
);
6634 if (mddev_is_clustered(mddev
))
6635 info
.state
|= (1<<MD_SB_CLUSTERED
);
6636 info
.active_disks
= insync
;
6637 info
.working_disks
= working
;
6638 info
.failed_disks
= failed
;
6639 info
.spare_disks
= spare
;
6641 info
.layout
= mddev
->layout
;
6642 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
6644 if (copy_to_user(arg
, &info
, sizeof(info
)))
6650 static int get_bitmap_file(struct mddev
*mddev
, void __user
* arg
)
6652 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
6656 file
= kzalloc(sizeof(*file
), GFP_NOIO
);
6661 spin_lock(&mddev
->lock
);
6662 /* bitmap enabled */
6663 if (mddev
->bitmap_info
.file
) {
6664 ptr
= file_path(mddev
->bitmap_info
.file
, file
->pathname
,
6665 sizeof(file
->pathname
));
6669 memmove(file
->pathname
, ptr
,
6670 sizeof(file
->pathname
)-(ptr
-file
->pathname
));
6672 spin_unlock(&mddev
->lock
);
6675 copy_to_user(arg
, file
, sizeof(*file
)))
6682 static int get_disk_info(struct mddev
*mddev
, void __user
* arg
)
6684 mdu_disk_info_t info
;
6685 struct md_rdev
*rdev
;
6687 if (copy_from_user(&info
, arg
, sizeof(info
)))
6691 rdev
= md_find_rdev_nr_rcu(mddev
, info
.number
);
6693 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
6694 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
6695 info
.raid_disk
= rdev
->raid_disk
;
6697 if (test_bit(Faulty
, &rdev
->flags
))
6698 info
.state
|= (1<<MD_DISK_FAULTY
);
6699 else if (test_bit(In_sync
, &rdev
->flags
)) {
6700 info
.state
|= (1<<MD_DISK_ACTIVE
);
6701 info
.state
|= (1<<MD_DISK_SYNC
);
6703 if (test_bit(Journal
, &rdev
->flags
))
6704 info
.state
|= (1<<MD_DISK_JOURNAL
);
6705 if (test_bit(WriteMostly
, &rdev
->flags
))
6706 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
6707 if (test_bit(FailFast
, &rdev
->flags
))
6708 info
.state
|= (1<<MD_DISK_FAILFAST
);
6710 info
.major
= info
.minor
= 0;
6711 info
.raid_disk
= -1;
6712 info
.state
= (1<<MD_DISK_REMOVED
);
6716 if (copy_to_user(arg
, &info
, sizeof(info
)))
6722 int md_add_new_disk(struct mddev
*mddev
, struct mdu_disk_info_s
*info
)
6724 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
6725 struct md_rdev
*rdev
;
6726 dev_t dev
= MKDEV(info
->major
,info
->minor
);
6728 if (mddev_is_clustered(mddev
) &&
6729 !(info
->state
& ((1 << MD_DISK_CLUSTER_ADD
) | (1 << MD_DISK_CANDIDATE
)))) {
6730 pr_warn("%s: Cannot add to clustered mddev.\n",
6735 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
6738 if (!mddev
->raid_disks
) {
6740 /* expecting a device which has a superblock */
6741 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
6743 pr_warn("md: md_import_device returned %ld\n",
6745 return PTR_ERR(rdev
);
6747 if (!list_empty(&mddev
->disks
)) {
6748 struct md_rdev
*rdev0
6749 = list_entry(mddev
->disks
.next
,
6750 struct md_rdev
, same_set
);
6751 err
= super_types
[mddev
->major_version
]
6752 .load_super(rdev
, rdev0
, mddev
->minor_version
);
6754 pr_warn("md: %s has different UUID to %s\n",
6755 bdevname(rdev
->bdev
,b
),
6756 bdevname(rdev0
->bdev
,b2
));
6761 err
= bind_rdev_to_array(rdev
, mddev
);
6768 * md_add_new_disk can be used once the array is assembled
6769 * to add "hot spares". They must already have a superblock
6774 if (!mddev
->pers
->hot_add_disk
) {
6775 pr_warn("%s: personality does not support diskops!\n",
6779 if (mddev
->persistent
)
6780 rdev
= md_import_device(dev
, mddev
->major_version
,
6781 mddev
->minor_version
);
6783 rdev
= md_import_device(dev
, -1, -1);
6785 pr_warn("md: md_import_device returned %ld\n",
6787 return PTR_ERR(rdev
);
6789 /* set saved_raid_disk if appropriate */
6790 if (!mddev
->persistent
) {
6791 if (info
->state
& (1<<MD_DISK_SYNC
) &&
6792 info
->raid_disk
< mddev
->raid_disks
) {
6793 rdev
->raid_disk
= info
->raid_disk
;
6794 set_bit(In_sync
, &rdev
->flags
);
6795 clear_bit(Bitmap_sync
, &rdev
->flags
);
6797 rdev
->raid_disk
= -1;
6798 rdev
->saved_raid_disk
= rdev
->raid_disk
;
6800 super_types
[mddev
->major_version
].
6801 validate_super(mddev
, rdev
);
6802 if ((info
->state
& (1<<MD_DISK_SYNC
)) &&
6803 rdev
->raid_disk
!= info
->raid_disk
) {
6804 /* This was a hot-add request, but events doesn't
6805 * match, so reject it.
6811 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
6812 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
6813 set_bit(WriteMostly
, &rdev
->flags
);
6815 clear_bit(WriteMostly
, &rdev
->flags
);
6816 if (info
->state
& (1<<MD_DISK_FAILFAST
))
6817 set_bit(FailFast
, &rdev
->flags
);
6819 clear_bit(FailFast
, &rdev
->flags
);
6821 if (info
->state
& (1<<MD_DISK_JOURNAL
)) {
6822 struct md_rdev
*rdev2
;
6823 bool has_journal
= false;
6825 /* make sure no existing journal disk */
6826 rdev_for_each(rdev2
, mddev
) {
6827 if (test_bit(Journal
, &rdev2
->flags
)) {
6832 if (has_journal
|| mddev
->bitmap
) {
6836 set_bit(Journal
, &rdev
->flags
);
6839 * check whether the device shows up in other nodes
6841 if (mddev_is_clustered(mddev
)) {
6842 if (info
->state
& (1 << MD_DISK_CANDIDATE
))
6843 set_bit(Candidate
, &rdev
->flags
);
6844 else if (info
->state
& (1 << MD_DISK_CLUSTER_ADD
)) {
6845 /* --add initiated by this node */
6846 err
= md_cluster_ops
->add_new_disk(mddev
, rdev
);
6854 rdev
->raid_disk
= -1;
6855 err
= bind_rdev_to_array(rdev
, mddev
);
6860 if (mddev_is_clustered(mddev
)) {
6861 if (info
->state
& (1 << MD_DISK_CANDIDATE
)) {
6863 err
= md_cluster_ops
->new_disk_ack(mddev
,
6866 md_kick_rdev_from_array(rdev
);
6870 md_cluster_ops
->add_new_disk_cancel(mddev
);
6872 err
= add_bound_rdev(rdev
);
6876 err
= add_bound_rdev(rdev
);
6881 /* otherwise, md_add_new_disk is only allowed
6882 * for major_version==0 superblocks
6884 if (mddev
->major_version
!= 0) {
6885 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev
));
6889 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
6891 rdev
= md_import_device(dev
, -1, 0);
6893 pr_warn("md: error, md_import_device() returned %ld\n",
6895 return PTR_ERR(rdev
);
6897 rdev
->desc_nr
= info
->number
;
6898 if (info
->raid_disk
< mddev
->raid_disks
)
6899 rdev
->raid_disk
= info
->raid_disk
;
6901 rdev
->raid_disk
= -1;
6903 if (rdev
->raid_disk
< mddev
->raid_disks
)
6904 if (info
->state
& (1<<MD_DISK_SYNC
))
6905 set_bit(In_sync
, &rdev
->flags
);
6907 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
6908 set_bit(WriteMostly
, &rdev
->flags
);
6909 if (info
->state
& (1<<MD_DISK_FAILFAST
))
6910 set_bit(FailFast
, &rdev
->flags
);
6912 if (!mddev
->persistent
) {
6913 pr_debug("md: nonpersistent superblock ...\n");
6914 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
6916 rdev
->sb_start
= calc_dev_sboffset(rdev
);
6917 rdev
->sectors
= rdev
->sb_start
;
6919 err
= bind_rdev_to_array(rdev
, mddev
);
6929 static int hot_remove_disk(struct mddev
*mddev
, dev_t dev
)
6931 char b
[BDEVNAME_SIZE
];
6932 struct md_rdev
*rdev
;
6937 rdev
= find_rdev(mddev
, dev
);
6941 if (rdev
->raid_disk
< 0)
6944 clear_bit(Blocked
, &rdev
->flags
);
6945 remove_and_add_spares(mddev
, rdev
);
6947 if (rdev
->raid_disk
>= 0)
6951 if (mddev_is_clustered(mddev
)) {
6952 if (md_cluster_ops
->remove_disk(mddev
, rdev
))
6956 md_kick_rdev_from_array(rdev
);
6957 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
6959 md_wakeup_thread(mddev
->thread
);
6961 md_update_sb(mddev
, 1);
6962 md_new_event(mddev
);
6966 pr_debug("md: cannot remove active disk %s from %s ...\n",
6967 bdevname(rdev
->bdev
,b
), mdname(mddev
));
6971 static int hot_add_disk(struct mddev
*mddev
, dev_t dev
)
6973 char b
[BDEVNAME_SIZE
];
6975 struct md_rdev
*rdev
;
6980 if (mddev
->major_version
!= 0) {
6981 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
6985 if (!mddev
->pers
->hot_add_disk
) {
6986 pr_warn("%s: personality does not support diskops!\n",
6991 rdev
= md_import_device(dev
, -1, 0);
6993 pr_warn("md: error, md_import_device() returned %ld\n",
6998 if (mddev
->persistent
)
6999 rdev
->sb_start
= calc_dev_sboffset(rdev
);
7001 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
7003 rdev
->sectors
= rdev
->sb_start
;
7005 if (test_bit(Faulty
, &rdev
->flags
)) {
7006 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
7007 bdevname(rdev
->bdev
,b
), mdname(mddev
));
7012 clear_bit(In_sync
, &rdev
->flags
);
7014 rdev
->saved_raid_disk
= -1;
7015 err
= bind_rdev_to_array(rdev
, mddev
);
7020 * The rest should better be atomic, we can have disk failures
7021 * noticed in interrupt contexts ...
7024 rdev
->raid_disk
= -1;
7026 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
7028 md_update_sb(mddev
, 1);
7030 * Kick recovery, maybe this spare has to be added to the
7031 * array immediately.
7033 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7034 md_wakeup_thread(mddev
->thread
);
7035 md_new_event(mddev
);
7043 static int set_bitmap_file(struct mddev
*mddev
, int fd
)
7048 if (!mddev
->pers
->quiesce
|| !mddev
->thread
)
7050 if (mddev
->recovery
|| mddev
->sync_thread
)
7052 /* we should be able to change the bitmap.. */
7056 struct inode
*inode
;
7059 if (mddev
->bitmap
|| mddev
->bitmap_info
.file
)
7060 return -EEXIST
; /* cannot add when bitmap is present */
7064 pr_warn("%s: error: failed to get bitmap file\n",
7069 inode
= f
->f_mapping
->host
;
7070 if (!S_ISREG(inode
->i_mode
)) {
7071 pr_warn("%s: error: bitmap file must be a regular file\n",
7074 } else if (!(f
->f_mode
& FMODE_WRITE
)) {
7075 pr_warn("%s: error: bitmap file must open for write\n",
7078 } else if (atomic_read(&inode
->i_writecount
) != 1) {
7079 pr_warn("%s: error: bitmap file is already in use\n",
7087 mddev
->bitmap_info
.file
= f
;
7088 mddev
->bitmap_info
.offset
= 0; /* file overrides offset */
7089 } else if (mddev
->bitmap
== NULL
)
7090 return -ENOENT
; /* cannot remove what isn't there */
7094 struct bitmap
*bitmap
;
7096 bitmap
= md_bitmap_create(mddev
, -1);
7097 mddev_suspend(mddev
);
7098 if (!IS_ERR(bitmap
)) {
7099 mddev
->bitmap
= bitmap
;
7100 err
= md_bitmap_load(mddev
);
7102 err
= PTR_ERR(bitmap
);
7104 md_bitmap_destroy(mddev
);
7107 mddev_resume(mddev
);
7108 } else if (fd
< 0) {
7109 mddev_suspend(mddev
);
7110 md_bitmap_destroy(mddev
);
7111 mddev_resume(mddev
);
7115 struct file
*f
= mddev
->bitmap_info
.file
;
7117 spin_lock(&mddev
->lock
);
7118 mddev
->bitmap_info
.file
= NULL
;
7119 spin_unlock(&mddev
->lock
);
7128 * md_set_array_info is used two different ways
7129 * The original usage is when creating a new array.
7130 * In this usage, raid_disks is > 0 and it together with
7131 * level, size, not_persistent,layout,chunksize determine the
7132 * shape of the array.
7133 * This will always create an array with a type-0.90.0 superblock.
7134 * The newer usage is when assembling an array.
7135 * In this case raid_disks will be 0, and the major_version field is
7136 * use to determine which style super-blocks are to be found on the devices.
7137 * The minor and patch _version numbers are also kept incase the
7138 * super_block handler wishes to interpret them.
7140 int md_set_array_info(struct mddev
*mddev
, struct mdu_array_info_s
*info
)
7142 if (info
->raid_disks
== 0) {
7143 /* just setting version number for superblock loading */
7144 if (info
->major_version
< 0 ||
7145 info
->major_version
>= ARRAY_SIZE(super_types
) ||
7146 super_types
[info
->major_version
].name
== NULL
) {
7147 /* maybe try to auto-load a module? */
7148 pr_warn("md: superblock version %d not known\n",
7149 info
->major_version
);
7152 mddev
->major_version
= info
->major_version
;
7153 mddev
->minor_version
= info
->minor_version
;
7154 mddev
->patch_version
= info
->patch_version
;
7155 mddev
->persistent
= !info
->not_persistent
;
7156 /* ensure mddev_put doesn't delete this now that there
7157 * is some minimal configuration.
7159 mddev
->ctime
= ktime_get_real_seconds();
7162 mddev
->major_version
= MD_MAJOR_VERSION
;
7163 mddev
->minor_version
= MD_MINOR_VERSION
;
7164 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
7165 mddev
->ctime
= ktime_get_real_seconds();
7167 mddev
->level
= info
->level
;
7168 mddev
->clevel
[0] = 0;
7169 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
7170 mddev
->raid_disks
= info
->raid_disks
;
7171 /* don't set md_minor, it is determined by which /dev/md* was
7174 if (info
->state
& (1<<MD_SB_CLEAN
))
7175 mddev
->recovery_cp
= MaxSector
;
7177 mddev
->recovery_cp
= 0;
7178 mddev
->persistent
= ! info
->not_persistent
;
7179 mddev
->external
= 0;
7181 mddev
->layout
= info
->layout
;
7182 if (mddev
->level
== 0)
7183 /* Cannot trust RAID0 layout info here */
7185 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
7187 if (mddev
->persistent
) {
7188 mddev
->max_disks
= MD_SB_DISKS
;
7190 mddev
->sb_flags
= 0;
7192 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
7194 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
7195 mddev
->bitmap_info
.default_space
= 64*2 - (MD_SB_BYTES
>> 9);
7196 mddev
->bitmap_info
.offset
= 0;
7198 mddev
->reshape_position
= MaxSector
;
7201 * Generate a 128 bit UUID
7203 get_random_bytes(mddev
->uuid
, 16);
7205 mddev
->new_level
= mddev
->level
;
7206 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
7207 mddev
->new_layout
= mddev
->layout
;
7208 mddev
->delta_disks
= 0;
7209 mddev
->reshape_backwards
= 0;
7214 void md_set_array_sectors(struct mddev
*mddev
, sector_t array_sectors
)
7216 lockdep_assert_held(&mddev
->reconfig_mutex
);
7218 if (mddev
->external_size
)
7221 mddev
->array_sectors
= array_sectors
;
7223 EXPORT_SYMBOL(md_set_array_sectors
);
7225 static int update_size(struct mddev
*mddev
, sector_t num_sectors
)
7227 struct md_rdev
*rdev
;
7229 int fit
= (num_sectors
== 0);
7230 sector_t old_dev_sectors
= mddev
->dev_sectors
;
7232 if (mddev
->pers
->resize
== NULL
)
7234 /* The "num_sectors" is the number of sectors of each device that
7235 * is used. This can only make sense for arrays with redundancy.
7236 * linear and raid0 always use whatever space is available. We can only
7237 * consider changing this number if no resync or reconstruction is
7238 * happening, and if the new size is acceptable. It must fit before the
7239 * sb_start or, if that is <data_offset, it must fit before the size
7240 * of each device. If num_sectors is zero, we find the largest size
7243 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
7249 rdev_for_each(rdev
, mddev
) {
7250 sector_t avail
= rdev
->sectors
;
7252 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
7253 num_sectors
= avail
;
7254 if (avail
< num_sectors
)
7257 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
7259 if (mddev_is_clustered(mddev
))
7260 md_cluster_ops
->update_size(mddev
, old_dev_sectors
);
7261 else if (mddev
->queue
) {
7262 set_capacity_and_notify(mddev
->gendisk
,
7263 mddev
->array_sectors
);
7269 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
)
7272 struct md_rdev
*rdev
;
7273 /* change the number of raid disks */
7274 if (mddev
->pers
->check_reshape
== NULL
)
7278 if (raid_disks
<= 0 ||
7279 (mddev
->max_disks
&& raid_disks
>= mddev
->max_disks
))
7281 if (mddev
->sync_thread
||
7282 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
7283 test_bit(MD_RESYNCING_REMOTE
, &mddev
->recovery
) ||
7284 mddev
->reshape_position
!= MaxSector
)
7287 rdev_for_each(rdev
, mddev
) {
7288 if (mddev
->raid_disks
< raid_disks
&&
7289 rdev
->data_offset
< rdev
->new_data_offset
)
7291 if (mddev
->raid_disks
> raid_disks
&&
7292 rdev
->data_offset
> rdev
->new_data_offset
)
7296 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
7297 if (mddev
->delta_disks
< 0)
7298 mddev
->reshape_backwards
= 1;
7299 else if (mddev
->delta_disks
> 0)
7300 mddev
->reshape_backwards
= 0;
7302 rv
= mddev
->pers
->check_reshape(mddev
);
7304 mddev
->delta_disks
= 0;
7305 mddev
->reshape_backwards
= 0;
7311 * update_array_info is used to change the configuration of an
7313 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
7314 * fields in the info are checked against the array.
7315 * Any differences that cannot be handled will cause an error.
7316 * Normally, only one change can be managed at a time.
7318 static int update_array_info(struct mddev
*mddev
, mdu_array_info_t
*info
)
7324 /* calculate expected state,ignoring low bits */
7325 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
7326 state
|= (1 << MD_SB_BITMAP_PRESENT
);
7328 if (mddev
->major_version
!= info
->major_version
||
7329 mddev
->minor_version
!= info
->minor_version
||
7330 /* mddev->patch_version != info->patch_version || */
7331 mddev
->ctime
!= info
->ctime
||
7332 mddev
->level
!= info
->level
||
7333 /* mddev->layout != info->layout || */
7334 mddev
->persistent
!= !info
->not_persistent
||
7335 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
7336 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
7337 ((state
^info
->state
) & 0xfffffe00)
7340 /* Check there is only one change */
7341 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
7343 if (mddev
->raid_disks
!= info
->raid_disks
)
7345 if (mddev
->layout
!= info
->layout
)
7347 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
7354 if (mddev
->layout
!= info
->layout
) {
7356 * we don't need to do anything at the md level, the
7357 * personality will take care of it all.
7359 if (mddev
->pers
->check_reshape
== NULL
)
7362 mddev
->new_layout
= info
->layout
;
7363 rv
= mddev
->pers
->check_reshape(mddev
);
7365 mddev
->new_layout
= mddev
->layout
;
7369 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
7370 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
7372 if (mddev
->raid_disks
!= info
->raid_disks
)
7373 rv
= update_raid_disks(mddev
, info
->raid_disks
);
7375 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
7376 if (mddev
->pers
->quiesce
== NULL
|| mddev
->thread
== NULL
) {
7380 if (mddev
->recovery
|| mddev
->sync_thread
) {
7384 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
7385 struct bitmap
*bitmap
;
7386 /* add the bitmap */
7387 if (mddev
->bitmap
) {
7391 if (mddev
->bitmap_info
.default_offset
== 0) {
7395 mddev
->bitmap_info
.offset
=
7396 mddev
->bitmap_info
.default_offset
;
7397 mddev
->bitmap_info
.space
=
7398 mddev
->bitmap_info
.default_space
;
7399 bitmap
= md_bitmap_create(mddev
, -1);
7400 mddev_suspend(mddev
);
7401 if (!IS_ERR(bitmap
)) {
7402 mddev
->bitmap
= bitmap
;
7403 rv
= md_bitmap_load(mddev
);
7405 rv
= PTR_ERR(bitmap
);
7407 md_bitmap_destroy(mddev
);
7408 mddev_resume(mddev
);
7410 /* remove the bitmap */
7411 if (!mddev
->bitmap
) {
7415 if (mddev
->bitmap
->storage
.file
) {
7419 if (mddev
->bitmap_info
.nodes
) {
7420 /* hold PW on all the bitmap lock */
7421 if (md_cluster_ops
->lock_all_bitmaps(mddev
) <= 0) {
7422 pr_warn("md: can't change bitmap to none since the array is in use by more than one node\n");
7424 md_cluster_ops
->unlock_all_bitmaps(mddev
);
7428 mddev
->bitmap_info
.nodes
= 0;
7429 md_cluster_ops
->leave(mddev
);
7430 module_put(md_cluster_mod
);
7431 mddev
->safemode_delay
= DEFAULT_SAFEMODE_DELAY
;
7433 mddev_suspend(mddev
);
7434 md_bitmap_destroy(mddev
);
7435 mddev_resume(mddev
);
7436 mddev
->bitmap_info
.offset
= 0;
7439 md_update_sb(mddev
, 1);
7445 static int set_disk_faulty(struct mddev
*mddev
, dev_t dev
)
7447 struct md_rdev
*rdev
;
7450 if (mddev
->pers
== NULL
)
7454 rdev
= md_find_rdev_rcu(mddev
, dev
);
7458 md_error(mddev
, rdev
);
7459 if (!test_bit(Faulty
, &rdev
->flags
))
7467 * We have a problem here : there is no easy way to give a CHS
7468 * virtual geometry. We currently pretend that we have a 2 heads
7469 * 4 sectors (with a BIG number of cylinders...). This drives
7470 * dosfs just mad... ;-)
7472 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
7474 struct mddev
*mddev
= bdev
->bd_disk
->private_data
;
7478 geo
->cylinders
= mddev
->array_sectors
/ 8;
7482 static inline bool md_ioctl_valid(unsigned int cmd
)
7486 case GET_ARRAY_INFO
:
7487 case GET_BITMAP_FILE
:
7490 case HOT_REMOVE_DISK
:
7492 case RESTART_ARRAY_RW
:
7494 case SET_ARRAY_INFO
:
7495 case SET_BITMAP_FILE
:
7496 case SET_DISK_FAULTY
:
7499 case CLUSTERED_DISK_NACK
:
7506 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
7507 unsigned int cmd
, unsigned long arg
)
7510 void __user
*argp
= (void __user
*)arg
;
7511 struct mddev
*mddev
= NULL
;
7512 bool did_set_md_closing
= false;
7514 if (!md_ioctl_valid(cmd
))
7519 case GET_ARRAY_INFO
:
7523 if (!capable(CAP_SYS_ADMIN
))
7528 * Commands dealing with the RAID driver but not any
7533 err
= get_version(argp
);
7539 * Commands creating/starting a new array:
7542 mddev
= bdev
->bd_disk
->private_data
;
7549 /* Some actions do not requires the mutex */
7551 case GET_ARRAY_INFO
:
7552 if (!mddev
->raid_disks
&& !mddev
->external
)
7555 err
= get_array_info(mddev
, argp
);
7559 if (!mddev
->raid_disks
&& !mddev
->external
)
7562 err
= get_disk_info(mddev
, argp
);
7565 case SET_DISK_FAULTY
:
7566 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
7569 case GET_BITMAP_FILE
:
7570 err
= get_bitmap_file(mddev
, argp
);
7575 if (cmd
== ADD_NEW_DISK
|| cmd
== HOT_ADD_DISK
)
7576 flush_rdev_wq(mddev
);
7578 if (cmd
== HOT_REMOVE_DISK
)
7579 /* need to ensure recovery thread has run */
7580 wait_event_interruptible_timeout(mddev
->sb_wait
,
7581 !test_bit(MD_RECOVERY_NEEDED
,
7583 msecs_to_jiffies(5000));
7584 if (cmd
== STOP_ARRAY
|| cmd
== STOP_ARRAY_RO
) {
7585 /* Need to flush page cache, and ensure no-one else opens
7588 mutex_lock(&mddev
->open_mutex
);
7589 if (mddev
->pers
&& atomic_read(&mddev
->openers
) > 1) {
7590 mutex_unlock(&mddev
->open_mutex
);
7594 if (test_and_set_bit(MD_CLOSING
, &mddev
->flags
)) {
7595 mutex_unlock(&mddev
->open_mutex
);
7599 did_set_md_closing
= true;
7600 mutex_unlock(&mddev
->open_mutex
);
7601 sync_blockdev(bdev
);
7603 err
= mddev_lock(mddev
);
7605 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7610 if (cmd
== SET_ARRAY_INFO
) {
7611 mdu_array_info_t info
;
7613 memset(&info
, 0, sizeof(info
));
7614 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
7619 err
= update_array_info(mddev
, &info
);
7621 pr_warn("md: couldn't update array info. %d\n", err
);
7626 if (!list_empty(&mddev
->disks
)) {
7627 pr_warn("md: array %s already has disks!\n", mdname(mddev
));
7631 if (mddev
->raid_disks
) {
7632 pr_warn("md: array %s already initialised!\n", mdname(mddev
));
7636 err
= md_set_array_info(mddev
, &info
);
7638 pr_warn("md: couldn't set array info. %d\n", err
);
7645 * Commands querying/configuring an existing array:
7647 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
7648 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
7649 if ((!mddev
->raid_disks
&& !mddev
->external
)
7650 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
7651 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
7652 && cmd
!= GET_BITMAP_FILE
) {
7658 * Commands even a read-only array can execute:
7661 case RESTART_ARRAY_RW
:
7662 err
= restart_array(mddev
);
7666 err
= do_md_stop(mddev
, 0, bdev
);
7670 err
= md_set_readonly(mddev
, bdev
);
7673 case HOT_REMOVE_DISK
:
7674 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
7678 /* We can support ADD_NEW_DISK on read-only arrays
7679 * only if we are re-adding a preexisting device.
7680 * So require mddev->pers and MD_DISK_SYNC.
7683 mdu_disk_info_t info
;
7684 if (copy_from_user(&info
, argp
, sizeof(info
)))
7686 else if (!(info
.state
& (1<<MD_DISK_SYNC
)))
7687 /* Need to clear read-only for this */
7690 err
= md_add_new_disk(mddev
, &info
);
7697 * The remaining ioctls are changing the state of the
7698 * superblock, so we do not allow them on read-only arrays.
7700 if (mddev
->ro
&& mddev
->pers
) {
7701 if (mddev
->ro
== 2) {
7703 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7704 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7705 /* mddev_unlock will wake thread */
7706 /* If a device failed while we were read-only, we
7707 * need to make sure the metadata is updated now.
7709 if (test_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
)) {
7710 mddev_unlock(mddev
);
7711 wait_event(mddev
->sb_wait
,
7712 !test_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
) &&
7713 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
7714 mddev_lock_nointr(mddev
);
7725 mdu_disk_info_t info
;
7726 if (copy_from_user(&info
, argp
, sizeof(info
)))
7729 err
= md_add_new_disk(mddev
, &info
);
7733 case CLUSTERED_DISK_NACK
:
7734 if (mddev_is_clustered(mddev
))
7735 md_cluster_ops
->new_disk_ack(mddev
, false);
7741 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
7745 err
= do_md_run(mddev
);
7748 case SET_BITMAP_FILE
:
7749 err
= set_bitmap_file(mddev
, (int)arg
);
7758 if (mddev
->hold_active
== UNTIL_IOCTL
&&
7760 mddev
->hold_active
= 0;
7761 mddev_unlock(mddev
);
7763 if(did_set_md_closing
)
7764 clear_bit(MD_CLOSING
, &mddev
->flags
);
7767 #ifdef CONFIG_COMPAT
7768 static int md_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
7769 unsigned int cmd
, unsigned long arg
)
7772 case HOT_REMOVE_DISK
:
7774 case SET_DISK_FAULTY
:
7775 case SET_BITMAP_FILE
:
7776 /* These take in integer arg, do not convert */
7779 arg
= (unsigned long)compat_ptr(arg
);
7783 return md_ioctl(bdev
, mode
, cmd
, arg
);
7785 #endif /* CONFIG_COMPAT */
7787 static int md_set_read_only(struct block_device
*bdev
, bool ro
)
7789 struct mddev
*mddev
= bdev
->bd_disk
->private_data
;
7792 err
= mddev_lock(mddev
);
7796 if (!mddev
->raid_disks
&& !mddev
->external
) {
7802 * Transitioning to read-auto need only happen for arrays that call
7803 * md_write_start and which are not ready for writes yet.
7805 if (!ro
&& mddev
->ro
== 1 && mddev
->pers
) {
7806 err
= restart_array(mddev
);
7813 mddev_unlock(mddev
);
7817 static int md_open(struct block_device
*bdev
, fmode_t mode
)
7820 * Succeed if we can lock the mddev, which confirms that
7821 * it isn't being stopped right now.
7823 struct mddev
*mddev
= mddev_find(bdev
->bd_dev
);
7829 if (mddev
->gendisk
!= bdev
->bd_disk
) {
7830 /* we are racing with mddev_put which is discarding this
7834 /* Wait until bdev->bd_disk is definitely gone */
7835 if (work_pending(&mddev
->del_work
))
7836 flush_workqueue(md_misc_wq
);
7837 /* Then retry the open from the top */
7838 return -ERESTARTSYS
;
7840 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
7842 if ((err
= mutex_lock_interruptible(&mddev
->open_mutex
)))
7845 if (test_bit(MD_CLOSING
, &mddev
->flags
)) {
7846 mutex_unlock(&mddev
->open_mutex
);
7852 atomic_inc(&mddev
->openers
);
7853 mutex_unlock(&mddev
->open_mutex
);
7855 bdev_check_media_change(bdev
);
7862 static void md_release(struct gendisk
*disk
, fmode_t mode
)
7864 struct mddev
*mddev
= disk
->private_data
;
7867 atomic_dec(&mddev
->openers
);
7871 static unsigned int md_check_events(struct gendisk
*disk
, unsigned int clearing
)
7873 struct mddev
*mddev
= disk
->private_data
;
7874 unsigned int ret
= 0;
7877 ret
= DISK_EVENT_MEDIA_CHANGE
;
7882 const struct block_device_operations md_fops
=
7884 .owner
= THIS_MODULE
,
7885 .submit_bio
= md_submit_bio
,
7887 .release
= md_release
,
7889 #ifdef CONFIG_COMPAT
7890 .compat_ioctl
= md_compat_ioctl
,
7892 .getgeo
= md_getgeo
,
7893 .check_events
= md_check_events
,
7894 .set_read_only
= md_set_read_only
,
7897 static int md_thread(void *arg
)
7899 struct md_thread
*thread
= arg
;
7902 * md_thread is a 'system-thread', it's priority should be very
7903 * high. We avoid resource deadlocks individually in each
7904 * raid personality. (RAID5 does preallocation) We also use RR and
7905 * the very same RT priority as kswapd, thus we will never get
7906 * into a priority inversion deadlock.
7908 * we definitely have to have equal or higher priority than
7909 * bdflush, otherwise bdflush will deadlock if there are too
7910 * many dirty RAID5 blocks.
7913 allow_signal(SIGKILL
);
7914 while (!kthread_should_stop()) {
7916 /* We need to wait INTERRUPTIBLE so that
7917 * we don't add to the load-average.
7918 * That means we need to be sure no signals are
7921 if (signal_pending(current
))
7922 flush_signals(current
);
7924 wait_event_interruptible_timeout
7926 test_bit(THREAD_WAKEUP
, &thread
->flags
)
7927 || kthread_should_stop() || kthread_should_park(),
7930 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
7931 if (kthread_should_park())
7933 if (!kthread_should_stop())
7934 thread
->run(thread
);
7940 void md_wakeup_thread(struct md_thread
*thread
)
7943 pr_debug("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
7944 set_bit(THREAD_WAKEUP
, &thread
->flags
);
7945 wake_up(&thread
->wqueue
);
7948 EXPORT_SYMBOL(md_wakeup_thread
);
7950 struct md_thread
*md_register_thread(void (*run
) (struct md_thread
*),
7951 struct mddev
*mddev
, const char *name
)
7953 struct md_thread
*thread
;
7955 thread
= kzalloc(sizeof(struct md_thread
), GFP_KERNEL
);
7959 init_waitqueue_head(&thread
->wqueue
);
7962 thread
->mddev
= mddev
;
7963 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
7964 thread
->tsk
= kthread_run(md_thread
, thread
,
7966 mdname(thread
->mddev
),
7968 if (IS_ERR(thread
->tsk
)) {
7974 EXPORT_SYMBOL(md_register_thread
);
7976 void md_unregister_thread(struct md_thread
**threadp
)
7978 struct md_thread
*thread
= *threadp
;
7981 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
7982 /* Locking ensures that mddev_unlock does not wake_up a
7983 * non-existent thread
7985 spin_lock(&pers_lock
);
7987 spin_unlock(&pers_lock
);
7989 kthread_stop(thread
->tsk
);
7992 EXPORT_SYMBOL(md_unregister_thread
);
7994 void md_error(struct mddev
*mddev
, struct md_rdev
*rdev
)
7996 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
7999 if (!mddev
->pers
|| !mddev
->pers
->error_handler
)
8001 mddev
->pers
->error_handler(mddev
,rdev
);
8002 if (mddev
->degraded
)
8003 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8004 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
8005 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8006 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8007 md_wakeup_thread(mddev
->thread
);
8008 if (mddev
->event_work
.func
)
8009 queue_work(md_misc_wq
, &mddev
->event_work
);
8010 md_new_event(mddev
);
8012 EXPORT_SYMBOL(md_error
);
8014 /* seq_file implementation /proc/mdstat */
8016 static void status_unused(struct seq_file
*seq
)
8019 struct md_rdev
*rdev
;
8021 seq_printf(seq
, "unused devices: ");
8023 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
8024 char b
[BDEVNAME_SIZE
];
8026 seq_printf(seq
, "%s ",
8027 bdevname(rdev
->bdev
,b
));
8030 seq_printf(seq
, "<none>");
8032 seq_printf(seq
, "\n");
8035 static int status_resync(struct seq_file
*seq
, struct mddev
*mddev
)
8037 sector_t max_sectors
, resync
, res
;
8038 unsigned long dt
, db
= 0;
8039 sector_t rt
, curr_mark_cnt
, resync_mark_cnt
;
8040 int scale
, recovery_active
;
8041 unsigned int per_milli
;
8043 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
8044 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
8045 max_sectors
= mddev
->resync_max_sectors
;
8047 max_sectors
= mddev
->dev_sectors
;
8049 resync
= mddev
->curr_resync
;
8051 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
8052 /* Still cleaning up */
8053 resync
= max_sectors
;
8054 } else if (resync
> max_sectors
)
8055 resync
= max_sectors
;
8057 resync
-= atomic_read(&mddev
->recovery_active
);
8060 if (test_bit(MD_RESYNCING_REMOTE
, &mddev
->recovery
)) {
8061 struct md_rdev
*rdev
;
8063 rdev_for_each(rdev
, mddev
)
8064 if (rdev
->raid_disk
>= 0 &&
8065 !test_bit(Faulty
, &rdev
->flags
) &&
8066 rdev
->recovery_offset
!= MaxSector
&&
8067 rdev
->recovery_offset
) {
8068 seq_printf(seq
, "\trecover=REMOTE");
8071 if (mddev
->reshape_position
!= MaxSector
)
8072 seq_printf(seq
, "\treshape=REMOTE");
8074 seq_printf(seq
, "\tresync=REMOTE");
8077 if (mddev
->recovery_cp
< MaxSector
) {
8078 seq_printf(seq
, "\tresync=PENDING");
8084 seq_printf(seq
, "\tresync=DELAYED");
8088 WARN_ON(max_sectors
== 0);
8089 /* Pick 'scale' such that (resync>>scale)*1000 will fit
8090 * in a sector_t, and (max_sectors>>scale) will fit in a
8091 * u32, as those are the requirements for sector_div.
8092 * Thus 'scale' must be at least 10
8095 if (sizeof(sector_t
) > sizeof(unsigned long)) {
8096 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
8099 res
= (resync
>>scale
)*1000;
8100 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
8104 int i
, x
= per_milli
/50, y
= 20-x
;
8105 seq_printf(seq
, "[");
8106 for (i
= 0; i
< x
; i
++)
8107 seq_printf(seq
, "=");
8108 seq_printf(seq
, ">");
8109 for (i
= 0; i
< y
; i
++)
8110 seq_printf(seq
, ".");
8111 seq_printf(seq
, "] ");
8113 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
8114 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
8116 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
8118 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
8119 "resync" : "recovery"))),
8120 per_milli
/10, per_milli
% 10,
8121 (unsigned long long) resync
/2,
8122 (unsigned long long) max_sectors
/2);
8125 * dt: time from mark until now
8126 * db: blocks written from mark until now
8127 * rt: remaining time
8129 * rt is a sector_t, which is always 64bit now. We are keeping
8130 * the original algorithm, but it is not really necessary.
8132 * Original algorithm:
8133 * So we divide before multiply in case it is 32bit and close
8135 * We scale the divisor (db) by 32 to avoid losing precision
8136 * near the end of resync when the number of remaining sectors
8138 * We then divide rt by 32 after multiplying by db to compensate.
8139 * The '+1' avoids division by zero if db is very small.
8141 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
8144 curr_mark_cnt
= mddev
->curr_mark_cnt
;
8145 recovery_active
= atomic_read(&mddev
->recovery_active
);
8146 resync_mark_cnt
= mddev
->resync_mark_cnt
;
8148 if (curr_mark_cnt
>= (recovery_active
+ resync_mark_cnt
))
8149 db
= curr_mark_cnt
- (recovery_active
+ resync_mark_cnt
);
8151 rt
= max_sectors
- resync
; /* number of remaining sectors */
8152 rt
= div64_u64(rt
, db
/32+1);
8156 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
8157 ((unsigned long)rt
% 60)/6);
8159 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
8163 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
8165 struct list_head
*tmp
;
8167 struct mddev
*mddev
;
8175 spin_lock(&all_mddevs_lock
);
8176 list_for_each(tmp
,&all_mddevs
)
8178 mddev
= list_entry(tmp
, struct mddev
, all_mddevs
);
8180 spin_unlock(&all_mddevs_lock
);
8183 spin_unlock(&all_mddevs_lock
);
8185 return (void*)2;/* tail */
8189 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
8191 struct list_head
*tmp
;
8192 struct mddev
*next_mddev
, *mddev
= v
;
8198 spin_lock(&all_mddevs_lock
);
8200 tmp
= all_mddevs
.next
;
8202 tmp
= mddev
->all_mddevs
.next
;
8203 if (tmp
!= &all_mddevs
)
8204 next_mddev
= mddev_get(list_entry(tmp
,struct mddev
,all_mddevs
));
8206 next_mddev
= (void*)2;
8209 spin_unlock(&all_mddevs_lock
);
8217 static void md_seq_stop(struct seq_file
*seq
, void *v
)
8219 struct mddev
*mddev
= v
;
8221 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
8225 static int md_seq_show(struct seq_file
*seq
, void *v
)
8227 struct mddev
*mddev
= v
;
8229 struct md_rdev
*rdev
;
8231 if (v
== (void*)1) {
8232 struct md_personality
*pers
;
8233 seq_printf(seq
, "Personalities : ");
8234 spin_lock(&pers_lock
);
8235 list_for_each_entry(pers
, &pers_list
, list
)
8236 seq_printf(seq
, "[%s] ", pers
->name
);
8238 spin_unlock(&pers_lock
);
8239 seq_printf(seq
, "\n");
8240 seq
->poll_event
= atomic_read(&md_event_count
);
8243 if (v
== (void*)2) {
8248 spin_lock(&mddev
->lock
);
8249 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
8250 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
8251 mddev
->pers
? "" : "in");
8254 seq_printf(seq
, " (read-only)");
8256 seq_printf(seq
, " (auto-read-only)");
8257 seq_printf(seq
, " %s", mddev
->pers
->name
);
8262 rdev_for_each_rcu(rdev
, mddev
) {
8263 char b
[BDEVNAME_SIZE
];
8264 seq_printf(seq
, " %s[%d]",
8265 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
8266 if (test_bit(WriteMostly
, &rdev
->flags
))
8267 seq_printf(seq
, "(W)");
8268 if (test_bit(Journal
, &rdev
->flags
))
8269 seq_printf(seq
, "(J)");
8270 if (test_bit(Faulty
, &rdev
->flags
)) {
8271 seq_printf(seq
, "(F)");
8274 if (rdev
->raid_disk
< 0)
8275 seq_printf(seq
, "(S)"); /* spare */
8276 if (test_bit(Replacement
, &rdev
->flags
))
8277 seq_printf(seq
, "(R)");
8278 sectors
+= rdev
->sectors
;
8282 if (!list_empty(&mddev
->disks
)) {
8284 seq_printf(seq
, "\n %llu blocks",
8285 (unsigned long long)
8286 mddev
->array_sectors
/ 2);
8288 seq_printf(seq
, "\n %llu blocks",
8289 (unsigned long long)sectors
/ 2);
8291 if (mddev
->persistent
) {
8292 if (mddev
->major_version
!= 0 ||
8293 mddev
->minor_version
!= 90) {
8294 seq_printf(seq
," super %d.%d",
8295 mddev
->major_version
,
8296 mddev
->minor_version
);
8298 } else if (mddev
->external
)
8299 seq_printf(seq
, " super external:%s",
8300 mddev
->metadata_type
);
8302 seq_printf(seq
, " super non-persistent");
8305 mddev
->pers
->status(seq
, mddev
);
8306 seq_printf(seq
, "\n ");
8307 if (mddev
->pers
->sync_request
) {
8308 if (status_resync(seq
, mddev
))
8309 seq_printf(seq
, "\n ");
8312 seq_printf(seq
, "\n ");
8314 md_bitmap_status(seq
, mddev
->bitmap
);
8316 seq_printf(seq
, "\n");
8318 spin_unlock(&mddev
->lock
);
8323 static const struct seq_operations md_seq_ops
= {
8324 .start
= md_seq_start
,
8325 .next
= md_seq_next
,
8326 .stop
= md_seq_stop
,
8327 .show
= md_seq_show
,
8330 static int md_seq_open(struct inode
*inode
, struct file
*file
)
8332 struct seq_file
*seq
;
8335 error
= seq_open(file
, &md_seq_ops
);
8339 seq
= file
->private_data
;
8340 seq
->poll_event
= atomic_read(&md_event_count
);
8344 static int md_unloading
;
8345 static __poll_t
mdstat_poll(struct file
*filp
, poll_table
*wait
)
8347 struct seq_file
*seq
= filp
->private_data
;
8351 return EPOLLIN
|EPOLLRDNORM
|EPOLLERR
|EPOLLPRI
;
8352 poll_wait(filp
, &md_event_waiters
, wait
);
8354 /* always allow read */
8355 mask
= EPOLLIN
| EPOLLRDNORM
;
8357 if (seq
->poll_event
!= atomic_read(&md_event_count
))
8358 mask
|= EPOLLERR
| EPOLLPRI
;
8362 static const struct proc_ops mdstat_proc_ops
= {
8363 .proc_open
= md_seq_open
,
8364 .proc_read
= seq_read
,
8365 .proc_lseek
= seq_lseek
,
8366 .proc_release
= seq_release
,
8367 .proc_poll
= mdstat_poll
,
8370 int register_md_personality(struct md_personality
*p
)
8372 pr_debug("md: %s personality registered for level %d\n",
8374 spin_lock(&pers_lock
);
8375 list_add_tail(&p
->list
, &pers_list
);
8376 spin_unlock(&pers_lock
);
8379 EXPORT_SYMBOL(register_md_personality
);
8381 int unregister_md_personality(struct md_personality
*p
)
8383 pr_debug("md: %s personality unregistered\n", p
->name
);
8384 spin_lock(&pers_lock
);
8385 list_del_init(&p
->list
);
8386 spin_unlock(&pers_lock
);
8389 EXPORT_SYMBOL(unregister_md_personality
);
8391 int register_md_cluster_operations(struct md_cluster_operations
*ops
,
8392 struct module
*module
)
8395 spin_lock(&pers_lock
);
8396 if (md_cluster_ops
!= NULL
)
8399 md_cluster_ops
= ops
;
8400 md_cluster_mod
= module
;
8402 spin_unlock(&pers_lock
);
8405 EXPORT_SYMBOL(register_md_cluster_operations
);
8407 int unregister_md_cluster_operations(void)
8409 spin_lock(&pers_lock
);
8410 md_cluster_ops
= NULL
;
8411 spin_unlock(&pers_lock
);
8414 EXPORT_SYMBOL(unregister_md_cluster_operations
);
8416 int md_setup_cluster(struct mddev
*mddev
, int nodes
)
8419 if (!md_cluster_ops
)
8420 request_module("md-cluster");
8421 spin_lock(&pers_lock
);
8422 /* ensure module won't be unloaded */
8423 if (!md_cluster_ops
|| !try_module_get(md_cluster_mod
)) {
8424 pr_warn("can't find md-cluster module or get it's reference.\n");
8425 spin_unlock(&pers_lock
);
8428 spin_unlock(&pers_lock
);
8430 ret
= md_cluster_ops
->join(mddev
, nodes
);
8432 mddev
->safemode_delay
= 0;
8436 void md_cluster_stop(struct mddev
*mddev
)
8438 if (!md_cluster_ops
)
8440 md_cluster_ops
->leave(mddev
);
8441 module_put(md_cluster_mod
);
8444 static int is_mddev_idle(struct mddev
*mddev
, int init
)
8446 struct md_rdev
*rdev
;
8452 rdev_for_each_rcu(rdev
, mddev
) {
8453 struct gendisk
*disk
= rdev
->bdev
->bd_disk
;
8454 curr_events
= (int)part_stat_read_accum(disk
->part0
, sectors
) -
8455 atomic_read(&disk
->sync_io
);
8456 /* sync IO will cause sync_io to increase before the disk_stats
8457 * as sync_io is counted when a request starts, and
8458 * disk_stats is counted when it completes.
8459 * So resync activity will cause curr_events to be smaller than
8460 * when there was no such activity.
8461 * non-sync IO will cause disk_stat to increase without
8462 * increasing sync_io so curr_events will (eventually)
8463 * be larger than it was before. Once it becomes
8464 * substantially larger, the test below will cause
8465 * the array to appear non-idle, and resync will slow
8467 * If there is a lot of outstanding resync activity when
8468 * we set last_event to curr_events, then all that activity
8469 * completing might cause the array to appear non-idle
8470 * and resync will be slowed down even though there might
8471 * not have been non-resync activity. This will only
8472 * happen once though. 'last_events' will soon reflect
8473 * the state where there is little or no outstanding
8474 * resync requests, and further resync activity will
8475 * always make curr_events less than last_events.
8478 if (init
|| curr_events
- rdev
->last_events
> 64) {
8479 rdev
->last_events
= curr_events
;
8487 void md_done_sync(struct mddev
*mddev
, int blocks
, int ok
)
8489 /* another "blocks" (512byte) blocks have been synced */
8490 atomic_sub(blocks
, &mddev
->recovery_active
);
8491 wake_up(&mddev
->recovery_wait
);
8493 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8494 set_bit(MD_RECOVERY_ERROR
, &mddev
->recovery
);
8495 md_wakeup_thread(mddev
->thread
);
8496 // stop recovery, signal do_sync ....
8499 EXPORT_SYMBOL(md_done_sync
);
8501 /* md_write_start(mddev, bi)
8502 * If we need to update some array metadata (e.g. 'active' flag
8503 * in superblock) before writing, schedule a superblock update
8504 * and wait for it to complete.
8505 * A return value of 'false' means that the write wasn't recorded
8506 * and cannot proceed as the array is being suspend.
8508 bool md_write_start(struct mddev
*mddev
, struct bio
*bi
)
8512 if (bio_data_dir(bi
) != WRITE
)
8515 BUG_ON(mddev
->ro
== 1);
8516 if (mddev
->ro
== 2) {
8517 /* need to switch to read/write */
8519 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8520 md_wakeup_thread(mddev
->thread
);
8521 md_wakeup_thread(mddev
->sync_thread
);
8525 percpu_ref_get(&mddev
->writes_pending
);
8526 smp_mb(); /* Match smp_mb in set_in_sync() */
8527 if (mddev
->safemode
== 1)
8528 mddev
->safemode
= 0;
8529 /* sync_checkers is always 0 when writes_pending is in per-cpu mode */
8530 if (mddev
->in_sync
|| mddev
->sync_checkers
) {
8531 spin_lock(&mddev
->lock
);
8532 if (mddev
->in_sync
) {
8534 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
8535 set_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
8536 md_wakeup_thread(mddev
->thread
);
8539 spin_unlock(&mddev
->lock
);
8543 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
8544 if (!mddev
->has_superblocks
)
8546 wait_event(mddev
->sb_wait
,
8547 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
) ||
8549 if (test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
)) {
8550 percpu_ref_put(&mddev
->writes_pending
);
8555 EXPORT_SYMBOL(md_write_start
);
8557 /* md_write_inc can only be called when md_write_start() has
8558 * already been called at least once of the current request.
8559 * It increments the counter and is useful when a single request
8560 * is split into several parts. Each part causes an increment and
8561 * so needs a matching md_write_end().
8562 * Unlike md_write_start(), it is safe to call md_write_inc() inside
8563 * a spinlocked region.
8565 void md_write_inc(struct mddev
*mddev
, struct bio
*bi
)
8567 if (bio_data_dir(bi
) != WRITE
)
8569 WARN_ON_ONCE(mddev
->in_sync
|| mddev
->ro
);
8570 percpu_ref_get(&mddev
->writes_pending
);
8572 EXPORT_SYMBOL(md_write_inc
);
8574 void md_write_end(struct mddev
*mddev
)
8576 percpu_ref_put(&mddev
->writes_pending
);
8578 if (mddev
->safemode
== 2)
8579 md_wakeup_thread(mddev
->thread
);
8580 else if (mddev
->safemode_delay
)
8581 /* The roundup() ensures this only performs locking once
8582 * every ->safemode_delay jiffies
8584 mod_timer(&mddev
->safemode_timer
,
8585 roundup(jiffies
, mddev
->safemode_delay
) +
8586 mddev
->safemode_delay
);
8589 EXPORT_SYMBOL(md_write_end
);
8591 /* md_allow_write(mddev)
8592 * Calling this ensures that the array is marked 'active' so that writes
8593 * may proceed without blocking. It is important to call this before
8594 * attempting a GFP_KERNEL allocation while holding the mddev lock.
8595 * Must be called with mddev_lock held.
8597 void md_allow_write(struct mddev
*mddev
)
8603 if (!mddev
->pers
->sync_request
)
8606 spin_lock(&mddev
->lock
);
8607 if (mddev
->in_sync
) {
8609 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
8610 set_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
8611 if (mddev
->safemode_delay
&&
8612 mddev
->safemode
== 0)
8613 mddev
->safemode
= 1;
8614 spin_unlock(&mddev
->lock
);
8615 md_update_sb(mddev
, 0);
8616 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
8617 /* wait for the dirty state to be recorded in the metadata */
8618 wait_event(mddev
->sb_wait
,
8619 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
8621 spin_unlock(&mddev
->lock
);
8623 EXPORT_SYMBOL_GPL(md_allow_write
);
8625 #define SYNC_MARKS 10
8626 #define SYNC_MARK_STEP (3*HZ)
8627 #define UPDATE_FREQUENCY (5*60*HZ)
8628 void md_do_sync(struct md_thread
*thread
)
8630 struct mddev
*mddev
= thread
->mddev
;
8631 struct mddev
*mddev2
;
8632 unsigned int currspeed
= 0, window
;
8633 sector_t max_sectors
,j
, io_sectors
, recovery_done
;
8634 unsigned long mark
[SYNC_MARKS
];
8635 unsigned long update_time
;
8636 sector_t mark_cnt
[SYNC_MARKS
];
8638 struct list_head
*tmp
;
8639 sector_t last_check
;
8641 struct md_rdev
*rdev
;
8642 char *desc
, *action
= NULL
;
8643 struct blk_plug plug
;
8646 /* just incase thread restarts... */
8647 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
8648 test_bit(MD_RECOVERY_WAIT
, &mddev
->recovery
))
8650 if (mddev
->ro
) {/* never try to sync a read-only array */
8651 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8655 if (mddev_is_clustered(mddev
)) {
8656 ret
= md_cluster_ops
->resync_start(mddev
);
8660 set_bit(MD_CLUSTER_RESYNC_LOCKED
, &mddev
->flags
);
8661 if (!(test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
8662 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) ||
8663 test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
8664 && ((unsigned long long)mddev
->curr_resync_completed
8665 < (unsigned long long)mddev
->resync_max_sectors
))
8669 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
8670 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)) {
8671 desc
= "data-check";
8673 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
8674 desc
= "requested-resync";
8678 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
8683 mddev
->last_sync_action
= action
?: desc
;
8685 /* we overload curr_resync somewhat here.
8686 * 0 == not engaged in resync at all
8687 * 2 == checking that there is no conflict with another sync
8688 * 1 == like 2, but have yielded to allow conflicting resync to
8690 * other == active in resync - this many blocks
8692 * Before starting a resync we must have set curr_resync to
8693 * 2, and then checked that every "conflicting" array has curr_resync
8694 * less than ours. When we find one that is the same or higher
8695 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8696 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8697 * This will mean we have to start checking from the beginning again.
8702 int mddev2_minor
= -1;
8703 mddev
->curr_resync
= 2;
8706 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8708 for_each_mddev(mddev2
, tmp
) {
8709 if (mddev2
== mddev
)
8711 if (!mddev
->parallel_resync
8712 && mddev2
->curr_resync
8713 && match_mddev_units(mddev
, mddev2
)) {
8715 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
8716 /* arbitrarily yield */
8717 mddev
->curr_resync
= 1;
8718 wake_up(&resync_wait
);
8720 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
8721 /* no need to wait here, we can wait the next
8722 * time 'round when curr_resync == 2
8725 /* We need to wait 'interruptible' so as not to
8726 * contribute to the load average, and not to
8727 * be caught by 'softlockup'
8729 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
8730 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
8731 mddev2
->curr_resync
>= mddev
->curr_resync
) {
8732 if (mddev2_minor
!= mddev2
->md_minor
) {
8733 mddev2_minor
= mddev2
->md_minor
;
8734 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8735 desc
, mdname(mddev
),
8739 if (signal_pending(current
))
8740 flush_signals(current
);
8742 finish_wait(&resync_wait
, &wq
);
8745 finish_wait(&resync_wait
, &wq
);
8748 } while (mddev
->curr_resync
< 2);
8751 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
8752 /* resync follows the size requested by the personality,
8753 * which defaults to physical size, but can be virtual size
8755 max_sectors
= mddev
->resync_max_sectors
;
8756 atomic64_set(&mddev
->resync_mismatches
, 0);
8757 /* we don't use the checkpoint if there's a bitmap */
8758 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
8759 j
= mddev
->resync_min
;
8760 else if (!mddev
->bitmap
)
8761 j
= mddev
->recovery_cp
;
8763 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)) {
8764 max_sectors
= mddev
->resync_max_sectors
;
8766 * If the original node aborts reshaping then we continue the
8767 * reshaping, so set j again to avoid restart reshape from the
8770 if (mddev_is_clustered(mddev
) &&
8771 mddev
->reshape_position
!= MaxSector
)
8772 j
= mddev
->reshape_position
;
8774 /* recovery follows the physical size of devices */
8775 max_sectors
= mddev
->dev_sectors
;
8778 rdev_for_each_rcu(rdev
, mddev
)
8779 if (rdev
->raid_disk
>= 0 &&
8780 !test_bit(Journal
, &rdev
->flags
) &&
8781 !test_bit(Faulty
, &rdev
->flags
) &&
8782 !test_bit(In_sync
, &rdev
->flags
) &&
8783 rdev
->recovery_offset
< j
)
8784 j
= rdev
->recovery_offset
;
8787 /* If there is a bitmap, we need to make sure all
8788 * writes that started before we added a spare
8789 * complete before we start doing a recovery.
8790 * Otherwise the write might complete and (via
8791 * bitmap_endwrite) set a bit in the bitmap after the
8792 * recovery has checked that bit and skipped that
8795 if (mddev
->bitmap
) {
8796 mddev
->pers
->quiesce(mddev
, 1);
8797 mddev
->pers
->quiesce(mddev
, 0);
8801 pr_info("md: %s of RAID array %s\n", desc
, mdname(mddev
));
8802 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev
));
8803 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8804 speed_max(mddev
), desc
);
8806 is_mddev_idle(mddev
, 1); /* this initializes IO event counters */
8809 for (m
= 0; m
< SYNC_MARKS
; m
++) {
8811 mark_cnt
[m
] = io_sectors
;
8814 mddev
->resync_mark
= mark
[last_mark
];
8815 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
8818 * Tune reconstruction:
8820 window
= 32 * (PAGE_SIZE
/ 512);
8821 pr_debug("md: using %dk window, over a total of %lluk.\n",
8822 window
/2, (unsigned long long)max_sectors
/2);
8824 atomic_set(&mddev
->recovery_active
, 0);
8828 pr_debug("md: resuming %s of %s from checkpoint.\n",
8829 desc
, mdname(mddev
));
8830 mddev
->curr_resync
= j
;
8832 mddev
->curr_resync
= 3; /* no longer delayed */
8833 mddev
->curr_resync_completed
= j
;
8834 sysfs_notify_dirent_safe(mddev
->sysfs_completed
);
8835 md_new_event(mddev
);
8836 update_time
= jiffies
;
8838 blk_start_plug(&plug
);
8839 while (j
< max_sectors
) {
8844 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8845 ((mddev
->curr_resync
> mddev
->curr_resync_completed
&&
8846 (mddev
->curr_resync
- mddev
->curr_resync_completed
)
8847 > (max_sectors
>> 4)) ||
8848 time_after_eq(jiffies
, update_time
+ UPDATE_FREQUENCY
) ||
8849 (j
- mddev
->curr_resync_completed
)*2
8850 >= mddev
->resync_max
- mddev
->curr_resync_completed
||
8851 mddev
->curr_resync_completed
> mddev
->resync_max
8853 /* time to update curr_resync_completed */
8854 wait_event(mddev
->recovery_wait
,
8855 atomic_read(&mddev
->recovery_active
) == 0);
8856 mddev
->curr_resync_completed
= j
;
8857 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) &&
8858 j
> mddev
->recovery_cp
)
8859 mddev
->recovery_cp
= j
;
8860 update_time
= jiffies
;
8861 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
8862 sysfs_notify_dirent_safe(mddev
->sysfs_completed
);
8865 while (j
>= mddev
->resync_max
&&
8866 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
8867 /* As this condition is controlled by user-space,
8868 * we can block indefinitely, so use '_interruptible'
8869 * to avoid triggering warnings.
8871 flush_signals(current
); /* just in case */
8872 wait_event_interruptible(mddev
->recovery_wait
,
8873 mddev
->resync_max
> j
8874 || test_bit(MD_RECOVERY_INTR
,
8878 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8881 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
);
8883 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8887 if (!skipped
) { /* actual IO requested */
8888 io_sectors
+= sectors
;
8889 atomic_add(sectors
, &mddev
->recovery_active
);
8892 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8896 if (j
> max_sectors
)
8897 /* when skipping, extra large numbers can be returned. */
8900 mddev
->curr_resync
= j
;
8901 mddev
->curr_mark_cnt
= io_sectors
;
8902 if (last_check
== 0)
8903 /* this is the earliest that rebuild will be
8904 * visible in /proc/mdstat
8906 md_new_event(mddev
);
8908 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
8911 last_check
= io_sectors
;
8913 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
8915 int next
= (last_mark
+1) % SYNC_MARKS
;
8917 mddev
->resync_mark
= mark
[next
];
8918 mddev
->resync_mark_cnt
= mark_cnt
[next
];
8919 mark
[next
] = jiffies
;
8920 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
8924 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8928 * this loop exits only if either when we are slower than
8929 * the 'hard' speed limit, or the system was IO-idle for
8931 * the system might be non-idle CPU-wise, but we only care
8932 * about not overloading the IO subsystem. (things like an
8933 * e2fsck being done on the RAID array should execute fast)
8937 recovery_done
= io_sectors
- atomic_read(&mddev
->recovery_active
);
8938 currspeed
= ((unsigned long)(recovery_done
- mddev
->resync_mark_cnt
))/2
8939 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
8941 if (currspeed
> speed_min(mddev
)) {
8942 if (currspeed
> speed_max(mddev
)) {
8946 if (!is_mddev_idle(mddev
, 0)) {
8948 * Give other IO more of a chance.
8949 * The faster the devices, the less we wait.
8951 wait_event(mddev
->recovery_wait
,
8952 !atomic_read(&mddev
->recovery_active
));
8956 pr_info("md: %s: %s %s.\n",mdname(mddev
), desc
,
8957 test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)
8958 ? "interrupted" : "done");
8960 * this also signals 'finished resyncing' to md_stop
8962 blk_finish_plug(&plug
);
8963 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
8965 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8966 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
8967 mddev
->curr_resync
> 3) {
8968 mddev
->curr_resync_completed
= mddev
->curr_resync
;
8969 sysfs_notify_dirent_safe(mddev
->sysfs_completed
);
8971 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
);
8973 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
8974 mddev
->curr_resync
> 3) {
8975 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
8976 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
8977 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
8978 pr_debug("md: checkpointing %s of %s.\n",
8979 desc
, mdname(mddev
));
8980 if (test_bit(MD_RECOVERY_ERROR
,
8982 mddev
->recovery_cp
=
8983 mddev
->curr_resync_completed
;
8985 mddev
->recovery_cp
=
8989 mddev
->recovery_cp
= MaxSector
;
8991 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8992 mddev
->curr_resync
= MaxSector
;
8993 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8994 test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
)) {
8996 rdev_for_each_rcu(rdev
, mddev
)
8997 if (rdev
->raid_disk
>= 0 &&
8998 mddev
->delta_disks
>= 0 &&
8999 !test_bit(Journal
, &rdev
->flags
) &&
9000 !test_bit(Faulty
, &rdev
->flags
) &&
9001 !test_bit(In_sync
, &rdev
->flags
) &&
9002 rdev
->recovery_offset
< mddev
->curr_resync
)
9003 rdev
->recovery_offset
= mddev
->curr_resync
;
9009 /* set CHANGE_PENDING here since maybe another update is needed,
9010 * so other nodes are informed. It should be harmless for normal
9012 set_mask_bits(&mddev
->sb_flags
, 0,
9013 BIT(MD_SB_CHANGE_PENDING
) | BIT(MD_SB_CHANGE_DEVS
));
9015 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
9016 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
9017 mddev
->delta_disks
> 0 &&
9018 mddev
->pers
->finish_reshape
&&
9019 mddev
->pers
->size
&&
9021 mddev_lock_nointr(mddev
);
9022 md_set_array_sectors(mddev
, mddev
->pers
->size(mddev
, 0, 0));
9023 mddev_unlock(mddev
);
9024 if (!mddev_is_clustered(mddev
))
9025 set_capacity_and_notify(mddev
->gendisk
,
9026 mddev
->array_sectors
);
9029 spin_lock(&mddev
->lock
);
9030 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
9031 /* We completed so min/max setting can be forgotten if used. */
9032 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
9033 mddev
->resync_min
= 0;
9034 mddev
->resync_max
= MaxSector
;
9035 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
9036 mddev
->resync_min
= mddev
->curr_resync_completed
;
9037 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
9038 mddev
->curr_resync
= 0;
9039 spin_unlock(&mddev
->lock
);
9041 wake_up(&resync_wait
);
9042 md_wakeup_thread(mddev
->thread
);
9045 EXPORT_SYMBOL_GPL(md_do_sync
);
9047 static int remove_and_add_spares(struct mddev
*mddev
,
9048 struct md_rdev
*this)
9050 struct md_rdev
*rdev
;
9053 bool remove_some
= false;
9055 if (this && test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
9056 /* Mustn't remove devices when resync thread is running */
9059 rdev_for_each(rdev
, mddev
) {
9060 if ((this == NULL
|| rdev
== this) &&
9061 rdev
->raid_disk
>= 0 &&
9062 !test_bit(Blocked
, &rdev
->flags
) &&
9063 test_bit(Faulty
, &rdev
->flags
) &&
9064 atomic_read(&rdev
->nr_pending
)==0) {
9065 /* Faulty non-Blocked devices with nr_pending == 0
9066 * never get nr_pending incremented,
9067 * never get Faulty cleared, and never get Blocked set.
9068 * So we can synchronize_rcu now rather than once per device
9071 set_bit(RemoveSynchronized
, &rdev
->flags
);
9077 rdev_for_each(rdev
, mddev
) {
9078 if ((this == NULL
|| rdev
== this) &&
9079 rdev
->raid_disk
>= 0 &&
9080 !test_bit(Blocked
, &rdev
->flags
) &&
9081 ((test_bit(RemoveSynchronized
, &rdev
->flags
) ||
9082 (!test_bit(In_sync
, &rdev
->flags
) &&
9083 !test_bit(Journal
, &rdev
->flags
))) &&
9084 atomic_read(&rdev
->nr_pending
)==0)) {
9085 if (mddev
->pers
->hot_remove_disk(
9086 mddev
, rdev
) == 0) {
9087 sysfs_unlink_rdev(mddev
, rdev
);
9088 rdev
->saved_raid_disk
= rdev
->raid_disk
;
9089 rdev
->raid_disk
= -1;
9093 if (remove_some
&& test_bit(RemoveSynchronized
, &rdev
->flags
))
9094 clear_bit(RemoveSynchronized
, &rdev
->flags
);
9097 if (removed
&& mddev
->kobj
.sd
)
9098 sysfs_notify_dirent_safe(mddev
->sysfs_degraded
);
9100 if (this && removed
)
9103 rdev_for_each(rdev
, mddev
) {
9104 if (this && this != rdev
)
9106 if (test_bit(Candidate
, &rdev
->flags
))
9108 if (rdev
->raid_disk
>= 0 &&
9109 !test_bit(In_sync
, &rdev
->flags
) &&
9110 !test_bit(Journal
, &rdev
->flags
) &&
9111 !test_bit(Faulty
, &rdev
->flags
))
9113 if (rdev
->raid_disk
>= 0)
9115 if (test_bit(Faulty
, &rdev
->flags
))
9117 if (!test_bit(Journal
, &rdev
->flags
)) {
9119 ! (rdev
->saved_raid_disk
>= 0 &&
9120 !test_bit(Bitmap_sync
, &rdev
->flags
)))
9123 rdev
->recovery_offset
= 0;
9125 if (mddev
->pers
->hot_add_disk(mddev
, rdev
) == 0) {
9126 /* failure here is OK */
9127 sysfs_link_rdev(mddev
, rdev
);
9128 if (!test_bit(Journal
, &rdev
->flags
))
9130 md_new_event(mddev
);
9131 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
9136 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
9140 static void md_start_sync(struct work_struct
*ws
)
9142 struct mddev
*mddev
= container_of(ws
, struct mddev
, del_work
);
9144 mddev
->sync_thread
= md_register_thread(md_do_sync
,
9147 if (!mddev
->sync_thread
) {
9148 pr_warn("%s: could not start resync thread...\n",
9150 /* leave the spares where they are, it shouldn't hurt */
9151 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
9152 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
9153 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
9154 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
9155 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
9156 wake_up(&resync_wait
);
9157 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
9159 if (mddev
->sysfs_action
)
9160 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
9162 md_wakeup_thread(mddev
->sync_thread
);
9163 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
9164 md_new_event(mddev
);
9168 * This routine is regularly called by all per-raid-array threads to
9169 * deal with generic issues like resync and super-block update.
9170 * Raid personalities that don't have a thread (linear/raid0) do not
9171 * need this as they never do any recovery or update the superblock.
9173 * It does not do any resync itself, but rather "forks" off other threads
9174 * to do that as needed.
9175 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
9176 * "->recovery" and create a thread at ->sync_thread.
9177 * When the thread finishes it sets MD_RECOVERY_DONE
9178 * and wakeups up this thread which will reap the thread and finish up.
9179 * This thread also removes any faulty devices (with nr_pending == 0).
9181 * The overall approach is:
9182 * 1/ if the superblock needs updating, update it.
9183 * 2/ If a recovery thread is running, don't do anything else.
9184 * 3/ If recovery has finished, clean up, possibly marking spares active.
9185 * 4/ If there are any faulty devices, remove them.
9186 * 5/ If array is degraded, try to add spares devices
9187 * 6/ If array has spares or is not in-sync, start a resync thread.
9189 void md_check_recovery(struct mddev
*mddev
)
9191 if (test_bit(MD_ALLOW_SB_UPDATE
, &mddev
->flags
) && mddev
->sb_flags
) {
9192 /* Write superblock - thread that called mddev_suspend()
9193 * holds reconfig_mutex for us.
9195 set_bit(MD_UPDATING_SB
, &mddev
->flags
);
9196 smp_mb__after_atomic();
9197 if (test_bit(MD_ALLOW_SB_UPDATE
, &mddev
->flags
))
9198 md_update_sb(mddev
, 0);
9199 clear_bit_unlock(MD_UPDATING_SB
, &mddev
->flags
);
9200 wake_up(&mddev
->sb_wait
);
9203 if (mddev
->suspended
)
9207 md_bitmap_daemon_work(mddev
);
9209 if (signal_pending(current
)) {
9210 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
9211 pr_debug("md: %s in immediate safe mode\n",
9213 mddev
->safemode
= 2;
9215 flush_signals(current
);
9218 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
9221 (mddev
->sb_flags
& ~ (1<<MD_SB_CHANGE_PENDING
)) ||
9222 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
9223 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
9224 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
9225 (mddev
->safemode
== 2
9226 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
9230 if (mddev_trylock(mddev
)) {
9232 bool try_set_sync
= mddev
->safemode
!= 0;
9234 if (!mddev
->external
&& mddev
->safemode
== 1)
9235 mddev
->safemode
= 0;
9238 struct md_rdev
*rdev
;
9239 if (!mddev
->external
&& mddev
->in_sync
)
9240 /* 'Blocked' flag not needed as failed devices
9241 * will be recorded if array switched to read/write.
9242 * Leaving it set will prevent the device
9243 * from being removed.
9245 rdev_for_each(rdev
, mddev
)
9246 clear_bit(Blocked
, &rdev
->flags
);
9247 /* On a read-only array we can:
9248 * - remove failed devices
9249 * - add already-in_sync devices if the array itself
9251 * As we only add devices that are already in-sync,
9252 * we can activate the spares immediately.
9254 remove_and_add_spares(mddev
, NULL
);
9255 /* There is no thread, but we need to call
9256 * ->spare_active and clear saved_raid_disk
9258 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
9259 md_reap_sync_thread(mddev
);
9260 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
9261 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
9262 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
9266 if (mddev_is_clustered(mddev
)) {
9267 struct md_rdev
*rdev
;
9268 /* kick the device if another node issued a
9271 rdev_for_each(rdev
, mddev
) {
9272 if (test_and_clear_bit(ClusterRemove
, &rdev
->flags
) &&
9273 rdev
->raid_disk
< 0)
9274 md_kick_rdev_from_array(rdev
);
9278 if (try_set_sync
&& !mddev
->external
&& !mddev
->in_sync
) {
9279 spin_lock(&mddev
->lock
);
9281 spin_unlock(&mddev
->lock
);
9284 if (mddev
->sb_flags
)
9285 md_update_sb(mddev
, 0);
9287 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
9288 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
9289 /* resync/recovery still happening */
9290 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
9293 if (mddev
->sync_thread
) {
9294 md_reap_sync_thread(mddev
);
9297 /* Set RUNNING before clearing NEEDED to avoid
9298 * any transients in the value of "sync_action".
9300 mddev
->curr_resync_completed
= 0;
9301 spin_lock(&mddev
->lock
);
9302 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
9303 spin_unlock(&mddev
->lock
);
9304 /* Clear some bits that don't mean anything, but
9307 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
9308 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
9310 if (!test_and_clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
9311 test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
9313 /* no recovery is running.
9314 * remove any failed drives, then
9315 * add spares if possible.
9316 * Spares are also removed and re-added, to allow
9317 * the personality to fail the re-add.
9320 if (mddev
->reshape_position
!= MaxSector
) {
9321 if (mddev
->pers
->check_reshape
== NULL
||
9322 mddev
->pers
->check_reshape(mddev
) != 0)
9323 /* Cannot proceed */
9325 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
9326 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
9327 } else if ((spares
= remove_and_add_spares(mddev
, NULL
))) {
9328 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
9329 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
9330 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
9331 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
9332 } else if (mddev
->recovery_cp
< MaxSector
) {
9333 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
9334 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
9335 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
9336 /* nothing to be done ... */
9339 if (mddev
->pers
->sync_request
) {
9341 /* We are adding a device or devices to an array
9342 * which has the bitmap stored on all devices.
9343 * So make sure all bitmap pages get written
9345 md_bitmap_write_all(mddev
->bitmap
);
9347 INIT_WORK(&mddev
->del_work
, md_start_sync
);
9348 queue_work(md_misc_wq
, &mddev
->del_work
);
9352 if (!mddev
->sync_thread
) {
9353 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
9354 wake_up(&resync_wait
);
9355 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
9357 if (mddev
->sysfs_action
)
9358 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
9361 wake_up(&mddev
->sb_wait
);
9362 mddev_unlock(mddev
);
9365 EXPORT_SYMBOL(md_check_recovery
);
9367 void md_reap_sync_thread(struct mddev
*mddev
)
9369 struct md_rdev
*rdev
;
9370 sector_t old_dev_sectors
= mddev
->dev_sectors
;
9371 bool is_reshaped
= false;
9373 /* resync has finished, collect result */
9374 md_unregister_thread(&mddev
->sync_thread
);
9375 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
9376 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
) &&
9377 mddev
->degraded
!= mddev
->raid_disks
) {
9379 /* activate any spares */
9380 if (mddev
->pers
->spare_active(mddev
)) {
9381 sysfs_notify_dirent_safe(mddev
->sysfs_degraded
);
9382 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
9385 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
9386 mddev
->pers
->finish_reshape
) {
9387 mddev
->pers
->finish_reshape(mddev
);
9388 if (mddev_is_clustered(mddev
))
9392 /* If array is no-longer degraded, then any saved_raid_disk
9393 * information must be scrapped.
9395 if (!mddev
->degraded
)
9396 rdev_for_each(rdev
, mddev
)
9397 rdev
->saved_raid_disk
= -1;
9399 md_update_sb(mddev
, 1);
9400 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
9401 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
9403 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED
, &mddev
->flags
))
9404 md_cluster_ops
->resync_finish(mddev
);
9405 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
9406 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
9407 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
9408 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
9409 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
9410 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
9412 * We call md_cluster_ops->update_size here because sync_size could
9413 * be changed by md_update_sb, and MD_RECOVERY_RESHAPE is cleared,
9414 * so it is time to update size across cluster.
9416 if (mddev_is_clustered(mddev
) && is_reshaped
9417 && !test_bit(MD_CLOSING
, &mddev
->flags
))
9418 md_cluster_ops
->update_size(mddev
, old_dev_sectors
);
9419 wake_up(&resync_wait
);
9420 /* flag recovery needed just to double check */
9421 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
9422 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
9423 md_new_event(mddev
);
9424 if (mddev
->event_work
.func
)
9425 queue_work(md_misc_wq
, &mddev
->event_work
);
9427 EXPORT_SYMBOL(md_reap_sync_thread
);
9429 void md_wait_for_blocked_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
9431 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
9432 wait_event_timeout(rdev
->blocked_wait
,
9433 !test_bit(Blocked
, &rdev
->flags
) &&
9434 !test_bit(BlockedBadBlocks
, &rdev
->flags
),
9435 msecs_to_jiffies(5000));
9436 rdev_dec_pending(rdev
, mddev
);
9438 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
9440 void md_finish_reshape(struct mddev
*mddev
)
9442 /* called be personality module when reshape completes. */
9443 struct md_rdev
*rdev
;
9445 rdev_for_each(rdev
, mddev
) {
9446 if (rdev
->data_offset
> rdev
->new_data_offset
)
9447 rdev
->sectors
+= rdev
->data_offset
- rdev
->new_data_offset
;
9449 rdev
->sectors
-= rdev
->new_data_offset
- rdev
->data_offset
;
9450 rdev
->data_offset
= rdev
->new_data_offset
;
9453 EXPORT_SYMBOL(md_finish_reshape
);
9455 /* Bad block management */
9457 /* Returns 1 on success, 0 on failure */
9458 int rdev_set_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
,
9461 struct mddev
*mddev
= rdev
->mddev
;
9464 s
+= rdev
->new_data_offset
;
9466 s
+= rdev
->data_offset
;
9467 rv
= badblocks_set(&rdev
->badblocks
, s
, sectors
, 0);
9469 /* Make sure they get written out promptly */
9470 if (test_bit(ExternalBbl
, &rdev
->flags
))
9471 sysfs_notify_dirent_safe(rdev
->sysfs_unack_badblocks
);
9472 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
9473 set_mask_bits(&mddev
->sb_flags
, 0,
9474 BIT(MD_SB_CHANGE_CLEAN
) | BIT(MD_SB_CHANGE_PENDING
));
9475 md_wakeup_thread(rdev
->mddev
->thread
);
9480 EXPORT_SYMBOL_GPL(rdev_set_badblocks
);
9482 int rdev_clear_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
,
9487 s
+= rdev
->new_data_offset
;
9489 s
+= rdev
->data_offset
;
9490 rv
= badblocks_clear(&rdev
->badblocks
, s
, sectors
);
9491 if ((rv
== 0) && test_bit(ExternalBbl
, &rdev
->flags
))
9492 sysfs_notify_dirent_safe(rdev
->sysfs_badblocks
);
9495 EXPORT_SYMBOL_GPL(rdev_clear_badblocks
);
9497 static int md_notify_reboot(struct notifier_block
*this,
9498 unsigned long code
, void *x
)
9500 struct list_head
*tmp
;
9501 struct mddev
*mddev
;
9504 for_each_mddev(mddev
, tmp
) {
9505 if (mddev_trylock(mddev
)) {
9507 __md_stop_writes(mddev
);
9508 if (mddev
->persistent
)
9509 mddev
->safemode
= 2;
9510 mddev_unlock(mddev
);
9515 * certain more exotic SCSI devices are known to be
9516 * volatile wrt too early system reboots. While the
9517 * right place to handle this issue is the given
9518 * driver, we do want to have a safe RAID driver ...
9526 static struct notifier_block md_notifier
= {
9527 .notifier_call
= md_notify_reboot
,
9529 .priority
= INT_MAX
, /* before any real devices */
9532 static void md_geninit(void)
9534 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
9536 proc_create("mdstat", S_IRUGO
, NULL
, &mdstat_proc_ops
);
9539 static int __init
md_init(void)
9543 md_wq
= alloc_workqueue("md", WQ_MEM_RECLAIM
, 0);
9547 md_misc_wq
= alloc_workqueue("md_misc", 0, 0);
9551 md_rdev_misc_wq
= alloc_workqueue("md_rdev_misc", 0, 0);
9552 if (!md_rdev_misc_wq
)
9553 goto err_rdev_misc_wq
;
9555 ret
= __register_blkdev(MD_MAJOR
, "md", md_probe
);
9559 ret
= __register_blkdev(0, "mdp", md_probe
);
9564 register_reboot_notifier(&md_notifier
);
9565 raid_table_header
= register_sysctl_table(raid_root_table
);
9571 unregister_blkdev(MD_MAJOR
, "md");
9573 destroy_workqueue(md_rdev_misc_wq
);
9575 destroy_workqueue(md_misc_wq
);
9577 destroy_workqueue(md_wq
);
9582 static void check_sb_changes(struct mddev
*mddev
, struct md_rdev
*rdev
)
9584 struct mdp_superblock_1
*sb
= page_address(rdev
->sb_page
);
9585 struct md_rdev
*rdev2
;
9587 char b
[BDEVNAME_SIZE
];
9590 * If size is changed in another node then we need to
9591 * do resize as well.
9593 if (mddev
->dev_sectors
!= le64_to_cpu(sb
->size
)) {
9594 ret
= mddev
->pers
->resize(mddev
, le64_to_cpu(sb
->size
));
9596 pr_info("md-cluster: resize failed\n");
9598 md_bitmap_update_sb(mddev
->bitmap
);
9601 /* Check for change of roles in the active devices */
9602 rdev_for_each(rdev2
, mddev
) {
9603 if (test_bit(Faulty
, &rdev2
->flags
))
9606 /* Check if the roles changed */
9607 role
= le16_to_cpu(sb
->dev_roles
[rdev2
->desc_nr
]);
9609 if (test_bit(Candidate
, &rdev2
->flags
)) {
9610 if (role
== 0xfffe) {
9611 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2
->bdev
,b
));
9612 md_kick_rdev_from_array(rdev2
);
9616 clear_bit(Candidate
, &rdev2
->flags
);
9619 if (role
!= rdev2
->raid_disk
) {
9621 * got activated except reshape is happening.
9623 if (rdev2
->raid_disk
== -1 && role
!= 0xffff &&
9624 !(le32_to_cpu(sb
->feature_map
) &
9625 MD_FEATURE_RESHAPE_ACTIVE
)) {
9626 rdev2
->saved_raid_disk
= role
;
9627 ret
= remove_and_add_spares(mddev
, rdev2
);
9628 pr_info("Activated spare: %s\n",
9629 bdevname(rdev2
->bdev
,b
));
9630 /* wakeup mddev->thread here, so array could
9631 * perform resync with the new activated disk */
9632 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
9633 md_wakeup_thread(mddev
->thread
);
9636 * We just want to do the minimum to mark the disk
9637 * as faulty. The recovery is performed by the
9638 * one who initiated the error.
9640 if ((role
== 0xfffe) || (role
== 0xfffd)) {
9641 md_error(mddev
, rdev2
);
9642 clear_bit(Blocked
, &rdev2
->flags
);
9647 if (mddev
->raid_disks
!= le32_to_cpu(sb
->raid_disks
)) {
9648 ret
= update_raid_disks(mddev
, le32_to_cpu(sb
->raid_disks
));
9650 pr_warn("md: updating array disks failed. %d\n", ret
);
9654 * Since mddev->delta_disks has already updated in update_raid_disks,
9655 * so it is time to check reshape.
9657 if (test_bit(MD_RESYNCING_REMOTE
, &mddev
->recovery
) &&
9658 (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
9660 * reshape is happening in the remote node, we need to
9661 * update reshape_position and call start_reshape.
9663 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
9664 if (mddev
->pers
->update_reshape_pos
)
9665 mddev
->pers
->update_reshape_pos(mddev
);
9666 if (mddev
->pers
->start_reshape
)
9667 mddev
->pers
->start_reshape(mddev
);
9668 } else if (test_bit(MD_RESYNCING_REMOTE
, &mddev
->recovery
) &&
9669 mddev
->reshape_position
!= MaxSector
&&
9670 !(le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
9671 /* reshape is just done in another node. */
9672 mddev
->reshape_position
= MaxSector
;
9673 if (mddev
->pers
->update_reshape_pos
)
9674 mddev
->pers
->update_reshape_pos(mddev
);
9677 /* Finally set the event to be up to date */
9678 mddev
->events
= le64_to_cpu(sb
->events
);
9681 static int read_rdev(struct mddev
*mddev
, struct md_rdev
*rdev
)
9684 struct page
*swapout
= rdev
->sb_page
;
9685 struct mdp_superblock_1
*sb
;
9687 /* Store the sb page of the rdev in the swapout temporary
9688 * variable in case we err in the future
9690 rdev
->sb_page
= NULL
;
9691 err
= alloc_disk_sb(rdev
);
9693 ClearPageUptodate(rdev
->sb_page
);
9694 rdev
->sb_loaded
= 0;
9695 err
= super_types
[mddev
->major_version
].
9696 load_super(rdev
, NULL
, mddev
->minor_version
);
9699 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9700 __func__
, __LINE__
, rdev
->desc_nr
, err
);
9702 put_page(rdev
->sb_page
);
9703 rdev
->sb_page
= swapout
;
9704 rdev
->sb_loaded
= 1;
9708 sb
= page_address(rdev
->sb_page
);
9709 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9713 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RECOVERY_OFFSET
))
9714 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
9716 /* The other node finished recovery, call spare_active to set
9717 * device In_sync and mddev->degraded
9719 if (rdev
->recovery_offset
== MaxSector
&&
9720 !test_bit(In_sync
, &rdev
->flags
) &&
9721 mddev
->pers
->spare_active(mddev
))
9722 sysfs_notify_dirent_safe(mddev
->sysfs_degraded
);
9728 void md_reload_sb(struct mddev
*mddev
, int nr
)
9730 struct md_rdev
*rdev
;
9734 rdev_for_each_rcu(rdev
, mddev
) {
9735 if (rdev
->desc_nr
== nr
)
9739 if (!rdev
|| rdev
->desc_nr
!= nr
) {
9740 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__
, __LINE__
, nr
);
9744 err
= read_rdev(mddev
, rdev
);
9748 check_sb_changes(mddev
, rdev
);
9750 /* Read all rdev's to update recovery_offset */
9751 rdev_for_each_rcu(rdev
, mddev
) {
9752 if (!test_bit(Faulty
, &rdev
->flags
))
9753 read_rdev(mddev
, rdev
);
9756 EXPORT_SYMBOL(md_reload_sb
);
9761 * Searches all registered partitions for autorun RAID arrays
9765 static DEFINE_MUTEX(detected_devices_mutex
);
9766 static LIST_HEAD(all_detected_devices
);
9767 struct detected_devices_node
{
9768 struct list_head list
;
9772 void md_autodetect_dev(dev_t dev
)
9774 struct detected_devices_node
*node_detected_dev
;
9776 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
9777 if (node_detected_dev
) {
9778 node_detected_dev
->dev
= dev
;
9779 mutex_lock(&detected_devices_mutex
);
9780 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
9781 mutex_unlock(&detected_devices_mutex
);
9785 void md_autostart_arrays(int part
)
9787 struct md_rdev
*rdev
;
9788 struct detected_devices_node
*node_detected_dev
;
9790 int i_scanned
, i_passed
;
9795 pr_info("md: Autodetecting RAID arrays.\n");
9797 mutex_lock(&detected_devices_mutex
);
9798 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
9800 node_detected_dev
= list_entry(all_detected_devices
.next
,
9801 struct detected_devices_node
, list
);
9802 list_del(&node_detected_dev
->list
);
9803 dev
= node_detected_dev
->dev
;
9804 kfree(node_detected_dev
);
9805 mutex_unlock(&detected_devices_mutex
);
9806 rdev
= md_import_device(dev
,0, 90);
9807 mutex_lock(&detected_devices_mutex
);
9811 if (test_bit(Faulty
, &rdev
->flags
))
9814 set_bit(AutoDetected
, &rdev
->flags
);
9815 list_add(&rdev
->same_set
, &pending_raid_disks
);
9818 mutex_unlock(&detected_devices_mutex
);
9820 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned
, i_passed
);
9822 autorun_devices(part
);
9825 #endif /* !MODULE */
9827 static __exit
void md_exit(void)
9829 struct mddev
*mddev
;
9830 struct list_head
*tmp
;
9833 unregister_blkdev(MD_MAJOR
,"md");
9834 unregister_blkdev(mdp_major
, "mdp");
9835 unregister_reboot_notifier(&md_notifier
);
9836 unregister_sysctl_table(raid_table_header
);
9838 /* We cannot unload the modules while some process is
9839 * waiting for us in select() or poll() - wake them up
9842 while (waitqueue_active(&md_event_waiters
)) {
9843 /* not safe to leave yet */
9844 wake_up(&md_event_waiters
);
9848 remove_proc_entry("mdstat", NULL
);
9850 for_each_mddev(mddev
, tmp
) {
9851 export_array(mddev
);
9853 mddev
->hold_active
= 0;
9855 * for_each_mddev() will call mddev_put() at the end of each
9856 * iteration. As the mddev is now fully clear, this will
9857 * schedule the mddev for destruction by a workqueue, and the
9858 * destroy_workqueue() below will wait for that to complete.
9861 destroy_workqueue(md_rdev_misc_wq
);
9862 destroy_workqueue(md_misc_wq
);
9863 destroy_workqueue(md_wq
);
9866 subsys_initcall(md_init
);
9867 module_exit(md_exit
)
9869 static int get_ro(char *buffer
, const struct kernel_param
*kp
)
9871 return sprintf(buffer
, "%d\n", start_readonly
);
9873 static int set_ro(const char *val
, const struct kernel_param
*kp
)
9875 return kstrtouint(val
, 10, (unsigned int *)&start_readonly
);
9878 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
9879 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
9880 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
9881 module_param(create_on_open
, bool, S_IRUSR
|S_IWUSR
);
9883 MODULE_LICENSE("GPL");
9884 MODULE_DESCRIPTION("MD RAID framework");
9886 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);