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 <trace/events/block.h>
71 #include "md-cluster.h"
74 static void autostart_arrays(int part
);
77 /* pers_list is a list of registered personalities protected
79 * pers_lock does extra service to protect accesses to
80 * mddev->thread when the mutex cannot be held.
82 static LIST_HEAD(pers_list
);
83 static DEFINE_SPINLOCK(pers_lock
);
85 struct md_cluster_operations
*md_cluster_ops
;
86 EXPORT_SYMBOL(md_cluster_ops
);
87 struct module
*md_cluster_mod
;
88 EXPORT_SYMBOL(md_cluster_mod
);
90 static DECLARE_WAIT_QUEUE_HEAD(resync_wait
);
91 static struct workqueue_struct
*md_wq
;
92 static struct workqueue_struct
*md_misc_wq
;
94 static int remove_and_add_spares(struct mddev
*mddev
,
95 struct md_rdev
*this);
96 static void mddev_detach(struct mddev
*mddev
);
99 * Default number of read corrections we'll attempt on an rdev
100 * before ejecting it from the array. We divide the read error
101 * count by 2 for every hour elapsed between read errors.
103 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
105 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
106 * is 1000 KB/sec, so the extra system load does not show up that much.
107 * Increase it if you want to have more _guaranteed_ speed. Note that
108 * the RAID driver will use the maximum available bandwidth if the IO
109 * subsystem is idle. There is also an 'absolute maximum' reconstruction
110 * speed limit - in case reconstruction slows down your system despite
113 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
114 * or /sys/block/mdX/md/sync_speed_{min,max}
117 static int sysctl_speed_limit_min
= 1000;
118 static int sysctl_speed_limit_max
= 200000;
119 static inline int speed_min(struct mddev
*mddev
)
121 return mddev
->sync_speed_min
?
122 mddev
->sync_speed_min
: sysctl_speed_limit_min
;
125 static inline int speed_max(struct mddev
*mddev
)
127 return mddev
->sync_speed_max
?
128 mddev
->sync_speed_max
: sysctl_speed_limit_max
;
131 static struct ctl_table_header
*raid_table_header
;
133 static struct ctl_table raid_table
[] = {
135 .procname
= "speed_limit_min",
136 .data
= &sysctl_speed_limit_min
,
137 .maxlen
= sizeof(int),
138 .mode
= S_IRUGO
|S_IWUSR
,
139 .proc_handler
= proc_dointvec
,
142 .procname
= "speed_limit_max",
143 .data
= &sysctl_speed_limit_max
,
144 .maxlen
= sizeof(int),
145 .mode
= S_IRUGO
|S_IWUSR
,
146 .proc_handler
= proc_dointvec
,
151 static struct ctl_table raid_dir_table
[] = {
155 .mode
= S_IRUGO
|S_IXUGO
,
161 static struct ctl_table raid_root_table
[] = {
166 .child
= raid_dir_table
,
171 static const struct block_device_operations md_fops
;
173 static int start_readonly
;
176 * like bio_clone, but with a local bio set
179 struct bio
*bio_alloc_mddev(gfp_t gfp_mask
, int nr_iovecs
,
184 if (!mddev
|| !mddev
->bio_set
)
185 return bio_alloc(gfp_mask
, nr_iovecs
);
187 b
= bio_alloc_bioset(gfp_mask
, nr_iovecs
, mddev
->bio_set
);
192 EXPORT_SYMBOL_GPL(bio_alloc_mddev
);
195 * We have a system wide 'event count' that is incremented
196 * on any 'interesting' event, and readers of /proc/mdstat
197 * can use 'poll' or 'select' to find out when the event
201 * start array, stop array, error, add device, remove device,
202 * start build, activate spare
204 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters
);
205 static atomic_t md_event_count
;
206 void md_new_event(struct mddev
*mddev
)
208 atomic_inc(&md_event_count
);
209 wake_up(&md_event_waiters
);
211 EXPORT_SYMBOL_GPL(md_new_event
);
214 * Enables to iterate over all existing md arrays
215 * all_mddevs_lock protects this list.
217 static LIST_HEAD(all_mddevs
);
218 static DEFINE_SPINLOCK(all_mddevs_lock
);
221 * iterates through all used mddevs in the system.
222 * We take care to grab the all_mddevs_lock whenever navigating
223 * the list, and to always hold a refcount when unlocked.
224 * Any code which breaks out of this loop while own
225 * a reference to the current mddev and must mddev_put it.
227 #define for_each_mddev(_mddev,_tmp) \
229 for (({ spin_lock(&all_mddevs_lock); \
230 _tmp = all_mddevs.next; \
232 ({ if (_tmp != &all_mddevs) \
233 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
234 spin_unlock(&all_mddevs_lock); \
235 if (_mddev) mddev_put(_mddev); \
236 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
237 _tmp != &all_mddevs;}); \
238 ({ spin_lock(&all_mddevs_lock); \
239 _tmp = _tmp->next;}) \
242 /* Rather than calling directly into the personality make_request function,
243 * IO requests come here first so that we can check if the device is
244 * being suspended pending a reconfiguration.
245 * We hold a refcount over the call to ->make_request. By the time that
246 * call has finished, the bio has been linked into some internal structure
247 * and so is visible to ->quiesce(), so we don't need the refcount any more.
249 static blk_qc_t
md_make_request(struct request_queue
*q
, struct bio
*bio
)
251 const int rw
= bio_data_dir(bio
);
252 struct mddev
*mddev
= q
->queuedata
;
253 unsigned int sectors
;
256 blk_queue_split(q
, &bio
, q
->bio_split
);
258 if (mddev
== NULL
|| mddev
->pers
== NULL
) {
260 return BLK_QC_T_NONE
;
262 if (mddev
->ro
== 1 && unlikely(rw
== WRITE
)) {
263 if (bio_sectors(bio
) != 0)
264 bio
->bi_error
= -EROFS
;
266 return BLK_QC_T_NONE
;
268 smp_rmb(); /* Ensure implications of 'active' are visible */
270 if (mddev
->suspended
) {
273 prepare_to_wait(&mddev
->sb_wait
, &__wait
,
274 TASK_UNINTERRUPTIBLE
);
275 if (!mddev
->suspended
)
281 finish_wait(&mddev
->sb_wait
, &__wait
);
283 atomic_inc(&mddev
->active_io
);
287 * save the sectors now since our bio can
288 * go away inside make_request
290 sectors
= bio_sectors(bio
);
291 /* bio could be mergeable after passing to underlayer */
292 bio
->bi_opf
&= ~REQ_NOMERGE
;
293 mddev
->pers
->make_request(mddev
, bio
);
295 cpu
= part_stat_lock();
296 part_stat_inc(cpu
, &mddev
->gendisk
->part0
, ios
[rw
]);
297 part_stat_add(cpu
, &mddev
->gendisk
->part0
, sectors
[rw
], sectors
);
300 if (atomic_dec_and_test(&mddev
->active_io
) && mddev
->suspended
)
301 wake_up(&mddev
->sb_wait
);
303 return BLK_QC_T_NONE
;
306 /* mddev_suspend makes sure no new requests are submitted
307 * to the device, and that any requests that have been submitted
308 * are completely handled.
309 * Once mddev_detach() is called and completes, the module will be
312 void mddev_suspend(struct mddev
*mddev
)
314 WARN_ON_ONCE(mddev
->thread
&& current
== mddev
->thread
->tsk
);
315 if (mddev
->suspended
++)
318 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->active_io
) == 0);
319 mddev
->pers
->quiesce(mddev
, 1);
321 del_timer_sync(&mddev
->safemode_timer
);
323 EXPORT_SYMBOL_GPL(mddev_suspend
);
325 void mddev_resume(struct mddev
*mddev
)
327 if (--mddev
->suspended
)
329 wake_up(&mddev
->sb_wait
);
330 mddev
->pers
->quiesce(mddev
, 0);
332 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
333 md_wakeup_thread(mddev
->thread
);
334 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
336 EXPORT_SYMBOL_GPL(mddev_resume
);
338 int mddev_congested(struct mddev
*mddev
, int bits
)
340 struct md_personality
*pers
= mddev
->pers
;
344 if (mddev
->suspended
)
346 else if (pers
&& pers
->congested
)
347 ret
= pers
->congested(mddev
, bits
);
351 EXPORT_SYMBOL_GPL(mddev_congested
);
352 static int md_congested(void *data
, int bits
)
354 struct mddev
*mddev
= data
;
355 return mddev_congested(mddev
, bits
);
359 * Generic flush handling for md
362 static void md_end_flush(struct bio
*bio
)
364 struct md_rdev
*rdev
= bio
->bi_private
;
365 struct mddev
*mddev
= rdev
->mddev
;
367 rdev_dec_pending(rdev
, mddev
);
369 if (atomic_dec_and_test(&mddev
->flush_pending
)) {
370 /* The pre-request flush has finished */
371 queue_work(md_wq
, &mddev
->flush_work
);
376 static void md_submit_flush_data(struct work_struct
*ws
);
378 static void submit_flushes(struct work_struct
*ws
)
380 struct mddev
*mddev
= container_of(ws
, struct mddev
, flush_work
);
381 struct md_rdev
*rdev
;
383 INIT_WORK(&mddev
->flush_work
, md_submit_flush_data
);
384 atomic_set(&mddev
->flush_pending
, 1);
386 rdev_for_each_rcu(rdev
, mddev
)
387 if (rdev
->raid_disk
>= 0 &&
388 !test_bit(Faulty
, &rdev
->flags
)) {
389 /* Take two references, one is dropped
390 * when request finishes, one after
391 * we reclaim rcu_read_lock
394 atomic_inc(&rdev
->nr_pending
);
395 atomic_inc(&rdev
->nr_pending
);
397 bi
= bio_alloc_mddev(GFP_NOIO
, 0, mddev
);
398 bi
->bi_end_io
= md_end_flush
;
399 bi
->bi_private
= rdev
;
400 bi
->bi_bdev
= rdev
->bdev
;
401 bi
->bi_opf
= REQ_OP_WRITE
| REQ_PREFLUSH
;
402 atomic_inc(&mddev
->flush_pending
);
405 rdev_dec_pending(rdev
, mddev
);
408 if (atomic_dec_and_test(&mddev
->flush_pending
))
409 queue_work(md_wq
, &mddev
->flush_work
);
412 static void md_submit_flush_data(struct work_struct
*ws
)
414 struct mddev
*mddev
= container_of(ws
, struct mddev
, flush_work
);
415 struct bio
*bio
= mddev
->flush_bio
;
417 if (bio
->bi_iter
.bi_size
== 0)
418 /* an empty barrier - all done */
421 bio
->bi_opf
&= ~REQ_PREFLUSH
;
422 mddev
->pers
->make_request(mddev
, bio
);
425 mddev
->flush_bio
= NULL
;
426 wake_up(&mddev
->sb_wait
);
429 void md_flush_request(struct mddev
*mddev
, struct bio
*bio
)
431 spin_lock_irq(&mddev
->lock
);
432 wait_event_lock_irq(mddev
->sb_wait
,
435 mddev
->flush_bio
= bio
;
436 spin_unlock_irq(&mddev
->lock
);
438 INIT_WORK(&mddev
->flush_work
, submit_flushes
);
439 queue_work(md_wq
, &mddev
->flush_work
);
441 EXPORT_SYMBOL(md_flush_request
);
443 void md_unplug(struct blk_plug_cb
*cb
, bool from_schedule
)
445 struct mddev
*mddev
= cb
->data
;
446 md_wakeup_thread(mddev
->thread
);
449 EXPORT_SYMBOL(md_unplug
);
451 static inline struct mddev
*mddev_get(struct mddev
*mddev
)
453 atomic_inc(&mddev
->active
);
457 static void mddev_delayed_delete(struct work_struct
*ws
);
459 static void mddev_put(struct mddev
*mddev
)
461 struct bio_set
*bs
= NULL
;
463 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
465 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
) &&
466 mddev
->ctime
== 0 && !mddev
->hold_active
) {
467 /* Array is not configured at all, and not held active,
469 list_del_init(&mddev
->all_mddevs
);
471 mddev
->bio_set
= NULL
;
472 if (mddev
->gendisk
) {
473 /* We did a probe so need to clean up. Call
474 * queue_work inside the spinlock so that
475 * flush_workqueue() after mddev_find will
476 * succeed in waiting for the work to be done.
478 INIT_WORK(&mddev
->del_work
, mddev_delayed_delete
);
479 queue_work(md_misc_wq
, &mddev
->del_work
);
483 spin_unlock(&all_mddevs_lock
);
488 static void md_safemode_timeout(unsigned long data
);
490 void mddev_init(struct mddev
*mddev
)
492 mutex_init(&mddev
->open_mutex
);
493 mutex_init(&mddev
->reconfig_mutex
);
494 mutex_init(&mddev
->bitmap_info
.mutex
);
495 INIT_LIST_HEAD(&mddev
->disks
);
496 INIT_LIST_HEAD(&mddev
->all_mddevs
);
497 setup_timer(&mddev
->safemode_timer
, md_safemode_timeout
,
498 (unsigned long) mddev
);
499 atomic_set(&mddev
->active
, 1);
500 atomic_set(&mddev
->openers
, 0);
501 atomic_set(&mddev
->active_io
, 0);
502 spin_lock_init(&mddev
->lock
);
503 atomic_set(&mddev
->flush_pending
, 0);
504 init_waitqueue_head(&mddev
->sb_wait
);
505 init_waitqueue_head(&mddev
->recovery_wait
);
506 mddev
->reshape_position
= MaxSector
;
507 mddev
->reshape_backwards
= 0;
508 mddev
->last_sync_action
= "none";
509 mddev
->resync_min
= 0;
510 mddev
->resync_max
= MaxSector
;
511 mddev
->level
= LEVEL_NONE
;
513 EXPORT_SYMBOL_GPL(mddev_init
);
515 static struct mddev
*mddev_find(dev_t unit
)
517 struct mddev
*mddev
, *new = NULL
;
519 if (unit
&& MAJOR(unit
) != MD_MAJOR
)
520 unit
&= ~((1<<MdpMinorShift
)-1);
523 spin_lock(&all_mddevs_lock
);
526 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
527 if (mddev
->unit
== unit
) {
529 spin_unlock(&all_mddevs_lock
);
535 list_add(&new->all_mddevs
, &all_mddevs
);
536 spin_unlock(&all_mddevs_lock
);
537 new->hold_active
= UNTIL_IOCTL
;
541 /* find an unused unit number */
542 static int next_minor
= 512;
543 int start
= next_minor
;
547 dev
= MKDEV(MD_MAJOR
, next_minor
);
549 if (next_minor
> MINORMASK
)
551 if (next_minor
== start
) {
552 /* Oh dear, all in use. */
553 spin_unlock(&all_mddevs_lock
);
559 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
560 if (mddev
->unit
== dev
) {
566 new->md_minor
= MINOR(dev
);
567 new->hold_active
= UNTIL_STOP
;
568 list_add(&new->all_mddevs
, &all_mddevs
);
569 spin_unlock(&all_mddevs_lock
);
572 spin_unlock(&all_mddevs_lock
);
574 new = kzalloc(sizeof(*new), GFP_KERNEL
);
579 if (MAJOR(unit
) == MD_MAJOR
)
580 new->md_minor
= MINOR(unit
);
582 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
589 static struct attribute_group md_redundancy_group
;
591 void mddev_unlock(struct mddev
*mddev
)
593 if (mddev
->to_remove
) {
594 /* These cannot be removed under reconfig_mutex as
595 * an access to the files will try to take reconfig_mutex
596 * while holding the file unremovable, which leads to
598 * So hold set sysfs_active while the remove in happeing,
599 * and anything else which might set ->to_remove or my
600 * otherwise change the sysfs namespace will fail with
601 * -EBUSY if sysfs_active is still set.
602 * We set sysfs_active under reconfig_mutex and elsewhere
603 * test it under the same mutex to ensure its correct value
606 struct attribute_group
*to_remove
= mddev
->to_remove
;
607 mddev
->to_remove
= NULL
;
608 mddev
->sysfs_active
= 1;
609 mutex_unlock(&mddev
->reconfig_mutex
);
611 if (mddev
->kobj
.sd
) {
612 if (to_remove
!= &md_redundancy_group
)
613 sysfs_remove_group(&mddev
->kobj
, to_remove
);
614 if (mddev
->pers
== NULL
||
615 mddev
->pers
->sync_request
== NULL
) {
616 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
617 if (mddev
->sysfs_action
)
618 sysfs_put(mddev
->sysfs_action
);
619 mddev
->sysfs_action
= NULL
;
622 mddev
->sysfs_active
= 0;
624 mutex_unlock(&mddev
->reconfig_mutex
);
626 /* As we've dropped the mutex we need a spinlock to
627 * make sure the thread doesn't disappear
629 spin_lock(&pers_lock
);
630 md_wakeup_thread(mddev
->thread
);
631 spin_unlock(&pers_lock
);
633 EXPORT_SYMBOL_GPL(mddev_unlock
);
635 struct md_rdev
*md_find_rdev_nr_rcu(struct mddev
*mddev
, int nr
)
637 struct md_rdev
*rdev
;
639 rdev_for_each_rcu(rdev
, mddev
)
640 if (rdev
->desc_nr
== nr
)
645 EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu
);
647 static struct md_rdev
*find_rdev(struct mddev
*mddev
, dev_t dev
)
649 struct md_rdev
*rdev
;
651 rdev_for_each(rdev
, mddev
)
652 if (rdev
->bdev
->bd_dev
== dev
)
658 static struct md_rdev
*find_rdev_rcu(struct mddev
*mddev
, dev_t dev
)
660 struct md_rdev
*rdev
;
662 rdev_for_each_rcu(rdev
, mddev
)
663 if (rdev
->bdev
->bd_dev
== dev
)
669 static struct md_personality
*find_pers(int level
, char *clevel
)
671 struct md_personality
*pers
;
672 list_for_each_entry(pers
, &pers_list
, list
) {
673 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
675 if (strcmp(pers
->name
, clevel
)==0)
681 /* return the offset of the super block in 512byte sectors */
682 static inline sector_t
calc_dev_sboffset(struct md_rdev
*rdev
)
684 sector_t num_sectors
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
685 return MD_NEW_SIZE_SECTORS(num_sectors
);
688 static int alloc_disk_sb(struct md_rdev
*rdev
)
690 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
696 void md_rdev_clear(struct md_rdev
*rdev
)
699 put_page(rdev
->sb_page
);
701 rdev
->sb_page
= NULL
;
706 put_page(rdev
->bb_page
);
707 rdev
->bb_page
= NULL
;
709 badblocks_exit(&rdev
->badblocks
);
711 EXPORT_SYMBOL_GPL(md_rdev_clear
);
713 static void super_written(struct bio
*bio
)
715 struct md_rdev
*rdev
= bio
->bi_private
;
716 struct mddev
*mddev
= rdev
->mddev
;
719 pr_err("md: super_written gets error=%d\n", bio
->bi_error
);
720 md_error(mddev
, rdev
);
721 if (!test_bit(Faulty
, &rdev
->flags
)
722 && (bio
->bi_opf
& MD_FAILFAST
)) {
723 set_bit(MD_SB_NEED_REWRITE
, &mddev
->sb_flags
);
724 set_bit(LastDev
, &rdev
->flags
);
727 clear_bit(LastDev
, &rdev
->flags
);
729 if (atomic_dec_and_test(&mddev
->pending_writes
))
730 wake_up(&mddev
->sb_wait
);
731 rdev_dec_pending(rdev
, mddev
);
735 void md_super_write(struct mddev
*mddev
, struct md_rdev
*rdev
,
736 sector_t sector
, int size
, struct page
*page
)
738 /* write first size bytes of page to sector of rdev
739 * Increment mddev->pending_writes before returning
740 * and decrement it on completion, waking up sb_wait
741 * if zero is reached.
742 * If an error occurred, call md_error
747 if (test_bit(Faulty
, &rdev
->flags
))
750 bio
= bio_alloc_mddev(GFP_NOIO
, 1, mddev
);
752 atomic_inc(&rdev
->nr_pending
);
754 bio
->bi_bdev
= rdev
->meta_bdev
? rdev
->meta_bdev
: rdev
->bdev
;
755 bio
->bi_iter
.bi_sector
= sector
;
756 bio_add_page(bio
, page
, size
, 0);
757 bio
->bi_private
= rdev
;
758 bio
->bi_end_io
= super_written
;
760 if (test_bit(MD_FAILFAST_SUPPORTED
, &mddev
->flags
) &&
761 test_bit(FailFast
, &rdev
->flags
) &&
762 !test_bit(LastDev
, &rdev
->flags
))
764 bio
->bi_opf
= REQ_OP_WRITE
| REQ_PREFLUSH
| REQ_FUA
| ff
;
766 atomic_inc(&mddev
->pending_writes
);
770 int md_super_wait(struct mddev
*mddev
)
772 /* wait for all superblock writes that were scheduled to complete */
773 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->pending_writes
)==0);
774 if (test_and_clear_bit(MD_SB_NEED_REWRITE
, &mddev
->sb_flags
))
779 int sync_page_io(struct md_rdev
*rdev
, sector_t sector
, int size
,
780 struct page
*page
, int op
, int op_flags
, bool metadata_op
)
782 struct bio
*bio
= bio_alloc_mddev(GFP_NOIO
, 1, rdev
->mddev
);
785 bio
->bi_bdev
= (metadata_op
&& rdev
->meta_bdev
) ?
786 rdev
->meta_bdev
: rdev
->bdev
;
787 bio_set_op_attrs(bio
, op
, op_flags
);
789 bio
->bi_iter
.bi_sector
= sector
+ rdev
->sb_start
;
790 else if (rdev
->mddev
->reshape_position
!= MaxSector
&&
791 (rdev
->mddev
->reshape_backwards
==
792 (sector
>= rdev
->mddev
->reshape_position
)))
793 bio
->bi_iter
.bi_sector
= sector
+ rdev
->new_data_offset
;
795 bio
->bi_iter
.bi_sector
= sector
+ rdev
->data_offset
;
796 bio_add_page(bio
, page
, size
, 0);
798 submit_bio_wait(bio
);
800 ret
= !bio
->bi_error
;
804 EXPORT_SYMBOL_GPL(sync_page_io
);
806 static int read_disk_sb(struct md_rdev
*rdev
, int size
)
808 char b
[BDEVNAME_SIZE
];
813 if (!sync_page_io(rdev
, 0, size
, rdev
->sb_page
, REQ_OP_READ
, 0, true))
819 pr_err("md: disabled device %s, could not read superblock.\n",
820 bdevname(rdev
->bdev
,b
));
824 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
826 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
827 sb1
->set_uuid1
== sb2
->set_uuid1
&&
828 sb1
->set_uuid2
== sb2
->set_uuid2
&&
829 sb1
->set_uuid3
== sb2
->set_uuid3
;
832 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
835 mdp_super_t
*tmp1
, *tmp2
;
837 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
838 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
840 if (!tmp1
|| !tmp2
) {
849 * nr_disks is not constant
854 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
861 static u32
md_csum_fold(u32 csum
)
863 csum
= (csum
& 0xffff) + (csum
>> 16);
864 return (csum
& 0xffff) + (csum
>> 16);
867 static unsigned int calc_sb_csum(mdp_super_t
*sb
)
870 u32
*sb32
= (u32
*)sb
;
872 unsigned int disk_csum
, csum
;
874 disk_csum
= sb
->sb_csum
;
877 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
879 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
882 /* This used to use csum_partial, which was wrong for several
883 * reasons including that different results are returned on
884 * different architectures. It isn't critical that we get exactly
885 * the same return value as before (we always csum_fold before
886 * testing, and that removes any differences). However as we
887 * know that csum_partial always returned a 16bit value on
888 * alphas, do a fold to maximise conformity to previous behaviour.
890 sb
->sb_csum
= md_csum_fold(disk_csum
);
892 sb
->sb_csum
= disk_csum
;
898 * Handle superblock details.
899 * We want to be able to handle multiple superblock formats
900 * so we have a common interface to them all, and an array of
901 * different handlers.
902 * We rely on user-space to write the initial superblock, and support
903 * reading and updating of superblocks.
904 * Interface methods are:
905 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
906 * loads and validates a superblock on dev.
907 * if refdev != NULL, compare superblocks on both devices
909 * 0 - dev has a superblock that is compatible with refdev
910 * 1 - dev has a superblock that is compatible and newer than refdev
911 * so dev should be used as the refdev in future
912 * -EINVAL superblock incompatible or invalid
913 * -othererror e.g. -EIO
915 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
916 * Verify that dev is acceptable into mddev.
917 * The first time, mddev->raid_disks will be 0, and data from
918 * dev should be merged in. Subsequent calls check that dev
919 * is new enough. Return 0 or -EINVAL
921 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
922 * Update the superblock for rdev with data in mddev
923 * This does not write to disc.
929 struct module
*owner
;
930 int (*load_super
)(struct md_rdev
*rdev
,
931 struct md_rdev
*refdev
,
933 int (*validate_super
)(struct mddev
*mddev
,
934 struct md_rdev
*rdev
);
935 void (*sync_super
)(struct mddev
*mddev
,
936 struct md_rdev
*rdev
);
937 unsigned long long (*rdev_size_change
)(struct md_rdev
*rdev
,
938 sector_t num_sectors
);
939 int (*allow_new_offset
)(struct md_rdev
*rdev
,
940 unsigned long long new_offset
);
944 * Check that the given mddev has no bitmap.
946 * This function is called from the run method of all personalities that do not
947 * support bitmaps. It prints an error message and returns non-zero if mddev
948 * has a bitmap. Otherwise, it returns 0.
951 int md_check_no_bitmap(struct mddev
*mddev
)
953 if (!mddev
->bitmap_info
.file
&& !mddev
->bitmap_info
.offset
)
955 pr_warn("%s: bitmaps are not supported for %s\n",
956 mdname(mddev
), mddev
->pers
->name
);
959 EXPORT_SYMBOL(md_check_no_bitmap
);
962 * load_super for 0.90.0
964 static int super_90_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
966 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
971 * Calculate the position of the superblock (512byte sectors),
972 * it's at the end of the disk.
974 * It also happens to be a multiple of 4Kb.
976 rdev
->sb_start
= calc_dev_sboffset(rdev
);
978 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
984 bdevname(rdev
->bdev
, b
);
985 sb
= page_address(rdev
->sb_page
);
987 if (sb
->md_magic
!= MD_SB_MAGIC
) {
988 pr_warn("md: invalid raid superblock magic on %s\n", b
);
992 if (sb
->major_version
!= 0 ||
993 sb
->minor_version
< 90 ||
994 sb
->minor_version
> 91) {
995 pr_warn("Bad version number %d.%d on %s\n",
996 sb
->major_version
, sb
->minor_version
, b
);
1000 if (sb
->raid_disks
<= 0)
1003 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
1004 pr_warn("md: invalid superblock checksum on %s\n", b
);
1008 rdev
->preferred_minor
= sb
->md_minor
;
1009 rdev
->data_offset
= 0;
1010 rdev
->new_data_offset
= 0;
1011 rdev
->sb_size
= MD_SB_BYTES
;
1012 rdev
->badblocks
.shift
= -1;
1014 if (sb
->level
== LEVEL_MULTIPATH
)
1017 rdev
->desc_nr
= sb
->this_disk
.number
;
1023 mdp_super_t
*refsb
= page_address(refdev
->sb_page
);
1024 if (!uuid_equal(refsb
, sb
)) {
1025 pr_warn("md: %s has different UUID to %s\n",
1026 b
, bdevname(refdev
->bdev
,b2
));
1029 if (!sb_equal(refsb
, sb
)) {
1030 pr_warn("md: %s has same UUID but different superblock to %s\n",
1031 b
, bdevname(refdev
->bdev
, b2
));
1035 ev2
= md_event(refsb
);
1041 rdev
->sectors
= rdev
->sb_start
;
1042 /* Limit to 4TB as metadata cannot record more than that.
1043 * (not needed for Linear and RAID0 as metadata doesn't
1046 if (IS_ENABLED(CONFIG_LBDAF
) && (u64
)rdev
->sectors
>= (2ULL << 32) &&
1048 rdev
->sectors
= (sector_t
)(2ULL << 32) - 2;
1050 if (rdev
->sectors
< ((sector_t
)sb
->size
) * 2 && sb
->level
>= 1)
1051 /* "this cannot possibly happen" ... */
1059 * validate_super for 0.90.0
1061 static int super_90_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
1064 mdp_super_t
*sb
= page_address(rdev
->sb_page
);
1065 __u64 ev1
= md_event(sb
);
1067 rdev
->raid_disk
= -1;
1068 clear_bit(Faulty
, &rdev
->flags
);
1069 clear_bit(In_sync
, &rdev
->flags
);
1070 clear_bit(Bitmap_sync
, &rdev
->flags
);
1071 clear_bit(WriteMostly
, &rdev
->flags
);
1073 if (mddev
->raid_disks
== 0) {
1074 mddev
->major_version
= 0;
1075 mddev
->minor_version
= sb
->minor_version
;
1076 mddev
->patch_version
= sb
->patch_version
;
1077 mddev
->external
= 0;
1078 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
1079 mddev
->ctime
= sb
->ctime
;
1080 mddev
->utime
= sb
->utime
;
1081 mddev
->level
= sb
->level
;
1082 mddev
->clevel
[0] = 0;
1083 mddev
->layout
= sb
->layout
;
1084 mddev
->raid_disks
= sb
->raid_disks
;
1085 mddev
->dev_sectors
= ((sector_t
)sb
->size
) * 2;
1086 mddev
->events
= ev1
;
1087 mddev
->bitmap_info
.offset
= 0;
1088 mddev
->bitmap_info
.space
= 0;
1089 /* bitmap can use 60 K after the 4K superblocks */
1090 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
1091 mddev
->bitmap_info
.default_space
= 64*2 - (MD_SB_BYTES
>> 9);
1092 mddev
->reshape_backwards
= 0;
1094 if (mddev
->minor_version
>= 91) {
1095 mddev
->reshape_position
= sb
->reshape_position
;
1096 mddev
->delta_disks
= sb
->delta_disks
;
1097 mddev
->new_level
= sb
->new_level
;
1098 mddev
->new_layout
= sb
->new_layout
;
1099 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
1100 if (mddev
->delta_disks
< 0)
1101 mddev
->reshape_backwards
= 1;
1103 mddev
->reshape_position
= MaxSector
;
1104 mddev
->delta_disks
= 0;
1105 mddev
->new_level
= mddev
->level
;
1106 mddev
->new_layout
= mddev
->layout
;
1107 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1110 if (sb
->state
& (1<<MD_SB_CLEAN
))
1111 mddev
->recovery_cp
= MaxSector
;
1113 if (sb
->events_hi
== sb
->cp_events_hi
&&
1114 sb
->events_lo
== sb
->cp_events_lo
) {
1115 mddev
->recovery_cp
= sb
->recovery_cp
;
1117 mddev
->recovery_cp
= 0;
1120 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
1121 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
1122 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
1123 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
1125 mddev
->max_disks
= MD_SB_DISKS
;
1127 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
1128 mddev
->bitmap_info
.file
== NULL
) {
1129 mddev
->bitmap_info
.offset
=
1130 mddev
->bitmap_info
.default_offset
;
1131 mddev
->bitmap_info
.space
=
1132 mddev
->bitmap_info
.default_space
;
1135 } else if (mddev
->pers
== NULL
) {
1136 /* Insist on good event counter while assembling, except
1137 * for spares (which don't need an event count) */
1139 if (sb
->disks
[rdev
->desc_nr
].state
& (
1140 (1<<MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
)))
1141 if (ev1
< mddev
->events
)
1143 } else if (mddev
->bitmap
) {
1144 /* if adding to array with a bitmap, then we can accept an
1145 * older device ... but not too old.
1147 if (ev1
< mddev
->bitmap
->events_cleared
)
1149 if (ev1
< mddev
->events
)
1150 set_bit(Bitmap_sync
, &rdev
->flags
);
1152 if (ev1
< mddev
->events
)
1153 /* just a hot-add of a new device, leave raid_disk at -1 */
1157 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1158 desc
= sb
->disks
+ rdev
->desc_nr
;
1160 if (desc
->state
& (1<<MD_DISK_FAULTY
))
1161 set_bit(Faulty
, &rdev
->flags
);
1162 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
1163 desc->raid_disk < mddev->raid_disks */) {
1164 set_bit(In_sync
, &rdev
->flags
);
1165 rdev
->raid_disk
= desc
->raid_disk
;
1166 rdev
->saved_raid_disk
= desc
->raid_disk
;
1167 } else if (desc
->state
& (1<<MD_DISK_ACTIVE
)) {
1168 /* active but not in sync implies recovery up to
1169 * reshape position. We don't know exactly where
1170 * that is, so set to zero for now */
1171 if (mddev
->minor_version
>= 91) {
1172 rdev
->recovery_offset
= 0;
1173 rdev
->raid_disk
= desc
->raid_disk
;
1176 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
1177 set_bit(WriteMostly
, &rdev
->flags
);
1178 if (desc
->state
& (1<<MD_DISK_FAILFAST
))
1179 set_bit(FailFast
, &rdev
->flags
);
1180 } else /* MULTIPATH are always insync */
1181 set_bit(In_sync
, &rdev
->flags
);
1186 * sync_super for 0.90.0
1188 static void super_90_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
1191 struct md_rdev
*rdev2
;
1192 int next_spare
= mddev
->raid_disks
;
1194 /* make rdev->sb match mddev data..
1197 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1198 * 3/ any empty disks < next_spare become removed
1200 * disks[0] gets initialised to REMOVED because
1201 * we cannot be sure from other fields if it has
1202 * been initialised or not.
1205 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
1207 rdev
->sb_size
= MD_SB_BYTES
;
1209 sb
= page_address(rdev
->sb_page
);
1211 memset(sb
, 0, sizeof(*sb
));
1213 sb
->md_magic
= MD_SB_MAGIC
;
1214 sb
->major_version
= mddev
->major_version
;
1215 sb
->patch_version
= mddev
->patch_version
;
1216 sb
->gvalid_words
= 0; /* ignored */
1217 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
1218 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
1219 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
1220 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
1222 sb
->ctime
= clamp_t(time64_t
, mddev
->ctime
, 0, U32_MAX
);
1223 sb
->level
= mddev
->level
;
1224 sb
->size
= mddev
->dev_sectors
/ 2;
1225 sb
->raid_disks
= mddev
->raid_disks
;
1226 sb
->md_minor
= mddev
->md_minor
;
1227 sb
->not_persistent
= 0;
1228 sb
->utime
= clamp_t(time64_t
, mddev
->utime
, 0, U32_MAX
);
1230 sb
->events_hi
= (mddev
->events
>>32);
1231 sb
->events_lo
= (u32
)mddev
->events
;
1233 if (mddev
->reshape_position
== MaxSector
)
1234 sb
->minor_version
= 90;
1236 sb
->minor_version
= 91;
1237 sb
->reshape_position
= mddev
->reshape_position
;
1238 sb
->new_level
= mddev
->new_level
;
1239 sb
->delta_disks
= mddev
->delta_disks
;
1240 sb
->new_layout
= mddev
->new_layout
;
1241 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1243 mddev
->minor_version
= sb
->minor_version
;
1246 sb
->recovery_cp
= mddev
->recovery_cp
;
1247 sb
->cp_events_hi
= (mddev
->events
>>32);
1248 sb
->cp_events_lo
= (u32
)mddev
->events
;
1249 if (mddev
->recovery_cp
== MaxSector
)
1250 sb
->state
= (1<< MD_SB_CLEAN
);
1252 sb
->recovery_cp
= 0;
1254 sb
->layout
= mddev
->layout
;
1255 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1257 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
)
1258 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1260 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1261 rdev_for_each(rdev2
, mddev
) {
1264 int is_active
= test_bit(In_sync
, &rdev2
->flags
);
1266 if (rdev2
->raid_disk
>= 0 &&
1267 sb
->minor_version
>= 91)
1268 /* we have nowhere to store the recovery_offset,
1269 * but if it is not below the reshape_position,
1270 * we can piggy-back on that.
1273 if (rdev2
->raid_disk
< 0 ||
1274 test_bit(Faulty
, &rdev2
->flags
))
1277 desc_nr
= rdev2
->raid_disk
;
1279 desc_nr
= next_spare
++;
1280 rdev2
->desc_nr
= desc_nr
;
1281 d
= &sb
->disks
[rdev2
->desc_nr
];
1283 d
->number
= rdev2
->desc_nr
;
1284 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1285 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1287 d
->raid_disk
= rdev2
->raid_disk
;
1289 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1290 if (test_bit(Faulty
, &rdev2
->flags
))
1291 d
->state
= (1<<MD_DISK_FAULTY
);
1292 else if (is_active
) {
1293 d
->state
= (1<<MD_DISK_ACTIVE
);
1294 if (test_bit(In_sync
, &rdev2
->flags
))
1295 d
->state
|= (1<<MD_DISK_SYNC
);
1303 if (test_bit(WriteMostly
, &rdev2
->flags
))
1304 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1305 if (test_bit(FailFast
, &rdev2
->flags
))
1306 d
->state
|= (1<<MD_DISK_FAILFAST
);
1308 /* now set the "removed" and "faulty" bits on any missing devices */
1309 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1310 mdp_disk_t
*d
= &sb
->disks
[i
];
1311 if (d
->state
== 0 && d
->number
== 0) {
1314 d
->state
= (1<<MD_DISK_REMOVED
);
1315 d
->state
|= (1<<MD_DISK_FAULTY
);
1319 sb
->nr_disks
= nr_disks
;
1320 sb
->active_disks
= active
;
1321 sb
->working_disks
= working
;
1322 sb
->failed_disks
= failed
;
1323 sb
->spare_disks
= spare
;
1325 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1326 sb
->sb_csum
= calc_sb_csum(sb
);
1330 * rdev_size_change for 0.90.0
1332 static unsigned long long
1333 super_90_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
1335 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1336 return 0; /* component must fit device */
1337 if (rdev
->mddev
->bitmap_info
.offset
)
1338 return 0; /* can't move bitmap */
1339 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1340 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1341 num_sectors
= rdev
->sb_start
;
1342 /* Limit to 4TB as metadata cannot record more than that.
1343 * 4TB == 2^32 KB, or 2*2^32 sectors.
1345 if (IS_ENABLED(CONFIG_LBDAF
) && (u64
)num_sectors
>= (2ULL << 32) &&
1346 rdev
->mddev
->level
>= 1)
1347 num_sectors
= (sector_t
)(2ULL << 32) - 2;
1349 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1351 } while (md_super_wait(rdev
->mddev
) < 0);
1356 super_90_allow_new_offset(struct md_rdev
*rdev
, unsigned long long new_offset
)
1358 /* non-zero offset changes not possible with v0.90 */
1359 return new_offset
== 0;
1363 * version 1 superblock
1366 static __le32
calc_sb_1_csum(struct mdp_superblock_1
*sb
)
1370 unsigned long long newcsum
;
1371 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1372 __le32
*isuper
= (__le32
*)sb
;
1374 disk_csum
= sb
->sb_csum
;
1377 for (; size
>= 4; size
-= 4)
1378 newcsum
+= le32_to_cpu(*isuper
++);
1381 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1383 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1384 sb
->sb_csum
= disk_csum
;
1385 return cpu_to_le32(csum
);
1388 static int super_1_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
1390 struct mdp_superblock_1
*sb
;
1394 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1398 * Calculate the position of the superblock in 512byte sectors.
1399 * It is always aligned to a 4K boundary and
1400 * depeding on minor_version, it can be:
1401 * 0: At least 8K, but less than 12K, from end of device
1402 * 1: At start of device
1403 * 2: 4K from start of device.
1405 switch(minor_version
) {
1407 sb_start
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1409 sb_start
&= ~(sector_t
)(4*2-1);
1420 rdev
->sb_start
= sb_start
;
1422 /* superblock is rarely larger than 1K, but it can be larger,
1423 * and it is safe to read 4k, so we do that
1425 ret
= read_disk_sb(rdev
, 4096);
1426 if (ret
) return ret
;
1428 sb
= page_address(rdev
->sb_page
);
1430 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1431 sb
->major_version
!= cpu_to_le32(1) ||
1432 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1433 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1434 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1437 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1438 pr_warn("md: invalid superblock checksum on %s\n",
1439 bdevname(rdev
->bdev
,b
));
1442 if (le64_to_cpu(sb
->data_size
) < 10) {
1443 pr_warn("md: data_size too small on %s\n",
1444 bdevname(rdev
->bdev
,b
));
1449 memcmp(sb
->pad3
, sb
->pad3
+1, sizeof(sb
->pad3
) - sizeof(sb
->pad3
[1])))
1450 /* Some padding is non-zero, might be a new feature */
1453 rdev
->preferred_minor
= 0xffff;
1454 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1455 rdev
->new_data_offset
= rdev
->data_offset
;
1456 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
) &&
1457 (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_NEW_OFFSET
))
1458 rdev
->new_data_offset
+= (s32
)le32_to_cpu(sb
->new_offset
);
1459 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1461 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1462 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1463 if (rdev
->sb_size
& bmask
)
1464 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1467 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1470 && rdev
->new_data_offset
< sb_start
+ (rdev
->sb_size
/512))
1473 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1476 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1478 if (!rdev
->bb_page
) {
1479 rdev
->bb_page
= alloc_page(GFP_KERNEL
);
1483 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BAD_BLOCKS
) &&
1484 rdev
->badblocks
.count
== 0) {
1485 /* need to load the bad block list.
1486 * Currently we limit it to one page.
1492 int sectors
= le16_to_cpu(sb
->bblog_size
);
1493 if (sectors
> (PAGE_SIZE
/ 512))
1495 offset
= le32_to_cpu(sb
->bblog_offset
);
1498 bb_sector
= (long long)offset
;
1499 if (!sync_page_io(rdev
, bb_sector
, sectors
<< 9,
1500 rdev
->bb_page
, REQ_OP_READ
, 0, true))
1502 bbp
= (u64
*)page_address(rdev
->bb_page
);
1503 rdev
->badblocks
.shift
= sb
->bblog_shift
;
1504 for (i
= 0 ; i
< (sectors
<< (9-3)) ; i
++, bbp
++) {
1505 u64 bb
= le64_to_cpu(*bbp
);
1506 int count
= bb
& (0x3ff);
1507 u64 sector
= bb
>> 10;
1508 sector
<<= sb
->bblog_shift
;
1509 count
<<= sb
->bblog_shift
;
1512 if (badblocks_set(&rdev
->badblocks
, sector
, count
, 1))
1515 } else if (sb
->bblog_offset
!= 0)
1516 rdev
->badblocks
.shift
= 0;
1522 struct mdp_superblock_1
*refsb
= page_address(refdev
->sb_page
);
1524 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1525 sb
->level
!= refsb
->level
||
1526 sb
->layout
!= refsb
->layout
||
1527 sb
->chunksize
!= refsb
->chunksize
) {
1528 pr_warn("md: %s has strangely different superblock to %s\n",
1529 bdevname(rdev
->bdev
,b
),
1530 bdevname(refdev
->bdev
,b2
));
1533 ev1
= le64_to_cpu(sb
->events
);
1534 ev2
= le64_to_cpu(refsb
->events
);
1541 if (minor_version
) {
1542 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9);
1543 sectors
-= rdev
->data_offset
;
1545 sectors
= rdev
->sb_start
;
1546 if (sectors
< le64_to_cpu(sb
->data_size
))
1548 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1552 static int super_1_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
1554 struct mdp_superblock_1
*sb
= page_address(rdev
->sb_page
);
1555 __u64 ev1
= le64_to_cpu(sb
->events
);
1557 rdev
->raid_disk
= -1;
1558 clear_bit(Faulty
, &rdev
->flags
);
1559 clear_bit(In_sync
, &rdev
->flags
);
1560 clear_bit(Bitmap_sync
, &rdev
->flags
);
1561 clear_bit(WriteMostly
, &rdev
->flags
);
1563 if (mddev
->raid_disks
== 0) {
1564 mddev
->major_version
= 1;
1565 mddev
->patch_version
= 0;
1566 mddev
->external
= 0;
1567 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1568 mddev
->ctime
= le64_to_cpu(sb
->ctime
);
1569 mddev
->utime
= le64_to_cpu(sb
->utime
);
1570 mddev
->level
= le32_to_cpu(sb
->level
);
1571 mddev
->clevel
[0] = 0;
1572 mddev
->layout
= le32_to_cpu(sb
->layout
);
1573 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1574 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1575 mddev
->events
= ev1
;
1576 mddev
->bitmap_info
.offset
= 0;
1577 mddev
->bitmap_info
.space
= 0;
1578 /* Default location for bitmap is 1K after superblock
1579 * using 3K - total of 4K
1581 mddev
->bitmap_info
.default_offset
= 1024 >> 9;
1582 mddev
->bitmap_info
.default_space
= (4096-1024) >> 9;
1583 mddev
->reshape_backwards
= 0;
1585 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1586 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1588 mddev
->max_disks
= (4096-256)/2;
1590 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1591 mddev
->bitmap_info
.file
== NULL
) {
1592 mddev
->bitmap_info
.offset
=
1593 (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1594 /* Metadata doesn't record how much space is available.
1595 * For 1.0, we assume we can use up to the superblock
1596 * if before, else to 4K beyond superblock.
1597 * For others, assume no change is possible.
1599 if (mddev
->minor_version
> 0)
1600 mddev
->bitmap_info
.space
= 0;
1601 else if (mddev
->bitmap_info
.offset
> 0)
1602 mddev
->bitmap_info
.space
=
1603 8 - mddev
->bitmap_info
.offset
;
1605 mddev
->bitmap_info
.space
=
1606 -mddev
->bitmap_info
.offset
;
1609 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1610 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1611 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1612 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1613 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1614 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1615 if (mddev
->delta_disks
< 0 ||
1616 (mddev
->delta_disks
== 0 &&
1617 (le32_to_cpu(sb
->feature_map
)
1618 & MD_FEATURE_RESHAPE_BACKWARDS
)))
1619 mddev
->reshape_backwards
= 1;
1621 mddev
->reshape_position
= MaxSector
;
1622 mddev
->delta_disks
= 0;
1623 mddev
->new_level
= mddev
->level
;
1624 mddev
->new_layout
= mddev
->layout
;
1625 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1628 if (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_JOURNAL
)
1629 set_bit(MD_HAS_JOURNAL
, &mddev
->flags
);
1630 } else if (mddev
->pers
== NULL
) {
1631 /* Insist of good event counter while assembling, except for
1632 * spares (which don't need an event count) */
1634 if (rdev
->desc_nr
>= 0 &&
1635 rdev
->desc_nr
< le32_to_cpu(sb
->max_dev
) &&
1636 (le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) < MD_DISK_ROLE_MAX
||
1637 le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) == MD_DISK_ROLE_JOURNAL
))
1638 if (ev1
< mddev
->events
)
1640 } else if (mddev
->bitmap
) {
1641 /* If adding to array with a bitmap, then we can accept an
1642 * older device, but not too old.
1644 if (ev1
< mddev
->bitmap
->events_cleared
)
1646 if (ev1
< mddev
->events
)
1647 set_bit(Bitmap_sync
, &rdev
->flags
);
1649 if (ev1
< mddev
->events
)
1650 /* just a hot-add of a new device, leave raid_disk at -1 */
1653 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1655 if (rdev
->desc_nr
< 0 ||
1656 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1657 role
= MD_DISK_ROLE_SPARE
;
1660 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1662 case MD_DISK_ROLE_SPARE
: /* spare */
1664 case MD_DISK_ROLE_FAULTY
: /* faulty */
1665 set_bit(Faulty
, &rdev
->flags
);
1667 case MD_DISK_ROLE_JOURNAL
: /* journal device */
1668 if (!(le32_to_cpu(sb
->feature_map
) & MD_FEATURE_JOURNAL
)) {
1669 /* journal device without journal feature */
1670 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
1673 set_bit(Journal
, &rdev
->flags
);
1674 rdev
->journal_tail
= le64_to_cpu(sb
->journal_tail
);
1675 rdev
->raid_disk
= 0;
1678 rdev
->saved_raid_disk
= role
;
1679 if ((le32_to_cpu(sb
->feature_map
) &
1680 MD_FEATURE_RECOVERY_OFFSET
)) {
1681 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1682 if (!(le32_to_cpu(sb
->feature_map
) &
1683 MD_FEATURE_RECOVERY_BITMAP
))
1684 rdev
->saved_raid_disk
= -1;
1686 set_bit(In_sync
, &rdev
->flags
);
1687 rdev
->raid_disk
= role
;
1690 if (sb
->devflags
& WriteMostly1
)
1691 set_bit(WriteMostly
, &rdev
->flags
);
1692 if (sb
->devflags
& FailFast1
)
1693 set_bit(FailFast
, &rdev
->flags
);
1694 if (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_REPLACEMENT
)
1695 set_bit(Replacement
, &rdev
->flags
);
1696 } else /* MULTIPATH are always insync */
1697 set_bit(In_sync
, &rdev
->flags
);
1702 static void super_1_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
1704 struct mdp_superblock_1
*sb
;
1705 struct md_rdev
*rdev2
;
1707 /* make rdev->sb match mddev and rdev data. */
1709 sb
= page_address(rdev
->sb_page
);
1711 sb
->feature_map
= 0;
1713 sb
->recovery_offset
= cpu_to_le64(0);
1714 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1716 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1717 sb
->events
= cpu_to_le64(mddev
->events
);
1719 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1720 else if (test_bit(MD_JOURNAL_CLEAN
, &mddev
->flags
))
1721 sb
->resync_offset
= cpu_to_le64(MaxSector
);
1723 sb
->resync_offset
= cpu_to_le64(0);
1725 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1727 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1728 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
1729 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
1730 sb
->level
= cpu_to_le32(mddev
->level
);
1731 sb
->layout
= cpu_to_le32(mddev
->layout
);
1732 if (test_bit(FailFast
, &rdev
->flags
))
1733 sb
->devflags
|= FailFast1
;
1735 sb
->devflags
&= ~FailFast1
;
1737 if (test_bit(WriteMostly
, &rdev
->flags
))
1738 sb
->devflags
|= WriteMostly1
;
1740 sb
->devflags
&= ~WriteMostly1
;
1741 sb
->data_offset
= cpu_to_le64(rdev
->data_offset
);
1742 sb
->data_size
= cpu_to_le64(rdev
->sectors
);
1744 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
) {
1745 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_info
.offset
);
1746 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1749 if (rdev
->raid_disk
>= 0 && !test_bit(Journal
, &rdev
->flags
) &&
1750 !test_bit(In_sync
, &rdev
->flags
)) {
1752 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1753 sb
->recovery_offset
=
1754 cpu_to_le64(rdev
->recovery_offset
);
1755 if (rdev
->saved_raid_disk
>= 0 && mddev
->bitmap
)
1757 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP
);
1759 /* Note: recovery_offset and journal_tail share space */
1760 if (test_bit(Journal
, &rdev
->flags
))
1761 sb
->journal_tail
= cpu_to_le64(rdev
->journal_tail
);
1762 if (test_bit(Replacement
, &rdev
->flags
))
1764 cpu_to_le32(MD_FEATURE_REPLACEMENT
);
1766 if (mddev
->reshape_position
!= MaxSector
) {
1767 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1768 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1769 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1770 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1771 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1772 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
1773 if (mddev
->delta_disks
== 0 &&
1774 mddev
->reshape_backwards
)
1776 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS
);
1777 if (rdev
->new_data_offset
!= rdev
->data_offset
) {
1779 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET
);
1780 sb
->new_offset
= cpu_to_le32((__u32
)(rdev
->new_data_offset
1781 - rdev
->data_offset
));
1785 if (mddev_is_clustered(mddev
))
1786 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_CLUSTERED
);
1788 if (rdev
->badblocks
.count
== 0)
1789 /* Nothing to do for bad blocks*/ ;
1790 else if (sb
->bblog_offset
== 0)
1791 /* Cannot record bad blocks on this device */
1792 md_error(mddev
, rdev
);
1794 struct badblocks
*bb
= &rdev
->badblocks
;
1795 u64
*bbp
= (u64
*)page_address(rdev
->bb_page
);
1797 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_BAD_BLOCKS
);
1802 seq
= read_seqbegin(&bb
->lock
);
1804 memset(bbp
, 0xff, PAGE_SIZE
);
1806 for (i
= 0 ; i
< bb
->count
; i
++) {
1807 u64 internal_bb
= p
[i
];
1808 u64 store_bb
= ((BB_OFFSET(internal_bb
) << 10)
1809 | BB_LEN(internal_bb
));
1810 bbp
[i
] = cpu_to_le64(store_bb
);
1813 if (read_seqretry(&bb
->lock
, seq
))
1816 bb
->sector
= (rdev
->sb_start
+
1817 (int)le32_to_cpu(sb
->bblog_offset
));
1818 bb
->size
= le16_to_cpu(sb
->bblog_size
);
1823 rdev_for_each(rdev2
, mddev
)
1824 if (rdev2
->desc_nr
+1 > max_dev
)
1825 max_dev
= rdev2
->desc_nr
+1;
1827 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
1829 sb
->max_dev
= cpu_to_le32(max_dev
);
1830 rdev
->sb_size
= max_dev
* 2 + 256;
1831 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1832 if (rdev
->sb_size
& bmask
)
1833 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1835 max_dev
= le32_to_cpu(sb
->max_dev
);
1837 for (i
=0; i
<max_dev
;i
++)
1838 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_FAULTY
);
1840 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
))
1841 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_JOURNAL
);
1843 rdev_for_each(rdev2
, mddev
) {
1845 if (test_bit(Faulty
, &rdev2
->flags
))
1846 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_FAULTY
);
1847 else if (test_bit(In_sync
, &rdev2
->flags
))
1848 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1849 else if (test_bit(Journal
, &rdev2
->flags
))
1850 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_JOURNAL
);
1851 else if (rdev2
->raid_disk
>= 0)
1852 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1854 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_SPARE
);
1857 sb
->sb_csum
= calc_sb_1_csum(sb
);
1860 static unsigned long long
1861 super_1_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
1863 struct mdp_superblock_1
*sb
;
1864 sector_t max_sectors
;
1865 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1866 return 0; /* component must fit device */
1867 if (rdev
->data_offset
!= rdev
->new_data_offset
)
1868 return 0; /* too confusing */
1869 if (rdev
->sb_start
< rdev
->data_offset
) {
1870 /* minor versions 1 and 2; superblock before data */
1871 max_sectors
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1872 max_sectors
-= rdev
->data_offset
;
1873 if (!num_sectors
|| num_sectors
> max_sectors
)
1874 num_sectors
= max_sectors
;
1875 } else if (rdev
->mddev
->bitmap_info
.offset
) {
1876 /* minor version 0 with bitmap we can't move */
1879 /* minor version 0; superblock after data */
1881 sb_start
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) - 8*2;
1882 sb_start
&= ~(sector_t
)(4*2 - 1);
1883 max_sectors
= rdev
->sectors
+ sb_start
- rdev
->sb_start
;
1884 if (!num_sectors
|| num_sectors
> max_sectors
)
1885 num_sectors
= max_sectors
;
1886 rdev
->sb_start
= sb_start
;
1888 sb
= page_address(rdev
->sb_page
);
1889 sb
->data_size
= cpu_to_le64(num_sectors
);
1890 sb
->super_offset
= rdev
->sb_start
;
1891 sb
->sb_csum
= calc_sb_1_csum(sb
);
1893 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1895 } while (md_super_wait(rdev
->mddev
) < 0);
1901 super_1_allow_new_offset(struct md_rdev
*rdev
,
1902 unsigned long long new_offset
)
1904 /* All necessary checks on new >= old have been done */
1905 struct bitmap
*bitmap
;
1906 if (new_offset
>= rdev
->data_offset
)
1909 /* with 1.0 metadata, there is no metadata to tread on
1910 * so we can always move back */
1911 if (rdev
->mddev
->minor_version
== 0)
1914 /* otherwise we must be sure not to step on
1915 * any metadata, so stay:
1916 * 36K beyond start of superblock
1917 * beyond end of badblocks
1918 * beyond write-intent bitmap
1920 if (rdev
->sb_start
+ (32+4)*2 > new_offset
)
1922 bitmap
= rdev
->mddev
->bitmap
;
1923 if (bitmap
&& !rdev
->mddev
->bitmap_info
.file
&&
1924 rdev
->sb_start
+ rdev
->mddev
->bitmap_info
.offset
+
1925 bitmap
->storage
.file_pages
* (PAGE_SIZE
>>9) > new_offset
)
1927 if (rdev
->badblocks
.sector
+ rdev
->badblocks
.size
> new_offset
)
1933 static struct super_type super_types
[] = {
1936 .owner
= THIS_MODULE
,
1937 .load_super
= super_90_load
,
1938 .validate_super
= super_90_validate
,
1939 .sync_super
= super_90_sync
,
1940 .rdev_size_change
= super_90_rdev_size_change
,
1941 .allow_new_offset
= super_90_allow_new_offset
,
1945 .owner
= THIS_MODULE
,
1946 .load_super
= super_1_load
,
1947 .validate_super
= super_1_validate
,
1948 .sync_super
= super_1_sync
,
1949 .rdev_size_change
= super_1_rdev_size_change
,
1950 .allow_new_offset
= super_1_allow_new_offset
,
1954 static void sync_super(struct mddev
*mddev
, struct md_rdev
*rdev
)
1956 if (mddev
->sync_super
) {
1957 mddev
->sync_super(mddev
, rdev
);
1961 BUG_ON(mddev
->major_version
>= ARRAY_SIZE(super_types
));
1963 super_types
[mddev
->major_version
].sync_super(mddev
, rdev
);
1966 static int match_mddev_units(struct mddev
*mddev1
, struct mddev
*mddev2
)
1968 struct md_rdev
*rdev
, *rdev2
;
1971 rdev_for_each_rcu(rdev
, mddev1
) {
1972 if (test_bit(Faulty
, &rdev
->flags
) ||
1973 test_bit(Journal
, &rdev
->flags
) ||
1974 rdev
->raid_disk
== -1)
1976 rdev_for_each_rcu(rdev2
, mddev2
) {
1977 if (test_bit(Faulty
, &rdev2
->flags
) ||
1978 test_bit(Journal
, &rdev2
->flags
) ||
1979 rdev2
->raid_disk
== -1)
1981 if (rdev
->bdev
->bd_contains
==
1982 rdev2
->bdev
->bd_contains
) {
1992 static LIST_HEAD(pending_raid_disks
);
1995 * Try to register data integrity profile for an mddev
1997 * This is called when an array is started and after a disk has been kicked
1998 * from the array. It only succeeds if all working and active component devices
1999 * are integrity capable with matching profiles.
2001 int md_integrity_register(struct mddev
*mddev
)
2003 struct md_rdev
*rdev
, *reference
= NULL
;
2005 if (list_empty(&mddev
->disks
))
2006 return 0; /* nothing to do */
2007 if (!mddev
->gendisk
|| blk_get_integrity(mddev
->gendisk
))
2008 return 0; /* shouldn't register, or already is */
2009 rdev_for_each(rdev
, mddev
) {
2010 /* skip spares and non-functional disks */
2011 if (test_bit(Faulty
, &rdev
->flags
))
2013 if (rdev
->raid_disk
< 0)
2016 /* Use the first rdev as the reference */
2020 /* does this rdev's profile match the reference profile? */
2021 if (blk_integrity_compare(reference
->bdev
->bd_disk
,
2022 rdev
->bdev
->bd_disk
) < 0)
2025 if (!reference
|| !bdev_get_integrity(reference
->bdev
))
2028 * All component devices are integrity capable and have matching
2029 * profiles, register the common profile for the md device.
2031 blk_integrity_register(mddev
->gendisk
,
2032 bdev_get_integrity(reference
->bdev
));
2034 pr_debug("md: data integrity enabled on %s\n", mdname(mddev
));
2035 if (bioset_integrity_create(mddev
->bio_set
, BIO_POOL_SIZE
)) {
2036 pr_err("md: failed to create integrity pool for %s\n",
2042 EXPORT_SYMBOL(md_integrity_register
);
2045 * Attempt to add an rdev, but only if it is consistent with the current
2048 int md_integrity_add_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
2050 struct blk_integrity
*bi_rdev
;
2051 struct blk_integrity
*bi_mddev
;
2052 char name
[BDEVNAME_SIZE
];
2054 if (!mddev
->gendisk
)
2057 bi_rdev
= bdev_get_integrity(rdev
->bdev
);
2058 bi_mddev
= blk_get_integrity(mddev
->gendisk
);
2060 if (!bi_mddev
) /* nothing to do */
2063 if (blk_integrity_compare(mddev
->gendisk
, rdev
->bdev
->bd_disk
) != 0) {
2064 pr_err("%s: incompatible integrity profile for %s\n",
2065 mdname(mddev
), bdevname(rdev
->bdev
, name
));
2071 EXPORT_SYMBOL(md_integrity_add_rdev
);
2073 static int bind_rdev_to_array(struct md_rdev
*rdev
, struct mddev
*mddev
)
2075 char b
[BDEVNAME_SIZE
];
2079 /* prevent duplicates */
2080 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
2083 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2084 if (!test_bit(Journal
, &rdev
->flags
) &&
2086 (mddev
->dev_sectors
== 0 || rdev
->sectors
< mddev
->dev_sectors
)) {
2088 /* Cannot change size, so fail
2089 * If mddev->level <= 0, then we don't care
2090 * about aligning sizes (e.g. linear)
2092 if (mddev
->level
> 0)
2095 mddev
->dev_sectors
= rdev
->sectors
;
2098 /* Verify rdev->desc_nr is unique.
2099 * If it is -1, assign a free number, else
2100 * check number is not in use
2103 if (rdev
->desc_nr
< 0) {
2106 choice
= mddev
->raid_disks
;
2107 while (md_find_rdev_nr_rcu(mddev
, choice
))
2109 rdev
->desc_nr
= choice
;
2111 if (md_find_rdev_nr_rcu(mddev
, rdev
->desc_nr
)) {
2117 if (!test_bit(Journal
, &rdev
->flags
) &&
2118 mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
2119 pr_warn("md: %s: array is limited to %d devices\n",
2120 mdname(mddev
), mddev
->max_disks
);
2123 bdevname(rdev
->bdev
,b
);
2124 strreplace(b
, '/', '!');
2126 rdev
->mddev
= mddev
;
2127 pr_debug("md: bind<%s>\n", b
);
2129 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
2132 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
2133 if (sysfs_create_link(&rdev
->kobj
, ko
, "block"))
2134 /* failure here is OK */;
2135 rdev
->sysfs_state
= sysfs_get_dirent_safe(rdev
->kobj
.sd
, "state");
2137 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
2138 bd_link_disk_holder(rdev
->bdev
, mddev
->gendisk
);
2140 /* May as well allow recovery to be retried once */
2141 mddev
->recovery_disabled
++;
2146 pr_warn("md: failed to register dev-%s for %s\n",
2151 static void md_delayed_delete(struct work_struct
*ws
)
2153 struct md_rdev
*rdev
= container_of(ws
, struct md_rdev
, del_work
);
2154 kobject_del(&rdev
->kobj
);
2155 kobject_put(&rdev
->kobj
);
2158 static void unbind_rdev_from_array(struct md_rdev
*rdev
)
2160 char b
[BDEVNAME_SIZE
];
2162 bd_unlink_disk_holder(rdev
->bdev
, rdev
->mddev
->gendisk
);
2163 list_del_rcu(&rdev
->same_set
);
2164 pr_debug("md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
2166 sysfs_remove_link(&rdev
->kobj
, "block");
2167 sysfs_put(rdev
->sysfs_state
);
2168 rdev
->sysfs_state
= NULL
;
2169 rdev
->badblocks
.count
= 0;
2170 /* We need to delay this, otherwise we can deadlock when
2171 * writing to 'remove' to "dev/state". We also need
2172 * to delay it due to rcu usage.
2175 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
2176 kobject_get(&rdev
->kobj
);
2177 queue_work(md_misc_wq
, &rdev
->del_work
);
2181 * prevent the device from being mounted, repartitioned or
2182 * otherwise reused by a RAID array (or any other kernel
2183 * subsystem), by bd_claiming the device.
2185 static int lock_rdev(struct md_rdev
*rdev
, dev_t dev
, int shared
)
2188 struct block_device
*bdev
;
2189 char b
[BDEVNAME_SIZE
];
2191 bdev
= blkdev_get_by_dev(dev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
,
2192 shared
? (struct md_rdev
*)lock_rdev
: rdev
);
2194 pr_warn("md: could not open %s.\n", __bdevname(dev
, b
));
2195 return PTR_ERR(bdev
);
2201 static void unlock_rdev(struct md_rdev
*rdev
)
2203 struct block_device
*bdev
= rdev
->bdev
;
2205 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
2208 void md_autodetect_dev(dev_t dev
);
2210 static void export_rdev(struct md_rdev
*rdev
)
2212 char b
[BDEVNAME_SIZE
];
2214 pr_debug("md: export_rdev(%s)\n", bdevname(rdev
->bdev
,b
));
2215 md_rdev_clear(rdev
);
2217 if (test_bit(AutoDetected
, &rdev
->flags
))
2218 md_autodetect_dev(rdev
->bdev
->bd_dev
);
2221 kobject_put(&rdev
->kobj
);
2224 void md_kick_rdev_from_array(struct md_rdev
*rdev
)
2226 unbind_rdev_from_array(rdev
);
2229 EXPORT_SYMBOL_GPL(md_kick_rdev_from_array
);
2231 static void export_array(struct mddev
*mddev
)
2233 struct md_rdev
*rdev
;
2235 while (!list_empty(&mddev
->disks
)) {
2236 rdev
= list_first_entry(&mddev
->disks
, struct md_rdev
,
2238 md_kick_rdev_from_array(rdev
);
2240 mddev
->raid_disks
= 0;
2241 mddev
->major_version
= 0;
2244 static void sync_sbs(struct mddev
*mddev
, int nospares
)
2246 /* Update each superblock (in-memory image), but
2247 * if we are allowed to, skip spares which already
2248 * have the right event counter, or have one earlier
2249 * (which would mean they aren't being marked as dirty
2250 * with the rest of the array)
2252 struct md_rdev
*rdev
;
2253 rdev_for_each(rdev
, mddev
) {
2254 if (rdev
->sb_events
== mddev
->events
||
2256 rdev
->raid_disk
< 0 &&
2257 rdev
->sb_events
+1 == mddev
->events
)) {
2258 /* Don't update this superblock */
2259 rdev
->sb_loaded
= 2;
2261 sync_super(mddev
, rdev
);
2262 rdev
->sb_loaded
= 1;
2267 static bool does_sb_need_changing(struct mddev
*mddev
)
2269 struct md_rdev
*rdev
;
2270 struct mdp_superblock_1
*sb
;
2273 /* Find a good rdev */
2274 rdev_for_each(rdev
, mddev
)
2275 if ((rdev
->raid_disk
>= 0) && !test_bit(Faulty
, &rdev
->flags
))
2278 /* No good device found. */
2282 sb
= page_address(rdev
->sb_page
);
2283 /* Check if a device has become faulty or a spare become active */
2284 rdev_for_each(rdev
, mddev
) {
2285 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
2286 /* Device activated? */
2287 if (role
== 0xffff && rdev
->raid_disk
>=0 &&
2288 !test_bit(Faulty
, &rdev
->flags
))
2290 /* Device turned faulty? */
2291 if (test_bit(Faulty
, &rdev
->flags
) && (role
< 0xfffd))
2295 /* Check if any mddev parameters have changed */
2296 if ((mddev
->dev_sectors
!= le64_to_cpu(sb
->size
)) ||
2297 (mddev
->reshape_position
!= le64_to_cpu(sb
->reshape_position
)) ||
2298 (mddev
->layout
!= le64_to_cpu(sb
->layout
)) ||
2299 (mddev
->raid_disks
!= le32_to_cpu(sb
->raid_disks
)) ||
2300 (mddev
->chunk_sectors
!= le32_to_cpu(sb
->chunksize
)))
2306 void md_update_sb(struct mddev
*mddev
, int force_change
)
2308 struct md_rdev
*rdev
;
2311 int any_badblocks_changed
= 0;
2316 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2321 if (mddev_is_clustered(mddev
)) {
2322 if (test_and_clear_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
))
2324 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
))
2326 ret
= md_cluster_ops
->metadata_update_start(mddev
);
2327 /* Has someone else has updated the sb */
2328 if (!does_sb_need_changing(mddev
)) {
2330 md_cluster_ops
->metadata_update_cancel(mddev
);
2331 bit_clear_unless(&mddev
->sb_flags
, BIT(MD_SB_CHANGE_PENDING
),
2332 BIT(MD_SB_CHANGE_DEVS
) |
2333 BIT(MD_SB_CHANGE_CLEAN
));
2338 /* First make sure individual recovery_offsets are correct */
2339 rdev_for_each(rdev
, mddev
) {
2340 if (rdev
->raid_disk
>= 0 &&
2341 mddev
->delta_disks
>= 0 &&
2342 !test_bit(Journal
, &rdev
->flags
) &&
2343 !test_bit(In_sync
, &rdev
->flags
) &&
2344 mddev
->curr_resync_completed
> rdev
->recovery_offset
)
2345 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
2348 if (!mddev
->persistent
) {
2349 clear_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
2350 clear_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2351 if (!mddev
->external
) {
2352 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
2353 rdev_for_each(rdev
, mddev
) {
2354 if (rdev
->badblocks
.changed
) {
2355 rdev
->badblocks
.changed
= 0;
2356 ack_all_badblocks(&rdev
->badblocks
);
2357 md_error(mddev
, rdev
);
2359 clear_bit(Blocked
, &rdev
->flags
);
2360 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2361 wake_up(&rdev
->blocked_wait
);
2364 wake_up(&mddev
->sb_wait
);
2368 spin_lock(&mddev
->lock
);
2370 mddev
->utime
= ktime_get_real_seconds();
2372 if (test_and_clear_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
))
2374 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
))
2375 /* just a clean<-> dirty transition, possibly leave spares alone,
2376 * though if events isn't the right even/odd, we will have to do
2382 if (mddev
->degraded
)
2383 /* If the array is degraded, then skipping spares is both
2384 * dangerous and fairly pointless.
2385 * Dangerous because a device that was removed from the array
2386 * might have a event_count that still looks up-to-date,
2387 * so it can be re-added without a resync.
2388 * Pointless because if there are any spares to skip,
2389 * then a recovery will happen and soon that array won't
2390 * be degraded any more and the spare can go back to sleep then.
2394 sync_req
= mddev
->in_sync
;
2396 /* If this is just a dirty<->clean transition, and the array is clean
2397 * and 'events' is odd, we can roll back to the previous clean state */
2399 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
2400 && mddev
->can_decrease_events
2401 && mddev
->events
!= 1) {
2403 mddev
->can_decrease_events
= 0;
2405 /* otherwise we have to go forward and ... */
2407 mddev
->can_decrease_events
= nospares
;
2411 * This 64-bit counter should never wrap.
2412 * Either we are in around ~1 trillion A.C., assuming
2413 * 1 reboot per second, or we have a bug...
2415 WARN_ON(mddev
->events
== 0);
2417 rdev_for_each(rdev
, mddev
) {
2418 if (rdev
->badblocks
.changed
)
2419 any_badblocks_changed
++;
2420 if (test_bit(Faulty
, &rdev
->flags
))
2421 set_bit(FaultRecorded
, &rdev
->flags
);
2424 sync_sbs(mddev
, nospares
);
2425 spin_unlock(&mddev
->lock
);
2427 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2428 mdname(mddev
), mddev
->in_sync
);
2431 blk_add_trace_msg(mddev
->queue
, "md md_update_sb");
2433 bitmap_update_sb(mddev
->bitmap
);
2434 rdev_for_each(rdev
, mddev
) {
2435 char b
[BDEVNAME_SIZE
];
2437 if (rdev
->sb_loaded
!= 1)
2438 continue; /* no noise on spare devices */
2440 if (!test_bit(Faulty
, &rdev
->flags
)) {
2441 md_super_write(mddev
,rdev
,
2442 rdev
->sb_start
, rdev
->sb_size
,
2444 pr_debug("md: (write) %s's sb offset: %llu\n",
2445 bdevname(rdev
->bdev
, b
),
2446 (unsigned long long)rdev
->sb_start
);
2447 rdev
->sb_events
= mddev
->events
;
2448 if (rdev
->badblocks
.size
) {
2449 md_super_write(mddev
, rdev
,
2450 rdev
->badblocks
.sector
,
2451 rdev
->badblocks
.size
<< 9,
2453 rdev
->badblocks
.size
= 0;
2457 pr_debug("md: %s (skipping faulty)\n",
2458 bdevname(rdev
->bdev
, b
));
2460 if (mddev
->level
== LEVEL_MULTIPATH
)
2461 /* only need to write one superblock... */
2464 if (md_super_wait(mddev
) < 0)
2466 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
2468 if (mddev_is_clustered(mddev
) && ret
== 0)
2469 md_cluster_ops
->metadata_update_finish(mddev
);
2471 if (mddev
->in_sync
!= sync_req
||
2472 !bit_clear_unless(&mddev
->sb_flags
, BIT(MD_SB_CHANGE_PENDING
),
2473 BIT(MD_SB_CHANGE_DEVS
) | BIT(MD_SB_CHANGE_CLEAN
)))
2474 /* have to write it out again */
2476 wake_up(&mddev
->sb_wait
);
2477 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2478 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
2480 rdev_for_each(rdev
, mddev
) {
2481 if (test_and_clear_bit(FaultRecorded
, &rdev
->flags
))
2482 clear_bit(Blocked
, &rdev
->flags
);
2484 if (any_badblocks_changed
)
2485 ack_all_badblocks(&rdev
->badblocks
);
2486 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2487 wake_up(&rdev
->blocked_wait
);
2490 EXPORT_SYMBOL(md_update_sb
);
2492 static int add_bound_rdev(struct md_rdev
*rdev
)
2494 struct mddev
*mddev
= rdev
->mddev
;
2496 bool add_journal
= test_bit(Journal
, &rdev
->flags
);
2498 if (!mddev
->pers
->hot_remove_disk
|| add_journal
) {
2499 /* If there is hot_add_disk but no hot_remove_disk
2500 * then added disks for geometry changes,
2501 * and should be added immediately.
2503 super_types
[mddev
->major_version
].
2504 validate_super(mddev
, rdev
);
2506 mddev_suspend(mddev
);
2507 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
2509 mddev_resume(mddev
);
2511 md_kick_rdev_from_array(rdev
);
2515 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2517 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2518 if (mddev
->degraded
)
2519 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
2520 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2521 md_new_event(mddev
);
2522 md_wakeup_thread(mddev
->thread
);
2526 /* words written to sysfs files may, or may not, be \n terminated.
2527 * We want to accept with case. For this we use cmd_match.
2529 static int cmd_match(const char *cmd
, const char *str
)
2531 /* See if cmd, written into a sysfs file, matches
2532 * str. They must either be the same, or cmd can
2533 * have a trailing newline
2535 while (*cmd
&& *str
&& *cmd
== *str
) {
2546 struct rdev_sysfs_entry
{
2547 struct attribute attr
;
2548 ssize_t (*show
)(struct md_rdev
*, char *);
2549 ssize_t (*store
)(struct md_rdev
*, const char *, size_t);
2553 state_show(struct md_rdev
*rdev
, char *page
)
2557 unsigned long flags
= ACCESS_ONCE(rdev
->flags
);
2559 if (test_bit(Faulty
, &flags
) ||
2560 (!test_bit(ExternalBbl
, &flags
) &&
2561 rdev
->badblocks
.unacked_exist
))
2562 len
+= sprintf(page
+len
, "faulty%s", sep
);
2563 if (test_bit(In_sync
, &flags
))
2564 len
+= sprintf(page
+len
, "in_sync%s", sep
);
2565 if (test_bit(Journal
, &flags
))
2566 len
+= sprintf(page
+len
, "journal%s", sep
);
2567 if (test_bit(WriteMostly
, &flags
))
2568 len
+= sprintf(page
+len
, "write_mostly%s", sep
);
2569 if (test_bit(Blocked
, &flags
) ||
2570 (rdev
->badblocks
.unacked_exist
2571 && !test_bit(Faulty
, &flags
)))
2572 len
+= sprintf(page
+len
, "blocked%s", sep
);
2573 if (!test_bit(Faulty
, &flags
) &&
2574 !test_bit(Journal
, &flags
) &&
2575 !test_bit(In_sync
, &flags
))
2576 len
+= sprintf(page
+len
, "spare%s", sep
);
2577 if (test_bit(WriteErrorSeen
, &flags
))
2578 len
+= sprintf(page
+len
, "write_error%s", sep
);
2579 if (test_bit(WantReplacement
, &flags
))
2580 len
+= sprintf(page
+len
, "want_replacement%s", sep
);
2581 if (test_bit(Replacement
, &flags
))
2582 len
+= sprintf(page
+len
, "replacement%s", sep
);
2583 if (test_bit(ExternalBbl
, &flags
))
2584 len
+= sprintf(page
+len
, "external_bbl%s", sep
);
2585 if (test_bit(FailFast
, &flags
))
2586 len
+= sprintf(page
+len
, "failfast%s", sep
);
2591 return len
+sprintf(page
+len
, "\n");
2595 state_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2598 * faulty - simulates an error
2599 * remove - disconnects the device
2600 * writemostly - sets write_mostly
2601 * -writemostly - clears write_mostly
2602 * blocked - sets the Blocked flags
2603 * -blocked - clears the Blocked and possibly simulates an error
2604 * insync - sets Insync providing device isn't active
2605 * -insync - clear Insync for a device with a slot assigned,
2606 * so that it gets rebuilt based on bitmap
2607 * write_error - sets WriteErrorSeen
2608 * -write_error - clears WriteErrorSeen
2609 * {,-}failfast - set/clear FailFast
2612 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
2613 md_error(rdev
->mddev
, rdev
);
2614 if (test_bit(Faulty
, &rdev
->flags
))
2618 } else if (cmd_match(buf
, "remove")) {
2619 if (rdev
->mddev
->pers
) {
2620 clear_bit(Blocked
, &rdev
->flags
);
2621 remove_and_add_spares(rdev
->mddev
, rdev
);
2623 if (rdev
->raid_disk
>= 0)
2626 struct mddev
*mddev
= rdev
->mddev
;
2628 if (mddev_is_clustered(mddev
))
2629 err
= md_cluster_ops
->remove_disk(mddev
, rdev
);
2632 md_kick_rdev_from_array(rdev
);
2634 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2635 md_wakeup_thread(mddev
->thread
);
2637 md_new_event(mddev
);
2640 } else if (cmd_match(buf
, "writemostly")) {
2641 set_bit(WriteMostly
, &rdev
->flags
);
2643 } else if (cmd_match(buf
, "-writemostly")) {
2644 clear_bit(WriteMostly
, &rdev
->flags
);
2646 } else if (cmd_match(buf
, "blocked")) {
2647 set_bit(Blocked
, &rdev
->flags
);
2649 } else if (cmd_match(buf
, "-blocked")) {
2650 if (!test_bit(Faulty
, &rdev
->flags
) &&
2651 !test_bit(ExternalBbl
, &rdev
->flags
) &&
2652 rdev
->badblocks
.unacked_exist
) {
2653 /* metadata handler doesn't understand badblocks,
2654 * so we need to fail the device
2656 md_error(rdev
->mddev
, rdev
);
2658 clear_bit(Blocked
, &rdev
->flags
);
2659 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2660 wake_up(&rdev
->blocked_wait
);
2661 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2662 md_wakeup_thread(rdev
->mddev
->thread
);
2665 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
2666 set_bit(In_sync
, &rdev
->flags
);
2668 } else if (cmd_match(buf
, "failfast")) {
2669 set_bit(FailFast
, &rdev
->flags
);
2671 } else if (cmd_match(buf
, "-failfast")) {
2672 clear_bit(FailFast
, &rdev
->flags
);
2674 } else if (cmd_match(buf
, "-insync") && rdev
->raid_disk
>= 0 &&
2675 !test_bit(Journal
, &rdev
->flags
)) {
2676 if (rdev
->mddev
->pers
== NULL
) {
2677 clear_bit(In_sync
, &rdev
->flags
);
2678 rdev
->saved_raid_disk
= rdev
->raid_disk
;
2679 rdev
->raid_disk
= -1;
2682 } else if (cmd_match(buf
, "write_error")) {
2683 set_bit(WriteErrorSeen
, &rdev
->flags
);
2685 } else if (cmd_match(buf
, "-write_error")) {
2686 clear_bit(WriteErrorSeen
, &rdev
->flags
);
2688 } else if (cmd_match(buf
, "want_replacement")) {
2689 /* Any non-spare device that is not a replacement can
2690 * become want_replacement at any time, but we then need to
2691 * check if recovery is needed.
2693 if (rdev
->raid_disk
>= 0 &&
2694 !test_bit(Journal
, &rdev
->flags
) &&
2695 !test_bit(Replacement
, &rdev
->flags
))
2696 set_bit(WantReplacement
, &rdev
->flags
);
2697 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2698 md_wakeup_thread(rdev
->mddev
->thread
);
2700 } else if (cmd_match(buf
, "-want_replacement")) {
2701 /* Clearing 'want_replacement' is always allowed.
2702 * Once replacements starts it is too late though.
2705 clear_bit(WantReplacement
, &rdev
->flags
);
2706 } else if (cmd_match(buf
, "replacement")) {
2707 /* Can only set a device as a replacement when array has not
2708 * yet been started. Once running, replacement is automatic
2709 * from spares, or by assigning 'slot'.
2711 if (rdev
->mddev
->pers
)
2714 set_bit(Replacement
, &rdev
->flags
);
2717 } else if (cmd_match(buf
, "-replacement")) {
2718 /* Similarly, can only clear Replacement before start */
2719 if (rdev
->mddev
->pers
)
2722 clear_bit(Replacement
, &rdev
->flags
);
2725 } else if (cmd_match(buf
, "re-add")) {
2726 if (test_bit(Faulty
, &rdev
->flags
) && (rdev
->raid_disk
== -1)) {
2727 /* clear_bit is performed _after_ all the devices
2728 * have their local Faulty bit cleared. If any writes
2729 * happen in the meantime in the local node, they
2730 * will land in the local bitmap, which will be synced
2731 * by this node eventually
2733 if (!mddev_is_clustered(rdev
->mddev
) ||
2734 (err
= md_cluster_ops
->gather_bitmaps(rdev
)) == 0) {
2735 clear_bit(Faulty
, &rdev
->flags
);
2736 err
= add_bound_rdev(rdev
);
2740 } else if (cmd_match(buf
, "external_bbl") && (rdev
->mddev
->external
)) {
2741 set_bit(ExternalBbl
, &rdev
->flags
);
2742 rdev
->badblocks
.shift
= 0;
2744 } else if (cmd_match(buf
, "-external_bbl") && (rdev
->mddev
->external
)) {
2745 clear_bit(ExternalBbl
, &rdev
->flags
);
2749 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2750 return err
? err
: len
;
2752 static struct rdev_sysfs_entry rdev_state
=
2753 __ATTR_PREALLOC(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
2756 errors_show(struct md_rdev
*rdev
, char *page
)
2758 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
2762 errors_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2767 rv
= kstrtouint(buf
, 10, &n
);
2770 atomic_set(&rdev
->corrected_errors
, n
);
2773 static struct rdev_sysfs_entry rdev_errors
=
2774 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
2777 slot_show(struct md_rdev
*rdev
, char *page
)
2779 if (test_bit(Journal
, &rdev
->flags
))
2780 return sprintf(page
, "journal\n");
2781 else if (rdev
->raid_disk
< 0)
2782 return sprintf(page
, "none\n");
2784 return sprintf(page
, "%d\n", rdev
->raid_disk
);
2788 slot_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2793 if (test_bit(Journal
, &rdev
->flags
))
2795 if (strncmp(buf
, "none", 4)==0)
2798 err
= kstrtouint(buf
, 10, (unsigned int *)&slot
);
2802 if (rdev
->mddev
->pers
&& slot
== -1) {
2803 /* Setting 'slot' on an active array requires also
2804 * updating the 'rd%d' link, and communicating
2805 * with the personality with ->hot_*_disk.
2806 * For now we only support removing
2807 * failed/spare devices. This normally happens automatically,
2808 * but not when the metadata is externally managed.
2810 if (rdev
->raid_disk
== -1)
2812 /* personality does all needed checks */
2813 if (rdev
->mddev
->pers
->hot_remove_disk
== NULL
)
2815 clear_bit(Blocked
, &rdev
->flags
);
2816 remove_and_add_spares(rdev
->mddev
, rdev
);
2817 if (rdev
->raid_disk
>= 0)
2819 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2820 md_wakeup_thread(rdev
->mddev
->thread
);
2821 } else if (rdev
->mddev
->pers
) {
2822 /* Activating a spare .. or possibly reactivating
2823 * if we ever get bitmaps working here.
2827 if (rdev
->raid_disk
!= -1)
2830 if (test_bit(MD_RECOVERY_RUNNING
, &rdev
->mddev
->recovery
))
2833 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2836 if (slot
>= rdev
->mddev
->raid_disks
&&
2837 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2840 rdev
->raid_disk
= slot
;
2841 if (test_bit(In_sync
, &rdev
->flags
))
2842 rdev
->saved_raid_disk
= slot
;
2844 rdev
->saved_raid_disk
= -1;
2845 clear_bit(In_sync
, &rdev
->flags
);
2846 clear_bit(Bitmap_sync
, &rdev
->flags
);
2847 err
= rdev
->mddev
->pers
->
2848 hot_add_disk(rdev
->mddev
, rdev
);
2850 rdev
->raid_disk
= -1;
2853 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2854 if (sysfs_link_rdev(rdev
->mddev
, rdev
))
2855 /* failure here is OK */;
2856 /* don't wakeup anyone, leave that to userspace. */
2858 if (slot
>= rdev
->mddev
->raid_disks
&&
2859 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2861 rdev
->raid_disk
= slot
;
2862 /* assume it is working */
2863 clear_bit(Faulty
, &rdev
->flags
);
2864 clear_bit(WriteMostly
, &rdev
->flags
);
2865 set_bit(In_sync
, &rdev
->flags
);
2866 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2871 static struct rdev_sysfs_entry rdev_slot
=
2872 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
2875 offset_show(struct md_rdev
*rdev
, char *page
)
2877 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
2881 offset_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2883 unsigned long long offset
;
2884 if (kstrtoull(buf
, 10, &offset
) < 0)
2886 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
2888 if (rdev
->sectors
&& rdev
->mddev
->external
)
2889 /* Must set offset before size, so overlap checks
2892 rdev
->data_offset
= offset
;
2893 rdev
->new_data_offset
= offset
;
2897 static struct rdev_sysfs_entry rdev_offset
=
2898 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
2900 static ssize_t
new_offset_show(struct md_rdev
*rdev
, char *page
)
2902 return sprintf(page
, "%llu\n",
2903 (unsigned long long)rdev
->new_data_offset
);
2906 static ssize_t
new_offset_store(struct md_rdev
*rdev
,
2907 const char *buf
, size_t len
)
2909 unsigned long long new_offset
;
2910 struct mddev
*mddev
= rdev
->mddev
;
2912 if (kstrtoull(buf
, 10, &new_offset
) < 0)
2915 if (mddev
->sync_thread
||
2916 test_bit(MD_RECOVERY_RUNNING
,&mddev
->recovery
))
2918 if (new_offset
== rdev
->data_offset
)
2919 /* reset is always permitted */
2921 else if (new_offset
> rdev
->data_offset
) {
2922 /* must not push array size beyond rdev_sectors */
2923 if (new_offset
- rdev
->data_offset
2924 + mddev
->dev_sectors
> rdev
->sectors
)
2927 /* Metadata worries about other space details. */
2929 /* decreasing the offset is inconsistent with a backwards
2932 if (new_offset
< rdev
->data_offset
&&
2933 mddev
->reshape_backwards
)
2935 /* Increasing offset is inconsistent with forwards
2936 * reshape. reshape_direction should be set to
2937 * 'backwards' first.
2939 if (new_offset
> rdev
->data_offset
&&
2940 !mddev
->reshape_backwards
)
2943 if (mddev
->pers
&& mddev
->persistent
&&
2944 !super_types
[mddev
->major_version
]
2945 .allow_new_offset(rdev
, new_offset
))
2947 rdev
->new_data_offset
= new_offset
;
2948 if (new_offset
> rdev
->data_offset
)
2949 mddev
->reshape_backwards
= 1;
2950 else if (new_offset
< rdev
->data_offset
)
2951 mddev
->reshape_backwards
= 0;
2955 static struct rdev_sysfs_entry rdev_new_offset
=
2956 __ATTR(new_offset
, S_IRUGO
|S_IWUSR
, new_offset_show
, new_offset_store
);
2959 rdev_size_show(struct md_rdev
*rdev
, char *page
)
2961 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
2964 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
2966 /* check if two start/length pairs overlap */
2974 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
2976 unsigned long long blocks
;
2979 if (kstrtoull(buf
, 10, &blocks
) < 0)
2982 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
2983 return -EINVAL
; /* sector conversion overflow */
2986 if (new != blocks
* 2)
2987 return -EINVAL
; /* unsigned long long to sector_t overflow */
2994 rdev_size_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2996 struct mddev
*my_mddev
= rdev
->mddev
;
2997 sector_t oldsectors
= rdev
->sectors
;
3000 if (test_bit(Journal
, &rdev
->flags
))
3002 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
3004 if (rdev
->data_offset
!= rdev
->new_data_offset
)
3005 return -EINVAL
; /* too confusing */
3006 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
3007 if (my_mddev
->persistent
) {
3008 sectors
= super_types
[my_mddev
->major_version
].
3009 rdev_size_change(rdev
, sectors
);
3012 } else if (!sectors
)
3013 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
3015 if (!my_mddev
->pers
->resize
)
3016 /* Cannot change size for RAID0 or Linear etc */
3019 if (sectors
< my_mddev
->dev_sectors
)
3020 return -EINVAL
; /* component must fit device */
3022 rdev
->sectors
= sectors
;
3023 if (sectors
> oldsectors
&& my_mddev
->external
) {
3024 /* Need to check that all other rdevs with the same
3025 * ->bdev do not overlap. 'rcu' is sufficient to walk
3026 * the rdev lists safely.
3027 * This check does not provide a hard guarantee, it
3028 * just helps avoid dangerous mistakes.
3030 struct mddev
*mddev
;
3032 struct list_head
*tmp
;
3035 for_each_mddev(mddev
, tmp
) {
3036 struct md_rdev
*rdev2
;
3038 rdev_for_each(rdev2
, mddev
)
3039 if (rdev
->bdev
== rdev2
->bdev
&&
3041 overlaps(rdev
->data_offset
, rdev
->sectors
,
3054 /* Someone else could have slipped in a size
3055 * change here, but doing so is just silly.
3056 * We put oldsectors back because we *know* it is
3057 * safe, and trust userspace not to race with
3060 rdev
->sectors
= oldsectors
;
3067 static struct rdev_sysfs_entry rdev_size
=
3068 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
3070 static ssize_t
recovery_start_show(struct md_rdev
*rdev
, char *page
)
3072 unsigned long long recovery_start
= rdev
->recovery_offset
;
3074 if (test_bit(In_sync
, &rdev
->flags
) ||
3075 recovery_start
== MaxSector
)
3076 return sprintf(page
, "none\n");
3078 return sprintf(page
, "%llu\n", recovery_start
);
3081 static ssize_t
recovery_start_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3083 unsigned long long recovery_start
;
3085 if (cmd_match(buf
, "none"))
3086 recovery_start
= MaxSector
;
3087 else if (kstrtoull(buf
, 10, &recovery_start
))
3090 if (rdev
->mddev
->pers
&&
3091 rdev
->raid_disk
>= 0)
3094 rdev
->recovery_offset
= recovery_start
;
3095 if (recovery_start
== MaxSector
)
3096 set_bit(In_sync
, &rdev
->flags
);
3098 clear_bit(In_sync
, &rdev
->flags
);
3102 static struct rdev_sysfs_entry rdev_recovery_start
=
3103 __ATTR(recovery_start
, S_IRUGO
|S_IWUSR
, recovery_start_show
, recovery_start_store
);
3105 /* sysfs access to bad-blocks list.
3106 * We present two files.
3107 * 'bad-blocks' lists sector numbers and lengths of ranges that
3108 * are recorded as bad. The list is truncated to fit within
3109 * the one-page limit of sysfs.
3110 * Writing "sector length" to this file adds an acknowledged
3112 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3113 * been acknowledged. Writing to this file adds bad blocks
3114 * without acknowledging them. This is largely for testing.
3116 static ssize_t
bb_show(struct md_rdev
*rdev
, char *page
)
3118 return badblocks_show(&rdev
->badblocks
, page
, 0);
3120 static ssize_t
bb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
3122 int rv
= badblocks_store(&rdev
->badblocks
, page
, len
, 0);
3123 /* Maybe that ack was all we needed */
3124 if (test_and_clear_bit(BlockedBadBlocks
, &rdev
->flags
))
3125 wake_up(&rdev
->blocked_wait
);
3128 static struct rdev_sysfs_entry rdev_bad_blocks
=
3129 __ATTR(bad_blocks
, S_IRUGO
|S_IWUSR
, bb_show
, bb_store
);
3131 static ssize_t
ubb_show(struct md_rdev
*rdev
, char *page
)
3133 return badblocks_show(&rdev
->badblocks
, page
, 1);
3135 static ssize_t
ubb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
3137 return badblocks_store(&rdev
->badblocks
, page
, len
, 1);
3139 static struct rdev_sysfs_entry rdev_unack_bad_blocks
=
3140 __ATTR(unacknowledged_bad_blocks
, S_IRUGO
|S_IWUSR
, ubb_show
, ubb_store
);
3142 static struct attribute
*rdev_default_attrs
[] = {
3147 &rdev_new_offset
.attr
,
3149 &rdev_recovery_start
.attr
,
3150 &rdev_bad_blocks
.attr
,
3151 &rdev_unack_bad_blocks
.attr
,
3155 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
3157 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3158 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3164 return entry
->show(rdev
, page
);
3168 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
3169 const char *page
, size_t length
)
3171 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3172 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3174 struct mddev
*mddev
= rdev
->mddev
;
3178 if (!capable(CAP_SYS_ADMIN
))
3180 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
3182 if (rdev
->mddev
== NULL
)
3185 rv
= entry
->store(rdev
, page
, length
);
3186 mddev_unlock(mddev
);
3191 static void rdev_free(struct kobject
*ko
)
3193 struct md_rdev
*rdev
= container_of(ko
, struct md_rdev
, kobj
);
3196 static const struct sysfs_ops rdev_sysfs_ops
= {
3197 .show
= rdev_attr_show
,
3198 .store
= rdev_attr_store
,
3200 static struct kobj_type rdev_ktype
= {
3201 .release
= rdev_free
,
3202 .sysfs_ops
= &rdev_sysfs_ops
,
3203 .default_attrs
= rdev_default_attrs
,
3206 int md_rdev_init(struct md_rdev
*rdev
)
3209 rdev
->saved_raid_disk
= -1;
3210 rdev
->raid_disk
= -1;
3212 rdev
->data_offset
= 0;
3213 rdev
->new_data_offset
= 0;
3214 rdev
->sb_events
= 0;
3215 rdev
->last_read_error
= 0;
3216 rdev
->sb_loaded
= 0;
3217 rdev
->bb_page
= NULL
;
3218 atomic_set(&rdev
->nr_pending
, 0);
3219 atomic_set(&rdev
->read_errors
, 0);
3220 atomic_set(&rdev
->corrected_errors
, 0);
3222 INIT_LIST_HEAD(&rdev
->same_set
);
3223 init_waitqueue_head(&rdev
->blocked_wait
);
3225 /* Add space to store bad block list.
3226 * This reserves the space even on arrays where it cannot
3227 * be used - I wonder if that matters
3229 return badblocks_init(&rdev
->badblocks
, 0);
3231 EXPORT_SYMBOL_GPL(md_rdev_init
);
3233 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3235 * mark the device faulty if:
3237 * - the device is nonexistent (zero size)
3238 * - the device has no valid superblock
3240 * a faulty rdev _never_ has rdev->sb set.
3242 static struct md_rdev
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
3244 char b
[BDEVNAME_SIZE
];
3246 struct md_rdev
*rdev
;
3249 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
3251 return ERR_PTR(-ENOMEM
);
3253 err
= md_rdev_init(rdev
);
3256 err
= alloc_disk_sb(rdev
);
3260 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
3264 kobject_init(&rdev
->kobj
, &rdev_ktype
);
3266 size
= i_size_read(rdev
->bdev
->bd_inode
) >> BLOCK_SIZE_BITS
;
3268 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
3269 bdevname(rdev
->bdev
,b
));
3274 if (super_format
>= 0) {
3275 err
= super_types
[super_format
].
3276 load_super(rdev
, NULL
, super_minor
);
3277 if (err
== -EINVAL
) {
3278 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
3279 bdevname(rdev
->bdev
,b
),
3280 super_format
, super_minor
);
3284 pr_warn("md: could not read %s's sb, not importing!\n",
3285 bdevname(rdev
->bdev
,b
));
3295 md_rdev_clear(rdev
);
3297 return ERR_PTR(err
);
3301 * Check a full RAID array for plausibility
3304 static void analyze_sbs(struct mddev
*mddev
)
3307 struct md_rdev
*rdev
, *freshest
, *tmp
;
3308 char b
[BDEVNAME_SIZE
];
3311 rdev_for_each_safe(rdev
, tmp
, mddev
)
3312 switch (super_types
[mddev
->major_version
].
3313 load_super(rdev
, freshest
, mddev
->minor_version
)) {
3320 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
3321 bdevname(rdev
->bdev
,b
));
3322 md_kick_rdev_from_array(rdev
);
3325 super_types
[mddev
->major_version
].
3326 validate_super(mddev
, freshest
);
3329 rdev_for_each_safe(rdev
, tmp
, mddev
) {
3330 if (mddev
->max_disks
&&
3331 (rdev
->desc_nr
>= mddev
->max_disks
||
3332 i
> mddev
->max_disks
)) {
3333 pr_warn("md: %s: %s: only %d devices permitted\n",
3334 mdname(mddev
), bdevname(rdev
->bdev
, b
),
3336 md_kick_rdev_from_array(rdev
);
3339 if (rdev
!= freshest
) {
3340 if (super_types
[mddev
->major_version
].
3341 validate_super(mddev
, rdev
)) {
3342 pr_warn("md: kicking non-fresh %s from array!\n",
3343 bdevname(rdev
->bdev
,b
));
3344 md_kick_rdev_from_array(rdev
);
3348 if (mddev
->level
== LEVEL_MULTIPATH
) {
3349 rdev
->desc_nr
= i
++;
3350 rdev
->raid_disk
= rdev
->desc_nr
;
3351 set_bit(In_sync
, &rdev
->flags
);
3352 } else if (rdev
->raid_disk
>=
3353 (mddev
->raid_disks
- min(0, mddev
->delta_disks
)) &&
3354 !test_bit(Journal
, &rdev
->flags
)) {
3355 rdev
->raid_disk
= -1;
3356 clear_bit(In_sync
, &rdev
->flags
);
3361 /* Read a fixed-point number.
3362 * Numbers in sysfs attributes should be in "standard" units where
3363 * possible, so time should be in seconds.
3364 * However we internally use a a much smaller unit such as
3365 * milliseconds or jiffies.
3366 * This function takes a decimal number with a possible fractional
3367 * component, and produces an integer which is the result of
3368 * multiplying that number by 10^'scale'.
3369 * all without any floating-point arithmetic.
3371 int strict_strtoul_scaled(const char *cp
, unsigned long *res
, int scale
)
3373 unsigned long result
= 0;
3375 while (isdigit(*cp
) || (*cp
== '.' && decimals
< 0)) {
3378 else if (decimals
< scale
) {
3381 result
= result
* 10 + value
;
3393 while (decimals
< scale
) {
3402 safe_delay_show(struct mddev
*mddev
, char *page
)
3404 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
3405 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
3408 safe_delay_store(struct mddev
*mddev
, const char *cbuf
, size_t len
)
3412 if (mddev_is_clustered(mddev
)) {
3413 pr_warn("md: Safemode is disabled for clustered mode\n");
3417 if (strict_strtoul_scaled(cbuf
, &msec
, 3) < 0)
3420 mddev
->safemode_delay
= 0;
3422 unsigned long old_delay
= mddev
->safemode_delay
;
3423 unsigned long new_delay
= (msec
*HZ
)/1000;
3427 mddev
->safemode_delay
= new_delay
;
3428 if (new_delay
< old_delay
|| old_delay
== 0)
3429 mod_timer(&mddev
->safemode_timer
, jiffies
+1);
3433 static struct md_sysfs_entry md_safe_delay
=
3434 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
3437 level_show(struct mddev
*mddev
, char *page
)
3439 struct md_personality
*p
;
3441 spin_lock(&mddev
->lock
);
3444 ret
= sprintf(page
, "%s\n", p
->name
);
3445 else if (mddev
->clevel
[0])
3446 ret
= sprintf(page
, "%s\n", mddev
->clevel
);
3447 else if (mddev
->level
!= LEVEL_NONE
)
3448 ret
= sprintf(page
, "%d\n", mddev
->level
);
3451 spin_unlock(&mddev
->lock
);
3456 level_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3461 struct md_personality
*pers
, *oldpers
;
3463 void *priv
, *oldpriv
;
3464 struct md_rdev
*rdev
;
3466 if (slen
== 0 || slen
>= sizeof(clevel
))
3469 rv
= mddev_lock(mddev
);
3473 if (mddev
->pers
== NULL
) {
3474 strncpy(mddev
->clevel
, buf
, slen
);
3475 if (mddev
->clevel
[slen
-1] == '\n')
3477 mddev
->clevel
[slen
] = 0;
3478 mddev
->level
= LEVEL_NONE
;
3486 /* request to change the personality. Need to ensure:
3487 * - array is not engaged in resync/recovery/reshape
3488 * - old personality can be suspended
3489 * - new personality will access other array.
3493 if (mddev
->sync_thread
||
3494 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3495 mddev
->reshape_position
!= MaxSector
||
3496 mddev
->sysfs_active
)
3500 if (!mddev
->pers
->quiesce
) {
3501 pr_warn("md: %s: %s does not support online personality change\n",
3502 mdname(mddev
), mddev
->pers
->name
);
3506 /* Now find the new personality */
3507 strncpy(clevel
, buf
, slen
);
3508 if (clevel
[slen
-1] == '\n')
3511 if (kstrtol(clevel
, 10, &level
))
3514 if (request_module("md-%s", clevel
) != 0)
3515 request_module("md-level-%s", clevel
);
3516 spin_lock(&pers_lock
);
3517 pers
= find_pers(level
, clevel
);
3518 if (!pers
|| !try_module_get(pers
->owner
)) {
3519 spin_unlock(&pers_lock
);
3520 pr_warn("md: personality %s not loaded\n", clevel
);
3524 spin_unlock(&pers_lock
);
3526 if (pers
== mddev
->pers
) {
3527 /* Nothing to do! */
3528 module_put(pers
->owner
);
3532 if (!pers
->takeover
) {
3533 module_put(pers
->owner
);
3534 pr_warn("md: %s: %s does not support personality takeover\n",
3535 mdname(mddev
), clevel
);
3540 rdev_for_each(rdev
, mddev
)
3541 rdev
->new_raid_disk
= rdev
->raid_disk
;
3543 /* ->takeover must set new_* and/or delta_disks
3544 * if it succeeds, and may set them when it fails.
3546 priv
= pers
->takeover(mddev
);
3548 mddev
->new_level
= mddev
->level
;
3549 mddev
->new_layout
= mddev
->layout
;
3550 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3551 mddev
->raid_disks
-= mddev
->delta_disks
;
3552 mddev
->delta_disks
= 0;
3553 mddev
->reshape_backwards
= 0;
3554 module_put(pers
->owner
);
3555 pr_warn("md: %s: %s would not accept array\n",
3556 mdname(mddev
), clevel
);
3561 /* Looks like we have a winner */
3562 mddev_suspend(mddev
);
3563 mddev_detach(mddev
);
3565 spin_lock(&mddev
->lock
);
3566 oldpers
= mddev
->pers
;
3567 oldpriv
= mddev
->private;
3569 mddev
->private = priv
;
3570 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3571 mddev
->level
= mddev
->new_level
;
3572 mddev
->layout
= mddev
->new_layout
;
3573 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
3574 mddev
->delta_disks
= 0;
3575 mddev
->reshape_backwards
= 0;
3576 mddev
->degraded
= 0;
3577 spin_unlock(&mddev
->lock
);
3579 if (oldpers
->sync_request
== NULL
&&
3581 /* We are converting from a no-redundancy array
3582 * to a redundancy array and metadata is managed
3583 * externally so we need to be sure that writes
3584 * won't block due to a need to transition
3586 * until external management is started.
3589 mddev
->safemode_delay
= 0;
3590 mddev
->safemode
= 0;
3593 oldpers
->free(mddev
, oldpriv
);
3595 if (oldpers
->sync_request
== NULL
&&
3596 pers
->sync_request
!= NULL
) {
3597 /* need to add the md_redundancy_group */
3598 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
3599 pr_warn("md: cannot register extra attributes for %s\n",
3601 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, "sync_action");
3603 if (oldpers
->sync_request
!= NULL
&&
3604 pers
->sync_request
== NULL
) {
3605 /* need to remove the md_redundancy_group */
3606 if (mddev
->to_remove
== NULL
)
3607 mddev
->to_remove
= &md_redundancy_group
;
3610 module_put(oldpers
->owner
);
3612 rdev_for_each(rdev
, mddev
) {
3613 if (rdev
->raid_disk
< 0)
3615 if (rdev
->new_raid_disk
>= mddev
->raid_disks
)
3616 rdev
->new_raid_disk
= -1;
3617 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3619 sysfs_unlink_rdev(mddev
, rdev
);
3621 rdev_for_each(rdev
, mddev
) {
3622 if (rdev
->raid_disk
< 0)
3624 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3626 rdev
->raid_disk
= rdev
->new_raid_disk
;
3627 if (rdev
->raid_disk
< 0)
3628 clear_bit(In_sync
, &rdev
->flags
);
3630 if (sysfs_link_rdev(mddev
, rdev
))
3631 pr_warn("md: cannot register rd%d for %s after level change\n",
3632 rdev
->raid_disk
, mdname(mddev
));
3636 if (pers
->sync_request
== NULL
) {
3637 /* this is now an array without redundancy, so
3638 * it must always be in_sync
3641 del_timer_sync(&mddev
->safemode_timer
);
3643 blk_set_stacking_limits(&mddev
->queue
->limits
);
3645 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
3646 mddev_resume(mddev
);
3648 md_update_sb(mddev
, 1);
3649 sysfs_notify(&mddev
->kobj
, NULL
, "level");
3650 md_new_event(mddev
);
3653 mddev_unlock(mddev
);
3657 static struct md_sysfs_entry md_level
=
3658 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
3661 layout_show(struct mddev
*mddev
, char *page
)
3663 /* just a number, not meaningful for all levels */
3664 if (mddev
->reshape_position
!= MaxSector
&&
3665 mddev
->layout
!= mddev
->new_layout
)
3666 return sprintf(page
, "%d (%d)\n",
3667 mddev
->new_layout
, mddev
->layout
);
3668 return sprintf(page
, "%d\n", mddev
->layout
);
3672 layout_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3677 err
= kstrtouint(buf
, 10, &n
);
3680 err
= mddev_lock(mddev
);
3685 if (mddev
->pers
->check_reshape
== NULL
)
3690 mddev
->new_layout
= n
;
3691 err
= mddev
->pers
->check_reshape(mddev
);
3693 mddev
->new_layout
= mddev
->layout
;
3696 mddev
->new_layout
= n
;
3697 if (mddev
->reshape_position
== MaxSector
)
3700 mddev_unlock(mddev
);
3703 static struct md_sysfs_entry md_layout
=
3704 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
3707 raid_disks_show(struct mddev
*mddev
, char *page
)
3709 if (mddev
->raid_disks
== 0)
3711 if (mddev
->reshape_position
!= MaxSector
&&
3712 mddev
->delta_disks
!= 0)
3713 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
3714 mddev
->raid_disks
- mddev
->delta_disks
);
3715 return sprintf(page
, "%d\n", mddev
->raid_disks
);
3718 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
);
3721 raid_disks_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3726 err
= kstrtouint(buf
, 10, &n
);
3730 err
= mddev_lock(mddev
);
3734 err
= update_raid_disks(mddev
, n
);
3735 else if (mddev
->reshape_position
!= MaxSector
) {
3736 struct md_rdev
*rdev
;
3737 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
3740 rdev_for_each(rdev
, mddev
) {
3742 rdev
->data_offset
< rdev
->new_data_offset
)
3745 rdev
->data_offset
> rdev
->new_data_offset
)
3749 mddev
->delta_disks
= n
- olddisks
;
3750 mddev
->raid_disks
= n
;
3751 mddev
->reshape_backwards
= (mddev
->delta_disks
< 0);
3753 mddev
->raid_disks
= n
;
3755 mddev_unlock(mddev
);
3756 return err
? err
: len
;
3758 static struct md_sysfs_entry md_raid_disks
=
3759 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
3762 chunk_size_show(struct mddev
*mddev
, char *page
)
3764 if (mddev
->reshape_position
!= MaxSector
&&
3765 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
3766 return sprintf(page
, "%d (%d)\n",
3767 mddev
->new_chunk_sectors
<< 9,
3768 mddev
->chunk_sectors
<< 9);
3769 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
3773 chunk_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3778 err
= kstrtoul(buf
, 10, &n
);
3782 err
= mddev_lock(mddev
);
3786 if (mddev
->pers
->check_reshape
== NULL
)
3791 mddev
->new_chunk_sectors
= n
>> 9;
3792 err
= mddev
->pers
->check_reshape(mddev
);
3794 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3797 mddev
->new_chunk_sectors
= n
>> 9;
3798 if (mddev
->reshape_position
== MaxSector
)
3799 mddev
->chunk_sectors
= n
>> 9;
3801 mddev_unlock(mddev
);
3804 static struct md_sysfs_entry md_chunk_size
=
3805 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
3808 resync_start_show(struct mddev
*mddev
, char *page
)
3810 if (mddev
->recovery_cp
== MaxSector
)
3811 return sprintf(page
, "none\n");
3812 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
3816 resync_start_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3818 unsigned long long n
;
3821 if (cmd_match(buf
, "none"))
3824 err
= kstrtoull(buf
, 10, &n
);
3827 if (n
!= (sector_t
)n
)
3831 err
= mddev_lock(mddev
);
3834 if (mddev
->pers
&& !test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
3838 mddev
->recovery_cp
= n
;
3840 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
3842 mddev_unlock(mddev
);
3845 static struct md_sysfs_entry md_resync_start
=
3846 __ATTR_PREALLOC(resync_start
, S_IRUGO
|S_IWUSR
,
3847 resync_start_show
, resync_start_store
);
3850 * The array state can be:
3853 * No devices, no size, no level
3854 * Equivalent to STOP_ARRAY ioctl
3856 * May have some settings, but array is not active
3857 * all IO results in error
3858 * When written, doesn't tear down array, but just stops it
3859 * suspended (not supported yet)
3860 * All IO requests will block. The array can be reconfigured.
3861 * Writing this, if accepted, will block until array is quiescent
3863 * no resync can happen. no superblocks get written.
3864 * write requests fail
3866 * like readonly, but behaves like 'clean' on a write request.
3868 * clean - no pending writes, but otherwise active.
3869 * When written to inactive array, starts without resync
3870 * If a write request arrives then
3871 * if metadata is known, mark 'dirty' and switch to 'active'.
3872 * if not known, block and switch to write-pending
3873 * If written to an active array that has pending writes, then fails.
3875 * fully active: IO and resync can be happening.
3876 * When written to inactive array, starts with resync
3879 * clean, but writes are blocked waiting for 'active' to be written.
3882 * like active, but no writes have been seen for a while (100msec).
3885 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
3886 write_pending
, active_idle
, bad_word
};
3887 static char *array_states
[] = {
3888 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3889 "write-pending", "active-idle", NULL
};
3891 static int match_word(const char *word
, char **list
)
3894 for (n
=0; list
[n
]; n
++)
3895 if (cmd_match(word
, list
[n
]))
3901 array_state_show(struct mddev
*mddev
, char *page
)
3903 enum array_state st
= inactive
;
3914 if (test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
))
3916 else if (mddev
->in_sync
)
3918 else if (mddev
->safemode
)
3924 if (list_empty(&mddev
->disks
) &&
3925 mddev
->raid_disks
== 0 &&
3926 mddev
->dev_sectors
== 0)
3931 return sprintf(page
, "%s\n", array_states
[st
]);
3934 static int do_md_stop(struct mddev
*mddev
, int ro
, struct block_device
*bdev
);
3935 static int md_set_readonly(struct mddev
*mddev
, struct block_device
*bdev
);
3936 static int do_md_run(struct mddev
*mddev
);
3937 static int restart_array(struct mddev
*mddev
);
3940 array_state_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3943 enum array_state st
= match_word(buf
, array_states
);
3945 if (mddev
->pers
&& (st
== active
|| st
== clean
) && mddev
->ro
!= 1) {
3946 /* don't take reconfig_mutex when toggling between
3949 spin_lock(&mddev
->lock
);
3951 restart_array(mddev
);
3952 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
3953 md_wakeup_thread(mddev
->thread
);
3954 wake_up(&mddev
->sb_wait
);
3956 } else /* st == clean */ {
3957 restart_array(mddev
);
3958 if (atomic_read(&mddev
->writes_pending
) == 0) {
3959 if (mddev
->in_sync
== 0) {
3961 if (mddev
->safemode
== 1)
3962 mddev
->safemode
= 0;
3963 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
3970 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
3971 spin_unlock(&mddev
->lock
);
3974 err
= mddev_lock(mddev
);
3982 /* stopping an active array */
3983 err
= do_md_stop(mddev
, 0, NULL
);
3986 /* stopping an active array */
3988 err
= do_md_stop(mddev
, 2, NULL
);
3990 err
= 0; /* already inactive */
3993 break; /* not supported yet */
3996 err
= md_set_readonly(mddev
, NULL
);
3999 set_disk_ro(mddev
->gendisk
, 1);
4000 err
= do_md_run(mddev
);
4006 err
= md_set_readonly(mddev
, NULL
);
4007 else if (mddev
->ro
== 1)
4008 err
= restart_array(mddev
);
4011 set_disk_ro(mddev
->gendisk
, 0);
4015 err
= do_md_run(mddev
);
4020 err
= restart_array(mddev
);
4023 spin_lock(&mddev
->lock
);
4024 if (atomic_read(&mddev
->writes_pending
) == 0) {
4025 if (mddev
->in_sync
== 0) {
4027 if (mddev
->safemode
== 1)
4028 mddev
->safemode
= 0;
4029 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
4034 spin_unlock(&mddev
->lock
);
4040 err
= restart_array(mddev
);
4043 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
4044 wake_up(&mddev
->sb_wait
);
4048 set_disk_ro(mddev
->gendisk
, 0);
4049 err
= do_md_run(mddev
);
4054 /* these cannot be set */
4059 if (mddev
->hold_active
== UNTIL_IOCTL
)
4060 mddev
->hold_active
= 0;
4061 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4063 mddev_unlock(mddev
);
4066 static struct md_sysfs_entry md_array_state
=
4067 __ATTR_PREALLOC(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
4070 max_corrected_read_errors_show(struct mddev
*mddev
, char *page
) {
4071 return sprintf(page
, "%d\n",
4072 atomic_read(&mddev
->max_corr_read_errors
));
4076 max_corrected_read_errors_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4081 rv
= kstrtouint(buf
, 10, &n
);
4084 atomic_set(&mddev
->max_corr_read_errors
, n
);
4088 static struct md_sysfs_entry max_corr_read_errors
=
4089 __ATTR(max_read_errors
, S_IRUGO
|S_IWUSR
, max_corrected_read_errors_show
,
4090 max_corrected_read_errors_store
);
4093 null_show(struct mddev
*mddev
, char *page
)
4099 new_dev_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4101 /* buf must be %d:%d\n? giving major and minor numbers */
4102 /* The new device is added to the array.
4103 * If the array has a persistent superblock, we read the
4104 * superblock to initialise info and check validity.
4105 * Otherwise, only checking done is that in bind_rdev_to_array,
4106 * which mainly checks size.
4109 int major
= simple_strtoul(buf
, &e
, 10);
4112 struct md_rdev
*rdev
;
4115 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
4117 minor
= simple_strtoul(e
+1, &e
, 10);
4118 if (*e
&& *e
!= '\n')
4120 dev
= MKDEV(major
, minor
);
4121 if (major
!= MAJOR(dev
) ||
4122 minor
!= MINOR(dev
))
4125 flush_workqueue(md_misc_wq
);
4127 err
= mddev_lock(mddev
);
4130 if (mddev
->persistent
) {
4131 rdev
= md_import_device(dev
, mddev
->major_version
,
4132 mddev
->minor_version
);
4133 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
4134 struct md_rdev
*rdev0
4135 = list_entry(mddev
->disks
.next
,
4136 struct md_rdev
, same_set
);
4137 err
= super_types
[mddev
->major_version
]
4138 .load_super(rdev
, rdev0
, mddev
->minor_version
);
4142 } else if (mddev
->external
)
4143 rdev
= md_import_device(dev
, -2, -1);
4145 rdev
= md_import_device(dev
, -1, -1);
4148 mddev_unlock(mddev
);
4149 return PTR_ERR(rdev
);
4151 err
= bind_rdev_to_array(rdev
, mddev
);
4155 mddev_unlock(mddev
);
4156 return err
? err
: len
;
4159 static struct md_sysfs_entry md_new_device
=
4160 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
4163 bitmap_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4166 unsigned long chunk
, end_chunk
;
4169 err
= mddev_lock(mddev
);
4174 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4176 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
4177 if (buf
== end
) break;
4178 if (*end
== '-') { /* range */
4180 end_chunk
= simple_strtoul(buf
, &end
, 0);
4181 if (buf
== end
) break;
4183 if (*end
&& !isspace(*end
)) break;
4184 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
4185 buf
= skip_spaces(end
);
4187 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
4189 mddev_unlock(mddev
);
4193 static struct md_sysfs_entry md_bitmap
=
4194 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
4197 size_show(struct mddev
*mddev
, char *page
)
4199 return sprintf(page
, "%llu\n",
4200 (unsigned long long)mddev
->dev_sectors
/ 2);
4203 static int update_size(struct mddev
*mddev
, sector_t num_sectors
);
4206 size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4208 /* If array is inactive, we can reduce the component size, but
4209 * not increase it (except from 0).
4210 * If array is active, we can try an on-line resize
4213 int err
= strict_blocks_to_sectors(buf
, §ors
);
4217 err
= mddev_lock(mddev
);
4221 err
= update_size(mddev
, sectors
);
4223 md_update_sb(mddev
, 1);
4225 if (mddev
->dev_sectors
== 0 ||
4226 mddev
->dev_sectors
> sectors
)
4227 mddev
->dev_sectors
= sectors
;
4231 mddev_unlock(mddev
);
4232 return err
? err
: len
;
4235 static struct md_sysfs_entry md_size
=
4236 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
4238 /* Metadata version.
4240 * 'none' for arrays with no metadata (good luck...)
4241 * 'external' for arrays with externally managed metadata,
4242 * or N.M for internally known formats
4245 metadata_show(struct mddev
*mddev
, char *page
)
4247 if (mddev
->persistent
)
4248 return sprintf(page
, "%d.%d\n",
4249 mddev
->major_version
, mddev
->minor_version
);
4250 else if (mddev
->external
)
4251 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
4253 return sprintf(page
, "none\n");
4257 metadata_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4262 /* Changing the details of 'external' metadata is
4263 * always permitted. Otherwise there must be
4264 * no devices attached to the array.
4267 err
= mddev_lock(mddev
);
4271 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
4273 else if (!list_empty(&mddev
->disks
))
4277 if (cmd_match(buf
, "none")) {
4278 mddev
->persistent
= 0;
4279 mddev
->external
= 0;
4280 mddev
->major_version
= 0;
4281 mddev
->minor_version
= 90;
4284 if (strncmp(buf
, "external:", 9) == 0) {
4285 size_t namelen
= len
-9;
4286 if (namelen
>= sizeof(mddev
->metadata_type
))
4287 namelen
= sizeof(mddev
->metadata_type
)-1;
4288 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
4289 mddev
->metadata_type
[namelen
] = 0;
4290 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
4291 mddev
->metadata_type
[--namelen
] = 0;
4292 mddev
->persistent
= 0;
4293 mddev
->external
= 1;
4294 mddev
->major_version
= 0;
4295 mddev
->minor_version
= 90;
4298 major
= simple_strtoul(buf
, &e
, 10);
4300 if (e
==buf
|| *e
!= '.')
4303 minor
= simple_strtoul(buf
, &e
, 10);
4304 if (e
==buf
|| (*e
&& *e
!= '\n') )
4307 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
4309 mddev
->major_version
= major
;
4310 mddev
->minor_version
= minor
;
4311 mddev
->persistent
= 1;
4312 mddev
->external
= 0;
4315 mddev_unlock(mddev
);
4319 static struct md_sysfs_entry md_metadata
=
4320 __ATTR_PREALLOC(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
4323 action_show(struct mddev
*mddev
, char *page
)
4325 char *type
= "idle";
4326 unsigned long recovery
= mddev
->recovery
;
4327 if (test_bit(MD_RECOVERY_FROZEN
, &recovery
))
4329 else if (test_bit(MD_RECOVERY_RUNNING
, &recovery
) ||
4330 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &recovery
))) {
4331 if (test_bit(MD_RECOVERY_RESHAPE
, &recovery
))
4333 else if (test_bit(MD_RECOVERY_SYNC
, &recovery
)) {
4334 if (!test_bit(MD_RECOVERY_REQUESTED
, &recovery
))
4336 else if (test_bit(MD_RECOVERY_CHECK
, &recovery
))
4340 } else if (test_bit(MD_RECOVERY_RECOVER
, &recovery
))
4342 else if (mddev
->reshape_position
!= MaxSector
)
4345 return sprintf(page
, "%s\n", type
);
4349 action_store(struct mddev
*mddev
, const char *page
, size_t len
)
4351 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
4355 if (cmd_match(page
, "idle") || cmd_match(page
, "frozen")) {
4356 if (cmd_match(page
, "frozen"))
4357 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4359 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4360 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
4361 mddev_lock(mddev
) == 0) {
4362 flush_workqueue(md_misc_wq
);
4363 if (mddev
->sync_thread
) {
4364 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4365 md_reap_sync_thread(mddev
);
4367 mddev_unlock(mddev
);
4369 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4371 else if (cmd_match(page
, "resync"))
4372 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4373 else if (cmd_match(page
, "recover")) {
4374 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4375 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
4376 } else if (cmd_match(page
, "reshape")) {
4378 if (mddev
->pers
->start_reshape
== NULL
)
4380 err
= mddev_lock(mddev
);
4382 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4385 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4386 err
= mddev
->pers
->start_reshape(mddev
);
4388 mddev_unlock(mddev
);
4392 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4394 if (cmd_match(page
, "check"))
4395 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
4396 else if (!cmd_match(page
, "repair"))
4398 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4399 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
4400 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
4402 if (mddev
->ro
== 2) {
4403 /* A write to sync_action is enough to justify
4404 * canceling read-auto mode
4407 md_wakeup_thread(mddev
->sync_thread
);
4409 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4410 md_wakeup_thread(mddev
->thread
);
4411 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4415 static struct md_sysfs_entry md_scan_mode
=
4416 __ATTR_PREALLOC(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
4419 last_sync_action_show(struct mddev
*mddev
, char *page
)
4421 return sprintf(page
, "%s\n", mddev
->last_sync_action
);
4424 static struct md_sysfs_entry md_last_scan_mode
= __ATTR_RO(last_sync_action
);
4427 mismatch_cnt_show(struct mddev
*mddev
, char *page
)
4429 return sprintf(page
, "%llu\n",
4430 (unsigned long long)
4431 atomic64_read(&mddev
->resync_mismatches
));
4434 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
4437 sync_min_show(struct mddev
*mddev
, char *page
)
4439 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
4440 mddev
->sync_speed_min
? "local": "system");
4444 sync_min_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4449 if (strncmp(buf
, "system", 6)==0) {
4452 rv
= kstrtouint(buf
, 10, &min
);
4458 mddev
->sync_speed_min
= min
;
4462 static struct md_sysfs_entry md_sync_min
=
4463 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
4466 sync_max_show(struct mddev
*mddev
, char *page
)
4468 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
4469 mddev
->sync_speed_max
? "local": "system");
4473 sync_max_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4478 if (strncmp(buf
, "system", 6)==0) {
4481 rv
= kstrtouint(buf
, 10, &max
);
4487 mddev
->sync_speed_max
= max
;
4491 static struct md_sysfs_entry md_sync_max
=
4492 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
4495 degraded_show(struct mddev
*mddev
, char *page
)
4497 return sprintf(page
, "%d\n", mddev
->degraded
);
4499 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
4502 sync_force_parallel_show(struct mddev
*mddev
, char *page
)
4504 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
4508 sync_force_parallel_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4512 if (kstrtol(buf
, 10, &n
))
4515 if (n
!= 0 && n
!= 1)
4518 mddev
->parallel_resync
= n
;
4520 if (mddev
->sync_thread
)
4521 wake_up(&resync_wait
);
4526 /* force parallel resync, even with shared block devices */
4527 static struct md_sysfs_entry md_sync_force_parallel
=
4528 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
4529 sync_force_parallel_show
, sync_force_parallel_store
);
4532 sync_speed_show(struct mddev
*mddev
, char *page
)
4534 unsigned long resync
, dt
, db
;
4535 if (mddev
->curr_resync
== 0)
4536 return sprintf(page
, "none\n");
4537 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
4538 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
4540 db
= resync
- mddev
->resync_mark_cnt
;
4541 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
4544 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
4547 sync_completed_show(struct mddev
*mddev
, char *page
)
4549 unsigned long long max_sectors
, resync
;
4551 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4552 return sprintf(page
, "none\n");
4554 if (mddev
->curr_resync
== 1 ||
4555 mddev
->curr_resync
== 2)
4556 return sprintf(page
, "delayed\n");
4558 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
4559 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
4560 max_sectors
= mddev
->resync_max_sectors
;
4562 max_sectors
= mddev
->dev_sectors
;
4564 resync
= mddev
->curr_resync_completed
;
4565 return sprintf(page
, "%llu / %llu\n", resync
, max_sectors
);
4568 static struct md_sysfs_entry md_sync_completed
=
4569 __ATTR_PREALLOC(sync_completed
, S_IRUGO
, sync_completed_show
, NULL
);
4572 min_sync_show(struct mddev
*mddev
, char *page
)
4574 return sprintf(page
, "%llu\n",
4575 (unsigned long long)mddev
->resync_min
);
4578 min_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4580 unsigned long long min
;
4583 if (kstrtoull(buf
, 10, &min
))
4586 spin_lock(&mddev
->lock
);
4588 if (min
> mddev
->resync_max
)
4592 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4595 /* Round down to multiple of 4K for safety */
4596 mddev
->resync_min
= round_down(min
, 8);
4600 spin_unlock(&mddev
->lock
);
4604 static struct md_sysfs_entry md_min_sync
=
4605 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
4608 max_sync_show(struct mddev
*mddev
, char *page
)
4610 if (mddev
->resync_max
== MaxSector
)
4611 return sprintf(page
, "max\n");
4613 return sprintf(page
, "%llu\n",
4614 (unsigned long long)mddev
->resync_max
);
4617 max_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4620 spin_lock(&mddev
->lock
);
4621 if (strncmp(buf
, "max", 3) == 0)
4622 mddev
->resync_max
= MaxSector
;
4624 unsigned long long max
;
4628 if (kstrtoull(buf
, 10, &max
))
4630 if (max
< mddev
->resync_min
)
4634 if (max
< mddev
->resync_max
&&
4636 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4639 /* Must be a multiple of chunk_size */
4640 chunk
= mddev
->chunk_sectors
;
4642 sector_t temp
= max
;
4645 if (sector_div(temp
, chunk
))
4648 mddev
->resync_max
= max
;
4650 wake_up(&mddev
->recovery_wait
);
4653 spin_unlock(&mddev
->lock
);
4657 static struct md_sysfs_entry md_max_sync
=
4658 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
4661 suspend_lo_show(struct mddev
*mddev
, char *page
)
4663 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
4667 suspend_lo_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4669 unsigned long long old
, new;
4672 err
= kstrtoull(buf
, 10, &new);
4675 if (new != (sector_t
)new)
4678 err
= mddev_lock(mddev
);
4682 if (mddev
->pers
== NULL
||
4683 mddev
->pers
->quiesce
== NULL
)
4685 old
= mddev
->suspend_lo
;
4686 mddev
->suspend_lo
= new;
4688 /* Shrinking suspended region */
4689 mddev
->pers
->quiesce(mddev
, 2);
4691 /* Expanding suspended region - need to wait */
4692 mddev
->pers
->quiesce(mddev
, 1);
4693 mddev
->pers
->quiesce(mddev
, 0);
4697 mddev_unlock(mddev
);
4700 static struct md_sysfs_entry md_suspend_lo
=
4701 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
4704 suspend_hi_show(struct mddev
*mddev
, char *page
)
4706 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
4710 suspend_hi_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4712 unsigned long long old
, new;
4715 err
= kstrtoull(buf
, 10, &new);
4718 if (new != (sector_t
)new)
4721 err
= mddev_lock(mddev
);
4725 if (mddev
->pers
== NULL
||
4726 mddev
->pers
->quiesce
== NULL
)
4728 old
= mddev
->suspend_hi
;
4729 mddev
->suspend_hi
= new;
4731 /* Shrinking suspended region */
4732 mddev
->pers
->quiesce(mddev
, 2);
4734 /* Expanding suspended region - need to wait */
4735 mddev
->pers
->quiesce(mddev
, 1);
4736 mddev
->pers
->quiesce(mddev
, 0);
4740 mddev_unlock(mddev
);
4743 static struct md_sysfs_entry md_suspend_hi
=
4744 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
4747 reshape_position_show(struct mddev
*mddev
, char *page
)
4749 if (mddev
->reshape_position
!= MaxSector
)
4750 return sprintf(page
, "%llu\n",
4751 (unsigned long long)mddev
->reshape_position
);
4752 strcpy(page
, "none\n");
4757 reshape_position_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4759 struct md_rdev
*rdev
;
4760 unsigned long long new;
4763 err
= kstrtoull(buf
, 10, &new);
4766 if (new != (sector_t
)new)
4768 err
= mddev_lock(mddev
);
4774 mddev
->reshape_position
= new;
4775 mddev
->delta_disks
= 0;
4776 mddev
->reshape_backwards
= 0;
4777 mddev
->new_level
= mddev
->level
;
4778 mddev
->new_layout
= mddev
->layout
;
4779 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4780 rdev_for_each(rdev
, mddev
)
4781 rdev
->new_data_offset
= rdev
->data_offset
;
4784 mddev_unlock(mddev
);
4788 static struct md_sysfs_entry md_reshape_position
=
4789 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
4790 reshape_position_store
);
4793 reshape_direction_show(struct mddev
*mddev
, char *page
)
4795 return sprintf(page
, "%s\n",
4796 mddev
->reshape_backwards
? "backwards" : "forwards");
4800 reshape_direction_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4805 if (cmd_match(buf
, "forwards"))
4807 else if (cmd_match(buf
, "backwards"))
4811 if (mddev
->reshape_backwards
== backwards
)
4814 err
= mddev_lock(mddev
);
4817 /* check if we are allowed to change */
4818 if (mddev
->delta_disks
)
4820 else if (mddev
->persistent
&&
4821 mddev
->major_version
== 0)
4824 mddev
->reshape_backwards
= backwards
;
4825 mddev_unlock(mddev
);
4829 static struct md_sysfs_entry md_reshape_direction
=
4830 __ATTR(reshape_direction
, S_IRUGO
|S_IWUSR
, reshape_direction_show
,
4831 reshape_direction_store
);
4834 array_size_show(struct mddev
*mddev
, char *page
)
4836 if (mddev
->external_size
)
4837 return sprintf(page
, "%llu\n",
4838 (unsigned long long)mddev
->array_sectors
/2);
4840 return sprintf(page
, "default\n");
4844 array_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4849 err
= mddev_lock(mddev
);
4853 /* cluster raid doesn't support change array_sectors */
4854 if (mddev_is_clustered(mddev
))
4857 if (strncmp(buf
, "default", 7) == 0) {
4859 sectors
= mddev
->pers
->size(mddev
, 0, 0);
4861 sectors
= mddev
->array_sectors
;
4863 mddev
->external_size
= 0;
4865 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
4867 else if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
4870 mddev
->external_size
= 1;
4874 mddev
->array_sectors
= sectors
;
4876 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4877 revalidate_disk(mddev
->gendisk
);
4880 mddev_unlock(mddev
);
4884 static struct md_sysfs_entry md_array_size
=
4885 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
4888 static struct attribute
*md_default_attrs
[] = {
4891 &md_raid_disks
.attr
,
4892 &md_chunk_size
.attr
,
4894 &md_resync_start
.attr
,
4896 &md_new_device
.attr
,
4897 &md_safe_delay
.attr
,
4898 &md_array_state
.attr
,
4899 &md_reshape_position
.attr
,
4900 &md_reshape_direction
.attr
,
4901 &md_array_size
.attr
,
4902 &max_corr_read_errors
.attr
,
4906 static struct attribute
*md_redundancy_attrs
[] = {
4908 &md_last_scan_mode
.attr
,
4909 &md_mismatches
.attr
,
4912 &md_sync_speed
.attr
,
4913 &md_sync_force_parallel
.attr
,
4914 &md_sync_completed
.attr
,
4917 &md_suspend_lo
.attr
,
4918 &md_suspend_hi
.attr
,
4923 static struct attribute_group md_redundancy_group
= {
4925 .attrs
= md_redundancy_attrs
,
4929 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
4931 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4932 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
4937 spin_lock(&all_mddevs_lock
);
4938 if (list_empty(&mddev
->all_mddevs
)) {
4939 spin_unlock(&all_mddevs_lock
);
4943 spin_unlock(&all_mddevs_lock
);
4945 rv
= entry
->show(mddev
, page
);
4951 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
4952 const char *page
, size_t length
)
4954 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4955 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
4960 if (!capable(CAP_SYS_ADMIN
))
4962 spin_lock(&all_mddevs_lock
);
4963 if (list_empty(&mddev
->all_mddevs
)) {
4964 spin_unlock(&all_mddevs_lock
);
4968 spin_unlock(&all_mddevs_lock
);
4969 rv
= entry
->store(mddev
, page
, length
);
4974 static void md_free(struct kobject
*ko
)
4976 struct mddev
*mddev
= container_of(ko
, struct mddev
, kobj
);
4978 if (mddev
->sysfs_state
)
4979 sysfs_put(mddev
->sysfs_state
);
4982 blk_cleanup_queue(mddev
->queue
);
4983 if (mddev
->gendisk
) {
4984 del_gendisk(mddev
->gendisk
);
4985 put_disk(mddev
->gendisk
);
4991 static const struct sysfs_ops md_sysfs_ops
= {
4992 .show
= md_attr_show
,
4993 .store
= md_attr_store
,
4995 static struct kobj_type md_ktype
= {
4997 .sysfs_ops
= &md_sysfs_ops
,
4998 .default_attrs
= md_default_attrs
,
5003 static void mddev_delayed_delete(struct work_struct
*ws
)
5005 struct mddev
*mddev
= container_of(ws
, struct mddev
, del_work
);
5007 sysfs_remove_group(&mddev
->kobj
, &md_bitmap_group
);
5008 kobject_del(&mddev
->kobj
);
5009 kobject_put(&mddev
->kobj
);
5012 static int md_alloc(dev_t dev
, char *name
)
5014 static DEFINE_MUTEX(disks_mutex
);
5015 struct mddev
*mddev
= mddev_find(dev
);
5016 struct gendisk
*disk
;
5025 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
5026 shift
= partitioned
? MdpMinorShift
: 0;
5027 unit
= MINOR(mddev
->unit
) >> shift
;
5029 /* wait for any previous instance of this device to be
5030 * completely removed (mddev_delayed_delete).
5032 flush_workqueue(md_misc_wq
);
5034 mutex_lock(&disks_mutex
);
5040 /* Need to ensure that 'name' is not a duplicate.
5042 struct mddev
*mddev2
;
5043 spin_lock(&all_mddevs_lock
);
5045 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
5046 if (mddev2
->gendisk
&&
5047 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
5048 spin_unlock(&all_mddevs_lock
);
5051 spin_unlock(&all_mddevs_lock
);
5055 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
5058 mddev
->queue
->queuedata
= mddev
;
5060 blk_queue_make_request(mddev
->queue
, md_make_request
);
5061 blk_set_stacking_limits(&mddev
->queue
->limits
);
5063 disk
= alloc_disk(1 << shift
);
5065 blk_cleanup_queue(mddev
->queue
);
5066 mddev
->queue
= NULL
;
5069 disk
->major
= MAJOR(mddev
->unit
);
5070 disk
->first_minor
= unit
<< shift
;
5072 strcpy(disk
->disk_name
, name
);
5073 else if (partitioned
)
5074 sprintf(disk
->disk_name
, "md_d%d", unit
);
5076 sprintf(disk
->disk_name
, "md%d", unit
);
5077 disk
->fops
= &md_fops
;
5078 disk
->private_data
= mddev
;
5079 disk
->queue
= mddev
->queue
;
5080 blk_queue_write_cache(mddev
->queue
, true, true);
5081 /* Allow extended partitions. This makes the
5082 * 'mdp' device redundant, but we can't really
5085 disk
->flags
|= GENHD_FL_EXT_DEVT
;
5086 mddev
->gendisk
= disk
;
5087 /* As soon as we call add_disk(), another thread could get
5088 * through to md_open, so make sure it doesn't get too far
5090 mutex_lock(&mddev
->open_mutex
);
5093 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
,
5094 &disk_to_dev(disk
)->kobj
, "%s", "md");
5096 /* This isn't possible, but as kobject_init_and_add is marked
5097 * __must_check, we must do something with the result
5099 pr_debug("md: cannot register %s/md - name in use\n",
5103 if (mddev
->kobj
.sd
&&
5104 sysfs_create_group(&mddev
->kobj
, &md_bitmap_group
))
5105 pr_debug("pointless warning\n");
5106 mutex_unlock(&mddev
->open_mutex
);
5108 mutex_unlock(&disks_mutex
);
5109 if (!error
&& mddev
->kobj
.sd
) {
5110 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
5111 mddev
->sysfs_state
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "array_state");
5117 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
5119 md_alloc(dev
, NULL
);
5123 static int add_named_array(const char *val
, struct kernel_param
*kp
)
5125 /* val must be "md_*" where * is not all digits.
5126 * We allocate an array with a large free minor number, and
5127 * set the name to val. val must not already be an active name.
5129 int len
= strlen(val
);
5130 char buf
[DISK_NAME_LEN
];
5132 while (len
&& val
[len
-1] == '\n')
5134 if (len
>= DISK_NAME_LEN
)
5136 strlcpy(buf
, val
, len
+1);
5137 if (strncmp(buf
, "md_", 3) != 0)
5139 return md_alloc(0, buf
);
5142 static void md_safemode_timeout(unsigned long data
)
5144 struct mddev
*mddev
= (struct mddev
*) data
;
5146 if (!atomic_read(&mddev
->writes_pending
)) {
5147 mddev
->safemode
= 1;
5148 if (mddev
->external
)
5149 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5151 md_wakeup_thread(mddev
->thread
);
5154 static int start_dirty_degraded
;
5156 int md_run(struct mddev
*mddev
)
5159 struct md_rdev
*rdev
;
5160 struct md_personality
*pers
;
5162 if (list_empty(&mddev
->disks
))
5163 /* cannot run an array with no devices.. */
5168 /* Cannot run until previous stop completes properly */
5169 if (mddev
->sysfs_active
)
5173 * Analyze all RAID superblock(s)
5175 if (!mddev
->raid_disks
) {
5176 if (!mddev
->persistent
)
5181 if (mddev
->level
!= LEVEL_NONE
)
5182 request_module("md-level-%d", mddev
->level
);
5183 else if (mddev
->clevel
[0])
5184 request_module("md-%s", mddev
->clevel
);
5187 * Drop all container device buffers, from now on
5188 * the only valid external interface is through the md
5191 rdev_for_each(rdev
, mddev
) {
5192 if (test_bit(Faulty
, &rdev
->flags
))
5194 sync_blockdev(rdev
->bdev
);
5195 invalidate_bdev(rdev
->bdev
);
5197 /* perform some consistency tests on the device.
5198 * We don't want the data to overlap the metadata,
5199 * Internal Bitmap issues have been handled elsewhere.
5201 if (rdev
->meta_bdev
) {
5202 /* Nothing to check */;
5203 } else if (rdev
->data_offset
< rdev
->sb_start
) {
5204 if (mddev
->dev_sectors
&&
5205 rdev
->data_offset
+ mddev
->dev_sectors
5207 pr_warn("md: %s: data overlaps metadata\n",
5212 if (rdev
->sb_start
+ rdev
->sb_size
/512
5213 > rdev
->data_offset
) {
5214 pr_warn("md: %s: metadata overlaps data\n",
5219 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
5222 if (mddev
->bio_set
== NULL
) {
5223 mddev
->bio_set
= bioset_create(BIO_POOL_SIZE
, 0);
5224 if (!mddev
->bio_set
)
5228 spin_lock(&pers_lock
);
5229 pers
= find_pers(mddev
->level
, mddev
->clevel
);
5230 if (!pers
|| !try_module_get(pers
->owner
)) {
5231 spin_unlock(&pers_lock
);
5232 if (mddev
->level
!= LEVEL_NONE
)
5233 pr_warn("md: personality for level %d is not loaded!\n",
5236 pr_warn("md: personality for level %s is not loaded!\n",
5240 spin_unlock(&pers_lock
);
5241 if (mddev
->level
!= pers
->level
) {
5242 mddev
->level
= pers
->level
;
5243 mddev
->new_level
= pers
->level
;
5245 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
5247 if (mddev
->reshape_position
!= MaxSector
&&
5248 pers
->start_reshape
== NULL
) {
5249 /* This personality cannot handle reshaping... */
5250 module_put(pers
->owner
);
5254 if (pers
->sync_request
) {
5255 /* Warn if this is a potentially silly
5258 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
5259 struct md_rdev
*rdev2
;
5262 rdev_for_each(rdev
, mddev
)
5263 rdev_for_each(rdev2
, mddev
) {
5265 rdev
->bdev
->bd_contains
==
5266 rdev2
->bdev
->bd_contains
) {
5267 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5269 bdevname(rdev
->bdev
,b
),
5270 bdevname(rdev2
->bdev
,b2
));
5276 pr_warn("True protection against single-disk failure might be compromised.\n");
5279 mddev
->recovery
= 0;
5280 /* may be over-ridden by personality */
5281 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
5283 mddev
->ok_start_degraded
= start_dirty_degraded
;
5285 if (start_readonly
&& mddev
->ro
== 0)
5286 mddev
->ro
= 2; /* read-only, but switch on first write */
5289 * NOTE: some pers->run(), for example r5l_recovery_log(), wakes
5290 * up mddev->thread. It is important to initialize critical
5291 * resources for mddev->thread BEFORE calling pers->run().
5293 err
= pers
->run(mddev
);
5295 pr_warn("md: pers->run() failed ...\n");
5296 else if (pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
5297 WARN_ONCE(!mddev
->external_size
,
5298 "%s: default size too small, but 'external_size' not in effect?\n",
5300 pr_warn("md: invalid array_size %llu > default size %llu\n",
5301 (unsigned long long)mddev
->array_sectors
/ 2,
5302 (unsigned long long)pers
->size(mddev
, 0, 0) / 2);
5305 if (err
== 0 && pers
->sync_request
&&
5306 (mddev
->bitmap_info
.file
|| mddev
->bitmap_info
.offset
)) {
5307 struct bitmap
*bitmap
;
5309 bitmap
= bitmap_create(mddev
, -1);
5310 if (IS_ERR(bitmap
)) {
5311 err
= PTR_ERR(bitmap
);
5312 pr_warn("%s: failed to create bitmap (%d)\n",
5313 mdname(mddev
), err
);
5315 mddev
->bitmap
= bitmap
;
5319 mddev_detach(mddev
);
5321 pers
->free(mddev
, mddev
->private);
5322 mddev
->private = NULL
;
5323 module_put(pers
->owner
);
5324 bitmap_destroy(mddev
);
5330 rdev_for_each(rdev
, mddev
) {
5331 if (rdev
->raid_disk
>= 0 &&
5332 !blk_queue_nonrot(bdev_get_queue(rdev
->bdev
))) {
5337 if (mddev
->degraded
)
5340 queue_flag_set_unlocked(QUEUE_FLAG_NONROT
, mddev
->queue
);
5342 queue_flag_clear_unlocked(QUEUE_FLAG_NONROT
, mddev
->queue
);
5343 mddev
->queue
->backing_dev_info
->congested_data
= mddev
;
5344 mddev
->queue
->backing_dev_info
->congested_fn
= md_congested
;
5346 if (pers
->sync_request
) {
5347 if (mddev
->kobj
.sd
&&
5348 sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
5349 pr_warn("md: cannot register extra attributes for %s\n",
5351 mddev
->sysfs_action
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "sync_action");
5352 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
5355 atomic_set(&mddev
->writes_pending
,0);
5356 atomic_set(&mddev
->max_corr_read_errors
,
5357 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS
);
5358 mddev
->safemode
= 0;
5359 if (mddev_is_clustered(mddev
))
5360 mddev
->safemode_delay
= 0;
5362 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
5365 spin_lock(&mddev
->lock
);
5367 spin_unlock(&mddev
->lock
);
5368 rdev_for_each(rdev
, mddev
)
5369 if (rdev
->raid_disk
>= 0)
5370 if (sysfs_link_rdev(mddev
, rdev
))
5371 /* failure here is OK */;
5373 if (mddev
->degraded
&& !mddev
->ro
)
5374 /* This ensures that recovering status is reported immediately
5375 * via sysfs - until a lack of spares is confirmed.
5377 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5378 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5380 if (mddev
->sb_flags
)
5381 md_update_sb(mddev
, 0);
5383 md_new_event(mddev
);
5384 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5385 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
5386 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
5389 EXPORT_SYMBOL_GPL(md_run
);
5391 static int do_md_run(struct mddev
*mddev
)
5395 err
= md_run(mddev
);
5398 err
= bitmap_load(mddev
);
5400 bitmap_destroy(mddev
);
5404 if (mddev_is_clustered(mddev
))
5405 md_allow_write(mddev
);
5407 md_wakeup_thread(mddev
->thread
);
5408 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
5410 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
5411 revalidate_disk(mddev
->gendisk
);
5413 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
5418 static int restart_array(struct mddev
*mddev
)
5420 struct gendisk
*disk
= mddev
->gendisk
;
5422 /* Complain if it has no devices */
5423 if (list_empty(&mddev
->disks
))
5429 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
)) {
5430 struct md_rdev
*rdev
;
5431 bool has_journal
= false;
5434 rdev_for_each_rcu(rdev
, mddev
) {
5435 if (test_bit(Journal
, &rdev
->flags
) &&
5436 !test_bit(Faulty
, &rdev
->flags
)) {
5443 /* Don't restart rw with journal missing/faulty */
5448 mddev
->safemode
= 0;
5450 set_disk_ro(disk
, 0);
5451 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev
));
5452 /* Kick recovery or resync if necessary */
5453 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5454 md_wakeup_thread(mddev
->thread
);
5455 md_wakeup_thread(mddev
->sync_thread
);
5456 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5460 static void md_clean(struct mddev
*mddev
)
5462 mddev
->array_sectors
= 0;
5463 mddev
->external_size
= 0;
5464 mddev
->dev_sectors
= 0;
5465 mddev
->raid_disks
= 0;
5466 mddev
->recovery_cp
= 0;
5467 mddev
->resync_min
= 0;
5468 mddev
->resync_max
= MaxSector
;
5469 mddev
->reshape_position
= MaxSector
;
5470 mddev
->external
= 0;
5471 mddev
->persistent
= 0;
5472 mddev
->level
= LEVEL_NONE
;
5473 mddev
->clevel
[0] = 0;
5475 mddev
->sb_flags
= 0;
5477 mddev
->metadata_type
[0] = 0;
5478 mddev
->chunk_sectors
= 0;
5479 mddev
->ctime
= mddev
->utime
= 0;
5481 mddev
->max_disks
= 0;
5483 mddev
->can_decrease_events
= 0;
5484 mddev
->delta_disks
= 0;
5485 mddev
->reshape_backwards
= 0;
5486 mddev
->new_level
= LEVEL_NONE
;
5487 mddev
->new_layout
= 0;
5488 mddev
->new_chunk_sectors
= 0;
5489 mddev
->curr_resync
= 0;
5490 atomic64_set(&mddev
->resync_mismatches
, 0);
5491 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
5492 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
5493 mddev
->recovery
= 0;
5496 mddev
->degraded
= 0;
5497 mddev
->safemode
= 0;
5498 mddev
->private = NULL
;
5499 mddev
->cluster_info
= NULL
;
5500 mddev
->bitmap_info
.offset
= 0;
5501 mddev
->bitmap_info
.default_offset
= 0;
5502 mddev
->bitmap_info
.default_space
= 0;
5503 mddev
->bitmap_info
.chunksize
= 0;
5504 mddev
->bitmap_info
.daemon_sleep
= 0;
5505 mddev
->bitmap_info
.max_write_behind
= 0;
5506 mddev
->bitmap_info
.nodes
= 0;
5509 static void __md_stop_writes(struct mddev
*mddev
)
5511 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5512 flush_workqueue(md_misc_wq
);
5513 if (mddev
->sync_thread
) {
5514 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5515 md_reap_sync_thread(mddev
);
5518 del_timer_sync(&mddev
->safemode_timer
);
5520 if (mddev
->pers
&& mddev
->pers
->quiesce
) {
5521 mddev
->pers
->quiesce(mddev
, 1);
5522 mddev
->pers
->quiesce(mddev
, 0);
5524 bitmap_flush(mddev
);
5526 if (mddev
->ro
== 0 &&
5527 ((!mddev
->in_sync
&& !mddev_is_clustered(mddev
)) ||
5529 /* mark array as shutdown cleanly */
5530 if (!mddev_is_clustered(mddev
))
5532 md_update_sb(mddev
, 1);
5536 void md_stop_writes(struct mddev
*mddev
)
5538 mddev_lock_nointr(mddev
);
5539 __md_stop_writes(mddev
);
5540 mddev_unlock(mddev
);
5542 EXPORT_SYMBOL_GPL(md_stop_writes
);
5544 static void mddev_detach(struct mddev
*mddev
)
5546 struct bitmap
*bitmap
= mddev
->bitmap
;
5547 /* wait for behind writes to complete */
5548 if (bitmap
&& atomic_read(&bitmap
->behind_writes
) > 0) {
5549 pr_debug("md:%s: behind writes in progress - waiting to stop.\n",
5551 /* need to kick something here to make sure I/O goes? */
5552 wait_event(bitmap
->behind_wait
,
5553 atomic_read(&bitmap
->behind_writes
) == 0);
5555 if (mddev
->pers
&& mddev
->pers
->quiesce
) {
5556 mddev
->pers
->quiesce(mddev
, 1);
5557 mddev
->pers
->quiesce(mddev
, 0);
5559 md_unregister_thread(&mddev
->thread
);
5561 blk_sync_queue(mddev
->queue
); /* the unplug fn references 'conf'*/
5564 static void __md_stop(struct mddev
*mddev
)
5566 struct md_personality
*pers
= mddev
->pers
;
5567 mddev_detach(mddev
);
5568 /* Ensure ->event_work is done */
5569 flush_workqueue(md_misc_wq
);
5570 spin_lock(&mddev
->lock
);
5572 spin_unlock(&mddev
->lock
);
5573 pers
->free(mddev
, mddev
->private);
5574 mddev
->private = NULL
;
5575 if (pers
->sync_request
&& mddev
->to_remove
== NULL
)
5576 mddev
->to_remove
= &md_redundancy_group
;
5577 module_put(pers
->owner
);
5578 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5581 void md_stop(struct mddev
*mddev
)
5583 /* stop the array and free an attached data structures.
5584 * This is called from dm-raid
5587 bitmap_destroy(mddev
);
5589 bioset_free(mddev
->bio_set
);
5592 EXPORT_SYMBOL_GPL(md_stop
);
5594 static int md_set_readonly(struct mddev
*mddev
, struct block_device
*bdev
)
5599 if (!test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
)) {
5601 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5602 md_wakeup_thread(mddev
->thread
);
5604 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
5605 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5606 if (mddev
->sync_thread
)
5607 /* Thread might be blocked waiting for metadata update
5608 * which will now never happen */
5609 wake_up_process(mddev
->sync_thread
->tsk
);
5611 if (mddev
->external
&& test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
))
5613 mddev_unlock(mddev
);
5614 wait_event(resync_wait
, !test_bit(MD_RECOVERY_RUNNING
,
5616 wait_event(mddev
->sb_wait
,
5617 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
5618 mddev_lock_nointr(mddev
);
5620 mutex_lock(&mddev
->open_mutex
);
5621 if ((mddev
->pers
&& atomic_read(&mddev
->openers
) > !!bdev
) ||
5622 mddev
->sync_thread
||
5623 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
)) {
5624 pr_warn("md: %s still in use.\n",mdname(mddev
));
5626 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5627 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5628 md_wakeup_thread(mddev
->thread
);
5634 __md_stop_writes(mddev
);
5640 set_disk_ro(mddev
->gendisk
, 1);
5641 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5642 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5643 md_wakeup_thread(mddev
->thread
);
5644 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5648 mutex_unlock(&mddev
->open_mutex
);
5653 * 0 - completely stop and dis-assemble array
5654 * 2 - stop but do not disassemble array
5656 static int do_md_stop(struct mddev
*mddev
, int mode
,
5657 struct block_device
*bdev
)
5659 struct gendisk
*disk
= mddev
->gendisk
;
5660 struct md_rdev
*rdev
;
5663 if (!test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
)) {
5665 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5666 md_wakeup_thread(mddev
->thread
);
5668 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
5669 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5670 if (mddev
->sync_thread
)
5671 /* Thread might be blocked waiting for metadata update
5672 * which will now never happen */
5673 wake_up_process(mddev
->sync_thread
->tsk
);
5675 mddev_unlock(mddev
);
5676 wait_event(resync_wait
, (mddev
->sync_thread
== NULL
&&
5677 !test_bit(MD_RECOVERY_RUNNING
,
5678 &mddev
->recovery
)));
5679 mddev_lock_nointr(mddev
);
5681 mutex_lock(&mddev
->open_mutex
);
5682 if ((mddev
->pers
&& atomic_read(&mddev
->openers
) > !!bdev
) ||
5683 mddev
->sysfs_active
||
5684 mddev
->sync_thread
||
5685 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
)) {
5686 pr_warn("md: %s still in use.\n",mdname(mddev
));
5687 mutex_unlock(&mddev
->open_mutex
);
5689 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5690 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5691 md_wakeup_thread(mddev
->thread
);
5697 set_disk_ro(disk
, 0);
5699 __md_stop_writes(mddev
);
5701 mddev
->queue
->backing_dev_info
->congested_fn
= NULL
;
5703 /* tell userspace to handle 'inactive' */
5704 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5706 rdev_for_each(rdev
, mddev
)
5707 if (rdev
->raid_disk
>= 0)
5708 sysfs_unlink_rdev(mddev
, rdev
);
5710 set_capacity(disk
, 0);
5711 mutex_unlock(&mddev
->open_mutex
);
5713 revalidate_disk(disk
);
5718 mutex_unlock(&mddev
->open_mutex
);
5720 * Free resources if final stop
5723 pr_info("md: %s stopped.\n", mdname(mddev
));
5725 bitmap_destroy(mddev
);
5726 if (mddev
->bitmap_info
.file
) {
5727 struct file
*f
= mddev
->bitmap_info
.file
;
5728 spin_lock(&mddev
->lock
);
5729 mddev
->bitmap_info
.file
= NULL
;
5730 spin_unlock(&mddev
->lock
);
5733 mddev
->bitmap_info
.offset
= 0;
5735 export_array(mddev
);
5738 if (mddev
->hold_active
== UNTIL_STOP
)
5739 mddev
->hold_active
= 0;
5741 md_new_event(mddev
);
5742 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5747 static void autorun_array(struct mddev
*mddev
)
5749 struct md_rdev
*rdev
;
5752 if (list_empty(&mddev
->disks
))
5755 pr_info("md: running: ");
5757 rdev_for_each(rdev
, mddev
) {
5758 char b
[BDEVNAME_SIZE
];
5759 pr_cont("<%s>", bdevname(rdev
->bdev
,b
));
5763 err
= do_md_run(mddev
);
5765 pr_warn("md: do_md_run() returned %d\n", err
);
5766 do_md_stop(mddev
, 0, NULL
);
5771 * lets try to run arrays based on all disks that have arrived
5772 * until now. (those are in pending_raid_disks)
5774 * the method: pick the first pending disk, collect all disks with
5775 * the same UUID, remove all from the pending list and put them into
5776 * the 'same_array' list. Then order this list based on superblock
5777 * update time (freshest comes first), kick out 'old' disks and
5778 * compare superblocks. If everything's fine then run it.
5780 * If "unit" is allocated, then bump its reference count
5782 static void autorun_devices(int part
)
5784 struct md_rdev
*rdev0
, *rdev
, *tmp
;
5785 struct mddev
*mddev
;
5786 char b
[BDEVNAME_SIZE
];
5788 pr_info("md: autorun ...\n");
5789 while (!list_empty(&pending_raid_disks
)) {
5792 LIST_HEAD(candidates
);
5793 rdev0
= list_entry(pending_raid_disks
.next
,
5794 struct md_rdev
, same_set
);
5796 pr_debug("md: considering %s ...\n", bdevname(rdev0
->bdev
,b
));
5797 INIT_LIST_HEAD(&candidates
);
5798 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
5799 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
5800 pr_debug("md: adding %s ...\n",
5801 bdevname(rdev
->bdev
,b
));
5802 list_move(&rdev
->same_set
, &candidates
);
5805 * now we have a set of devices, with all of them having
5806 * mostly sane superblocks. It's time to allocate the
5810 dev
= MKDEV(mdp_major
,
5811 rdev0
->preferred_minor
<< MdpMinorShift
);
5812 unit
= MINOR(dev
) >> MdpMinorShift
;
5814 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
5817 if (rdev0
->preferred_minor
!= unit
) {
5818 pr_warn("md: unit number in %s is bad: %d\n",
5819 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
5823 md_probe(dev
, NULL
, NULL
);
5824 mddev
= mddev_find(dev
);
5825 if (!mddev
|| !mddev
->gendisk
) {
5830 if (mddev_lock(mddev
))
5831 pr_warn("md: %s locked, cannot run\n", mdname(mddev
));
5832 else if (mddev
->raid_disks
|| mddev
->major_version
5833 || !list_empty(&mddev
->disks
)) {
5834 pr_warn("md: %s already running, cannot run %s\n",
5835 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
5836 mddev_unlock(mddev
);
5838 pr_debug("md: created %s\n", mdname(mddev
));
5839 mddev
->persistent
= 1;
5840 rdev_for_each_list(rdev
, tmp
, &candidates
) {
5841 list_del_init(&rdev
->same_set
);
5842 if (bind_rdev_to_array(rdev
, mddev
))
5845 autorun_array(mddev
);
5846 mddev_unlock(mddev
);
5848 /* on success, candidates will be empty, on error
5851 rdev_for_each_list(rdev
, tmp
, &candidates
) {
5852 list_del_init(&rdev
->same_set
);
5857 pr_info("md: ... autorun DONE.\n");
5859 #endif /* !MODULE */
5861 static int get_version(void __user
*arg
)
5865 ver
.major
= MD_MAJOR_VERSION
;
5866 ver
.minor
= MD_MINOR_VERSION
;
5867 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
5869 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
5875 static int get_array_info(struct mddev
*mddev
, void __user
*arg
)
5877 mdu_array_info_t info
;
5878 int nr
,working
,insync
,failed
,spare
;
5879 struct md_rdev
*rdev
;
5881 nr
= working
= insync
= failed
= spare
= 0;
5883 rdev_for_each_rcu(rdev
, mddev
) {
5885 if (test_bit(Faulty
, &rdev
->flags
))
5889 if (test_bit(In_sync
, &rdev
->flags
))
5891 else if (test_bit(Journal
, &rdev
->flags
))
5892 /* TODO: add journal count to md_u.h */
5900 info
.major_version
= mddev
->major_version
;
5901 info
.minor_version
= mddev
->minor_version
;
5902 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
5903 info
.ctime
= clamp_t(time64_t
, mddev
->ctime
, 0, U32_MAX
);
5904 info
.level
= mddev
->level
;
5905 info
.size
= mddev
->dev_sectors
/ 2;
5906 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
5909 info
.raid_disks
= mddev
->raid_disks
;
5910 info
.md_minor
= mddev
->md_minor
;
5911 info
.not_persistent
= !mddev
->persistent
;
5913 info
.utime
= clamp_t(time64_t
, mddev
->utime
, 0, U32_MAX
);
5916 info
.state
= (1<<MD_SB_CLEAN
);
5917 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5918 info
.state
|= (1<<MD_SB_BITMAP_PRESENT
);
5919 if (mddev_is_clustered(mddev
))
5920 info
.state
|= (1<<MD_SB_CLUSTERED
);
5921 info
.active_disks
= insync
;
5922 info
.working_disks
= working
;
5923 info
.failed_disks
= failed
;
5924 info
.spare_disks
= spare
;
5926 info
.layout
= mddev
->layout
;
5927 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
5929 if (copy_to_user(arg
, &info
, sizeof(info
)))
5935 static int get_bitmap_file(struct mddev
*mddev
, void __user
* arg
)
5937 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
5941 file
= kzalloc(sizeof(*file
), GFP_NOIO
);
5946 spin_lock(&mddev
->lock
);
5947 /* bitmap enabled */
5948 if (mddev
->bitmap_info
.file
) {
5949 ptr
= file_path(mddev
->bitmap_info
.file
, file
->pathname
,
5950 sizeof(file
->pathname
));
5954 memmove(file
->pathname
, ptr
,
5955 sizeof(file
->pathname
)-(ptr
-file
->pathname
));
5957 spin_unlock(&mddev
->lock
);
5960 copy_to_user(arg
, file
, sizeof(*file
)))
5967 static int get_disk_info(struct mddev
*mddev
, void __user
* arg
)
5969 mdu_disk_info_t info
;
5970 struct md_rdev
*rdev
;
5972 if (copy_from_user(&info
, arg
, sizeof(info
)))
5976 rdev
= md_find_rdev_nr_rcu(mddev
, info
.number
);
5978 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
5979 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
5980 info
.raid_disk
= rdev
->raid_disk
;
5982 if (test_bit(Faulty
, &rdev
->flags
))
5983 info
.state
|= (1<<MD_DISK_FAULTY
);
5984 else if (test_bit(In_sync
, &rdev
->flags
)) {
5985 info
.state
|= (1<<MD_DISK_ACTIVE
);
5986 info
.state
|= (1<<MD_DISK_SYNC
);
5988 if (test_bit(Journal
, &rdev
->flags
))
5989 info
.state
|= (1<<MD_DISK_JOURNAL
);
5990 if (test_bit(WriteMostly
, &rdev
->flags
))
5991 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
5992 if (test_bit(FailFast
, &rdev
->flags
))
5993 info
.state
|= (1<<MD_DISK_FAILFAST
);
5995 info
.major
= info
.minor
= 0;
5996 info
.raid_disk
= -1;
5997 info
.state
= (1<<MD_DISK_REMOVED
);
6001 if (copy_to_user(arg
, &info
, sizeof(info
)))
6007 static int add_new_disk(struct mddev
*mddev
, mdu_disk_info_t
*info
)
6009 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
6010 struct md_rdev
*rdev
;
6011 dev_t dev
= MKDEV(info
->major
,info
->minor
);
6013 if (mddev_is_clustered(mddev
) &&
6014 !(info
->state
& ((1 << MD_DISK_CLUSTER_ADD
) | (1 << MD_DISK_CANDIDATE
)))) {
6015 pr_warn("%s: Cannot add to clustered mddev.\n",
6020 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
6023 if (!mddev
->raid_disks
) {
6025 /* expecting a device which has a superblock */
6026 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
6028 pr_warn("md: md_import_device returned %ld\n",
6030 return PTR_ERR(rdev
);
6032 if (!list_empty(&mddev
->disks
)) {
6033 struct md_rdev
*rdev0
6034 = list_entry(mddev
->disks
.next
,
6035 struct md_rdev
, same_set
);
6036 err
= super_types
[mddev
->major_version
]
6037 .load_super(rdev
, rdev0
, mddev
->minor_version
);
6039 pr_warn("md: %s has different UUID to %s\n",
6040 bdevname(rdev
->bdev
,b
),
6041 bdevname(rdev0
->bdev
,b2
));
6046 err
= bind_rdev_to_array(rdev
, mddev
);
6053 * add_new_disk can be used once the array is assembled
6054 * to add "hot spares". They must already have a superblock
6059 if (!mddev
->pers
->hot_add_disk
) {
6060 pr_warn("%s: personality does not support diskops!\n",
6064 if (mddev
->persistent
)
6065 rdev
= md_import_device(dev
, mddev
->major_version
,
6066 mddev
->minor_version
);
6068 rdev
= md_import_device(dev
, -1, -1);
6070 pr_warn("md: md_import_device returned %ld\n",
6072 return PTR_ERR(rdev
);
6074 /* set saved_raid_disk if appropriate */
6075 if (!mddev
->persistent
) {
6076 if (info
->state
& (1<<MD_DISK_SYNC
) &&
6077 info
->raid_disk
< mddev
->raid_disks
) {
6078 rdev
->raid_disk
= info
->raid_disk
;
6079 set_bit(In_sync
, &rdev
->flags
);
6080 clear_bit(Bitmap_sync
, &rdev
->flags
);
6082 rdev
->raid_disk
= -1;
6083 rdev
->saved_raid_disk
= rdev
->raid_disk
;
6085 super_types
[mddev
->major_version
].
6086 validate_super(mddev
, rdev
);
6087 if ((info
->state
& (1<<MD_DISK_SYNC
)) &&
6088 rdev
->raid_disk
!= info
->raid_disk
) {
6089 /* This was a hot-add request, but events doesn't
6090 * match, so reject it.
6096 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
6097 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
6098 set_bit(WriteMostly
, &rdev
->flags
);
6100 clear_bit(WriteMostly
, &rdev
->flags
);
6101 if (info
->state
& (1<<MD_DISK_FAILFAST
))
6102 set_bit(FailFast
, &rdev
->flags
);
6104 clear_bit(FailFast
, &rdev
->flags
);
6106 if (info
->state
& (1<<MD_DISK_JOURNAL
)) {
6107 struct md_rdev
*rdev2
;
6108 bool has_journal
= false;
6110 /* make sure no existing journal disk */
6111 rdev_for_each(rdev2
, mddev
) {
6112 if (test_bit(Journal
, &rdev2
->flags
)) {
6121 set_bit(Journal
, &rdev
->flags
);
6124 * check whether the device shows up in other nodes
6126 if (mddev_is_clustered(mddev
)) {
6127 if (info
->state
& (1 << MD_DISK_CANDIDATE
))
6128 set_bit(Candidate
, &rdev
->flags
);
6129 else if (info
->state
& (1 << MD_DISK_CLUSTER_ADD
)) {
6130 /* --add initiated by this node */
6131 err
= md_cluster_ops
->add_new_disk(mddev
, rdev
);
6139 rdev
->raid_disk
= -1;
6140 err
= bind_rdev_to_array(rdev
, mddev
);
6145 if (mddev_is_clustered(mddev
)) {
6146 if (info
->state
& (1 << MD_DISK_CANDIDATE
)) {
6148 err
= md_cluster_ops
->new_disk_ack(mddev
,
6151 md_kick_rdev_from_array(rdev
);
6155 md_cluster_ops
->add_new_disk_cancel(mddev
);
6157 err
= add_bound_rdev(rdev
);
6161 err
= add_bound_rdev(rdev
);
6166 /* otherwise, add_new_disk is only allowed
6167 * for major_version==0 superblocks
6169 if (mddev
->major_version
!= 0) {
6170 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev
));
6174 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
6176 rdev
= md_import_device(dev
, -1, 0);
6178 pr_warn("md: error, md_import_device() returned %ld\n",
6180 return PTR_ERR(rdev
);
6182 rdev
->desc_nr
= info
->number
;
6183 if (info
->raid_disk
< mddev
->raid_disks
)
6184 rdev
->raid_disk
= info
->raid_disk
;
6186 rdev
->raid_disk
= -1;
6188 if (rdev
->raid_disk
< mddev
->raid_disks
)
6189 if (info
->state
& (1<<MD_DISK_SYNC
))
6190 set_bit(In_sync
, &rdev
->flags
);
6192 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
6193 set_bit(WriteMostly
, &rdev
->flags
);
6194 if (info
->state
& (1<<MD_DISK_FAILFAST
))
6195 set_bit(FailFast
, &rdev
->flags
);
6197 if (!mddev
->persistent
) {
6198 pr_debug("md: nonpersistent superblock ...\n");
6199 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
6201 rdev
->sb_start
= calc_dev_sboffset(rdev
);
6202 rdev
->sectors
= rdev
->sb_start
;
6204 err
= bind_rdev_to_array(rdev
, mddev
);
6214 static int hot_remove_disk(struct mddev
*mddev
, dev_t dev
)
6216 char b
[BDEVNAME_SIZE
];
6217 struct md_rdev
*rdev
;
6219 rdev
= find_rdev(mddev
, dev
);
6223 if (rdev
->raid_disk
< 0)
6226 clear_bit(Blocked
, &rdev
->flags
);
6227 remove_and_add_spares(mddev
, rdev
);
6229 if (rdev
->raid_disk
>= 0)
6233 if (mddev_is_clustered(mddev
))
6234 md_cluster_ops
->remove_disk(mddev
, rdev
);
6236 md_kick_rdev_from_array(rdev
);
6237 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
6239 md_wakeup_thread(mddev
->thread
);
6241 md_update_sb(mddev
, 1);
6242 md_new_event(mddev
);
6246 pr_debug("md: cannot remove active disk %s from %s ...\n",
6247 bdevname(rdev
->bdev
,b
), mdname(mddev
));
6251 static int hot_add_disk(struct mddev
*mddev
, dev_t dev
)
6253 char b
[BDEVNAME_SIZE
];
6255 struct md_rdev
*rdev
;
6260 if (mddev
->major_version
!= 0) {
6261 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
6265 if (!mddev
->pers
->hot_add_disk
) {
6266 pr_warn("%s: personality does not support diskops!\n",
6271 rdev
= md_import_device(dev
, -1, 0);
6273 pr_warn("md: error, md_import_device() returned %ld\n",
6278 if (mddev
->persistent
)
6279 rdev
->sb_start
= calc_dev_sboffset(rdev
);
6281 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
6283 rdev
->sectors
= rdev
->sb_start
;
6285 if (test_bit(Faulty
, &rdev
->flags
)) {
6286 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
6287 bdevname(rdev
->bdev
,b
), mdname(mddev
));
6292 clear_bit(In_sync
, &rdev
->flags
);
6294 rdev
->saved_raid_disk
= -1;
6295 err
= bind_rdev_to_array(rdev
, mddev
);
6300 * The rest should better be atomic, we can have disk failures
6301 * noticed in interrupt contexts ...
6304 rdev
->raid_disk
= -1;
6306 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
6308 md_update_sb(mddev
, 1);
6310 * Kick recovery, maybe this spare has to be added to the
6311 * array immediately.
6313 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6314 md_wakeup_thread(mddev
->thread
);
6315 md_new_event(mddev
);
6323 static int set_bitmap_file(struct mddev
*mddev
, int fd
)
6328 if (!mddev
->pers
->quiesce
|| !mddev
->thread
)
6330 if (mddev
->recovery
|| mddev
->sync_thread
)
6332 /* we should be able to change the bitmap.. */
6336 struct inode
*inode
;
6339 if (mddev
->bitmap
|| mddev
->bitmap_info
.file
)
6340 return -EEXIST
; /* cannot add when bitmap is present */
6344 pr_warn("%s: error: failed to get bitmap file\n",
6349 inode
= f
->f_mapping
->host
;
6350 if (!S_ISREG(inode
->i_mode
)) {
6351 pr_warn("%s: error: bitmap file must be a regular file\n",
6354 } else if (!(f
->f_mode
& FMODE_WRITE
)) {
6355 pr_warn("%s: error: bitmap file must open for write\n",
6358 } else if (atomic_read(&inode
->i_writecount
) != 1) {
6359 pr_warn("%s: error: bitmap file is already in use\n",
6367 mddev
->bitmap_info
.file
= f
;
6368 mddev
->bitmap_info
.offset
= 0; /* file overrides offset */
6369 } else if (mddev
->bitmap
== NULL
)
6370 return -ENOENT
; /* cannot remove what isn't there */
6373 mddev
->pers
->quiesce(mddev
, 1);
6375 struct bitmap
*bitmap
;
6377 bitmap
= bitmap_create(mddev
, -1);
6378 if (!IS_ERR(bitmap
)) {
6379 mddev
->bitmap
= bitmap
;
6380 err
= bitmap_load(mddev
);
6382 err
= PTR_ERR(bitmap
);
6384 if (fd
< 0 || err
) {
6385 bitmap_destroy(mddev
);
6386 fd
= -1; /* make sure to put the file */
6388 mddev
->pers
->quiesce(mddev
, 0);
6391 struct file
*f
= mddev
->bitmap_info
.file
;
6393 spin_lock(&mddev
->lock
);
6394 mddev
->bitmap_info
.file
= NULL
;
6395 spin_unlock(&mddev
->lock
);
6404 * set_array_info is used two different ways
6405 * The original usage is when creating a new array.
6406 * In this usage, raid_disks is > 0 and it together with
6407 * level, size, not_persistent,layout,chunksize determine the
6408 * shape of the array.
6409 * This will always create an array with a type-0.90.0 superblock.
6410 * The newer usage is when assembling an array.
6411 * In this case raid_disks will be 0, and the major_version field is
6412 * use to determine which style super-blocks are to be found on the devices.
6413 * The minor and patch _version numbers are also kept incase the
6414 * super_block handler wishes to interpret them.
6416 static int set_array_info(struct mddev
*mddev
, mdu_array_info_t
*info
)
6419 if (info
->raid_disks
== 0) {
6420 /* just setting version number for superblock loading */
6421 if (info
->major_version
< 0 ||
6422 info
->major_version
>= ARRAY_SIZE(super_types
) ||
6423 super_types
[info
->major_version
].name
== NULL
) {
6424 /* maybe try to auto-load a module? */
6425 pr_warn("md: superblock version %d not known\n",
6426 info
->major_version
);
6429 mddev
->major_version
= info
->major_version
;
6430 mddev
->minor_version
= info
->minor_version
;
6431 mddev
->patch_version
= info
->patch_version
;
6432 mddev
->persistent
= !info
->not_persistent
;
6433 /* ensure mddev_put doesn't delete this now that there
6434 * is some minimal configuration.
6436 mddev
->ctime
= ktime_get_real_seconds();
6439 mddev
->major_version
= MD_MAJOR_VERSION
;
6440 mddev
->minor_version
= MD_MINOR_VERSION
;
6441 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
6442 mddev
->ctime
= ktime_get_real_seconds();
6444 mddev
->level
= info
->level
;
6445 mddev
->clevel
[0] = 0;
6446 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
6447 mddev
->raid_disks
= info
->raid_disks
;
6448 /* don't set md_minor, it is determined by which /dev/md* was
6451 if (info
->state
& (1<<MD_SB_CLEAN
))
6452 mddev
->recovery_cp
= MaxSector
;
6454 mddev
->recovery_cp
= 0;
6455 mddev
->persistent
= ! info
->not_persistent
;
6456 mddev
->external
= 0;
6458 mddev
->layout
= info
->layout
;
6459 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
6461 mddev
->max_disks
= MD_SB_DISKS
;
6463 if (mddev
->persistent
) {
6465 mddev
->sb_flags
= 0;
6467 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
6469 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
6470 mddev
->bitmap_info
.default_space
= 64*2 - (MD_SB_BYTES
>> 9);
6471 mddev
->bitmap_info
.offset
= 0;
6473 mddev
->reshape_position
= MaxSector
;
6476 * Generate a 128 bit UUID
6478 get_random_bytes(mddev
->uuid
, 16);
6480 mddev
->new_level
= mddev
->level
;
6481 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
6482 mddev
->new_layout
= mddev
->layout
;
6483 mddev
->delta_disks
= 0;
6484 mddev
->reshape_backwards
= 0;
6489 void md_set_array_sectors(struct mddev
*mddev
, sector_t array_sectors
)
6491 WARN(!mddev_is_locked(mddev
), "%s: unlocked mddev!\n", __func__
);
6493 if (mddev
->external_size
)
6496 mddev
->array_sectors
= array_sectors
;
6498 EXPORT_SYMBOL(md_set_array_sectors
);
6500 static int update_size(struct mddev
*mddev
, sector_t num_sectors
)
6502 struct md_rdev
*rdev
;
6504 int fit
= (num_sectors
== 0);
6506 /* cluster raid doesn't support update size */
6507 if (mddev_is_clustered(mddev
))
6510 if (mddev
->pers
->resize
== NULL
)
6512 /* The "num_sectors" is the number of sectors of each device that
6513 * is used. This can only make sense for arrays with redundancy.
6514 * linear and raid0 always use whatever space is available. We can only
6515 * consider changing this number if no resync or reconstruction is
6516 * happening, and if the new size is acceptable. It must fit before the
6517 * sb_start or, if that is <data_offset, it must fit before the size
6518 * of each device. If num_sectors is zero, we find the largest size
6521 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
6527 rdev_for_each(rdev
, mddev
) {
6528 sector_t avail
= rdev
->sectors
;
6530 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
6531 num_sectors
= avail
;
6532 if (avail
< num_sectors
)
6535 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
6537 revalidate_disk(mddev
->gendisk
);
6541 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
)
6544 struct md_rdev
*rdev
;
6545 /* change the number of raid disks */
6546 if (mddev
->pers
->check_reshape
== NULL
)
6550 if (raid_disks
<= 0 ||
6551 (mddev
->max_disks
&& raid_disks
>= mddev
->max_disks
))
6553 if (mddev
->sync_thread
||
6554 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
6555 mddev
->reshape_position
!= MaxSector
)
6558 rdev_for_each(rdev
, mddev
) {
6559 if (mddev
->raid_disks
< raid_disks
&&
6560 rdev
->data_offset
< rdev
->new_data_offset
)
6562 if (mddev
->raid_disks
> raid_disks
&&
6563 rdev
->data_offset
> rdev
->new_data_offset
)
6567 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
6568 if (mddev
->delta_disks
< 0)
6569 mddev
->reshape_backwards
= 1;
6570 else if (mddev
->delta_disks
> 0)
6571 mddev
->reshape_backwards
= 0;
6573 rv
= mddev
->pers
->check_reshape(mddev
);
6575 mddev
->delta_disks
= 0;
6576 mddev
->reshape_backwards
= 0;
6582 * update_array_info is used to change the configuration of an
6584 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6585 * fields in the info are checked against the array.
6586 * Any differences that cannot be handled will cause an error.
6587 * Normally, only one change can be managed at a time.
6589 static int update_array_info(struct mddev
*mddev
, mdu_array_info_t
*info
)
6595 /* calculate expected state,ignoring low bits */
6596 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
6597 state
|= (1 << MD_SB_BITMAP_PRESENT
);
6599 if (mddev
->major_version
!= info
->major_version
||
6600 mddev
->minor_version
!= info
->minor_version
||
6601 /* mddev->patch_version != info->patch_version || */
6602 mddev
->ctime
!= info
->ctime
||
6603 mddev
->level
!= info
->level
||
6604 /* mddev->layout != info->layout || */
6605 mddev
->persistent
!= !info
->not_persistent
||
6606 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
6607 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6608 ((state
^info
->state
) & 0xfffffe00)
6611 /* Check there is only one change */
6612 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
6614 if (mddev
->raid_disks
!= info
->raid_disks
)
6616 if (mddev
->layout
!= info
->layout
)
6618 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
6625 if (mddev
->layout
!= info
->layout
) {
6627 * we don't need to do anything at the md level, the
6628 * personality will take care of it all.
6630 if (mddev
->pers
->check_reshape
== NULL
)
6633 mddev
->new_layout
= info
->layout
;
6634 rv
= mddev
->pers
->check_reshape(mddev
);
6636 mddev
->new_layout
= mddev
->layout
;
6640 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
6641 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
6643 if (mddev
->raid_disks
!= info
->raid_disks
)
6644 rv
= update_raid_disks(mddev
, info
->raid_disks
);
6646 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
6647 if (mddev
->pers
->quiesce
== NULL
|| mddev
->thread
== NULL
) {
6651 if (mddev
->recovery
|| mddev
->sync_thread
) {
6655 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
6656 struct bitmap
*bitmap
;
6657 /* add the bitmap */
6658 if (mddev
->bitmap
) {
6662 if (mddev
->bitmap_info
.default_offset
== 0) {
6666 mddev
->bitmap_info
.offset
=
6667 mddev
->bitmap_info
.default_offset
;
6668 mddev
->bitmap_info
.space
=
6669 mddev
->bitmap_info
.default_space
;
6670 mddev
->pers
->quiesce(mddev
, 1);
6671 bitmap
= bitmap_create(mddev
, -1);
6672 if (!IS_ERR(bitmap
)) {
6673 mddev
->bitmap
= bitmap
;
6674 rv
= bitmap_load(mddev
);
6676 rv
= PTR_ERR(bitmap
);
6678 bitmap_destroy(mddev
);
6679 mddev
->pers
->quiesce(mddev
, 0);
6681 /* remove the bitmap */
6682 if (!mddev
->bitmap
) {
6686 if (mddev
->bitmap
->storage
.file
) {
6690 if (mddev
->bitmap_info
.nodes
) {
6691 /* hold PW on all the bitmap lock */
6692 if (md_cluster_ops
->lock_all_bitmaps(mddev
) <= 0) {
6693 pr_warn("md: can't change bitmap to none since the array is in use by more than one node\n");
6695 md_cluster_ops
->unlock_all_bitmaps(mddev
);
6699 mddev
->bitmap_info
.nodes
= 0;
6700 md_cluster_ops
->leave(mddev
);
6702 mddev
->pers
->quiesce(mddev
, 1);
6703 bitmap_destroy(mddev
);
6704 mddev
->pers
->quiesce(mddev
, 0);
6705 mddev
->bitmap_info
.offset
= 0;
6708 md_update_sb(mddev
, 1);
6714 static int set_disk_faulty(struct mddev
*mddev
, dev_t dev
)
6716 struct md_rdev
*rdev
;
6719 if (mddev
->pers
== NULL
)
6723 rdev
= find_rdev_rcu(mddev
, dev
);
6727 md_error(mddev
, rdev
);
6728 if (!test_bit(Faulty
, &rdev
->flags
))
6736 * We have a problem here : there is no easy way to give a CHS
6737 * virtual geometry. We currently pretend that we have a 2 heads
6738 * 4 sectors (with a BIG number of cylinders...). This drives
6739 * dosfs just mad... ;-)
6741 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
6743 struct mddev
*mddev
= bdev
->bd_disk
->private_data
;
6747 geo
->cylinders
= mddev
->array_sectors
/ 8;
6751 static inline bool md_ioctl_valid(unsigned int cmd
)
6756 case GET_ARRAY_INFO
:
6757 case GET_BITMAP_FILE
:
6760 case HOT_REMOVE_DISK
:
6763 case RESTART_ARRAY_RW
:
6765 case SET_ARRAY_INFO
:
6766 case SET_BITMAP_FILE
:
6767 case SET_DISK_FAULTY
:
6770 case CLUSTERED_DISK_NACK
:
6777 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
6778 unsigned int cmd
, unsigned long arg
)
6781 void __user
*argp
= (void __user
*)arg
;
6782 struct mddev
*mddev
= NULL
;
6785 if (!md_ioctl_valid(cmd
))
6790 case GET_ARRAY_INFO
:
6794 if (!capable(CAP_SYS_ADMIN
))
6799 * Commands dealing with the RAID driver but not any
6804 err
= get_version(argp
);
6810 autostart_arrays(arg
);
6817 * Commands creating/starting a new array:
6820 mddev
= bdev
->bd_disk
->private_data
;
6827 /* Some actions do not requires the mutex */
6829 case GET_ARRAY_INFO
:
6830 if (!mddev
->raid_disks
&& !mddev
->external
)
6833 err
= get_array_info(mddev
, argp
);
6837 if (!mddev
->raid_disks
&& !mddev
->external
)
6840 err
= get_disk_info(mddev
, argp
);
6843 case SET_DISK_FAULTY
:
6844 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
6847 case GET_BITMAP_FILE
:
6848 err
= get_bitmap_file(mddev
, argp
);
6853 if (cmd
== ADD_NEW_DISK
)
6854 /* need to ensure md_delayed_delete() has completed */
6855 flush_workqueue(md_misc_wq
);
6857 if (cmd
== HOT_REMOVE_DISK
)
6858 /* need to ensure recovery thread has run */
6859 wait_event_interruptible_timeout(mddev
->sb_wait
,
6860 !test_bit(MD_RECOVERY_NEEDED
,
6862 msecs_to_jiffies(5000));
6863 if (cmd
== STOP_ARRAY
|| cmd
== STOP_ARRAY_RO
) {
6864 /* Need to flush page cache, and ensure no-one else opens
6867 mutex_lock(&mddev
->open_mutex
);
6868 if (mddev
->pers
&& atomic_read(&mddev
->openers
) > 1) {
6869 mutex_unlock(&mddev
->open_mutex
);
6873 set_bit(MD_CLOSING
, &mddev
->flags
);
6874 mutex_unlock(&mddev
->open_mutex
);
6875 sync_blockdev(bdev
);
6877 err
= mddev_lock(mddev
);
6879 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
6884 if (cmd
== SET_ARRAY_INFO
) {
6885 mdu_array_info_t info
;
6887 memset(&info
, 0, sizeof(info
));
6888 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
6893 err
= update_array_info(mddev
, &info
);
6895 pr_warn("md: couldn't update array info. %d\n", err
);
6900 if (!list_empty(&mddev
->disks
)) {
6901 pr_warn("md: array %s already has disks!\n", mdname(mddev
));
6905 if (mddev
->raid_disks
) {
6906 pr_warn("md: array %s already initialised!\n", mdname(mddev
));
6910 err
= set_array_info(mddev
, &info
);
6912 pr_warn("md: couldn't set array info. %d\n", err
);
6919 * Commands querying/configuring an existing array:
6921 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
6922 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
6923 if ((!mddev
->raid_disks
&& !mddev
->external
)
6924 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
6925 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
6926 && cmd
!= GET_BITMAP_FILE
) {
6932 * Commands even a read-only array can execute:
6935 case RESTART_ARRAY_RW
:
6936 err
= restart_array(mddev
);
6940 err
= do_md_stop(mddev
, 0, bdev
);
6944 err
= md_set_readonly(mddev
, bdev
);
6947 case HOT_REMOVE_DISK
:
6948 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
6952 /* We can support ADD_NEW_DISK on read-only arrays
6953 * only if we are re-adding a preexisting device.
6954 * So require mddev->pers and MD_DISK_SYNC.
6957 mdu_disk_info_t info
;
6958 if (copy_from_user(&info
, argp
, sizeof(info
)))
6960 else if (!(info
.state
& (1<<MD_DISK_SYNC
)))
6961 /* Need to clear read-only for this */
6964 err
= add_new_disk(mddev
, &info
);
6970 if (get_user(ro
, (int __user
*)(arg
))) {
6976 /* if the bdev is going readonly the value of mddev->ro
6977 * does not matter, no writes are coming
6982 /* are we are already prepared for writes? */
6986 /* transitioning to readauto need only happen for
6987 * arrays that call md_write_start
6990 err
= restart_array(mddev
);
6993 set_disk_ro(mddev
->gendisk
, 0);
7000 * The remaining ioctls are changing the state of the
7001 * superblock, so we do not allow them on read-only arrays.
7003 if (mddev
->ro
&& mddev
->pers
) {
7004 if (mddev
->ro
== 2) {
7006 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7007 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7008 /* mddev_unlock will wake thread */
7009 /* If a device failed while we were read-only, we
7010 * need to make sure the metadata is updated now.
7012 if (test_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
)) {
7013 mddev_unlock(mddev
);
7014 wait_event(mddev
->sb_wait
,
7015 !test_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
) &&
7016 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
7017 mddev_lock_nointr(mddev
);
7028 mdu_disk_info_t info
;
7029 if (copy_from_user(&info
, argp
, sizeof(info
)))
7032 err
= add_new_disk(mddev
, &info
);
7036 case CLUSTERED_DISK_NACK
:
7037 if (mddev_is_clustered(mddev
))
7038 md_cluster_ops
->new_disk_ack(mddev
, false);
7044 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
7048 err
= do_md_run(mddev
);
7051 case SET_BITMAP_FILE
:
7052 err
= set_bitmap_file(mddev
, (int)arg
);
7061 if (mddev
->hold_active
== UNTIL_IOCTL
&&
7063 mddev
->hold_active
= 0;
7064 mddev_unlock(mddev
);
7068 #ifdef CONFIG_COMPAT
7069 static int md_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
7070 unsigned int cmd
, unsigned long arg
)
7073 case HOT_REMOVE_DISK
:
7075 case SET_DISK_FAULTY
:
7076 case SET_BITMAP_FILE
:
7077 /* These take in integer arg, do not convert */
7080 arg
= (unsigned long)compat_ptr(arg
);
7084 return md_ioctl(bdev
, mode
, cmd
, arg
);
7086 #endif /* CONFIG_COMPAT */
7088 static int md_open(struct block_device
*bdev
, fmode_t mode
)
7091 * Succeed if we can lock the mddev, which confirms that
7092 * it isn't being stopped right now.
7094 struct mddev
*mddev
= mddev_find(bdev
->bd_dev
);
7100 if (mddev
->gendisk
!= bdev
->bd_disk
) {
7101 /* we are racing with mddev_put which is discarding this
7105 /* Wait until bdev->bd_disk is definitely gone */
7106 flush_workqueue(md_misc_wq
);
7107 /* Then retry the open from the top */
7108 return -ERESTARTSYS
;
7110 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
7112 if ((err
= mutex_lock_interruptible(&mddev
->open_mutex
)))
7115 if (test_bit(MD_CLOSING
, &mddev
->flags
)) {
7116 mutex_unlock(&mddev
->open_mutex
);
7122 atomic_inc(&mddev
->openers
);
7123 mutex_unlock(&mddev
->open_mutex
);
7125 check_disk_change(bdev
);
7132 static void md_release(struct gendisk
*disk
, fmode_t mode
)
7134 struct mddev
*mddev
= disk
->private_data
;
7137 atomic_dec(&mddev
->openers
);
7141 static int md_media_changed(struct gendisk
*disk
)
7143 struct mddev
*mddev
= disk
->private_data
;
7145 return mddev
->changed
;
7148 static int md_revalidate(struct gendisk
*disk
)
7150 struct mddev
*mddev
= disk
->private_data
;
7155 static const struct block_device_operations md_fops
=
7157 .owner
= THIS_MODULE
,
7159 .release
= md_release
,
7161 #ifdef CONFIG_COMPAT
7162 .compat_ioctl
= md_compat_ioctl
,
7164 .getgeo
= md_getgeo
,
7165 .media_changed
= md_media_changed
,
7166 .revalidate_disk
= md_revalidate
,
7169 static int md_thread(void *arg
)
7171 struct md_thread
*thread
= arg
;
7174 * md_thread is a 'system-thread', it's priority should be very
7175 * high. We avoid resource deadlocks individually in each
7176 * raid personality. (RAID5 does preallocation) We also use RR and
7177 * the very same RT priority as kswapd, thus we will never get
7178 * into a priority inversion deadlock.
7180 * we definitely have to have equal or higher priority than
7181 * bdflush, otherwise bdflush will deadlock if there are too
7182 * many dirty RAID5 blocks.
7185 allow_signal(SIGKILL
);
7186 while (!kthread_should_stop()) {
7188 /* We need to wait INTERRUPTIBLE so that
7189 * we don't add to the load-average.
7190 * That means we need to be sure no signals are
7193 if (signal_pending(current
))
7194 flush_signals(current
);
7196 wait_event_interruptible_timeout
7198 test_bit(THREAD_WAKEUP
, &thread
->flags
)
7199 || kthread_should_stop() || kthread_should_park(),
7202 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
7203 if (kthread_should_park())
7205 if (!kthread_should_stop())
7206 thread
->run(thread
);
7212 void md_wakeup_thread(struct md_thread
*thread
)
7215 pr_debug("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
7216 set_bit(THREAD_WAKEUP
, &thread
->flags
);
7217 wake_up(&thread
->wqueue
);
7220 EXPORT_SYMBOL(md_wakeup_thread
);
7222 struct md_thread
*md_register_thread(void (*run
) (struct md_thread
*),
7223 struct mddev
*mddev
, const char *name
)
7225 struct md_thread
*thread
;
7227 thread
= kzalloc(sizeof(struct md_thread
), GFP_KERNEL
);
7231 init_waitqueue_head(&thread
->wqueue
);
7234 thread
->mddev
= mddev
;
7235 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
7236 thread
->tsk
= kthread_run(md_thread
, thread
,
7238 mdname(thread
->mddev
),
7240 if (IS_ERR(thread
->tsk
)) {
7246 EXPORT_SYMBOL(md_register_thread
);
7248 void md_unregister_thread(struct md_thread
**threadp
)
7250 struct md_thread
*thread
= *threadp
;
7253 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
7254 /* Locking ensures that mddev_unlock does not wake_up a
7255 * non-existent thread
7257 spin_lock(&pers_lock
);
7259 spin_unlock(&pers_lock
);
7261 kthread_stop(thread
->tsk
);
7264 EXPORT_SYMBOL(md_unregister_thread
);
7266 void md_error(struct mddev
*mddev
, struct md_rdev
*rdev
)
7268 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
7271 if (!mddev
->pers
|| !mddev
->pers
->error_handler
)
7273 mddev
->pers
->error_handler(mddev
,rdev
);
7274 if (mddev
->degraded
)
7275 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7276 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
7277 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7278 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7279 md_wakeup_thread(mddev
->thread
);
7280 if (mddev
->event_work
.func
)
7281 queue_work(md_misc_wq
, &mddev
->event_work
);
7282 md_new_event(mddev
);
7284 EXPORT_SYMBOL(md_error
);
7286 /* seq_file implementation /proc/mdstat */
7288 static void status_unused(struct seq_file
*seq
)
7291 struct md_rdev
*rdev
;
7293 seq_printf(seq
, "unused devices: ");
7295 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
7296 char b
[BDEVNAME_SIZE
];
7298 seq_printf(seq
, "%s ",
7299 bdevname(rdev
->bdev
,b
));
7302 seq_printf(seq
, "<none>");
7304 seq_printf(seq
, "\n");
7307 static int status_resync(struct seq_file
*seq
, struct mddev
*mddev
)
7309 sector_t max_sectors
, resync
, res
;
7310 unsigned long dt
, db
;
7313 unsigned int per_milli
;
7315 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
7316 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
7317 max_sectors
= mddev
->resync_max_sectors
;
7319 max_sectors
= mddev
->dev_sectors
;
7321 resync
= mddev
->curr_resync
;
7323 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
7324 /* Still cleaning up */
7325 resync
= max_sectors
;
7327 resync
-= atomic_read(&mddev
->recovery_active
);
7330 if (mddev
->recovery_cp
< MaxSector
) {
7331 seq_printf(seq
, "\tresync=PENDING");
7337 seq_printf(seq
, "\tresync=DELAYED");
7341 WARN_ON(max_sectors
== 0);
7342 /* Pick 'scale' such that (resync>>scale)*1000 will fit
7343 * in a sector_t, and (max_sectors>>scale) will fit in a
7344 * u32, as those are the requirements for sector_div.
7345 * Thus 'scale' must be at least 10
7348 if (sizeof(sector_t
) > sizeof(unsigned long)) {
7349 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
7352 res
= (resync
>>scale
)*1000;
7353 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
7357 int i
, x
= per_milli
/50, y
= 20-x
;
7358 seq_printf(seq
, "[");
7359 for (i
= 0; i
< x
; i
++)
7360 seq_printf(seq
, "=");
7361 seq_printf(seq
, ">");
7362 for (i
= 0; i
< y
; i
++)
7363 seq_printf(seq
, ".");
7364 seq_printf(seq
, "] ");
7366 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
7367 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
7369 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
7371 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
7372 "resync" : "recovery"))),
7373 per_milli
/10, per_milli
% 10,
7374 (unsigned long long) resync
/2,
7375 (unsigned long long) max_sectors
/2);
7378 * dt: time from mark until now
7379 * db: blocks written from mark until now
7380 * rt: remaining time
7382 * rt is a sector_t, so could be 32bit or 64bit.
7383 * So we divide before multiply in case it is 32bit and close
7385 * We scale the divisor (db) by 32 to avoid losing precision
7386 * near the end of resync when the number of remaining sectors
7388 * We then divide rt by 32 after multiplying by db to compensate.
7389 * The '+1' avoids division by zero if db is very small.
7391 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
7393 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
7394 - mddev
->resync_mark_cnt
;
7396 rt
= max_sectors
- resync
; /* number of remaining sectors */
7397 sector_div(rt
, db
/32+1);
7401 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
7402 ((unsigned long)rt
% 60)/6);
7404 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
7408 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
7410 struct list_head
*tmp
;
7412 struct mddev
*mddev
;
7420 spin_lock(&all_mddevs_lock
);
7421 list_for_each(tmp
,&all_mddevs
)
7423 mddev
= list_entry(tmp
, struct mddev
, all_mddevs
);
7425 spin_unlock(&all_mddevs_lock
);
7428 spin_unlock(&all_mddevs_lock
);
7430 return (void*)2;/* tail */
7434 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
7436 struct list_head
*tmp
;
7437 struct mddev
*next_mddev
, *mddev
= v
;
7443 spin_lock(&all_mddevs_lock
);
7445 tmp
= all_mddevs
.next
;
7447 tmp
= mddev
->all_mddevs
.next
;
7448 if (tmp
!= &all_mddevs
)
7449 next_mddev
= mddev_get(list_entry(tmp
,struct mddev
,all_mddevs
));
7451 next_mddev
= (void*)2;
7454 spin_unlock(&all_mddevs_lock
);
7462 static void md_seq_stop(struct seq_file
*seq
, void *v
)
7464 struct mddev
*mddev
= v
;
7466 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
7470 static int md_seq_show(struct seq_file
*seq
, void *v
)
7472 struct mddev
*mddev
= v
;
7474 struct md_rdev
*rdev
;
7476 if (v
== (void*)1) {
7477 struct md_personality
*pers
;
7478 seq_printf(seq
, "Personalities : ");
7479 spin_lock(&pers_lock
);
7480 list_for_each_entry(pers
, &pers_list
, list
)
7481 seq_printf(seq
, "[%s] ", pers
->name
);
7483 spin_unlock(&pers_lock
);
7484 seq_printf(seq
, "\n");
7485 seq
->poll_event
= atomic_read(&md_event_count
);
7488 if (v
== (void*)2) {
7493 spin_lock(&mddev
->lock
);
7494 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
7495 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
7496 mddev
->pers
? "" : "in");
7499 seq_printf(seq
, " (read-only)");
7501 seq_printf(seq
, " (auto-read-only)");
7502 seq_printf(seq
, " %s", mddev
->pers
->name
);
7507 rdev_for_each_rcu(rdev
, mddev
) {
7508 char b
[BDEVNAME_SIZE
];
7509 seq_printf(seq
, " %s[%d]",
7510 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
7511 if (test_bit(WriteMostly
, &rdev
->flags
))
7512 seq_printf(seq
, "(W)");
7513 if (test_bit(Journal
, &rdev
->flags
))
7514 seq_printf(seq
, "(J)");
7515 if (test_bit(Faulty
, &rdev
->flags
)) {
7516 seq_printf(seq
, "(F)");
7519 if (rdev
->raid_disk
< 0)
7520 seq_printf(seq
, "(S)"); /* spare */
7521 if (test_bit(Replacement
, &rdev
->flags
))
7522 seq_printf(seq
, "(R)");
7523 sectors
+= rdev
->sectors
;
7527 if (!list_empty(&mddev
->disks
)) {
7529 seq_printf(seq
, "\n %llu blocks",
7530 (unsigned long long)
7531 mddev
->array_sectors
/ 2);
7533 seq_printf(seq
, "\n %llu blocks",
7534 (unsigned long long)sectors
/ 2);
7536 if (mddev
->persistent
) {
7537 if (mddev
->major_version
!= 0 ||
7538 mddev
->minor_version
!= 90) {
7539 seq_printf(seq
," super %d.%d",
7540 mddev
->major_version
,
7541 mddev
->minor_version
);
7543 } else if (mddev
->external
)
7544 seq_printf(seq
, " super external:%s",
7545 mddev
->metadata_type
);
7547 seq_printf(seq
, " super non-persistent");
7550 mddev
->pers
->status(seq
, mddev
);
7551 seq_printf(seq
, "\n ");
7552 if (mddev
->pers
->sync_request
) {
7553 if (status_resync(seq
, mddev
))
7554 seq_printf(seq
, "\n ");
7557 seq_printf(seq
, "\n ");
7559 bitmap_status(seq
, mddev
->bitmap
);
7561 seq_printf(seq
, "\n");
7563 spin_unlock(&mddev
->lock
);
7568 static const struct seq_operations md_seq_ops
= {
7569 .start
= md_seq_start
,
7570 .next
= md_seq_next
,
7571 .stop
= md_seq_stop
,
7572 .show
= md_seq_show
,
7575 static int md_seq_open(struct inode
*inode
, struct file
*file
)
7577 struct seq_file
*seq
;
7580 error
= seq_open(file
, &md_seq_ops
);
7584 seq
= file
->private_data
;
7585 seq
->poll_event
= atomic_read(&md_event_count
);
7589 static int md_unloading
;
7590 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
7592 struct seq_file
*seq
= filp
->private_data
;
7596 return POLLIN
|POLLRDNORM
|POLLERR
|POLLPRI
;
7597 poll_wait(filp
, &md_event_waiters
, wait
);
7599 /* always allow read */
7600 mask
= POLLIN
| POLLRDNORM
;
7602 if (seq
->poll_event
!= atomic_read(&md_event_count
))
7603 mask
|= POLLERR
| POLLPRI
;
7607 static const struct file_operations md_seq_fops
= {
7608 .owner
= THIS_MODULE
,
7609 .open
= md_seq_open
,
7611 .llseek
= seq_lseek
,
7612 .release
= seq_release_private
,
7613 .poll
= mdstat_poll
,
7616 int register_md_personality(struct md_personality
*p
)
7618 pr_debug("md: %s personality registered for level %d\n",
7620 spin_lock(&pers_lock
);
7621 list_add_tail(&p
->list
, &pers_list
);
7622 spin_unlock(&pers_lock
);
7625 EXPORT_SYMBOL(register_md_personality
);
7627 int unregister_md_personality(struct md_personality
*p
)
7629 pr_debug("md: %s personality unregistered\n", p
->name
);
7630 spin_lock(&pers_lock
);
7631 list_del_init(&p
->list
);
7632 spin_unlock(&pers_lock
);
7635 EXPORT_SYMBOL(unregister_md_personality
);
7637 int register_md_cluster_operations(struct md_cluster_operations
*ops
,
7638 struct module
*module
)
7641 spin_lock(&pers_lock
);
7642 if (md_cluster_ops
!= NULL
)
7645 md_cluster_ops
= ops
;
7646 md_cluster_mod
= module
;
7648 spin_unlock(&pers_lock
);
7651 EXPORT_SYMBOL(register_md_cluster_operations
);
7653 int unregister_md_cluster_operations(void)
7655 spin_lock(&pers_lock
);
7656 md_cluster_ops
= NULL
;
7657 spin_unlock(&pers_lock
);
7660 EXPORT_SYMBOL(unregister_md_cluster_operations
);
7662 int md_setup_cluster(struct mddev
*mddev
, int nodes
)
7664 if (!md_cluster_ops
)
7665 request_module("md-cluster");
7666 spin_lock(&pers_lock
);
7667 /* ensure module won't be unloaded */
7668 if (!md_cluster_ops
|| !try_module_get(md_cluster_mod
)) {
7669 pr_warn("can't find md-cluster module or get it's reference.\n");
7670 spin_unlock(&pers_lock
);
7673 spin_unlock(&pers_lock
);
7675 return md_cluster_ops
->join(mddev
, nodes
);
7678 void md_cluster_stop(struct mddev
*mddev
)
7680 if (!md_cluster_ops
)
7682 md_cluster_ops
->leave(mddev
);
7683 module_put(md_cluster_mod
);
7686 static int is_mddev_idle(struct mddev
*mddev
, int init
)
7688 struct md_rdev
*rdev
;
7694 rdev_for_each_rcu(rdev
, mddev
) {
7695 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
7696 curr_events
= (int)part_stat_read(&disk
->part0
, sectors
[0]) +
7697 (int)part_stat_read(&disk
->part0
, sectors
[1]) -
7698 atomic_read(&disk
->sync_io
);
7699 /* sync IO will cause sync_io to increase before the disk_stats
7700 * as sync_io is counted when a request starts, and
7701 * disk_stats is counted when it completes.
7702 * So resync activity will cause curr_events to be smaller than
7703 * when there was no such activity.
7704 * non-sync IO will cause disk_stat to increase without
7705 * increasing sync_io so curr_events will (eventually)
7706 * be larger than it was before. Once it becomes
7707 * substantially larger, the test below will cause
7708 * the array to appear non-idle, and resync will slow
7710 * If there is a lot of outstanding resync activity when
7711 * we set last_event to curr_events, then all that activity
7712 * completing might cause the array to appear non-idle
7713 * and resync will be slowed down even though there might
7714 * not have been non-resync activity. This will only
7715 * happen once though. 'last_events' will soon reflect
7716 * the state where there is little or no outstanding
7717 * resync requests, and further resync activity will
7718 * always make curr_events less than last_events.
7721 if (init
|| curr_events
- rdev
->last_events
> 64) {
7722 rdev
->last_events
= curr_events
;
7730 void md_done_sync(struct mddev
*mddev
, int blocks
, int ok
)
7732 /* another "blocks" (512byte) blocks have been synced */
7733 atomic_sub(blocks
, &mddev
->recovery_active
);
7734 wake_up(&mddev
->recovery_wait
);
7736 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7737 set_bit(MD_RECOVERY_ERROR
, &mddev
->recovery
);
7738 md_wakeup_thread(mddev
->thread
);
7739 // stop recovery, signal do_sync ....
7742 EXPORT_SYMBOL(md_done_sync
);
7744 /* md_write_start(mddev, bi)
7745 * If we need to update some array metadata (e.g. 'active' flag
7746 * in superblock) before writing, schedule a superblock update
7747 * and wait for it to complete.
7749 void md_write_start(struct mddev
*mddev
, struct bio
*bi
)
7752 if (bio_data_dir(bi
) != WRITE
)
7755 BUG_ON(mddev
->ro
== 1);
7756 if (mddev
->ro
== 2) {
7757 /* need to switch to read/write */
7759 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7760 md_wakeup_thread(mddev
->thread
);
7761 md_wakeup_thread(mddev
->sync_thread
);
7764 atomic_inc(&mddev
->writes_pending
);
7765 if (mddev
->safemode
== 1)
7766 mddev
->safemode
= 0;
7767 if (mddev
->in_sync
) {
7768 spin_lock(&mddev
->lock
);
7769 if (mddev
->in_sync
) {
7771 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
7772 set_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
7773 md_wakeup_thread(mddev
->thread
);
7776 spin_unlock(&mddev
->lock
);
7779 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7780 wait_event(mddev
->sb_wait
,
7781 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
7783 EXPORT_SYMBOL(md_write_start
);
7785 void md_write_end(struct mddev
*mddev
)
7787 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
7788 if (mddev
->safemode
== 2)
7789 md_wakeup_thread(mddev
->thread
);
7790 else if (mddev
->safemode_delay
)
7791 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
7794 EXPORT_SYMBOL(md_write_end
);
7796 /* md_allow_write(mddev)
7797 * Calling this ensures that the array is marked 'active' so that writes
7798 * may proceed without blocking. It is important to call this before
7799 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7800 * Must be called with mddev_lock held.
7802 * In the ->external case MD_SB_CHANGE_PENDING can not be cleared until mddev->lock
7803 * is dropped, so return -EAGAIN after notifying userspace.
7805 int md_allow_write(struct mddev
*mddev
)
7811 if (!mddev
->pers
->sync_request
)
7814 spin_lock(&mddev
->lock
);
7815 if (mddev
->in_sync
) {
7817 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
7818 set_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
7819 if (mddev
->safemode_delay
&&
7820 mddev
->safemode
== 0)
7821 mddev
->safemode
= 1;
7822 spin_unlock(&mddev
->lock
);
7823 md_update_sb(mddev
, 0);
7824 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7826 spin_unlock(&mddev
->lock
);
7828 if (test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
))
7833 EXPORT_SYMBOL_GPL(md_allow_write
);
7835 #define SYNC_MARKS 10
7836 #define SYNC_MARK_STEP (3*HZ)
7837 #define UPDATE_FREQUENCY (5*60*HZ)
7838 void md_do_sync(struct md_thread
*thread
)
7840 struct mddev
*mddev
= thread
->mddev
;
7841 struct mddev
*mddev2
;
7842 unsigned int currspeed
= 0,
7844 sector_t max_sectors
,j
, io_sectors
, recovery_done
;
7845 unsigned long mark
[SYNC_MARKS
];
7846 unsigned long update_time
;
7847 sector_t mark_cnt
[SYNC_MARKS
];
7849 struct list_head
*tmp
;
7850 sector_t last_check
;
7852 struct md_rdev
*rdev
;
7853 char *desc
, *action
= NULL
;
7854 struct blk_plug plug
;
7857 /* just incase thread restarts... */
7858 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
7860 if (mddev
->ro
) {/* never try to sync a read-only array */
7861 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7865 if (mddev_is_clustered(mddev
)) {
7866 ret
= md_cluster_ops
->resync_start(mddev
);
7870 set_bit(MD_CLUSTER_RESYNC_LOCKED
, &mddev
->flags
);
7871 if (!(test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
7872 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) ||
7873 test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
7874 && ((unsigned long long)mddev
->curr_resync_completed
7875 < (unsigned long long)mddev
->resync_max_sectors
))
7879 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
7880 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)) {
7881 desc
= "data-check";
7883 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
7884 desc
= "requested-resync";
7888 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
7893 mddev
->last_sync_action
= action
?: desc
;
7895 /* we overload curr_resync somewhat here.
7896 * 0 == not engaged in resync at all
7897 * 2 == checking that there is no conflict with another sync
7898 * 1 == like 2, but have yielded to allow conflicting resync to
7900 * other == active in resync - this many blocks
7902 * Before starting a resync we must have set curr_resync to
7903 * 2, and then checked that every "conflicting" array has curr_resync
7904 * less than ours. When we find one that is the same or higher
7905 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7906 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7907 * This will mean we have to start checking from the beginning again.
7912 int mddev2_minor
= -1;
7913 mddev
->curr_resync
= 2;
7916 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
7918 for_each_mddev(mddev2
, tmp
) {
7919 if (mddev2
== mddev
)
7921 if (!mddev
->parallel_resync
7922 && mddev2
->curr_resync
7923 && match_mddev_units(mddev
, mddev2
)) {
7925 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
7926 /* arbitrarily yield */
7927 mddev
->curr_resync
= 1;
7928 wake_up(&resync_wait
);
7930 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
7931 /* no need to wait here, we can wait the next
7932 * time 'round when curr_resync == 2
7935 /* We need to wait 'interruptible' so as not to
7936 * contribute to the load average, and not to
7937 * be caught by 'softlockup'
7939 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
7940 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
7941 mddev2
->curr_resync
>= mddev
->curr_resync
) {
7942 if (mddev2_minor
!= mddev2
->md_minor
) {
7943 mddev2_minor
= mddev2
->md_minor
;
7944 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
7945 desc
, mdname(mddev
),
7949 if (signal_pending(current
))
7950 flush_signals(current
);
7952 finish_wait(&resync_wait
, &wq
);
7955 finish_wait(&resync_wait
, &wq
);
7958 } while (mddev
->curr_resync
< 2);
7961 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
7962 /* resync follows the size requested by the personality,
7963 * which defaults to physical size, but can be virtual size
7965 max_sectors
= mddev
->resync_max_sectors
;
7966 atomic64_set(&mddev
->resync_mismatches
, 0);
7967 /* we don't use the checkpoint if there's a bitmap */
7968 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7969 j
= mddev
->resync_min
;
7970 else if (!mddev
->bitmap
)
7971 j
= mddev
->recovery_cp
;
7973 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
7974 max_sectors
= mddev
->resync_max_sectors
;
7976 /* recovery follows the physical size of devices */
7977 max_sectors
= mddev
->dev_sectors
;
7980 rdev_for_each_rcu(rdev
, mddev
)
7981 if (rdev
->raid_disk
>= 0 &&
7982 !test_bit(Journal
, &rdev
->flags
) &&
7983 !test_bit(Faulty
, &rdev
->flags
) &&
7984 !test_bit(In_sync
, &rdev
->flags
) &&
7985 rdev
->recovery_offset
< j
)
7986 j
= rdev
->recovery_offset
;
7989 /* If there is a bitmap, we need to make sure all
7990 * writes that started before we added a spare
7991 * complete before we start doing a recovery.
7992 * Otherwise the write might complete and (via
7993 * bitmap_endwrite) set a bit in the bitmap after the
7994 * recovery has checked that bit and skipped that
7997 if (mddev
->bitmap
) {
7998 mddev
->pers
->quiesce(mddev
, 1);
7999 mddev
->pers
->quiesce(mddev
, 0);
8003 pr_info("md: %s of RAID array %s\n", desc
, mdname(mddev
));
8004 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev
));
8005 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8006 speed_max(mddev
), desc
);
8008 is_mddev_idle(mddev
, 1); /* this initializes IO event counters */
8011 for (m
= 0; m
< SYNC_MARKS
; m
++) {
8013 mark_cnt
[m
] = io_sectors
;
8016 mddev
->resync_mark
= mark
[last_mark
];
8017 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
8020 * Tune reconstruction:
8022 window
= 32*(PAGE_SIZE
/512);
8023 pr_debug("md: using %dk window, over a total of %lluk.\n",
8024 window
/2, (unsigned long long)max_sectors
/2);
8026 atomic_set(&mddev
->recovery_active
, 0);
8030 pr_debug("md: resuming %s of %s from checkpoint.\n",
8031 desc
, mdname(mddev
));
8032 mddev
->curr_resync
= j
;
8034 mddev
->curr_resync
= 3; /* no longer delayed */
8035 mddev
->curr_resync_completed
= j
;
8036 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
8037 md_new_event(mddev
);
8038 update_time
= jiffies
;
8040 blk_start_plug(&plug
);
8041 while (j
< max_sectors
) {
8046 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8047 ((mddev
->curr_resync
> mddev
->curr_resync_completed
&&
8048 (mddev
->curr_resync
- mddev
->curr_resync_completed
)
8049 > (max_sectors
>> 4)) ||
8050 time_after_eq(jiffies
, update_time
+ UPDATE_FREQUENCY
) ||
8051 (j
- mddev
->curr_resync_completed
)*2
8052 >= mddev
->resync_max
- mddev
->curr_resync_completed
||
8053 mddev
->curr_resync_completed
> mddev
->resync_max
8055 /* time to update curr_resync_completed */
8056 wait_event(mddev
->recovery_wait
,
8057 atomic_read(&mddev
->recovery_active
) == 0);
8058 mddev
->curr_resync_completed
= j
;
8059 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) &&
8060 j
> mddev
->recovery_cp
)
8061 mddev
->recovery_cp
= j
;
8062 update_time
= jiffies
;
8063 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
8064 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
8067 while (j
>= mddev
->resync_max
&&
8068 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
8069 /* As this condition is controlled by user-space,
8070 * we can block indefinitely, so use '_interruptible'
8071 * to avoid triggering warnings.
8073 flush_signals(current
); /* just in case */
8074 wait_event_interruptible(mddev
->recovery_wait
,
8075 mddev
->resync_max
> j
8076 || test_bit(MD_RECOVERY_INTR
,
8080 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8083 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
);
8085 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8089 if (!skipped
) { /* actual IO requested */
8090 io_sectors
+= sectors
;
8091 atomic_add(sectors
, &mddev
->recovery_active
);
8094 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8098 if (j
> max_sectors
)
8099 /* when skipping, extra large numbers can be returned. */
8102 mddev
->curr_resync
= j
;
8103 mddev
->curr_mark_cnt
= io_sectors
;
8104 if (last_check
== 0)
8105 /* this is the earliest that rebuild will be
8106 * visible in /proc/mdstat
8108 md_new_event(mddev
);
8110 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
8113 last_check
= io_sectors
;
8115 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
8117 int next
= (last_mark
+1) % SYNC_MARKS
;
8119 mddev
->resync_mark
= mark
[next
];
8120 mddev
->resync_mark_cnt
= mark_cnt
[next
];
8121 mark
[next
] = jiffies
;
8122 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
8126 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8130 * this loop exits only if either when we are slower than
8131 * the 'hard' speed limit, or the system was IO-idle for
8133 * the system might be non-idle CPU-wise, but we only care
8134 * about not overloading the IO subsystem. (things like an
8135 * e2fsck being done on the RAID array should execute fast)
8139 recovery_done
= io_sectors
- atomic_read(&mddev
->recovery_active
);
8140 currspeed
= ((unsigned long)(recovery_done
- mddev
->resync_mark_cnt
))/2
8141 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
8143 if (currspeed
> speed_min(mddev
)) {
8144 if (currspeed
> speed_max(mddev
)) {
8148 if (!is_mddev_idle(mddev
, 0)) {
8150 * Give other IO more of a chance.
8151 * The faster the devices, the less we wait.
8153 wait_event(mddev
->recovery_wait
,
8154 !atomic_read(&mddev
->recovery_active
));
8158 pr_info("md: %s: %s %s.\n",mdname(mddev
), desc
,
8159 test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)
8160 ? "interrupted" : "done");
8162 * this also signals 'finished resyncing' to md_stop
8164 blk_finish_plug(&plug
);
8165 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
8167 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8168 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
8169 mddev
->curr_resync
> 3) {
8170 mddev
->curr_resync_completed
= mddev
->curr_resync
;
8171 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
8173 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
);
8175 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
8176 mddev
->curr_resync
> 3) {
8177 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
8178 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
8179 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
8180 pr_debug("md: checkpointing %s of %s.\n",
8181 desc
, mdname(mddev
));
8182 if (test_bit(MD_RECOVERY_ERROR
,
8184 mddev
->recovery_cp
=
8185 mddev
->curr_resync_completed
;
8187 mddev
->recovery_cp
=
8191 mddev
->recovery_cp
= MaxSector
;
8193 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8194 mddev
->curr_resync
= MaxSector
;
8196 rdev_for_each_rcu(rdev
, mddev
)
8197 if (rdev
->raid_disk
>= 0 &&
8198 mddev
->delta_disks
>= 0 &&
8199 !test_bit(Journal
, &rdev
->flags
) &&
8200 !test_bit(Faulty
, &rdev
->flags
) &&
8201 !test_bit(In_sync
, &rdev
->flags
) &&
8202 rdev
->recovery_offset
< mddev
->curr_resync
)
8203 rdev
->recovery_offset
= mddev
->curr_resync
;
8208 /* set CHANGE_PENDING here since maybe another update is needed,
8209 * so other nodes are informed. It should be harmless for normal
8211 set_mask_bits(&mddev
->sb_flags
, 0,
8212 BIT(MD_SB_CHANGE_PENDING
) | BIT(MD_SB_CHANGE_DEVS
));
8214 spin_lock(&mddev
->lock
);
8215 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
8216 /* We completed so min/max setting can be forgotten if used. */
8217 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
8218 mddev
->resync_min
= 0;
8219 mddev
->resync_max
= MaxSector
;
8220 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
8221 mddev
->resync_min
= mddev
->curr_resync_completed
;
8222 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
8223 mddev
->curr_resync
= 0;
8224 spin_unlock(&mddev
->lock
);
8226 wake_up(&resync_wait
);
8227 md_wakeup_thread(mddev
->thread
);
8230 EXPORT_SYMBOL_GPL(md_do_sync
);
8232 static int remove_and_add_spares(struct mddev
*mddev
,
8233 struct md_rdev
*this)
8235 struct md_rdev
*rdev
;
8238 bool remove_some
= false;
8240 rdev_for_each(rdev
, mddev
) {
8241 if ((this == NULL
|| rdev
== this) &&
8242 rdev
->raid_disk
>= 0 &&
8243 !test_bit(Blocked
, &rdev
->flags
) &&
8244 test_bit(Faulty
, &rdev
->flags
) &&
8245 atomic_read(&rdev
->nr_pending
)==0) {
8246 /* Faulty non-Blocked devices with nr_pending == 0
8247 * never get nr_pending incremented,
8248 * never get Faulty cleared, and never get Blocked set.
8249 * So we can synchronize_rcu now rather than once per device
8252 set_bit(RemoveSynchronized
, &rdev
->flags
);
8258 rdev_for_each(rdev
, mddev
) {
8259 if ((this == NULL
|| rdev
== this) &&
8260 rdev
->raid_disk
>= 0 &&
8261 !test_bit(Blocked
, &rdev
->flags
) &&
8262 ((test_bit(RemoveSynchronized
, &rdev
->flags
) ||
8263 (!test_bit(In_sync
, &rdev
->flags
) &&
8264 !test_bit(Journal
, &rdev
->flags
))) &&
8265 atomic_read(&rdev
->nr_pending
)==0)) {
8266 if (mddev
->pers
->hot_remove_disk(
8267 mddev
, rdev
) == 0) {
8268 sysfs_unlink_rdev(mddev
, rdev
);
8269 rdev
->raid_disk
= -1;
8273 if (remove_some
&& test_bit(RemoveSynchronized
, &rdev
->flags
))
8274 clear_bit(RemoveSynchronized
, &rdev
->flags
);
8277 if (removed
&& mddev
->kobj
.sd
)
8278 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
8280 if (this && removed
)
8283 rdev_for_each(rdev
, mddev
) {
8284 if (this && this != rdev
)
8286 if (test_bit(Candidate
, &rdev
->flags
))
8288 if (rdev
->raid_disk
>= 0 &&
8289 !test_bit(In_sync
, &rdev
->flags
) &&
8290 !test_bit(Journal
, &rdev
->flags
) &&
8291 !test_bit(Faulty
, &rdev
->flags
))
8293 if (rdev
->raid_disk
>= 0)
8295 if (test_bit(Faulty
, &rdev
->flags
))
8297 if (!test_bit(Journal
, &rdev
->flags
)) {
8299 ! (rdev
->saved_raid_disk
>= 0 &&
8300 !test_bit(Bitmap_sync
, &rdev
->flags
)))
8303 rdev
->recovery_offset
= 0;
8306 hot_add_disk(mddev
, rdev
) == 0) {
8307 if (sysfs_link_rdev(mddev
, rdev
))
8308 /* failure here is OK */;
8309 if (!test_bit(Journal
, &rdev
->flags
))
8311 md_new_event(mddev
);
8312 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
8317 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
8321 static void md_start_sync(struct work_struct
*ws
)
8323 struct mddev
*mddev
= container_of(ws
, struct mddev
, del_work
);
8325 mddev
->sync_thread
= md_register_thread(md_do_sync
,
8328 if (!mddev
->sync_thread
) {
8329 pr_warn("%s: could not start resync thread...\n",
8331 /* leave the spares where they are, it shouldn't hurt */
8332 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
8333 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
8334 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
8335 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
8336 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
8337 wake_up(&resync_wait
);
8338 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
8340 if (mddev
->sysfs_action
)
8341 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
8343 md_wakeup_thread(mddev
->sync_thread
);
8344 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
8345 md_new_event(mddev
);
8349 * This routine is regularly called by all per-raid-array threads to
8350 * deal with generic issues like resync and super-block update.
8351 * Raid personalities that don't have a thread (linear/raid0) do not
8352 * need this as they never do any recovery or update the superblock.
8354 * It does not do any resync itself, but rather "forks" off other threads
8355 * to do that as needed.
8356 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8357 * "->recovery" and create a thread at ->sync_thread.
8358 * When the thread finishes it sets MD_RECOVERY_DONE
8359 * and wakeups up this thread which will reap the thread and finish up.
8360 * This thread also removes any faulty devices (with nr_pending == 0).
8362 * The overall approach is:
8363 * 1/ if the superblock needs updating, update it.
8364 * 2/ If a recovery thread is running, don't do anything else.
8365 * 3/ If recovery has finished, clean up, possibly marking spares active.
8366 * 4/ If there are any faulty devices, remove them.
8367 * 5/ If array is degraded, try to add spares devices
8368 * 6/ If array has spares or is not in-sync, start a resync thread.
8370 void md_check_recovery(struct mddev
*mddev
)
8372 if (mddev
->suspended
)
8376 bitmap_daemon_work(mddev
);
8378 if (signal_pending(current
)) {
8379 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
8380 pr_debug("md: %s in immediate safe mode\n",
8382 mddev
->safemode
= 2;
8384 flush_signals(current
);
8387 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
8390 (mddev
->sb_flags
& ~ (1<<MD_SB_CHANGE_PENDING
)) ||
8391 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
8392 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
8393 test_bit(MD_RELOAD_SB
, &mddev
->flags
) ||
8394 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
8395 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
8396 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
8400 if (mddev_trylock(mddev
)) {
8404 struct md_rdev
*rdev
;
8405 if (!mddev
->external
&& mddev
->in_sync
)
8406 /* 'Blocked' flag not needed as failed devices
8407 * will be recorded if array switched to read/write.
8408 * Leaving it set will prevent the device
8409 * from being removed.
8411 rdev_for_each(rdev
, mddev
)
8412 clear_bit(Blocked
, &rdev
->flags
);
8413 /* On a read-only array we can:
8414 * - remove failed devices
8415 * - add already-in_sync devices if the array itself
8417 * As we only add devices that are already in-sync,
8418 * we can activate the spares immediately.
8420 remove_and_add_spares(mddev
, NULL
);
8421 /* There is no thread, but we need to call
8422 * ->spare_active and clear saved_raid_disk
8424 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8425 md_reap_sync_thread(mddev
);
8426 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8427 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8428 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
8432 if (mddev_is_clustered(mddev
)) {
8433 struct md_rdev
*rdev
;
8434 /* kick the device if another node issued a
8437 rdev_for_each(rdev
, mddev
) {
8438 if (test_and_clear_bit(ClusterRemove
, &rdev
->flags
) &&
8439 rdev
->raid_disk
< 0)
8440 md_kick_rdev_from_array(rdev
);
8443 if (test_and_clear_bit(MD_RELOAD_SB
, &mddev
->flags
))
8444 md_reload_sb(mddev
, mddev
->good_device_nr
);
8447 if (!mddev
->external
) {
8449 spin_lock(&mddev
->lock
);
8450 if (mddev
->safemode
&&
8451 !atomic_read(&mddev
->writes_pending
) &&
8453 mddev
->recovery_cp
== MaxSector
) {
8456 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
8458 if (mddev
->safemode
== 1)
8459 mddev
->safemode
= 0;
8460 spin_unlock(&mddev
->lock
);
8462 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
8465 if (mddev
->sb_flags
)
8466 md_update_sb(mddev
, 0);
8468 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
8469 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
8470 /* resync/recovery still happening */
8471 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8474 if (mddev
->sync_thread
) {
8475 md_reap_sync_thread(mddev
);
8478 /* Set RUNNING before clearing NEEDED to avoid
8479 * any transients in the value of "sync_action".
8481 mddev
->curr_resync_completed
= 0;
8482 spin_lock(&mddev
->lock
);
8483 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
8484 spin_unlock(&mddev
->lock
);
8485 /* Clear some bits that don't mean anything, but
8488 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8489 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
8491 if (!test_and_clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
8492 test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
8494 /* no recovery is running.
8495 * remove any failed drives, then
8496 * add spares if possible.
8497 * Spares are also removed and re-added, to allow
8498 * the personality to fail the re-add.
8501 if (mddev
->reshape_position
!= MaxSector
) {
8502 if (mddev
->pers
->check_reshape
== NULL
||
8503 mddev
->pers
->check_reshape(mddev
) != 0)
8504 /* Cannot proceed */
8506 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
8507 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8508 } else if ((spares
= remove_and_add_spares(mddev
, NULL
))) {
8509 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
8510 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
8511 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
8512 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8513 } else if (mddev
->recovery_cp
< MaxSector
) {
8514 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
8515 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8516 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
8517 /* nothing to be done ... */
8520 if (mddev
->pers
->sync_request
) {
8522 /* We are adding a device or devices to an array
8523 * which has the bitmap stored on all devices.
8524 * So make sure all bitmap pages get written
8526 bitmap_write_all(mddev
->bitmap
);
8528 INIT_WORK(&mddev
->del_work
, md_start_sync
);
8529 queue_work(md_misc_wq
, &mddev
->del_work
);
8533 if (!mddev
->sync_thread
) {
8534 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
8535 wake_up(&resync_wait
);
8536 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
8538 if (mddev
->sysfs_action
)
8539 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
8542 wake_up(&mddev
->sb_wait
);
8543 mddev_unlock(mddev
);
8546 EXPORT_SYMBOL(md_check_recovery
);
8548 void md_reap_sync_thread(struct mddev
*mddev
)
8550 struct md_rdev
*rdev
;
8552 /* resync has finished, collect result */
8553 md_unregister_thread(&mddev
->sync_thread
);
8554 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
8555 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
8557 /* activate any spares */
8558 if (mddev
->pers
->spare_active(mddev
)) {
8559 sysfs_notify(&mddev
->kobj
, NULL
,
8561 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
8564 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8565 mddev
->pers
->finish_reshape
)
8566 mddev
->pers
->finish_reshape(mddev
);
8568 /* If array is no-longer degraded, then any saved_raid_disk
8569 * information must be scrapped.
8571 if (!mddev
->degraded
)
8572 rdev_for_each(rdev
, mddev
)
8573 rdev
->saved_raid_disk
= -1;
8575 md_update_sb(mddev
, 1);
8576 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
8577 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
8579 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED
, &mddev
->flags
))
8580 md_cluster_ops
->resync_finish(mddev
);
8581 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
8582 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
8583 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
8584 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
8585 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
8586 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
8587 wake_up(&resync_wait
);
8588 /* flag recovery needed just to double check */
8589 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8590 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
8591 md_new_event(mddev
);
8592 if (mddev
->event_work
.func
)
8593 queue_work(md_misc_wq
, &mddev
->event_work
);
8595 EXPORT_SYMBOL(md_reap_sync_thread
);
8597 void md_wait_for_blocked_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
8599 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
8600 wait_event_timeout(rdev
->blocked_wait
,
8601 !test_bit(Blocked
, &rdev
->flags
) &&
8602 !test_bit(BlockedBadBlocks
, &rdev
->flags
),
8603 msecs_to_jiffies(5000));
8604 rdev_dec_pending(rdev
, mddev
);
8606 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
8608 void md_finish_reshape(struct mddev
*mddev
)
8610 /* called be personality module when reshape completes. */
8611 struct md_rdev
*rdev
;
8613 rdev_for_each(rdev
, mddev
) {
8614 if (rdev
->data_offset
> rdev
->new_data_offset
)
8615 rdev
->sectors
+= rdev
->data_offset
- rdev
->new_data_offset
;
8617 rdev
->sectors
-= rdev
->new_data_offset
- rdev
->data_offset
;
8618 rdev
->data_offset
= rdev
->new_data_offset
;
8621 EXPORT_SYMBOL(md_finish_reshape
);
8623 /* Bad block management */
8625 /* Returns 1 on success, 0 on failure */
8626 int rdev_set_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
,
8629 struct mddev
*mddev
= rdev
->mddev
;
8632 s
+= rdev
->new_data_offset
;
8634 s
+= rdev
->data_offset
;
8635 rv
= badblocks_set(&rdev
->badblocks
, s
, sectors
, 0);
8637 /* Make sure they get written out promptly */
8638 if (test_bit(ExternalBbl
, &rdev
->flags
))
8639 sysfs_notify(&rdev
->kobj
, NULL
,
8640 "unacknowledged_bad_blocks");
8641 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
8642 set_mask_bits(&mddev
->sb_flags
, 0,
8643 BIT(MD_SB_CHANGE_CLEAN
) | BIT(MD_SB_CHANGE_PENDING
));
8644 md_wakeup_thread(rdev
->mddev
->thread
);
8649 EXPORT_SYMBOL_GPL(rdev_set_badblocks
);
8651 int rdev_clear_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
,
8656 s
+= rdev
->new_data_offset
;
8658 s
+= rdev
->data_offset
;
8659 rv
= badblocks_clear(&rdev
->badblocks
, s
, sectors
);
8660 if ((rv
== 0) && test_bit(ExternalBbl
, &rdev
->flags
))
8661 sysfs_notify(&rdev
->kobj
, NULL
, "bad_blocks");
8664 EXPORT_SYMBOL_GPL(rdev_clear_badblocks
);
8666 static int md_notify_reboot(struct notifier_block
*this,
8667 unsigned long code
, void *x
)
8669 struct list_head
*tmp
;
8670 struct mddev
*mddev
;
8673 for_each_mddev(mddev
, tmp
) {
8674 if (mddev_trylock(mddev
)) {
8676 __md_stop_writes(mddev
);
8677 if (mddev
->persistent
)
8678 mddev
->safemode
= 2;
8679 mddev_unlock(mddev
);
8684 * certain more exotic SCSI devices are known to be
8685 * volatile wrt too early system reboots. While the
8686 * right place to handle this issue is the given
8687 * driver, we do want to have a safe RAID driver ...
8695 static struct notifier_block md_notifier
= {
8696 .notifier_call
= md_notify_reboot
,
8698 .priority
= INT_MAX
, /* before any real devices */
8701 static void md_geninit(void)
8703 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
8705 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
8708 static int __init
md_init(void)
8712 md_wq
= alloc_workqueue("md", WQ_MEM_RECLAIM
, 0);
8716 md_misc_wq
= alloc_workqueue("md_misc", 0, 0);
8720 if ((ret
= register_blkdev(MD_MAJOR
, "md")) < 0)
8723 if ((ret
= register_blkdev(0, "mdp")) < 0)
8727 blk_register_region(MKDEV(MD_MAJOR
, 0), 512, THIS_MODULE
,
8728 md_probe
, NULL
, NULL
);
8729 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
8730 md_probe
, NULL
, NULL
);
8732 register_reboot_notifier(&md_notifier
);
8733 raid_table_header
= register_sysctl_table(raid_root_table
);
8739 unregister_blkdev(MD_MAJOR
, "md");
8741 destroy_workqueue(md_misc_wq
);
8743 destroy_workqueue(md_wq
);
8748 static void check_sb_changes(struct mddev
*mddev
, struct md_rdev
*rdev
)
8750 struct mdp_superblock_1
*sb
= page_address(rdev
->sb_page
);
8751 struct md_rdev
*rdev2
;
8753 char b
[BDEVNAME_SIZE
];
8755 /* Check for change of roles in the active devices */
8756 rdev_for_each(rdev2
, mddev
) {
8757 if (test_bit(Faulty
, &rdev2
->flags
))
8760 /* Check if the roles changed */
8761 role
= le16_to_cpu(sb
->dev_roles
[rdev2
->desc_nr
]);
8763 if (test_bit(Candidate
, &rdev2
->flags
)) {
8764 if (role
== 0xfffe) {
8765 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2
->bdev
,b
));
8766 md_kick_rdev_from_array(rdev2
);
8770 clear_bit(Candidate
, &rdev2
->flags
);
8773 if (role
!= rdev2
->raid_disk
) {
8775 if (rdev2
->raid_disk
== -1 && role
!= 0xffff) {
8776 rdev2
->saved_raid_disk
= role
;
8777 ret
= remove_and_add_spares(mddev
, rdev2
);
8778 pr_info("Activated spare: %s\n",
8779 bdevname(rdev2
->bdev
,b
));
8780 /* wakeup mddev->thread here, so array could
8781 * perform resync with the new activated disk */
8782 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8783 md_wakeup_thread(mddev
->thread
);
8787 * We just want to do the minimum to mark the disk
8788 * as faulty. The recovery is performed by the
8789 * one who initiated the error.
8791 if ((role
== 0xfffe) || (role
== 0xfffd)) {
8792 md_error(mddev
, rdev2
);
8793 clear_bit(Blocked
, &rdev2
->flags
);
8798 if (mddev
->raid_disks
!= le32_to_cpu(sb
->raid_disks
))
8799 update_raid_disks(mddev
, le32_to_cpu(sb
->raid_disks
));
8801 /* Finally set the event to be up to date */
8802 mddev
->events
= le64_to_cpu(sb
->events
);
8805 static int read_rdev(struct mddev
*mddev
, struct md_rdev
*rdev
)
8808 struct page
*swapout
= rdev
->sb_page
;
8809 struct mdp_superblock_1
*sb
;
8811 /* Store the sb page of the rdev in the swapout temporary
8812 * variable in case we err in the future
8814 rdev
->sb_page
= NULL
;
8815 err
= alloc_disk_sb(rdev
);
8817 ClearPageUptodate(rdev
->sb_page
);
8818 rdev
->sb_loaded
= 0;
8819 err
= super_types
[mddev
->major_version
].
8820 load_super(rdev
, NULL
, mddev
->minor_version
);
8823 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
8824 __func__
, __LINE__
, rdev
->desc_nr
, err
);
8826 put_page(rdev
->sb_page
);
8827 rdev
->sb_page
= swapout
;
8828 rdev
->sb_loaded
= 1;
8832 sb
= page_address(rdev
->sb_page
);
8833 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
8837 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RECOVERY_OFFSET
))
8838 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
8840 /* The other node finished recovery, call spare_active to set
8841 * device In_sync and mddev->degraded
8843 if (rdev
->recovery_offset
== MaxSector
&&
8844 !test_bit(In_sync
, &rdev
->flags
) &&
8845 mddev
->pers
->spare_active(mddev
))
8846 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
8852 void md_reload_sb(struct mddev
*mddev
, int nr
)
8854 struct md_rdev
*rdev
;
8858 rdev_for_each_rcu(rdev
, mddev
) {
8859 if (rdev
->desc_nr
== nr
)
8863 if (!rdev
|| rdev
->desc_nr
!= nr
) {
8864 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__
, __LINE__
, nr
);
8868 err
= read_rdev(mddev
, rdev
);
8872 check_sb_changes(mddev
, rdev
);
8874 /* Read all rdev's to update recovery_offset */
8875 rdev_for_each_rcu(rdev
, mddev
)
8876 read_rdev(mddev
, rdev
);
8878 EXPORT_SYMBOL(md_reload_sb
);
8883 * Searches all registered partitions for autorun RAID arrays
8887 static DEFINE_MUTEX(detected_devices_mutex
);
8888 static LIST_HEAD(all_detected_devices
);
8889 struct detected_devices_node
{
8890 struct list_head list
;
8894 void md_autodetect_dev(dev_t dev
)
8896 struct detected_devices_node
*node_detected_dev
;
8898 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
8899 if (node_detected_dev
) {
8900 node_detected_dev
->dev
= dev
;
8901 mutex_lock(&detected_devices_mutex
);
8902 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
8903 mutex_unlock(&detected_devices_mutex
);
8907 static void autostart_arrays(int part
)
8909 struct md_rdev
*rdev
;
8910 struct detected_devices_node
*node_detected_dev
;
8912 int i_scanned
, i_passed
;
8917 pr_info("md: Autodetecting RAID arrays.\n");
8919 mutex_lock(&detected_devices_mutex
);
8920 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
8922 node_detected_dev
= list_entry(all_detected_devices
.next
,
8923 struct detected_devices_node
, list
);
8924 list_del(&node_detected_dev
->list
);
8925 dev
= node_detected_dev
->dev
;
8926 kfree(node_detected_dev
);
8927 mutex_unlock(&detected_devices_mutex
);
8928 rdev
= md_import_device(dev
,0, 90);
8929 mutex_lock(&detected_devices_mutex
);
8933 if (test_bit(Faulty
, &rdev
->flags
))
8936 set_bit(AutoDetected
, &rdev
->flags
);
8937 list_add(&rdev
->same_set
, &pending_raid_disks
);
8940 mutex_unlock(&detected_devices_mutex
);
8942 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned
, i_passed
);
8944 autorun_devices(part
);
8947 #endif /* !MODULE */
8949 static __exit
void md_exit(void)
8951 struct mddev
*mddev
;
8952 struct list_head
*tmp
;
8955 blk_unregister_region(MKDEV(MD_MAJOR
,0), 512);
8956 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
8958 unregister_blkdev(MD_MAJOR
,"md");
8959 unregister_blkdev(mdp_major
, "mdp");
8960 unregister_reboot_notifier(&md_notifier
);
8961 unregister_sysctl_table(raid_table_header
);
8963 /* We cannot unload the modules while some process is
8964 * waiting for us in select() or poll() - wake them up
8967 while (waitqueue_active(&md_event_waiters
)) {
8968 /* not safe to leave yet */
8969 wake_up(&md_event_waiters
);
8973 remove_proc_entry("mdstat", NULL
);
8975 for_each_mddev(mddev
, tmp
) {
8976 export_array(mddev
);
8978 mddev
->hold_active
= 0;
8980 * for_each_mddev() will call mddev_put() at the end of each
8981 * iteration. As the mddev is now fully clear, this will
8982 * schedule the mddev for destruction by a workqueue, and the
8983 * destroy_workqueue() below will wait for that to complete.
8986 destroy_workqueue(md_misc_wq
);
8987 destroy_workqueue(md_wq
);
8990 subsys_initcall(md_init
);
8991 module_exit(md_exit
)
8993 static int get_ro(char *buffer
, struct kernel_param
*kp
)
8995 return sprintf(buffer
, "%d", start_readonly
);
8997 static int set_ro(const char *val
, struct kernel_param
*kp
)
8999 return kstrtouint(val
, 10, (unsigned int *)&start_readonly
);
9002 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
9003 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
9004 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
9006 MODULE_LICENSE("GPL");
9007 MODULE_DESCRIPTION("MD RAID framework");
9009 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);