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 static struct md_rdev
*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
)
725 static struct md_personality
*find_pers(int level
, char *clevel
)
727 struct md_personality
*pers
;
728 list_for_each_entry(pers
, &pers_list
, list
) {
729 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
731 if (strcmp(pers
->name
, clevel
)==0)
737 /* return the offset of the super block in 512byte sectors */
738 static inline sector_t
calc_dev_sboffset(struct md_rdev
*rdev
)
740 sector_t num_sectors
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
741 return MD_NEW_SIZE_SECTORS(num_sectors
);
744 static int alloc_disk_sb(struct md_rdev
*rdev
)
746 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
752 void md_rdev_clear(struct md_rdev
*rdev
)
755 put_page(rdev
->sb_page
);
757 rdev
->sb_page
= NULL
;
762 put_page(rdev
->bb_page
);
763 rdev
->bb_page
= NULL
;
765 badblocks_exit(&rdev
->badblocks
);
767 EXPORT_SYMBOL_GPL(md_rdev_clear
);
769 static void super_written(struct bio
*bio
)
771 struct md_rdev
*rdev
= bio
->bi_private
;
772 struct mddev
*mddev
= rdev
->mddev
;
774 if (bio
->bi_status
) {
775 pr_err("md: super_written gets error=%d\n", bio
->bi_status
);
776 md_error(mddev
, rdev
);
777 if (!test_bit(Faulty
, &rdev
->flags
)
778 && (bio
->bi_opf
& MD_FAILFAST
)) {
779 set_bit(MD_SB_NEED_REWRITE
, &mddev
->sb_flags
);
780 set_bit(LastDev
, &rdev
->flags
);
783 clear_bit(LastDev
, &rdev
->flags
);
785 if (atomic_dec_and_test(&mddev
->pending_writes
))
786 wake_up(&mddev
->sb_wait
);
787 rdev_dec_pending(rdev
, mddev
);
791 void md_super_write(struct mddev
*mddev
, struct md_rdev
*rdev
,
792 sector_t sector
, int size
, struct page
*page
)
794 /* write first size bytes of page to sector of rdev
795 * Increment mddev->pending_writes before returning
796 * and decrement it on completion, waking up sb_wait
797 * if zero is reached.
798 * If an error occurred, call md_error
803 if (test_bit(Faulty
, &rdev
->flags
))
806 bio
= md_bio_alloc_sync(mddev
);
808 atomic_inc(&rdev
->nr_pending
);
810 bio_set_dev(bio
, rdev
->meta_bdev
? rdev
->meta_bdev
: rdev
->bdev
);
811 bio
->bi_iter
.bi_sector
= sector
;
812 bio_add_page(bio
, page
, size
, 0);
813 bio
->bi_private
= rdev
;
814 bio
->bi_end_io
= super_written
;
816 if (test_bit(MD_FAILFAST_SUPPORTED
, &mddev
->flags
) &&
817 test_bit(FailFast
, &rdev
->flags
) &&
818 !test_bit(LastDev
, &rdev
->flags
))
820 bio
->bi_opf
= REQ_OP_WRITE
| REQ_SYNC
| REQ_PREFLUSH
| REQ_FUA
| ff
;
822 atomic_inc(&mddev
->pending_writes
);
826 int md_super_wait(struct mddev
*mddev
)
828 /* wait for all superblock writes that were scheduled to complete */
829 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->pending_writes
)==0);
830 if (test_and_clear_bit(MD_SB_NEED_REWRITE
, &mddev
->sb_flags
))
835 int sync_page_io(struct md_rdev
*rdev
, sector_t sector
, int size
,
836 struct page
*page
, int op
, int op_flags
, bool metadata_op
)
838 struct bio
*bio
= md_bio_alloc_sync(rdev
->mddev
);
841 if (metadata_op
&& rdev
->meta_bdev
)
842 bio_set_dev(bio
, rdev
->meta_bdev
);
844 bio_set_dev(bio
, rdev
->bdev
);
845 bio_set_op_attrs(bio
, op
, op_flags
);
847 bio
->bi_iter
.bi_sector
= sector
+ rdev
->sb_start
;
848 else if (rdev
->mddev
->reshape_position
!= MaxSector
&&
849 (rdev
->mddev
->reshape_backwards
==
850 (sector
>= rdev
->mddev
->reshape_position
)))
851 bio
->bi_iter
.bi_sector
= sector
+ rdev
->new_data_offset
;
853 bio
->bi_iter
.bi_sector
= sector
+ rdev
->data_offset
;
854 bio_add_page(bio
, page
, size
, 0);
856 submit_bio_wait(bio
);
858 ret
= !bio
->bi_status
;
862 EXPORT_SYMBOL_GPL(sync_page_io
);
864 static int read_disk_sb(struct md_rdev
*rdev
, int size
)
866 char b
[BDEVNAME_SIZE
];
871 if (!sync_page_io(rdev
, 0, size
, rdev
->sb_page
, REQ_OP_READ
, 0, true))
877 pr_err("md: disabled device %s, could not read superblock.\n",
878 bdevname(rdev
->bdev
,b
));
882 static int md_uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
884 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
885 sb1
->set_uuid1
== sb2
->set_uuid1
&&
886 sb1
->set_uuid2
== sb2
->set_uuid2
&&
887 sb1
->set_uuid3
== sb2
->set_uuid3
;
890 static int md_sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
893 mdp_super_t
*tmp1
, *tmp2
;
895 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
896 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
898 if (!tmp1
|| !tmp2
) {
907 * nr_disks is not constant
912 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
919 static u32
md_csum_fold(u32 csum
)
921 csum
= (csum
& 0xffff) + (csum
>> 16);
922 return (csum
& 0xffff) + (csum
>> 16);
925 static unsigned int calc_sb_csum(mdp_super_t
*sb
)
928 u32
*sb32
= (u32
*)sb
;
930 unsigned int disk_csum
, csum
;
932 disk_csum
= sb
->sb_csum
;
935 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
937 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
940 /* This used to use csum_partial, which was wrong for several
941 * reasons including that different results are returned on
942 * different architectures. It isn't critical that we get exactly
943 * the same return value as before (we always csum_fold before
944 * testing, and that removes any differences). However as we
945 * know that csum_partial always returned a 16bit value on
946 * alphas, do a fold to maximise conformity to previous behaviour.
948 sb
->sb_csum
= md_csum_fold(disk_csum
);
950 sb
->sb_csum
= disk_csum
;
956 * Handle superblock details.
957 * We want to be able to handle multiple superblock formats
958 * so we have a common interface to them all, and an array of
959 * different handlers.
960 * We rely on user-space to write the initial superblock, and support
961 * reading and updating of superblocks.
962 * Interface methods are:
963 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
964 * loads and validates a superblock on dev.
965 * if refdev != NULL, compare superblocks on both devices
967 * 0 - dev has a superblock that is compatible with refdev
968 * 1 - dev has a superblock that is compatible and newer than refdev
969 * so dev should be used as the refdev in future
970 * -EINVAL superblock incompatible or invalid
971 * -othererror e.g. -EIO
973 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
974 * Verify that dev is acceptable into mddev.
975 * The first time, mddev->raid_disks will be 0, and data from
976 * dev should be merged in. Subsequent calls check that dev
977 * is new enough. Return 0 or -EINVAL
979 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
980 * Update the superblock for rdev with data in mddev
981 * This does not write to disc.
987 struct module
*owner
;
988 int (*load_super
)(struct md_rdev
*rdev
,
989 struct md_rdev
*refdev
,
991 int (*validate_super
)(struct mddev
*mddev
,
992 struct md_rdev
*rdev
);
993 void (*sync_super
)(struct mddev
*mddev
,
994 struct md_rdev
*rdev
);
995 unsigned long long (*rdev_size_change
)(struct md_rdev
*rdev
,
996 sector_t num_sectors
);
997 int (*allow_new_offset
)(struct md_rdev
*rdev
,
998 unsigned long long new_offset
);
1002 * Check that the given mddev has no bitmap.
1004 * This function is called from the run method of all personalities that do not
1005 * support bitmaps. It prints an error message and returns non-zero if mddev
1006 * has a bitmap. Otherwise, it returns 0.
1009 int md_check_no_bitmap(struct mddev
*mddev
)
1011 if (!mddev
->bitmap_info
.file
&& !mddev
->bitmap_info
.offset
)
1013 pr_warn("%s: bitmaps are not supported for %s\n",
1014 mdname(mddev
), mddev
->pers
->name
);
1017 EXPORT_SYMBOL(md_check_no_bitmap
);
1020 * load_super for 0.90.0
1022 static int super_90_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
1024 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1029 * Calculate the position of the superblock (512byte sectors),
1030 * it's at the end of the disk.
1032 * It also happens to be a multiple of 4Kb.
1034 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1036 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
1042 bdevname(rdev
->bdev
, b
);
1043 sb
= page_address(rdev
->sb_page
);
1045 if (sb
->md_magic
!= MD_SB_MAGIC
) {
1046 pr_warn("md: invalid raid superblock magic on %s\n", b
);
1050 if (sb
->major_version
!= 0 ||
1051 sb
->minor_version
< 90 ||
1052 sb
->minor_version
> 91) {
1053 pr_warn("Bad version number %d.%d on %s\n",
1054 sb
->major_version
, sb
->minor_version
, b
);
1058 if (sb
->raid_disks
<= 0)
1061 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
1062 pr_warn("md: invalid superblock checksum on %s\n", b
);
1066 rdev
->preferred_minor
= sb
->md_minor
;
1067 rdev
->data_offset
= 0;
1068 rdev
->new_data_offset
= 0;
1069 rdev
->sb_size
= MD_SB_BYTES
;
1070 rdev
->badblocks
.shift
= -1;
1072 if (sb
->level
== LEVEL_MULTIPATH
)
1075 rdev
->desc_nr
= sb
->this_disk
.number
;
1081 mdp_super_t
*refsb
= page_address(refdev
->sb_page
);
1082 if (!md_uuid_equal(refsb
, sb
)) {
1083 pr_warn("md: %s has different UUID to %s\n",
1084 b
, bdevname(refdev
->bdev
,b2
));
1087 if (!md_sb_equal(refsb
, sb
)) {
1088 pr_warn("md: %s has same UUID but different superblock to %s\n",
1089 b
, bdevname(refdev
->bdev
, b2
));
1093 ev2
= md_event(refsb
);
1099 rdev
->sectors
= rdev
->sb_start
;
1100 /* Limit to 4TB as metadata cannot record more than that.
1101 * (not needed for Linear and RAID0 as metadata doesn't
1104 if (IS_ENABLED(CONFIG_LBDAF
) && (u64
)rdev
->sectors
>= (2ULL << 32) &&
1106 rdev
->sectors
= (sector_t
)(2ULL << 32) - 2;
1108 if (rdev
->sectors
< ((sector_t
)sb
->size
) * 2 && sb
->level
>= 1)
1109 /* "this cannot possibly happen" ... */
1117 * validate_super for 0.90.0
1119 static int super_90_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
1122 mdp_super_t
*sb
= page_address(rdev
->sb_page
);
1123 __u64 ev1
= md_event(sb
);
1125 rdev
->raid_disk
= -1;
1126 clear_bit(Faulty
, &rdev
->flags
);
1127 clear_bit(In_sync
, &rdev
->flags
);
1128 clear_bit(Bitmap_sync
, &rdev
->flags
);
1129 clear_bit(WriteMostly
, &rdev
->flags
);
1131 if (mddev
->raid_disks
== 0) {
1132 mddev
->major_version
= 0;
1133 mddev
->minor_version
= sb
->minor_version
;
1134 mddev
->patch_version
= sb
->patch_version
;
1135 mddev
->external
= 0;
1136 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
1137 mddev
->ctime
= sb
->ctime
;
1138 mddev
->utime
= sb
->utime
;
1139 mddev
->level
= sb
->level
;
1140 mddev
->clevel
[0] = 0;
1141 mddev
->layout
= sb
->layout
;
1142 mddev
->raid_disks
= sb
->raid_disks
;
1143 mddev
->dev_sectors
= ((sector_t
)sb
->size
) * 2;
1144 mddev
->events
= ev1
;
1145 mddev
->bitmap_info
.offset
= 0;
1146 mddev
->bitmap_info
.space
= 0;
1147 /* bitmap can use 60 K after the 4K superblocks */
1148 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
1149 mddev
->bitmap_info
.default_space
= 64*2 - (MD_SB_BYTES
>> 9);
1150 mddev
->reshape_backwards
= 0;
1152 if (mddev
->minor_version
>= 91) {
1153 mddev
->reshape_position
= sb
->reshape_position
;
1154 mddev
->delta_disks
= sb
->delta_disks
;
1155 mddev
->new_level
= sb
->new_level
;
1156 mddev
->new_layout
= sb
->new_layout
;
1157 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
1158 if (mddev
->delta_disks
< 0)
1159 mddev
->reshape_backwards
= 1;
1161 mddev
->reshape_position
= MaxSector
;
1162 mddev
->delta_disks
= 0;
1163 mddev
->new_level
= mddev
->level
;
1164 mddev
->new_layout
= mddev
->layout
;
1165 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1168 if (sb
->state
& (1<<MD_SB_CLEAN
))
1169 mddev
->recovery_cp
= MaxSector
;
1171 if (sb
->events_hi
== sb
->cp_events_hi
&&
1172 sb
->events_lo
== sb
->cp_events_lo
) {
1173 mddev
->recovery_cp
= sb
->recovery_cp
;
1175 mddev
->recovery_cp
= 0;
1178 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
1179 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
1180 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
1181 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
1183 mddev
->max_disks
= MD_SB_DISKS
;
1185 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
1186 mddev
->bitmap_info
.file
== NULL
) {
1187 mddev
->bitmap_info
.offset
=
1188 mddev
->bitmap_info
.default_offset
;
1189 mddev
->bitmap_info
.space
=
1190 mddev
->bitmap_info
.default_space
;
1193 } else if (mddev
->pers
== NULL
) {
1194 /* Insist on good event counter while assembling, except
1195 * for spares (which don't need an event count) */
1197 if (sb
->disks
[rdev
->desc_nr
].state
& (
1198 (1<<MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
)))
1199 if (ev1
< mddev
->events
)
1201 } else if (mddev
->bitmap
) {
1202 /* if adding to array with a bitmap, then we can accept an
1203 * older device ... but not too old.
1205 if (ev1
< mddev
->bitmap
->events_cleared
)
1207 if (ev1
< mddev
->events
)
1208 set_bit(Bitmap_sync
, &rdev
->flags
);
1210 if (ev1
< mddev
->events
)
1211 /* just a hot-add of a new device, leave raid_disk at -1 */
1215 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1216 desc
= sb
->disks
+ rdev
->desc_nr
;
1218 if (desc
->state
& (1<<MD_DISK_FAULTY
))
1219 set_bit(Faulty
, &rdev
->flags
);
1220 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
1221 desc->raid_disk < mddev->raid_disks */) {
1222 set_bit(In_sync
, &rdev
->flags
);
1223 rdev
->raid_disk
= desc
->raid_disk
;
1224 rdev
->saved_raid_disk
= desc
->raid_disk
;
1225 } else if (desc
->state
& (1<<MD_DISK_ACTIVE
)) {
1226 /* active but not in sync implies recovery up to
1227 * reshape position. We don't know exactly where
1228 * that is, so set to zero for now */
1229 if (mddev
->minor_version
>= 91) {
1230 rdev
->recovery_offset
= 0;
1231 rdev
->raid_disk
= desc
->raid_disk
;
1234 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
1235 set_bit(WriteMostly
, &rdev
->flags
);
1236 if (desc
->state
& (1<<MD_DISK_FAILFAST
))
1237 set_bit(FailFast
, &rdev
->flags
);
1238 } else /* MULTIPATH are always insync */
1239 set_bit(In_sync
, &rdev
->flags
);
1244 * sync_super for 0.90.0
1246 static void super_90_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
1249 struct md_rdev
*rdev2
;
1250 int next_spare
= mddev
->raid_disks
;
1252 /* make rdev->sb match mddev data..
1255 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1256 * 3/ any empty disks < next_spare become removed
1258 * disks[0] gets initialised to REMOVED because
1259 * we cannot be sure from other fields if it has
1260 * been initialised or not.
1263 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
1265 rdev
->sb_size
= MD_SB_BYTES
;
1267 sb
= page_address(rdev
->sb_page
);
1269 memset(sb
, 0, sizeof(*sb
));
1271 sb
->md_magic
= MD_SB_MAGIC
;
1272 sb
->major_version
= mddev
->major_version
;
1273 sb
->patch_version
= mddev
->patch_version
;
1274 sb
->gvalid_words
= 0; /* ignored */
1275 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
1276 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
1277 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
1278 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
1280 sb
->ctime
= clamp_t(time64_t
, mddev
->ctime
, 0, U32_MAX
);
1281 sb
->level
= mddev
->level
;
1282 sb
->size
= mddev
->dev_sectors
/ 2;
1283 sb
->raid_disks
= mddev
->raid_disks
;
1284 sb
->md_minor
= mddev
->md_minor
;
1285 sb
->not_persistent
= 0;
1286 sb
->utime
= clamp_t(time64_t
, mddev
->utime
, 0, U32_MAX
);
1288 sb
->events_hi
= (mddev
->events
>>32);
1289 sb
->events_lo
= (u32
)mddev
->events
;
1291 if (mddev
->reshape_position
== MaxSector
)
1292 sb
->minor_version
= 90;
1294 sb
->minor_version
= 91;
1295 sb
->reshape_position
= mddev
->reshape_position
;
1296 sb
->new_level
= mddev
->new_level
;
1297 sb
->delta_disks
= mddev
->delta_disks
;
1298 sb
->new_layout
= mddev
->new_layout
;
1299 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1301 mddev
->minor_version
= sb
->minor_version
;
1304 sb
->recovery_cp
= mddev
->recovery_cp
;
1305 sb
->cp_events_hi
= (mddev
->events
>>32);
1306 sb
->cp_events_lo
= (u32
)mddev
->events
;
1307 if (mddev
->recovery_cp
== MaxSector
)
1308 sb
->state
= (1<< MD_SB_CLEAN
);
1310 sb
->recovery_cp
= 0;
1312 sb
->layout
= mddev
->layout
;
1313 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1315 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
)
1316 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1318 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1319 rdev_for_each(rdev2
, mddev
) {
1322 int is_active
= test_bit(In_sync
, &rdev2
->flags
);
1324 if (rdev2
->raid_disk
>= 0 &&
1325 sb
->minor_version
>= 91)
1326 /* we have nowhere to store the recovery_offset,
1327 * but if it is not below the reshape_position,
1328 * we can piggy-back on that.
1331 if (rdev2
->raid_disk
< 0 ||
1332 test_bit(Faulty
, &rdev2
->flags
))
1335 desc_nr
= rdev2
->raid_disk
;
1337 desc_nr
= next_spare
++;
1338 rdev2
->desc_nr
= desc_nr
;
1339 d
= &sb
->disks
[rdev2
->desc_nr
];
1341 d
->number
= rdev2
->desc_nr
;
1342 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1343 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1345 d
->raid_disk
= rdev2
->raid_disk
;
1347 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1348 if (test_bit(Faulty
, &rdev2
->flags
))
1349 d
->state
= (1<<MD_DISK_FAULTY
);
1350 else if (is_active
) {
1351 d
->state
= (1<<MD_DISK_ACTIVE
);
1352 if (test_bit(In_sync
, &rdev2
->flags
))
1353 d
->state
|= (1<<MD_DISK_SYNC
);
1361 if (test_bit(WriteMostly
, &rdev2
->flags
))
1362 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1363 if (test_bit(FailFast
, &rdev2
->flags
))
1364 d
->state
|= (1<<MD_DISK_FAILFAST
);
1366 /* now set the "removed" and "faulty" bits on any missing devices */
1367 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1368 mdp_disk_t
*d
= &sb
->disks
[i
];
1369 if (d
->state
== 0 && d
->number
== 0) {
1372 d
->state
= (1<<MD_DISK_REMOVED
);
1373 d
->state
|= (1<<MD_DISK_FAULTY
);
1377 sb
->nr_disks
= nr_disks
;
1378 sb
->active_disks
= active
;
1379 sb
->working_disks
= working
;
1380 sb
->failed_disks
= failed
;
1381 sb
->spare_disks
= spare
;
1383 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1384 sb
->sb_csum
= calc_sb_csum(sb
);
1388 * rdev_size_change for 0.90.0
1390 static unsigned long long
1391 super_90_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
1393 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1394 return 0; /* component must fit device */
1395 if (rdev
->mddev
->bitmap_info
.offset
)
1396 return 0; /* can't move bitmap */
1397 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1398 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1399 num_sectors
= rdev
->sb_start
;
1400 /* Limit to 4TB as metadata cannot record more than that.
1401 * 4TB == 2^32 KB, or 2*2^32 sectors.
1403 if (IS_ENABLED(CONFIG_LBDAF
) && (u64
)num_sectors
>= (2ULL << 32) &&
1404 rdev
->mddev
->level
>= 1)
1405 num_sectors
= (sector_t
)(2ULL << 32) - 2;
1407 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1409 } while (md_super_wait(rdev
->mddev
) < 0);
1414 super_90_allow_new_offset(struct md_rdev
*rdev
, unsigned long long new_offset
)
1416 /* non-zero offset changes not possible with v0.90 */
1417 return new_offset
== 0;
1421 * version 1 superblock
1424 static __le32
calc_sb_1_csum(struct mdp_superblock_1
*sb
)
1428 unsigned long long newcsum
;
1429 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1430 __le32
*isuper
= (__le32
*)sb
;
1432 disk_csum
= sb
->sb_csum
;
1435 for (; size
>= 4; size
-= 4)
1436 newcsum
+= le32_to_cpu(*isuper
++);
1439 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1441 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1442 sb
->sb_csum
= disk_csum
;
1443 return cpu_to_le32(csum
);
1446 static int super_1_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
1448 struct mdp_superblock_1
*sb
;
1452 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1456 * Calculate the position of the superblock in 512byte sectors.
1457 * It is always aligned to a 4K boundary and
1458 * depeding on minor_version, it can be:
1459 * 0: At least 8K, but less than 12K, from end of device
1460 * 1: At start of device
1461 * 2: 4K from start of device.
1463 switch(minor_version
) {
1465 sb_start
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1467 sb_start
&= ~(sector_t
)(4*2-1);
1478 rdev
->sb_start
= sb_start
;
1480 /* superblock is rarely larger than 1K, but it can be larger,
1481 * and it is safe to read 4k, so we do that
1483 ret
= read_disk_sb(rdev
, 4096);
1484 if (ret
) return ret
;
1486 sb
= page_address(rdev
->sb_page
);
1488 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1489 sb
->major_version
!= cpu_to_le32(1) ||
1490 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1491 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1492 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1495 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1496 pr_warn("md: invalid superblock checksum on %s\n",
1497 bdevname(rdev
->bdev
,b
));
1500 if (le64_to_cpu(sb
->data_size
) < 10) {
1501 pr_warn("md: data_size too small on %s\n",
1502 bdevname(rdev
->bdev
,b
));
1507 memcmp(sb
->pad3
, sb
->pad3
+1, sizeof(sb
->pad3
) - sizeof(sb
->pad3
[1])))
1508 /* Some padding is non-zero, might be a new feature */
1511 rdev
->preferred_minor
= 0xffff;
1512 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1513 rdev
->new_data_offset
= rdev
->data_offset
;
1514 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
) &&
1515 (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_NEW_OFFSET
))
1516 rdev
->new_data_offset
+= (s32
)le32_to_cpu(sb
->new_offset
);
1517 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1519 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1520 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1521 if (rdev
->sb_size
& bmask
)
1522 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1525 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1528 && rdev
->new_data_offset
< sb_start
+ (rdev
->sb_size
/512))
1531 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1534 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1536 if (!rdev
->bb_page
) {
1537 rdev
->bb_page
= alloc_page(GFP_KERNEL
);
1541 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BAD_BLOCKS
) &&
1542 rdev
->badblocks
.count
== 0) {
1543 /* need to load the bad block list.
1544 * Currently we limit it to one page.
1550 int sectors
= le16_to_cpu(sb
->bblog_size
);
1551 if (sectors
> (PAGE_SIZE
/ 512))
1553 offset
= le32_to_cpu(sb
->bblog_offset
);
1556 bb_sector
= (long long)offset
;
1557 if (!sync_page_io(rdev
, bb_sector
, sectors
<< 9,
1558 rdev
->bb_page
, REQ_OP_READ
, 0, true))
1560 bbp
= (u64
*)page_address(rdev
->bb_page
);
1561 rdev
->badblocks
.shift
= sb
->bblog_shift
;
1562 for (i
= 0 ; i
< (sectors
<< (9-3)) ; i
++, bbp
++) {
1563 u64 bb
= le64_to_cpu(*bbp
);
1564 int count
= bb
& (0x3ff);
1565 u64 sector
= bb
>> 10;
1566 sector
<<= sb
->bblog_shift
;
1567 count
<<= sb
->bblog_shift
;
1570 if (badblocks_set(&rdev
->badblocks
, sector
, count
, 1))
1573 } else if (sb
->bblog_offset
!= 0)
1574 rdev
->badblocks
.shift
= 0;
1576 if ((le32_to_cpu(sb
->feature_map
) &
1577 (MD_FEATURE_PPL
| MD_FEATURE_MULTIPLE_PPLS
))) {
1578 rdev
->ppl
.offset
= (__s16
)le16_to_cpu(sb
->ppl
.offset
);
1579 rdev
->ppl
.size
= le16_to_cpu(sb
->ppl
.size
);
1580 rdev
->ppl
.sector
= rdev
->sb_start
+ rdev
->ppl
.offset
;
1587 struct mdp_superblock_1
*refsb
= page_address(refdev
->sb_page
);
1589 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1590 sb
->level
!= refsb
->level
||
1591 sb
->layout
!= refsb
->layout
||
1592 sb
->chunksize
!= refsb
->chunksize
) {
1593 pr_warn("md: %s has strangely different superblock to %s\n",
1594 bdevname(rdev
->bdev
,b
),
1595 bdevname(refdev
->bdev
,b2
));
1598 ev1
= le64_to_cpu(sb
->events
);
1599 ev2
= le64_to_cpu(refsb
->events
);
1606 if (minor_version
) {
1607 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9);
1608 sectors
-= rdev
->data_offset
;
1610 sectors
= rdev
->sb_start
;
1611 if (sectors
< le64_to_cpu(sb
->data_size
))
1613 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1617 static int super_1_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
1619 struct mdp_superblock_1
*sb
= page_address(rdev
->sb_page
);
1620 __u64 ev1
= le64_to_cpu(sb
->events
);
1622 rdev
->raid_disk
= -1;
1623 clear_bit(Faulty
, &rdev
->flags
);
1624 clear_bit(In_sync
, &rdev
->flags
);
1625 clear_bit(Bitmap_sync
, &rdev
->flags
);
1626 clear_bit(WriteMostly
, &rdev
->flags
);
1628 if (mddev
->raid_disks
== 0) {
1629 mddev
->major_version
= 1;
1630 mddev
->patch_version
= 0;
1631 mddev
->external
= 0;
1632 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1633 mddev
->ctime
= le64_to_cpu(sb
->ctime
);
1634 mddev
->utime
= le64_to_cpu(sb
->utime
);
1635 mddev
->level
= le32_to_cpu(sb
->level
);
1636 mddev
->clevel
[0] = 0;
1637 mddev
->layout
= le32_to_cpu(sb
->layout
);
1638 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1639 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1640 mddev
->events
= ev1
;
1641 mddev
->bitmap_info
.offset
= 0;
1642 mddev
->bitmap_info
.space
= 0;
1643 /* Default location for bitmap is 1K after superblock
1644 * using 3K - total of 4K
1646 mddev
->bitmap_info
.default_offset
= 1024 >> 9;
1647 mddev
->bitmap_info
.default_space
= (4096-1024) >> 9;
1648 mddev
->reshape_backwards
= 0;
1650 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1651 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1653 mddev
->max_disks
= (4096-256)/2;
1655 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1656 mddev
->bitmap_info
.file
== NULL
) {
1657 mddev
->bitmap_info
.offset
=
1658 (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1659 /* Metadata doesn't record how much space is available.
1660 * For 1.0, we assume we can use up to the superblock
1661 * if before, else to 4K beyond superblock.
1662 * For others, assume no change is possible.
1664 if (mddev
->minor_version
> 0)
1665 mddev
->bitmap_info
.space
= 0;
1666 else if (mddev
->bitmap_info
.offset
> 0)
1667 mddev
->bitmap_info
.space
=
1668 8 - mddev
->bitmap_info
.offset
;
1670 mddev
->bitmap_info
.space
=
1671 -mddev
->bitmap_info
.offset
;
1674 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1675 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1676 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1677 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1678 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1679 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1680 if (mddev
->delta_disks
< 0 ||
1681 (mddev
->delta_disks
== 0 &&
1682 (le32_to_cpu(sb
->feature_map
)
1683 & MD_FEATURE_RESHAPE_BACKWARDS
)))
1684 mddev
->reshape_backwards
= 1;
1686 mddev
->reshape_position
= MaxSector
;
1687 mddev
->delta_disks
= 0;
1688 mddev
->new_level
= mddev
->level
;
1689 mddev
->new_layout
= mddev
->layout
;
1690 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1693 if (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_JOURNAL
)
1694 set_bit(MD_HAS_JOURNAL
, &mddev
->flags
);
1696 if (le32_to_cpu(sb
->feature_map
) &
1697 (MD_FEATURE_PPL
| MD_FEATURE_MULTIPLE_PPLS
)) {
1698 if (le32_to_cpu(sb
->feature_map
) &
1699 (MD_FEATURE_BITMAP_OFFSET
| MD_FEATURE_JOURNAL
))
1701 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_PPL
) &&
1702 (le32_to_cpu(sb
->feature_map
) &
1703 MD_FEATURE_MULTIPLE_PPLS
))
1705 set_bit(MD_HAS_PPL
, &mddev
->flags
);
1707 } else if (mddev
->pers
== NULL
) {
1708 /* Insist of good event counter while assembling, except for
1709 * spares (which don't need an event count) */
1711 if (rdev
->desc_nr
>= 0 &&
1712 rdev
->desc_nr
< le32_to_cpu(sb
->max_dev
) &&
1713 (le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) < MD_DISK_ROLE_MAX
||
1714 le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) == MD_DISK_ROLE_JOURNAL
))
1715 if (ev1
< mddev
->events
)
1717 } else if (mddev
->bitmap
) {
1718 /* If adding to array with a bitmap, then we can accept an
1719 * older device, but not too old.
1721 if (ev1
< mddev
->bitmap
->events_cleared
)
1723 if (ev1
< mddev
->events
)
1724 set_bit(Bitmap_sync
, &rdev
->flags
);
1726 if (ev1
< mddev
->events
)
1727 /* just a hot-add of a new device, leave raid_disk at -1 */
1730 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1732 if (rdev
->desc_nr
< 0 ||
1733 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1734 role
= MD_DISK_ROLE_SPARE
;
1737 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1739 case MD_DISK_ROLE_SPARE
: /* spare */
1741 case MD_DISK_ROLE_FAULTY
: /* faulty */
1742 set_bit(Faulty
, &rdev
->flags
);
1744 case MD_DISK_ROLE_JOURNAL
: /* journal device */
1745 if (!(le32_to_cpu(sb
->feature_map
) & MD_FEATURE_JOURNAL
)) {
1746 /* journal device without journal feature */
1747 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
1750 set_bit(Journal
, &rdev
->flags
);
1751 rdev
->journal_tail
= le64_to_cpu(sb
->journal_tail
);
1752 rdev
->raid_disk
= 0;
1755 rdev
->saved_raid_disk
= role
;
1756 if ((le32_to_cpu(sb
->feature_map
) &
1757 MD_FEATURE_RECOVERY_OFFSET
)) {
1758 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1759 if (!(le32_to_cpu(sb
->feature_map
) &
1760 MD_FEATURE_RECOVERY_BITMAP
))
1761 rdev
->saved_raid_disk
= -1;
1763 set_bit(In_sync
, &rdev
->flags
);
1764 rdev
->raid_disk
= role
;
1767 if (sb
->devflags
& WriteMostly1
)
1768 set_bit(WriteMostly
, &rdev
->flags
);
1769 if (sb
->devflags
& FailFast1
)
1770 set_bit(FailFast
, &rdev
->flags
);
1771 if (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_REPLACEMENT
)
1772 set_bit(Replacement
, &rdev
->flags
);
1773 } else /* MULTIPATH are always insync */
1774 set_bit(In_sync
, &rdev
->flags
);
1779 static void super_1_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
1781 struct mdp_superblock_1
*sb
;
1782 struct md_rdev
*rdev2
;
1784 /* make rdev->sb match mddev and rdev data. */
1786 sb
= page_address(rdev
->sb_page
);
1788 sb
->feature_map
= 0;
1790 sb
->recovery_offset
= cpu_to_le64(0);
1791 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1793 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1794 sb
->events
= cpu_to_le64(mddev
->events
);
1796 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1797 else if (test_bit(MD_JOURNAL_CLEAN
, &mddev
->flags
))
1798 sb
->resync_offset
= cpu_to_le64(MaxSector
);
1800 sb
->resync_offset
= cpu_to_le64(0);
1802 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1804 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1805 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
1806 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
1807 sb
->level
= cpu_to_le32(mddev
->level
);
1808 sb
->layout
= cpu_to_le32(mddev
->layout
);
1809 if (test_bit(FailFast
, &rdev
->flags
))
1810 sb
->devflags
|= FailFast1
;
1812 sb
->devflags
&= ~FailFast1
;
1814 if (test_bit(WriteMostly
, &rdev
->flags
))
1815 sb
->devflags
|= WriteMostly1
;
1817 sb
->devflags
&= ~WriteMostly1
;
1818 sb
->data_offset
= cpu_to_le64(rdev
->data_offset
);
1819 sb
->data_size
= cpu_to_le64(rdev
->sectors
);
1821 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
) {
1822 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_info
.offset
);
1823 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1826 if (rdev
->raid_disk
>= 0 && !test_bit(Journal
, &rdev
->flags
) &&
1827 !test_bit(In_sync
, &rdev
->flags
)) {
1829 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1830 sb
->recovery_offset
=
1831 cpu_to_le64(rdev
->recovery_offset
);
1832 if (rdev
->saved_raid_disk
>= 0 && mddev
->bitmap
)
1834 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP
);
1836 /* Note: recovery_offset and journal_tail share space */
1837 if (test_bit(Journal
, &rdev
->flags
))
1838 sb
->journal_tail
= cpu_to_le64(rdev
->journal_tail
);
1839 if (test_bit(Replacement
, &rdev
->flags
))
1841 cpu_to_le32(MD_FEATURE_REPLACEMENT
);
1843 if (mddev
->reshape_position
!= MaxSector
) {
1844 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1845 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1846 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1847 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1848 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1849 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
1850 if (mddev
->delta_disks
== 0 &&
1851 mddev
->reshape_backwards
)
1853 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS
);
1854 if (rdev
->new_data_offset
!= rdev
->data_offset
) {
1856 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET
);
1857 sb
->new_offset
= cpu_to_le32((__u32
)(rdev
->new_data_offset
1858 - rdev
->data_offset
));
1862 if (mddev_is_clustered(mddev
))
1863 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_CLUSTERED
);
1865 if (rdev
->badblocks
.count
== 0)
1866 /* Nothing to do for bad blocks*/ ;
1867 else if (sb
->bblog_offset
== 0)
1868 /* Cannot record bad blocks on this device */
1869 md_error(mddev
, rdev
);
1871 struct badblocks
*bb
= &rdev
->badblocks
;
1872 u64
*bbp
= (u64
*)page_address(rdev
->bb_page
);
1874 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_BAD_BLOCKS
);
1879 seq
= read_seqbegin(&bb
->lock
);
1881 memset(bbp
, 0xff, PAGE_SIZE
);
1883 for (i
= 0 ; i
< bb
->count
; i
++) {
1884 u64 internal_bb
= p
[i
];
1885 u64 store_bb
= ((BB_OFFSET(internal_bb
) << 10)
1886 | BB_LEN(internal_bb
));
1887 bbp
[i
] = cpu_to_le64(store_bb
);
1890 if (read_seqretry(&bb
->lock
, seq
))
1893 bb
->sector
= (rdev
->sb_start
+
1894 (int)le32_to_cpu(sb
->bblog_offset
));
1895 bb
->size
= le16_to_cpu(sb
->bblog_size
);
1900 rdev_for_each(rdev2
, mddev
)
1901 if (rdev2
->desc_nr
+1 > max_dev
)
1902 max_dev
= rdev2
->desc_nr
+1;
1904 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
1906 sb
->max_dev
= cpu_to_le32(max_dev
);
1907 rdev
->sb_size
= max_dev
* 2 + 256;
1908 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1909 if (rdev
->sb_size
& bmask
)
1910 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1912 max_dev
= le32_to_cpu(sb
->max_dev
);
1914 for (i
=0; i
<max_dev
;i
++)
1915 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_SPARE
);
1917 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
))
1918 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_JOURNAL
);
1920 if (test_bit(MD_HAS_PPL
, &mddev
->flags
)) {
1921 if (test_bit(MD_HAS_MULTIPLE_PPLS
, &mddev
->flags
))
1923 cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS
);
1925 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_PPL
);
1926 sb
->ppl
.offset
= cpu_to_le16(rdev
->ppl
.offset
);
1927 sb
->ppl
.size
= cpu_to_le16(rdev
->ppl
.size
);
1930 rdev_for_each(rdev2
, mddev
) {
1932 if (test_bit(Faulty
, &rdev2
->flags
))
1933 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_FAULTY
);
1934 else if (test_bit(In_sync
, &rdev2
->flags
))
1935 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1936 else if (test_bit(Journal
, &rdev2
->flags
))
1937 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_JOURNAL
);
1938 else if (rdev2
->raid_disk
>= 0)
1939 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1941 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_SPARE
);
1944 sb
->sb_csum
= calc_sb_1_csum(sb
);
1947 static unsigned long long
1948 super_1_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
1950 struct mdp_superblock_1
*sb
;
1951 sector_t max_sectors
;
1952 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1953 return 0; /* component must fit device */
1954 if (rdev
->data_offset
!= rdev
->new_data_offset
)
1955 return 0; /* too confusing */
1956 if (rdev
->sb_start
< rdev
->data_offset
) {
1957 /* minor versions 1 and 2; superblock before data */
1958 max_sectors
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1959 max_sectors
-= rdev
->data_offset
;
1960 if (!num_sectors
|| num_sectors
> max_sectors
)
1961 num_sectors
= max_sectors
;
1962 } else if (rdev
->mddev
->bitmap_info
.offset
) {
1963 /* minor version 0 with bitmap we can't move */
1966 /* minor version 0; superblock after data */
1968 sb_start
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) - 8*2;
1969 sb_start
&= ~(sector_t
)(4*2 - 1);
1970 max_sectors
= rdev
->sectors
+ sb_start
- rdev
->sb_start
;
1971 if (!num_sectors
|| num_sectors
> max_sectors
)
1972 num_sectors
= max_sectors
;
1973 rdev
->sb_start
= sb_start
;
1975 sb
= page_address(rdev
->sb_page
);
1976 sb
->data_size
= cpu_to_le64(num_sectors
);
1977 sb
->super_offset
= cpu_to_le64(rdev
->sb_start
);
1978 sb
->sb_csum
= calc_sb_1_csum(sb
);
1980 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1982 } while (md_super_wait(rdev
->mddev
) < 0);
1988 super_1_allow_new_offset(struct md_rdev
*rdev
,
1989 unsigned long long new_offset
)
1991 /* All necessary checks on new >= old have been done */
1992 struct bitmap
*bitmap
;
1993 if (new_offset
>= rdev
->data_offset
)
1996 /* with 1.0 metadata, there is no metadata to tread on
1997 * so we can always move back */
1998 if (rdev
->mddev
->minor_version
== 0)
2001 /* otherwise we must be sure not to step on
2002 * any metadata, so stay:
2003 * 36K beyond start of superblock
2004 * beyond end of badblocks
2005 * beyond write-intent bitmap
2007 if (rdev
->sb_start
+ (32+4)*2 > new_offset
)
2009 bitmap
= rdev
->mddev
->bitmap
;
2010 if (bitmap
&& !rdev
->mddev
->bitmap_info
.file
&&
2011 rdev
->sb_start
+ rdev
->mddev
->bitmap_info
.offset
+
2012 bitmap
->storage
.file_pages
* (PAGE_SIZE
>>9) > new_offset
)
2014 if (rdev
->badblocks
.sector
+ rdev
->badblocks
.size
> new_offset
)
2020 static struct super_type super_types
[] = {
2023 .owner
= THIS_MODULE
,
2024 .load_super
= super_90_load
,
2025 .validate_super
= super_90_validate
,
2026 .sync_super
= super_90_sync
,
2027 .rdev_size_change
= super_90_rdev_size_change
,
2028 .allow_new_offset
= super_90_allow_new_offset
,
2032 .owner
= THIS_MODULE
,
2033 .load_super
= super_1_load
,
2034 .validate_super
= super_1_validate
,
2035 .sync_super
= super_1_sync
,
2036 .rdev_size_change
= super_1_rdev_size_change
,
2037 .allow_new_offset
= super_1_allow_new_offset
,
2041 static void sync_super(struct mddev
*mddev
, struct md_rdev
*rdev
)
2043 if (mddev
->sync_super
) {
2044 mddev
->sync_super(mddev
, rdev
);
2048 BUG_ON(mddev
->major_version
>= ARRAY_SIZE(super_types
));
2050 super_types
[mddev
->major_version
].sync_super(mddev
, rdev
);
2053 static int match_mddev_units(struct mddev
*mddev1
, struct mddev
*mddev2
)
2055 struct md_rdev
*rdev
, *rdev2
;
2058 rdev_for_each_rcu(rdev
, mddev1
) {
2059 if (test_bit(Faulty
, &rdev
->flags
) ||
2060 test_bit(Journal
, &rdev
->flags
) ||
2061 rdev
->raid_disk
== -1)
2063 rdev_for_each_rcu(rdev2
, mddev2
) {
2064 if (test_bit(Faulty
, &rdev2
->flags
) ||
2065 test_bit(Journal
, &rdev2
->flags
) ||
2066 rdev2
->raid_disk
== -1)
2068 if (rdev
->bdev
->bd_contains
==
2069 rdev2
->bdev
->bd_contains
) {
2079 static LIST_HEAD(pending_raid_disks
);
2082 * Try to register data integrity profile for an mddev
2084 * This is called when an array is started and after a disk has been kicked
2085 * from the array. It only succeeds if all working and active component devices
2086 * are integrity capable with matching profiles.
2088 int md_integrity_register(struct mddev
*mddev
)
2090 struct md_rdev
*rdev
, *reference
= NULL
;
2092 if (list_empty(&mddev
->disks
))
2093 return 0; /* nothing to do */
2094 if (!mddev
->gendisk
|| blk_get_integrity(mddev
->gendisk
))
2095 return 0; /* shouldn't register, or already is */
2096 rdev_for_each(rdev
, mddev
) {
2097 /* skip spares and non-functional disks */
2098 if (test_bit(Faulty
, &rdev
->flags
))
2100 if (rdev
->raid_disk
< 0)
2103 /* Use the first rdev as the reference */
2107 /* does this rdev's profile match the reference profile? */
2108 if (blk_integrity_compare(reference
->bdev
->bd_disk
,
2109 rdev
->bdev
->bd_disk
) < 0)
2112 if (!reference
|| !bdev_get_integrity(reference
->bdev
))
2115 * All component devices are integrity capable and have matching
2116 * profiles, register the common profile for the md device.
2118 blk_integrity_register(mddev
->gendisk
,
2119 bdev_get_integrity(reference
->bdev
));
2121 pr_debug("md: data integrity enabled on %s\n", mdname(mddev
));
2122 if (bioset_integrity_create(mddev
->bio_set
, BIO_POOL_SIZE
)) {
2123 pr_err("md: failed to create integrity pool for %s\n",
2129 EXPORT_SYMBOL(md_integrity_register
);
2132 * Attempt to add an rdev, but only if it is consistent with the current
2135 int md_integrity_add_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
2137 struct blk_integrity
*bi_rdev
;
2138 struct blk_integrity
*bi_mddev
;
2139 char name
[BDEVNAME_SIZE
];
2141 if (!mddev
->gendisk
)
2144 bi_rdev
= bdev_get_integrity(rdev
->bdev
);
2145 bi_mddev
= blk_get_integrity(mddev
->gendisk
);
2147 if (!bi_mddev
) /* nothing to do */
2150 if (blk_integrity_compare(mddev
->gendisk
, rdev
->bdev
->bd_disk
) != 0) {
2151 pr_err("%s: incompatible integrity profile for %s\n",
2152 mdname(mddev
), bdevname(rdev
->bdev
, name
));
2158 EXPORT_SYMBOL(md_integrity_add_rdev
);
2160 static int bind_rdev_to_array(struct md_rdev
*rdev
, struct mddev
*mddev
)
2162 char b
[BDEVNAME_SIZE
];
2166 /* prevent duplicates */
2167 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
2170 if ((bdev_read_only(rdev
->bdev
) || bdev_read_only(rdev
->meta_bdev
)) &&
2174 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2175 if (!test_bit(Journal
, &rdev
->flags
) &&
2177 (mddev
->dev_sectors
== 0 || rdev
->sectors
< mddev
->dev_sectors
)) {
2179 /* Cannot change size, so fail
2180 * If mddev->level <= 0, then we don't care
2181 * about aligning sizes (e.g. linear)
2183 if (mddev
->level
> 0)
2186 mddev
->dev_sectors
= rdev
->sectors
;
2189 /* Verify rdev->desc_nr is unique.
2190 * If it is -1, assign a free number, else
2191 * check number is not in use
2194 if (rdev
->desc_nr
< 0) {
2197 choice
= mddev
->raid_disks
;
2198 while (md_find_rdev_nr_rcu(mddev
, choice
))
2200 rdev
->desc_nr
= choice
;
2202 if (md_find_rdev_nr_rcu(mddev
, rdev
->desc_nr
)) {
2208 if (!test_bit(Journal
, &rdev
->flags
) &&
2209 mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
2210 pr_warn("md: %s: array is limited to %d devices\n",
2211 mdname(mddev
), mddev
->max_disks
);
2214 bdevname(rdev
->bdev
,b
);
2215 strreplace(b
, '/', '!');
2217 rdev
->mddev
= mddev
;
2218 pr_debug("md: bind<%s>\n", b
);
2220 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
2223 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
2224 if (sysfs_create_link(&rdev
->kobj
, ko
, "block"))
2225 /* failure here is OK */;
2226 rdev
->sysfs_state
= sysfs_get_dirent_safe(rdev
->kobj
.sd
, "state");
2228 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
2229 bd_link_disk_holder(rdev
->bdev
, mddev
->gendisk
);
2231 /* May as well allow recovery to be retried once */
2232 mddev
->recovery_disabled
++;
2237 pr_warn("md: failed to register dev-%s for %s\n",
2242 static void md_delayed_delete(struct work_struct
*ws
)
2244 struct md_rdev
*rdev
= container_of(ws
, struct md_rdev
, del_work
);
2245 kobject_del(&rdev
->kobj
);
2246 kobject_put(&rdev
->kobj
);
2249 static void unbind_rdev_from_array(struct md_rdev
*rdev
)
2251 char b
[BDEVNAME_SIZE
];
2253 bd_unlink_disk_holder(rdev
->bdev
, rdev
->mddev
->gendisk
);
2254 list_del_rcu(&rdev
->same_set
);
2255 pr_debug("md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
2257 sysfs_remove_link(&rdev
->kobj
, "block");
2258 sysfs_put(rdev
->sysfs_state
);
2259 rdev
->sysfs_state
= NULL
;
2260 rdev
->badblocks
.count
= 0;
2261 /* We need to delay this, otherwise we can deadlock when
2262 * writing to 'remove' to "dev/state". We also need
2263 * to delay it due to rcu usage.
2266 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
2267 kobject_get(&rdev
->kobj
);
2268 queue_work(md_misc_wq
, &rdev
->del_work
);
2272 * prevent the device from being mounted, repartitioned or
2273 * otherwise reused by a RAID array (or any other kernel
2274 * subsystem), by bd_claiming the device.
2276 static int lock_rdev(struct md_rdev
*rdev
, dev_t dev
, int shared
)
2279 struct block_device
*bdev
;
2280 char b
[BDEVNAME_SIZE
];
2282 bdev
= blkdev_get_by_dev(dev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
,
2283 shared
? (struct md_rdev
*)lock_rdev
: rdev
);
2285 pr_warn("md: could not open %s.\n", __bdevname(dev
, b
));
2286 return PTR_ERR(bdev
);
2292 static void unlock_rdev(struct md_rdev
*rdev
)
2294 struct block_device
*bdev
= rdev
->bdev
;
2296 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
2299 void md_autodetect_dev(dev_t dev
);
2301 static void export_rdev(struct md_rdev
*rdev
)
2303 char b
[BDEVNAME_SIZE
];
2305 pr_debug("md: export_rdev(%s)\n", bdevname(rdev
->bdev
,b
));
2306 md_rdev_clear(rdev
);
2308 if (test_bit(AutoDetected
, &rdev
->flags
))
2309 md_autodetect_dev(rdev
->bdev
->bd_dev
);
2312 kobject_put(&rdev
->kobj
);
2315 void md_kick_rdev_from_array(struct md_rdev
*rdev
)
2317 unbind_rdev_from_array(rdev
);
2320 EXPORT_SYMBOL_GPL(md_kick_rdev_from_array
);
2322 static void export_array(struct mddev
*mddev
)
2324 struct md_rdev
*rdev
;
2326 while (!list_empty(&mddev
->disks
)) {
2327 rdev
= list_first_entry(&mddev
->disks
, struct md_rdev
,
2329 md_kick_rdev_from_array(rdev
);
2331 mddev
->raid_disks
= 0;
2332 mddev
->major_version
= 0;
2335 static bool set_in_sync(struct mddev
*mddev
)
2337 lockdep_assert_held(&mddev
->lock
);
2338 if (!mddev
->in_sync
) {
2339 mddev
->sync_checkers
++;
2340 spin_unlock(&mddev
->lock
);
2341 percpu_ref_switch_to_atomic_sync(&mddev
->writes_pending
);
2342 spin_lock(&mddev
->lock
);
2343 if (!mddev
->in_sync
&&
2344 percpu_ref_is_zero(&mddev
->writes_pending
)) {
2347 * Ensure ->in_sync is visible before we clear
2351 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
2352 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
2354 if (--mddev
->sync_checkers
== 0)
2355 percpu_ref_switch_to_percpu(&mddev
->writes_pending
);
2357 if (mddev
->safemode
== 1)
2358 mddev
->safemode
= 0;
2359 return mddev
->in_sync
;
2362 static void sync_sbs(struct mddev
*mddev
, int nospares
)
2364 /* Update each superblock (in-memory image), but
2365 * if we are allowed to, skip spares which already
2366 * have the right event counter, or have one earlier
2367 * (which would mean they aren't being marked as dirty
2368 * with the rest of the array)
2370 struct md_rdev
*rdev
;
2371 rdev_for_each(rdev
, mddev
) {
2372 if (rdev
->sb_events
== mddev
->events
||
2374 rdev
->raid_disk
< 0 &&
2375 rdev
->sb_events
+1 == mddev
->events
)) {
2376 /* Don't update this superblock */
2377 rdev
->sb_loaded
= 2;
2379 sync_super(mddev
, rdev
);
2380 rdev
->sb_loaded
= 1;
2385 static bool does_sb_need_changing(struct mddev
*mddev
)
2387 struct md_rdev
*rdev
;
2388 struct mdp_superblock_1
*sb
;
2391 /* Find a good rdev */
2392 rdev_for_each(rdev
, mddev
)
2393 if ((rdev
->raid_disk
>= 0) && !test_bit(Faulty
, &rdev
->flags
))
2396 /* No good device found. */
2400 sb
= page_address(rdev
->sb_page
);
2401 /* Check if a device has become faulty or a spare become active */
2402 rdev_for_each(rdev
, mddev
) {
2403 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
2404 /* Device activated? */
2405 if (role
== 0xffff && rdev
->raid_disk
>=0 &&
2406 !test_bit(Faulty
, &rdev
->flags
))
2408 /* Device turned faulty? */
2409 if (test_bit(Faulty
, &rdev
->flags
) && (role
< 0xfffd))
2413 /* Check if any mddev parameters have changed */
2414 if ((mddev
->dev_sectors
!= le64_to_cpu(sb
->size
)) ||
2415 (mddev
->reshape_position
!= le64_to_cpu(sb
->reshape_position
)) ||
2416 (mddev
->layout
!= le32_to_cpu(sb
->layout
)) ||
2417 (mddev
->raid_disks
!= le32_to_cpu(sb
->raid_disks
)) ||
2418 (mddev
->chunk_sectors
!= le32_to_cpu(sb
->chunksize
)))
2424 void md_update_sb(struct mddev
*mddev
, int force_change
)
2426 struct md_rdev
*rdev
;
2429 int any_badblocks_changed
= 0;
2434 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2439 if (mddev_is_clustered(mddev
)) {
2440 if (test_and_clear_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
))
2442 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
))
2444 ret
= md_cluster_ops
->metadata_update_start(mddev
);
2445 /* Has someone else has updated the sb */
2446 if (!does_sb_need_changing(mddev
)) {
2448 md_cluster_ops
->metadata_update_cancel(mddev
);
2449 bit_clear_unless(&mddev
->sb_flags
, BIT(MD_SB_CHANGE_PENDING
),
2450 BIT(MD_SB_CHANGE_DEVS
) |
2451 BIT(MD_SB_CHANGE_CLEAN
));
2457 * First make sure individual recovery_offsets are correct
2458 * curr_resync_completed can only be used during recovery.
2459 * During reshape/resync it might use array-addresses rather
2460 * that device addresses.
2462 rdev_for_each(rdev
, mddev
) {
2463 if (rdev
->raid_disk
>= 0 &&
2464 mddev
->delta_disks
>= 0 &&
2465 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
2466 test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
) &&
2467 !test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
2468 !test_bit(Journal
, &rdev
->flags
) &&
2469 !test_bit(In_sync
, &rdev
->flags
) &&
2470 mddev
->curr_resync_completed
> rdev
->recovery_offset
)
2471 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
2474 if (!mddev
->persistent
) {
2475 clear_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
2476 clear_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2477 if (!mddev
->external
) {
2478 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
2479 rdev_for_each(rdev
, mddev
) {
2480 if (rdev
->badblocks
.changed
) {
2481 rdev
->badblocks
.changed
= 0;
2482 ack_all_badblocks(&rdev
->badblocks
);
2483 md_error(mddev
, rdev
);
2485 clear_bit(Blocked
, &rdev
->flags
);
2486 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2487 wake_up(&rdev
->blocked_wait
);
2490 wake_up(&mddev
->sb_wait
);
2494 spin_lock(&mddev
->lock
);
2496 mddev
->utime
= ktime_get_real_seconds();
2498 if (test_and_clear_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
))
2500 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
))
2501 /* just a clean<-> dirty transition, possibly leave spares alone,
2502 * though if events isn't the right even/odd, we will have to do
2508 if (mddev
->degraded
)
2509 /* If the array is degraded, then skipping spares is both
2510 * dangerous and fairly pointless.
2511 * Dangerous because a device that was removed from the array
2512 * might have a event_count that still looks up-to-date,
2513 * so it can be re-added without a resync.
2514 * Pointless because if there are any spares to skip,
2515 * then a recovery will happen and soon that array won't
2516 * be degraded any more and the spare can go back to sleep then.
2520 sync_req
= mddev
->in_sync
;
2522 /* If this is just a dirty<->clean transition, and the array is clean
2523 * and 'events' is odd, we can roll back to the previous clean state */
2525 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
2526 && mddev
->can_decrease_events
2527 && mddev
->events
!= 1) {
2529 mddev
->can_decrease_events
= 0;
2531 /* otherwise we have to go forward and ... */
2533 mddev
->can_decrease_events
= nospares
;
2537 * This 64-bit counter should never wrap.
2538 * Either we are in around ~1 trillion A.C., assuming
2539 * 1 reboot per second, or we have a bug...
2541 WARN_ON(mddev
->events
== 0);
2543 rdev_for_each(rdev
, mddev
) {
2544 if (rdev
->badblocks
.changed
)
2545 any_badblocks_changed
++;
2546 if (test_bit(Faulty
, &rdev
->flags
))
2547 set_bit(FaultRecorded
, &rdev
->flags
);
2550 sync_sbs(mddev
, nospares
);
2551 spin_unlock(&mddev
->lock
);
2553 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2554 mdname(mddev
), mddev
->in_sync
);
2557 blk_add_trace_msg(mddev
->queue
, "md md_update_sb");
2559 bitmap_update_sb(mddev
->bitmap
);
2560 rdev_for_each(rdev
, mddev
) {
2561 char b
[BDEVNAME_SIZE
];
2563 if (rdev
->sb_loaded
!= 1)
2564 continue; /* no noise on spare devices */
2566 if (!test_bit(Faulty
, &rdev
->flags
)) {
2567 md_super_write(mddev
,rdev
,
2568 rdev
->sb_start
, rdev
->sb_size
,
2570 pr_debug("md: (write) %s's sb offset: %llu\n",
2571 bdevname(rdev
->bdev
, b
),
2572 (unsigned long long)rdev
->sb_start
);
2573 rdev
->sb_events
= mddev
->events
;
2574 if (rdev
->badblocks
.size
) {
2575 md_super_write(mddev
, rdev
,
2576 rdev
->badblocks
.sector
,
2577 rdev
->badblocks
.size
<< 9,
2579 rdev
->badblocks
.size
= 0;
2583 pr_debug("md: %s (skipping faulty)\n",
2584 bdevname(rdev
->bdev
, b
));
2586 if (mddev
->level
== LEVEL_MULTIPATH
)
2587 /* only need to write one superblock... */
2590 if (md_super_wait(mddev
) < 0)
2592 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
2594 if (mddev_is_clustered(mddev
) && ret
== 0)
2595 md_cluster_ops
->metadata_update_finish(mddev
);
2597 if (mddev
->in_sync
!= sync_req
||
2598 !bit_clear_unless(&mddev
->sb_flags
, BIT(MD_SB_CHANGE_PENDING
),
2599 BIT(MD_SB_CHANGE_DEVS
) | BIT(MD_SB_CHANGE_CLEAN
)))
2600 /* have to write it out again */
2602 wake_up(&mddev
->sb_wait
);
2603 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2604 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
2606 rdev_for_each(rdev
, mddev
) {
2607 if (test_and_clear_bit(FaultRecorded
, &rdev
->flags
))
2608 clear_bit(Blocked
, &rdev
->flags
);
2610 if (any_badblocks_changed
)
2611 ack_all_badblocks(&rdev
->badblocks
);
2612 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2613 wake_up(&rdev
->blocked_wait
);
2616 EXPORT_SYMBOL(md_update_sb
);
2618 static int add_bound_rdev(struct md_rdev
*rdev
)
2620 struct mddev
*mddev
= rdev
->mddev
;
2622 bool add_journal
= test_bit(Journal
, &rdev
->flags
);
2624 if (!mddev
->pers
->hot_remove_disk
|| add_journal
) {
2625 /* If there is hot_add_disk but no hot_remove_disk
2626 * then added disks for geometry changes,
2627 * and should be added immediately.
2629 super_types
[mddev
->major_version
].
2630 validate_super(mddev
, rdev
);
2632 mddev_suspend(mddev
);
2633 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
2635 mddev_resume(mddev
);
2637 md_kick_rdev_from_array(rdev
);
2641 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2643 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2644 if (mddev
->degraded
)
2645 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
2646 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2647 md_new_event(mddev
);
2648 md_wakeup_thread(mddev
->thread
);
2652 /* words written to sysfs files may, or may not, be \n terminated.
2653 * We want to accept with case. For this we use cmd_match.
2655 static int cmd_match(const char *cmd
, const char *str
)
2657 /* See if cmd, written into a sysfs file, matches
2658 * str. They must either be the same, or cmd can
2659 * have a trailing newline
2661 while (*cmd
&& *str
&& *cmd
== *str
) {
2672 struct rdev_sysfs_entry
{
2673 struct attribute attr
;
2674 ssize_t (*show
)(struct md_rdev
*, char *);
2675 ssize_t (*store
)(struct md_rdev
*, const char *, size_t);
2679 state_show(struct md_rdev
*rdev
, char *page
)
2683 unsigned long flags
= READ_ONCE(rdev
->flags
);
2685 if (test_bit(Faulty
, &flags
) ||
2686 (!test_bit(ExternalBbl
, &flags
) &&
2687 rdev
->badblocks
.unacked_exist
))
2688 len
+= sprintf(page
+len
, "faulty%s", sep
);
2689 if (test_bit(In_sync
, &flags
))
2690 len
+= sprintf(page
+len
, "in_sync%s", sep
);
2691 if (test_bit(Journal
, &flags
))
2692 len
+= sprintf(page
+len
, "journal%s", sep
);
2693 if (test_bit(WriteMostly
, &flags
))
2694 len
+= sprintf(page
+len
, "write_mostly%s", sep
);
2695 if (test_bit(Blocked
, &flags
) ||
2696 (rdev
->badblocks
.unacked_exist
2697 && !test_bit(Faulty
, &flags
)))
2698 len
+= sprintf(page
+len
, "blocked%s", sep
);
2699 if (!test_bit(Faulty
, &flags
) &&
2700 !test_bit(Journal
, &flags
) &&
2701 !test_bit(In_sync
, &flags
))
2702 len
+= sprintf(page
+len
, "spare%s", sep
);
2703 if (test_bit(WriteErrorSeen
, &flags
))
2704 len
+= sprintf(page
+len
, "write_error%s", sep
);
2705 if (test_bit(WantReplacement
, &flags
))
2706 len
+= sprintf(page
+len
, "want_replacement%s", sep
);
2707 if (test_bit(Replacement
, &flags
))
2708 len
+= sprintf(page
+len
, "replacement%s", sep
);
2709 if (test_bit(ExternalBbl
, &flags
))
2710 len
+= sprintf(page
+len
, "external_bbl%s", sep
);
2711 if (test_bit(FailFast
, &flags
))
2712 len
+= sprintf(page
+len
, "failfast%s", sep
);
2717 return len
+sprintf(page
+len
, "\n");
2721 state_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2724 * faulty - simulates an error
2725 * remove - disconnects the device
2726 * writemostly - sets write_mostly
2727 * -writemostly - clears write_mostly
2728 * blocked - sets the Blocked flags
2729 * -blocked - clears the Blocked and possibly simulates an error
2730 * insync - sets Insync providing device isn't active
2731 * -insync - clear Insync for a device with a slot assigned,
2732 * so that it gets rebuilt based on bitmap
2733 * write_error - sets WriteErrorSeen
2734 * -write_error - clears WriteErrorSeen
2735 * {,-}failfast - set/clear FailFast
2738 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
2739 md_error(rdev
->mddev
, rdev
);
2740 if (test_bit(Faulty
, &rdev
->flags
))
2744 } else if (cmd_match(buf
, "remove")) {
2745 if (rdev
->mddev
->pers
) {
2746 clear_bit(Blocked
, &rdev
->flags
);
2747 remove_and_add_spares(rdev
->mddev
, rdev
);
2749 if (rdev
->raid_disk
>= 0)
2752 struct mddev
*mddev
= rdev
->mddev
;
2754 if (mddev_is_clustered(mddev
))
2755 err
= md_cluster_ops
->remove_disk(mddev
, rdev
);
2758 md_kick_rdev_from_array(rdev
);
2760 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2761 md_wakeup_thread(mddev
->thread
);
2763 md_new_event(mddev
);
2766 } else if (cmd_match(buf
, "writemostly")) {
2767 set_bit(WriteMostly
, &rdev
->flags
);
2769 } else if (cmd_match(buf
, "-writemostly")) {
2770 clear_bit(WriteMostly
, &rdev
->flags
);
2772 } else if (cmd_match(buf
, "blocked")) {
2773 set_bit(Blocked
, &rdev
->flags
);
2775 } else if (cmd_match(buf
, "-blocked")) {
2776 if (!test_bit(Faulty
, &rdev
->flags
) &&
2777 !test_bit(ExternalBbl
, &rdev
->flags
) &&
2778 rdev
->badblocks
.unacked_exist
) {
2779 /* metadata handler doesn't understand badblocks,
2780 * so we need to fail the device
2782 md_error(rdev
->mddev
, rdev
);
2784 clear_bit(Blocked
, &rdev
->flags
);
2785 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2786 wake_up(&rdev
->blocked_wait
);
2787 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2788 md_wakeup_thread(rdev
->mddev
->thread
);
2791 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
2792 set_bit(In_sync
, &rdev
->flags
);
2794 } else if (cmd_match(buf
, "failfast")) {
2795 set_bit(FailFast
, &rdev
->flags
);
2797 } else if (cmd_match(buf
, "-failfast")) {
2798 clear_bit(FailFast
, &rdev
->flags
);
2800 } else if (cmd_match(buf
, "-insync") && rdev
->raid_disk
>= 0 &&
2801 !test_bit(Journal
, &rdev
->flags
)) {
2802 if (rdev
->mddev
->pers
== NULL
) {
2803 clear_bit(In_sync
, &rdev
->flags
);
2804 rdev
->saved_raid_disk
= rdev
->raid_disk
;
2805 rdev
->raid_disk
= -1;
2808 } else if (cmd_match(buf
, "write_error")) {
2809 set_bit(WriteErrorSeen
, &rdev
->flags
);
2811 } else if (cmd_match(buf
, "-write_error")) {
2812 clear_bit(WriteErrorSeen
, &rdev
->flags
);
2814 } else if (cmd_match(buf
, "want_replacement")) {
2815 /* Any non-spare device that is not a replacement can
2816 * become want_replacement at any time, but we then need to
2817 * check if recovery is needed.
2819 if (rdev
->raid_disk
>= 0 &&
2820 !test_bit(Journal
, &rdev
->flags
) &&
2821 !test_bit(Replacement
, &rdev
->flags
))
2822 set_bit(WantReplacement
, &rdev
->flags
);
2823 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2824 md_wakeup_thread(rdev
->mddev
->thread
);
2826 } else if (cmd_match(buf
, "-want_replacement")) {
2827 /* Clearing 'want_replacement' is always allowed.
2828 * Once replacements starts it is too late though.
2831 clear_bit(WantReplacement
, &rdev
->flags
);
2832 } else if (cmd_match(buf
, "replacement")) {
2833 /* Can only set a device as a replacement when array has not
2834 * yet been started. Once running, replacement is automatic
2835 * from spares, or by assigning 'slot'.
2837 if (rdev
->mddev
->pers
)
2840 set_bit(Replacement
, &rdev
->flags
);
2843 } else if (cmd_match(buf
, "-replacement")) {
2844 /* Similarly, can only clear Replacement before start */
2845 if (rdev
->mddev
->pers
)
2848 clear_bit(Replacement
, &rdev
->flags
);
2851 } else if (cmd_match(buf
, "re-add")) {
2852 if (test_bit(Faulty
, &rdev
->flags
) && (rdev
->raid_disk
== -1)) {
2853 /* clear_bit is performed _after_ all the devices
2854 * have their local Faulty bit cleared. If any writes
2855 * happen in the meantime in the local node, they
2856 * will land in the local bitmap, which will be synced
2857 * by this node eventually
2859 if (!mddev_is_clustered(rdev
->mddev
) ||
2860 (err
= md_cluster_ops
->gather_bitmaps(rdev
)) == 0) {
2861 clear_bit(Faulty
, &rdev
->flags
);
2862 err
= add_bound_rdev(rdev
);
2866 } else if (cmd_match(buf
, "external_bbl") && (rdev
->mddev
->external
)) {
2867 set_bit(ExternalBbl
, &rdev
->flags
);
2868 rdev
->badblocks
.shift
= 0;
2870 } else if (cmd_match(buf
, "-external_bbl") && (rdev
->mddev
->external
)) {
2871 clear_bit(ExternalBbl
, &rdev
->flags
);
2875 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2876 return err
? err
: len
;
2878 static struct rdev_sysfs_entry rdev_state
=
2879 __ATTR_PREALLOC(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
2882 errors_show(struct md_rdev
*rdev
, char *page
)
2884 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
2888 errors_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2893 rv
= kstrtouint(buf
, 10, &n
);
2896 atomic_set(&rdev
->corrected_errors
, n
);
2899 static struct rdev_sysfs_entry rdev_errors
=
2900 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
2903 slot_show(struct md_rdev
*rdev
, char *page
)
2905 if (test_bit(Journal
, &rdev
->flags
))
2906 return sprintf(page
, "journal\n");
2907 else if (rdev
->raid_disk
< 0)
2908 return sprintf(page
, "none\n");
2910 return sprintf(page
, "%d\n", rdev
->raid_disk
);
2914 slot_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2919 if (test_bit(Journal
, &rdev
->flags
))
2921 if (strncmp(buf
, "none", 4)==0)
2924 err
= kstrtouint(buf
, 10, (unsigned int *)&slot
);
2928 if (rdev
->mddev
->pers
&& slot
== -1) {
2929 /* Setting 'slot' on an active array requires also
2930 * updating the 'rd%d' link, and communicating
2931 * with the personality with ->hot_*_disk.
2932 * For now we only support removing
2933 * failed/spare devices. This normally happens automatically,
2934 * but not when the metadata is externally managed.
2936 if (rdev
->raid_disk
== -1)
2938 /* personality does all needed checks */
2939 if (rdev
->mddev
->pers
->hot_remove_disk
== NULL
)
2941 clear_bit(Blocked
, &rdev
->flags
);
2942 remove_and_add_spares(rdev
->mddev
, rdev
);
2943 if (rdev
->raid_disk
>= 0)
2945 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2946 md_wakeup_thread(rdev
->mddev
->thread
);
2947 } else if (rdev
->mddev
->pers
) {
2948 /* Activating a spare .. or possibly reactivating
2949 * if we ever get bitmaps working here.
2953 if (rdev
->raid_disk
!= -1)
2956 if (test_bit(MD_RECOVERY_RUNNING
, &rdev
->mddev
->recovery
))
2959 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2962 if (slot
>= rdev
->mddev
->raid_disks
&&
2963 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2966 rdev
->raid_disk
= slot
;
2967 if (test_bit(In_sync
, &rdev
->flags
))
2968 rdev
->saved_raid_disk
= slot
;
2970 rdev
->saved_raid_disk
= -1;
2971 clear_bit(In_sync
, &rdev
->flags
);
2972 clear_bit(Bitmap_sync
, &rdev
->flags
);
2973 err
= rdev
->mddev
->pers
->
2974 hot_add_disk(rdev
->mddev
, rdev
);
2976 rdev
->raid_disk
= -1;
2979 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2980 if (sysfs_link_rdev(rdev
->mddev
, rdev
))
2981 /* failure here is OK */;
2982 /* don't wakeup anyone, leave that to userspace. */
2984 if (slot
>= rdev
->mddev
->raid_disks
&&
2985 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2987 rdev
->raid_disk
= slot
;
2988 /* assume it is working */
2989 clear_bit(Faulty
, &rdev
->flags
);
2990 clear_bit(WriteMostly
, &rdev
->flags
);
2991 set_bit(In_sync
, &rdev
->flags
);
2992 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2997 static struct rdev_sysfs_entry rdev_slot
=
2998 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
3001 offset_show(struct md_rdev
*rdev
, char *page
)
3003 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
3007 offset_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3009 unsigned long long offset
;
3010 if (kstrtoull(buf
, 10, &offset
) < 0)
3012 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
3014 if (rdev
->sectors
&& rdev
->mddev
->external
)
3015 /* Must set offset before size, so overlap checks
3018 rdev
->data_offset
= offset
;
3019 rdev
->new_data_offset
= offset
;
3023 static struct rdev_sysfs_entry rdev_offset
=
3024 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
3026 static ssize_t
new_offset_show(struct md_rdev
*rdev
, char *page
)
3028 return sprintf(page
, "%llu\n",
3029 (unsigned long long)rdev
->new_data_offset
);
3032 static ssize_t
new_offset_store(struct md_rdev
*rdev
,
3033 const char *buf
, size_t len
)
3035 unsigned long long new_offset
;
3036 struct mddev
*mddev
= rdev
->mddev
;
3038 if (kstrtoull(buf
, 10, &new_offset
) < 0)
3041 if (mddev
->sync_thread
||
3042 test_bit(MD_RECOVERY_RUNNING
,&mddev
->recovery
))
3044 if (new_offset
== rdev
->data_offset
)
3045 /* reset is always permitted */
3047 else if (new_offset
> rdev
->data_offset
) {
3048 /* must not push array size beyond rdev_sectors */
3049 if (new_offset
- rdev
->data_offset
3050 + mddev
->dev_sectors
> rdev
->sectors
)
3053 /* Metadata worries about other space details. */
3055 /* decreasing the offset is inconsistent with a backwards
3058 if (new_offset
< rdev
->data_offset
&&
3059 mddev
->reshape_backwards
)
3061 /* Increasing offset is inconsistent with forwards
3062 * reshape. reshape_direction should be set to
3063 * 'backwards' first.
3065 if (new_offset
> rdev
->data_offset
&&
3066 !mddev
->reshape_backwards
)
3069 if (mddev
->pers
&& mddev
->persistent
&&
3070 !super_types
[mddev
->major_version
]
3071 .allow_new_offset(rdev
, new_offset
))
3073 rdev
->new_data_offset
= new_offset
;
3074 if (new_offset
> rdev
->data_offset
)
3075 mddev
->reshape_backwards
= 1;
3076 else if (new_offset
< rdev
->data_offset
)
3077 mddev
->reshape_backwards
= 0;
3081 static struct rdev_sysfs_entry rdev_new_offset
=
3082 __ATTR(new_offset
, S_IRUGO
|S_IWUSR
, new_offset_show
, new_offset_store
);
3085 rdev_size_show(struct md_rdev
*rdev
, char *page
)
3087 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
3090 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
3092 /* check if two start/length pairs overlap */
3100 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
3102 unsigned long long blocks
;
3105 if (kstrtoull(buf
, 10, &blocks
) < 0)
3108 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
3109 return -EINVAL
; /* sector conversion overflow */
3112 if (new != blocks
* 2)
3113 return -EINVAL
; /* unsigned long long to sector_t overflow */
3120 rdev_size_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3122 struct mddev
*my_mddev
= rdev
->mddev
;
3123 sector_t oldsectors
= rdev
->sectors
;
3126 if (test_bit(Journal
, &rdev
->flags
))
3128 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
3130 if (rdev
->data_offset
!= rdev
->new_data_offset
)
3131 return -EINVAL
; /* too confusing */
3132 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
3133 if (my_mddev
->persistent
) {
3134 sectors
= super_types
[my_mddev
->major_version
].
3135 rdev_size_change(rdev
, sectors
);
3138 } else if (!sectors
)
3139 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
3141 if (!my_mddev
->pers
->resize
)
3142 /* Cannot change size for RAID0 or Linear etc */
3145 if (sectors
< my_mddev
->dev_sectors
)
3146 return -EINVAL
; /* component must fit device */
3148 rdev
->sectors
= sectors
;
3149 if (sectors
> oldsectors
&& my_mddev
->external
) {
3150 /* Need to check that all other rdevs with the same
3151 * ->bdev do not overlap. 'rcu' is sufficient to walk
3152 * the rdev lists safely.
3153 * This check does not provide a hard guarantee, it
3154 * just helps avoid dangerous mistakes.
3156 struct mddev
*mddev
;
3158 struct list_head
*tmp
;
3161 for_each_mddev(mddev
, tmp
) {
3162 struct md_rdev
*rdev2
;
3164 rdev_for_each(rdev2
, mddev
)
3165 if (rdev
->bdev
== rdev2
->bdev
&&
3167 overlaps(rdev
->data_offset
, rdev
->sectors
,
3180 /* Someone else could have slipped in a size
3181 * change here, but doing so is just silly.
3182 * We put oldsectors back because we *know* it is
3183 * safe, and trust userspace not to race with
3186 rdev
->sectors
= oldsectors
;
3193 static struct rdev_sysfs_entry rdev_size
=
3194 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
3196 static ssize_t
recovery_start_show(struct md_rdev
*rdev
, char *page
)
3198 unsigned long long recovery_start
= rdev
->recovery_offset
;
3200 if (test_bit(In_sync
, &rdev
->flags
) ||
3201 recovery_start
== MaxSector
)
3202 return sprintf(page
, "none\n");
3204 return sprintf(page
, "%llu\n", recovery_start
);
3207 static ssize_t
recovery_start_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3209 unsigned long long recovery_start
;
3211 if (cmd_match(buf
, "none"))
3212 recovery_start
= MaxSector
;
3213 else if (kstrtoull(buf
, 10, &recovery_start
))
3216 if (rdev
->mddev
->pers
&&
3217 rdev
->raid_disk
>= 0)
3220 rdev
->recovery_offset
= recovery_start
;
3221 if (recovery_start
== MaxSector
)
3222 set_bit(In_sync
, &rdev
->flags
);
3224 clear_bit(In_sync
, &rdev
->flags
);
3228 static struct rdev_sysfs_entry rdev_recovery_start
=
3229 __ATTR(recovery_start
, S_IRUGO
|S_IWUSR
, recovery_start_show
, recovery_start_store
);
3231 /* sysfs access to bad-blocks list.
3232 * We present two files.
3233 * 'bad-blocks' lists sector numbers and lengths of ranges that
3234 * are recorded as bad. The list is truncated to fit within
3235 * the one-page limit of sysfs.
3236 * Writing "sector length" to this file adds an acknowledged
3238 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3239 * been acknowledged. Writing to this file adds bad blocks
3240 * without acknowledging them. This is largely for testing.
3242 static ssize_t
bb_show(struct md_rdev
*rdev
, char *page
)
3244 return badblocks_show(&rdev
->badblocks
, page
, 0);
3246 static ssize_t
bb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
3248 int rv
= badblocks_store(&rdev
->badblocks
, page
, len
, 0);
3249 /* Maybe that ack was all we needed */
3250 if (test_and_clear_bit(BlockedBadBlocks
, &rdev
->flags
))
3251 wake_up(&rdev
->blocked_wait
);
3254 static struct rdev_sysfs_entry rdev_bad_blocks
=
3255 __ATTR(bad_blocks
, S_IRUGO
|S_IWUSR
, bb_show
, bb_store
);
3257 static ssize_t
ubb_show(struct md_rdev
*rdev
, char *page
)
3259 return badblocks_show(&rdev
->badblocks
, page
, 1);
3261 static ssize_t
ubb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
3263 return badblocks_store(&rdev
->badblocks
, page
, len
, 1);
3265 static struct rdev_sysfs_entry rdev_unack_bad_blocks
=
3266 __ATTR(unacknowledged_bad_blocks
, S_IRUGO
|S_IWUSR
, ubb_show
, ubb_store
);
3269 ppl_sector_show(struct md_rdev
*rdev
, char *page
)
3271 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->ppl
.sector
);
3275 ppl_sector_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3277 unsigned long long sector
;
3279 if (kstrtoull(buf
, 10, §or
) < 0)
3281 if (sector
!= (sector_t
)sector
)
3284 if (rdev
->mddev
->pers
&& test_bit(MD_HAS_PPL
, &rdev
->mddev
->flags
) &&
3285 rdev
->raid_disk
>= 0)
3288 if (rdev
->mddev
->persistent
) {
3289 if (rdev
->mddev
->major_version
== 0)
3291 if ((sector
> rdev
->sb_start
&&
3292 sector
- rdev
->sb_start
> S16_MAX
) ||
3293 (sector
< rdev
->sb_start
&&
3294 rdev
->sb_start
- sector
> -S16_MIN
))
3296 rdev
->ppl
.offset
= sector
- rdev
->sb_start
;
3297 } else if (!rdev
->mddev
->external
) {
3300 rdev
->ppl
.sector
= sector
;
3304 static struct rdev_sysfs_entry rdev_ppl_sector
=
3305 __ATTR(ppl_sector
, S_IRUGO
|S_IWUSR
, ppl_sector_show
, ppl_sector_store
);
3308 ppl_size_show(struct md_rdev
*rdev
, char *page
)
3310 return sprintf(page
, "%u\n", rdev
->ppl
.size
);
3314 ppl_size_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3318 if (kstrtouint(buf
, 10, &size
) < 0)
3321 if (rdev
->mddev
->pers
&& test_bit(MD_HAS_PPL
, &rdev
->mddev
->flags
) &&
3322 rdev
->raid_disk
>= 0)
3325 if (rdev
->mddev
->persistent
) {
3326 if (rdev
->mddev
->major_version
== 0)
3330 } else if (!rdev
->mddev
->external
) {
3333 rdev
->ppl
.size
= size
;
3337 static struct rdev_sysfs_entry rdev_ppl_size
=
3338 __ATTR(ppl_size
, S_IRUGO
|S_IWUSR
, ppl_size_show
, ppl_size_store
);
3340 static struct attribute
*rdev_default_attrs
[] = {
3345 &rdev_new_offset
.attr
,
3347 &rdev_recovery_start
.attr
,
3348 &rdev_bad_blocks
.attr
,
3349 &rdev_unack_bad_blocks
.attr
,
3350 &rdev_ppl_sector
.attr
,
3351 &rdev_ppl_size
.attr
,
3355 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
3357 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3358 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3364 return entry
->show(rdev
, page
);
3368 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
3369 const char *page
, size_t length
)
3371 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3372 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3374 struct mddev
*mddev
= rdev
->mddev
;
3378 if (!capable(CAP_SYS_ADMIN
))
3380 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
3382 if (rdev
->mddev
== NULL
)
3385 rv
= entry
->store(rdev
, page
, length
);
3386 mddev_unlock(mddev
);
3391 static void rdev_free(struct kobject
*ko
)
3393 struct md_rdev
*rdev
= container_of(ko
, struct md_rdev
, kobj
);
3396 static const struct sysfs_ops rdev_sysfs_ops
= {
3397 .show
= rdev_attr_show
,
3398 .store
= rdev_attr_store
,
3400 static struct kobj_type rdev_ktype
= {
3401 .release
= rdev_free
,
3402 .sysfs_ops
= &rdev_sysfs_ops
,
3403 .default_attrs
= rdev_default_attrs
,
3406 int md_rdev_init(struct md_rdev
*rdev
)
3409 rdev
->saved_raid_disk
= -1;
3410 rdev
->raid_disk
= -1;
3412 rdev
->data_offset
= 0;
3413 rdev
->new_data_offset
= 0;
3414 rdev
->sb_events
= 0;
3415 rdev
->last_read_error
= 0;
3416 rdev
->sb_loaded
= 0;
3417 rdev
->bb_page
= NULL
;
3418 atomic_set(&rdev
->nr_pending
, 0);
3419 atomic_set(&rdev
->read_errors
, 0);
3420 atomic_set(&rdev
->corrected_errors
, 0);
3422 INIT_LIST_HEAD(&rdev
->same_set
);
3423 init_waitqueue_head(&rdev
->blocked_wait
);
3425 /* Add space to store bad block list.
3426 * This reserves the space even on arrays where it cannot
3427 * be used - I wonder if that matters
3429 return badblocks_init(&rdev
->badblocks
, 0);
3431 EXPORT_SYMBOL_GPL(md_rdev_init
);
3433 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3435 * mark the device faulty if:
3437 * - the device is nonexistent (zero size)
3438 * - the device has no valid superblock
3440 * a faulty rdev _never_ has rdev->sb set.
3442 static struct md_rdev
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
3444 char b
[BDEVNAME_SIZE
];
3446 struct md_rdev
*rdev
;
3449 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
3451 return ERR_PTR(-ENOMEM
);
3453 err
= md_rdev_init(rdev
);
3456 err
= alloc_disk_sb(rdev
);
3460 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
3464 kobject_init(&rdev
->kobj
, &rdev_ktype
);
3466 size
= i_size_read(rdev
->bdev
->bd_inode
) >> BLOCK_SIZE_BITS
;
3468 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
3469 bdevname(rdev
->bdev
,b
));
3474 if (super_format
>= 0) {
3475 err
= super_types
[super_format
].
3476 load_super(rdev
, NULL
, super_minor
);
3477 if (err
== -EINVAL
) {
3478 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
3479 bdevname(rdev
->bdev
,b
),
3480 super_format
, super_minor
);
3484 pr_warn("md: could not read %s's sb, not importing!\n",
3485 bdevname(rdev
->bdev
,b
));
3495 md_rdev_clear(rdev
);
3497 return ERR_PTR(err
);
3501 * Check a full RAID array for plausibility
3504 static void analyze_sbs(struct mddev
*mddev
)
3507 struct md_rdev
*rdev
, *freshest
, *tmp
;
3508 char b
[BDEVNAME_SIZE
];
3511 rdev_for_each_safe(rdev
, tmp
, mddev
)
3512 switch (super_types
[mddev
->major_version
].
3513 load_super(rdev
, freshest
, mddev
->minor_version
)) {
3520 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
3521 bdevname(rdev
->bdev
,b
));
3522 md_kick_rdev_from_array(rdev
);
3525 super_types
[mddev
->major_version
].
3526 validate_super(mddev
, freshest
);
3529 rdev_for_each_safe(rdev
, tmp
, mddev
) {
3530 if (mddev
->max_disks
&&
3531 (rdev
->desc_nr
>= mddev
->max_disks
||
3532 i
> mddev
->max_disks
)) {
3533 pr_warn("md: %s: %s: only %d devices permitted\n",
3534 mdname(mddev
), bdevname(rdev
->bdev
, b
),
3536 md_kick_rdev_from_array(rdev
);
3539 if (rdev
!= freshest
) {
3540 if (super_types
[mddev
->major_version
].
3541 validate_super(mddev
, rdev
)) {
3542 pr_warn("md: kicking non-fresh %s from array!\n",
3543 bdevname(rdev
->bdev
,b
));
3544 md_kick_rdev_from_array(rdev
);
3548 if (mddev
->level
== LEVEL_MULTIPATH
) {
3549 rdev
->desc_nr
= i
++;
3550 rdev
->raid_disk
= rdev
->desc_nr
;
3551 set_bit(In_sync
, &rdev
->flags
);
3552 } else if (rdev
->raid_disk
>=
3553 (mddev
->raid_disks
- min(0, mddev
->delta_disks
)) &&
3554 !test_bit(Journal
, &rdev
->flags
)) {
3555 rdev
->raid_disk
= -1;
3556 clear_bit(In_sync
, &rdev
->flags
);
3561 /* Read a fixed-point number.
3562 * Numbers in sysfs attributes should be in "standard" units where
3563 * possible, so time should be in seconds.
3564 * However we internally use a a much smaller unit such as
3565 * milliseconds or jiffies.
3566 * This function takes a decimal number with a possible fractional
3567 * component, and produces an integer which is the result of
3568 * multiplying that number by 10^'scale'.
3569 * all without any floating-point arithmetic.
3571 int strict_strtoul_scaled(const char *cp
, unsigned long *res
, int scale
)
3573 unsigned long result
= 0;
3575 while (isdigit(*cp
) || (*cp
== '.' && decimals
< 0)) {
3578 else if (decimals
< scale
) {
3581 result
= result
* 10 + value
;
3593 while (decimals
< scale
) {
3602 safe_delay_show(struct mddev
*mddev
, char *page
)
3604 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
3605 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
3608 safe_delay_store(struct mddev
*mddev
, const char *cbuf
, size_t len
)
3612 if (mddev_is_clustered(mddev
)) {
3613 pr_warn("md: Safemode is disabled for clustered mode\n");
3617 if (strict_strtoul_scaled(cbuf
, &msec
, 3) < 0)
3620 mddev
->safemode_delay
= 0;
3622 unsigned long old_delay
= mddev
->safemode_delay
;
3623 unsigned long new_delay
= (msec
*HZ
)/1000;
3627 mddev
->safemode_delay
= new_delay
;
3628 if (new_delay
< old_delay
|| old_delay
== 0)
3629 mod_timer(&mddev
->safemode_timer
, jiffies
+1);
3633 static struct md_sysfs_entry md_safe_delay
=
3634 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
3637 level_show(struct mddev
*mddev
, char *page
)
3639 struct md_personality
*p
;
3641 spin_lock(&mddev
->lock
);
3644 ret
= sprintf(page
, "%s\n", p
->name
);
3645 else if (mddev
->clevel
[0])
3646 ret
= sprintf(page
, "%s\n", mddev
->clevel
);
3647 else if (mddev
->level
!= LEVEL_NONE
)
3648 ret
= sprintf(page
, "%d\n", mddev
->level
);
3651 spin_unlock(&mddev
->lock
);
3656 level_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3661 struct md_personality
*pers
, *oldpers
;
3663 void *priv
, *oldpriv
;
3664 struct md_rdev
*rdev
;
3666 if (slen
== 0 || slen
>= sizeof(clevel
))
3669 rv
= mddev_lock(mddev
);
3673 if (mddev
->pers
== NULL
) {
3674 strncpy(mddev
->clevel
, buf
, slen
);
3675 if (mddev
->clevel
[slen
-1] == '\n')
3677 mddev
->clevel
[slen
] = 0;
3678 mddev
->level
= LEVEL_NONE
;
3686 /* request to change the personality. Need to ensure:
3687 * - array is not engaged in resync/recovery/reshape
3688 * - old personality can be suspended
3689 * - new personality will access other array.
3693 if (mddev
->sync_thread
||
3694 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3695 mddev
->reshape_position
!= MaxSector
||
3696 mddev
->sysfs_active
)
3700 if (!mddev
->pers
->quiesce
) {
3701 pr_warn("md: %s: %s does not support online personality change\n",
3702 mdname(mddev
), mddev
->pers
->name
);
3706 /* Now find the new personality */
3707 strncpy(clevel
, buf
, slen
);
3708 if (clevel
[slen
-1] == '\n')
3711 if (kstrtol(clevel
, 10, &level
))
3714 if (request_module("md-%s", clevel
) != 0)
3715 request_module("md-level-%s", clevel
);
3716 spin_lock(&pers_lock
);
3717 pers
= find_pers(level
, clevel
);
3718 if (!pers
|| !try_module_get(pers
->owner
)) {
3719 spin_unlock(&pers_lock
);
3720 pr_warn("md: personality %s not loaded\n", clevel
);
3724 spin_unlock(&pers_lock
);
3726 if (pers
== mddev
->pers
) {
3727 /* Nothing to do! */
3728 module_put(pers
->owner
);
3732 if (!pers
->takeover
) {
3733 module_put(pers
->owner
);
3734 pr_warn("md: %s: %s does not support personality takeover\n",
3735 mdname(mddev
), clevel
);
3740 rdev_for_each(rdev
, mddev
)
3741 rdev
->new_raid_disk
= rdev
->raid_disk
;
3743 /* ->takeover must set new_* and/or delta_disks
3744 * if it succeeds, and may set them when it fails.
3746 priv
= pers
->takeover(mddev
);
3748 mddev
->new_level
= mddev
->level
;
3749 mddev
->new_layout
= mddev
->layout
;
3750 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3751 mddev
->raid_disks
-= mddev
->delta_disks
;
3752 mddev
->delta_disks
= 0;
3753 mddev
->reshape_backwards
= 0;
3754 module_put(pers
->owner
);
3755 pr_warn("md: %s: %s would not accept array\n",
3756 mdname(mddev
), clevel
);
3761 /* Looks like we have a winner */
3762 mddev_suspend(mddev
);
3763 mddev_detach(mddev
);
3765 spin_lock(&mddev
->lock
);
3766 oldpers
= mddev
->pers
;
3767 oldpriv
= mddev
->private;
3769 mddev
->private = priv
;
3770 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3771 mddev
->level
= mddev
->new_level
;
3772 mddev
->layout
= mddev
->new_layout
;
3773 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
3774 mddev
->delta_disks
= 0;
3775 mddev
->reshape_backwards
= 0;
3776 mddev
->degraded
= 0;
3777 spin_unlock(&mddev
->lock
);
3779 if (oldpers
->sync_request
== NULL
&&
3781 /* We are converting from a no-redundancy array
3782 * to a redundancy array and metadata is managed
3783 * externally so we need to be sure that writes
3784 * won't block due to a need to transition
3786 * until external management is started.
3789 mddev
->safemode_delay
= 0;
3790 mddev
->safemode
= 0;
3793 oldpers
->free(mddev
, oldpriv
);
3795 if (oldpers
->sync_request
== NULL
&&
3796 pers
->sync_request
!= NULL
) {
3797 /* need to add the md_redundancy_group */
3798 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
3799 pr_warn("md: cannot register extra attributes for %s\n",
3801 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, "sync_action");
3803 if (oldpers
->sync_request
!= NULL
&&
3804 pers
->sync_request
== NULL
) {
3805 /* need to remove the md_redundancy_group */
3806 if (mddev
->to_remove
== NULL
)
3807 mddev
->to_remove
= &md_redundancy_group
;
3810 module_put(oldpers
->owner
);
3812 rdev_for_each(rdev
, mddev
) {
3813 if (rdev
->raid_disk
< 0)
3815 if (rdev
->new_raid_disk
>= mddev
->raid_disks
)
3816 rdev
->new_raid_disk
= -1;
3817 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3819 sysfs_unlink_rdev(mddev
, rdev
);
3821 rdev_for_each(rdev
, mddev
) {
3822 if (rdev
->raid_disk
< 0)
3824 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3826 rdev
->raid_disk
= rdev
->new_raid_disk
;
3827 if (rdev
->raid_disk
< 0)
3828 clear_bit(In_sync
, &rdev
->flags
);
3830 if (sysfs_link_rdev(mddev
, rdev
))
3831 pr_warn("md: cannot register rd%d for %s after level change\n",
3832 rdev
->raid_disk
, mdname(mddev
));
3836 if (pers
->sync_request
== NULL
) {
3837 /* this is now an array without redundancy, so
3838 * it must always be in_sync
3841 del_timer_sync(&mddev
->safemode_timer
);
3843 blk_set_stacking_limits(&mddev
->queue
->limits
);
3845 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
3846 mddev_resume(mddev
);
3848 md_update_sb(mddev
, 1);
3849 sysfs_notify(&mddev
->kobj
, NULL
, "level");
3850 md_new_event(mddev
);
3853 mddev_unlock(mddev
);
3857 static struct md_sysfs_entry md_level
=
3858 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
3861 layout_show(struct mddev
*mddev
, char *page
)
3863 /* just a number, not meaningful for all levels */
3864 if (mddev
->reshape_position
!= MaxSector
&&
3865 mddev
->layout
!= mddev
->new_layout
)
3866 return sprintf(page
, "%d (%d)\n",
3867 mddev
->new_layout
, mddev
->layout
);
3868 return sprintf(page
, "%d\n", mddev
->layout
);
3872 layout_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3877 err
= kstrtouint(buf
, 10, &n
);
3880 err
= mddev_lock(mddev
);
3885 if (mddev
->pers
->check_reshape
== NULL
)
3890 mddev
->new_layout
= n
;
3891 err
= mddev
->pers
->check_reshape(mddev
);
3893 mddev
->new_layout
= mddev
->layout
;
3896 mddev
->new_layout
= n
;
3897 if (mddev
->reshape_position
== MaxSector
)
3900 mddev_unlock(mddev
);
3903 static struct md_sysfs_entry md_layout
=
3904 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
3907 raid_disks_show(struct mddev
*mddev
, char *page
)
3909 if (mddev
->raid_disks
== 0)
3911 if (mddev
->reshape_position
!= MaxSector
&&
3912 mddev
->delta_disks
!= 0)
3913 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
3914 mddev
->raid_disks
- mddev
->delta_disks
);
3915 return sprintf(page
, "%d\n", mddev
->raid_disks
);
3918 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
);
3921 raid_disks_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3926 err
= kstrtouint(buf
, 10, &n
);
3930 err
= mddev_lock(mddev
);
3934 err
= update_raid_disks(mddev
, n
);
3935 else if (mddev
->reshape_position
!= MaxSector
) {
3936 struct md_rdev
*rdev
;
3937 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
3940 rdev_for_each(rdev
, mddev
) {
3942 rdev
->data_offset
< rdev
->new_data_offset
)
3945 rdev
->data_offset
> rdev
->new_data_offset
)
3949 mddev
->delta_disks
= n
- olddisks
;
3950 mddev
->raid_disks
= n
;
3951 mddev
->reshape_backwards
= (mddev
->delta_disks
< 0);
3953 mddev
->raid_disks
= n
;
3955 mddev_unlock(mddev
);
3956 return err
? err
: len
;
3958 static struct md_sysfs_entry md_raid_disks
=
3959 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
3962 chunk_size_show(struct mddev
*mddev
, char *page
)
3964 if (mddev
->reshape_position
!= MaxSector
&&
3965 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
3966 return sprintf(page
, "%d (%d)\n",
3967 mddev
->new_chunk_sectors
<< 9,
3968 mddev
->chunk_sectors
<< 9);
3969 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
3973 chunk_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3978 err
= kstrtoul(buf
, 10, &n
);
3982 err
= mddev_lock(mddev
);
3986 if (mddev
->pers
->check_reshape
== NULL
)
3991 mddev
->new_chunk_sectors
= n
>> 9;
3992 err
= mddev
->pers
->check_reshape(mddev
);
3994 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3997 mddev
->new_chunk_sectors
= n
>> 9;
3998 if (mddev
->reshape_position
== MaxSector
)
3999 mddev
->chunk_sectors
= n
>> 9;
4001 mddev_unlock(mddev
);
4004 static struct md_sysfs_entry md_chunk_size
=
4005 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
4008 resync_start_show(struct mddev
*mddev
, char *page
)
4010 if (mddev
->recovery_cp
== MaxSector
)
4011 return sprintf(page
, "none\n");
4012 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
4016 resync_start_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4018 unsigned long long n
;
4021 if (cmd_match(buf
, "none"))
4024 err
= kstrtoull(buf
, 10, &n
);
4027 if (n
!= (sector_t
)n
)
4031 err
= mddev_lock(mddev
);
4034 if (mddev
->pers
&& !test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
4038 mddev
->recovery_cp
= n
;
4040 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
4042 mddev_unlock(mddev
);
4045 static struct md_sysfs_entry md_resync_start
=
4046 __ATTR_PREALLOC(resync_start
, S_IRUGO
|S_IWUSR
,
4047 resync_start_show
, resync_start_store
);
4050 * The array state can be:
4053 * No devices, no size, no level
4054 * Equivalent to STOP_ARRAY ioctl
4056 * May have some settings, but array is not active
4057 * all IO results in error
4058 * When written, doesn't tear down array, but just stops it
4059 * suspended (not supported yet)
4060 * All IO requests will block. The array can be reconfigured.
4061 * Writing this, if accepted, will block until array is quiescent
4063 * no resync can happen. no superblocks get written.
4064 * write requests fail
4066 * like readonly, but behaves like 'clean' on a write request.
4068 * clean - no pending writes, but otherwise active.
4069 * When written to inactive array, starts without resync
4070 * If a write request arrives then
4071 * if metadata is known, mark 'dirty' and switch to 'active'.
4072 * if not known, block and switch to write-pending
4073 * If written to an active array that has pending writes, then fails.
4075 * fully active: IO and resync can be happening.
4076 * When written to inactive array, starts with resync
4079 * clean, but writes are blocked waiting for 'active' to be written.
4082 * like active, but no writes have been seen for a while (100msec).
4085 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
4086 write_pending
, active_idle
, bad_word
};
4087 static char *array_states
[] = {
4088 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
4089 "write-pending", "active-idle", NULL
};
4091 static int match_word(const char *word
, char **list
)
4094 for (n
=0; list
[n
]; n
++)
4095 if (cmd_match(word
, list
[n
]))
4101 array_state_show(struct mddev
*mddev
, char *page
)
4103 enum array_state st
= inactive
;
4114 spin_lock(&mddev
->lock
);
4115 if (test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
))
4117 else if (mddev
->in_sync
)
4119 else if (mddev
->safemode
)
4123 spin_unlock(&mddev
->lock
);
4126 if (list_empty(&mddev
->disks
) &&
4127 mddev
->raid_disks
== 0 &&
4128 mddev
->dev_sectors
== 0)
4133 return sprintf(page
, "%s\n", array_states
[st
]);
4136 static int do_md_stop(struct mddev
*mddev
, int ro
, struct block_device
*bdev
);
4137 static int md_set_readonly(struct mddev
*mddev
, struct block_device
*bdev
);
4138 static int do_md_run(struct mddev
*mddev
);
4139 static int restart_array(struct mddev
*mddev
);
4142 array_state_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4145 enum array_state st
= match_word(buf
, array_states
);
4147 if (mddev
->pers
&& (st
== active
|| st
== clean
) && mddev
->ro
!= 1) {
4148 /* don't take reconfig_mutex when toggling between
4151 spin_lock(&mddev
->lock
);
4153 restart_array(mddev
);
4154 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
4155 md_wakeup_thread(mddev
->thread
);
4156 wake_up(&mddev
->sb_wait
);
4157 } else /* st == clean */ {
4158 restart_array(mddev
);
4159 if (!set_in_sync(mddev
))
4163 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4164 spin_unlock(&mddev
->lock
);
4167 err
= mddev_lock(mddev
);
4175 /* stopping an active array */
4176 err
= do_md_stop(mddev
, 0, NULL
);
4179 /* stopping an active array */
4181 err
= do_md_stop(mddev
, 2, NULL
);
4183 err
= 0; /* already inactive */
4186 break; /* not supported yet */
4189 err
= md_set_readonly(mddev
, NULL
);
4192 set_disk_ro(mddev
->gendisk
, 1);
4193 err
= do_md_run(mddev
);
4199 err
= md_set_readonly(mddev
, NULL
);
4200 else if (mddev
->ro
== 1)
4201 err
= restart_array(mddev
);
4204 set_disk_ro(mddev
->gendisk
, 0);
4208 err
= do_md_run(mddev
);
4213 err
= restart_array(mddev
);
4216 spin_lock(&mddev
->lock
);
4217 if (!set_in_sync(mddev
))
4219 spin_unlock(&mddev
->lock
);
4225 err
= restart_array(mddev
);
4228 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
4229 wake_up(&mddev
->sb_wait
);
4233 set_disk_ro(mddev
->gendisk
, 0);
4234 err
= do_md_run(mddev
);
4239 /* these cannot be set */
4244 if (mddev
->hold_active
== UNTIL_IOCTL
)
4245 mddev
->hold_active
= 0;
4246 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4248 mddev_unlock(mddev
);
4251 static struct md_sysfs_entry md_array_state
=
4252 __ATTR_PREALLOC(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
4255 max_corrected_read_errors_show(struct mddev
*mddev
, char *page
) {
4256 return sprintf(page
, "%d\n",
4257 atomic_read(&mddev
->max_corr_read_errors
));
4261 max_corrected_read_errors_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4266 rv
= kstrtouint(buf
, 10, &n
);
4269 atomic_set(&mddev
->max_corr_read_errors
, n
);
4273 static struct md_sysfs_entry max_corr_read_errors
=
4274 __ATTR(max_read_errors
, S_IRUGO
|S_IWUSR
, max_corrected_read_errors_show
,
4275 max_corrected_read_errors_store
);
4278 null_show(struct mddev
*mddev
, char *page
)
4284 new_dev_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4286 /* buf must be %d:%d\n? giving major and minor numbers */
4287 /* The new device is added to the array.
4288 * If the array has a persistent superblock, we read the
4289 * superblock to initialise info and check validity.
4290 * Otherwise, only checking done is that in bind_rdev_to_array,
4291 * which mainly checks size.
4294 int major
= simple_strtoul(buf
, &e
, 10);
4297 struct md_rdev
*rdev
;
4300 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
4302 minor
= simple_strtoul(e
+1, &e
, 10);
4303 if (*e
&& *e
!= '\n')
4305 dev
= MKDEV(major
, minor
);
4306 if (major
!= MAJOR(dev
) ||
4307 minor
!= MINOR(dev
))
4310 flush_workqueue(md_misc_wq
);
4312 err
= mddev_lock(mddev
);
4315 if (mddev
->persistent
) {
4316 rdev
= md_import_device(dev
, mddev
->major_version
,
4317 mddev
->minor_version
);
4318 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
4319 struct md_rdev
*rdev0
4320 = list_entry(mddev
->disks
.next
,
4321 struct md_rdev
, same_set
);
4322 err
= super_types
[mddev
->major_version
]
4323 .load_super(rdev
, rdev0
, mddev
->minor_version
);
4327 } else if (mddev
->external
)
4328 rdev
= md_import_device(dev
, -2, -1);
4330 rdev
= md_import_device(dev
, -1, -1);
4333 mddev_unlock(mddev
);
4334 return PTR_ERR(rdev
);
4336 err
= bind_rdev_to_array(rdev
, mddev
);
4340 mddev_unlock(mddev
);
4342 md_new_event(mddev
);
4343 return err
? err
: len
;
4346 static struct md_sysfs_entry md_new_device
=
4347 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
4350 bitmap_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4353 unsigned long chunk
, end_chunk
;
4356 err
= mddev_lock(mddev
);
4361 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4363 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
4364 if (buf
== end
) break;
4365 if (*end
== '-') { /* range */
4367 end_chunk
= simple_strtoul(buf
, &end
, 0);
4368 if (buf
== end
) break;
4370 if (*end
&& !isspace(*end
)) break;
4371 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
4372 buf
= skip_spaces(end
);
4374 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
4376 mddev_unlock(mddev
);
4380 static struct md_sysfs_entry md_bitmap
=
4381 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
4384 size_show(struct mddev
*mddev
, char *page
)
4386 return sprintf(page
, "%llu\n",
4387 (unsigned long long)mddev
->dev_sectors
/ 2);
4390 static int update_size(struct mddev
*mddev
, sector_t num_sectors
);
4393 size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4395 /* If array is inactive, we can reduce the component size, but
4396 * not increase it (except from 0).
4397 * If array is active, we can try an on-line resize
4400 int err
= strict_blocks_to_sectors(buf
, §ors
);
4404 err
= mddev_lock(mddev
);
4408 err
= update_size(mddev
, sectors
);
4410 md_update_sb(mddev
, 1);
4412 if (mddev
->dev_sectors
== 0 ||
4413 mddev
->dev_sectors
> sectors
)
4414 mddev
->dev_sectors
= sectors
;
4418 mddev_unlock(mddev
);
4419 return err
? err
: len
;
4422 static struct md_sysfs_entry md_size
=
4423 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
4425 /* Metadata version.
4427 * 'none' for arrays with no metadata (good luck...)
4428 * 'external' for arrays with externally managed metadata,
4429 * or N.M for internally known formats
4432 metadata_show(struct mddev
*mddev
, char *page
)
4434 if (mddev
->persistent
)
4435 return sprintf(page
, "%d.%d\n",
4436 mddev
->major_version
, mddev
->minor_version
);
4437 else if (mddev
->external
)
4438 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
4440 return sprintf(page
, "none\n");
4444 metadata_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4449 /* Changing the details of 'external' metadata is
4450 * always permitted. Otherwise there must be
4451 * no devices attached to the array.
4454 err
= mddev_lock(mddev
);
4458 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
4460 else if (!list_empty(&mddev
->disks
))
4464 if (cmd_match(buf
, "none")) {
4465 mddev
->persistent
= 0;
4466 mddev
->external
= 0;
4467 mddev
->major_version
= 0;
4468 mddev
->minor_version
= 90;
4471 if (strncmp(buf
, "external:", 9) == 0) {
4472 size_t namelen
= len
-9;
4473 if (namelen
>= sizeof(mddev
->metadata_type
))
4474 namelen
= sizeof(mddev
->metadata_type
)-1;
4475 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
4476 mddev
->metadata_type
[namelen
] = 0;
4477 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
4478 mddev
->metadata_type
[--namelen
] = 0;
4479 mddev
->persistent
= 0;
4480 mddev
->external
= 1;
4481 mddev
->major_version
= 0;
4482 mddev
->minor_version
= 90;
4485 major
= simple_strtoul(buf
, &e
, 10);
4487 if (e
==buf
|| *e
!= '.')
4490 minor
= simple_strtoul(buf
, &e
, 10);
4491 if (e
==buf
|| (*e
&& *e
!= '\n') )
4494 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
4496 mddev
->major_version
= major
;
4497 mddev
->minor_version
= minor
;
4498 mddev
->persistent
= 1;
4499 mddev
->external
= 0;
4502 mddev_unlock(mddev
);
4506 static struct md_sysfs_entry md_metadata
=
4507 __ATTR_PREALLOC(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
4510 action_show(struct mddev
*mddev
, char *page
)
4512 char *type
= "idle";
4513 unsigned long recovery
= mddev
->recovery
;
4514 if (test_bit(MD_RECOVERY_FROZEN
, &recovery
))
4516 else if (test_bit(MD_RECOVERY_RUNNING
, &recovery
) ||
4517 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &recovery
))) {
4518 if (test_bit(MD_RECOVERY_RESHAPE
, &recovery
))
4520 else if (test_bit(MD_RECOVERY_SYNC
, &recovery
)) {
4521 if (!test_bit(MD_RECOVERY_REQUESTED
, &recovery
))
4523 else if (test_bit(MD_RECOVERY_CHECK
, &recovery
))
4527 } else if (test_bit(MD_RECOVERY_RECOVER
, &recovery
))
4529 else if (mddev
->reshape_position
!= MaxSector
)
4532 return sprintf(page
, "%s\n", type
);
4536 action_store(struct mddev
*mddev
, const char *page
, size_t len
)
4538 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
4542 if (cmd_match(page
, "idle") || cmd_match(page
, "frozen")) {
4543 if (cmd_match(page
, "frozen"))
4544 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4546 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4547 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
4548 mddev_lock(mddev
) == 0) {
4549 flush_workqueue(md_misc_wq
);
4550 if (mddev
->sync_thread
) {
4551 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4552 md_reap_sync_thread(mddev
);
4554 mddev_unlock(mddev
);
4556 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4558 else if (cmd_match(page
, "resync"))
4559 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4560 else if (cmd_match(page
, "recover")) {
4561 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4562 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
4563 } else if (cmd_match(page
, "reshape")) {
4565 if (mddev
->pers
->start_reshape
== NULL
)
4567 err
= mddev_lock(mddev
);
4569 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4572 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4573 err
= mddev
->pers
->start_reshape(mddev
);
4575 mddev_unlock(mddev
);
4579 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4581 if (cmd_match(page
, "check"))
4582 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
4583 else if (!cmd_match(page
, "repair"))
4585 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4586 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
4587 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
4589 if (mddev
->ro
== 2) {
4590 /* A write to sync_action is enough to justify
4591 * canceling read-auto mode
4594 md_wakeup_thread(mddev
->sync_thread
);
4596 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4597 md_wakeup_thread(mddev
->thread
);
4598 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4602 static struct md_sysfs_entry md_scan_mode
=
4603 __ATTR_PREALLOC(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
4606 last_sync_action_show(struct mddev
*mddev
, char *page
)
4608 return sprintf(page
, "%s\n", mddev
->last_sync_action
);
4611 static struct md_sysfs_entry md_last_scan_mode
= __ATTR_RO(last_sync_action
);
4614 mismatch_cnt_show(struct mddev
*mddev
, char *page
)
4616 return sprintf(page
, "%llu\n",
4617 (unsigned long long)
4618 atomic64_read(&mddev
->resync_mismatches
));
4621 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
4624 sync_min_show(struct mddev
*mddev
, char *page
)
4626 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
4627 mddev
->sync_speed_min
? "local": "system");
4631 sync_min_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4636 if (strncmp(buf
, "system", 6)==0) {
4639 rv
= kstrtouint(buf
, 10, &min
);
4645 mddev
->sync_speed_min
= min
;
4649 static struct md_sysfs_entry md_sync_min
=
4650 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
4653 sync_max_show(struct mddev
*mddev
, char *page
)
4655 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
4656 mddev
->sync_speed_max
? "local": "system");
4660 sync_max_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4665 if (strncmp(buf
, "system", 6)==0) {
4668 rv
= kstrtouint(buf
, 10, &max
);
4674 mddev
->sync_speed_max
= max
;
4678 static struct md_sysfs_entry md_sync_max
=
4679 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
4682 degraded_show(struct mddev
*mddev
, char *page
)
4684 return sprintf(page
, "%d\n", mddev
->degraded
);
4686 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
4689 sync_force_parallel_show(struct mddev
*mddev
, char *page
)
4691 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
4695 sync_force_parallel_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4699 if (kstrtol(buf
, 10, &n
))
4702 if (n
!= 0 && n
!= 1)
4705 mddev
->parallel_resync
= n
;
4707 if (mddev
->sync_thread
)
4708 wake_up(&resync_wait
);
4713 /* force parallel resync, even with shared block devices */
4714 static struct md_sysfs_entry md_sync_force_parallel
=
4715 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
4716 sync_force_parallel_show
, sync_force_parallel_store
);
4719 sync_speed_show(struct mddev
*mddev
, char *page
)
4721 unsigned long resync
, dt
, db
;
4722 if (mddev
->curr_resync
== 0)
4723 return sprintf(page
, "none\n");
4724 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
4725 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
4727 db
= resync
- mddev
->resync_mark_cnt
;
4728 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
4731 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
4734 sync_completed_show(struct mddev
*mddev
, char *page
)
4736 unsigned long long max_sectors
, resync
;
4738 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4739 return sprintf(page
, "none\n");
4741 if (mddev
->curr_resync
== 1 ||
4742 mddev
->curr_resync
== 2)
4743 return sprintf(page
, "delayed\n");
4745 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
4746 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
4747 max_sectors
= mddev
->resync_max_sectors
;
4749 max_sectors
= mddev
->dev_sectors
;
4751 resync
= mddev
->curr_resync_completed
;
4752 return sprintf(page
, "%llu / %llu\n", resync
, max_sectors
);
4755 static struct md_sysfs_entry md_sync_completed
=
4756 __ATTR_PREALLOC(sync_completed
, S_IRUGO
, sync_completed_show
, NULL
);
4759 min_sync_show(struct mddev
*mddev
, char *page
)
4761 return sprintf(page
, "%llu\n",
4762 (unsigned long long)mddev
->resync_min
);
4765 min_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4767 unsigned long long min
;
4770 if (kstrtoull(buf
, 10, &min
))
4773 spin_lock(&mddev
->lock
);
4775 if (min
> mddev
->resync_max
)
4779 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4782 /* Round down to multiple of 4K for safety */
4783 mddev
->resync_min
= round_down(min
, 8);
4787 spin_unlock(&mddev
->lock
);
4791 static struct md_sysfs_entry md_min_sync
=
4792 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
4795 max_sync_show(struct mddev
*mddev
, char *page
)
4797 if (mddev
->resync_max
== MaxSector
)
4798 return sprintf(page
, "max\n");
4800 return sprintf(page
, "%llu\n",
4801 (unsigned long long)mddev
->resync_max
);
4804 max_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4807 spin_lock(&mddev
->lock
);
4808 if (strncmp(buf
, "max", 3) == 0)
4809 mddev
->resync_max
= MaxSector
;
4811 unsigned long long max
;
4815 if (kstrtoull(buf
, 10, &max
))
4817 if (max
< mddev
->resync_min
)
4821 if (max
< mddev
->resync_max
&&
4823 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4826 /* Must be a multiple of chunk_size */
4827 chunk
= mddev
->chunk_sectors
;
4829 sector_t temp
= max
;
4832 if (sector_div(temp
, chunk
))
4835 mddev
->resync_max
= max
;
4837 wake_up(&mddev
->recovery_wait
);
4840 spin_unlock(&mddev
->lock
);
4844 static struct md_sysfs_entry md_max_sync
=
4845 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
4848 suspend_lo_show(struct mddev
*mddev
, char *page
)
4850 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
4854 suspend_lo_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4856 unsigned long long new;
4859 err
= kstrtoull(buf
, 10, &new);
4862 if (new != (sector_t
)new)
4865 err
= mddev_lock(mddev
);
4869 if (mddev
->pers
== NULL
||
4870 mddev
->pers
->quiesce
== NULL
)
4872 mddev_suspend(mddev
);
4873 mddev
->suspend_lo
= new;
4874 mddev_resume(mddev
);
4878 mddev_unlock(mddev
);
4881 static struct md_sysfs_entry md_suspend_lo
=
4882 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
4885 suspend_hi_show(struct mddev
*mddev
, char *page
)
4887 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
4891 suspend_hi_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4893 unsigned long long new;
4896 err
= kstrtoull(buf
, 10, &new);
4899 if (new != (sector_t
)new)
4902 err
= mddev_lock(mddev
);
4906 if (mddev
->pers
== NULL
)
4909 mddev_suspend(mddev
);
4910 mddev
->suspend_hi
= new;
4911 mddev_resume(mddev
);
4915 mddev_unlock(mddev
);
4918 static struct md_sysfs_entry md_suspend_hi
=
4919 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
4922 reshape_position_show(struct mddev
*mddev
, char *page
)
4924 if (mddev
->reshape_position
!= MaxSector
)
4925 return sprintf(page
, "%llu\n",
4926 (unsigned long long)mddev
->reshape_position
);
4927 strcpy(page
, "none\n");
4932 reshape_position_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4934 struct md_rdev
*rdev
;
4935 unsigned long long new;
4938 err
= kstrtoull(buf
, 10, &new);
4941 if (new != (sector_t
)new)
4943 err
= mddev_lock(mddev
);
4949 mddev
->reshape_position
= new;
4950 mddev
->delta_disks
= 0;
4951 mddev
->reshape_backwards
= 0;
4952 mddev
->new_level
= mddev
->level
;
4953 mddev
->new_layout
= mddev
->layout
;
4954 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4955 rdev_for_each(rdev
, mddev
)
4956 rdev
->new_data_offset
= rdev
->data_offset
;
4959 mddev_unlock(mddev
);
4963 static struct md_sysfs_entry md_reshape_position
=
4964 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
4965 reshape_position_store
);
4968 reshape_direction_show(struct mddev
*mddev
, char *page
)
4970 return sprintf(page
, "%s\n",
4971 mddev
->reshape_backwards
? "backwards" : "forwards");
4975 reshape_direction_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4980 if (cmd_match(buf
, "forwards"))
4982 else if (cmd_match(buf
, "backwards"))
4986 if (mddev
->reshape_backwards
== backwards
)
4989 err
= mddev_lock(mddev
);
4992 /* check if we are allowed to change */
4993 if (mddev
->delta_disks
)
4995 else if (mddev
->persistent
&&
4996 mddev
->major_version
== 0)
4999 mddev
->reshape_backwards
= backwards
;
5000 mddev_unlock(mddev
);
5004 static struct md_sysfs_entry md_reshape_direction
=
5005 __ATTR(reshape_direction
, S_IRUGO
|S_IWUSR
, reshape_direction_show
,
5006 reshape_direction_store
);
5009 array_size_show(struct mddev
*mddev
, char *page
)
5011 if (mddev
->external_size
)
5012 return sprintf(page
, "%llu\n",
5013 (unsigned long long)mddev
->array_sectors
/2);
5015 return sprintf(page
, "default\n");
5019 array_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5024 err
= mddev_lock(mddev
);
5028 /* cluster raid doesn't support change array_sectors */
5029 if (mddev_is_clustered(mddev
)) {
5030 mddev_unlock(mddev
);
5034 if (strncmp(buf
, "default", 7) == 0) {
5036 sectors
= mddev
->pers
->size(mddev
, 0, 0);
5038 sectors
= mddev
->array_sectors
;
5040 mddev
->external_size
= 0;
5042 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
5044 else if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
5047 mddev
->external_size
= 1;
5051 mddev
->array_sectors
= sectors
;
5053 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
5054 revalidate_disk(mddev
->gendisk
);
5057 mddev_unlock(mddev
);
5061 static struct md_sysfs_entry md_array_size
=
5062 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
5066 consistency_policy_show(struct mddev
*mddev
, char *page
)
5070 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
)) {
5071 ret
= sprintf(page
, "journal\n");
5072 } else if (test_bit(MD_HAS_PPL
, &mddev
->flags
)) {
5073 ret
= sprintf(page
, "ppl\n");
5074 } else if (mddev
->bitmap
) {
5075 ret
= sprintf(page
, "bitmap\n");
5076 } else if (mddev
->pers
) {
5077 if (mddev
->pers
->sync_request
)
5078 ret
= sprintf(page
, "resync\n");
5080 ret
= sprintf(page
, "none\n");
5082 ret
= sprintf(page
, "unknown\n");
5089 consistency_policy_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5094 if (mddev
->pers
->change_consistency_policy
)
5095 err
= mddev
->pers
->change_consistency_policy(mddev
, buf
);
5098 } else if (mddev
->external
&& strncmp(buf
, "ppl", 3) == 0) {
5099 set_bit(MD_HAS_PPL
, &mddev
->flags
);
5104 return err
? err
: len
;
5107 static struct md_sysfs_entry md_consistency_policy
=
5108 __ATTR(consistency_policy
, S_IRUGO
| S_IWUSR
, consistency_policy_show
,
5109 consistency_policy_store
);
5111 static struct attribute
*md_default_attrs
[] = {
5114 &md_raid_disks
.attr
,
5115 &md_chunk_size
.attr
,
5117 &md_resync_start
.attr
,
5119 &md_new_device
.attr
,
5120 &md_safe_delay
.attr
,
5121 &md_array_state
.attr
,
5122 &md_reshape_position
.attr
,
5123 &md_reshape_direction
.attr
,
5124 &md_array_size
.attr
,
5125 &max_corr_read_errors
.attr
,
5126 &md_consistency_policy
.attr
,
5130 static struct attribute
*md_redundancy_attrs
[] = {
5132 &md_last_scan_mode
.attr
,
5133 &md_mismatches
.attr
,
5136 &md_sync_speed
.attr
,
5137 &md_sync_force_parallel
.attr
,
5138 &md_sync_completed
.attr
,
5141 &md_suspend_lo
.attr
,
5142 &md_suspend_hi
.attr
,
5147 static struct attribute_group md_redundancy_group
= {
5149 .attrs
= md_redundancy_attrs
,
5153 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
5155 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
5156 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
5161 spin_lock(&all_mddevs_lock
);
5162 if (list_empty(&mddev
->all_mddevs
)) {
5163 spin_unlock(&all_mddevs_lock
);
5167 spin_unlock(&all_mddevs_lock
);
5169 rv
= entry
->show(mddev
, page
);
5175 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
5176 const char *page
, size_t length
)
5178 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
5179 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
5184 if (!capable(CAP_SYS_ADMIN
))
5186 spin_lock(&all_mddevs_lock
);
5187 if (list_empty(&mddev
->all_mddevs
)) {
5188 spin_unlock(&all_mddevs_lock
);
5192 spin_unlock(&all_mddevs_lock
);
5193 rv
= entry
->store(mddev
, page
, length
);
5198 static void md_free(struct kobject
*ko
)
5200 struct mddev
*mddev
= container_of(ko
, struct mddev
, kobj
);
5202 if (mddev
->sysfs_state
)
5203 sysfs_put(mddev
->sysfs_state
);
5206 blk_cleanup_queue(mddev
->queue
);
5207 if (mddev
->gendisk
) {
5208 del_gendisk(mddev
->gendisk
);
5209 put_disk(mddev
->gendisk
);
5211 percpu_ref_exit(&mddev
->writes_pending
);
5216 static const struct sysfs_ops md_sysfs_ops
= {
5217 .show
= md_attr_show
,
5218 .store
= md_attr_store
,
5220 static struct kobj_type md_ktype
= {
5222 .sysfs_ops
= &md_sysfs_ops
,
5223 .default_attrs
= md_default_attrs
,
5228 static void mddev_delayed_delete(struct work_struct
*ws
)
5230 struct mddev
*mddev
= container_of(ws
, struct mddev
, del_work
);
5232 sysfs_remove_group(&mddev
->kobj
, &md_bitmap_group
);
5233 kobject_del(&mddev
->kobj
);
5234 kobject_put(&mddev
->kobj
);
5237 static void no_op(struct percpu_ref
*r
) {}
5239 int mddev_init_writes_pending(struct mddev
*mddev
)
5241 if (mddev
->writes_pending
.percpu_count_ptr
)
5243 if (percpu_ref_init(&mddev
->writes_pending
, no_op
, 0, GFP_KERNEL
) < 0)
5245 /* We want to start with the refcount at zero */
5246 percpu_ref_put(&mddev
->writes_pending
);
5249 EXPORT_SYMBOL_GPL(mddev_init_writes_pending
);
5251 static int md_alloc(dev_t dev
, char *name
)
5254 * If dev is zero, name is the name of a device to allocate with
5255 * an arbitrary minor number. It will be "md_???"
5256 * If dev is non-zero it must be a device number with a MAJOR of
5257 * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then
5258 * the device is being created by opening a node in /dev.
5259 * If "name" is not NULL, the device is being created by
5260 * writing to /sys/module/md_mod/parameters/new_array.
5262 static DEFINE_MUTEX(disks_mutex
);
5263 struct mddev
*mddev
= mddev_find(dev
);
5264 struct gendisk
*disk
;
5273 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
5274 shift
= partitioned
? MdpMinorShift
: 0;
5275 unit
= MINOR(mddev
->unit
) >> shift
;
5277 /* wait for any previous instance of this device to be
5278 * completely removed (mddev_delayed_delete).
5280 flush_workqueue(md_misc_wq
);
5282 mutex_lock(&disks_mutex
);
5288 /* Need to ensure that 'name' is not a duplicate.
5290 struct mddev
*mddev2
;
5291 spin_lock(&all_mddevs_lock
);
5293 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
5294 if (mddev2
->gendisk
&&
5295 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
5296 spin_unlock(&all_mddevs_lock
);
5299 spin_unlock(&all_mddevs_lock
);
5303 * Creating /dev/mdNNN via "newarray", so adjust hold_active.
5305 mddev
->hold_active
= UNTIL_STOP
;
5308 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
5311 mddev
->queue
->queuedata
= mddev
;
5313 blk_queue_make_request(mddev
->queue
, md_make_request
);
5314 blk_set_stacking_limits(&mddev
->queue
->limits
);
5316 disk
= alloc_disk(1 << shift
);
5318 blk_cleanup_queue(mddev
->queue
);
5319 mddev
->queue
= NULL
;
5322 disk
->major
= MAJOR(mddev
->unit
);
5323 disk
->first_minor
= unit
<< shift
;
5325 strcpy(disk
->disk_name
, name
);
5326 else if (partitioned
)
5327 sprintf(disk
->disk_name
, "md_d%d", unit
);
5329 sprintf(disk
->disk_name
, "md%d", unit
);
5330 disk
->fops
= &md_fops
;
5331 disk
->private_data
= mddev
;
5332 disk
->queue
= mddev
->queue
;
5333 blk_queue_write_cache(mddev
->queue
, true, true);
5334 /* Allow extended partitions. This makes the
5335 * 'mdp' device redundant, but we can't really
5338 disk
->flags
|= GENHD_FL_EXT_DEVT
;
5339 mddev
->gendisk
= disk
;
5340 /* As soon as we call add_disk(), another thread could get
5341 * through to md_open, so make sure it doesn't get too far
5343 mutex_lock(&mddev
->open_mutex
);
5346 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
,
5347 &disk_to_dev(disk
)->kobj
, "%s", "md");
5349 /* This isn't possible, but as kobject_init_and_add is marked
5350 * __must_check, we must do something with the result
5352 pr_debug("md: cannot register %s/md - name in use\n",
5356 if (mddev
->kobj
.sd
&&
5357 sysfs_create_group(&mddev
->kobj
, &md_bitmap_group
))
5358 pr_debug("pointless warning\n");
5359 mutex_unlock(&mddev
->open_mutex
);
5361 mutex_unlock(&disks_mutex
);
5362 if (!error
&& mddev
->kobj
.sd
) {
5363 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
5364 mddev
->sysfs_state
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "array_state");
5370 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
5373 md_alloc(dev
, NULL
);
5377 static int add_named_array(const char *val
, const struct kernel_param
*kp
)
5380 * val must be "md_*" or "mdNNN".
5381 * For "md_*" we allocate an array with a large free minor number, and
5382 * set the name to val. val must not already be an active name.
5383 * For "mdNNN" we allocate an array with the minor number NNN
5384 * which must not already be in use.
5386 int len
= strlen(val
);
5387 char buf
[DISK_NAME_LEN
];
5388 unsigned long devnum
;
5390 while (len
&& val
[len
-1] == '\n')
5392 if (len
>= DISK_NAME_LEN
)
5394 strlcpy(buf
, val
, len
+1);
5395 if (strncmp(buf
, "md_", 3) == 0)
5396 return md_alloc(0, buf
);
5397 if (strncmp(buf
, "md", 2) == 0 &&
5399 kstrtoul(buf
+2, 10, &devnum
) == 0 &&
5400 devnum
<= MINORMASK
)
5401 return md_alloc(MKDEV(MD_MAJOR
, devnum
), NULL
);
5406 static void md_safemode_timeout(struct timer_list
*t
)
5408 struct mddev
*mddev
= from_timer(mddev
, t
, safemode_timer
);
5410 mddev
->safemode
= 1;
5411 if (mddev
->external
)
5412 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5414 md_wakeup_thread(mddev
->thread
);
5417 static int start_dirty_degraded
;
5419 int md_run(struct mddev
*mddev
)
5422 struct md_rdev
*rdev
;
5423 struct md_personality
*pers
;
5425 if (list_empty(&mddev
->disks
))
5426 /* cannot run an array with no devices.. */
5431 /* Cannot run until previous stop completes properly */
5432 if (mddev
->sysfs_active
)
5436 * Analyze all RAID superblock(s)
5438 if (!mddev
->raid_disks
) {
5439 if (!mddev
->persistent
)
5444 if (mddev
->level
!= LEVEL_NONE
)
5445 request_module("md-level-%d", mddev
->level
);
5446 else if (mddev
->clevel
[0])
5447 request_module("md-%s", mddev
->clevel
);
5450 * Drop all container device buffers, from now on
5451 * the only valid external interface is through the md
5454 rdev_for_each(rdev
, mddev
) {
5455 if (test_bit(Faulty
, &rdev
->flags
))
5457 sync_blockdev(rdev
->bdev
);
5458 invalidate_bdev(rdev
->bdev
);
5459 if (mddev
->ro
!= 1 &&
5460 (bdev_read_only(rdev
->bdev
) ||
5461 bdev_read_only(rdev
->meta_bdev
))) {
5464 set_disk_ro(mddev
->gendisk
, 1);
5467 /* perform some consistency tests on the device.
5468 * We don't want the data to overlap the metadata,
5469 * Internal Bitmap issues have been handled elsewhere.
5471 if (rdev
->meta_bdev
) {
5472 /* Nothing to check */;
5473 } else if (rdev
->data_offset
< rdev
->sb_start
) {
5474 if (mddev
->dev_sectors
&&
5475 rdev
->data_offset
+ mddev
->dev_sectors
5477 pr_warn("md: %s: data overlaps metadata\n",
5482 if (rdev
->sb_start
+ rdev
->sb_size
/512
5483 > rdev
->data_offset
) {
5484 pr_warn("md: %s: metadata overlaps data\n",
5489 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
5492 if (mddev
->bio_set
== NULL
) {
5493 mddev
->bio_set
= bioset_create(BIO_POOL_SIZE
, 0, BIOSET_NEED_BVECS
);
5494 if (!mddev
->bio_set
)
5497 if (mddev
->sync_set
== NULL
) {
5498 mddev
->sync_set
= bioset_create(BIO_POOL_SIZE
, 0, BIOSET_NEED_BVECS
);
5499 if (!mddev
->sync_set
)
5503 spin_lock(&pers_lock
);
5504 pers
= find_pers(mddev
->level
, mddev
->clevel
);
5505 if (!pers
|| !try_module_get(pers
->owner
)) {
5506 spin_unlock(&pers_lock
);
5507 if (mddev
->level
!= LEVEL_NONE
)
5508 pr_warn("md: personality for level %d is not loaded!\n",
5511 pr_warn("md: personality for level %s is not loaded!\n",
5515 spin_unlock(&pers_lock
);
5516 if (mddev
->level
!= pers
->level
) {
5517 mddev
->level
= pers
->level
;
5518 mddev
->new_level
= pers
->level
;
5520 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
5522 if (mddev
->reshape_position
!= MaxSector
&&
5523 pers
->start_reshape
== NULL
) {
5524 /* This personality cannot handle reshaping... */
5525 module_put(pers
->owner
);
5529 if (pers
->sync_request
) {
5530 /* Warn if this is a potentially silly
5533 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
5534 struct md_rdev
*rdev2
;
5537 rdev_for_each(rdev
, mddev
)
5538 rdev_for_each(rdev2
, mddev
) {
5540 rdev
->bdev
->bd_contains
==
5541 rdev2
->bdev
->bd_contains
) {
5542 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5544 bdevname(rdev
->bdev
,b
),
5545 bdevname(rdev2
->bdev
,b2
));
5551 pr_warn("True protection against single-disk failure might be compromised.\n");
5554 mddev
->recovery
= 0;
5555 /* may be over-ridden by personality */
5556 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
5558 mddev
->ok_start_degraded
= start_dirty_degraded
;
5560 if (start_readonly
&& mddev
->ro
== 0)
5561 mddev
->ro
= 2; /* read-only, but switch on first write */
5564 * NOTE: some pers->run(), for example r5l_recovery_log(), wakes
5565 * up mddev->thread. It is important to initialize critical
5566 * resources for mddev->thread BEFORE calling pers->run().
5568 err
= pers
->run(mddev
);
5570 pr_warn("md: pers->run() failed ...\n");
5571 else if (pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
5572 WARN_ONCE(!mddev
->external_size
,
5573 "%s: default size too small, but 'external_size' not in effect?\n",
5575 pr_warn("md: invalid array_size %llu > default size %llu\n",
5576 (unsigned long long)mddev
->array_sectors
/ 2,
5577 (unsigned long long)pers
->size(mddev
, 0, 0) / 2);
5580 if (err
== 0 && pers
->sync_request
&&
5581 (mddev
->bitmap_info
.file
|| mddev
->bitmap_info
.offset
)) {
5582 struct bitmap
*bitmap
;
5584 bitmap
= bitmap_create(mddev
, -1);
5585 if (IS_ERR(bitmap
)) {
5586 err
= PTR_ERR(bitmap
);
5587 pr_warn("%s: failed to create bitmap (%d)\n",
5588 mdname(mddev
), err
);
5590 mddev
->bitmap
= bitmap
;
5594 mddev_detach(mddev
);
5596 pers
->free(mddev
, mddev
->private);
5597 mddev
->private = NULL
;
5598 module_put(pers
->owner
);
5599 bitmap_destroy(mddev
);
5605 rdev_for_each(rdev
, mddev
) {
5606 if (rdev
->raid_disk
>= 0 &&
5607 !blk_queue_nonrot(bdev_get_queue(rdev
->bdev
))) {
5612 if (mddev
->degraded
)
5615 queue_flag_set_unlocked(QUEUE_FLAG_NONROT
, mddev
->queue
);
5617 queue_flag_clear_unlocked(QUEUE_FLAG_NONROT
, mddev
->queue
);
5618 mddev
->queue
->backing_dev_info
->congested_data
= mddev
;
5619 mddev
->queue
->backing_dev_info
->congested_fn
= md_congested
;
5621 if (pers
->sync_request
) {
5622 if (mddev
->kobj
.sd
&&
5623 sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
5624 pr_warn("md: cannot register extra attributes for %s\n",
5626 mddev
->sysfs_action
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "sync_action");
5627 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
5630 atomic_set(&mddev
->max_corr_read_errors
,
5631 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS
);
5632 mddev
->safemode
= 0;
5633 if (mddev_is_clustered(mddev
))
5634 mddev
->safemode_delay
= 0;
5636 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
5639 spin_lock(&mddev
->lock
);
5641 spin_unlock(&mddev
->lock
);
5642 rdev_for_each(rdev
, mddev
)
5643 if (rdev
->raid_disk
>= 0)
5644 if (sysfs_link_rdev(mddev
, rdev
))
5645 /* failure here is OK */;
5647 if (mddev
->degraded
&& !mddev
->ro
)
5648 /* This ensures that recovering status is reported immediately
5649 * via sysfs - until a lack of spares is confirmed.
5651 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5652 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5654 if (mddev
->sb_flags
)
5655 md_update_sb(mddev
, 0);
5657 md_new_event(mddev
);
5658 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5659 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
5660 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
5663 EXPORT_SYMBOL_GPL(md_run
);
5665 static int do_md_run(struct mddev
*mddev
)
5669 err
= md_run(mddev
);
5672 err
= bitmap_load(mddev
);
5674 bitmap_destroy(mddev
);
5678 if (mddev_is_clustered(mddev
))
5679 md_allow_write(mddev
);
5681 md_wakeup_thread(mddev
->thread
);
5682 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
5684 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
5685 revalidate_disk(mddev
->gendisk
);
5687 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
5692 static int restart_array(struct mddev
*mddev
)
5694 struct gendisk
*disk
= mddev
->gendisk
;
5695 struct md_rdev
*rdev
;
5696 bool has_journal
= false;
5697 bool has_readonly
= false;
5699 /* Complain if it has no devices */
5700 if (list_empty(&mddev
->disks
))
5708 rdev_for_each_rcu(rdev
, mddev
) {
5709 if (test_bit(Journal
, &rdev
->flags
) &&
5710 !test_bit(Faulty
, &rdev
->flags
))
5712 if (bdev_read_only(rdev
->bdev
))
5713 has_readonly
= true;
5716 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
) && !has_journal
)
5717 /* Don't restart rw with journal missing/faulty */
5722 mddev
->safemode
= 0;
5724 set_disk_ro(disk
, 0);
5725 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev
));
5726 /* Kick recovery or resync if necessary */
5727 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5728 md_wakeup_thread(mddev
->thread
);
5729 md_wakeup_thread(mddev
->sync_thread
);
5730 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5734 static void md_clean(struct mddev
*mddev
)
5736 mddev
->array_sectors
= 0;
5737 mddev
->external_size
= 0;
5738 mddev
->dev_sectors
= 0;
5739 mddev
->raid_disks
= 0;
5740 mddev
->recovery_cp
= 0;
5741 mddev
->resync_min
= 0;
5742 mddev
->resync_max
= MaxSector
;
5743 mddev
->reshape_position
= MaxSector
;
5744 mddev
->external
= 0;
5745 mddev
->persistent
= 0;
5746 mddev
->level
= LEVEL_NONE
;
5747 mddev
->clevel
[0] = 0;
5749 mddev
->sb_flags
= 0;
5751 mddev
->metadata_type
[0] = 0;
5752 mddev
->chunk_sectors
= 0;
5753 mddev
->ctime
= mddev
->utime
= 0;
5755 mddev
->max_disks
= 0;
5757 mddev
->can_decrease_events
= 0;
5758 mddev
->delta_disks
= 0;
5759 mddev
->reshape_backwards
= 0;
5760 mddev
->new_level
= LEVEL_NONE
;
5761 mddev
->new_layout
= 0;
5762 mddev
->new_chunk_sectors
= 0;
5763 mddev
->curr_resync
= 0;
5764 atomic64_set(&mddev
->resync_mismatches
, 0);
5765 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
5766 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
5767 mddev
->recovery
= 0;
5770 mddev
->degraded
= 0;
5771 mddev
->safemode
= 0;
5772 mddev
->private = NULL
;
5773 mddev
->cluster_info
= NULL
;
5774 mddev
->bitmap_info
.offset
= 0;
5775 mddev
->bitmap_info
.default_offset
= 0;
5776 mddev
->bitmap_info
.default_space
= 0;
5777 mddev
->bitmap_info
.chunksize
= 0;
5778 mddev
->bitmap_info
.daemon_sleep
= 0;
5779 mddev
->bitmap_info
.max_write_behind
= 0;
5780 mddev
->bitmap_info
.nodes
= 0;
5783 static void __md_stop_writes(struct mddev
*mddev
)
5785 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5786 flush_workqueue(md_misc_wq
);
5787 if (mddev
->sync_thread
) {
5788 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5789 md_reap_sync_thread(mddev
);
5792 del_timer_sync(&mddev
->safemode_timer
);
5794 if (mddev
->pers
&& mddev
->pers
->quiesce
) {
5795 mddev
->pers
->quiesce(mddev
, 1);
5796 mddev
->pers
->quiesce(mddev
, 0);
5798 bitmap_flush(mddev
);
5800 if (mddev
->ro
== 0 &&
5801 ((!mddev
->in_sync
&& !mddev_is_clustered(mddev
)) ||
5803 /* mark array as shutdown cleanly */
5804 if (!mddev_is_clustered(mddev
))
5806 md_update_sb(mddev
, 1);
5810 void md_stop_writes(struct mddev
*mddev
)
5812 mddev_lock_nointr(mddev
);
5813 __md_stop_writes(mddev
);
5814 mddev_unlock(mddev
);
5816 EXPORT_SYMBOL_GPL(md_stop_writes
);
5818 static void mddev_detach(struct mddev
*mddev
)
5820 bitmap_wait_behind_writes(mddev
);
5821 if (mddev
->pers
&& mddev
->pers
->quiesce
) {
5822 mddev
->pers
->quiesce(mddev
, 1);
5823 mddev
->pers
->quiesce(mddev
, 0);
5825 md_unregister_thread(&mddev
->thread
);
5827 blk_sync_queue(mddev
->queue
); /* the unplug fn references 'conf'*/
5830 static void __md_stop(struct mddev
*mddev
)
5832 struct md_personality
*pers
= mddev
->pers
;
5833 bitmap_destroy(mddev
);
5834 mddev_detach(mddev
);
5835 /* Ensure ->event_work is done */
5836 flush_workqueue(md_misc_wq
);
5837 spin_lock(&mddev
->lock
);
5839 spin_unlock(&mddev
->lock
);
5840 pers
->free(mddev
, mddev
->private);
5841 mddev
->private = NULL
;
5842 if (pers
->sync_request
&& mddev
->to_remove
== NULL
)
5843 mddev
->to_remove
= &md_redundancy_group
;
5844 module_put(pers
->owner
);
5845 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5848 void md_stop(struct mddev
*mddev
)
5850 /* stop the array and free an attached data structures.
5851 * This is called from dm-raid
5854 if (mddev
->bio_set
) {
5855 bioset_free(mddev
->bio_set
);
5856 mddev
->bio_set
= NULL
;
5858 if (mddev
->sync_set
) {
5859 bioset_free(mddev
->sync_set
);
5860 mddev
->sync_set
= NULL
;
5864 EXPORT_SYMBOL_GPL(md_stop
);
5866 static int md_set_readonly(struct mddev
*mddev
, struct block_device
*bdev
)
5871 if (!test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
)) {
5873 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5874 md_wakeup_thread(mddev
->thread
);
5876 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
5877 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5878 if (mddev
->sync_thread
)
5879 /* Thread might be blocked waiting for metadata update
5880 * which will now never happen */
5881 wake_up_process(mddev
->sync_thread
->tsk
);
5883 if (mddev
->external
&& test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
))
5885 mddev_unlock(mddev
);
5886 wait_event(resync_wait
, !test_bit(MD_RECOVERY_RUNNING
,
5888 wait_event(mddev
->sb_wait
,
5889 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
5890 mddev_lock_nointr(mddev
);
5892 mutex_lock(&mddev
->open_mutex
);
5893 if ((mddev
->pers
&& atomic_read(&mddev
->openers
) > !!bdev
) ||
5894 mddev
->sync_thread
||
5895 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
)) {
5896 pr_warn("md: %s still in use.\n",mdname(mddev
));
5898 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5899 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5900 md_wakeup_thread(mddev
->thread
);
5906 __md_stop_writes(mddev
);
5912 set_disk_ro(mddev
->gendisk
, 1);
5913 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5914 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5915 md_wakeup_thread(mddev
->thread
);
5916 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5920 mutex_unlock(&mddev
->open_mutex
);
5925 * 0 - completely stop and dis-assemble array
5926 * 2 - stop but do not disassemble array
5928 static int do_md_stop(struct mddev
*mddev
, int mode
,
5929 struct block_device
*bdev
)
5931 struct gendisk
*disk
= mddev
->gendisk
;
5932 struct md_rdev
*rdev
;
5935 if (!test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
)) {
5937 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5938 md_wakeup_thread(mddev
->thread
);
5940 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
5941 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5942 if (mddev
->sync_thread
)
5943 /* Thread might be blocked waiting for metadata update
5944 * which will now never happen */
5945 wake_up_process(mddev
->sync_thread
->tsk
);
5947 mddev_unlock(mddev
);
5948 wait_event(resync_wait
, (mddev
->sync_thread
== NULL
&&
5949 !test_bit(MD_RECOVERY_RUNNING
,
5950 &mddev
->recovery
)));
5951 mddev_lock_nointr(mddev
);
5953 mutex_lock(&mddev
->open_mutex
);
5954 if ((mddev
->pers
&& atomic_read(&mddev
->openers
) > !!bdev
) ||
5955 mddev
->sysfs_active
||
5956 mddev
->sync_thread
||
5957 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
)) {
5958 pr_warn("md: %s still in use.\n",mdname(mddev
));
5959 mutex_unlock(&mddev
->open_mutex
);
5961 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5962 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5963 md_wakeup_thread(mddev
->thread
);
5969 set_disk_ro(disk
, 0);
5971 __md_stop_writes(mddev
);
5973 mddev
->queue
->backing_dev_info
->congested_fn
= NULL
;
5975 /* tell userspace to handle 'inactive' */
5976 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5978 rdev_for_each(rdev
, mddev
)
5979 if (rdev
->raid_disk
>= 0)
5980 sysfs_unlink_rdev(mddev
, rdev
);
5982 set_capacity(disk
, 0);
5983 mutex_unlock(&mddev
->open_mutex
);
5985 revalidate_disk(disk
);
5990 mutex_unlock(&mddev
->open_mutex
);
5992 * Free resources if final stop
5995 pr_info("md: %s stopped.\n", mdname(mddev
));
5997 if (mddev
->bitmap_info
.file
) {
5998 struct file
*f
= mddev
->bitmap_info
.file
;
5999 spin_lock(&mddev
->lock
);
6000 mddev
->bitmap_info
.file
= NULL
;
6001 spin_unlock(&mddev
->lock
);
6004 mddev
->bitmap_info
.offset
= 0;
6006 export_array(mddev
);
6009 if (mddev
->hold_active
== UNTIL_STOP
)
6010 mddev
->hold_active
= 0;
6012 md_new_event(mddev
);
6013 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6018 static void autorun_array(struct mddev
*mddev
)
6020 struct md_rdev
*rdev
;
6023 if (list_empty(&mddev
->disks
))
6026 pr_info("md: running: ");
6028 rdev_for_each(rdev
, mddev
) {
6029 char b
[BDEVNAME_SIZE
];
6030 pr_cont("<%s>", bdevname(rdev
->bdev
,b
));
6034 err
= do_md_run(mddev
);
6036 pr_warn("md: do_md_run() returned %d\n", err
);
6037 do_md_stop(mddev
, 0, NULL
);
6042 * lets try to run arrays based on all disks that have arrived
6043 * until now. (those are in pending_raid_disks)
6045 * the method: pick the first pending disk, collect all disks with
6046 * the same UUID, remove all from the pending list and put them into
6047 * the 'same_array' list. Then order this list based on superblock
6048 * update time (freshest comes first), kick out 'old' disks and
6049 * compare superblocks. If everything's fine then run it.
6051 * If "unit" is allocated, then bump its reference count
6053 static void autorun_devices(int part
)
6055 struct md_rdev
*rdev0
, *rdev
, *tmp
;
6056 struct mddev
*mddev
;
6057 char b
[BDEVNAME_SIZE
];
6059 pr_info("md: autorun ...\n");
6060 while (!list_empty(&pending_raid_disks
)) {
6063 LIST_HEAD(candidates
);
6064 rdev0
= list_entry(pending_raid_disks
.next
,
6065 struct md_rdev
, same_set
);
6067 pr_debug("md: considering %s ...\n", bdevname(rdev0
->bdev
,b
));
6068 INIT_LIST_HEAD(&candidates
);
6069 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
6070 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
6071 pr_debug("md: adding %s ...\n",
6072 bdevname(rdev
->bdev
,b
));
6073 list_move(&rdev
->same_set
, &candidates
);
6076 * now we have a set of devices, with all of them having
6077 * mostly sane superblocks. It's time to allocate the
6081 dev
= MKDEV(mdp_major
,
6082 rdev0
->preferred_minor
<< MdpMinorShift
);
6083 unit
= MINOR(dev
) >> MdpMinorShift
;
6085 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
6088 if (rdev0
->preferred_minor
!= unit
) {
6089 pr_warn("md: unit number in %s is bad: %d\n",
6090 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
6094 md_probe(dev
, NULL
, NULL
);
6095 mddev
= mddev_find(dev
);
6096 if (!mddev
|| !mddev
->gendisk
) {
6101 if (mddev_lock(mddev
))
6102 pr_warn("md: %s locked, cannot run\n", mdname(mddev
));
6103 else if (mddev
->raid_disks
|| mddev
->major_version
6104 || !list_empty(&mddev
->disks
)) {
6105 pr_warn("md: %s already running, cannot run %s\n",
6106 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
6107 mddev_unlock(mddev
);
6109 pr_debug("md: created %s\n", mdname(mddev
));
6110 mddev
->persistent
= 1;
6111 rdev_for_each_list(rdev
, tmp
, &candidates
) {
6112 list_del_init(&rdev
->same_set
);
6113 if (bind_rdev_to_array(rdev
, mddev
))
6116 autorun_array(mddev
);
6117 mddev_unlock(mddev
);
6119 /* on success, candidates will be empty, on error
6122 rdev_for_each_list(rdev
, tmp
, &candidates
) {
6123 list_del_init(&rdev
->same_set
);
6128 pr_info("md: ... autorun DONE.\n");
6130 #endif /* !MODULE */
6132 static int get_version(void __user
*arg
)
6136 ver
.major
= MD_MAJOR_VERSION
;
6137 ver
.minor
= MD_MINOR_VERSION
;
6138 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
6140 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
6146 static int get_array_info(struct mddev
*mddev
, void __user
*arg
)
6148 mdu_array_info_t info
;
6149 int nr
,working
,insync
,failed
,spare
;
6150 struct md_rdev
*rdev
;
6152 nr
= working
= insync
= failed
= spare
= 0;
6154 rdev_for_each_rcu(rdev
, mddev
) {
6156 if (test_bit(Faulty
, &rdev
->flags
))
6160 if (test_bit(In_sync
, &rdev
->flags
))
6162 else if (test_bit(Journal
, &rdev
->flags
))
6163 /* TODO: add journal count to md_u.h */
6171 info
.major_version
= mddev
->major_version
;
6172 info
.minor_version
= mddev
->minor_version
;
6173 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
6174 info
.ctime
= clamp_t(time64_t
, mddev
->ctime
, 0, U32_MAX
);
6175 info
.level
= mddev
->level
;
6176 info
.size
= mddev
->dev_sectors
/ 2;
6177 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
6180 info
.raid_disks
= mddev
->raid_disks
;
6181 info
.md_minor
= mddev
->md_minor
;
6182 info
.not_persistent
= !mddev
->persistent
;
6184 info
.utime
= clamp_t(time64_t
, mddev
->utime
, 0, U32_MAX
);
6187 info
.state
= (1<<MD_SB_CLEAN
);
6188 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
6189 info
.state
|= (1<<MD_SB_BITMAP_PRESENT
);
6190 if (mddev_is_clustered(mddev
))
6191 info
.state
|= (1<<MD_SB_CLUSTERED
);
6192 info
.active_disks
= insync
;
6193 info
.working_disks
= working
;
6194 info
.failed_disks
= failed
;
6195 info
.spare_disks
= spare
;
6197 info
.layout
= mddev
->layout
;
6198 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
6200 if (copy_to_user(arg
, &info
, sizeof(info
)))
6206 static int get_bitmap_file(struct mddev
*mddev
, void __user
* arg
)
6208 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
6212 file
= kzalloc(sizeof(*file
), GFP_NOIO
);
6217 spin_lock(&mddev
->lock
);
6218 /* bitmap enabled */
6219 if (mddev
->bitmap_info
.file
) {
6220 ptr
= file_path(mddev
->bitmap_info
.file
, file
->pathname
,
6221 sizeof(file
->pathname
));
6225 memmove(file
->pathname
, ptr
,
6226 sizeof(file
->pathname
)-(ptr
-file
->pathname
));
6228 spin_unlock(&mddev
->lock
);
6231 copy_to_user(arg
, file
, sizeof(*file
)))
6238 static int get_disk_info(struct mddev
*mddev
, void __user
* arg
)
6240 mdu_disk_info_t info
;
6241 struct md_rdev
*rdev
;
6243 if (copy_from_user(&info
, arg
, sizeof(info
)))
6247 rdev
= md_find_rdev_nr_rcu(mddev
, info
.number
);
6249 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
6250 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
6251 info
.raid_disk
= rdev
->raid_disk
;
6253 if (test_bit(Faulty
, &rdev
->flags
))
6254 info
.state
|= (1<<MD_DISK_FAULTY
);
6255 else if (test_bit(In_sync
, &rdev
->flags
)) {
6256 info
.state
|= (1<<MD_DISK_ACTIVE
);
6257 info
.state
|= (1<<MD_DISK_SYNC
);
6259 if (test_bit(Journal
, &rdev
->flags
))
6260 info
.state
|= (1<<MD_DISK_JOURNAL
);
6261 if (test_bit(WriteMostly
, &rdev
->flags
))
6262 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
6263 if (test_bit(FailFast
, &rdev
->flags
))
6264 info
.state
|= (1<<MD_DISK_FAILFAST
);
6266 info
.major
= info
.minor
= 0;
6267 info
.raid_disk
= -1;
6268 info
.state
= (1<<MD_DISK_REMOVED
);
6272 if (copy_to_user(arg
, &info
, sizeof(info
)))
6278 static int add_new_disk(struct mddev
*mddev
, mdu_disk_info_t
*info
)
6280 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
6281 struct md_rdev
*rdev
;
6282 dev_t dev
= MKDEV(info
->major
,info
->minor
);
6284 if (mddev_is_clustered(mddev
) &&
6285 !(info
->state
& ((1 << MD_DISK_CLUSTER_ADD
) | (1 << MD_DISK_CANDIDATE
)))) {
6286 pr_warn("%s: Cannot add to clustered mddev.\n",
6291 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
6294 if (!mddev
->raid_disks
) {
6296 /* expecting a device which has a superblock */
6297 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
6299 pr_warn("md: md_import_device returned %ld\n",
6301 return PTR_ERR(rdev
);
6303 if (!list_empty(&mddev
->disks
)) {
6304 struct md_rdev
*rdev0
6305 = list_entry(mddev
->disks
.next
,
6306 struct md_rdev
, same_set
);
6307 err
= super_types
[mddev
->major_version
]
6308 .load_super(rdev
, rdev0
, mddev
->minor_version
);
6310 pr_warn("md: %s has different UUID to %s\n",
6311 bdevname(rdev
->bdev
,b
),
6312 bdevname(rdev0
->bdev
,b2
));
6317 err
= bind_rdev_to_array(rdev
, mddev
);
6324 * add_new_disk can be used once the array is assembled
6325 * to add "hot spares". They must already have a superblock
6330 if (!mddev
->pers
->hot_add_disk
) {
6331 pr_warn("%s: personality does not support diskops!\n",
6335 if (mddev
->persistent
)
6336 rdev
= md_import_device(dev
, mddev
->major_version
,
6337 mddev
->minor_version
);
6339 rdev
= md_import_device(dev
, -1, -1);
6341 pr_warn("md: md_import_device returned %ld\n",
6343 return PTR_ERR(rdev
);
6345 /* set saved_raid_disk if appropriate */
6346 if (!mddev
->persistent
) {
6347 if (info
->state
& (1<<MD_DISK_SYNC
) &&
6348 info
->raid_disk
< mddev
->raid_disks
) {
6349 rdev
->raid_disk
= info
->raid_disk
;
6350 set_bit(In_sync
, &rdev
->flags
);
6351 clear_bit(Bitmap_sync
, &rdev
->flags
);
6353 rdev
->raid_disk
= -1;
6354 rdev
->saved_raid_disk
= rdev
->raid_disk
;
6356 super_types
[mddev
->major_version
].
6357 validate_super(mddev
, rdev
);
6358 if ((info
->state
& (1<<MD_DISK_SYNC
)) &&
6359 rdev
->raid_disk
!= info
->raid_disk
) {
6360 /* This was a hot-add request, but events doesn't
6361 * match, so reject it.
6367 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
6368 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
6369 set_bit(WriteMostly
, &rdev
->flags
);
6371 clear_bit(WriteMostly
, &rdev
->flags
);
6372 if (info
->state
& (1<<MD_DISK_FAILFAST
))
6373 set_bit(FailFast
, &rdev
->flags
);
6375 clear_bit(FailFast
, &rdev
->flags
);
6377 if (info
->state
& (1<<MD_DISK_JOURNAL
)) {
6378 struct md_rdev
*rdev2
;
6379 bool has_journal
= false;
6381 /* make sure no existing journal disk */
6382 rdev_for_each(rdev2
, mddev
) {
6383 if (test_bit(Journal
, &rdev2
->flags
)) {
6388 if (has_journal
|| mddev
->bitmap
) {
6392 set_bit(Journal
, &rdev
->flags
);
6395 * check whether the device shows up in other nodes
6397 if (mddev_is_clustered(mddev
)) {
6398 if (info
->state
& (1 << MD_DISK_CANDIDATE
))
6399 set_bit(Candidate
, &rdev
->flags
);
6400 else if (info
->state
& (1 << MD_DISK_CLUSTER_ADD
)) {
6401 /* --add initiated by this node */
6402 err
= md_cluster_ops
->add_new_disk(mddev
, rdev
);
6410 rdev
->raid_disk
= -1;
6411 err
= bind_rdev_to_array(rdev
, mddev
);
6416 if (mddev_is_clustered(mddev
)) {
6417 if (info
->state
& (1 << MD_DISK_CANDIDATE
)) {
6419 err
= md_cluster_ops
->new_disk_ack(mddev
,
6422 md_kick_rdev_from_array(rdev
);
6426 md_cluster_ops
->add_new_disk_cancel(mddev
);
6428 err
= add_bound_rdev(rdev
);
6432 err
= add_bound_rdev(rdev
);
6437 /* otherwise, add_new_disk is only allowed
6438 * for major_version==0 superblocks
6440 if (mddev
->major_version
!= 0) {
6441 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev
));
6445 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
6447 rdev
= md_import_device(dev
, -1, 0);
6449 pr_warn("md: error, md_import_device() returned %ld\n",
6451 return PTR_ERR(rdev
);
6453 rdev
->desc_nr
= info
->number
;
6454 if (info
->raid_disk
< mddev
->raid_disks
)
6455 rdev
->raid_disk
= info
->raid_disk
;
6457 rdev
->raid_disk
= -1;
6459 if (rdev
->raid_disk
< mddev
->raid_disks
)
6460 if (info
->state
& (1<<MD_DISK_SYNC
))
6461 set_bit(In_sync
, &rdev
->flags
);
6463 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
6464 set_bit(WriteMostly
, &rdev
->flags
);
6465 if (info
->state
& (1<<MD_DISK_FAILFAST
))
6466 set_bit(FailFast
, &rdev
->flags
);
6468 if (!mddev
->persistent
) {
6469 pr_debug("md: nonpersistent superblock ...\n");
6470 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
6472 rdev
->sb_start
= calc_dev_sboffset(rdev
);
6473 rdev
->sectors
= rdev
->sb_start
;
6475 err
= bind_rdev_to_array(rdev
, mddev
);
6485 static int hot_remove_disk(struct mddev
*mddev
, dev_t dev
)
6487 char b
[BDEVNAME_SIZE
];
6488 struct md_rdev
*rdev
;
6490 rdev
= find_rdev(mddev
, dev
);
6494 if (rdev
->raid_disk
< 0)
6497 clear_bit(Blocked
, &rdev
->flags
);
6498 remove_and_add_spares(mddev
, rdev
);
6500 if (rdev
->raid_disk
>= 0)
6504 if (mddev_is_clustered(mddev
))
6505 md_cluster_ops
->remove_disk(mddev
, rdev
);
6507 md_kick_rdev_from_array(rdev
);
6508 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
6510 md_wakeup_thread(mddev
->thread
);
6512 md_update_sb(mddev
, 1);
6513 md_new_event(mddev
);
6517 pr_debug("md: cannot remove active disk %s from %s ...\n",
6518 bdevname(rdev
->bdev
,b
), mdname(mddev
));
6522 static int hot_add_disk(struct mddev
*mddev
, dev_t dev
)
6524 char b
[BDEVNAME_SIZE
];
6526 struct md_rdev
*rdev
;
6531 if (mddev
->major_version
!= 0) {
6532 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
6536 if (!mddev
->pers
->hot_add_disk
) {
6537 pr_warn("%s: personality does not support diskops!\n",
6542 rdev
= md_import_device(dev
, -1, 0);
6544 pr_warn("md: error, md_import_device() returned %ld\n",
6549 if (mddev
->persistent
)
6550 rdev
->sb_start
= calc_dev_sboffset(rdev
);
6552 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
6554 rdev
->sectors
= rdev
->sb_start
;
6556 if (test_bit(Faulty
, &rdev
->flags
)) {
6557 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
6558 bdevname(rdev
->bdev
,b
), mdname(mddev
));
6563 clear_bit(In_sync
, &rdev
->flags
);
6565 rdev
->saved_raid_disk
= -1;
6566 err
= bind_rdev_to_array(rdev
, mddev
);
6571 * The rest should better be atomic, we can have disk failures
6572 * noticed in interrupt contexts ...
6575 rdev
->raid_disk
= -1;
6577 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
6579 md_update_sb(mddev
, 1);
6581 * Kick recovery, maybe this spare has to be added to the
6582 * array immediately.
6584 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6585 md_wakeup_thread(mddev
->thread
);
6586 md_new_event(mddev
);
6594 static int set_bitmap_file(struct mddev
*mddev
, int fd
)
6599 if (!mddev
->pers
->quiesce
|| !mddev
->thread
)
6601 if (mddev
->recovery
|| mddev
->sync_thread
)
6603 /* we should be able to change the bitmap.. */
6607 struct inode
*inode
;
6610 if (mddev
->bitmap
|| mddev
->bitmap_info
.file
)
6611 return -EEXIST
; /* cannot add when bitmap is present */
6615 pr_warn("%s: error: failed to get bitmap file\n",
6620 inode
= f
->f_mapping
->host
;
6621 if (!S_ISREG(inode
->i_mode
)) {
6622 pr_warn("%s: error: bitmap file must be a regular file\n",
6625 } else if (!(f
->f_mode
& FMODE_WRITE
)) {
6626 pr_warn("%s: error: bitmap file must open for write\n",
6629 } else if (atomic_read(&inode
->i_writecount
) != 1) {
6630 pr_warn("%s: error: bitmap file is already in use\n",
6638 mddev
->bitmap_info
.file
= f
;
6639 mddev
->bitmap_info
.offset
= 0; /* file overrides offset */
6640 } else if (mddev
->bitmap
== NULL
)
6641 return -ENOENT
; /* cannot remove what isn't there */
6645 struct bitmap
*bitmap
;
6647 bitmap
= bitmap_create(mddev
, -1);
6648 mddev_suspend(mddev
);
6649 if (!IS_ERR(bitmap
)) {
6650 mddev
->bitmap
= bitmap
;
6651 err
= bitmap_load(mddev
);
6653 err
= PTR_ERR(bitmap
);
6655 bitmap_destroy(mddev
);
6658 mddev_resume(mddev
);
6659 } else if (fd
< 0) {
6660 mddev_suspend(mddev
);
6661 bitmap_destroy(mddev
);
6662 mddev_resume(mddev
);
6666 struct file
*f
= mddev
->bitmap_info
.file
;
6668 spin_lock(&mddev
->lock
);
6669 mddev
->bitmap_info
.file
= NULL
;
6670 spin_unlock(&mddev
->lock
);
6679 * set_array_info is used two different ways
6680 * The original usage is when creating a new array.
6681 * In this usage, raid_disks is > 0 and it together with
6682 * level, size, not_persistent,layout,chunksize determine the
6683 * shape of the array.
6684 * This will always create an array with a type-0.90.0 superblock.
6685 * The newer usage is when assembling an array.
6686 * In this case raid_disks will be 0, and the major_version field is
6687 * use to determine which style super-blocks are to be found on the devices.
6688 * The minor and patch _version numbers are also kept incase the
6689 * super_block handler wishes to interpret them.
6691 static int set_array_info(struct mddev
*mddev
, mdu_array_info_t
*info
)
6694 if (info
->raid_disks
== 0) {
6695 /* just setting version number for superblock loading */
6696 if (info
->major_version
< 0 ||
6697 info
->major_version
>= ARRAY_SIZE(super_types
) ||
6698 super_types
[info
->major_version
].name
== NULL
) {
6699 /* maybe try to auto-load a module? */
6700 pr_warn("md: superblock version %d not known\n",
6701 info
->major_version
);
6704 mddev
->major_version
= info
->major_version
;
6705 mddev
->minor_version
= info
->minor_version
;
6706 mddev
->patch_version
= info
->patch_version
;
6707 mddev
->persistent
= !info
->not_persistent
;
6708 /* ensure mddev_put doesn't delete this now that there
6709 * is some minimal configuration.
6711 mddev
->ctime
= ktime_get_real_seconds();
6714 mddev
->major_version
= MD_MAJOR_VERSION
;
6715 mddev
->minor_version
= MD_MINOR_VERSION
;
6716 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
6717 mddev
->ctime
= ktime_get_real_seconds();
6719 mddev
->level
= info
->level
;
6720 mddev
->clevel
[0] = 0;
6721 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
6722 mddev
->raid_disks
= info
->raid_disks
;
6723 /* don't set md_minor, it is determined by which /dev/md* was
6726 if (info
->state
& (1<<MD_SB_CLEAN
))
6727 mddev
->recovery_cp
= MaxSector
;
6729 mddev
->recovery_cp
= 0;
6730 mddev
->persistent
= ! info
->not_persistent
;
6731 mddev
->external
= 0;
6733 mddev
->layout
= info
->layout
;
6734 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
6736 if (mddev
->persistent
) {
6737 mddev
->max_disks
= MD_SB_DISKS
;
6739 mddev
->sb_flags
= 0;
6741 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
6743 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
6744 mddev
->bitmap_info
.default_space
= 64*2 - (MD_SB_BYTES
>> 9);
6745 mddev
->bitmap_info
.offset
= 0;
6747 mddev
->reshape_position
= MaxSector
;
6750 * Generate a 128 bit UUID
6752 get_random_bytes(mddev
->uuid
, 16);
6754 mddev
->new_level
= mddev
->level
;
6755 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
6756 mddev
->new_layout
= mddev
->layout
;
6757 mddev
->delta_disks
= 0;
6758 mddev
->reshape_backwards
= 0;
6763 void md_set_array_sectors(struct mddev
*mddev
, sector_t array_sectors
)
6765 lockdep_assert_held(&mddev
->reconfig_mutex
);
6767 if (mddev
->external_size
)
6770 mddev
->array_sectors
= array_sectors
;
6772 EXPORT_SYMBOL(md_set_array_sectors
);
6774 static int update_size(struct mddev
*mddev
, sector_t num_sectors
)
6776 struct md_rdev
*rdev
;
6778 int fit
= (num_sectors
== 0);
6779 sector_t old_dev_sectors
= mddev
->dev_sectors
;
6781 if (mddev
->pers
->resize
== NULL
)
6783 /* The "num_sectors" is the number of sectors of each device that
6784 * is used. This can only make sense for arrays with redundancy.
6785 * linear and raid0 always use whatever space is available. We can only
6786 * consider changing this number if no resync or reconstruction is
6787 * happening, and if the new size is acceptable. It must fit before the
6788 * sb_start or, if that is <data_offset, it must fit before the size
6789 * of each device. If num_sectors is zero, we find the largest size
6792 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
6798 rdev_for_each(rdev
, mddev
) {
6799 sector_t avail
= rdev
->sectors
;
6801 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
6802 num_sectors
= avail
;
6803 if (avail
< num_sectors
)
6806 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
6808 if (mddev_is_clustered(mddev
))
6809 md_cluster_ops
->update_size(mddev
, old_dev_sectors
);
6810 else if (mddev
->queue
) {
6811 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
6812 revalidate_disk(mddev
->gendisk
);
6818 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
)
6821 struct md_rdev
*rdev
;
6822 /* change the number of raid disks */
6823 if (mddev
->pers
->check_reshape
== NULL
)
6827 if (raid_disks
<= 0 ||
6828 (mddev
->max_disks
&& raid_disks
>= mddev
->max_disks
))
6830 if (mddev
->sync_thread
||
6831 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
6832 mddev
->reshape_position
!= MaxSector
)
6835 rdev_for_each(rdev
, mddev
) {
6836 if (mddev
->raid_disks
< raid_disks
&&
6837 rdev
->data_offset
< rdev
->new_data_offset
)
6839 if (mddev
->raid_disks
> raid_disks
&&
6840 rdev
->data_offset
> rdev
->new_data_offset
)
6844 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
6845 if (mddev
->delta_disks
< 0)
6846 mddev
->reshape_backwards
= 1;
6847 else if (mddev
->delta_disks
> 0)
6848 mddev
->reshape_backwards
= 0;
6850 rv
= mddev
->pers
->check_reshape(mddev
);
6852 mddev
->delta_disks
= 0;
6853 mddev
->reshape_backwards
= 0;
6859 * update_array_info is used to change the configuration of an
6861 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6862 * fields in the info are checked against the array.
6863 * Any differences that cannot be handled will cause an error.
6864 * Normally, only one change can be managed at a time.
6866 static int update_array_info(struct mddev
*mddev
, mdu_array_info_t
*info
)
6872 /* calculate expected state,ignoring low bits */
6873 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
6874 state
|= (1 << MD_SB_BITMAP_PRESENT
);
6876 if (mddev
->major_version
!= info
->major_version
||
6877 mddev
->minor_version
!= info
->minor_version
||
6878 /* mddev->patch_version != info->patch_version || */
6879 mddev
->ctime
!= info
->ctime
||
6880 mddev
->level
!= info
->level
||
6881 /* mddev->layout != info->layout || */
6882 mddev
->persistent
!= !info
->not_persistent
||
6883 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
6884 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6885 ((state
^info
->state
) & 0xfffffe00)
6888 /* Check there is only one change */
6889 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
6891 if (mddev
->raid_disks
!= info
->raid_disks
)
6893 if (mddev
->layout
!= info
->layout
)
6895 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
6902 if (mddev
->layout
!= info
->layout
) {
6904 * we don't need to do anything at the md level, the
6905 * personality will take care of it all.
6907 if (mddev
->pers
->check_reshape
== NULL
)
6910 mddev
->new_layout
= info
->layout
;
6911 rv
= mddev
->pers
->check_reshape(mddev
);
6913 mddev
->new_layout
= mddev
->layout
;
6917 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
6918 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
6920 if (mddev
->raid_disks
!= info
->raid_disks
)
6921 rv
= update_raid_disks(mddev
, info
->raid_disks
);
6923 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
6924 if (mddev
->pers
->quiesce
== NULL
|| mddev
->thread
== NULL
) {
6928 if (mddev
->recovery
|| mddev
->sync_thread
) {
6932 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
6933 struct bitmap
*bitmap
;
6934 /* add the bitmap */
6935 if (mddev
->bitmap
) {
6939 if (mddev
->bitmap_info
.default_offset
== 0) {
6943 mddev
->bitmap_info
.offset
=
6944 mddev
->bitmap_info
.default_offset
;
6945 mddev
->bitmap_info
.space
=
6946 mddev
->bitmap_info
.default_space
;
6947 bitmap
= bitmap_create(mddev
, -1);
6948 mddev_suspend(mddev
);
6949 if (!IS_ERR(bitmap
)) {
6950 mddev
->bitmap
= bitmap
;
6951 rv
= bitmap_load(mddev
);
6953 rv
= PTR_ERR(bitmap
);
6955 bitmap_destroy(mddev
);
6956 mddev_resume(mddev
);
6958 /* remove the bitmap */
6959 if (!mddev
->bitmap
) {
6963 if (mddev
->bitmap
->storage
.file
) {
6967 if (mddev
->bitmap_info
.nodes
) {
6968 /* hold PW on all the bitmap lock */
6969 if (md_cluster_ops
->lock_all_bitmaps(mddev
) <= 0) {
6970 pr_warn("md: can't change bitmap to none since the array is in use by more than one node\n");
6972 md_cluster_ops
->unlock_all_bitmaps(mddev
);
6976 mddev
->bitmap_info
.nodes
= 0;
6977 md_cluster_ops
->leave(mddev
);
6979 mddev_suspend(mddev
);
6980 bitmap_destroy(mddev
);
6981 mddev_resume(mddev
);
6982 mddev
->bitmap_info
.offset
= 0;
6985 md_update_sb(mddev
, 1);
6991 static int set_disk_faulty(struct mddev
*mddev
, dev_t dev
)
6993 struct md_rdev
*rdev
;
6996 if (mddev
->pers
== NULL
)
7000 rdev
= find_rdev_rcu(mddev
, dev
);
7004 md_error(mddev
, rdev
);
7005 if (!test_bit(Faulty
, &rdev
->flags
))
7013 * We have a problem here : there is no easy way to give a CHS
7014 * virtual geometry. We currently pretend that we have a 2 heads
7015 * 4 sectors (with a BIG number of cylinders...). This drives
7016 * dosfs just mad... ;-)
7018 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
7020 struct mddev
*mddev
= bdev
->bd_disk
->private_data
;
7024 geo
->cylinders
= mddev
->array_sectors
/ 8;
7028 static inline bool md_ioctl_valid(unsigned int cmd
)
7033 case GET_ARRAY_INFO
:
7034 case GET_BITMAP_FILE
:
7037 case HOT_REMOVE_DISK
:
7040 case RESTART_ARRAY_RW
:
7042 case SET_ARRAY_INFO
:
7043 case SET_BITMAP_FILE
:
7044 case SET_DISK_FAULTY
:
7047 case CLUSTERED_DISK_NACK
:
7054 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
7055 unsigned int cmd
, unsigned long arg
)
7058 void __user
*argp
= (void __user
*)arg
;
7059 struct mddev
*mddev
= NULL
;
7061 bool did_set_md_closing
= false;
7063 if (!md_ioctl_valid(cmd
))
7068 case GET_ARRAY_INFO
:
7072 if (!capable(CAP_SYS_ADMIN
))
7077 * Commands dealing with the RAID driver but not any
7082 err
= get_version(argp
);
7088 autostart_arrays(arg
);
7095 * Commands creating/starting a new array:
7098 mddev
= bdev
->bd_disk
->private_data
;
7105 /* Some actions do not requires the mutex */
7107 case GET_ARRAY_INFO
:
7108 if (!mddev
->raid_disks
&& !mddev
->external
)
7111 err
= get_array_info(mddev
, argp
);
7115 if (!mddev
->raid_disks
&& !mddev
->external
)
7118 err
= get_disk_info(mddev
, argp
);
7121 case SET_DISK_FAULTY
:
7122 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
7125 case GET_BITMAP_FILE
:
7126 err
= get_bitmap_file(mddev
, argp
);
7131 if (cmd
== ADD_NEW_DISK
)
7132 /* need to ensure md_delayed_delete() has completed */
7133 flush_workqueue(md_misc_wq
);
7135 if (cmd
== HOT_REMOVE_DISK
)
7136 /* need to ensure recovery thread has run */
7137 wait_event_interruptible_timeout(mddev
->sb_wait
,
7138 !test_bit(MD_RECOVERY_NEEDED
,
7140 msecs_to_jiffies(5000));
7141 if (cmd
== STOP_ARRAY
|| cmd
== STOP_ARRAY_RO
) {
7142 /* Need to flush page cache, and ensure no-one else opens
7145 mutex_lock(&mddev
->open_mutex
);
7146 if (mddev
->pers
&& atomic_read(&mddev
->openers
) > 1) {
7147 mutex_unlock(&mddev
->open_mutex
);
7151 WARN_ON_ONCE(test_bit(MD_CLOSING
, &mddev
->flags
));
7152 set_bit(MD_CLOSING
, &mddev
->flags
);
7153 did_set_md_closing
= true;
7154 mutex_unlock(&mddev
->open_mutex
);
7155 sync_blockdev(bdev
);
7157 err
= mddev_lock(mddev
);
7159 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7164 if (cmd
== SET_ARRAY_INFO
) {
7165 mdu_array_info_t info
;
7167 memset(&info
, 0, sizeof(info
));
7168 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
7173 err
= update_array_info(mddev
, &info
);
7175 pr_warn("md: couldn't update array info. %d\n", err
);
7180 if (!list_empty(&mddev
->disks
)) {
7181 pr_warn("md: array %s already has disks!\n", mdname(mddev
));
7185 if (mddev
->raid_disks
) {
7186 pr_warn("md: array %s already initialised!\n", mdname(mddev
));
7190 err
= set_array_info(mddev
, &info
);
7192 pr_warn("md: couldn't set array info. %d\n", err
);
7199 * Commands querying/configuring an existing array:
7201 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
7202 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
7203 if ((!mddev
->raid_disks
&& !mddev
->external
)
7204 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
7205 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
7206 && cmd
!= GET_BITMAP_FILE
) {
7212 * Commands even a read-only array can execute:
7215 case RESTART_ARRAY_RW
:
7216 err
= restart_array(mddev
);
7220 err
= do_md_stop(mddev
, 0, bdev
);
7224 err
= md_set_readonly(mddev
, bdev
);
7227 case HOT_REMOVE_DISK
:
7228 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
7232 /* We can support ADD_NEW_DISK on read-only arrays
7233 * only if we are re-adding a preexisting device.
7234 * So require mddev->pers and MD_DISK_SYNC.
7237 mdu_disk_info_t info
;
7238 if (copy_from_user(&info
, argp
, sizeof(info
)))
7240 else if (!(info
.state
& (1<<MD_DISK_SYNC
)))
7241 /* Need to clear read-only for this */
7244 err
= add_new_disk(mddev
, &info
);
7250 if (get_user(ro
, (int __user
*)(arg
))) {
7256 /* if the bdev is going readonly the value of mddev->ro
7257 * does not matter, no writes are coming
7262 /* are we are already prepared for writes? */
7266 /* transitioning to readauto need only happen for
7267 * arrays that call md_write_start
7270 err
= restart_array(mddev
);
7273 set_disk_ro(mddev
->gendisk
, 0);
7280 * The remaining ioctls are changing the state of the
7281 * superblock, so we do not allow them on read-only arrays.
7283 if (mddev
->ro
&& mddev
->pers
) {
7284 if (mddev
->ro
== 2) {
7286 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7287 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7288 /* mddev_unlock will wake thread */
7289 /* If a device failed while we were read-only, we
7290 * need to make sure the metadata is updated now.
7292 if (test_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
)) {
7293 mddev_unlock(mddev
);
7294 wait_event(mddev
->sb_wait
,
7295 !test_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
) &&
7296 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
7297 mddev_lock_nointr(mddev
);
7308 mdu_disk_info_t info
;
7309 if (copy_from_user(&info
, argp
, sizeof(info
)))
7312 err
= add_new_disk(mddev
, &info
);
7316 case CLUSTERED_DISK_NACK
:
7317 if (mddev_is_clustered(mddev
))
7318 md_cluster_ops
->new_disk_ack(mddev
, false);
7324 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
7328 err
= do_md_run(mddev
);
7331 case SET_BITMAP_FILE
:
7332 err
= set_bitmap_file(mddev
, (int)arg
);
7341 if (mddev
->hold_active
== UNTIL_IOCTL
&&
7343 mddev
->hold_active
= 0;
7344 mddev_unlock(mddev
);
7346 if(did_set_md_closing
)
7347 clear_bit(MD_CLOSING
, &mddev
->flags
);
7350 #ifdef CONFIG_COMPAT
7351 static int md_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
7352 unsigned int cmd
, unsigned long arg
)
7355 case HOT_REMOVE_DISK
:
7357 case SET_DISK_FAULTY
:
7358 case SET_BITMAP_FILE
:
7359 /* These take in integer arg, do not convert */
7362 arg
= (unsigned long)compat_ptr(arg
);
7366 return md_ioctl(bdev
, mode
, cmd
, arg
);
7368 #endif /* CONFIG_COMPAT */
7370 static int md_open(struct block_device
*bdev
, fmode_t mode
)
7373 * Succeed if we can lock the mddev, which confirms that
7374 * it isn't being stopped right now.
7376 struct mddev
*mddev
= mddev_find(bdev
->bd_dev
);
7382 if (mddev
->gendisk
!= bdev
->bd_disk
) {
7383 /* we are racing with mddev_put which is discarding this
7387 /* Wait until bdev->bd_disk is definitely gone */
7388 flush_workqueue(md_misc_wq
);
7389 /* Then retry the open from the top */
7390 return -ERESTARTSYS
;
7392 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
7394 if ((err
= mutex_lock_interruptible(&mddev
->open_mutex
)))
7397 if (test_bit(MD_CLOSING
, &mddev
->flags
)) {
7398 mutex_unlock(&mddev
->open_mutex
);
7404 atomic_inc(&mddev
->openers
);
7405 mutex_unlock(&mddev
->open_mutex
);
7407 check_disk_change(bdev
);
7414 static void md_release(struct gendisk
*disk
, fmode_t mode
)
7416 struct mddev
*mddev
= disk
->private_data
;
7419 atomic_dec(&mddev
->openers
);
7423 static int md_media_changed(struct gendisk
*disk
)
7425 struct mddev
*mddev
= disk
->private_data
;
7427 return mddev
->changed
;
7430 static int md_revalidate(struct gendisk
*disk
)
7432 struct mddev
*mddev
= disk
->private_data
;
7437 static const struct block_device_operations md_fops
=
7439 .owner
= THIS_MODULE
,
7441 .release
= md_release
,
7443 #ifdef CONFIG_COMPAT
7444 .compat_ioctl
= md_compat_ioctl
,
7446 .getgeo
= md_getgeo
,
7447 .media_changed
= md_media_changed
,
7448 .revalidate_disk
= md_revalidate
,
7451 static int md_thread(void *arg
)
7453 struct md_thread
*thread
= arg
;
7456 * md_thread is a 'system-thread', it's priority should be very
7457 * high. We avoid resource deadlocks individually in each
7458 * raid personality. (RAID5 does preallocation) We also use RR and
7459 * the very same RT priority as kswapd, thus we will never get
7460 * into a priority inversion deadlock.
7462 * we definitely have to have equal or higher priority than
7463 * bdflush, otherwise bdflush will deadlock if there are too
7464 * many dirty RAID5 blocks.
7467 allow_signal(SIGKILL
);
7468 while (!kthread_should_stop()) {
7470 /* We need to wait INTERRUPTIBLE so that
7471 * we don't add to the load-average.
7472 * That means we need to be sure no signals are
7475 if (signal_pending(current
))
7476 flush_signals(current
);
7478 wait_event_interruptible_timeout
7480 test_bit(THREAD_WAKEUP
, &thread
->flags
)
7481 || kthread_should_stop() || kthread_should_park(),
7484 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
7485 if (kthread_should_park())
7487 if (!kthread_should_stop())
7488 thread
->run(thread
);
7494 void md_wakeup_thread(struct md_thread
*thread
)
7497 pr_debug("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
7498 set_bit(THREAD_WAKEUP
, &thread
->flags
);
7499 wake_up(&thread
->wqueue
);
7502 EXPORT_SYMBOL(md_wakeup_thread
);
7504 struct md_thread
*md_register_thread(void (*run
) (struct md_thread
*),
7505 struct mddev
*mddev
, const char *name
)
7507 struct md_thread
*thread
;
7509 thread
= kzalloc(sizeof(struct md_thread
), GFP_KERNEL
);
7513 init_waitqueue_head(&thread
->wqueue
);
7516 thread
->mddev
= mddev
;
7517 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
7518 thread
->tsk
= kthread_run(md_thread
, thread
,
7520 mdname(thread
->mddev
),
7522 if (IS_ERR(thread
->tsk
)) {
7528 EXPORT_SYMBOL(md_register_thread
);
7530 void md_unregister_thread(struct md_thread
**threadp
)
7532 struct md_thread
*thread
= *threadp
;
7535 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
7536 /* Locking ensures that mddev_unlock does not wake_up a
7537 * non-existent thread
7539 spin_lock(&pers_lock
);
7541 spin_unlock(&pers_lock
);
7543 kthread_stop(thread
->tsk
);
7546 EXPORT_SYMBOL(md_unregister_thread
);
7548 void md_error(struct mddev
*mddev
, struct md_rdev
*rdev
)
7550 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
7553 if (!mddev
->pers
|| !mddev
->pers
->error_handler
)
7555 mddev
->pers
->error_handler(mddev
,rdev
);
7556 if (mddev
->degraded
)
7557 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7558 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
7559 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7560 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7561 md_wakeup_thread(mddev
->thread
);
7562 if (mddev
->event_work
.func
)
7563 queue_work(md_misc_wq
, &mddev
->event_work
);
7564 md_new_event(mddev
);
7566 EXPORT_SYMBOL(md_error
);
7568 /* seq_file implementation /proc/mdstat */
7570 static void status_unused(struct seq_file
*seq
)
7573 struct md_rdev
*rdev
;
7575 seq_printf(seq
, "unused devices: ");
7577 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
7578 char b
[BDEVNAME_SIZE
];
7580 seq_printf(seq
, "%s ",
7581 bdevname(rdev
->bdev
,b
));
7584 seq_printf(seq
, "<none>");
7586 seq_printf(seq
, "\n");
7589 static int status_resync(struct seq_file
*seq
, struct mddev
*mddev
)
7591 sector_t max_sectors
, resync
, res
;
7592 unsigned long dt
, db
;
7595 unsigned int per_milli
;
7597 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
7598 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
7599 max_sectors
= mddev
->resync_max_sectors
;
7601 max_sectors
= mddev
->dev_sectors
;
7603 resync
= mddev
->curr_resync
;
7605 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
7606 /* Still cleaning up */
7607 resync
= max_sectors
;
7609 resync
-= atomic_read(&mddev
->recovery_active
);
7612 if (mddev
->recovery_cp
< MaxSector
) {
7613 seq_printf(seq
, "\tresync=PENDING");
7619 seq_printf(seq
, "\tresync=DELAYED");
7623 WARN_ON(max_sectors
== 0);
7624 /* Pick 'scale' such that (resync>>scale)*1000 will fit
7625 * in a sector_t, and (max_sectors>>scale) will fit in a
7626 * u32, as those are the requirements for sector_div.
7627 * Thus 'scale' must be at least 10
7630 if (sizeof(sector_t
) > sizeof(unsigned long)) {
7631 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
7634 res
= (resync
>>scale
)*1000;
7635 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
7639 int i
, x
= per_milli
/50, y
= 20-x
;
7640 seq_printf(seq
, "[");
7641 for (i
= 0; i
< x
; i
++)
7642 seq_printf(seq
, "=");
7643 seq_printf(seq
, ">");
7644 for (i
= 0; i
< y
; i
++)
7645 seq_printf(seq
, ".");
7646 seq_printf(seq
, "] ");
7648 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
7649 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
7651 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
7653 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
7654 "resync" : "recovery"))),
7655 per_milli
/10, per_milli
% 10,
7656 (unsigned long long) resync
/2,
7657 (unsigned long long) max_sectors
/2);
7660 * dt: time from mark until now
7661 * db: blocks written from mark until now
7662 * rt: remaining time
7664 * rt is a sector_t, so could be 32bit or 64bit.
7665 * So we divide before multiply in case it is 32bit and close
7667 * We scale the divisor (db) by 32 to avoid losing precision
7668 * near the end of resync when the number of remaining sectors
7670 * We then divide rt by 32 after multiplying by db to compensate.
7671 * The '+1' avoids division by zero if db is very small.
7673 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
7675 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
7676 - mddev
->resync_mark_cnt
;
7678 rt
= max_sectors
- resync
; /* number of remaining sectors */
7679 sector_div(rt
, db
/32+1);
7683 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
7684 ((unsigned long)rt
% 60)/6);
7686 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
7690 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
7692 struct list_head
*tmp
;
7694 struct mddev
*mddev
;
7702 spin_lock(&all_mddevs_lock
);
7703 list_for_each(tmp
,&all_mddevs
)
7705 mddev
= list_entry(tmp
, struct mddev
, all_mddevs
);
7707 spin_unlock(&all_mddevs_lock
);
7710 spin_unlock(&all_mddevs_lock
);
7712 return (void*)2;/* tail */
7716 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
7718 struct list_head
*tmp
;
7719 struct mddev
*next_mddev
, *mddev
= v
;
7725 spin_lock(&all_mddevs_lock
);
7727 tmp
= all_mddevs
.next
;
7729 tmp
= mddev
->all_mddevs
.next
;
7730 if (tmp
!= &all_mddevs
)
7731 next_mddev
= mddev_get(list_entry(tmp
,struct mddev
,all_mddevs
));
7733 next_mddev
= (void*)2;
7736 spin_unlock(&all_mddevs_lock
);
7744 static void md_seq_stop(struct seq_file
*seq
, void *v
)
7746 struct mddev
*mddev
= v
;
7748 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
7752 static int md_seq_show(struct seq_file
*seq
, void *v
)
7754 struct mddev
*mddev
= v
;
7756 struct md_rdev
*rdev
;
7758 if (v
== (void*)1) {
7759 struct md_personality
*pers
;
7760 seq_printf(seq
, "Personalities : ");
7761 spin_lock(&pers_lock
);
7762 list_for_each_entry(pers
, &pers_list
, list
)
7763 seq_printf(seq
, "[%s] ", pers
->name
);
7765 spin_unlock(&pers_lock
);
7766 seq_printf(seq
, "\n");
7767 seq
->poll_event
= atomic_read(&md_event_count
);
7770 if (v
== (void*)2) {
7775 spin_lock(&mddev
->lock
);
7776 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
7777 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
7778 mddev
->pers
? "" : "in");
7781 seq_printf(seq
, " (read-only)");
7783 seq_printf(seq
, " (auto-read-only)");
7784 seq_printf(seq
, " %s", mddev
->pers
->name
);
7789 rdev_for_each_rcu(rdev
, mddev
) {
7790 char b
[BDEVNAME_SIZE
];
7791 seq_printf(seq
, " %s[%d]",
7792 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
7793 if (test_bit(WriteMostly
, &rdev
->flags
))
7794 seq_printf(seq
, "(W)");
7795 if (test_bit(Journal
, &rdev
->flags
))
7796 seq_printf(seq
, "(J)");
7797 if (test_bit(Faulty
, &rdev
->flags
)) {
7798 seq_printf(seq
, "(F)");
7801 if (rdev
->raid_disk
< 0)
7802 seq_printf(seq
, "(S)"); /* spare */
7803 if (test_bit(Replacement
, &rdev
->flags
))
7804 seq_printf(seq
, "(R)");
7805 sectors
+= rdev
->sectors
;
7809 if (!list_empty(&mddev
->disks
)) {
7811 seq_printf(seq
, "\n %llu blocks",
7812 (unsigned long long)
7813 mddev
->array_sectors
/ 2);
7815 seq_printf(seq
, "\n %llu blocks",
7816 (unsigned long long)sectors
/ 2);
7818 if (mddev
->persistent
) {
7819 if (mddev
->major_version
!= 0 ||
7820 mddev
->minor_version
!= 90) {
7821 seq_printf(seq
," super %d.%d",
7822 mddev
->major_version
,
7823 mddev
->minor_version
);
7825 } else if (mddev
->external
)
7826 seq_printf(seq
, " super external:%s",
7827 mddev
->metadata_type
);
7829 seq_printf(seq
, " super non-persistent");
7832 mddev
->pers
->status(seq
, mddev
);
7833 seq_printf(seq
, "\n ");
7834 if (mddev
->pers
->sync_request
) {
7835 if (status_resync(seq
, mddev
))
7836 seq_printf(seq
, "\n ");
7839 seq_printf(seq
, "\n ");
7841 bitmap_status(seq
, mddev
->bitmap
);
7843 seq_printf(seq
, "\n");
7845 spin_unlock(&mddev
->lock
);
7850 static const struct seq_operations md_seq_ops
= {
7851 .start
= md_seq_start
,
7852 .next
= md_seq_next
,
7853 .stop
= md_seq_stop
,
7854 .show
= md_seq_show
,
7857 static int md_seq_open(struct inode
*inode
, struct file
*file
)
7859 struct seq_file
*seq
;
7862 error
= seq_open(file
, &md_seq_ops
);
7866 seq
= file
->private_data
;
7867 seq
->poll_event
= atomic_read(&md_event_count
);
7871 static int md_unloading
;
7872 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
7874 struct seq_file
*seq
= filp
->private_data
;
7878 return POLLIN
|POLLRDNORM
|POLLERR
|POLLPRI
;
7879 poll_wait(filp
, &md_event_waiters
, wait
);
7881 /* always allow read */
7882 mask
= POLLIN
| POLLRDNORM
;
7884 if (seq
->poll_event
!= atomic_read(&md_event_count
))
7885 mask
|= POLLERR
| POLLPRI
;
7889 static const struct file_operations md_seq_fops
= {
7890 .owner
= THIS_MODULE
,
7891 .open
= md_seq_open
,
7893 .llseek
= seq_lseek
,
7894 .release
= seq_release
,
7895 .poll
= mdstat_poll
,
7898 int register_md_personality(struct md_personality
*p
)
7900 pr_debug("md: %s personality registered for level %d\n",
7902 spin_lock(&pers_lock
);
7903 list_add_tail(&p
->list
, &pers_list
);
7904 spin_unlock(&pers_lock
);
7907 EXPORT_SYMBOL(register_md_personality
);
7909 int unregister_md_personality(struct md_personality
*p
)
7911 pr_debug("md: %s personality unregistered\n", p
->name
);
7912 spin_lock(&pers_lock
);
7913 list_del_init(&p
->list
);
7914 spin_unlock(&pers_lock
);
7917 EXPORT_SYMBOL(unregister_md_personality
);
7919 int register_md_cluster_operations(struct md_cluster_operations
*ops
,
7920 struct module
*module
)
7923 spin_lock(&pers_lock
);
7924 if (md_cluster_ops
!= NULL
)
7927 md_cluster_ops
= ops
;
7928 md_cluster_mod
= module
;
7930 spin_unlock(&pers_lock
);
7933 EXPORT_SYMBOL(register_md_cluster_operations
);
7935 int unregister_md_cluster_operations(void)
7937 spin_lock(&pers_lock
);
7938 md_cluster_ops
= NULL
;
7939 spin_unlock(&pers_lock
);
7942 EXPORT_SYMBOL(unregister_md_cluster_operations
);
7944 int md_setup_cluster(struct mddev
*mddev
, int nodes
)
7946 if (!md_cluster_ops
)
7947 request_module("md-cluster");
7948 spin_lock(&pers_lock
);
7949 /* ensure module won't be unloaded */
7950 if (!md_cluster_ops
|| !try_module_get(md_cluster_mod
)) {
7951 pr_warn("can't find md-cluster module or get it's reference.\n");
7952 spin_unlock(&pers_lock
);
7955 spin_unlock(&pers_lock
);
7957 return md_cluster_ops
->join(mddev
, nodes
);
7960 void md_cluster_stop(struct mddev
*mddev
)
7962 if (!md_cluster_ops
)
7964 md_cluster_ops
->leave(mddev
);
7965 module_put(md_cluster_mod
);
7968 static int is_mddev_idle(struct mddev
*mddev
, int init
)
7970 struct md_rdev
*rdev
;
7976 rdev_for_each_rcu(rdev
, mddev
) {
7977 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
7978 curr_events
= (int)part_stat_read(&disk
->part0
, sectors
[0]) +
7979 (int)part_stat_read(&disk
->part0
, sectors
[1]) -
7980 atomic_read(&disk
->sync_io
);
7981 /* sync IO will cause sync_io to increase before the disk_stats
7982 * as sync_io is counted when a request starts, and
7983 * disk_stats is counted when it completes.
7984 * So resync activity will cause curr_events to be smaller than
7985 * when there was no such activity.
7986 * non-sync IO will cause disk_stat to increase without
7987 * increasing sync_io so curr_events will (eventually)
7988 * be larger than it was before. Once it becomes
7989 * substantially larger, the test below will cause
7990 * the array to appear non-idle, and resync will slow
7992 * If there is a lot of outstanding resync activity when
7993 * we set last_event to curr_events, then all that activity
7994 * completing might cause the array to appear non-idle
7995 * and resync will be slowed down even though there might
7996 * not have been non-resync activity. This will only
7997 * happen once though. 'last_events' will soon reflect
7998 * the state where there is little or no outstanding
7999 * resync requests, and further resync activity will
8000 * always make curr_events less than last_events.
8003 if (init
|| curr_events
- rdev
->last_events
> 64) {
8004 rdev
->last_events
= curr_events
;
8012 void md_done_sync(struct mddev
*mddev
, int blocks
, int ok
)
8014 /* another "blocks" (512byte) blocks have been synced */
8015 atomic_sub(blocks
, &mddev
->recovery_active
);
8016 wake_up(&mddev
->recovery_wait
);
8018 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8019 set_bit(MD_RECOVERY_ERROR
, &mddev
->recovery
);
8020 md_wakeup_thread(mddev
->thread
);
8021 // stop recovery, signal do_sync ....
8024 EXPORT_SYMBOL(md_done_sync
);
8026 /* md_write_start(mddev, bi)
8027 * If we need to update some array metadata (e.g. 'active' flag
8028 * in superblock) before writing, schedule a superblock update
8029 * and wait for it to complete.
8030 * A return value of 'false' means that the write wasn't recorded
8031 * and cannot proceed as the array is being suspend.
8033 bool md_write_start(struct mddev
*mddev
, struct bio
*bi
)
8036 if (bio_data_dir(bi
) != WRITE
)
8039 BUG_ON(mddev
->ro
== 1);
8040 if (mddev
->ro
== 2) {
8041 /* need to switch to read/write */
8043 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8044 md_wakeup_thread(mddev
->thread
);
8045 md_wakeup_thread(mddev
->sync_thread
);
8049 percpu_ref_get(&mddev
->writes_pending
);
8050 smp_mb(); /* Match smp_mb in set_in_sync() */
8051 if (mddev
->safemode
== 1)
8052 mddev
->safemode
= 0;
8053 /* sync_checkers is always 0 when writes_pending is in per-cpu mode */
8054 if (mddev
->in_sync
|| mddev
->sync_checkers
) {
8055 spin_lock(&mddev
->lock
);
8056 if (mddev
->in_sync
) {
8058 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
8059 set_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
8060 md_wakeup_thread(mddev
->thread
);
8063 spin_unlock(&mddev
->lock
);
8067 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
8068 wait_event(mddev
->sb_wait
,
8069 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
) ||
8071 if (test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
)) {
8072 percpu_ref_put(&mddev
->writes_pending
);
8077 EXPORT_SYMBOL(md_write_start
);
8079 /* md_write_inc can only be called when md_write_start() has
8080 * already been called at least once of the current request.
8081 * It increments the counter and is useful when a single request
8082 * is split into several parts. Each part causes an increment and
8083 * so needs a matching md_write_end().
8084 * Unlike md_write_start(), it is safe to call md_write_inc() inside
8085 * a spinlocked region.
8087 void md_write_inc(struct mddev
*mddev
, struct bio
*bi
)
8089 if (bio_data_dir(bi
) != WRITE
)
8091 WARN_ON_ONCE(mddev
->in_sync
|| mddev
->ro
);
8092 percpu_ref_get(&mddev
->writes_pending
);
8094 EXPORT_SYMBOL(md_write_inc
);
8096 void md_write_end(struct mddev
*mddev
)
8098 percpu_ref_put(&mddev
->writes_pending
);
8100 if (mddev
->safemode
== 2)
8101 md_wakeup_thread(mddev
->thread
);
8102 else if (mddev
->safemode_delay
)
8103 /* The roundup() ensures this only performs locking once
8104 * every ->safemode_delay jiffies
8106 mod_timer(&mddev
->safemode_timer
,
8107 roundup(jiffies
, mddev
->safemode_delay
) +
8108 mddev
->safemode_delay
);
8111 EXPORT_SYMBOL(md_write_end
);
8113 /* md_allow_write(mddev)
8114 * Calling this ensures that the array is marked 'active' so that writes
8115 * may proceed without blocking. It is important to call this before
8116 * attempting a GFP_KERNEL allocation while holding the mddev lock.
8117 * Must be called with mddev_lock held.
8119 void md_allow_write(struct mddev
*mddev
)
8125 if (!mddev
->pers
->sync_request
)
8128 spin_lock(&mddev
->lock
);
8129 if (mddev
->in_sync
) {
8131 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
8132 set_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
8133 if (mddev
->safemode_delay
&&
8134 mddev
->safemode
== 0)
8135 mddev
->safemode
= 1;
8136 spin_unlock(&mddev
->lock
);
8137 md_update_sb(mddev
, 0);
8138 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
8139 /* wait for the dirty state to be recorded in the metadata */
8140 wait_event(mddev
->sb_wait
,
8141 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
8143 spin_unlock(&mddev
->lock
);
8145 EXPORT_SYMBOL_GPL(md_allow_write
);
8147 #define SYNC_MARKS 10
8148 #define SYNC_MARK_STEP (3*HZ)
8149 #define UPDATE_FREQUENCY (5*60*HZ)
8150 void md_do_sync(struct md_thread
*thread
)
8152 struct mddev
*mddev
= thread
->mddev
;
8153 struct mddev
*mddev2
;
8154 unsigned int currspeed
= 0,
8156 sector_t max_sectors
,j
, io_sectors
, recovery_done
;
8157 unsigned long mark
[SYNC_MARKS
];
8158 unsigned long update_time
;
8159 sector_t mark_cnt
[SYNC_MARKS
];
8161 struct list_head
*tmp
;
8162 sector_t last_check
;
8164 struct md_rdev
*rdev
;
8165 char *desc
, *action
= NULL
;
8166 struct blk_plug plug
;
8169 /* just incase thread restarts... */
8170 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
8172 if (mddev
->ro
) {/* never try to sync a read-only array */
8173 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8177 if (mddev_is_clustered(mddev
)) {
8178 ret
= md_cluster_ops
->resync_start(mddev
);
8182 set_bit(MD_CLUSTER_RESYNC_LOCKED
, &mddev
->flags
);
8183 if (!(test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
8184 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) ||
8185 test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
8186 && ((unsigned long long)mddev
->curr_resync_completed
8187 < (unsigned long long)mddev
->resync_max_sectors
))
8191 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
8192 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)) {
8193 desc
= "data-check";
8195 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
8196 desc
= "requested-resync";
8200 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
8205 mddev
->last_sync_action
= action
?: desc
;
8207 /* we overload curr_resync somewhat here.
8208 * 0 == not engaged in resync at all
8209 * 2 == checking that there is no conflict with another sync
8210 * 1 == like 2, but have yielded to allow conflicting resync to
8212 * other == active in resync - this many blocks
8214 * Before starting a resync we must have set curr_resync to
8215 * 2, and then checked that every "conflicting" array has curr_resync
8216 * less than ours. When we find one that is the same or higher
8217 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8218 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8219 * This will mean we have to start checking from the beginning again.
8224 int mddev2_minor
= -1;
8225 mddev
->curr_resync
= 2;
8228 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8230 for_each_mddev(mddev2
, tmp
) {
8231 if (mddev2
== mddev
)
8233 if (!mddev
->parallel_resync
8234 && mddev2
->curr_resync
8235 && match_mddev_units(mddev
, mddev2
)) {
8237 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
8238 /* arbitrarily yield */
8239 mddev
->curr_resync
= 1;
8240 wake_up(&resync_wait
);
8242 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
8243 /* no need to wait here, we can wait the next
8244 * time 'round when curr_resync == 2
8247 /* We need to wait 'interruptible' so as not to
8248 * contribute to the load average, and not to
8249 * be caught by 'softlockup'
8251 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
8252 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
8253 mddev2
->curr_resync
>= mddev
->curr_resync
) {
8254 if (mddev2_minor
!= mddev2
->md_minor
) {
8255 mddev2_minor
= mddev2
->md_minor
;
8256 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8257 desc
, mdname(mddev
),
8261 if (signal_pending(current
))
8262 flush_signals(current
);
8264 finish_wait(&resync_wait
, &wq
);
8267 finish_wait(&resync_wait
, &wq
);
8270 } while (mddev
->curr_resync
< 2);
8273 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
8274 /* resync follows the size requested by the personality,
8275 * which defaults to physical size, but can be virtual size
8277 max_sectors
= mddev
->resync_max_sectors
;
8278 atomic64_set(&mddev
->resync_mismatches
, 0);
8279 /* we don't use the checkpoint if there's a bitmap */
8280 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
8281 j
= mddev
->resync_min
;
8282 else if (!mddev
->bitmap
)
8283 j
= mddev
->recovery_cp
;
8285 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
8286 max_sectors
= mddev
->resync_max_sectors
;
8288 /* recovery follows the physical size of devices */
8289 max_sectors
= mddev
->dev_sectors
;
8292 rdev_for_each_rcu(rdev
, mddev
)
8293 if (rdev
->raid_disk
>= 0 &&
8294 !test_bit(Journal
, &rdev
->flags
) &&
8295 !test_bit(Faulty
, &rdev
->flags
) &&
8296 !test_bit(In_sync
, &rdev
->flags
) &&
8297 rdev
->recovery_offset
< j
)
8298 j
= rdev
->recovery_offset
;
8301 /* If there is a bitmap, we need to make sure all
8302 * writes that started before we added a spare
8303 * complete before we start doing a recovery.
8304 * Otherwise the write might complete and (via
8305 * bitmap_endwrite) set a bit in the bitmap after the
8306 * recovery has checked that bit and skipped that
8309 if (mddev
->bitmap
) {
8310 mddev
->pers
->quiesce(mddev
, 1);
8311 mddev
->pers
->quiesce(mddev
, 0);
8315 pr_info("md: %s of RAID array %s\n", desc
, mdname(mddev
));
8316 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev
));
8317 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8318 speed_max(mddev
), desc
);
8320 is_mddev_idle(mddev
, 1); /* this initializes IO event counters */
8323 for (m
= 0; m
< SYNC_MARKS
; m
++) {
8325 mark_cnt
[m
] = io_sectors
;
8328 mddev
->resync_mark
= mark
[last_mark
];
8329 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
8332 * Tune reconstruction:
8334 window
= 32*(PAGE_SIZE
/512);
8335 pr_debug("md: using %dk window, over a total of %lluk.\n",
8336 window
/2, (unsigned long long)max_sectors
/2);
8338 atomic_set(&mddev
->recovery_active
, 0);
8342 pr_debug("md: resuming %s of %s from checkpoint.\n",
8343 desc
, mdname(mddev
));
8344 mddev
->curr_resync
= j
;
8346 mddev
->curr_resync
= 3; /* no longer delayed */
8347 mddev
->curr_resync_completed
= j
;
8348 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
8349 md_new_event(mddev
);
8350 update_time
= jiffies
;
8352 blk_start_plug(&plug
);
8353 while (j
< max_sectors
) {
8358 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8359 ((mddev
->curr_resync
> mddev
->curr_resync_completed
&&
8360 (mddev
->curr_resync
- mddev
->curr_resync_completed
)
8361 > (max_sectors
>> 4)) ||
8362 time_after_eq(jiffies
, update_time
+ UPDATE_FREQUENCY
) ||
8363 (j
- mddev
->curr_resync_completed
)*2
8364 >= mddev
->resync_max
- mddev
->curr_resync_completed
||
8365 mddev
->curr_resync_completed
> mddev
->resync_max
8367 /* time to update curr_resync_completed */
8368 wait_event(mddev
->recovery_wait
,
8369 atomic_read(&mddev
->recovery_active
) == 0);
8370 mddev
->curr_resync_completed
= j
;
8371 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) &&
8372 j
> mddev
->recovery_cp
)
8373 mddev
->recovery_cp
= j
;
8374 update_time
= jiffies
;
8375 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
8376 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
8379 while (j
>= mddev
->resync_max
&&
8380 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
8381 /* As this condition is controlled by user-space,
8382 * we can block indefinitely, so use '_interruptible'
8383 * to avoid triggering warnings.
8385 flush_signals(current
); /* just in case */
8386 wait_event_interruptible(mddev
->recovery_wait
,
8387 mddev
->resync_max
> j
8388 || test_bit(MD_RECOVERY_INTR
,
8392 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8395 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
);
8397 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8401 if (!skipped
) { /* actual IO requested */
8402 io_sectors
+= sectors
;
8403 atomic_add(sectors
, &mddev
->recovery_active
);
8406 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8410 if (j
> max_sectors
)
8411 /* when skipping, extra large numbers can be returned. */
8414 mddev
->curr_resync
= j
;
8415 mddev
->curr_mark_cnt
= io_sectors
;
8416 if (last_check
== 0)
8417 /* this is the earliest that rebuild will be
8418 * visible in /proc/mdstat
8420 md_new_event(mddev
);
8422 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
8425 last_check
= io_sectors
;
8427 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
8429 int next
= (last_mark
+1) % SYNC_MARKS
;
8431 mddev
->resync_mark
= mark
[next
];
8432 mddev
->resync_mark_cnt
= mark_cnt
[next
];
8433 mark
[next
] = jiffies
;
8434 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
8438 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8442 * this loop exits only if either when we are slower than
8443 * the 'hard' speed limit, or the system was IO-idle for
8445 * the system might be non-idle CPU-wise, but we only care
8446 * about not overloading the IO subsystem. (things like an
8447 * e2fsck being done on the RAID array should execute fast)
8451 recovery_done
= io_sectors
- atomic_read(&mddev
->recovery_active
);
8452 currspeed
= ((unsigned long)(recovery_done
- mddev
->resync_mark_cnt
))/2
8453 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
8455 if (currspeed
> speed_min(mddev
)) {
8456 if (currspeed
> speed_max(mddev
)) {
8460 if (!is_mddev_idle(mddev
, 0)) {
8462 * Give other IO more of a chance.
8463 * The faster the devices, the less we wait.
8465 wait_event(mddev
->recovery_wait
,
8466 !atomic_read(&mddev
->recovery_active
));
8470 pr_info("md: %s: %s %s.\n",mdname(mddev
), desc
,
8471 test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)
8472 ? "interrupted" : "done");
8474 * this also signals 'finished resyncing' to md_stop
8476 blk_finish_plug(&plug
);
8477 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
8479 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8480 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
8481 mddev
->curr_resync
> 3) {
8482 mddev
->curr_resync_completed
= mddev
->curr_resync
;
8483 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
8485 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
);
8487 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
8488 mddev
->curr_resync
> 3) {
8489 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
8490 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
8491 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
8492 pr_debug("md: checkpointing %s of %s.\n",
8493 desc
, mdname(mddev
));
8494 if (test_bit(MD_RECOVERY_ERROR
,
8496 mddev
->recovery_cp
=
8497 mddev
->curr_resync_completed
;
8499 mddev
->recovery_cp
=
8503 mddev
->recovery_cp
= MaxSector
;
8505 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8506 mddev
->curr_resync
= MaxSector
;
8507 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8508 test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
)) {
8510 rdev_for_each_rcu(rdev
, mddev
)
8511 if (rdev
->raid_disk
>= 0 &&
8512 mddev
->delta_disks
>= 0 &&
8513 !test_bit(Journal
, &rdev
->flags
) &&
8514 !test_bit(Faulty
, &rdev
->flags
) &&
8515 !test_bit(In_sync
, &rdev
->flags
) &&
8516 rdev
->recovery_offset
< mddev
->curr_resync
)
8517 rdev
->recovery_offset
= mddev
->curr_resync
;
8523 /* set CHANGE_PENDING here since maybe another update is needed,
8524 * so other nodes are informed. It should be harmless for normal
8526 set_mask_bits(&mddev
->sb_flags
, 0,
8527 BIT(MD_SB_CHANGE_PENDING
) | BIT(MD_SB_CHANGE_DEVS
));
8529 spin_lock(&mddev
->lock
);
8530 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
8531 /* We completed so min/max setting can be forgotten if used. */
8532 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
8533 mddev
->resync_min
= 0;
8534 mddev
->resync_max
= MaxSector
;
8535 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
8536 mddev
->resync_min
= mddev
->curr_resync_completed
;
8537 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
8538 mddev
->curr_resync
= 0;
8539 spin_unlock(&mddev
->lock
);
8541 wake_up(&resync_wait
);
8542 md_wakeup_thread(mddev
->thread
);
8545 EXPORT_SYMBOL_GPL(md_do_sync
);
8547 static int remove_and_add_spares(struct mddev
*mddev
,
8548 struct md_rdev
*this)
8550 struct md_rdev
*rdev
;
8553 bool remove_some
= false;
8555 rdev_for_each(rdev
, mddev
) {
8556 if ((this == NULL
|| rdev
== this) &&
8557 rdev
->raid_disk
>= 0 &&
8558 !test_bit(Blocked
, &rdev
->flags
) &&
8559 test_bit(Faulty
, &rdev
->flags
) &&
8560 atomic_read(&rdev
->nr_pending
)==0) {
8561 /* Faulty non-Blocked devices with nr_pending == 0
8562 * never get nr_pending incremented,
8563 * never get Faulty cleared, and never get Blocked set.
8564 * So we can synchronize_rcu now rather than once per device
8567 set_bit(RemoveSynchronized
, &rdev
->flags
);
8573 rdev_for_each(rdev
, mddev
) {
8574 if ((this == NULL
|| rdev
== this) &&
8575 rdev
->raid_disk
>= 0 &&
8576 !test_bit(Blocked
, &rdev
->flags
) &&
8577 ((test_bit(RemoveSynchronized
, &rdev
->flags
) ||
8578 (!test_bit(In_sync
, &rdev
->flags
) &&
8579 !test_bit(Journal
, &rdev
->flags
))) &&
8580 atomic_read(&rdev
->nr_pending
)==0)) {
8581 if (mddev
->pers
->hot_remove_disk(
8582 mddev
, rdev
) == 0) {
8583 sysfs_unlink_rdev(mddev
, rdev
);
8584 rdev
->raid_disk
= -1;
8588 if (remove_some
&& test_bit(RemoveSynchronized
, &rdev
->flags
))
8589 clear_bit(RemoveSynchronized
, &rdev
->flags
);
8592 if (removed
&& mddev
->kobj
.sd
)
8593 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
8595 if (this && removed
)
8598 rdev_for_each(rdev
, mddev
) {
8599 if (this && this != rdev
)
8601 if (test_bit(Candidate
, &rdev
->flags
))
8603 if (rdev
->raid_disk
>= 0 &&
8604 !test_bit(In_sync
, &rdev
->flags
) &&
8605 !test_bit(Journal
, &rdev
->flags
) &&
8606 !test_bit(Faulty
, &rdev
->flags
))
8608 if (rdev
->raid_disk
>= 0)
8610 if (test_bit(Faulty
, &rdev
->flags
))
8612 if (!test_bit(Journal
, &rdev
->flags
)) {
8614 ! (rdev
->saved_raid_disk
>= 0 &&
8615 !test_bit(Bitmap_sync
, &rdev
->flags
)))
8618 rdev
->recovery_offset
= 0;
8621 hot_add_disk(mddev
, rdev
) == 0) {
8622 if (sysfs_link_rdev(mddev
, rdev
))
8623 /* failure here is OK */;
8624 if (!test_bit(Journal
, &rdev
->flags
))
8626 md_new_event(mddev
);
8627 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
8632 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
8636 static void md_start_sync(struct work_struct
*ws
)
8638 struct mddev
*mddev
= container_of(ws
, struct mddev
, del_work
);
8640 mddev
->sync_thread
= md_register_thread(md_do_sync
,
8643 if (!mddev
->sync_thread
) {
8644 pr_warn("%s: could not start resync thread...\n",
8646 /* leave the spares where they are, it shouldn't hurt */
8647 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
8648 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
8649 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
8650 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
8651 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
8652 wake_up(&resync_wait
);
8653 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
8655 if (mddev
->sysfs_action
)
8656 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
8658 md_wakeup_thread(mddev
->sync_thread
);
8659 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
8660 md_new_event(mddev
);
8664 * This routine is regularly called by all per-raid-array threads to
8665 * deal with generic issues like resync and super-block update.
8666 * Raid personalities that don't have a thread (linear/raid0) do not
8667 * need this as they never do any recovery or update the superblock.
8669 * It does not do any resync itself, but rather "forks" off other threads
8670 * to do that as needed.
8671 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8672 * "->recovery" and create a thread at ->sync_thread.
8673 * When the thread finishes it sets MD_RECOVERY_DONE
8674 * and wakeups up this thread which will reap the thread and finish up.
8675 * This thread also removes any faulty devices (with nr_pending == 0).
8677 * The overall approach is:
8678 * 1/ if the superblock needs updating, update it.
8679 * 2/ If a recovery thread is running, don't do anything else.
8680 * 3/ If recovery has finished, clean up, possibly marking spares active.
8681 * 4/ If there are any faulty devices, remove them.
8682 * 5/ If array is degraded, try to add spares devices
8683 * 6/ If array has spares or is not in-sync, start a resync thread.
8685 void md_check_recovery(struct mddev
*mddev
)
8687 if (mddev
->suspended
)
8691 bitmap_daemon_work(mddev
);
8693 if (signal_pending(current
)) {
8694 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
8695 pr_debug("md: %s in immediate safe mode\n",
8697 mddev
->safemode
= 2;
8699 flush_signals(current
);
8702 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
8705 (mddev
->sb_flags
& ~ (1<<MD_SB_CHANGE_PENDING
)) ||
8706 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
8707 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
8708 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
8709 (mddev
->safemode
== 2
8710 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
8714 if (mddev_trylock(mddev
)) {
8717 if (!mddev
->external
&& mddev
->safemode
== 1)
8718 mddev
->safemode
= 0;
8721 struct md_rdev
*rdev
;
8722 if (!mddev
->external
&& mddev
->in_sync
)
8723 /* 'Blocked' flag not needed as failed devices
8724 * will be recorded if array switched to read/write.
8725 * Leaving it set will prevent the device
8726 * from being removed.
8728 rdev_for_each(rdev
, mddev
)
8729 clear_bit(Blocked
, &rdev
->flags
);
8730 /* On a read-only array we can:
8731 * - remove failed devices
8732 * - add already-in_sync devices if the array itself
8734 * As we only add devices that are already in-sync,
8735 * we can activate the spares immediately.
8737 remove_and_add_spares(mddev
, NULL
);
8738 /* There is no thread, but we need to call
8739 * ->spare_active and clear saved_raid_disk
8741 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8742 md_reap_sync_thread(mddev
);
8743 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8744 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8745 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
8749 if (mddev_is_clustered(mddev
)) {
8750 struct md_rdev
*rdev
;
8751 /* kick the device if another node issued a
8754 rdev_for_each(rdev
, mddev
) {
8755 if (test_and_clear_bit(ClusterRemove
, &rdev
->flags
) &&
8756 rdev
->raid_disk
< 0)
8757 md_kick_rdev_from_array(rdev
);
8761 if (!mddev
->external
&& !mddev
->in_sync
) {
8762 spin_lock(&mddev
->lock
);
8764 spin_unlock(&mddev
->lock
);
8767 if (mddev
->sb_flags
)
8768 md_update_sb(mddev
, 0);
8770 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
8771 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
8772 /* resync/recovery still happening */
8773 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8776 if (mddev
->sync_thread
) {
8777 md_reap_sync_thread(mddev
);
8780 /* Set RUNNING before clearing NEEDED to avoid
8781 * any transients in the value of "sync_action".
8783 mddev
->curr_resync_completed
= 0;
8784 spin_lock(&mddev
->lock
);
8785 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
8786 spin_unlock(&mddev
->lock
);
8787 /* Clear some bits that don't mean anything, but
8790 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8791 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
8793 if (!test_and_clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
8794 test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
8796 /* no recovery is running.
8797 * remove any failed drives, then
8798 * add spares if possible.
8799 * Spares are also removed and re-added, to allow
8800 * the personality to fail the re-add.
8803 if (mddev
->reshape_position
!= MaxSector
) {
8804 if (mddev
->pers
->check_reshape
== NULL
||
8805 mddev
->pers
->check_reshape(mddev
) != 0)
8806 /* Cannot proceed */
8808 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
8809 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8810 } else if ((spares
= remove_and_add_spares(mddev
, NULL
))) {
8811 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
8812 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
8813 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
8814 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8815 } else if (mddev
->recovery_cp
< MaxSector
) {
8816 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
8817 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8818 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
8819 /* nothing to be done ... */
8822 if (mddev
->pers
->sync_request
) {
8824 /* We are adding a device or devices to an array
8825 * which has the bitmap stored on all devices.
8826 * So make sure all bitmap pages get written
8828 bitmap_write_all(mddev
->bitmap
);
8830 INIT_WORK(&mddev
->del_work
, md_start_sync
);
8831 queue_work(md_misc_wq
, &mddev
->del_work
);
8835 if (!mddev
->sync_thread
) {
8836 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
8837 wake_up(&resync_wait
);
8838 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
8840 if (mddev
->sysfs_action
)
8841 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
8844 wake_up(&mddev
->sb_wait
);
8845 mddev_unlock(mddev
);
8846 } else if (test_bit(MD_ALLOW_SB_UPDATE
, &mddev
->flags
) && mddev
->sb_flags
) {
8847 /* Write superblock - thread that called mddev_suspend()
8848 * holds reconfig_mutex for us.
8850 set_bit(MD_UPDATING_SB
, &mddev
->flags
);
8851 smp_mb__after_atomic();
8852 if (test_bit(MD_ALLOW_SB_UPDATE
, &mddev
->flags
))
8853 md_update_sb(mddev
, 0);
8854 clear_bit_unlock(MD_UPDATING_SB
, &mddev
->flags
);
8855 wake_up(&mddev
->sb_wait
);
8858 EXPORT_SYMBOL(md_check_recovery
);
8860 void md_reap_sync_thread(struct mddev
*mddev
)
8862 struct md_rdev
*rdev
;
8864 /* resync has finished, collect result */
8865 md_unregister_thread(&mddev
->sync_thread
);
8866 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
8867 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
8869 /* activate any spares */
8870 if (mddev
->pers
->spare_active(mddev
)) {
8871 sysfs_notify(&mddev
->kobj
, NULL
,
8873 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
8876 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8877 mddev
->pers
->finish_reshape
)
8878 mddev
->pers
->finish_reshape(mddev
);
8880 /* If array is no-longer degraded, then any saved_raid_disk
8881 * information must be scrapped.
8883 if (!mddev
->degraded
)
8884 rdev_for_each(rdev
, mddev
)
8885 rdev
->saved_raid_disk
= -1;
8887 md_update_sb(mddev
, 1);
8888 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
8889 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
8891 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED
, &mddev
->flags
))
8892 md_cluster_ops
->resync_finish(mddev
);
8893 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
8894 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
8895 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
8896 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
8897 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
8898 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
8899 wake_up(&resync_wait
);
8900 /* flag recovery needed just to double check */
8901 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8902 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
8903 md_new_event(mddev
);
8904 if (mddev
->event_work
.func
)
8905 queue_work(md_misc_wq
, &mddev
->event_work
);
8907 EXPORT_SYMBOL(md_reap_sync_thread
);
8909 void md_wait_for_blocked_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
8911 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
8912 wait_event_timeout(rdev
->blocked_wait
,
8913 !test_bit(Blocked
, &rdev
->flags
) &&
8914 !test_bit(BlockedBadBlocks
, &rdev
->flags
),
8915 msecs_to_jiffies(5000));
8916 rdev_dec_pending(rdev
, mddev
);
8918 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
8920 void md_finish_reshape(struct mddev
*mddev
)
8922 /* called be personality module when reshape completes. */
8923 struct md_rdev
*rdev
;
8925 rdev_for_each(rdev
, mddev
) {
8926 if (rdev
->data_offset
> rdev
->new_data_offset
)
8927 rdev
->sectors
+= rdev
->data_offset
- rdev
->new_data_offset
;
8929 rdev
->sectors
-= rdev
->new_data_offset
- rdev
->data_offset
;
8930 rdev
->data_offset
= rdev
->new_data_offset
;
8933 EXPORT_SYMBOL(md_finish_reshape
);
8935 /* Bad block management */
8937 /* Returns 1 on success, 0 on failure */
8938 int rdev_set_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
,
8941 struct mddev
*mddev
= rdev
->mddev
;
8944 s
+= rdev
->new_data_offset
;
8946 s
+= rdev
->data_offset
;
8947 rv
= badblocks_set(&rdev
->badblocks
, s
, sectors
, 0);
8949 /* Make sure they get written out promptly */
8950 if (test_bit(ExternalBbl
, &rdev
->flags
))
8951 sysfs_notify(&rdev
->kobj
, NULL
,
8952 "unacknowledged_bad_blocks");
8953 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
8954 set_mask_bits(&mddev
->sb_flags
, 0,
8955 BIT(MD_SB_CHANGE_CLEAN
) | BIT(MD_SB_CHANGE_PENDING
));
8956 md_wakeup_thread(rdev
->mddev
->thread
);
8961 EXPORT_SYMBOL_GPL(rdev_set_badblocks
);
8963 int rdev_clear_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
,
8968 s
+= rdev
->new_data_offset
;
8970 s
+= rdev
->data_offset
;
8971 rv
= badblocks_clear(&rdev
->badblocks
, s
, sectors
);
8972 if ((rv
== 0) && test_bit(ExternalBbl
, &rdev
->flags
))
8973 sysfs_notify(&rdev
->kobj
, NULL
, "bad_blocks");
8976 EXPORT_SYMBOL_GPL(rdev_clear_badblocks
);
8978 static int md_notify_reboot(struct notifier_block
*this,
8979 unsigned long code
, void *x
)
8981 struct list_head
*tmp
;
8982 struct mddev
*mddev
;
8985 for_each_mddev(mddev
, tmp
) {
8986 if (mddev_trylock(mddev
)) {
8988 __md_stop_writes(mddev
);
8989 if (mddev
->persistent
)
8990 mddev
->safemode
= 2;
8991 mddev_unlock(mddev
);
8996 * certain more exotic SCSI devices are known to be
8997 * volatile wrt too early system reboots. While the
8998 * right place to handle this issue is the given
8999 * driver, we do want to have a safe RAID driver ...
9007 static struct notifier_block md_notifier
= {
9008 .notifier_call
= md_notify_reboot
,
9010 .priority
= INT_MAX
, /* before any real devices */
9013 static void md_geninit(void)
9015 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
9017 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
9020 static int __init
md_init(void)
9024 md_wq
= alloc_workqueue("md", WQ_MEM_RECLAIM
, 0);
9028 md_misc_wq
= alloc_workqueue("md_misc", 0, 0);
9032 if ((ret
= register_blkdev(MD_MAJOR
, "md")) < 0)
9035 if ((ret
= register_blkdev(0, "mdp")) < 0)
9039 blk_register_region(MKDEV(MD_MAJOR
, 0), 512, THIS_MODULE
,
9040 md_probe
, NULL
, NULL
);
9041 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
9042 md_probe
, NULL
, NULL
);
9044 register_reboot_notifier(&md_notifier
);
9045 raid_table_header
= register_sysctl_table(raid_root_table
);
9051 unregister_blkdev(MD_MAJOR
, "md");
9053 destroy_workqueue(md_misc_wq
);
9055 destroy_workqueue(md_wq
);
9060 static void check_sb_changes(struct mddev
*mddev
, struct md_rdev
*rdev
)
9062 struct mdp_superblock_1
*sb
= page_address(rdev
->sb_page
);
9063 struct md_rdev
*rdev2
;
9065 char b
[BDEVNAME_SIZE
];
9068 * If size is changed in another node then we need to
9069 * do resize as well.
9071 if (mddev
->dev_sectors
!= le64_to_cpu(sb
->size
)) {
9072 ret
= mddev
->pers
->resize(mddev
, le64_to_cpu(sb
->size
));
9074 pr_info("md-cluster: resize failed\n");
9076 bitmap_update_sb(mddev
->bitmap
);
9079 /* Check for change of roles in the active devices */
9080 rdev_for_each(rdev2
, mddev
) {
9081 if (test_bit(Faulty
, &rdev2
->flags
))
9084 /* Check if the roles changed */
9085 role
= le16_to_cpu(sb
->dev_roles
[rdev2
->desc_nr
]);
9087 if (test_bit(Candidate
, &rdev2
->flags
)) {
9088 if (role
== 0xfffe) {
9089 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2
->bdev
,b
));
9090 md_kick_rdev_from_array(rdev2
);
9094 clear_bit(Candidate
, &rdev2
->flags
);
9097 if (role
!= rdev2
->raid_disk
) {
9099 if (rdev2
->raid_disk
== -1 && role
!= 0xffff) {
9100 rdev2
->saved_raid_disk
= role
;
9101 ret
= remove_and_add_spares(mddev
, rdev2
);
9102 pr_info("Activated spare: %s\n",
9103 bdevname(rdev2
->bdev
,b
));
9104 /* wakeup mddev->thread here, so array could
9105 * perform resync with the new activated disk */
9106 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
9107 md_wakeup_thread(mddev
->thread
);
9111 * We just want to do the minimum to mark the disk
9112 * as faulty. The recovery is performed by the
9113 * one who initiated the error.
9115 if ((role
== 0xfffe) || (role
== 0xfffd)) {
9116 md_error(mddev
, rdev2
);
9117 clear_bit(Blocked
, &rdev2
->flags
);
9122 if (mddev
->raid_disks
!= le32_to_cpu(sb
->raid_disks
))
9123 update_raid_disks(mddev
, le32_to_cpu(sb
->raid_disks
));
9125 /* Finally set the event to be up to date */
9126 mddev
->events
= le64_to_cpu(sb
->events
);
9129 static int read_rdev(struct mddev
*mddev
, struct md_rdev
*rdev
)
9132 struct page
*swapout
= rdev
->sb_page
;
9133 struct mdp_superblock_1
*sb
;
9135 /* Store the sb page of the rdev in the swapout temporary
9136 * variable in case we err in the future
9138 rdev
->sb_page
= NULL
;
9139 err
= alloc_disk_sb(rdev
);
9141 ClearPageUptodate(rdev
->sb_page
);
9142 rdev
->sb_loaded
= 0;
9143 err
= super_types
[mddev
->major_version
].
9144 load_super(rdev
, NULL
, mddev
->minor_version
);
9147 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9148 __func__
, __LINE__
, rdev
->desc_nr
, err
);
9150 put_page(rdev
->sb_page
);
9151 rdev
->sb_page
= swapout
;
9152 rdev
->sb_loaded
= 1;
9156 sb
= page_address(rdev
->sb_page
);
9157 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9161 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RECOVERY_OFFSET
))
9162 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
9164 /* The other node finished recovery, call spare_active to set
9165 * device In_sync and mddev->degraded
9167 if (rdev
->recovery_offset
== MaxSector
&&
9168 !test_bit(In_sync
, &rdev
->flags
) &&
9169 mddev
->pers
->spare_active(mddev
))
9170 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
9176 void md_reload_sb(struct mddev
*mddev
, int nr
)
9178 struct md_rdev
*rdev
;
9182 rdev_for_each_rcu(rdev
, mddev
) {
9183 if (rdev
->desc_nr
== nr
)
9187 if (!rdev
|| rdev
->desc_nr
!= nr
) {
9188 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__
, __LINE__
, nr
);
9192 err
= read_rdev(mddev
, rdev
);
9196 check_sb_changes(mddev
, rdev
);
9198 /* Read all rdev's to update recovery_offset */
9199 rdev_for_each_rcu(rdev
, mddev
)
9200 read_rdev(mddev
, rdev
);
9202 EXPORT_SYMBOL(md_reload_sb
);
9207 * Searches all registered partitions for autorun RAID arrays
9211 static DEFINE_MUTEX(detected_devices_mutex
);
9212 static LIST_HEAD(all_detected_devices
);
9213 struct detected_devices_node
{
9214 struct list_head list
;
9218 void md_autodetect_dev(dev_t dev
)
9220 struct detected_devices_node
*node_detected_dev
;
9222 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
9223 if (node_detected_dev
) {
9224 node_detected_dev
->dev
= dev
;
9225 mutex_lock(&detected_devices_mutex
);
9226 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
9227 mutex_unlock(&detected_devices_mutex
);
9231 static void autostart_arrays(int part
)
9233 struct md_rdev
*rdev
;
9234 struct detected_devices_node
*node_detected_dev
;
9236 int i_scanned
, i_passed
;
9241 pr_info("md: Autodetecting RAID arrays.\n");
9243 mutex_lock(&detected_devices_mutex
);
9244 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
9246 node_detected_dev
= list_entry(all_detected_devices
.next
,
9247 struct detected_devices_node
, list
);
9248 list_del(&node_detected_dev
->list
);
9249 dev
= node_detected_dev
->dev
;
9250 kfree(node_detected_dev
);
9251 mutex_unlock(&detected_devices_mutex
);
9252 rdev
= md_import_device(dev
,0, 90);
9253 mutex_lock(&detected_devices_mutex
);
9257 if (test_bit(Faulty
, &rdev
->flags
))
9260 set_bit(AutoDetected
, &rdev
->flags
);
9261 list_add(&rdev
->same_set
, &pending_raid_disks
);
9264 mutex_unlock(&detected_devices_mutex
);
9266 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned
, i_passed
);
9268 autorun_devices(part
);
9271 #endif /* !MODULE */
9273 static __exit
void md_exit(void)
9275 struct mddev
*mddev
;
9276 struct list_head
*tmp
;
9279 blk_unregister_region(MKDEV(MD_MAJOR
,0), 512);
9280 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
9282 unregister_blkdev(MD_MAJOR
,"md");
9283 unregister_blkdev(mdp_major
, "mdp");
9284 unregister_reboot_notifier(&md_notifier
);
9285 unregister_sysctl_table(raid_table_header
);
9287 /* We cannot unload the modules while some process is
9288 * waiting for us in select() or poll() - wake them up
9291 while (waitqueue_active(&md_event_waiters
)) {
9292 /* not safe to leave yet */
9293 wake_up(&md_event_waiters
);
9297 remove_proc_entry("mdstat", NULL
);
9299 for_each_mddev(mddev
, tmp
) {
9300 export_array(mddev
);
9302 mddev
->hold_active
= 0;
9304 * for_each_mddev() will call mddev_put() at the end of each
9305 * iteration. As the mddev is now fully clear, this will
9306 * schedule the mddev for destruction by a workqueue, and the
9307 * destroy_workqueue() below will wait for that to complete.
9310 destroy_workqueue(md_misc_wq
);
9311 destroy_workqueue(md_wq
);
9314 subsys_initcall(md_init
);
9315 module_exit(md_exit
)
9317 static int get_ro(char *buffer
, const struct kernel_param
*kp
)
9319 return sprintf(buffer
, "%d", start_readonly
);
9321 static int set_ro(const char *val
, const struct kernel_param
*kp
)
9323 return kstrtouint(val
, 10, (unsigned int *)&start_readonly
);
9326 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
9327 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
9328 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
9329 module_param(create_on_open
, bool, S_IRUSR
|S_IWUSR
);
9331 MODULE_LICENSE("GPL");
9332 MODULE_DESCRIPTION("MD RAID framework");
9334 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);