2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
5 completely rewritten, based on the MD driver code from Marc Zyngier
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
20 Neil Brown <neilb@cse.unsw.edu.au>.
22 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
25 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 Errors, Warnings, etc.
36 pr_crit() for error conditions that risk data loss
37 pr_err() for error conditions that are unexpected, like an IO error
38 or internal inconsistency
39 pr_warn() for error conditions that could have been predicated, like
40 adding a device to an array when it has incompatible metadata
41 pr_info() for every interesting, very rare events, like an array starting
42 or stopping, or resync starting or stopping
43 pr_debug() for everything else.
47 #include <linux/sched/signal.h>
48 #include <linux/kthread.h>
49 #include <linux/blkdev.h>
50 #include <linux/badblocks.h>
51 #include <linux/sysctl.h>
52 #include <linux/seq_file.h>
54 #include <linux/poll.h>
55 #include <linux/ctype.h>
56 #include <linux/string.h>
57 #include <linux/hdreg.h>
58 #include <linux/proc_fs.h>
59 #include <linux/random.h>
60 #include <linux/module.h>
61 #include <linux/reboot.h>
62 #include <linux/file.h>
63 #include <linux/compat.h>
64 #include <linux/delay.h>
65 #include <linux/raid/md_p.h>
66 #include <linux/raid/md_u.h>
67 #include <linux/slab.h>
68 #include <linux/percpu-refcount.h>
70 #include <trace/events/block.h>
72 #include "md-bitmap.h"
73 #include "md-cluster.h"
76 static void autostart_arrays(int part
);
79 /* pers_list is a list of registered personalities protected
81 * pers_lock does extra service to protect accesses to
82 * mddev->thread when the mutex cannot be held.
84 static LIST_HEAD(pers_list
);
85 static DEFINE_SPINLOCK(pers_lock
);
87 static struct kobj_type md_ktype
;
89 struct md_cluster_operations
*md_cluster_ops
;
90 EXPORT_SYMBOL(md_cluster_ops
);
91 struct module
*md_cluster_mod
;
92 EXPORT_SYMBOL(md_cluster_mod
);
94 static DECLARE_WAIT_QUEUE_HEAD(resync_wait
);
95 static struct workqueue_struct
*md_wq
;
96 static struct workqueue_struct
*md_misc_wq
;
98 static int remove_and_add_spares(struct mddev
*mddev
,
99 struct md_rdev
*this);
100 static void mddev_detach(struct mddev
*mddev
);
103 * Default number of read corrections we'll attempt on an rdev
104 * before ejecting it from the array. We divide the read error
105 * count by 2 for every hour elapsed between read errors.
107 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
109 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
110 * is 1000 KB/sec, so the extra system load does not show up that much.
111 * Increase it if you want to have more _guaranteed_ speed. Note that
112 * the RAID driver will use the maximum available bandwidth if the IO
113 * subsystem is idle. There is also an 'absolute maximum' reconstruction
114 * speed limit - in case reconstruction slows down your system despite
117 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
118 * or /sys/block/mdX/md/sync_speed_{min,max}
121 static int sysctl_speed_limit_min
= 1000;
122 static int sysctl_speed_limit_max
= 200000;
123 static inline int speed_min(struct mddev
*mddev
)
125 return mddev
->sync_speed_min
?
126 mddev
->sync_speed_min
: sysctl_speed_limit_min
;
129 static inline int speed_max(struct mddev
*mddev
)
131 return mddev
->sync_speed_max
?
132 mddev
->sync_speed_max
: sysctl_speed_limit_max
;
135 static void * flush_info_alloc(gfp_t gfp_flags
, void *data
)
137 return kzalloc(sizeof(struct flush_info
), gfp_flags
);
139 static void flush_info_free(void *flush_info
, void *data
)
144 static void * flush_bio_alloc(gfp_t gfp_flags
, void *data
)
146 return kzalloc(sizeof(struct flush_bio
), gfp_flags
);
148 static void flush_bio_free(void *flush_bio
, void *data
)
153 static struct ctl_table_header
*raid_table_header
;
155 static struct ctl_table raid_table
[] = {
157 .procname
= "speed_limit_min",
158 .data
= &sysctl_speed_limit_min
,
159 .maxlen
= sizeof(int),
160 .mode
= S_IRUGO
|S_IWUSR
,
161 .proc_handler
= proc_dointvec
,
164 .procname
= "speed_limit_max",
165 .data
= &sysctl_speed_limit_max
,
166 .maxlen
= sizeof(int),
167 .mode
= S_IRUGO
|S_IWUSR
,
168 .proc_handler
= proc_dointvec
,
173 static struct ctl_table raid_dir_table
[] = {
177 .mode
= S_IRUGO
|S_IXUGO
,
183 static struct ctl_table raid_root_table
[] = {
188 .child
= raid_dir_table
,
193 static const struct block_device_operations md_fops
;
195 static int start_readonly
;
198 * The original mechanism for creating an md device is to create
199 * a device node in /dev and to open it. This causes races with device-close.
200 * The preferred method is to write to the "new_array" module parameter.
201 * This can avoid races.
202 * Setting create_on_open to false disables the original mechanism
203 * so all the races disappear.
205 static bool create_on_open
= true;
207 struct bio
*bio_alloc_mddev(gfp_t gfp_mask
, int nr_iovecs
,
212 if (!mddev
|| !bioset_initialized(&mddev
->bio_set
))
213 return bio_alloc(gfp_mask
, nr_iovecs
);
215 b
= bio_alloc_bioset(gfp_mask
, nr_iovecs
, &mddev
->bio_set
);
220 EXPORT_SYMBOL_GPL(bio_alloc_mddev
);
222 static struct bio
*md_bio_alloc_sync(struct mddev
*mddev
)
224 if (!mddev
|| !bioset_initialized(&mddev
->sync_set
))
225 return bio_alloc(GFP_NOIO
, 1);
227 return bio_alloc_bioset(GFP_NOIO
, 1, &mddev
->sync_set
);
231 * We have a system wide 'event count' that is incremented
232 * on any 'interesting' event, and readers of /proc/mdstat
233 * can use 'poll' or 'select' to find out when the event
237 * start array, stop array, error, add device, remove device,
238 * start build, activate spare
240 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters
);
241 static atomic_t md_event_count
;
242 void md_new_event(struct mddev
*mddev
)
244 atomic_inc(&md_event_count
);
245 wake_up(&md_event_waiters
);
247 EXPORT_SYMBOL_GPL(md_new_event
);
250 * Enables to iterate over all existing md arrays
251 * all_mddevs_lock protects this list.
253 static LIST_HEAD(all_mddevs
);
254 static DEFINE_SPINLOCK(all_mddevs_lock
);
257 * iterates through all used mddevs in the system.
258 * We take care to grab the all_mddevs_lock whenever navigating
259 * the list, and to always hold a refcount when unlocked.
260 * Any code which breaks out of this loop while own
261 * a reference to the current mddev and must mddev_put it.
263 #define for_each_mddev(_mddev,_tmp) \
265 for (({ spin_lock(&all_mddevs_lock); \
266 _tmp = all_mddevs.next; \
268 ({ if (_tmp != &all_mddevs) \
269 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
270 spin_unlock(&all_mddevs_lock); \
271 if (_mddev) mddev_put(_mddev); \
272 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
273 _tmp != &all_mddevs;}); \
274 ({ spin_lock(&all_mddevs_lock); \
275 _tmp = _tmp->next;}) \
278 /* Rather than calling directly into the personality make_request function,
279 * IO requests come here first so that we can check if the device is
280 * being suspended pending a reconfiguration.
281 * We hold a refcount over the call to ->make_request. By the time that
282 * call has finished, the bio has been linked into some internal structure
283 * and so is visible to ->quiesce(), so we don't need the refcount any more.
285 static bool is_suspended(struct mddev
*mddev
, struct bio
*bio
)
287 if (mddev
->suspended
)
289 if (bio_data_dir(bio
) != WRITE
)
291 if (mddev
->suspend_lo
>= mddev
->suspend_hi
)
293 if (bio
->bi_iter
.bi_sector
>= mddev
->suspend_hi
)
295 if (bio_end_sector(bio
) < mddev
->suspend_lo
)
300 void md_handle_request(struct mddev
*mddev
, struct bio
*bio
)
304 if (is_suspended(mddev
, bio
)) {
307 prepare_to_wait(&mddev
->sb_wait
, &__wait
,
308 TASK_UNINTERRUPTIBLE
);
309 if (!is_suspended(mddev
, bio
))
315 finish_wait(&mddev
->sb_wait
, &__wait
);
317 atomic_inc(&mddev
->active_io
);
320 if (!mddev
->pers
->make_request(mddev
, bio
)) {
321 atomic_dec(&mddev
->active_io
);
322 wake_up(&mddev
->sb_wait
);
323 goto check_suspended
;
326 if (atomic_dec_and_test(&mddev
->active_io
) && mddev
->suspended
)
327 wake_up(&mddev
->sb_wait
);
329 EXPORT_SYMBOL(md_handle_request
);
331 static blk_qc_t
md_make_request(struct request_queue
*q
, struct bio
*bio
)
333 const int rw
= bio_data_dir(bio
);
334 const int sgrp
= op_stat_group(bio_op(bio
));
335 struct mddev
*mddev
= q
->queuedata
;
336 unsigned int sectors
;
339 blk_queue_split(q
, &bio
);
341 if (mddev
== NULL
|| mddev
->pers
== NULL
) {
343 return BLK_QC_T_NONE
;
345 if (mddev
->ro
== 1 && unlikely(rw
== WRITE
)) {
346 if (bio_sectors(bio
) != 0)
347 bio
->bi_status
= BLK_STS_IOERR
;
349 return BLK_QC_T_NONE
;
353 * save the sectors now since our bio can
354 * go away inside make_request
356 sectors
= bio_sectors(bio
);
357 /* bio could be mergeable after passing to underlayer */
358 bio
->bi_opf
&= ~REQ_NOMERGE
;
360 md_handle_request(mddev
, bio
);
362 cpu
= part_stat_lock();
363 part_stat_inc(cpu
, &mddev
->gendisk
->part0
, ios
[sgrp
]);
364 part_stat_add(cpu
, &mddev
->gendisk
->part0
, sectors
[sgrp
], sectors
);
367 return BLK_QC_T_NONE
;
370 /* mddev_suspend makes sure no new requests are submitted
371 * to the device, and that any requests that have been submitted
372 * are completely handled.
373 * Once mddev_detach() is called and completes, the module will be
376 void mddev_suspend(struct mddev
*mddev
)
378 WARN_ON_ONCE(mddev
->thread
&& current
== mddev
->thread
->tsk
);
379 lockdep_assert_held(&mddev
->reconfig_mutex
);
380 if (mddev
->suspended
++)
383 wake_up(&mddev
->sb_wait
);
384 set_bit(MD_ALLOW_SB_UPDATE
, &mddev
->flags
);
385 smp_mb__after_atomic();
386 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->active_io
) == 0);
387 mddev
->pers
->quiesce(mddev
, 1);
388 clear_bit_unlock(MD_ALLOW_SB_UPDATE
, &mddev
->flags
);
389 wait_event(mddev
->sb_wait
, !test_bit(MD_UPDATING_SB
, &mddev
->flags
));
391 del_timer_sync(&mddev
->safemode_timer
);
393 EXPORT_SYMBOL_GPL(mddev_suspend
);
395 void mddev_resume(struct mddev
*mddev
)
397 lockdep_assert_held(&mddev
->reconfig_mutex
);
398 if (--mddev
->suspended
)
400 wake_up(&mddev
->sb_wait
);
401 mddev
->pers
->quiesce(mddev
, 0);
403 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
404 md_wakeup_thread(mddev
->thread
);
405 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
407 EXPORT_SYMBOL_GPL(mddev_resume
);
409 int mddev_congested(struct mddev
*mddev
, int bits
)
411 struct md_personality
*pers
= mddev
->pers
;
415 if (mddev
->suspended
)
417 else if (pers
&& pers
->congested
)
418 ret
= pers
->congested(mddev
, bits
);
422 EXPORT_SYMBOL_GPL(mddev_congested
);
423 static int md_congested(void *data
, int bits
)
425 struct mddev
*mddev
= data
;
426 return mddev_congested(mddev
, bits
);
430 * Generic flush handling for md
432 static void submit_flushes(struct work_struct
*ws
)
434 struct flush_info
*fi
= container_of(ws
, struct flush_info
, flush_work
);
435 struct mddev
*mddev
= fi
->mddev
;
436 struct bio
*bio
= fi
->bio
;
438 bio
->bi_opf
&= ~REQ_PREFLUSH
;
439 md_handle_request(mddev
, bio
);
441 mempool_free(fi
, mddev
->flush_pool
);
444 static void md_end_flush(struct bio
*fbio
)
446 struct flush_bio
*fb
= fbio
->bi_private
;
447 struct md_rdev
*rdev
= fb
->rdev
;
448 struct flush_info
*fi
= fb
->fi
;
449 struct bio
*bio
= fi
->bio
;
450 struct mddev
*mddev
= fi
->mddev
;
452 rdev_dec_pending(rdev
, mddev
);
454 if (atomic_dec_and_test(&fi
->flush_pending
)) {
455 if (bio
->bi_iter
.bi_size
== 0)
456 /* an empty barrier - all done */
459 INIT_WORK(&fi
->flush_work
, submit_flushes
);
460 queue_work(md_wq
, &fi
->flush_work
);
464 mempool_free(fb
, mddev
->flush_bio_pool
);
468 void md_flush_request(struct mddev
*mddev
, struct bio
*bio
)
470 struct md_rdev
*rdev
;
471 struct flush_info
*fi
;
473 fi
= mempool_alloc(mddev
->flush_pool
, GFP_NOIO
);
477 atomic_set(&fi
->flush_pending
, 1);
480 rdev_for_each_rcu(rdev
, mddev
)
481 if (rdev
->raid_disk
>= 0 &&
482 !test_bit(Faulty
, &rdev
->flags
)) {
483 /* Take two references, one is dropped
484 * when request finishes, one after
485 * we reclaim rcu_read_lock
488 struct flush_bio
*fb
;
489 atomic_inc(&rdev
->nr_pending
);
490 atomic_inc(&rdev
->nr_pending
);
493 fb
= mempool_alloc(mddev
->flush_bio_pool
, GFP_NOIO
);
497 bi
= bio_alloc_mddev(GFP_NOIO
, 0, mddev
);
498 bio_set_dev(bi
, rdev
->bdev
);
499 bi
->bi_end_io
= md_end_flush
;
501 bi
->bi_opf
= REQ_OP_WRITE
| REQ_PREFLUSH
;
503 atomic_inc(&fi
->flush_pending
);
507 rdev_dec_pending(rdev
, mddev
);
511 if (atomic_dec_and_test(&fi
->flush_pending
)) {
512 if (bio
->bi_iter
.bi_size
== 0)
513 /* an empty barrier - all done */
516 INIT_WORK(&fi
->flush_work
, submit_flushes
);
517 queue_work(md_wq
, &fi
->flush_work
);
521 EXPORT_SYMBOL(md_flush_request
);
523 static inline struct mddev
*mddev_get(struct mddev
*mddev
)
525 atomic_inc(&mddev
->active
);
529 static void mddev_delayed_delete(struct work_struct
*ws
);
531 static void mddev_put(struct mddev
*mddev
)
533 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
535 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
) &&
536 mddev
->ctime
== 0 && !mddev
->hold_active
) {
537 /* Array is not configured at all, and not held active,
539 list_del_init(&mddev
->all_mddevs
);
542 * Call queue_work inside the spinlock so that
543 * flush_workqueue() after mddev_find will succeed in waiting
544 * for the work to be done.
546 INIT_WORK(&mddev
->del_work
, mddev_delayed_delete
);
547 queue_work(md_misc_wq
, &mddev
->del_work
);
549 spin_unlock(&all_mddevs_lock
);
552 static void md_safemode_timeout(struct timer_list
*t
);
554 void mddev_init(struct mddev
*mddev
)
556 kobject_init(&mddev
->kobj
, &md_ktype
);
557 mutex_init(&mddev
->open_mutex
);
558 mutex_init(&mddev
->reconfig_mutex
);
559 mutex_init(&mddev
->bitmap_info
.mutex
);
560 INIT_LIST_HEAD(&mddev
->disks
);
561 INIT_LIST_HEAD(&mddev
->all_mddevs
);
562 timer_setup(&mddev
->safemode_timer
, md_safemode_timeout
, 0);
563 atomic_set(&mddev
->active
, 1);
564 atomic_set(&mddev
->openers
, 0);
565 atomic_set(&mddev
->active_io
, 0);
566 spin_lock_init(&mddev
->lock
);
567 init_waitqueue_head(&mddev
->sb_wait
);
568 init_waitqueue_head(&mddev
->recovery_wait
);
569 mddev
->reshape_position
= MaxSector
;
570 mddev
->reshape_backwards
= 0;
571 mddev
->last_sync_action
= "none";
572 mddev
->resync_min
= 0;
573 mddev
->resync_max
= MaxSector
;
574 mddev
->level
= LEVEL_NONE
;
576 EXPORT_SYMBOL_GPL(mddev_init
);
578 static struct mddev
*mddev_find(dev_t unit
)
580 struct mddev
*mddev
, *new = NULL
;
582 if (unit
&& MAJOR(unit
) != MD_MAJOR
)
583 unit
&= ~((1<<MdpMinorShift
)-1);
586 spin_lock(&all_mddevs_lock
);
589 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
590 if (mddev
->unit
== unit
) {
592 spin_unlock(&all_mddevs_lock
);
598 list_add(&new->all_mddevs
, &all_mddevs
);
599 spin_unlock(&all_mddevs_lock
);
600 new->hold_active
= UNTIL_IOCTL
;
604 /* find an unused unit number */
605 static int next_minor
= 512;
606 int start
= next_minor
;
610 dev
= MKDEV(MD_MAJOR
, next_minor
);
612 if (next_minor
> MINORMASK
)
614 if (next_minor
== start
) {
615 /* Oh dear, all in use. */
616 spin_unlock(&all_mddevs_lock
);
622 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
623 if (mddev
->unit
== dev
) {
629 new->md_minor
= MINOR(dev
);
630 new->hold_active
= UNTIL_STOP
;
631 list_add(&new->all_mddevs
, &all_mddevs
);
632 spin_unlock(&all_mddevs_lock
);
635 spin_unlock(&all_mddevs_lock
);
637 new = kzalloc(sizeof(*new), GFP_KERNEL
);
642 if (MAJOR(unit
) == MD_MAJOR
)
643 new->md_minor
= MINOR(unit
);
645 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
652 static struct attribute_group md_redundancy_group
;
654 void mddev_unlock(struct mddev
*mddev
)
656 if (mddev
->to_remove
) {
657 /* These cannot be removed under reconfig_mutex as
658 * an access to the files will try to take reconfig_mutex
659 * while holding the file unremovable, which leads to
661 * So hold set sysfs_active while the remove in happeing,
662 * and anything else which might set ->to_remove or my
663 * otherwise change the sysfs namespace will fail with
664 * -EBUSY if sysfs_active is still set.
665 * We set sysfs_active under reconfig_mutex and elsewhere
666 * test it under the same mutex to ensure its correct value
669 struct attribute_group
*to_remove
= mddev
->to_remove
;
670 mddev
->to_remove
= NULL
;
671 mddev
->sysfs_active
= 1;
672 mutex_unlock(&mddev
->reconfig_mutex
);
674 if (mddev
->kobj
.sd
) {
675 if (to_remove
!= &md_redundancy_group
)
676 sysfs_remove_group(&mddev
->kobj
, to_remove
);
677 if (mddev
->pers
== NULL
||
678 mddev
->pers
->sync_request
== NULL
) {
679 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
680 if (mddev
->sysfs_action
)
681 sysfs_put(mddev
->sysfs_action
);
682 mddev
->sysfs_action
= NULL
;
685 mddev
->sysfs_active
= 0;
687 mutex_unlock(&mddev
->reconfig_mutex
);
689 /* As we've dropped the mutex we need a spinlock to
690 * make sure the thread doesn't disappear
692 spin_lock(&pers_lock
);
693 md_wakeup_thread(mddev
->thread
);
694 wake_up(&mddev
->sb_wait
);
695 spin_unlock(&pers_lock
);
697 EXPORT_SYMBOL_GPL(mddev_unlock
);
699 struct md_rdev
*md_find_rdev_nr_rcu(struct mddev
*mddev
, int nr
)
701 struct md_rdev
*rdev
;
703 rdev_for_each_rcu(rdev
, mddev
)
704 if (rdev
->desc_nr
== nr
)
709 EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu
);
711 static struct md_rdev
*find_rdev(struct mddev
*mddev
, dev_t dev
)
713 struct md_rdev
*rdev
;
715 rdev_for_each(rdev
, mddev
)
716 if (rdev
->bdev
->bd_dev
== dev
)
722 struct md_rdev
*md_find_rdev_rcu(struct mddev
*mddev
, dev_t dev
)
724 struct md_rdev
*rdev
;
726 rdev_for_each_rcu(rdev
, mddev
)
727 if (rdev
->bdev
->bd_dev
== dev
)
732 EXPORT_SYMBOL_GPL(md_find_rdev_rcu
);
734 static struct md_personality
*find_pers(int level
, char *clevel
)
736 struct md_personality
*pers
;
737 list_for_each_entry(pers
, &pers_list
, list
) {
738 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
740 if (strcmp(pers
->name
, clevel
)==0)
746 /* return the offset of the super block in 512byte sectors */
747 static inline sector_t
calc_dev_sboffset(struct md_rdev
*rdev
)
749 sector_t num_sectors
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
750 return MD_NEW_SIZE_SECTORS(num_sectors
);
753 static int alloc_disk_sb(struct md_rdev
*rdev
)
755 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
761 void md_rdev_clear(struct md_rdev
*rdev
)
764 put_page(rdev
->sb_page
);
766 rdev
->sb_page
= NULL
;
771 put_page(rdev
->bb_page
);
772 rdev
->bb_page
= NULL
;
774 badblocks_exit(&rdev
->badblocks
);
776 EXPORT_SYMBOL_GPL(md_rdev_clear
);
778 static void super_written(struct bio
*bio
)
780 struct md_rdev
*rdev
= bio
->bi_private
;
781 struct mddev
*mddev
= rdev
->mddev
;
783 if (bio
->bi_status
) {
784 pr_err("md: super_written gets error=%d\n", bio
->bi_status
);
785 md_error(mddev
, rdev
);
786 if (!test_bit(Faulty
, &rdev
->flags
)
787 && (bio
->bi_opf
& MD_FAILFAST
)) {
788 set_bit(MD_SB_NEED_REWRITE
, &mddev
->sb_flags
);
789 set_bit(LastDev
, &rdev
->flags
);
792 clear_bit(LastDev
, &rdev
->flags
);
794 if (atomic_dec_and_test(&mddev
->pending_writes
))
795 wake_up(&mddev
->sb_wait
);
796 rdev_dec_pending(rdev
, mddev
);
800 void md_super_write(struct mddev
*mddev
, struct md_rdev
*rdev
,
801 sector_t sector
, int size
, struct page
*page
)
803 /* write first size bytes of page to sector of rdev
804 * Increment mddev->pending_writes before returning
805 * and decrement it on completion, waking up sb_wait
806 * if zero is reached.
807 * If an error occurred, call md_error
815 if (test_bit(Faulty
, &rdev
->flags
))
818 bio
= md_bio_alloc_sync(mddev
);
820 atomic_inc(&rdev
->nr_pending
);
822 bio_set_dev(bio
, rdev
->meta_bdev
? rdev
->meta_bdev
: rdev
->bdev
);
823 bio
->bi_iter
.bi_sector
= sector
;
824 bio_add_page(bio
, page
, size
, 0);
825 bio
->bi_private
= rdev
;
826 bio
->bi_end_io
= super_written
;
828 if (test_bit(MD_FAILFAST_SUPPORTED
, &mddev
->flags
) &&
829 test_bit(FailFast
, &rdev
->flags
) &&
830 !test_bit(LastDev
, &rdev
->flags
))
832 bio
->bi_opf
= REQ_OP_WRITE
| REQ_SYNC
| REQ_PREFLUSH
| REQ_FUA
| ff
;
834 atomic_inc(&mddev
->pending_writes
);
838 int md_super_wait(struct mddev
*mddev
)
840 /* wait for all superblock writes that were scheduled to complete */
841 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->pending_writes
)==0);
842 if (test_and_clear_bit(MD_SB_NEED_REWRITE
, &mddev
->sb_flags
))
847 int sync_page_io(struct md_rdev
*rdev
, sector_t sector
, int size
,
848 struct page
*page
, int op
, int op_flags
, bool metadata_op
)
850 struct bio
*bio
= md_bio_alloc_sync(rdev
->mddev
);
853 if (metadata_op
&& rdev
->meta_bdev
)
854 bio_set_dev(bio
, rdev
->meta_bdev
);
856 bio_set_dev(bio
, rdev
->bdev
);
857 bio_set_op_attrs(bio
, op
, op_flags
);
859 bio
->bi_iter
.bi_sector
= sector
+ rdev
->sb_start
;
860 else if (rdev
->mddev
->reshape_position
!= MaxSector
&&
861 (rdev
->mddev
->reshape_backwards
==
862 (sector
>= rdev
->mddev
->reshape_position
)))
863 bio
->bi_iter
.bi_sector
= sector
+ rdev
->new_data_offset
;
865 bio
->bi_iter
.bi_sector
= sector
+ rdev
->data_offset
;
866 bio_add_page(bio
, page
, size
, 0);
868 submit_bio_wait(bio
);
870 ret
= !bio
->bi_status
;
874 EXPORT_SYMBOL_GPL(sync_page_io
);
876 static int read_disk_sb(struct md_rdev
*rdev
, int size
)
878 char b
[BDEVNAME_SIZE
];
883 if (!sync_page_io(rdev
, 0, size
, rdev
->sb_page
, REQ_OP_READ
, 0, true))
889 pr_err("md: disabled device %s, could not read superblock.\n",
890 bdevname(rdev
->bdev
,b
));
894 static int md_uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
896 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
897 sb1
->set_uuid1
== sb2
->set_uuid1
&&
898 sb1
->set_uuid2
== sb2
->set_uuid2
&&
899 sb1
->set_uuid3
== sb2
->set_uuid3
;
902 static int md_sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
905 mdp_super_t
*tmp1
, *tmp2
;
907 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
908 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
910 if (!tmp1
|| !tmp2
) {
919 * nr_disks is not constant
924 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
931 static u32
md_csum_fold(u32 csum
)
933 csum
= (csum
& 0xffff) + (csum
>> 16);
934 return (csum
& 0xffff) + (csum
>> 16);
937 static unsigned int calc_sb_csum(mdp_super_t
*sb
)
940 u32
*sb32
= (u32
*)sb
;
942 unsigned int disk_csum
, csum
;
944 disk_csum
= sb
->sb_csum
;
947 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
949 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
952 /* This used to use csum_partial, which was wrong for several
953 * reasons including that different results are returned on
954 * different architectures. It isn't critical that we get exactly
955 * the same return value as before (we always csum_fold before
956 * testing, and that removes any differences). However as we
957 * know that csum_partial always returned a 16bit value on
958 * alphas, do a fold to maximise conformity to previous behaviour.
960 sb
->sb_csum
= md_csum_fold(disk_csum
);
962 sb
->sb_csum
= disk_csum
;
968 * Handle superblock details.
969 * We want to be able to handle multiple superblock formats
970 * so we have a common interface to them all, and an array of
971 * different handlers.
972 * We rely on user-space to write the initial superblock, and support
973 * reading and updating of superblocks.
974 * Interface methods are:
975 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
976 * loads and validates a superblock on dev.
977 * if refdev != NULL, compare superblocks on both devices
979 * 0 - dev has a superblock that is compatible with refdev
980 * 1 - dev has a superblock that is compatible and newer than refdev
981 * so dev should be used as the refdev in future
982 * -EINVAL superblock incompatible or invalid
983 * -othererror e.g. -EIO
985 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
986 * Verify that dev is acceptable into mddev.
987 * The first time, mddev->raid_disks will be 0, and data from
988 * dev should be merged in. Subsequent calls check that dev
989 * is new enough. Return 0 or -EINVAL
991 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
992 * Update the superblock for rdev with data in mddev
993 * This does not write to disc.
999 struct module
*owner
;
1000 int (*load_super
)(struct md_rdev
*rdev
,
1001 struct md_rdev
*refdev
,
1003 int (*validate_super
)(struct mddev
*mddev
,
1004 struct md_rdev
*rdev
);
1005 void (*sync_super
)(struct mddev
*mddev
,
1006 struct md_rdev
*rdev
);
1007 unsigned long long (*rdev_size_change
)(struct md_rdev
*rdev
,
1008 sector_t num_sectors
);
1009 int (*allow_new_offset
)(struct md_rdev
*rdev
,
1010 unsigned long long new_offset
);
1014 * Check that the given mddev has no bitmap.
1016 * This function is called from the run method of all personalities that do not
1017 * support bitmaps. It prints an error message and returns non-zero if mddev
1018 * has a bitmap. Otherwise, it returns 0.
1021 int md_check_no_bitmap(struct mddev
*mddev
)
1023 if (!mddev
->bitmap_info
.file
&& !mddev
->bitmap_info
.offset
)
1025 pr_warn("%s: bitmaps are not supported for %s\n",
1026 mdname(mddev
), mddev
->pers
->name
);
1029 EXPORT_SYMBOL(md_check_no_bitmap
);
1032 * load_super for 0.90.0
1034 static int super_90_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
1036 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1041 * Calculate the position of the superblock (512byte sectors),
1042 * it's at the end of the disk.
1044 * It also happens to be a multiple of 4Kb.
1046 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1048 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
1054 bdevname(rdev
->bdev
, b
);
1055 sb
= page_address(rdev
->sb_page
);
1057 if (sb
->md_magic
!= MD_SB_MAGIC
) {
1058 pr_warn("md: invalid raid superblock magic on %s\n", b
);
1062 if (sb
->major_version
!= 0 ||
1063 sb
->minor_version
< 90 ||
1064 sb
->minor_version
> 91) {
1065 pr_warn("Bad version number %d.%d on %s\n",
1066 sb
->major_version
, sb
->minor_version
, b
);
1070 if (sb
->raid_disks
<= 0)
1073 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
1074 pr_warn("md: invalid superblock checksum on %s\n", b
);
1078 rdev
->preferred_minor
= sb
->md_minor
;
1079 rdev
->data_offset
= 0;
1080 rdev
->new_data_offset
= 0;
1081 rdev
->sb_size
= MD_SB_BYTES
;
1082 rdev
->badblocks
.shift
= -1;
1084 if (sb
->level
== LEVEL_MULTIPATH
)
1087 rdev
->desc_nr
= sb
->this_disk
.number
;
1093 mdp_super_t
*refsb
= page_address(refdev
->sb_page
);
1094 if (!md_uuid_equal(refsb
, sb
)) {
1095 pr_warn("md: %s has different UUID to %s\n",
1096 b
, bdevname(refdev
->bdev
,b2
));
1099 if (!md_sb_equal(refsb
, sb
)) {
1100 pr_warn("md: %s has same UUID but different superblock to %s\n",
1101 b
, bdevname(refdev
->bdev
, b2
));
1105 ev2
= md_event(refsb
);
1111 rdev
->sectors
= rdev
->sb_start
;
1112 /* Limit to 4TB as metadata cannot record more than that.
1113 * (not needed for Linear and RAID0 as metadata doesn't
1116 if (IS_ENABLED(CONFIG_LBDAF
) && (u64
)rdev
->sectors
>= (2ULL << 32) &&
1118 rdev
->sectors
= (sector_t
)(2ULL << 32) - 2;
1120 if (rdev
->sectors
< ((sector_t
)sb
->size
) * 2 && sb
->level
>= 1)
1121 /* "this cannot possibly happen" ... */
1129 * validate_super for 0.90.0
1131 static int super_90_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
1134 mdp_super_t
*sb
= page_address(rdev
->sb_page
);
1135 __u64 ev1
= md_event(sb
);
1137 rdev
->raid_disk
= -1;
1138 clear_bit(Faulty
, &rdev
->flags
);
1139 clear_bit(In_sync
, &rdev
->flags
);
1140 clear_bit(Bitmap_sync
, &rdev
->flags
);
1141 clear_bit(WriteMostly
, &rdev
->flags
);
1143 if (mddev
->raid_disks
== 0) {
1144 mddev
->major_version
= 0;
1145 mddev
->minor_version
= sb
->minor_version
;
1146 mddev
->patch_version
= sb
->patch_version
;
1147 mddev
->external
= 0;
1148 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
1149 mddev
->ctime
= sb
->ctime
;
1150 mddev
->utime
= sb
->utime
;
1151 mddev
->level
= sb
->level
;
1152 mddev
->clevel
[0] = 0;
1153 mddev
->layout
= sb
->layout
;
1154 mddev
->raid_disks
= sb
->raid_disks
;
1155 mddev
->dev_sectors
= ((sector_t
)sb
->size
) * 2;
1156 mddev
->events
= ev1
;
1157 mddev
->bitmap_info
.offset
= 0;
1158 mddev
->bitmap_info
.space
= 0;
1159 /* bitmap can use 60 K after the 4K superblocks */
1160 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
1161 mddev
->bitmap_info
.default_space
= 64*2 - (MD_SB_BYTES
>> 9);
1162 mddev
->reshape_backwards
= 0;
1164 if (mddev
->minor_version
>= 91) {
1165 mddev
->reshape_position
= sb
->reshape_position
;
1166 mddev
->delta_disks
= sb
->delta_disks
;
1167 mddev
->new_level
= sb
->new_level
;
1168 mddev
->new_layout
= sb
->new_layout
;
1169 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
1170 if (mddev
->delta_disks
< 0)
1171 mddev
->reshape_backwards
= 1;
1173 mddev
->reshape_position
= MaxSector
;
1174 mddev
->delta_disks
= 0;
1175 mddev
->new_level
= mddev
->level
;
1176 mddev
->new_layout
= mddev
->layout
;
1177 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1180 if (sb
->state
& (1<<MD_SB_CLEAN
))
1181 mddev
->recovery_cp
= MaxSector
;
1183 if (sb
->events_hi
== sb
->cp_events_hi
&&
1184 sb
->events_lo
== sb
->cp_events_lo
) {
1185 mddev
->recovery_cp
= sb
->recovery_cp
;
1187 mddev
->recovery_cp
= 0;
1190 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
1191 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
1192 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
1193 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
1195 mddev
->max_disks
= MD_SB_DISKS
;
1197 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
1198 mddev
->bitmap_info
.file
== NULL
) {
1199 mddev
->bitmap_info
.offset
=
1200 mddev
->bitmap_info
.default_offset
;
1201 mddev
->bitmap_info
.space
=
1202 mddev
->bitmap_info
.default_space
;
1205 } else if (mddev
->pers
== NULL
) {
1206 /* Insist on good event counter while assembling, except
1207 * for spares (which don't need an event count) */
1209 if (sb
->disks
[rdev
->desc_nr
].state
& (
1210 (1<<MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
)))
1211 if (ev1
< mddev
->events
)
1213 } else if (mddev
->bitmap
) {
1214 /* if adding to array with a bitmap, then we can accept an
1215 * older device ... but not too old.
1217 if (ev1
< mddev
->bitmap
->events_cleared
)
1219 if (ev1
< mddev
->events
)
1220 set_bit(Bitmap_sync
, &rdev
->flags
);
1222 if (ev1
< mddev
->events
)
1223 /* just a hot-add of a new device, leave raid_disk at -1 */
1227 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1228 desc
= sb
->disks
+ rdev
->desc_nr
;
1230 if (desc
->state
& (1<<MD_DISK_FAULTY
))
1231 set_bit(Faulty
, &rdev
->flags
);
1232 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
1233 desc->raid_disk < mddev->raid_disks */) {
1234 set_bit(In_sync
, &rdev
->flags
);
1235 rdev
->raid_disk
= desc
->raid_disk
;
1236 rdev
->saved_raid_disk
= desc
->raid_disk
;
1237 } else if (desc
->state
& (1<<MD_DISK_ACTIVE
)) {
1238 /* active but not in sync implies recovery up to
1239 * reshape position. We don't know exactly where
1240 * that is, so set to zero for now */
1241 if (mddev
->minor_version
>= 91) {
1242 rdev
->recovery_offset
= 0;
1243 rdev
->raid_disk
= desc
->raid_disk
;
1246 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
1247 set_bit(WriteMostly
, &rdev
->flags
);
1248 if (desc
->state
& (1<<MD_DISK_FAILFAST
))
1249 set_bit(FailFast
, &rdev
->flags
);
1250 } else /* MULTIPATH are always insync */
1251 set_bit(In_sync
, &rdev
->flags
);
1256 * sync_super for 0.90.0
1258 static void super_90_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
1261 struct md_rdev
*rdev2
;
1262 int next_spare
= mddev
->raid_disks
;
1264 /* make rdev->sb match mddev data..
1267 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1268 * 3/ any empty disks < next_spare become removed
1270 * disks[0] gets initialised to REMOVED because
1271 * we cannot be sure from other fields if it has
1272 * been initialised or not.
1275 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
1277 rdev
->sb_size
= MD_SB_BYTES
;
1279 sb
= page_address(rdev
->sb_page
);
1281 memset(sb
, 0, sizeof(*sb
));
1283 sb
->md_magic
= MD_SB_MAGIC
;
1284 sb
->major_version
= mddev
->major_version
;
1285 sb
->patch_version
= mddev
->patch_version
;
1286 sb
->gvalid_words
= 0; /* ignored */
1287 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
1288 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
1289 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
1290 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
1292 sb
->ctime
= clamp_t(time64_t
, mddev
->ctime
, 0, U32_MAX
);
1293 sb
->level
= mddev
->level
;
1294 sb
->size
= mddev
->dev_sectors
/ 2;
1295 sb
->raid_disks
= mddev
->raid_disks
;
1296 sb
->md_minor
= mddev
->md_minor
;
1297 sb
->not_persistent
= 0;
1298 sb
->utime
= clamp_t(time64_t
, mddev
->utime
, 0, U32_MAX
);
1300 sb
->events_hi
= (mddev
->events
>>32);
1301 sb
->events_lo
= (u32
)mddev
->events
;
1303 if (mddev
->reshape_position
== MaxSector
)
1304 sb
->minor_version
= 90;
1306 sb
->minor_version
= 91;
1307 sb
->reshape_position
= mddev
->reshape_position
;
1308 sb
->new_level
= mddev
->new_level
;
1309 sb
->delta_disks
= mddev
->delta_disks
;
1310 sb
->new_layout
= mddev
->new_layout
;
1311 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1313 mddev
->minor_version
= sb
->minor_version
;
1316 sb
->recovery_cp
= mddev
->recovery_cp
;
1317 sb
->cp_events_hi
= (mddev
->events
>>32);
1318 sb
->cp_events_lo
= (u32
)mddev
->events
;
1319 if (mddev
->recovery_cp
== MaxSector
)
1320 sb
->state
= (1<< MD_SB_CLEAN
);
1322 sb
->recovery_cp
= 0;
1324 sb
->layout
= mddev
->layout
;
1325 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1327 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
)
1328 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1330 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1331 rdev_for_each(rdev2
, mddev
) {
1334 int is_active
= test_bit(In_sync
, &rdev2
->flags
);
1336 if (rdev2
->raid_disk
>= 0 &&
1337 sb
->minor_version
>= 91)
1338 /* we have nowhere to store the recovery_offset,
1339 * but if it is not below the reshape_position,
1340 * we can piggy-back on that.
1343 if (rdev2
->raid_disk
< 0 ||
1344 test_bit(Faulty
, &rdev2
->flags
))
1347 desc_nr
= rdev2
->raid_disk
;
1349 desc_nr
= next_spare
++;
1350 rdev2
->desc_nr
= desc_nr
;
1351 d
= &sb
->disks
[rdev2
->desc_nr
];
1353 d
->number
= rdev2
->desc_nr
;
1354 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1355 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1357 d
->raid_disk
= rdev2
->raid_disk
;
1359 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1360 if (test_bit(Faulty
, &rdev2
->flags
))
1361 d
->state
= (1<<MD_DISK_FAULTY
);
1362 else if (is_active
) {
1363 d
->state
= (1<<MD_DISK_ACTIVE
);
1364 if (test_bit(In_sync
, &rdev2
->flags
))
1365 d
->state
|= (1<<MD_DISK_SYNC
);
1373 if (test_bit(WriteMostly
, &rdev2
->flags
))
1374 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1375 if (test_bit(FailFast
, &rdev2
->flags
))
1376 d
->state
|= (1<<MD_DISK_FAILFAST
);
1378 /* now set the "removed" and "faulty" bits on any missing devices */
1379 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1380 mdp_disk_t
*d
= &sb
->disks
[i
];
1381 if (d
->state
== 0 && d
->number
== 0) {
1384 d
->state
= (1<<MD_DISK_REMOVED
);
1385 d
->state
|= (1<<MD_DISK_FAULTY
);
1389 sb
->nr_disks
= nr_disks
;
1390 sb
->active_disks
= active
;
1391 sb
->working_disks
= working
;
1392 sb
->failed_disks
= failed
;
1393 sb
->spare_disks
= spare
;
1395 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1396 sb
->sb_csum
= calc_sb_csum(sb
);
1400 * rdev_size_change for 0.90.0
1402 static unsigned long long
1403 super_90_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
1405 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1406 return 0; /* component must fit device */
1407 if (rdev
->mddev
->bitmap_info
.offset
)
1408 return 0; /* can't move bitmap */
1409 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1410 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1411 num_sectors
= rdev
->sb_start
;
1412 /* Limit to 4TB as metadata cannot record more than that.
1413 * 4TB == 2^32 KB, or 2*2^32 sectors.
1415 if (IS_ENABLED(CONFIG_LBDAF
) && (u64
)num_sectors
>= (2ULL << 32) &&
1416 rdev
->mddev
->level
>= 1)
1417 num_sectors
= (sector_t
)(2ULL << 32) - 2;
1419 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1421 } while (md_super_wait(rdev
->mddev
) < 0);
1426 super_90_allow_new_offset(struct md_rdev
*rdev
, unsigned long long new_offset
)
1428 /* non-zero offset changes not possible with v0.90 */
1429 return new_offset
== 0;
1433 * version 1 superblock
1436 static __le32
calc_sb_1_csum(struct mdp_superblock_1
*sb
)
1440 unsigned long long newcsum
;
1441 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1442 __le32
*isuper
= (__le32
*)sb
;
1444 disk_csum
= sb
->sb_csum
;
1447 for (; size
>= 4; size
-= 4)
1448 newcsum
+= le32_to_cpu(*isuper
++);
1451 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1453 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1454 sb
->sb_csum
= disk_csum
;
1455 return cpu_to_le32(csum
);
1458 static int super_1_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
1460 struct mdp_superblock_1
*sb
;
1464 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1468 * Calculate the position of the superblock in 512byte sectors.
1469 * It is always aligned to a 4K boundary and
1470 * depeding on minor_version, it can be:
1471 * 0: At least 8K, but less than 12K, from end of device
1472 * 1: At start of device
1473 * 2: 4K from start of device.
1475 switch(minor_version
) {
1477 sb_start
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1479 sb_start
&= ~(sector_t
)(4*2-1);
1490 rdev
->sb_start
= sb_start
;
1492 /* superblock is rarely larger than 1K, but it can be larger,
1493 * and it is safe to read 4k, so we do that
1495 ret
= read_disk_sb(rdev
, 4096);
1496 if (ret
) return ret
;
1498 sb
= page_address(rdev
->sb_page
);
1500 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1501 sb
->major_version
!= cpu_to_le32(1) ||
1502 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1503 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1504 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1507 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1508 pr_warn("md: invalid superblock checksum on %s\n",
1509 bdevname(rdev
->bdev
,b
));
1512 if (le64_to_cpu(sb
->data_size
) < 10) {
1513 pr_warn("md: data_size too small on %s\n",
1514 bdevname(rdev
->bdev
,b
));
1519 memcmp(sb
->pad3
, sb
->pad3
+1, sizeof(sb
->pad3
) - sizeof(sb
->pad3
[1])))
1520 /* Some padding is non-zero, might be a new feature */
1523 rdev
->preferred_minor
= 0xffff;
1524 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1525 rdev
->new_data_offset
= rdev
->data_offset
;
1526 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
) &&
1527 (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_NEW_OFFSET
))
1528 rdev
->new_data_offset
+= (s32
)le32_to_cpu(sb
->new_offset
);
1529 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1531 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1532 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1533 if (rdev
->sb_size
& bmask
)
1534 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1537 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1540 && rdev
->new_data_offset
< sb_start
+ (rdev
->sb_size
/512))
1543 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1546 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1548 if (!rdev
->bb_page
) {
1549 rdev
->bb_page
= alloc_page(GFP_KERNEL
);
1553 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BAD_BLOCKS
) &&
1554 rdev
->badblocks
.count
== 0) {
1555 /* need to load the bad block list.
1556 * Currently we limit it to one page.
1562 int sectors
= le16_to_cpu(sb
->bblog_size
);
1563 if (sectors
> (PAGE_SIZE
/ 512))
1565 offset
= le32_to_cpu(sb
->bblog_offset
);
1568 bb_sector
= (long long)offset
;
1569 if (!sync_page_io(rdev
, bb_sector
, sectors
<< 9,
1570 rdev
->bb_page
, REQ_OP_READ
, 0, true))
1572 bbp
= (u64
*)page_address(rdev
->bb_page
);
1573 rdev
->badblocks
.shift
= sb
->bblog_shift
;
1574 for (i
= 0 ; i
< (sectors
<< (9-3)) ; i
++, bbp
++) {
1575 u64 bb
= le64_to_cpu(*bbp
);
1576 int count
= bb
& (0x3ff);
1577 u64 sector
= bb
>> 10;
1578 sector
<<= sb
->bblog_shift
;
1579 count
<<= sb
->bblog_shift
;
1582 if (badblocks_set(&rdev
->badblocks
, sector
, count
, 1))
1585 } else if (sb
->bblog_offset
!= 0)
1586 rdev
->badblocks
.shift
= 0;
1588 if ((le32_to_cpu(sb
->feature_map
) &
1589 (MD_FEATURE_PPL
| MD_FEATURE_MULTIPLE_PPLS
))) {
1590 rdev
->ppl
.offset
= (__s16
)le16_to_cpu(sb
->ppl
.offset
);
1591 rdev
->ppl
.size
= le16_to_cpu(sb
->ppl
.size
);
1592 rdev
->ppl
.sector
= rdev
->sb_start
+ rdev
->ppl
.offset
;
1599 struct mdp_superblock_1
*refsb
= page_address(refdev
->sb_page
);
1601 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1602 sb
->level
!= refsb
->level
||
1603 sb
->layout
!= refsb
->layout
||
1604 sb
->chunksize
!= refsb
->chunksize
) {
1605 pr_warn("md: %s has strangely different superblock to %s\n",
1606 bdevname(rdev
->bdev
,b
),
1607 bdevname(refdev
->bdev
,b2
));
1610 ev1
= le64_to_cpu(sb
->events
);
1611 ev2
= le64_to_cpu(refsb
->events
);
1618 if (minor_version
) {
1619 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9);
1620 sectors
-= rdev
->data_offset
;
1622 sectors
= rdev
->sb_start
;
1623 if (sectors
< le64_to_cpu(sb
->data_size
))
1625 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1629 static int super_1_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
1631 struct mdp_superblock_1
*sb
= page_address(rdev
->sb_page
);
1632 __u64 ev1
= le64_to_cpu(sb
->events
);
1634 rdev
->raid_disk
= -1;
1635 clear_bit(Faulty
, &rdev
->flags
);
1636 clear_bit(In_sync
, &rdev
->flags
);
1637 clear_bit(Bitmap_sync
, &rdev
->flags
);
1638 clear_bit(WriteMostly
, &rdev
->flags
);
1640 if (mddev
->raid_disks
== 0) {
1641 mddev
->major_version
= 1;
1642 mddev
->patch_version
= 0;
1643 mddev
->external
= 0;
1644 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1645 mddev
->ctime
= le64_to_cpu(sb
->ctime
);
1646 mddev
->utime
= le64_to_cpu(sb
->utime
);
1647 mddev
->level
= le32_to_cpu(sb
->level
);
1648 mddev
->clevel
[0] = 0;
1649 mddev
->layout
= le32_to_cpu(sb
->layout
);
1650 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1651 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1652 mddev
->events
= ev1
;
1653 mddev
->bitmap_info
.offset
= 0;
1654 mddev
->bitmap_info
.space
= 0;
1655 /* Default location for bitmap is 1K after superblock
1656 * using 3K - total of 4K
1658 mddev
->bitmap_info
.default_offset
= 1024 >> 9;
1659 mddev
->bitmap_info
.default_space
= (4096-1024) >> 9;
1660 mddev
->reshape_backwards
= 0;
1662 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1663 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1665 mddev
->max_disks
= (4096-256)/2;
1667 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1668 mddev
->bitmap_info
.file
== NULL
) {
1669 mddev
->bitmap_info
.offset
=
1670 (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1671 /* Metadata doesn't record how much space is available.
1672 * For 1.0, we assume we can use up to the superblock
1673 * if before, else to 4K beyond superblock.
1674 * For others, assume no change is possible.
1676 if (mddev
->minor_version
> 0)
1677 mddev
->bitmap_info
.space
= 0;
1678 else if (mddev
->bitmap_info
.offset
> 0)
1679 mddev
->bitmap_info
.space
=
1680 8 - mddev
->bitmap_info
.offset
;
1682 mddev
->bitmap_info
.space
=
1683 -mddev
->bitmap_info
.offset
;
1686 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1687 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1688 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1689 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1690 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1691 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1692 if (mddev
->delta_disks
< 0 ||
1693 (mddev
->delta_disks
== 0 &&
1694 (le32_to_cpu(sb
->feature_map
)
1695 & MD_FEATURE_RESHAPE_BACKWARDS
)))
1696 mddev
->reshape_backwards
= 1;
1698 mddev
->reshape_position
= MaxSector
;
1699 mddev
->delta_disks
= 0;
1700 mddev
->new_level
= mddev
->level
;
1701 mddev
->new_layout
= mddev
->layout
;
1702 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1705 if (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_JOURNAL
)
1706 set_bit(MD_HAS_JOURNAL
, &mddev
->flags
);
1708 if (le32_to_cpu(sb
->feature_map
) &
1709 (MD_FEATURE_PPL
| MD_FEATURE_MULTIPLE_PPLS
)) {
1710 if (le32_to_cpu(sb
->feature_map
) &
1711 (MD_FEATURE_BITMAP_OFFSET
| MD_FEATURE_JOURNAL
))
1713 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_PPL
) &&
1714 (le32_to_cpu(sb
->feature_map
) &
1715 MD_FEATURE_MULTIPLE_PPLS
))
1717 set_bit(MD_HAS_PPL
, &mddev
->flags
);
1719 } else if (mddev
->pers
== NULL
) {
1720 /* Insist of good event counter while assembling, except for
1721 * spares (which don't need an event count) */
1723 if (rdev
->desc_nr
>= 0 &&
1724 rdev
->desc_nr
< le32_to_cpu(sb
->max_dev
) &&
1725 (le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) < MD_DISK_ROLE_MAX
||
1726 le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) == MD_DISK_ROLE_JOURNAL
))
1727 if (ev1
< mddev
->events
)
1729 } else if (mddev
->bitmap
) {
1730 /* If adding to array with a bitmap, then we can accept an
1731 * older device, but not too old.
1733 if (ev1
< mddev
->bitmap
->events_cleared
)
1735 if (ev1
< mddev
->events
)
1736 set_bit(Bitmap_sync
, &rdev
->flags
);
1738 if (ev1
< mddev
->events
)
1739 /* just a hot-add of a new device, leave raid_disk at -1 */
1742 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1744 if (rdev
->desc_nr
< 0 ||
1745 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1746 role
= MD_DISK_ROLE_SPARE
;
1749 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1751 case MD_DISK_ROLE_SPARE
: /* spare */
1753 case MD_DISK_ROLE_FAULTY
: /* faulty */
1754 set_bit(Faulty
, &rdev
->flags
);
1756 case MD_DISK_ROLE_JOURNAL
: /* journal device */
1757 if (!(le32_to_cpu(sb
->feature_map
) & MD_FEATURE_JOURNAL
)) {
1758 /* journal device without journal feature */
1759 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
1762 set_bit(Journal
, &rdev
->flags
);
1763 rdev
->journal_tail
= le64_to_cpu(sb
->journal_tail
);
1764 rdev
->raid_disk
= 0;
1767 rdev
->saved_raid_disk
= role
;
1768 if ((le32_to_cpu(sb
->feature_map
) &
1769 MD_FEATURE_RECOVERY_OFFSET
)) {
1770 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1771 if (!(le32_to_cpu(sb
->feature_map
) &
1772 MD_FEATURE_RECOVERY_BITMAP
))
1773 rdev
->saved_raid_disk
= -1;
1775 set_bit(In_sync
, &rdev
->flags
);
1776 rdev
->raid_disk
= role
;
1779 if (sb
->devflags
& WriteMostly1
)
1780 set_bit(WriteMostly
, &rdev
->flags
);
1781 if (sb
->devflags
& FailFast1
)
1782 set_bit(FailFast
, &rdev
->flags
);
1783 if (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_REPLACEMENT
)
1784 set_bit(Replacement
, &rdev
->flags
);
1785 } else /* MULTIPATH are always insync */
1786 set_bit(In_sync
, &rdev
->flags
);
1791 static void super_1_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
1793 struct mdp_superblock_1
*sb
;
1794 struct md_rdev
*rdev2
;
1796 /* make rdev->sb match mddev and rdev data. */
1798 sb
= page_address(rdev
->sb_page
);
1800 sb
->feature_map
= 0;
1802 sb
->recovery_offset
= cpu_to_le64(0);
1803 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1805 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1806 sb
->events
= cpu_to_le64(mddev
->events
);
1808 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1809 else if (test_bit(MD_JOURNAL_CLEAN
, &mddev
->flags
))
1810 sb
->resync_offset
= cpu_to_le64(MaxSector
);
1812 sb
->resync_offset
= cpu_to_le64(0);
1814 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1816 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1817 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
1818 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
1819 sb
->level
= cpu_to_le32(mddev
->level
);
1820 sb
->layout
= cpu_to_le32(mddev
->layout
);
1821 if (test_bit(FailFast
, &rdev
->flags
))
1822 sb
->devflags
|= FailFast1
;
1824 sb
->devflags
&= ~FailFast1
;
1826 if (test_bit(WriteMostly
, &rdev
->flags
))
1827 sb
->devflags
|= WriteMostly1
;
1829 sb
->devflags
&= ~WriteMostly1
;
1830 sb
->data_offset
= cpu_to_le64(rdev
->data_offset
);
1831 sb
->data_size
= cpu_to_le64(rdev
->sectors
);
1833 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
) {
1834 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_info
.offset
);
1835 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1838 if (rdev
->raid_disk
>= 0 && !test_bit(Journal
, &rdev
->flags
) &&
1839 !test_bit(In_sync
, &rdev
->flags
)) {
1841 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1842 sb
->recovery_offset
=
1843 cpu_to_le64(rdev
->recovery_offset
);
1844 if (rdev
->saved_raid_disk
>= 0 && mddev
->bitmap
)
1846 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP
);
1848 /* Note: recovery_offset and journal_tail share space */
1849 if (test_bit(Journal
, &rdev
->flags
))
1850 sb
->journal_tail
= cpu_to_le64(rdev
->journal_tail
);
1851 if (test_bit(Replacement
, &rdev
->flags
))
1853 cpu_to_le32(MD_FEATURE_REPLACEMENT
);
1855 if (mddev
->reshape_position
!= MaxSector
) {
1856 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1857 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1858 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1859 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1860 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1861 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
1862 if (mddev
->delta_disks
== 0 &&
1863 mddev
->reshape_backwards
)
1865 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS
);
1866 if (rdev
->new_data_offset
!= rdev
->data_offset
) {
1868 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET
);
1869 sb
->new_offset
= cpu_to_le32((__u32
)(rdev
->new_data_offset
1870 - rdev
->data_offset
));
1874 if (mddev_is_clustered(mddev
))
1875 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_CLUSTERED
);
1877 if (rdev
->badblocks
.count
== 0)
1878 /* Nothing to do for bad blocks*/ ;
1879 else if (sb
->bblog_offset
== 0)
1880 /* Cannot record bad blocks on this device */
1881 md_error(mddev
, rdev
);
1883 struct badblocks
*bb
= &rdev
->badblocks
;
1884 u64
*bbp
= (u64
*)page_address(rdev
->bb_page
);
1886 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_BAD_BLOCKS
);
1891 seq
= read_seqbegin(&bb
->lock
);
1893 memset(bbp
, 0xff, PAGE_SIZE
);
1895 for (i
= 0 ; i
< bb
->count
; i
++) {
1896 u64 internal_bb
= p
[i
];
1897 u64 store_bb
= ((BB_OFFSET(internal_bb
) << 10)
1898 | BB_LEN(internal_bb
));
1899 bbp
[i
] = cpu_to_le64(store_bb
);
1902 if (read_seqretry(&bb
->lock
, seq
))
1905 bb
->sector
= (rdev
->sb_start
+
1906 (int)le32_to_cpu(sb
->bblog_offset
));
1907 bb
->size
= le16_to_cpu(sb
->bblog_size
);
1912 rdev_for_each(rdev2
, mddev
)
1913 if (rdev2
->desc_nr
+1 > max_dev
)
1914 max_dev
= rdev2
->desc_nr
+1;
1916 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
1918 sb
->max_dev
= cpu_to_le32(max_dev
);
1919 rdev
->sb_size
= max_dev
* 2 + 256;
1920 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1921 if (rdev
->sb_size
& bmask
)
1922 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1924 max_dev
= le32_to_cpu(sb
->max_dev
);
1926 for (i
=0; i
<max_dev
;i
++)
1927 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_SPARE
);
1929 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
))
1930 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_JOURNAL
);
1932 if (test_bit(MD_HAS_PPL
, &mddev
->flags
)) {
1933 if (test_bit(MD_HAS_MULTIPLE_PPLS
, &mddev
->flags
))
1935 cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS
);
1937 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_PPL
);
1938 sb
->ppl
.offset
= cpu_to_le16(rdev
->ppl
.offset
);
1939 sb
->ppl
.size
= cpu_to_le16(rdev
->ppl
.size
);
1942 rdev_for_each(rdev2
, mddev
) {
1944 if (test_bit(Faulty
, &rdev2
->flags
))
1945 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_FAULTY
);
1946 else if (test_bit(In_sync
, &rdev2
->flags
))
1947 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1948 else if (test_bit(Journal
, &rdev2
->flags
))
1949 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_JOURNAL
);
1950 else if (rdev2
->raid_disk
>= 0)
1951 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1953 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_SPARE
);
1956 sb
->sb_csum
= calc_sb_1_csum(sb
);
1959 static unsigned long long
1960 super_1_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
1962 struct mdp_superblock_1
*sb
;
1963 sector_t max_sectors
;
1964 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1965 return 0; /* component must fit device */
1966 if (rdev
->data_offset
!= rdev
->new_data_offset
)
1967 return 0; /* too confusing */
1968 if (rdev
->sb_start
< rdev
->data_offset
) {
1969 /* minor versions 1 and 2; superblock before data */
1970 max_sectors
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1971 max_sectors
-= rdev
->data_offset
;
1972 if (!num_sectors
|| num_sectors
> max_sectors
)
1973 num_sectors
= max_sectors
;
1974 } else if (rdev
->mddev
->bitmap_info
.offset
) {
1975 /* minor version 0 with bitmap we can't move */
1978 /* minor version 0; superblock after data */
1980 sb_start
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) - 8*2;
1981 sb_start
&= ~(sector_t
)(4*2 - 1);
1982 max_sectors
= rdev
->sectors
+ sb_start
- rdev
->sb_start
;
1983 if (!num_sectors
|| num_sectors
> max_sectors
)
1984 num_sectors
= max_sectors
;
1985 rdev
->sb_start
= sb_start
;
1987 sb
= page_address(rdev
->sb_page
);
1988 sb
->data_size
= cpu_to_le64(num_sectors
);
1989 sb
->super_offset
= cpu_to_le64(rdev
->sb_start
);
1990 sb
->sb_csum
= calc_sb_1_csum(sb
);
1992 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1994 } while (md_super_wait(rdev
->mddev
) < 0);
2000 super_1_allow_new_offset(struct md_rdev
*rdev
,
2001 unsigned long long new_offset
)
2003 /* All necessary checks on new >= old have been done */
2004 struct bitmap
*bitmap
;
2005 if (new_offset
>= rdev
->data_offset
)
2008 /* with 1.0 metadata, there is no metadata to tread on
2009 * so we can always move back */
2010 if (rdev
->mddev
->minor_version
== 0)
2013 /* otherwise we must be sure not to step on
2014 * any metadata, so stay:
2015 * 36K beyond start of superblock
2016 * beyond end of badblocks
2017 * beyond write-intent bitmap
2019 if (rdev
->sb_start
+ (32+4)*2 > new_offset
)
2021 bitmap
= rdev
->mddev
->bitmap
;
2022 if (bitmap
&& !rdev
->mddev
->bitmap_info
.file
&&
2023 rdev
->sb_start
+ rdev
->mddev
->bitmap_info
.offset
+
2024 bitmap
->storage
.file_pages
* (PAGE_SIZE
>>9) > new_offset
)
2026 if (rdev
->badblocks
.sector
+ rdev
->badblocks
.size
> new_offset
)
2032 static struct super_type super_types
[] = {
2035 .owner
= THIS_MODULE
,
2036 .load_super
= super_90_load
,
2037 .validate_super
= super_90_validate
,
2038 .sync_super
= super_90_sync
,
2039 .rdev_size_change
= super_90_rdev_size_change
,
2040 .allow_new_offset
= super_90_allow_new_offset
,
2044 .owner
= THIS_MODULE
,
2045 .load_super
= super_1_load
,
2046 .validate_super
= super_1_validate
,
2047 .sync_super
= super_1_sync
,
2048 .rdev_size_change
= super_1_rdev_size_change
,
2049 .allow_new_offset
= super_1_allow_new_offset
,
2053 static void sync_super(struct mddev
*mddev
, struct md_rdev
*rdev
)
2055 if (mddev
->sync_super
) {
2056 mddev
->sync_super(mddev
, rdev
);
2060 BUG_ON(mddev
->major_version
>= ARRAY_SIZE(super_types
));
2062 super_types
[mddev
->major_version
].sync_super(mddev
, rdev
);
2065 static int match_mddev_units(struct mddev
*mddev1
, struct mddev
*mddev2
)
2067 struct md_rdev
*rdev
, *rdev2
;
2070 rdev_for_each_rcu(rdev
, mddev1
) {
2071 if (test_bit(Faulty
, &rdev
->flags
) ||
2072 test_bit(Journal
, &rdev
->flags
) ||
2073 rdev
->raid_disk
== -1)
2075 rdev_for_each_rcu(rdev2
, mddev2
) {
2076 if (test_bit(Faulty
, &rdev2
->flags
) ||
2077 test_bit(Journal
, &rdev2
->flags
) ||
2078 rdev2
->raid_disk
== -1)
2080 if (rdev
->bdev
->bd_contains
==
2081 rdev2
->bdev
->bd_contains
) {
2091 static LIST_HEAD(pending_raid_disks
);
2094 * Try to register data integrity profile for an mddev
2096 * This is called when an array is started and after a disk has been kicked
2097 * from the array. It only succeeds if all working and active component devices
2098 * are integrity capable with matching profiles.
2100 int md_integrity_register(struct mddev
*mddev
)
2102 struct md_rdev
*rdev
, *reference
= NULL
;
2104 if (list_empty(&mddev
->disks
))
2105 return 0; /* nothing to do */
2106 if (!mddev
->gendisk
|| blk_get_integrity(mddev
->gendisk
))
2107 return 0; /* shouldn't register, or already is */
2108 rdev_for_each(rdev
, mddev
) {
2109 /* skip spares and non-functional disks */
2110 if (test_bit(Faulty
, &rdev
->flags
))
2112 if (rdev
->raid_disk
< 0)
2115 /* Use the first rdev as the reference */
2119 /* does this rdev's profile match the reference profile? */
2120 if (blk_integrity_compare(reference
->bdev
->bd_disk
,
2121 rdev
->bdev
->bd_disk
) < 0)
2124 if (!reference
|| !bdev_get_integrity(reference
->bdev
))
2127 * All component devices are integrity capable and have matching
2128 * profiles, register the common profile for the md device.
2130 blk_integrity_register(mddev
->gendisk
,
2131 bdev_get_integrity(reference
->bdev
));
2133 pr_debug("md: data integrity enabled on %s\n", mdname(mddev
));
2134 if (bioset_integrity_create(&mddev
->bio_set
, BIO_POOL_SIZE
)) {
2135 pr_err("md: failed to create integrity pool for %s\n",
2141 EXPORT_SYMBOL(md_integrity_register
);
2144 * Attempt to add an rdev, but only if it is consistent with the current
2147 int md_integrity_add_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
2149 struct blk_integrity
*bi_rdev
;
2150 struct blk_integrity
*bi_mddev
;
2151 char name
[BDEVNAME_SIZE
];
2153 if (!mddev
->gendisk
)
2156 bi_rdev
= bdev_get_integrity(rdev
->bdev
);
2157 bi_mddev
= blk_get_integrity(mddev
->gendisk
);
2159 if (!bi_mddev
) /* nothing to do */
2162 if (blk_integrity_compare(mddev
->gendisk
, rdev
->bdev
->bd_disk
) != 0) {
2163 pr_err("%s: incompatible integrity profile for %s\n",
2164 mdname(mddev
), bdevname(rdev
->bdev
, name
));
2170 EXPORT_SYMBOL(md_integrity_add_rdev
);
2172 static int bind_rdev_to_array(struct md_rdev
*rdev
, struct mddev
*mddev
)
2174 char b
[BDEVNAME_SIZE
];
2178 /* prevent duplicates */
2179 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
2182 if ((bdev_read_only(rdev
->bdev
) || bdev_read_only(rdev
->meta_bdev
)) &&
2186 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2187 if (!test_bit(Journal
, &rdev
->flags
) &&
2189 (mddev
->dev_sectors
== 0 || rdev
->sectors
< mddev
->dev_sectors
)) {
2191 /* Cannot change size, so fail
2192 * If mddev->level <= 0, then we don't care
2193 * about aligning sizes (e.g. linear)
2195 if (mddev
->level
> 0)
2198 mddev
->dev_sectors
= rdev
->sectors
;
2201 /* Verify rdev->desc_nr is unique.
2202 * If it is -1, assign a free number, else
2203 * check number is not in use
2206 if (rdev
->desc_nr
< 0) {
2209 choice
= mddev
->raid_disks
;
2210 while (md_find_rdev_nr_rcu(mddev
, choice
))
2212 rdev
->desc_nr
= choice
;
2214 if (md_find_rdev_nr_rcu(mddev
, rdev
->desc_nr
)) {
2220 if (!test_bit(Journal
, &rdev
->flags
) &&
2221 mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
2222 pr_warn("md: %s: array is limited to %d devices\n",
2223 mdname(mddev
), mddev
->max_disks
);
2226 bdevname(rdev
->bdev
,b
);
2227 strreplace(b
, '/', '!');
2229 rdev
->mddev
= mddev
;
2230 pr_debug("md: bind<%s>\n", b
);
2232 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
2235 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
2236 if (sysfs_create_link(&rdev
->kobj
, ko
, "block"))
2237 /* failure here is OK */;
2238 rdev
->sysfs_state
= sysfs_get_dirent_safe(rdev
->kobj
.sd
, "state");
2240 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
2241 bd_link_disk_holder(rdev
->bdev
, mddev
->gendisk
);
2243 /* May as well allow recovery to be retried once */
2244 mddev
->recovery_disabled
++;
2249 pr_warn("md: failed to register dev-%s for %s\n",
2254 static void md_delayed_delete(struct work_struct
*ws
)
2256 struct md_rdev
*rdev
= container_of(ws
, struct md_rdev
, del_work
);
2257 kobject_del(&rdev
->kobj
);
2258 kobject_put(&rdev
->kobj
);
2261 static void unbind_rdev_from_array(struct md_rdev
*rdev
)
2263 char b
[BDEVNAME_SIZE
];
2265 bd_unlink_disk_holder(rdev
->bdev
, rdev
->mddev
->gendisk
);
2266 list_del_rcu(&rdev
->same_set
);
2267 pr_debug("md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
2269 sysfs_remove_link(&rdev
->kobj
, "block");
2270 sysfs_put(rdev
->sysfs_state
);
2271 rdev
->sysfs_state
= NULL
;
2272 rdev
->badblocks
.count
= 0;
2273 /* We need to delay this, otherwise we can deadlock when
2274 * writing to 'remove' to "dev/state". We also need
2275 * to delay it due to rcu usage.
2278 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
2279 kobject_get(&rdev
->kobj
);
2280 queue_work(md_misc_wq
, &rdev
->del_work
);
2284 * prevent the device from being mounted, repartitioned or
2285 * otherwise reused by a RAID array (or any other kernel
2286 * subsystem), by bd_claiming the device.
2288 static int lock_rdev(struct md_rdev
*rdev
, dev_t dev
, int shared
)
2291 struct block_device
*bdev
;
2292 char b
[BDEVNAME_SIZE
];
2294 bdev
= blkdev_get_by_dev(dev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
,
2295 shared
? (struct md_rdev
*)lock_rdev
: rdev
);
2297 pr_warn("md: could not open %s.\n", __bdevname(dev
, b
));
2298 return PTR_ERR(bdev
);
2304 static void unlock_rdev(struct md_rdev
*rdev
)
2306 struct block_device
*bdev
= rdev
->bdev
;
2308 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
2311 void md_autodetect_dev(dev_t dev
);
2313 static void export_rdev(struct md_rdev
*rdev
)
2315 char b
[BDEVNAME_SIZE
];
2317 pr_debug("md: export_rdev(%s)\n", bdevname(rdev
->bdev
,b
));
2318 md_rdev_clear(rdev
);
2320 if (test_bit(AutoDetected
, &rdev
->flags
))
2321 md_autodetect_dev(rdev
->bdev
->bd_dev
);
2324 kobject_put(&rdev
->kobj
);
2327 void md_kick_rdev_from_array(struct md_rdev
*rdev
)
2329 unbind_rdev_from_array(rdev
);
2332 EXPORT_SYMBOL_GPL(md_kick_rdev_from_array
);
2334 static void export_array(struct mddev
*mddev
)
2336 struct md_rdev
*rdev
;
2338 while (!list_empty(&mddev
->disks
)) {
2339 rdev
= list_first_entry(&mddev
->disks
, struct md_rdev
,
2341 md_kick_rdev_from_array(rdev
);
2343 mddev
->raid_disks
= 0;
2344 mddev
->major_version
= 0;
2347 static bool set_in_sync(struct mddev
*mddev
)
2349 lockdep_assert_held(&mddev
->lock
);
2350 if (!mddev
->in_sync
) {
2351 mddev
->sync_checkers
++;
2352 spin_unlock(&mddev
->lock
);
2353 percpu_ref_switch_to_atomic_sync(&mddev
->writes_pending
);
2354 spin_lock(&mddev
->lock
);
2355 if (!mddev
->in_sync
&&
2356 percpu_ref_is_zero(&mddev
->writes_pending
)) {
2359 * Ensure ->in_sync is visible before we clear
2363 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
2364 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
2366 if (--mddev
->sync_checkers
== 0)
2367 percpu_ref_switch_to_percpu(&mddev
->writes_pending
);
2369 if (mddev
->safemode
== 1)
2370 mddev
->safemode
= 0;
2371 return mddev
->in_sync
;
2374 static void sync_sbs(struct mddev
*mddev
, int nospares
)
2376 /* Update each superblock (in-memory image), but
2377 * if we are allowed to, skip spares which already
2378 * have the right event counter, or have one earlier
2379 * (which would mean they aren't being marked as dirty
2380 * with the rest of the array)
2382 struct md_rdev
*rdev
;
2383 rdev_for_each(rdev
, mddev
) {
2384 if (rdev
->sb_events
== mddev
->events
||
2386 rdev
->raid_disk
< 0 &&
2387 rdev
->sb_events
+1 == mddev
->events
)) {
2388 /* Don't update this superblock */
2389 rdev
->sb_loaded
= 2;
2391 sync_super(mddev
, rdev
);
2392 rdev
->sb_loaded
= 1;
2397 static bool does_sb_need_changing(struct mddev
*mddev
)
2399 struct md_rdev
*rdev
;
2400 struct mdp_superblock_1
*sb
;
2403 /* Find a good rdev */
2404 rdev_for_each(rdev
, mddev
)
2405 if ((rdev
->raid_disk
>= 0) && !test_bit(Faulty
, &rdev
->flags
))
2408 /* No good device found. */
2412 sb
= page_address(rdev
->sb_page
);
2413 /* Check if a device has become faulty or a spare become active */
2414 rdev_for_each(rdev
, mddev
) {
2415 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
2416 /* Device activated? */
2417 if (role
== 0xffff && rdev
->raid_disk
>=0 &&
2418 !test_bit(Faulty
, &rdev
->flags
))
2420 /* Device turned faulty? */
2421 if (test_bit(Faulty
, &rdev
->flags
) && (role
< 0xfffd))
2425 /* Check if any mddev parameters have changed */
2426 if ((mddev
->dev_sectors
!= le64_to_cpu(sb
->size
)) ||
2427 (mddev
->reshape_position
!= le64_to_cpu(sb
->reshape_position
)) ||
2428 (mddev
->layout
!= le32_to_cpu(sb
->layout
)) ||
2429 (mddev
->raid_disks
!= le32_to_cpu(sb
->raid_disks
)) ||
2430 (mddev
->chunk_sectors
!= le32_to_cpu(sb
->chunksize
)))
2436 void md_update_sb(struct mddev
*mddev
, int force_change
)
2438 struct md_rdev
*rdev
;
2441 int any_badblocks_changed
= 0;
2446 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2451 if (mddev_is_clustered(mddev
)) {
2452 if (test_and_clear_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
))
2454 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
))
2456 ret
= md_cluster_ops
->metadata_update_start(mddev
);
2457 /* Has someone else has updated the sb */
2458 if (!does_sb_need_changing(mddev
)) {
2460 md_cluster_ops
->metadata_update_cancel(mddev
);
2461 bit_clear_unless(&mddev
->sb_flags
, BIT(MD_SB_CHANGE_PENDING
),
2462 BIT(MD_SB_CHANGE_DEVS
) |
2463 BIT(MD_SB_CHANGE_CLEAN
));
2469 * First make sure individual recovery_offsets are correct
2470 * curr_resync_completed can only be used during recovery.
2471 * During reshape/resync it might use array-addresses rather
2472 * that device addresses.
2474 rdev_for_each(rdev
, mddev
) {
2475 if (rdev
->raid_disk
>= 0 &&
2476 mddev
->delta_disks
>= 0 &&
2477 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
2478 test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
) &&
2479 !test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
2480 !test_bit(Journal
, &rdev
->flags
) &&
2481 !test_bit(In_sync
, &rdev
->flags
) &&
2482 mddev
->curr_resync_completed
> rdev
->recovery_offset
)
2483 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
2486 if (!mddev
->persistent
) {
2487 clear_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
2488 clear_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2489 if (!mddev
->external
) {
2490 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
2491 rdev_for_each(rdev
, mddev
) {
2492 if (rdev
->badblocks
.changed
) {
2493 rdev
->badblocks
.changed
= 0;
2494 ack_all_badblocks(&rdev
->badblocks
);
2495 md_error(mddev
, rdev
);
2497 clear_bit(Blocked
, &rdev
->flags
);
2498 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2499 wake_up(&rdev
->blocked_wait
);
2502 wake_up(&mddev
->sb_wait
);
2506 spin_lock(&mddev
->lock
);
2508 mddev
->utime
= ktime_get_real_seconds();
2510 if (test_and_clear_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
))
2512 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
))
2513 /* just a clean<-> dirty transition, possibly leave spares alone,
2514 * though if events isn't the right even/odd, we will have to do
2520 if (mddev
->degraded
)
2521 /* If the array is degraded, then skipping spares is both
2522 * dangerous and fairly pointless.
2523 * Dangerous because a device that was removed from the array
2524 * might have a event_count that still looks up-to-date,
2525 * so it can be re-added without a resync.
2526 * Pointless because if there are any spares to skip,
2527 * then a recovery will happen and soon that array won't
2528 * be degraded any more and the spare can go back to sleep then.
2532 sync_req
= mddev
->in_sync
;
2534 /* If this is just a dirty<->clean transition, and the array is clean
2535 * and 'events' is odd, we can roll back to the previous clean state */
2537 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
2538 && mddev
->can_decrease_events
2539 && mddev
->events
!= 1) {
2541 mddev
->can_decrease_events
= 0;
2543 /* otherwise we have to go forward and ... */
2545 mddev
->can_decrease_events
= nospares
;
2549 * This 64-bit counter should never wrap.
2550 * Either we are in around ~1 trillion A.C., assuming
2551 * 1 reboot per second, or we have a bug...
2553 WARN_ON(mddev
->events
== 0);
2555 rdev_for_each(rdev
, mddev
) {
2556 if (rdev
->badblocks
.changed
)
2557 any_badblocks_changed
++;
2558 if (test_bit(Faulty
, &rdev
->flags
))
2559 set_bit(FaultRecorded
, &rdev
->flags
);
2562 sync_sbs(mddev
, nospares
);
2563 spin_unlock(&mddev
->lock
);
2565 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2566 mdname(mddev
), mddev
->in_sync
);
2569 blk_add_trace_msg(mddev
->queue
, "md md_update_sb");
2571 md_bitmap_update_sb(mddev
->bitmap
);
2572 rdev_for_each(rdev
, mddev
) {
2573 char b
[BDEVNAME_SIZE
];
2575 if (rdev
->sb_loaded
!= 1)
2576 continue; /* no noise on spare devices */
2578 if (!test_bit(Faulty
, &rdev
->flags
)) {
2579 md_super_write(mddev
,rdev
,
2580 rdev
->sb_start
, rdev
->sb_size
,
2582 pr_debug("md: (write) %s's sb offset: %llu\n",
2583 bdevname(rdev
->bdev
, b
),
2584 (unsigned long long)rdev
->sb_start
);
2585 rdev
->sb_events
= mddev
->events
;
2586 if (rdev
->badblocks
.size
) {
2587 md_super_write(mddev
, rdev
,
2588 rdev
->badblocks
.sector
,
2589 rdev
->badblocks
.size
<< 9,
2591 rdev
->badblocks
.size
= 0;
2595 pr_debug("md: %s (skipping faulty)\n",
2596 bdevname(rdev
->bdev
, b
));
2598 if (mddev
->level
== LEVEL_MULTIPATH
)
2599 /* only need to write one superblock... */
2602 if (md_super_wait(mddev
) < 0)
2604 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
2606 if (mddev_is_clustered(mddev
) && ret
== 0)
2607 md_cluster_ops
->metadata_update_finish(mddev
);
2609 if (mddev
->in_sync
!= sync_req
||
2610 !bit_clear_unless(&mddev
->sb_flags
, BIT(MD_SB_CHANGE_PENDING
),
2611 BIT(MD_SB_CHANGE_DEVS
) | BIT(MD_SB_CHANGE_CLEAN
)))
2612 /* have to write it out again */
2614 wake_up(&mddev
->sb_wait
);
2615 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2616 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
2618 rdev_for_each(rdev
, mddev
) {
2619 if (test_and_clear_bit(FaultRecorded
, &rdev
->flags
))
2620 clear_bit(Blocked
, &rdev
->flags
);
2622 if (any_badblocks_changed
)
2623 ack_all_badblocks(&rdev
->badblocks
);
2624 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2625 wake_up(&rdev
->blocked_wait
);
2628 EXPORT_SYMBOL(md_update_sb
);
2630 static int add_bound_rdev(struct md_rdev
*rdev
)
2632 struct mddev
*mddev
= rdev
->mddev
;
2634 bool add_journal
= test_bit(Journal
, &rdev
->flags
);
2636 if (!mddev
->pers
->hot_remove_disk
|| add_journal
) {
2637 /* If there is hot_add_disk but no hot_remove_disk
2638 * then added disks for geometry changes,
2639 * and should be added immediately.
2641 super_types
[mddev
->major_version
].
2642 validate_super(mddev
, rdev
);
2644 mddev_suspend(mddev
);
2645 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
2647 mddev_resume(mddev
);
2649 md_kick_rdev_from_array(rdev
);
2653 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2655 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2656 if (mddev
->degraded
)
2657 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
2658 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2659 md_new_event(mddev
);
2660 md_wakeup_thread(mddev
->thread
);
2664 /* words written to sysfs files may, or may not, be \n terminated.
2665 * We want to accept with case. For this we use cmd_match.
2667 static int cmd_match(const char *cmd
, const char *str
)
2669 /* See if cmd, written into a sysfs file, matches
2670 * str. They must either be the same, or cmd can
2671 * have a trailing newline
2673 while (*cmd
&& *str
&& *cmd
== *str
) {
2684 struct rdev_sysfs_entry
{
2685 struct attribute attr
;
2686 ssize_t (*show
)(struct md_rdev
*, char *);
2687 ssize_t (*store
)(struct md_rdev
*, const char *, size_t);
2691 state_show(struct md_rdev
*rdev
, char *page
)
2695 unsigned long flags
= READ_ONCE(rdev
->flags
);
2697 if (test_bit(Faulty
, &flags
) ||
2698 (!test_bit(ExternalBbl
, &flags
) &&
2699 rdev
->badblocks
.unacked_exist
))
2700 len
+= sprintf(page
+len
, "faulty%s", sep
);
2701 if (test_bit(In_sync
, &flags
))
2702 len
+= sprintf(page
+len
, "in_sync%s", sep
);
2703 if (test_bit(Journal
, &flags
))
2704 len
+= sprintf(page
+len
, "journal%s", sep
);
2705 if (test_bit(WriteMostly
, &flags
))
2706 len
+= sprintf(page
+len
, "write_mostly%s", sep
);
2707 if (test_bit(Blocked
, &flags
) ||
2708 (rdev
->badblocks
.unacked_exist
2709 && !test_bit(Faulty
, &flags
)))
2710 len
+= sprintf(page
+len
, "blocked%s", sep
);
2711 if (!test_bit(Faulty
, &flags
) &&
2712 !test_bit(Journal
, &flags
) &&
2713 !test_bit(In_sync
, &flags
))
2714 len
+= sprintf(page
+len
, "spare%s", sep
);
2715 if (test_bit(WriteErrorSeen
, &flags
))
2716 len
+= sprintf(page
+len
, "write_error%s", sep
);
2717 if (test_bit(WantReplacement
, &flags
))
2718 len
+= sprintf(page
+len
, "want_replacement%s", sep
);
2719 if (test_bit(Replacement
, &flags
))
2720 len
+= sprintf(page
+len
, "replacement%s", sep
);
2721 if (test_bit(ExternalBbl
, &flags
))
2722 len
+= sprintf(page
+len
, "external_bbl%s", sep
);
2723 if (test_bit(FailFast
, &flags
))
2724 len
+= sprintf(page
+len
, "failfast%s", sep
);
2729 return len
+sprintf(page
+len
, "\n");
2733 state_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2736 * faulty - simulates an error
2737 * remove - disconnects the device
2738 * writemostly - sets write_mostly
2739 * -writemostly - clears write_mostly
2740 * blocked - sets the Blocked flags
2741 * -blocked - clears the Blocked and possibly simulates an error
2742 * insync - sets Insync providing device isn't active
2743 * -insync - clear Insync for a device with a slot assigned,
2744 * so that it gets rebuilt based on bitmap
2745 * write_error - sets WriteErrorSeen
2746 * -write_error - clears WriteErrorSeen
2747 * {,-}failfast - set/clear FailFast
2750 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
2751 md_error(rdev
->mddev
, rdev
);
2752 if (test_bit(Faulty
, &rdev
->flags
))
2756 } else if (cmd_match(buf
, "remove")) {
2757 if (rdev
->mddev
->pers
) {
2758 clear_bit(Blocked
, &rdev
->flags
);
2759 remove_and_add_spares(rdev
->mddev
, rdev
);
2761 if (rdev
->raid_disk
>= 0)
2764 struct mddev
*mddev
= rdev
->mddev
;
2766 if (mddev_is_clustered(mddev
))
2767 err
= md_cluster_ops
->remove_disk(mddev
, rdev
);
2770 md_kick_rdev_from_array(rdev
);
2772 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2773 md_wakeup_thread(mddev
->thread
);
2775 md_new_event(mddev
);
2778 } else if (cmd_match(buf
, "writemostly")) {
2779 set_bit(WriteMostly
, &rdev
->flags
);
2781 } else if (cmd_match(buf
, "-writemostly")) {
2782 clear_bit(WriteMostly
, &rdev
->flags
);
2784 } else if (cmd_match(buf
, "blocked")) {
2785 set_bit(Blocked
, &rdev
->flags
);
2787 } else if (cmd_match(buf
, "-blocked")) {
2788 if (!test_bit(Faulty
, &rdev
->flags
) &&
2789 !test_bit(ExternalBbl
, &rdev
->flags
) &&
2790 rdev
->badblocks
.unacked_exist
) {
2791 /* metadata handler doesn't understand badblocks,
2792 * so we need to fail the device
2794 md_error(rdev
->mddev
, rdev
);
2796 clear_bit(Blocked
, &rdev
->flags
);
2797 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2798 wake_up(&rdev
->blocked_wait
);
2799 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2800 md_wakeup_thread(rdev
->mddev
->thread
);
2803 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
2804 set_bit(In_sync
, &rdev
->flags
);
2806 } else if (cmd_match(buf
, "failfast")) {
2807 set_bit(FailFast
, &rdev
->flags
);
2809 } else if (cmd_match(buf
, "-failfast")) {
2810 clear_bit(FailFast
, &rdev
->flags
);
2812 } else if (cmd_match(buf
, "-insync") && rdev
->raid_disk
>= 0 &&
2813 !test_bit(Journal
, &rdev
->flags
)) {
2814 if (rdev
->mddev
->pers
== NULL
) {
2815 clear_bit(In_sync
, &rdev
->flags
);
2816 rdev
->saved_raid_disk
= rdev
->raid_disk
;
2817 rdev
->raid_disk
= -1;
2820 } else if (cmd_match(buf
, "write_error")) {
2821 set_bit(WriteErrorSeen
, &rdev
->flags
);
2823 } else if (cmd_match(buf
, "-write_error")) {
2824 clear_bit(WriteErrorSeen
, &rdev
->flags
);
2826 } else if (cmd_match(buf
, "want_replacement")) {
2827 /* Any non-spare device that is not a replacement can
2828 * become want_replacement at any time, but we then need to
2829 * check if recovery is needed.
2831 if (rdev
->raid_disk
>= 0 &&
2832 !test_bit(Journal
, &rdev
->flags
) &&
2833 !test_bit(Replacement
, &rdev
->flags
))
2834 set_bit(WantReplacement
, &rdev
->flags
);
2835 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2836 md_wakeup_thread(rdev
->mddev
->thread
);
2838 } else if (cmd_match(buf
, "-want_replacement")) {
2839 /* Clearing 'want_replacement' is always allowed.
2840 * Once replacements starts it is too late though.
2843 clear_bit(WantReplacement
, &rdev
->flags
);
2844 } else if (cmd_match(buf
, "replacement")) {
2845 /* Can only set a device as a replacement when array has not
2846 * yet been started. Once running, replacement is automatic
2847 * from spares, or by assigning 'slot'.
2849 if (rdev
->mddev
->pers
)
2852 set_bit(Replacement
, &rdev
->flags
);
2855 } else if (cmd_match(buf
, "-replacement")) {
2856 /* Similarly, can only clear Replacement before start */
2857 if (rdev
->mddev
->pers
)
2860 clear_bit(Replacement
, &rdev
->flags
);
2863 } else if (cmd_match(buf
, "re-add")) {
2864 if (test_bit(Faulty
, &rdev
->flags
) && (rdev
->raid_disk
== -1) &&
2865 rdev
->saved_raid_disk
>= 0) {
2866 /* clear_bit is performed _after_ all the devices
2867 * have their local Faulty bit cleared. If any writes
2868 * happen in the meantime in the local node, they
2869 * will land in the local bitmap, which will be synced
2870 * by this node eventually
2872 if (!mddev_is_clustered(rdev
->mddev
) ||
2873 (err
= md_cluster_ops
->gather_bitmaps(rdev
)) == 0) {
2874 clear_bit(Faulty
, &rdev
->flags
);
2875 err
= add_bound_rdev(rdev
);
2879 } else if (cmd_match(buf
, "external_bbl") && (rdev
->mddev
->external
)) {
2880 set_bit(ExternalBbl
, &rdev
->flags
);
2881 rdev
->badblocks
.shift
= 0;
2883 } else if (cmd_match(buf
, "-external_bbl") && (rdev
->mddev
->external
)) {
2884 clear_bit(ExternalBbl
, &rdev
->flags
);
2888 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2889 return err
? err
: len
;
2891 static struct rdev_sysfs_entry rdev_state
=
2892 __ATTR_PREALLOC(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
2895 errors_show(struct md_rdev
*rdev
, char *page
)
2897 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
2901 errors_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2906 rv
= kstrtouint(buf
, 10, &n
);
2909 atomic_set(&rdev
->corrected_errors
, n
);
2912 static struct rdev_sysfs_entry rdev_errors
=
2913 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
2916 slot_show(struct md_rdev
*rdev
, char *page
)
2918 if (test_bit(Journal
, &rdev
->flags
))
2919 return sprintf(page
, "journal\n");
2920 else if (rdev
->raid_disk
< 0)
2921 return sprintf(page
, "none\n");
2923 return sprintf(page
, "%d\n", rdev
->raid_disk
);
2927 slot_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2932 if (test_bit(Journal
, &rdev
->flags
))
2934 if (strncmp(buf
, "none", 4)==0)
2937 err
= kstrtouint(buf
, 10, (unsigned int *)&slot
);
2941 if (rdev
->mddev
->pers
&& slot
== -1) {
2942 /* Setting 'slot' on an active array requires also
2943 * updating the 'rd%d' link, and communicating
2944 * with the personality with ->hot_*_disk.
2945 * For now we only support removing
2946 * failed/spare devices. This normally happens automatically,
2947 * but not when the metadata is externally managed.
2949 if (rdev
->raid_disk
== -1)
2951 /* personality does all needed checks */
2952 if (rdev
->mddev
->pers
->hot_remove_disk
== NULL
)
2954 clear_bit(Blocked
, &rdev
->flags
);
2955 remove_and_add_spares(rdev
->mddev
, rdev
);
2956 if (rdev
->raid_disk
>= 0)
2958 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2959 md_wakeup_thread(rdev
->mddev
->thread
);
2960 } else if (rdev
->mddev
->pers
) {
2961 /* Activating a spare .. or possibly reactivating
2962 * if we ever get bitmaps working here.
2966 if (rdev
->raid_disk
!= -1)
2969 if (test_bit(MD_RECOVERY_RUNNING
, &rdev
->mddev
->recovery
))
2972 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2975 if (slot
>= rdev
->mddev
->raid_disks
&&
2976 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2979 rdev
->raid_disk
= slot
;
2980 if (test_bit(In_sync
, &rdev
->flags
))
2981 rdev
->saved_raid_disk
= slot
;
2983 rdev
->saved_raid_disk
= -1;
2984 clear_bit(In_sync
, &rdev
->flags
);
2985 clear_bit(Bitmap_sync
, &rdev
->flags
);
2986 err
= rdev
->mddev
->pers
->
2987 hot_add_disk(rdev
->mddev
, rdev
);
2989 rdev
->raid_disk
= -1;
2992 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2993 if (sysfs_link_rdev(rdev
->mddev
, rdev
))
2994 /* failure here is OK */;
2995 /* don't wakeup anyone, leave that to userspace. */
2997 if (slot
>= rdev
->mddev
->raid_disks
&&
2998 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
3000 rdev
->raid_disk
= slot
;
3001 /* assume it is working */
3002 clear_bit(Faulty
, &rdev
->flags
);
3003 clear_bit(WriteMostly
, &rdev
->flags
);
3004 set_bit(In_sync
, &rdev
->flags
);
3005 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
3010 static struct rdev_sysfs_entry rdev_slot
=
3011 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
3014 offset_show(struct md_rdev
*rdev
, char *page
)
3016 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
3020 offset_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3022 unsigned long long offset
;
3023 if (kstrtoull(buf
, 10, &offset
) < 0)
3025 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
3027 if (rdev
->sectors
&& rdev
->mddev
->external
)
3028 /* Must set offset before size, so overlap checks
3031 rdev
->data_offset
= offset
;
3032 rdev
->new_data_offset
= offset
;
3036 static struct rdev_sysfs_entry rdev_offset
=
3037 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
3039 static ssize_t
new_offset_show(struct md_rdev
*rdev
, char *page
)
3041 return sprintf(page
, "%llu\n",
3042 (unsigned long long)rdev
->new_data_offset
);
3045 static ssize_t
new_offset_store(struct md_rdev
*rdev
,
3046 const char *buf
, size_t len
)
3048 unsigned long long new_offset
;
3049 struct mddev
*mddev
= rdev
->mddev
;
3051 if (kstrtoull(buf
, 10, &new_offset
) < 0)
3054 if (mddev
->sync_thread
||
3055 test_bit(MD_RECOVERY_RUNNING
,&mddev
->recovery
))
3057 if (new_offset
== rdev
->data_offset
)
3058 /* reset is always permitted */
3060 else if (new_offset
> rdev
->data_offset
) {
3061 /* must not push array size beyond rdev_sectors */
3062 if (new_offset
- rdev
->data_offset
3063 + mddev
->dev_sectors
> rdev
->sectors
)
3066 /* Metadata worries about other space details. */
3068 /* decreasing the offset is inconsistent with a backwards
3071 if (new_offset
< rdev
->data_offset
&&
3072 mddev
->reshape_backwards
)
3074 /* Increasing offset is inconsistent with forwards
3075 * reshape. reshape_direction should be set to
3076 * 'backwards' first.
3078 if (new_offset
> rdev
->data_offset
&&
3079 !mddev
->reshape_backwards
)
3082 if (mddev
->pers
&& mddev
->persistent
&&
3083 !super_types
[mddev
->major_version
]
3084 .allow_new_offset(rdev
, new_offset
))
3086 rdev
->new_data_offset
= new_offset
;
3087 if (new_offset
> rdev
->data_offset
)
3088 mddev
->reshape_backwards
= 1;
3089 else if (new_offset
< rdev
->data_offset
)
3090 mddev
->reshape_backwards
= 0;
3094 static struct rdev_sysfs_entry rdev_new_offset
=
3095 __ATTR(new_offset
, S_IRUGO
|S_IWUSR
, new_offset_show
, new_offset_store
);
3098 rdev_size_show(struct md_rdev
*rdev
, char *page
)
3100 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
3103 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
3105 /* check if two start/length pairs overlap */
3113 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
3115 unsigned long long blocks
;
3118 if (kstrtoull(buf
, 10, &blocks
) < 0)
3121 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
3122 return -EINVAL
; /* sector conversion overflow */
3125 if (new != blocks
* 2)
3126 return -EINVAL
; /* unsigned long long to sector_t overflow */
3133 rdev_size_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3135 struct mddev
*my_mddev
= rdev
->mddev
;
3136 sector_t oldsectors
= rdev
->sectors
;
3139 if (test_bit(Journal
, &rdev
->flags
))
3141 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
3143 if (rdev
->data_offset
!= rdev
->new_data_offset
)
3144 return -EINVAL
; /* too confusing */
3145 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
3146 if (my_mddev
->persistent
) {
3147 sectors
= super_types
[my_mddev
->major_version
].
3148 rdev_size_change(rdev
, sectors
);
3151 } else if (!sectors
)
3152 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
3154 if (!my_mddev
->pers
->resize
)
3155 /* Cannot change size for RAID0 or Linear etc */
3158 if (sectors
< my_mddev
->dev_sectors
)
3159 return -EINVAL
; /* component must fit device */
3161 rdev
->sectors
= sectors
;
3162 if (sectors
> oldsectors
&& my_mddev
->external
) {
3163 /* Need to check that all other rdevs with the same
3164 * ->bdev do not overlap. 'rcu' is sufficient to walk
3165 * the rdev lists safely.
3166 * This check does not provide a hard guarantee, it
3167 * just helps avoid dangerous mistakes.
3169 struct mddev
*mddev
;
3171 struct list_head
*tmp
;
3174 for_each_mddev(mddev
, tmp
) {
3175 struct md_rdev
*rdev2
;
3177 rdev_for_each(rdev2
, mddev
)
3178 if (rdev
->bdev
== rdev2
->bdev
&&
3180 overlaps(rdev
->data_offset
, rdev
->sectors
,
3193 /* Someone else could have slipped in a size
3194 * change here, but doing so is just silly.
3195 * We put oldsectors back because we *know* it is
3196 * safe, and trust userspace not to race with
3199 rdev
->sectors
= oldsectors
;
3206 static struct rdev_sysfs_entry rdev_size
=
3207 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
3209 static ssize_t
recovery_start_show(struct md_rdev
*rdev
, char *page
)
3211 unsigned long long recovery_start
= rdev
->recovery_offset
;
3213 if (test_bit(In_sync
, &rdev
->flags
) ||
3214 recovery_start
== MaxSector
)
3215 return sprintf(page
, "none\n");
3217 return sprintf(page
, "%llu\n", recovery_start
);
3220 static ssize_t
recovery_start_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3222 unsigned long long recovery_start
;
3224 if (cmd_match(buf
, "none"))
3225 recovery_start
= MaxSector
;
3226 else if (kstrtoull(buf
, 10, &recovery_start
))
3229 if (rdev
->mddev
->pers
&&
3230 rdev
->raid_disk
>= 0)
3233 rdev
->recovery_offset
= recovery_start
;
3234 if (recovery_start
== MaxSector
)
3235 set_bit(In_sync
, &rdev
->flags
);
3237 clear_bit(In_sync
, &rdev
->flags
);
3241 static struct rdev_sysfs_entry rdev_recovery_start
=
3242 __ATTR(recovery_start
, S_IRUGO
|S_IWUSR
, recovery_start_show
, recovery_start_store
);
3244 /* sysfs access to bad-blocks list.
3245 * We present two files.
3246 * 'bad-blocks' lists sector numbers and lengths of ranges that
3247 * are recorded as bad. The list is truncated to fit within
3248 * the one-page limit of sysfs.
3249 * Writing "sector length" to this file adds an acknowledged
3251 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3252 * been acknowledged. Writing to this file adds bad blocks
3253 * without acknowledging them. This is largely for testing.
3255 static ssize_t
bb_show(struct md_rdev
*rdev
, char *page
)
3257 return badblocks_show(&rdev
->badblocks
, page
, 0);
3259 static ssize_t
bb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
3261 int rv
= badblocks_store(&rdev
->badblocks
, page
, len
, 0);
3262 /* Maybe that ack was all we needed */
3263 if (test_and_clear_bit(BlockedBadBlocks
, &rdev
->flags
))
3264 wake_up(&rdev
->blocked_wait
);
3267 static struct rdev_sysfs_entry rdev_bad_blocks
=
3268 __ATTR(bad_blocks
, S_IRUGO
|S_IWUSR
, bb_show
, bb_store
);
3270 static ssize_t
ubb_show(struct md_rdev
*rdev
, char *page
)
3272 return badblocks_show(&rdev
->badblocks
, page
, 1);
3274 static ssize_t
ubb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
3276 return badblocks_store(&rdev
->badblocks
, page
, len
, 1);
3278 static struct rdev_sysfs_entry rdev_unack_bad_blocks
=
3279 __ATTR(unacknowledged_bad_blocks
, S_IRUGO
|S_IWUSR
, ubb_show
, ubb_store
);
3282 ppl_sector_show(struct md_rdev
*rdev
, char *page
)
3284 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->ppl
.sector
);
3288 ppl_sector_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3290 unsigned long long sector
;
3292 if (kstrtoull(buf
, 10, §or
) < 0)
3294 if (sector
!= (sector_t
)sector
)
3297 if (rdev
->mddev
->pers
&& test_bit(MD_HAS_PPL
, &rdev
->mddev
->flags
) &&
3298 rdev
->raid_disk
>= 0)
3301 if (rdev
->mddev
->persistent
) {
3302 if (rdev
->mddev
->major_version
== 0)
3304 if ((sector
> rdev
->sb_start
&&
3305 sector
- rdev
->sb_start
> S16_MAX
) ||
3306 (sector
< rdev
->sb_start
&&
3307 rdev
->sb_start
- sector
> -S16_MIN
))
3309 rdev
->ppl
.offset
= sector
- rdev
->sb_start
;
3310 } else if (!rdev
->mddev
->external
) {
3313 rdev
->ppl
.sector
= sector
;
3317 static struct rdev_sysfs_entry rdev_ppl_sector
=
3318 __ATTR(ppl_sector
, S_IRUGO
|S_IWUSR
, ppl_sector_show
, ppl_sector_store
);
3321 ppl_size_show(struct md_rdev
*rdev
, char *page
)
3323 return sprintf(page
, "%u\n", rdev
->ppl
.size
);
3327 ppl_size_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3331 if (kstrtouint(buf
, 10, &size
) < 0)
3334 if (rdev
->mddev
->pers
&& test_bit(MD_HAS_PPL
, &rdev
->mddev
->flags
) &&
3335 rdev
->raid_disk
>= 0)
3338 if (rdev
->mddev
->persistent
) {
3339 if (rdev
->mddev
->major_version
== 0)
3343 } else if (!rdev
->mddev
->external
) {
3346 rdev
->ppl
.size
= size
;
3350 static struct rdev_sysfs_entry rdev_ppl_size
=
3351 __ATTR(ppl_size
, S_IRUGO
|S_IWUSR
, ppl_size_show
, ppl_size_store
);
3353 static struct attribute
*rdev_default_attrs
[] = {
3358 &rdev_new_offset
.attr
,
3360 &rdev_recovery_start
.attr
,
3361 &rdev_bad_blocks
.attr
,
3362 &rdev_unack_bad_blocks
.attr
,
3363 &rdev_ppl_sector
.attr
,
3364 &rdev_ppl_size
.attr
,
3368 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
3370 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3371 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3377 return entry
->show(rdev
, page
);
3381 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
3382 const char *page
, size_t length
)
3384 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3385 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3387 struct mddev
*mddev
= rdev
->mddev
;
3391 if (!capable(CAP_SYS_ADMIN
))
3393 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
3395 if (rdev
->mddev
== NULL
)
3398 rv
= entry
->store(rdev
, page
, length
);
3399 mddev_unlock(mddev
);
3404 static void rdev_free(struct kobject
*ko
)
3406 struct md_rdev
*rdev
= container_of(ko
, struct md_rdev
, kobj
);
3409 static const struct sysfs_ops rdev_sysfs_ops
= {
3410 .show
= rdev_attr_show
,
3411 .store
= rdev_attr_store
,
3413 static struct kobj_type rdev_ktype
= {
3414 .release
= rdev_free
,
3415 .sysfs_ops
= &rdev_sysfs_ops
,
3416 .default_attrs
= rdev_default_attrs
,
3419 int md_rdev_init(struct md_rdev
*rdev
)
3422 rdev
->saved_raid_disk
= -1;
3423 rdev
->raid_disk
= -1;
3425 rdev
->data_offset
= 0;
3426 rdev
->new_data_offset
= 0;
3427 rdev
->sb_events
= 0;
3428 rdev
->last_read_error
= 0;
3429 rdev
->sb_loaded
= 0;
3430 rdev
->bb_page
= NULL
;
3431 atomic_set(&rdev
->nr_pending
, 0);
3432 atomic_set(&rdev
->read_errors
, 0);
3433 atomic_set(&rdev
->corrected_errors
, 0);
3435 INIT_LIST_HEAD(&rdev
->same_set
);
3436 init_waitqueue_head(&rdev
->blocked_wait
);
3438 /* Add space to store bad block list.
3439 * This reserves the space even on arrays where it cannot
3440 * be used - I wonder if that matters
3442 return badblocks_init(&rdev
->badblocks
, 0);
3444 EXPORT_SYMBOL_GPL(md_rdev_init
);
3446 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3448 * mark the device faulty if:
3450 * - the device is nonexistent (zero size)
3451 * - the device has no valid superblock
3453 * a faulty rdev _never_ has rdev->sb set.
3455 static struct md_rdev
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
3457 char b
[BDEVNAME_SIZE
];
3459 struct md_rdev
*rdev
;
3462 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
3464 return ERR_PTR(-ENOMEM
);
3466 err
= md_rdev_init(rdev
);
3469 err
= alloc_disk_sb(rdev
);
3473 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
3477 kobject_init(&rdev
->kobj
, &rdev_ktype
);
3479 size
= i_size_read(rdev
->bdev
->bd_inode
) >> BLOCK_SIZE_BITS
;
3481 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
3482 bdevname(rdev
->bdev
,b
));
3487 if (super_format
>= 0) {
3488 err
= super_types
[super_format
].
3489 load_super(rdev
, NULL
, super_minor
);
3490 if (err
== -EINVAL
) {
3491 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
3492 bdevname(rdev
->bdev
,b
),
3493 super_format
, super_minor
);
3497 pr_warn("md: could not read %s's sb, not importing!\n",
3498 bdevname(rdev
->bdev
,b
));
3508 md_rdev_clear(rdev
);
3510 return ERR_PTR(err
);
3514 * Check a full RAID array for plausibility
3517 static void analyze_sbs(struct mddev
*mddev
)
3520 struct md_rdev
*rdev
, *freshest
, *tmp
;
3521 char b
[BDEVNAME_SIZE
];
3524 rdev_for_each_safe(rdev
, tmp
, mddev
)
3525 switch (super_types
[mddev
->major_version
].
3526 load_super(rdev
, freshest
, mddev
->minor_version
)) {
3533 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
3534 bdevname(rdev
->bdev
,b
));
3535 md_kick_rdev_from_array(rdev
);
3538 super_types
[mddev
->major_version
].
3539 validate_super(mddev
, freshest
);
3542 rdev_for_each_safe(rdev
, tmp
, mddev
) {
3543 if (mddev
->max_disks
&&
3544 (rdev
->desc_nr
>= mddev
->max_disks
||
3545 i
> mddev
->max_disks
)) {
3546 pr_warn("md: %s: %s: only %d devices permitted\n",
3547 mdname(mddev
), bdevname(rdev
->bdev
, b
),
3549 md_kick_rdev_from_array(rdev
);
3552 if (rdev
!= freshest
) {
3553 if (super_types
[mddev
->major_version
].
3554 validate_super(mddev
, rdev
)) {
3555 pr_warn("md: kicking non-fresh %s from array!\n",
3556 bdevname(rdev
->bdev
,b
));
3557 md_kick_rdev_from_array(rdev
);
3561 if (mddev
->level
== LEVEL_MULTIPATH
) {
3562 rdev
->desc_nr
= i
++;
3563 rdev
->raid_disk
= rdev
->desc_nr
;
3564 set_bit(In_sync
, &rdev
->flags
);
3565 } else if (rdev
->raid_disk
>=
3566 (mddev
->raid_disks
- min(0, mddev
->delta_disks
)) &&
3567 !test_bit(Journal
, &rdev
->flags
)) {
3568 rdev
->raid_disk
= -1;
3569 clear_bit(In_sync
, &rdev
->flags
);
3574 /* Read a fixed-point number.
3575 * Numbers in sysfs attributes should be in "standard" units where
3576 * possible, so time should be in seconds.
3577 * However we internally use a a much smaller unit such as
3578 * milliseconds or jiffies.
3579 * This function takes a decimal number with a possible fractional
3580 * component, and produces an integer which is the result of
3581 * multiplying that number by 10^'scale'.
3582 * all without any floating-point arithmetic.
3584 int strict_strtoul_scaled(const char *cp
, unsigned long *res
, int scale
)
3586 unsigned long result
= 0;
3588 while (isdigit(*cp
) || (*cp
== '.' && decimals
< 0)) {
3591 else if (decimals
< scale
) {
3594 result
= result
* 10 + value
;
3606 while (decimals
< scale
) {
3615 safe_delay_show(struct mddev
*mddev
, char *page
)
3617 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
3618 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
3621 safe_delay_store(struct mddev
*mddev
, const char *cbuf
, size_t len
)
3625 if (mddev_is_clustered(mddev
)) {
3626 pr_warn("md: Safemode is disabled for clustered mode\n");
3630 if (strict_strtoul_scaled(cbuf
, &msec
, 3) < 0)
3633 mddev
->safemode_delay
= 0;
3635 unsigned long old_delay
= mddev
->safemode_delay
;
3636 unsigned long new_delay
= (msec
*HZ
)/1000;
3640 mddev
->safemode_delay
= new_delay
;
3641 if (new_delay
< old_delay
|| old_delay
== 0)
3642 mod_timer(&mddev
->safemode_timer
, jiffies
+1);
3646 static struct md_sysfs_entry md_safe_delay
=
3647 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
3650 level_show(struct mddev
*mddev
, char *page
)
3652 struct md_personality
*p
;
3654 spin_lock(&mddev
->lock
);
3657 ret
= sprintf(page
, "%s\n", p
->name
);
3658 else if (mddev
->clevel
[0])
3659 ret
= sprintf(page
, "%s\n", mddev
->clevel
);
3660 else if (mddev
->level
!= LEVEL_NONE
)
3661 ret
= sprintf(page
, "%d\n", mddev
->level
);
3664 spin_unlock(&mddev
->lock
);
3669 level_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3674 struct md_personality
*pers
, *oldpers
;
3676 void *priv
, *oldpriv
;
3677 struct md_rdev
*rdev
;
3679 if (slen
== 0 || slen
>= sizeof(clevel
))
3682 rv
= mddev_lock(mddev
);
3686 if (mddev
->pers
== NULL
) {
3687 strncpy(mddev
->clevel
, buf
, slen
);
3688 if (mddev
->clevel
[slen
-1] == '\n')
3690 mddev
->clevel
[slen
] = 0;
3691 mddev
->level
= LEVEL_NONE
;
3699 /* request to change the personality. Need to ensure:
3700 * - array is not engaged in resync/recovery/reshape
3701 * - old personality can be suspended
3702 * - new personality will access other array.
3706 if (mddev
->sync_thread
||
3707 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3708 mddev
->reshape_position
!= MaxSector
||
3709 mddev
->sysfs_active
)
3713 if (!mddev
->pers
->quiesce
) {
3714 pr_warn("md: %s: %s does not support online personality change\n",
3715 mdname(mddev
), mddev
->pers
->name
);
3719 /* Now find the new personality */
3720 strncpy(clevel
, buf
, slen
);
3721 if (clevel
[slen
-1] == '\n')
3724 if (kstrtol(clevel
, 10, &level
))
3727 if (request_module("md-%s", clevel
) != 0)
3728 request_module("md-level-%s", clevel
);
3729 spin_lock(&pers_lock
);
3730 pers
= find_pers(level
, clevel
);
3731 if (!pers
|| !try_module_get(pers
->owner
)) {
3732 spin_unlock(&pers_lock
);
3733 pr_warn("md: personality %s not loaded\n", clevel
);
3737 spin_unlock(&pers_lock
);
3739 if (pers
== mddev
->pers
) {
3740 /* Nothing to do! */
3741 module_put(pers
->owner
);
3745 if (!pers
->takeover
) {
3746 module_put(pers
->owner
);
3747 pr_warn("md: %s: %s does not support personality takeover\n",
3748 mdname(mddev
), clevel
);
3753 rdev_for_each(rdev
, mddev
)
3754 rdev
->new_raid_disk
= rdev
->raid_disk
;
3756 /* ->takeover must set new_* and/or delta_disks
3757 * if it succeeds, and may set them when it fails.
3759 priv
= pers
->takeover(mddev
);
3761 mddev
->new_level
= mddev
->level
;
3762 mddev
->new_layout
= mddev
->layout
;
3763 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3764 mddev
->raid_disks
-= mddev
->delta_disks
;
3765 mddev
->delta_disks
= 0;
3766 mddev
->reshape_backwards
= 0;
3767 module_put(pers
->owner
);
3768 pr_warn("md: %s: %s would not accept array\n",
3769 mdname(mddev
), clevel
);
3774 /* Looks like we have a winner */
3775 mddev_suspend(mddev
);
3776 mddev_detach(mddev
);
3778 spin_lock(&mddev
->lock
);
3779 oldpers
= mddev
->pers
;
3780 oldpriv
= mddev
->private;
3782 mddev
->private = priv
;
3783 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3784 mddev
->level
= mddev
->new_level
;
3785 mddev
->layout
= mddev
->new_layout
;
3786 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
3787 mddev
->delta_disks
= 0;
3788 mddev
->reshape_backwards
= 0;
3789 mddev
->degraded
= 0;
3790 spin_unlock(&mddev
->lock
);
3792 if (oldpers
->sync_request
== NULL
&&
3794 /* We are converting from a no-redundancy array
3795 * to a redundancy array and metadata is managed
3796 * externally so we need to be sure that writes
3797 * won't block due to a need to transition
3799 * until external management is started.
3802 mddev
->safemode_delay
= 0;
3803 mddev
->safemode
= 0;
3806 oldpers
->free(mddev
, oldpriv
);
3808 if (oldpers
->sync_request
== NULL
&&
3809 pers
->sync_request
!= NULL
) {
3810 /* need to add the md_redundancy_group */
3811 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
3812 pr_warn("md: cannot register extra attributes for %s\n",
3814 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, "sync_action");
3816 if (oldpers
->sync_request
!= NULL
&&
3817 pers
->sync_request
== NULL
) {
3818 /* need to remove the md_redundancy_group */
3819 if (mddev
->to_remove
== NULL
)
3820 mddev
->to_remove
= &md_redundancy_group
;
3823 module_put(oldpers
->owner
);
3825 rdev_for_each(rdev
, mddev
) {
3826 if (rdev
->raid_disk
< 0)
3828 if (rdev
->new_raid_disk
>= mddev
->raid_disks
)
3829 rdev
->new_raid_disk
= -1;
3830 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3832 sysfs_unlink_rdev(mddev
, rdev
);
3834 rdev_for_each(rdev
, mddev
) {
3835 if (rdev
->raid_disk
< 0)
3837 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3839 rdev
->raid_disk
= rdev
->new_raid_disk
;
3840 if (rdev
->raid_disk
< 0)
3841 clear_bit(In_sync
, &rdev
->flags
);
3843 if (sysfs_link_rdev(mddev
, rdev
))
3844 pr_warn("md: cannot register rd%d for %s after level change\n",
3845 rdev
->raid_disk
, mdname(mddev
));
3849 if (pers
->sync_request
== NULL
) {
3850 /* this is now an array without redundancy, so
3851 * it must always be in_sync
3854 del_timer_sync(&mddev
->safemode_timer
);
3856 blk_set_stacking_limits(&mddev
->queue
->limits
);
3858 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
3859 mddev_resume(mddev
);
3861 md_update_sb(mddev
, 1);
3862 sysfs_notify(&mddev
->kobj
, NULL
, "level");
3863 md_new_event(mddev
);
3866 mddev_unlock(mddev
);
3870 static struct md_sysfs_entry md_level
=
3871 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
3874 layout_show(struct mddev
*mddev
, char *page
)
3876 /* just a number, not meaningful for all levels */
3877 if (mddev
->reshape_position
!= MaxSector
&&
3878 mddev
->layout
!= mddev
->new_layout
)
3879 return sprintf(page
, "%d (%d)\n",
3880 mddev
->new_layout
, mddev
->layout
);
3881 return sprintf(page
, "%d\n", mddev
->layout
);
3885 layout_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3890 err
= kstrtouint(buf
, 10, &n
);
3893 err
= mddev_lock(mddev
);
3898 if (mddev
->pers
->check_reshape
== NULL
)
3903 mddev
->new_layout
= n
;
3904 err
= mddev
->pers
->check_reshape(mddev
);
3906 mddev
->new_layout
= mddev
->layout
;
3909 mddev
->new_layout
= n
;
3910 if (mddev
->reshape_position
== MaxSector
)
3913 mddev_unlock(mddev
);
3916 static struct md_sysfs_entry md_layout
=
3917 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
3920 raid_disks_show(struct mddev
*mddev
, char *page
)
3922 if (mddev
->raid_disks
== 0)
3924 if (mddev
->reshape_position
!= MaxSector
&&
3925 mddev
->delta_disks
!= 0)
3926 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
3927 mddev
->raid_disks
- mddev
->delta_disks
);
3928 return sprintf(page
, "%d\n", mddev
->raid_disks
);
3931 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
);
3934 raid_disks_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3939 err
= kstrtouint(buf
, 10, &n
);
3943 err
= mddev_lock(mddev
);
3947 err
= update_raid_disks(mddev
, n
);
3948 else if (mddev
->reshape_position
!= MaxSector
) {
3949 struct md_rdev
*rdev
;
3950 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
3953 rdev_for_each(rdev
, mddev
) {
3955 rdev
->data_offset
< rdev
->new_data_offset
)
3958 rdev
->data_offset
> rdev
->new_data_offset
)
3962 mddev
->delta_disks
= n
- olddisks
;
3963 mddev
->raid_disks
= n
;
3964 mddev
->reshape_backwards
= (mddev
->delta_disks
< 0);
3966 mddev
->raid_disks
= n
;
3968 mddev_unlock(mddev
);
3969 return err
? err
: len
;
3971 static struct md_sysfs_entry md_raid_disks
=
3972 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
3975 chunk_size_show(struct mddev
*mddev
, char *page
)
3977 if (mddev
->reshape_position
!= MaxSector
&&
3978 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
3979 return sprintf(page
, "%d (%d)\n",
3980 mddev
->new_chunk_sectors
<< 9,
3981 mddev
->chunk_sectors
<< 9);
3982 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
3986 chunk_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3991 err
= kstrtoul(buf
, 10, &n
);
3995 err
= mddev_lock(mddev
);
3999 if (mddev
->pers
->check_reshape
== NULL
)
4004 mddev
->new_chunk_sectors
= n
>> 9;
4005 err
= mddev
->pers
->check_reshape(mddev
);
4007 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4010 mddev
->new_chunk_sectors
= n
>> 9;
4011 if (mddev
->reshape_position
== MaxSector
)
4012 mddev
->chunk_sectors
= n
>> 9;
4014 mddev_unlock(mddev
);
4017 static struct md_sysfs_entry md_chunk_size
=
4018 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
4021 resync_start_show(struct mddev
*mddev
, char *page
)
4023 if (mddev
->recovery_cp
== MaxSector
)
4024 return sprintf(page
, "none\n");
4025 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
4029 resync_start_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4031 unsigned long long n
;
4034 if (cmd_match(buf
, "none"))
4037 err
= kstrtoull(buf
, 10, &n
);
4040 if (n
!= (sector_t
)n
)
4044 err
= mddev_lock(mddev
);
4047 if (mddev
->pers
&& !test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
4051 mddev
->recovery_cp
= n
;
4053 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
4055 mddev_unlock(mddev
);
4058 static struct md_sysfs_entry md_resync_start
=
4059 __ATTR_PREALLOC(resync_start
, S_IRUGO
|S_IWUSR
,
4060 resync_start_show
, resync_start_store
);
4063 * The array state can be:
4066 * No devices, no size, no level
4067 * Equivalent to STOP_ARRAY ioctl
4069 * May have some settings, but array is not active
4070 * all IO results in error
4071 * When written, doesn't tear down array, but just stops it
4072 * suspended (not supported yet)
4073 * All IO requests will block. The array can be reconfigured.
4074 * Writing this, if accepted, will block until array is quiescent
4076 * no resync can happen. no superblocks get written.
4077 * write requests fail
4079 * like readonly, but behaves like 'clean' on a write request.
4081 * clean - no pending writes, but otherwise active.
4082 * When written to inactive array, starts without resync
4083 * If a write request arrives then
4084 * if metadata is known, mark 'dirty' and switch to 'active'.
4085 * if not known, block and switch to write-pending
4086 * If written to an active array that has pending writes, then fails.
4088 * fully active: IO and resync can be happening.
4089 * When written to inactive array, starts with resync
4092 * clean, but writes are blocked waiting for 'active' to be written.
4095 * like active, but no writes have been seen for a while (100msec).
4098 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
4099 write_pending
, active_idle
, bad_word
};
4100 static char *array_states
[] = {
4101 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
4102 "write-pending", "active-idle", NULL
};
4104 static int match_word(const char *word
, char **list
)
4107 for (n
=0; list
[n
]; n
++)
4108 if (cmd_match(word
, list
[n
]))
4114 array_state_show(struct mddev
*mddev
, char *page
)
4116 enum array_state st
= inactive
;
4127 spin_lock(&mddev
->lock
);
4128 if (test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
))
4130 else if (mddev
->in_sync
)
4132 else if (mddev
->safemode
)
4136 spin_unlock(&mddev
->lock
);
4139 if (list_empty(&mddev
->disks
) &&
4140 mddev
->raid_disks
== 0 &&
4141 mddev
->dev_sectors
== 0)
4146 return sprintf(page
, "%s\n", array_states
[st
]);
4149 static int do_md_stop(struct mddev
*mddev
, int ro
, struct block_device
*bdev
);
4150 static int md_set_readonly(struct mddev
*mddev
, struct block_device
*bdev
);
4151 static int do_md_run(struct mddev
*mddev
);
4152 static int restart_array(struct mddev
*mddev
);
4155 array_state_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4158 enum array_state st
= match_word(buf
, array_states
);
4160 if (mddev
->pers
&& (st
== active
|| st
== clean
) && mddev
->ro
!= 1) {
4161 /* don't take reconfig_mutex when toggling between
4164 spin_lock(&mddev
->lock
);
4166 restart_array(mddev
);
4167 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
4168 md_wakeup_thread(mddev
->thread
);
4169 wake_up(&mddev
->sb_wait
);
4170 } else /* st == clean */ {
4171 restart_array(mddev
);
4172 if (!set_in_sync(mddev
))
4176 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4177 spin_unlock(&mddev
->lock
);
4180 err
= mddev_lock(mddev
);
4188 /* stopping an active array */
4189 err
= do_md_stop(mddev
, 0, NULL
);
4192 /* stopping an active array */
4194 err
= do_md_stop(mddev
, 2, NULL
);
4196 err
= 0; /* already inactive */
4199 break; /* not supported yet */
4202 err
= md_set_readonly(mddev
, NULL
);
4205 set_disk_ro(mddev
->gendisk
, 1);
4206 err
= do_md_run(mddev
);
4212 err
= md_set_readonly(mddev
, NULL
);
4213 else if (mddev
->ro
== 1)
4214 err
= restart_array(mddev
);
4217 set_disk_ro(mddev
->gendisk
, 0);
4221 err
= do_md_run(mddev
);
4226 err
= restart_array(mddev
);
4229 spin_lock(&mddev
->lock
);
4230 if (!set_in_sync(mddev
))
4232 spin_unlock(&mddev
->lock
);
4238 err
= restart_array(mddev
);
4241 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
4242 wake_up(&mddev
->sb_wait
);
4246 set_disk_ro(mddev
->gendisk
, 0);
4247 err
= do_md_run(mddev
);
4252 /* these cannot be set */
4257 if (mddev
->hold_active
== UNTIL_IOCTL
)
4258 mddev
->hold_active
= 0;
4259 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4261 mddev_unlock(mddev
);
4264 static struct md_sysfs_entry md_array_state
=
4265 __ATTR_PREALLOC(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
4268 max_corrected_read_errors_show(struct mddev
*mddev
, char *page
) {
4269 return sprintf(page
, "%d\n",
4270 atomic_read(&mddev
->max_corr_read_errors
));
4274 max_corrected_read_errors_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4279 rv
= kstrtouint(buf
, 10, &n
);
4282 atomic_set(&mddev
->max_corr_read_errors
, n
);
4286 static struct md_sysfs_entry max_corr_read_errors
=
4287 __ATTR(max_read_errors
, S_IRUGO
|S_IWUSR
, max_corrected_read_errors_show
,
4288 max_corrected_read_errors_store
);
4291 null_show(struct mddev
*mddev
, char *page
)
4297 new_dev_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4299 /* buf must be %d:%d\n? giving major and minor numbers */
4300 /* The new device is added to the array.
4301 * If the array has a persistent superblock, we read the
4302 * superblock to initialise info and check validity.
4303 * Otherwise, only checking done is that in bind_rdev_to_array,
4304 * which mainly checks size.
4307 int major
= simple_strtoul(buf
, &e
, 10);
4310 struct md_rdev
*rdev
;
4313 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
4315 minor
= simple_strtoul(e
+1, &e
, 10);
4316 if (*e
&& *e
!= '\n')
4318 dev
= MKDEV(major
, minor
);
4319 if (major
!= MAJOR(dev
) ||
4320 minor
!= MINOR(dev
))
4323 flush_workqueue(md_misc_wq
);
4325 err
= mddev_lock(mddev
);
4328 if (mddev
->persistent
) {
4329 rdev
= md_import_device(dev
, mddev
->major_version
,
4330 mddev
->minor_version
);
4331 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
4332 struct md_rdev
*rdev0
4333 = list_entry(mddev
->disks
.next
,
4334 struct md_rdev
, same_set
);
4335 err
= super_types
[mddev
->major_version
]
4336 .load_super(rdev
, rdev0
, mddev
->minor_version
);
4340 } else if (mddev
->external
)
4341 rdev
= md_import_device(dev
, -2, -1);
4343 rdev
= md_import_device(dev
, -1, -1);
4346 mddev_unlock(mddev
);
4347 return PTR_ERR(rdev
);
4349 err
= bind_rdev_to_array(rdev
, mddev
);
4353 mddev_unlock(mddev
);
4355 md_new_event(mddev
);
4356 return err
? err
: len
;
4359 static struct md_sysfs_entry md_new_device
=
4360 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
4363 bitmap_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4366 unsigned long chunk
, end_chunk
;
4369 err
= mddev_lock(mddev
);
4374 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4376 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
4377 if (buf
== end
) break;
4378 if (*end
== '-') { /* range */
4380 end_chunk
= simple_strtoul(buf
, &end
, 0);
4381 if (buf
== end
) break;
4383 if (*end
&& !isspace(*end
)) break;
4384 md_bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
4385 buf
= skip_spaces(end
);
4387 md_bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
4389 mddev_unlock(mddev
);
4393 static struct md_sysfs_entry md_bitmap
=
4394 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
4397 size_show(struct mddev
*mddev
, char *page
)
4399 return sprintf(page
, "%llu\n",
4400 (unsigned long long)mddev
->dev_sectors
/ 2);
4403 static int update_size(struct mddev
*mddev
, sector_t num_sectors
);
4406 size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4408 /* If array is inactive, we can reduce the component size, but
4409 * not increase it (except from 0).
4410 * If array is active, we can try an on-line resize
4413 int err
= strict_blocks_to_sectors(buf
, §ors
);
4417 err
= mddev_lock(mddev
);
4421 err
= update_size(mddev
, sectors
);
4423 md_update_sb(mddev
, 1);
4425 if (mddev
->dev_sectors
== 0 ||
4426 mddev
->dev_sectors
> sectors
)
4427 mddev
->dev_sectors
= sectors
;
4431 mddev_unlock(mddev
);
4432 return err
? err
: len
;
4435 static struct md_sysfs_entry md_size
=
4436 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
4438 /* Metadata version.
4440 * 'none' for arrays with no metadata (good luck...)
4441 * 'external' for arrays with externally managed metadata,
4442 * or N.M for internally known formats
4445 metadata_show(struct mddev
*mddev
, char *page
)
4447 if (mddev
->persistent
)
4448 return sprintf(page
, "%d.%d\n",
4449 mddev
->major_version
, mddev
->minor_version
);
4450 else if (mddev
->external
)
4451 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
4453 return sprintf(page
, "none\n");
4457 metadata_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4462 /* Changing the details of 'external' metadata is
4463 * always permitted. Otherwise there must be
4464 * no devices attached to the array.
4467 err
= mddev_lock(mddev
);
4471 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
4473 else if (!list_empty(&mddev
->disks
))
4477 if (cmd_match(buf
, "none")) {
4478 mddev
->persistent
= 0;
4479 mddev
->external
= 0;
4480 mddev
->major_version
= 0;
4481 mddev
->minor_version
= 90;
4484 if (strncmp(buf
, "external:", 9) == 0) {
4485 size_t namelen
= len
-9;
4486 if (namelen
>= sizeof(mddev
->metadata_type
))
4487 namelen
= sizeof(mddev
->metadata_type
)-1;
4488 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
4489 mddev
->metadata_type
[namelen
] = 0;
4490 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
4491 mddev
->metadata_type
[--namelen
] = 0;
4492 mddev
->persistent
= 0;
4493 mddev
->external
= 1;
4494 mddev
->major_version
= 0;
4495 mddev
->minor_version
= 90;
4498 major
= simple_strtoul(buf
, &e
, 10);
4500 if (e
==buf
|| *e
!= '.')
4503 minor
= simple_strtoul(buf
, &e
, 10);
4504 if (e
==buf
|| (*e
&& *e
!= '\n') )
4507 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
4509 mddev
->major_version
= major
;
4510 mddev
->minor_version
= minor
;
4511 mddev
->persistent
= 1;
4512 mddev
->external
= 0;
4515 mddev_unlock(mddev
);
4519 static struct md_sysfs_entry md_metadata
=
4520 __ATTR_PREALLOC(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
4523 action_show(struct mddev
*mddev
, char *page
)
4525 char *type
= "idle";
4526 unsigned long recovery
= mddev
->recovery
;
4527 if (test_bit(MD_RECOVERY_FROZEN
, &recovery
))
4529 else if (test_bit(MD_RECOVERY_RUNNING
, &recovery
) ||
4530 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &recovery
))) {
4531 if (test_bit(MD_RECOVERY_RESHAPE
, &recovery
))
4533 else if (test_bit(MD_RECOVERY_SYNC
, &recovery
)) {
4534 if (!test_bit(MD_RECOVERY_REQUESTED
, &recovery
))
4536 else if (test_bit(MD_RECOVERY_CHECK
, &recovery
))
4540 } else if (test_bit(MD_RECOVERY_RECOVER
, &recovery
))
4542 else if (mddev
->reshape_position
!= MaxSector
)
4545 return sprintf(page
, "%s\n", type
);
4549 action_store(struct mddev
*mddev
, const char *page
, size_t len
)
4551 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
4555 if (cmd_match(page
, "idle") || cmd_match(page
, "frozen")) {
4556 if (cmd_match(page
, "frozen"))
4557 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4559 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4560 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
4561 mddev_lock(mddev
) == 0) {
4562 flush_workqueue(md_misc_wq
);
4563 if (mddev
->sync_thread
) {
4564 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4565 md_reap_sync_thread(mddev
);
4567 mddev_unlock(mddev
);
4569 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4571 else if (cmd_match(page
, "resync"))
4572 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4573 else if (cmd_match(page
, "recover")) {
4574 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4575 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
4576 } else if (cmd_match(page
, "reshape")) {
4578 if (mddev
->pers
->start_reshape
== NULL
)
4580 err
= mddev_lock(mddev
);
4582 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4585 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4586 err
= mddev
->pers
->start_reshape(mddev
);
4588 mddev_unlock(mddev
);
4592 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4594 if (cmd_match(page
, "check"))
4595 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
4596 else if (!cmd_match(page
, "repair"))
4598 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4599 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
4600 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
4602 if (mddev
->ro
== 2) {
4603 /* A write to sync_action is enough to justify
4604 * canceling read-auto mode
4607 md_wakeup_thread(mddev
->sync_thread
);
4609 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4610 md_wakeup_thread(mddev
->thread
);
4611 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4615 static struct md_sysfs_entry md_scan_mode
=
4616 __ATTR_PREALLOC(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
4619 last_sync_action_show(struct mddev
*mddev
, char *page
)
4621 return sprintf(page
, "%s\n", mddev
->last_sync_action
);
4624 static struct md_sysfs_entry md_last_scan_mode
= __ATTR_RO(last_sync_action
);
4627 mismatch_cnt_show(struct mddev
*mddev
, char *page
)
4629 return sprintf(page
, "%llu\n",
4630 (unsigned long long)
4631 atomic64_read(&mddev
->resync_mismatches
));
4634 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
4637 sync_min_show(struct mddev
*mddev
, char *page
)
4639 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
4640 mddev
->sync_speed_min
? "local": "system");
4644 sync_min_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4649 if (strncmp(buf
, "system", 6)==0) {
4652 rv
= kstrtouint(buf
, 10, &min
);
4658 mddev
->sync_speed_min
= min
;
4662 static struct md_sysfs_entry md_sync_min
=
4663 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
4666 sync_max_show(struct mddev
*mddev
, char *page
)
4668 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
4669 mddev
->sync_speed_max
? "local": "system");
4673 sync_max_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4678 if (strncmp(buf
, "system", 6)==0) {
4681 rv
= kstrtouint(buf
, 10, &max
);
4687 mddev
->sync_speed_max
= max
;
4691 static struct md_sysfs_entry md_sync_max
=
4692 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
4695 degraded_show(struct mddev
*mddev
, char *page
)
4697 return sprintf(page
, "%d\n", mddev
->degraded
);
4699 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
4702 sync_force_parallel_show(struct mddev
*mddev
, char *page
)
4704 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
4708 sync_force_parallel_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4712 if (kstrtol(buf
, 10, &n
))
4715 if (n
!= 0 && n
!= 1)
4718 mddev
->parallel_resync
= n
;
4720 if (mddev
->sync_thread
)
4721 wake_up(&resync_wait
);
4726 /* force parallel resync, even with shared block devices */
4727 static struct md_sysfs_entry md_sync_force_parallel
=
4728 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
4729 sync_force_parallel_show
, sync_force_parallel_store
);
4732 sync_speed_show(struct mddev
*mddev
, char *page
)
4734 unsigned long resync
, dt
, db
;
4735 if (mddev
->curr_resync
== 0)
4736 return sprintf(page
, "none\n");
4737 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
4738 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
4740 db
= resync
- mddev
->resync_mark_cnt
;
4741 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
4744 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
4747 sync_completed_show(struct mddev
*mddev
, char *page
)
4749 unsigned long long max_sectors
, resync
;
4751 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4752 return sprintf(page
, "none\n");
4754 if (mddev
->curr_resync
== 1 ||
4755 mddev
->curr_resync
== 2)
4756 return sprintf(page
, "delayed\n");
4758 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
4759 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
4760 max_sectors
= mddev
->resync_max_sectors
;
4762 max_sectors
= mddev
->dev_sectors
;
4764 resync
= mddev
->curr_resync_completed
;
4765 return sprintf(page
, "%llu / %llu\n", resync
, max_sectors
);
4768 static struct md_sysfs_entry md_sync_completed
=
4769 __ATTR_PREALLOC(sync_completed
, S_IRUGO
, sync_completed_show
, NULL
);
4772 min_sync_show(struct mddev
*mddev
, char *page
)
4774 return sprintf(page
, "%llu\n",
4775 (unsigned long long)mddev
->resync_min
);
4778 min_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4780 unsigned long long min
;
4783 if (kstrtoull(buf
, 10, &min
))
4786 spin_lock(&mddev
->lock
);
4788 if (min
> mddev
->resync_max
)
4792 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4795 /* Round down to multiple of 4K for safety */
4796 mddev
->resync_min
= round_down(min
, 8);
4800 spin_unlock(&mddev
->lock
);
4804 static struct md_sysfs_entry md_min_sync
=
4805 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
4808 max_sync_show(struct mddev
*mddev
, char *page
)
4810 if (mddev
->resync_max
== MaxSector
)
4811 return sprintf(page
, "max\n");
4813 return sprintf(page
, "%llu\n",
4814 (unsigned long long)mddev
->resync_max
);
4817 max_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4820 spin_lock(&mddev
->lock
);
4821 if (strncmp(buf
, "max", 3) == 0)
4822 mddev
->resync_max
= MaxSector
;
4824 unsigned long long max
;
4828 if (kstrtoull(buf
, 10, &max
))
4830 if (max
< mddev
->resync_min
)
4834 if (max
< mddev
->resync_max
&&
4836 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4839 /* Must be a multiple of chunk_size */
4840 chunk
= mddev
->chunk_sectors
;
4842 sector_t temp
= max
;
4845 if (sector_div(temp
, chunk
))
4848 mddev
->resync_max
= max
;
4850 wake_up(&mddev
->recovery_wait
);
4853 spin_unlock(&mddev
->lock
);
4857 static struct md_sysfs_entry md_max_sync
=
4858 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
4861 suspend_lo_show(struct mddev
*mddev
, char *page
)
4863 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
4867 suspend_lo_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4869 unsigned long long new;
4872 err
= kstrtoull(buf
, 10, &new);
4875 if (new != (sector_t
)new)
4878 err
= mddev_lock(mddev
);
4882 if (mddev
->pers
== NULL
||
4883 mddev
->pers
->quiesce
== NULL
)
4885 mddev_suspend(mddev
);
4886 mddev
->suspend_lo
= new;
4887 mddev_resume(mddev
);
4891 mddev_unlock(mddev
);
4894 static struct md_sysfs_entry md_suspend_lo
=
4895 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
4898 suspend_hi_show(struct mddev
*mddev
, char *page
)
4900 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
4904 suspend_hi_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4906 unsigned long long new;
4909 err
= kstrtoull(buf
, 10, &new);
4912 if (new != (sector_t
)new)
4915 err
= mddev_lock(mddev
);
4919 if (mddev
->pers
== NULL
)
4922 mddev_suspend(mddev
);
4923 mddev
->suspend_hi
= new;
4924 mddev_resume(mddev
);
4928 mddev_unlock(mddev
);
4931 static struct md_sysfs_entry md_suspend_hi
=
4932 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
4935 reshape_position_show(struct mddev
*mddev
, char *page
)
4937 if (mddev
->reshape_position
!= MaxSector
)
4938 return sprintf(page
, "%llu\n",
4939 (unsigned long long)mddev
->reshape_position
);
4940 strcpy(page
, "none\n");
4945 reshape_position_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4947 struct md_rdev
*rdev
;
4948 unsigned long long new;
4951 err
= kstrtoull(buf
, 10, &new);
4954 if (new != (sector_t
)new)
4956 err
= mddev_lock(mddev
);
4962 mddev
->reshape_position
= new;
4963 mddev
->delta_disks
= 0;
4964 mddev
->reshape_backwards
= 0;
4965 mddev
->new_level
= mddev
->level
;
4966 mddev
->new_layout
= mddev
->layout
;
4967 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4968 rdev_for_each(rdev
, mddev
)
4969 rdev
->new_data_offset
= rdev
->data_offset
;
4972 mddev_unlock(mddev
);
4976 static struct md_sysfs_entry md_reshape_position
=
4977 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
4978 reshape_position_store
);
4981 reshape_direction_show(struct mddev
*mddev
, char *page
)
4983 return sprintf(page
, "%s\n",
4984 mddev
->reshape_backwards
? "backwards" : "forwards");
4988 reshape_direction_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4993 if (cmd_match(buf
, "forwards"))
4995 else if (cmd_match(buf
, "backwards"))
4999 if (mddev
->reshape_backwards
== backwards
)
5002 err
= mddev_lock(mddev
);
5005 /* check if we are allowed to change */
5006 if (mddev
->delta_disks
)
5008 else if (mddev
->persistent
&&
5009 mddev
->major_version
== 0)
5012 mddev
->reshape_backwards
= backwards
;
5013 mddev_unlock(mddev
);
5017 static struct md_sysfs_entry md_reshape_direction
=
5018 __ATTR(reshape_direction
, S_IRUGO
|S_IWUSR
, reshape_direction_show
,
5019 reshape_direction_store
);
5022 array_size_show(struct mddev
*mddev
, char *page
)
5024 if (mddev
->external_size
)
5025 return sprintf(page
, "%llu\n",
5026 (unsigned long long)mddev
->array_sectors
/2);
5028 return sprintf(page
, "default\n");
5032 array_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5037 err
= mddev_lock(mddev
);
5041 /* cluster raid doesn't support change array_sectors */
5042 if (mddev_is_clustered(mddev
)) {
5043 mddev_unlock(mddev
);
5047 if (strncmp(buf
, "default", 7) == 0) {
5049 sectors
= mddev
->pers
->size(mddev
, 0, 0);
5051 sectors
= mddev
->array_sectors
;
5053 mddev
->external_size
= 0;
5055 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
5057 else if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
5060 mddev
->external_size
= 1;
5064 mddev
->array_sectors
= sectors
;
5066 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
5067 revalidate_disk(mddev
->gendisk
);
5070 mddev_unlock(mddev
);
5074 static struct md_sysfs_entry md_array_size
=
5075 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
5079 consistency_policy_show(struct mddev
*mddev
, char *page
)
5083 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
)) {
5084 ret
= sprintf(page
, "journal\n");
5085 } else if (test_bit(MD_HAS_PPL
, &mddev
->flags
)) {
5086 ret
= sprintf(page
, "ppl\n");
5087 } else if (mddev
->bitmap
) {
5088 ret
= sprintf(page
, "bitmap\n");
5089 } else if (mddev
->pers
) {
5090 if (mddev
->pers
->sync_request
)
5091 ret
= sprintf(page
, "resync\n");
5093 ret
= sprintf(page
, "none\n");
5095 ret
= sprintf(page
, "unknown\n");
5102 consistency_policy_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5107 if (mddev
->pers
->change_consistency_policy
)
5108 err
= mddev
->pers
->change_consistency_policy(mddev
, buf
);
5111 } else if (mddev
->external
&& strncmp(buf
, "ppl", 3) == 0) {
5112 set_bit(MD_HAS_PPL
, &mddev
->flags
);
5117 return err
? err
: len
;
5120 static struct md_sysfs_entry md_consistency_policy
=
5121 __ATTR(consistency_policy
, S_IRUGO
| S_IWUSR
, consistency_policy_show
,
5122 consistency_policy_store
);
5124 static struct attribute
*md_default_attrs
[] = {
5127 &md_raid_disks
.attr
,
5128 &md_chunk_size
.attr
,
5130 &md_resync_start
.attr
,
5132 &md_new_device
.attr
,
5133 &md_safe_delay
.attr
,
5134 &md_array_state
.attr
,
5135 &md_reshape_position
.attr
,
5136 &md_reshape_direction
.attr
,
5137 &md_array_size
.attr
,
5138 &max_corr_read_errors
.attr
,
5139 &md_consistency_policy
.attr
,
5143 static struct attribute
*md_redundancy_attrs
[] = {
5145 &md_last_scan_mode
.attr
,
5146 &md_mismatches
.attr
,
5149 &md_sync_speed
.attr
,
5150 &md_sync_force_parallel
.attr
,
5151 &md_sync_completed
.attr
,
5154 &md_suspend_lo
.attr
,
5155 &md_suspend_hi
.attr
,
5160 static struct attribute_group md_redundancy_group
= {
5162 .attrs
= md_redundancy_attrs
,
5166 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
5168 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
5169 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
5174 spin_lock(&all_mddevs_lock
);
5175 if (list_empty(&mddev
->all_mddevs
)) {
5176 spin_unlock(&all_mddevs_lock
);
5180 spin_unlock(&all_mddevs_lock
);
5182 rv
= entry
->show(mddev
, page
);
5188 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
5189 const char *page
, size_t length
)
5191 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
5192 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
5197 if (!capable(CAP_SYS_ADMIN
))
5199 spin_lock(&all_mddevs_lock
);
5200 if (list_empty(&mddev
->all_mddevs
)) {
5201 spin_unlock(&all_mddevs_lock
);
5205 spin_unlock(&all_mddevs_lock
);
5206 rv
= entry
->store(mddev
, page
, length
);
5211 static void md_free(struct kobject
*ko
)
5213 struct mddev
*mddev
= container_of(ko
, struct mddev
, kobj
);
5215 if (mddev
->sysfs_state
)
5216 sysfs_put(mddev
->sysfs_state
);
5219 del_gendisk(mddev
->gendisk
);
5221 blk_cleanup_queue(mddev
->queue
);
5223 put_disk(mddev
->gendisk
);
5224 percpu_ref_exit(&mddev
->writes_pending
);
5226 bioset_exit(&mddev
->bio_set
);
5227 bioset_exit(&mddev
->sync_set
);
5231 static const struct sysfs_ops md_sysfs_ops
= {
5232 .show
= md_attr_show
,
5233 .store
= md_attr_store
,
5235 static struct kobj_type md_ktype
= {
5237 .sysfs_ops
= &md_sysfs_ops
,
5238 .default_attrs
= md_default_attrs
,
5243 static void mddev_delayed_delete(struct work_struct
*ws
)
5245 struct mddev
*mddev
= container_of(ws
, struct mddev
, del_work
);
5247 sysfs_remove_group(&mddev
->kobj
, &md_bitmap_group
);
5248 kobject_del(&mddev
->kobj
);
5249 kobject_put(&mddev
->kobj
);
5252 static void no_op(struct percpu_ref
*r
) {}
5254 int mddev_init_writes_pending(struct mddev
*mddev
)
5256 if (mddev
->writes_pending
.percpu_count_ptr
)
5258 if (percpu_ref_init(&mddev
->writes_pending
, no_op
, 0, GFP_KERNEL
) < 0)
5260 /* We want to start with the refcount at zero */
5261 percpu_ref_put(&mddev
->writes_pending
);
5264 EXPORT_SYMBOL_GPL(mddev_init_writes_pending
);
5266 static int md_alloc(dev_t dev
, char *name
)
5269 * If dev is zero, name is the name of a device to allocate with
5270 * an arbitrary minor number. It will be "md_???"
5271 * If dev is non-zero it must be a device number with a MAJOR of
5272 * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then
5273 * the device is being created by opening a node in /dev.
5274 * If "name" is not NULL, the device is being created by
5275 * writing to /sys/module/md_mod/parameters/new_array.
5277 static DEFINE_MUTEX(disks_mutex
);
5278 struct mddev
*mddev
= mddev_find(dev
);
5279 struct gendisk
*disk
;
5288 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
5289 shift
= partitioned
? MdpMinorShift
: 0;
5290 unit
= MINOR(mddev
->unit
) >> shift
;
5292 /* wait for any previous instance of this device to be
5293 * completely removed (mddev_delayed_delete).
5295 flush_workqueue(md_misc_wq
);
5297 mutex_lock(&disks_mutex
);
5303 /* Need to ensure that 'name' is not a duplicate.
5305 struct mddev
*mddev2
;
5306 spin_lock(&all_mddevs_lock
);
5308 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
5309 if (mddev2
->gendisk
&&
5310 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
5311 spin_unlock(&all_mddevs_lock
);
5314 spin_unlock(&all_mddevs_lock
);
5318 * Creating /dev/mdNNN via "newarray", so adjust hold_active.
5320 mddev
->hold_active
= UNTIL_STOP
;
5323 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
5326 mddev
->queue
->queuedata
= mddev
;
5328 blk_queue_make_request(mddev
->queue
, md_make_request
);
5329 blk_set_stacking_limits(&mddev
->queue
->limits
);
5331 disk
= alloc_disk(1 << shift
);
5333 blk_cleanup_queue(mddev
->queue
);
5334 mddev
->queue
= NULL
;
5337 disk
->major
= MAJOR(mddev
->unit
);
5338 disk
->first_minor
= unit
<< shift
;
5340 strcpy(disk
->disk_name
, name
);
5341 else if (partitioned
)
5342 sprintf(disk
->disk_name
, "md_d%d", unit
);
5344 sprintf(disk
->disk_name
, "md%d", unit
);
5345 disk
->fops
= &md_fops
;
5346 disk
->private_data
= mddev
;
5347 disk
->queue
= mddev
->queue
;
5348 blk_queue_write_cache(mddev
->queue
, true, true);
5349 /* Allow extended partitions. This makes the
5350 * 'mdp' device redundant, but we can't really
5353 disk
->flags
|= GENHD_FL_EXT_DEVT
;
5354 mddev
->gendisk
= disk
;
5355 /* As soon as we call add_disk(), another thread could get
5356 * through to md_open, so make sure it doesn't get too far
5358 mutex_lock(&mddev
->open_mutex
);
5361 error
= kobject_add(&mddev
->kobj
, &disk_to_dev(disk
)->kobj
, "%s", "md");
5363 /* This isn't possible, but as kobject_init_and_add is marked
5364 * __must_check, we must do something with the result
5366 pr_debug("md: cannot register %s/md - name in use\n",
5370 if (mddev
->kobj
.sd
&&
5371 sysfs_create_group(&mddev
->kobj
, &md_bitmap_group
))
5372 pr_debug("pointless warning\n");
5373 mutex_unlock(&mddev
->open_mutex
);
5375 mutex_unlock(&disks_mutex
);
5376 if (!error
&& mddev
->kobj
.sd
) {
5377 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
5378 mddev
->sysfs_state
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "array_state");
5384 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
5387 md_alloc(dev
, NULL
);
5391 static int add_named_array(const char *val
, const struct kernel_param
*kp
)
5394 * val must be "md_*" or "mdNNN".
5395 * For "md_*" we allocate an array with a large free minor number, and
5396 * set the name to val. val must not already be an active name.
5397 * For "mdNNN" we allocate an array with the minor number NNN
5398 * which must not already be in use.
5400 int len
= strlen(val
);
5401 char buf
[DISK_NAME_LEN
];
5402 unsigned long devnum
;
5404 while (len
&& val
[len
-1] == '\n')
5406 if (len
>= DISK_NAME_LEN
)
5408 strlcpy(buf
, val
, len
+1);
5409 if (strncmp(buf
, "md_", 3) == 0)
5410 return md_alloc(0, buf
);
5411 if (strncmp(buf
, "md", 2) == 0 &&
5413 kstrtoul(buf
+2, 10, &devnum
) == 0 &&
5414 devnum
<= MINORMASK
)
5415 return md_alloc(MKDEV(MD_MAJOR
, devnum
), NULL
);
5420 static void md_safemode_timeout(struct timer_list
*t
)
5422 struct mddev
*mddev
= from_timer(mddev
, t
, safemode_timer
);
5424 mddev
->safemode
= 1;
5425 if (mddev
->external
)
5426 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5428 md_wakeup_thread(mddev
->thread
);
5431 static int start_dirty_degraded
;
5433 int md_run(struct mddev
*mddev
)
5436 struct md_rdev
*rdev
;
5437 struct md_personality
*pers
;
5439 if (list_empty(&mddev
->disks
))
5440 /* cannot run an array with no devices.. */
5445 /* Cannot run until previous stop completes properly */
5446 if (mddev
->sysfs_active
)
5450 * Analyze all RAID superblock(s)
5452 if (!mddev
->raid_disks
) {
5453 if (!mddev
->persistent
)
5458 if (mddev
->level
!= LEVEL_NONE
)
5459 request_module("md-level-%d", mddev
->level
);
5460 else if (mddev
->clevel
[0])
5461 request_module("md-%s", mddev
->clevel
);
5464 * Drop all container device buffers, from now on
5465 * the only valid external interface is through the md
5468 mddev
->has_superblocks
= false;
5469 rdev_for_each(rdev
, mddev
) {
5470 if (test_bit(Faulty
, &rdev
->flags
))
5472 sync_blockdev(rdev
->bdev
);
5473 invalidate_bdev(rdev
->bdev
);
5474 if (mddev
->ro
!= 1 &&
5475 (bdev_read_only(rdev
->bdev
) ||
5476 bdev_read_only(rdev
->meta_bdev
))) {
5479 set_disk_ro(mddev
->gendisk
, 1);
5483 mddev
->has_superblocks
= true;
5485 /* perform some consistency tests on the device.
5486 * We don't want the data to overlap the metadata,
5487 * Internal Bitmap issues have been handled elsewhere.
5489 if (rdev
->meta_bdev
) {
5490 /* Nothing to check */;
5491 } else if (rdev
->data_offset
< rdev
->sb_start
) {
5492 if (mddev
->dev_sectors
&&
5493 rdev
->data_offset
+ mddev
->dev_sectors
5495 pr_warn("md: %s: data overlaps metadata\n",
5500 if (rdev
->sb_start
+ rdev
->sb_size
/512
5501 > rdev
->data_offset
) {
5502 pr_warn("md: %s: metadata overlaps data\n",
5507 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
5510 if (!bioset_initialized(&mddev
->bio_set
)) {
5511 err
= bioset_init(&mddev
->bio_set
, BIO_POOL_SIZE
, 0, BIOSET_NEED_BVECS
);
5515 if (!bioset_initialized(&mddev
->sync_set
)) {
5516 err
= bioset_init(&mddev
->sync_set
, BIO_POOL_SIZE
, 0, BIOSET_NEED_BVECS
);
5520 if (mddev
->flush_pool
== NULL
) {
5521 mddev
->flush_pool
= mempool_create(NR_FLUSH_INFOS
, flush_info_alloc
,
5522 flush_info_free
, mddev
);
5523 if (!mddev
->flush_pool
) {
5528 if (mddev
->flush_bio_pool
== NULL
) {
5529 mddev
->flush_bio_pool
= mempool_create(NR_FLUSH_BIOS
, flush_bio_alloc
,
5530 flush_bio_free
, mddev
);
5531 if (!mddev
->flush_bio_pool
) {
5537 spin_lock(&pers_lock
);
5538 pers
= find_pers(mddev
->level
, mddev
->clevel
);
5539 if (!pers
|| !try_module_get(pers
->owner
)) {
5540 spin_unlock(&pers_lock
);
5541 if (mddev
->level
!= LEVEL_NONE
)
5542 pr_warn("md: personality for level %d is not loaded!\n",
5545 pr_warn("md: personality for level %s is not loaded!\n",
5550 spin_unlock(&pers_lock
);
5551 if (mddev
->level
!= pers
->level
) {
5552 mddev
->level
= pers
->level
;
5553 mddev
->new_level
= pers
->level
;
5555 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
5557 if (mddev
->reshape_position
!= MaxSector
&&
5558 pers
->start_reshape
== NULL
) {
5559 /* This personality cannot handle reshaping... */
5560 module_put(pers
->owner
);
5565 if (pers
->sync_request
) {
5566 /* Warn if this is a potentially silly
5569 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
5570 struct md_rdev
*rdev2
;
5573 rdev_for_each(rdev
, mddev
)
5574 rdev_for_each(rdev2
, mddev
) {
5576 rdev
->bdev
->bd_contains
==
5577 rdev2
->bdev
->bd_contains
) {
5578 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5580 bdevname(rdev
->bdev
,b
),
5581 bdevname(rdev2
->bdev
,b2
));
5587 pr_warn("True protection against single-disk failure might be compromised.\n");
5590 mddev
->recovery
= 0;
5591 /* may be over-ridden by personality */
5592 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
5594 mddev
->ok_start_degraded
= start_dirty_degraded
;
5596 if (start_readonly
&& mddev
->ro
== 0)
5597 mddev
->ro
= 2; /* read-only, but switch on first write */
5599 err
= pers
->run(mddev
);
5601 pr_warn("md: pers->run() failed ...\n");
5602 else if (pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
5603 WARN_ONCE(!mddev
->external_size
,
5604 "%s: default size too small, but 'external_size' not in effect?\n",
5606 pr_warn("md: invalid array_size %llu > default size %llu\n",
5607 (unsigned long long)mddev
->array_sectors
/ 2,
5608 (unsigned long long)pers
->size(mddev
, 0, 0) / 2);
5611 if (err
== 0 && pers
->sync_request
&&
5612 (mddev
->bitmap_info
.file
|| mddev
->bitmap_info
.offset
)) {
5613 struct bitmap
*bitmap
;
5615 bitmap
= md_bitmap_create(mddev
, -1);
5616 if (IS_ERR(bitmap
)) {
5617 err
= PTR_ERR(bitmap
);
5618 pr_warn("%s: failed to create bitmap (%d)\n",
5619 mdname(mddev
), err
);
5621 mddev
->bitmap
= bitmap
;
5625 mddev_detach(mddev
);
5627 pers
->free(mddev
, mddev
->private);
5628 mddev
->private = NULL
;
5629 module_put(pers
->owner
);
5630 md_bitmap_destroy(mddev
);
5636 rdev_for_each(rdev
, mddev
) {
5637 if (rdev
->raid_disk
>= 0 &&
5638 !blk_queue_nonrot(bdev_get_queue(rdev
->bdev
))) {
5643 if (mddev
->degraded
)
5646 blk_queue_flag_set(QUEUE_FLAG_NONROT
, mddev
->queue
);
5648 blk_queue_flag_clear(QUEUE_FLAG_NONROT
, mddev
->queue
);
5649 mddev
->queue
->backing_dev_info
->congested_data
= mddev
;
5650 mddev
->queue
->backing_dev_info
->congested_fn
= md_congested
;
5652 if (pers
->sync_request
) {
5653 if (mddev
->kobj
.sd
&&
5654 sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
5655 pr_warn("md: cannot register extra attributes for %s\n",
5657 mddev
->sysfs_action
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "sync_action");
5658 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
5661 atomic_set(&mddev
->max_corr_read_errors
,
5662 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS
);
5663 mddev
->safemode
= 0;
5664 if (mddev_is_clustered(mddev
))
5665 mddev
->safemode_delay
= 0;
5667 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
5670 spin_lock(&mddev
->lock
);
5672 spin_unlock(&mddev
->lock
);
5673 rdev_for_each(rdev
, mddev
)
5674 if (rdev
->raid_disk
>= 0)
5675 if (sysfs_link_rdev(mddev
, rdev
))
5676 /* failure here is OK */;
5678 if (mddev
->degraded
&& !mddev
->ro
)
5679 /* This ensures that recovering status is reported immediately
5680 * via sysfs - until a lack of spares is confirmed.
5682 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5683 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5685 if (mddev
->sb_flags
)
5686 md_update_sb(mddev
, 0);
5688 md_new_event(mddev
);
5689 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5690 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
5691 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
5695 if (mddev
->flush_bio_pool
) {
5696 mempool_destroy(mddev
->flush_bio_pool
);
5697 mddev
->flush_bio_pool
= NULL
;
5699 if (mddev
->flush_pool
){
5700 mempool_destroy(mddev
->flush_pool
);
5701 mddev
->flush_pool
= NULL
;
5706 EXPORT_SYMBOL_GPL(md_run
);
5708 static int do_md_run(struct mddev
*mddev
)
5712 err
= md_run(mddev
);
5715 err
= md_bitmap_load(mddev
);
5717 md_bitmap_destroy(mddev
);
5721 if (mddev_is_clustered(mddev
))
5722 md_allow_write(mddev
);
5724 /* run start up tasks that require md_thread */
5727 md_wakeup_thread(mddev
->thread
);
5728 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
5730 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
5731 revalidate_disk(mddev
->gendisk
);
5733 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
5738 int md_start(struct mddev
*mddev
)
5742 if (mddev
->pers
->start
) {
5743 set_bit(MD_RECOVERY_WAIT
, &mddev
->recovery
);
5744 md_wakeup_thread(mddev
->thread
);
5745 ret
= mddev
->pers
->start(mddev
);
5746 clear_bit(MD_RECOVERY_WAIT
, &mddev
->recovery
);
5747 md_wakeup_thread(mddev
->sync_thread
);
5751 EXPORT_SYMBOL_GPL(md_start
);
5753 static int restart_array(struct mddev
*mddev
)
5755 struct gendisk
*disk
= mddev
->gendisk
;
5756 struct md_rdev
*rdev
;
5757 bool has_journal
= false;
5758 bool has_readonly
= false;
5760 /* Complain if it has no devices */
5761 if (list_empty(&mddev
->disks
))
5769 rdev_for_each_rcu(rdev
, mddev
) {
5770 if (test_bit(Journal
, &rdev
->flags
) &&
5771 !test_bit(Faulty
, &rdev
->flags
))
5773 if (bdev_read_only(rdev
->bdev
))
5774 has_readonly
= true;
5777 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
) && !has_journal
)
5778 /* Don't restart rw with journal missing/faulty */
5783 mddev
->safemode
= 0;
5785 set_disk_ro(disk
, 0);
5786 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev
));
5787 /* Kick recovery or resync if necessary */
5788 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5789 md_wakeup_thread(mddev
->thread
);
5790 md_wakeup_thread(mddev
->sync_thread
);
5791 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5795 static void md_clean(struct mddev
*mddev
)
5797 mddev
->array_sectors
= 0;
5798 mddev
->external_size
= 0;
5799 mddev
->dev_sectors
= 0;
5800 mddev
->raid_disks
= 0;
5801 mddev
->recovery_cp
= 0;
5802 mddev
->resync_min
= 0;
5803 mddev
->resync_max
= MaxSector
;
5804 mddev
->reshape_position
= MaxSector
;
5805 mddev
->external
= 0;
5806 mddev
->persistent
= 0;
5807 mddev
->level
= LEVEL_NONE
;
5808 mddev
->clevel
[0] = 0;
5810 mddev
->sb_flags
= 0;
5812 mddev
->metadata_type
[0] = 0;
5813 mddev
->chunk_sectors
= 0;
5814 mddev
->ctime
= mddev
->utime
= 0;
5816 mddev
->max_disks
= 0;
5818 mddev
->can_decrease_events
= 0;
5819 mddev
->delta_disks
= 0;
5820 mddev
->reshape_backwards
= 0;
5821 mddev
->new_level
= LEVEL_NONE
;
5822 mddev
->new_layout
= 0;
5823 mddev
->new_chunk_sectors
= 0;
5824 mddev
->curr_resync
= 0;
5825 atomic64_set(&mddev
->resync_mismatches
, 0);
5826 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
5827 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
5828 mddev
->recovery
= 0;
5831 mddev
->degraded
= 0;
5832 mddev
->safemode
= 0;
5833 mddev
->private = NULL
;
5834 mddev
->cluster_info
= NULL
;
5835 mddev
->bitmap_info
.offset
= 0;
5836 mddev
->bitmap_info
.default_offset
= 0;
5837 mddev
->bitmap_info
.default_space
= 0;
5838 mddev
->bitmap_info
.chunksize
= 0;
5839 mddev
->bitmap_info
.daemon_sleep
= 0;
5840 mddev
->bitmap_info
.max_write_behind
= 0;
5841 mddev
->bitmap_info
.nodes
= 0;
5844 static void __md_stop_writes(struct mddev
*mddev
)
5846 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5847 flush_workqueue(md_misc_wq
);
5848 if (mddev
->sync_thread
) {
5849 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5850 md_reap_sync_thread(mddev
);
5853 del_timer_sync(&mddev
->safemode_timer
);
5855 if (mddev
->pers
&& mddev
->pers
->quiesce
) {
5856 mddev
->pers
->quiesce(mddev
, 1);
5857 mddev
->pers
->quiesce(mddev
, 0);
5859 md_bitmap_flush(mddev
);
5861 if (mddev
->ro
== 0 &&
5862 ((!mddev
->in_sync
&& !mddev_is_clustered(mddev
)) ||
5864 /* mark array as shutdown cleanly */
5865 if (!mddev_is_clustered(mddev
))
5867 md_update_sb(mddev
, 1);
5871 void md_stop_writes(struct mddev
*mddev
)
5873 mddev_lock_nointr(mddev
);
5874 __md_stop_writes(mddev
);
5875 mddev_unlock(mddev
);
5877 EXPORT_SYMBOL_GPL(md_stop_writes
);
5879 static void mddev_detach(struct mddev
*mddev
)
5881 md_bitmap_wait_behind_writes(mddev
);
5882 if (mddev
->pers
&& mddev
->pers
->quiesce
) {
5883 mddev
->pers
->quiesce(mddev
, 1);
5884 mddev
->pers
->quiesce(mddev
, 0);
5886 md_unregister_thread(&mddev
->thread
);
5888 blk_sync_queue(mddev
->queue
); /* the unplug fn references 'conf'*/
5891 static void __md_stop(struct mddev
*mddev
)
5893 struct md_personality
*pers
= mddev
->pers
;
5894 md_bitmap_destroy(mddev
);
5895 mddev_detach(mddev
);
5896 /* Ensure ->event_work is done */
5897 flush_workqueue(md_misc_wq
);
5898 spin_lock(&mddev
->lock
);
5900 spin_unlock(&mddev
->lock
);
5901 pers
->free(mddev
, mddev
->private);
5902 mddev
->private = NULL
;
5903 if (pers
->sync_request
&& mddev
->to_remove
== NULL
)
5904 mddev
->to_remove
= &md_redundancy_group
;
5905 module_put(pers
->owner
);
5906 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5909 void md_stop(struct mddev
*mddev
)
5911 /* stop the array and free an attached data structures.
5912 * This is called from dm-raid
5915 if (mddev
->flush_bio_pool
) {
5916 mempool_destroy(mddev
->flush_bio_pool
);
5917 mddev
->flush_bio_pool
= NULL
;
5919 if (mddev
->flush_pool
) {
5920 mempool_destroy(mddev
->flush_pool
);
5921 mddev
->flush_pool
= NULL
;
5923 bioset_exit(&mddev
->bio_set
);
5924 bioset_exit(&mddev
->sync_set
);
5927 EXPORT_SYMBOL_GPL(md_stop
);
5929 static int md_set_readonly(struct mddev
*mddev
, struct block_device
*bdev
)
5934 if (!test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
)) {
5936 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5937 md_wakeup_thread(mddev
->thread
);
5939 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
5940 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5941 if (mddev
->sync_thread
)
5942 /* Thread might be blocked waiting for metadata update
5943 * which will now never happen */
5944 wake_up_process(mddev
->sync_thread
->tsk
);
5946 if (mddev
->external
&& test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
))
5948 mddev_unlock(mddev
);
5949 wait_event(resync_wait
, !test_bit(MD_RECOVERY_RUNNING
,
5951 wait_event(mddev
->sb_wait
,
5952 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
5953 mddev_lock_nointr(mddev
);
5955 mutex_lock(&mddev
->open_mutex
);
5956 if ((mddev
->pers
&& atomic_read(&mddev
->openers
) > !!bdev
) ||
5957 mddev
->sync_thread
||
5958 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
)) {
5959 pr_warn("md: %s still in use.\n",mdname(mddev
));
5961 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5962 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5963 md_wakeup_thread(mddev
->thread
);
5969 __md_stop_writes(mddev
);
5975 set_disk_ro(mddev
->gendisk
, 1);
5976 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5977 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5978 md_wakeup_thread(mddev
->thread
);
5979 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5983 mutex_unlock(&mddev
->open_mutex
);
5988 * 0 - completely stop and dis-assemble array
5989 * 2 - stop but do not disassemble array
5991 static int do_md_stop(struct mddev
*mddev
, int mode
,
5992 struct block_device
*bdev
)
5994 struct gendisk
*disk
= mddev
->gendisk
;
5995 struct md_rdev
*rdev
;
5998 if (!test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
)) {
6000 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
6001 md_wakeup_thread(mddev
->thread
);
6003 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
6004 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6005 if (mddev
->sync_thread
)
6006 /* Thread might be blocked waiting for metadata update
6007 * which will now never happen */
6008 wake_up_process(mddev
->sync_thread
->tsk
);
6010 mddev_unlock(mddev
);
6011 wait_event(resync_wait
, (mddev
->sync_thread
== NULL
&&
6012 !test_bit(MD_RECOVERY_RUNNING
,
6013 &mddev
->recovery
)));
6014 mddev_lock_nointr(mddev
);
6016 mutex_lock(&mddev
->open_mutex
);
6017 if ((mddev
->pers
&& atomic_read(&mddev
->openers
) > !!bdev
) ||
6018 mddev
->sysfs_active
||
6019 mddev
->sync_thread
||
6020 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
)) {
6021 pr_warn("md: %s still in use.\n",mdname(mddev
));
6022 mutex_unlock(&mddev
->open_mutex
);
6024 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
6025 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6026 md_wakeup_thread(mddev
->thread
);
6032 set_disk_ro(disk
, 0);
6034 __md_stop_writes(mddev
);
6036 mddev
->queue
->backing_dev_info
->congested_fn
= NULL
;
6038 /* tell userspace to handle 'inactive' */
6039 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6041 rdev_for_each(rdev
, mddev
)
6042 if (rdev
->raid_disk
>= 0)
6043 sysfs_unlink_rdev(mddev
, rdev
);
6045 set_capacity(disk
, 0);
6046 mutex_unlock(&mddev
->open_mutex
);
6048 revalidate_disk(disk
);
6053 mutex_unlock(&mddev
->open_mutex
);
6055 * Free resources if final stop
6058 pr_info("md: %s stopped.\n", mdname(mddev
));
6060 if (mddev
->bitmap_info
.file
) {
6061 struct file
*f
= mddev
->bitmap_info
.file
;
6062 spin_lock(&mddev
->lock
);
6063 mddev
->bitmap_info
.file
= NULL
;
6064 spin_unlock(&mddev
->lock
);
6067 mddev
->bitmap_info
.offset
= 0;
6069 export_array(mddev
);
6072 if (mddev
->hold_active
== UNTIL_STOP
)
6073 mddev
->hold_active
= 0;
6075 md_new_event(mddev
);
6076 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6081 static void autorun_array(struct mddev
*mddev
)
6083 struct md_rdev
*rdev
;
6086 if (list_empty(&mddev
->disks
))
6089 pr_info("md: running: ");
6091 rdev_for_each(rdev
, mddev
) {
6092 char b
[BDEVNAME_SIZE
];
6093 pr_cont("<%s>", bdevname(rdev
->bdev
,b
));
6097 err
= do_md_run(mddev
);
6099 pr_warn("md: do_md_run() returned %d\n", err
);
6100 do_md_stop(mddev
, 0, NULL
);
6105 * lets try to run arrays based on all disks that have arrived
6106 * until now. (those are in pending_raid_disks)
6108 * the method: pick the first pending disk, collect all disks with
6109 * the same UUID, remove all from the pending list and put them into
6110 * the 'same_array' list. Then order this list based on superblock
6111 * update time (freshest comes first), kick out 'old' disks and
6112 * compare superblocks. If everything's fine then run it.
6114 * If "unit" is allocated, then bump its reference count
6116 static void autorun_devices(int part
)
6118 struct md_rdev
*rdev0
, *rdev
, *tmp
;
6119 struct mddev
*mddev
;
6120 char b
[BDEVNAME_SIZE
];
6122 pr_info("md: autorun ...\n");
6123 while (!list_empty(&pending_raid_disks
)) {
6126 LIST_HEAD(candidates
);
6127 rdev0
= list_entry(pending_raid_disks
.next
,
6128 struct md_rdev
, same_set
);
6130 pr_debug("md: considering %s ...\n", bdevname(rdev0
->bdev
,b
));
6131 INIT_LIST_HEAD(&candidates
);
6132 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
6133 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
6134 pr_debug("md: adding %s ...\n",
6135 bdevname(rdev
->bdev
,b
));
6136 list_move(&rdev
->same_set
, &candidates
);
6139 * now we have a set of devices, with all of them having
6140 * mostly sane superblocks. It's time to allocate the
6144 dev
= MKDEV(mdp_major
,
6145 rdev0
->preferred_minor
<< MdpMinorShift
);
6146 unit
= MINOR(dev
) >> MdpMinorShift
;
6148 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
6151 if (rdev0
->preferred_minor
!= unit
) {
6152 pr_warn("md: unit number in %s is bad: %d\n",
6153 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
6157 md_probe(dev
, NULL
, NULL
);
6158 mddev
= mddev_find(dev
);
6159 if (!mddev
|| !mddev
->gendisk
) {
6164 if (mddev_lock(mddev
))
6165 pr_warn("md: %s locked, cannot run\n", mdname(mddev
));
6166 else if (mddev
->raid_disks
|| mddev
->major_version
6167 || !list_empty(&mddev
->disks
)) {
6168 pr_warn("md: %s already running, cannot run %s\n",
6169 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
6170 mddev_unlock(mddev
);
6172 pr_debug("md: created %s\n", mdname(mddev
));
6173 mddev
->persistent
= 1;
6174 rdev_for_each_list(rdev
, tmp
, &candidates
) {
6175 list_del_init(&rdev
->same_set
);
6176 if (bind_rdev_to_array(rdev
, mddev
))
6179 autorun_array(mddev
);
6180 mddev_unlock(mddev
);
6182 /* on success, candidates will be empty, on error
6185 rdev_for_each_list(rdev
, tmp
, &candidates
) {
6186 list_del_init(&rdev
->same_set
);
6191 pr_info("md: ... autorun DONE.\n");
6193 #endif /* !MODULE */
6195 static int get_version(void __user
*arg
)
6199 ver
.major
= MD_MAJOR_VERSION
;
6200 ver
.minor
= MD_MINOR_VERSION
;
6201 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
6203 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
6209 static int get_array_info(struct mddev
*mddev
, void __user
*arg
)
6211 mdu_array_info_t info
;
6212 int nr
,working
,insync
,failed
,spare
;
6213 struct md_rdev
*rdev
;
6215 nr
= working
= insync
= failed
= spare
= 0;
6217 rdev_for_each_rcu(rdev
, mddev
) {
6219 if (test_bit(Faulty
, &rdev
->flags
))
6223 if (test_bit(In_sync
, &rdev
->flags
))
6225 else if (test_bit(Journal
, &rdev
->flags
))
6226 /* TODO: add journal count to md_u.h */
6234 info
.major_version
= mddev
->major_version
;
6235 info
.minor_version
= mddev
->minor_version
;
6236 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
6237 info
.ctime
= clamp_t(time64_t
, mddev
->ctime
, 0, U32_MAX
);
6238 info
.level
= mddev
->level
;
6239 info
.size
= mddev
->dev_sectors
/ 2;
6240 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
6243 info
.raid_disks
= mddev
->raid_disks
;
6244 info
.md_minor
= mddev
->md_minor
;
6245 info
.not_persistent
= !mddev
->persistent
;
6247 info
.utime
= clamp_t(time64_t
, mddev
->utime
, 0, U32_MAX
);
6250 info
.state
= (1<<MD_SB_CLEAN
);
6251 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
6252 info
.state
|= (1<<MD_SB_BITMAP_PRESENT
);
6253 if (mddev_is_clustered(mddev
))
6254 info
.state
|= (1<<MD_SB_CLUSTERED
);
6255 info
.active_disks
= insync
;
6256 info
.working_disks
= working
;
6257 info
.failed_disks
= failed
;
6258 info
.spare_disks
= spare
;
6260 info
.layout
= mddev
->layout
;
6261 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
6263 if (copy_to_user(arg
, &info
, sizeof(info
)))
6269 static int get_bitmap_file(struct mddev
*mddev
, void __user
* arg
)
6271 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
6275 file
= kzalloc(sizeof(*file
), GFP_NOIO
);
6280 spin_lock(&mddev
->lock
);
6281 /* bitmap enabled */
6282 if (mddev
->bitmap_info
.file
) {
6283 ptr
= file_path(mddev
->bitmap_info
.file
, file
->pathname
,
6284 sizeof(file
->pathname
));
6288 memmove(file
->pathname
, ptr
,
6289 sizeof(file
->pathname
)-(ptr
-file
->pathname
));
6291 spin_unlock(&mddev
->lock
);
6294 copy_to_user(arg
, file
, sizeof(*file
)))
6301 static int get_disk_info(struct mddev
*mddev
, void __user
* arg
)
6303 mdu_disk_info_t info
;
6304 struct md_rdev
*rdev
;
6306 if (copy_from_user(&info
, arg
, sizeof(info
)))
6310 rdev
= md_find_rdev_nr_rcu(mddev
, info
.number
);
6312 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
6313 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
6314 info
.raid_disk
= rdev
->raid_disk
;
6316 if (test_bit(Faulty
, &rdev
->flags
))
6317 info
.state
|= (1<<MD_DISK_FAULTY
);
6318 else if (test_bit(In_sync
, &rdev
->flags
)) {
6319 info
.state
|= (1<<MD_DISK_ACTIVE
);
6320 info
.state
|= (1<<MD_DISK_SYNC
);
6322 if (test_bit(Journal
, &rdev
->flags
))
6323 info
.state
|= (1<<MD_DISK_JOURNAL
);
6324 if (test_bit(WriteMostly
, &rdev
->flags
))
6325 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
6326 if (test_bit(FailFast
, &rdev
->flags
))
6327 info
.state
|= (1<<MD_DISK_FAILFAST
);
6329 info
.major
= info
.minor
= 0;
6330 info
.raid_disk
= -1;
6331 info
.state
= (1<<MD_DISK_REMOVED
);
6335 if (copy_to_user(arg
, &info
, sizeof(info
)))
6341 static int add_new_disk(struct mddev
*mddev
, mdu_disk_info_t
*info
)
6343 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
6344 struct md_rdev
*rdev
;
6345 dev_t dev
= MKDEV(info
->major
,info
->minor
);
6347 if (mddev_is_clustered(mddev
) &&
6348 !(info
->state
& ((1 << MD_DISK_CLUSTER_ADD
) | (1 << MD_DISK_CANDIDATE
)))) {
6349 pr_warn("%s: Cannot add to clustered mddev.\n",
6354 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
6357 if (!mddev
->raid_disks
) {
6359 /* expecting a device which has a superblock */
6360 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
6362 pr_warn("md: md_import_device returned %ld\n",
6364 return PTR_ERR(rdev
);
6366 if (!list_empty(&mddev
->disks
)) {
6367 struct md_rdev
*rdev0
6368 = list_entry(mddev
->disks
.next
,
6369 struct md_rdev
, same_set
);
6370 err
= super_types
[mddev
->major_version
]
6371 .load_super(rdev
, rdev0
, mddev
->minor_version
);
6373 pr_warn("md: %s has different UUID to %s\n",
6374 bdevname(rdev
->bdev
,b
),
6375 bdevname(rdev0
->bdev
,b2
));
6380 err
= bind_rdev_to_array(rdev
, mddev
);
6387 * add_new_disk can be used once the array is assembled
6388 * to add "hot spares". They must already have a superblock
6393 if (!mddev
->pers
->hot_add_disk
) {
6394 pr_warn("%s: personality does not support diskops!\n",
6398 if (mddev
->persistent
)
6399 rdev
= md_import_device(dev
, mddev
->major_version
,
6400 mddev
->minor_version
);
6402 rdev
= md_import_device(dev
, -1, -1);
6404 pr_warn("md: md_import_device returned %ld\n",
6406 return PTR_ERR(rdev
);
6408 /* set saved_raid_disk if appropriate */
6409 if (!mddev
->persistent
) {
6410 if (info
->state
& (1<<MD_DISK_SYNC
) &&
6411 info
->raid_disk
< mddev
->raid_disks
) {
6412 rdev
->raid_disk
= info
->raid_disk
;
6413 set_bit(In_sync
, &rdev
->flags
);
6414 clear_bit(Bitmap_sync
, &rdev
->flags
);
6416 rdev
->raid_disk
= -1;
6417 rdev
->saved_raid_disk
= rdev
->raid_disk
;
6419 super_types
[mddev
->major_version
].
6420 validate_super(mddev
, rdev
);
6421 if ((info
->state
& (1<<MD_DISK_SYNC
)) &&
6422 rdev
->raid_disk
!= info
->raid_disk
) {
6423 /* This was a hot-add request, but events doesn't
6424 * match, so reject it.
6430 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
6431 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
6432 set_bit(WriteMostly
, &rdev
->flags
);
6434 clear_bit(WriteMostly
, &rdev
->flags
);
6435 if (info
->state
& (1<<MD_DISK_FAILFAST
))
6436 set_bit(FailFast
, &rdev
->flags
);
6438 clear_bit(FailFast
, &rdev
->flags
);
6440 if (info
->state
& (1<<MD_DISK_JOURNAL
)) {
6441 struct md_rdev
*rdev2
;
6442 bool has_journal
= false;
6444 /* make sure no existing journal disk */
6445 rdev_for_each(rdev2
, mddev
) {
6446 if (test_bit(Journal
, &rdev2
->flags
)) {
6451 if (has_journal
|| mddev
->bitmap
) {
6455 set_bit(Journal
, &rdev
->flags
);
6458 * check whether the device shows up in other nodes
6460 if (mddev_is_clustered(mddev
)) {
6461 if (info
->state
& (1 << MD_DISK_CANDIDATE
))
6462 set_bit(Candidate
, &rdev
->flags
);
6463 else if (info
->state
& (1 << MD_DISK_CLUSTER_ADD
)) {
6464 /* --add initiated by this node */
6465 err
= md_cluster_ops
->add_new_disk(mddev
, rdev
);
6473 rdev
->raid_disk
= -1;
6474 err
= bind_rdev_to_array(rdev
, mddev
);
6479 if (mddev_is_clustered(mddev
)) {
6480 if (info
->state
& (1 << MD_DISK_CANDIDATE
)) {
6482 err
= md_cluster_ops
->new_disk_ack(mddev
,
6485 md_kick_rdev_from_array(rdev
);
6489 md_cluster_ops
->add_new_disk_cancel(mddev
);
6491 err
= add_bound_rdev(rdev
);
6495 err
= add_bound_rdev(rdev
);
6500 /* otherwise, add_new_disk is only allowed
6501 * for major_version==0 superblocks
6503 if (mddev
->major_version
!= 0) {
6504 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev
));
6508 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
6510 rdev
= md_import_device(dev
, -1, 0);
6512 pr_warn("md: error, md_import_device() returned %ld\n",
6514 return PTR_ERR(rdev
);
6516 rdev
->desc_nr
= info
->number
;
6517 if (info
->raid_disk
< mddev
->raid_disks
)
6518 rdev
->raid_disk
= info
->raid_disk
;
6520 rdev
->raid_disk
= -1;
6522 if (rdev
->raid_disk
< mddev
->raid_disks
)
6523 if (info
->state
& (1<<MD_DISK_SYNC
))
6524 set_bit(In_sync
, &rdev
->flags
);
6526 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
6527 set_bit(WriteMostly
, &rdev
->flags
);
6528 if (info
->state
& (1<<MD_DISK_FAILFAST
))
6529 set_bit(FailFast
, &rdev
->flags
);
6531 if (!mddev
->persistent
) {
6532 pr_debug("md: nonpersistent superblock ...\n");
6533 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
6535 rdev
->sb_start
= calc_dev_sboffset(rdev
);
6536 rdev
->sectors
= rdev
->sb_start
;
6538 err
= bind_rdev_to_array(rdev
, mddev
);
6548 static int hot_remove_disk(struct mddev
*mddev
, dev_t dev
)
6550 char b
[BDEVNAME_SIZE
];
6551 struct md_rdev
*rdev
;
6556 rdev
= find_rdev(mddev
, dev
);
6560 if (rdev
->raid_disk
< 0)
6563 clear_bit(Blocked
, &rdev
->flags
);
6564 remove_and_add_spares(mddev
, rdev
);
6566 if (rdev
->raid_disk
>= 0)
6570 if (mddev_is_clustered(mddev
))
6571 md_cluster_ops
->remove_disk(mddev
, rdev
);
6573 md_kick_rdev_from_array(rdev
);
6574 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
6576 md_wakeup_thread(mddev
->thread
);
6578 md_update_sb(mddev
, 1);
6579 md_new_event(mddev
);
6583 pr_debug("md: cannot remove active disk %s from %s ...\n",
6584 bdevname(rdev
->bdev
,b
), mdname(mddev
));
6588 static int hot_add_disk(struct mddev
*mddev
, dev_t dev
)
6590 char b
[BDEVNAME_SIZE
];
6592 struct md_rdev
*rdev
;
6597 if (mddev
->major_version
!= 0) {
6598 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
6602 if (!mddev
->pers
->hot_add_disk
) {
6603 pr_warn("%s: personality does not support diskops!\n",
6608 rdev
= md_import_device(dev
, -1, 0);
6610 pr_warn("md: error, md_import_device() returned %ld\n",
6615 if (mddev
->persistent
)
6616 rdev
->sb_start
= calc_dev_sboffset(rdev
);
6618 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
6620 rdev
->sectors
= rdev
->sb_start
;
6622 if (test_bit(Faulty
, &rdev
->flags
)) {
6623 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
6624 bdevname(rdev
->bdev
,b
), mdname(mddev
));
6629 clear_bit(In_sync
, &rdev
->flags
);
6631 rdev
->saved_raid_disk
= -1;
6632 err
= bind_rdev_to_array(rdev
, mddev
);
6637 * The rest should better be atomic, we can have disk failures
6638 * noticed in interrupt contexts ...
6641 rdev
->raid_disk
= -1;
6643 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
6645 md_update_sb(mddev
, 1);
6647 * Kick recovery, maybe this spare has to be added to the
6648 * array immediately.
6650 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6651 md_wakeup_thread(mddev
->thread
);
6652 md_new_event(mddev
);
6660 static int set_bitmap_file(struct mddev
*mddev
, int fd
)
6665 if (!mddev
->pers
->quiesce
|| !mddev
->thread
)
6667 if (mddev
->recovery
|| mddev
->sync_thread
)
6669 /* we should be able to change the bitmap.. */
6673 struct inode
*inode
;
6676 if (mddev
->bitmap
|| mddev
->bitmap_info
.file
)
6677 return -EEXIST
; /* cannot add when bitmap is present */
6681 pr_warn("%s: error: failed to get bitmap file\n",
6686 inode
= f
->f_mapping
->host
;
6687 if (!S_ISREG(inode
->i_mode
)) {
6688 pr_warn("%s: error: bitmap file must be a regular file\n",
6691 } else if (!(f
->f_mode
& FMODE_WRITE
)) {
6692 pr_warn("%s: error: bitmap file must open for write\n",
6695 } else if (atomic_read(&inode
->i_writecount
) != 1) {
6696 pr_warn("%s: error: bitmap file is already in use\n",
6704 mddev
->bitmap_info
.file
= f
;
6705 mddev
->bitmap_info
.offset
= 0; /* file overrides offset */
6706 } else if (mddev
->bitmap
== NULL
)
6707 return -ENOENT
; /* cannot remove what isn't there */
6711 struct bitmap
*bitmap
;
6713 bitmap
= md_bitmap_create(mddev
, -1);
6714 mddev_suspend(mddev
);
6715 if (!IS_ERR(bitmap
)) {
6716 mddev
->bitmap
= bitmap
;
6717 err
= md_bitmap_load(mddev
);
6719 err
= PTR_ERR(bitmap
);
6721 md_bitmap_destroy(mddev
);
6724 mddev_resume(mddev
);
6725 } else if (fd
< 0) {
6726 mddev_suspend(mddev
);
6727 md_bitmap_destroy(mddev
);
6728 mddev_resume(mddev
);
6732 struct file
*f
= mddev
->bitmap_info
.file
;
6734 spin_lock(&mddev
->lock
);
6735 mddev
->bitmap_info
.file
= NULL
;
6736 spin_unlock(&mddev
->lock
);
6745 * set_array_info is used two different ways
6746 * The original usage is when creating a new array.
6747 * In this usage, raid_disks is > 0 and it together with
6748 * level, size, not_persistent,layout,chunksize determine the
6749 * shape of the array.
6750 * This will always create an array with a type-0.90.0 superblock.
6751 * The newer usage is when assembling an array.
6752 * In this case raid_disks will be 0, and the major_version field is
6753 * use to determine which style super-blocks are to be found on the devices.
6754 * The minor and patch _version numbers are also kept incase the
6755 * super_block handler wishes to interpret them.
6757 static int set_array_info(struct mddev
*mddev
, mdu_array_info_t
*info
)
6760 if (info
->raid_disks
== 0) {
6761 /* just setting version number for superblock loading */
6762 if (info
->major_version
< 0 ||
6763 info
->major_version
>= ARRAY_SIZE(super_types
) ||
6764 super_types
[info
->major_version
].name
== NULL
) {
6765 /* maybe try to auto-load a module? */
6766 pr_warn("md: superblock version %d not known\n",
6767 info
->major_version
);
6770 mddev
->major_version
= info
->major_version
;
6771 mddev
->minor_version
= info
->minor_version
;
6772 mddev
->patch_version
= info
->patch_version
;
6773 mddev
->persistent
= !info
->not_persistent
;
6774 /* ensure mddev_put doesn't delete this now that there
6775 * is some minimal configuration.
6777 mddev
->ctime
= ktime_get_real_seconds();
6780 mddev
->major_version
= MD_MAJOR_VERSION
;
6781 mddev
->minor_version
= MD_MINOR_VERSION
;
6782 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
6783 mddev
->ctime
= ktime_get_real_seconds();
6785 mddev
->level
= info
->level
;
6786 mddev
->clevel
[0] = 0;
6787 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
6788 mddev
->raid_disks
= info
->raid_disks
;
6789 /* don't set md_minor, it is determined by which /dev/md* was
6792 if (info
->state
& (1<<MD_SB_CLEAN
))
6793 mddev
->recovery_cp
= MaxSector
;
6795 mddev
->recovery_cp
= 0;
6796 mddev
->persistent
= ! info
->not_persistent
;
6797 mddev
->external
= 0;
6799 mddev
->layout
= info
->layout
;
6800 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
6802 if (mddev
->persistent
) {
6803 mddev
->max_disks
= MD_SB_DISKS
;
6805 mddev
->sb_flags
= 0;
6807 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
6809 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
6810 mddev
->bitmap_info
.default_space
= 64*2 - (MD_SB_BYTES
>> 9);
6811 mddev
->bitmap_info
.offset
= 0;
6813 mddev
->reshape_position
= MaxSector
;
6816 * Generate a 128 bit UUID
6818 get_random_bytes(mddev
->uuid
, 16);
6820 mddev
->new_level
= mddev
->level
;
6821 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
6822 mddev
->new_layout
= mddev
->layout
;
6823 mddev
->delta_disks
= 0;
6824 mddev
->reshape_backwards
= 0;
6829 void md_set_array_sectors(struct mddev
*mddev
, sector_t array_sectors
)
6831 lockdep_assert_held(&mddev
->reconfig_mutex
);
6833 if (mddev
->external_size
)
6836 mddev
->array_sectors
= array_sectors
;
6838 EXPORT_SYMBOL(md_set_array_sectors
);
6840 static int update_size(struct mddev
*mddev
, sector_t num_sectors
)
6842 struct md_rdev
*rdev
;
6844 int fit
= (num_sectors
== 0);
6845 sector_t old_dev_sectors
= mddev
->dev_sectors
;
6847 if (mddev
->pers
->resize
== NULL
)
6849 /* The "num_sectors" is the number of sectors of each device that
6850 * is used. This can only make sense for arrays with redundancy.
6851 * linear and raid0 always use whatever space is available. We can only
6852 * consider changing this number if no resync or reconstruction is
6853 * happening, and if the new size is acceptable. It must fit before the
6854 * sb_start or, if that is <data_offset, it must fit before the size
6855 * of each device. If num_sectors is zero, we find the largest size
6858 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
6864 rdev_for_each(rdev
, mddev
) {
6865 sector_t avail
= rdev
->sectors
;
6867 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
6868 num_sectors
= avail
;
6869 if (avail
< num_sectors
)
6872 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
6874 if (mddev_is_clustered(mddev
))
6875 md_cluster_ops
->update_size(mddev
, old_dev_sectors
);
6876 else if (mddev
->queue
) {
6877 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
6878 revalidate_disk(mddev
->gendisk
);
6884 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
)
6887 struct md_rdev
*rdev
;
6888 /* change the number of raid disks */
6889 if (mddev
->pers
->check_reshape
== NULL
)
6893 if (raid_disks
<= 0 ||
6894 (mddev
->max_disks
&& raid_disks
>= mddev
->max_disks
))
6896 if (mddev
->sync_thread
||
6897 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
6898 mddev
->reshape_position
!= MaxSector
)
6901 rdev_for_each(rdev
, mddev
) {
6902 if (mddev
->raid_disks
< raid_disks
&&
6903 rdev
->data_offset
< rdev
->new_data_offset
)
6905 if (mddev
->raid_disks
> raid_disks
&&
6906 rdev
->data_offset
> rdev
->new_data_offset
)
6910 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
6911 if (mddev
->delta_disks
< 0)
6912 mddev
->reshape_backwards
= 1;
6913 else if (mddev
->delta_disks
> 0)
6914 mddev
->reshape_backwards
= 0;
6916 rv
= mddev
->pers
->check_reshape(mddev
);
6918 mddev
->delta_disks
= 0;
6919 mddev
->reshape_backwards
= 0;
6925 * update_array_info is used to change the configuration of an
6927 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6928 * fields in the info are checked against the array.
6929 * Any differences that cannot be handled will cause an error.
6930 * Normally, only one change can be managed at a time.
6932 static int update_array_info(struct mddev
*mddev
, mdu_array_info_t
*info
)
6938 /* calculate expected state,ignoring low bits */
6939 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
6940 state
|= (1 << MD_SB_BITMAP_PRESENT
);
6942 if (mddev
->major_version
!= info
->major_version
||
6943 mddev
->minor_version
!= info
->minor_version
||
6944 /* mddev->patch_version != info->patch_version || */
6945 mddev
->ctime
!= info
->ctime
||
6946 mddev
->level
!= info
->level
||
6947 /* mddev->layout != info->layout || */
6948 mddev
->persistent
!= !info
->not_persistent
||
6949 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
6950 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6951 ((state
^info
->state
) & 0xfffffe00)
6954 /* Check there is only one change */
6955 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
6957 if (mddev
->raid_disks
!= info
->raid_disks
)
6959 if (mddev
->layout
!= info
->layout
)
6961 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
6968 if (mddev
->layout
!= info
->layout
) {
6970 * we don't need to do anything at the md level, the
6971 * personality will take care of it all.
6973 if (mddev
->pers
->check_reshape
== NULL
)
6976 mddev
->new_layout
= info
->layout
;
6977 rv
= mddev
->pers
->check_reshape(mddev
);
6979 mddev
->new_layout
= mddev
->layout
;
6983 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
6984 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
6986 if (mddev
->raid_disks
!= info
->raid_disks
)
6987 rv
= update_raid_disks(mddev
, info
->raid_disks
);
6989 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
6990 if (mddev
->pers
->quiesce
== NULL
|| mddev
->thread
== NULL
) {
6994 if (mddev
->recovery
|| mddev
->sync_thread
) {
6998 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
6999 struct bitmap
*bitmap
;
7000 /* add the bitmap */
7001 if (mddev
->bitmap
) {
7005 if (mddev
->bitmap_info
.default_offset
== 0) {
7009 mddev
->bitmap_info
.offset
=
7010 mddev
->bitmap_info
.default_offset
;
7011 mddev
->bitmap_info
.space
=
7012 mddev
->bitmap_info
.default_space
;
7013 bitmap
= md_bitmap_create(mddev
, -1);
7014 mddev_suspend(mddev
);
7015 if (!IS_ERR(bitmap
)) {
7016 mddev
->bitmap
= bitmap
;
7017 rv
= md_bitmap_load(mddev
);
7019 rv
= PTR_ERR(bitmap
);
7021 md_bitmap_destroy(mddev
);
7022 mddev_resume(mddev
);
7024 /* remove the bitmap */
7025 if (!mddev
->bitmap
) {
7029 if (mddev
->bitmap
->storage
.file
) {
7033 if (mddev
->bitmap_info
.nodes
) {
7034 /* hold PW on all the bitmap lock */
7035 if (md_cluster_ops
->lock_all_bitmaps(mddev
) <= 0) {
7036 pr_warn("md: can't change bitmap to none since the array is in use by more than one node\n");
7038 md_cluster_ops
->unlock_all_bitmaps(mddev
);
7042 mddev
->bitmap_info
.nodes
= 0;
7043 md_cluster_ops
->leave(mddev
);
7045 mddev_suspend(mddev
);
7046 md_bitmap_destroy(mddev
);
7047 mddev_resume(mddev
);
7048 mddev
->bitmap_info
.offset
= 0;
7051 md_update_sb(mddev
, 1);
7057 static int set_disk_faulty(struct mddev
*mddev
, dev_t dev
)
7059 struct md_rdev
*rdev
;
7062 if (mddev
->pers
== NULL
)
7066 rdev
= md_find_rdev_rcu(mddev
, dev
);
7070 md_error(mddev
, rdev
);
7071 if (!test_bit(Faulty
, &rdev
->flags
))
7079 * We have a problem here : there is no easy way to give a CHS
7080 * virtual geometry. We currently pretend that we have a 2 heads
7081 * 4 sectors (with a BIG number of cylinders...). This drives
7082 * dosfs just mad... ;-)
7084 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
7086 struct mddev
*mddev
= bdev
->bd_disk
->private_data
;
7090 geo
->cylinders
= mddev
->array_sectors
/ 8;
7094 static inline bool md_ioctl_valid(unsigned int cmd
)
7099 case GET_ARRAY_INFO
:
7100 case GET_BITMAP_FILE
:
7103 case HOT_REMOVE_DISK
:
7106 case RESTART_ARRAY_RW
:
7108 case SET_ARRAY_INFO
:
7109 case SET_BITMAP_FILE
:
7110 case SET_DISK_FAULTY
:
7113 case CLUSTERED_DISK_NACK
:
7120 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
7121 unsigned int cmd
, unsigned long arg
)
7124 void __user
*argp
= (void __user
*)arg
;
7125 struct mddev
*mddev
= NULL
;
7127 bool did_set_md_closing
= false;
7129 if (!md_ioctl_valid(cmd
))
7134 case GET_ARRAY_INFO
:
7138 if (!capable(CAP_SYS_ADMIN
))
7143 * Commands dealing with the RAID driver but not any
7148 err
= get_version(argp
);
7154 autostart_arrays(arg
);
7161 * Commands creating/starting a new array:
7164 mddev
= bdev
->bd_disk
->private_data
;
7171 /* Some actions do not requires the mutex */
7173 case GET_ARRAY_INFO
:
7174 if (!mddev
->raid_disks
&& !mddev
->external
)
7177 err
= get_array_info(mddev
, argp
);
7181 if (!mddev
->raid_disks
&& !mddev
->external
)
7184 err
= get_disk_info(mddev
, argp
);
7187 case SET_DISK_FAULTY
:
7188 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
7191 case GET_BITMAP_FILE
:
7192 err
= get_bitmap_file(mddev
, argp
);
7197 if (cmd
== ADD_NEW_DISK
)
7198 /* need to ensure md_delayed_delete() has completed */
7199 flush_workqueue(md_misc_wq
);
7201 if (cmd
== HOT_REMOVE_DISK
)
7202 /* need to ensure recovery thread has run */
7203 wait_event_interruptible_timeout(mddev
->sb_wait
,
7204 !test_bit(MD_RECOVERY_NEEDED
,
7206 msecs_to_jiffies(5000));
7207 if (cmd
== STOP_ARRAY
|| cmd
== STOP_ARRAY_RO
) {
7208 /* Need to flush page cache, and ensure no-one else opens
7211 mutex_lock(&mddev
->open_mutex
);
7212 if (mddev
->pers
&& atomic_read(&mddev
->openers
) > 1) {
7213 mutex_unlock(&mddev
->open_mutex
);
7217 WARN_ON_ONCE(test_bit(MD_CLOSING
, &mddev
->flags
));
7218 set_bit(MD_CLOSING
, &mddev
->flags
);
7219 did_set_md_closing
= true;
7220 mutex_unlock(&mddev
->open_mutex
);
7221 sync_blockdev(bdev
);
7223 err
= mddev_lock(mddev
);
7225 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7230 if (cmd
== SET_ARRAY_INFO
) {
7231 mdu_array_info_t info
;
7233 memset(&info
, 0, sizeof(info
));
7234 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
7239 err
= update_array_info(mddev
, &info
);
7241 pr_warn("md: couldn't update array info. %d\n", err
);
7246 if (!list_empty(&mddev
->disks
)) {
7247 pr_warn("md: array %s already has disks!\n", mdname(mddev
));
7251 if (mddev
->raid_disks
) {
7252 pr_warn("md: array %s already initialised!\n", mdname(mddev
));
7256 err
= set_array_info(mddev
, &info
);
7258 pr_warn("md: couldn't set array info. %d\n", err
);
7265 * Commands querying/configuring an existing array:
7267 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
7268 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
7269 if ((!mddev
->raid_disks
&& !mddev
->external
)
7270 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
7271 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
7272 && cmd
!= GET_BITMAP_FILE
) {
7278 * Commands even a read-only array can execute:
7281 case RESTART_ARRAY_RW
:
7282 err
= restart_array(mddev
);
7286 err
= do_md_stop(mddev
, 0, bdev
);
7290 err
= md_set_readonly(mddev
, bdev
);
7293 case HOT_REMOVE_DISK
:
7294 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
7298 /* We can support ADD_NEW_DISK on read-only arrays
7299 * only if we are re-adding a preexisting device.
7300 * So require mddev->pers and MD_DISK_SYNC.
7303 mdu_disk_info_t info
;
7304 if (copy_from_user(&info
, argp
, sizeof(info
)))
7306 else if (!(info
.state
& (1<<MD_DISK_SYNC
)))
7307 /* Need to clear read-only for this */
7310 err
= add_new_disk(mddev
, &info
);
7316 if (get_user(ro
, (int __user
*)(arg
))) {
7322 /* if the bdev is going readonly the value of mddev->ro
7323 * does not matter, no writes are coming
7328 /* are we are already prepared for writes? */
7332 /* transitioning to readauto need only happen for
7333 * arrays that call md_write_start
7336 err
= restart_array(mddev
);
7339 set_disk_ro(mddev
->gendisk
, 0);
7346 * The remaining ioctls are changing the state of the
7347 * superblock, so we do not allow them on read-only arrays.
7349 if (mddev
->ro
&& mddev
->pers
) {
7350 if (mddev
->ro
== 2) {
7352 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7353 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7354 /* mddev_unlock will wake thread */
7355 /* If a device failed while we were read-only, we
7356 * need to make sure the metadata is updated now.
7358 if (test_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
)) {
7359 mddev_unlock(mddev
);
7360 wait_event(mddev
->sb_wait
,
7361 !test_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
) &&
7362 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
7363 mddev_lock_nointr(mddev
);
7374 mdu_disk_info_t info
;
7375 if (copy_from_user(&info
, argp
, sizeof(info
)))
7378 err
= add_new_disk(mddev
, &info
);
7382 case CLUSTERED_DISK_NACK
:
7383 if (mddev_is_clustered(mddev
))
7384 md_cluster_ops
->new_disk_ack(mddev
, false);
7390 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
7394 err
= do_md_run(mddev
);
7397 case SET_BITMAP_FILE
:
7398 err
= set_bitmap_file(mddev
, (int)arg
);
7407 if (mddev
->hold_active
== UNTIL_IOCTL
&&
7409 mddev
->hold_active
= 0;
7410 mddev_unlock(mddev
);
7412 if(did_set_md_closing
)
7413 clear_bit(MD_CLOSING
, &mddev
->flags
);
7416 #ifdef CONFIG_COMPAT
7417 static int md_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
7418 unsigned int cmd
, unsigned long arg
)
7421 case HOT_REMOVE_DISK
:
7423 case SET_DISK_FAULTY
:
7424 case SET_BITMAP_FILE
:
7425 /* These take in integer arg, do not convert */
7428 arg
= (unsigned long)compat_ptr(arg
);
7432 return md_ioctl(bdev
, mode
, cmd
, arg
);
7434 #endif /* CONFIG_COMPAT */
7436 static int md_open(struct block_device
*bdev
, fmode_t mode
)
7439 * Succeed if we can lock the mddev, which confirms that
7440 * it isn't being stopped right now.
7442 struct mddev
*mddev
= mddev_find(bdev
->bd_dev
);
7448 if (mddev
->gendisk
!= bdev
->bd_disk
) {
7449 /* we are racing with mddev_put which is discarding this
7453 /* Wait until bdev->bd_disk is definitely gone */
7454 flush_workqueue(md_misc_wq
);
7455 /* Then retry the open from the top */
7456 return -ERESTARTSYS
;
7458 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
7460 if ((err
= mutex_lock_interruptible(&mddev
->open_mutex
)))
7463 if (test_bit(MD_CLOSING
, &mddev
->flags
)) {
7464 mutex_unlock(&mddev
->open_mutex
);
7470 atomic_inc(&mddev
->openers
);
7471 mutex_unlock(&mddev
->open_mutex
);
7473 check_disk_change(bdev
);
7480 static void md_release(struct gendisk
*disk
, fmode_t mode
)
7482 struct mddev
*mddev
= disk
->private_data
;
7485 atomic_dec(&mddev
->openers
);
7489 static int md_media_changed(struct gendisk
*disk
)
7491 struct mddev
*mddev
= disk
->private_data
;
7493 return mddev
->changed
;
7496 static int md_revalidate(struct gendisk
*disk
)
7498 struct mddev
*mddev
= disk
->private_data
;
7503 static const struct block_device_operations md_fops
=
7505 .owner
= THIS_MODULE
,
7507 .release
= md_release
,
7509 #ifdef CONFIG_COMPAT
7510 .compat_ioctl
= md_compat_ioctl
,
7512 .getgeo
= md_getgeo
,
7513 .media_changed
= md_media_changed
,
7514 .revalidate_disk
= md_revalidate
,
7517 static int md_thread(void *arg
)
7519 struct md_thread
*thread
= arg
;
7522 * md_thread is a 'system-thread', it's priority should be very
7523 * high. We avoid resource deadlocks individually in each
7524 * raid personality. (RAID5 does preallocation) We also use RR and
7525 * the very same RT priority as kswapd, thus we will never get
7526 * into a priority inversion deadlock.
7528 * we definitely have to have equal or higher priority than
7529 * bdflush, otherwise bdflush will deadlock if there are too
7530 * many dirty RAID5 blocks.
7533 allow_signal(SIGKILL
);
7534 while (!kthread_should_stop()) {
7536 /* We need to wait INTERRUPTIBLE so that
7537 * we don't add to the load-average.
7538 * That means we need to be sure no signals are
7541 if (signal_pending(current
))
7542 flush_signals(current
);
7544 wait_event_interruptible_timeout
7546 test_bit(THREAD_WAKEUP
, &thread
->flags
)
7547 || kthread_should_stop() || kthread_should_park(),
7550 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
7551 if (kthread_should_park())
7553 if (!kthread_should_stop())
7554 thread
->run(thread
);
7560 void md_wakeup_thread(struct md_thread
*thread
)
7563 pr_debug("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
7564 set_bit(THREAD_WAKEUP
, &thread
->flags
);
7565 wake_up(&thread
->wqueue
);
7568 EXPORT_SYMBOL(md_wakeup_thread
);
7570 struct md_thread
*md_register_thread(void (*run
) (struct md_thread
*),
7571 struct mddev
*mddev
, const char *name
)
7573 struct md_thread
*thread
;
7575 thread
= kzalloc(sizeof(struct md_thread
), GFP_KERNEL
);
7579 init_waitqueue_head(&thread
->wqueue
);
7582 thread
->mddev
= mddev
;
7583 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
7584 thread
->tsk
= kthread_run(md_thread
, thread
,
7586 mdname(thread
->mddev
),
7588 if (IS_ERR(thread
->tsk
)) {
7594 EXPORT_SYMBOL(md_register_thread
);
7596 void md_unregister_thread(struct md_thread
**threadp
)
7598 struct md_thread
*thread
= *threadp
;
7601 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
7602 /* Locking ensures that mddev_unlock does not wake_up a
7603 * non-existent thread
7605 spin_lock(&pers_lock
);
7607 spin_unlock(&pers_lock
);
7609 kthread_stop(thread
->tsk
);
7612 EXPORT_SYMBOL(md_unregister_thread
);
7614 void md_error(struct mddev
*mddev
, struct md_rdev
*rdev
)
7616 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
7619 if (!mddev
->pers
|| !mddev
->pers
->error_handler
)
7621 mddev
->pers
->error_handler(mddev
,rdev
);
7622 if (mddev
->degraded
)
7623 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7624 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
7625 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7626 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7627 md_wakeup_thread(mddev
->thread
);
7628 if (mddev
->event_work
.func
)
7629 queue_work(md_misc_wq
, &mddev
->event_work
);
7630 md_new_event(mddev
);
7632 EXPORT_SYMBOL(md_error
);
7634 /* seq_file implementation /proc/mdstat */
7636 static void status_unused(struct seq_file
*seq
)
7639 struct md_rdev
*rdev
;
7641 seq_printf(seq
, "unused devices: ");
7643 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
7644 char b
[BDEVNAME_SIZE
];
7646 seq_printf(seq
, "%s ",
7647 bdevname(rdev
->bdev
,b
));
7650 seq_printf(seq
, "<none>");
7652 seq_printf(seq
, "\n");
7655 static int status_resync(struct seq_file
*seq
, struct mddev
*mddev
)
7657 sector_t max_sectors
, resync
, res
;
7658 unsigned long dt
, db
;
7661 unsigned int per_milli
;
7663 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
7664 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
7665 max_sectors
= mddev
->resync_max_sectors
;
7667 max_sectors
= mddev
->dev_sectors
;
7669 resync
= mddev
->curr_resync
;
7671 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
7672 /* Still cleaning up */
7673 resync
= max_sectors
;
7674 } else if (resync
> max_sectors
)
7675 resync
= max_sectors
;
7677 resync
-= atomic_read(&mddev
->recovery_active
);
7680 if (test_bit(MD_RESYNCING_REMOTE
, &mddev
->recovery
)) {
7681 struct md_rdev
*rdev
;
7683 rdev_for_each(rdev
, mddev
)
7684 if (rdev
->raid_disk
>= 0 &&
7685 !test_bit(Faulty
, &rdev
->flags
) &&
7686 rdev
->recovery_offset
!= MaxSector
&&
7687 rdev
->recovery_offset
) {
7688 seq_printf(seq
, "\trecover=REMOTE");
7691 if (mddev
->reshape_position
!= MaxSector
)
7692 seq_printf(seq
, "\treshape=REMOTE");
7694 seq_printf(seq
, "\tresync=REMOTE");
7697 if (mddev
->recovery_cp
< MaxSector
) {
7698 seq_printf(seq
, "\tresync=PENDING");
7704 seq_printf(seq
, "\tresync=DELAYED");
7708 WARN_ON(max_sectors
== 0);
7709 /* Pick 'scale' such that (resync>>scale)*1000 will fit
7710 * in a sector_t, and (max_sectors>>scale) will fit in a
7711 * u32, as those are the requirements for sector_div.
7712 * Thus 'scale' must be at least 10
7715 if (sizeof(sector_t
) > sizeof(unsigned long)) {
7716 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
7719 res
= (resync
>>scale
)*1000;
7720 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
7724 int i
, x
= per_milli
/50, y
= 20-x
;
7725 seq_printf(seq
, "[");
7726 for (i
= 0; i
< x
; i
++)
7727 seq_printf(seq
, "=");
7728 seq_printf(seq
, ">");
7729 for (i
= 0; i
< y
; i
++)
7730 seq_printf(seq
, ".");
7731 seq_printf(seq
, "] ");
7733 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
7734 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
7736 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
7738 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
7739 "resync" : "recovery"))),
7740 per_milli
/10, per_milli
% 10,
7741 (unsigned long long) resync
/2,
7742 (unsigned long long) max_sectors
/2);
7745 * dt: time from mark until now
7746 * db: blocks written from mark until now
7747 * rt: remaining time
7749 * rt is a sector_t, so could be 32bit or 64bit.
7750 * So we divide before multiply in case it is 32bit and close
7752 * We scale the divisor (db) by 32 to avoid losing precision
7753 * near the end of resync when the number of remaining sectors
7755 * We then divide rt by 32 after multiplying by db to compensate.
7756 * The '+1' avoids division by zero if db is very small.
7758 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
7760 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
7761 - mddev
->resync_mark_cnt
;
7763 rt
= max_sectors
- resync
; /* number of remaining sectors */
7764 sector_div(rt
, db
/32+1);
7768 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
7769 ((unsigned long)rt
% 60)/6);
7771 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
7775 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
7777 struct list_head
*tmp
;
7779 struct mddev
*mddev
;
7787 spin_lock(&all_mddevs_lock
);
7788 list_for_each(tmp
,&all_mddevs
)
7790 mddev
= list_entry(tmp
, struct mddev
, all_mddevs
);
7792 spin_unlock(&all_mddevs_lock
);
7795 spin_unlock(&all_mddevs_lock
);
7797 return (void*)2;/* tail */
7801 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
7803 struct list_head
*tmp
;
7804 struct mddev
*next_mddev
, *mddev
= v
;
7810 spin_lock(&all_mddevs_lock
);
7812 tmp
= all_mddevs
.next
;
7814 tmp
= mddev
->all_mddevs
.next
;
7815 if (tmp
!= &all_mddevs
)
7816 next_mddev
= mddev_get(list_entry(tmp
,struct mddev
,all_mddevs
));
7818 next_mddev
= (void*)2;
7821 spin_unlock(&all_mddevs_lock
);
7829 static void md_seq_stop(struct seq_file
*seq
, void *v
)
7831 struct mddev
*mddev
= v
;
7833 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
7837 static int md_seq_show(struct seq_file
*seq
, void *v
)
7839 struct mddev
*mddev
= v
;
7841 struct md_rdev
*rdev
;
7843 if (v
== (void*)1) {
7844 struct md_personality
*pers
;
7845 seq_printf(seq
, "Personalities : ");
7846 spin_lock(&pers_lock
);
7847 list_for_each_entry(pers
, &pers_list
, list
)
7848 seq_printf(seq
, "[%s] ", pers
->name
);
7850 spin_unlock(&pers_lock
);
7851 seq_printf(seq
, "\n");
7852 seq
->poll_event
= atomic_read(&md_event_count
);
7855 if (v
== (void*)2) {
7860 spin_lock(&mddev
->lock
);
7861 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
7862 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
7863 mddev
->pers
? "" : "in");
7866 seq_printf(seq
, " (read-only)");
7868 seq_printf(seq
, " (auto-read-only)");
7869 seq_printf(seq
, " %s", mddev
->pers
->name
);
7874 rdev_for_each_rcu(rdev
, mddev
) {
7875 char b
[BDEVNAME_SIZE
];
7876 seq_printf(seq
, " %s[%d]",
7877 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
7878 if (test_bit(WriteMostly
, &rdev
->flags
))
7879 seq_printf(seq
, "(W)");
7880 if (test_bit(Journal
, &rdev
->flags
))
7881 seq_printf(seq
, "(J)");
7882 if (test_bit(Faulty
, &rdev
->flags
)) {
7883 seq_printf(seq
, "(F)");
7886 if (rdev
->raid_disk
< 0)
7887 seq_printf(seq
, "(S)"); /* spare */
7888 if (test_bit(Replacement
, &rdev
->flags
))
7889 seq_printf(seq
, "(R)");
7890 sectors
+= rdev
->sectors
;
7894 if (!list_empty(&mddev
->disks
)) {
7896 seq_printf(seq
, "\n %llu blocks",
7897 (unsigned long long)
7898 mddev
->array_sectors
/ 2);
7900 seq_printf(seq
, "\n %llu blocks",
7901 (unsigned long long)sectors
/ 2);
7903 if (mddev
->persistent
) {
7904 if (mddev
->major_version
!= 0 ||
7905 mddev
->minor_version
!= 90) {
7906 seq_printf(seq
," super %d.%d",
7907 mddev
->major_version
,
7908 mddev
->minor_version
);
7910 } else if (mddev
->external
)
7911 seq_printf(seq
, " super external:%s",
7912 mddev
->metadata_type
);
7914 seq_printf(seq
, " super non-persistent");
7917 mddev
->pers
->status(seq
, mddev
);
7918 seq_printf(seq
, "\n ");
7919 if (mddev
->pers
->sync_request
) {
7920 if (status_resync(seq
, mddev
))
7921 seq_printf(seq
, "\n ");
7924 seq_printf(seq
, "\n ");
7926 md_bitmap_status(seq
, mddev
->bitmap
);
7928 seq_printf(seq
, "\n");
7930 spin_unlock(&mddev
->lock
);
7935 static const struct seq_operations md_seq_ops
= {
7936 .start
= md_seq_start
,
7937 .next
= md_seq_next
,
7938 .stop
= md_seq_stop
,
7939 .show
= md_seq_show
,
7942 static int md_seq_open(struct inode
*inode
, struct file
*file
)
7944 struct seq_file
*seq
;
7947 error
= seq_open(file
, &md_seq_ops
);
7951 seq
= file
->private_data
;
7952 seq
->poll_event
= atomic_read(&md_event_count
);
7956 static int md_unloading
;
7957 static __poll_t
mdstat_poll(struct file
*filp
, poll_table
*wait
)
7959 struct seq_file
*seq
= filp
->private_data
;
7963 return EPOLLIN
|EPOLLRDNORM
|EPOLLERR
|EPOLLPRI
;
7964 poll_wait(filp
, &md_event_waiters
, wait
);
7966 /* always allow read */
7967 mask
= EPOLLIN
| EPOLLRDNORM
;
7969 if (seq
->poll_event
!= atomic_read(&md_event_count
))
7970 mask
|= EPOLLERR
| EPOLLPRI
;
7974 static const struct file_operations md_seq_fops
= {
7975 .owner
= THIS_MODULE
,
7976 .open
= md_seq_open
,
7978 .llseek
= seq_lseek
,
7979 .release
= seq_release
,
7980 .poll
= mdstat_poll
,
7983 int register_md_personality(struct md_personality
*p
)
7985 pr_debug("md: %s personality registered for level %d\n",
7987 spin_lock(&pers_lock
);
7988 list_add_tail(&p
->list
, &pers_list
);
7989 spin_unlock(&pers_lock
);
7992 EXPORT_SYMBOL(register_md_personality
);
7994 int unregister_md_personality(struct md_personality
*p
)
7996 pr_debug("md: %s personality unregistered\n", p
->name
);
7997 spin_lock(&pers_lock
);
7998 list_del_init(&p
->list
);
7999 spin_unlock(&pers_lock
);
8002 EXPORT_SYMBOL(unregister_md_personality
);
8004 int register_md_cluster_operations(struct md_cluster_operations
*ops
,
8005 struct module
*module
)
8008 spin_lock(&pers_lock
);
8009 if (md_cluster_ops
!= NULL
)
8012 md_cluster_ops
= ops
;
8013 md_cluster_mod
= module
;
8015 spin_unlock(&pers_lock
);
8018 EXPORT_SYMBOL(register_md_cluster_operations
);
8020 int unregister_md_cluster_operations(void)
8022 spin_lock(&pers_lock
);
8023 md_cluster_ops
= NULL
;
8024 spin_unlock(&pers_lock
);
8027 EXPORT_SYMBOL(unregister_md_cluster_operations
);
8029 int md_setup_cluster(struct mddev
*mddev
, int nodes
)
8031 if (!md_cluster_ops
)
8032 request_module("md-cluster");
8033 spin_lock(&pers_lock
);
8034 /* ensure module won't be unloaded */
8035 if (!md_cluster_ops
|| !try_module_get(md_cluster_mod
)) {
8036 pr_warn("can't find md-cluster module or get it's reference.\n");
8037 spin_unlock(&pers_lock
);
8040 spin_unlock(&pers_lock
);
8042 return md_cluster_ops
->join(mddev
, nodes
);
8045 void md_cluster_stop(struct mddev
*mddev
)
8047 if (!md_cluster_ops
)
8049 md_cluster_ops
->leave(mddev
);
8050 module_put(md_cluster_mod
);
8053 static int is_mddev_idle(struct mddev
*mddev
, int init
)
8055 struct md_rdev
*rdev
;
8061 rdev_for_each_rcu(rdev
, mddev
) {
8062 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
8063 curr_events
= (int)part_stat_read_accum(&disk
->part0
, sectors
) -
8064 atomic_read(&disk
->sync_io
);
8065 /* sync IO will cause sync_io to increase before the disk_stats
8066 * as sync_io is counted when a request starts, and
8067 * disk_stats is counted when it completes.
8068 * So resync activity will cause curr_events to be smaller than
8069 * when there was no such activity.
8070 * non-sync IO will cause disk_stat to increase without
8071 * increasing sync_io so curr_events will (eventually)
8072 * be larger than it was before. Once it becomes
8073 * substantially larger, the test below will cause
8074 * the array to appear non-idle, and resync will slow
8076 * If there is a lot of outstanding resync activity when
8077 * we set last_event to curr_events, then all that activity
8078 * completing might cause the array to appear non-idle
8079 * and resync will be slowed down even though there might
8080 * not have been non-resync activity. This will only
8081 * happen once though. 'last_events' will soon reflect
8082 * the state where there is little or no outstanding
8083 * resync requests, and further resync activity will
8084 * always make curr_events less than last_events.
8087 if (init
|| curr_events
- rdev
->last_events
> 64) {
8088 rdev
->last_events
= curr_events
;
8096 void md_done_sync(struct mddev
*mddev
, int blocks
, int ok
)
8098 /* another "blocks" (512byte) blocks have been synced */
8099 atomic_sub(blocks
, &mddev
->recovery_active
);
8100 wake_up(&mddev
->recovery_wait
);
8102 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8103 set_bit(MD_RECOVERY_ERROR
, &mddev
->recovery
);
8104 md_wakeup_thread(mddev
->thread
);
8105 // stop recovery, signal do_sync ....
8108 EXPORT_SYMBOL(md_done_sync
);
8110 /* md_write_start(mddev, bi)
8111 * If we need to update some array metadata (e.g. 'active' flag
8112 * in superblock) before writing, schedule a superblock update
8113 * and wait for it to complete.
8114 * A return value of 'false' means that the write wasn't recorded
8115 * and cannot proceed as the array is being suspend.
8117 bool md_write_start(struct mddev
*mddev
, struct bio
*bi
)
8121 if (bio_data_dir(bi
) != WRITE
)
8124 BUG_ON(mddev
->ro
== 1);
8125 if (mddev
->ro
== 2) {
8126 /* need to switch to read/write */
8128 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8129 md_wakeup_thread(mddev
->thread
);
8130 md_wakeup_thread(mddev
->sync_thread
);
8134 percpu_ref_get(&mddev
->writes_pending
);
8135 smp_mb(); /* Match smp_mb in set_in_sync() */
8136 if (mddev
->safemode
== 1)
8137 mddev
->safemode
= 0;
8138 /* sync_checkers is always 0 when writes_pending is in per-cpu mode */
8139 if (mddev
->in_sync
|| mddev
->sync_checkers
) {
8140 spin_lock(&mddev
->lock
);
8141 if (mddev
->in_sync
) {
8143 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
8144 set_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
8145 md_wakeup_thread(mddev
->thread
);
8148 spin_unlock(&mddev
->lock
);
8152 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
8153 if (!mddev
->has_superblocks
)
8155 wait_event(mddev
->sb_wait
,
8156 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
) ||
8158 if (test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
)) {
8159 percpu_ref_put(&mddev
->writes_pending
);
8164 EXPORT_SYMBOL(md_write_start
);
8166 /* md_write_inc can only be called when md_write_start() has
8167 * already been called at least once of the current request.
8168 * It increments the counter and is useful when a single request
8169 * is split into several parts. Each part causes an increment and
8170 * so needs a matching md_write_end().
8171 * Unlike md_write_start(), it is safe to call md_write_inc() inside
8172 * a spinlocked region.
8174 void md_write_inc(struct mddev
*mddev
, struct bio
*bi
)
8176 if (bio_data_dir(bi
) != WRITE
)
8178 WARN_ON_ONCE(mddev
->in_sync
|| mddev
->ro
);
8179 percpu_ref_get(&mddev
->writes_pending
);
8181 EXPORT_SYMBOL(md_write_inc
);
8183 void md_write_end(struct mddev
*mddev
)
8185 percpu_ref_put(&mddev
->writes_pending
);
8187 if (mddev
->safemode
== 2)
8188 md_wakeup_thread(mddev
->thread
);
8189 else if (mddev
->safemode_delay
)
8190 /* The roundup() ensures this only performs locking once
8191 * every ->safemode_delay jiffies
8193 mod_timer(&mddev
->safemode_timer
,
8194 roundup(jiffies
, mddev
->safemode_delay
) +
8195 mddev
->safemode_delay
);
8198 EXPORT_SYMBOL(md_write_end
);
8200 /* md_allow_write(mddev)
8201 * Calling this ensures that the array is marked 'active' so that writes
8202 * may proceed without blocking. It is important to call this before
8203 * attempting a GFP_KERNEL allocation while holding the mddev lock.
8204 * Must be called with mddev_lock held.
8206 void md_allow_write(struct mddev
*mddev
)
8212 if (!mddev
->pers
->sync_request
)
8215 spin_lock(&mddev
->lock
);
8216 if (mddev
->in_sync
) {
8218 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
8219 set_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
8220 if (mddev
->safemode_delay
&&
8221 mddev
->safemode
== 0)
8222 mddev
->safemode
= 1;
8223 spin_unlock(&mddev
->lock
);
8224 md_update_sb(mddev
, 0);
8225 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
8226 /* wait for the dirty state to be recorded in the metadata */
8227 wait_event(mddev
->sb_wait
,
8228 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
8230 spin_unlock(&mddev
->lock
);
8232 EXPORT_SYMBOL_GPL(md_allow_write
);
8234 #define SYNC_MARKS 10
8235 #define SYNC_MARK_STEP (3*HZ)
8236 #define UPDATE_FREQUENCY (5*60*HZ)
8237 void md_do_sync(struct md_thread
*thread
)
8239 struct mddev
*mddev
= thread
->mddev
;
8240 struct mddev
*mddev2
;
8241 unsigned int currspeed
= 0,
8243 sector_t max_sectors
,j
, io_sectors
, recovery_done
;
8244 unsigned long mark
[SYNC_MARKS
];
8245 unsigned long update_time
;
8246 sector_t mark_cnt
[SYNC_MARKS
];
8248 struct list_head
*tmp
;
8249 sector_t last_check
;
8251 struct md_rdev
*rdev
;
8252 char *desc
, *action
= NULL
;
8253 struct blk_plug plug
;
8256 /* just incase thread restarts... */
8257 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
8258 test_bit(MD_RECOVERY_WAIT
, &mddev
->recovery
))
8260 if (mddev
->ro
) {/* never try to sync a read-only array */
8261 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8265 if (mddev_is_clustered(mddev
)) {
8266 ret
= md_cluster_ops
->resync_start(mddev
);
8270 set_bit(MD_CLUSTER_RESYNC_LOCKED
, &mddev
->flags
);
8271 if (!(test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
8272 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) ||
8273 test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
8274 && ((unsigned long long)mddev
->curr_resync_completed
8275 < (unsigned long long)mddev
->resync_max_sectors
))
8279 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
8280 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)) {
8281 desc
= "data-check";
8283 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
8284 desc
= "requested-resync";
8288 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
8293 mddev
->last_sync_action
= action
?: desc
;
8295 /* we overload curr_resync somewhat here.
8296 * 0 == not engaged in resync at all
8297 * 2 == checking that there is no conflict with another sync
8298 * 1 == like 2, but have yielded to allow conflicting resync to
8300 * other == active in resync - this many blocks
8302 * Before starting a resync we must have set curr_resync to
8303 * 2, and then checked that every "conflicting" array has curr_resync
8304 * less than ours. When we find one that is the same or higher
8305 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8306 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8307 * This will mean we have to start checking from the beginning again.
8312 int mddev2_minor
= -1;
8313 mddev
->curr_resync
= 2;
8316 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8318 for_each_mddev(mddev2
, tmp
) {
8319 if (mddev2
== mddev
)
8321 if (!mddev
->parallel_resync
8322 && mddev2
->curr_resync
8323 && match_mddev_units(mddev
, mddev2
)) {
8325 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
8326 /* arbitrarily yield */
8327 mddev
->curr_resync
= 1;
8328 wake_up(&resync_wait
);
8330 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
8331 /* no need to wait here, we can wait the next
8332 * time 'round when curr_resync == 2
8335 /* We need to wait 'interruptible' so as not to
8336 * contribute to the load average, and not to
8337 * be caught by 'softlockup'
8339 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
8340 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
8341 mddev2
->curr_resync
>= mddev
->curr_resync
) {
8342 if (mddev2_minor
!= mddev2
->md_minor
) {
8343 mddev2_minor
= mddev2
->md_minor
;
8344 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8345 desc
, mdname(mddev
),
8349 if (signal_pending(current
))
8350 flush_signals(current
);
8352 finish_wait(&resync_wait
, &wq
);
8355 finish_wait(&resync_wait
, &wq
);
8358 } while (mddev
->curr_resync
< 2);
8361 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
8362 /* resync follows the size requested by the personality,
8363 * which defaults to physical size, but can be virtual size
8365 max_sectors
= mddev
->resync_max_sectors
;
8366 atomic64_set(&mddev
->resync_mismatches
, 0);
8367 /* we don't use the checkpoint if there's a bitmap */
8368 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
8369 j
= mddev
->resync_min
;
8370 else if (!mddev
->bitmap
)
8371 j
= mddev
->recovery_cp
;
8373 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
8374 max_sectors
= mddev
->resync_max_sectors
;
8376 /* recovery follows the physical size of devices */
8377 max_sectors
= mddev
->dev_sectors
;
8380 rdev_for_each_rcu(rdev
, mddev
)
8381 if (rdev
->raid_disk
>= 0 &&
8382 !test_bit(Journal
, &rdev
->flags
) &&
8383 !test_bit(Faulty
, &rdev
->flags
) &&
8384 !test_bit(In_sync
, &rdev
->flags
) &&
8385 rdev
->recovery_offset
< j
)
8386 j
= rdev
->recovery_offset
;
8389 /* If there is a bitmap, we need to make sure all
8390 * writes that started before we added a spare
8391 * complete before we start doing a recovery.
8392 * Otherwise the write might complete and (via
8393 * bitmap_endwrite) set a bit in the bitmap after the
8394 * recovery has checked that bit and skipped that
8397 if (mddev
->bitmap
) {
8398 mddev
->pers
->quiesce(mddev
, 1);
8399 mddev
->pers
->quiesce(mddev
, 0);
8403 pr_info("md: %s of RAID array %s\n", desc
, mdname(mddev
));
8404 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev
));
8405 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8406 speed_max(mddev
), desc
);
8408 is_mddev_idle(mddev
, 1); /* this initializes IO event counters */
8411 for (m
= 0; m
< SYNC_MARKS
; m
++) {
8413 mark_cnt
[m
] = io_sectors
;
8416 mddev
->resync_mark
= mark
[last_mark
];
8417 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
8420 * Tune reconstruction:
8422 window
= 32*(PAGE_SIZE
/512);
8423 pr_debug("md: using %dk window, over a total of %lluk.\n",
8424 window
/2, (unsigned long long)max_sectors
/2);
8426 atomic_set(&mddev
->recovery_active
, 0);
8430 pr_debug("md: resuming %s of %s from checkpoint.\n",
8431 desc
, mdname(mddev
));
8432 mddev
->curr_resync
= j
;
8434 mddev
->curr_resync
= 3; /* no longer delayed */
8435 mddev
->curr_resync_completed
= j
;
8436 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
8437 md_new_event(mddev
);
8438 update_time
= jiffies
;
8440 blk_start_plug(&plug
);
8441 while (j
< max_sectors
) {
8446 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8447 ((mddev
->curr_resync
> mddev
->curr_resync_completed
&&
8448 (mddev
->curr_resync
- mddev
->curr_resync_completed
)
8449 > (max_sectors
>> 4)) ||
8450 time_after_eq(jiffies
, update_time
+ UPDATE_FREQUENCY
) ||
8451 (j
- mddev
->curr_resync_completed
)*2
8452 >= mddev
->resync_max
- mddev
->curr_resync_completed
||
8453 mddev
->curr_resync_completed
> mddev
->resync_max
8455 /* time to update curr_resync_completed */
8456 wait_event(mddev
->recovery_wait
,
8457 atomic_read(&mddev
->recovery_active
) == 0);
8458 mddev
->curr_resync_completed
= j
;
8459 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) &&
8460 j
> mddev
->recovery_cp
)
8461 mddev
->recovery_cp
= j
;
8462 update_time
= jiffies
;
8463 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
8464 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
8467 while (j
>= mddev
->resync_max
&&
8468 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
8469 /* As this condition is controlled by user-space,
8470 * we can block indefinitely, so use '_interruptible'
8471 * to avoid triggering warnings.
8473 flush_signals(current
); /* just in case */
8474 wait_event_interruptible(mddev
->recovery_wait
,
8475 mddev
->resync_max
> j
8476 || test_bit(MD_RECOVERY_INTR
,
8480 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8483 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
);
8485 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8489 if (!skipped
) { /* actual IO requested */
8490 io_sectors
+= sectors
;
8491 atomic_add(sectors
, &mddev
->recovery_active
);
8494 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8498 if (j
> max_sectors
)
8499 /* when skipping, extra large numbers can be returned. */
8502 mddev
->curr_resync
= j
;
8503 mddev
->curr_mark_cnt
= io_sectors
;
8504 if (last_check
== 0)
8505 /* this is the earliest that rebuild will be
8506 * visible in /proc/mdstat
8508 md_new_event(mddev
);
8510 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
8513 last_check
= io_sectors
;
8515 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
8517 int next
= (last_mark
+1) % SYNC_MARKS
;
8519 mddev
->resync_mark
= mark
[next
];
8520 mddev
->resync_mark_cnt
= mark_cnt
[next
];
8521 mark
[next
] = jiffies
;
8522 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
8526 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8530 * this loop exits only if either when we are slower than
8531 * the 'hard' speed limit, or the system was IO-idle for
8533 * the system might be non-idle CPU-wise, but we only care
8534 * about not overloading the IO subsystem. (things like an
8535 * e2fsck being done on the RAID array should execute fast)
8539 recovery_done
= io_sectors
- atomic_read(&mddev
->recovery_active
);
8540 currspeed
= ((unsigned long)(recovery_done
- mddev
->resync_mark_cnt
))/2
8541 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
8543 if (currspeed
> speed_min(mddev
)) {
8544 if (currspeed
> speed_max(mddev
)) {
8548 if (!is_mddev_idle(mddev
, 0)) {
8550 * Give other IO more of a chance.
8551 * The faster the devices, the less we wait.
8553 wait_event(mddev
->recovery_wait
,
8554 !atomic_read(&mddev
->recovery_active
));
8558 pr_info("md: %s: %s %s.\n",mdname(mddev
), desc
,
8559 test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)
8560 ? "interrupted" : "done");
8562 * this also signals 'finished resyncing' to md_stop
8564 blk_finish_plug(&plug
);
8565 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
8567 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8568 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
8569 mddev
->curr_resync
> 3) {
8570 mddev
->curr_resync_completed
= mddev
->curr_resync
;
8571 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
8573 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
);
8575 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
8576 mddev
->curr_resync
> 3) {
8577 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
8578 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
8579 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
8580 pr_debug("md: checkpointing %s of %s.\n",
8581 desc
, mdname(mddev
));
8582 if (test_bit(MD_RECOVERY_ERROR
,
8584 mddev
->recovery_cp
=
8585 mddev
->curr_resync_completed
;
8587 mddev
->recovery_cp
=
8591 mddev
->recovery_cp
= MaxSector
;
8593 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8594 mddev
->curr_resync
= MaxSector
;
8595 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8596 test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
)) {
8598 rdev_for_each_rcu(rdev
, mddev
)
8599 if (rdev
->raid_disk
>= 0 &&
8600 mddev
->delta_disks
>= 0 &&
8601 !test_bit(Journal
, &rdev
->flags
) &&
8602 !test_bit(Faulty
, &rdev
->flags
) &&
8603 !test_bit(In_sync
, &rdev
->flags
) &&
8604 rdev
->recovery_offset
< mddev
->curr_resync
)
8605 rdev
->recovery_offset
= mddev
->curr_resync
;
8611 /* set CHANGE_PENDING here since maybe another update is needed,
8612 * so other nodes are informed. It should be harmless for normal
8614 set_mask_bits(&mddev
->sb_flags
, 0,
8615 BIT(MD_SB_CHANGE_PENDING
) | BIT(MD_SB_CHANGE_DEVS
));
8617 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8618 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
8619 mddev
->delta_disks
> 0 &&
8620 mddev
->pers
->finish_reshape
&&
8621 mddev
->pers
->size
&&
8623 mddev_lock_nointr(mddev
);
8624 md_set_array_sectors(mddev
, mddev
->pers
->size(mddev
, 0, 0));
8625 mddev_unlock(mddev
);
8626 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
8627 revalidate_disk(mddev
->gendisk
);
8630 spin_lock(&mddev
->lock
);
8631 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
8632 /* We completed so min/max setting can be forgotten if used. */
8633 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
8634 mddev
->resync_min
= 0;
8635 mddev
->resync_max
= MaxSector
;
8636 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
8637 mddev
->resync_min
= mddev
->curr_resync_completed
;
8638 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
8639 mddev
->curr_resync
= 0;
8640 spin_unlock(&mddev
->lock
);
8642 wake_up(&resync_wait
);
8643 md_wakeup_thread(mddev
->thread
);
8646 EXPORT_SYMBOL_GPL(md_do_sync
);
8648 static int remove_and_add_spares(struct mddev
*mddev
,
8649 struct md_rdev
*this)
8651 struct md_rdev
*rdev
;
8654 bool remove_some
= false;
8656 if (this && test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
8657 /* Mustn't remove devices when resync thread is running */
8660 rdev_for_each(rdev
, mddev
) {
8661 if ((this == NULL
|| rdev
== this) &&
8662 rdev
->raid_disk
>= 0 &&
8663 !test_bit(Blocked
, &rdev
->flags
) &&
8664 test_bit(Faulty
, &rdev
->flags
) &&
8665 atomic_read(&rdev
->nr_pending
)==0) {
8666 /* Faulty non-Blocked devices with nr_pending == 0
8667 * never get nr_pending incremented,
8668 * never get Faulty cleared, and never get Blocked set.
8669 * So we can synchronize_rcu now rather than once per device
8672 set_bit(RemoveSynchronized
, &rdev
->flags
);
8678 rdev_for_each(rdev
, mddev
) {
8679 if ((this == NULL
|| rdev
== this) &&
8680 rdev
->raid_disk
>= 0 &&
8681 !test_bit(Blocked
, &rdev
->flags
) &&
8682 ((test_bit(RemoveSynchronized
, &rdev
->flags
) ||
8683 (!test_bit(In_sync
, &rdev
->flags
) &&
8684 !test_bit(Journal
, &rdev
->flags
))) &&
8685 atomic_read(&rdev
->nr_pending
)==0)) {
8686 if (mddev
->pers
->hot_remove_disk(
8687 mddev
, rdev
) == 0) {
8688 sysfs_unlink_rdev(mddev
, rdev
);
8689 rdev
->saved_raid_disk
= rdev
->raid_disk
;
8690 rdev
->raid_disk
= -1;
8694 if (remove_some
&& test_bit(RemoveSynchronized
, &rdev
->flags
))
8695 clear_bit(RemoveSynchronized
, &rdev
->flags
);
8698 if (removed
&& mddev
->kobj
.sd
)
8699 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
8701 if (this && removed
)
8704 rdev_for_each(rdev
, mddev
) {
8705 if (this && this != rdev
)
8707 if (test_bit(Candidate
, &rdev
->flags
))
8709 if (rdev
->raid_disk
>= 0 &&
8710 !test_bit(In_sync
, &rdev
->flags
) &&
8711 !test_bit(Journal
, &rdev
->flags
) &&
8712 !test_bit(Faulty
, &rdev
->flags
))
8714 if (rdev
->raid_disk
>= 0)
8716 if (test_bit(Faulty
, &rdev
->flags
))
8718 if (!test_bit(Journal
, &rdev
->flags
)) {
8720 ! (rdev
->saved_raid_disk
>= 0 &&
8721 !test_bit(Bitmap_sync
, &rdev
->flags
)))
8724 rdev
->recovery_offset
= 0;
8727 hot_add_disk(mddev
, rdev
) == 0) {
8728 if (sysfs_link_rdev(mddev
, rdev
))
8729 /* failure here is OK */;
8730 if (!test_bit(Journal
, &rdev
->flags
))
8732 md_new_event(mddev
);
8733 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
8738 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
8742 static void md_start_sync(struct work_struct
*ws
)
8744 struct mddev
*mddev
= container_of(ws
, struct mddev
, del_work
);
8746 mddev
->sync_thread
= md_register_thread(md_do_sync
,
8749 if (!mddev
->sync_thread
) {
8750 pr_warn("%s: could not start resync thread...\n",
8752 /* leave the spares where they are, it shouldn't hurt */
8753 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
8754 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
8755 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
8756 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
8757 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
8758 wake_up(&resync_wait
);
8759 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
8761 if (mddev
->sysfs_action
)
8762 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
8764 md_wakeup_thread(mddev
->sync_thread
);
8765 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
8766 md_new_event(mddev
);
8770 * This routine is regularly called by all per-raid-array threads to
8771 * deal with generic issues like resync and super-block update.
8772 * Raid personalities that don't have a thread (linear/raid0) do not
8773 * need this as they never do any recovery or update the superblock.
8775 * It does not do any resync itself, but rather "forks" off other threads
8776 * to do that as needed.
8777 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8778 * "->recovery" and create a thread at ->sync_thread.
8779 * When the thread finishes it sets MD_RECOVERY_DONE
8780 * and wakeups up this thread which will reap the thread and finish up.
8781 * This thread also removes any faulty devices (with nr_pending == 0).
8783 * The overall approach is:
8784 * 1/ if the superblock needs updating, update it.
8785 * 2/ If a recovery thread is running, don't do anything else.
8786 * 3/ If recovery has finished, clean up, possibly marking spares active.
8787 * 4/ If there are any faulty devices, remove them.
8788 * 5/ If array is degraded, try to add spares devices
8789 * 6/ If array has spares or is not in-sync, start a resync thread.
8791 void md_check_recovery(struct mddev
*mddev
)
8793 if (mddev
->suspended
)
8797 md_bitmap_daemon_work(mddev
);
8799 if (signal_pending(current
)) {
8800 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
8801 pr_debug("md: %s in immediate safe mode\n",
8803 mddev
->safemode
= 2;
8805 flush_signals(current
);
8808 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
8811 (mddev
->sb_flags
& ~ (1<<MD_SB_CHANGE_PENDING
)) ||
8812 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
8813 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
8814 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
8815 (mddev
->safemode
== 2
8816 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
8820 if (mddev_trylock(mddev
)) {
8823 if (!mddev
->external
&& mddev
->safemode
== 1)
8824 mddev
->safemode
= 0;
8827 struct md_rdev
*rdev
;
8828 if (!mddev
->external
&& mddev
->in_sync
)
8829 /* 'Blocked' flag not needed as failed devices
8830 * will be recorded if array switched to read/write.
8831 * Leaving it set will prevent the device
8832 * from being removed.
8834 rdev_for_each(rdev
, mddev
)
8835 clear_bit(Blocked
, &rdev
->flags
);
8836 /* On a read-only array we can:
8837 * - remove failed devices
8838 * - add already-in_sync devices if the array itself
8840 * As we only add devices that are already in-sync,
8841 * we can activate the spares immediately.
8843 remove_and_add_spares(mddev
, NULL
);
8844 /* There is no thread, but we need to call
8845 * ->spare_active and clear saved_raid_disk
8847 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8848 md_reap_sync_thread(mddev
);
8849 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8850 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8851 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
8855 if (mddev_is_clustered(mddev
)) {
8856 struct md_rdev
*rdev
;
8857 /* kick the device if another node issued a
8860 rdev_for_each(rdev
, mddev
) {
8861 if (test_and_clear_bit(ClusterRemove
, &rdev
->flags
) &&
8862 rdev
->raid_disk
< 0)
8863 md_kick_rdev_from_array(rdev
);
8867 if (!mddev
->external
&& !mddev
->in_sync
) {
8868 spin_lock(&mddev
->lock
);
8870 spin_unlock(&mddev
->lock
);
8873 if (mddev
->sb_flags
)
8874 md_update_sb(mddev
, 0);
8876 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
8877 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
8878 /* resync/recovery still happening */
8879 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8882 if (mddev
->sync_thread
) {
8883 md_reap_sync_thread(mddev
);
8886 /* Set RUNNING before clearing NEEDED to avoid
8887 * any transients in the value of "sync_action".
8889 mddev
->curr_resync_completed
= 0;
8890 spin_lock(&mddev
->lock
);
8891 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
8892 spin_unlock(&mddev
->lock
);
8893 /* Clear some bits that don't mean anything, but
8896 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8897 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
8899 if (!test_and_clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
8900 test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
8902 /* no recovery is running.
8903 * remove any failed drives, then
8904 * add spares if possible.
8905 * Spares are also removed and re-added, to allow
8906 * the personality to fail the re-add.
8909 if (mddev
->reshape_position
!= MaxSector
) {
8910 if (mddev
->pers
->check_reshape
== NULL
||
8911 mddev
->pers
->check_reshape(mddev
) != 0)
8912 /* Cannot proceed */
8914 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
8915 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8916 } else if ((spares
= remove_and_add_spares(mddev
, NULL
))) {
8917 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
8918 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
8919 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
8920 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8921 } else if (mddev
->recovery_cp
< MaxSector
) {
8922 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
8923 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8924 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
8925 /* nothing to be done ... */
8928 if (mddev
->pers
->sync_request
) {
8930 /* We are adding a device or devices to an array
8931 * which has the bitmap stored on all devices.
8932 * So make sure all bitmap pages get written
8934 md_bitmap_write_all(mddev
->bitmap
);
8936 INIT_WORK(&mddev
->del_work
, md_start_sync
);
8937 queue_work(md_misc_wq
, &mddev
->del_work
);
8941 if (!mddev
->sync_thread
) {
8942 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
8943 wake_up(&resync_wait
);
8944 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
8946 if (mddev
->sysfs_action
)
8947 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
8950 wake_up(&mddev
->sb_wait
);
8951 mddev_unlock(mddev
);
8952 } else if (test_bit(MD_ALLOW_SB_UPDATE
, &mddev
->flags
) && mddev
->sb_flags
) {
8953 /* Write superblock - thread that called mddev_suspend()
8954 * holds reconfig_mutex for us.
8956 set_bit(MD_UPDATING_SB
, &mddev
->flags
);
8957 smp_mb__after_atomic();
8958 if (test_bit(MD_ALLOW_SB_UPDATE
, &mddev
->flags
))
8959 md_update_sb(mddev
, 0);
8960 clear_bit_unlock(MD_UPDATING_SB
, &mddev
->flags
);
8961 wake_up(&mddev
->sb_wait
);
8964 EXPORT_SYMBOL(md_check_recovery
);
8966 void md_reap_sync_thread(struct mddev
*mddev
)
8968 struct md_rdev
*rdev
;
8970 /* resync has finished, collect result */
8971 md_unregister_thread(&mddev
->sync_thread
);
8972 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
8973 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
8975 /* activate any spares */
8976 if (mddev
->pers
->spare_active(mddev
)) {
8977 sysfs_notify(&mddev
->kobj
, NULL
,
8979 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
8982 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8983 mddev
->pers
->finish_reshape
)
8984 mddev
->pers
->finish_reshape(mddev
);
8986 /* If array is no-longer degraded, then any saved_raid_disk
8987 * information must be scrapped.
8989 if (!mddev
->degraded
)
8990 rdev_for_each(rdev
, mddev
)
8991 rdev
->saved_raid_disk
= -1;
8993 md_update_sb(mddev
, 1);
8994 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
8995 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
8997 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED
, &mddev
->flags
))
8998 md_cluster_ops
->resync_finish(mddev
);
8999 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
9000 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
9001 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
9002 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
9003 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
9004 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
9005 wake_up(&resync_wait
);
9006 /* flag recovery needed just to double check */
9007 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
9008 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
9009 md_new_event(mddev
);
9010 if (mddev
->event_work
.func
)
9011 queue_work(md_misc_wq
, &mddev
->event_work
);
9013 EXPORT_SYMBOL(md_reap_sync_thread
);
9015 void md_wait_for_blocked_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
9017 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
9018 wait_event_timeout(rdev
->blocked_wait
,
9019 !test_bit(Blocked
, &rdev
->flags
) &&
9020 !test_bit(BlockedBadBlocks
, &rdev
->flags
),
9021 msecs_to_jiffies(5000));
9022 rdev_dec_pending(rdev
, mddev
);
9024 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
9026 void md_finish_reshape(struct mddev
*mddev
)
9028 /* called be personality module when reshape completes. */
9029 struct md_rdev
*rdev
;
9031 rdev_for_each(rdev
, mddev
) {
9032 if (rdev
->data_offset
> rdev
->new_data_offset
)
9033 rdev
->sectors
+= rdev
->data_offset
- rdev
->new_data_offset
;
9035 rdev
->sectors
-= rdev
->new_data_offset
- rdev
->data_offset
;
9036 rdev
->data_offset
= rdev
->new_data_offset
;
9039 EXPORT_SYMBOL(md_finish_reshape
);
9041 /* Bad block management */
9043 /* Returns 1 on success, 0 on failure */
9044 int rdev_set_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
,
9047 struct mddev
*mddev
= rdev
->mddev
;
9050 s
+= rdev
->new_data_offset
;
9052 s
+= rdev
->data_offset
;
9053 rv
= badblocks_set(&rdev
->badblocks
, s
, sectors
, 0);
9055 /* Make sure they get written out promptly */
9056 if (test_bit(ExternalBbl
, &rdev
->flags
))
9057 sysfs_notify(&rdev
->kobj
, NULL
,
9058 "unacknowledged_bad_blocks");
9059 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
9060 set_mask_bits(&mddev
->sb_flags
, 0,
9061 BIT(MD_SB_CHANGE_CLEAN
) | BIT(MD_SB_CHANGE_PENDING
));
9062 md_wakeup_thread(rdev
->mddev
->thread
);
9067 EXPORT_SYMBOL_GPL(rdev_set_badblocks
);
9069 int rdev_clear_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
,
9074 s
+= rdev
->new_data_offset
;
9076 s
+= rdev
->data_offset
;
9077 rv
= badblocks_clear(&rdev
->badblocks
, s
, sectors
);
9078 if ((rv
== 0) && test_bit(ExternalBbl
, &rdev
->flags
))
9079 sysfs_notify(&rdev
->kobj
, NULL
, "bad_blocks");
9082 EXPORT_SYMBOL_GPL(rdev_clear_badblocks
);
9084 static int md_notify_reboot(struct notifier_block
*this,
9085 unsigned long code
, void *x
)
9087 struct list_head
*tmp
;
9088 struct mddev
*mddev
;
9091 for_each_mddev(mddev
, tmp
) {
9092 if (mddev_trylock(mddev
)) {
9094 __md_stop_writes(mddev
);
9095 if (mddev
->persistent
)
9096 mddev
->safemode
= 2;
9097 mddev_unlock(mddev
);
9102 * certain more exotic SCSI devices are known to be
9103 * volatile wrt too early system reboots. While the
9104 * right place to handle this issue is the given
9105 * driver, we do want to have a safe RAID driver ...
9113 static struct notifier_block md_notifier
= {
9114 .notifier_call
= md_notify_reboot
,
9116 .priority
= INT_MAX
, /* before any real devices */
9119 static void md_geninit(void)
9121 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
9123 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
9126 static int __init
md_init(void)
9130 md_wq
= alloc_workqueue("md", WQ_MEM_RECLAIM
, 0);
9134 md_misc_wq
= alloc_workqueue("md_misc", 0, 0);
9138 if ((ret
= register_blkdev(MD_MAJOR
, "md")) < 0)
9141 if ((ret
= register_blkdev(0, "mdp")) < 0)
9145 blk_register_region(MKDEV(MD_MAJOR
, 0), 512, THIS_MODULE
,
9146 md_probe
, NULL
, NULL
);
9147 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
9148 md_probe
, NULL
, NULL
);
9150 register_reboot_notifier(&md_notifier
);
9151 raid_table_header
= register_sysctl_table(raid_root_table
);
9157 unregister_blkdev(MD_MAJOR
, "md");
9159 destroy_workqueue(md_misc_wq
);
9161 destroy_workqueue(md_wq
);
9166 static void check_sb_changes(struct mddev
*mddev
, struct md_rdev
*rdev
)
9168 struct mdp_superblock_1
*sb
= page_address(rdev
->sb_page
);
9169 struct md_rdev
*rdev2
;
9171 char b
[BDEVNAME_SIZE
];
9174 * If size is changed in another node then we need to
9175 * do resize as well.
9177 if (mddev
->dev_sectors
!= le64_to_cpu(sb
->size
)) {
9178 ret
= mddev
->pers
->resize(mddev
, le64_to_cpu(sb
->size
));
9180 pr_info("md-cluster: resize failed\n");
9182 md_bitmap_update_sb(mddev
->bitmap
);
9185 /* Check for change of roles in the active devices */
9186 rdev_for_each(rdev2
, mddev
) {
9187 if (test_bit(Faulty
, &rdev2
->flags
))
9190 /* Check if the roles changed */
9191 role
= le16_to_cpu(sb
->dev_roles
[rdev2
->desc_nr
]);
9193 if (test_bit(Candidate
, &rdev2
->flags
)) {
9194 if (role
== 0xfffe) {
9195 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2
->bdev
,b
));
9196 md_kick_rdev_from_array(rdev2
);
9200 clear_bit(Candidate
, &rdev2
->flags
);
9203 if (role
!= rdev2
->raid_disk
) {
9205 if (rdev2
->raid_disk
== -1 && role
!= 0xffff) {
9206 rdev2
->saved_raid_disk
= role
;
9207 ret
= remove_and_add_spares(mddev
, rdev2
);
9208 pr_info("Activated spare: %s\n",
9209 bdevname(rdev2
->bdev
,b
));
9210 /* wakeup mddev->thread here, so array could
9211 * perform resync with the new activated disk */
9212 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
9213 md_wakeup_thread(mddev
->thread
);
9217 * We just want to do the minimum to mark the disk
9218 * as faulty. The recovery is performed by the
9219 * one who initiated the error.
9221 if ((role
== 0xfffe) || (role
== 0xfffd)) {
9222 md_error(mddev
, rdev2
);
9223 clear_bit(Blocked
, &rdev2
->flags
);
9228 if (mddev
->raid_disks
!= le32_to_cpu(sb
->raid_disks
))
9229 update_raid_disks(mddev
, le32_to_cpu(sb
->raid_disks
));
9231 /* Finally set the event to be up to date */
9232 mddev
->events
= le64_to_cpu(sb
->events
);
9235 static int read_rdev(struct mddev
*mddev
, struct md_rdev
*rdev
)
9238 struct page
*swapout
= rdev
->sb_page
;
9239 struct mdp_superblock_1
*sb
;
9241 /* Store the sb page of the rdev in the swapout temporary
9242 * variable in case we err in the future
9244 rdev
->sb_page
= NULL
;
9245 err
= alloc_disk_sb(rdev
);
9247 ClearPageUptodate(rdev
->sb_page
);
9248 rdev
->sb_loaded
= 0;
9249 err
= super_types
[mddev
->major_version
].
9250 load_super(rdev
, NULL
, mddev
->minor_version
);
9253 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9254 __func__
, __LINE__
, rdev
->desc_nr
, err
);
9256 put_page(rdev
->sb_page
);
9257 rdev
->sb_page
= swapout
;
9258 rdev
->sb_loaded
= 1;
9262 sb
= page_address(rdev
->sb_page
);
9263 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9267 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RECOVERY_OFFSET
))
9268 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
9270 /* The other node finished recovery, call spare_active to set
9271 * device In_sync and mddev->degraded
9273 if (rdev
->recovery_offset
== MaxSector
&&
9274 !test_bit(In_sync
, &rdev
->flags
) &&
9275 mddev
->pers
->spare_active(mddev
))
9276 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
9282 void md_reload_sb(struct mddev
*mddev
, int nr
)
9284 struct md_rdev
*rdev
;
9288 rdev_for_each_rcu(rdev
, mddev
) {
9289 if (rdev
->desc_nr
== nr
)
9293 if (!rdev
|| rdev
->desc_nr
!= nr
) {
9294 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__
, __LINE__
, nr
);
9298 err
= read_rdev(mddev
, rdev
);
9302 check_sb_changes(mddev
, rdev
);
9304 /* Read all rdev's to update recovery_offset */
9305 rdev_for_each_rcu(rdev
, mddev
) {
9306 if (!test_bit(Faulty
, &rdev
->flags
))
9307 read_rdev(mddev
, rdev
);
9310 EXPORT_SYMBOL(md_reload_sb
);
9315 * Searches all registered partitions for autorun RAID arrays
9319 static DEFINE_MUTEX(detected_devices_mutex
);
9320 static LIST_HEAD(all_detected_devices
);
9321 struct detected_devices_node
{
9322 struct list_head list
;
9326 void md_autodetect_dev(dev_t dev
)
9328 struct detected_devices_node
*node_detected_dev
;
9330 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
9331 if (node_detected_dev
) {
9332 node_detected_dev
->dev
= dev
;
9333 mutex_lock(&detected_devices_mutex
);
9334 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
9335 mutex_unlock(&detected_devices_mutex
);
9339 static void autostart_arrays(int part
)
9341 struct md_rdev
*rdev
;
9342 struct detected_devices_node
*node_detected_dev
;
9344 int i_scanned
, i_passed
;
9349 pr_info("md: Autodetecting RAID arrays.\n");
9351 mutex_lock(&detected_devices_mutex
);
9352 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
9354 node_detected_dev
= list_entry(all_detected_devices
.next
,
9355 struct detected_devices_node
, list
);
9356 list_del(&node_detected_dev
->list
);
9357 dev
= node_detected_dev
->dev
;
9358 kfree(node_detected_dev
);
9359 mutex_unlock(&detected_devices_mutex
);
9360 rdev
= md_import_device(dev
,0, 90);
9361 mutex_lock(&detected_devices_mutex
);
9365 if (test_bit(Faulty
, &rdev
->flags
))
9368 set_bit(AutoDetected
, &rdev
->flags
);
9369 list_add(&rdev
->same_set
, &pending_raid_disks
);
9372 mutex_unlock(&detected_devices_mutex
);
9374 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned
, i_passed
);
9376 autorun_devices(part
);
9379 #endif /* !MODULE */
9381 static __exit
void md_exit(void)
9383 struct mddev
*mddev
;
9384 struct list_head
*tmp
;
9387 blk_unregister_region(MKDEV(MD_MAJOR
,0), 512);
9388 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
9390 unregister_blkdev(MD_MAJOR
,"md");
9391 unregister_blkdev(mdp_major
, "mdp");
9392 unregister_reboot_notifier(&md_notifier
);
9393 unregister_sysctl_table(raid_table_header
);
9395 /* We cannot unload the modules while some process is
9396 * waiting for us in select() or poll() - wake them up
9399 while (waitqueue_active(&md_event_waiters
)) {
9400 /* not safe to leave yet */
9401 wake_up(&md_event_waiters
);
9405 remove_proc_entry("mdstat", NULL
);
9407 for_each_mddev(mddev
, tmp
) {
9408 export_array(mddev
);
9410 mddev
->hold_active
= 0;
9412 * for_each_mddev() will call mddev_put() at the end of each
9413 * iteration. As the mddev is now fully clear, this will
9414 * schedule the mddev for destruction by a workqueue, and the
9415 * destroy_workqueue() below will wait for that to complete.
9418 destroy_workqueue(md_misc_wq
);
9419 destroy_workqueue(md_wq
);
9422 subsys_initcall(md_init
);
9423 module_exit(md_exit
)
9425 static int get_ro(char *buffer
, const struct kernel_param
*kp
)
9427 return sprintf(buffer
, "%d", start_readonly
);
9429 static int set_ro(const char *val
, const struct kernel_param
*kp
)
9431 return kstrtouint(val
, 10, (unsigned int *)&start_readonly
);
9434 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
9435 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
9436 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
9437 module_param(create_on_open
, bool, S_IRUSR
|S_IWUSR
);
9439 MODULE_LICENSE("GPL");
9440 MODULE_DESCRIPTION("MD RAID framework");
9442 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);