2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
5 completely rewritten, based on the MD driver code from Marc Zyngier
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
20 Neil Brown <neilb@cse.unsw.edu.au>.
22 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
25 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 Errors, Warnings, etc.
36 pr_crit() for error conditions that risk data loss
37 pr_err() for error conditions that are unexpected, like an IO error
38 or internal inconsistency
39 pr_warn() for error conditions that could have been predicated, like
40 adding a device to an array when it has incompatible metadata
41 pr_info() for every interesting, very rare events, like an array starting
42 or stopping, or resync starting or stopping
43 pr_debug() for everything else.
47 #include <linux/sched/signal.h>
48 #include <linux/kthread.h>
49 #include <linux/blkdev.h>
50 #include <linux/badblocks.h>
51 #include <linux/sysctl.h>
52 #include <linux/seq_file.h>
54 #include <linux/poll.h>
55 #include <linux/ctype.h>
56 #include <linux/string.h>
57 #include <linux/hdreg.h>
58 #include <linux/proc_fs.h>
59 #include <linux/random.h>
60 #include <linux/module.h>
61 #include <linux/reboot.h>
62 #include <linux/file.h>
63 #include <linux/compat.h>
64 #include <linux/delay.h>
65 #include <linux/raid/md_p.h>
66 #include <linux/raid/md_u.h>
67 #include <linux/slab.h>
68 #include <linux/percpu-refcount.h>
70 #include <trace/events/block.h>
72 #include "md-bitmap.h"
73 #include "md-cluster.h"
76 static void autostart_arrays(int part
);
79 /* pers_list is a list of registered personalities protected
81 * pers_lock does extra service to protect accesses to
82 * mddev->thread when the mutex cannot be held.
84 static LIST_HEAD(pers_list
);
85 static DEFINE_SPINLOCK(pers_lock
);
87 struct md_cluster_operations
*md_cluster_ops
;
88 EXPORT_SYMBOL(md_cluster_ops
);
89 struct module
*md_cluster_mod
;
90 EXPORT_SYMBOL(md_cluster_mod
);
92 static DECLARE_WAIT_QUEUE_HEAD(resync_wait
);
93 static struct workqueue_struct
*md_wq
;
94 static struct workqueue_struct
*md_misc_wq
;
96 static int remove_and_add_spares(struct mddev
*mddev
,
97 struct md_rdev
*this);
98 static void mddev_detach(struct mddev
*mddev
);
101 * Default number of read corrections we'll attempt on an rdev
102 * before ejecting it from the array. We divide the read error
103 * count by 2 for every hour elapsed between read errors.
105 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
107 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
108 * is 1000 KB/sec, so the extra system load does not show up that much.
109 * Increase it if you want to have more _guaranteed_ speed. Note that
110 * the RAID driver will use the maximum available bandwidth if the IO
111 * subsystem is idle. There is also an 'absolute maximum' reconstruction
112 * speed limit - in case reconstruction slows down your system despite
115 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
116 * or /sys/block/mdX/md/sync_speed_{min,max}
119 static int sysctl_speed_limit_min
= 1000;
120 static int sysctl_speed_limit_max
= 200000;
121 static inline int speed_min(struct mddev
*mddev
)
123 return mddev
->sync_speed_min
?
124 mddev
->sync_speed_min
: sysctl_speed_limit_min
;
127 static inline int speed_max(struct mddev
*mddev
)
129 return mddev
->sync_speed_max
?
130 mddev
->sync_speed_max
: sysctl_speed_limit_max
;
133 static struct ctl_table_header
*raid_table_header
;
135 static struct ctl_table raid_table
[] = {
137 .procname
= "speed_limit_min",
138 .data
= &sysctl_speed_limit_min
,
139 .maxlen
= sizeof(int),
140 .mode
= S_IRUGO
|S_IWUSR
,
141 .proc_handler
= proc_dointvec
,
144 .procname
= "speed_limit_max",
145 .data
= &sysctl_speed_limit_max
,
146 .maxlen
= sizeof(int),
147 .mode
= S_IRUGO
|S_IWUSR
,
148 .proc_handler
= proc_dointvec
,
153 static struct ctl_table raid_dir_table
[] = {
157 .mode
= S_IRUGO
|S_IXUGO
,
163 static struct ctl_table raid_root_table
[] = {
168 .child
= raid_dir_table
,
173 static const struct block_device_operations md_fops
;
175 static int start_readonly
;
178 * The original mechanism for creating an md device is to create
179 * a device node in /dev and to open it. This causes races with device-close.
180 * The preferred method is to write to the "new_array" module parameter.
181 * This can avoid races.
182 * Setting create_on_open to false disables the original mechanism
183 * so all the races disappear.
185 static bool create_on_open
= true;
188 * like bio_clone_bioset, but with a local bio set
191 struct bio
*bio_alloc_mddev(gfp_t gfp_mask
, int nr_iovecs
,
196 if (!mddev
|| !mddev
->bio_set
)
197 return bio_alloc(gfp_mask
, nr_iovecs
);
199 b
= bio_alloc_bioset(gfp_mask
, nr_iovecs
, mddev
->bio_set
);
204 EXPORT_SYMBOL_GPL(bio_alloc_mddev
);
206 static struct bio
*md_bio_alloc_sync(struct mddev
*mddev
)
208 if (!mddev
|| !mddev
->sync_set
)
209 return bio_alloc(GFP_NOIO
, 1);
211 return bio_alloc_bioset(GFP_NOIO
, 1, mddev
->sync_set
);
215 * We have a system wide 'event count' that is incremented
216 * on any 'interesting' event, and readers of /proc/mdstat
217 * can use 'poll' or 'select' to find out when the event
221 * start array, stop array, error, add device, remove device,
222 * start build, activate spare
224 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters
);
225 static atomic_t md_event_count
;
226 void md_new_event(struct mddev
*mddev
)
228 atomic_inc(&md_event_count
);
229 wake_up(&md_event_waiters
);
231 EXPORT_SYMBOL_GPL(md_new_event
);
234 * Enables to iterate over all existing md arrays
235 * all_mddevs_lock protects this list.
237 static LIST_HEAD(all_mddevs
);
238 static DEFINE_SPINLOCK(all_mddevs_lock
);
241 * iterates through all used mddevs in the system.
242 * We take care to grab the all_mddevs_lock whenever navigating
243 * the list, and to always hold a refcount when unlocked.
244 * Any code which breaks out of this loop while own
245 * a reference to the current mddev and must mddev_put it.
247 #define for_each_mddev(_mddev,_tmp) \
249 for (({ spin_lock(&all_mddevs_lock); \
250 _tmp = all_mddevs.next; \
252 ({ if (_tmp != &all_mddevs) \
253 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
254 spin_unlock(&all_mddevs_lock); \
255 if (_mddev) mddev_put(_mddev); \
256 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
257 _tmp != &all_mddevs;}); \
258 ({ spin_lock(&all_mddevs_lock); \
259 _tmp = _tmp->next;}) \
262 /* Rather than calling directly into the personality make_request function,
263 * IO requests come here first so that we can check if the device is
264 * being suspended pending a reconfiguration.
265 * We hold a refcount over the call to ->make_request. By the time that
266 * call has finished, the bio has been linked into some internal structure
267 * and so is visible to ->quiesce(), so we don't need the refcount any more.
269 static bool is_suspended(struct mddev
*mddev
, struct bio
*bio
)
271 if (mddev
->suspended
)
273 if (bio_data_dir(bio
) != WRITE
)
275 if (mddev
->suspend_lo
>= mddev
->suspend_hi
)
277 if (bio
->bi_iter
.bi_sector
>= mddev
->suspend_hi
)
279 if (bio_end_sector(bio
) < mddev
->suspend_lo
)
284 void md_handle_request(struct mddev
*mddev
, struct bio
*bio
)
288 if (is_suspended(mddev
, bio
)) {
291 prepare_to_wait(&mddev
->sb_wait
, &__wait
,
292 TASK_UNINTERRUPTIBLE
);
293 if (!is_suspended(mddev
, bio
))
299 finish_wait(&mddev
->sb_wait
, &__wait
);
301 atomic_inc(&mddev
->active_io
);
304 if (!mddev
->pers
->make_request(mddev
, bio
)) {
305 atomic_dec(&mddev
->active_io
);
306 wake_up(&mddev
->sb_wait
);
307 goto check_suspended
;
310 if (atomic_dec_and_test(&mddev
->active_io
) && mddev
->suspended
)
311 wake_up(&mddev
->sb_wait
);
313 EXPORT_SYMBOL(md_handle_request
);
315 static blk_qc_t
md_make_request(struct request_queue
*q
, struct bio
*bio
)
317 const int rw
= bio_data_dir(bio
);
318 struct mddev
*mddev
= q
->queuedata
;
319 unsigned int sectors
;
322 blk_queue_split(q
, &bio
);
324 if (mddev
== NULL
|| mddev
->pers
== NULL
) {
326 return BLK_QC_T_NONE
;
328 if (mddev
->ro
== 1 && unlikely(rw
== WRITE
)) {
329 if (bio_sectors(bio
) != 0)
330 bio
->bi_status
= BLK_STS_IOERR
;
332 return BLK_QC_T_NONE
;
336 * save the sectors now since our bio can
337 * go away inside make_request
339 sectors
= bio_sectors(bio
);
340 /* bio could be mergeable after passing to underlayer */
341 bio
->bi_opf
&= ~REQ_NOMERGE
;
343 md_handle_request(mddev
, bio
);
345 cpu
= part_stat_lock();
346 part_stat_inc(cpu
, &mddev
->gendisk
->part0
, ios
[rw
]);
347 part_stat_add(cpu
, &mddev
->gendisk
->part0
, sectors
[rw
], sectors
);
350 return BLK_QC_T_NONE
;
353 /* mddev_suspend makes sure no new requests are submitted
354 * to the device, and that any requests that have been submitted
355 * are completely handled.
356 * Once mddev_detach() is called and completes, the module will be
359 void mddev_suspend(struct mddev
*mddev
)
361 WARN_ON_ONCE(mddev
->thread
&& current
== mddev
->thread
->tsk
);
362 lockdep_assert_held(&mddev
->reconfig_mutex
);
363 if (mddev
->suspended
++)
366 wake_up(&mddev
->sb_wait
);
367 set_bit(MD_ALLOW_SB_UPDATE
, &mddev
->flags
);
368 smp_mb__after_atomic();
369 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->active_io
) == 0);
370 mddev
->pers
->quiesce(mddev
, 1);
371 clear_bit_unlock(MD_ALLOW_SB_UPDATE
, &mddev
->flags
);
372 wait_event(mddev
->sb_wait
, !test_bit(MD_UPDATING_SB
, &mddev
->flags
));
374 del_timer_sync(&mddev
->safemode_timer
);
376 EXPORT_SYMBOL_GPL(mddev_suspend
);
378 void mddev_resume(struct mddev
*mddev
)
380 lockdep_assert_held(&mddev
->reconfig_mutex
);
381 if (--mddev
->suspended
)
383 wake_up(&mddev
->sb_wait
);
384 mddev
->pers
->quiesce(mddev
, 0);
386 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
387 md_wakeup_thread(mddev
->thread
);
388 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
390 EXPORT_SYMBOL_GPL(mddev_resume
);
392 int mddev_congested(struct mddev
*mddev
, int bits
)
394 struct md_personality
*pers
= mddev
->pers
;
398 if (mddev
->suspended
)
400 else if (pers
&& pers
->congested
)
401 ret
= pers
->congested(mddev
, bits
);
405 EXPORT_SYMBOL_GPL(mddev_congested
);
406 static int md_congested(void *data
, int bits
)
408 struct mddev
*mddev
= data
;
409 return mddev_congested(mddev
, bits
);
413 * Generic flush handling for md
416 static void md_end_flush(struct bio
*bio
)
418 struct md_rdev
*rdev
= bio
->bi_private
;
419 struct mddev
*mddev
= rdev
->mddev
;
421 rdev_dec_pending(rdev
, mddev
);
423 if (atomic_dec_and_test(&mddev
->flush_pending
)) {
424 /* The pre-request flush has finished */
425 queue_work(md_wq
, &mddev
->flush_work
);
430 static void md_submit_flush_data(struct work_struct
*ws
);
432 static void submit_flushes(struct work_struct
*ws
)
434 struct mddev
*mddev
= container_of(ws
, struct mddev
, flush_work
);
435 struct md_rdev
*rdev
;
437 INIT_WORK(&mddev
->flush_work
, md_submit_flush_data
);
438 atomic_set(&mddev
->flush_pending
, 1);
440 rdev_for_each_rcu(rdev
, mddev
)
441 if (rdev
->raid_disk
>= 0 &&
442 !test_bit(Faulty
, &rdev
->flags
)) {
443 /* Take two references, one is dropped
444 * when request finishes, one after
445 * we reclaim rcu_read_lock
448 atomic_inc(&rdev
->nr_pending
);
449 atomic_inc(&rdev
->nr_pending
);
451 bi
= bio_alloc_mddev(GFP_NOIO
, 0, mddev
);
452 bi
->bi_end_io
= md_end_flush
;
453 bi
->bi_private
= rdev
;
454 bio_set_dev(bi
, rdev
->bdev
);
455 bi
->bi_opf
= REQ_OP_WRITE
| REQ_PREFLUSH
;
456 atomic_inc(&mddev
->flush_pending
);
459 rdev_dec_pending(rdev
, mddev
);
462 if (atomic_dec_and_test(&mddev
->flush_pending
))
463 queue_work(md_wq
, &mddev
->flush_work
);
466 static void md_submit_flush_data(struct work_struct
*ws
)
468 struct mddev
*mddev
= container_of(ws
, struct mddev
, flush_work
);
469 struct bio
*bio
= mddev
->flush_bio
;
472 * must reset flush_bio before calling into md_handle_request to avoid a
473 * deadlock, because other bios passed md_handle_request suspend check
474 * could wait for this and below md_handle_request could wait for those
475 * bios because of suspend check
477 mddev
->flush_bio
= NULL
;
478 wake_up(&mddev
->sb_wait
);
480 if (bio
->bi_iter
.bi_size
== 0)
481 /* an empty barrier - all done */
484 bio
->bi_opf
&= ~REQ_PREFLUSH
;
485 md_handle_request(mddev
, bio
);
489 void md_flush_request(struct mddev
*mddev
, struct bio
*bio
)
491 spin_lock_irq(&mddev
->lock
);
492 wait_event_lock_irq(mddev
->sb_wait
,
495 mddev
->flush_bio
= bio
;
496 spin_unlock_irq(&mddev
->lock
);
498 INIT_WORK(&mddev
->flush_work
, submit_flushes
);
499 queue_work(md_wq
, &mddev
->flush_work
);
501 EXPORT_SYMBOL(md_flush_request
);
503 static inline struct mddev
*mddev_get(struct mddev
*mddev
)
505 atomic_inc(&mddev
->active
);
509 static void mddev_delayed_delete(struct work_struct
*ws
);
511 static void mddev_put(struct mddev
*mddev
)
513 struct bio_set
*bs
= NULL
, *sync_bs
= NULL
;
515 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
517 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
) &&
518 mddev
->ctime
== 0 && !mddev
->hold_active
) {
519 /* Array is not configured at all, and not held active,
521 list_del_init(&mddev
->all_mddevs
);
523 sync_bs
= mddev
->sync_set
;
524 mddev
->bio_set
= NULL
;
525 mddev
->sync_set
= NULL
;
526 if (mddev
->gendisk
) {
527 /* We did a probe so need to clean up. Call
528 * queue_work inside the spinlock so that
529 * flush_workqueue() after mddev_find will
530 * succeed in waiting for the work to be done.
532 INIT_WORK(&mddev
->del_work
, mddev_delayed_delete
);
533 queue_work(md_misc_wq
, &mddev
->del_work
);
537 spin_unlock(&all_mddevs_lock
);
541 bioset_free(sync_bs
);
544 static void md_safemode_timeout(struct timer_list
*t
);
546 void mddev_init(struct mddev
*mddev
)
548 mutex_init(&mddev
->open_mutex
);
549 mutex_init(&mddev
->reconfig_mutex
);
550 mutex_init(&mddev
->bitmap_info
.mutex
);
551 INIT_LIST_HEAD(&mddev
->disks
);
552 INIT_LIST_HEAD(&mddev
->all_mddevs
);
553 timer_setup(&mddev
->safemode_timer
, md_safemode_timeout
, 0);
554 atomic_set(&mddev
->active
, 1);
555 atomic_set(&mddev
->openers
, 0);
556 atomic_set(&mddev
->active_io
, 0);
557 spin_lock_init(&mddev
->lock
);
558 atomic_set(&mddev
->flush_pending
, 0);
559 init_waitqueue_head(&mddev
->sb_wait
);
560 init_waitqueue_head(&mddev
->recovery_wait
);
561 mddev
->reshape_position
= MaxSector
;
562 mddev
->reshape_backwards
= 0;
563 mddev
->last_sync_action
= "none";
564 mddev
->resync_min
= 0;
565 mddev
->resync_max
= MaxSector
;
566 mddev
->level
= LEVEL_NONE
;
568 EXPORT_SYMBOL_GPL(mddev_init
);
570 static struct mddev
*mddev_find(dev_t unit
)
572 struct mddev
*mddev
, *new = NULL
;
574 if (unit
&& MAJOR(unit
) != MD_MAJOR
)
575 unit
&= ~((1<<MdpMinorShift
)-1);
578 spin_lock(&all_mddevs_lock
);
581 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
582 if (mddev
->unit
== unit
) {
584 spin_unlock(&all_mddevs_lock
);
590 list_add(&new->all_mddevs
, &all_mddevs
);
591 spin_unlock(&all_mddevs_lock
);
592 new->hold_active
= UNTIL_IOCTL
;
596 /* find an unused unit number */
597 static int next_minor
= 512;
598 int start
= next_minor
;
602 dev
= MKDEV(MD_MAJOR
, next_minor
);
604 if (next_minor
> MINORMASK
)
606 if (next_minor
== start
) {
607 /* Oh dear, all in use. */
608 spin_unlock(&all_mddevs_lock
);
614 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
615 if (mddev
->unit
== dev
) {
621 new->md_minor
= MINOR(dev
);
622 new->hold_active
= UNTIL_STOP
;
623 list_add(&new->all_mddevs
, &all_mddevs
);
624 spin_unlock(&all_mddevs_lock
);
627 spin_unlock(&all_mddevs_lock
);
629 new = kzalloc(sizeof(*new), GFP_KERNEL
);
634 if (MAJOR(unit
) == MD_MAJOR
)
635 new->md_minor
= MINOR(unit
);
637 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
644 static struct attribute_group md_redundancy_group
;
646 void mddev_unlock(struct mddev
*mddev
)
648 if (mddev
->to_remove
) {
649 /* These cannot be removed under reconfig_mutex as
650 * an access to the files will try to take reconfig_mutex
651 * while holding the file unremovable, which leads to
653 * So hold set sysfs_active while the remove in happeing,
654 * and anything else which might set ->to_remove or my
655 * otherwise change the sysfs namespace will fail with
656 * -EBUSY if sysfs_active is still set.
657 * We set sysfs_active under reconfig_mutex and elsewhere
658 * test it under the same mutex to ensure its correct value
661 struct attribute_group
*to_remove
= mddev
->to_remove
;
662 mddev
->to_remove
= NULL
;
663 mddev
->sysfs_active
= 1;
664 mutex_unlock(&mddev
->reconfig_mutex
);
666 if (mddev
->kobj
.sd
) {
667 if (to_remove
!= &md_redundancy_group
)
668 sysfs_remove_group(&mddev
->kobj
, to_remove
);
669 if (mddev
->pers
== NULL
||
670 mddev
->pers
->sync_request
== NULL
) {
671 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
672 if (mddev
->sysfs_action
)
673 sysfs_put(mddev
->sysfs_action
);
674 mddev
->sysfs_action
= NULL
;
677 mddev
->sysfs_active
= 0;
679 mutex_unlock(&mddev
->reconfig_mutex
);
681 /* As we've dropped the mutex we need a spinlock to
682 * make sure the thread doesn't disappear
684 spin_lock(&pers_lock
);
685 md_wakeup_thread(mddev
->thread
);
686 wake_up(&mddev
->sb_wait
);
687 spin_unlock(&pers_lock
);
689 EXPORT_SYMBOL_GPL(mddev_unlock
);
691 struct md_rdev
*md_find_rdev_nr_rcu(struct mddev
*mddev
, int nr
)
693 struct md_rdev
*rdev
;
695 rdev_for_each_rcu(rdev
, mddev
)
696 if (rdev
->desc_nr
== nr
)
701 EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu
);
703 static struct md_rdev
*find_rdev(struct mddev
*mddev
, dev_t dev
)
705 struct md_rdev
*rdev
;
707 rdev_for_each(rdev
, mddev
)
708 if (rdev
->bdev
->bd_dev
== dev
)
714 struct md_rdev
*md_find_rdev_rcu(struct mddev
*mddev
, dev_t dev
)
716 struct md_rdev
*rdev
;
718 rdev_for_each_rcu(rdev
, mddev
)
719 if (rdev
->bdev
->bd_dev
== dev
)
724 EXPORT_SYMBOL_GPL(md_find_rdev_rcu
);
726 static struct md_personality
*find_pers(int level
, char *clevel
)
728 struct md_personality
*pers
;
729 list_for_each_entry(pers
, &pers_list
, list
) {
730 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
732 if (strcmp(pers
->name
, clevel
)==0)
738 /* return the offset of the super block in 512byte sectors */
739 static inline sector_t
calc_dev_sboffset(struct md_rdev
*rdev
)
741 sector_t num_sectors
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
742 return MD_NEW_SIZE_SECTORS(num_sectors
);
745 static int alloc_disk_sb(struct md_rdev
*rdev
)
747 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
753 void md_rdev_clear(struct md_rdev
*rdev
)
756 put_page(rdev
->sb_page
);
758 rdev
->sb_page
= NULL
;
763 put_page(rdev
->bb_page
);
764 rdev
->bb_page
= NULL
;
766 badblocks_exit(&rdev
->badblocks
);
768 EXPORT_SYMBOL_GPL(md_rdev_clear
);
770 static void super_written(struct bio
*bio
)
772 struct md_rdev
*rdev
= bio
->bi_private
;
773 struct mddev
*mddev
= rdev
->mddev
;
775 if (bio
->bi_status
) {
776 pr_err("md: super_written gets error=%d\n", bio
->bi_status
);
777 md_error(mddev
, rdev
);
778 if (!test_bit(Faulty
, &rdev
->flags
)
779 && (bio
->bi_opf
& MD_FAILFAST
)) {
780 set_bit(MD_SB_NEED_REWRITE
, &mddev
->sb_flags
);
781 set_bit(LastDev
, &rdev
->flags
);
784 clear_bit(LastDev
, &rdev
->flags
);
786 if (atomic_dec_and_test(&mddev
->pending_writes
))
787 wake_up(&mddev
->sb_wait
);
788 rdev_dec_pending(rdev
, mddev
);
792 void md_super_write(struct mddev
*mddev
, struct md_rdev
*rdev
,
793 sector_t sector
, int size
, struct page
*page
)
795 /* write first size bytes of page to sector of rdev
796 * Increment mddev->pending_writes before returning
797 * and decrement it on completion, waking up sb_wait
798 * if zero is reached.
799 * If an error occurred, call md_error
807 if (test_bit(Faulty
, &rdev
->flags
))
810 bio
= md_bio_alloc_sync(mddev
);
812 atomic_inc(&rdev
->nr_pending
);
814 bio_set_dev(bio
, rdev
->meta_bdev
? rdev
->meta_bdev
: rdev
->bdev
);
815 bio
->bi_iter
.bi_sector
= sector
;
816 bio_add_page(bio
, page
, size
, 0);
817 bio
->bi_private
= rdev
;
818 bio
->bi_end_io
= super_written
;
820 if (test_bit(MD_FAILFAST_SUPPORTED
, &mddev
->flags
) &&
821 test_bit(FailFast
, &rdev
->flags
) &&
822 !test_bit(LastDev
, &rdev
->flags
))
824 bio
->bi_opf
= REQ_OP_WRITE
| REQ_SYNC
| REQ_PREFLUSH
| REQ_FUA
| ff
;
826 atomic_inc(&mddev
->pending_writes
);
830 int md_super_wait(struct mddev
*mddev
)
832 /* wait for all superblock writes that were scheduled to complete */
833 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->pending_writes
)==0);
834 if (test_and_clear_bit(MD_SB_NEED_REWRITE
, &mddev
->sb_flags
))
839 int sync_page_io(struct md_rdev
*rdev
, sector_t sector
, int size
,
840 struct page
*page
, int op
, int op_flags
, bool metadata_op
)
842 struct bio
*bio
= md_bio_alloc_sync(rdev
->mddev
);
845 if (metadata_op
&& rdev
->meta_bdev
)
846 bio_set_dev(bio
, rdev
->meta_bdev
);
848 bio_set_dev(bio
, rdev
->bdev
);
849 bio_set_op_attrs(bio
, op
, op_flags
);
851 bio
->bi_iter
.bi_sector
= sector
+ rdev
->sb_start
;
852 else if (rdev
->mddev
->reshape_position
!= MaxSector
&&
853 (rdev
->mddev
->reshape_backwards
==
854 (sector
>= rdev
->mddev
->reshape_position
)))
855 bio
->bi_iter
.bi_sector
= sector
+ rdev
->new_data_offset
;
857 bio
->bi_iter
.bi_sector
= sector
+ rdev
->data_offset
;
858 bio_add_page(bio
, page
, size
, 0);
860 submit_bio_wait(bio
);
862 ret
= !bio
->bi_status
;
866 EXPORT_SYMBOL_GPL(sync_page_io
);
868 static int read_disk_sb(struct md_rdev
*rdev
, int size
)
870 char b
[BDEVNAME_SIZE
];
875 if (!sync_page_io(rdev
, 0, size
, rdev
->sb_page
, REQ_OP_READ
, 0, true))
881 pr_err("md: disabled device %s, could not read superblock.\n",
882 bdevname(rdev
->bdev
,b
));
886 static int md_uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
888 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
889 sb1
->set_uuid1
== sb2
->set_uuid1
&&
890 sb1
->set_uuid2
== sb2
->set_uuid2
&&
891 sb1
->set_uuid3
== sb2
->set_uuid3
;
894 static int md_sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
897 mdp_super_t
*tmp1
, *tmp2
;
899 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
900 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
902 if (!tmp1
|| !tmp2
) {
911 * nr_disks is not constant
916 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
923 static u32
md_csum_fold(u32 csum
)
925 csum
= (csum
& 0xffff) + (csum
>> 16);
926 return (csum
& 0xffff) + (csum
>> 16);
929 static unsigned int calc_sb_csum(mdp_super_t
*sb
)
932 u32
*sb32
= (u32
*)sb
;
934 unsigned int disk_csum
, csum
;
936 disk_csum
= sb
->sb_csum
;
939 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
941 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
944 /* This used to use csum_partial, which was wrong for several
945 * reasons including that different results are returned on
946 * different architectures. It isn't critical that we get exactly
947 * the same return value as before (we always csum_fold before
948 * testing, and that removes any differences). However as we
949 * know that csum_partial always returned a 16bit value on
950 * alphas, do a fold to maximise conformity to previous behaviour.
952 sb
->sb_csum
= md_csum_fold(disk_csum
);
954 sb
->sb_csum
= disk_csum
;
960 * Handle superblock details.
961 * We want to be able to handle multiple superblock formats
962 * so we have a common interface to them all, and an array of
963 * different handlers.
964 * We rely on user-space to write the initial superblock, and support
965 * reading and updating of superblocks.
966 * Interface methods are:
967 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
968 * loads and validates a superblock on dev.
969 * if refdev != NULL, compare superblocks on both devices
971 * 0 - dev has a superblock that is compatible with refdev
972 * 1 - dev has a superblock that is compatible and newer than refdev
973 * so dev should be used as the refdev in future
974 * -EINVAL superblock incompatible or invalid
975 * -othererror e.g. -EIO
977 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
978 * Verify that dev is acceptable into mddev.
979 * The first time, mddev->raid_disks will be 0, and data from
980 * dev should be merged in. Subsequent calls check that dev
981 * is new enough. Return 0 or -EINVAL
983 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
984 * Update the superblock for rdev with data in mddev
985 * This does not write to disc.
991 struct module
*owner
;
992 int (*load_super
)(struct md_rdev
*rdev
,
993 struct md_rdev
*refdev
,
995 int (*validate_super
)(struct mddev
*mddev
,
996 struct md_rdev
*rdev
);
997 void (*sync_super
)(struct mddev
*mddev
,
998 struct md_rdev
*rdev
);
999 unsigned long long (*rdev_size_change
)(struct md_rdev
*rdev
,
1000 sector_t num_sectors
);
1001 int (*allow_new_offset
)(struct md_rdev
*rdev
,
1002 unsigned long long new_offset
);
1006 * Check that the given mddev has no bitmap.
1008 * This function is called from the run method of all personalities that do not
1009 * support bitmaps. It prints an error message and returns non-zero if mddev
1010 * has a bitmap. Otherwise, it returns 0.
1013 int md_check_no_bitmap(struct mddev
*mddev
)
1015 if (!mddev
->bitmap_info
.file
&& !mddev
->bitmap_info
.offset
)
1017 pr_warn("%s: bitmaps are not supported for %s\n",
1018 mdname(mddev
), mddev
->pers
->name
);
1021 EXPORT_SYMBOL(md_check_no_bitmap
);
1024 * load_super for 0.90.0
1026 static int super_90_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
1028 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1033 * Calculate the position of the superblock (512byte sectors),
1034 * it's at the end of the disk.
1036 * It also happens to be a multiple of 4Kb.
1038 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1040 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
1046 bdevname(rdev
->bdev
, b
);
1047 sb
= page_address(rdev
->sb_page
);
1049 if (sb
->md_magic
!= MD_SB_MAGIC
) {
1050 pr_warn("md: invalid raid superblock magic on %s\n", b
);
1054 if (sb
->major_version
!= 0 ||
1055 sb
->minor_version
< 90 ||
1056 sb
->minor_version
> 91) {
1057 pr_warn("Bad version number %d.%d on %s\n",
1058 sb
->major_version
, sb
->minor_version
, b
);
1062 if (sb
->raid_disks
<= 0)
1065 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
1066 pr_warn("md: invalid superblock checksum on %s\n", b
);
1070 rdev
->preferred_minor
= sb
->md_minor
;
1071 rdev
->data_offset
= 0;
1072 rdev
->new_data_offset
= 0;
1073 rdev
->sb_size
= MD_SB_BYTES
;
1074 rdev
->badblocks
.shift
= -1;
1076 if (sb
->level
== LEVEL_MULTIPATH
)
1079 rdev
->desc_nr
= sb
->this_disk
.number
;
1085 mdp_super_t
*refsb
= page_address(refdev
->sb_page
);
1086 if (!md_uuid_equal(refsb
, sb
)) {
1087 pr_warn("md: %s has different UUID to %s\n",
1088 b
, bdevname(refdev
->bdev
,b2
));
1091 if (!md_sb_equal(refsb
, sb
)) {
1092 pr_warn("md: %s has same UUID but different superblock to %s\n",
1093 b
, bdevname(refdev
->bdev
, b2
));
1097 ev2
= md_event(refsb
);
1103 rdev
->sectors
= rdev
->sb_start
;
1104 /* Limit to 4TB as metadata cannot record more than that.
1105 * (not needed for Linear and RAID0 as metadata doesn't
1108 if (IS_ENABLED(CONFIG_LBDAF
) && (u64
)rdev
->sectors
>= (2ULL << 32) &&
1110 rdev
->sectors
= (sector_t
)(2ULL << 32) - 2;
1112 if (rdev
->sectors
< ((sector_t
)sb
->size
) * 2 && sb
->level
>= 1)
1113 /* "this cannot possibly happen" ... */
1121 * validate_super for 0.90.0
1123 static int super_90_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
1126 mdp_super_t
*sb
= page_address(rdev
->sb_page
);
1127 __u64 ev1
= md_event(sb
);
1129 rdev
->raid_disk
= -1;
1130 clear_bit(Faulty
, &rdev
->flags
);
1131 clear_bit(In_sync
, &rdev
->flags
);
1132 clear_bit(Bitmap_sync
, &rdev
->flags
);
1133 clear_bit(WriteMostly
, &rdev
->flags
);
1135 if (mddev
->raid_disks
== 0) {
1136 mddev
->major_version
= 0;
1137 mddev
->minor_version
= sb
->minor_version
;
1138 mddev
->patch_version
= sb
->patch_version
;
1139 mddev
->external
= 0;
1140 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
1141 mddev
->ctime
= sb
->ctime
;
1142 mddev
->utime
= sb
->utime
;
1143 mddev
->level
= sb
->level
;
1144 mddev
->clevel
[0] = 0;
1145 mddev
->layout
= sb
->layout
;
1146 mddev
->raid_disks
= sb
->raid_disks
;
1147 mddev
->dev_sectors
= ((sector_t
)sb
->size
) * 2;
1148 mddev
->events
= ev1
;
1149 mddev
->bitmap_info
.offset
= 0;
1150 mddev
->bitmap_info
.space
= 0;
1151 /* bitmap can use 60 K after the 4K superblocks */
1152 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
1153 mddev
->bitmap_info
.default_space
= 64*2 - (MD_SB_BYTES
>> 9);
1154 mddev
->reshape_backwards
= 0;
1156 if (mddev
->minor_version
>= 91) {
1157 mddev
->reshape_position
= sb
->reshape_position
;
1158 mddev
->delta_disks
= sb
->delta_disks
;
1159 mddev
->new_level
= sb
->new_level
;
1160 mddev
->new_layout
= sb
->new_layout
;
1161 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
1162 if (mddev
->delta_disks
< 0)
1163 mddev
->reshape_backwards
= 1;
1165 mddev
->reshape_position
= MaxSector
;
1166 mddev
->delta_disks
= 0;
1167 mddev
->new_level
= mddev
->level
;
1168 mddev
->new_layout
= mddev
->layout
;
1169 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1172 if (sb
->state
& (1<<MD_SB_CLEAN
))
1173 mddev
->recovery_cp
= MaxSector
;
1175 if (sb
->events_hi
== sb
->cp_events_hi
&&
1176 sb
->events_lo
== sb
->cp_events_lo
) {
1177 mddev
->recovery_cp
= sb
->recovery_cp
;
1179 mddev
->recovery_cp
= 0;
1182 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
1183 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
1184 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
1185 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
1187 mddev
->max_disks
= MD_SB_DISKS
;
1189 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
1190 mddev
->bitmap_info
.file
== NULL
) {
1191 mddev
->bitmap_info
.offset
=
1192 mddev
->bitmap_info
.default_offset
;
1193 mddev
->bitmap_info
.space
=
1194 mddev
->bitmap_info
.default_space
;
1197 } else if (mddev
->pers
== NULL
) {
1198 /* Insist on good event counter while assembling, except
1199 * for spares (which don't need an event count) */
1201 if (sb
->disks
[rdev
->desc_nr
].state
& (
1202 (1<<MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
)))
1203 if (ev1
< mddev
->events
)
1205 } else if (mddev
->bitmap
) {
1206 /* if adding to array with a bitmap, then we can accept an
1207 * older device ... but not too old.
1209 if (ev1
< mddev
->bitmap
->events_cleared
)
1211 if (ev1
< mddev
->events
)
1212 set_bit(Bitmap_sync
, &rdev
->flags
);
1214 if (ev1
< mddev
->events
)
1215 /* just a hot-add of a new device, leave raid_disk at -1 */
1219 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1220 desc
= sb
->disks
+ rdev
->desc_nr
;
1222 if (desc
->state
& (1<<MD_DISK_FAULTY
))
1223 set_bit(Faulty
, &rdev
->flags
);
1224 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
1225 desc->raid_disk < mddev->raid_disks */) {
1226 set_bit(In_sync
, &rdev
->flags
);
1227 rdev
->raid_disk
= desc
->raid_disk
;
1228 rdev
->saved_raid_disk
= desc
->raid_disk
;
1229 } else if (desc
->state
& (1<<MD_DISK_ACTIVE
)) {
1230 /* active but not in sync implies recovery up to
1231 * reshape position. We don't know exactly where
1232 * that is, so set to zero for now */
1233 if (mddev
->minor_version
>= 91) {
1234 rdev
->recovery_offset
= 0;
1235 rdev
->raid_disk
= desc
->raid_disk
;
1238 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
1239 set_bit(WriteMostly
, &rdev
->flags
);
1240 if (desc
->state
& (1<<MD_DISK_FAILFAST
))
1241 set_bit(FailFast
, &rdev
->flags
);
1242 } else /* MULTIPATH are always insync */
1243 set_bit(In_sync
, &rdev
->flags
);
1248 * sync_super for 0.90.0
1250 static void super_90_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
1253 struct md_rdev
*rdev2
;
1254 int next_spare
= mddev
->raid_disks
;
1256 /* make rdev->sb match mddev data..
1259 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1260 * 3/ any empty disks < next_spare become removed
1262 * disks[0] gets initialised to REMOVED because
1263 * we cannot be sure from other fields if it has
1264 * been initialised or not.
1267 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
1269 rdev
->sb_size
= MD_SB_BYTES
;
1271 sb
= page_address(rdev
->sb_page
);
1273 memset(sb
, 0, sizeof(*sb
));
1275 sb
->md_magic
= MD_SB_MAGIC
;
1276 sb
->major_version
= mddev
->major_version
;
1277 sb
->patch_version
= mddev
->patch_version
;
1278 sb
->gvalid_words
= 0; /* ignored */
1279 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
1280 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
1281 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
1282 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
1284 sb
->ctime
= clamp_t(time64_t
, mddev
->ctime
, 0, U32_MAX
);
1285 sb
->level
= mddev
->level
;
1286 sb
->size
= mddev
->dev_sectors
/ 2;
1287 sb
->raid_disks
= mddev
->raid_disks
;
1288 sb
->md_minor
= mddev
->md_minor
;
1289 sb
->not_persistent
= 0;
1290 sb
->utime
= clamp_t(time64_t
, mddev
->utime
, 0, U32_MAX
);
1292 sb
->events_hi
= (mddev
->events
>>32);
1293 sb
->events_lo
= (u32
)mddev
->events
;
1295 if (mddev
->reshape_position
== MaxSector
)
1296 sb
->minor_version
= 90;
1298 sb
->minor_version
= 91;
1299 sb
->reshape_position
= mddev
->reshape_position
;
1300 sb
->new_level
= mddev
->new_level
;
1301 sb
->delta_disks
= mddev
->delta_disks
;
1302 sb
->new_layout
= mddev
->new_layout
;
1303 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1305 mddev
->minor_version
= sb
->minor_version
;
1308 sb
->recovery_cp
= mddev
->recovery_cp
;
1309 sb
->cp_events_hi
= (mddev
->events
>>32);
1310 sb
->cp_events_lo
= (u32
)mddev
->events
;
1311 if (mddev
->recovery_cp
== MaxSector
)
1312 sb
->state
= (1<< MD_SB_CLEAN
);
1314 sb
->recovery_cp
= 0;
1316 sb
->layout
= mddev
->layout
;
1317 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1319 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
)
1320 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1322 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1323 rdev_for_each(rdev2
, mddev
) {
1326 int is_active
= test_bit(In_sync
, &rdev2
->flags
);
1328 if (rdev2
->raid_disk
>= 0 &&
1329 sb
->minor_version
>= 91)
1330 /* we have nowhere to store the recovery_offset,
1331 * but if it is not below the reshape_position,
1332 * we can piggy-back on that.
1335 if (rdev2
->raid_disk
< 0 ||
1336 test_bit(Faulty
, &rdev2
->flags
))
1339 desc_nr
= rdev2
->raid_disk
;
1341 desc_nr
= next_spare
++;
1342 rdev2
->desc_nr
= desc_nr
;
1343 d
= &sb
->disks
[rdev2
->desc_nr
];
1345 d
->number
= rdev2
->desc_nr
;
1346 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1347 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1349 d
->raid_disk
= rdev2
->raid_disk
;
1351 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1352 if (test_bit(Faulty
, &rdev2
->flags
))
1353 d
->state
= (1<<MD_DISK_FAULTY
);
1354 else if (is_active
) {
1355 d
->state
= (1<<MD_DISK_ACTIVE
);
1356 if (test_bit(In_sync
, &rdev2
->flags
))
1357 d
->state
|= (1<<MD_DISK_SYNC
);
1365 if (test_bit(WriteMostly
, &rdev2
->flags
))
1366 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1367 if (test_bit(FailFast
, &rdev2
->flags
))
1368 d
->state
|= (1<<MD_DISK_FAILFAST
);
1370 /* now set the "removed" and "faulty" bits on any missing devices */
1371 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1372 mdp_disk_t
*d
= &sb
->disks
[i
];
1373 if (d
->state
== 0 && d
->number
== 0) {
1376 d
->state
= (1<<MD_DISK_REMOVED
);
1377 d
->state
|= (1<<MD_DISK_FAULTY
);
1381 sb
->nr_disks
= nr_disks
;
1382 sb
->active_disks
= active
;
1383 sb
->working_disks
= working
;
1384 sb
->failed_disks
= failed
;
1385 sb
->spare_disks
= spare
;
1387 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1388 sb
->sb_csum
= calc_sb_csum(sb
);
1392 * rdev_size_change for 0.90.0
1394 static unsigned long long
1395 super_90_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
1397 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1398 return 0; /* component must fit device */
1399 if (rdev
->mddev
->bitmap_info
.offset
)
1400 return 0; /* can't move bitmap */
1401 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1402 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1403 num_sectors
= rdev
->sb_start
;
1404 /* Limit to 4TB as metadata cannot record more than that.
1405 * 4TB == 2^32 KB, or 2*2^32 sectors.
1407 if (IS_ENABLED(CONFIG_LBDAF
) && (u64
)num_sectors
>= (2ULL << 32) &&
1408 rdev
->mddev
->level
>= 1)
1409 num_sectors
= (sector_t
)(2ULL << 32) - 2;
1411 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1413 } while (md_super_wait(rdev
->mddev
) < 0);
1418 super_90_allow_new_offset(struct md_rdev
*rdev
, unsigned long long new_offset
)
1420 /* non-zero offset changes not possible with v0.90 */
1421 return new_offset
== 0;
1425 * version 1 superblock
1428 static __le32
calc_sb_1_csum(struct mdp_superblock_1
*sb
)
1432 unsigned long long newcsum
;
1433 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1434 __le32
*isuper
= (__le32
*)sb
;
1436 disk_csum
= sb
->sb_csum
;
1439 for (; size
>= 4; size
-= 4)
1440 newcsum
+= le32_to_cpu(*isuper
++);
1443 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1445 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1446 sb
->sb_csum
= disk_csum
;
1447 return cpu_to_le32(csum
);
1450 static int super_1_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
1452 struct mdp_superblock_1
*sb
;
1456 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1460 * Calculate the position of the superblock in 512byte sectors.
1461 * It is always aligned to a 4K boundary and
1462 * depeding on minor_version, it can be:
1463 * 0: At least 8K, but less than 12K, from end of device
1464 * 1: At start of device
1465 * 2: 4K from start of device.
1467 switch(minor_version
) {
1469 sb_start
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1471 sb_start
&= ~(sector_t
)(4*2-1);
1482 rdev
->sb_start
= sb_start
;
1484 /* superblock is rarely larger than 1K, but it can be larger,
1485 * and it is safe to read 4k, so we do that
1487 ret
= read_disk_sb(rdev
, 4096);
1488 if (ret
) return ret
;
1490 sb
= page_address(rdev
->sb_page
);
1492 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1493 sb
->major_version
!= cpu_to_le32(1) ||
1494 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1495 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1496 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1499 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1500 pr_warn("md: invalid superblock checksum on %s\n",
1501 bdevname(rdev
->bdev
,b
));
1504 if (le64_to_cpu(sb
->data_size
) < 10) {
1505 pr_warn("md: data_size too small on %s\n",
1506 bdevname(rdev
->bdev
,b
));
1511 memcmp(sb
->pad3
, sb
->pad3
+1, sizeof(sb
->pad3
) - sizeof(sb
->pad3
[1])))
1512 /* Some padding is non-zero, might be a new feature */
1515 rdev
->preferred_minor
= 0xffff;
1516 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1517 rdev
->new_data_offset
= rdev
->data_offset
;
1518 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
) &&
1519 (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_NEW_OFFSET
))
1520 rdev
->new_data_offset
+= (s32
)le32_to_cpu(sb
->new_offset
);
1521 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1523 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1524 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1525 if (rdev
->sb_size
& bmask
)
1526 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1529 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1532 && rdev
->new_data_offset
< sb_start
+ (rdev
->sb_size
/512))
1535 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1538 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1540 if (!rdev
->bb_page
) {
1541 rdev
->bb_page
= alloc_page(GFP_KERNEL
);
1545 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BAD_BLOCKS
) &&
1546 rdev
->badblocks
.count
== 0) {
1547 /* need to load the bad block list.
1548 * Currently we limit it to one page.
1554 int sectors
= le16_to_cpu(sb
->bblog_size
);
1555 if (sectors
> (PAGE_SIZE
/ 512))
1557 offset
= le32_to_cpu(sb
->bblog_offset
);
1560 bb_sector
= (long long)offset
;
1561 if (!sync_page_io(rdev
, bb_sector
, sectors
<< 9,
1562 rdev
->bb_page
, REQ_OP_READ
, 0, true))
1564 bbp
= (u64
*)page_address(rdev
->bb_page
);
1565 rdev
->badblocks
.shift
= sb
->bblog_shift
;
1566 for (i
= 0 ; i
< (sectors
<< (9-3)) ; i
++, bbp
++) {
1567 u64 bb
= le64_to_cpu(*bbp
);
1568 int count
= bb
& (0x3ff);
1569 u64 sector
= bb
>> 10;
1570 sector
<<= sb
->bblog_shift
;
1571 count
<<= sb
->bblog_shift
;
1574 if (badblocks_set(&rdev
->badblocks
, sector
, count
, 1))
1577 } else if (sb
->bblog_offset
!= 0)
1578 rdev
->badblocks
.shift
= 0;
1580 if ((le32_to_cpu(sb
->feature_map
) &
1581 (MD_FEATURE_PPL
| MD_FEATURE_MULTIPLE_PPLS
))) {
1582 rdev
->ppl
.offset
= (__s16
)le16_to_cpu(sb
->ppl
.offset
);
1583 rdev
->ppl
.size
= le16_to_cpu(sb
->ppl
.size
);
1584 rdev
->ppl
.sector
= rdev
->sb_start
+ rdev
->ppl
.offset
;
1591 struct mdp_superblock_1
*refsb
= page_address(refdev
->sb_page
);
1593 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1594 sb
->level
!= refsb
->level
||
1595 sb
->layout
!= refsb
->layout
||
1596 sb
->chunksize
!= refsb
->chunksize
) {
1597 pr_warn("md: %s has strangely different superblock to %s\n",
1598 bdevname(rdev
->bdev
,b
),
1599 bdevname(refdev
->bdev
,b2
));
1602 ev1
= le64_to_cpu(sb
->events
);
1603 ev2
= le64_to_cpu(refsb
->events
);
1610 if (minor_version
) {
1611 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9);
1612 sectors
-= rdev
->data_offset
;
1614 sectors
= rdev
->sb_start
;
1615 if (sectors
< le64_to_cpu(sb
->data_size
))
1617 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1621 static int super_1_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
1623 struct mdp_superblock_1
*sb
= page_address(rdev
->sb_page
);
1624 __u64 ev1
= le64_to_cpu(sb
->events
);
1626 rdev
->raid_disk
= -1;
1627 clear_bit(Faulty
, &rdev
->flags
);
1628 clear_bit(In_sync
, &rdev
->flags
);
1629 clear_bit(Bitmap_sync
, &rdev
->flags
);
1630 clear_bit(WriteMostly
, &rdev
->flags
);
1632 if (mddev
->raid_disks
== 0) {
1633 mddev
->major_version
= 1;
1634 mddev
->patch_version
= 0;
1635 mddev
->external
= 0;
1636 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1637 mddev
->ctime
= le64_to_cpu(sb
->ctime
);
1638 mddev
->utime
= le64_to_cpu(sb
->utime
);
1639 mddev
->level
= le32_to_cpu(sb
->level
);
1640 mddev
->clevel
[0] = 0;
1641 mddev
->layout
= le32_to_cpu(sb
->layout
);
1642 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1643 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1644 mddev
->events
= ev1
;
1645 mddev
->bitmap_info
.offset
= 0;
1646 mddev
->bitmap_info
.space
= 0;
1647 /* Default location for bitmap is 1K after superblock
1648 * using 3K - total of 4K
1650 mddev
->bitmap_info
.default_offset
= 1024 >> 9;
1651 mddev
->bitmap_info
.default_space
= (4096-1024) >> 9;
1652 mddev
->reshape_backwards
= 0;
1654 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1655 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1657 mddev
->max_disks
= (4096-256)/2;
1659 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1660 mddev
->bitmap_info
.file
== NULL
) {
1661 mddev
->bitmap_info
.offset
=
1662 (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1663 /* Metadata doesn't record how much space is available.
1664 * For 1.0, we assume we can use up to the superblock
1665 * if before, else to 4K beyond superblock.
1666 * For others, assume no change is possible.
1668 if (mddev
->minor_version
> 0)
1669 mddev
->bitmap_info
.space
= 0;
1670 else if (mddev
->bitmap_info
.offset
> 0)
1671 mddev
->bitmap_info
.space
=
1672 8 - mddev
->bitmap_info
.offset
;
1674 mddev
->bitmap_info
.space
=
1675 -mddev
->bitmap_info
.offset
;
1678 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1679 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1680 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1681 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1682 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1683 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1684 if (mddev
->delta_disks
< 0 ||
1685 (mddev
->delta_disks
== 0 &&
1686 (le32_to_cpu(sb
->feature_map
)
1687 & MD_FEATURE_RESHAPE_BACKWARDS
)))
1688 mddev
->reshape_backwards
= 1;
1690 mddev
->reshape_position
= MaxSector
;
1691 mddev
->delta_disks
= 0;
1692 mddev
->new_level
= mddev
->level
;
1693 mddev
->new_layout
= mddev
->layout
;
1694 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1697 if (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_JOURNAL
)
1698 set_bit(MD_HAS_JOURNAL
, &mddev
->flags
);
1700 if (le32_to_cpu(sb
->feature_map
) &
1701 (MD_FEATURE_PPL
| MD_FEATURE_MULTIPLE_PPLS
)) {
1702 if (le32_to_cpu(sb
->feature_map
) &
1703 (MD_FEATURE_BITMAP_OFFSET
| MD_FEATURE_JOURNAL
))
1705 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_PPL
) &&
1706 (le32_to_cpu(sb
->feature_map
) &
1707 MD_FEATURE_MULTIPLE_PPLS
))
1709 set_bit(MD_HAS_PPL
, &mddev
->flags
);
1711 } else if (mddev
->pers
== NULL
) {
1712 /* Insist of good event counter while assembling, except for
1713 * spares (which don't need an event count) */
1715 if (rdev
->desc_nr
>= 0 &&
1716 rdev
->desc_nr
< le32_to_cpu(sb
->max_dev
) &&
1717 (le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) < MD_DISK_ROLE_MAX
||
1718 le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) == MD_DISK_ROLE_JOURNAL
))
1719 if (ev1
< mddev
->events
)
1721 } else if (mddev
->bitmap
) {
1722 /* If adding to array with a bitmap, then we can accept an
1723 * older device, but not too old.
1725 if (ev1
< mddev
->bitmap
->events_cleared
)
1727 if (ev1
< mddev
->events
)
1728 set_bit(Bitmap_sync
, &rdev
->flags
);
1730 if (ev1
< mddev
->events
)
1731 /* just a hot-add of a new device, leave raid_disk at -1 */
1734 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1736 if (rdev
->desc_nr
< 0 ||
1737 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1738 role
= MD_DISK_ROLE_SPARE
;
1741 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1743 case MD_DISK_ROLE_SPARE
: /* spare */
1745 case MD_DISK_ROLE_FAULTY
: /* faulty */
1746 set_bit(Faulty
, &rdev
->flags
);
1748 case MD_DISK_ROLE_JOURNAL
: /* journal device */
1749 if (!(le32_to_cpu(sb
->feature_map
) & MD_FEATURE_JOURNAL
)) {
1750 /* journal device without journal feature */
1751 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
1754 set_bit(Journal
, &rdev
->flags
);
1755 rdev
->journal_tail
= le64_to_cpu(sb
->journal_tail
);
1756 rdev
->raid_disk
= 0;
1759 rdev
->saved_raid_disk
= role
;
1760 if ((le32_to_cpu(sb
->feature_map
) &
1761 MD_FEATURE_RECOVERY_OFFSET
)) {
1762 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1763 if (!(le32_to_cpu(sb
->feature_map
) &
1764 MD_FEATURE_RECOVERY_BITMAP
))
1765 rdev
->saved_raid_disk
= -1;
1767 set_bit(In_sync
, &rdev
->flags
);
1768 rdev
->raid_disk
= role
;
1771 if (sb
->devflags
& WriteMostly1
)
1772 set_bit(WriteMostly
, &rdev
->flags
);
1773 if (sb
->devflags
& FailFast1
)
1774 set_bit(FailFast
, &rdev
->flags
);
1775 if (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_REPLACEMENT
)
1776 set_bit(Replacement
, &rdev
->flags
);
1777 } else /* MULTIPATH are always insync */
1778 set_bit(In_sync
, &rdev
->flags
);
1783 static void super_1_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
1785 struct mdp_superblock_1
*sb
;
1786 struct md_rdev
*rdev2
;
1788 /* make rdev->sb match mddev and rdev data. */
1790 sb
= page_address(rdev
->sb_page
);
1792 sb
->feature_map
= 0;
1794 sb
->recovery_offset
= cpu_to_le64(0);
1795 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1797 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1798 sb
->events
= cpu_to_le64(mddev
->events
);
1800 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1801 else if (test_bit(MD_JOURNAL_CLEAN
, &mddev
->flags
))
1802 sb
->resync_offset
= cpu_to_le64(MaxSector
);
1804 sb
->resync_offset
= cpu_to_le64(0);
1806 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1808 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1809 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
1810 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
1811 sb
->level
= cpu_to_le32(mddev
->level
);
1812 sb
->layout
= cpu_to_le32(mddev
->layout
);
1813 if (test_bit(FailFast
, &rdev
->flags
))
1814 sb
->devflags
|= FailFast1
;
1816 sb
->devflags
&= ~FailFast1
;
1818 if (test_bit(WriteMostly
, &rdev
->flags
))
1819 sb
->devflags
|= WriteMostly1
;
1821 sb
->devflags
&= ~WriteMostly1
;
1822 sb
->data_offset
= cpu_to_le64(rdev
->data_offset
);
1823 sb
->data_size
= cpu_to_le64(rdev
->sectors
);
1825 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
) {
1826 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_info
.offset
);
1827 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1830 if (rdev
->raid_disk
>= 0 && !test_bit(Journal
, &rdev
->flags
) &&
1831 !test_bit(In_sync
, &rdev
->flags
)) {
1833 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1834 sb
->recovery_offset
=
1835 cpu_to_le64(rdev
->recovery_offset
);
1836 if (rdev
->saved_raid_disk
>= 0 && mddev
->bitmap
)
1838 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP
);
1840 /* Note: recovery_offset and journal_tail share space */
1841 if (test_bit(Journal
, &rdev
->flags
))
1842 sb
->journal_tail
= cpu_to_le64(rdev
->journal_tail
);
1843 if (test_bit(Replacement
, &rdev
->flags
))
1845 cpu_to_le32(MD_FEATURE_REPLACEMENT
);
1847 if (mddev
->reshape_position
!= MaxSector
) {
1848 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1849 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1850 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1851 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1852 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1853 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
1854 if (mddev
->delta_disks
== 0 &&
1855 mddev
->reshape_backwards
)
1857 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS
);
1858 if (rdev
->new_data_offset
!= rdev
->data_offset
) {
1860 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET
);
1861 sb
->new_offset
= cpu_to_le32((__u32
)(rdev
->new_data_offset
1862 - rdev
->data_offset
));
1866 if (mddev_is_clustered(mddev
))
1867 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_CLUSTERED
);
1869 if (rdev
->badblocks
.count
== 0)
1870 /* Nothing to do for bad blocks*/ ;
1871 else if (sb
->bblog_offset
== 0)
1872 /* Cannot record bad blocks on this device */
1873 md_error(mddev
, rdev
);
1875 struct badblocks
*bb
= &rdev
->badblocks
;
1876 u64
*bbp
= (u64
*)page_address(rdev
->bb_page
);
1878 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_BAD_BLOCKS
);
1883 seq
= read_seqbegin(&bb
->lock
);
1885 memset(bbp
, 0xff, PAGE_SIZE
);
1887 for (i
= 0 ; i
< bb
->count
; i
++) {
1888 u64 internal_bb
= p
[i
];
1889 u64 store_bb
= ((BB_OFFSET(internal_bb
) << 10)
1890 | BB_LEN(internal_bb
));
1891 bbp
[i
] = cpu_to_le64(store_bb
);
1894 if (read_seqretry(&bb
->lock
, seq
))
1897 bb
->sector
= (rdev
->sb_start
+
1898 (int)le32_to_cpu(sb
->bblog_offset
));
1899 bb
->size
= le16_to_cpu(sb
->bblog_size
);
1904 rdev_for_each(rdev2
, mddev
)
1905 if (rdev2
->desc_nr
+1 > max_dev
)
1906 max_dev
= rdev2
->desc_nr
+1;
1908 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
1910 sb
->max_dev
= cpu_to_le32(max_dev
);
1911 rdev
->sb_size
= max_dev
* 2 + 256;
1912 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1913 if (rdev
->sb_size
& bmask
)
1914 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1916 max_dev
= le32_to_cpu(sb
->max_dev
);
1918 for (i
=0; i
<max_dev
;i
++)
1919 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_SPARE
);
1921 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
))
1922 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_JOURNAL
);
1924 if (test_bit(MD_HAS_PPL
, &mddev
->flags
)) {
1925 if (test_bit(MD_HAS_MULTIPLE_PPLS
, &mddev
->flags
))
1927 cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS
);
1929 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_PPL
);
1930 sb
->ppl
.offset
= cpu_to_le16(rdev
->ppl
.offset
);
1931 sb
->ppl
.size
= cpu_to_le16(rdev
->ppl
.size
);
1934 rdev_for_each(rdev2
, mddev
) {
1936 if (test_bit(Faulty
, &rdev2
->flags
))
1937 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_FAULTY
);
1938 else if (test_bit(In_sync
, &rdev2
->flags
))
1939 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1940 else if (test_bit(Journal
, &rdev2
->flags
))
1941 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_JOURNAL
);
1942 else if (rdev2
->raid_disk
>= 0)
1943 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1945 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_SPARE
);
1948 sb
->sb_csum
= calc_sb_1_csum(sb
);
1951 static unsigned long long
1952 super_1_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
1954 struct mdp_superblock_1
*sb
;
1955 sector_t max_sectors
;
1956 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1957 return 0; /* component must fit device */
1958 if (rdev
->data_offset
!= rdev
->new_data_offset
)
1959 return 0; /* too confusing */
1960 if (rdev
->sb_start
< rdev
->data_offset
) {
1961 /* minor versions 1 and 2; superblock before data */
1962 max_sectors
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1963 max_sectors
-= rdev
->data_offset
;
1964 if (!num_sectors
|| num_sectors
> max_sectors
)
1965 num_sectors
= max_sectors
;
1966 } else if (rdev
->mddev
->bitmap_info
.offset
) {
1967 /* minor version 0 with bitmap we can't move */
1970 /* minor version 0; superblock after data */
1972 sb_start
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) - 8*2;
1973 sb_start
&= ~(sector_t
)(4*2 - 1);
1974 max_sectors
= rdev
->sectors
+ sb_start
- rdev
->sb_start
;
1975 if (!num_sectors
|| num_sectors
> max_sectors
)
1976 num_sectors
= max_sectors
;
1977 rdev
->sb_start
= sb_start
;
1979 sb
= page_address(rdev
->sb_page
);
1980 sb
->data_size
= cpu_to_le64(num_sectors
);
1981 sb
->super_offset
= cpu_to_le64(rdev
->sb_start
);
1982 sb
->sb_csum
= calc_sb_1_csum(sb
);
1984 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1986 } while (md_super_wait(rdev
->mddev
) < 0);
1992 super_1_allow_new_offset(struct md_rdev
*rdev
,
1993 unsigned long long new_offset
)
1995 /* All necessary checks on new >= old have been done */
1996 struct bitmap
*bitmap
;
1997 if (new_offset
>= rdev
->data_offset
)
2000 /* with 1.0 metadata, there is no metadata to tread on
2001 * so we can always move back */
2002 if (rdev
->mddev
->minor_version
== 0)
2005 /* otherwise we must be sure not to step on
2006 * any metadata, so stay:
2007 * 36K beyond start of superblock
2008 * beyond end of badblocks
2009 * beyond write-intent bitmap
2011 if (rdev
->sb_start
+ (32+4)*2 > new_offset
)
2013 bitmap
= rdev
->mddev
->bitmap
;
2014 if (bitmap
&& !rdev
->mddev
->bitmap_info
.file
&&
2015 rdev
->sb_start
+ rdev
->mddev
->bitmap_info
.offset
+
2016 bitmap
->storage
.file_pages
* (PAGE_SIZE
>>9) > new_offset
)
2018 if (rdev
->badblocks
.sector
+ rdev
->badblocks
.size
> new_offset
)
2024 static struct super_type super_types
[] = {
2027 .owner
= THIS_MODULE
,
2028 .load_super
= super_90_load
,
2029 .validate_super
= super_90_validate
,
2030 .sync_super
= super_90_sync
,
2031 .rdev_size_change
= super_90_rdev_size_change
,
2032 .allow_new_offset
= super_90_allow_new_offset
,
2036 .owner
= THIS_MODULE
,
2037 .load_super
= super_1_load
,
2038 .validate_super
= super_1_validate
,
2039 .sync_super
= super_1_sync
,
2040 .rdev_size_change
= super_1_rdev_size_change
,
2041 .allow_new_offset
= super_1_allow_new_offset
,
2045 static void sync_super(struct mddev
*mddev
, struct md_rdev
*rdev
)
2047 if (mddev
->sync_super
) {
2048 mddev
->sync_super(mddev
, rdev
);
2052 BUG_ON(mddev
->major_version
>= ARRAY_SIZE(super_types
));
2054 super_types
[mddev
->major_version
].sync_super(mddev
, rdev
);
2057 static int match_mddev_units(struct mddev
*mddev1
, struct mddev
*mddev2
)
2059 struct md_rdev
*rdev
, *rdev2
;
2062 rdev_for_each_rcu(rdev
, mddev1
) {
2063 if (test_bit(Faulty
, &rdev
->flags
) ||
2064 test_bit(Journal
, &rdev
->flags
) ||
2065 rdev
->raid_disk
== -1)
2067 rdev_for_each_rcu(rdev2
, mddev2
) {
2068 if (test_bit(Faulty
, &rdev2
->flags
) ||
2069 test_bit(Journal
, &rdev2
->flags
) ||
2070 rdev2
->raid_disk
== -1)
2072 if (rdev
->bdev
->bd_contains
==
2073 rdev2
->bdev
->bd_contains
) {
2083 static LIST_HEAD(pending_raid_disks
);
2086 * Try to register data integrity profile for an mddev
2088 * This is called when an array is started and after a disk has been kicked
2089 * from the array. It only succeeds if all working and active component devices
2090 * are integrity capable with matching profiles.
2092 int md_integrity_register(struct mddev
*mddev
)
2094 struct md_rdev
*rdev
, *reference
= NULL
;
2096 if (list_empty(&mddev
->disks
))
2097 return 0; /* nothing to do */
2098 if (!mddev
->gendisk
|| blk_get_integrity(mddev
->gendisk
))
2099 return 0; /* shouldn't register, or already is */
2100 rdev_for_each(rdev
, mddev
) {
2101 /* skip spares and non-functional disks */
2102 if (test_bit(Faulty
, &rdev
->flags
))
2104 if (rdev
->raid_disk
< 0)
2107 /* Use the first rdev as the reference */
2111 /* does this rdev's profile match the reference profile? */
2112 if (blk_integrity_compare(reference
->bdev
->bd_disk
,
2113 rdev
->bdev
->bd_disk
) < 0)
2116 if (!reference
|| !bdev_get_integrity(reference
->bdev
))
2119 * All component devices are integrity capable and have matching
2120 * profiles, register the common profile for the md device.
2122 blk_integrity_register(mddev
->gendisk
,
2123 bdev_get_integrity(reference
->bdev
));
2125 pr_debug("md: data integrity enabled on %s\n", mdname(mddev
));
2126 if (bioset_integrity_create(mddev
->bio_set
, BIO_POOL_SIZE
)) {
2127 pr_err("md: failed to create integrity pool for %s\n",
2133 EXPORT_SYMBOL(md_integrity_register
);
2136 * Attempt to add an rdev, but only if it is consistent with the current
2139 int md_integrity_add_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
2141 struct blk_integrity
*bi_rdev
;
2142 struct blk_integrity
*bi_mddev
;
2143 char name
[BDEVNAME_SIZE
];
2145 if (!mddev
->gendisk
)
2148 bi_rdev
= bdev_get_integrity(rdev
->bdev
);
2149 bi_mddev
= blk_get_integrity(mddev
->gendisk
);
2151 if (!bi_mddev
) /* nothing to do */
2154 if (blk_integrity_compare(mddev
->gendisk
, rdev
->bdev
->bd_disk
) != 0) {
2155 pr_err("%s: incompatible integrity profile for %s\n",
2156 mdname(mddev
), bdevname(rdev
->bdev
, name
));
2162 EXPORT_SYMBOL(md_integrity_add_rdev
);
2164 static int bind_rdev_to_array(struct md_rdev
*rdev
, struct mddev
*mddev
)
2166 char b
[BDEVNAME_SIZE
];
2170 /* prevent duplicates */
2171 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
2174 if ((bdev_read_only(rdev
->bdev
) || bdev_read_only(rdev
->meta_bdev
)) &&
2178 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2179 if (!test_bit(Journal
, &rdev
->flags
) &&
2181 (mddev
->dev_sectors
== 0 || rdev
->sectors
< mddev
->dev_sectors
)) {
2183 /* Cannot change size, so fail
2184 * If mddev->level <= 0, then we don't care
2185 * about aligning sizes (e.g. linear)
2187 if (mddev
->level
> 0)
2190 mddev
->dev_sectors
= rdev
->sectors
;
2193 /* Verify rdev->desc_nr is unique.
2194 * If it is -1, assign a free number, else
2195 * check number is not in use
2198 if (rdev
->desc_nr
< 0) {
2201 choice
= mddev
->raid_disks
;
2202 while (md_find_rdev_nr_rcu(mddev
, choice
))
2204 rdev
->desc_nr
= choice
;
2206 if (md_find_rdev_nr_rcu(mddev
, rdev
->desc_nr
)) {
2212 if (!test_bit(Journal
, &rdev
->flags
) &&
2213 mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
2214 pr_warn("md: %s: array is limited to %d devices\n",
2215 mdname(mddev
), mddev
->max_disks
);
2218 bdevname(rdev
->bdev
,b
);
2219 strreplace(b
, '/', '!');
2221 rdev
->mddev
= mddev
;
2222 pr_debug("md: bind<%s>\n", b
);
2224 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
2227 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
2228 if (sysfs_create_link(&rdev
->kobj
, ko
, "block"))
2229 /* failure here is OK */;
2230 rdev
->sysfs_state
= sysfs_get_dirent_safe(rdev
->kobj
.sd
, "state");
2232 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
2233 bd_link_disk_holder(rdev
->bdev
, mddev
->gendisk
);
2235 /* May as well allow recovery to be retried once */
2236 mddev
->recovery_disabled
++;
2241 pr_warn("md: failed to register dev-%s for %s\n",
2246 static void md_delayed_delete(struct work_struct
*ws
)
2248 struct md_rdev
*rdev
= container_of(ws
, struct md_rdev
, del_work
);
2249 kobject_del(&rdev
->kobj
);
2250 kobject_put(&rdev
->kobj
);
2253 static void unbind_rdev_from_array(struct md_rdev
*rdev
)
2255 char b
[BDEVNAME_SIZE
];
2257 bd_unlink_disk_holder(rdev
->bdev
, rdev
->mddev
->gendisk
);
2258 list_del_rcu(&rdev
->same_set
);
2259 pr_debug("md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
2261 sysfs_remove_link(&rdev
->kobj
, "block");
2262 sysfs_put(rdev
->sysfs_state
);
2263 rdev
->sysfs_state
= NULL
;
2264 rdev
->badblocks
.count
= 0;
2265 /* We need to delay this, otherwise we can deadlock when
2266 * writing to 'remove' to "dev/state". We also need
2267 * to delay it due to rcu usage.
2270 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
2271 kobject_get(&rdev
->kobj
);
2272 queue_work(md_misc_wq
, &rdev
->del_work
);
2276 * prevent the device from being mounted, repartitioned or
2277 * otherwise reused by a RAID array (or any other kernel
2278 * subsystem), by bd_claiming the device.
2280 static int lock_rdev(struct md_rdev
*rdev
, dev_t dev
, int shared
)
2283 struct block_device
*bdev
;
2284 char b
[BDEVNAME_SIZE
];
2286 bdev
= blkdev_get_by_dev(dev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
,
2287 shared
? (struct md_rdev
*)lock_rdev
: rdev
);
2289 pr_warn("md: could not open %s.\n", __bdevname(dev
, b
));
2290 return PTR_ERR(bdev
);
2296 static void unlock_rdev(struct md_rdev
*rdev
)
2298 struct block_device
*bdev
= rdev
->bdev
;
2300 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
2303 void md_autodetect_dev(dev_t dev
);
2305 static void export_rdev(struct md_rdev
*rdev
)
2307 char b
[BDEVNAME_SIZE
];
2309 pr_debug("md: export_rdev(%s)\n", bdevname(rdev
->bdev
,b
));
2310 md_rdev_clear(rdev
);
2312 if (test_bit(AutoDetected
, &rdev
->flags
))
2313 md_autodetect_dev(rdev
->bdev
->bd_dev
);
2316 kobject_put(&rdev
->kobj
);
2319 void md_kick_rdev_from_array(struct md_rdev
*rdev
)
2321 unbind_rdev_from_array(rdev
);
2324 EXPORT_SYMBOL_GPL(md_kick_rdev_from_array
);
2326 static void export_array(struct mddev
*mddev
)
2328 struct md_rdev
*rdev
;
2330 while (!list_empty(&mddev
->disks
)) {
2331 rdev
= list_first_entry(&mddev
->disks
, struct md_rdev
,
2333 md_kick_rdev_from_array(rdev
);
2335 mddev
->raid_disks
= 0;
2336 mddev
->major_version
= 0;
2339 static bool set_in_sync(struct mddev
*mddev
)
2341 lockdep_assert_held(&mddev
->lock
);
2342 if (!mddev
->in_sync
) {
2343 mddev
->sync_checkers
++;
2344 spin_unlock(&mddev
->lock
);
2345 percpu_ref_switch_to_atomic_sync(&mddev
->writes_pending
);
2346 spin_lock(&mddev
->lock
);
2347 if (!mddev
->in_sync
&&
2348 percpu_ref_is_zero(&mddev
->writes_pending
)) {
2351 * Ensure ->in_sync is visible before we clear
2355 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
2356 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
2358 if (--mddev
->sync_checkers
== 0)
2359 percpu_ref_switch_to_percpu(&mddev
->writes_pending
);
2361 if (mddev
->safemode
== 1)
2362 mddev
->safemode
= 0;
2363 return mddev
->in_sync
;
2366 static void sync_sbs(struct mddev
*mddev
, int nospares
)
2368 /* Update each superblock (in-memory image), but
2369 * if we are allowed to, skip spares which already
2370 * have the right event counter, or have one earlier
2371 * (which would mean they aren't being marked as dirty
2372 * with the rest of the array)
2374 struct md_rdev
*rdev
;
2375 rdev_for_each(rdev
, mddev
) {
2376 if (rdev
->sb_events
== mddev
->events
||
2378 rdev
->raid_disk
< 0 &&
2379 rdev
->sb_events
+1 == mddev
->events
)) {
2380 /* Don't update this superblock */
2381 rdev
->sb_loaded
= 2;
2383 sync_super(mddev
, rdev
);
2384 rdev
->sb_loaded
= 1;
2389 static bool does_sb_need_changing(struct mddev
*mddev
)
2391 struct md_rdev
*rdev
;
2392 struct mdp_superblock_1
*sb
;
2395 /* Find a good rdev */
2396 rdev_for_each(rdev
, mddev
)
2397 if ((rdev
->raid_disk
>= 0) && !test_bit(Faulty
, &rdev
->flags
))
2400 /* No good device found. */
2404 sb
= page_address(rdev
->sb_page
);
2405 /* Check if a device has become faulty or a spare become active */
2406 rdev_for_each(rdev
, mddev
) {
2407 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
2408 /* Device activated? */
2409 if (role
== 0xffff && rdev
->raid_disk
>=0 &&
2410 !test_bit(Faulty
, &rdev
->flags
))
2412 /* Device turned faulty? */
2413 if (test_bit(Faulty
, &rdev
->flags
) && (role
< 0xfffd))
2417 /* Check if any mddev parameters have changed */
2418 if ((mddev
->dev_sectors
!= le64_to_cpu(sb
->size
)) ||
2419 (mddev
->reshape_position
!= le64_to_cpu(sb
->reshape_position
)) ||
2420 (mddev
->layout
!= le32_to_cpu(sb
->layout
)) ||
2421 (mddev
->raid_disks
!= le32_to_cpu(sb
->raid_disks
)) ||
2422 (mddev
->chunk_sectors
!= le32_to_cpu(sb
->chunksize
)))
2428 void md_update_sb(struct mddev
*mddev
, int force_change
)
2430 struct md_rdev
*rdev
;
2433 int any_badblocks_changed
= 0;
2438 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2443 if (mddev_is_clustered(mddev
)) {
2444 if (test_and_clear_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
))
2446 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
))
2448 ret
= md_cluster_ops
->metadata_update_start(mddev
);
2449 /* Has someone else has updated the sb */
2450 if (!does_sb_need_changing(mddev
)) {
2452 md_cluster_ops
->metadata_update_cancel(mddev
);
2453 bit_clear_unless(&mddev
->sb_flags
, BIT(MD_SB_CHANGE_PENDING
),
2454 BIT(MD_SB_CHANGE_DEVS
) |
2455 BIT(MD_SB_CHANGE_CLEAN
));
2461 * First make sure individual recovery_offsets are correct
2462 * curr_resync_completed can only be used during recovery.
2463 * During reshape/resync it might use array-addresses rather
2464 * that device addresses.
2466 rdev_for_each(rdev
, mddev
) {
2467 if (rdev
->raid_disk
>= 0 &&
2468 mddev
->delta_disks
>= 0 &&
2469 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
2470 test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
) &&
2471 !test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
2472 !test_bit(Journal
, &rdev
->flags
) &&
2473 !test_bit(In_sync
, &rdev
->flags
) &&
2474 mddev
->curr_resync_completed
> rdev
->recovery_offset
)
2475 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
2478 if (!mddev
->persistent
) {
2479 clear_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
2480 clear_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2481 if (!mddev
->external
) {
2482 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
2483 rdev_for_each(rdev
, mddev
) {
2484 if (rdev
->badblocks
.changed
) {
2485 rdev
->badblocks
.changed
= 0;
2486 ack_all_badblocks(&rdev
->badblocks
);
2487 md_error(mddev
, rdev
);
2489 clear_bit(Blocked
, &rdev
->flags
);
2490 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2491 wake_up(&rdev
->blocked_wait
);
2494 wake_up(&mddev
->sb_wait
);
2498 spin_lock(&mddev
->lock
);
2500 mddev
->utime
= ktime_get_real_seconds();
2502 if (test_and_clear_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
))
2504 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
))
2505 /* just a clean<-> dirty transition, possibly leave spares alone,
2506 * though if events isn't the right even/odd, we will have to do
2512 if (mddev
->degraded
)
2513 /* If the array is degraded, then skipping spares is both
2514 * dangerous and fairly pointless.
2515 * Dangerous because a device that was removed from the array
2516 * might have a event_count that still looks up-to-date,
2517 * so it can be re-added without a resync.
2518 * Pointless because if there are any spares to skip,
2519 * then a recovery will happen and soon that array won't
2520 * be degraded any more and the spare can go back to sleep then.
2524 sync_req
= mddev
->in_sync
;
2526 /* If this is just a dirty<->clean transition, and the array is clean
2527 * and 'events' is odd, we can roll back to the previous clean state */
2529 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
2530 && mddev
->can_decrease_events
2531 && mddev
->events
!= 1) {
2533 mddev
->can_decrease_events
= 0;
2535 /* otherwise we have to go forward and ... */
2537 mddev
->can_decrease_events
= nospares
;
2541 * This 64-bit counter should never wrap.
2542 * Either we are in around ~1 trillion A.C., assuming
2543 * 1 reboot per second, or we have a bug...
2545 WARN_ON(mddev
->events
== 0);
2547 rdev_for_each(rdev
, mddev
) {
2548 if (rdev
->badblocks
.changed
)
2549 any_badblocks_changed
++;
2550 if (test_bit(Faulty
, &rdev
->flags
))
2551 set_bit(FaultRecorded
, &rdev
->flags
);
2554 sync_sbs(mddev
, nospares
);
2555 spin_unlock(&mddev
->lock
);
2557 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2558 mdname(mddev
), mddev
->in_sync
);
2561 blk_add_trace_msg(mddev
->queue
, "md md_update_sb");
2563 bitmap_update_sb(mddev
->bitmap
);
2564 rdev_for_each(rdev
, mddev
) {
2565 char b
[BDEVNAME_SIZE
];
2567 if (rdev
->sb_loaded
!= 1)
2568 continue; /* no noise on spare devices */
2570 if (!test_bit(Faulty
, &rdev
->flags
)) {
2571 md_super_write(mddev
,rdev
,
2572 rdev
->sb_start
, rdev
->sb_size
,
2574 pr_debug("md: (write) %s's sb offset: %llu\n",
2575 bdevname(rdev
->bdev
, b
),
2576 (unsigned long long)rdev
->sb_start
);
2577 rdev
->sb_events
= mddev
->events
;
2578 if (rdev
->badblocks
.size
) {
2579 md_super_write(mddev
, rdev
,
2580 rdev
->badblocks
.sector
,
2581 rdev
->badblocks
.size
<< 9,
2583 rdev
->badblocks
.size
= 0;
2587 pr_debug("md: %s (skipping faulty)\n",
2588 bdevname(rdev
->bdev
, b
));
2590 if (mddev
->level
== LEVEL_MULTIPATH
)
2591 /* only need to write one superblock... */
2594 if (md_super_wait(mddev
) < 0)
2596 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
2598 if (mddev_is_clustered(mddev
) && ret
== 0)
2599 md_cluster_ops
->metadata_update_finish(mddev
);
2601 if (mddev
->in_sync
!= sync_req
||
2602 !bit_clear_unless(&mddev
->sb_flags
, BIT(MD_SB_CHANGE_PENDING
),
2603 BIT(MD_SB_CHANGE_DEVS
) | BIT(MD_SB_CHANGE_CLEAN
)))
2604 /* have to write it out again */
2606 wake_up(&mddev
->sb_wait
);
2607 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2608 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
2610 rdev_for_each(rdev
, mddev
) {
2611 if (test_and_clear_bit(FaultRecorded
, &rdev
->flags
))
2612 clear_bit(Blocked
, &rdev
->flags
);
2614 if (any_badblocks_changed
)
2615 ack_all_badblocks(&rdev
->badblocks
);
2616 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2617 wake_up(&rdev
->blocked_wait
);
2620 EXPORT_SYMBOL(md_update_sb
);
2622 static int add_bound_rdev(struct md_rdev
*rdev
)
2624 struct mddev
*mddev
= rdev
->mddev
;
2626 bool add_journal
= test_bit(Journal
, &rdev
->flags
);
2628 if (!mddev
->pers
->hot_remove_disk
|| add_journal
) {
2629 /* If there is hot_add_disk but no hot_remove_disk
2630 * then added disks for geometry changes,
2631 * and should be added immediately.
2633 super_types
[mddev
->major_version
].
2634 validate_super(mddev
, rdev
);
2636 mddev_suspend(mddev
);
2637 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
2639 mddev_resume(mddev
);
2641 md_kick_rdev_from_array(rdev
);
2645 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2647 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2648 if (mddev
->degraded
)
2649 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
2650 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2651 md_new_event(mddev
);
2652 md_wakeup_thread(mddev
->thread
);
2656 /* words written to sysfs files may, or may not, be \n terminated.
2657 * We want to accept with case. For this we use cmd_match.
2659 static int cmd_match(const char *cmd
, const char *str
)
2661 /* See if cmd, written into a sysfs file, matches
2662 * str. They must either be the same, or cmd can
2663 * have a trailing newline
2665 while (*cmd
&& *str
&& *cmd
== *str
) {
2676 struct rdev_sysfs_entry
{
2677 struct attribute attr
;
2678 ssize_t (*show
)(struct md_rdev
*, char *);
2679 ssize_t (*store
)(struct md_rdev
*, const char *, size_t);
2683 state_show(struct md_rdev
*rdev
, char *page
)
2687 unsigned long flags
= READ_ONCE(rdev
->flags
);
2689 if (test_bit(Faulty
, &flags
) ||
2690 (!test_bit(ExternalBbl
, &flags
) &&
2691 rdev
->badblocks
.unacked_exist
))
2692 len
+= sprintf(page
+len
, "faulty%s", sep
);
2693 if (test_bit(In_sync
, &flags
))
2694 len
+= sprintf(page
+len
, "in_sync%s", sep
);
2695 if (test_bit(Journal
, &flags
))
2696 len
+= sprintf(page
+len
, "journal%s", sep
);
2697 if (test_bit(WriteMostly
, &flags
))
2698 len
+= sprintf(page
+len
, "write_mostly%s", sep
);
2699 if (test_bit(Blocked
, &flags
) ||
2700 (rdev
->badblocks
.unacked_exist
2701 && !test_bit(Faulty
, &flags
)))
2702 len
+= sprintf(page
+len
, "blocked%s", sep
);
2703 if (!test_bit(Faulty
, &flags
) &&
2704 !test_bit(Journal
, &flags
) &&
2705 !test_bit(In_sync
, &flags
))
2706 len
+= sprintf(page
+len
, "spare%s", sep
);
2707 if (test_bit(WriteErrorSeen
, &flags
))
2708 len
+= sprintf(page
+len
, "write_error%s", sep
);
2709 if (test_bit(WantReplacement
, &flags
))
2710 len
+= sprintf(page
+len
, "want_replacement%s", sep
);
2711 if (test_bit(Replacement
, &flags
))
2712 len
+= sprintf(page
+len
, "replacement%s", sep
);
2713 if (test_bit(ExternalBbl
, &flags
))
2714 len
+= sprintf(page
+len
, "external_bbl%s", sep
);
2715 if (test_bit(FailFast
, &flags
))
2716 len
+= sprintf(page
+len
, "failfast%s", sep
);
2721 return len
+sprintf(page
+len
, "\n");
2725 state_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2728 * faulty - simulates an error
2729 * remove - disconnects the device
2730 * writemostly - sets write_mostly
2731 * -writemostly - clears write_mostly
2732 * blocked - sets the Blocked flags
2733 * -blocked - clears the Blocked and possibly simulates an error
2734 * insync - sets Insync providing device isn't active
2735 * -insync - clear Insync for a device with a slot assigned,
2736 * so that it gets rebuilt based on bitmap
2737 * write_error - sets WriteErrorSeen
2738 * -write_error - clears WriteErrorSeen
2739 * {,-}failfast - set/clear FailFast
2742 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
2743 md_error(rdev
->mddev
, rdev
);
2744 if (test_bit(Faulty
, &rdev
->flags
))
2748 } else if (cmd_match(buf
, "remove")) {
2749 if (rdev
->mddev
->pers
) {
2750 clear_bit(Blocked
, &rdev
->flags
);
2751 remove_and_add_spares(rdev
->mddev
, rdev
);
2753 if (rdev
->raid_disk
>= 0)
2756 struct mddev
*mddev
= rdev
->mddev
;
2758 if (mddev_is_clustered(mddev
))
2759 err
= md_cluster_ops
->remove_disk(mddev
, rdev
);
2762 md_kick_rdev_from_array(rdev
);
2764 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2765 md_wakeup_thread(mddev
->thread
);
2767 md_new_event(mddev
);
2770 } else if (cmd_match(buf
, "writemostly")) {
2771 set_bit(WriteMostly
, &rdev
->flags
);
2773 } else if (cmd_match(buf
, "-writemostly")) {
2774 clear_bit(WriteMostly
, &rdev
->flags
);
2776 } else if (cmd_match(buf
, "blocked")) {
2777 set_bit(Blocked
, &rdev
->flags
);
2779 } else if (cmd_match(buf
, "-blocked")) {
2780 if (!test_bit(Faulty
, &rdev
->flags
) &&
2781 !test_bit(ExternalBbl
, &rdev
->flags
) &&
2782 rdev
->badblocks
.unacked_exist
) {
2783 /* metadata handler doesn't understand badblocks,
2784 * so we need to fail the device
2786 md_error(rdev
->mddev
, rdev
);
2788 clear_bit(Blocked
, &rdev
->flags
);
2789 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2790 wake_up(&rdev
->blocked_wait
);
2791 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2792 md_wakeup_thread(rdev
->mddev
->thread
);
2795 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
2796 set_bit(In_sync
, &rdev
->flags
);
2798 } else if (cmd_match(buf
, "failfast")) {
2799 set_bit(FailFast
, &rdev
->flags
);
2801 } else if (cmd_match(buf
, "-failfast")) {
2802 clear_bit(FailFast
, &rdev
->flags
);
2804 } else if (cmd_match(buf
, "-insync") && rdev
->raid_disk
>= 0 &&
2805 !test_bit(Journal
, &rdev
->flags
)) {
2806 if (rdev
->mddev
->pers
== NULL
) {
2807 clear_bit(In_sync
, &rdev
->flags
);
2808 rdev
->saved_raid_disk
= rdev
->raid_disk
;
2809 rdev
->raid_disk
= -1;
2812 } else if (cmd_match(buf
, "write_error")) {
2813 set_bit(WriteErrorSeen
, &rdev
->flags
);
2815 } else if (cmd_match(buf
, "-write_error")) {
2816 clear_bit(WriteErrorSeen
, &rdev
->flags
);
2818 } else if (cmd_match(buf
, "want_replacement")) {
2819 /* Any non-spare device that is not a replacement can
2820 * become want_replacement at any time, but we then need to
2821 * check if recovery is needed.
2823 if (rdev
->raid_disk
>= 0 &&
2824 !test_bit(Journal
, &rdev
->flags
) &&
2825 !test_bit(Replacement
, &rdev
->flags
))
2826 set_bit(WantReplacement
, &rdev
->flags
);
2827 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2828 md_wakeup_thread(rdev
->mddev
->thread
);
2830 } else if (cmd_match(buf
, "-want_replacement")) {
2831 /* Clearing 'want_replacement' is always allowed.
2832 * Once replacements starts it is too late though.
2835 clear_bit(WantReplacement
, &rdev
->flags
);
2836 } else if (cmd_match(buf
, "replacement")) {
2837 /* Can only set a device as a replacement when array has not
2838 * yet been started. Once running, replacement is automatic
2839 * from spares, or by assigning 'slot'.
2841 if (rdev
->mddev
->pers
)
2844 set_bit(Replacement
, &rdev
->flags
);
2847 } else if (cmd_match(buf
, "-replacement")) {
2848 /* Similarly, can only clear Replacement before start */
2849 if (rdev
->mddev
->pers
)
2852 clear_bit(Replacement
, &rdev
->flags
);
2855 } else if (cmd_match(buf
, "re-add")) {
2856 if (test_bit(Faulty
, &rdev
->flags
) && (rdev
->raid_disk
== -1)) {
2857 /* clear_bit is performed _after_ all the devices
2858 * have their local Faulty bit cleared. If any writes
2859 * happen in the meantime in the local node, they
2860 * will land in the local bitmap, which will be synced
2861 * by this node eventually
2863 if (!mddev_is_clustered(rdev
->mddev
) ||
2864 (err
= md_cluster_ops
->gather_bitmaps(rdev
)) == 0) {
2865 clear_bit(Faulty
, &rdev
->flags
);
2866 err
= add_bound_rdev(rdev
);
2870 } else if (cmd_match(buf
, "external_bbl") && (rdev
->mddev
->external
)) {
2871 set_bit(ExternalBbl
, &rdev
->flags
);
2872 rdev
->badblocks
.shift
= 0;
2874 } else if (cmd_match(buf
, "-external_bbl") && (rdev
->mddev
->external
)) {
2875 clear_bit(ExternalBbl
, &rdev
->flags
);
2879 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2880 return err
? err
: len
;
2882 static struct rdev_sysfs_entry rdev_state
=
2883 __ATTR_PREALLOC(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
2886 errors_show(struct md_rdev
*rdev
, char *page
)
2888 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
2892 errors_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2897 rv
= kstrtouint(buf
, 10, &n
);
2900 atomic_set(&rdev
->corrected_errors
, n
);
2903 static struct rdev_sysfs_entry rdev_errors
=
2904 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
2907 slot_show(struct md_rdev
*rdev
, char *page
)
2909 if (test_bit(Journal
, &rdev
->flags
))
2910 return sprintf(page
, "journal\n");
2911 else if (rdev
->raid_disk
< 0)
2912 return sprintf(page
, "none\n");
2914 return sprintf(page
, "%d\n", rdev
->raid_disk
);
2918 slot_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2923 if (test_bit(Journal
, &rdev
->flags
))
2925 if (strncmp(buf
, "none", 4)==0)
2928 err
= kstrtouint(buf
, 10, (unsigned int *)&slot
);
2932 if (rdev
->mddev
->pers
&& slot
== -1) {
2933 /* Setting 'slot' on an active array requires also
2934 * updating the 'rd%d' link, and communicating
2935 * with the personality with ->hot_*_disk.
2936 * For now we only support removing
2937 * failed/spare devices. This normally happens automatically,
2938 * but not when the metadata is externally managed.
2940 if (rdev
->raid_disk
== -1)
2942 /* personality does all needed checks */
2943 if (rdev
->mddev
->pers
->hot_remove_disk
== NULL
)
2945 clear_bit(Blocked
, &rdev
->flags
);
2946 remove_and_add_spares(rdev
->mddev
, rdev
);
2947 if (rdev
->raid_disk
>= 0)
2949 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2950 md_wakeup_thread(rdev
->mddev
->thread
);
2951 } else if (rdev
->mddev
->pers
) {
2952 /* Activating a spare .. or possibly reactivating
2953 * if we ever get bitmaps working here.
2957 if (rdev
->raid_disk
!= -1)
2960 if (test_bit(MD_RECOVERY_RUNNING
, &rdev
->mddev
->recovery
))
2963 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2966 if (slot
>= rdev
->mddev
->raid_disks
&&
2967 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2970 rdev
->raid_disk
= slot
;
2971 if (test_bit(In_sync
, &rdev
->flags
))
2972 rdev
->saved_raid_disk
= slot
;
2974 rdev
->saved_raid_disk
= -1;
2975 clear_bit(In_sync
, &rdev
->flags
);
2976 clear_bit(Bitmap_sync
, &rdev
->flags
);
2977 err
= rdev
->mddev
->pers
->
2978 hot_add_disk(rdev
->mddev
, rdev
);
2980 rdev
->raid_disk
= -1;
2983 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2984 if (sysfs_link_rdev(rdev
->mddev
, rdev
))
2985 /* failure here is OK */;
2986 /* don't wakeup anyone, leave that to userspace. */
2988 if (slot
>= rdev
->mddev
->raid_disks
&&
2989 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2991 rdev
->raid_disk
= slot
;
2992 /* assume it is working */
2993 clear_bit(Faulty
, &rdev
->flags
);
2994 clear_bit(WriteMostly
, &rdev
->flags
);
2995 set_bit(In_sync
, &rdev
->flags
);
2996 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
3001 static struct rdev_sysfs_entry rdev_slot
=
3002 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
3005 offset_show(struct md_rdev
*rdev
, char *page
)
3007 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
3011 offset_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3013 unsigned long long offset
;
3014 if (kstrtoull(buf
, 10, &offset
) < 0)
3016 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
3018 if (rdev
->sectors
&& rdev
->mddev
->external
)
3019 /* Must set offset before size, so overlap checks
3022 rdev
->data_offset
= offset
;
3023 rdev
->new_data_offset
= offset
;
3027 static struct rdev_sysfs_entry rdev_offset
=
3028 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
3030 static ssize_t
new_offset_show(struct md_rdev
*rdev
, char *page
)
3032 return sprintf(page
, "%llu\n",
3033 (unsigned long long)rdev
->new_data_offset
);
3036 static ssize_t
new_offset_store(struct md_rdev
*rdev
,
3037 const char *buf
, size_t len
)
3039 unsigned long long new_offset
;
3040 struct mddev
*mddev
= rdev
->mddev
;
3042 if (kstrtoull(buf
, 10, &new_offset
) < 0)
3045 if (mddev
->sync_thread
||
3046 test_bit(MD_RECOVERY_RUNNING
,&mddev
->recovery
))
3048 if (new_offset
== rdev
->data_offset
)
3049 /* reset is always permitted */
3051 else if (new_offset
> rdev
->data_offset
) {
3052 /* must not push array size beyond rdev_sectors */
3053 if (new_offset
- rdev
->data_offset
3054 + mddev
->dev_sectors
> rdev
->sectors
)
3057 /* Metadata worries about other space details. */
3059 /* decreasing the offset is inconsistent with a backwards
3062 if (new_offset
< rdev
->data_offset
&&
3063 mddev
->reshape_backwards
)
3065 /* Increasing offset is inconsistent with forwards
3066 * reshape. reshape_direction should be set to
3067 * 'backwards' first.
3069 if (new_offset
> rdev
->data_offset
&&
3070 !mddev
->reshape_backwards
)
3073 if (mddev
->pers
&& mddev
->persistent
&&
3074 !super_types
[mddev
->major_version
]
3075 .allow_new_offset(rdev
, new_offset
))
3077 rdev
->new_data_offset
= new_offset
;
3078 if (new_offset
> rdev
->data_offset
)
3079 mddev
->reshape_backwards
= 1;
3080 else if (new_offset
< rdev
->data_offset
)
3081 mddev
->reshape_backwards
= 0;
3085 static struct rdev_sysfs_entry rdev_new_offset
=
3086 __ATTR(new_offset
, S_IRUGO
|S_IWUSR
, new_offset_show
, new_offset_store
);
3089 rdev_size_show(struct md_rdev
*rdev
, char *page
)
3091 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
3094 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
3096 /* check if two start/length pairs overlap */
3104 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
3106 unsigned long long blocks
;
3109 if (kstrtoull(buf
, 10, &blocks
) < 0)
3112 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
3113 return -EINVAL
; /* sector conversion overflow */
3116 if (new != blocks
* 2)
3117 return -EINVAL
; /* unsigned long long to sector_t overflow */
3124 rdev_size_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3126 struct mddev
*my_mddev
= rdev
->mddev
;
3127 sector_t oldsectors
= rdev
->sectors
;
3130 if (test_bit(Journal
, &rdev
->flags
))
3132 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
3134 if (rdev
->data_offset
!= rdev
->new_data_offset
)
3135 return -EINVAL
; /* too confusing */
3136 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
3137 if (my_mddev
->persistent
) {
3138 sectors
= super_types
[my_mddev
->major_version
].
3139 rdev_size_change(rdev
, sectors
);
3142 } else if (!sectors
)
3143 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
3145 if (!my_mddev
->pers
->resize
)
3146 /* Cannot change size for RAID0 or Linear etc */
3149 if (sectors
< my_mddev
->dev_sectors
)
3150 return -EINVAL
; /* component must fit device */
3152 rdev
->sectors
= sectors
;
3153 if (sectors
> oldsectors
&& my_mddev
->external
) {
3154 /* Need to check that all other rdevs with the same
3155 * ->bdev do not overlap. 'rcu' is sufficient to walk
3156 * the rdev lists safely.
3157 * This check does not provide a hard guarantee, it
3158 * just helps avoid dangerous mistakes.
3160 struct mddev
*mddev
;
3162 struct list_head
*tmp
;
3165 for_each_mddev(mddev
, tmp
) {
3166 struct md_rdev
*rdev2
;
3168 rdev_for_each(rdev2
, mddev
)
3169 if (rdev
->bdev
== rdev2
->bdev
&&
3171 overlaps(rdev
->data_offset
, rdev
->sectors
,
3184 /* Someone else could have slipped in a size
3185 * change here, but doing so is just silly.
3186 * We put oldsectors back because we *know* it is
3187 * safe, and trust userspace not to race with
3190 rdev
->sectors
= oldsectors
;
3197 static struct rdev_sysfs_entry rdev_size
=
3198 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
3200 static ssize_t
recovery_start_show(struct md_rdev
*rdev
, char *page
)
3202 unsigned long long recovery_start
= rdev
->recovery_offset
;
3204 if (test_bit(In_sync
, &rdev
->flags
) ||
3205 recovery_start
== MaxSector
)
3206 return sprintf(page
, "none\n");
3208 return sprintf(page
, "%llu\n", recovery_start
);
3211 static ssize_t
recovery_start_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3213 unsigned long long recovery_start
;
3215 if (cmd_match(buf
, "none"))
3216 recovery_start
= MaxSector
;
3217 else if (kstrtoull(buf
, 10, &recovery_start
))
3220 if (rdev
->mddev
->pers
&&
3221 rdev
->raid_disk
>= 0)
3224 rdev
->recovery_offset
= recovery_start
;
3225 if (recovery_start
== MaxSector
)
3226 set_bit(In_sync
, &rdev
->flags
);
3228 clear_bit(In_sync
, &rdev
->flags
);
3232 static struct rdev_sysfs_entry rdev_recovery_start
=
3233 __ATTR(recovery_start
, S_IRUGO
|S_IWUSR
, recovery_start_show
, recovery_start_store
);
3235 /* sysfs access to bad-blocks list.
3236 * We present two files.
3237 * 'bad-blocks' lists sector numbers and lengths of ranges that
3238 * are recorded as bad. The list is truncated to fit within
3239 * the one-page limit of sysfs.
3240 * Writing "sector length" to this file adds an acknowledged
3242 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3243 * been acknowledged. Writing to this file adds bad blocks
3244 * without acknowledging them. This is largely for testing.
3246 static ssize_t
bb_show(struct md_rdev
*rdev
, char *page
)
3248 return badblocks_show(&rdev
->badblocks
, page
, 0);
3250 static ssize_t
bb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
3252 int rv
= badblocks_store(&rdev
->badblocks
, page
, len
, 0);
3253 /* Maybe that ack was all we needed */
3254 if (test_and_clear_bit(BlockedBadBlocks
, &rdev
->flags
))
3255 wake_up(&rdev
->blocked_wait
);
3258 static struct rdev_sysfs_entry rdev_bad_blocks
=
3259 __ATTR(bad_blocks
, S_IRUGO
|S_IWUSR
, bb_show
, bb_store
);
3261 static ssize_t
ubb_show(struct md_rdev
*rdev
, char *page
)
3263 return badblocks_show(&rdev
->badblocks
, page
, 1);
3265 static ssize_t
ubb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
3267 return badblocks_store(&rdev
->badblocks
, page
, len
, 1);
3269 static struct rdev_sysfs_entry rdev_unack_bad_blocks
=
3270 __ATTR(unacknowledged_bad_blocks
, S_IRUGO
|S_IWUSR
, ubb_show
, ubb_store
);
3273 ppl_sector_show(struct md_rdev
*rdev
, char *page
)
3275 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->ppl
.sector
);
3279 ppl_sector_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3281 unsigned long long sector
;
3283 if (kstrtoull(buf
, 10, §or
) < 0)
3285 if (sector
!= (sector_t
)sector
)
3288 if (rdev
->mddev
->pers
&& test_bit(MD_HAS_PPL
, &rdev
->mddev
->flags
) &&
3289 rdev
->raid_disk
>= 0)
3292 if (rdev
->mddev
->persistent
) {
3293 if (rdev
->mddev
->major_version
== 0)
3295 if ((sector
> rdev
->sb_start
&&
3296 sector
- rdev
->sb_start
> S16_MAX
) ||
3297 (sector
< rdev
->sb_start
&&
3298 rdev
->sb_start
- sector
> -S16_MIN
))
3300 rdev
->ppl
.offset
= sector
- rdev
->sb_start
;
3301 } else if (!rdev
->mddev
->external
) {
3304 rdev
->ppl
.sector
= sector
;
3308 static struct rdev_sysfs_entry rdev_ppl_sector
=
3309 __ATTR(ppl_sector
, S_IRUGO
|S_IWUSR
, ppl_sector_show
, ppl_sector_store
);
3312 ppl_size_show(struct md_rdev
*rdev
, char *page
)
3314 return sprintf(page
, "%u\n", rdev
->ppl
.size
);
3318 ppl_size_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3322 if (kstrtouint(buf
, 10, &size
) < 0)
3325 if (rdev
->mddev
->pers
&& test_bit(MD_HAS_PPL
, &rdev
->mddev
->flags
) &&
3326 rdev
->raid_disk
>= 0)
3329 if (rdev
->mddev
->persistent
) {
3330 if (rdev
->mddev
->major_version
== 0)
3334 } else if (!rdev
->mddev
->external
) {
3337 rdev
->ppl
.size
= size
;
3341 static struct rdev_sysfs_entry rdev_ppl_size
=
3342 __ATTR(ppl_size
, S_IRUGO
|S_IWUSR
, ppl_size_show
, ppl_size_store
);
3344 static struct attribute
*rdev_default_attrs
[] = {
3349 &rdev_new_offset
.attr
,
3351 &rdev_recovery_start
.attr
,
3352 &rdev_bad_blocks
.attr
,
3353 &rdev_unack_bad_blocks
.attr
,
3354 &rdev_ppl_sector
.attr
,
3355 &rdev_ppl_size
.attr
,
3359 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
3361 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3362 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3368 return entry
->show(rdev
, page
);
3372 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
3373 const char *page
, size_t length
)
3375 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3376 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3378 struct mddev
*mddev
= rdev
->mddev
;
3382 if (!capable(CAP_SYS_ADMIN
))
3384 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
3386 if (rdev
->mddev
== NULL
)
3389 rv
= entry
->store(rdev
, page
, length
);
3390 mddev_unlock(mddev
);
3395 static void rdev_free(struct kobject
*ko
)
3397 struct md_rdev
*rdev
= container_of(ko
, struct md_rdev
, kobj
);
3400 static const struct sysfs_ops rdev_sysfs_ops
= {
3401 .show
= rdev_attr_show
,
3402 .store
= rdev_attr_store
,
3404 static struct kobj_type rdev_ktype
= {
3405 .release
= rdev_free
,
3406 .sysfs_ops
= &rdev_sysfs_ops
,
3407 .default_attrs
= rdev_default_attrs
,
3410 int md_rdev_init(struct md_rdev
*rdev
)
3413 rdev
->saved_raid_disk
= -1;
3414 rdev
->raid_disk
= -1;
3416 rdev
->data_offset
= 0;
3417 rdev
->new_data_offset
= 0;
3418 rdev
->sb_events
= 0;
3419 rdev
->last_read_error
= 0;
3420 rdev
->sb_loaded
= 0;
3421 rdev
->bb_page
= NULL
;
3422 atomic_set(&rdev
->nr_pending
, 0);
3423 atomic_set(&rdev
->read_errors
, 0);
3424 atomic_set(&rdev
->corrected_errors
, 0);
3426 INIT_LIST_HEAD(&rdev
->same_set
);
3427 init_waitqueue_head(&rdev
->blocked_wait
);
3429 /* Add space to store bad block list.
3430 * This reserves the space even on arrays where it cannot
3431 * be used - I wonder if that matters
3433 return badblocks_init(&rdev
->badblocks
, 0);
3435 EXPORT_SYMBOL_GPL(md_rdev_init
);
3437 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3439 * mark the device faulty if:
3441 * - the device is nonexistent (zero size)
3442 * - the device has no valid superblock
3444 * a faulty rdev _never_ has rdev->sb set.
3446 static struct md_rdev
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
3448 char b
[BDEVNAME_SIZE
];
3450 struct md_rdev
*rdev
;
3453 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
3455 return ERR_PTR(-ENOMEM
);
3457 err
= md_rdev_init(rdev
);
3460 err
= alloc_disk_sb(rdev
);
3464 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
3468 kobject_init(&rdev
->kobj
, &rdev_ktype
);
3470 size
= i_size_read(rdev
->bdev
->bd_inode
) >> BLOCK_SIZE_BITS
;
3472 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
3473 bdevname(rdev
->bdev
,b
));
3478 if (super_format
>= 0) {
3479 err
= super_types
[super_format
].
3480 load_super(rdev
, NULL
, super_minor
);
3481 if (err
== -EINVAL
) {
3482 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
3483 bdevname(rdev
->bdev
,b
),
3484 super_format
, super_minor
);
3488 pr_warn("md: could not read %s's sb, not importing!\n",
3489 bdevname(rdev
->bdev
,b
));
3499 md_rdev_clear(rdev
);
3501 return ERR_PTR(err
);
3505 * Check a full RAID array for plausibility
3508 static void analyze_sbs(struct mddev
*mddev
)
3511 struct md_rdev
*rdev
, *freshest
, *tmp
;
3512 char b
[BDEVNAME_SIZE
];
3515 rdev_for_each_safe(rdev
, tmp
, mddev
)
3516 switch (super_types
[mddev
->major_version
].
3517 load_super(rdev
, freshest
, mddev
->minor_version
)) {
3524 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
3525 bdevname(rdev
->bdev
,b
));
3526 md_kick_rdev_from_array(rdev
);
3529 super_types
[mddev
->major_version
].
3530 validate_super(mddev
, freshest
);
3533 rdev_for_each_safe(rdev
, tmp
, mddev
) {
3534 if (mddev
->max_disks
&&
3535 (rdev
->desc_nr
>= mddev
->max_disks
||
3536 i
> mddev
->max_disks
)) {
3537 pr_warn("md: %s: %s: only %d devices permitted\n",
3538 mdname(mddev
), bdevname(rdev
->bdev
, b
),
3540 md_kick_rdev_from_array(rdev
);
3543 if (rdev
!= freshest
) {
3544 if (super_types
[mddev
->major_version
].
3545 validate_super(mddev
, rdev
)) {
3546 pr_warn("md: kicking non-fresh %s from array!\n",
3547 bdevname(rdev
->bdev
,b
));
3548 md_kick_rdev_from_array(rdev
);
3552 if (mddev
->level
== LEVEL_MULTIPATH
) {
3553 rdev
->desc_nr
= i
++;
3554 rdev
->raid_disk
= rdev
->desc_nr
;
3555 set_bit(In_sync
, &rdev
->flags
);
3556 } else if (rdev
->raid_disk
>=
3557 (mddev
->raid_disks
- min(0, mddev
->delta_disks
)) &&
3558 !test_bit(Journal
, &rdev
->flags
)) {
3559 rdev
->raid_disk
= -1;
3560 clear_bit(In_sync
, &rdev
->flags
);
3565 /* Read a fixed-point number.
3566 * Numbers in sysfs attributes should be in "standard" units where
3567 * possible, so time should be in seconds.
3568 * However we internally use a a much smaller unit such as
3569 * milliseconds or jiffies.
3570 * This function takes a decimal number with a possible fractional
3571 * component, and produces an integer which is the result of
3572 * multiplying that number by 10^'scale'.
3573 * all without any floating-point arithmetic.
3575 int strict_strtoul_scaled(const char *cp
, unsigned long *res
, int scale
)
3577 unsigned long result
= 0;
3579 while (isdigit(*cp
) || (*cp
== '.' && decimals
< 0)) {
3582 else if (decimals
< scale
) {
3585 result
= result
* 10 + value
;
3597 while (decimals
< scale
) {
3606 safe_delay_show(struct mddev
*mddev
, char *page
)
3608 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
3609 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
3612 safe_delay_store(struct mddev
*mddev
, const char *cbuf
, size_t len
)
3616 if (mddev_is_clustered(mddev
)) {
3617 pr_warn("md: Safemode is disabled for clustered mode\n");
3621 if (strict_strtoul_scaled(cbuf
, &msec
, 3) < 0)
3624 mddev
->safemode_delay
= 0;
3626 unsigned long old_delay
= mddev
->safemode_delay
;
3627 unsigned long new_delay
= (msec
*HZ
)/1000;
3631 mddev
->safemode_delay
= new_delay
;
3632 if (new_delay
< old_delay
|| old_delay
== 0)
3633 mod_timer(&mddev
->safemode_timer
, jiffies
+1);
3637 static struct md_sysfs_entry md_safe_delay
=
3638 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
3641 level_show(struct mddev
*mddev
, char *page
)
3643 struct md_personality
*p
;
3645 spin_lock(&mddev
->lock
);
3648 ret
= sprintf(page
, "%s\n", p
->name
);
3649 else if (mddev
->clevel
[0])
3650 ret
= sprintf(page
, "%s\n", mddev
->clevel
);
3651 else if (mddev
->level
!= LEVEL_NONE
)
3652 ret
= sprintf(page
, "%d\n", mddev
->level
);
3655 spin_unlock(&mddev
->lock
);
3660 level_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3665 struct md_personality
*pers
, *oldpers
;
3667 void *priv
, *oldpriv
;
3668 struct md_rdev
*rdev
;
3670 if (slen
== 0 || slen
>= sizeof(clevel
))
3673 rv
= mddev_lock(mddev
);
3677 if (mddev
->pers
== NULL
) {
3678 strncpy(mddev
->clevel
, buf
, slen
);
3679 if (mddev
->clevel
[slen
-1] == '\n')
3681 mddev
->clevel
[slen
] = 0;
3682 mddev
->level
= LEVEL_NONE
;
3690 /* request to change the personality. Need to ensure:
3691 * - array is not engaged in resync/recovery/reshape
3692 * - old personality can be suspended
3693 * - new personality will access other array.
3697 if (mddev
->sync_thread
||
3698 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3699 mddev
->reshape_position
!= MaxSector
||
3700 mddev
->sysfs_active
)
3704 if (!mddev
->pers
->quiesce
) {
3705 pr_warn("md: %s: %s does not support online personality change\n",
3706 mdname(mddev
), mddev
->pers
->name
);
3710 /* Now find the new personality */
3711 strncpy(clevel
, buf
, slen
);
3712 if (clevel
[slen
-1] == '\n')
3715 if (kstrtol(clevel
, 10, &level
))
3718 if (request_module("md-%s", clevel
) != 0)
3719 request_module("md-level-%s", clevel
);
3720 spin_lock(&pers_lock
);
3721 pers
= find_pers(level
, clevel
);
3722 if (!pers
|| !try_module_get(pers
->owner
)) {
3723 spin_unlock(&pers_lock
);
3724 pr_warn("md: personality %s not loaded\n", clevel
);
3728 spin_unlock(&pers_lock
);
3730 if (pers
== mddev
->pers
) {
3731 /* Nothing to do! */
3732 module_put(pers
->owner
);
3736 if (!pers
->takeover
) {
3737 module_put(pers
->owner
);
3738 pr_warn("md: %s: %s does not support personality takeover\n",
3739 mdname(mddev
), clevel
);
3744 rdev_for_each(rdev
, mddev
)
3745 rdev
->new_raid_disk
= rdev
->raid_disk
;
3747 /* ->takeover must set new_* and/or delta_disks
3748 * if it succeeds, and may set them when it fails.
3750 priv
= pers
->takeover(mddev
);
3752 mddev
->new_level
= mddev
->level
;
3753 mddev
->new_layout
= mddev
->layout
;
3754 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3755 mddev
->raid_disks
-= mddev
->delta_disks
;
3756 mddev
->delta_disks
= 0;
3757 mddev
->reshape_backwards
= 0;
3758 module_put(pers
->owner
);
3759 pr_warn("md: %s: %s would not accept array\n",
3760 mdname(mddev
), clevel
);
3765 /* Looks like we have a winner */
3766 mddev_suspend(mddev
);
3767 mddev_detach(mddev
);
3769 spin_lock(&mddev
->lock
);
3770 oldpers
= mddev
->pers
;
3771 oldpriv
= mddev
->private;
3773 mddev
->private = priv
;
3774 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3775 mddev
->level
= mddev
->new_level
;
3776 mddev
->layout
= mddev
->new_layout
;
3777 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
3778 mddev
->delta_disks
= 0;
3779 mddev
->reshape_backwards
= 0;
3780 mddev
->degraded
= 0;
3781 spin_unlock(&mddev
->lock
);
3783 if (oldpers
->sync_request
== NULL
&&
3785 /* We are converting from a no-redundancy array
3786 * to a redundancy array and metadata is managed
3787 * externally so we need to be sure that writes
3788 * won't block due to a need to transition
3790 * until external management is started.
3793 mddev
->safemode_delay
= 0;
3794 mddev
->safemode
= 0;
3797 oldpers
->free(mddev
, oldpriv
);
3799 if (oldpers
->sync_request
== NULL
&&
3800 pers
->sync_request
!= NULL
) {
3801 /* need to add the md_redundancy_group */
3802 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
3803 pr_warn("md: cannot register extra attributes for %s\n",
3805 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, "sync_action");
3807 if (oldpers
->sync_request
!= NULL
&&
3808 pers
->sync_request
== NULL
) {
3809 /* need to remove the md_redundancy_group */
3810 if (mddev
->to_remove
== NULL
)
3811 mddev
->to_remove
= &md_redundancy_group
;
3814 module_put(oldpers
->owner
);
3816 rdev_for_each(rdev
, mddev
) {
3817 if (rdev
->raid_disk
< 0)
3819 if (rdev
->new_raid_disk
>= mddev
->raid_disks
)
3820 rdev
->new_raid_disk
= -1;
3821 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3823 sysfs_unlink_rdev(mddev
, rdev
);
3825 rdev_for_each(rdev
, mddev
) {
3826 if (rdev
->raid_disk
< 0)
3828 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3830 rdev
->raid_disk
= rdev
->new_raid_disk
;
3831 if (rdev
->raid_disk
< 0)
3832 clear_bit(In_sync
, &rdev
->flags
);
3834 if (sysfs_link_rdev(mddev
, rdev
))
3835 pr_warn("md: cannot register rd%d for %s after level change\n",
3836 rdev
->raid_disk
, mdname(mddev
));
3840 if (pers
->sync_request
== NULL
) {
3841 /* this is now an array without redundancy, so
3842 * it must always be in_sync
3845 del_timer_sync(&mddev
->safemode_timer
);
3847 blk_set_stacking_limits(&mddev
->queue
->limits
);
3849 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
3850 mddev_resume(mddev
);
3852 md_update_sb(mddev
, 1);
3853 sysfs_notify(&mddev
->kobj
, NULL
, "level");
3854 md_new_event(mddev
);
3857 mddev_unlock(mddev
);
3861 static struct md_sysfs_entry md_level
=
3862 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
3865 layout_show(struct mddev
*mddev
, char *page
)
3867 /* just a number, not meaningful for all levels */
3868 if (mddev
->reshape_position
!= MaxSector
&&
3869 mddev
->layout
!= mddev
->new_layout
)
3870 return sprintf(page
, "%d (%d)\n",
3871 mddev
->new_layout
, mddev
->layout
);
3872 return sprintf(page
, "%d\n", mddev
->layout
);
3876 layout_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3881 err
= kstrtouint(buf
, 10, &n
);
3884 err
= mddev_lock(mddev
);
3889 if (mddev
->pers
->check_reshape
== NULL
)
3894 mddev
->new_layout
= n
;
3895 err
= mddev
->pers
->check_reshape(mddev
);
3897 mddev
->new_layout
= mddev
->layout
;
3900 mddev
->new_layout
= n
;
3901 if (mddev
->reshape_position
== MaxSector
)
3904 mddev_unlock(mddev
);
3907 static struct md_sysfs_entry md_layout
=
3908 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
3911 raid_disks_show(struct mddev
*mddev
, char *page
)
3913 if (mddev
->raid_disks
== 0)
3915 if (mddev
->reshape_position
!= MaxSector
&&
3916 mddev
->delta_disks
!= 0)
3917 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
3918 mddev
->raid_disks
- mddev
->delta_disks
);
3919 return sprintf(page
, "%d\n", mddev
->raid_disks
);
3922 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
);
3925 raid_disks_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3930 err
= kstrtouint(buf
, 10, &n
);
3934 err
= mddev_lock(mddev
);
3938 err
= update_raid_disks(mddev
, n
);
3939 else if (mddev
->reshape_position
!= MaxSector
) {
3940 struct md_rdev
*rdev
;
3941 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
3944 rdev_for_each(rdev
, mddev
) {
3946 rdev
->data_offset
< rdev
->new_data_offset
)
3949 rdev
->data_offset
> rdev
->new_data_offset
)
3953 mddev
->delta_disks
= n
- olddisks
;
3954 mddev
->raid_disks
= n
;
3955 mddev
->reshape_backwards
= (mddev
->delta_disks
< 0);
3957 mddev
->raid_disks
= n
;
3959 mddev_unlock(mddev
);
3960 return err
? err
: len
;
3962 static struct md_sysfs_entry md_raid_disks
=
3963 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
3966 chunk_size_show(struct mddev
*mddev
, char *page
)
3968 if (mddev
->reshape_position
!= MaxSector
&&
3969 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
3970 return sprintf(page
, "%d (%d)\n",
3971 mddev
->new_chunk_sectors
<< 9,
3972 mddev
->chunk_sectors
<< 9);
3973 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
3977 chunk_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3982 err
= kstrtoul(buf
, 10, &n
);
3986 err
= mddev_lock(mddev
);
3990 if (mddev
->pers
->check_reshape
== NULL
)
3995 mddev
->new_chunk_sectors
= n
>> 9;
3996 err
= mddev
->pers
->check_reshape(mddev
);
3998 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4001 mddev
->new_chunk_sectors
= n
>> 9;
4002 if (mddev
->reshape_position
== MaxSector
)
4003 mddev
->chunk_sectors
= n
>> 9;
4005 mddev_unlock(mddev
);
4008 static struct md_sysfs_entry md_chunk_size
=
4009 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
4012 resync_start_show(struct mddev
*mddev
, char *page
)
4014 if (mddev
->recovery_cp
== MaxSector
)
4015 return sprintf(page
, "none\n");
4016 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
4020 resync_start_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4022 unsigned long long n
;
4025 if (cmd_match(buf
, "none"))
4028 err
= kstrtoull(buf
, 10, &n
);
4031 if (n
!= (sector_t
)n
)
4035 err
= mddev_lock(mddev
);
4038 if (mddev
->pers
&& !test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
4042 mddev
->recovery_cp
= n
;
4044 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
4046 mddev_unlock(mddev
);
4049 static struct md_sysfs_entry md_resync_start
=
4050 __ATTR_PREALLOC(resync_start
, S_IRUGO
|S_IWUSR
,
4051 resync_start_show
, resync_start_store
);
4054 * The array state can be:
4057 * No devices, no size, no level
4058 * Equivalent to STOP_ARRAY ioctl
4060 * May have some settings, but array is not active
4061 * all IO results in error
4062 * When written, doesn't tear down array, but just stops it
4063 * suspended (not supported yet)
4064 * All IO requests will block. The array can be reconfigured.
4065 * Writing this, if accepted, will block until array is quiescent
4067 * no resync can happen. no superblocks get written.
4068 * write requests fail
4070 * like readonly, but behaves like 'clean' on a write request.
4072 * clean - no pending writes, but otherwise active.
4073 * When written to inactive array, starts without resync
4074 * If a write request arrives then
4075 * if metadata is known, mark 'dirty' and switch to 'active'.
4076 * if not known, block and switch to write-pending
4077 * If written to an active array that has pending writes, then fails.
4079 * fully active: IO and resync can be happening.
4080 * When written to inactive array, starts with resync
4083 * clean, but writes are blocked waiting for 'active' to be written.
4086 * like active, but no writes have been seen for a while (100msec).
4089 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
4090 write_pending
, active_idle
, bad_word
};
4091 static char *array_states
[] = {
4092 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
4093 "write-pending", "active-idle", NULL
};
4095 static int match_word(const char *word
, char **list
)
4098 for (n
=0; list
[n
]; n
++)
4099 if (cmd_match(word
, list
[n
]))
4105 array_state_show(struct mddev
*mddev
, char *page
)
4107 enum array_state st
= inactive
;
4118 spin_lock(&mddev
->lock
);
4119 if (test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
))
4121 else if (mddev
->in_sync
)
4123 else if (mddev
->safemode
)
4127 spin_unlock(&mddev
->lock
);
4130 if (list_empty(&mddev
->disks
) &&
4131 mddev
->raid_disks
== 0 &&
4132 mddev
->dev_sectors
== 0)
4137 return sprintf(page
, "%s\n", array_states
[st
]);
4140 static int do_md_stop(struct mddev
*mddev
, int ro
, struct block_device
*bdev
);
4141 static int md_set_readonly(struct mddev
*mddev
, struct block_device
*bdev
);
4142 static int do_md_run(struct mddev
*mddev
);
4143 static int restart_array(struct mddev
*mddev
);
4146 array_state_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4149 enum array_state st
= match_word(buf
, array_states
);
4151 if (mddev
->pers
&& (st
== active
|| st
== clean
) && mddev
->ro
!= 1) {
4152 /* don't take reconfig_mutex when toggling between
4155 spin_lock(&mddev
->lock
);
4157 restart_array(mddev
);
4158 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
4159 md_wakeup_thread(mddev
->thread
);
4160 wake_up(&mddev
->sb_wait
);
4161 } else /* st == clean */ {
4162 restart_array(mddev
);
4163 if (!set_in_sync(mddev
))
4167 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4168 spin_unlock(&mddev
->lock
);
4171 err
= mddev_lock(mddev
);
4179 /* stopping an active array */
4180 err
= do_md_stop(mddev
, 0, NULL
);
4183 /* stopping an active array */
4185 err
= do_md_stop(mddev
, 2, NULL
);
4187 err
= 0; /* already inactive */
4190 break; /* not supported yet */
4193 err
= md_set_readonly(mddev
, NULL
);
4196 set_disk_ro(mddev
->gendisk
, 1);
4197 err
= do_md_run(mddev
);
4203 err
= md_set_readonly(mddev
, NULL
);
4204 else if (mddev
->ro
== 1)
4205 err
= restart_array(mddev
);
4208 set_disk_ro(mddev
->gendisk
, 0);
4212 err
= do_md_run(mddev
);
4217 err
= restart_array(mddev
);
4220 spin_lock(&mddev
->lock
);
4221 if (!set_in_sync(mddev
))
4223 spin_unlock(&mddev
->lock
);
4229 err
= restart_array(mddev
);
4232 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
4233 wake_up(&mddev
->sb_wait
);
4237 set_disk_ro(mddev
->gendisk
, 0);
4238 err
= do_md_run(mddev
);
4243 /* these cannot be set */
4248 if (mddev
->hold_active
== UNTIL_IOCTL
)
4249 mddev
->hold_active
= 0;
4250 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4252 mddev_unlock(mddev
);
4255 static struct md_sysfs_entry md_array_state
=
4256 __ATTR_PREALLOC(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
4259 max_corrected_read_errors_show(struct mddev
*mddev
, char *page
) {
4260 return sprintf(page
, "%d\n",
4261 atomic_read(&mddev
->max_corr_read_errors
));
4265 max_corrected_read_errors_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4270 rv
= kstrtouint(buf
, 10, &n
);
4273 atomic_set(&mddev
->max_corr_read_errors
, n
);
4277 static struct md_sysfs_entry max_corr_read_errors
=
4278 __ATTR(max_read_errors
, S_IRUGO
|S_IWUSR
, max_corrected_read_errors_show
,
4279 max_corrected_read_errors_store
);
4282 null_show(struct mddev
*mddev
, char *page
)
4288 new_dev_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4290 /* buf must be %d:%d\n? giving major and minor numbers */
4291 /* The new device is added to the array.
4292 * If the array has a persistent superblock, we read the
4293 * superblock to initialise info and check validity.
4294 * Otherwise, only checking done is that in bind_rdev_to_array,
4295 * which mainly checks size.
4298 int major
= simple_strtoul(buf
, &e
, 10);
4301 struct md_rdev
*rdev
;
4304 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
4306 minor
= simple_strtoul(e
+1, &e
, 10);
4307 if (*e
&& *e
!= '\n')
4309 dev
= MKDEV(major
, minor
);
4310 if (major
!= MAJOR(dev
) ||
4311 minor
!= MINOR(dev
))
4314 flush_workqueue(md_misc_wq
);
4316 err
= mddev_lock(mddev
);
4319 if (mddev
->persistent
) {
4320 rdev
= md_import_device(dev
, mddev
->major_version
,
4321 mddev
->minor_version
);
4322 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
4323 struct md_rdev
*rdev0
4324 = list_entry(mddev
->disks
.next
,
4325 struct md_rdev
, same_set
);
4326 err
= super_types
[mddev
->major_version
]
4327 .load_super(rdev
, rdev0
, mddev
->minor_version
);
4331 } else if (mddev
->external
)
4332 rdev
= md_import_device(dev
, -2, -1);
4334 rdev
= md_import_device(dev
, -1, -1);
4337 mddev_unlock(mddev
);
4338 return PTR_ERR(rdev
);
4340 err
= bind_rdev_to_array(rdev
, mddev
);
4344 mddev_unlock(mddev
);
4346 md_new_event(mddev
);
4347 return err
? err
: len
;
4350 static struct md_sysfs_entry md_new_device
=
4351 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
4354 bitmap_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4357 unsigned long chunk
, end_chunk
;
4360 err
= mddev_lock(mddev
);
4365 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4367 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
4368 if (buf
== end
) break;
4369 if (*end
== '-') { /* range */
4371 end_chunk
= simple_strtoul(buf
, &end
, 0);
4372 if (buf
== end
) break;
4374 if (*end
&& !isspace(*end
)) break;
4375 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
4376 buf
= skip_spaces(end
);
4378 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
4380 mddev_unlock(mddev
);
4384 static struct md_sysfs_entry md_bitmap
=
4385 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
4388 size_show(struct mddev
*mddev
, char *page
)
4390 return sprintf(page
, "%llu\n",
4391 (unsigned long long)mddev
->dev_sectors
/ 2);
4394 static int update_size(struct mddev
*mddev
, sector_t num_sectors
);
4397 size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4399 /* If array is inactive, we can reduce the component size, but
4400 * not increase it (except from 0).
4401 * If array is active, we can try an on-line resize
4404 int err
= strict_blocks_to_sectors(buf
, §ors
);
4408 err
= mddev_lock(mddev
);
4412 err
= update_size(mddev
, sectors
);
4414 md_update_sb(mddev
, 1);
4416 if (mddev
->dev_sectors
== 0 ||
4417 mddev
->dev_sectors
> sectors
)
4418 mddev
->dev_sectors
= sectors
;
4422 mddev_unlock(mddev
);
4423 return err
? err
: len
;
4426 static struct md_sysfs_entry md_size
=
4427 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
4429 /* Metadata version.
4431 * 'none' for arrays with no metadata (good luck...)
4432 * 'external' for arrays with externally managed metadata,
4433 * or N.M for internally known formats
4436 metadata_show(struct mddev
*mddev
, char *page
)
4438 if (mddev
->persistent
)
4439 return sprintf(page
, "%d.%d\n",
4440 mddev
->major_version
, mddev
->minor_version
);
4441 else if (mddev
->external
)
4442 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
4444 return sprintf(page
, "none\n");
4448 metadata_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4453 /* Changing the details of 'external' metadata is
4454 * always permitted. Otherwise there must be
4455 * no devices attached to the array.
4458 err
= mddev_lock(mddev
);
4462 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
4464 else if (!list_empty(&mddev
->disks
))
4468 if (cmd_match(buf
, "none")) {
4469 mddev
->persistent
= 0;
4470 mddev
->external
= 0;
4471 mddev
->major_version
= 0;
4472 mddev
->minor_version
= 90;
4475 if (strncmp(buf
, "external:", 9) == 0) {
4476 size_t namelen
= len
-9;
4477 if (namelen
>= sizeof(mddev
->metadata_type
))
4478 namelen
= sizeof(mddev
->metadata_type
)-1;
4479 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
4480 mddev
->metadata_type
[namelen
] = 0;
4481 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
4482 mddev
->metadata_type
[--namelen
] = 0;
4483 mddev
->persistent
= 0;
4484 mddev
->external
= 1;
4485 mddev
->major_version
= 0;
4486 mddev
->minor_version
= 90;
4489 major
= simple_strtoul(buf
, &e
, 10);
4491 if (e
==buf
|| *e
!= '.')
4494 minor
= simple_strtoul(buf
, &e
, 10);
4495 if (e
==buf
|| (*e
&& *e
!= '\n') )
4498 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
4500 mddev
->major_version
= major
;
4501 mddev
->minor_version
= minor
;
4502 mddev
->persistent
= 1;
4503 mddev
->external
= 0;
4506 mddev_unlock(mddev
);
4510 static struct md_sysfs_entry md_metadata
=
4511 __ATTR_PREALLOC(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
4514 action_show(struct mddev
*mddev
, char *page
)
4516 char *type
= "idle";
4517 unsigned long recovery
= mddev
->recovery
;
4518 if (test_bit(MD_RECOVERY_FROZEN
, &recovery
))
4520 else if (test_bit(MD_RECOVERY_RUNNING
, &recovery
) ||
4521 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &recovery
))) {
4522 if (test_bit(MD_RECOVERY_RESHAPE
, &recovery
))
4524 else if (test_bit(MD_RECOVERY_SYNC
, &recovery
)) {
4525 if (!test_bit(MD_RECOVERY_REQUESTED
, &recovery
))
4527 else if (test_bit(MD_RECOVERY_CHECK
, &recovery
))
4531 } else if (test_bit(MD_RECOVERY_RECOVER
, &recovery
))
4533 else if (mddev
->reshape_position
!= MaxSector
)
4536 return sprintf(page
, "%s\n", type
);
4540 action_store(struct mddev
*mddev
, const char *page
, size_t len
)
4542 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
4546 if (cmd_match(page
, "idle") || cmd_match(page
, "frozen")) {
4547 if (cmd_match(page
, "frozen"))
4548 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4550 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4551 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
4552 mddev_lock(mddev
) == 0) {
4553 flush_workqueue(md_misc_wq
);
4554 if (mddev
->sync_thread
) {
4555 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4556 md_reap_sync_thread(mddev
);
4558 mddev_unlock(mddev
);
4560 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4562 else if (cmd_match(page
, "resync"))
4563 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4564 else if (cmd_match(page
, "recover")) {
4565 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4566 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
4567 } else if (cmd_match(page
, "reshape")) {
4569 if (mddev
->pers
->start_reshape
== NULL
)
4571 err
= mddev_lock(mddev
);
4573 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4576 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4577 err
= mddev
->pers
->start_reshape(mddev
);
4579 mddev_unlock(mddev
);
4583 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4585 if (cmd_match(page
, "check"))
4586 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
4587 else if (!cmd_match(page
, "repair"))
4589 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4590 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
4591 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
4593 if (mddev
->ro
== 2) {
4594 /* A write to sync_action is enough to justify
4595 * canceling read-auto mode
4598 md_wakeup_thread(mddev
->sync_thread
);
4600 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4601 md_wakeup_thread(mddev
->thread
);
4602 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4606 static struct md_sysfs_entry md_scan_mode
=
4607 __ATTR_PREALLOC(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
4610 last_sync_action_show(struct mddev
*mddev
, char *page
)
4612 return sprintf(page
, "%s\n", mddev
->last_sync_action
);
4615 static struct md_sysfs_entry md_last_scan_mode
= __ATTR_RO(last_sync_action
);
4618 mismatch_cnt_show(struct mddev
*mddev
, char *page
)
4620 return sprintf(page
, "%llu\n",
4621 (unsigned long long)
4622 atomic64_read(&mddev
->resync_mismatches
));
4625 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
4628 sync_min_show(struct mddev
*mddev
, char *page
)
4630 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
4631 mddev
->sync_speed_min
? "local": "system");
4635 sync_min_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4640 if (strncmp(buf
, "system", 6)==0) {
4643 rv
= kstrtouint(buf
, 10, &min
);
4649 mddev
->sync_speed_min
= min
;
4653 static struct md_sysfs_entry md_sync_min
=
4654 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
4657 sync_max_show(struct mddev
*mddev
, char *page
)
4659 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
4660 mddev
->sync_speed_max
? "local": "system");
4664 sync_max_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4669 if (strncmp(buf
, "system", 6)==0) {
4672 rv
= kstrtouint(buf
, 10, &max
);
4678 mddev
->sync_speed_max
= max
;
4682 static struct md_sysfs_entry md_sync_max
=
4683 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
4686 degraded_show(struct mddev
*mddev
, char *page
)
4688 return sprintf(page
, "%d\n", mddev
->degraded
);
4690 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
4693 sync_force_parallel_show(struct mddev
*mddev
, char *page
)
4695 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
4699 sync_force_parallel_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4703 if (kstrtol(buf
, 10, &n
))
4706 if (n
!= 0 && n
!= 1)
4709 mddev
->parallel_resync
= n
;
4711 if (mddev
->sync_thread
)
4712 wake_up(&resync_wait
);
4717 /* force parallel resync, even with shared block devices */
4718 static struct md_sysfs_entry md_sync_force_parallel
=
4719 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
4720 sync_force_parallel_show
, sync_force_parallel_store
);
4723 sync_speed_show(struct mddev
*mddev
, char *page
)
4725 unsigned long resync
, dt
, db
;
4726 if (mddev
->curr_resync
== 0)
4727 return sprintf(page
, "none\n");
4728 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
4729 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
4731 db
= resync
- mddev
->resync_mark_cnt
;
4732 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
4735 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
4738 sync_completed_show(struct mddev
*mddev
, char *page
)
4740 unsigned long long max_sectors
, resync
;
4742 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4743 return sprintf(page
, "none\n");
4745 if (mddev
->curr_resync
== 1 ||
4746 mddev
->curr_resync
== 2)
4747 return sprintf(page
, "delayed\n");
4749 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
4750 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
4751 max_sectors
= mddev
->resync_max_sectors
;
4753 max_sectors
= mddev
->dev_sectors
;
4755 resync
= mddev
->curr_resync_completed
;
4756 return sprintf(page
, "%llu / %llu\n", resync
, max_sectors
);
4759 static struct md_sysfs_entry md_sync_completed
=
4760 __ATTR_PREALLOC(sync_completed
, S_IRUGO
, sync_completed_show
, NULL
);
4763 min_sync_show(struct mddev
*mddev
, char *page
)
4765 return sprintf(page
, "%llu\n",
4766 (unsigned long long)mddev
->resync_min
);
4769 min_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4771 unsigned long long min
;
4774 if (kstrtoull(buf
, 10, &min
))
4777 spin_lock(&mddev
->lock
);
4779 if (min
> mddev
->resync_max
)
4783 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4786 /* Round down to multiple of 4K for safety */
4787 mddev
->resync_min
= round_down(min
, 8);
4791 spin_unlock(&mddev
->lock
);
4795 static struct md_sysfs_entry md_min_sync
=
4796 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
4799 max_sync_show(struct mddev
*mddev
, char *page
)
4801 if (mddev
->resync_max
== MaxSector
)
4802 return sprintf(page
, "max\n");
4804 return sprintf(page
, "%llu\n",
4805 (unsigned long long)mddev
->resync_max
);
4808 max_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4811 spin_lock(&mddev
->lock
);
4812 if (strncmp(buf
, "max", 3) == 0)
4813 mddev
->resync_max
= MaxSector
;
4815 unsigned long long max
;
4819 if (kstrtoull(buf
, 10, &max
))
4821 if (max
< mddev
->resync_min
)
4825 if (max
< mddev
->resync_max
&&
4827 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4830 /* Must be a multiple of chunk_size */
4831 chunk
= mddev
->chunk_sectors
;
4833 sector_t temp
= max
;
4836 if (sector_div(temp
, chunk
))
4839 mddev
->resync_max
= max
;
4841 wake_up(&mddev
->recovery_wait
);
4844 spin_unlock(&mddev
->lock
);
4848 static struct md_sysfs_entry md_max_sync
=
4849 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
4852 suspend_lo_show(struct mddev
*mddev
, char *page
)
4854 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
4858 suspend_lo_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4860 unsigned long long new;
4863 err
= kstrtoull(buf
, 10, &new);
4866 if (new != (sector_t
)new)
4869 err
= mddev_lock(mddev
);
4873 if (mddev
->pers
== NULL
||
4874 mddev
->pers
->quiesce
== NULL
)
4876 mddev_suspend(mddev
);
4877 mddev
->suspend_lo
= new;
4878 mddev_resume(mddev
);
4882 mddev_unlock(mddev
);
4885 static struct md_sysfs_entry md_suspend_lo
=
4886 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
4889 suspend_hi_show(struct mddev
*mddev
, char *page
)
4891 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
4895 suspend_hi_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4897 unsigned long long new;
4900 err
= kstrtoull(buf
, 10, &new);
4903 if (new != (sector_t
)new)
4906 err
= mddev_lock(mddev
);
4910 if (mddev
->pers
== NULL
)
4913 mddev_suspend(mddev
);
4914 mddev
->suspend_hi
= new;
4915 mddev_resume(mddev
);
4919 mddev_unlock(mddev
);
4922 static struct md_sysfs_entry md_suspend_hi
=
4923 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
4926 reshape_position_show(struct mddev
*mddev
, char *page
)
4928 if (mddev
->reshape_position
!= MaxSector
)
4929 return sprintf(page
, "%llu\n",
4930 (unsigned long long)mddev
->reshape_position
);
4931 strcpy(page
, "none\n");
4936 reshape_position_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4938 struct md_rdev
*rdev
;
4939 unsigned long long new;
4942 err
= kstrtoull(buf
, 10, &new);
4945 if (new != (sector_t
)new)
4947 err
= mddev_lock(mddev
);
4953 mddev
->reshape_position
= new;
4954 mddev
->delta_disks
= 0;
4955 mddev
->reshape_backwards
= 0;
4956 mddev
->new_level
= mddev
->level
;
4957 mddev
->new_layout
= mddev
->layout
;
4958 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4959 rdev_for_each(rdev
, mddev
)
4960 rdev
->new_data_offset
= rdev
->data_offset
;
4963 mddev_unlock(mddev
);
4967 static struct md_sysfs_entry md_reshape_position
=
4968 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
4969 reshape_position_store
);
4972 reshape_direction_show(struct mddev
*mddev
, char *page
)
4974 return sprintf(page
, "%s\n",
4975 mddev
->reshape_backwards
? "backwards" : "forwards");
4979 reshape_direction_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4984 if (cmd_match(buf
, "forwards"))
4986 else if (cmd_match(buf
, "backwards"))
4990 if (mddev
->reshape_backwards
== backwards
)
4993 err
= mddev_lock(mddev
);
4996 /* check if we are allowed to change */
4997 if (mddev
->delta_disks
)
4999 else if (mddev
->persistent
&&
5000 mddev
->major_version
== 0)
5003 mddev
->reshape_backwards
= backwards
;
5004 mddev_unlock(mddev
);
5008 static struct md_sysfs_entry md_reshape_direction
=
5009 __ATTR(reshape_direction
, S_IRUGO
|S_IWUSR
, reshape_direction_show
,
5010 reshape_direction_store
);
5013 array_size_show(struct mddev
*mddev
, char *page
)
5015 if (mddev
->external_size
)
5016 return sprintf(page
, "%llu\n",
5017 (unsigned long long)mddev
->array_sectors
/2);
5019 return sprintf(page
, "default\n");
5023 array_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5028 err
= mddev_lock(mddev
);
5032 /* cluster raid doesn't support change array_sectors */
5033 if (mddev_is_clustered(mddev
)) {
5034 mddev_unlock(mddev
);
5038 if (strncmp(buf
, "default", 7) == 0) {
5040 sectors
= mddev
->pers
->size(mddev
, 0, 0);
5042 sectors
= mddev
->array_sectors
;
5044 mddev
->external_size
= 0;
5046 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
5048 else if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
5051 mddev
->external_size
= 1;
5055 mddev
->array_sectors
= sectors
;
5057 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
5058 revalidate_disk(mddev
->gendisk
);
5061 mddev_unlock(mddev
);
5065 static struct md_sysfs_entry md_array_size
=
5066 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
5070 consistency_policy_show(struct mddev
*mddev
, char *page
)
5074 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
)) {
5075 ret
= sprintf(page
, "journal\n");
5076 } else if (test_bit(MD_HAS_PPL
, &mddev
->flags
)) {
5077 ret
= sprintf(page
, "ppl\n");
5078 } else if (mddev
->bitmap
) {
5079 ret
= sprintf(page
, "bitmap\n");
5080 } else if (mddev
->pers
) {
5081 if (mddev
->pers
->sync_request
)
5082 ret
= sprintf(page
, "resync\n");
5084 ret
= sprintf(page
, "none\n");
5086 ret
= sprintf(page
, "unknown\n");
5093 consistency_policy_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5098 if (mddev
->pers
->change_consistency_policy
)
5099 err
= mddev
->pers
->change_consistency_policy(mddev
, buf
);
5102 } else if (mddev
->external
&& strncmp(buf
, "ppl", 3) == 0) {
5103 set_bit(MD_HAS_PPL
, &mddev
->flags
);
5108 return err
? err
: len
;
5111 static struct md_sysfs_entry md_consistency_policy
=
5112 __ATTR(consistency_policy
, S_IRUGO
| S_IWUSR
, consistency_policy_show
,
5113 consistency_policy_store
);
5115 static struct attribute
*md_default_attrs
[] = {
5118 &md_raid_disks
.attr
,
5119 &md_chunk_size
.attr
,
5121 &md_resync_start
.attr
,
5123 &md_new_device
.attr
,
5124 &md_safe_delay
.attr
,
5125 &md_array_state
.attr
,
5126 &md_reshape_position
.attr
,
5127 &md_reshape_direction
.attr
,
5128 &md_array_size
.attr
,
5129 &max_corr_read_errors
.attr
,
5130 &md_consistency_policy
.attr
,
5134 static struct attribute
*md_redundancy_attrs
[] = {
5136 &md_last_scan_mode
.attr
,
5137 &md_mismatches
.attr
,
5140 &md_sync_speed
.attr
,
5141 &md_sync_force_parallel
.attr
,
5142 &md_sync_completed
.attr
,
5145 &md_suspend_lo
.attr
,
5146 &md_suspend_hi
.attr
,
5151 static struct attribute_group md_redundancy_group
= {
5153 .attrs
= md_redundancy_attrs
,
5157 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
5159 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
5160 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
5165 spin_lock(&all_mddevs_lock
);
5166 if (list_empty(&mddev
->all_mddevs
)) {
5167 spin_unlock(&all_mddevs_lock
);
5171 spin_unlock(&all_mddevs_lock
);
5173 rv
= entry
->show(mddev
, page
);
5179 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
5180 const char *page
, size_t length
)
5182 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
5183 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
5188 if (!capable(CAP_SYS_ADMIN
))
5190 spin_lock(&all_mddevs_lock
);
5191 if (list_empty(&mddev
->all_mddevs
)) {
5192 spin_unlock(&all_mddevs_lock
);
5196 spin_unlock(&all_mddevs_lock
);
5197 rv
= entry
->store(mddev
, page
, length
);
5202 static void md_free(struct kobject
*ko
)
5204 struct mddev
*mddev
= container_of(ko
, struct mddev
, kobj
);
5206 if (mddev
->sysfs_state
)
5207 sysfs_put(mddev
->sysfs_state
);
5210 blk_cleanup_queue(mddev
->queue
);
5211 if (mddev
->gendisk
) {
5212 del_gendisk(mddev
->gendisk
);
5213 put_disk(mddev
->gendisk
);
5215 percpu_ref_exit(&mddev
->writes_pending
);
5220 static const struct sysfs_ops md_sysfs_ops
= {
5221 .show
= md_attr_show
,
5222 .store
= md_attr_store
,
5224 static struct kobj_type md_ktype
= {
5226 .sysfs_ops
= &md_sysfs_ops
,
5227 .default_attrs
= md_default_attrs
,
5232 static void mddev_delayed_delete(struct work_struct
*ws
)
5234 struct mddev
*mddev
= container_of(ws
, struct mddev
, del_work
);
5236 sysfs_remove_group(&mddev
->kobj
, &md_bitmap_group
);
5237 kobject_del(&mddev
->kobj
);
5238 kobject_put(&mddev
->kobj
);
5241 static void no_op(struct percpu_ref
*r
) {}
5243 int mddev_init_writes_pending(struct mddev
*mddev
)
5245 if (mddev
->writes_pending
.percpu_count_ptr
)
5247 if (percpu_ref_init(&mddev
->writes_pending
, no_op
, 0, GFP_KERNEL
) < 0)
5249 /* We want to start with the refcount at zero */
5250 percpu_ref_put(&mddev
->writes_pending
);
5253 EXPORT_SYMBOL_GPL(mddev_init_writes_pending
);
5255 static int md_alloc(dev_t dev
, char *name
)
5258 * If dev is zero, name is the name of a device to allocate with
5259 * an arbitrary minor number. It will be "md_???"
5260 * If dev is non-zero it must be a device number with a MAJOR of
5261 * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then
5262 * the device is being created by opening a node in /dev.
5263 * If "name" is not NULL, the device is being created by
5264 * writing to /sys/module/md_mod/parameters/new_array.
5266 static DEFINE_MUTEX(disks_mutex
);
5267 struct mddev
*mddev
= mddev_find(dev
);
5268 struct gendisk
*disk
;
5277 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
5278 shift
= partitioned
? MdpMinorShift
: 0;
5279 unit
= MINOR(mddev
->unit
) >> shift
;
5281 /* wait for any previous instance of this device to be
5282 * completely removed (mddev_delayed_delete).
5284 flush_workqueue(md_misc_wq
);
5286 mutex_lock(&disks_mutex
);
5292 /* Need to ensure that 'name' is not a duplicate.
5294 struct mddev
*mddev2
;
5295 spin_lock(&all_mddevs_lock
);
5297 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
5298 if (mddev2
->gendisk
&&
5299 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
5300 spin_unlock(&all_mddevs_lock
);
5303 spin_unlock(&all_mddevs_lock
);
5307 * Creating /dev/mdNNN via "newarray", so adjust hold_active.
5309 mddev
->hold_active
= UNTIL_STOP
;
5312 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
5315 mddev
->queue
->queuedata
= mddev
;
5317 blk_queue_make_request(mddev
->queue
, md_make_request
);
5318 blk_set_stacking_limits(&mddev
->queue
->limits
);
5320 disk
= alloc_disk(1 << shift
);
5322 blk_cleanup_queue(mddev
->queue
);
5323 mddev
->queue
= NULL
;
5326 disk
->major
= MAJOR(mddev
->unit
);
5327 disk
->first_minor
= unit
<< shift
;
5329 strcpy(disk
->disk_name
, name
);
5330 else if (partitioned
)
5331 sprintf(disk
->disk_name
, "md_d%d", unit
);
5333 sprintf(disk
->disk_name
, "md%d", unit
);
5334 disk
->fops
= &md_fops
;
5335 disk
->private_data
= mddev
;
5336 disk
->queue
= mddev
->queue
;
5337 blk_queue_write_cache(mddev
->queue
, true, true);
5338 /* Allow extended partitions. This makes the
5339 * 'mdp' device redundant, but we can't really
5342 disk
->flags
|= GENHD_FL_EXT_DEVT
;
5343 mddev
->gendisk
= disk
;
5344 /* As soon as we call add_disk(), another thread could get
5345 * through to md_open, so make sure it doesn't get too far
5347 mutex_lock(&mddev
->open_mutex
);
5350 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
,
5351 &disk_to_dev(disk
)->kobj
, "%s", "md");
5353 /* This isn't possible, but as kobject_init_and_add is marked
5354 * __must_check, we must do something with the result
5356 pr_debug("md: cannot register %s/md - name in use\n",
5360 if (mddev
->kobj
.sd
&&
5361 sysfs_create_group(&mddev
->kobj
, &md_bitmap_group
))
5362 pr_debug("pointless warning\n");
5363 mutex_unlock(&mddev
->open_mutex
);
5365 mutex_unlock(&disks_mutex
);
5366 if (!error
&& mddev
->kobj
.sd
) {
5367 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
5368 mddev
->sysfs_state
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "array_state");
5374 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
5377 md_alloc(dev
, NULL
);
5381 static int add_named_array(const char *val
, const struct kernel_param
*kp
)
5384 * val must be "md_*" or "mdNNN".
5385 * For "md_*" we allocate an array with a large free minor number, and
5386 * set the name to val. val must not already be an active name.
5387 * For "mdNNN" we allocate an array with the minor number NNN
5388 * which must not already be in use.
5390 int len
= strlen(val
);
5391 char buf
[DISK_NAME_LEN
];
5392 unsigned long devnum
;
5394 while (len
&& val
[len
-1] == '\n')
5396 if (len
>= DISK_NAME_LEN
)
5398 strlcpy(buf
, val
, len
+1);
5399 if (strncmp(buf
, "md_", 3) == 0)
5400 return md_alloc(0, buf
);
5401 if (strncmp(buf
, "md", 2) == 0 &&
5403 kstrtoul(buf
+2, 10, &devnum
) == 0 &&
5404 devnum
<= MINORMASK
)
5405 return md_alloc(MKDEV(MD_MAJOR
, devnum
), NULL
);
5410 static void md_safemode_timeout(struct timer_list
*t
)
5412 struct mddev
*mddev
= from_timer(mddev
, t
, safemode_timer
);
5414 mddev
->safemode
= 1;
5415 if (mddev
->external
)
5416 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5418 md_wakeup_thread(mddev
->thread
);
5421 static int start_dirty_degraded
;
5423 int md_run(struct mddev
*mddev
)
5426 struct md_rdev
*rdev
;
5427 struct md_personality
*pers
;
5429 if (list_empty(&mddev
->disks
))
5430 /* cannot run an array with no devices.. */
5435 /* Cannot run until previous stop completes properly */
5436 if (mddev
->sysfs_active
)
5440 * Analyze all RAID superblock(s)
5442 if (!mddev
->raid_disks
) {
5443 if (!mddev
->persistent
)
5448 if (mddev
->level
!= LEVEL_NONE
)
5449 request_module("md-level-%d", mddev
->level
);
5450 else if (mddev
->clevel
[0])
5451 request_module("md-%s", mddev
->clevel
);
5454 * Drop all container device buffers, from now on
5455 * the only valid external interface is through the md
5458 mddev
->has_superblocks
= false;
5459 rdev_for_each(rdev
, mddev
) {
5460 if (test_bit(Faulty
, &rdev
->flags
))
5462 sync_blockdev(rdev
->bdev
);
5463 invalidate_bdev(rdev
->bdev
);
5464 if (mddev
->ro
!= 1 &&
5465 (bdev_read_only(rdev
->bdev
) ||
5466 bdev_read_only(rdev
->meta_bdev
))) {
5469 set_disk_ro(mddev
->gendisk
, 1);
5473 mddev
->has_superblocks
= true;
5475 /* perform some consistency tests on the device.
5476 * We don't want the data to overlap the metadata,
5477 * Internal Bitmap issues have been handled elsewhere.
5479 if (rdev
->meta_bdev
) {
5480 /* Nothing to check */;
5481 } else if (rdev
->data_offset
< rdev
->sb_start
) {
5482 if (mddev
->dev_sectors
&&
5483 rdev
->data_offset
+ mddev
->dev_sectors
5485 pr_warn("md: %s: data overlaps metadata\n",
5490 if (rdev
->sb_start
+ rdev
->sb_size
/512
5491 > rdev
->data_offset
) {
5492 pr_warn("md: %s: metadata overlaps data\n",
5497 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
5500 if (mddev
->bio_set
== NULL
) {
5501 mddev
->bio_set
= bioset_create(BIO_POOL_SIZE
, 0, BIOSET_NEED_BVECS
);
5502 if (!mddev
->bio_set
)
5505 if (mddev
->sync_set
== NULL
) {
5506 mddev
->sync_set
= bioset_create(BIO_POOL_SIZE
, 0, BIOSET_NEED_BVECS
);
5507 if (!mddev
->sync_set
) {
5513 spin_lock(&pers_lock
);
5514 pers
= find_pers(mddev
->level
, mddev
->clevel
);
5515 if (!pers
|| !try_module_get(pers
->owner
)) {
5516 spin_unlock(&pers_lock
);
5517 if (mddev
->level
!= LEVEL_NONE
)
5518 pr_warn("md: personality for level %d is not loaded!\n",
5521 pr_warn("md: personality for level %s is not loaded!\n",
5526 spin_unlock(&pers_lock
);
5527 if (mddev
->level
!= pers
->level
) {
5528 mddev
->level
= pers
->level
;
5529 mddev
->new_level
= pers
->level
;
5531 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
5533 if (mddev
->reshape_position
!= MaxSector
&&
5534 pers
->start_reshape
== NULL
) {
5535 /* This personality cannot handle reshaping... */
5536 module_put(pers
->owner
);
5541 if (pers
->sync_request
) {
5542 /* Warn if this is a potentially silly
5545 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
5546 struct md_rdev
*rdev2
;
5549 rdev_for_each(rdev
, mddev
)
5550 rdev_for_each(rdev2
, mddev
) {
5552 rdev
->bdev
->bd_contains
==
5553 rdev2
->bdev
->bd_contains
) {
5554 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5556 bdevname(rdev
->bdev
,b
),
5557 bdevname(rdev2
->bdev
,b2
));
5563 pr_warn("True protection against single-disk failure might be compromised.\n");
5566 mddev
->recovery
= 0;
5567 /* may be over-ridden by personality */
5568 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
5570 mddev
->ok_start_degraded
= start_dirty_degraded
;
5572 if (start_readonly
&& mddev
->ro
== 0)
5573 mddev
->ro
= 2; /* read-only, but switch on first write */
5575 err
= pers
->run(mddev
);
5577 pr_warn("md: pers->run() failed ...\n");
5578 else if (pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
5579 WARN_ONCE(!mddev
->external_size
,
5580 "%s: default size too small, but 'external_size' not in effect?\n",
5582 pr_warn("md: invalid array_size %llu > default size %llu\n",
5583 (unsigned long long)mddev
->array_sectors
/ 2,
5584 (unsigned long long)pers
->size(mddev
, 0, 0) / 2);
5587 if (err
== 0 && pers
->sync_request
&&
5588 (mddev
->bitmap_info
.file
|| mddev
->bitmap_info
.offset
)) {
5589 struct bitmap
*bitmap
;
5591 bitmap
= bitmap_create(mddev
, -1);
5592 if (IS_ERR(bitmap
)) {
5593 err
= PTR_ERR(bitmap
);
5594 pr_warn("%s: failed to create bitmap (%d)\n",
5595 mdname(mddev
), err
);
5597 mddev
->bitmap
= bitmap
;
5601 mddev_detach(mddev
);
5603 pers
->free(mddev
, mddev
->private);
5604 mddev
->private = NULL
;
5605 module_put(pers
->owner
);
5606 bitmap_destroy(mddev
);
5612 rdev_for_each(rdev
, mddev
) {
5613 if (rdev
->raid_disk
>= 0 &&
5614 !blk_queue_nonrot(bdev_get_queue(rdev
->bdev
))) {
5619 if (mddev
->degraded
)
5622 queue_flag_set_unlocked(QUEUE_FLAG_NONROT
, mddev
->queue
);
5624 queue_flag_clear_unlocked(QUEUE_FLAG_NONROT
, mddev
->queue
);
5625 mddev
->queue
->backing_dev_info
->congested_data
= mddev
;
5626 mddev
->queue
->backing_dev_info
->congested_fn
= md_congested
;
5628 if (pers
->sync_request
) {
5629 if (mddev
->kobj
.sd
&&
5630 sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
5631 pr_warn("md: cannot register extra attributes for %s\n",
5633 mddev
->sysfs_action
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "sync_action");
5634 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
5637 atomic_set(&mddev
->max_corr_read_errors
,
5638 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS
);
5639 mddev
->safemode
= 0;
5640 if (mddev_is_clustered(mddev
))
5641 mddev
->safemode_delay
= 0;
5643 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
5646 spin_lock(&mddev
->lock
);
5648 spin_unlock(&mddev
->lock
);
5649 rdev_for_each(rdev
, mddev
)
5650 if (rdev
->raid_disk
>= 0)
5651 if (sysfs_link_rdev(mddev
, rdev
))
5652 /* failure here is OK */;
5654 if (mddev
->degraded
&& !mddev
->ro
)
5655 /* This ensures that recovering status is reported immediately
5656 * via sysfs - until a lack of spares is confirmed.
5658 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5659 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5661 if (mddev
->sb_flags
)
5662 md_update_sb(mddev
, 0);
5664 md_new_event(mddev
);
5665 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5666 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
5667 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
5671 if (mddev
->bio_set
) {
5672 bioset_free(mddev
->bio_set
);
5673 mddev
->bio_set
= NULL
;
5675 if (mddev
->sync_set
) {
5676 bioset_free(mddev
->sync_set
);
5677 mddev
->sync_set
= NULL
;
5682 EXPORT_SYMBOL_GPL(md_run
);
5684 static int do_md_run(struct mddev
*mddev
)
5688 err
= md_run(mddev
);
5691 err
= bitmap_load(mddev
);
5693 bitmap_destroy(mddev
);
5697 if (mddev_is_clustered(mddev
))
5698 md_allow_write(mddev
);
5700 /* run start up tasks that require md_thread */
5703 md_wakeup_thread(mddev
->thread
);
5704 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
5706 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
5707 revalidate_disk(mddev
->gendisk
);
5709 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
5714 int md_start(struct mddev
*mddev
)
5718 if (mddev
->pers
->start
) {
5719 set_bit(MD_RECOVERY_WAIT
, &mddev
->recovery
);
5720 md_wakeup_thread(mddev
->thread
);
5721 ret
= mddev
->pers
->start(mddev
);
5722 clear_bit(MD_RECOVERY_WAIT
, &mddev
->recovery
);
5723 md_wakeup_thread(mddev
->sync_thread
);
5727 EXPORT_SYMBOL_GPL(md_start
);
5729 static int restart_array(struct mddev
*mddev
)
5731 struct gendisk
*disk
= mddev
->gendisk
;
5732 struct md_rdev
*rdev
;
5733 bool has_journal
= false;
5734 bool has_readonly
= false;
5736 /* Complain if it has no devices */
5737 if (list_empty(&mddev
->disks
))
5745 rdev_for_each_rcu(rdev
, mddev
) {
5746 if (test_bit(Journal
, &rdev
->flags
) &&
5747 !test_bit(Faulty
, &rdev
->flags
))
5749 if (bdev_read_only(rdev
->bdev
))
5750 has_readonly
= true;
5753 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
) && !has_journal
)
5754 /* Don't restart rw with journal missing/faulty */
5759 mddev
->safemode
= 0;
5761 set_disk_ro(disk
, 0);
5762 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev
));
5763 /* Kick recovery or resync if necessary */
5764 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5765 md_wakeup_thread(mddev
->thread
);
5766 md_wakeup_thread(mddev
->sync_thread
);
5767 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5771 static void md_clean(struct mddev
*mddev
)
5773 mddev
->array_sectors
= 0;
5774 mddev
->external_size
= 0;
5775 mddev
->dev_sectors
= 0;
5776 mddev
->raid_disks
= 0;
5777 mddev
->recovery_cp
= 0;
5778 mddev
->resync_min
= 0;
5779 mddev
->resync_max
= MaxSector
;
5780 mddev
->reshape_position
= MaxSector
;
5781 mddev
->external
= 0;
5782 mddev
->persistent
= 0;
5783 mddev
->level
= LEVEL_NONE
;
5784 mddev
->clevel
[0] = 0;
5786 mddev
->sb_flags
= 0;
5788 mddev
->metadata_type
[0] = 0;
5789 mddev
->chunk_sectors
= 0;
5790 mddev
->ctime
= mddev
->utime
= 0;
5792 mddev
->max_disks
= 0;
5794 mddev
->can_decrease_events
= 0;
5795 mddev
->delta_disks
= 0;
5796 mddev
->reshape_backwards
= 0;
5797 mddev
->new_level
= LEVEL_NONE
;
5798 mddev
->new_layout
= 0;
5799 mddev
->new_chunk_sectors
= 0;
5800 mddev
->curr_resync
= 0;
5801 atomic64_set(&mddev
->resync_mismatches
, 0);
5802 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
5803 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
5804 mddev
->recovery
= 0;
5807 mddev
->degraded
= 0;
5808 mddev
->safemode
= 0;
5809 mddev
->private = NULL
;
5810 mddev
->cluster_info
= NULL
;
5811 mddev
->bitmap_info
.offset
= 0;
5812 mddev
->bitmap_info
.default_offset
= 0;
5813 mddev
->bitmap_info
.default_space
= 0;
5814 mddev
->bitmap_info
.chunksize
= 0;
5815 mddev
->bitmap_info
.daemon_sleep
= 0;
5816 mddev
->bitmap_info
.max_write_behind
= 0;
5817 mddev
->bitmap_info
.nodes
= 0;
5820 static void __md_stop_writes(struct mddev
*mddev
)
5822 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5823 flush_workqueue(md_misc_wq
);
5824 if (mddev
->sync_thread
) {
5825 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5826 md_reap_sync_thread(mddev
);
5829 del_timer_sync(&mddev
->safemode_timer
);
5831 if (mddev
->pers
&& mddev
->pers
->quiesce
) {
5832 mddev
->pers
->quiesce(mddev
, 1);
5833 mddev
->pers
->quiesce(mddev
, 0);
5835 bitmap_flush(mddev
);
5837 if (mddev
->ro
== 0 &&
5838 ((!mddev
->in_sync
&& !mddev_is_clustered(mddev
)) ||
5840 /* mark array as shutdown cleanly */
5841 if (!mddev_is_clustered(mddev
))
5843 md_update_sb(mddev
, 1);
5847 void md_stop_writes(struct mddev
*mddev
)
5849 mddev_lock_nointr(mddev
);
5850 __md_stop_writes(mddev
);
5851 mddev_unlock(mddev
);
5853 EXPORT_SYMBOL_GPL(md_stop_writes
);
5855 static void mddev_detach(struct mddev
*mddev
)
5857 bitmap_wait_behind_writes(mddev
);
5858 if (mddev
->pers
&& mddev
->pers
->quiesce
) {
5859 mddev
->pers
->quiesce(mddev
, 1);
5860 mddev
->pers
->quiesce(mddev
, 0);
5862 md_unregister_thread(&mddev
->thread
);
5864 blk_sync_queue(mddev
->queue
); /* the unplug fn references 'conf'*/
5867 static void __md_stop(struct mddev
*mddev
)
5869 struct md_personality
*pers
= mddev
->pers
;
5870 bitmap_destroy(mddev
);
5871 mddev_detach(mddev
);
5872 /* Ensure ->event_work is done */
5873 flush_workqueue(md_misc_wq
);
5874 spin_lock(&mddev
->lock
);
5876 spin_unlock(&mddev
->lock
);
5877 pers
->free(mddev
, mddev
->private);
5878 mddev
->private = NULL
;
5879 if (pers
->sync_request
&& mddev
->to_remove
== NULL
)
5880 mddev
->to_remove
= &md_redundancy_group
;
5881 module_put(pers
->owner
);
5882 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5885 void md_stop(struct mddev
*mddev
)
5887 /* stop the array and free an attached data structures.
5888 * This is called from dm-raid
5891 if (mddev
->bio_set
) {
5892 bioset_free(mddev
->bio_set
);
5893 mddev
->bio_set
= NULL
;
5895 if (mddev
->sync_set
) {
5896 bioset_free(mddev
->sync_set
);
5897 mddev
->sync_set
= NULL
;
5901 EXPORT_SYMBOL_GPL(md_stop
);
5903 static int md_set_readonly(struct mddev
*mddev
, struct block_device
*bdev
)
5908 if (!test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
)) {
5910 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5911 md_wakeup_thread(mddev
->thread
);
5913 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
5914 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5915 if (mddev
->sync_thread
)
5916 /* Thread might be blocked waiting for metadata update
5917 * which will now never happen */
5918 wake_up_process(mddev
->sync_thread
->tsk
);
5920 if (mddev
->external
&& test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
))
5922 mddev_unlock(mddev
);
5923 wait_event(resync_wait
, !test_bit(MD_RECOVERY_RUNNING
,
5925 wait_event(mddev
->sb_wait
,
5926 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
5927 mddev_lock_nointr(mddev
);
5929 mutex_lock(&mddev
->open_mutex
);
5930 if ((mddev
->pers
&& atomic_read(&mddev
->openers
) > !!bdev
) ||
5931 mddev
->sync_thread
||
5932 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
)) {
5933 pr_warn("md: %s still in use.\n",mdname(mddev
));
5935 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5936 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5937 md_wakeup_thread(mddev
->thread
);
5943 __md_stop_writes(mddev
);
5949 set_disk_ro(mddev
->gendisk
, 1);
5950 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5951 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5952 md_wakeup_thread(mddev
->thread
);
5953 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5957 mutex_unlock(&mddev
->open_mutex
);
5962 * 0 - completely stop and dis-assemble array
5963 * 2 - stop but do not disassemble array
5965 static int do_md_stop(struct mddev
*mddev
, int mode
,
5966 struct block_device
*bdev
)
5968 struct gendisk
*disk
= mddev
->gendisk
;
5969 struct md_rdev
*rdev
;
5972 if (!test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
)) {
5974 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5975 md_wakeup_thread(mddev
->thread
);
5977 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
5978 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5979 if (mddev
->sync_thread
)
5980 /* Thread might be blocked waiting for metadata update
5981 * which will now never happen */
5982 wake_up_process(mddev
->sync_thread
->tsk
);
5984 mddev_unlock(mddev
);
5985 wait_event(resync_wait
, (mddev
->sync_thread
== NULL
&&
5986 !test_bit(MD_RECOVERY_RUNNING
,
5987 &mddev
->recovery
)));
5988 mddev_lock_nointr(mddev
);
5990 mutex_lock(&mddev
->open_mutex
);
5991 if ((mddev
->pers
&& atomic_read(&mddev
->openers
) > !!bdev
) ||
5992 mddev
->sysfs_active
||
5993 mddev
->sync_thread
||
5994 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
)) {
5995 pr_warn("md: %s still in use.\n",mdname(mddev
));
5996 mutex_unlock(&mddev
->open_mutex
);
5998 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5999 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6000 md_wakeup_thread(mddev
->thread
);
6006 set_disk_ro(disk
, 0);
6008 __md_stop_writes(mddev
);
6010 mddev
->queue
->backing_dev_info
->congested_fn
= NULL
;
6012 /* tell userspace to handle 'inactive' */
6013 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6015 rdev_for_each(rdev
, mddev
)
6016 if (rdev
->raid_disk
>= 0)
6017 sysfs_unlink_rdev(mddev
, rdev
);
6019 set_capacity(disk
, 0);
6020 mutex_unlock(&mddev
->open_mutex
);
6022 revalidate_disk(disk
);
6027 mutex_unlock(&mddev
->open_mutex
);
6029 * Free resources if final stop
6032 pr_info("md: %s stopped.\n", mdname(mddev
));
6034 if (mddev
->bitmap_info
.file
) {
6035 struct file
*f
= mddev
->bitmap_info
.file
;
6036 spin_lock(&mddev
->lock
);
6037 mddev
->bitmap_info
.file
= NULL
;
6038 spin_unlock(&mddev
->lock
);
6041 mddev
->bitmap_info
.offset
= 0;
6043 export_array(mddev
);
6046 if (mddev
->hold_active
== UNTIL_STOP
)
6047 mddev
->hold_active
= 0;
6049 md_new_event(mddev
);
6050 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6055 static void autorun_array(struct mddev
*mddev
)
6057 struct md_rdev
*rdev
;
6060 if (list_empty(&mddev
->disks
))
6063 pr_info("md: running: ");
6065 rdev_for_each(rdev
, mddev
) {
6066 char b
[BDEVNAME_SIZE
];
6067 pr_cont("<%s>", bdevname(rdev
->bdev
,b
));
6071 err
= do_md_run(mddev
);
6073 pr_warn("md: do_md_run() returned %d\n", err
);
6074 do_md_stop(mddev
, 0, NULL
);
6079 * lets try to run arrays based on all disks that have arrived
6080 * until now. (those are in pending_raid_disks)
6082 * the method: pick the first pending disk, collect all disks with
6083 * the same UUID, remove all from the pending list and put them into
6084 * the 'same_array' list. Then order this list based on superblock
6085 * update time (freshest comes first), kick out 'old' disks and
6086 * compare superblocks. If everything's fine then run it.
6088 * If "unit" is allocated, then bump its reference count
6090 static void autorun_devices(int part
)
6092 struct md_rdev
*rdev0
, *rdev
, *tmp
;
6093 struct mddev
*mddev
;
6094 char b
[BDEVNAME_SIZE
];
6096 pr_info("md: autorun ...\n");
6097 while (!list_empty(&pending_raid_disks
)) {
6100 LIST_HEAD(candidates
);
6101 rdev0
= list_entry(pending_raid_disks
.next
,
6102 struct md_rdev
, same_set
);
6104 pr_debug("md: considering %s ...\n", bdevname(rdev0
->bdev
,b
));
6105 INIT_LIST_HEAD(&candidates
);
6106 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
6107 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
6108 pr_debug("md: adding %s ...\n",
6109 bdevname(rdev
->bdev
,b
));
6110 list_move(&rdev
->same_set
, &candidates
);
6113 * now we have a set of devices, with all of them having
6114 * mostly sane superblocks. It's time to allocate the
6118 dev
= MKDEV(mdp_major
,
6119 rdev0
->preferred_minor
<< MdpMinorShift
);
6120 unit
= MINOR(dev
) >> MdpMinorShift
;
6122 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
6125 if (rdev0
->preferred_minor
!= unit
) {
6126 pr_warn("md: unit number in %s is bad: %d\n",
6127 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
6131 md_probe(dev
, NULL
, NULL
);
6132 mddev
= mddev_find(dev
);
6133 if (!mddev
|| !mddev
->gendisk
) {
6138 if (mddev_lock(mddev
))
6139 pr_warn("md: %s locked, cannot run\n", mdname(mddev
));
6140 else if (mddev
->raid_disks
|| mddev
->major_version
6141 || !list_empty(&mddev
->disks
)) {
6142 pr_warn("md: %s already running, cannot run %s\n",
6143 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
6144 mddev_unlock(mddev
);
6146 pr_debug("md: created %s\n", mdname(mddev
));
6147 mddev
->persistent
= 1;
6148 rdev_for_each_list(rdev
, tmp
, &candidates
) {
6149 list_del_init(&rdev
->same_set
);
6150 if (bind_rdev_to_array(rdev
, mddev
))
6153 autorun_array(mddev
);
6154 mddev_unlock(mddev
);
6156 /* on success, candidates will be empty, on error
6159 rdev_for_each_list(rdev
, tmp
, &candidates
) {
6160 list_del_init(&rdev
->same_set
);
6165 pr_info("md: ... autorun DONE.\n");
6167 #endif /* !MODULE */
6169 static int get_version(void __user
*arg
)
6173 ver
.major
= MD_MAJOR_VERSION
;
6174 ver
.minor
= MD_MINOR_VERSION
;
6175 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
6177 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
6183 static int get_array_info(struct mddev
*mddev
, void __user
*arg
)
6185 mdu_array_info_t info
;
6186 int nr
,working
,insync
,failed
,spare
;
6187 struct md_rdev
*rdev
;
6189 nr
= working
= insync
= failed
= spare
= 0;
6191 rdev_for_each_rcu(rdev
, mddev
) {
6193 if (test_bit(Faulty
, &rdev
->flags
))
6197 if (test_bit(In_sync
, &rdev
->flags
))
6199 else if (test_bit(Journal
, &rdev
->flags
))
6200 /* TODO: add journal count to md_u.h */
6208 info
.major_version
= mddev
->major_version
;
6209 info
.minor_version
= mddev
->minor_version
;
6210 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
6211 info
.ctime
= clamp_t(time64_t
, mddev
->ctime
, 0, U32_MAX
);
6212 info
.level
= mddev
->level
;
6213 info
.size
= mddev
->dev_sectors
/ 2;
6214 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
6217 info
.raid_disks
= mddev
->raid_disks
;
6218 info
.md_minor
= mddev
->md_minor
;
6219 info
.not_persistent
= !mddev
->persistent
;
6221 info
.utime
= clamp_t(time64_t
, mddev
->utime
, 0, U32_MAX
);
6224 info
.state
= (1<<MD_SB_CLEAN
);
6225 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
6226 info
.state
|= (1<<MD_SB_BITMAP_PRESENT
);
6227 if (mddev_is_clustered(mddev
))
6228 info
.state
|= (1<<MD_SB_CLUSTERED
);
6229 info
.active_disks
= insync
;
6230 info
.working_disks
= working
;
6231 info
.failed_disks
= failed
;
6232 info
.spare_disks
= spare
;
6234 info
.layout
= mddev
->layout
;
6235 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
6237 if (copy_to_user(arg
, &info
, sizeof(info
)))
6243 static int get_bitmap_file(struct mddev
*mddev
, void __user
* arg
)
6245 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
6249 file
= kzalloc(sizeof(*file
), GFP_NOIO
);
6254 spin_lock(&mddev
->lock
);
6255 /* bitmap enabled */
6256 if (mddev
->bitmap_info
.file
) {
6257 ptr
= file_path(mddev
->bitmap_info
.file
, file
->pathname
,
6258 sizeof(file
->pathname
));
6262 memmove(file
->pathname
, ptr
,
6263 sizeof(file
->pathname
)-(ptr
-file
->pathname
));
6265 spin_unlock(&mddev
->lock
);
6268 copy_to_user(arg
, file
, sizeof(*file
)))
6275 static int get_disk_info(struct mddev
*mddev
, void __user
* arg
)
6277 mdu_disk_info_t info
;
6278 struct md_rdev
*rdev
;
6280 if (copy_from_user(&info
, arg
, sizeof(info
)))
6284 rdev
= md_find_rdev_nr_rcu(mddev
, info
.number
);
6286 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
6287 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
6288 info
.raid_disk
= rdev
->raid_disk
;
6290 if (test_bit(Faulty
, &rdev
->flags
))
6291 info
.state
|= (1<<MD_DISK_FAULTY
);
6292 else if (test_bit(In_sync
, &rdev
->flags
)) {
6293 info
.state
|= (1<<MD_DISK_ACTIVE
);
6294 info
.state
|= (1<<MD_DISK_SYNC
);
6296 if (test_bit(Journal
, &rdev
->flags
))
6297 info
.state
|= (1<<MD_DISK_JOURNAL
);
6298 if (test_bit(WriteMostly
, &rdev
->flags
))
6299 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
6300 if (test_bit(FailFast
, &rdev
->flags
))
6301 info
.state
|= (1<<MD_DISK_FAILFAST
);
6303 info
.major
= info
.minor
= 0;
6304 info
.raid_disk
= -1;
6305 info
.state
= (1<<MD_DISK_REMOVED
);
6309 if (copy_to_user(arg
, &info
, sizeof(info
)))
6315 static int add_new_disk(struct mddev
*mddev
, mdu_disk_info_t
*info
)
6317 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
6318 struct md_rdev
*rdev
;
6319 dev_t dev
= MKDEV(info
->major
,info
->minor
);
6321 if (mddev_is_clustered(mddev
) &&
6322 !(info
->state
& ((1 << MD_DISK_CLUSTER_ADD
) | (1 << MD_DISK_CANDIDATE
)))) {
6323 pr_warn("%s: Cannot add to clustered mddev.\n",
6328 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
6331 if (!mddev
->raid_disks
) {
6333 /* expecting a device which has a superblock */
6334 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
6336 pr_warn("md: md_import_device returned %ld\n",
6338 return PTR_ERR(rdev
);
6340 if (!list_empty(&mddev
->disks
)) {
6341 struct md_rdev
*rdev0
6342 = list_entry(mddev
->disks
.next
,
6343 struct md_rdev
, same_set
);
6344 err
= super_types
[mddev
->major_version
]
6345 .load_super(rdev
, rdev0
, mddev
->minor_version
);
6347 pr_warn("md: %s has different UUID to %s\n",
6348 bdevname(rdev
->bdev
,b
),
6349 bdevname(rdev0
->bdev
,b2
));
6354 err
= bind_rdev_to_array(rdev
, mddev
);
6361 * add_new_disk can be used once the array is assembled
6362 * to add "hot spares". They must already have a superblock
6367 if (!mddev
->pers
->hot_add_disk
) {
6368 pr_warn("%s: personality does not support diskops!\n",
6372 if (mddev
->persistent
)
6373 rdev
= md_import_device(dev
, mddev
->major_version
,
6374 mddev
->minor_version
);
6376 rdev
= md_import_device(dev
, -1, -1);
6378 pr_warn("md: md_import_device returned %ld\n",
6380 return PTR_ERR(rdev
);
6382 /* set saved_raid_disk if appropriate */
6383 if (!mddev
->persistent
) {
6384 if (info
->state
& (1<<MD_DISK_SYNC
) &&
6385 info
->raid_disk
< mddev
->raid_disks
) {
6386 rdev
->raid_disk
= info
->raid_disk
;
6387 set_bit(In_sync
, &rdev
->flags
);
6388 clear_bit(Bitmap_sync
, &rdev
->flags
);
6390 rdev
->raid_disk
= -1;
6391 rdev
->saved_raid_disk
= rdev
->raid_disk
;
6393 super_types
[mddev
->major_version
].
6394 validate_super(mddev
, rdev
);
6395 if ((info
->state
& (1<<MD_DISK_SYNC
)) &&
6396 rdev
->raid_disk
!= info
->raid_disk
) {
6397 /* This was a hot-add request, but events doesn't
6398 * match, so reject it.
6404 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
6405 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
6406 set_bit(WriteMostly
, &rdev
->flags
);
6408 clear_bit(WriteMostly
, &rdev
->flags
);
6409 if (info
->state
& (1<<MD_DISK_FAILFAST
))
6410 set_bit(FailFast
, &rdev
->flags
);
6412 clear_bit(FailFast
, &rdev
->flags
);
6414 if (info
->state
& (1<<MD_DISK_JOURNAL
)) {
6415 struct md_rdev
*rdev2
;
6416 bool has_journal
= false;
6418 /* make sure no existing journal disk */
6419 rdev_for_each(rdev2
, mddev
) {
6420 if (test_bit(Journal
, &rdev2
->flags
)) {
6425 if (has_journal
|| mddev
->bitmap
) {
6429 set_bit(Journal
, &rdev
->flags
);
6432 * check whether the device shows up in other nodes
6434 if (mddev_is_clustered(mddev
)) {
6435 if (info
->state
& (1 << MD_DISK_CANDIDATE
))
6436 set_bit(Candidate
, &rdev
->flags
);
6437 else if (info
->state
& (1 << MD_DISK_CLUSTER_ADD
)) {
6438 /* --add initiated by this node */
6439 err
= md_cluster_ops
->add_new_disk(mddev
, rdev
);
6447 rdev
->raid_disk
= -1;
6448 err
= bind_rdev_to_array(rdev
, mddev
);
6453 if (mddev_is_clustered(mddev
)) {
6454 if (info
->state
& (1 << MD_DISK_CANDIDATE
)) {
6456 err
= md_cluster_ops
->new_disk_ack(mddev
,
6459 md_kick_rdev_from_array(rdev
);
6463 md_cluster_ops
->add_new_disk_cancel(mddev
);
6465 err
= add_bound_rdev(rdev
);
6469 err
= add_bound_rdev(rdev
);
6474 /* otherwise, add_new_disk is only allowed
6475 * for major_version==0 superblocks
6477 if (mddev
->major_version
!= 0) {
6478 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev
));
6482 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
6484 rdev
= md_import_device(dev
, -1, 0);
6486 pr_warn("md: error, md_import_device() returned %ld\n",
6488 return PTR_ERR(rdev
);
6490 rdev
->desc_nr
= info
->number
;
6491 if (info
->raid_disk
< mddev
->raid_disks
)
6492 rdev
->raid_disk
= info
->raid_disk
;
6494 rdev
->raid_disk
= -1;
6496 if (rdev
->raid_disk
< mddev
->raid_disks
)
6497 if (info
->state
& (1<<MD_DISK_SYNC
))
6498 set_bit(In_sync
, &rdev
->flags
);
6500 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
6501 set_bit(WriteMostly
, &rdev
->flags
);
6502 if (info
->state
& (1<<MD_DISK_FAILFAST
))
6503 set_bit(FailFast
, &rdev
->flags
);
6505 if (!mddev
->persistent
) {
6506 pr_debug("md: nonpersistent superblock ...\n");
6507 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
6509 rdev
->sb_start
= calc_dev_sboffset(rdev
);
6510 rdev
->sectors
= rdev
->sb_start
;
6512 err
= bind_rdev_to_array(rdev
, mddev
);
6522 static int hot_remove_disk(struct mddev
*mddev
, dev_t dev
)
6524 char b
[BDEVNAME_SIZE
];
6525 struct md_rdev
*rdev
;
6527 rdev
= find_rdev(mddev
, dev
);
6531 if (rdev
->raid_disk
< 0)
6534 clear_bit(Blocked
, &rdev
->flags
);
6535 remove_and_add_spares(mddev
, rdev
);
6537 if (rdev
->raid_disk
>= 0)
6541 if (mddev_is_clustered(mddev
))
6542 md_cluster_ops
->remove_disk(mddev
, rdev
);
6544 md_kick_rdev_from_array(rdev
);
6545 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
6547 md_wakeup_thread(mddev
->thread
);
6549 md_update_sb(mddev
, 1);
6550 md_new_event(mddev
);
6554 pr_debug("md: cannot remove active disk %s from %s ...\n",
6555 bdevname(rdev
->bdev
,b
), mdname(mddev
));
6559 static int hot_add_disk(struct mddev
*mddev
, dev_t dev
)
6561 char b
[BDEVNAME_SIZE
];
6563 struct md_rdev
*rdev
;
6568 if (mddev
->major_version
!= 0) {
6569 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
6573 if (!mddev
->pers
->hot_add_disk
) {
6574 pr_warn("%s: personality does not support diskops!\n",
6579 rdev
= md_import_device(dev
, -1, 0);
6581 pr_warn("md: error, md_import_device() returned %ld\n",
6586 if (mddev
->persistent
)
6587 rdev
->sb_start
= calc_dev_sboffset(rdev
);
6589 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
6591 rdev
->sectors
= rdev
->sb_start
;
6593 if (test_bit(Faulty
, &rdev
->flags
)) {
6594 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
6595 bdevname(rdev
->bdev
,b
), mdname(mddev
));
6600 clear_bit(In_sync
, &rdev
->flags
);
6602 rdev
->saved_raid_disk
= -1;
6603 err
= bind_rdev_to_array(rdev
, mddev
);
6608 * The rest should better be atomic, we can have disk failures
6609 * noticed in interrupt contexts ...
6612 rdev
->raid_disk
= -1;
6614 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
6616 md_update_sb(mddev
, 1);
6618 * Kick recovery, maybe this spare has to be added to the
6619 * array immediately.
6621 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6622 md_wakeup_thread(mddev
->thread
);
6623 md_new_event(mddev
);
6631 static int set_bitmap_file(struct mddev
*mddev
, int fd
)
6636 if (!mddev
->pers
->quiesce
|| !mddev
->thread
)
6638 if (mddev
->recovery
|| mddev
->sync_thread
)
6640 /* we should be able to change the bitmap.. */
6644 struct inode
*inode
;
6647 if (mddev
->bitmap
|| mddev
->bitmap_info
.file
)
6648 return -EEXIST
; /* cannot add when bitmap is present */
6652 pr_warn("%s: error: failed to get bitmap file\n",
6657 inode
= f
->f_mapping
->host
;
6658 if (!S_ISREG(inode
->i_mode
)) {
6659 pr_warn("%s: error: bitmap file must be a regular file\n",
6662 } else if (!(f
->f_mode
& FMODE_WRITE
)) {
6663 pr_warn("%s: error: bitmap file must open for write\n",
6666 } else if (atomic_read(&inode
->i_writecount
) != 1) {
6667 pr_warn("%s: error: bitmap file is already in use\n",
6675 mddev
->bitmap_info
.file
= f
;
6676 mddev
->bitmap_info
.offset
= 0; /* file overrides offset */
6677 } else if (mddev
->bitmap
== NULL
)
6678 return -ENOENT
; /* cannot remove what isn't there */
6682 struct bitmap
*bitmap
;
6684 bitmap
= bitmap_create(mddev
, -1);
6685 mddev_suspend(mddev
);
6686 if (!IS_ERR(bitmap
)) {
6687 mddev
->bitmap
= bitmap
;
6688 err
= bitmap_load(mddev
);
6690 err
= PTR_ERR(bitmap
);
6692 bitmap_destroy(mddev
);
6695 mddev_resume(mddev
);
6696 } else if (fd
< 0) {
6697 mddev_suspend(mddev
);
6698 bitmap_destroy(mddev
);
6699 mddev_resume(mddev
);
6703 struct file
*f
= mddev
->bitmap_info
.file
;
6705 spin_lock(&mddev
->lock
);
6706 mddev
->bitmap_info
.file
= NULL
;
6707 spin_unlock(&mddev
->lock
);
6716 * set_array_info is used two different ways
6717 * The original usage is when creating a new array.
6718 * In this usage, raid_disks is > 0 and it together with
6719 * level, size, not_persistent,layout,chunksize determine the
6720 * shape of the array.
6721 * This will always create an array with a type-0.90.0 superblock.
6722 * The newer usage is when assembling an array.
6723 * In this case raid_disks will be 0, and the major_version field is
6724 * use to determine which style super-blocks are to be found on the devices.
6725 * The minor and patch _version numbers are also kept incase the
6726 * super_block handler wishes to interpret them.
6728 static int set_array_info(struct mddev
*mddev
, mdu_array_info_t
*info
)
6731 if (info
->raid_disks
== 0) {
6732 /* just setting version number for superblock loading */
6733 if (info
->major_version
< 0 ||
6734 info
->major_version
>= ARRAY_SIZE(super_types
) ||
6735 super_types
[info
->major_version
].name
== NULL
) {
6736 /* maybe try to auto-load a module? */
6737 pr_warn("md: superblock version %d not known\n",
6738 info
->major_version
);
6741 mddev
->major_version
= info
->major_version
;
6742 mddev
->minor_version
= info
->minor_version
;
6743 mddev
->patch_version
= info
->patch_version
;
6744 mddev
->persistent
= !info
->not_persistent
;
6745 /* ensure mddev_put doesn't delete this now that there
6746 * is some minimal configuration.
6748 mddev
->ctime
= ktime_get_real_seconds();
6751 mddev
->major_version
= MD_MAJOR_VERSION
;
6752 mddev
->minor_version
= MD_MINOR_VERSION
;
6753 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
6754 mddev
->ctime
= ktime_get_real_seconds();
6756 mddev
->level
= info
->level
;
6757 mddev
->clevel
[0] = 0;
6758 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
6759 mddev
->raid_disks
= info
->raid_disks
;
6760 /* don't set md_minor, it is determined by which /dev/md* was
6763 if (info
->state
& (1<<MD_SB_CLEAN
))
6764 mddev
->recovery_cp
= MaxSector
;
6766 mddev
->recovery_cp
= 0;
6767 mddev
->persistent
= ! info
->not_persistent
;
6768 mddev
->external
= 0;
6770 mddev
->layout
= info
->layout
;
6771 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
6773 if (mddev
->persistent
) {
6774 mddev
->max_disks
= MD_SB_DISKS
;
6776 mddev
->sb_flags
= 0;
6778 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
6780 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
6781 mddev
->bitmap_info
.default_space
= 64*2 - (MD_SB_BYTES
>> 9);
6782 mddev
->bitmap_info
.offset
= 0;
6784 mddev
->reshape_position
= MaxSector
;
6787 * Generate a 128 bit UUID
6789 get_random_bytes(mddev
->uuid
, 16);
6791 mddev
->new_level
= mddev
->level
;
6792 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
6793 mddev
->new_layout
= mddev
->layout
;
6794 mddev
->delta_disks
= 0;
6795 mddev
->reshape_backwards
= 0;
6800 void md_set_array_sectors(struct mddev
*mddev
, sector_t array_sectors
)
6802 lockdep_assert_held(&mddev
->reconfig_mutex
);
6804 if (mddev
->external_size
)
6807 mddev
->array_sectors
= array_sectors
;
6809 EXPORT_SYMBOL(md_set_array_sectors
);
6811 static int update_size(struct mddev
*mddev
, sector_t num_sectors
)
6813 struct md_rdev
*rdev
;
6815 int fit
= (num_sectors
== 0);
6816 sector_t old_dev_sectors
= mddev
->dev_sectors
;
6818 if (mddev
->pers
->resize
== NULL
)
6820 /* The "num_sectors" is the number of sectors of each device that
6821 * is used. This can only make sense for arrays with redundancy.
6822 * linear and raid0 always use whatever space is available. We can only
6823 * consider changing this number if no resync or reconstruction is
6824 * happening, and if the new size is acceptable. It must fit before the
6825 * sb_start or, if that is <data_offset, it must fit before the size
6826 * of each device. If num_sectors is zero, we find the largest size
6829 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
6835 rdev_for_each(rdev
, mddev
) {
6836 sector_t avail
= rdev
->sectors
;
6838 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
6839 num_sectors
= avail
;
6840 if (avail
< num_sectors
)
6843 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
6845 if (mddev_is_clustered(mddev
))
6846 md_cluster_ops
->update_size(mddev
, old_dev_sectors
);
6847 else if (mddev
->queue
) {
6848 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
6849 revalidate_disk(mddev
->gendisk
);
6855 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
)
6858 struct md_rdev
*rdev
;
6859 /* change the number of raid disks */
6860 if (mddev
->pers
->check_reshape
== NULL
)
6864 if (raid_disks
<= 0 ||
6865 (mddev
->max_disks
&& raid_disks
>= mddev
->max_disks
))
6867 if (mddev
->sync_thread
||
6868 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
6869 mddev
->reshape_position
!= MaxSector
)
6872 rdev_for_each(rdev
, mddev
) {
6873 if (mddev
->raid_disks
< raid_disks
&&
6874 rdev
->data_offset
< rdev
->new_data_offset
)
6876 if (mddev
->raid_disks
> raid_disks
&&
6877 rdev
->data_offset
> rdev
->new_data_offset
)
6881 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
6882 if (mddev
->delta_disks
< 0)
6883 mddev
->reshape_backwards
= 1;
6884 else if (mddev
->delta_disks
> 0)
6885 mddev
->reshape_backwards
= 0;
6887 rv
= mddev
->pers
->check_reshape(mddev
);
6889 mddev
->delta_disks
= 0;
6890 mddev
->reshape_backwards
= 0;
6896 * update_array_info is used to change the configuration of an
6898 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6899 * fields in the info are checked against the array.
6900 * Any differences that cannot be handled will cause an error.
6901 * Normally, only one change can be managed at a time.
6903 static int update_array_info(struct mddev
*mddev
, mdu_array_info_t
*info
)
6909 /* calculate expected state,ignoring low bits */
6910 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
6911 state
|= (1 << MD_SB_BITMAP_PRESENT
);
6913 if (mddev
->major_version
!= info
->major_version
||
6914 mddev
->minor_version
!= info
->minor_version
||
6915 /* mddev->patch_version != info->patch_version || */
6916 mddev
->ctime
!= info
->ctime
||
6917 mddev
->level
!= info
->level
||
6918 /* mddev->layout != info->layout || */
6919 mddev
->persistent
!= !info
->not_persistent
||
6920 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
6921 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6922 ((state
^info
->state
) & 0xfffffe00)
6925 /* Check there is only one change */
6926 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
6928 if (mddev
->raid_disks
!= info
->raid_disks
)
6930 if (mddev
->layout
!= info
->layout
)
6932 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
6939 if (mddev
->layout
!= info
->layout
) {
6941 * we don't need to do anything at the md level, the
6942 * personality will take care of it all.
6944 if (mddev
->pers
->check_reshape
== NULL
)
6947 mddev
->new_layout
= info
->layout
;
6948 rv
= mddev
->pers
->check_reshape(mddev
);
6950 mddev
->new_layout
= mddev
->layout
;
6954 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
6955 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
6957 if (mddev
->raid_disks
!= info
->raid_disks
)
6958 rv
= update_raid_disks(mddev
, info
->raid_disks
);
6960 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
6961 if (mddev
->pers
->quiesce
== NULL
|| mddev
->thread
== NULL
) {
6965 if (mddev
->recovery
|| mddev
->sync_thread
) {
6969 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
6970 struct bitmap
*bitmap
;
6971 /* add the bitmap */
6972 if (mddev
->bitmap
) {
6976 if (mddev
->bitmap_info
.default_offset
== 0) {
6980 mddev
->bitmap_info
.offset
=
6981 mddev
->bitmap_info
.default_offset
;
6982 mddev
->bitmap_info
.space
=
6983 mddev
->bitmap_info
.default_space
;
6984 bitmap
= bitmap_create(mddev
, -1);
6985 mddev_suspend(mddev
);
6986 if (!IS_ERR(bitmap
)) {
6987 mddev
->bitmap
= bitmap
;
6988 rv
= bitmap_load(mddev
);
6990 rv
= PTR_ERR(bitmap
);
6992 bitmap_destroy(mddev
);
6993 mddev_resume(mddev
);
6995 /* remove the bitmap */
6996 if (!mddev
->bitmap
) {
7000 if (mddev
->bitmap
->storage
.file
) {
7004 if (mddev
->bitmap_info
.nodes
) {
7005 /* hold PW on all the bitmap lock */
7006 if (md_cluster_ops
->lock_all_bitmaps(mddev
) <= 0) {
7007 pr_warn("md: can't change bitmap to none since the array is in use by more than one node\n");
7009 md_cluster_ops
->unlock_all_bitmaps(mddev
);
7013 mddev
->bitmap_info
.nodes
= 0;
7014 md_cluster_ops
->leave(mddev
);
7016 mddev_suspend(mddev
);
7017 bitmap_destroy(mddev
);
7018 mddev_resume(mddev
);
7019 mddev
->bitmap_info
.offset
= 0;
7022 md_update_sb(mddev
, 1);
7028 static int set_disk_faulty(struct mddev
*mddev
, dev_t dev
)
7030 struct md_rdev
*rdev
;
7033 if (mddev
->pers
== NULL
)
7037 rdev
= md_find_rdev_rcu(mddev
, dev
);
7041 md_error(mddev
, rdev
);
7042 if (!test_bit(Faulty
, &rdev
->flags
))
7050 * We have a problem here : there is no easy way to give a CHS
7051 * virtual geometry. We currently pretend that we have a 2 heads
7052 * 4 sectors (with a BIG number of cylinders...). This drives
7053 * dosfs just mad... ;-)
7055 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
7057 struct mddev
*mddev
= bdev
->bd_disk
->private_data
;
7061 geo
->cylinders
= mddev
->array_sectors
/ 8;
7065 static inline bool md_ioctl_valid(unsigned int cmd
)
7070 case GET_ARRAY_INFO
:
7071 case GET_BITMAP_FILE
:
7074 case HOT_REMOVE_DISK
:
7077 case RESTART_ARRAY_RW
:
7079 case SET_ARRAY_INFO
:
7080 case SET_BITMAP_FILE
:
7081 case SET_DISK_FAULTY
:
7084 case CLUSTERED_DISK_NACK
:
7091 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
7092 unsigned int cmd
, unsigned long arg
)
7095 void __user
*argp
= (void __user
*)arg
;
7096 struct mddev
*mddev
= NULL
;
7098 bool did_set_md_closing
= false;
7100 if (!md_ioctl_valid(cmd
))
7105 case GET_ARRAY_INFO
:
7109 if (!capable(CAP_SYS_ADMIN
))
7114 * Commands dealing with the RAID driver but not any
7119 err
= get_version(argp
);
7125 autostart_arrays(arg
);
7132 * Commands creating/starting a new array:
7135 mddev
= bdev
->bd_disk
->private_data
;
7142 /* Some actions do not requires the mutex */
7144 case GET_ARRAY_INFO
:
7145 if (!mddev
->raid_disks
&& !mddev
->external
)
7148 err
= get_array_info(mddev
, argp
);
7152 if (!mddev
->raid_disks
&& !mddev
->external
)
7155 err
= get_disk_info(mddev
, argp
);
7158 case SET_DISK_FAULTY
:
7159 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
7162 case GET_BITMAP_FILE
:
7163 err
= get_bitmap_file(mddev
, argp
);
7168 if (cmd
== ADD_NEW_DISK
)
7169 /* need to ensure md_delayed_delete() has completed */
7170 flush_workqueue(md_misc_wq
);
7172 if (cmd
== HOT_REMOVE_DISK
)
7173 /* need to ensure recovery thread has run */
7174 wait_event_interruptible_timeout(mddev
->sb_wait
,
7175 !test_bit(MD_RECOVERY_NEEDED
,
7177 msecs_to_jiffies(5000));
7178 if (cmd
== STOP_ARRAY
|| cmd
== STOP_ARRAY_RO
) {
7179 /* Need to flush page cache, and ensure no-one else opens
7182 mutex_lock(&mddev
->open_mutex
);
7183 if (mddev
->pers
&& atomic_read(&mddev
->openers
) > 1) {
7184 mutex_unlock(&mddev
->open_mutex
);
7188 WARN_ON_ONCE(test_bit(MD_CLOSING
, &mddev
->flags
));
7189 set_bit(MD_CLOSING
, &mddev
->flags
);
7190 did_set_md_closing
= true;
7191 mutex_unlock(&mddev
->open_mutex
);
7192 sync_blockdev(bdev
);
7194 err
= mddev_lock(mddev
);
7196 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7201 if (cmd
== SET_ARRAY_INFO
) {
7202 mdu_array_info_t info
;
7204 memset(&info
, 0, sizeof(info
));
7205 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
7210 err
= update_array_info(mddev
, &info
);
7212 pr_warn("md: couldn't update array info. %d\n", err
);
7217 if (!list_empty(&mddev
->disks
)) {
7218 pr_warn("md: array %s already has disks!\n", mdname(mddev
));
7222 if (mddev
->raid_disks
) {
7223 pr_warn("md: array %s already initialised!\n", mdname(mddev
));
7227 err
= set_array_info(mddev
, &info
);
7229 pr_warn("md: couldn't set array info. %d\n", err
);
7236 * Commands querying/configuring an existing array:
7238 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
7239 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
7240 if ((!mddev
->raid_disks
&& !mddev
->external
)
7241 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
7242 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
7243 && cmd
!= GET_BITMAP_FILE
) {
7249 * Commands even a read-only array can execute:
7252 case RESTART_ARRAY_RW
:
7253 err
= restart_array(mddev
);
7257 err
= do_md_stop(mddev
, 0, bdev
);
7261 err
= md_set_readonly(mddev
, bdev
);
7264 case HOT_REMOVE_DISK
:
7265 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
7269 /* We can support ADD_NEW_DISK on read-only arrays
7270 * only if we are re-adding a preexisting device.
7271 * So require mddev->pers and MD_DISK_SYNC.
7274 mdu_disk_info_t info
;
7275 if (copy_from_user(&info
, argp
, sizeof(info
)))
7277 else if (!(info
.state
& (1<<MD_DISK_SYNC
)))
7278 /* Need to clear read-only for this */
7281 err
= add_new_disk(mddev
, &info
);
7287 if (get_user(ro
, (int __user
*)(arg
))) {
7293 /* if the bdev is going readonly the value of mddev->ro
7294 * does not matter, no writes are coming
7299 /* are we are already prepared for writes? */
7303 /* transitioning to readauto need only happen for
7304 * arrays that call md_write_start
7307 err
= restart_array(mddev
);
7310 set_disk_ro(mddev
->gendisk
, 0);
7317 * The remaining ioctls are changing the state of the
7318 * superblock, so we do not allow them on read-only arrays.
7320 if (mddev
->ro
&& mddev
->pers
) {
7321 if (mddev
->ro
== 2) {
7323 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7324 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7325 /* mddev_unlock will wake thread */
7326 /* If a device failed while we were read-only, we
7327 * need to make sure the metadata is updated now.
7329 if (test_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
)) {
7330 mddev_unlock(mddev
);
7331 wait_event(mddev
->sb_wait
,
7332 !test_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
) &&
7333 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
7334 mddev_lock_nointr(mddev
);
7345 mdu_disk_info_t info
;
7346 if (copy_from_user(&info
, argp
, sizeof(info
)))
7349 err
= add_new_disk(mddev
, &info
);
7353 case CLUSTERED_DISK_NACK
:
7354 if (mddev_is_clustered(mddev
))
7355 md_cluster_ops
->new_disk_ack(mddev
, false);
7361 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
7365 err
= do_md_run(mddev
);
7368 case SET_BITMAP_FILE
:
7369 err
= set_bitmap_file(mddev
, (int)arg
);
7378 if (mddev
->hold_active
== UNTIL_IOCTL
&&
7380 mddev
->hold_active
= 0;
7381 mddev_unlock(mddev
);
7383 if(did_set_md_closing
)
7384 clear_bit(MD_CLOSING
, &mddev
->flags
);
7387 #ifdef CONFIG_COMPAT
7388 static int md_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
7389 unsigned int cmd
, unsigned long arg
)
7392 case HOT_REMOVE_DISK
:
7394 case SET_DISK_FAULTY
:
7395 case SET_BITMAP_FILE
:
7396 /* These take in integer arg, do not convert */
7399 arg
= (unsigned long)compat_ptr(arg
);
7403 return md_ioctl(bdev
, mode
, cmd
, arg
);
7405 #endif /* CONFIG_COMPAT */
7407 static int md_open(struct block_device
*bdev
, fmode_t mode
)
7410 * Succeed if we can lock the mddev, which confirms that
7411 * it isn't being stopped right now.
7413 struct mddev
*mddev
= mddev_find(bdev
->bd_dev
);
7419 if (mddev
->gendisk
!= bdev
->bd_disk
) {
7420 /* we are racing with mddev_put which is discarding this
7424 /* Wait until bdev->bd_disk is definitely gone */
7425 flush_workqueue(md_misc_wq
);
7426 /* Then retry the open from the top */
7427 return -ERESTARTSYS
;
7429 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
7431 if ((err
= mutex_lock_interruptible(&mddev
->open_mutex
)))
7434 if (test_bit(MD_CLOSING
, &mddev
->flags
)) {
7435 mutex_unlock(&mddev
->open_mutex
);
7441 atomic_inc(&mddev
->openers
);
7442 mutex_unlock(&mddev
->open_mutex
);
7444 check_disk_change(bdev
);
7451 static void md_release(struct gendisk
*disk
, fmode_t mode
)
7453 struct mddev
*mddev
= disk
->private_data
;
7456 atomic_dec(&mddev
->openers
);
7460 static int md_media_changed(struct gendisk
*disk
)
7462 struct mddev
*mddev
= disk
->private_data
;
7464 return mddev
->changed
;
7467 static int md_revalidate(struct gendisk
*disk
)
7469 struct mddev
*mddev
= disk
->private_data
;
7474 static const struct block_device_operations md_fops
=
7476 .owner
= THIS_MODULE
,
7478 .release
= md_release
,
7480 #ifdef CONFIG_COMPAT
7481 .compat_ioctl
= md_compat_ioctl
,
7483 .getgeo
= md_getgeo
,
7484 .media_changed
= md_media_changed
,
7485 .revalidate_disk
= md_revalidate
,
7488 static int md_thread(void *arg
)
7490 struct md_thread
*thread
= arg
;
7493 * md_thread is a 'system-thread', it's priority should be very
7494 * high. We avoid resource deadlocks individually in each
7495 * raid personality. (RAID5 does preallocation) We also use RR and
7496 * the very same RT priority as kswapd, thus we will never get
7497 * into a priority inversion deadlock.
7499 * we definitely have to have equal or higher priority than
7500 * bdflush, otherwise bdflush will deadlock if there are too
7501 * many dirty RAID5 blocks.
7504 allow_signal(SIGKILL
);
7505 while (!kthread_should_stop()) {
7507 /* We need to wait INTERRUPTIBLE so that
7508 * we don't add to the load-average.
7509 * That means we need to be sure no signals are
7512 if (signal_pending(current
))
7513 flush_signals(current
);
7515 wait_event_interruptible_timeout
7517 test_bit(THREAD_WAKEUP
, &thread
->flags
)
7518 || kthread_should_stop() || kthread_should_park(),
7521 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
7522 if (kthread_should_park())
7524 if (!kthread_should_stop())
7525 thread
->run(thread
);
7531 void md_wakeup_thread(struct md_thread
*thread
)
7534 pr_debug("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
7535 set_bit(THREAD_WAKEUP
, &thread
->flags
);
7536 wake_up(&thread
->wqueue
);
7539 EXPORT_SYMBOL(md_wakeup_thread
);
7541 struct md_thread
*md_register_thread(void (*run
) (struct md_thread
*),
7542 struct mddev
*mddev
, const char *name
)
7544 struct md_thread
*thread
;
7546 thread
= kzalloc(sizeof(struct md_thread
), GFP_KERNEL
);
7550 init_waitqueue_head(&thread
->wqueue
);
7553 thread
->mddev
= mddev
;
7554 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
7555 thread
->tsk
= kthread_run(md_thread
, thread
,
7557 mdname(thread
->mddev
),
7559 if (IS_ERR(thread
->tsk
)) {
7565 EXPORT_SYMBOL(md_register_thread
);
7567 void md_unregister_thread(struct md_thread
**threadp
)
7569 struct md_thread
*thread
= *threadp
;
7572 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
7573 /* Locking ensures that mddev_unlock does not wake_up a
7574 * non-existent thread
7576 spin_lock(&pers_lock
);
7578 spin_unlock(&pers_lock
);
7580 kthread_stop(thread
->tsk
);
7583 EXPORT_SYMBOL(md_unregister_thread
);
7585 void md_error(struct mddev
*mddev
, struct md_rdev
*rdev
)
7587 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
7590 if (!mddev
->pers
|| !mddev
->pers
->error_handler
)
7592 mddev
->pers
->error_handler(mddev
,rdev
);
7593 if (mddev
->degraded
)
7594 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7595 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
7596 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7597 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7598 md_wakeup_thread(mddev
->thread
);
7599 if (mddev
->event_work
.func
)
7600 queue_work(md_misc_wq
, &mddev
->event_work
);
7601 md_new_event(mddev
);
7603 EXPORT_SYMBOL(md_error
);
7605 /* seq_file implementation /proc/mdstat */
7607 static void status_unused(struct seq_file
*seq
)
7610 struct md_rdev
*rdev
;
7612 seq_printf(seq
, "unused devices: ");
7614 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
7615 char b
[BDEVNAME_SIZE
];
7617 seq_printf(seq
, "%s ",
7618 bdevname(rdev
->bdev
,b
));
7621 seq_printf(seq
, "<none>");
7623 seq_printf(seq
, "\n");
7626 static int status_resync(struct seq_file
*seq
, struct mddev
*mddev
)
7628 sector_t max_sectors
, resync
, res
;
7629 unsigned long dt
, db
;
7632 unsigned int per_milli
;
7634 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
7635 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
7636 max_sectors
= mddev
->resync_max_sectors
;
7638 max_sectors
= mddev
->dev_sectors
;
7640 resync
= mddev
->curr_resync
;
7642 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
7643 /* Still cleaning up */
7644 resync
= max_sectors
;
7645 } else if (resync
> max_sectors
)
7646 resync
= max_sectors
;
7648 resync
-= atomic_read(&mddev
->recovery_active
);
7651 if (mddev
->recovery_cp
< MaxSector
) {
7652 seq_printf(seq
, "\tresync=PENDING");
7658 seq_printf(seq
, "\tresync=DELAYED");
7662 WARN_ON(max_sectors
== 0);
7663 /* Pick 'scale' such that (resync>>scale)*1000 will fit
7664 * in a sector_t, and (max_sectors>>scale) will fit in a
7665 * u32, as those are the requirements for sector_div.
7666 * Thus 'scale' must be at least 10
7669 if (sizeof(sector_t
) > sizeof(unsigned long)) {
7670 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
7673 res
= (resync
>>scale
)*1000;
7674 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
7678 int i
, x
= per_milli
/50, y
= 20-x
;
7679 seq_printf(seq
, "[");
7680 for (i
= 0; i
< x
; i
++)
7681 seq_printf(seq
, "=");
7682 seq_printf(seq
, ">");
7683 for (i
= 0; i
< y
; i
++)
7684 seq_printf(seq
, ".");
7685 seq_printf(seq
, "] ");
7687 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
7688 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
7690 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
7692 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
7693 "resync" : "recovery"))),
7694 per_milli
/10, per_milli
% 10,
7695 (unsigned long long) resync
/2,
7696 (unsigned long long) max_sectors
/2);
7699 * dt: time from mark until now
7700 * db: blocks written from mark until now
7701 * rt: remaining time
7703 * rt is a sector_t, so could be 32bit or 64bit.
7704 * So we divide before multiply in case it is 32bit and close
7706 * We scale the divisor (db) by 32 to avoid losing precision
7707 * near the end of resync when the number of remaining sectors
7709 * We then divide rt by 32 after multiplying by db to compensate.
7710 * The '+1' avoids division by zero if db is very small.
7712 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
7714 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
7715 - mddev
->resync_mark_cnt
;
7717 rt
= max_sectors
- resync
; /* number of remaining sectors */
7718 sector_div(rt
, db
/32+1);
7722 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
7723 ((unsigned long)rt
% 60)/6);
7725 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
7729 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
7731 struct list_head
*tmp
;
7733 struct mddev
*mddev
;
7741 spin_lock(&all_mddevs_lock
);
7742 list_for_each(tmp
,&all_mddevs
)
7744 mddev
= list_entry(tmp
, struct mddev
, all_mddevs
);
7746 spin_unlock(&all_mddevs_lock
);
7749 spin_unlock(&all_mddevs_lock
);
7751 return (void*)2;/* tail */
7755 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
7757 struct list_head
*tmp
;
7758 struct mddev
*next_mddev
, *mddev
= v
;
7764 spin_lock(&all_mddevs_lock
);
7766 tmp
= all_mddevs
.next
;
7768 tmp
= mddev
->all_mddevs
.next
;
7769 if (tmp
!= &all_mddevs
)
7770 next_mddev
= mddev_get(list_entry(tmp
,struct mddev
,all_mddevs
));
7772 next_mddev
= (void*)2;
7775 spin_unlock(&all_mddevs_lock
);
7783 static void md_seq_stop(struct seq_file
*seq
, void *v
)
7785 struct mddev
*mddev
= v
;
7787 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
7791 static int md_seq_show(struct seq_file
*seq
, void *v
)
7793 struct mddev
*mddev
= v
;
7795 struct md_rdev
*rdev
;
7797 if (v
== (void*)1) {
7798 struct md_personality
*pers
;
7799 seq_printf(seq
, "Personalities : ");
7800 spin_lock(&pers_lock
);
7801 list_for_each_entry(pers
, &pers_list
, list
)
7802 seq_printf(seq
, "[%s] ", pers
->name
);
7804 spin_unlock(&pers_lock
);
7805 seq_printf(seq
, "\n");
7806 seq
->poll_event
= atomic_read(&md_event_count
);
7809 if (v
== (void*)2) {
7814 spin_lock(&mddev
->lock
);
7815 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
7816 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
7817 mddev
->pers
? "" : "in");
7820 seq_printf(seq
, " (read-only)");
7822 seq_printf(seq
, " (auto-read-only)");
7823 seq_printf(seq
, " %s", mddev
->pers
->name
);
7828 rdev_for_each_rcu(rdev
, mddev
) {
7829 char b
[BDEVNAME_SIZE
];
7830 seq_printf(seq
, " %s[%d]",
7831 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
7832 if (test_bit(WriteMostly
, &rdev
->flags
))
7833 seq_printf(seq
, "(W)");
7834 if (test_bit(Journal
, &rdev
->flags
))
7835 seq_printf(seq
, "(J)");
7836 if (test_bit(Faulty
, &rdev
->flags
)) {
7837 seq_printf(seq
, "(F)");
7840 if (rdev
->raid_disk
< 0)
7841 seq_printf(seq
, "(S)"); /* spare */
7842 if (test_bit(Replacement
, &rdev
->flags
))
7843 seq_printf(seq
, "(R)");
7844 sectors
+= rdev
->sectors
;
7848 if (!list_empty(&mddev
->disks
)) {
7850 seq_printf(seq
, "\n %llu blocks",
7851 (unsigned long long)
7852 mddev
->array_sectors
/ 2);
7854 seq_printf(seq
, "\n %llu blocks",
7855 (unsigned long long)sectors
/ 2);
7857 if (mddev
->persistent
) {
7858 if (mddev
->major_version
!= 0 ||
7859 mddev
->minor_version
!= 90) {
7860 seq_printf(seq
," super %d.%d",
7861 mddev
->major_version
,
7862 mddev
->minor_version
);
7864 } else if (mddev
->external
)
7865 seq_printf(seq
, " super external:%s",
7866 mddev
->metadata_type
);
7868 seq_printf(seq
, " super non-persistent");
7871 mddev
->pers
->status(seq
, mddev
);
7872 seq_printf(seq
, "\n ");
7873 if (mddev
->pers
->sync_request
) {
7874 if (status_resync(seq
, mddev
))
7875 seq_printf(seq
, "\n ");
7878 seq_printf(seq
, "\n ");
7880 bitmap_status(seq
, mddev
->bitmap
);
7882 seq_printf(seq
, "\n");
7884 spin_unlock(&mddev
->lock
);
7889 static const struct seq_operations md_seq_ops
= {
7890 .start
= md_seq_start
,
7891 .next
= md_seq_next
,
7892 .stop
= md_seq_stop
,
7893 .show
= md_seq_show
,
7896 static int md_seq_open(struct inode
*inode
, struct file
*file
)
7898 struct seq_file
*seq
;
7901 error
= seq_open(file
, &md_seq_ops
);
7905 seq
= file
->private_data
;
7906 seq
->poll_event
= atomic_read(&md_event_count
);
7910 static int md_unloading
;
7911 static __poll_t
mdstat_poll(struct file
*filp
, poll_table
*wait
)
7913 struct seq_file
*seq
= filp
->private_data
;
7917 return EPOLLIN
|EPOLLRDNORM
|EPOLLERR
|EPOLLPRI
;
7918 poll_wait(filp
, &md_event_waiters
, wait
);
7920 /* always allow read */
7921 mask
= EPOLLIN
| EPOLLRDNORM
;
7923 if (seq
->poll_event
!= atomic_read(&md_event_count
))
7924 mask
|= EPOLLERR
| EPOLLPRI
;
7928 static const struct file_operations md_seq_fops
= {
7929 .owner
= THIS_MODULE
,
7930 .open
= md_seq_open
,
7932 .llseek
= seq_lseek
,
7933 .release
= seq_release
,
7934 .poll
= mdstat_poll
,
7937 int register_md_personality(struct md_personality
*p
)
7939 pr_debug("md: %s personality registered for level %d\n",
7941 spin_lock(&pers_lock
);
7942 list_add_tail(&p
->list
, &pers_list
);
7943 spin_unlock(&pers_lock
);
7946 EXPORT_SYMBOL(register_md_personality
);
7948 int unregister_md_personality(struct md_personality
*p
)
7950 pr_debug("md: %s personality unregistered\n", p
->name
);
7951 spin_lock(&pers_lock
);
7952 list_del_init(&p
->list
);
7953 spin_unlock(&pers_lock
);
7956 EXPORT_SYMBOL(unregister_md_personality
);
7958 int register_md_cluster_operations(struct md_cluster_operations
*ops
,
7959 struct module
*module
)
7962 spin_lock(&pers_lock
);
7963 if (md_cluster_ops
!= NULL
)
7966 md_cluster_ops
= ops
;
7967 md_cluster_mod
= module
;
7969 spin_unlock(&pers_lock
);
7972 EXPORT_SYMBOL(register_md_cluster_operations
);
7974 int unregister_md_cluster_operations(void)
7976 spin_lock(&pers_lock
);
7977 md_cluster_ops
= NULL
;
7978 spin_unlock(&pers_lock
);
7981 EXPORT_SYMBOL(unregister_md_cluster_operations
);
7983 int md_setup_cluster(struct mddev
*mddev
, int nodes
)
7985 if (!md_cluster_ops
)
7986 request_module("md-cluster");
7987 spin_lock(&pers_lock
);
7988 /* ensure module won't be unloaded */
7989 if (!md_cluster_ops
|| !try_module_get(md_cluster_mod
)) {
7990 pr_warn("can't find md-cluster module or get it's reference.\n");
7991 spin_unlock(&pers_lock
);
7994 spin_unlock(&pers_lock
);
7996 return md_cluster_ops
->join(mddev
, nodes
);
7999 void md_cluster_stop(struct mddev
*mddev
)
8001 if (!md_cluster_ops
)
8003 md_cluster_ops
->leave(mddev
);
8004 module_put(md_cluster_mod
);
8007 static int is_mddev_idle(struct mddev
*mddev
, int init
)
8009 struct md_rdev
*rdev
;
8015 rdev_for_each_rcu(rdev
, mddev
) {
8016 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
8017 curr_events
= (int)part_stat_read(&disk
->part0
, sectors
[0]) +
8018 (int)part_stat_read(&disk
->part0
, sectors
[1]) -
8019 atomic_read(&disk
->sync_io
);
8020 /* sync IO will cause sync_io to increase before the disk_stats
8021 * as sync_io is counted when a request starts, and
8022 * disk_stats is counted when it completes.
8023 * So resync activity will cause curr_events to be smaller than
8024 * when there was no such activity.
8025 * non-sync IO will cause disk_stat to increase without
8026 * increasing sync_io so curr_events will (eventually)
8027 * be larger than it was before. Once it becomes
8028 * substantially larger, the test below will cause
8029 * the array to appear non-idle, and resync will slow
8031 * If there is a lot of outstanding resync activity when
8032 * we set last_event to curr_events, then all that activity
8033 * completing might cause the array to appear non-idle
8034 * and resync will be slowed down even though there might
8035 * not have been non-resync activity. This will only
8036 * happen once though. 'last_events' will soon reflect
8037 * the state where there is little or no outstanding
8038 * resync requests, and further resync activity will
8039 * always make curr_events less than last_events.
8042 if (init
|| curr_events
- rdev
->last_events
> 64) {
8043 rdev
->last_events
= curr_events
;
8051 void md_done_sync(struct mddev
*mddev
, int blocks
, int ok
)
8053 /* another "blocks" (512byte) blocks have been synced */
8054 atomic_sub(blocks
, &mddev
->recovery_active
);
8055 wake_up(&mddev
->recovery_wait
);
8057 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8058 set_bit(MD_RECOVERY_ERROR
, &mddev
->recovery
);
8059 md_wakeup_thread(mddev
->thread
);
8060 // stop recovery, signal do_sync ....
8063 EXPORT_SYMBOL(md_done_sync
);
8065 /* md_write_start(mddev, bi)
8066 * If we need to update some array metadata (e.g. 'active' flag
8067 * in superblock) before writing, schedule a superblock update
8068 * and wait for it to complete.
8069 * A return value of 'false' means that the write wasn't recorded
8070 * and cannot proceed as the array is being suspend.
8072 bool md_write_start(struct mddev
*mddev
, struct bio
*bi
)
8076 if (bio_data_dir(bi
) != WRITE
)
8079 BUG_ON(mddev
->ro
== 1);
8080 if (mddev
->ro
== 2) {
8081 /* need to switch to read/write */
8083 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8084 md_wakeup_thread(mddev
->thread
);
8085 md_wakeup_thread(mddev
->sync_thread
);
8089 percpu_ref_get(&mddev
->writes_pending
);
8090 smp_mb(); /* Match smp_mb in set_in_sync() */
8091 if (mddev
->safemode
== 1)
8092 mddev
->safemode
= 0;
8093 /* sync_checkers is always 0 when writes_pending is in per-cpu mode */
8094 if (mddev
->in_sync
|| mddev
->sync_checkers
) {
8095 spin_lock(&mddev
->lock
);
8096 if (mddev
->in_sync
) {
8098 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
8099 set_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
8100 md_wakeup_thread(mddev
->thread
);
8103 spin_unlock(&mddev
->lock
);
8107 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
8108 if (!mddev
->has_superblocks
)
8110 wait_event(mddev
->sb_wait
,
8111 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
) ||
8113 if (test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
)) {
8114 percpu_ref_put(&mddev
->writes_pending
);
8119 EXPORT_SYMBOL(md_write_start
);
8121 /* md_write_inc can only be called when md_write_start() has
8122 * already been called at least once of the current request.
8123 * It increments the counter and is useful when a single request
8124 * is split into several parts. Each part causes an increment and
8125 * so needs a matching md_write_end().
8126 * Unlike md_write_start(), it is safe to call md_write_inc() inside
8127 * a spinlocked region.
8129 void md_write_inc(struct mddev
*mddev
, struct bio
*bi
)
8131 if (bio_data_dir(bi
) != WRITE
)
8133 WARN_ON_ONCE(mddev
->in_sync
|| mddev
->ro
);
8134 percpu_ref_get(&mddev
->writes_pending
);
8136 EXPORT_SYMBOL(md_write_inc
);
8138 void md_write_end(struct mddev
*mddev
)
8140 percpu_ref_put(&mddev
->writes_pending
);
8142 if (mddev
->safemode
== 2)
8143 md_wakeup_thread(mddev
->thread
);
8144 else if (mddev
->safemode_delay
)
8145 /* The roundup() ensures this only performs locking once
8146 * every ->safemode_delay jiffies
8148 mod_timer(&mddev
->safemode_timer
,
8149 roundup(jiffies
, mddev
->safemode_delay
) +
8150 mddev
->safemode_delay
);
8153 EXPORT_SYMBOL(md_write_end
);
8155 /* md_allow_write(mddev)
8156 * Calling this ensures that the array is marked 'active' so that writes
8157 * may proceed without blocking. It is important to call this before
8158 * attempting a GFP_KERNEL allocation while holding the mddev lock.
8159 * Must be called with mddev_lock held.
8161 void md_allow_write(struct mddev
*mddev
)
8167 if (!mddev
->pers
->sync_request
)
8170 spin_lock(&mddev
->lock
);
8171 if (mddev
->in_sync
) {
8173 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
8174 set_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
8175 if (mddev
->safemode_delay
&&
8176 mddev
->safemode
== 0)
8177 mddev
->safemode
= 1;
8178 spin_unlock(&mddev
->lock
);
8179 md_update_sb(mddev
, 0);
8180 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
8181 /* wait for the dirty state to be recorded in the metadata */
8182 wait_event(mddev
->sb_wait
,
8183 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
8185 spin_unlock(&mddev
->lock
);
8187 EXPORT_SYMBOL_GPL(md_allow_write
);
8189 #define SYNC_MARKS 10
8190 #define SYNC_MARK_STEP (3*HZ)
8191 #define UPDATE_FREQUENCY (5*60*HZ)
8192 void md_do_sync(struct md_thread
*thread
)
8194 struct mddev
*mddev
= thread
->mddev
;
8195 struct mddev
*mddev2
;
8196 unsigned int currspeed
= 0,
8198 sector_t max_sectors
,j
, io_sectors
, recovery_done
;
8199 unsigned long mark
[SYNC_MARKS
];
8200 unsigned long update_time
;
8201 sector_t mark_cnt
[SYNC_MARKS
];
8203 struct list_head
*tmp
;
8204 sector_t last_check
;
8206 struct md_rdev
*rdev
;
8207 char *desc
, *action
= NULL
;
8208 struct blk_plug plug
;
8211 /* just incase thread restarts... */
8212 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
8213 test_bit(MD_RECOVERY_WAIT
, &mddev
->recovery
))
8215 if (mddev
->ro
) {/* never try to sync a read-only array */
8216 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8220 if (mddev_is_clustered(mddev
)) {
8221 ret
= md_cluster_ops
->resync_start(mddev
);
8225 set_bit(MD_CLUSTER_RESYNC_LOCKED
, &mddev
->flags
);
8226 if (!(test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
8227 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) ||
8228 test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
8229 && ((unsigned long long)mddev
->curr_resync_completed
8230 < (unsigned long long)mddev
->resync_max_sectors
))
8234 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
8235 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)) {
8236 desc
= "data-check";
8238 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
8239 desc
= "requested-resync";
8243 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
8248 mddev
->last_sync_action
= action
?: desc
;
8250 /* we overload curr_resync somewhat here.
8251 * 0 == not engaged in resync at all
8252 * 2 == checking that there is no conflict with another sync
8253 * 1 == like 2, but have yielded to allow conflicting resync to
8255 * other == active in resync - this many blocks
8257 * Before starting a resync we must have set curr_resync to
8258 * 2, and then checked that every "conflicting" array has curr_resync
8259 * less than ours. When we find one that is the same or higher
8260 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8261 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8262 * This will mean we have to start checking from the beginning again.
8267 int mddev2_minor
= -1;
8268 mddev
->curr_resync
= 2;
8271 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8273 for_each_mddev(mddev2
, tmp
) {
8274 if (mddev2
== mddev
)
8276 if (!mddev
->parallel_resync
8277 && mddev2
->curr_resync
8278 && match_mddev_units(mddev
, mddev2
)) {
8280 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
8281 /* arbitrarily yield */
8282 mddev
->curr_resync
= 1;
8283 wake_up(&resync_wait
);
8285 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
8286 /* no need to wait here, we can wait the next
8287 * time 'round when curr_resync == 2
8290 /* We need to wait 'interruptible' so as not to
8291 * contribute to the load average, and not to
8292 * be caught by 'softlockup'
8294 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
8295 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
8296 mddev2
->curr_resync
>= mddev
->curr_resync
) {
8297 if (mddev2_minor
!= mddev2
->md_minor
) {
8298 mddev2_minor
= mddev2
->md_minor
;
8299 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8300 desc
, mdname(mddev
),
8304 if (signal_pending(current
))
8305 flush_signals(current
);
8307 finish_wait(&resync_wait
, &wq
);
8310 finish_wait(&resync_wait
, &wq
);
8313 } while (mddev
->curr_resync
< 2);
8316 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
8317 /* resync follows the size requested by the personality,
8318 * which defaults to physical size, but can be virtual size
8320 max_sectors
= mddev
->resync_max_sectors
;
8321 atomic64_set(&mddev
->resync_mismatches
, 0);
8322 /* we don't use the checkpoint if there's a bitmap */
8323 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
8324 j
= mddev
->resync_min
;
8325 else if (!mddev
->bitmap
)
8326 j
= mddev
->recovery_cp
;
8328 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
8329 max_sectors
= mddev
->resync_max_sectors
;
8331 /* recovery follows the physical size of devices */
8332 max_sectors
= mddev
->dev_sectors
;
8335 rdev_for_each_rcu(rdev
, mddev
)
8336 if (rdev
->raid_disk
>= 0 &&
8337 !test_bit(Journal
, &rdev
->flags
) &&
8338 !test_bit(Faulty
, &rdev
->flags
) &&
8339 !test_bit(In_sync
, &rdev
->flags
) &&
8340 rdev
->recovery_offset
< j
)
8341 j
= rdev
->recovery_offset
;
8344 /* If there is a bitmap, we need to make sure all
8345 * writes that started before we added a spare
8346 * complete before we start doing a recovery.
8347 * Otherwise the write might complete and (via
8348 * bitmap_endwrite) set a bit in the bitmap after the
8349 * recovery has checked that bit and skipped that
8352 if (mddev
->bitmap
) {
8353 mddev
->pers
->quiesce(mddev
, 1);
8354 mddev
->pers
->quiesce(mddev
, 0);
8358 pr_info("md: %s of RAID array %s\n", desc
, mdname(mddev
));
8359 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev
));
8360 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8361 speed_max(mddev
), desc
);
8363 is_mddev_idle(mddev
, 1); /* this initializes IO event counters */
8366 for (m
= 0; m
< SYNC_MARKS
; m
++) {
8368 mark_cnt
[m
] = io_sectors
;
8371 mddev
->resync_mark
= mark
[last_mark
];
8372 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
8375 * Tune reconstruction:
8377 window
= 32*(PAGE_SIZE
/512);
8378 pr_debug("md: using %dk window, over a total of %lluk.\n",
8379 window
/2, (unsigned long long)max_sectors
/2);
8381 atomic_set(&mddev
->recovery_active
, 0);
8385 pr_debug("md: resuming %s of %s from checkpoint.\n",
8386 desc
, mdname(mddev
));
8387 mddev
->curr_resync
= j
;
8389 mddev
->curr_resync
= 3; /* no longer delayed */
8390 mddev
->curr_resync_completed
= j
;
8391 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
8392 md_new_event(mddev
);
8393 update_time
= jiffies
;
8395 blk_start_plug(&plug
);
8396 while (j
< max_sectors
) {
8401 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8402 ((mddev
->curr_resync
> mddev
->curr_resync_completed
&&
8403 (mddev
->curr_resync
- mddev
->curr_resync_completed
)
8404 > (max_sectors
>> 4)) ||
8405 time_after_eq(jiffies
, update_time
+ UPDATE_FREQUENCY
) ||
8406 (j
- mddev
->curr_resync_completed
)*2
8407 >= mddev
->resync_max
- mddev
->curr_resync_completed
||
8408 mddev
->curr_resync_completed
> mddev
->resync_max
8410 /* time to update curr_resync_completed */
8411 wait_event(mddev
->recovery_wait
,
8412 atomic_read(&mddev
->recovery_active
) == 0);
8413 mddev
->curr_resync_completed
= j
;
8414 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) &&
8415 j
> mddev
->recovery_cp
)
8416 mddev
->recovery_cp
= j
;
8417 update_time
= jiffies
;
8418 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
8419 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
8422 while (j
>= mddev
->resync_max
&&
8423 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
8424 /* As this condition is controlled by user-space,
8425 * we can block indefinitely, so use '_interruptible'
8426 * to avoid triggering warnings.
8428 flush_signals(current
); /* just in case */
8429 wait_event_interruptible(mddev
->recovery_wait
,
8430 mddev
->resync_max
> j
8431 || test_bit(MD_RECOVERY_INTR
,
8435 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8438 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
);
8440 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8444 if (!skipped
) { /* actual IO requested */
8445 io_sectors
+= sectors
;
8446 atomic_add(sectors
, &mddev
->recovery_active
);
8449 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8453 if (j
> max_sectors
)
8454 /* when skipping, extra large numbers can be returned. */
8457 mddev
->curr_resync
= j
;
8458 mddev
->curr_mark_cnt
= io_sectors
;
8459 if (last_check
== 0)
8460 /* this is the earliest that rebuild will be
8461 * visible in /proc/mdstat
8463 md_new_event(mddev
);
8465 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
8468 last_check
= io_sectors
;
8470 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
8472 int next
= (last_mark
+1) % SYNC_MARKS
;
8474 mddev
->resync_mark
= mark
[next
];
8475 mddev
->resync_mark_cnt
= mark_cnt
[next
];
8476 mark
[next
] = jiffies
;
8477 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
8481 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8485 * this loop exits only if either when we are slower than
8486 * the 'hard' speed limit, or the system was IO-idle for
8488 * the system might be non-idle CPU-wise, but we only care
8489 * about not overloading the IO subsystem. (things like an
8490 * e2fsck being done on the RAID array should execute fast)
8494 recovery_done
= io_sectors
- atomic_read(&mddev
->recovery_active
);
8495 currspeed
= ((unsigned long)(recovery_done
- mddev
->resync_mark_cnt
))/2
8496 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
8498 if (currspeed
> speed_min(mddev
)) {
8499 if (currspeed
> speed_max(mddev
)) {
8503 if (!is_mddev_idle(mddev
, 0)) {
8505 * Give other IO more of a chance.
8506 * The faster the devices, the less we wait.
8508 wait_event(mddev
->recovery_wait
,
8509 !atomic_read(&mddev
->recovery_active
));
8513 pr_info("md: %s: %s %s.\n",mdname(mddev
), desc
,
8514 test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)
8515 ? "interrupted" : "done");
8517 * this also signals 'finished resyncing' to md_stop
8519 blk_finish_plug(&plug
);
8520 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
8522 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8523 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
8524 mddev
->curr_resync
> 3) {
8525 mddev
->curr_resync_completed
= mddev
->curr_resync
;
8526 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
8528 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
);
8530 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
8531 mddev
->curr_resync
> 3) {
8532 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
8533 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
8534 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
8535 pr_debug("md: checkpointing %s of %s.\n",
8536 desc
, mdname(mddev
));
8537 if (test_bit(MD_RECOVERY_ERROR
,
8539 mddev
->recovery_cp
=
8540 mddev
->curr_resync_completed
;
8542 mddev
->recovery_cp
=
8546 mddev
->recovery_cp
= MaxSector
;
8548 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8549 mddev
->curr_resync
= MaxSector
;
8550 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8551 test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
)) {
8553 rdev_for_each_rcu(rdev
, mddev
)
8554 if (rdev
->raid_disk
>= 0 &&
8555 mddev
->delta_disks
>= 0 &&
8556 !test_bit(Journal
, &rdev
->flags
) &&
8557 !test_bit(Faulty
, &rdev
->flags
) &&
8558 !test_bit(In_sync
, &rdev
->flags
) &&
8559 rdev
->recovery_offset
< mddev
->curr_resync
)
8560 rdev
->recovery_offset
= mddev
->curr_resync
;
8566 /* set CHANGE_PENDING here since maybe another update is needed,
8567 * so other nodes are informed. It should be harmless for normal
8569 set_mask_bits(&mddev
->sb_flags
, 0,
8570 BIT(MD_SB_CHANGE_PENDING
) | BIT(MD_SB_CHANGE_DEVS
));
8572 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8573 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
8574 mddev
->delta_disks
> 0 &&
8575 mddev
->pers
->finish_reshape
&&
8576 mddev
->pers
->size
&&
8578 mddev_lock_nointr(mddev
);
8579 md_set_array_sectors(mddev
, mddev
->pers
->size(mddev
, 0, 0));
8580 mddev_unlock(mddev
);
8581 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
8582 revalidate_disk(mddev
->gendisk
);
8585 spin_lock(&mddev
->lock
);
8586 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
8587 /* We completed so min/max setting can be forgotten if used. */
8588 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
8589 mddev
->resync_min
= 0;
8590 mddev
->resync_max
= MaxSector
;
8591 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
8592 mddev
->resync_min
= mddev
->curr_resync_completed
;
8593 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
8594 mddev
->curr_resync
= 0;
8595 spin_unlock(&mddev
->lock
);
8597 wake_up(&resync_wait
);
8598 md_wakeup_thread(mddev
->thread
);
8601 EXPORT_SYMBOL_GPL(md_do_sync
);
8603 static int remove_and_add_spares(struct mddev
*mddev
,
8604 struct md_rdev
*this)
8606 struct md_rdev
*rdev
;
8609 bool remove_some
= false;
8611 if (this && test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
8612 /* Mustn't remove devices when resync thread is running */
8615 rdev_for_each(rdev
, mddev
) {
8616 if ((this == NULL
|| rdev
== this) &&
8617 rdev
->raid_disk
>= 0 &&
8618 !test_bit(Blocked
, &rdev
->flags
) &&
8619 test_bit(Faulty
, &rdev
->flags
) &&
8620 atomic_read(&rdev
->nr_pending
)==0) {
8621 /* Faulty non-Blocked devices with nr_pending == 0
8622 * never get nr_pending incremented,
8623 * never get Faulty cleared, and never get Blocked set.
8624 * So we can synchronize_rcu now rather than once per device
8627 set_bit(RemoveSynchronized
, &rdev
->flags
);
8633 rdev_for_each(rdev
, mddev
) {
8634 if ((this == NULL
|| rdev
== this) &&
8635 rdev
->raid_disk
>= 0 &&
8636 !test_bit(Blocked
, &rdev
->flags
) &&
8637 ((test_bit(RemoveSynchronized
, &rdev
->flags
) ||
8638 (!test_bit(In_sync
, &rdev
->flags
) &&
8639 !test_bit(Journal
, &rdev
->flags
))) &&
8640 atomic_read(&rdev
->nr_pending
)==0)) {
8641 if (mddev
->pers
->hot_remove_disk(
8642 mddev
, rdev
) == 0) {
8643 sysfs_unlink_rdev(mddev
, rdev
);
8644 rdev
->raid_disk
= -1;
8648 if (remove_some
&& test_bit(RemoveSynchronized
, &rdev
->flags
))
8649 clear_bit(RemoveSynchronized
, &rdev
->flags
);
8652 if (removed
&& mddev
->kobj
.sd
)
8653 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
8655 if (this && removed
)
8658 rdev_for_each(rdev
, mddev
) {
8659 if (this && this != rdev
)
8661 if (test_bit(Candidate
, &rdev
->flags
))
8663 if (rdev
->raid_disk
>= 0 &&
8664 !test_bit(In_sync
, &rdev
->flags
) &&
8665 !test_bit(Journal
, &rdev
->flags
) &&
8666 !test_bit(Faulty
, &rdev
->flags
))
8668 if (rdev
->raid_disk
>= 0)
8670 if (test_bit(Faulty
, &rdev
->flags
))
8672 if (!test_bit(Journal
, &rdev
->flags
)) {
8674 ! (rdev
->saved_raid_disk
>= 0 &&
8675 !test_bit(Bitmap_sync
, &rdev
->flags
)))
8678 rdev
->recovery_offset
= 0;
8681 hot_add_disk(mddev
, rdev
) == 0) {
8682 if (sysfs_link_rdev(mddev
, rdev
))
8683 /* failure here is OK */;
8684 if (!test_bit(Journal
, &rdev
->flags
))
8686 md_new_event(mddev
);
8687 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
8692 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
8696 static void md_start_sync(struct work_struct
*ws
)
8698 struct mddev
*mddev
= container_of(ws
, struct mddev
, del_work
);
8700 mddev
->sync_thread
= md_register_thread(md_do_sync
,
8703 if (!mddev
->sync_thread
) {
8704 pr_warn("%s: could not start resync thread...\n",
8706 /* leave the spares where they are, it shouldn't hurt */
8707 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
8708 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
8709 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
8710 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
8711 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
8712 wake_up(&resync_wait
);
8713 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
8715 if (mddev
->sysfs_action
)
8716 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
8718 md_wakeup_thread(mddev
->sync_thread
);
8719 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
8720 md_new_event(mddev
);
8724 * This routine is regularly called by all per-raid-array threads to
8725 * deal with generic issues like resync and super-block update.
8726 * Raid personalities that don't have a thread (linear/raid0) do not
8727 * need this as they never do any recovery or update the superblock.
8729 * It does not do any resync itself, but rather "forks" off other threads
8730 * to do that as needed.
8731 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8732 * "->recovery" and create a thread at ->sync_thread.
8733 * When the thread finishes it sets MD_RECOVERY_DONE
8734 * and wakeups up this thread which will reap the thread and finish up.
8735 * This thread also removes any faulty devices (with nr_pending == 0).
8737 * The overall approach is:
8738 * 1/ if the superblock needs updating, update it.
8739 * 2/ If a recovery thread is running, don't do anything else.
8740 * 3/ If recovery has finished, clean up, possibly marking spares active.
8741 * 4/ If there are any faulty devices, remove them.
8742 * 5/ If array is degraded, try to add spares devices
8743 * 6/ If array has spares or is not in-sync, start a resync thread.
8745 void md_check_recovery(struct mddev
*mddev
)
8747 if (mddev
->suspended
)
8751 bitmap_daemon_work(mddev
);
8753 if (signal_pending(current
)) {
8754 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
8755 pr_debug("md: %s in immediate safe mode\n",
8757 mddev
->safemode
= 2;
8759 flush_signals(current
);
8762 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
8765 (mddev
->sb_flags
& ~ (1<<MD_SB_CHANGE_PENDING
)) ||
8766 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
8767 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
8768 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
8769 (mddev
->safemode
== 2
8770 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
8774 if (mddev_trylock(mddev
)) {
8777 if (!mddev
->external
&& mddev
->safemode
== 1)
8778 mddev
->safemode
= 0;
8781 struct md_rdev
*rdev
;
8782 if (!mddev
->external
&& mddev
->in_sync
)
8783 /* 'Blocked' flag not needed as failed devices
8784 * will be recorded if array switched to read/write.
8785 * Leaving it set will prevent the device
8786 * from being removed.
8788 rdev_for_each(rdev
, mddev
)
8789 clear_bit(Blocked
, &rdev
->flags
);
8790 /* On a read-only array we can:
8791 * - remove failed devices
8792 * - add already-in_sync devices if the array itself
8794 * As we only add devices that are already in-sync,
8795 * we can activate the spares immediately.
8797 remove_and_add_spares(mddev
, NULL
);
8798 /* There is no thread, but we need to call
8799 * ->spare_active and clear saved_raid_disk
8801 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8802 md_reap_sync_thread(mddev
);
8803 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8804 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8805 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
8809 if (mddev_is_clustered(mddev
)) {
8810 struct md_rdev
*rdev
;
8811 /* kick the device if another node issued a
8814 rdev_for_each(rdev
, mddev
) {
8815 if (test_and_clear_bit(ClusterRemove
, &rdev
->flags
) &&
8816 rdev
->raid_disk
< 0)
8817 md_kick_rdev_from_array(rdev
);
8821 if (!mddev
->external
&& !mddev
->in_sync
) {
8822 spin_lock(&mddev
->lock
);
8824 spin_unlock(&mddev
->lock
);
8827 if (mddev
->sb_flags
)
8828 md_update_sb(mddev
, 0);
8830 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
8831 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
8832 /* resync/recovery still happening */
8833 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8836 if (mddev
->sync_thread
) {
8837 md_reap_sync_thread(mddev
);
8840 /* Set RUNNING before clearing NEEDED to avoid
8841 * any transients in the value of "sync_action".
8843 mddev
->curr_resync_completed
= 0;
8844 spin_lock(&mddev
->lock
);
8845 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
8846 spin_unlock(&mddev
->lock
);
8847 /* Clear some bits that don't mean anything, but
8850 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8851 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
8853 if (!test_and_clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
8854 test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
8856 /* no recovery is running.
8857 * remove any failed drives, then
8858 * add spares if possible.
8859 * Spares are also removed and re-added, to allow
8860 * the personality to fail the re-add.
8863 if (mddev
->reshape_position
!= MaxSector
) {
8864 if (mddev
->pers
->check_reshape
== NULL
||
8865 mddev
->pers
->check_reshape(mddev
) != 0)
8866 /* Cannot proceed */
8868 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
8869 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8870 } else if ((spares
= remove_and_add_spares(mddev
, NULL
))) {
8871 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
8872 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
8873 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
8874 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8875 } else if (mddev
->recovery_cp
< MaxSector
) {
8876 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
8877 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8878 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
8879 /* nothing to be done ... */
8882 if (mddev
->pers
->sync_request
) {
8884 /* We are adding a device or devices to an array
8885 * which has the bitmap stored on all devices.
8886 * So make sure all bitmap pages get written
8888 bitmap_write_all(mddev
->bitmap
);
8890 INIT_WORK(&mddev
->del_work
, md_start_sync
);
8891 queue_work(md_misc_wq
, &mddev
->del_work
);
8895 if (!mddev
->sync_thread
) {
8896 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
8897 wake_up(&resync_wait
);
8898 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
8900 if (mddev
->sysfs_action
)
8901 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
8904 wake_up(&mddev
->sb_wait
);
8905 mddev_unlock(mddev
);
8906 } else if (test_bit(MD_ALLOW_SB_UPDATE
, &mddev
->flags
) && mddev
->sb_flags
) {
8907 /* Write superblock - thread that called mddev_suspend()
8908 * holds reconfig_mutex for us.
8910 set_bit(MD_UPDATING_SB
, &mddev
->flags
);
8911 smp_mb__after_atomic();
8912 if (test_bit(MD_ALLOW_SB_UPDATE
, &mddev
->flags
))
8913 md_update_sb(mddev
, 0);
8914 clear_bit_unlock(MD_UPDATING_SB
, &mddev
->flags
);
8915 wake_up(&mddev
->sb_wait
);
8918 EXPORT_SYMBOL(md_check_recovery
);
8920 void md_reap_sync_thread(struct mddev
*mddev
)
8922 struct md_rdev
*rdev
;
8924 /* resync has finished, collect result */
8925 md_unregister_thread(&mddev
->sync_thread
);
8926 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
8927 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
8929 /* activate any spares */
8930 if (mddev
->pers
->spare_active(mddev
)) {
8931 sysfs_notify(&mddev
->kobj
, NULL
,
8933 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
8936 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8937 mddev
->pers
->finish_reshape
)
8938 mddev
->pers
->finish_reshape(mddev
);
8940 /* If array is no-longer degraded, then any saved_raid_disk
8941 * information must be scrapped.
8943 if (!mddev
->degraded
)
8944 rdev_for_each(rdev
, mddev
)
8945 rdev
->saved_raid_disk
= -1;
8947 md_update_sb(mddev
, 1);
8948 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
8949 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
8951 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED
, &mddev
->flags
))
8952 md_cluster_ops
->resync_finish(mddev
);
8953 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
8954 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
8955 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
8956 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
8957 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
8958 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
8959 wake_up(&resync_wait
);
8960 /* flag recovery needed just to double check */
8961 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8962 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
8963 md_new_event(mddev
);
8964 if (mddev
->event_work
.func
)
8965 queue_work(md_misc_wq
, &mddev
->event_work
);
8967 EXPORT_SYMBOL(md_reap_sync_thread
);
8969 void md_wait_for_blocked_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
8971 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
8972 wait_event_timeout(rdev
->blocked_wait
,
8973 !test_bit(Blocked
, &rdev
->flags
) &&
8974 !test_bit(BlockedBadBlocks
, &rdev
->flags
),
8975 msecs_to_jiffies(5000));
8976 rdev_dec_pending(rdev
, mddev
);
8978 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
8980 void md_finish_reshape(struct mddev
*mddev
)
8982 /* called be personality module when reshape completes. */
8983 struct md_rdev
*rdev
;
8985 rdev_for_each(rdev
, mddev
) {
8986 if (rdev
->data_offset
> rdev
->new_data_offset
)
8987 rdev
->sectors
+= rdev
->data_offset
- rdev
->new_data_offset
;
8989 rdev
->sectors
-= rdev
->new_data_offset
- rdev
->data_offset
;
8990 rdev
->data_offset
= rdev
->new_data_offset
;
8993 EXPORT_SYMBOL(md_finish_reshape
);
8995 /* Bad block management */
8997 /* Returns 1 on success, 0 on failure */
8998 int rdev_set_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
,
9001 struct mddev
*mddev
= rdev
->mddev
;
9004 s
+= rdev
->new_data_offset
;
9006 s
+= rdev
->data_offset
;
9007 rv
= badblocks_set(&rdev
->badblocks
, s
, sectors
, 0);
9009 /* Make sure they get written out promptly */
9010 if (test_bit(ExternalBbl
, &rdev
->flags
))
9011 sysfs_notify(&rdev
->kobj
, NULL
,
9012 "unacknowledged_bad_blocks");
9013 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
9014 set_mask_bits(&mddev
->sb_flags
, 0,
9015 BIT(MD_SB_CHANGE_CLEAN
) | BIT(MD_SB_CHANGE_PENDING
));
9016 md_wakeup_thread(rdev
->mddev
->thread
);
9021 EXPORT_SYMBOL_GPL(rdev_set_badblocks
);
9023 int rdev_clear_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
,
9028 s
+= rdev
->new_data_offset
;
9030 s
+= rdev
->data_offset
;
9031 rv
= badblocks_clear(&rdev
->badblocks
, s
, sectors
);
9032 if ((rv
== 0) && test_bit(ExternalBbl
, &rdev
->flags
))
9033 sysfs_notify(&rdev
->kobj
, NULL
, "bad_blocks");
9036 EXPORT_SYMBOL_GPL(rdev_clear_badblocks
);
9038 static int md_notify_reboot(struct notifier_block
*this,
9039 unsigned long code
, void *x
)
9041 struct list_head
*tmp
;
9042 struct mddev
*mddev
;
9045 for_each_mddev(mddev
, tmp
) {
9046 if (mddev_trylock(mddev
)) {
9048 __md_stop_writes(mddev
);
9049 if (mddev
->persistent
)
9050 mddev
->safemode
= 2;
9051 mddev_unlock(mddev
);
9056 * certain more exotic SCSI devices are known to be
9057 * volatile wrt too early system reboots. While the
9058 * right place to handle this issue is the given
9059 * driver, we do want to have a safe RAID driver ...
9067 static struct notifier_block md_notifier
= {
9068 .notifier_call
= md_notify_reboot
,
9070 .priority
= INT_MAX
, /* before any real devices */
9073 static void md_geninit(void)
9075 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
9077 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
9080 static int __init
md_init(void)
9084 md_wq
= alloc_workqueue("md", WQ_MEM_RECLAIM
, 0);
9088 md_misc_wq
= alloc_workqueue("md_misc", 0, 0);
9092 if ((ret
= register_blkdev(MD_MAJOR
, "md")) < 0)
9095 if ((ret
= register_blkdev(0, "mdp")) < 0)
9099 blk_register_region(MKDEV(MD_MAJOR
, 0), 512, THIS_MODULE
,
9100 md_probe
, NULL
, NULL
);
9101 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
9102 md_probe
, NULL
, NULL
);
9104 register_reboot_notifier(&md_notifier
);
9105 raid_table_header
= register_sysctl_table(raid_root_table
);
9111 unregister_blkdev(MD_MAJOR
, "md");
9113 destroy_workqueue(md_misc_wq
);
9115 destroy_workqueue(md_wq
);
9120 static void check_sb_changes(struct mddev
*mddev
, struct md_rdev
*rdev
)
9122 struct mdp_superblock_1
*sb
= page_address(rdev
->sb_page
);
9123 struct md_rdev
*rdev2
;
9125 char b
[BDEVNAME_SIZE
];
9128 * If size is changed in another node then we need to
9129 * do resize as well.
9131 if (mddev
->dev_sectors
!= le64_to_cpu(sb
->size
)) {
9132 ret
= mddev
->pers
->resize(mddev
, le64_to_cpu(sb
->size
));
9134 pr_info("md-cluster: resize failed\n");
9136 bitmap_update_sb(mddev
->bitmap
);
9139 /* Check for change of roles in the active devices */
9140 rdev_for_each(rdev2
, mddev
) {
9141 if (test_bit(Faulty
, &rdev2
->flags
))
9144 /* Check if the roles changed */
9145 role
= le16_to_cpu(sb
->dev_roles
[rdev2
->desc_nr
]);
9147 if (test_bit(Candidate
, &rdev2
->flags
)) {
9148 if (role
== 0xfffe) {
9149 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2
->bdev
,b
));
9150 md_kick_rdev_from_array(rdev2
);
9154 clear_bit(Candidate
, &rdev2
->flags
);
9157 if (role
!= rdev2
->raid_disk
) {
9159 if (rdev2
->raid_disk
== -1 && role
!= 0xffff) {
9160 rdev2
->saved_raid_disk
= role
;
9161 ret
= remove_and_add_spares(mddev
, rdev2
);
9162 pr_info("Activated spare: %s\n",
9163 bdevname(rdev2
->bdev
,b
));
9164 /* wakeup mddev->thread here, so array could
9165 * perform resync with the new activated disk */
9166 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
9167 md_wakeup_thread(mddev
->thread
);
9171 * We just want to do the minimum to mark the disk
9172 * as faulty. The recovery is performed by the
9173 * one who initiated the error.
9175 if ((role
== 0xfffe) || (role
== 0xfffd)) {
9176 md_error(mddev
, rdev2
);
9177 clear_bit(Blocked
, &rdev2
->flags
);
9182 if (mddev
->raid_disks
!= le32_to_cpu(sb
->raid_disks
))
9183 update_raid_disks(mddev
, le32_to_cpu(sb
->raid_disks
));
9185 /* Finally set the event to be up to date */
9186 mddev
->events
= le64_to_cpu(sb
->events
);
9189 static int read_rdev(struct mddev
*mddev
, struct md_rdev
*rdev
)
9192 struct page
*swapout
= rdev
->sb_page
;
9193 struct mdp_superblock_1
*sb
;
9195 /* Store the sb page of the rdev in the swapout temporary
9196 * variable in case we err in the future
9198 rdev
->sb_page
= NULL
;
9199 err
= alloc_disk_sb(rdev
);
9201 ClearPageUptodate(rdev
->sb_page
);
9202 rdev
->sb_loaded
= 0;
9203 err
= super_types
[mddev
->major_version
].
9204 load_super(rdev
, NULL
, mddev
->minor_version
);
9207 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9208 __func__
, __LINE__
, rdev
->desc_nr
, err
);
9210 put_page(rdev
->sb_page
);
9211 rdev
->sb_page
= swapout
;
9212 rdev
->sb_loaded
= 1;
9216 sb
= page_address(rdev
->sb_page
);
9217 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9221 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RECOVERY_OFFSET
))
9222 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
9224 /* The other node finished recovery, call spare_active to set
9225 * device In_sync and mddev->degraded
9227 if (rdev
->recovery_offset
== MaxSector
&&
9228 !test_bit(In_sync
, &rdev
->flags
) &&
9229 mddev
->pers
->spare_active(mddev
))
9230 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
9236 void md_reload_sb(struct mddev
*mddev
, int nr
)
9238 struct md_rdev
*rdev
;
9242 rdev_for_each_rcu(rdev
, mddev
) {
9243 if (rdev
->desc_nr
== nr
)
9247 if (!rdev
|| rdev
->desc_nr
!= nr
) {
9248 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__
, __LINE__
, nr
);
9252 err
= read_rdev(mddev
, rdev
);
9256 check_sb_changes(mddev
, rdev
);
9258 /* Read all rdev's to update recovery_offset */
9259 rdev_for_each_rcu(rdev
, mddev
)
9260 read_rdev(mddev
, rdev
);
9262 EXPORT_SYMBOL(md_reload_sb
);
9267 * Searches all registered partitions for autorun RAID arrays
9271 static DEFINE_MUTEX(detected_devices_mutex
);
9272 static LIST_HEAD(all_detected_devices
);
9273 struct detected_devices_node
{
9274 struct list_head list
;
9278 void md_autodetect_dev(dev_t dev
)
9280 struct detected_devices_node
*node_detected_dev
;
9282 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
9283 if (node_detected_dev
) {
9284 node_detected_dev
->dev
= dev
;
9285 mutex_lock(&detected_devices_mutex
);
9286 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
9287 mutex_unlock(&detected_devices_mutex
);
9291 static void autostart_arrays(int part
)
9293 struct md_rdev
*rdev
;
9294 struct detected_devices_node
*node_detected_dev
;
9296 int i_scanned
, i_passed
;
9301 pr_info("md: Autodetecting RAID arrays.\n");
9303 mutex_lock(&detected_devices_mutex
);
9304 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
9306 node_detected_dev
= list_entry(all_detected_devices
.next
,
9307 struct detected_devices_node
, list
);
9308 list_del(&node_detected_dev
->list
);
9309 dev
= node_detected_dev
->dev
;
9310 kfree(node_detected_dev
);
9311 mutex_unlock(&detected_devices_mutex
);
9312 rdev
= md_import_device(dev
,0, 90);
9313 mutex_lock(&detected_devices_mutex
);
9317 if (test_bit(Faulty
, &rdev
->flags
))
9320 set_bit(AutoDetected
, &rdev
->flags
);
9321 list_add(&rdev
->same_set
, &pending_raid_disks
);
9324 mutex_unlock(&detected_devices_mutex
);
9326 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned
, i_passed
);
9328 autorun_devices(part
);
9331 #endif /* !MODULE */
9333 static __exit
void md_exit(void)
9335 struct mddev
*mddev
;
9336 struct list_head
*tmp
;
9339 blk_unregister_region(MKDEV(MD_MAJOR
,0), 512);
9340 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
9342 unregister_blkdev(MD_MAJOR
,"md");
9343 unregister_blkdev(mdp_major
, "mdp");
9344 unregister_reboot_notifier(&md_notifier
);
9345 unregister_sysctl_table(raid_table_header
);
9347 /* We cannot unload the modules while some process is
9348 * waiting for us in select() or poll() - wake them up
9351 while (waitqueue_active(&md_event_waiters
)) {
9352 /* not safe to leave yet */
9353 wake_up(&md_event_waiters
);
9357 remove_proc_entry("mdstat", NULL
);
9359 for_each_mddev(mddev
, tmp
) {
9360 export_array(mddev
);
9362 mddev
->hold_active
= 0;
9364 * for_each_mddev() will call mddev_put() at the end of each
9365 * iteration. As the mddev is now fully clear, this will
9366 * schedule the mddev for destruction by a workqueue, and the
9367 * destroy_workqueue() below will wait for that to complete.
9370 destroy_workqueue(md_misc_wq
);
9371 destroy_workqueue(md_wq
);
9374 subsys_initcall(md_init
);
9375 module_exit(md_exit
)
9377 static int get_ro(char *buffer
, const struct kernel_param
*kp
)
9379 return sprintf(buffer
, "%d", start_readonly
);
9381 static int set_ro(const char *val
, const struct kernel_param
*kp
)
9383 return kstrtouint(val
, 10, (unsigned int *)&start_readonly
);
9386 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
9387 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
9388 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
9389 module_param(create_on_open
, bool, S_IRUSR
|S_IWUSR
);
9391 MODULE_LICENSE("GPL");
9392 MODULE_DESCRIPTION("MD RAID framework");
9394 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);