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/kthread.h>
48 #include <linux/blkdev.h>
49 #include <linux/badblocks.h>
50 #include <linux/sysctl.h>
51 #include <linux/seq_file.h>
53 #include <linux/poll.h>
54 #include <linux/ctype.h>
55 #include <linux/string.h>
56 #include <linux/hdreg.h>
57 #include <linux/proc_fs.h>
58 #include <linux/random.h>
59 #include <linux/module.h>
60 #include <linux/reboot.h>
61 #include <linux/file.h>
62 #include <linux/compat.h>
63 #include <linux/delay.h>
64 #include <linux/raid/md_p.h>
65 #include <linux/raid/md_u.h>
66 #include <linux/slab.h>
67 #include <trace/events/block.h>
70 #include "md-cluster.h"
73 static void autostart_arrays(int part
);
76 /* pers_list is a list of registered personalities protected
78 * pers_lock does extra service to protect accesses to
79 * mddev->thread when the mutex cannot be held.
81 static LIST_HEAD(pers_list
);
82 static DEFINE_SPINLOCK(pers_lock
);
84 struct md_cluster_operations
*md_cluster_ops
;
85 EXPORT_SYMBOL(md_cluster_ops
);
86 struct module
*md_cluster_mod
;
87 EXPORT_SYMBOL(md_cluster_mod
);
89 static DECLARE_WAIT_QUEUE_HEAD(resync_wait
);
90 static struct workqueue_struct
*md_wq
;
91 static struct workqueue_struct
*md_misc_wq
;
93 static int remove_and_add_spares(struct mddev
*mddev
,
94 struct md_rdev
*this);
95 static void mddev_detach(struct mddev
*mddev
);
98 * Default number of read corrections we'll attempt on an rdev
99 * before ejecting it from the array. We divide the read error
100 * count by 2 for every hour elapsed between read errors.
102 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
104 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
105 * is 1000 KB/sec, so the extra system load does not show up that much.
106 * Increase it if you want to have more _guaranteed_ speed. Note that
107 * the RAID driver will use the maximum available bandwidth if the IO
108 * subsystem is idle. There is also an 'absolute maximum' reconstruction
109 * speed limit - in case reconstruction slows down your system despite
112 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
113 * or /sys/block/mdX/md/sync_speed_{min,max}
116 static int sysctl_speed_limit_min
= 1000;
117 static int sysctl_speed_limit_max
= 200000;
118 static inline int speed_min(struct mddev
*mddev
)
120 return mddev
->sync_speed_min
?
121 mddev
->sync_speed_min
: sysctl_speed_limit_min
;
124 static inline int speed_max(struct mddev
*mddev
)
126 return mddev
->sync_speed_max
?
127 mddev
->sync_speed_max
: sysctl_speed_limit_max
;
130 static struct ctl_table_header
*raid_table_header
;
132 static struct ctl_table raid_table
[] = {
134 .procname
= "speed_limit_min",
135 .data
= &sysctl_speed_limit_min
,
136 .maxlen
= sizeof(int),
137 .mode
= S_IRUGO
|S_IWUSR
,
138 .proc_handler
= proc_dointvec
,
141 .procname
= "speed_limit_max",
142 .data
= &sysctl_speed_limit_max
,
143 .maxlen
= sizeof(int),
144 .mode
= S_IRUGO
|S_IWUSR
,
145 .proc_handler
= proc_dointvec
,
150 static struct ctl_table raid_dir_table
[] = {
154 .mode
= S_IRUGO
|S_IXUGO
,
160 static struct ctl_table raid_root_table
[] = {
165 .child
= raid_dir_table
,
170 static const struct block_device_operations md_fops
;
172 static int start_readonly
;
175 * like bio_clone, but with a local bio set
178 struct bio
*bio_alloc_mddev(gfp_t gfp_mask
, int nr_iovecs
,
183 if (!mddev
|| !mddev
->bio_set
)
184 return bio_alloc(gfp_mask
, nr_iovecs
);
186 b
= bio_alloc_bioset(gfp_mask
, nr_iovecs
, mddev
->bio_set
);
191 EXPORT_SYMBOL_GPL(bio_alloc_mddev
);
193 struct bio
*bio_clone_mddev(struct bio
*bio
, gfp_t gfp_mask
,
196 if (!mddev
|| !mddev
->bio_set
)
197 return bio_clone(bio
, gfp_mask
);
199 return bio_clone_bioset(bio
, gfp_mask
, mddev
->bio_set
);
201 EXPORT_SYMBOL_GPL(bio_clone_mddev
);
204 * We have a system wide 'event count' that is incremented
205 * on any 'interesting' event, and readers of /proc/mdstat
206 * can use 'poll' or 'select' to find out when the event
210 * start array, stop array, error, add device, remove device,
211 * start build, activate spare
213 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters
);
214 static atomic_t md_event_count
;
215 void md_new_event(struct mddev
*mddev
)
217 atomic_inc(&md_event_count
);
218 wake_up(&md_event_waiters
);
220 EXPORT_SYMBOL_GPL(md_new_event
);
223 * Enables to iterate over all existing md arrays
224 * all_mddevs_lock protects this list.
226 static LIST_HEAD(all_mddevs
);
227 static DEFINE_SPINLOCK(all_mddevs_lock
);
230 * iterates through all used mddevs in the system.
231 * We take care to grab the all_mddevs_lock whenever navigating
232 * the list, and to always hold a refcount when unlocked.
233 * Any code which breaks out of this loop while own
234 * a reference to the current mddev and must mddev_put it.
236 #define for_each_mddev(_mddev,_tmp) \
238 for (({ spin_lock(&all_mddevs_lock); \
239 _tmp = all_mddevs.next; \
241 ({ if (_tmp != &all_mddevs) \
242 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
243 spin_unlock(&all_mddevs_lock); \
244 if (_mddev) mddev_put(_mddev); \
245 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
246 _tmp != &all_mddevs;}); \
247 ({ spin_lock(&all_mddevs_lock); \
248 _tmp = _tmp->next;}) \
251 /* Rather than calling directly into the personality make_request function,
252 * IO requests come here first so that we can check if the device is
253 * being suspended pending a reconfiguration.
254 * We hold a refcount over the call to ->make_request. By the time that
255 * call has finished, the bio has been linked into some internal structure
256 * and so is visible to ->quiesce(), so we don't need the refcount any more.
258 static blk_qc_t
md_make_request(struct request_queue
*q
, struct bio
*bio
)
260 const int rw
= bio_data_dir(bio
);
261 struct mddev
*mddev
= q
->queuedata
;
262 unsigned int sectors
;
265 blk_queue_split(q
, &bio
, q
->bio_split
);
267 if (mddev
== NULL
|| mddev
->pers
== NULL
) {
269 return BLK_QC_T_NONE
;
271 if (mddev
->ro
== 1 && unlikely(rw
== WRITE
)) {
272 if (bio_sectors(bio
) != 0)
273 bio
->bi_error
= -EROFS
;
275 return BLK_QC_T_NONE
;
277 smp_rmb(); /* Ensure implications of 'active' are visible */
279 if (mddev
->suspended
) {
282 prepare_to_wait(&mddev
->sb_wait
, &__wait
,
283 TASK_UNINTERRUPTIBLE
);
284 if (!mddev
->suspended
)
290 finish_wait(&mddev
->sb_wait
, &__wait
);
292 atomic_inc(&mddev
->active_io
);
296 * save the sectors now since our bio can
297 * go away inside make_request
299 sectors
= bio_sectors(bio
);
300 /* bio could be mergeable after passing to underlayer */
301 bio
->bi_opf
&= ~REQ_NOMERGE
;
302 mddev
->pers
->make_request(mddev
, bio
);
304 cpu
= part_stat_lock();
305 part_stat_inc(cpu
, &mddev
->gendisk
->part0
, ios
[rw
]);
306 part_stat_add(cpu
, &mddev
->gendisk
->part0
, sectors
[rw
], sectors
);
309 if (atomic_dec_and_test(&mddev
->active_io
) && mddev
->suspended
)
310 wake_up(&mddev
->sb_wait
);
312 return BLK_QC_T_NONE
;
315 /* mddev_suspend makes sure no new requests are submitted
316 * to the device, and that any requests that have been submitted
317 * are completely handled.
318 * Once mddev_detach() is called and completes, the module will be
321 void mddev_suspend(struct mddev
*mddev
)
323 WARN_ON_ONCE(mddev
->thread
&& current
== mddev
->thread
->tsk
);
324 if (mddev
->suspended
++)
327 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->active_io
) == 0);
328 mddev
->pers
->quiesce(mddev
, 1);
330 del_timer_sync(&mddev
->safemode_timer
);
332 EXPORT_SYMBOL_GPL(mddev_suspend
);
334 void mddev_resume(struct mddev
*mddev
)
336 if (--mddev
->suspended
)
338 wake_up(&mddev
->sb_wait
);
339 mddev
->pers
->quiesce(mddev
, 0);
341 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
342 md_wakeup_thread(mddev
->thread
);
343 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
345 EXPORT_SYMBOL_GPL(mddev_resume
);
347 int mddev_congested(struct mddev
*mddev
, int bits
)
349 struct md_personality
*pers
= mddev
->pers
;
353 if (mddev
->suspended
)
355 else if (pers
&& pers
->congested
)
356 ret
= pers
->congested(mddev
, bits
);
360 EXPORT_SYMBOL_GPL(mddev_congested
);
361 static int md_congested(void *data
, int bits
)
363 struct mddev
*mddev
= data
;
364 return mddev_congested(mddev
, bits
);
368 * Generic flush handling for md
371 static void md_end_flush(struct bio
*bio
)
373 struct md_rdev
*rdev
= bio
->bi_private
;
374 struct mddev
*mddev
= rdev
->mddev
;
376 rdev_dec_pending(rdev
, mddev
);
378 if (atomic_dec_and_test(&mddev
->flush_pending
)) {
379 /* The pre-request flush has finished */
380 queue_work(md_wq
, &mddev
->flush_work
);
385 static void md_submit_flush_data(struct work_struct
*ws
);
387 static void submit_flushes(struct work_struct
*ws
)
389 struct mddev
*mddev
= container_of(ws
, struct mddev
, flush_work
);
390 struct md_rdev
*rdev
;
392 INIT_WORK(&mddev
->flush_work
, md_submit_flush_data
);
393 atomic_set(&mddev
->flush_pending
, 1);
395 rdev_for_each_rcu(rdev
, mddev
)
396 if (rdev
->raid_disk
>= 0 &&
397 !test_bit(Faulty
, &rdev
->flags
)) {
398 /* Take two references, one is dropped
399 * when request finishes, one after
400 * we reclaim rcu_read_lock
403 atomic_inc(&rdev
->nr_pending
);
404 atomic_inc(&rdev
->nr_pending
);
406 bi
= bio_alloc_mddev(GFP_NOIO
, 0, mddev
);
407 bi
->bi_end_io
= md_end_flush
;
408 bi
->bi_private
= rdev
;
409 bi
->bi_bdev
= rdev
->bdev
;
410 bi
->bi_opf
= REQ_OP_WRITE
| REQ_PREFLUSH
;
411 atomic_inc(&mddev
->flush_pending
);
414 rdev_dec_pending(rdev
, mddev
);
417 if (atomic_dec_and_test(&mddev
->flush_pending
))
418 queue_work(md_wq
, &mddev
->flush_work
);
421 static void md_submit_flush_data(struct work_struct
*ws
)
423 struct mddev
*mddev
= container_of(ws
, struct mddev
, flush_work
);
424 struct bio
*bio
= mddev
->flush_bio
;
426 if (bio
->bi_iter
.bi_size
== 0)
427 /* an empty barrier - all done */
430 bio
->bi_opf
&= ~REQ_PREFLUSH
;
431 mddev
->pers
->make_request(mddev
, bio
);
434 mddev
->flush_bio
= NULL
;
435 wake_up(&mddev
->sb_wait
);
438 void md_flush_request(struct mddev
*mddev
, struct bio
*bio
)
440 spin_lock_irq(&mddev
->lock
);
441 wait_event_lock_irq(mddev
->sb_wait
,
444 mddev
->flush_bio
= bio
;
445 spin_unlock_irq(&mddev
->lock
);
447 INIT_WORK(&mddev
->flush_work
, submit_flushes
);
448 queue_work(md_wq
, &mddev
->flush_work
);
450 EXPORT_SYMBOL(md_flush_request
);
452 void md_unplug(struct blk_plug_cb
*cb
, bool from_schedule
)
454 struct mddev
*mddev
= cb
->data
;
455 md_wakeup_thread(mddev
->thread
);
458 EXPORT_SYMBOL(md_unplug
);
460 static inline struct mddev
*mddev_get(struct mddev
*mddev
)
462 atomic_inc(&mddev
->active
);
466 static void mddev_delayed_delete(struct work_struct
*ws
);
468 static void mddev_put(struct mddev
*mddev
)
470 struct bio_set
*bs
= NULL
;
472 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
474 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
) &&
475 mddev
->ctime
== 0 && !mddev
->hold_active
) {
476 /* Array is not configured at all, and not held active,
478 list_del_init(&mddev
->all_mddevs
);
480 mddev
->bio_set
= NULL
;
481 if (mddev
->gendisk
) {
482 /* We did a probe so need to clean up. Call
483 * queue_work inside the spinlock so that
484 * flush_workqueue() after mddev_find will
485 * succeed in waiting for the work to be done.
487 INIT_WORK(&mddev
->del_work
, mddev_delayed_delete
);
488 queue_work(md_misc_wq
, &mddev
->del_work
);
492 spin_unlock(&all_mddevs_lock
);
497 static void md_safemode_timeout(unsigned long data
);
499 void mddev_init(struct mddev
*mddev
)
501 mutex_init(&mddev
->open_mutex
);
502 mutex_init(&mddev
->reconfig_mutex
);
503 mutex_init(&mddev
->bitmap_info
.mutex
);
504 INIT_LIST_HEAD(&mddev
->disks
);
505 INIT_LIST_HEAD(&mddev
->all_mddevs
);
506 setup_timer(&mddev
->safemode_timer
, md_safemode_timeout
,
507 (unsigned long) mddev
);
508 atomic_set(&mddev
->active
, 1);
509 atomic_set(&mddev
->openers
, 0);
510 atomic_set(&mddev
->active_io
, 0);
511 spin_lock_init(&mddev
->lock
);
512 atomic_set(&mddev
->flush_pending
, 0);
513 init_waitqueue_head(&mddev
->sb_wait
);
514 init_waitqueue_head(&mddev
->recovery_wait
);
515 mddev
->reshape_position
= MaxSector
;
516 mddev
->reshape_backwards
= 0;
517 mddev
->last_sync_action
= "none";
518 mddev
->resync_min
= 0;
519 mddev
->resync_max
= MaxSector
;
520 mddev
->level
= LEVEL_NONE
;
522 EXPORT_SYMBOL_GPL(mddev_init
);
524 static struct mddev
*mddev_find(dev_t unit
)
526 struct mddev
*mddev
, *new = NULL
;
528 if (unit
&& MAJOR(unit
) != MD_MAJOR
)
529 unit
&= ~((1<<MdpMinorShift
)-1);
532 spin_lock(&all_mddevs_lock
);
535 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
536 if (mddev
->unit
== unit
) {
538 spin_unlock(&all_mddevs_lock
);
544 list_add(&new->all_mddevs
, &all_mddevs
);
545 spin_unlock(&all_mddevs_lock
);
546 new->hold_active
= UNTIL_IOCTL
;
550 /* find an unused unit number */
551 static int next_minor
= 512;
552 int start
= next_minor
;
556 dev
= MKDEV(MD_MAJOR
, next_minor
);
558 if (next_minor
> MINORMASK
)
560 if (next_minor
== start
) {
561 /* Oh dear, all in use. */
562 spin_unlock(&all_mddevs_lock
);
568 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
569 if (mddev
->unit
== dev
) {
575 new->md_minor
= MINOR(dev
);
576 new->hold_active
= UNTIL_STOP
;
577 list_add(&new->all_mddevs
, &all_mddevs
);
578 spin_unlock(&all_mddevs_lock
);
581 spin_unlock(&all_mddevs_lock
);
583 new = kzalloc(sizeof(*new), GFP_KERNEL
);
588 if (MAJOR(unit
) == MD_MAJOR
)
589 new->md_minor
= MINOR(unit
);
591 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
598 static struct attribute_group md_redundancy_group
;
600 void mddev_unlock(struct mddev
*mddev
)
602 if (mddev
->to_remove
) {
603 /* These cannot be removed under reconfig_mutex as
604 * an access to the files will try to take reconfig_mutex
605 * while holding the file unremovable, which leads to
607 * So hold set sysfs_active while the remove in happeing,
608 * and anything else which might set ->to_remove or my
609 * otherwise change the sysfs namespace will fail with
610 * -EBUSY if sysfs_active is still set.
611 * We set sysfs_active under reconfig_mutex and elsewhere
612 * test it under the same mutex to ensure its correct value
615 struct attribute_group
*to_remove
= mddev
->to_remove
;
616 mddev
->to_remove
= NULL
;
617 mddev
->sysfs_active
= 1;
618 mutex_unlock(&mddev
->reconfig_mutex
);
620 if (mddev
->kobj
.sd
) {
621 if (to_remove
!= &md_redundancy_group
)
622 sysfs_remove_group(&mddev
->kobj
, to_remove
);
623 if (mddev
->pers
== NULL
||
624 mddev
->pers
->sync_request
== NULL
) {
625 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
626 if (mddev
->sysfs_action
)
627 sysfs_put(mddev
->sysfs_action
);
628 mddev
->sysfs_action
= NULL
;
631 mddev
->sysfs_active
= 0;
633 mutex_unlock(&mddev
->reconfig_mutex
);
635 /* As we've dropped the mutex we need a spinlock to
636 * make sure the thread doesn't disappear
638 spin_lock(&pers_lock
);
639 md_wakeup_thread(mddev
->thread
);
640 spin_unlock(&pers_lock
);
642 EXPORT_SYMBOL_GPL(mddev_unlock
);
644 struct md_rdev
*md_find_rdev_nr_rcu(struct mddev
*mddev
, int nr
)
646 struct md_rdev
*rdev
;
648 rdev_for_each_rcu(rdev
, mddev
)
649 if (rdev
->desc_nr
== nr
)
654 EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu
);
656 static struct md_rdev
*find_rdev(struct mddev
*mddev
, dev_t dev
)
658 struct md_rdev
*rdev
;
660 rdev_for_each(rdev
, mddev
)
661 if (rdev
->bdev
->bd_dev
== dev
)
667 static struct md_rdev
*find_rdev_rcu(struct mddev
*mddev
, dev_t dev
)
669 struct md_rdev
*rdev
;
671 rdev_for_each_rcu(rdev
, mddev
)
672 if (rdev
->bdev
->bd_dev
== dev
)
678 static struct md_personality
*find_pers(int level
, char *clevel
)
680 struct md_personality
*pers
;
681 list_for_each_entry(pers
, &pers_list
, list
) {
682 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
684 if (strcmp(pers
->name
, clevel
)==0)
690 /* return the offset of the super block in 512byte sectors */
691 static inline sector_t
calc_dev_sboffset(struct md_rdev
*rdev
)
693 sector_t num_sectors
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
694 return MD_NEW_SIZE_SECTORS(num_sectors
);
697 static int alloc_disk_sb(struct md_rdev
*rdev
)
699 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
705 void md_rdev_clear(struct md_rdev
*rdev
)
708 put_page(rdev
->sb_page
);
710 rdev
->sb_page
= NULL
;
715 put_page(rdev
->bb_page
);
716 rdev
->bb_page
= NULL
;
718 badblocks_exit(&rdev
->badblocks
);
720 EXPORT_SYMBOL_GPL(md_rdev_clear
);
722 static void super_written(struct bio
*bio
)
724 struct md_rdev
*rdev
= bio
->bi_private
;
725 struct mddev
*mddev
= rdev
->mddev
;
728 pr_err("md: super_written gets error=%d\n", bio
->bi_error
);
729 md_error(mddev
, rdev
);
730 if (!test_bit(Faulty
, &rdev
->flags
)
731 && (bio
->bi_opf
& MD_FAILFAST
)) {
732 set_bit(MD_SB_NEED_REWRITE
, &mddev
->sb_flags
);
733 set_bit(LastDev
, &rdev
->flags
);
736 clear_bit(LastDev
, &rdev
->flags
);
738 if (atomic_dec_and_test(&mddev
->pending_writes
))
739 wake_up(&mddev
->sb_wait
);
740 rdev_dec_pending(rdev
, mddev
);
744 void md_super_write(struct mddev
*mddev
, struct md_rdev
*rdev
,
745 sector_t sector
, int size
, struct page
*page
)
747 /* write first size bytes of page to sector of rdev
748 * Increment mddev->pending_writes before returning
749 * and decrement it on completion, waking up sb_wait
750 * if zero is reached.
751 * If an error occurred, call md_error
756 if (test_bit(Faulty
, &rdev
->flags
))
759 bio
= bio_alloc_mddev(GFP_NOIO
, 1, mddev
);
761 atomic_inc(&rdev
->nr_pending
);
763 bio
->bi_bdev
= rdev
->meta_bdev
? rdev
->meta_bdev
: rdev
->bdev
;
764 bio
->bi_iter
.bi_sector
= sector
;
765 bio_add_page(bio
, page
, size
, 0);
766 bio
->bi_private
= rdev
;
767 bio
->bi_end_io
= super_written
;
769 if (test_bit(MD_FAILFAST_SUPPORTED
, &mddev
->flags
) &&
770 test_bit(FailFast
, &rdev
->flags
) &&
771 !test_bit(LastDev
, &rdev
->flags
))
773 bio
->bi_opf
= REQ_OP_WRITE
| REQ_PREFLUSH
| REQ_FUA
| ff
;
775 atomic_inc(&mddev
->pending_writes
);
779 int md_super_wait(struct mddev
*mddev
)
781 /* wait for all superblock writes that were scheduled to complete */
782 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->pending_writes
)==0);
783 if (test_and_clear_bit(MD_SB_NEED_REWRITE
, &mddev
->sb_flags
))
788 int sync_page_io(struct md_rdev
*rdev
, sector_t sector
, int size
,
789 struct page
*page
, int op
, int op_flags
, bool metadata_op
)
791 struct bio
*bio
= bio_alloc_mddev(GFP_NOIO
, 1, rdev
->mddev
);
794 bio
->bi_bdev
= (metadata_op
&& rdev
->meta_bdev
) ?
795 rdev
->meta_bdev
: rdev
->bdev
;
796 bio_set_op_attrs(bio
, op
, op_flags
);
798 bio
->bi_iter
.bi_sector
= sector
+ rdev
->sb_start
;
799 else if (rdev
->mddev
->reshape_position
!= MaxSector
&&
800 (rdev
->mddev
->reshape_backwards
==
801 (sector
>= rdev
->mddev
->reshape_position
)))
802 bio
->bi_iter
.bi_sector
= sector
+ rdev
->new_data_offset
;
804 bio
->bi_iter
.bi_sector
= sector
+ rdev
->data_offset
;
805 bio_add_page(bio
, page
, size
, 0);
807 submit_bio_wait(bio
);
809 ret
= !bio
->bi_error
;
813 EXPORT_SYMBOL_GPL(sync_page_io
);
815 static int read_disk_sb(struct md_rdev
*rdev
, int size
)
817 char b
[BDEVNAME_SIZE
];
822 if (!sync_page_io(rdev
, 0, size
, rdev
->sb_page
, REQ_OP_READ
, 0, true))
828 pr_err("md: disabled device %s, could not read superblock.\n",
829 bdevname(rdev
->bdev
,b
));
833 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
835 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
836 sb1
->set_uuid1
== sb2
->set_uuid1
&&
837 sb1
->set_uuid2
== sb2
->set_uuid2
&&
838 sb1
->set_uuid3
== sb2
->set_uuid3
;
841 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
844 mdp_super_t
*tmp1
, *tmp2
;
846 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
847 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
849 if (!tmp1
|| !tmp2
) {
858 * nr_disks is not constant
863 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
870 static u32
md_csum_fold(u32 csum
)
872 csum
= (csum
& 0xffff) + (csum
>> 16);
873 return (csum
& 0xffff) + (csum
>> 16);
876 static unsigned int calc_sb_csum(mdp_super_t
*sb
)
879 u32
*sb32
= (u32
*)sb
;
881 unsigned int disk_csum
, csum
;
883 disk_csum
= sb
->sb_csum
;
886 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
888 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
891 /* This used to use csum_partial, which was wrong for several
892 * reasons including that different results are returned on
893 * different architectures. It isn't critical that we get exactly
894 * the same return value as before (we always csum_fold before
895 * testing, and that removes any differences). However as we
896 * know that csum_partial always returned a 16bit value on
897 * alphas, do a fold to maximise conformity to previous behaviour.
899 sb
->sb_csum
= md_csum_fold(disk_csum
);
901 sb
->sb_csum
= disk_csum
;
907 * Handle superblock details.
908 * We want to be able to handle multiple superblock formats
909 * so we have a common interface to them all, and an array of
910 * different handlers.
911 * We rely on user-space to write the initial superblock, and support
912 * reading and updating of superblocks.
913 * Interface methods are:
914 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
915 * loads and validates a superblock on dev.
916 * if refdev != NULL, compare superblocks on both devices
918 * 0 - dev has a superblock that is compatible with refdev
919 * 1 - dev has a superblock that is compatible and newer than refdev
920 * so dev should be used as the refdev in future
921 * -EINVAL superblock incompatible or invalid
922 * -othererror e.g. -EIO
924 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
925 * Verify that dev is acceptable into mddev.
926 * The first time, mddev->raid_disks will be 0, and data from
927 * dev should be merged in. Subsequent calls check that dev
928 * is new enough. Return 0 or -EINVAL
930 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
931 * Update the superblock for rdev with data in mddev
932 * This does not write to disc.
938 struct module
*owner
;
939 int (*load_super
)(struct md_rdev
*rdev
,
940 struct md_rdev
*refdev
,
942 int (*validate_super
)(struct mddev
*mddev
,
943 struct md_rdev
*rdev
);
944 void (*sync_super
)(struct mddev
*mddev
,
945 struct md_rdev
*rdev
);
946 unsigned long long (*rdev_size_change
)(struct md_rdev
*rdev
,
947 sector_t num_sectors
);
948 int (*allow_new_offset
)(struct md_rdev
*rdev
,
949 unsigned long long new_offset
);
953 * Check that the given mddev has no bitmap.
955 * This function is called from the run method of all personalities that do not
956 * support bitmaps. It prints an error message and returns non-zero if mddev
957 * has a bitmap. Otherwise, it returns 0.
960 int md_check_no_bitmap(struct mddev
*mddev
)
962 if (!mddev
->bitmap_info
.file
&& !mddev
->bitmap_info
.offset
)
964 pr_warn("%s: bitmaps are not supported for %s\n",
965 mdname(mddev
), mddev
->pers
->name
);
968 EXPORT_SYMBOL(md_check_no_bitmap
);
971 * load_super for 0.90.0
973 static int super_90_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
975 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
980 * Calculate the position of the superblock (512byte sectors),
981 * it's at the end of the disk.
983 * It also happens to be a multiple of 4Kb.
985 rdev
->sb_start
= calc_dev_sboffset(rdev
);
987 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
993 bdevname(rdev
->bdev
, b
);
994 sb
= page_address(rdev
->sb_page
);
996 if (sb
->md_magic
!= MD_SB_MAGIC
) {
997 pr_warn("md: invalid raid superblock magic on %s\n", b
);
1001 if (sb
->major_version
!= 0 ||
1002 sb
->minor_version
< 90 ||
1003 sb
->minor_version
> 91) {
1004 pr_warn("Bad version number %d.%d on %s\n",
1005 sb
->major_version
, sb
->minor_version
, b
);
1009 if (sb
->raid_disks
<= 0)
1012 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
1013 pr_warn("md: invalid superblock checksum on %s\n", b
);
1017 rdev
->preferred_minor
= sb
->md_minor
;
1018 rdev
->data_offset
= 0;
1019 rdev
->new_data_offset
= 0;
1020 rdev
->sb_size
= MD_SB_BYTES
;
1021 rdev
->badblocks
.shift
= -1;
1023 if (sb
->level
== LEVEL_MULTIPATH
)
1026 rdev
->desc_nr
= sb
->this_disk
.number
;
1032 mdp_super_t
*refsb
= page_address(refdev
->sb_page
);
1033 if (!uuid_equal(refsb
, sb
)) {
1034 pr_warn("md: %s has different UUID to %s\n",
1035 b
, bdevname(refdev
->bdev
,b2
));
1038 if (!sb_equal(refsb
, sb
)) {
1039 pr_warn("md: %s has same UUID but different superblock to %s\n",
1040 b
, bdevname(refdev
->bdev
, b2
));
1044 ev2
= md_event(refsb
);
1050 rdev
->sectors
= rdev
->sb_start
;
1051 /* Limit to 4TB as metadata cannot record more than that.
1052 * (not needed for Linear and RAID0 as metadata doesn't
1055 if (IS_ENABLED(CONFIG_LBDAF
) && (u64
)rdev
->sectors
>= (2ULL << 32) &&
1057 rdev
->sectors
= (sector_t
)(2ULL << 32) - 2;
1059 if (rdev
->sectors
< ((sector_t
)sb
->size
) * 2 && sb
->level
>= 1)
1060 /* "this cannot possibly happen" ... */
1068 * validate_super for 0.90.0
1070 static int super_90_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
1073 mdp_super_t
*sb
= page_address(rdev
->sb_page
);
1074 __u64 ev1
= md_event(sb
);
1076 rdev
->raid_disk
= -1;
1077 clear_bit(Faulty
, &rdev
->flags
);
1078 clear_bit(In_sync
, &rdev
->flags
);
1079 clear_bit(Bitmap_sync
, &rdev
->flags
);
1080 clear_bit(WriteMostly
, &rdev
->flags
);
1082 if (mddev
->raid_disks
== 0) {
1083 mddev
->major_version
= 0;
1084 mddev
->minor_version
= sb
->minor_version
;
1085 mddev
->patch_version
= sb
->patch_version
;
1086 mddev
->external
= 0;
1087 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
1088 mddev
->ctime
= sb
->ctime
;
1089 mddev
->utime
= sb
->utime
;
1090 mddev
->level
= sb
->level
;
1091 mddev
->clevel
[0] = 0;
1092 mddev
->layout
= sb
->layout
;
1093 mddev
->raid_disks
= sb
->raid_disks
;
1094 mddev
->dev_sectors
= ((sector_t
)sb
->size
) * 2;
1095 mddev
->events
= ev1
;
1096 mddev
->bitmap_info
.offset
= 0;
1097 mddev
->bitmap_info
.space
= 0;
1098 /* bitmap can use 60 K after the 4K superblocks */
1099 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
1100 mddev
->bitmap_info
.default_space
= 64*2 - (MD_SB_BYTES
>> 9);
1101 mddev
->reshape_backwards
= 0;
1103 if (mddev
->minor_version
>= 91) {
1104 mddev
->reshape_position
= sb
->reshape_position
;
1105 mddev
->delta_disks
= sb
->delta_disks
;
1106 mddev
->new_level
= sb
->new_level
;
1107 mddev
->new_layout
= sb
->new_layout
;
1108 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
1109 if (mddev
->delta_disks
< 0)
1110 mddev
->reshape_backwards
= 1;
1112 mddev
->reshape_position
= MaxSector
;
1113 mddev
->delta_disks
= 0;
1114 mddev
->new_level
= mddev
->level
;
1115 mddev
->new_layout
= mddev
->layout
;
1116 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1119 if (sb
->state
& (1<<MD_SB_CLEAN
))
1120 mddev
->recovery_cp
= MaxSector
;
1122 if (sb
->events_hi
== sb
->cp_events_hi
&&
1123 sb
->events_lo
== sb
->cp_events_lo
) {
1124 mddev
->recovery_cp
= sb
->recovery_cp
;
1126 mddev
->recovery_cp
= 0;
1129 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
1130 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
1131 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
1132 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
1134 mddev
->max_disks
= MD_SB_DISKS
;
1136 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
1137 mddev
->bitmap_info
.file
== NULL
) {
1138 mddev
->bitmap_info
.offset
=
1139 mddev
->bitmap_info
.default_offset
;
1140 mddev
->bitmap_info
.space
=
1141 mddev
->bitmap_info
.default_space
;
1144 } else if (mddev
->pers
== NULL
) {
1145 /* Insist on good event counter while assembling, except
1146 * for spares (which don't need an event count) */
1148 if (sb
->disks
[rdev
->desc_nr
].state
& (
1149 (1<<MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
)))
1150 if (ev1
< mddev
->events
)
1152 } else if (mddev
->bitmap
) {
1153 /* if adding to array with a bitmap, then we can accept an
1154 * older device ... but not too old.
1156 if (ev1
< mddev
->bitmap
->events_cleared
)
1158 if (ev1
< mddev
->events
)
1159 set_bit(Bitmap_sync
, &rdev
->flags
);
1161 if (ev1
< mddev
->events
)
1162 /* just a hot-add of a new device, leave raid_disk at -1 */
1166 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1167 desc
= sb
->disks
+ rdev
->desc_nr
;
1169 if (desc
->state
& (1<<MD_DISK_FAULTY
))
1170 set_bit(Faulty
, &rdev
->flags
);
1171 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
1172 desc->raid_disk < mddev->raid_disks */) {
1173 set_bit(In_sync
, &rdev
->flags
);
1174 rdev
->raid_disk
= desc
->raid_disk
;
1175 rdev
->saved_raid_disk
= desc
->raid_disk
;
1176 } else if (desc
->state
& (1<<MD_DISK_ACTIVE
)) {
1177 /* active but not in sync implies recovery up to
1178 * reshape position. We don't know exactly where
1179 * that is, so set to zero for now */
1180 if (mddev
->minor_version
>= 91) {
1181 rdev
->recovery_offset
= 0;
1182 rdev
->raid_disk
= desc
->raid_disk
;
1185 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
1186 set_bit(WriteMostly
, &rdev
->flags
);
1187 if (desc
->state
& (1<<MD_DISK_FAILFAST
))
1188 set_bit(FailFast
, &rdev
->flags
);
1189 } else /* MULTIPATH are always insync */
1190 set_bit(In_sync
, &rdev
->flags
);
1195 * sync_super for 0.90.0
1197 static void super_90_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
1200 struct md_rdev
*rdev2
;
1201 int next_spare
= mddev
->raid_disks
;
1203 /* make rdev->sb match mddev data..
1206 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1207 * 3/ any empty disks < next_spare become removed
1209 * disks[0] gets initialised to REMOVED because
1210 * we cannot be sure from other fields if it has
1211 * been initialised or not.
1214 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
1216 rdev
->sb_size
= MD_SB_BYTES
;
1218 sb
= page_address(rdev
->sb_page
);
1220 memset(sb
, 0, sizeof(*sb
));
1222 sb
->md_magic
= MD_SB_MAGIC
;
1223 sb
->major_version
= mddev
->major_version
;
1224 sb
->patch_version
= mddev
->patch_version
;
1225 sb
->gvalid_words
= 0; /* ignored */
1226 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
1227 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
1228 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
1229 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
1231 sb
->ctime
= clamp_t(time64_t
, mddev
->ctime
, 0, U32_MAX
);
1232 sb
->level
= mddev
->level
;
1233 sb
->size
= mddev
->dev_sectors
/ 2;
1234 sb
->raid_disks
= mddev
->raid_disks
;
1235 sb
->md_minor
= mddev
->md_minor
;
1236 sb
->not_persistent
= 0;
1237 sb
->utime
= clamp_t(time64_t
, mddev
->utime
, 0, U32_MAX
);
1239 sb
->events_hi
= (mddev
->events
>>32);
1240 sb
->events_lo
= (u32
)mddev
->events
;
1242 if (mddev
->reshape_position
== MaxSector
)
1243 sb
->minor_version
= 90;
1245 sb
->minor_version
= 91;
1246 sb
->reshape_position
= mddev
->reshape_position
;
1247 sb
->new_level
= mddev
->new_level
;
1248 sb
->delta_disks
= mddev
->delta_disks
;
1249 sb
->new_layout
= mddev
->new_layout
;
1250 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1252 mddev
->minor_version
= sb
->minor_version
;
1255 sb
->recovery_cp
= mddev
->recovery_cp
;
1256 sb
->cp_events_hi
= (mddev
->events
>>32);
1257 sb
->cp_events_lo
= (u32
)mddev
->events
;
1258 if (mddev
->recovery_cp
== MaxSector
)
1259 sb
->state
= (1<< MD_SB_CLEAN
);
1261 sb
->recovery_cp
= 0;
1263 sb
->layout
= mddev
->layout
;
1264 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1266 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
)
1267 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1269 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1270 rdev_for_each(rdev2
, mddev
) {
1273 int is_active
= test_bit(In_sync
, &rdev2
->flags
);
1275 if (rdev2
->raid_disk
>= 0 &&
1276 sb
->minor_version
>= 91)
1277 /* we have nowhere to store the recovery_offset,
1278 * but if it is not below the reshape_position,
1279 * we can piggy-back on that.
1282 if (rdev2
->raid_disk
< 0 ||
1283 test_bit(Faulty
, &rdev2
->flags
))
1286 desc_nr
= rdev2
->raid_disk
;
1288 desc_nr
= next_spare
++;
1289 rdev2
->desc_nr
= desc_nr
;
1290 d
= &sb
->disks
[rdev2
->desc_nr
];
1292 d
->number
= rdev2
->desc_nr
;
1293 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1294 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1296 d
->raid_disk
= rdev2
->raid_disk
;
1298 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1299 if (test_bit(Faulty
, &rdev2
->flags
))
1300 d
->state
= (1<<MD_DISK_FAULTY
);
1301 else if (is_active
) {
1302 d
->state
= (1<<MD_DISK_ACTIVE
);
1303 if (test_bit(In_sync
, &rdev2
->flags
))
1304 d
->state
|= (1<<MD_DISK_SYNC
);
1312 if (test_bit(WriteMostly
, &rdev2
->flags
))
1313 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1314 if (test_bit(FailFast
, &rdev2
->flags
))
1315 d
->state
|= (1<<MD_DISK_FAILFAST
);
1317 /* now set the "removed" and "faulty" bits on any missing devices */
1318 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1319 mdp_disk_t
*d
= &sb
->disks
[i
];
1320 if (d
->state
== 0 && d
->number
== 0) {
1323 d
->state
= (1<<MD_DISK_REMOVED
);
1324 d
->state
|= (1<<MD_DISK_FAULTY
);
1328 sb
->nr_disks
= nr_disks
;
1329 sb
->active_disks
= active
;
1330 sb
->working_disks
= working
;
1331 sb
->failed_disks
= failed
;
1332 sb
->spare_disks
= spare
;
1334 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1335 sb
->sb_csum
= calc_sb_csum(sb
);
1339 * rdev_size_change for 0.90.0
1341 static unsigned long long
1342 super_90_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
1344 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1345 return 0; /* component must fit device */
1346 if (rdev
->mddev
->bitmap_info
.offset
)
1347 return 0; /* can't move bitmap */
1348 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1349 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1350 num_sectors
= rdev
->sb_start
;
1351 /* Limit to 4TB as metadata cannot record more than that.
1352 * 4TB == 2^32 KB, or 2*2^32 sectors.
1354 if (IS_ENABLED(CONFIG_LBDAF
) && (u64
)num_sectors
>= (2ULL << 32) &&
1355 rdev
->mddev
->level
>= 1)
1356 num_sectors
= (sector_t
)(2ULL << 32) - 2;
1358 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1360 } while (md_super_wait(rdev
->mddev
) < 0);
1365 super_90_allow_new_offset(struct md_rdev
*rdev
, unsigned long long new_offset
)
1367 /* non-zero offset changes not possible with v0.90 */
1368 return new_offset
== 0;
1372 * version 1 superblock
1375 static __le32
calc_sb_1_csum(struct mdp_superblock_1
*sb
)
1379 unsigned long long newcsum
;
1380 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1381 __le32
*isuper
= (__le32
*)sb
;
1383 disk_csum
= sb
->sb_csum
;
1386 for (; size
>= 4; size
-= 4)
1387 newcsum
+= le32_to_cpu(*isuper
++);
1390 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1392 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1393 sb
->sb_csum
= disk_csum
;
1394 return cpu_to_le32(csum
);
1397 static int super_1_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
1399 struct mdp_superblock_1
*sb
;
1403 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1407 * Calculate the position of the superblock in 512byte sectors.
1408 * It is always aligned to a 4K boundary and
1409 * depeding on minor_version, it can be:
1410 * 0: At least 8K, but less than 12K, from end of device
1411 * 1: At start of device
1412 * 2: 4K from start of device.
1414 switch(minor_version
) {
1416 sb_start
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1418 sb_start
&= ~(sector_t
)(4*2-1);
1429 rdev
->sb_start
= sb_start
;
1431 /* superblock is rarely larger than 1K, but it can be larger,
1432 * and it is safe to read 4k, so we do that
1434 ret
= read_disk_sb(rdev
, 4096);
1435 if (ret
) return ret
;
1437 sb
= page_address(rdev
->sb_page
);
1439 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1440 sb
->major_version
!= cpu_to_le32(1) ||
1441 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1442 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1443 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1446 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1447 pr_warn("md: invalid superblock checksum on %s\n",
1448 bdevname(rdev
->bdev
,b
));
1451 if (le64_to_cpu(sb
->data_size
) < 10) {
1452 pr_warn("md: data_size too small on %s\n",
1453 bdevname(rdev
->bdev
,b
));
1458 memcmp(sb
->pad3
, sb
->pad3
+1, sizeof(sb
->pad3
) - sizeof(sb
->pad3
[1])))
1459 /* Some padding is non-zero, might be a new feature */
1462 rdev
->preferred_minor
= 0xffff;
1463 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1464 rdev
->new_data_offset
= rdev
->data_offset
;
1465 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
) &&
1466 (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_NEW_OFFSET
))
1467 rdev
->new_data_offset
+= (s32
)le32_to_cpu(sb
->new_offset
);
1468 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1470 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1471 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1472 if (rdev
->sb_size
& bmask
)
1473 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1476 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1479 && rdev
->new_data_offset
< sb_start
+ (rdev
->sb_size
/512))
1482 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1485 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1487 if (!rdev
->bb_page
) {
1488 rdev
->bb_page
= alloc_page(GFP_KERNEL
);
1492 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BAD_BLOCKS
) &&
1493 rdev
->badblocks
.count
== 0) {
1494 /* need to load the bad block list.
1495 * Currently we limit it to one page.
1501 int sectors
= le16_to_cpu(sb
->bblog_size
);
1502 if (sectors
> (PAGE_SIZE
/ 512))
1504 offset
= le32_to_cpu(sb
->bblog_offset
);
1507 bb_sector
= (long long)offset
;
1508 if (!sync_page_io(rdev
, bb_sector
, sectors
<< 9,
1509 rdev
->bb_page
, REQ_OP_READ
, 0, true))
1511 bbp
= (u64
*)page_address(rdev
->bb_page
);
1512 rdev
->badblocks
.shift
= sb
->bblog_shift
;
1513 for (i
= 0 ; i
< (sectors
<< (9-3)) ; i
++, bbp
++) {
1514 u64 bb
= le64_to_cpu(*bbp
);
1515 int count
= bb
& (0x3ff);
1516 u64 sector
= bb
>> 10;
1517 sector
<<= sb
->bblog_shift
;
1518 count
<<= sb
->bblog_shift
;
1521 if (badblocks_set(&rdev
->badblocks
, sector
, count
, 1))
1524 } else if (sb
->bblog_offset
!= 0)
1525 rdev
->badblocks
.shift
= 0;
1531 struct mdp_superblock_1
*refsb
= page_address(refdev
->sb_page
);
1533 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1534 sb
->level
!= refsb
->level
||
1535 sb
->layout
!= refsb
->layout
||
1536 sb
->chunksize
!= refsb
->chunksize
) {
1537 pr_warn("md: %s has strangely different superblock to %s\n",
1538 bdevname(rdev
->bdev
,b
),
1539 bdevname(refdev
->bdev
,b2
));
1542 ev1
= le64_to_cpu(sb
->events
);
1543 ev2
= le64_to_cpu(refsb
->events
);
1550 if (minor_version
) {
1551 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9);
1552 sectors
-= rdev
->data_offset
;
1554 sectors
= rdev
->sb_start
;
1555 if (sectors
< le64_to_cpu(sb
->data_size
))
1557 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1561 static int super_1_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
1563 struct mdp_superblock_1
*sb
= page_address(rdev
->sb_page
);
1564 __u64 ev1
= le64_to_cpu(sb
->events
);
1566 rdev
->raid_disk
= -1;
1567 clear_bit(Faulty
, &rdev
->flags
);
1568 clear_bit(In_sync
, &rdev
->flags
);
1569 clear_bit(Bitmap_sync
, &rdev
->flags
);
1570 clear_bit(WriteMostly
, &rdev
->flags
);
1572 if (mddev
->raid_disks
== 0) {
1573 mddev
->major_version
= 1;
1574 mddev
->patch_version
= 0;
1575 mddev
->external
= 0;
1576 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1577 mddev
->ctime
= le64_to_cpu(sb
->ctime
);
1578 mddev
->utime
= le64_to_cpu(sb
->utime
);
1579 mddev
->level
= le32_to_cpu(sb
->level
);
1580 mddev
->clevel
[0] = 0;
1581 mddev
->layout
= le32_to_cpu(sb
->layout
);
1582 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1583 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1584 mddev
->events
= ev1
;
1585 mddev
->bitmap_info
.offset
= 0;
1586 mddev
->bitmap_info
.space
= 0;
1587 /* Default location for bitmap is 1K after superblock
1588 * using 3K - total of 4K
1590 mddev
->bitmap_info
.default_offset
= 1024 >> 9;
1591 mddev
->bitmap_info
.default_space
= (4096-1024) >> 9;
1592 mddev
->reshape_backwards
= 0;
1594 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1595 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1597 mddev
->max_disks
= (4096-256)/2;
1599 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1600 mddev
->bitmap_info
.file
== NULL
) {
1601 mddev
->bitmap_info
.offset
=
1602 (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1603 /* Metadata doesn't record how much space is available.
1604 * For 1.0, we assume we can use up to the superblock
1605 * if before, else to 4K beyond superblock.
1606 * For others, assume no change is possible.
1608 if (mddev
->minor_version
> 0)
1609 mddev
->bitmap_info
.space
= 0;
1610 else if (mddev
->bitmap_info
.offset
> 0)
1611 mddev
->bitmap_info
.space
=
1612 8 - mddev
->bitmap_info
.offset
;
1614 mddev
->bitmap_info
.space
=
1615 -mddev
->bitmap_info
.offset
;
1618 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1619 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1620 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1621 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1622 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1623 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1624 if (mddev
->delta_disks
< 0 ||
1625 (mddev
->delta_disks
== 0 &&
1626 (le32_to_cpu(sb
->feature_map
)
1627 & MD_FEATURE_RESHAPE_BACKWARDS
)))
1628 mddev
->reshape_backwards
= 1;
1630 mddev
->reshape_position
= MaxSector
;
1631 mddev
->delta_disks
= 0;
1632 mddev
->new_level
= mddev
->level
;
1633 mddev
->new_layout
= mddev
->layout
;
1634 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1637 if (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_JOURNAL
)
1638 set_bit(MD_HAS_JOURNAL
, &mddev
->flags
);
1639 } else if (mddev
->pers
== NULL
) {
1640 /* Insist of good event counter while assembling, except for
1641 * spares (which don't need an event count) */
1643 if (rdev
->desc_nr
>= 0 &&
1644 rdev
->desc_nr
< le32_to_cpu(sb
->max_dev
) &&
1645 (le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) < MD_DISK_ROLE_MAX
||
1646 le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) == MD_DISK_ROLE_JOURNAL
))
1647 if (ev1
< mddev
->events
)
1649 } else if (mddev
->bitmap
) {
1650 /* If adding to array with a bitmap, then we can accept an
1651 * older device, but not too old.
1653 if (ev1
< mddev
->bitmap
->events_cleared
)
1655 if (ev1
< mddev
->events
)
1656 set_bit(Bitmap_sync
, &rdev
->flags
);
1658 if (ev1
< mddev
->events
)
1659 /* just a hot-add of a new device, leave raid_disk at -1 */
1662 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1664 if (rdev
->desc_nr
< 0 ||
1665 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1666 role
= MD_DISK_ROLE_SPARE
;
1669 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1671 case MD_DISK_ROLE_SPARE
: /* spare */
1673 case MD_DISK_ROLE_FAULTY
: /* faulty */
1674 set_bit(Faulty
, &rdev
->flags
);
1676 case MD_DISK_ROLE_JOURNAL
: /* journal device */
1677 if (!(le32_to_cpu(sb
->feature_map
) & MD_FEATURE_JOURNAL
)) {
1678 /* journal device without journal feature */
1679 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
1682 set_bit(Journal
, &rdev
->flags
);
1683 rdev
->journal_tail
= le64_to_cpu(sb
->journal_tail
);
1684 rdev
->raid_disk
= 0;
1687 rdev
->saved_raid_disk
= role
;
1688 if ((le32_to_cpu(sb
->feature_map
) &
1689 MD_FEATURE_RECOVERY_OFFSET
)) {
1690 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1691 if (!(le32_to_cpu(sb
->feature_map
) &
1692 MD_FEATURE_RECOVERY_BITMAP
))
1693 rdev
->saved_raid_disk
= -1;
1695 set_bit(In_sync
, &rdev
->flags
);
1696 rdev
->raid_disk
= role
;
1699 if (sb
->devflags
& WriteMostly1
)
1700 set_bit(WriteMostly
, &rdev
->flags
);
1701 if (sb
->devflags
& FailFast1
)
1702 set_bit(FailFast
, &rdev
->flags
);
1703 if (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_REPLACEMENT
)
1704 set_bit(Replacement
, &rdev
->flags
);
1705 } else /* MULTIPATH are always insync */
1706 set_bit(In_sync
, &rdev
->flags
);
1711 static void super_1_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
1713 struct mdp_superblock_1
*sb
;
1714 struct md_rdev
*rdev2
;
1716 /* make rdev->sb match mddev and rdev data. */
1718 sb
= page_address(rdev
->sb_page
);
1720 sb
->feature_map
= 0;
1722 sb
->recovery_offset
= cpu_to_le64(0);
1723 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1725 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1726 sb
->events
= cpu_to_le64(mddev
->events
);
1728 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1729 else if (test_bit(MD_JOURNAL_CLEAN
, &mddev
->flags
))
1730 sb
->resync_offset
= cpu_to_le64(MaxSector
);
1732 sb
->resync_offset
= cpu_to_le64(0);
1734 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1736 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1737 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
1738 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
1739 sb
->level
= cpu_to_le32(mddev
->level
);
1740 sb
->layout
= cpu_to_le32(mddev
->layout
);
1741 if (test_bit(FailFast
, &rdev
->flags
))
1742 sb
->devflags
|= FailFast1
;
1744 sb
->devflags
&= ~FailFast1
;
1746 if (test_bit(WriteMostly
, &rdev
->flags
))
1747 sb
->devflags
|= WriteMostly1
;
1749 sb
->devflags
&= ~WriteMostly1
;
1750 sb
->data_offset
= cpu_to_le64(rdev
->data_offset
);
1751 sb
->data_size
= cpu_to_le64(rdev
->sectors
);
1753 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
) {
1754 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_info
.offset
);
1755 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1758 if (rdev
->raid_disk
>= 0 && !test_bit(Journal
, &rdev
->flags
) &&
1759 !test_bit(In_sync
, &rdev
->flags
)) {
1761 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1762 sb
->recovery_offset
=
1763 cpu_to_le64(rdev
->recovery_offset
);
1764 if (rdev
->saved_raid_disk
>= 0 && mddev
->bitmap
)
1766 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP
);
1768 /* Note: recovery_offset and journal_tail share space */
1769 if (test_bit(Journal
, &rdev
->flags
))
1770 sb
->journal_tail
= cpu_to_le64(rdev
->journal_tail
);
1771 if (test_bit(Replacement
, &rdev
->flags
))
1773 cpu_to_le32(MD_FEATURE_REPLACEMENT
);
1775 if (mddev
->reshape_position
!= MaxSector
) {
1776 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1777 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1778 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1779 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1780 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1781 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
1782 if (mddev
->delta_disks
== 0 &&
1783 mddev
->reshape_backwards
)
1785 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS
);
1786 if (rdev
->new_data_offset
!= rdev
->data_offset
) {
1788 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET
);
1789 sb
->new_offset
= cpu_to_le32((__u32
)(rdev
->new_data_offset
1790 - rdev
->data_offset
));
1794 if (mddev_is_clustered(mddev
))
1795 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_CLUSTERED
);
1797 if (rdev
->badblocks
.count
== 0)
1798 /* Nothing to do for bad blocks*/ ;
1799 else if (sb
->bblog_offset
== 0)
1800 /* Cannot record bad blocks on this device */
1801 md_error(mddev
, rdev
);
1803 struct badblocks
*bb
= &rdev
->badblocks
;
1804 u64
*bbp
= (u64
*)page_address(rdev
->bb_page
);
1806 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_BAD_BLOCKS
);
1811 seq
= read_seqbegin(&bb
->lock
);
1813 memset(bbp
, 0xff, PAGE_SIZE
);
1815 for (i
= 0 ; i
< bb
->count
; i
++) {
1816 u64 internal_bb
= p
[i
];
1817 u64 store_bb
= ((BB_OFFSET(internal_bb
) << 10)
1818 | BB_LEN(internal_bb
));
1819 bbp
[i
] = cpu_to_le64(store_bb
);
1822 if (read_seqretry(&bb
->lock
, seq
))
1825 bb
->sector
= (rdev
->sb_start
+
1826 (int)le32_to_cpu(sb
->bblog_offset
));
1827 bb
->size
= le16_to_cpu(sb
->bblog_size
);
1832 rdev_for_each(rdev2
, mddev
)
1833 if (rdev2
->desc_nr
+1 > max_dev
)
1834 max_dev
= rdev2
->desc_nr
+1;
1836 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
1838 sb
->max_dev
= cpu_to_le32(max_dev
);
1839 rdev
->sb_size
= max_dev
* 2 + 256;
1840 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1841 if (rdev
->sb_size
& bmask
)
1842 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1844 max_dev
= le32_to_cpu(sb
->max_dev
);
1846 for (i
=0; i
<max_dev
;i
++)
1847 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_FAULTY
);
1849 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
))
1850 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_JOURNAL
);
1852 rdev_for_each(rdev2
, mddev
) {
1854 if (test_bit(Faulty
, &rdev2
->flags
))
1855 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_FAULTY
);
1856 else if (test_bit(In_sync
, &rdev2
->flags
))
1857 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1858 else if (test_bit(Journal
, &rdev2
->flags
))
1859 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_JOURNAL
);
1860 else if (rdev2
->raid_disk
>= 0)
1861 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1863 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_SPARE
);
1866 sb
->sb_csum
= calc_sb_1_csum(sb
);
1869 static unsigned long long
1870 super_1_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
1872 struct mdp_superblock_1
*sb
;
1873 sector_t max_sectors
;
1874 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1875 return 0; /* component must fit device */
1876 if (rdev
->data_offset
!= rdev
->new_data_offset
)
1877 return 0; /* too confusing */
1878 if (rdev
->sb_start
< rdev
->data_offset
) {
1879 /* minor versions 1 and 2; superblock before data */
1880 max_sectors
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1881 max_sectors
-= rdev
->data_offset
;
1882 if (!num_sectors
|| num_sectors
> max_sectors
)
1883 num_sectors
= max_sectors
;
1884 } else if (rdev
->mddev
->bitmap_info
.offset
) {
1885 /* minor version 0 with bitmap we can't move */
1888 /* minor version 0; superblock after data */
1890 sb_start
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) - 8*2;
1891 sb_start
&= ~(sector_t
)(4*2 - 1);
1892 max_sectors
= rdev
->sectors
+ sb_start
- rdev
->sb_start
;
1893 if (!num_sectors
|| num_sectors
> max_sectors
)
1894 num_sectors
= max_sectors
;
1895 rdev
->sb_start
= sb_start
;
1897 sb
= page_address(rdev
->sb_page
);
1898 sb
->data_size
= cpu_to_le64(num_sectors
);
1899 sb
->super_offset
= rdev
->sb_start
;
1900 sb
->sb_csum
= calc_sb_1_csum(sb
);
1902 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1904 } while (md_super_wait(rdev
->mddev
) < 0);
1910 super_1_allow_new_offset(struct md_rdev
*rdev
,
1911 unsigned long long new_offset
)
1913 /* All necessary checks on new >= old have been done */
1914 struct bitmap
*bitmap
;
1915 if (new_offset
>= rdev
->data_offset
)
1918 /* with 1.0 metadata, there is no metadata to tread on
1919 * so we can always move back */
1920 if (rdev
->mddev
->minor_version
== 0)
1923 /* otherwise we must be sure not to step on
1924 * any metadata, so stay:
1925 * 36K beyond start of superblock
1926 * beyond end of badblocks
1927 * beyond write-intent bitmap
1929 if (rdev
->sb_start
+ (32+4)*2 > new_offset
)
1931 bitmap
= rdev
->mddev
->bitmap
;
1932 if (bitmap
&& !rdev
->mddev
->bitmap_info
.file
&&
1933 rdev
->sb_start
+ rdev
->mddev
->bitmap_info
.offset
+
1934 bitmap
->storage
.file_pages
* (PAGE_SIZE
>>9) > new_offset
)
1936 if (rdev
->badblocks
.sector
+ rdev
->badblocks
.size
> new_offset
)
1942 static struct super_type super_types
[] = {
1945 .owner
= THIS_MODULE
,
1946 .load_super
= super_90_load
,
1947 .validate_super
= super_90_validate
,
1948 .sync_super
= super_90_sync
,
1949 .rdev_size_change
= super_90_rdev_size_change
,
1950 .allow_new_offset
= super_90_allow_new_offset
,
1954 .owner
= THIS_MODULE
,
1955 .load_super
= super_1_load
,
1956 .validate_super
= super_1_validate
,
1957 .sync_super
= super_1_sync
,
1958 .rdev_size_change
= super_1_rdev_size_change
,
1959 .allow_new_offset
= super_1_allow_new_offset
,
1963 static void sync_super(struct mddev
*mddev
, struct md_rdev
*rdev
)
1965 if (mddev
->sync_super
) {
1966 mddev
->sync_super(mddev
, rdev
);
1970 BUG_ON(mddev
->major_version
>= ARRAY_SIZE(super_types
));
1972 super_types
[mddev
->major_version
].sync_super(mddev
, rdev
);
1975 static int match_mddev_units(struct mddev
*mddev1
, struct mddev
*mddev2
)
1977 struct md_rdev
*rdev
, *rdev2
;
1980 rdev_for_each_rcu(rdev
, mddev1
) {
1981 if (test_bit(Faulty
, &rdev
->flags
) ||
1982 test_bit(Journal
, &rdev
->flags
) ||
1983 rdev
->raid_disk
== -1)
1985 rdev_for_each_rcu(rdev2
, mddev2
) {
1986 if (test_bit(Faulty
, &rdev2
->flags
) ||
1987 test_bit(Journal
, &rdev2
->flags
) ||
1988 rdev2
->raid_disk
== -1)
1990 if (rdev
->bdev
->bd_contains
==
1991 rdev2
->bdev
->bd_contains
) {
2001 static LIST_HEAD(pending_raid_disks
);
2004 * Try to register data integrity profile for an mddev
2006 * This is called when an array is started and after a disk has been kicked
2007 * from the array. It only succeeds if all working and active component devices
2008 * are integrity capable with matching profiles.
2010 int md_integrity_register(struct mddev
*mddev
)
2012 struct md_rdev
*rdev
, *reference
= NULL
;
2014 if (list_empty(&mddev
->disks
))
2015 return 0; /* nothing to do */
2016 if (!mddev
->gendisk
|| blk_get_integrity(mddev
->gendisk
))
2017 return 0; /* shouldn't register, or already is */
2018 rdev_for_each(rdev
, mddev
) {
2019 /* skip spares and non-functional disks */
2020 if (test_bit(Faulty
, &rdev
->flags
))
2022 if (rdev
->raid_disk
< 0)
2025 /* Use the first rdev as the reference */
2029 /* does this rdev's profile match the reference profile? */
2030 if (blk_integrity_compare(reference
->bdev
->bd_disk
,
2031 rdev
->bdev
->bd_disk
) < 0)
2034 if (!reference
|| !bdev_get_integrity(reference
->bdev
))
2037 * All component devices are integrity capable and have matching
2038 * profiles, register the common profile for the md device.
2040 blk_integrity_register(mddev
->gendisk
,
2041 bdev_get_integrity(reference
->bdev
));
2043 pr_debug("md: data integrity enabled on %s\n", mdname(mddev
));
2044 if (bioset_integrity_create(mddev
->bio_set
, BIO_POOL_SIZE
)) {
2045 pr_err("md: failed to create integrity pool for %s\n",
2051 EXPORT_SYMBOL(md_integrity_register
);
2054 * Attempt to add an rdev, but only if it is consistent with the current
2057 int md_integrity_add_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
2059 struct blk_integrity
*bi_rdev
;
2060 struct blk_integrity
*bi_mddev
;
2061 char name
[BDEVNAME_SIZE
];
2063 if (!mddev
->gendisk
)
2066 bi_rdev
= bdev_get_integrity(rdev
->bdev
);
2067 bi_mddev
= blk_get_integrity(mddev
->gendisk
);
2069 if (!bi_mddev
) /* nothing to do */
2072 if (blk_integrity_compare(mddev
->gendisk
, rdev
->bdev
->bd_disk
) != 0) {
2073 pr_err("%s: incompatible integrity profile for %s\n",
2074 mdname(mddev
), bdevname(rdev
->bdev
, name
));
2080 EXPORT_SYMBOL(md_integrity_add_rdev
);
2082 static int bind_rdev_to_array(struct md_rdev
*rdev
, struct mddev
*mddev
)
2084 char b
[BDEVNAME_SIZE
];
2088 /* prevent duplicates */
2089 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
2092 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2093 if (!test_bit(Journal
, &rdev
->flags
) &&
2095 (mddev
->dev_sectors
== 0 || rdev
->sectors
< mddev
->dev_sectors
)) {
2097 /* Cannot change size, so fail
2098 * If mddev->level <= 0, then we don't care
2099 * about aligning sizes (e.g. linear)
2101 if (mddev
->level
> 0)
2104 mddev
->dev_sectors
= rdev
->sectors
;
2107 /* Verify rdev->desc_nr is unique.
2108 * If it is -1, assign a free number, else
2109 * check number is not in use
2112 if (rdev
->desc_nr
< 0) {
2115 choice
= mddev
->raid_disks
;
2116 while (md_find_rdev_nr_rcu(mddev
, choice
))
2118 rdev
->desc_nr
= choice
;
2120 if (md_find_rdev_nr_rcu(mddev
, rdev
->desc_nr
)) {
2126 if (!test_bit(Journal
, &rdev
->flags
) &&
2127 mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
2128 pr_warn("md: %s: array is limited to %d devices\n",
2129 mdname(mddev
), mddev
->max_disks
);
2132 bdevname(rdev
->bdev
,b
);
2133 strreplace(b
, '/', '!');
2135 rdev
->mddev
= mddev
;
2136 pr_debug("md: bind<%s>\n", b
);
2138 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
2141 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
2142 if (sysfs_create_link(&rdev
->kobj
, ko
, "block"))
2143 /* failure here is OK */;
2144 rdev
->sysfs_state
= sysfs_get_dirent_safe(rdev
->kobj
.sd
, "state");
2146 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
2147 bd_link_disk_holder(rdev
->bdev
, mddev
->gendisk
);
2149 /* May as well allow recovery to be retried once */
2150 mddev
->recovery_disabled
++;
2155 pr_warn("md: failed to register dev-%s for %s\n",
2160 static void md_delayed_delete(struct work_struct
*ws
)
2162 struct md_rdev
*rdev
= container_of(ws
, struct md_rdev
, del_work
);
2163 kobject_del(&rdev
->kobj
);
2164 kobject_put(&rdev
->kobj
);
2167 static void unbind_rdev_from_array(struct md_rdev
*rdev
)
2169 char b
[BDEVNAME_SIZE
];
2171 bd_unlink_disk_holder(rdev
->bdev
, rdev
->mddev
->gendisk
);
2172 list_del_rcu(&rdev
->same_set
);
2173 pr_debug("md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
2175 sysfs_remove_link(&rdev
->kobj
, "block");
2176 sysfs_put(rdev
->sysfs_state
);
2177 rdev
->sysfs_state
= NULL
;
2178 rdev
->badblocks
.count
= 0;
2179 /* We need to delay this, otherwise we can deadlock when
2180 * writing to 'remove' to "dev/state". We also need
2181 * to delay it due to rcu usage.
2184 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
2185 kobject_get(&rdev
->kobj
);
2186 queue_work(md_misc_wq
, &rdev
->del_work
);
2190 * prevent the device from being mounted, repartitioned or
2191 * otherwise reused by a RAID array (or any other kernel
2192 * subsystem), by bd_claiming the device.
2194 static int lock_rdev(struct md_rdev
*rdev
, dev_t dev
, int shared
)
2197 struct block_device
*bdev
;
2198 char b
[BDEVNAME_SIZE
];
2200 bdev
= blkdev_get_by_dev(dev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
,
2201 shared
? (struct md_rdev
*)lock_rdev
: rdev
);
2203 pr_warn("md: could not open %s.\n", __bdevname(dev
, b
));
2204 return PTR_ERR(bdev
);
2210 static void unlock_rdev(struct md_rdev
*rdev
)
2212 struct block_device
*bdev
= rdev
->bdev
;
2214 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
2217 void md_autodetect_dev(dev_t dev
);
2219 static void export_rdev(struct md_rdev
*rdev
)
2221 char b
[BDEVNAME_SIZE
];
2223 pr_debug("md: export_rdev(%s)\n", bdevname(rdev
->bdev
,b
));
2224 md_rdev_clear(rdev
);
2226 if (test_bit(AutoDetected
, &rdev
->flags
))
2227 md_autodetect_dev(rdev
->bdev
->bd_dev
);
2230 kobject_put(&rdev
->kobj
);
2233 void md_kick_rdev_from_array(struct md_rdev
*rdev
)
2235 unbind_rdev_from_array(rdev
);
2238 EXPORT_SYMBOL_GPL(md_kick_rdev_from_array
);
2240 static void export_array(struct mddev
*mddev
)
2242 struct md_rdev
*rdev
;
2244 while (!list_empty(&mddev
->disks
)) {
2245 rdev
= list_first_entry(&mddev
->disks
, struct md_rdev
,
2247 md_kick_rdev_from_array(rdev
);
2249 mddev
->raid_disks
= 0;
2250 mddev
->major_version
= 0;
2253 static void sync_sbs(struct mddev
*mddev
, int nospares
)
2255 /* Update each superblock (in-memory image), but
2256 * if we are allowed to, skip spares which already
2257 * have the right event counter, or have one earlier
2258 * (which would mean they aren't being marked as dirty
2259 * with the rest of the array)
2261 struct md_rdev
*rdev
;
2262 rdev_for_each(rdev
, mddev
) {
2263 if (rdev
->sb_events
== mddev
->events
||
2265 rdev
->raid_disk
< 0 &&
2266 rdev
->sb_events
+1 == mddev
->events
)) {
2267 /* Don't update this superblock */
2268 rdev
->sb_loaded
= 2;
2270 sync_super(mddev
, rdev
);
2271 rdev
->sb_loaded
= 1;
2276 static bool does_sb_need_changing(struct mddev
*mddev
)
2278 struct md_rdev
*rdev
;
2279 struct mdp_superblock_1
*sb
;
2282 /* Find a good rdev */
2283 rdev_for_each(rdev
, mddev
)
2284 if ((rdev
->raid_disk
>= 0) && !test_bit(Faulty
, &rdev
->flags
))
2287 /* No good device found. */
2291 sb
= page_address(rdev
->sb_page
);
2292 /* Check if a device has become faulty or a spare become active */
2293 rdev_for_each(rdev
, mddev
) {
2294 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
2295 /* Device activated? */
2296 if (role
== 0xffff && rdev
->raid_disk
>=0 &&
2297 !test_bit(Faulty
, &rdev
->flags
))
2299 /* Device turned faulty? */
2300 if (test_bit(Faulty
, &rdev
->flags
) && (role
< 0xfffd))
2304 /* Check if any mddev parameters have changed */
2305 if ((mddev
->dev_sectors
!= le64_to_cpu(sb
->size
)) ||
2306 (mddev
->reshape_position
!= le64_to_cpu(sb
->reshape_position
)) ||
2307 (mddev
->layout
!= le64_to_cpu(sb
->layout
)) ||
2308 (mddev
->raid_disks
!= le32_to_cpu(sb
->raid_disks
)) ||
2309 (mddev
->chunk_sectors
!= le32_to_cpu(sb
->chunksize
)))
2315 void md_update_sb(struct mddev
*mddev
, int force_change
)
2317 struct md_rdev
*rdev
;
2320 int any_badblocks_changed
= 0;
2325 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2330 if (mddev_is_clustered(mddev
)) {
2331 if (test_and_clear_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
))
2333 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
))
2335 ret
= md_cluster_ops
->metadata_update_start(mddev
);
2336 /* Has someone else has updated the sb */
2337 if (!does_sb_need_changing(mddev
)) {
2339 md_cluster_ops
->metadata_update_cancel(mddev
);
2340 bit_clear_unless(&mddev
->sb_flags
, BIT(MD_SB_CHANGE_PENDING
),
2341 BIT(MD_SB_CHANGE_DEVS
) |
2342 BIT(MD_SB_CHANGE_CLEAN
));
2347 /* First make sure individual recovery_offsets are correct */
2348 rdev_for_each(rdev
, mddev
) {
2349 if (rdev
->raid_disk
>= 0 &&
2350 mddev
->delta_disks
>= 0 &&
2351 !test_bit(Journal
, &rdev
->flags
) &&
2352 !test_bit(In_sync
, &rdev
->flags
) &&
2353 mddev
->curr_resync_completed
> rdev
->recovery_offset
)
2354 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
2357 if (!mddev
->persistent
) {
2358 clear_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
2359 clear_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2360 if (!mddev
->external
) {
2361 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
2362 rdev_for_each(rdev
, mddev
) {
2363 if (rdev
->badblocks
.changed
) {
2364 rdev
->badblocks
.changed
= 0;
2365 ack_all_badblocks(&rdev
->badblocks
);
2366 md_error(mddev
, rdev
);
2368 clear_bit(Blocked
, &rdev
->flags
);
2369 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2370 wake_up(&rdev
->blocked_wait
);
2373 wake_up(&mddev
->sb_wait
);
2377 spin_lock(&mddev
->lock
);
2379 mddev
->utime
= ktime_get_real_seconds();
2381 if (test_and_clear_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
))
2383 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
))
2384 /* just a clean<-> dirty transition, possibly leave spares alone,
2385 * though if events isn't the right even/odd, we will have to do
2391 if (mddev
->degraded
)
2392 /* If the array is degraded, then skipping spares is both
2393 * dangerous and fairly pointless.
2394 * Dangerous because a device that was removed from the array
2395 * might have a event_count that still looks up-to-date,
2396 * so it can be re-added without a resync.
2397 * Pointless because if there are any spares to skip,
2398 * then a recovery will happen and soon that array won't
2399 * be degraded any more and the spare can go back to sleep then.
2403 sync_req
= mddev
->in_sync
;
2405 /* If this is just a dirty<->clean transition, and the array is clean
2406 * and 'events' is odd, we can roll back to the previous clean state */
2408 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
2409 && mddev
->can_decrease_events
2410 && mddev
->events
!= 1) {
2412 mddev
->can_decrease_events
= 0;
2414 /* otherwise we have to go forward and ... */
2416 mddev
->can_decrease_events
= nospares
;
2420 * This 64-bit counter should never wrap.
2421 * Either we are in around ~1 trillion A.C., assuming
2422 * 1 reboot per second, or we have a bug...
2424 WARN_ON(mddev
->events
== 0);
2426 rdev_for_each(rdev
, mddev
) {
2427 if (rdev
->badblocks
.changed
)
2428 any_badblocks_changed
++;
2429 if (test_bit(Faulty
, &rdev
->flags
))
2430 set_bit(FaultRecorded
, &rdev
->flags
);
2433 sync_sbs(mddev
, nospares
);
2434 spin_unlock(&mddev
->lock
);
2436 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2437 mdname(mddev
), mddev
->in_sync
);
2440 blk_add_trace_msg(mddev
->queue
, "md md_update_sb");
2442 bitmap_update_sb(mddev
->bitmap
);
2443 rdev_for_each(rdev
, mddev
) {
2444 char b
[BDEVNAME_SIZE
];
2446 if (rdev
->sb_loaded
!= 1)
2447 continue; /* no noise on spare devices */
2449 if (!test_bit(Faulty
, &rdev
->flags
)) {
2450 md_super_write(mddev
,rdev
,
2451 rdev
->sb_start
, rdev
->sb_size
,
2453 pr_debug("md: (write) %s's sb offset: %llu\n",
2454 bdevname(rdev
->bdev
, b
),
2455 (unsigned long long)rdev
->sb_start
);
2456 rdev
->sb_events
= mddev
->events
;
2457 if (rdev
->badblocks
.size
) {
2458 md_super_write(mddev
, rdev
,
2459 rdev
->badblocks
.sector
,
2460 rdev
->badblocks
.size
<< 9,
2462 rdev
->badblocks
.size
= 0;
2466 pr_debug("md: %s (skipping faulty)\n",
2467 bdevname(rdev
->bdev
, b
));
2469 if (mddev
->level
== LEVEL_MULTIPATH
)
2470 /* only need to write one superblock... */
2473 if (md_super_wait(mddev
) < 0)
2475 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
2477 if (mddev_is_clustered(mddev
) && ret
== 0)
2478 md_cluster_ops
->metadata_update_finish(mddev
);
2480 if (mddev
->in_sync
!= sync_req
||
2481 !bit_clear_unless(&mddev
->sb_flags
, BIT(MD_SB_CHANGE_PENDING
),
2482 BIT(MD_SB_CHANGE_DEVS
) | BIT(MD_SB_CHANGE_CLEAN
)))
2483 /* have to write it out again */
2485 wake_up(&mddev
->sb_wait
);
2486 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2487 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
2489 rdev_for_each(rdev
, mddev
) {
2490 if (test_and_clear_bit(FaultRecorded
, &rdev
->flags
))
2491 clear_bit(Blocked
, &rdev
->flags
);
2493 if (any_badblocks_changed
)
2494 ack_all_badblocks(&rdev
->badblocks
);
2495 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2496 wake_up(&rdev
->blocked_wait
);
2499 EXPORT_SYMBOL(md_update_sb
);
2501 static int add_bound_rdev(struct md_rdev
*rdev
)
2503 struct mddev
*mddev
= rdev
->mddev
;
2505 bool add_journal
= test_bit(Journal
, &rdev
->flags
);
2507 if (!mddev
->pers
->hot_remove_disk
|| add_journal
) {
2508 /* If there is hot_add_disk but no hot_remove_disk
2509 * then added disks for geometry changes,
2510 * and should be added immediately.
2512 super_types
[mddev
->major_version
].
2513 validate_super(mddev
, rdev
);
2515 mddev_suspend(mddev
);
2516 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
2518 mddev_resume(mddev
);
2520 md_kick_rdev_from_array(rdev
);
2524 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2526 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2527 if (mddev
->degraded
)
2528 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
2529 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2530 md_new_event(mddev
);
2531 md_wakeup_thread(mddev
->thread
);
2535 /* words written to sysfs files may, or may not, be \n terminated.
2536 * We want to accept with case. For this we use cmd_match.
2538 static int cmd_match(const char *cmd
, const char *str
)
2540 /* See if cmd, written into a sysfs file, matches
2541 * str. They must either be the same, or cmd can
2542 * have a trailing newline
2544 while (*cmd
&& *str
&& *cmd
== *str
) {
2555 struct rdev_sysfs_entry
{
2556 struct attribute attr
;
2557 ssize_t (*show
)(struct md_rdev
*, char *);
2558 ssize_t (*store
)(struct md_rdev
*, const char *, size_t);
2562 state_show(struct md_rdev
*rdev
, char *page
)
2566 unsigned long flags
= ACCESS_ONCE(rdev
->flags
);
2568 if (test_bit(Faulty
, &flags
) ||
2569 (!test_bit(ExternalBbl
, &flags
) &&
2570 rdev
->badblocks
.unacked_exist
))
2571 len
+= sprintf(page
+len
, "faulty%s", sep
);
2572 if (test_bit(In_sync
, &flags
))
2573 len
+= sprintf(page
+len
, "in_sync%s", sep
);
2574 if (test_bit(Journal
, &flags
))
2575 len
+= sprintf(page
+len
, "journal%s", sep
);
2576 if (test_bit(WriteMostly
, &flags
))
2577 len
+= sprintf(page
+len
, "write_mostly%s", sep
);
2578 if (test_bit(Blocked
, &flags
) ||
2579 (rdev
->badblocks
.unacked_exist
2580 && !test_bit(Faulty
, &flags
)))
2581 len
+= sprintf(page
+len
, "blocked%s", sep
);
2582 if (!test_bit(Faulty
, &flags
) &&
2583 !test_bit(Journal
, &flags
) &&
2584 !test_bit(In_sync
, &flags
))
2585 len
+= sprintf(page
+len
, "spare%s", sep
);
2586 if (test_bit(WriteErrorSeen
, &flags
))
2587 len
+= sprintf(page
+len
, "write_error%s", sep
);
2588 if (test_bit(WantReplacement
, &flags
))
2589 len
+= sprintf(page
+len
, "want_replacement%s", sep
);
2590 if (test_bit(Replacement
, &flags
))
2591 len
+= sprintf(page
+len
, "replacement%s", sep
);
2592 if (test_bit(ExternalBbl
, &flags
))
2593 len
+= sprintf(page
+len
, "external_bbl%s", sep
);
2594 if (test_bit(FailFast
, &flags
))
2595 len
+= sprintf(page
+len
, "failfast%s", sep
);
2600 return len
+sprintf(page
+len
, "\n");
2604 state_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2607 * faulty - simulates an error
2608 * remove - disconnects the device
2609 * writemostly - sets write_mostly
2610 * -writemostly - clears write_mostly
2611 * blocked - sets the Blocked flags
2612 * -blocked - clears the Blocked and possibly simulates an error
2613 * insync - sets Insync providing device isn't active
2614 * -insync - clear Insync for a device with a slot assigned,
2615 * so that it gets rebuilt based on bitmap
2616 * write_error - sets WriteErrorSeen
2617 * -write_error - clears WriteErrorSeen
2618 * {,-}failfast - set/clear FailFast
2621 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
2622 md_error(rdev
->mddev
, rdev
);
2623 if (test_bit(Faulty
, &rdev
->flags
))
2627 } else if (cmd_match(buf
, "remove")) {
2628 if (rdev
->mddev
->pers
) {
2629 clear_bit(Blocked
, &rdev
->flags
);
2630 remove_and_add_spares(rdev
->mddev
, rdev
);
2632 if (rdev
->raid_disk
>= 0)
2635 struct mddev
*mddev
= rdev
->mddev
;
2637 if (mddev_is_clustered(mddev
))
2638 err
= md_cluster_ops
->remove_disk(mddev
, rdev
);
2641 md_kick_rdev_from_array(rdev
);
2643 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2644 md_wakeup_thread(mddev
->thread
);
2646 md_new_event(mddev
);
2649 } else if (cmd_match(buf
, "writemostly")) {
2650 set_bit(WriteMostly
, &rdev
->flags
);
2652 } else if (cmd_match(buf
, "-writemostly")) {
2653 clear_bit(WriteMostly
, &rdev
->flags
);
2655 } else if (cmd_match(buf
, "blocked")) {
2656 set_bit(Blocked
, &rdev
->flags
);
2658 } else if (cmd_match(buf
, "-blocked")) {
2659 if (!test_bit(Faulty
, &rdev
->flags
) &&
2660 !test_bit(ExternalBbl
, &rdev
->flags
) &&
2661 rdev
->badblocks
.unacked_exist
) {
2662 /* metadata handler doesn't understand badblocks,
2663 * so we need to fail the device
2665 md_error(rdev
->mddev
, rdev
);
2667 clear_bit(Blocked
, &rdev
->flags
);
2668 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2669 wake_up(&rdev
->blocked_wait
);
2670 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2671 md_wakeup_thread(rdev
->mddev
->thread
);
2674 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
2675 set_bit(In_sync
, &rdev
->flags
);
2677 } else if (cmd_match(buf
, "failfast")) {
2678 set_bit(FailFast
, &rdev
->flags
);
2680 } else if (cmd_match(buf
, "-failfast")) {
2681 clear_bit(FailFast
, &rdev
->flags
);
2683 } else if (cmd_match(buf
, "-insync") && rdev
->raid_disk
>= 0 &&
2684 !test_bit(Journal
, &rdev
->flags
)) {
2685 if (rdev
->mddev
->pers
== NULL
) {
2686 clear_bit(In_sync
, &rdev
->flags
);
2687 rdev
->saved_raid_disk
= rdev
->raid_disk
;
2688 rdev
->raid_disk
= -1;
2691 } else if (cmd_match(buf
, "write_error")) {
2692 set_bit(WriteErrorSeen
, &rdev
->flags
);
2694 } else if (cmd_match(buf
, "-write_error")) {
2695 clear_bit(WriteErrorSeen
, &rdev
->flags
);
2697 } else if (cmd_match(buf
, "want_replacement")) {
2698 /* Any non-spare device that is not a replacement can
2699 * become want_replacement at any time, but we then need to
2700 * check if recovery is needed.
2702 if (rdev
->raid_disk
>= 0 &&
2703 !test_bit(Journal
, &rdev
->flags
) &&
2704 !test_bit(Replacement
, &rdev
->flags
))
2705 set_bit(WantReplacement
, &rdev
->flags
);
2706 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2707 md_wakeup_thread(rdev
->mddev
->thread
);
2709 } else if (cmd_match(buf
, "-want_replacement")) {
2710 /* Clearing 'want_replacement' is always allowed.
2711 * Once replacements starts it is too late though.
2714 clear_bit(WantReplacement
, &rdev
->flags
);
2715 } else if (cmd_match(buf
, "replacement")) {
2716 /* Can only set a device as a replacement when array has not
2717 * yet been started. Once running, replacement is automatic
2718 * from spares, or by assigning 'slot'.
2720 if (rdev
->mddev
->pers
)
2723 set_bit(Replacement
, &rdev
->flags
);
2726 } else if (cmd_match(buf
, "-replacement")) {
2727 /* Similarly, can only clear Replacement before start */
2728 if (rdev
->mddev
->pers
)
2731 clear_bit(Replacement
, &rdev
->flags
);
2734 } else if (cmd_match(buf
, "re-add")) {
2735 if (test_bit(Faulty
, &rdev
->flags
) && (rdev
->raid_disk
== -1)) {
2736 /* clear_bit is performed _after_ all the devices
2737 * have their local Faulty bit cleared. If any writes
2738 * happen in the meantime in the local node, they
2739 * will land in the local bitmap, which will be synced
2740 * by this node eventually
2742 if (!mddev_is_clustered(rdev
->mddev
) ||
2743 (err
= md_cluster_ops
->gather_bitmaps(rdev
)) == 0) {
2744 clear_bit(Faulty
, &rdev
->flags
);
2745 err
= add_bound_rdev(rdev
);
2749 } else if (cmd_match(buf
, "external_bbl") && (rdev
->mddev
->external
)) {
2750 set_bit(ExternalBbl
, &rdev
->flags
);
2751 rdev
->badblocks
.shift
= 0;
2753 } else if (cmd_match(buf
, "-external_bbl") && (rdev
->mddev
->external
)) {
2754 clear_bit(ExternalBbl
, &rdev
->flags
);
2758 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2759 return err
? err
: len
;
2761 static struct rdev_sysfs_entry rdev_state
=
2762 __ATTR_PREALLOC(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
2765 errors_show(struct md_rdev
*rdev
, char *page
)
2767 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
2771 errors_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2776 rv
= kstrtouint(buf
, 10, &n
);
2779 atomic_set(&rdev
->corrected_errors
, n
);
2782 static struct rdev_sysfs_entry rdev_errors
=
2783 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
2786 slot_show(struct md_rdev
*rdev
, char *page
)
2788 if (test_bit(Journal
, &rdev
->flags
))
2789 return sprintf(page
, "journal\n");
2790 else if (rdev
->raid_disk
< 0)
2791 return sprintf(page
, "none\n");
2793 return sprintf(page
, "%d\n", rdev
->raid_disk
);
2797 slot_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2802 if (test_bit(Journal
, &rdev
->flags
))
2804 if (strncmp(buf
, "none", 4)==0)
2807 err
= kstrtouint(buf
, 10, (unsigned int *)&slot
);
2811 if (rdev
->mddev
->pers
&& slot
== -1) {
2812 /* Setting 'slot' on an active array requires also
2813 * updating the 'rd%d' link, and communicating
2814 * with the personality with ->hot_*_disk.
2815 * For now we only support removing
2816 * failed/spare devices. This normally happens automatically,
2817 * but not when the metadata is externally managed.
2819 if (rdev
->raid_disk
== -1)
2821 /* personality does all needed checks */
2822 if (rdev
->mddev
->pers
->hot_remove_disk
== NULL
)
2824 clear_bit(Blocked
, &rdev
->flags
);
2825 remove_and_add_spares(rdev
->mddev
, rdev
);
2826 if (rdev
->raid_disk
>= 0)
2828 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2829 md_wakeup_thread(rdev
->mddev
->thread
);
2830 } else if (rdev
->mddev
->pers
) {
2831 /* Activating a spare .. or possibly reactivating
2832 * if we ever get bitmaps working here.
2836 if (rdev
->raid_disk
!= -1)
2839 if (test_bit(MD_RECOVERY_RUNNING
, &rdev
->mddev
->recovery
))
2842 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2845 if (slot
>= rdev
->mddev
->raid_disks
&&
2846 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2849 rdev
->raid_disk
= slot
;
2850 if (test_bit(In_sync
, &rdev
->flags
))
2851 rdev
->saved_raid_disk
= slot
;
2853 rdev
->saved_raid_disk
= -1;
2854 clear_bit(In_sync
, &rdev
->flags
);
2855 clear_bit(Bitmap_sync
, &rdev
->flags
);
2856 err
= rdev
->mddev
->pers
->
2857 hot_add_disk(rdev
->mddev
, rdev
);
2859 rdev
->raid_disk
= -1;
2862 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2863 if (sysfs_link_rdev(rdev
->mddev
, rdev
))
2864 /* failure here is OK */;
2865 /* don't wakeup anyone, leave that to userspace. */
2867 if (slot
>= rdev
->mddev
->raid_disks
&&
2868 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2870 rdev
->raid_disk
= slot
;
2871 /* assume it is working */
2872 clear_bit(Faulty
, &rdev
->flags
);
2873 clear_bit(WriteMostly
, &rdev
->flags
);
2874 set_bit(In_sync
, &rdev
->flags
);
2875 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2880 static struct rdev_sysfs_entry rdev_slot
=
2881 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
2884 offset_show(struct md_rdev
*rdev
, char *page
)
2886 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
2890 offset_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2892 unsigned long long offset
;
2893 if (kstrtoull(buf
, 10, &offset
) < 0)
2895 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
2897 if (rdev
->sectors
&& rdev
->mddev
->external
)
2898 /* Must set offset before size, so overlap checks
2901 rdev
->data_offset
= offset
;
2902 rdev
->new_data_offset
= offset
;
2906 static struct rdev_sysfs_entry rdev_offset
=
2907 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
2909 static ssize_t
new_offset_show(struct md_rdev
*rdev
, char *page
)
2911 return sprintf(page
, "%llu\n",
2912 (unsigned long long)rdev
->new_data_offset
);
2915 static ssize_t
new_offset_store(struct md_rdev
*rdev
,
2916 const char *buf
, size_t len
)
2918 unsigned long long new_offset
;
2919 struct mddev
*mddev
= rdev
->mddev
;
2921 if (kstrtoull(buf
, 10, &new_offset
) < 0)
2924 if (mddev
->sync_thread
||
2925 test_bit(MD_RECOVERY_RUNNING
,&mddev
->recovery
))
2927 if (new_offset
== rdev
->data_offset
)
2928 /* reset is always permitted */
2930 else if (new_offset
> rdev
->data_offset
) {
2931 /* must not push array size beyond rdev_sectors */
2932 if (new_offset
- rdev
->data_offset
2933 + mddev
->dev_sectors
> rdev
->sectors
)
2936 /* Metadata worries about other space details. */
2938 /* decreasing the offset is inconsistent with a backwards
2941 if (new_offset
< rdev
->data_offset
&&
2942 mddev
->reshape_backwards
)
2944 /* Increasing offset is inconsistent with forwards
2945 * reshape. reshape_direction should be set to
2946 * 'backwards' first.
2948 if (new_offset
> rdev
->data_offset
&&
2949 !mddev
->reshape_backwards
)
2952 if (mddev
->pers
&& mddev
->persistent
&&
2953 !super_types
[mddev
->major_version
]
2954 .allow_new_offset(rdev
, new_offset
))
2956 rdev
->new_data_offset
= new_offset
;
2957 if (new_offset
> rdev
->data_offset
)
2958 mddev
->reshape_backwards
= 1;
2959 else if (new_offset
< rdev
->data_offset
)
2960 mddev
->reshape_backwards
= 0;
2964 static struct rdev_sysfs_entry rdev_new_offset
=
2965 __ATTR(new_offset
, S_IRUGO
|S_IWUSR
, new_offset_show
, new_offset_store
);
2968 rdev_size_show(struct md_rdev
*rdev
, char *page
)
2970 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
2973 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
2975 /* check if two start/length pairs overlap */
2983 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
2985 unsigned long long blocks
;
2988 if (kstrtoull(buf
, 10, &blocks
) < 0)
2991 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
2992 return -EINVAL
; /* sector conversion overflow */
2995 if (new != blocks
* 2)
2996 return -EINVAL
; /* unsigned long long to sector_t overflow */
3003 rdev_size_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3005 struct mddev
*my_mddev
= rdev
->mddev
;
3006 sector_t oldsectors
= rdev
->sectors
;
3009 if (test_bit(Journal
, &rdev
->flags
))
3011 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
3013 if (rdev
->data_offset
!= rdev
->new_data_offset
)
3014 return -EINVAL
; /* too confusing */
3015 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
3016 if (my_mddev
->persistent
) {
3017 sectors
= super_types
[my_mddev
->major_version
].
3018 rdev_size_change(rdev
, sectors
);
3021 } else if (!sectors
)
3022 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
3024 if (!my_mddev
->pers
->resize
)
3025 /* Cannot change size for RAID0 or Linear etc */
3028 if (sectors
< my_mddev
->dev_sectors
)
3029 return -EINVAL
; /* component must fit device */
3031 rdev
->sectors
= sectors
;
3032 if (sectors
> oldsectors
&& my_mddev
->external
) {
3033 /* Need to check that all other rdevs with the same
3034 * ->bdev do not overlap. 'rcu' is sufficient to walk
3035 * the rdev lists safely.
3036 * This check does not provide a hard guarantee, it
3037 * just helps avoid dangerous mistakes.
3039 struct mddev
*mddev
;
3041 struct list_head
*tmp
;
3044 for_each_mddev(mddev
, tmp
) {
3045 struct md_rdev
*rdev2
;
3047 rdev_for_each(rdev2
, mddev
)
3048 if (rdev
->bdev
== rdev2
->bdev
&&
3050 overlaps(rdev
->data_offset
, rdev
->sectors
,
3063 /* Someone else could have slipped in a size
3064 * change here, but doing so is just silly.
3065 * We put oldsectors back because we *know* it is
3066 * safe, and trust userspace not to race with
3069 rdev
->sectors
= oldsectors
;
3076 static struct rdev_sysfs_entry rdev_size
=
3077 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
3079 static ssize_t
recovery_start_show(struct md_rdev
*rdev
, char *page
)
3081 unsigned long long recovery_start
= rdev
->recovery_offset
;
3083 if (test_bit(In_sync
, &rdev
->flags
) ||
3084 recovery_start
== MaxSector
)
3085 return sprintf(page
, "none\n");
3087 return sprintf(page
, "%llu\n", recovery_start
);
3090 static ssize_t
recovery_start_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3092 unsigned long long recovery_start
;
3094 if (cmd_match(buf
, "none"))
3095 recovery_start
= MaxSector
;
3096 else if (kstrtoull(buf
, 10, &recovery_start
))
3099 if (rdev
->mddev
->pers
&&
3100 rdev
->raid_disk
>= 0)
3103 rdev
->recovery_offset
= recovery_start
;
3104 if (recovery_start
== MaxSector
)
3105 set_bit(In_sync
, &rdev
->flags
);
3107 clear_bit(In_sync
, &rdev
->flags
);
3111 static struct rdev_sysfs_entry rdev_recovery_start
=
3112 __ATTR(recovery_start
, S_IRUGO
|S_IWUSR
, recovery_start_show
, recovery_start_store
);
3114 /* sysfs access to bad-blocks list.
3115 * We present two files.
3116 * 'bad-blocks' lists sector numbers and lengths of ranges that
3117 * are recorded as bad. The list is truncated to fit within
3118 * the one-page limit of sysfs.
3119 * Writing "sector length" to this file adds an acknowledged
3121 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3122 * been acknowledged. Writing to this file adds bad blocks
3123 * without acknowledging them. This is largely for testing.
3125 static ssize_t
bb_show(struct md_rdev
*rdev
, char *page
)
3127 return badblocks_show(&rdev
->badblocks
, page
, 0);
3129 static ssize_t
bb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
3131 int rv
= badblocks_store(&rdev
->badblocks
, page
, len
, 0);
3132 /* Maybe that ack was all we needed */
3133 if (test_and_clear_bit(BlockedBadBlocks
, &rdev
->flags
))
3134 wake_up(&rdev
->blocked_wait
);
3137 static struct rdev_sysfs_entry rdev_bad_blocks
=
3138 __ATTR(bad_blocks
, S_IRUGO
|S_IWUSR
, bb_show
, bb_store
);
3140 static ssize_t
ubb_show(struct md_rdev
*rdev
, char *page
)
3142 return badblocks_show(&rdev
->badblocks
, page
, 1);
3144 static ssize_t
ubb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
3146 return badblocks_store(&rdev
->badblocks
, page
, len
, 1);
3148 static struct rdev_sysfs_entry rdev_unack_bad_blocks
=
3149 __ATTR(unacknowledged_bad_blocks
, S_IRUGO
|S_IWUSR
, ubb_show
, ubb_store
);
3151 static struct attribute
*rdev_default_attrs
[] = {
3156 &rdev_new_offset
.attr
,
3158 &rdev_recovery_start
.attr
,
3159 &rdev_bad_blocks
.attr
,
3160 &rdev_unack_bad_blocks
.attr
,
3164 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
3166 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3167 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3173 return entry
->show(rdev
, page
);
3177 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
3178 const char *page
, size_t length
)
3180 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3181 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3183 struct mddev
*mddev
= rdev
->mddev
;
3187 if (!capable(CAP_SYS_ADMIN
))
3189 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
3191 if (rdev
->mddev
== NULL
)
3194 rv
= entry
->store(rdev
, page
, length
);
3195 mddev_unlock(mddev
);
3200 static void rdev_free(struct kobject
*ko
)
3202 struct md_rdev
*rdev
= container_of(ko
, struct md_rdev
, kobj
);
3205 static const struct sysfs_ops rdev_sysfs_ops
= {
3206 .show
= rdev_attr_show
,
3207 .store
= rdev_attr_store
,
3209 static struct kobj_type rdev_ktype
= {
3210 .release
= rdev_free
,
3211 .sysfs_ops
= &rdev_sysfs_ops
,
3212 .default_attrs
= rdev_default_attrs
,
3215 int md_rdev_init(struct md_rdev
*rdev
)
3218 rdev
->saved_raid_disk
= -1;
3219 rdev
->raid_disk
= -1;
3221 rdev
->data_offset
= 0;
3222 rdev
->new_data_offset
= 0;
3223 rdev
->sb_events
= 0;
3224 rdev
->last_read_error
= 0;
3225 rdev
->sb_loaded
= 0;
3226 rdev
->bb_page
= NULL
;
3227 atomic_set(&rdev
->nr_pending
, 0);
3228 atomic_set(&rdev
->read_errors
, 0);
3229 atomic_set(&rdev
->corrected_errors
, 0);
3231 INIT_LIST_HEAD(&rdev
->same_set
);
3232 init_waitqueue_head(&rdev
->blocked_wait
);
3234 /* Add space to store bad block list.
3235 * This reserves the space even on arrays where it cannot
3236 * be used - I wonder if that matters
3238 return badblocks_init(&rdev
->badblocks
, 0);
3240 EXPORT_SYMBOL_GPL(md_rdev_init
);
3242 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3244 * mark the device faulty if:
3246 * - the device is nonexistent (zero size)
3247 * - the device has no valid superblock
3249 * a faulty rdev _never_ has rdev->sb set.
3251 static struct md_rdev
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
3253 char b
[BDEVNAME_SIZE
];
3255 struct md_rdev
*rdev
;
3258 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
3260 return ERR_PTR(-ENOMEM
);
3262 err
= md_rdev_init(rdev
);
3265 err
= alloc_disk_sb(rdev
);
3269 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
3273 kobject_init(&rdev
->kobj
, &rdev_ktype
);
3275 size
= i_size_read(rdev
->bdev
->bd_inode
) >> BLOCK_SIZE_BITS
;
3277 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
3278 bdevname(rdev
->bdev
,b
));
3283 if (super_format
>= 0) {
3284 err
= super_types
[super_format
].
3285 load_super(rdev
, NULL
, super_minor
);
3286 if (err
== -EINVAL
) {
3287 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
3288 bdevname(rdev
->bdev
,b
),
3289 super_format
, super_minor
);
3293 pr_warn("md: could not read %s's sb, not importing!\n",
3294 bdevname(rdev
->bdev
,b
));
3304 md_rdev_clear(rdev
);
3306 return ERR_PTR(err
);
3310 * Check a full RAID array for plausibility
3313 static void analyze_sbs(struct mddev
*mddev
)
3316 struct md_rdev
*rdev
, *freshest
, *tmp
;
3317 char b
[BDEVNAME_SIZE
];
3320 rdev_for_each_safe(rdev
, tmp
, mddev
)
3321 switch (super_types
[mddev
->major_version
].
3322 load_super(rdev
, freshest
, mddev
->minor_version
)) {
3329 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
3330 bdevname(rdev
->bdev
,b
));
3331 md_kick_rdev_from_array(rdev
);
3334 super_types
[mddev
->major_version
].
3335 validate_super(mddev
, freshest
);
3338 rdev_for_each_safe(rdev
, tmp
, mddev
) {
3339 if (mddev
->max_disks
&&
3340 (rdev
->desc_nr
>= mddev
->max_disks
||
3341 i
> mddev
->max_disks
)) {
3342 pr_warn("md: %s: %s: only %d devices permitted\n",
3343 mdname(mddev
), bdevname(rdev
->bdev
, b
),
3345 md_kick_rdev_from_array(rdev
);
3348 if (rdev
!= freshest
) {
3349 if (super_types
[mddev
->major_version
].
3350 validate_super(mddev
, rdev
)) {
3351 pr_warn("md: kicking non-fresh %s from array!\n",
3352 bdevname(rdev
->bdev
,b
));
3353 md_kick_rdev_from_array(rdev
);
3357 if (mddev
->level
== LEVEL_MULTIPATH
) {
3358 rdev
->desc_nr
= i
++;
3359 rdev
->raid_disk
= rdev
->desc_nr
;
3360 set_bit(In_sync
, &rdev
->flags
);
3361 } else if (rdev
->raid_disk
>=
3362 (mddev
->raid_disks
- min(0, mddev
->delta_disks
)) &&
3363 !test_bit(Journal
, &rdev
->flags
)) {
3364 rdev
->raid_disk
= -1;
3365 clear_bit(In_sync
, &rdev
->flags
);
3370 /* Read a fixed-point number.
3371 * Numbers in sysfs attributes should be in "standard" units where
3372 * possible, so time should be in seconds.
3373 * However we internally use a a much smaller unit such as
3374 * milliseconds or jiffies.
3375 * This function takes a decimal number with a possible fractional
3376 * component, and produces an integer which is the result of
3377 * multiplying that number by 10^'scale'.
3378 * all without any floating-point arithmetic.
3380 int strict_strtoul_scaled(const char *cp
, unsigned long *res
, int scale
)
3382 unsigned long result
= 0;
3384 while (isdigit(*cp
) || (*cp
== '.' && decimals
< 0)) {
3387 else if (decimals
< scale
) {
3390 result
= result
* 10 + value
;
3402 while (decimals
< scale
) {
3411 safe_delay_show(struct mddev
*mddev
, char *page
)
3413 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
3414 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
3417 safe_delay_store(struct mddev
*mddev
, const char *cbuf
, size_t len
)
3421 if (mddev_is_clustered(mddev
)) {
3422 pr_warn("md: Safemode is disabled for clustered mode\n");
3426 if (strict_strtoul_scaled(cbuf
, &msec
, 3) < 0)
3429 mddev
->safemode_delay
= 0;
3431 unsigned long old_delay
= mddev
->safemode_delay
;
3432 unsigned long new_delay
= (msec
*HZ
)/1000;
3436 mddev
->safemode_delay
= new_delay
;
3437 if (new_delay
< old_delay
|| old_delay
== 0)
3438 mod_timer(&mddev
->safemode_timer
, jiffies
+1);
3442 static struct md_sysfs_entry md_safe_delay
=
3443 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
3446 level_show(struct mddev
*mddev
, char *page
)
3448 struct md_personality
*p
;
3450 spin_lock(&mddev
->lock
);
3453 ret
= sprintf(page
, "%s\n", p
->name
);
3454 else if (mddev
->clevel
[0])
3455 ret
= sprintf(page
, "%s\n", mddev
->clevel
);
3456 else if (mddev
->level
!= LEVEL_NONE
)
3457 ret
= sprintf(page
, "%d\n", mddev
->level
);
3460 spin_unlock(&mddev
->lock
);
3465 level_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3470 struct md_personality
*pers
, *oldpers
;
3472 void *priv
, *oldpriv
;
3473 struct md_rdev
*rdev
;
3475 if (slen
== 0 || slen
>= sizeof(clevel
))
3478 rv
= mddev_lock(mddev
);
3482 if (mddev
->pers
== NULL
) {
3483 strncpy(mddev
->clevel
, buf
, slen
);
3484 if (mddev
->clevel
[slen
-1] == '\n')
3486 mddev
->clevel
[slen
] = 0;
3487 mddev
->level
= LEVEL_NONE
;
3495 /* request to change the personality. Need to ensure:
3496 * - array is not engaged in resync/recovery/reshape
3497 * - old personality can be suspended
3498 * - new personality will access other array.
3502 if (mddev
->sync_thread
||
3503 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3504 mddev
->reshape_position
!= MaxSector
||
3505 mddev
->sysfs_active
)
3509 if (!mddev
->pers
->quiesce
) {
3510 pr_warn("md: %s: %s does not support online personality change\n",
3511 mdname(mddev
), mddev
->pers
->name
);
3515 /* Now find the new personality */
3516 strncpy(clevel
, buf
, slen
);
3517 if (clevel
[slen
-1] == '\n')
3520 if (kstrtol(clevel
, 10, &level
))
3523 if (request_module("md-%s", clevel
) != 0)
3524 request_module("md-level-%s", clevel
);
3525 spin_lock(&pers_lock
);
3526 pers
= find_pers(level
, clevel
);
3527 if (!pers
|| !try_module_get(pers
->owner
)) {
3528 spin_unlock(&pers_lock
);
3529 pr_warn("md: personality %s not loaded\n", clevel
);
3533 spin_unlock(&pers_lock
);
3535 if (pers
== mddev
->pers
) {
3536 /* Nothing to do! */
3537 module_put(pers
->owner
);
3541 if (!pers
->takeover
) {
3542 module_put(pers
->owner
);
3543 pr_warn("md: %s: %s does not support personality takeover\n",
3544 mdname(mddev
), clevel
);
3549 rdev_for_each(rdev
, mddev
)
3550 rdev
->new_raid_disk
= rdev
->raid_disk
;
3552 /* ->takeover must set new_* and/or delta_disks
3553 * if it succeeds, and may set them when it fails.
3555 priv
= pers
->takeover(mddev
);
3557 mddev
->new_level
= mddev
->level
;
3558 mddev
->new_layout
= mddev
->layout
;
3559 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3560 mddev
->raid_disks
-= mddev
->delta_disks
;
3561 mddev
->delta_disks
= 0;
3562 mddev
->reshape_backwards
= 0;
3563 module_put(pers
->owner
);
3564 pr_warn("md: %s: %s would not accept array\n",
3565 mdname(mddev
), clevel
);
3570 /* Looks like we have a winner */
3571 mddev_suspend(mddev
);
3572 mddev_detach(mddev
);
3574 spin_lock(&mddev
->lock
);
3575 oldpers
= mddev
->pers
;
3576 oldpriv
= mddev
->private;
3578 mddev
->private = priv
;
3579 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3580 mddev
->level
= mddev
->new_level
;
3581 mddev
->layout
= mddev
->new_layout
;
3582 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
3583 mddev
->delta_disks
= 0;
3584 mddev
->reshape_backwards
= 0;
3585 mddev
->degraded
= 0;
3586 spin_unlock(&mddev
->lock
);
3588 if (oldpers
->sync_request
== NULL
&&
3590 /* We are converting from a no-redundancy array
3591 * to a redundancy array and metadata is managed
3592 * externally so we need to be sure that writes
3593 * won't block due to a need to transition
3595 * until external management is started.
3598 mddev
->safemode_delay
= 0;
3599 mddev
->safemode
= 0;
3602 oldpers
->free(mddev
, oldpriv
);
3604 if (oldpers
->sync_request
== NULL
&&
3605 pers
->sync_request
!= NULL
) {
3606 /* need to add the md_redundancy_group */
3607 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
3608 pr_warn("md: cannot register extra attributes for %s\n",
3610 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, "sync_action");
3612 if (oldpers
->sync_request
!= NULL
&&
3613 pers
->sync_request
== NULL
) {
3614 /* need to remove the md_redundancy_group */
3615 if (mddev
->to_remove
== NULL
)
3616 mddev
->to_remove
= &md_redundancy_group
;
3619 module_put(oldpers
->owner
);
3621 rdev_for_each(rdev
, mddev
) {
3622 if (rdev
->raid_disk
< 0)
3624 if (rdev
->new_raid_disk
>= mddev
->raid_disks
)
3625 rdev
->new_raid_disk
= -1;
3626 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3628 sysfs_unlink_rdev(mddev
, rdev
);
3630 rdev_for_each(rdev
, mddev
) {
3631 if (rdev
->raid_disk
< 0)
3633 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3635 rdev
->raid_disk
= rdev
->new_raid_disk
;
3636 if (rdev
->raid_disk
< 0)
3637 clear_bit(In_sync
, &rdev
->flags
);
3639 if (sysfs_link_rdev(mddev
, rdev
))
3640 pr_warn("md: cannot register rd%d for %s after level change\n",
3641 rdev
->raid_disk
, mdname(mddev
));
3645 if (pers
->sync_request
== NULL
) {
3646 /* this is now an array without redundancy, so
3647 * it must always be in_sync
3650 del_timer_sync(&mddev
->safemode_timer
);
3652 blk_set_stacking_limits(&mddev
->queue
->limits
);
3654 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
3655 mddev_resume(mddev
);
3657 md_update_sb(mddev
, 1);
3658 sysfs_notify(&mddev
->kobj
, NULL
, "level");
3659 md_new_event(mddev
);
3662 mddev_unlock(mddev
);
3666 static struct md_sysfs_entry md_level
=
3667 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
3670 layout_show(struct mddev
*mddev
, char *page
)
3672 /* just a number, not meaningful for all levels */
3673 if (mddev
->reshape_position
!= MaxSector
&&
3674 mddev
->layout
!= mddev
->new_layout
)
3675 return sprintf(page
, "%d (%d)\n",
3676 mddev
->new_layout
, mddev
->layout
);
3677 return sprintf(page
, "%d\n", mddev
->layout
);
3681 layout_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3686 err
= kstrtouint(buf
, 10, &n
);
3689 err
= mddev_lock(mddev
);
3694 if (mddev
->pers
->check_reshape
== NULL
)
3699 mddev
->new_layout
= n
;
3700 err
= mddev
->pers
->check_reshape(mddev
);
3702 mddev
->new_layout
= mddev
->layout
;
3705 mddev
->new_layout
= n
;
3706 if (mddev
->reshape_position
== MaxSector
)
3709 mddev_unlock(mddev
);
3712 static struct md_sysfs_entry md_layout
=
3713 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
3716 raid_disks_show(struct mddev
*mddev
, char *page
)
3718 if (mddev
->raid_disks
== 0)
3720 if (mddev
->reshape_position
!= MaxSector
&&
3721 mddev
->delta_disks
!= 0)
3722 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
3723 mddev
->raid_disks
- mddev
->delta_disks
);
3724 return sprintf(page
, "%d\n", mddev
->raid_disks
);
3727 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
);
3730 raid_disks_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3735 err
= kstrtouint(buf
, 10, &n
);
3739 err
= mddev_lock(mddev
);
3743 err
= update_raid_disks(mddev
, n
);
3744 else if (mddev
->reshape_position
!= MaxSector
) {
3745 struct md_rdev
*rdev
;
3746 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
3749 rdev_for_each(rdev
, mddev
) {
3751 rdev
->data_offset
< rdev
->new_data_offset
)
3754 rdev
->data_offset
> rdev
->new_data_offset
)
3758 mddev
->delta_disks
= n
- olddisks
;
3759 mddev
->raid_disks
= n
;
3760 mddev
->reshape_backwards
= (mddev
->delta_disks
< 0);
3762 mddev
->raid_disks
= n
;
3764 mddev_unlock(mddev
);
3765 return err
? err
: len
;
3767 static struct md_sysfs_entry md_raid_disks
=
3768 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
3771 chunk_size_show(struct mddev
*mddev
, char *page
)
3773 if (mddev
->reshape_position
!= MaxSector
&&
3774 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
3775 return sprintf(page
, "%d (%d)\n",
3776 mddev
->new_chunk_sectors
<< 9,
3777 mddev
->chunk_sectors
<< 9);
3778 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
3782 chunk_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3787 err
= kstrtoul(buf
, 10, &n
);
3791 err
= mddev_lock(mddev
);
3795 if (mddev
->pers
->check_reshape
== NULL
)
3800 mddev
->new_chunk_sectors
= n
>> 9;
3801 err
= mddev
->pers
->check_reshape(mddev
);
3803 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3806 mddev
->new_chunk_sectors
= n
>> 9;
3807 if (mddev
->reshape_position
== MaxSector
)
3808 mddev
->chunk_sectors
= n
>> 9;
3810 mddev_unlock(mddev
);
3813 static struct md_sysfs_entry md_chunk_size
=
3814 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
3817 resync_start_show(struct mddev
*mddev
, char *page
)
3819 if (mddev
->recovery_cp
== MaxSector
)
3820 return sprintf(page
, "none\n");
3821 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
3825 resync_start_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3827 unsigned long long n
;
3830 if (cmd_match(buf
, "none"))
3833 err
= kstrtoull(buf
, 10, &n
);
3836 if (n
!= (sector_t
)n
)
3840 err
= mddev_lock(mddev
);
3843 if (mddev
->pers
&& !test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
3847 mddev
->recovery_cp
= n
;
3849 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
3851 mddev_unlock(mddev
);
3854 static struct md_sysfs_entry md_resync_start
=
3855 __ATTR_PREALLOC(resync_start
, S_IRUGO
|S_IWUSR
,
3856 resync_start_show
, resync_start_store
);
3859 * The array state can be:
3862 * No devices, no size, no level
3863 * Equivalent to STOP_ARRAY ioctl
3865 * May have some settings, but array is not active
3866 * all IO results in error
3867 * When written, doesn't tear down array, but just stops it
3868 * suspended (not supported yet)
3869 * All IO requests will block. The array can be reconfigured.
3870 * Writing this, if accepted, will block until array is quiescent
3872 * no resync can happen. no superblocks get written.
3873 * write requests fail
3875 * like readonly, but behaves like 'clean' on a write request.
3877 * clean - no pending writes, but otherwise active.
3878 * When written to inactive array, starts without resync
3879 * If a write request arrives then
3880 * if metadata is known, mark 'dirty' and switch to 'active'.
3881 * if not known, block and switch to write-pending
3882 * If written to an active array that has pending writes, then fails.
3884 * fully active: IO and resync can be happening.
3885 * When written to inactive array, starts with resync
3888 * clean, but writes are blocked waiting for 'active' to be written.
3891 * like active, but no writes have been seen for a while (100msec).
3894 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
3895 write_pending
, active_idle
, bad_word
};
3896 static char *array_states
[] = {
3897 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3898 "write-pending", "active-idle", NULL
};
3900 static int match_word(const char *word
, char **list
)
3903 for (n
=0; list
[n
]; n
++)
3904 if (cmd_match(word
, list
[n
]))
3910 array_state_show(struct mddev
*mddev
, char *page
)
3912 enum array_state st
= inactive
;
3923 if (test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
))
3925 else if (mddev
->in_sync
)
3927 else if (mddev
->safemode
)
3933 if (list_empty(&mddev
->disks
) &&
3934 mddev
->raid_disks
== 0 &&
3935 mddev
->dev_sectors
== 0)
3940 return sprintf(page
, "%s\n", array_states
[st
]);
3943 static int do_md_stop(struct mddev
*mddev
, int ro
, struct block_device
*bdev
);
3944 static int md_set_readonly(struct mddev
*mddev
, struct block_device
*bdev
);
3945 static int do_md_run(struct mddev
*mddev
);
3946 static int restart_array(struct mddev
*mddev
);
3949 array_state_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3952 enum array_state st
= match_word(buf
, array_states
);
3954 if (mddev
->pers
&& (st
== active
|| st
== clean
) && mddev
->ro
!= 1) {
3955 /* don't take reconfig_mutex when toggling between
3958 spin_lock(&mddev
->lock
);
3960 restart_array(mddev
);
3961 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
3962 md_wakeup_thread(mddev
->thread
);
3963 wake_up(&mddev
->sb_wait
);
3965 } else /* st == clean */ {
3966 restart_array(mddev
);
3967 if (atomic_read(&mddev
->writes_pending
) == 0) {
3968 if (mddev
->in_sync
== 0) {
3970 if (mddev
->safemode
== 1)
3971 mddev
->safemode
= 0;
3972 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
3979 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
3980 spin_unlock(&mddev
->lock
);
3983 err
= mddev_lock(mddev
);
3991 /* stopping an active array */
3992 err
= do_md_stop(mddev
, 0, NULL
);
3995 /* stopping an active array */
3997 err
= do_md_stop(mddev
, 2, NULL
);
3999 err
= 0; /* already inactive */
4002 break; /* not supported yet */
4005 err
= md_set_readonly(mddev
, NULL
);
4008 set_disk_ro(mddev
->gendisk
, 1);
4009 err
= do_md_run(mddev
);
4015 err
= md_set_readonly(mddev
, NULL
);
4016 else if (mddev
->ro
== 1)
4017 err
= restart_array(mddev
);
4020 set_disk_ro(mddev
->gendisk
, 0);
4024 err
= do_md_run(mddev
);
4029 err
= restart_array(mddev
);
4032 spin_lock(&mddev
->lock
);
4033 if (atomic_read(&mddev
->writes_pending
) == 0) {
4034 if (mddev
->in_sync
== 0) {
4036 if (mddev
->safemode
== 1)
4037 mddev
->safemode
= 0;
4038 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
4043 spin_unlock(&mddev
->lock
);
4049 err
= restart_array(mddev
);
4052 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
4053 wake_up(&mddev
->sb_wait
);
4057 set_disk_ro(mddev
->gendisk
, 0);
4058 err
= do_md_run(mddev
);
4063 /* these cannot be set */
4068 if (mddev
->hold_active
== UNTIL_IOCTL
)
4069 mddev
->hold_active
= 0;
4070 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4072 mddev_unlock(mddev
);
4075 static struct md_sysfs_entry md_array_state
=
4076 __ATTR_PREALLOC(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
4079 max_corrected_read_errors_show(struct mddev
*mddev
, char *page
) {
4080 return sprintf(page
, "%d\n",
4081 atomic_read(&mddev
->max_corr_read_errors
));
4085 max_corrected_read_errors_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4090 rv
= kstrtouint(buf
, 10, &n
);
4093 atomic_set(&mddev
->max_corr_read_errors
, n
);
4097 static struct md_sysfs_entry max_corr_read_errors
=
4098 __ATTR(max_read_errors
, S_IRUGO
|S_IWUSR
, max_corrected_read_errors_show
,
4099 max_corrected_read_errors_store
);
4102 null_show(struct mddev
*mddev
, char *page
)
4108 new_dev_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4110 /* buf must be %d:%d\n? giving major and minor numbers */
4111 /* The new device is added to the array.
4112 * If the array has a persistent superblock, we read the
4113 * superblock to initialise info and check validity.
4114 * Otherwise, only checking done is that in bind_rdev_to_array,
4115 * which mainly checks size.
4118 int major
= simple_strtoul(buf
, &e
, 10);
4121 struct md_rdev
*rdev
;
4124 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
4126 minor
= simple_strtoul(e
+1, &e
, 10);
4127 if (*e
&& *e
!= '\n')
4129 dev
= MKDEV(major
, minor
);
4130 if (major
!= MAJOR(dev
) ||
4131 minor
!= MINOR(dev
))
4134 flush_workqueue(md_misc_wq
);
4136 err
= mddev_lock(mddev
);
4139 if (mddev
->persistent
) {
4140 rdev
= md_import_device(dev
, mddev
->major_version
,
4141 mddev
->minor_version
);
4142 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
4143 struct md_rdev
*rdev0
4144 = list_entry(mddev
->disks
.next
,
4145 struct md_rdev
, same_set
);
4146 err
= super_types
[mddev
->major_version
]
4147 .load_super(rdev
, rdev0
, mddev
->minor_version
);
4151 } else if (mddev
->external
)
4152 rdev
= md_import_device(dev
, -2, -1);
4154 rdev
= md_import_device(dev
, -1, -1);
4157 mddev_unlock(mddev
);
4158 return PTR_ERR(rdev
);
4160 err
= bind_rdev_to_array(rdev
, mddev
);
4164 mddev_unlock(mddev
);
4165 return err
? err
: len
;
4168 static struct md_sysfs_entry md_new_device
=
4169 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
4172 bitmap_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4175 unsigned long chunk
, end_chunk
;
4178 err
= mddev_lock(mddev
);
4183 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4185 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
4186 if (buf
== end
) break;
4187 if (*end
== '-') { /* range */
4189 end_chunk
= simple_strtoul(buf
, &end
, 0);
4190 if (buf
== end
) break;
4192 if (*end
&& !isspace(*end
)) break;
4193 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
4194 buf
= skip_spaces(end
);
4196 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
4198 mddev_unlock(mddev
);
4202 static struct md_sysfs_entry md_bitmap
=
4203 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
4206 size_show(struct mddev
*mddev
, char *page
)
4208 return sprintf(page
, "%llu\n",
4209 (unsigned long long)mddev
->dev_sectors
/ 2);
4212 static int update_size(struct mddev
*mddev
, sector_t num_sectors
);
4215 size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4217 /* If array is inactive, we can reduce the component size, but
4218 * not increase it (except from 0).
4219 * If array is active, we can try an on-line resize
4222 int err
= strict_blocks_to_sectors(buf
, §ors
);
4226 err
= mddev_lock(mddev
);
4230 err
= update_size(mddev
, sectors
);
4232 md_update_sb(mddev
, 1);
4234 if (mddev
->dev_sectors
== 0 ||
4235 mddev
->dev_sectors
> sectors
)
4236 mddev
->dev_sectors
= sectors
;
4240 mddev_unlock(mddev
);
4241 return err
? err
: len
;
4244 static struct md_sysfs_entry md_size
=
4245 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
4247 /* Metadata version.
4249 * 'none' for arrays with no metadata (good luck...)
4250 * 'external' for arrays with externally managed metadata,
4251 * or N.M for internally known formats
4254 metadata_show(struct mddev
*mddev
, char *page
)
4256 if (mddev
->persistent
)
4257 return sprintf(page
, "%d.%d\n",
4258 mddev
->major_version
, mddev
->minor_version
);
4259 else if (mddev
->external
)
4260 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
4262 return sprintf(page
, "none\n");
4266 metadata_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4271 /* Changing the details of 'external' metadata is
4272 * always permitted. Otherwise there must be
4273 * no devices attached to the array.
4276 err
= mddev_lock(mddev
);
4280 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
4282 else if (!list_empty(&mddev
->disks
))
4286 if (cmd_match(buf
, "none")) {
4287 mddev
->persistent
= 0;
4288 mddev
->external
= 0;
4289 mddev
->major_version
= 0;
4290 mddev
->minor_version
= 90;
4293 if (strncmp(buf
, "external:", 9) == 0) {
4294 size_t namelen
= len
-9;
4295 if (namelen
>= sizeof(mddev
->metadata_type
))
4296 namelen
= sizeof(mddev
->metadata_type
)-1;
4297 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
4298 mddev
->metadata_type
[namelen
] = 0;
4299 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
4300 mddev
->metadata_type
[--namelen
] = 0;
4301 mddev
->persistent
= 0;
4302 mddev
->external
= 1;
4303 mddev
->major_version
= 0;
4304 mddev
->minor_version
= 90;
4307 major
= simple_strtoul(buf
, &e
, 10);
4309 if (e
==buf
|| *e
!= '.')
4312 minor
= simple_strtoul(buf
, &e
, 10);
4313 if (e
==buf
|| (*e
&& *e
!= '\n') )
4316 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
4318 mddev
->major_version
= major
;
4319 mddev
->minor_version
= minor
;
4320 mddev
->persistent
= 1;
4321 mddev
->external
= 0;
4324 mddev_unlock(mddev
);
4328 static struct md_sysfs_entry md_metadata
=
4329 __ATTR_PREALLOC(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
4332 action_show(struct mddev
*mddev
, char *page
)
4334 char *type
= "idle";
4335 unsigned long recovery
= mddev
->recovery
;
4336 if (test_bit(MD_RECOVERY_FROZEN
, &recovery
))
4338 else if (test_bit(MD_RECOVERY_RUNNING
, &recovery
) ||
4339 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &recovery
))) {
4340 if (test_bit(MD_RECOVERY_RESHAPE
, &recovery
))
4342 else if (test_bit(MD_RECOVERY_SYNC
, &recovery
)) {
4343 if (!test_bit(MD_RECOVERY_REQUESTED
, &recovery
))
4345 else if (test_bit(MD_RECOVERY_CHECK
, &recovery
))
4349 } else if (test_bit(MD_RECOVERY_RECOVER
, &recovery
))
4351 else if (mddev
->reshape_position
!= MaxSector
)
4354 return sprintf(page
, "%s\n", type
);
4358 action_store(struct mddev
*mddev
, const char *page
, size_t len
)
4360 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
4364 if (cmd_match(page
, "idle") || cmd_match(page
, "frozen")) {
4365 if (cmd_match(page
, "frozen"))
4366 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4368 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4369 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
4370 mddev_lock(mddev
) == 0) {
4371 flush_workqueue(md_misc_wq
);
4372 if (mddev
->sync_thread
) {
4373 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4374 md_reap_sync_thread(mddev
);
4376 mddev_unlock(mddev
);
4378 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4380 else if (cmd_match(page
, "resync"))
4381 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4382 else if (cmd_match(page
, "recover")) {
4383 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4384 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
4385 } else if (cmd_match(page
, "reshape")) {
4387 if (mddev
->pers
->start_reshape
== NULL
)
4389 err
= mddev_lock(mddev
);
4391 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4394 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4395 err
= mddev
->pers
->start_reshape(mddev
);
4397 mddev_unlock(mddev
);
4401 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4403 if (cmd_match(page
, "check"))
4404 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
4405 else if (!cmd_match(page
, "repair"))
4407 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4408 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
4409 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
4411 if (mddev
->ro
== 2) {
4412 /* A write to sync_action is enough to justify
4413 * canceling read-auto mode
4416 md_wakeup_thread(mddev
->sync_thread
);
4418 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4419 md_wakeup_thread(mddev
->thread
);
4420 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4424 static struct md_sysfs_entry md_scan_mode
=
4425 __ATTR_PREALLOC(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
4428 last_sync_action_show(struct mddev
*mddev
, char *page
)
4430 return sprintf(page
, "%s\n", mddev
->last_sync_action
);
4433 static struct md_sysfs_entry md_last_scan_mode
= __ATTR_RO(last_sync_action
);
4436 mismatch_cnt_show(struct mddev
*mddev
, char *page
)
4438 return sprintf(page
, "%llu\n",
4439 (unsigned long long)
4440 atomic64_read(&mddev
->resync_mismatches
));
4443 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
4446 sync_min_show(struct mddev
*mddev
, char *page
)
4448 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
4449 mddev
->sync_speed_min
? "local": "system");
4453 sync_min_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4458 if (strncmp(buf
, "system", 6)==0) {
4461 rv
= kstrtouint(buf
, 10, &min
);
4467 mddev
->sync_speed_min
= min
;
4471 static struct md_sysfs_entry md_sync_min
=
4472 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
4475 sync_max_show(struct mddev
*mddev
, char *page
)
4477 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
4478 mddev
->sync_speed_max
? "local": "system");
4482 sync_max_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4487 if (strncmp(buf
, "system", 6)==0) {
4490 rv
= kstrtouint(buf
, 10, &max
);
4496 mddev
->sync_speed_max
= max
;
4500 static struct md_sysfs_entry md_sync_max
=
4501 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
4504 degraded_show(struct mddev
*mddev
, char *page
)
4506 return sprintf(page
, "%d\n", mddev
->degraded
);
4508 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
4511 sync_force_parallel_show(struct mddev
*mddev
, char *page
)
4513 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
4517 sync_force_parallel_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4521 if (kstrtol(buf
, 10, &n
))
4524 if (n
!= 0 && n
!= 1)
4527 mddev
->parallel_resync
= n
;
4529 if (mddev
->sync_thread
)
4530 wake_up(&resync_wait
);
4535 /* force parallel resync, even with shared block devices */
4536 static struct md_sysfs_entry md_sync_force_parallel
=
4537 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
4538 sync_force_parallel_show
, sync_force_parallel_store
);
4541 sync_speed_show(struct mddev
*mddev
, char *page
)
4543 unsigned long resync
, dt
, db
;
4544 if (mddev
->curr_resync
== 0)
4545 return sprintf(page
, "none\n");
4546 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
4547 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
4549 db
= resync
- mddev
->resync_mark_cnt
;
4550 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
4553 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
4556 sync_completed_show(struct mddev
*mddev
, char *page
)
4558 unsigned long long max_sectors
, resync
;
4560 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4561 return sprintf(page
, "none\n");
4563 if (mddev
->curr_resync
== 1 ||
4564 mddev
->curr_resync
== 2)
4565 return sprintf(page
, "delayed\n");
4567 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
4568 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
4569 max_sectors
= mddev
->resync_max_sectors
;
4571 max_sectors
= mddev
->dev_sectors
;
4573 resync
= mddev
->curr_resync_completed
;
4574 return sprintf(page
, "%llu / %llu\n", resync
, max_sectors
);
4577 static struct md_sysfs_entry md_sync_completed
=
4578 __ATTR_PREALLOC(sync_completed
, S_IRUGO
, sync_completed_show
, NULL
);
4581 min_sync_show(struct mddev
*mddev
, char *page
)
4583 return sprintf(page
, "%llu\n",
4584 (unsigned long long)mddev
->resync_min
);
4587 min_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4589 unsigned long long min
;
4592 if (kstrtoull(buf
, 10, &min
))
4595 spin_lock(&mddev
->lock
);
4597 if (min
> mddev
->resync_max
)
4601 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4604 /* Round down to multiple of 4K for safety */
4605 mddev
->resync_min
= round_down(min
, 8);
4609 spin_unlock(&mddev
->lock
);
4613 static struct md_sysfs_entry md_min_sync
=
4614 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
4617 max_sync_show(struct mddev
*mddev
, char *page
)
4619 if (mddev
->resync_max
== MaxSector
)
4620 return sprintf(page
, "max\n");
4622 return sprintf(page
, "%llu\n",
4623 (unsigned long long)mddev
->resync_max
);
4626 max_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4629 spin_lock(&mddev
->lock
);
4630 if (strncmp(buf
, "max", 3) == 0)
4631 mddev
->resync_max
= MaxSector
;
4633 unsigned long long max
;
4637 if (kstrtoull(buf
, 10, &max
))
4639 if (max
< mddev
->resync_min
)
4643 if (max
< mddev
->resync_max
&&
4645 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4648 /* Must be a multiple of chunk_size */
4649 chunk
= mddev
->chunk_sectors
;
4651 sector_t temp
= max
;
4654 if (sector_div(temp
, chunk
))
4657 mddev
->resync_max
= max
;
4659 wake_up(&mddev
->recovery_wait
);
4662 spin_unlock(&mddev
->lock
);
4666 static struct md_sysfs_entry md_max_sync
=
4667 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
4670 suspend_lo_show(struct mddev
*mddev
, char *page
)
4672 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
4676 suspend_lo_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4678 unsigned long long old
, new;
4681 err
= kstrtoull(buf
, 10, &new);
4684 if (new != (sector_t
)new)
4687 err
= mddev_lock(mddev
);
4691 if (mddev
->pers
== NULL
||
4692 mddev
->pers
->quiesce
== NULL
)
4694 old
= mddev
->suspend_lo
;
4695 mddev
->suspend_lo
= new;
4697 /* Shrinking suspended region */
4698 mddev
->pers
->quiesce(mddev
, 2);
4700 /* Expanding suspended region - need to wait */
4701 mddev
->pers
->quiesce(mddev
, 1);
4702 mddev
->pers
->quiesce(mddev
, 0);
4706 mddev_unlock(mddev
);
4709 static struct md_sysfs_entry md_suspend_lo
=
4710 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
4713 suspend_hi_show(struct mddev
*mddev
, char *page
)
4715 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
4719 suspend_hi_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4721 unsigned long long old
, new;
4724 err
= kstrtoull(buf
, 10, &new);
4727 if (new != (sector_t
)new)
4730 err
= mddev_lock(mddev
);
4734 if (mddev
->pers
== NULL
||
4735 mddev
->pers
->quiesce
== NULL
)
4737 old
= mddev
->suspend_hi
;
4738 mddev
->suspend_hi
= new;
4740 /* Shrinking suspended region */
4741 mddev
->pers
->quiesce(mddev
, 2);
4743 /* Expanding suspended region - need to wait */
4744 mddev
->pers
->quiesce(mddev
, 1);
4745 mddev
->pers
->quiesce(mddev
, 0);
4749 mddev_unlock(mddev
);
4752 static struct md_sysfs_entry md_suspend_hi
=
4753 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
4756 reshape_position_show(struct mddev
*mddev
, char *page
)
4758 if (mddev
->reshape_position
!= MaxSector
)
4759 return sprintf(page
, "%llu\n",
4760 (unsigned long long)mddev
->reshape_position
);
4761 strcpy(page
, "none\n");
4766 reshape_position_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4768 struct md_rdev
*rdev
;
4769 unsigned long long new;
4772 err
= kstrtoull(buf
, 10, &new);
4775 if (new != (sector_t
)new)
4777 err
= mddev_lock(mddev
);
4783 mddev
->reshape_position
= new;
4784 mddev
->delta_disks
= 0;
4785 mddev
->reshape_backwards
= 0;
4786 mddev
->new_level
= mddev
->level
;
4787 mddev
->new_layout
= mddev
->layout
;
4788 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4789 rdev_for_each(rdev
, mddev
)
4790 rdev
->new_data_offset
= rdev
->data_offset
;
4793 mddev_unlock(mddev
);
4797 static struct md_sysfs_entry md_reshape_position
=
4798 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
4799 reshape_position_store
);
4802 reshape_direction_show(struct mddev
*mddev
, char *page
)
4804 return sprintf(page
, "%s\n",
4805 mddev
->reshape_backwards
? "backwards" : "forwards");
4809 reshape_direction_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4814 if (cmd_match(buf
, "forwards"))
4816 else if (cmd_match(buf
, "backwards"))
4820 if (mddev
->reshape_backwards
== backwards
)
4823 err
= mddev_lock(mddev
);
4826 /* check if we are allowed to change */
4827 if (mddev
->delta_disks
)
4829 else if (mddev
->persistent
&&
4830 mddev
->major_version
== 0)
4833 mddev
->reshape_backwards
= backwards
;
4834 mddev_unlock(mddev
);
4838 static struct md_sysfs_entry md_reshape_direction
=
4839 __ATTR(reshape_direction
, S_IRUGO
|S_IWUSR
, reshape_direction_show
,
4840 reshape_direction_store
);
4843 array_size_show(struct mddev
*mddev
, char *page
)
4845 if (mddev
->external_size
)
4846 return sprintf(page
, "%llu\n",
4847 (unsigned long long)mddev
->array_sectors
/2);
4849 return sprintf(page
, "default\n");
4853 array_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4858 err
= mddev_lock(mddev
);
4862 /* cluster raid doesn't support change array_sectors */
4863 if (mddev_is_clustered(mddev
))
4866 if (strncmp(buf
, "default", 7) == 0) {
4868 sectors
= mddev
->pers
->size(mddev
, 0, 0);
4870 sectors
= mddev
->array_sectors
;
4872 mddev
->external_size
= 0;
4874 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
4876 else if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
4879 mddev
->external_size
= 1;
4883 mddev
->array_sectors
= sectors
;
4885 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4886 revalidate_disk(mddev
->gendisk
);
4889 mddev_unlock(mddev
);
4893 static struct md_sysfs_entry md_array_size
=
4894 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
4897 static struct attribute
*md_default_attrs
[] = {
4900 &md_raid_disks
.attr
,
4901 &md_chunk_size
.attr
,
4903 &md_resync_start
.attr
,
4905 &md_new_device
.attr
,
4906 &md_safe_delay
.attr
,
4907 &md_array_state
.attr
,
4908 &md_reshape_position
.attr
,
4909 &md_reshape_direction
.attr
,
4910 &md_array_size
.attr
,
4911 &max_corr_read_errors
.attr
,
4915 static struct attribute
*md_redundancy_attrs
[] = {
4917 &md_last_scan_mode
.attr
,
4918 &md_mismatches
.attr
,
4921 &md_sync_speed
.attr
,
4922 &md_sync_force_parallel
.attr
,
4923 &md_sync_completed
.attr
,
4926 &md_suspend_lo
.attr
,
4927 &md_suspend_hi
.attr
,
4932 static struct attribute_group md_redundancy_group
= {
4934 .attrs
= md_redundancy_attrs
,
4938 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
4940 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4941 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
4946 spin_lock(&all_mddevs_lock
);
4947 if (list_empty(&mddev
->all_mddevs
)) {
4948 spin_unlock(&all_mddevs_lock
);
4952 spin_unlock(&all_mddevs_lock
);
4954 rv
= entry
->show(mddev
, page
);
4960 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
4961 const char *page
, size_t length
)
4963 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4964 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
4969 if (!capable(CAP_SYS_ADMIN
))
4971 spin_lock(&all_mddevs_lock
);
4972 if (list_empty(&mddev
->all_mddevs
)) {
4973 spin_unlock(&all_mddevs_lock
);
4977 spin_unlock(&all_mddevs_lock
);
4978 rv
= entry
->store(mddev
, page
, length
);
4983 static void md_free(struct kobject
*ko
)
4985 struct mddev
*mddev
= container_of(ko
, struct mddev
, kobj
);
4987 if (mddev
->sysfs_state
)
4988 sysfs_put(mddev
->sysfs_state
);
4991 blk_cleanup_queue(mddev
->queue
);
4992 if (mddev
->gendisk
) {
4993 del_gendisk(mddev
->gendisk
);
4994 put_disk(mddev
->gendisk
);
5000 static const struct sysfs_ops md_sysfs_ops
= {
5001 .show
= md_attr_show
,
5002 .store
= md_attr_store
,
5004 static struct kobj_type md_ktype
= {
5006 .sysfs_ops
= &md_sysfs_ops
,
5007 .default_attrs
= md_default_attrs
,
5012 static void mddev_delayed_delete(struct work_struct
*ws
)
5014 struct mddev
*mddev
= container_of(ws
, struct mddev
, del_work
);
5016 sysfs_remove_group(&mddev
->kobj
, &md_bitmap_group
);
5017 kobject_del(&mddev
->kobj
);
5018 kobject_put(&mddev
->kobj
);
5021 static int md_alloc(dev_t dev
, char *name
)
5023 static DEFINE_MUTEX(disks_mutex
);
5024 struct mddev
*mddev
= mddev_find(dev
);
5025 struct gendisk
*disk
;
5034 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
5035 shift
= partitioned
? MdpMinorShift
: 0;
5036 unit
= MINOR(mddev
->unit
) >> shift
;
5038 /* wait for any previous instance of this device to be
5039 * completely removed (mddev_delayed_delete).
5041 flush_workqueue(md_misc_wq
);
5043 mutex_lock(&disks_mutex
);
5049 /* Need to ensure that 'name' is not a duplicate.
5051 struct mddev
*mddev2
;
5052 spin_lock(&all_mddevs_lock
);
5054 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
5055 if (mddev2
->gendisk
&&
5056 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
5057 spin_unlock(&all_mddevs_lock
);
5060 spin_unlock(&all_mddevs_lock
);
5064 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
5067 mddev
->queue
->queuedata
= mddev
;
5069 blk_queue_make_request(mddev
->queue
, md_make_request
);
5070 blk_set_stacking_limits(&mddev
->queue
->limits
);
5072 disk
= alloc_disk(1 << shift
);
5074 blk_cleanup_queue(mddev
->queue
);
5075 mddev
->queue
= NULL
;
5078 disk
->major
= MAJOR(mddev
->unit
);
5079 disk
->first_minor
= unit
<< shift
;
5081 strcpy(disk
->disk_name
, name
);
5082 else if (partitioned
)
5083 sprintf(disk
->disk_name
, "md_d%d", unit
);
5085 sprintf(disk
->disk_name
, "md%d", unit
);
5086 disk
->fops
= &md_fops
;
5087 disk
->private_data
= mddev
;
5088 disk
->queue
= mddev
->queue
;
5089 blk_queue_write_cache(mddev
->queue
, true, true);
5090 /* Allow extended partitions. This makes the
5091 * 'mdp' device redundant, but we can't really
5094 disk
->flags
|= GENHD_FL_EXT_DEVT
;
5095 mddev
->gendisk
= disk
;
5096 /* As soon as we call add_disk(), another thread could get
5097 * through to md_open, so make sure it doesn't get too far
5099 mutex_lock(&mddev
->open_mutex
);
5102 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
,
5103 &disk_to_dev(disk
)->kobj
, "%s", "md");
5105 /* This isn't possible, but as kobject_init_and_add is marked
5106 * __must_check, we must do something with the result
5108 pr_debug("md: cannot register %s/md - name in use\n",
5112 if (mddev
->kobj
.sd
&&
5113 sysfs_create_group(&mddev
->kobj
, &md_bitmap_group
))
5114 pr_debug("pointless warning\n");
5115 mutex_unlock(&mddev
->open_mutex
);
5117 mutex_unlock(&disks_mutex
);
5118 if (!error
&& mddev
->kobj
.sd
) {
5119 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
5120 mddev
->sysfs_state
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "array_state");
5126 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
5128 md_alloc(dev
, NULL
);
5132 static int add_named_array(const char *val
, struct kernel_param
*kp
)
5134 /* val must be "md_*" where * is not all digits.
5135 * We allocate an array with a large free minor number, and
5136 * set the name to val. val must not already be an active name.
5138 int len
= strlen(val
);
5139 char buf
[DISK_NAME_LEN
];
5141 while (len
&& val
[len
-1] == '\n')
5143 if (len
>= DISK_NAME_LEN
)
5145 strlcpy(buf
, val
, len
+1);
5146 if (strncmp(buf
, "md_", 3) != 0)
5148 return md_alloc(0, buf
);
5151 static void md_safemode_timeout(unsigned long data
)
5153 struct mddev
*mddev
= (struct mddev
*) data
;
5155 if (!atomic_read(&mddev
->writes_pending
)) {
5156 mddev
->safemode
= 1;
5157 if (mddev
->external
)
5158 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5160 md_wakeup_thread(mddev
->thread
);
5163 static int start_dirty_degraded
;
5165 int md_run(struct mddev
*mddev
)
5168 struct md_rdev
*rdev
;
5169 struct md_personality
*pers
;
5171 if (list_empty(&mddev
->disks
))
5172 /* cannot run an array with no devices.. */
5177 /* Cannot run until previous stop completes properly */
5178 if (mddev
->sysfs_active
)
5182 * Analyze all RAID superblock(s)
5184 if (!mddev
->raid_disks
) {
5185 if (!mddev
->persistent
)
5190 if (mddev
->level
!= LEVEL_NONE
)
5191 request_module("md-level-%d", mddev
->level
);
5192 else if (mddev
->clevel
[0])
5193 request_module("md-%s", mddev
->clevel
);
5196 * Drop all container device buffers, from now on
5197 * the only valid external interface is through the md
5200 rdev_for_each(rdev
, mddev
) {
5201 if (test_bit(Faulty
, &rdev
->flags
))
5203 sync_blockdev(rdev
->bdev
);
5204 invalidate_bdev(rdev
->bdev
);
5206 /* perform some consistency tests on the device.
5207 * We don't want the data to overlap the metadata,
5208 * Internal Bitmap issues have been handled elsewhere.
5210 if (rdev
->meta_bdev
) {
5211 /* Nothing to check */;
5212 } else if (rdev
->data_offset
< rdev
->sb_start
) {
5213 if (mddev
->dev_sectors
&&
5214 rdev
->data_offset
+ mddev
->dev_sectors
5216 pr_warn("md: %s: data overlaps metadata\n",
5221 if (rdev
->sb_start
+ rdev
->sb_size
/512
5222 > rdev
->data_offset
) {
5223 pr_warn("md: %s: metadata overlaps data\n",
5228 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
5231 if (mddev
->bio_set
== NULL
)
5232 mddev
->bio_set
= bioset_create(BIO_POOL_SIZE
, 0);
5234 spin_lock(&pers_lock
);
5235 pers
= find_pers(mddev
->level
, mddev
->clevel
);
5236 if (!pers
|| !try_module_get(pers
->owner
)) {
5237 spin_unlock(&pers_lock
);
5238 if (mddev
->level
!= LEVEL_NONE
)
5239 pr_warn("md: personality for level %d is not loaded!\n",
5242 pr_warn("md: personality for level %s is not loaded!\n",
5246 spin_unlock(&pers_lock
);
5247 if (mddev
->level
!= pers
->level
) {
5248 mddev
->level
= pers
->level
;
5249 mddev
->new_level
= pers
->level
;
5251 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
5253 if (mddev
->reshape_position
!= MaxSector
&&
5254 pers
->start_reshape
== NULL
) {
5255 /* This personality cannot handle reshaping... */
5256 module_put(pers
->owner
);
5260 if (pers
->sync_request
) {
5261 /* Warn if this is a potentially silly
5264 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
5265 struct md_rdev
*rdev2
;
5268 rdev_for_each(rdev
, mddev
)
5269 rdev_for_each(rdev2
, mddev
) {
5271 rdev
->bdev
->bd_contains
==
5272 rdev2
->bdev
->bd_contains
) {
5273 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5275 bdevname(rdev
->bdev
,b
),
5276 bdevname(rdev2
->bdev
,b2
));
5282 pr_warn("True protection against single-disk failure might be compromised.\n");
5285 mddev
->recovery
= 0;
5286 /* may be over-ridden by personality */
5287 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
5289 mddev
->ok_start_degraded
= start_dirty_degraded
;
5291 if (start_readonly
&& mddev
->ro
== 0)
5292 mddev
->ro
= 2; /* read-only, but switch on first write */
5294 err
= pers
->run(mddev
);
5296 pr_warn("md: pers->run() failed ...\n");
5297 else if (pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
5298 WARN_ONCE(!mddev
->external_size
,
5299 "%s: default size too small, but 'external_size' not in effect?\n",
5301 pr_warn("md: invalid array_size %llu > default size %llu\n",
5302 (unsigned long long)mddev
->array_sectors
/ 2,
5303 (unsigned long long)pers
->size(mddev
, 0, 0) / 2);
5306 if (err
== 0 && pers
->sync_request
&&
5307 (mddev
->bitmap_info
.file
|| mddev
->bitmap_info
.offset
)) {
5308 struct bitmap
*bitmap
;
5310 bitmap
= bitmap_create(mddev
, -1);
5311 if (IS_ERR(bitmap
)) {
5312 err
= PTR_ERR(bitmap
);
5313 pr_warn("%s: failed to create bitmap (%d)\n",
5314 mdname(mddev
), err
);
5316 mddev
->bitmap
= bitmap
;
5320 mddev_detach(mddev
);
5322 pers
->free(mddev
, mddev
->private);
5323 mddev
->private = NULL
;
5324 module_put(pers
->owner
);
5325 bitmap_destroy(mddev
);
5331 rdev_for_each(rdev
, mddev
) {
5332 if (rdev
->raid_disk
>= 0 &&
5333 !blk_queue_nonrot(bdev_get_queue(rdev
->bdev
))) {
5338 if (mddev
->degraded
)
5341 queue_flag_set_unlocked(QUEUE_FLAG_NONROT
, mddev
->queue
);
5343 queue_flag_clear_unlocked(QUEUE_FLAG_NONROT
, mddev
->queue
);
5344 mddev
->queue
->backing_dev_info
.congested_data
= mddev
;
5345 mddev
->queue
->backing_dev_info
.congested_fn
= md_congested
;
5347 if (pers
->sync_request
) {
5348 if (mddev
->kobj
.sd
&&
5349 sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
5350 pr_warn("md: cannot register extra attributes for %s\n",
5352 mddev
->sysfs_action
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "sync_action");
5353 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
5356 atomic_set(&mddev
->writes_pending
,0);
5357 atomic_set(&mddev
->max_corr_read_errors
,
5358 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS
);
5359 mddev
->safemode
= 0;
5360 if (mddev_is_clustered(mddev
))
5361 mddev
->safemode_delay
= 0;
5363 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
5366 spin_lock(&mddev
->lock
);
5368 spin_unlock(&mddev
->lock
);
5369 rdev_for_each(rdev
, mddev
)
5370 if (rdev
->raid_disk
>= 0)
5371 if (sysfs_link_rdev(mddev
, rdev
))
5372 /* failure here is OK */;
5374 if (mddev
->degraded
&& !mddev
->ro
)
5375 /* This ensures that recovering status is reported immediately
5376 * via sysfs - until a lack of spares is confirmed.
5378 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5379 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5381 if (mddev
->sb_flags
)
5382 md_update_sb(mddev
, 0);
5384 md_new_event(mddev
);
5385 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5386 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
5387 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
5390 EXPORT_SYMBOL_GPL(md_run
);
5392 static int do_md_run(struct mddev
*mddev
)
5396 err
= md_run(mddev
);
5399 err
= bitmap_load(mddev
);
5401 bitmap_destroy(mddev
);
5405 if (mddev_is_clustered(mddev
))
5406 md_allow_write(mddev
);
5408 md_wakeup_thread(mddev
->thread
);
5409 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
5411 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
5412 revalidate_disk(mddev
->gendisk
);
5414 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
5419 static int restart_array(struct mddev
*mddev
)
5421 struct gendisk
*disk
= mddev
->gendisk
;
5423 /* Complain if it has no devices */
5424 if (list_empty(&mddev
->disks
))
5430 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
)) {
5431 struct md_rdev
*rdev
;
5432 bool has_journal
= false;
5435 rdev_for_each_rcu(rdev
, mddev
) {
5436 if (test_bit(Journal
, &rdev
->flags
) &&
5437 !test_bit(Faulty
, &rdev
->flags
)) {
5444 /* Don't restart rw with journal missing/faulty */
5449 mddev
->safemode
= 0;
5451 set_disk_ro(disk
, 0);
5452 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev
));
5453 /* Kick recovery or resync if necessary */
5454 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5455 md_wakeup_thread(mddev
->thread
);
5456 md_wakeup_thread(mddev
->sync_thread
);
5457 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5461 static void md_clean(struct mddev
*mddev
)
5463 mddev
->array_sectors
= 0;
5464 mddev
->external_size
= 0;
5465 mddev
->dev_sectors
= 0;
5466 mddev
->raid_disks
= 0;
5467 mddev
->recovery_cp
= 0;
5468 mddev
->resync_min
= 0;
5469 mddev
->resync_max
= MaxSector
;
5470 mddev
->reshape_position
= MaxSector
;
5471 mddev
->external
= 0;
5472 mddev
->persistent
= 0;
5473 mddev
->level
= LEVEL_NONE
;
5474 mddev
->clevel
[0] = 0;
5476 mddev
->sb_flags
= 0;
5478 mddev
->metadata_type
[0] = 0;
5479 mddev
->chunk_sectors
= 0;
5480 mddev
->ctime
= mddev
->utime
= 0;
5482 mddev
->max_disks
= 0;
5484 mddev
->can_decrease_events
= 0;
5485 mddev
->delta_disks
= 0;
5486 mddev
->reshape_backwards
= 0;
5487 mddev
->new_level
= LEVEL_NONE
;
5488 mddev
->new_layout
= 0;
5489 mddev
->new_chunk_sectors
= 0;
5490 mddev
->curr_resync
= 0;
5491 atomic64_set(&mddev
->resync_mismatches
, 0);
5492 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
5493 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
5494 mddev
->recovery
= 0;
5497 mddev
->degraded
= 0;
5498 mddev
->safemode
= 0;
5499 mddev
->private = NULL
;
5500 mddev
->cluster_info
= NULL
;
5501 mddev
->bitmap_info
.offset
= 0;
5502 mddev
->bitmap_info
.default_offset
= 0;
5503 mddev
->bitmap_info
.default_space
= 0;
5504 mddev
->bitmap_info
.chunksize
= 0;
5505 mddev
->bitmap_info
.daemon_sleep
= 0;
5506 mddev
->bitmap_info
.max_write_behind
= 0;
5507 mddev
->bitmap_info
.nodes
= 0;
5510 static void __md_stop_writes(struct mddev
*mddev
)
5512 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5513 flush_workqueue(md_misc_wq
);
5514 if (mddev
->sync_thread
) {
5515 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5516 md_reap_sync_thread(mddev
);
5519 del_timer_sync(&mddev
->safemode_timer
);
5521 if (mddev
->pers
&& mddev
->pers
->quiesce
) {
5522 mddev
->pers
->quiesce(mddev
, 1);
5523 mddev
->pers
->quiesce(mddev
, 0);
5525 bitmap_flush(mddev
);
5527 if (mddev
->ro
== 0 &&
5528 ((!mddev
->in_sync
&& !mddev_is_clustered(mddev
)) ||
5530 /* mark array as shutdown cleanly */
5531 if (!mddev_is_clustered(mddev
))
5533 md_update_sb(mddev
, 1);
5537 void md_stop_writes(struct mddev
*mddev
)
5539 mddev_lock_nointr(mddev
);
5540 __md_stop_writes(mddev
);
5541 mddev_unlock(mddev
);
5543 EXPORT_SYMBOL_GPL(md_stop_writes
);
5545 static void mddev_detach(struct mddev
*mddev
)
5547 struct bitmap
*bitmap
= mddev
->bitmap
;
5548 /* wait for behind writes to complete */
5549 if (bitmap
&& atomic_read(&bitmap
->behind_writes
) > 0) {
5550 pr_debug("md:%s: behind writes in progress - waiting to stop.\n",
5552 /* need to kick something here to make sure I/O goes? */
5553 wait_event(bitmap
->behind_wait
,
5554 atomic_read(&bitmap
->behind_writes
) == 0);
5556 if (mddev
->pers
&& mddev
->pers
->quiesce
) {
5557 mddev
->pers
->quiesce(mddev
, 1);
5558 mddev
->pers
->quiesce(mddev
, 0);
5560 md_unregister_thread(&mddev
->thread
);
5562 blk_sync_queue(mddev
->queue
); /* the unplug fn references 'conf'*/
5565 static void __md_stop(struct mddev
*mddev
)
5567 struct md_personality
*pers
= mddev
->pers
;
5568 mddev_detach(mddev
);
5569 /* Ensure ->event_work is done */
5570 flush_workqueue(md_misc_wq
);
5571 spin_lock(&mddev
->lock
);
5573 spin_unlock(&mddev
->lock
);
5574 pers
->free(mddev
, mddev
->private);
5575 mddev
->private = NULL
;
5576 if (pers
->sync_request
&& mddev
->to_remove
== NULL
)
5577 mddev
->to_remove
= &md_redundancy_group
;
5578 module_put(pers
->owner
);
5579 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5582 void md_stop(struct mddev
*mddev
)
5584 /* stop the array and free an attached data structures.
5585 * This is called from dm-raid
5588 bitmap_destroy(mddev
);
5590 bioset_free(mddev
->bio_set
);
5593 EXPORT_SYMBOL_GPL(md_stop
);
5595 static int md_set_readonly(struct mddev
*mddev
, struct block_device
*bdev
)
5600 if (!test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
)) {
5602 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5603 md_wakeup_thread(mddev
->thread
);
5605 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
5606 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5607 if (mddev
->sync_thread
)
5608 /* Thread might be blocked waiting for metadata update
5609 * which will now never happen */
5610 wake_up_process(mddev
->sync_thread
->tsk
);
5612 if (mddev
->external
&& test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
))
5614 mddev_unlock(mddev
);
5615 wait_event(resync_wait
, !test_bit(MD_RECOVERY_RUNNING
,
5617 wait_event(mddev
->sb_wait
,
5618 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
5619 mddev_lock_nointr(mddev
);
5621 mutex_lock(&mddev
->open_mutex
);
5622 if ((mddev
->pers
&& atomic_read(&mddev
->openers
) > !!bdev
) ||
5623 mddev
->sync_thread
||
5624 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
)) {
5625 pr_warn("md: %s still in use.\n",mdname(mddev
));
5627 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5628 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5629 md_wakeup_thread(mddev
->thread
);
5635 __md_stop_writes(mddev
);
5641 set_disk_ro(mddev
->gendisk
, 1);
5642 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5643 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5644 md_wakeup_thread(mddev
->thread
);
5645 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5649 mutex_unlock(&mddev
->open_mutex
);
5654 * 0 - completely stop and dis-assemble array
5655 * 2 - stop but do not disassemble array
5657 static int do_md_stop(struct mddev
*mddev
, int mode
,
5658 struct block_device
*bdev
)
5660 struct gendisk
*disk
= mddev
->gendisk
;
5661 struct md_rdev
*rdev
;
5664 if (!test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
)) {
5666 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5667 md_wakeup_thread(mddev
->thread
);
5669 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
5670 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5671 if (mddev
->sync_thread
)
5672 /* Thread might be blocked waiting for metadata update
5673 * which will now never happen */
5674 wake_up_process(mddev
->sync_thread
->tsk
);
5676 mddev_unlock(mddev
);
5677 wait_event(resync_wait
, (mddev
->sync_thread
== NULL
&&
5678 !test_bit(MD_RECOVERY_RUNNING
,
5679 &mddev
->recovery
)));
5680 mddev_lock_nointr(mddev
);
5682 mutex_lock(&mddev
->open_mutex
);
5683 if ((mddev
->pers
&& atomic_read(&mddev
->openers
) > !!bdev
) ||
5684 mddev
->sysfs_active
||
5685 mddev
->sync_thread
||
5686 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
)) {
5687 pr_warn("md: %s still in use.\n",mdname(mddev
));
5688 mutex_unlock(&mddev
->open_mutex
);
5690 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5691 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5692 md_wakeup_thread(mddev
->thread
);
5698 set_disk_ro(disk
, 0);
5700 __md_stop_writes(mddev
);
5702 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
5704 /* tell userspace to handle 'inactive' */
5705 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5707 rdev_for_each(rdev
, mddev
)
5708 if (rdev
->raid_disk
>= 0)
5709 sysfs_unlink_rdev(mddev
, rdev
);
5711 set_capacity(disk
, 0);
5712 mutex_unlock(&mddev
->open_mutex
);
5714 revalidate_disk(disk
);
5719 mutex_unlock(&mddev
->open_mutex
);
5721 * Free resources if final stop
5724 pr_info("md: %s stopped.\n", mdname(mddev
));
5726 bitmap_destroy(mddev
);
5727 if (mddev
->bitmap_info
.file
) {
5728 struct file
*f
= mddev
->bitmap_info
.file
;
5729 spin_lock(&mddev
->lock
);
5730 mddev
->bitmap_info
.file
= NULL
;
5731 spin_unlock(&mddev
->lock
);
5734 mddev
->bitmap_info
.offset
= 0;
5736 export_array(mddev
);
5739 if (mddev
->hold_active
== UNTIL_STOP
)
5740 mddev
->hold_active
= 0;
5742 md_new_event(mddev
);
5743 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5748 static void autorun_array(struct mddev
*mddev
)
5750 struct md_rdev
*rdev
;
5753 if (list_empty(&mddev
->disks
))
5756 pr_info("md: running: ");
5758 rdev_for_each(rdev
, mddev
) {
5759 char b
[BDEVNAME_SIZE
];
5760 pr_cont("<%s>", bdevname(rdev
->bdev
,b
));
5764 err
= do_md_run(mddev
);
5766 pr_warn("md: do_md_run() returned %d\n", err
);
5767 do_md_stop(mddev
, 0, NULL
);
5772 * lets try to run arrays based on all disks that have arrived
5773 * until now. (those are in pending_raid_disks)
5775 * the method: pick the first pending disk, collect all disks with
5776 * the same UUID, remove all from the pending list and put them into
5777 * the 'same_array' list. Then order this list based on superblock
5778 * update time (freshest comes first), kick out 'old' disks and
5779 * compare superblocks. If everything's fine then run it.
5781 * If "unit" is allocated, then bump its reference count
5783 static void autorun_devices(int part
)
5785 struct md_rdev
*rdev0
, *rdev
, *tmp
;
5786 struct mddev
*mddev
;
5787 char b
[BDEVNAME_SIZE
];
5789 pr_info("md: autorun ...\n");
5790 while (!list_empty(&pending_raid_disks
)) {
5793 LIST_HEAD(candidates
);
5794 rdev0
= list_entry(pending_raid_disks
.next
,
5795 struct md_rdev
, same_set
);
5797 pr_debug("md: considering %s ...\n", bdevname(rdev0
->bdev
,b
));
5798 INIT_LIST_HEAD(&candidates
);
5799 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
5800 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
5801 pr_debug("md: adding %s ...\n",
5802 bdevname(rdev
->bdev
,b
));
5803 list_move(&rdev
->same_set
, &candidates
);
5806 * now we have a set of devices, with all of them having
5807 * mostly sane superblocks. It's time to allocate the
5811 dev
= MKDEV(mdp_major
,
5812 rdev0
->preferred_minor
<< MdpMinorShift
);
5813 unit
= MINOR(dev
) >> MdpMinorShift
;
5815 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
5818 if (rdev0
->preferred_minor
!= unit
) {
5819 pr_warn("md: unit number in %s is bad: %d\n",
5820 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
5824 md_probe(dev
, NULL
, NULL
);
5825 mddev
= mddev_find(dev
);
5826 if (!mddev
|| !mddev
->gendisk
) {
5831 if (mddev_lock(mddev
))
5832 pr_warn("md: %s locked, cannot run\n", mdname(mddev
));
5833 else if (mddev
->raid_disks
|| mddev
->major_version
5834 || !list_empty(&mddev
->disks
)) {
5835 pr_warn("md: %s already running, cannot run %s\n",
5836 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
5837 mddev_unlock(mddev
);
5839 pr_debug("md: created %s\n", mdname(mddev
));
5840 mddev
->persistent
= 1;
5841 rdev_for_each_list(rdev
, tmp
, &candidates
) {
5842 list_del_init(&rdev
->same_set
);
5843 if (bind_rdev_to_array(rdev
, mddev
))
5846 autorun_array(mddev
);
5847 mddev_unlock(mddev
);
5849 /* on success, candidates will be empty, on error
5852 rdev_for_each_list(rdev
, tmp
, &candidates
) {
5853 list_del_init(&rdev
->same_set
);
5858 pr_info("md: ... autorun DONE.\n");
5860 #endif /* !MODULE */
5862 static int get_version(void __user
*arg
)
5866 ver
.major
= MD_MAJOR_VERSION
;
5867 ver
.minor
= MD_MINOR_VERSION
;
5868 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
5870 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
5876 static int get_array_info(struct mddev
*mddev
, void __user
*arg
)
5878 mdu_array_info_t info
;
5879 int nr
,working
,insync
,failed
,spare
;
5880 struct md_rdev
*rdev
;
5882 nr
= working
= insync
= failed
= spare
= 0;
5884 rdev_for_each_rcu(rdev
, mddev
) {
5886 if (test_bit(Faulty
, &rdev
->flags
))
5890 if (test_bit(In_sync
, &rdev
->flags
))
5892 else if (test_bit(Journal
, &rdev
->flags
))
5893 /* TODO: add journal count to md_u.h */
5901 info
.major_version
= mddev
->major_version
;
5902 info
.minor_version
= mddev
->minor_version
;
5903 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
5904 info
.ctime
= clamp_t(time64_t
, mddev
->ctime
, 0, U32_MAX
);
5905 info
.level
= mddev
->level
;
5906 info
.size
= mddev
->dev_sectors
/ 2;
5907 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
5910 info
.raid_disks
= mddev
->raid_disks
;
5911 info
.md_minor
= mddev
->md_minor
;
5912 info
.not_persistent
= !mddev
->persistent
;
5914 info
.utime
= clamp_t(time64_t
, mddev
->utime
, 0, U32_MAX
);
5917 info
.state
= (1<<MD_SB_CLEAN
);
5918 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5919 info
.state
|= (1<<MD_SB_BITMAP_PRESENT
);
5920 if (mddev_is_clustered(mddev
))
5921 info
.state
|= (1<<MD_SB_CLUSTERED
);
5922 info
.active_disks
= insync
;
5923 info
.working_disks
= working
;
5924 info
.failed_disks
= failed
;
5925 info
.spare_disks
= spare
;
5927 info
.layout
= mddev
->layout
;
5928 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
5930 if (copy_to_user(arg
, &info
, sizeof(info
)))
5936 static int get_bitmap_file(struct mddev
*mddev
, void __user
* arg
)
5938 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
5942 file
= kzalloc(sizeof(*file
), GFP_NOIO
);
5947 spin_lock(&mddev
->lock
);
5948 /* bitmap enabled */
5949 if (mddev
->bitmap_info
.file
) {
5950 ptr
= file_path(mddev
->bitmap_info
.file
, file
->pathname
,
5951 sizeof(file
->pathname
));
5955 memmove(file
->pathname
, ptr
,
5956 sizeof(file
->pathname
)-(ptr
-file
->pathname
));
5958 spin_unlock(&mddev
->lock
);
5961 copy_to_user(arg
, file
, sizeof(*file
)))
5968 static int get_disk_info(struct mddev
*mddev
, void __user
* arg
)
5970 mdu_disk_info_t info
;
5971 struct md_rdev
*rdev
;
5973 if (copy_from_user(&info
, arg
, sizeof(info
)))
5977 rdev
= md_find_rdev_nr_rcu(mddev
, info
.number
);
5979 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
5980 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
5981 info
.raid_disk
= rdev
->raid_disk
;
5983 if (test_bit(Faulty
, &rdev
->flags
))
5984 info
.state
|= (1<<MD_DISK_FAULTY
);
5985 else if (test_bit(In_sync
, &rdev
->flags
)) {
5986 info
.state
|= (1<<MD_DISK_ACTIVE
);
5987 info
.state
|= (1<<MD_DISK_SYNC
);
5989 if (test_bit(Journal
, &rdev
->flags
))
5990 info
.state
|= (1<<MD_DISK_JOURNAL
);
5991 if (test_bit(WriteMostly
, &rdev
->flags
))
5992 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
5993 if (test_bit(FailFast
, &rdev
->flags
))
5994 info
.state
|= (1<<MD_DISK_FAILFAST
);
5996 info
.major
= info
.minor
= 0;
5997 info
.raid_disk
= -1;
5998 info
.state
= (1<<MD_DISK_REMOVED
);
6002 if (copy_to_user(arg
, &info
, sizeof(info
)))
6008 static int add_new_disk(struct mddev
*mddev
, mdu_disk_info_t
*info
)
6010 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
6011 struct md_rdev
*rdev
;
6012 dev_t dev
= MKDEV(info
->major
,info
->minor
);
6014 if (mddev_is_clustered(mddev
) &&
6015 !(info
->state
& ((1 << MD_DISK_CLUSTER_ADD
) | (1 << MD_DISK_CANDIDATE
)))) {
6016 pr_warn("%s: Cannot add to clustered mddev.\n",
6021 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
6024 if (!mddev
->raid_disks
) {
6026 /* expecting a device which has a superblock */
6027 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
6029 pr_warn("md: md_import_device returned %ld\n",
6031 return PTR_ERR(rdev
);
6033 if (!list_empty(&mddev
->disks
)) {
6034 struct md_rdev
*rdev0
6035 = list_entry(mddev
->disks
.next
,
6036 struct md_rdev
, same_set
);
6037 err
= super_types
[mddev
->major_version
]
6038 .load_super(rdev
, rdev0
, mddev
->minor_version
);
6040 pr_warn("md: %s has different UUID to %s\n",
6041 bdevname(rdev
->bdev
,b
),
6042 bdevname(rdev0
->bdev
,b2
));
6047 err
= bind_rdev_to_array(rdev
, mddev
);
6054 * add_new_disk can be used once the array is assembled
6055 * to add "hot spares". They must already have a superblock
6060 if (!mddev
->pers
->hot_add_disk
) {
6061 pr_warn("%s: personality does not support diskops!\n",
6065 if (mddev
->persistent
)
6066 rdev
= md_import_device(dev
, mddev
->major_version
,
6067 mddev
->minor_version
);
6069 rdev
= md_import_device(dev
, -1, -1);
6071 pr_warn("md: md_import_device returned %ld\n",
6073 return PTR_ERR(rdev
);
6075 /* set saved_raid_disk if appropriate */
6076 if (!mddev
->persistent
) {
6077 if (info
->state
& (1<<MD_DISK_SYNC
) &&
6078 info
->raid_disk
< mddev
->raid_disks
) {
6079 rdev
->raid_disk
= info
->raid_disk
;
6080 set_bit(In_sync
, &rdev
->flags
);
6081 clear_bit(Bitmap_sync
, &rdev
->flags
);
6083 rdev
->raid_disk
= -1;
6084 rdev
->saved_raid_disk
= rdev
->raid_disk
;
6086 super_types
[mddev
->major_version
].
6087 validate_super(mddev
, rdev
);
6088 if ((info
->state
& (1<<MD_DISK_SYNC
)) &&
6089 rdev
->raid_disk
!= info
->raid_disk
) {
6090 /* This was a hot-add request, but events doesn't
6091 * match, so reject it.
6097 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
6098 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
6099 set_bit(WriteMostly
, &rdev
->flags
);
6101 clear_bit(WriteMostly
, &rdev
->flags
);
6102 if (info
->state
& (1<<MD_DISK_FAILFAST
))
6103 set_bit(FailFast
, &rdev
->flags
);
6105 clear_bit(FailFast
, &rdev
->flags
);
6107 if (info
->state
& (1<<MD_DISK_JOURNAL
)) {
6108 struct md_rdev
*rdev2
;
6109 bool has_journal
= false;
6111 /* make sure no existing journal disk */
6112 rdev_for_each(rdev2
, mddev
) {
6113 if (test_bit(Journal
, &rdev2
->flags
)) {
6122 set_bit(Journal
, &rdev
->flags
);
6125 * check whether the device shows up in other nodes
6127 if (mddev_is_clustered(mddev
)) {
6128 if (info
->state
& (1 << MD_DISK_CANDIDATE
))
6129 set_bit(Candidate
, &rdev
->flags
);
6130 else if (info
->state
& (1 << MD_DISK_CLUSTER_ADD
)) {
6131 /* --add initiated by this node */
6132 err
= md_cluster_ops
->add_new_disk(mddev
, rdev
);
6140 rdev
->raid_disk
= -1;
6141 err
= bind_rdev_to_array(rdev
, mddev
);
6146 if (mddev_is_clustered(mddev
)) {
6147 if (info
->state
& (1 << MD_DISK_CANDIDATE
)) {
6149 err
= md_cluster_ops
->new_disk_ack(mddev
,
6152 md_kick_rdev_from_array(rdev
);
6156 md_cluster_ops
->add_new_disk_cancel(mddev
);
6158 err
= add_bound_rdev(rdev
);
6162 err
= add_bound_rdev(rdev
);
6167 /* otherwise, add_new_disk is only allowed
6168 * for major_version==0 superblocks
6170 if (mddev
->major_version
!= 0) {
6171 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev
));
6175 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
6177 rdev
= md_import_device(dev
, -1, 0);
6179 pr_warn("md: error, md_import_device() returned %ld\n",
6181 return PTR_ERR(rdev
);
6183 rdev
->desc_nr
= info
->number
;
6184 if (info
->raid_disk
< mddev
->raid_disks
)
6185 rdev
->raid_disk
= info
->raid_disk
;
6187 rdev
->raid_disk
= -1;
6189 if (rdev
->raid_disk
< mddev
->raid_disks
)
6190 if (info
->state
& (1<<MD_DISK_SYNC
))
6191 set_bit(In_sync
, &rdev
->flags
);
6193 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
6194 set_bit(WriteMostly
, &rdev
->flags
);
6195 if (info
->state
& (1<<MD_DISK_FAILFAST
))
6196 set_bit(FailFast
, &rdev
->flags
);
6198 if (!mddev
->persistent
) {
6199 pr_debug("md: nonpersistent superblock ...\n");
6200 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
6202 rdev
->sb_start
= calc_dev_sboffset(rdev
);
6203 rdev
->sectors
= rdev
->sb_start
;
6205 err
= bind_rdev_to_array(rdev
, mddev
);
6215 static int hot_remove_disk(struct mddev
*mddev
, dev_t dev
)
6217 char b
[BDEVNAME_SIZE
];
6218 struct md_rdev
*rdev
;
6220 rdev
= find_rdev(mddev
, dev
);
6224 if (rdev
->raid_disk
< 0)
6227 clear_bit(Blocked
, &rdev
->flags
);
6228 remove_and_add_spares(mddev
, rdev
);
6230 if (rdev
->raid_disk
>= 0)
6234 if (mddev_is_clustered(mddev
))
6235 md_cluster_ops
->remove_disk(mddev
, rdev
);
6237 md_kick_rdev_from_array(rdev
);
6238 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
6240 md_wakeup_thread(mddev
->thread
);
6242 md_update_sb(mddev
, 1);
6243 md_new_event(mddev
);
6247 pr_debug("md: cannot remove active disk %s from %s ...\n",
6248 bdevname(rdev
->bdev
,b
), mdname(mddev
));
6252 static int hot_add_disk(struct mddev
*mddev
, dev_t dev
)
6254 char b
[BDEVNAME_SIZE
];
6256 struct md_rdev
*rdev
;
6261 if (mddev
->major_version
!= 0) {
6262 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
6266 if (!mddev
->pers
->hot_add_disk
) {
6267 pr_warn("%s: personality does not support diskops!\n",
6272 rdev
= md_import_device(dev
, -1, 0);
6274 pr_warn("md: error, md_import_device() returned %ld\n",
6279 if (mddev
->persistent
)
6280 rdev
->sb_start
= calc_dev_sboffset(rdev
);
6282 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
6284 rdev
->sectors
= rdev
->sb_start
;
6286 if (test_bit(Faulty
, &rdev
->flags
)) {
6287 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
6288 bdevname(rdev
->bdev
,b
), mdname(mddev
));
6293 clear_bit(In_sync
, &rdev
->flags
);
6295 rdev
->saved_raid_disk
= -1;
6296 err
= bind_rdev_to_array(rdev
, mddev
);
6301 * The rest should better be atomic, we can have disk failures
6302 * noticed in interrupt contexts ...
6305 rdev
->raid_disk
= -1;
6307 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
6309 md_update_sb(mddev
, 1);
6311 * Kick recovery, maybe this spare has to be added to the
6312 * array immediately.
6314 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6315 md_wakeup_thread(mddev
->thread
);
6316 md_new_event(mddev
);
6324 static int set_bitmap_file(struct mddev
*mddev
, int fd
)
6329 if (!mddev
->pers
->quiesce
|| !mddev
->thread
)
6331 if (mddev
->recovery
|| mddev
->sync_thread
)
6333 /* we should be able to change the bitmap.. */
6337 struct inode
*inode
;
6340 if (mddev
->bitmap
|| mddev
->bitmap_info
.file
)
6341 return -EEXIST
; /* cannot add when bitmap is present */
6345 pr_warn("%s: error: failed to get bitmap file\n",
6350 inode
= f
->f_mapping
->host
;
6351 if (!S_ISREG(inode
->i_mode
)) {
6352 pr_warn("%s: error: bitmap file must be a regular file\n",
6355 } else if (!(f
->f_mode
& FMODE_WRITE
)) {
6356 pr_warn("%s: error: bitmap file must open for write\n",
6359 } else if (atomic_read(&inode
->i_writecount
) != 1) {
6360 pr_warn("%s: error: bitmap file is already in use\n",
6368 mddev
->bitmap_info
.file
= f
;
6369 mddev
->bitmap_info
.offset
= 0; /* file overrides offset */
6370 } else if (mddev
->bitmap
== NULL
)
6371 return -ENOENT
; /* cannot remove what isn't there */
6374 mddev
->pers
->quiesce(mddev
, 1);
6376 struct bitmap
*bitmap
;
6378 bitmap
= bitmap_create(mddev
, -1);
6379 if (!IS_ERR(bitmap
)) {
6380 mddev
->bitmap
= bitmap
;
6381 err
= bitmap_load(mddev
);
6383 err
= PTR_ERR(bitmap
);
6385 if (fd
< 0 || err
) {
6386 bitmap_destroy(mddev
);
6387 fd
= -1; /* make sure to put the file */
6389 mddev
->pers
->quiesce(mddev
, 0);
6392 struct file
*f
= mddev
->bitmap_info
.file
;
6394 spin_lock(&mddev
->lock
);
6395 mddev
->bitmap_info
.file
= NULL
;
6396 spin_unlock(&mddev
->lock
);
6405 * set_array_info is used two different ways
6406 * The original usage is when creating a new array.
6407 * In this usage, raid_disks is > 0 and it together with
6408 * level, size, not_persistent,layout,chunksize determine the
6409 * shape of the array.
6410 * This will always create an array with a type-0.90.0 superblock.
6411 * The newer usage is when assembling an array.
6412 * In this case raid_disks will be 0, and the major_version field is
6413 * use to determine which style super-blocks are to be found on the devices.
6414 * The minor and patch _version numbers are also kept incase the
6415 * super_block handler wishes to interpret them.
6417 static int set_array_info(struct mddev
*mddev
, mdu_array_info_t
*info
)
6420 if (info
->raid_disks
== 0) {
6421 /* just setting version number for superblock loading */
6422 if (info
->major_version
< 0 ||
6423 info
->major_version
>= ARRAY_SIZE(super_types
) ||
6424 super_types
[info
->major_version
].name
== NULL
) {
6425 /* maybe try to auto-load a module? */
6426 pr_warn("md: superblock version %d not known\n",
6427 info
->major_version
);
6430 mddev
->major_version
= info
->major_version
;
6431 mddev
->minor_version
= info
->minor_version
;
6432 mddev
->patch_version
= info
->patch_version
;
6433 mddev
->persistent
= !info
->not_persistent
;
6434 /* ensure mddev_put doesn't delete this now that there
6435 * is some minimal configuration.
6437 mddev
->ctime
= ktime_get_real_seconds();
6440 mddev
->major_version
= MD_MAJOR_VERSION
;
6441 mddev
->minor_version
= MD_MINOR_VERSION
;
6442 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
6443 mddev
->ctime
= ktime_get_real_seconds();
6445 mddev
->level
= info
->level
;
6446 mddev
->clevel
[0] = 0;
6447 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
6448 mddev
->raid_disks
= info
->raid_disks
;
6449 /* don't set md_minor, it is determined by which /dev/md* was
6452 if (info
->state
& (1<<MD_SB_CLEAN
))
6453 mddev
->recovery_cp
= MaxSector
;
6455 mddev
->recovery_cp
= 0;
6456 mddev
->persistent
= ! info
->not_persistent
;
6457 mddev
->external
= 0;
6459 mddev
->layout
= info
->layout
;
6460 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
6462 mddev
->max_disks
= MD_SB_DISKS
;
6464 if (mddev
->persistent
) {
6466 mddev
->sb_flags
= 0;
6468 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
6470 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
6471 mddev
->bitmap_info
.default_space
= 64*2 - (MD_SB_BYTES
>> 9);
6472 mddev
->bitmap_info
.offset
= 0;
6474 mddev
->reshape_position
= MaxSector
;
6477 * Generate a 128 bit UUID
6479 get_random_bytes(mddev
->uuid
, 16);
6481 mddev
->new_level
= mddev
->level
;
6482 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
6483 mddev
->new_layout
= mddev
->layout
;
6484 mddev
->delta_disks
= 0;
6485 mddev
->reshape_backwards
= 0;
6490 void md_set_array_sectors(struct mddev
*mddev
, sector_t array_sectors
)
6492 WARN(!mddev_is_locked(mddev
), "%s: unlocked mddev!\n", __func__
);
6494 if (mddev
->external_size
)
6497 mddev
->array_sectors
= array_sectors
;
6499 EXPORT_SYMBOL(md_set_array_sectors
);
6501 static int update_size(struct mddev
*mddev
, sector_t num_sectors
)
6503 struct md_rdev
*rdev
;
6505 int fit
= (num_sectors
== 0);
6507 /* cluster raid doesn't support update size */
6508 if (mddev_is_clustered(mddev
))
6511 if (mddev
->pers
->resize
== NULL
)
6513 /* The "num_sectors" is the number of sectors of each device that
6514 * is used. This can only make sense for arrays with redundancy.
6515 * linear and raid0 always use whatever space is available. We can only
6516 * consider changing this number if no resync or reconstruction is
6517 * happening, and if the new size is acceptable. It must fit before the
6518 * sb_start or, if that is <data_offset, it must fit before the size
6519 * of each device. If num_sectors is zero, we find the largest size
6522 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
6528 rdev_for_each(rdev
, mddev
) {
6529 sector_t avail
= rdev
->sectors
;
6531 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
6532 num_sectors
= avail
;
6533 if (avail
< num_sectors
)
6536 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
6538 revalidate_disk(mddev
->gendisk
);
6542 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
)
6545 struct md_rdev
*rdev
;
6546 /* change the number of raid disks */
6547 if (mddev
->pers
->check_reshape
== NULL
)
6551 if (raid_disks
<= 0 ||
6552 (mddev
->max_disks
&& raid_disks
>= mddev
->max_disks
))
6554 if (mddev
->sync_thread
||
6555 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
6556 mddev
->reshape_position
!= MaxSector
)
6559 rdev_for_each(rdev
, mddev
) {
6560 if (mddev
->raid_disks
< raid_disks
&&
6561 rdev
->data_offset
< rdev
->new_data_offset
)
6563 if (mddev
->raid_disks
> raid_disks
&&
6564 rdev
->data_offset
> rdev
->new_data_offset
)
6568 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
6569 if (mddev
->delta_disks
< 0)
6570 mddev
->reshape_backwards
= 1;
6571 else if (mddev
->delta_disks
> 0)
6572 mddev
->reshape_backwards
= 0;
6574 rv
= mddev
->pers
->check_reshape(mddev
);
6576 mddev
->delta_disks
= 0;
6577 mddev
->reshape_backwards
= 0;
6583 * update_array_info is used to change the configuration of an
6585 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6586 * fields in the info are checked against the array.
6587 * Any differences that cannot be handled will cause an error.
6588 * Normally, only one change can be managed at a time.
6590 static int update_array_info(struct mddev
*mddev
, mdu_array_info_t
*info
)
6596 /* calculate expected state,ignoring low bits */
6597 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
6598 state
|= (1 << MD_SB_BITMAP_PRESENT
);
6600 if (mddev
->major_version
!= info
->major_version
||
6601 mddev
->minor_version
!= info
->minor_version
||
6602 /* mddev->patch_version != info->patch_version || */
6603 mddev
->ctime
!= info
->ctime
||
6604 mddev
->level
!= info
->level
||
6605 /* mddev->layout != info->layout || */
6606 mddev
->persistent
!= !info
->not_persistent
||
6607 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
6608 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6609 ((state
^info
->state
) & 0xfffffe00)
6612 /* Check there is only one change */
6613 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
6615 if (mddev
->raid_disks
!= info
->raid_disks
)
6617 if (mddev
->layout
!= info
->layout
)
6619 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
6626 if (mddev
->layout
!= info
->layout
) {
6628 * we don't need to do anything at the md level, the
6629 * personality will take care of it all.
6631 if (mddev
->pers
->check_reshape
== NULL
)
6634 mddev
->new_layout
= info
->layout
;
6635 rv
= mddev
->pers
->check_reshape(mddev
);
6637 mddev
->new_layout
= mddev
->layout
;
6641 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
6642 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
6644 if (mddev
->raid_disks
!= info
->raid_disks
)
6645 rv
= update_raid_disks(mddev
, info
->raid_disks
);
6647 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
6648 if (mddev
->pers
->quiesce
== NULL
|| mddev
->thread
== NULL
) {
6652 if (mddev
->recovery
|| mddev
->sync_thread
) {
6656 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
6657 struct bitmap
*bitmap
;
6658 /* add the bitmap */
6659 if (mddev
->bitmap
) {
6663 if (mddev
->bitmap_info
.default_offset
== 0) {
6667 mddev
->bitmap_info
.offset
=
6668 mddev
->bitmap_info
.default_offset
;
6669 mddev
->bitmap_info
.space
=
6670 mddev
->bitmap_info
.default_space
;
6671 mddev
->pers
->quiesce(mddev
, 1);
6672 bitmap
= bitmap_create(mddev
, -1);
6673 if (!IS_ERR(bitmap
)) {
6674 mddev
->bitmap
= bitmap
;
6675 rv
= bitmap_load(mddev
);
6677 rv
= PTR_ERR(bitmap
);
6679 bitmap_destroy(mddev
);
6680 mddev
->pers
->quiesce(mddev
, 0);
6682 /* remove the bitmap */
6683 if (!mddev
->bitmap
) {
6687 if (mddev
->bitmap
->storage
.file
) {
6691 if (mddev
->bitmap_info
.nodes
) {
6692 /* hold PW on all the bitmap lock */
6693 if (md_cluster_ops
->lock_all_bitmaps(mddev
) <= 0) {
6694 pr_warn("md: can't change bitmap to none since the array is in use by more than one node\n");
6696 md_cluster_ops
->unlock_all_bitmaps(mddev
);
6700 mddev
->bitmap_info
.nodes
= 0;
6701 md_cluster_ops
->leave(mddev
);
6703 mddev
->pers
->quiesce(mddev
, 1);
6704 bitmap_destroy(mddev
);
6705 mddev
->pers
->quiesce(mddev
, 0);
6706 mddev
->bitmap_info
.offset
= 0;
6709 md_update_sb(mddev
, 1);
6715 static int set_disk_faulty(struct mddev
*mddev
, dev_t dev
)
6717 struct md_rdev
*rdev
;
6720 if (mddev
->pers
== NULL
)
6724 rdev
= find_rdev_rcu(mddev
, dev
);
6728 md_error(mddev
, rdev
);
6729 if (!test_bit(Faulty
, &rdev
->flags
))
6737 * We have a problem here : there is no easy way to give a CHS
6738 * virtual geometry. We currently pretend that we have a 2 heads
6739 * 4 sectors (with a BIG number of cylinders...). This drives
6740 * dosfs just mad... ;-)
6742 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
6744 struct mddev
*mddev
= bdev
->bd_disk
->private_data
;
6748 geo
->cylinders
= mddev
->array_sectors
/ 8;
6752 static inline bool md_ioctl_valid(unsigned int cmd
)
6757 case GET_ARRAY_INFO
:
6758 case GET_BITMAP_FILE
:
6761 case HOT_REMOVE_DISK
:
6764 case RESTART_ARRAY_RW
:
6766 case SET_ARRAY_INFO
:
6767 case SET_BITMAP_FILE
:
6768 case SET_DISK_FAULTY
:
6771 case CLUSTERED_DISK_NACK
:
6778 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
6779 unsigned int cmd
, unsigned long arg
)
6782 void __user
*argp
= (void __user
*)arg
;
6783 struct mddev
*mddev
= NULL
;
6786 if (!md_ioctl_valid(cmd
))
6791 case GET_ARRAY_INFO
:
6795 if (!capable(CAP_SYS_ADMIN
))
6800 * Commands dealing with the RAID driver but not any
6805 err
= get_version(argp
);
6811 autostart_arrays(arg
);
6818 * Commands creating/starting a new array:
6821 mddev
= bdev
->bd_disk
->private_data
;
6828 /* Some actions do not requires the mutex */
6830 case GET_ARRAY_INFO
:
6831 if (!mddev
->raid_disks
&& !mddev
->external
)
6834 err
= get_array_info(mddev
, argp
);
6838 if (!mddev
->raid_disks
&& !mddev
->external
)
6841 err
= get_disk_info(mddev
, argp
);
6844 case SET_DISK_FAULTY
:
6845 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
6848 case GET_BITMAP_FILE
:
6849 err
= get_bitmap_file(mddev
, argp
);
6854 if (cmd
== ADD_NEW_DISK
)
6855 /* need to ensure md_delayed_delete() has completed */
6856 flush_workqueue(md_misc_wq
);
6858 if (cmd
== HOT_REMOVE_DISK
)
6859 /* need to ensure recovery thread has run */
6860 wait_event_interruptible_timeout(mddev
->sb_wait
,
6861 !test_bit(MD_RECOVERY_NEEDED
,
6863 msecs_to_jiffies(5000));
6864 if (cmd
== STOP_ARRAY
|| cmd
== STOP_ARRAY_RO
) {
6865 /* Need to flush page cache, and ensure no-one else opens
6868 mutex_lock(&mddev
->open_mutex
);
6869 if (mddev
->pers
&& atomic_read(&mddev
->openers
) > 1) {
6870 mutex_unlock(&mddev
->open_mutex
);
6874 set_bit(MD_CLOSING
, &mddev
->flags
);
6875 mutex_unlock(&mddev
->open_mutex
);
6876 sync_blockdev(bdev
);
6878 err
= mddev_lock(mddev
);
6880 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
6885 if (cmd
== SET_ARRAY_INFO
) {
6886 mdu_array_info_t info
;
6888 memset(&info
, 0, sizeof(info
));
6889 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
6894 err
= update_array_info(mddev
, &info
);
6896 pr_warn("md: couldn't update array info. %d\n", err
);
6901 if (!list_empty(&mddev
->disks
)) {
6902 pr_warn("md: array %s already has disks!\n", mdname(mddev
));
6906 if (mddev
->raid_disks
) {
6907 pr_warn("md: array %s already initialised!\n", mdname(mddev
));
6911 err
= set_array_info(mddev
, &info
);
6913 pr_warn("md: couldn't set array info. %d\n", err
);
6920 * Commands querying/configuring an existing array:
6922 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
6923 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
6924 if ((!mddev
->raid_disks
&& !mddev
->external
)
6925 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
6926 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
6927 && cmd
!= GET_BITMAP_FILE
) {
6933 * Commands even a read-only array can execute:
6936 case RESTART_ARRAY_RW
:
6937 err
= restart_array(mddev
);
6941 err
= do_md_stop(mddev
, 0, bdev
);
6945 err
= md_set_readonly(mddev
, bdev
);
6948 case HOT_REMOVE_DISK
:
6949 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
6953 /* We can support ADD_NEW_DISK on read-only arrays
6954 * only if we are re-adding a preexisting device.
6955 * So require mddev->pers and MD_DISK_SYNC.
6958 mdu_disk_info_t info
;
6959 if (copy_from_user(&info
, argp
, sizeof(info
)))
6961 else if (!(info
.state
& (1<<MD_DISK_SYNC
)))
6962 /* Need to clear read-only for this */
6965 err
= add_new_disk(mddev
, &info
);
6971 if (get_user(ro
, (int __user
*)(arg
))) {
6977 /* if the bdev is going readonly the value of mddev->ro
6978 * does not matter, no writes are coming
6983 /* are we are already prepared for writes? */
6987 /* transitioning to readauto need only happen for
6988 * arrays that call md_write_start
6991 err
= restart_array(mddev
);
6994 set_disk_ro(mddev
->gendisk
, 0);
7001 * The remaining ioctls are changing the state of the
7002 * superblock, so we do not allow them on read-only arrays.
7004 if (mddev
->ro
&& mddev
->pers
) {
7005 if (mddev
->ro
== 2) {
7007 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7008 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7009 /* mddev_unlock will wake thread */
7010 /* If a device failed while we were read-only, we
7011 * need to make sure the metadata is updated now.
7013 if (test_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
)) {
7014 mddev_unlock(mddev
);
7015 wait_event(mddev
->sb_wait
,
7016 !test_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
) &&
7017 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
7018 mddev_lock_nointr(mddev
);
7029 mdu_disk_info_t info
;
7030 if (copy_from_user(&info
, argp
, sizeof(info
)))
7033 err
= add_new_disk(mddev
, &info
);
7037 case CLUSTERED_DISK_NACK
:
7038 if (mddev_is_clustered(mddev
))
7039 md_cluster_ops
->new_disk_ack(mddev
, false);
7045 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
7049 err
= do_md_run(mddev
);
7052 case SET_BITMAP_FILE
:
7053 err
= set_bitmap_file(mddev
, (int)arg
);
7062 if (mddev
->hold_active
== UNTIL_IOCTL
&&
7064 mddev
->hold_active
= 0;
7065 mddev_unlock(mddev
);
7069 #ifdef CONFIG_COMPAT
7070 static int md_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
7071 unsigned int cmd
, unsigned long arg
)
7074 case HOT_REMOVE_DISK
:
7076 case SET_DISK_FAULTY
:
7077 case SET_BITMAP_FILE
:
7078 /* These take in integer arg, do not convert */
7081 arg
= (unsigned long)compat_ptr(arg
);
7085 return md_ioctl(bdev
, mode
, cmd
, arg
);
7087 #endif /* CONFIG_COMPAT */
7089 static int md_open(struct block_device
*bdev
, fmode_t mode
)
7092 * Succeed if we can lock the mddev, which confirms that
7093 * it isn't being stopped right now.
7095 struct mddev
*mddev
= mddev_find(bdev
->bd_dev
);
7101 if (mddev
->gendisk
!= bdev
->bd_disk
) {
7102 /* we are racing with mddev_put which is discarding this
7106 /* Wait until bdev->bd_disk is definitely gone */
7107 flush_workqueue(md_misc_wq
);
7108 /* Then retry the open from the top */
7109 return -ERESTARTSYS
;
7111 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
7113 if ((err
= mutex_lock_interruptible(&mddev
->open_mutex
)))
7116 if (test_bit(MD_CLOSING
, &mddev
->flags
)) {
7117 mutex_unlock(&mddev
->open_mutex
);
7123 atomic_inc(&mddev
->openers
);
7124 mutex_unlock(&mddev
->open_mutex
);
7126 check_disk_change(bdev
);
7133 static void md_release(struct gendisk
*disk
, fmode_t mode
)
7135 struct mddev
*mddev
= disk
->private_data
;
7138 atomic_dec(&mddev
->openers
);
7142 static int md_media_changed(struct gendisk
*disk
)
7144 struct mddev
*mddev
= disk
->private_data
;
7146 return mddev
->changed
;
7149 static int md_revalidate(struct gendisk
*disk
)
7151 struct mddev
*mddev
= disk
->private_data
;
7156 static const struct block_device_operations md_fops
=
7158 .owner
= THIS_MODULE
,
7160 .release
= md_release
,
7162 #ifdef CONFIG_COMPAT
7163 .compat_ioctl
= md_compat_ioctl
,
7165 .getgeo
= md_getgeo
,
7166 .media_changed
= md_media_changed
,
7167 .revalidate_disk
= md_revalidate
,
7170 static int md_thread(void *arg
)
7172 struct md_thread
*thread
= arg
;
7175 * md_thread is a 'system-thread', it's priority should be very
7176 * high. We avoid resource deadlocks individually in each
7177 * raid personality. (RAID5 does preallocation) We also use RR and
7178 * the very same RT priority as kswapd, thus we will never get
7179 * into a priority inversion deadlock.
7181 * we definitely have to have equal or higher priority than
7182 * bdflush, otherwise bdflush will deadlock if there are too
7183 * many dirty RAID5 blocks.
7186 allow_signal(SIGKILL
);
7187 while (!kthread_should_stop()) {
7189 /* We need to wait INTERRUPTIBLE so that
7190 * we don't add to the load-average.
7191 * That means we need to be sure no signals are
7194 if (signal_pending(current
))
7195 flush_signals(current
);
7197 wait_event_interruptible_timeout
7199 test_bit(THREAD_WAKEUP
, &thread
->flags
)
7200 || kthread_should_stop() || kthread_should_park(),
7203 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
7204 if (kthread_should_park())
7206 if (!kthread_should_stop())
7207 thread
->run(thread
);
7213 void md_wakeup_thread(struct md_thread
*thread
)
7216 pr_debug("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
7217 set_bit(THREAD_WAKEUP
, &thread
->flags
);
7218 wake_up(&thread
->wqueue
);
7221 EXPORT_SYMBOL(md_wakeup_thread
);
7223 struct md_thread
*md_register_thread(void (*run
) (struct md_thread
*),
7224 struct mddev
*mddev
, const char *name
)
7226 struct md_thread
*thread
;
7228 thread
= kzalloc(sizeof(struct md_thread
), GFP_KERNEL
);
7232 init_waitqueue_head(&thread
->wqueue
);
7235 thread
->mddev
= mddev
;
7236 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
7237 thread
->tsk
= kthread_run(md_thread
, thread
,
7239 mdname(thread
->mddev
),
7241 if (IS_ERR(thread
->tsk
)) {
7247 EXPORT_SYMBOL(md_register_thread
);
7249 void md_unregister_thread(struct md_thread
**threadp
)
7251 struct md_thread
*thread
= *threadp
;
7254 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
7255 /* Locking ensures that mddev_unlock does not wake_up a
7256 * non-existent thread
7258 spin_lock(&pers_lock
);
7260 spin_unlock(&pers_lock
);
7262 kthread_stop(thread
->tsk
);
7265 EXPORT_SYMBOL(md_unregister_thread
);
7267 void md_error(struct mddev
*mddev
, struct md_rdev
*rdev
)
7269 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
7272 if (!mddev
->pers
|| !mddev
->pers
->error_handler
)
7274 mddev
->pers
->error_handler(mddev
,rdev
);
7275 if (mddev
->degraded
)
7276 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7277 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
7278 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7279 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7280 md_wakeup_thread(mddev
->thread
);
7281 if (mddev
->event_work
.func
)
7282 queue_work(md_misc_wq
, &mddev
->event_work
);
7283 md_new_event(mddev
);
7285 EXPORT_SYMBOL(md_error
);
7287 /* seq_file implementation /proc/mdstat */
7289 static void status_unused(struct seq_file
*seq
)
7292 struct md_rdev
*rdev
;
7294 seq_printf(seq
, "unused devices: ");
7296 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
7297 char b
[BDEVNAME_SIZE
];
7299 seq_printf(seq
, "%s ",
7300 bdevname(rdev
->bdev
,b
));
7303 seq_printf(seq
, "<none>");
7305 seq_printf(seq
, "\n");
7308 static int status_resync(struct seq_file
*seq
, struct mddev
*mddev
)
7310 sector_t max_sectors
, resync
, res
;
7311 unsigned long dt
, db
;
7314 unsigned int per_milli
;
7316 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
7317 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
7318 max_sectors
= mddev
->resync_max_sectors
;
7320 max_sectors
= mddev
->dev_sectors
;
7322 resync
= mddev
->curr_resync
;
7324 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
7325 /* Still cleaning up */
7326 resync
= max_sectors
;
7328 resync
-= atomic_read(&mddev
->recovery_active
);
7331 if (mddev
->recovery_cp
< MaxSector
) {
7332 seq_printf(seq
, "\tresync=PENDING");
7338 seq_printf(seq
, "\tresync=DELAYED");
7342 WARN_ON(max_sectors
== 0);
7343 /* Pick 'scale' such that (resync>>scale)*1000 will fit
7344 * in a sector_t, and (max_sectors>>scale) will fit in a
7345 * u32, as those are the requirements for sector_div.
7346 * Thus 'scale' must be at least 10
7349 if (sizeof(sector_t
) > sizeof(unsigned long)) {
7350 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
7353 res
= (resync
>>scale
)*1000;
7354 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
7358 int i
, x
= per_milli
/50, y
= 20-x
;
7359 seq_printf(seq
, "[");
7360 for (i
= 0; i
< x
; i
++)
7361 seq_printf(seq
, "=");
7362 seq_printf(seq
, ">");
7363 for (i
= 0; i
< y
; i
++)
7364 seq_printf(seq
, ".");
7365 seq_printf(seq
, "] ");
7367 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
7368 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
7370 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
7372 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
7373 "resync" : "recovery"))),
7374 per_milli
/10, per_milli
% 10,
7375 (unsigned long long) resync
/2,
7376 (unsigned long long) max_sectors
/2);
7379 * dt: time from mark until now
7380 * db: blocks written from mark until now
7381 * rt: remaining time
7383 * rt is a sector_t, so could be 32bit or 64bit.
7384 * So we divide before multiply in case it is 32bit and close
7386 * We scale the divisor (db) by 32 to avoid losing precision
7387 * near the end of resync when the number of remaining sectors
7389 * We then divide rt by 32 after multiplying by db to compensate.
7390 * The '+1' avoids division by zero if db is very small.
7392 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
7394 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
7395 - mddev
->resync_mark_cnt
;
7397 rt
= max_sectors
- resync
; /* number of remaining sectors */
7398 sector_div(rt
, db
/32+1);
7402 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
7403 ((unsigned long)rt
% 60)/6);
7405 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
7409 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
7411 struct list_head
*tmp
;
7413 struct mddev
*mddev
;
7421 spin_lock(&all_mddevs_lock
);
7422 list_for_each(tmp
,&all_mddevs
)
7424 mddev
= list_entry(tmp
, struct mddev
, all_mddevs
);
7426 spin_unlock(&all_mddevs_lock
);
7429 spin_unlock(&all_mddevs_lock
);
7431 return (void*)2;/* tail */
7435 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
7437 struct list_head
*tmp
;
7438 struct mddev
*next_mddev
, *mddev
= v
;
7444 spin_lock(&all_mddevs_lock
);
7446 tmp
= all_mddevs
.next
;
7448 tmp
= mddev
->all_mddevs
.next
;
7449 if (tmp
!= &all_mddevs
)
7450 next_mddev
= mddev_get(list_entry(tmp
,struct mddev
,all_mddevs
));
7452 next_mddev
= (void*)2;
7455 spin_unlock(&all_mddevs_lock
);
7463 static void md_seq_stop(struct seq_file
*seq
, void *v
)
7465 struct mddev
*mddev
= v
;
7467 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
7471 static int md_seq_show(struct seq_file
*seq
, void *v
)
7473 struct mddev
*mddev
= v
;
7475 struct md_rdev
*rdev
;
7477 if (v
== (void*)1) {
7478 struct md_personality
*pers
;
7479 seq_printf(seq
, "Personalities : ");
7480 spin_lock(&pers_lock
);
7481 list_for_each_entry(pers
, &pers_list
, list
)
7482 seq_printf(seq
, "[%s] ", pers
->name
);
7484 spin_unlock(&pers_lock
);
7485 seq_printf(seq
, "\n");
7486 seq
->poll_event
= atomic_read(&md_event_count
);
7489 if (v
== (void*)2) {
7494 spin_lock(&mddev
->lock
);
7495 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
7496 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
7497 mddev
->pers
? "" : "in");
7500 seq_printf(seq
, " (read-only)");
7502 seq_printf(seq
, " (auto-read-only)");
7503 seq_printf(seq
, " %s", mddev
->pers
->name
);
7508 rdev_for_each_rcu(rdev
, mddev
) {
7509 char b
[BDEVNAME_SIZE
];
7510 seq_printf(seq
, " %s[%d]",
7511 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
7512 if (test_bit(WriteMostly
, &rdev
->flags
))
7513 seq_printf(seq
, "(W)");
7514 if (test_bit(Journal
, &rdev
->flags
))
7515 seq_printf(seq
, "(J)");
7516 if (test_bit(Faulty
, &rdev
->flags
)) {
7517 seq_printf(seq
, "(F)");
7520 if (rdev
->raid_disk
< 0)
7521 seq_printf(seq
, "(S)"); /* spare */
7522 if (test_bit(Replacement
, &rdev
->flags
))
7523 seq_printf(seq
, "(R)");
7524 sectors
+= rdev
->sectors
;
7528 if (!list_empty(&mddev
->disks
)) {
7530 seq_printf(seq
, "\n %llu blocks",
7531 (unsigned long long)
7532 mddev
->array_sectors
/ 2);
7534 seq_printf(seq
, "\n %llu blocks",
7535 (unsigned long long)sectors
/ 2);
7537 if (mddev
->persistent
) {
7538 if (mddev
->major_version
!= 0 ||
7539 mddev
->minor_version
!= 90) {
7540 seq_printf(seq
," super %d.%d",
7541 mddev
->major_version
,
7542 mddev
->minor_version
);
7544 } else if (mddev
->external
)
7545 seq_printf(seq
, " super external:%s",
7546 mddev
->metadata_type
);
7548 seq_printf(seq
, " super non-persistent");
7551 mddev
->pers
->status(seq
, mddev
);
7552 seq_printf(seq
, "\n ");
7553 if (mddev
->pers
->sync_request
) {
7554 if (status_resync(seq
, mddev
))
7555 seq_printf(seq
, "\n ");
7558 seq_printf(seq
, "\n ");
7560 bitmap_status(seq
, mddev
->bitmap
);
7562 seq_printf(seq
, "\n");
7564 spin_unlock(&mddev
->lock
);
7569 static const struct seq_operations md_seq_ops
= {
7570 .start
= md_seq_start
,
7571 .next
= md_seq_next
,
7572 .stop
= md_seq_stop
,
7573 .show
= md_seq_show
,
7576 static int md_seq_open(struct inode
*inode
, struct file
*file
)
7578 struct seq_file
*seq
;
7581 error
= seq_open(file
, &md_seq_ops
);
7585 seq
= file
->private_data
;
7586 seq
->poll_event
= atomic_read(&md_event_count
);
7590 static int md_unloading
;
7591 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
7593 struct seq_file
*seq
= filp
->private_data
;
7597 return POLLIN
|POLLRDNORM
|POLLERR
|POLLPRI
;
7598 poll_wait(filp
, &md_event_waiters
, wait
);
7600 /* always allow read */
7601 mask
= POLLIN
| POLLRDNORM
;
7603 if (seq
->poll_event
!= atomic_read(&md_event_count
))
7604 mask
|= POLLERR
| POLLPRI
;
7608 static const struct file_operations md_seq_fops
= {
7609 .owner
= THIS_MODULE
,
7610 .open
= md_seq_open
,
7612 .llseek
= seq_lseek
,
7613 .release
= seq_release_private
,
7614 .poll
= mdstat_poll
,
7617 int register_md_personality(struct md_personality
*p
)
7619 pr_debug("md: %s personality registered for level %d\n",
7621 spin_lock(&pers_lock
);
7622 list_add_tail(&p
->list
, &pers_list
);
7623 spin_unlock(&pers_lock
);
7626 EXPORT_SYMBOL(register_md_personality
);
7628 int unregister_md_personality(struct md_personality
*p
)
7630 pr_debug("md: %s personality unregistered\n", p
->name
);
7631 spin_lock(&pers_lock
);
7632 list_del_init(&p
->list
);
7633 spin_unlock(&pers_lock
);
7636 EXPORT_SYMBOL(unregister_md_personality
);
7638 int register_md_cluster_operations(struct md_cluster_operations
*ops
,
7639 struct module
*module
)
7642 spin_lock(&pers_lock
);
7643 if (md_cluster_ops
!= NULL
)
7646 md_cluster_ops
= ops
;
7647 md_cluster_mod
= module
;
7649 spin_unlock(&pers_lock
);
7652 EXPORT_SYMBOL(register_md_cluster_operations
);
7654 int unregister_md_cluster_operations(void)
7656 spin_lock(&pers_lock
);
7657 md_cluster_ops
= NULL
;
7658 spin_unlock(&pers_lock
);
7661 EXPORT_SYMBOL(unregister_md_cluster_operations
);
7663 int md_setup_cluster(struct mddev
*mddev
, int nodes
)
7665 if (!md_cluster_ops
)
7666 request_module("md-cluster");
7667 spin_lock(&pers_lock
);
7668 /* ensure module won't be unloaded */
7669 if (!md_cluster_ops
|| !try_module_get(md_cluster_mod
)) {
7670 pr_warn("can't find md-cluster module or get it's reference.\n");
7671 spin_unlock(&pers_lock
);
7674 spin_unlock(&pers_lock
);
7676 return md_cluster_ops
->join(mddev
, nodes
);
7679 void md_cluster_stop(struct mddev
*mddev
)
7681 if (!md_cluster_ops
)
7683 md_cluster_ops
->leave(mddev
);
7684 module_put(md_cluster_mod
);
7687 static int is_mddev_idle(struct mddev
*mddev
, int init
)
7689 struct md_rdev
*rdev
;
7695 rdev_for_each_rcu(rdev
, mddev
) {
7696 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
7697 curr_events
= (int)part_stat_read(&disk
->part0
, sectors
[0]) +
7698 (int)part_stat_read(&disk
->part0
, sectors
[1]) -
7699 atomic_read(&disk
->sync_io
);
7700 /* sync IO will cause sync_io to increase before the disk_stats
7701 * as sync_io is counted when a request starts, and
7702 * disk_stats is counted when it completes.
7703 * So resync activity will cause curr_events to be smaller than
7704 * when there was no such activity.
7705 * non-sync IO will cause disk_stat to increase without
7706 * increasing sync_io so curr_events will (eventually)
7707 * be larger than it was before. Once it becomes
7708 * substantially larger, the test below will cause
7709 * the array to appear non-idle, and resync will slow
7711 * If there is a lot of outstanding resync activity when
7712 * we set last_event to curr_events, then all that activity
7713 * completing might cause the array to appear non-idle
7714 * and resync will be slowed down even though there might
7715 * not have been non-resync activity. This will only
7716 * happen once though. 'last_events' will soon reflect
7717 * the state where there is little or no outstanding
7718 * resync requests, and further resync activity will
7719 * always make curr_events less than last_events.
7722 if (init
|| curr_events
- rdev
->last_events
> 64) {
7723 rdev
->last_events
= curr_events
;
7731 void md_done_sync(struct mddev
*mddev
, int blocks
, int ok
)
7733 /* another "blocks" (512byte) blocks have been synced */
7734 atomic_sub(blocks
, &mddev
->recovery_active
);
7735 wake_up(&mddev
->recovery_wait
);
7737 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7738 set_bit(MD_RECOVERY_ERROR
, &mddev
->recovery
);
7739 md_wakeup_thread(mddev
->thread
);
7740 // stop recovery, signal do_sync ....
7743 EXPORT_SYMBOL(md_done_sync
);
7745 /* md_write_start(mddev, bi)
7746 * If we need to update some array metadata (e.g. 'active' flag
7747 * in superblock) before writing, schedule a superblock update
7748 * and wait for it to complete.
7750 void md_write_start(struct mddev
*mddev
, struct bio
*bi
)
7753 if (bio_data_dir(bi
) != WRITE
)
7756 BUG_ON(mddev
->ro
== 1);
7757 if (mddev
->ro
== 2) {
7758 /* need to switch to read/write */
7760 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7761 md_wakeup_thread(mddev
->thread
);
7762 md_wakeup_thread(mddev
->sync_thread
);
7765 atomic_inc(&mddev
->writes_pending
);
7766 if (mddev
->safemode
== 1)
7767 mddev
->safemode
= 0;
7768 if (mddev
->in_sync
) {
7769 spin_lock(&mddev
->lock
);
7770 if (mddev
->in_sync
) {
7772 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
7773 set_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
7774 md_wakeup_thread(mddev
->thread
);
7777 spin_unlock(&mddev
->lock
);
7780 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7781 wait_event(mddev
->sb_wait
,
7782 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
7784 EXPORT_SYMBOL(md_write_start
);
7786 void md_write_end(struct mddev
*mddev
)
7788 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
7789 if (mddev
->safemode
== 2)
7790 md_wakeup_thread(mddev
->thread
);
7791 else if (mddev
->safemode_delay
)
7792 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
7795 EXPORT_SYMBOL(md_write_end
);
7797 /* md_allow_write(mddev)
7798 * Calling this ensures that the array is marked 'active' so that writes
7799 * may proceed without blocking. It is important to call this before
7800 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7801 * Must be called with mddev_lock held.
7803 * In the ->external case MD_SB_CHANGE_PENDING can not be cleared until mddev->lock
7804 * is dropped, so return -EAGAIN after notifying userspace.
7806 int md_allow_write(struct mddev
*mddev
)
7812 if (!mddev
->pers
->sync_request
)
7815 spin_lock(&mddev
->lock
);
7816 if (mddev
->in_sync
) {
7818 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
7819 set_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
7820 if (mddev
->safemode_delay
&&
7821 mddev
->safemode
== 0)
7822 mddev
->safemode
= 1;
7823 spin_unlock(&mddev
->lock
);
7824 md_update_sb(mddev
, 0);
7825 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7827 spin_unlock(&mddev
->lock
);
7829 if (test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
))
7834 EXPORT_SYMBOL_GPL(md_allow_write
);
7836 #define SYNC_MARKS 10
7837 #define SYNC_MARK_STEP (3*HZ)
7838 #define UPDATE_FREQUENCY (5*60*HZ)
7839 void md_do_sync(struct md_thread
*thread
)
7841 struct mddev
*mddev
= thread
->mddev
;
7842 struct mddev
*mddev2
;
7843 unsigned int currspeed
= 0,
7845 sector_t max_sectors
,j
, io_sectors
, recovery_done
;
7846 unsigned long mark
[SYNC_MARKS
];
7847 unsigned long update_time
;
7848 sector_t mark_cnt
[SYNC_MARKS
];
7850 struct list_head
*tmp
;
7851 sector_t last_check
;
7853 struct md_rdev
*rdev
;
7854 char *desc
, *action
= NULL
;
7855 struct blk_plug plug
;
7858 /* just incase thread restarts... */
7859 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
7861 if (mddev
->ro
) {/* never try to sync a read-only array */
7862 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7866 if (mddev_is_clustered(mddev
)) {
7867 ret
= md_cluster_ops
->resync_start(mddev
);
7871 set_bit(MD_CLUSTER_RESYNC_LOCKED
, &mddev
->flags
);
7872 if (!(test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
7873 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) ||
7874 test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
7875 && ((unsigned long long)mddev
->curr_resync_completed
7876 < (unsigned long long)mddev
->resync_max_sectors
))
7880 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
7881 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)) {
7882 desc
= "data-check";
7884 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
7885 desc
= "requested-resync";
7889 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
7894 mddev
->last_sync_action
= action
?: desc
;
7896 /* we overload curr_resync somewhat here.
7897 * 0 == not engaged in resync at all
7898 * 2 == checking that there is no conflict with another sync
7899 * 1 == like 2, but have yielded to allow conflicting resync to
7901 * other == active in resync - this many blocks
7903 * Before starting a resync we must have set curr_resync to
7904 * 2, and then checked that every "conflicting" array has curr_resync
7905 * less than ours. When we find one that is the same or higher
7906 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7907 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7908 * This will mean we have to start checking from the beginning again.
7913 int mddev2_minor
= -1;
7914 mddev
->curr_resync
= 2;
7917 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
7919 for_each_mddev(mddev2
, tmp
) {
7920 if (mddev2
== mddev
)
7922 if (!mddev
->parallel_resync
7923 && mddev2
->curr_resync
7924 && match_mddev_units(mddev
, mddev2
)) {
7926 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
7927 /* arbitrarily yield */
7928 mddev
->curr_resync
= 1;
7929 wake_up(&resync_wait
);
7931 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
7932 /* no need to wait here, we can wait the next
7933 * time 'round when curr_resync == 2
7936 /* We need to wait 'interruptible' so as not to
7937 * contribute to the load average, and not to
7938 * be caught by 'softlockup'
7940 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
7941 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
7942 mddev2
->curr_resync
>= mddev
->curr_resync
) {
7943 if (mddev2_minor
!= mddev2
->md_minor
) {
7944 mddev2_minor
= mddev2
->md_minor
;
7945 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
7946 desc
, mdname(mddev
),
7950 if (signal_pending(current
))
7951 flush_signals(current
);
7953 finish_wait(&resync_wait
, &wq
);
7956 finish_wait(&resync_wait
, &wq
);
7959 } while (mddev
->curr_resync
< 2);
7962 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
7963 /* resync follows the size requested by the personality,
7964 * which defaults to physical size, but can be virtual size
7966 max_sectors
= mddev
->resync_max_sectors
;
7967 atomic64_set(&mddev
->resync_mismatches
, 0);
7968 /* we don't use the checkpoint if there's a bitmap */
7969 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7970 j
= mddev
->resync_min
;
7971 else if (!mddev
->bitmap
)
7972 j
= mddev
->recovery_cp
;
7974 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
7975 max_sectors
= mddev
->resync_max_sectors
;
7977 /* recovery follows the physical size of devices */
7978 max_sectors
= mddev
->dev_sectors
;
7981 rdev_for_each_rcu(rdev
, mddev
)
7982 if (rdev
->raid_disk
>= 0 &&
7983 !test_bit(Journal
, &rdev
->flags
) &&
7984 !test_bit(Faulty
, &rdev
->flags
) &&
7985 !test_bit(In_sync
, &rdev
->flags
) &&
7986 rdev
->recovery_offset
< j
)
7987 j
= rdev
->recovery_offset
;
7990 /* If there is a bitmap, we need to make sure all
7991 * writes that started before we added a spare
7992 * complete before we start doing a recovery.
7993 * Otherwise the write might complete and (via
7994 * bitmap_endwrite) set a bit in the bitmap after the
7995 * recovery has checked that bit and skipped that
7998 if (mddev
->bitmap
) {
7999 mddev
->pers
->quiesce(mddev
, 1);
8000 mddev
->pers
->quiesce(mddev
, 0);
8004 pr_info("md: %s of RAID array %s\n", desc
, mdname(mddev
));
8005 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev
));
8006 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8007 speed_max(mddev
), desc
);
8009 is_mddev_idle(mddev
, 1); /* this initializes IO event counters */
8012 for (m
= 0; m
< SYNC_MARKS
; m
++) {
8014 mark_cnt
[m
] = io_sectors
;
8017 mddev
->resync_mark
= mark
[last_mark
];
8018 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
8021 * Tune reconstruction:
8023 window
= 32*(PAGE_SIZE
/512);
8024 pr_debug("md: using %dk window, over a total of %lluk.\n",
8025 window
/2, (unsigned long long)max_sectors
/2);
8027 atomic_set(&mddev
->recovery_active
, 0);
8031 pr_debug("md: resuming %s of %s from checkpoint.\n",
8032 desc
, mdname(mddev
));
8033 mddev
->curr_resync
= j
;
8035 mddev
->curr_resync
= 3; /* no longer delayed */
8036 mddev
->curr_resync_completed
= j
;
8037 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
8038 md_new_event(mddev
);
8039 update_time
= jiffies
;
8041 blk_start_plug(&plug
);
8042 while (j
< max_sectors
) {
8047 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8048 ((mddev
->curr_resync
> mddev
->curr_resync_completed
&&
8049 (mddev
->curr_resync
- mddev
->curr_resync_completed
)
8050 > (max_sectors
>> 4)) ||
8051 time_after_eq(jiffies
, update_time
+ UPDATE_FREQUENCY
) ||
8052 (j
- mddev
->curr_resync_completed
)*2
8053 >= mddev
->resync_max
- mddev
->curr_resync_completed
||
8054 mddev
->curr_resync_completed
> mddev
->resync_max
8056 /* time to update curr_resync_completed */
8057 wait_event(mddev
->recovery_wait
,
8058 atomic_read(&mddev
->recovery_active
) == 0);
8059 mddev
->curr_resync_completed
= j
;
8060 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) &&
8061 j
> mddev
->recovery_cp
)
8062 mddev
->recovery_cp
= j
;
8063 update_time
= jiffies
;
8064 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
8065 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
8068 while (j
>= mddev
->resync_max
&&
8069 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
8070 /* As this condition is controlled by user-space,
8071 * we can block indefinitely, so use '_interruptible'
8072 * to avoid triggering warnings.
8074 flush_signals(current
); /* just in case */
8075 wait_event_interruptible(mddev
->recovery_wait
,
8076 mddev
->resync_max
> j
8077 || test_bit(MD_RECOVERY_INTR
,
8081 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8084 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
);
8086 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8090 if (!skipped
) { /* actual IO requested */
8091 io_sectors
+= sectors
;
8092 atomic_add(sectors
, &mddev
->recovery_active
);
8095 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8099 if (j
> max_sectors
)
8100 /* when skipping, extra large numbers can be returned. */
8103 mddev
->curr_resync
= j
;
8104 mddev
->curr_mark_cnt
= io_sectors
;
8105 if (last_check
== 0)
8106 /* this is the earliest that rebuild will be
8107 * visible in /proc/mdstat
8109 md_new_event(mddev
);
8111 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
8114 last_check
= io_sectors
;
8116 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
8118 int next
= (last_mark
+1) % SYNC_MARKS
;
8120 mddev
->resync_mark
= mark
[next
];
8121 mddev
->resync_mark_cnt
= mark_cnt
[next
];
8122 mark
[next
] = jiffies
;
8123 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
8127 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8131 * this loop exits only if either when we are slower than
8132 * the 'hard' speed limit, or the system was IO-idle for
8134 * the system might be non-idle CPU-wise, but we only care
8135 * about not overloading the IO subsystem. (things like an
8136 * e2fsck being done on the RAID array should execute fast)
8140 recovery_done
= io_sectors
- atomic_read(&mddev
->recovery_active
);
8141 currspeed
= ((unsigned long)(recovery_done
- mddev
->resync_mark_cnt
))/2
8142 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
8144 if (currspeed
> speed_min(mddev
)) {
8145 if (currspeed
> speed_max(mddev
)) {
8149 if (!is_mddev_idle(mddev
, 0)) {
8151 * Give other IO more of a chance.
8152 * The faster the devices, the less we wait.
8154 wait_event(mddev
->recovery_wait
,
8155 !atomic_read(&mddev
->recovery_active
));
8159 pr_info("md: %s: %s %s.\n",mdname(mddev
), desc
,
8160 test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)
8161 ? "interrupted" : "done");
8163 * this also signals 'finished resyncing' to md_stop
8165 blk_finish_plug(&plug
);
8166 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
8168 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8169 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
8170 mddev
->curr_resync
> 3) {
8171 mddev
->curr_resync_completed
= mddev
->curr_resync
;
8172 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
8174 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
);
8176 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
8177 mddev
->curr_resync
> 3) {
8178 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
8179 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
8180 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
8181 pr_debug("md: checkpointing %s of %s.\n",
8182 desc
, mdname(mddev
));
8183 if (test_bit(MD_RECOVERY_ERROR
,
8185 mddev
->recovery_cp
=
8186 mddev
->curr_resync_completed
;
8188 mddev
->recovery_cp
=
8192 mddev
->recovery_cp
= MaxSector
;
8194 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8195 mddev
->curr_resync
= MaxSector
;
8197 rdev_for_each_rcu(rdev
, mddev
)
8198 if (rdev
->raid_disk
>= 0 &&
8199 mddev
->delta_disks
>= 0 &&
8200 !test_bit(Journal
, &rdev
->flags
) &&
8201 !test_bit(Faulty
, &rdev
->flags
) &&
8202 !test_bit(In_sync
, &rdev
->flags
) &&
8203 rdev
->recovery_offset
< mddev
->curr_resync
)
8204 rdev
->recovery_offset
= mddev
->curr_resync
;
8209 /* set CHANGE_PENDING here since maybe another update is needed,
8210 * so other nodes are informed. It should be harmless for normal
8212 set_mask_bits(&mddev
->sb_flags
, 0,
8213 BIT(MD_SB_CHANGE_PENDING
) | BIT(MD_SB_CHANGE_DEVS
));
8215 spin_lock(&mddev
->lock
);
8216 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
8217 /* We completed so min/max setting can be forgotten if used. */
8218 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
8219 mddev
->resync_min
= 0;
8220 mddev
->resync_max
= MaxSector
;
8221 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
8222 mddev
->resync_min
= mddev
->curr_resync_completed
;
8223 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
8224 mddev
->curr_resync
= 0;
8225 spin_unlock(&mddev
->lock
);
8227 wake_up(&resync_wait
);
8228 md_wakeup_thread(mddev
->thread
);
8231 EXPORT_SYMBOL_GPL(md_do_sync
);
8233 static int remove_and_add_spares(struct mddev
*mddev
,
8234 struct md_rdev
*this)
8236 struct md_rdev
*rdev
;
8239 bool remove_some
= false;
8241 rdev_for_each(rdev
, mddev
) {
8242 if ((this == NULL
|| rdev
== this) &&
8243 rdev
->raid_disk
>= 0 &&
8244 !test_bit(Blocked
, &rdev
->flags
) &&
8245 test_bit(Faulty
, &rdev
->flags
) &&
8246 atomic_read(&rdev
->nr_pending
)==0) {
8247 /* Faulty non-Blocked devices with nr_pending == 0
8248 * never get nr_pending incremented,
8249 * never get Faulty cleared, and never get Blocked set.
8250 * So we can synchronize_rcu now rather than once per device
8253 set_bit(RemoveSynchronized
, &rdev
->flags
);
8259 rdev_for_each(rdev
, mddev
) {
8260 if ((this == NULL
|| rdev
== this) &&
8261 rdev
->raid_disk
>= 0 &&
8262 !test_bit(Blocked
, &rdev
->flags
) &&
8263 ((test_bit(RemoveSynchronized
, &rdev
->flags
) ||
8264 (!test_bit(In_sync
, &rdev
->flags
) &&
8265 !test_bit(Journal
, &rdev
->flags
))) &&
8266 atomic_read(&rdev
->nr_pending
)==0)) {
8267 if (mddev
->pers
->hot_remove_disk(
8268 mddev
, rdev
) == 0) {
8269 sysfs_unlink_rdev(mddev
, rdev
);
8270 rdev
->raid_disk
= -1;
8274 if (remove_some
&& test_bit(RemoveSynchronized
, &rdev
->flags
))
8275 clear_bit(RemoveSynchronized
, &rdev
->flags
);
8278 if (removed
&& mddev
->kobj
.sd
)
8279 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
8281 if (this && removed
)
8284 rdev_for_each(rdev
, mddev
) {
8285 if (this && this != rdev
)
8287 if (test_bit(Candidate
, &rdev
->flags
))
8289 if (rdev
->raid_disk
>= 0 &&
8290 !test_bit(In_sync
, &rdev
->flags
) &&
8291 !test_bit(Journal
, &rdev
->flags
) &&
8292 !test_bit(Faulty
, &rdev
->flags
))
8294 if (rdev
->raid_disk
>= 0)
8296 if (test_bit(Faulty
, &rdev
->flags
))
8298 if (!test_bit(Journal
, &rdev
->flags
)) {
8300 ! (rdev
->saved_raid_disk
>= 0 &&
8301 !test_bit(Bitmap_sync
, &rdev
->flags
)))
8304 rdev
->recovery_offset
= 0;
8307 hot_add_disk(mddev
, rdev
) == 0) {
8308 if (sysfs_link_rdev(mddev
, rdev
))
8309 /* failure here is OK */;
8310 if (!test_bit(Journal
, &rdev
->flags
))
8312 md_new_event(mddev
);
8313 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
8318 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
8322 static void md_start_sync(struct work_struct
*ws
)
8324 struct mddev
*mddev
= container_of(ws
, struct mddev
, del_work
);
8326 mddev
->sync_thread
= md_register_thread(md_do_sync
,
8329 if (!mddev
->sync_thread
) {
8330 pr_warn("%s: could not start resync thread...\n",
8332 /* leave the spares where they are, it shouldn't hurt */
8333 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
8334 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
8335 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
8336 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
8337 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
8338 wake_up(&resync_wait
);
8339 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
8341 if (mddev
->sysfs_action
)
8342 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
8344 md_wakeup_thread(mddev
->sync_thread
);
8345 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
8346 md_new_event(mddev
);
8350 * This routine is regularly called by all per-raid-array threads to
8351 * deal with generic issues like resync and super-block update.
8352 * Raid personalities that don't have a thread (linear/raid0) do not
8353 * need this as they never do any recovery or update the superblock.
8355 * It does not do any resync itself, but rather "forks" off other threads
8356 * to do that as needed.
8357 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8358 * "->recovery" and create a thread at ->sync_thread.
8359 * When the thread finishes it sets MD_RECOVERY_DONE
8360 * and wakeups up this thread which will reap the thread and finish up.
8361 * This thread also removes any faulty devices (with nr_pending == 0).
8363 * The overall approach is:
8364 * 1/ if the superblock needs updating, update it.
8365 * 2/ If a recovery thread is running, don't do anything else.
8366 * 3/ If recovery has finished, clean up, possibly marking spares active.
8367 * 4/ If there are any faulty devices, remove them.
8368 * 5/ If array is degraded, try to add spares devices
8369 * 6/ If array has spares or is not in-sync, start a resync thread.
8371 void md_check_recovery(struct mddev
*mddev
)
8373 if (mddev
->suspended
)
8377 bitmap_daemon_work(mddev
);
8379 if (signal_pending(current
)) {
8380 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
8381 pr_debug("md: %s in immediate safe mode\n",
8383 mddev
->safemode
= 2;
8385 flush_signals(current
);
8388 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
8391 (mddev
->sb_flags
& ~ (1<<MD_SB_CHANGE_PENDING
)) ||
8392 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
8393 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
8394 test_bit(MD_RELOAD_SB
, &mddev
->flags
) ||
8395 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
8396 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
8397 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
8401 if (mddev_trylock(mddev
)) {
8405 struct md_rdev
*rdev
;
8406 if (!mddev
->external
&& mddev
->in_sync
)
8407 /* 'Blocked' flag not needed as failed devices
8408 * will be recorded if array switched to read/write.
8409 * Leaving it set will prevent the device
8410 * from being removed.
8412 rdev_for_each(rdev
, mddev
)
8413 clear_bit(Blocked
, &rdev
->flags
);
8414 /* On a read-only array we can:
8415 * - remove failed devices
8416 * - add already-in_sync devices if the array itself
8418 * As we only add devices that are already in-sync,
8419 * we can activate the spares immediately.
8421 remove_and_add_spares(mddev
, NULL
);
8422 /* There is no thread, but we need to call
8423 * ->spare_active and clear saved_raid_disk
8425 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8426 md_reap_sync_thread(mddev
);
8427 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8428 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8429 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
8433 if (mddev_is_clustered(mddev
)) {
8434 struct md_rdev
*rdev
;
8435 /* kick the device if another node issued a
8438 rdev_for_each(rdev
, mddev
) {
8439 if (test_and_clear_bit(ClusterRemove
, &rdev
->flags
) &&
8440 rdev
->raid_disk
< 0)
8441 md_kick_rdev_from_array(rdev
);
8444 if (test_and_clear_bit(MD_RELOAD_SB
, &mddev
->flags
))
8445 md_reload_sb(mddev
, mddev
->good_device_nr
);
8448 if (!mddev
->external
) {
8450 spin_lock(&mddev
->lock
);
8451 if (mddev
->safemode
&&
8452 !atomic_read(&mddev
->writes_pending
) &&
8454 mddev
->recovery_cp
== MaxSector
) {
8457 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
8459 if (mddev
->safemode
== 1)
8460 mddev
->safemode
= 0;
8461 spin_unlock(&mddev
->lock
);
8463 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
8466 if (mddev
->sb_flags
)
8467 md_update_sb(mddev
, 0);
8469 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
8470 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
8471 /* resync/recovery still happening */
8472 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8475 if (mddev
->sync_thread
) {
8476 md_reap_sync_thread(mddev
);
8479 /* Set RUNNING before clearing NEEDED to avoid
8480 * any transients in the value of "sync_action".
8482 mddev
->curr_resync_completed
= 0;
8483 spin_lock(&mddev
->lock
);
8484 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
8485 spin_unlock(&mddev
->lock
);
8486 /* Clear some bits that don't mean anything, but
8489 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8490 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
8492 if (!test_and_clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
8493 test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
8495 /* no recovery is running.
8496 * remove any failed drives, then
8497 * add spares if possible.
8498 * Spares are also removed and re-added, to allow
8499 * the personality to fail the re-add.
8502 if (mddev
->reshape_position
!= MaxSector
) {
8503 if (mddev
->pers
->check_reshape
== NULL
||
8504 mddev
->pers
->check_reshape(mddev
) != 0)
8505 /* Cannot proceed */
8507 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
8508 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8509 } else if ((spares
= remove_and_add_spares(mddev
, NULL
))) {
8510 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
8511 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
8512 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
8513 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8514 } else if (mddev
->recovery_cp
< MaxSector
) {
8515 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
8516 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8517 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
8518 /* nothing to be done ... */
8521 if (mddev
->pers
->sync_request
) {
8523 /* We are adding a device or devices to an array
8524 * which has the bitmap stored on all devices.
8525 * So make sure all bitmap pages get written
8527 bitmap_write_all(mddev
->bitmap
);
8529 INIT_WORK(&mddev
->del_work
, md_start_sync
);
8530 queue_work(md_misc_wq
, &mddev
->del_work
);
8534 if (!mddev
->sync_thread
) {
8535 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
8536 wake_up(&resync_wait
);
8537 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
8539 if (mddev
->sysfs_action
)
8540 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
8543 wake_up(&mddev
->sb_wait
);
8544 mddev_unlock(mddev
);
8547 EXPORT_SYMBOL(md_check_recovery
);
8549 void md_reap_sync_thread(struct mddev
*mddev
)
8551 struct md_rdev
*rdev
;
8553 /* resync has finished, collect result */
8554 md_unregister_thread(&mddev
->sync_thread
);
8555 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
8556 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
8558 /* activate any spares */
8559 if (mddev
->pers
->spare_active(mddev
)) {
8560 sysfs_notify(&mddev
->kobj
, NULL
,
8562 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
8565 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8566 mddev
->pers
->finish_reshape
)
8567 mddev
->pers
->finish_reshape(mddev
);
8569 /* If array is no-longer degraded, then any saved_raid_disk
8570 * information must be scrapped.
8572 if (!mddev
->degraded
)
8573 rdev_for_each(rdev
, mddev
)
8574 rdev
->saved_raid_disk
= -1;
8576 md_update_sb(mddev
, 1);
8577 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
8578 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
8580 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED
, &mddev
->flags
))
8581 md_cluster_ops
->resync_finish(mddev
);
8582 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
8583 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
8584 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
8585 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
8586 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
8587 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
8588 wake_up(&resync_wait
);
8589 /* flag recovery needed just to double check */
8590 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8591 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
8592 md_new_event(mddev
);
8593 if (mddev
->event_work
.func
)
8594 queue_work(md_misc_wq
, &mddev
->event_work
);
8596 EXPORT_SYMBOL(md_reap_sync_thread
);
8598 void md_wait_for_blocked_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
8600 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
8601 wait_event_timeout(rdev
->blocked_wait
,
8602 !test_bit(Blocked
, &rdev
->flags
) &&
8603 !test_bit(BlockedBadBlocks
, &rdev
->flags
),
8604 msecs_to_jiffies(5000));
8605 rdev_dec_pending(rdev
, mddev
);
8607 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
8609 void md_finish_reshape(struct mddev
*mddev
)
8611 /* called be personality module when reshape completes. */
8612 struct md_rdev
*rdev
;
8614 rdev_for_each(rdev
, mddev
) {
8615 if (rdev
->data_offset
> rdev
->new_data_offset
)
8616 rdev
->sectors
+= rdev
->data_offset
- rdev
->new_data_offset
;
8618 rdev
->sectors
-= rdev
->new_data_offset
- rdev
->data_offset
;
8619 rdev
->data_offset
= rdev
->new_data_offset
;
8622 EXPORT_SYMBOL(md_finish_reshape
);
8624 /* Bad block management */
8626 /* Returns 1 on success, 0 on failure */
8627 int rdev_set_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
,
8630 struct mddev
*mddev
= rdev
->mddev
;
8633 s
+= rdev
->new_data_offset
;
8635 s
+= rdev
->data_offset
;
8636 rv
= badblocks_set(&rdev
->badblocks
, s
, sectors
, 0);
8638 /* Make sure they get written out promptly */
8639 if (test_bit(ExternalBbl
, &rdev
->flags
))
8640 sysfs_notify(&rdev
->kobj
, NULL
,
8641 "unacknowledged_bad_blocks");
8642 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
8643 set_mask_bits(&mddev
->sb_flags
, 0,
8644 BIT(MD_SB_CHANGE_CLEAN
) | BIT(MD_SB_CHANGE_PENDING
));
8645 md_wakeup_thread(rdev
->mddev
->thread
);
8650 EXPORT_SYMBOL_GPL(rdev_set_badblocks
);
8652 int rdev_clear_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
,
8657 s
+= rdev
->new_data_offset
;
8659 s
+= rdev
->data_offset
;
8660 rv
= badblocks_clear(&rdev
->badblocks
, s
, sectors
);
8661 if ((rv
== 0) && test_bit(ExternalBbl
, &rdev
->flags
))
8662 sysfs_notify(&rdev
->kobj
, NULL
, "bad_blocks");
8665 EXPORT_SYMBOL_GPL(rdev_clear_badblocks
);
8667 static int md_notify_reboot(struct notifier_block
*this,
8668 unsigned long code
, void *x
)
8670 struct list_head
*tmp
;
8671 struct mddev
*mddev
;
8674 for_each_mddev(mddev
, tmp
) {
8675 if (mddev_trylock(mddev
)) {
8677 __md_stop_writes(mddev
);
8678 if (mddev
->persistent
)
8679 mddev
->safemode
= 2;
8680 mddev_unlock(mddev
);
8685 * certain more exotic SCSI devices are known to be
8686 * volatile wrt too early system reboots. While the
8687 * right place to handle this issue is the given
8688 * driver, we do want to have a safe RAID driver ...
8696 static struct notifier_block md_notifier
= {
8697 .notifier_call
= md_notify_reboot
,
8699 .priority
= INT_MAX
, /* before any real devices */
8702 static void md_geninit(void)
8704 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
8706 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
8709 static int __init
md_init(void)
8713 md_wq
= alloc_workqueue("md", WQ_MEM_RECLAIM
, 0);
8717 md_misc_wq
= alloc_workqueue("md_misc", 0, 0);
8721 if ((ret
= register_blkdev(MD_MAJOR
, "md")) < 0)
8724 if ((ret
= register_blkdev(0, "mdp")) < 0)
8728 blk_register_region(MKDEV(MD_MAJOR
, 0), 512, THIS_MODULE
,
8729 md_probe
, NULL
, NULL
);
8730 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
8731 md_probe
, NULL
, NULL
);
8733 register_reboot_notifier(&md_notifier
);
8734 raid_table_header
= register_sysctl_table(raid_root_table
);
8740 unregister_blkdev(MD_MAJOR
, "md");
8742 destroy_workqueue(md_misc_wq
);
8744 destroy_workqueue(md_wq
);
8749 static void check_sb_changes(struct mddev
*mddev
, struct md_rdev
*rdev
)
8751 struct mdp_superblock_1
*sb
= page_address(rdev
->sb_page
);
8752 struct md_rdev
*rdev2
;
8754 char b
[BDEVNAME_SIZE
];
8756 /* Check for change of roles in the active devices */
8757 rdev_for_each(rdev2
, mddev
) {
8758 if (test_bit(Faulty
, &rdev2
->flags
))
8761 /* Check if the roles changed */
8762 role
= le16_to_cpu(sb
->dev_roles
[rdev2
->desc_nr
]);
8764 if (test_bit(Candidate
, &rdev2
->flags
)) {
8765 if (role
== 0xfffe) {
8766 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2
->bdev
,b
));
8767 md_kick_rdev_from_array(rdev2
);
8771 clear_bit(Candidate
, &rdev2
->flags
);
8774 if (role
!= rdev2
->raid_disk
) {
8776 if (rdev2
->raid_disk
== -1 && role
!= 0xffff) {
8777 rdev2
->saved_raid_disk
= role
;
8778 ret
= remove_and_add_spares(mddev
, rdev2
);
8779 pr_info("Activated spare: %s\n",
8780 bdevname(rdev2
->bdev
,b
));
8781 /* wakeup mddev->thread here, so array could
8782 * perform resync with the new activated disk */
8783 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8784 md_wakeup_thread(mddev
->thread
);
8788 * We just want to do the minimum to mark the disk
8789 * as faulty. The recovery is performed by the
8790 * one who initiated the error.
8792 if ((role
== 0xfffe) || (role
== 0xfffd)) {
8793 md_error(mddev
, rdev2
);
8794 clear_bit(Blocked
, &rdev2
->flags
);
8799 if (mddev
->raid_disks
!= le32_to_cpu(sb
->raid_disks
))
8800 update_raid_disks(mddev
, le32_to_cpu(sb
->raid_disks
));
8802 /* Finally set the event to be up to date */
8803 mddev
->events
= le64_to_cpu(sb
->events
);
8806 static int read_rdev(struct mddev
*mddev
, struct md_rdev
*rdev
)
8809 struct page
*swapout
= rdev
->sb_page
;
8810 struct mdp_superblock_1
*sb
;
8812 /* Store the sb page of the rdev in the swapout temporary
8813 * variable in case we err in the future
8815 rdev
->sb_page
= NULL
;
8816 err
= alloc_disk_sb(rdev
);
8818 ClearPageUptodate(rdev
->sb_page
);
8819 rdev
->sb_loaded
= 0;
8820 err
= super_types
[mddev
->major_version
].
8821 load_super(rdev
, NULL
, mddev
->minor_version
);
8824 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
8825 __func__
, __LINE__
, rdev
->desc_nr
, err
);
8827 put_page(rdev
->sb_page
);
8828 rdev
->sb_page
= swapout
;
8829 rdev
->sb_loaded
= 1;
8833 sb
= page_address(rdev
->sb_page
);
8834 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
8838 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RECOVERY_OFFSET
))
8839 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
8841 /* The other node finished recovery, call spare_active to set
8842 * device In_sync and mddev->degraded
8844 if (rdev
->recovery_offset
== MaxSector
&&
8845 !test_bit(In_sync
, &rdev
->flags
) &&
8846 mddev
->pers
->spare_active(mddev
))
8847 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
8853 void md_reload_sb(struct mddev
*mddev
, int nr
)
8855 struct md_rdev
*rdev
;
8859 rdev_for_each_rcu(rdev
, mddev
) {
8860 if (rdev
->desc_nr
== nr
)
8864 if (!rdev
|| rdev
->desc_nr
!= nr
) {
8865 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__
, __LINE__
, nr
);
8869 err
= read_rdev(mddev
, rdev
);
8873 check_sb_changes(mddev
, rdev
);
8875 /* Read all rdev's to update recovery_offset */
8876 rdev_for_each_rcu(rdev
, mddev
)
8877 read_rdev(mddev
, rdev
);
8879 EXPORT_SYMBOL(md_reload_sb
);
8884 * Searches all registered partitions for autorun RAID arrays
8888 static DEFINE_MUTEX(detected_devices_mutex
);
8889 static LIST_HEAD(all_detected_devices
);
8890 struct detected_devices_node
{
8891 struct list_head list
;
8895 void md_autodetect_dev(dev_t dev
)
8897 struct detected_devices_node
*node_detected_dev
;
8899 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
8900 if (node_detected_dev
) {
8901 node_detected_dev
->dev
= dev
;
8902 mutex_lock(&detected_devices_mutex
);
8903 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
8904 mutex_unlock(&detected_devices_mutex
);
8908 static void autostart_arrays(int part
)
8910 struct md_rdev
*rdev
;
8911 struct detected_devices_node
*node_detected_dev
;
8913 int i_scanned
, i_passed
;
8918 pr_info("md: Autodetecting RAID arrays.\n");
8920 mutex_lock(&detected_devices_mutex
);
8921 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
8923 node_detected_dev
= list_entry(all_detected_devices
.next
,
8924 struct detected_devices_node
, list
);
8925 list_del(&node_detected_dev
->list
);
8926 dev
= node_detected_dev
->dev
;
8927 kfree(node_detected_dev
);
8928 mutex_unlock(&detected_devices_mutex
);
8929 rdev
= md_import_device(dev
,0, 90);
8930 mutex_lock(&detected_devices_mutex
);
8934 if (test_bit(Faulty
, &rdev
->flags
))
8937 set_bit(AutoDetected
, &rdev
->flags
);
8938 list_add(&rdev
->same_set
, &pending_raid_disks
);
8941 mutex_unlock(&detected_devices_mutex
);
8943 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned
, i_passed
);
8945 autorun_devices(part
);
8948 #endif /* !MODULE */
8950 static __exit
void md_exit(void)
8952 struct mddev
*mddev
;
8953 struct list_head
*tmp
;
8956 blk_unregister_region(MKDEV(MD_MAJOR
,0), 512);
8957 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
8959 unregister_blkdev(MD_MAJOR
,"md");
8960 unregister_blkdev(mdp_major
, "mdp");
8961 unregister_reboot_notifier(&md_notifier
);
8962 unregister_sysctl_table(raid_table_header
);
8964 /* We cannot unload the modules while some process is
8965 * waiting for us in select() or poll() - wake them up
8968 while (waitqueue_active(&md_event_waiters
)) {
8969 /* not safe to leave yet */
8970 wake_up(&md_event_waiters
);
8974 remove_proc_entry("mdstat", NULL
);
8976 for_each_mddev(mddev
, tmp
) {
8977 export_array(mddev
);
8978 mddev
->hold_active
= 0;
8980 destroy_workqueue(md_misc_wq
);
8981 destroy_workqueue(md_wq
);
8984 subsys_initcall(md_init
);
8985 module_exit(md_exit
)
8987 static int get_ro(char *buffer
, struct kernel_param
*kp
)
8989 return sprintf(buffer
, "%d", start_readonly
);
8991 static int set_ro(const char *val
, struct kernel_param
*kp
)
8993 return kstrtouint(val
, 10, (unsigned int *)&start_readonly
);
8996 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
8997 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
8998 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
9000 MODULE_LICENSE("GPL");
9001 MODULE_DESCRIPTION("MD RAID framework");
9003 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);