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;
208 * like bio_clone_bioset, but with a local bio set
211 struct bio
*bio_alloc_mddev(gfp_t gfp_mask
, int nr_iovecs
,
216 if (!mddev
|| !bioset_initialized(&mddev
->bio_set
))
217 return bio_alloc(gfp_mask
, nr_iovecs
);
219 b
= bio_alloc_bioset(gfp_mask
, nr_iovecs
, &mddev
->bio_set
);
224 EXPORT_SYMBOL_GPL(bio_alloc_mddev
);
226 static struct bio
*md_bio_alloc_sync(struct mddev
*mddev
)
228 if (!mddev
|| !bioset_initialized(&mddev
->sync_set
))
229 return bio_alloc(GFP_NOIO
, 1);
231 return bio_alloc_bioset(GFP_NOIO
, 1, &mddev
->sync_set
);
235 * We have a system wide 'event count' that is incremented
236 * on any 'interesting' event, and readers of /proc/mdstat
237 * can use 'poll' or 'select' to find out when the event
241 * start array, stop array, error, add device, remove device,
242 * start build, activate spare
244 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters
);
245 static atomic_t md_event_count
;
246 void md_new_event(struct mddev
*mddev
)
248 atomic_inc(&md_event_count
);
249 wake_up(&md_event_waiters
);
251 EXPORT_SYMBOL_GPL(md_new_event
);
254 * Enables to iterate over all existing md arrays
255 * all_mddevs_lock protects this list.
257 static LIST_HEAD(all_mddevs
);
258 static DEFINE_SPINLOCK(all_mddevs_lock
);
261 * iterates through all used mddevs in the system.
262 * We take care to grab the all_mddevs_lock whenever navigating
263 * the list, and to always hold a refcount when unlocked.
264 * Any code which breaks out of this loop while own
265 * a reference to the current mddev and must mddev_put it.
267 #define for_each_mddev(_mddev,_tmp) \
269 for (({ spin_lock(&all_mddevs_lock); \
270 _tmp = all_mddevs.next; \
272 ({ if (_tmp != &all_mddevs) \
273 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
274 spin_unlock(&all_mddevs_lock); \
275 if (_mddev) mddev_put(_mddev); \
276 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
277 _tmp != &all_mddevs;}); \
278 ({ spin_lock(&all_mddevs_lock); \
279 _tmp = _tmp->next;}) \
282 /* Rather than calling directly into the personality make_request function,
283 * IO requests come here first so that we can check if the device is
284 * being suspended pending a reconfiguration.
285 * We hold a refcount over the call to ->make_request. By the time that
286 * call has finished, the bio has been linked into some internal structure
287 * and so is visible to ->quiesce(), so we don't need the refcount any more.
289 static bool is_suspended(struct mddev
*mddev
, struct bio
*bio
)
291 if (mddev
->suspended
)
293 if (bio_data_dir(bio
) != WRITE
)
295 if (mddev
->suspend_lo
>= mddev
->suspend_hi
)
297 if (bio
->bi_iter
.bi_sector
>= mddev
->suspend_hi
)
299 if (bio_end_sector(bio
) < mddev
->suspend_lo
)
304 void md_handle_request(struct mddev
*mddev
, struct bio
*bio
)
308 if (is_suspended(mddev
, bio
)) {
311 prepare_to_wait(&mddev
->sb_wait
, &__wait
,
312 TASK_UNINTERRUPTIBLE
);
313 if (!is_suspended(mddev
, bio
))
319 finish_wait(&mddev
->sb_wait
, &__wait
);
321 atomic_inc(&mddev
->active_io
);
324 if (!mddev
->pers
->make_request(mddev
, bio
)) {
325 atomic_dec(&mddev
->active_io
);
326 wake_up(&mddev
->sb_wait
);
327 goto check_suspended
;
330 if (atomic_dec_and_test(&mddev
->active_io
) && mddev
->suspended
)
331 wake_up(&mddev
->sb_wait
);
333 EXPORT_SYMBOL(md_handle_request
);
335 static blk_qc_t
md_make_request(struct request_queue
*q
, struct bio
*bio
)
337 const int rw
= bio_data_dir(bio
);
338 struct mddev
*mddev
= q
->queuedata
;
339 unsigned int sectors
;
342 blk_queue_split(q
, &bio
);
344 if (mddev
== NULL
|| mddev
->pers
== NULL
) {
346 return BLK_QC_T_NONE
;
348 if (mddev
->ro
== 1 && unlikely(rw
== WRITE
)) {
349 if (bio_sectors(bio
) != 0)
350 bio
->bi_status
= BLK_STS_IOERR
;
352 return BLK_QC_T_NONE
;
356 * save the sectors now since our bio can
357 * go away inside make_request
359 sectors
= bio_sectors(bio
);
360 /* bio could be mergeable after passing to underlayer */
361 bio
->bi_opf
&= ~REQ_NOMERGE
;
363 md_handle_request(mddev
, bio
);
365 cpu
= part_stat_lock();
366 part_stat_inc(cpu
, &mddev
->gendisk
->part0
, ios
[rw
]);
367 part_stat_add(cpu
, &mddev
->gendisk
->part0
, sectors
[rw
], sectors
);
370 return BLK_QC_T_NONE
;
373 /* mddev_suspend makes sure no new requests are submitted
374 * to the device, and that any requests that have been submitted
375 * are completely handled.
376 * Once mddev_detach() is called and completes, the module will be
379 void mddev_suspend(struct mddev
*mddev
)
381 WARN_ON_ONCE(mddev
->thread
&& current
== mddev
->thread
->tsk
);
382 lockdep_assert_held(&mddev
->reconfig_mutex
);
383 if (mddev
->suspended
++)
386 wake_up(&mddev
->sb_wait
);
387 set_bit(MD_ALLOW_SB_UPDATE
, &mddev
->flags
);
388 smp_mb__after_atomic();
389 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->active_io
) == 0);
390 mddev
->pers
->quiesce(mddev
, 1);
391 clear_bit_unlock(MD_ALLOW_SB_UPDATE
, &mddev
->flags
);
392 wait_event(mddev
->sb_wait
, !test_bit(MD_UPDATING_SB
, &mddev
->flags
));
394 del_timer_sync(&mddev
->safemode_timer
);
396 EXPORT_SYMBOL_GPL(mddev_suspend
);
398 void mddev_resume(struct mddev
*mddev
)
400 lockdep_assert_held(&mddev
->reconfig_mutex
);
401 if (--mddev
->suspended
)
403 wake_up(&mddev
->sb_wait
);
404 mddev
->pers
->quiesce(mddev
, 0);
406 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
407 md_wakeup_thread(mddev
->thread
);
408 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
410 EXPORT_SYMBOL_GPL(mddev_resume
);
412 int mddev_congested(struct mddev
*mddev
, int bits
)
414 struct md_personality
*pers
= mddev
->pers
;
418 if (mddev
->suspended
)
420 else if (pers
&& pers
->congested
)
421 ret
= pers
->congested(mddev
, bits
);
425 EXPORT_SYMBOL_GPL(mddev_congested
);
426 static int md_congested(void *data
, int bits
)
428 struct mddev
*mddev
= data
;
429 return mddev_congested(mddev
, bits
);
433 * Generic flush handling for md
435 static void submit_flushes(struct work_struct
*ws
)
437 struct flush_info
*fi
= container_of(ws
, struct flush_info
, flush_work
);
438 struct mddev
*mddev
= fi
->mddev
;
439 struct bio
*bio
= fi
->bio
;
441 bio
->bi_opf
&= ~REQ_PREFLUSH
;
442 md_handle_request(mddev
, bio
);
444 mempool_free(fi
, mddev
->flush_pool
);
447 static void md_end_flush(struct bio
*fbio
)
449 struct flush_bio
*fb
= fbio
->bi_private
;
450 struct md_rdev
*rdev
= fb
->rdev
;
451 struct flush_info
*fi
= fb
->fi
;
452 struct bio
*bio
= fi
->bio
;
453 struct mddev
*mddev
= fi
->mddev
;
455 rdev_dec_pending(rdev
, mddev
);
457 if (atomic_dec_and_test(&fi
->flush_pending
)) {
458 if (bio
->bi_iter
.bi_size
== 0)
459 /* an empty barrier - all done */
462 INIT_WORK(&fi
->flush_work
, submit_flushes
);
463 queue_work(md_wq
, &fi
->flush_work
);
467 mempool_free(fb
, mddev
->flush_bio_pool
);
471 void md_flush_request(struct mddev
*mddev
, struct bio
*bio
)
473 struct md_rdev
*rdev
;
474 struct flush_info
*fi
;
476 fi
= mempool_alloc(mddev
->flush_pool
, GFP_NOIO
);
480 atomic_set(&fi
->flush_pending
, 1);
483 rdev_for_each_rcu(rdev
, mddev
)
484 if (rdev
->raid_disk
>= 0 &&
485 !test_bit(Faulty
, &rdev
->flags
)) {
486 /* Take two references, one is dropped
487 * when request finishes, one after
488 * we reclaim rcu_read_lock
491 struct flush_bio
*fb
;
492 atomic_inc(&rdev
->nr_pending
);
493 atomic_inc(&rdev
->nr_pending
);
496 fb
= mempool_alloc(mddev
->flush_bio_pool
, GFP_NOIO
);
500 bi
= bio_alloc_mddev(GFP_NOIO
, 0, mddev
);
501 bio_set_dev(bi
, rdev
->bdev
);
502 bi
->bi_end_io
= md_end_flush
;
504 bi
->bi_opf
= REQ_OP_WRITE
| REQ_PREFLUSH
;
506 atomic_inc(&fi
->flush_pending
);
510 rdev_dec_pending(rdev
, mddev
);
514 if (atomic_dec_and_test(&fi
->flush_pending
)) {
515 if (bio
->bi_iter
.bi_size
== 0)
516 /* an empty barrier - all done */
519 INIT_WORK(&fi
->flush_work
, submit_flushes
);
520 queue_work(md_wq
, &fi
->flush_work
);
524 EXPORT_SYMBOL(md_flush_request
);
526 static inline struct mddev
*mddev_get(struct mddev
*mddev
)
528 atomic_inc(&mddev
->active
);
532 static void mddev_delayed_delete(struct work_struct
*ws
);
534 static void mddev_put(struct mddev
*mddev
)
536 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
538 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
) &&
539 mddev
->ctime
== 0 && !mddev
->hold_active
) {
540 /* Array is not configured at all, and not held active,
542 list_del_init(&mddev
->all_mddevs
);
545 * Call queue_work inside the spinlock so that
546 * flush_workqueue() after mddev_find will succeed in waiting
547 * for the work to be done.
549 INIT_WORK(&mddev
->del_work
, mddev_delayed_delete
);
550 queue_work(md_misc_wq
, &mddev
->del_work
);
552 spin_unlock(&all_mddevs_lock
);
555 static void md_safemode_timeout(struct timer_list
*t
);
557 void mddev_init(struct mddev
*mddev
)
559 kobject_init(&mddev
->kobj
, &md_ktype
);
560 mutex_init(&mddev
->open_mutex
);
561 mutex_init(&mddev
->reconfig_mutex
);
562 mutex_init(&mddev
->bitmap_info
.mutex
);
563 INIT_LIST_HEAD(&mddev
->disks
);
564 INIT_LIST_HEAD(&mddev
->all_mddevs
);
565 timer_setup(&mddev
->safemode_timer
, md_safemode_timeout
, 0);
566 atomic_set(&mddev
->active
, 1);
567 atomic_set(&mddev
->openers
, 0);
568 atomic_set(&mddev
->active_io
, 0);
569 spin_lock_init(&mddev
->lock
);
570 init_waitqueue_head(&mddev
->sb_wait
);
571 init_waitqueue_head(&mddev
->recovery_wait
);
572 mddev
->reshape_position
= MaxSector
;
573 mddev
->reshape_backwards
= 0;
574 mddev
->last_sync_action
= "none";
575 mddev
->resync_min
= 0;
576 mddev
->resync_max
= MaxSector
;
577 mddev
->level
= LEVEL_NONE
;
579 EXPORT_SYMBOL_GPL(mddev_init
);
581 static struct mddev
*mddev_find(dev_t unit
)
583 struct mddev
*mddev
, *new = NULL
;
585 if (unit
&& MAJOR(unit
) != MD_MAJOR
)
586 unit
&= ~((1<<MdpMinorShift
)-1);
589 spin_lock(&all_mddevs_lock
);
592 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
593 if (mddev
->unit
== unit
) {
595 spin_unlock(&all_mddevs_lock
);
601 list_add(&new->all_mddevs
, &all_mddevs
);
602 spin_unlock(&all_mddevs_lock
);
603 new->hold_active
= UNTIL_IOCTL
;
607 /* find an unused unit number */
608 static int next_minor
= 512;
609 int start
= next_minor
;
613 dev
= MKDEV(MD_MAJOR
, next_minor
);
615 if (next_minor
> MINORMASK
)
617 if (next_minor
== start
) {
618 /* Oh dear, all in use. */
619 spin_unlock(&all_mddevs_lock
);
625 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
626 if (mddev
->unit
== dev
) {
632 new->md_minor
= MINOR(dev
);
633 new->hold_active
= UNTIL_STOP
;
634 list_add(&new->all_mddevs
, &all_mddevs
);
635 spin_unlock(&all_mddevs_lock
);
638 spin_unlock(&all_mddevs_lock
);
640 new = kzalloc(sizeof(*new), GFP_KERNEL
);
645 if (MAJOR(unit
) == MD_MAJOR
)
646 new->md_minor
= MINOR(unit
);
648 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
655 static struct attribute_group md_redundancy_group
;
657 void mddev_unlock(struct mddev
*mddev
)
659 if (mddev
->to_remove
) {
660 /* These cannot be removed under reconfig_mutex as
661 * an access to the files will try to take reconfig_mutex
662 * while holding the file unremovable, which leads to
664 * So hold set sysfs_active while the remove in happeing,
665 * and anything else which might set ->to_remove or my
666 * otherwise change the sysfs namespace will fail with
667 * -EBUSY if sysfs_active is still set.
668 * We set sysfs_active under reconfig_mutex and elsewhere
669 * test it under the same mutex to ensure its correct value
672 struct attribute_group
*to_remove
= mddev
->to_remove
;
673 mddev
->to_remove
= NULL
;
674 mddev
->sysfs_active
= 1;
675 mutex_unlock(&mddev
->reconfig_mutex
);
677 if (mddev
->kobj
.sd
) {
678 if (to_remove
!= &md_redundancy_group
)
679 sysfs_remove_group(&mddev
->kobj
, to_remove
);
680 if (mddev
->pers
== NULL
||
681 mddev
->pers
->sync_request
== NULL
) {
682 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
683 if (mddev
->sysfs_action
)
684 sysfs_put(mddev
->sysfs_action
);
685 mddev
->sysfs_action
= NULL
;
688 mddev
->sysfs_active
= 0;
690 mutex_unlock(&mddev
->reconfig_mutex
);
692 /* As we've dropped the mutex we need a spinlock to
693 * make sure the thread doesn't disappear
695 spin_lock(&pers_lock
);
696 md_wakeup_thread(mddev
->thread
);
697 wake_up(&mddev
->sb_wait
);
698 spin_unlock(&pers_lock
);
700 EXPORT_SYMBOL_GPL(mddev_unlock
);
702 struct md_rdev
*md_find_rdev_nr_rcu(struct mddev
*mddev
, int nr
)
704 struct md_rdev
*rdev
;
706 rdev_for_each_rcu(rdev
, mddev
)
707 if (rdev
->desc_nr
== nr
)
712 EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu
);
714 static struct md_rdev
*find_rdev(struct mddev
*mddev
, dev_t dev
)
716 struct md_rdev
*rdev
;
718 rdev_for_each(rdev
, mddev
)
719 if (rdev
->bdev
->bd_dev
== dev
)
725 struct md_rdev
*md_find_rdev_rcu(struct mddev
*mddev
, dev_t dev
)
727 struct md_rdev
*rdev
;
729 rdev_for_each_rcu(rdev
, mddev
)
730 if (rdev
->bdev
->bd_dev
== dev
)
735 EXPORT_SYMBOL_GPL(md_find_rdev_rcu
);
737 static struct md_personality
*find_pers(int level
, char *clevel
)
739 struct md_personality
*pers
;
740 list_for_each_entry(pers
, &pers_list
, list
) {
741 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
743 if (strcmp(pers
->name
, clevel
)==0)
749 /* return the offset of the super block in 512byte sectors */
750 static inline sector_t
calc_dev_sboffset(struct md_rdev
*rdev
)
752 sector_t num_sectors
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
753 return MD_NEW_SIZE_SECTORS(num_sectors
);
756 static int alloc_disk_sb(struct md_rdev
*rdev
)
758 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
764 void md_rdev_clear(struct md_rdev
*rdev
)
767 put_page(rdev
->sb_page
);
769 rdev
->sb_page
= NULL
;
774 put_page(rdev
->bb_page
);
775 rdev
->bb_page
= NULL
;
777 badblocks_exit(&rdev
->badblocks
);
779 EXPORT_SYMBOL_GPL(md_rdev_clear
);
781 static void super_written(struct bio
*bio
)
783 struct md_rdev
*rdev
= bio
->bi_private
;
784 struct mddev
*mddev
= rdev
->mddev
;
786 if (bio
->bi_status
) {
787 pr_err("md: super_written gets error=%d\n", bio
->bi_status
);
788 md_error(mddev
, rdev
);
789 if (!test_bit(Faulty
, &rdev
->flags
)
790 && (bio
->bi_opf
& MD_FAILFAST
)) {
791 set_bit(MD_SB_NEED_REWRITE
, &mddev
->sb_flags
);
792 set_bit(LastDev
, &rdev
->flags
);
795 clear_bit(LastDev
, &rdev
->flags
);
797 if (atomic_dec_and_test(&mddev
->pending_writes
))
798 wake_up(&mddev
->sb_wait
);
799 rdev_dec_pending(rdev
, mddev
);
803 void md_super_write(struct mddev
*mddev
, struct md_rdev
*rdev
,
804 sector_t sector
, int size
, struct page
*page
)
806 /* write first size bytes of page to sector of rdev
807 * Increment mddev->pending_writes before returning
808 * and decrement it on completion, waking up sb_wait
809 * if zero is reached.
810 * If an error occurred, call md_error
818 if (test_bit(Faulty
, &rdev
->flags
))
821 bio
= md_bio_alloc_sync(mddev
);
823 atomic_inc(&rdev
->nr_pending
);
825 bio_set_dev(bio
, rdev
->meta_bdev
? rdev
->meta_bdev
: rdev
->bdev
);
826 bio
->bi_iter
.bi_sector
= sector
;
827 bio_add_page(bio
, page
, size
, 0);
828 bio
->bi_private
= rdev
;
829 bio
->bi_end_io
= super_written
;
831 if (test_bit(MD_FAILFAST_SUPPORTED
, &mddev
->flags
) &&
832 test_bit(FailFast
, &rdev
->flags
) &&
833 !test_bit(LastDev
, &rdev
->flags
))
835 bio
->bi_opf
= REQ_OP_WRITE
| REQ_SYNC
| REQ_PREFLUSH
| REQ_FUA
| ff
;
837 atomic_inc(&mddev
->pending_writes
);
841 int md_super_wait(struct mddev
*mddev
)
843 /* wait for all superblock writes that were scheduled to complete */
844 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->pending_writes
)==0);
845 if (test_and_clear_bit(MD_SB_NEED_REWRITE
, &mddev
->sb_flags
))
850 int sync_page_io(struct md_rdev
*rdev
, sector_t sector
, int size
,
851 struct page
*page
, int op
, int op_flags
, bool metadata_op
)
853 struct bio
*bio
= md_bio_alloc_sync(rdev
->mddev
);
856 if (metadata_op
&& rdev
->meta_bdev
)
857 bio_set_dev(bio
, rdev
->meta_bdev
);
859 bio_set_dev(bio
, rdev
->bdev
);
860 bio_set_op_attrs(bio
, op
, op_flags
);
862 bio
->bi_iter
.bi_sector
= sector
+ rdev
->sb_start
;
863 else if (rdev
->mddev
->reshape_position
!= MaxSector
&&
864 (rdev
->mddev
->reshape_backwards
==
865 (sector
>= rdev
->mddev
->reshape_position
)))
866 bio
->bi_iter
.bi_sector
= sector
+ rdev
->new_data_offset
;
868 bio
->bi_iter
.bi_sector
= sector
+ rdev
->data_offset
;
869 bio_add_page(bio
, page
, size
, 0);
871 submit_bio_wait(bio
);
873 ret
= !bio
->bi_status
;
877 EXPORT_SYMBOL_GPL(sync_page_io
);
879 static int read_disk_sb(struct md_rdev
*rdev
, int size
)
881 char b
[BDEVNAME_SIZE
];
886 if (!sync_page_io(rdev
, 0, size
, rdev
->sb_page
, REQ_OP_READ
, 0, true))
892 pr_err("md: disabled device %s, could not read superblock.\n",
893 bdevname(rdev
->bdev
,b
));
897 static int md_uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
899 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
900 sb1
->set_uuid1
== sb2
->set_uuid1
&&
901 sb1
->set_uuid2
== sb2
->set_uuid2
&&
902 sb1
->set_uuid3
== sb2
->set_uuid3
;
905 static int md_sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
908 mdp_super_t
*tmp1
, *tmp2
;
910 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
911 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
913 if (!tmp1
|| !tmp2
) {
922 * nr_disks is not constant
927 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
934 static u32
md_csum_fold(u32 csum
)
936 csum
= (csum
& 0xffff) + (csum
>> 16);
937 return (csum
& 0xffff) + (csum
>> 16);
940 static unsigned int calc_sb_csum(mdp_super_t
*sb
)
943 u32
*sb32
= (u32
*)sb
;
945 unsigned int disk_csum
, csum
;
947 disk_csum
= sb
->sb_csum
;
950 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
952 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
955 /* This used to use csum_partial, which was wrong for several
956 * reasons including that different results are returned on
957 * different architectures. It isn't critical that we get exactly
958 * the same return value as before (we always csum_fold before
959 * testing, and that removes any differences). However as we
960 * know that csum_partial always returned a 16bit value on
961 * alphas, do a fold to maximise conformity to previous behaviour.
963 sb
->sb_csum
= md_csum_fold(disk_csum
);
965 sb
->sb_csum
= disk_csum
;
971 * Handle superblock details.
972 * We want to be able to handle multiple superblock formats
973 * so we have a common interface to them all, and an array of
974 * different handlers.
975 * We rely on user-space to write the initial superblock, and support
976 * reading and updating of superblocks.
977 * Interface methods are:
978 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
979 * loads and validates a superblock on dev.
980 * if refdev != NULL, compare superblocks on both devices
982 * 0 - dev has a superblock that is compatible with refdev
983 * 1 - dev has a superblock that is compatible and newer than refdev
984 * so dev should be used as the refdev in future
985 * -EINVAL superblock incompatible or invalid
986 * -othererror e.g. -EIO
988 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
989 * Verify that dev is acceptable into mddev.
990 * The first time, mddev->raid_disks will be 0, and data from
991 * dev should be merged in. Subsequent calls check that dev
992 * is new enough. Return 0 or -EINVAL
994 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
995 * Update the superblock for rdev with data in mddev
996 * This does not write to disc.
1002 struct module
*owner
;
1003 int (*load_super
)(struct md_rdev
*rdev
,
1004 struct md_rdev
*refdev
,
1006 int (*validate_super
)(struct mddev
*mddev
,
1007 struct md_rdev
*rdev
);
1008 void (*sync_super
)(struct mddev
*mddev
,
1009 struct md_rdev
*rdev
);
1010 unsigned long long (*rdev_size_change
)(struct md_rdev
*rdev
,
1011 sector_t num_sectors
);
1012 int (*allow_new_offset
)(struct md_rdev
*rdev
,
1013 unsigned long long new_offset
);
1017 * Check that the given mddev has no bitmap.
1019 * This function is called from the run method of all personalities that do not
1020 * support bitmaps. It prints an error message and returns non-zero if mddev
1021 * has a bitmap. Otherwise, it returns 0.
1024 int md_check_no_bitmap(struct mddev
*mddev
)
1026 if (!mddev
->bitmap_info
.file
&& !mddev
->bitmap_info
.offset
)
1028 pr_warn("%s: bitmaps are not supported for %s\n",
1029 mdname(mddev
), mddev
->pers
->name
);
1032 EXPORT_SYMBOL(md_check_no_bitmap
);
1035 * load_super for 0.90.0
1037 static int super_90_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
1039 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1044 * Calculate the position of the superblock (512byte sectors),
1045 * it's at the end of the disk.
1047 * It also happens to be a multiple of 4Kb.
1049 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1051 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
1057 bdevname(rdev
->bdev
, b
);
1058 sb
= page_address(rdev
->sb_page
);
1060 if (sb
->md_magic
!= MD_SB_MAGIC
) {
1061 pr_warn("md: invalid raid superblock magic on %s\n", b
);
1065 if (sb
->major_version
!= 0 ||
1066 sb
->minor_version
< 90 ||
1067 sb
->minor_version
> 91) {
1068 pr_warn("Bad version number %d.%d on %s\n",
1069 sb
->major_version
, sb
->minor_version
, b
);
1073 if (sb
->raid_disks
<= 0)
1076 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
1077 pr_warn("md: invalid superblock checksum on %s\n", b
);
1081 rdev
->preferred_minor
= sb
->md_minor
;
1082 rdev
->data_offset
= 0;
1083 rdev
->new_data_offset
= 0;
1084 rdev
->sb_size
= MD_SB_BYTES
;
1085 rdev
->badblocks
.shift
= -1;
1087 if (sb
->level
== LEVEL_MULTIPATH
)
1090 rdev
->desc_nr
= sb
->this_disk
.number
;
1096 mdp_super_t
*refsb
= page_address(refdev
->sb_page
);
1097 if (!md_uuid_equal(refsb
, sb
)) {
1098 pr_warn("md: %s has different UUID to %s\n",
1099 b
, bdevname(refdev
->bdev
,b2
));
1102 if (!md_sb_equal(refsb
, sb
)) {
1103 pr_warn("md: %s has same UUID but different superblock to %s\n",
1104 b
, bdevname(refdev
->bdev
, b2
));
1108 ev2
= md_event(refsb
);
1114 rdev
->sectors
= rdev
->sb_start
;
1115 /* Limit to 4TB as metadata cannot record more than that.
1116 * (not needed for Linear and RAID0 as metadata doesn't
1119 if (IS_ENABLED(CONFIG_LBDAF
) && (u64
)rdev
->sectors
>= (2ULL << 32) &&
1121 rdev
->sectors
= (sector_t
)(2ULL << 32) - 2;
1123 if (rdev
->sectors
< ((sector_t
)sb
->size
) * 2 && sb
->level
>= 1)
1124 /* "this cannot possibly happen" ... */
1132 * validate_super for 0.90.0
1134 static int super_90_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
1137 mdp_super_t
*sb
= page_address(rdev
->sb_page
);
1138 __u64 ev1
= md_event(sb
);
1140 rdev
->raid_disk
= -1;
1141 clear_bit(Faulty
, &rdev
->flags
);
1142 clear_bit(In_sync
, &rdev
->flags
);
1143 clear_bit(Bitmap_sync
, &rdev
->flags
);
1144 clear_bit(WriteMostly
, &rdev
->flags
);
1146 if (mddev
->raid_disks
== 0) {
1147 mddev
->major_version
= 0;
1148 mddev
->minor_version
= sb
->minor_version
;
1149 mddev
->patch_version
= sb
->patch_version
;
1150 mddev
->external
= 0;
1151 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
1152 mddev
->ctime
= sb
->ctime
;
1153 mddev
->utime
= sb
->utime
;
1154 mddev
->level
= sb
->level
;
1155 mddev
->clevel
[0] = 0;
1156 mddev
->layout
= sb
->layout
;
1157 mddev
->raid_disks
= sb
->raid_disks
;
1158 mddev
->dev_sectors
= ((sector_t
)sb
->size
) * 2;
1159 mddev
->events
= ev1
;
1160 mddev
->bitmap_info
.offset
= 0;
1161 mddev
->bitmap_info
.space
= 0;
1162 /* bitmap can use 60 K after the 4K superblocks */
1163 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
1164 mddev
->bitmap_info
.default_space
= 64*2 - (MD_SB_BYTES
>> 9);
1165 mddev
->reshape_backwards
= 0;
1167 if (mddev
->minor_version
>= 91) {
1168 mddev
->reshape_position
= sb
->reshape_position
;
1169 mddev
->delta_disks
= sb
->delta_disks
;
1170 mddev
->new_level
= sb
->new_level
;
1171 mddev
->new_layout
= sb
->new_layout
;
1172 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
1173 if (mddev
->delta_disks
< 0)
1174 mddev
->reshape_backwards
= 1;
1176 mddev
->reshape_position
= MaxSector
;
1177 mddev
->delta_disks
= 0;
1178 mddev
->new_level
= mddev
->level
;
1179 mddev
->new_layout
= mddev
->layout
;
1180 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1183 if (sb
->state
& (1<<MD_SB_CLEAN
))
1184 mddev
->recovery_cp
= MaxSector
;
1186 if (sb
->events_hi
== sb
->cp_events_hi
&&
1187 sb
->events_lo
== sb
->cp_events_lo
) {
1188 mddev
->recovery_cp
= sb
->recovery_cp
;
1190 mddev
->recovery_cp
= 0;
1193 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
1194 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
1195 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
1196 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
1198 mddev
->max_disks
= MD_SB_DISKS
;
1200 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
1201 mddev
->bitmap_info
.file
== NULL
) {
1202 mddev
->bitmap_info
.offset
=
1203 mddev
->bitmap_info
.default_offset
;
1204 mddev
->bitmap_info
.space
=
1205 mddev
->bitmap_info
.default_space
;
1208 } else if (mddev
->pers
== NULL
) {
1209 /* Insist on good event counter while assembling, except
1210 * for spares (which don't need an event count) */
1212 if (sb
->disks
[rdev
->desc_nr
].state
& (
1213 (1<<MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
)))
1214 if (ev1
< mddev
->events
)
1216 } else if (mddev
->bitmap
) {
1217 /* if adding to array with a bitmap, then we can accept an
1218 * older device ... but not too old.
1220 if (ev1
< mddev
->bitmap
->events_cleared
)
1222 if (ev1
< mddev
->events
)
1223 set_bit(Bitmap_sync
, &rdev
->flags
);
1225 if (ev1
< mddev
->events
)
1226 /* just a hot-add of a new device, leave raid_disk at -1 */
1230 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1231 desc
= sb
->disks
+ rdev
->desc_nr
;
1233 if (desc
->state
& (1<<MD_DISK_FAULTY
))
1234 set_bit(Faulty
, &rdev
->flags
);
1235 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
1236 desc->raid_disk < mddev->raid_disks */) {
1237 set_bit(In_sync
, &rdev
->flags
);
1238 rdev
->raid_disk
= desc
->raid_disk
;
1239 rdev
->saved_raid_disk
= desc
->raid_disk
;
1240 } else if (desc
->state
& (1<<MD_DISK_ACTIVE
)) {
1241 /* active but not in sync implies recovery up to
1242 * reshape position. We don't know exactly where
1243 * that is, so set to zero for now */
1244 if (mddev
->minor_version
>= 91) {
1245 rdev
->recovery_offset
= 0;
1246 rdev
->raid_disk
= desc
->raid_disk
;
1249 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
1250 set_bit(WriteMostly
, &rdev
->flags
);
1251 if (desc
->state
& (1<<MD_DISK_FAILFAST
))
1252 set_bit(FailFast
, &rdev
->flags
);
1253 } else /* MULTIPATH are always insync */
1254 set_bit(In_sync
, &rdev
->flags
);
1259 * sync_super for 0.90.0
1261 static void super_90_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
1264 struct md_rdev
*rdev2
;
1265 int next_spare
= mddev
->raid_disks
;
1267 /* make rdev->sb match mddev data..
1270 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1271 * 3/ any empty disks < next_spare become removed
1273 * disks[0] gets initialised to REMOVED because
1274 * we cannot be sure from other fields if it has
1275 * been initialised or not.
1278 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
1280 rdev
->sb_size
= MD_SB_BYTES
;
1282 sb
= page_address(rdev
->sb_page
);
1284 memset(sb
, 0, sizeof(*sb
));
1286 sb
->md_magic
= MD_SB_MAGIC
;
1287 sb
->major_version
= mddev
->major_version
;
1288 sb
->patch_version
= mddev
->patch_version
;
1289 sb
->gvalid_words
= 0; /* ignored */
1290 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
1291 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
1292 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
1293 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
1295 sb
->ctime
= clamp_t(time64_t
, mddev
->ctime
, 0, U32_MAX
);
1296 sb
->level
= mddev
->level
;
1297 sb
->size
= mddev
->dev_sectors
/ 2;
1298 sb
->raid_disks
= mddev
->raid_disks
;
1299 sb
->md_minor
= mddev
->md_minor
;
1300 sb
->not_persistent
= 0;
1301 sb
->utime
= clamp_t(time64_t
, mddev
->utime
, 0, U32_MAX
);
1303 sb
->events_hi
= (mddev
->events
>>32);
1304 sb
->events_lo
= (u32
)mddev
->events
;
1306 if (mddev
->reshape_position
== MaxSector
)
1307 sb
->minor_version
= 90;
1309 sb
->minor_version
= 91;
1310 sb
->reshape_position
= mddev
->reshape_position
;
1311 sb
->new_level
= mddev
->new_level
;
1312 sb
->delta_disks
= mddev
->delta_disks
;
1313 sb
->new_layout
= mddev
->new_layout
;
1314 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1316 mddev
->minor_version
= sb
->minor_version
;
1319 sb
->recovery_cp
= mddev
->recovery_cp
;
1320 sb
->cp_events_hi
= (mddev
->events
>>32);
1321 sb
->cp_events_lo
= (u32
)mddev
->events
;
1322 if (mddev
->recovery_cp
== MaxSector
)
1323 sb
->state
= (1<< MD_SB_CLEAN
);
1325 sb
->recovery_cp
= 0;
1327 sb
->layout
= mddev
->layout
;
1328 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1330 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
)
1331 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1333 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1334 rdev_for_each(rdev2
, mddev
) {
1337 int is_active
= test_bit(In_sync
, &rdev2
->flags
);
1339 if (rdev2
->raid_disk
>= 0 &&
1340 sb
->minor_version
>= 91)
1341 /* we have nowhere to store the recovery_offset,
1342 * but if it is not below the reshape_position,
1343 * we can piggy-back on that.
1346 if (rdev2
->raid_disk
< 0 ||
1347 test_bit(Faulty
, &rdev2
->flags
))
1350 desc_nr
= rdev2
->raid_disk
;
1352 desc_nr
= next_spare
++;
1353 rdev2
->desc_nr
= desc_nr
;
1354 d
= &sb
->disks
[rdev2
->desc_nr
];
1356 d
->number
= rdev2
->desc_nr
;
1357 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1358 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1360 d
->raid_disk
= rdev2
->raid_disk
;
1362 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1363 if (test_bit(Faulty
, &rdev2
->flags
))
1364 d
->state
= (1<<MD_DISK_FAULTY
);
1365 else if (is_active
) {
1366 d
->state
= (1<<MD_DISK_ACTIVE
);
1367 if (test_bit(In_sync
, &rdev2
->flags
))
1368 d
->state
|= (1<<MD_DISK_SYNC
);
1376 if (test_bit(WriteMostly
, &rdev2
->flags
))
1377 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1378 if (test_bit(FailFast
, &rdev2
->flags
))
1379 d
->state
|= (1<<MD_DISK_FAILFAST
);
1381 /* now set the "removed" and "faulty" bits on any missing devices */
1382 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1383 mdp_disk_t
*d
= &sb
->disks
[i
];
1384 if (d
->state
== 0 && d
->number
== 0) {
1387 d
->state
= (1<<MD_DISK_REMOVED
);
1388 d
->state
|= (1<<MD_DISK_FAULTY
);
1392 sb
->nr_disks
= nr_disks
;
1393 sb
->active_disks
= active
;
1394 sb
->working_disks
= working
;
1395 sb
->failed_disks
= failed
;
1396 sb
->spare_disks
= spare
;
1398 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1399 sb
->sb_csum
= calc_sb_csum(sb
);
1403 * rdev_size_change for 0.90.0
1405 static unsigned long long
1406 super_90_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
1408 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1409 return 0; /* component must fit device */
1410 if (rdev
->mddev
->bitmap_info
.offset
)
1411 return 0; /* can't move bitmap */
1412 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1413 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1414 num_sectors
= rdev
->sb_start
;
1415 /* Limit to 4TB as metadata cannot record more than that.
1416 * 4TB == 2^32 KB, or 2*2^32 sectors.
1418 if (IS_ENABLED(CONFIG_LBDAF
) && (u64
)num_sectors
>= (2ULL << 32) &&
1419 rdev
->mddev
->level
>= 1)
1420 num_sectors
= (sector_t
)(2ULL << 32) - 2;
1422 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1424 } while (md_super_wait(rdev
->mddev
) < 0);
1429 super_90_allow_new_offset(struct md_rdev
*rdev
, unsigned long long new_offset
)
1431 /* non-zero offset changes not possible with v0.90 */
1432 return new_offset
== 0;
1436 * version 1 superblock
1439 static __le32
calc_sb_1_csum(struct mdp_superblock_1
*sb
)
1443 unsigned long long newcsum
;
1444 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1445 __le32
*isuper
= (__le32
*)sb
;
1447 disk_csum
= sb
->sb_csum
;
1450 for (; size
>= 4; size
-= 4)
1451 newcsum
+= le32_to_cpu(*isuper
++);
1454 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1456 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1457 sb
->sb_csum
= disk_csum
;
1458 return cpu_to_le32(csum
);
1461 static int super_1_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
1463 struct mdp_superblock_1
*sb
;
1467 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1471 * Calculate the position of the superblock in 512byte sectors.
1472 * It is always aligned to a 4K boundary and
1473 * depeding on minor_version, it can be:
1474 * 0: At least 8K, but less than 12K, from end of device
1475 * 1: At start of device
1476 * 2: 4K from start of device.
1478 switch(minor_version
) {
1480 sb_start
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1482 sb_start
&= ~(sector_t
)(4*2-1);
1493 rdev
->sb_start
= sb_start
;
1495 /* superblock is rarely larger than 1K, but it can be larger,
1496 * and it is safe to read 4k, so we do that
1498 ret
= read_disk_sb(rdev
, 4096);
1499 if (ret
) return ret
;
1501 sb
= page_address(rdev
->sb_page
);
1503 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1504 sb
->major_version
!= cpu_to_le32(1) ||
1505 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1506 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1507 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1510 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1511 pr_warn("md: invalid superblock checksum on %s\n",
1512 bdevname(rdev
->bdev
,b
));
1515 if (le64_to_cpu(sb
->data_size
) < 10) {
1516 pr_warn("md: data_size too small on %s\n",
1517 bdevname(rdev
->bdev
,b
));
1522 memcmp(sb
->pad3
, sb
->pad3
+1, sizeof(sb
->pad3
) - sizeof(sb
->pad3
[1])))
1523 /* Some padding is non-zero, might be a new feature */
1526 rdev
->preferred_minor
= 0xffff;
1527 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1528 rdev
->new_data_offset
= rdev
->data_offset
;
1529 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
) &&
1530 (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_NEW_OFFSET
))
1531 rdev
->new_data_offset
+= (s32
)le32_to_cpu(sb
->new_offset
);
1532 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1534 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1535 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1536 if (rdev
->sb_size
& bmask
)
1537 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1540 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1543 && rdev
->new_data_offset
< sb_start
+ (rdev
->sb_size
/512))
1546 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1549 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1551 if (!rdev
->bb_page
) {
1552 rdev
->bb_page
= alloc_page(GFP_KERNEL
);
1556 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BAD_BLOCKS
) &&
1557 rdev
->badblocks
.count
== 0) {
1558 /* need to load the bad block list.
1559 * Currently we limit it to one page.
1565 int sectors
= le16_to_cpu(sb
->bblog_size
);
1566 if (sectors
> (PAGE_SIZE
/ 512))
1568 offset
= le32_to_cpu(sb
->bblog_offset
);
1571 bb_sector
= (long long)offset
;
1572 if (!sync_page_io(rdev
, bb_sector
, sectors
<< 9,
1573 rdev
->bb_page
, REQ_OP_READ
, 0, true))
1575 bbp
= (u64
*)page_address(rdev
->bb_page
);
1576 rdev
->badblocks
.shift
= sb
->bblog_shift
;
1577 for (i
= 0 ; i
< (sectors
<< (9-3)) ; i
++, bbp
++) {
1578 u64 bb
= le64_to_cpu(*bbp
);
1579 int count
= bb
& (0x3ff);
1580 u64 sector
= bb
>> 10;
1581 sector
<<= sb
->bblog_shift
;
1582 count
<<= sb
->bblog_shift
;
1585 if (badblocks_set(&rdev
->badblocks
, sector
, count
, 1))
1588 } else if (sb
->bblog_offset
!= 0)
1589 rdev
->badblocks
.shift
= 0;
1591 if ((le32_to_cpu(sb
->feature_map
) &
1592 (MD_FEATURE_PPL
| MD_FEATURE_MULTIPLE_PPLS
))) {
1593 rdev
->ppl
.offset
= (__s16
)le16_to_cpu(sb
->ppl
.offset
);
1594 rdev
->ppl
.size
= le16_to_cpu(sb
->ppl
.size
);
1595 rdev
->ppl
.sector
= rdev
->sb_start
+ rdev
->ppl
.offset
;
1602 struct mdp_superblock_1
*refsb
= page_address(refdev
->sb_page
);
1604 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1605 sb
->level
!= refsb
->level
||
1606 sb
->layout
!= refsb
->layout
||
1607 sb
->chunksize
!= refsb
->chunksize
) {
1608 pr_warn("md: %s has strangely different superblock to %s\n",
1609 bdevname(rdev
->bdev
,b
),
1610 bdevname(refdev
->bdev
,b2
));
1613 ev1
= le64_to_cpu(sb
->events
);
1614 ev2
= le64_to_cpu(refsb
->events
);
1621 if (minor_version
) {
1622 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9);
1623 sectors
-= rdev
->data_offset
;
1625 sectors
= rdev
->sb_start
;
1626 if (sectors
< le64_to_cpu(sb
->data_size
))
1628 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1632 static int super_1_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
1634 struct mdp_superblock_1
*sb
= page_address(rdev
->sb_page
);
1635 __u64 ev1
= le64_to_cpu(sb
->events
);
1637 rdev
->raid_disk
= -1;
1638 clear_bit(Faulty
, &rdev
->flags
);
1639 clear_bit(In_sync
, &rdev
->flags
);
1640 clear_bit(Bitmap_sync
, &rdev
->flags
);
1641 clear_bit(WriteMostly
, &rdev
->flags
);
1643 if (mddev
->raid_disks
== 0) {
1644 mddev
->major_version
= 1;
1645 mddev
->patch_version
= 0;
1646 mddev
->external
= 0;
1647 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1648 mddev
->ctime
= le64_to_cpu(sb
->ctime
);
1649 mddev
->utime
= le64_to_cpu(sb
->utime
);
1650 mddev
->level
= le32_to_cpu(sb
->level
);
1651 mddev
->clevel
[0] = 0;
1652 mddev
->layout
= le32_to_cpu(sb
->layout
);
1653 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1654 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1655 mddev
->events
= ev1
;
1656 mddev
->bitmap_info
.offset
= 0;
1657 mddev
->bitmap_info
.space
= 0;
1658 /* Default location for bitmap is 1K after superblock
1659 * using 3K - total of 4K
1661 mddev
->bitmap_info
.default_offset
= 1024 >> 9;
1662 mddev
->bitmap_info
.default_space
= (4096-1024) >> 9;
1663 mddev
->reshape_backwards
= 0;
1665 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1666 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1668 mddev
->max_disks
= (4096-256)/2;
1670 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1671 mddev
->bitmap_info
.file
== NULL
) {
1672 mddev
->bitmap_info
.offset
=
1673 (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1674 /* Metadata doesn't record how much space is available.
1675 * For 1.0, we assume we can use up to the superblock
1676 * if before, else to 4K beyond superblock.
1677 * For others, assume no change is possible.
1679 if (mddev
->minor_version
> 0)
1680 mddev
->bitmap_info
.space
= 0;
1681 else if (mddev
->bitmap_info
.offset
> 0)
1682 mddev
->bitmap_info
.space
=
1683 8 - mddev
->bitmap_info
.offset
;
1685 mddev
->bitmap_info
.space
=
1686 -mddev
->bitmap_info
.offset
;
1689 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1690 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1691 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1692 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1693 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1694 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1695 if (mddev
->delta_disks
< 0 ||
1696 (mddev
->delta_disks
== 0 &&
1697 (le32_to_cpu(sb
->feature_map
)
1698 & MD_FEATURE_RESHAPE_BACKWARDS
)))
1699 mddev
->reshape_backwards
= 1;
1701 mddev
->reshape_position
= MaxSector
;
1702 mddev
->delta_disks
= 0;
1703 mddev
->new_level
= mddev
->level
;
1704 mddev
->new_layout
= mddev
->layout
;
1705 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1708 if (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_JOURNAL
)
1709 set_bit(MD_HAS_JOURNAL
, &mddev
->flags
);
1711 if (le32_to_cpu(sb
->feature_map
) &
1712 (MD_FEATURE_PPL
| MD_FEATURE_MULTIPLE_PPLS
)) {
1713 if (le32_to_cpu(sb
->feature_map
) &
1714 (MD_FEATURE_BITMAP_OFFSET
| MD_FEATURE_JOURNAL
))
1716 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_PPL
) &&
1717 (le32_to_cpu(sb
->feature_map
) &
1718 MD_FEATURE_MULTIPLE_PPLS
))
1720 set_bit(MD_HAS_PPL
, &mddev
->flags
);
1722 } else if (mddev
->pers
== NULL
) {
1723 /* Insist of good event counter while assembling, except for
1724 * spares (which don't need an event count) */
1726 if (rdev
->desc_nr
>= 0 &&
1727 rdev
->desc_nr
< le32_to_cpu(sb
->max_dev
) &&
1728 (le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) < MD_DISK_ROLE_MAX
||
1729 le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) == MD_DISK_ROLE_JOURNAL
))
1730 if (ev1
< mddev
->events
)
1732 } else if (mddev
->bitmap
) {
1733 /* If adding to array with a bitmap, then we can accept an
1734 * older device, but not too old.
1736 if (ev1
< mddev
->bitmap
->events_cleared
)
1738 if (ev1
< mddev
->events
)
1739 set_bit(Bitmap_sync
, &rdev
->flags
);
1741 if (ev1
< mddev
->events
)
1742 /* just a hot-add of a new device, leave raid_disk at -1 */
1745 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1747 if (rdev
->desc_nr
< 0 ||
1748 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1749 role
= MD_DISK_ROLE_SPARE
;
1752 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1754 case MD_DISK_ROLE_SPARE
: /* spare */
1756 case MD_DISK_ROLE_FAULTY
: /* faulty */
1757 set_bit(Faulty
, &rdev
->flags
);
1759 case MD_DISK_ROLE_JOURNAL
: /* journal device */
1760 if (!(le32_to_cpu(sb
->feature_map
) & MD_FEATURE_JOURNAL
)) {
1761 /* journal device without journal feature */
1762 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
1765 set_bit(Journal
, &rdev
->flags
);
1766 rdev
->journal_tail
= le64_to_cpu(sb
->journal_tail
);
1767 rdev
->raid_disk
= 0;
1770 rdev
->saved_raid_disk
= role
;
1771 if ((le32_to_cpu(sb
->feature_map
) &
1772 MD_FEATURE_RECOVERY_OFFSET
)) {
1773 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1774 if (!(le32_to_cpu(sb
->feature_map
) &
1775 MD_FEATURE_RECOVERY_BITMAP
))
1776 rdev
->saved_raid_disk
= -1;
1778 set_bit(In_sync
, &rdev
->flags
);
1779 rdev
->raid_disk
= role
;
1782 if (sb
->devflags
& WriteMostly1
)
1783 set_bit(WriteMostly
, &rdev
->flags
);
1784 if (sb
->devflags
& FailFast1
)
1785 set_bit(FailFast
, &rdev
->flags
);
1786 if (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_REPLACEMENT
)
1787 set_bit(Replacement
, &rdev
->flags
);
1788 } else /* MULTIPATH are always insync */
1789 set_bit(In_sync
, &rdev
->flags
);
1794 static void super_1_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
1796 struct mdp_superblock_1
*sb
;
1797 struct md_rdev
*rdev2
;
1799 /* make rdev->sb match mddev and rdev data. */
1801 sb
= page_address(rdev
->sb_page
);
1803 sb
->feature_map
= 0;
1805 sb
->recovery_offset
= cpu_to_le64(0);
1806 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1808 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1809 sb
->events
= cpu_to_le64(mddev
->events
);
1811 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1812 else if (test_bit(MD_JOURNAL_CLEAN
, &mddev
->flags
))
1813 sb
->resync_offset
= cpu_to_le64(MaxSector
);
1815 sb
->resync_offset
= cpu_to_le64(0);
1817 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1819 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1820 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
1821 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
1822 sb
->level
= cpu_to_le32(mddev
->level
);
1823 sb
->layout
= cpu_to_le32(mddev
->layout
);
1824 if (test_bit(FailFast
, &rdev
->flags
))
1825 sb
->devflags
|= FailFast1
;
1827 sb
->devflags
&= ~FailFast1
;
1829 if (test_bit(WriteMostly
, &rdev
->flags
))
1830 sb
->devflags
|= WriteMostly1
;
1832 sb
->devflags
&= ~WriteMostly1
;
1833 sb
->data_offset
= cpu_to_le64(rdev
->data_offset
);
1834 sb
->data_size
= cpu_to_le64(rdev
->sectors
);
1836 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
) {
1837 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_info
.offset
);
1838 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1841 if (rdev
->raid_disk
>= 0 && !test_bit(Journal
, &rdev
->flags
) &&
1842 !test_bit(In_sync
, &rdev
->flags
)) {
1844 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1845 sb
->recovery_offset
=
1846 cpu_to_le64(rdev
->recovery_offset
);
1847 if (rdev
->saved_raid_disk
>= 0 && mddev
->bitmap
)
1849 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP
);
1851 /* Note: recovery_offset and journal_tail share space */
1852 if (test_bit(Journal
, &rdev
->flags
))
1853 sb
->journal_tail
= cpu_to_le64(rdev
->journal_tail
);
1854 if (test_bit(Replacement
, &rdev
->flags
))
1856 cpu_to_le32(MD_FEATURE_REPLACEMENT
);
1858 if (mddev
->reshape_position
!= MaxSector
) {
1859 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1860 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1861 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1862 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1863 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1864 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
1865 if (mddev
->delta_disks
== 0 &&
1866 mddev
->reshape_backwards
)
1868 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS
);
1869 if (rdev
->new_data_offset
!= rdev
->data_offset
) {
1871 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET
);
1872 sb
->new_offset
= cpu_to_le32((__u32
)(rdev
->new_data_offset
1873 - rdev
->data_offset
));
1877 if (mddev_is_clustered(mddev
))
1878 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_CLUSTERED
);
1880 if (rdev
->badblocks
.count
== 0)
1881 /* Nothing to do for bad blocks*/ ;
1882 else if (sb
->bblog_offset
== 0)
1883 /* Cannot record bad blocks on this device */
1884 md_error(mddev
, rdev
);
1886 struct badblocks
*bb
= &rdev
->badblocks
;
1887 u64
*bbp
= (u64
*)page_address(rdev
->bb_page
);
1889 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_BAD_BLOCKS
);
1894 seq
= read_seqbegin(&bb
->lock
);
1896 memset(bbp
, 0xff, PAGE_SIZE
);
1898 for (i
= 0 ; i
< bb
->count
; i
++) {
1899 u64 internal_bb
= p
[i
];
1900 u64 store_bb
= ((BB_OFFSET(internal_bb
) << 10)
1901 | BB_LEN(internal_bb
));
1902 bbp
[i
] = cpu_to_le64(store_bb
);
1905 if (read_seqretry(&bb
->lock
, seq
))
1908 bb
->sector
= (rdev
->sb_start
+
1909 (int)le32_to_cpu(sb
->bblog_offset
));
1910 bb
->size
= le16_to_cpu(sb
->bblog_size
);
1915 rdev_for_each(rdev2
, mddev
)
1916 if (rdev2
->desc_nr
+1 > max_dev
)
1917 max_dev
= rdev2
->desc_nr
+1;
1919 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
1921 sb
->max_dev
= cpu_to_le32(max_dev
);
1922 rdev
->sb_size
= max_dev
* 2 + 256;
1923 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1924 if (rdev
->sb_size
& bmask
)
1925 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1927 max_dev
= le32_to_cpu(sb
->max_dev
);
1929 for (i
=0; i
<max_dev
;i
++)
1930 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_SPARE
);
1932 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
))
1933 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_JOURNAL
);
1935 if (test_bit(MD_HAS_PPL
, &mddev
->flags
)) {
1936 if (test_bit(MD_HAS_MULTIPLE_PPLS
, &mddev
->flags
))
1938 cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS
);
1940 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_PPL
);
1941 sb
->ppl
.offset
= cpu_to_le16(rdev
->ppl
.offset
);
1942 sb
->ppl
.size
= cpu_to_le16(rdev
->ppl
.size
);
1945 rdev_for_each(rdev2
, mddev
) {
1947 if (test_bit(Faulty
, &rdev2
->flags
))
1948 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_FAULTY
);
1949 else if (test_bit(In_sync
, &rdev2
->flags
))
1950 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1951 else if (test_bit(Journal
, &rdev2
->flags
))
1952 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_JOURNAL
);
1953 else if (rdev2
->raid_disk
>= 0)
1954 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1956 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_SPARE
);
1959 sb
->sb_csum
= calc_sb_1_csum(sb
);
1962 static unsigned long long
1963 super_1_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
1965 struct mdp_superblock_1
*sb
;
1966 sector_t max_sectors
;
1967 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1968 return 0; /* component must fit device */
1969 if (rdev
->data_offset
!= rdev
->new_data_offset
)
1970 return 0; /* too confusing */
1971 if (rdev
->sb_start
< rdev
->data_offset
) {
1972 /* minor versions 1 and 2; superblock before data */
1973 max_sectors
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1974 max_sectors
-= rdev
->data_offset
;
1975 if (!num_sectors
|| num_sectors
> max_sectors
)
1976 num_sectors
= max_sectors
;
1977 } else if (rdev
->mddev
->bitmap_info
.offset
) {
1978 /* minor version 0 with bitmap we can't move */
1981 /* minor version 0; superblock after data */
1983 sb_start
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) - 8*2;
1984 sb_start
&= ~(sector_t
)(4*2 - 1);
1985 max_sectors
= rdev
->sectors
+ sb_start
- rdev
->sb_start
;
1986 if (!num_sectors
|| num_sectors
> max_sectors
)
1987 num_sectors
= max_sectors
;
1988 rdev
->sb_start
= sb_start
;
1990 sb
= page_address(rdev
->sb_page
);
1991 sb
->data_size
= cpu_to_le64(num_sectors
);
1992 sb
->super_offset
= cpu_to_le64(rdev
->sb_start
);
1993 sb
->sb_csum
= calc_sb_1_csum(sb
);
1995 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1997 } while (md_super_wait(rdev
->mddev
) < 0);
2003 super_1_allow_new_offset(struct md_rdev
*rdev
,
2004 unsigned long long new_offset
)
2006 /* All necessary checks on new >= old have been done */
2007 struct bitmap
*bitmap
;
2008 if (new_offset
>= rdev
->data_offset
)
2011 /* with 1.0 metadata, there is no metadata to tread on
2012 * so we can always move back */
2013 if (rdev
->mddev
->minor_version
== 0)
2016 /* otherwise we must be sure not to step on
2017 * any metadata, so stay:
2018 * 36K beyond start of superblock
2019 * beyond end of badblocks
2020 * beyond write-intent bitmap
2022 if (rdev
->sb_start
+ (32+4)*2 > new_offset
)
2024 bitmap
= rdev
->mddev
->bitmap
;
2025 if (bitmap
&& !rdev
->mddev
->bitmap_info
.file
&&
2026 rdev
->sb_start
+ rdev
->mddev
->bitmap_info
.offset
+
2027 bitmap
->storage
.file_pages
* (PAGE_SIZE
>>9) > new_offset
)
2029 if (rdev
->badblocks
.sector
+ rdev
->badblocks
.size
> new_offset
)
2035 static struct super_type super_types
[] = {
2038 .owner
= THIS_MODULE
,
2039 .load_super
= super_90_load
,
2040 .validate_super
= super_90_validate
,
2041 .sync_super
= super_90_sync
,
2042 .rdev_size_change
= super_90_rdev_size_change
,
2043 .allow_new_offset
= super_90_allow_new_offset
,
2047 .owner
= THIS_MODULE
,
2048 .load_super
= super_1_load
,
2049 .validate_super
= super_1_validate
,
2050 .sync_super
= super_1_sync
,
2051 .rdev_size_change
= super_1_rdev_size_change
,
2052 .allow_new_offset
= super_1_allow_new_offset
,
2056 static void sync_super(struct mddev
*mddev
, struct md_rdev
*rdev
)
2058 if (mddev
->sync_super
) {
2059 mddev
->sync_super(mddev
, rdev
);
2063 BUG_ON(mddev
->major_version
>= ARRAY_SIZE(super_types
));
2065 super_types
[mddev
->major_version
].sync_super(mddev
, rdev
);
2068 static int match_mddev_units(struct mddev
*mddev1
, struct mddev
*mddev2
)
2070 struct md_rdev
*rdev
, *rdev2
;
2073 rdev_for_each_rcu(rdev
, mddev1
) {
2074 if (test_bit(Faulty
, &rdev
->flags
) ||
2075 test_bit(Journal
, &rdev
->flags
) ||
2076 rdev
->raid_disk
== -1)
2078 rdev_for_each_rcu(rdev2
, mddev2
) {
2079 if (test_bit(Faulty
, &rdev2
->flags
) ||
2080 test_bit(Journal
, &rdev2
->flags
) ||
2081 rdev2
->raid_disk
== -1)
2083 if (rdev
->bdev
->bd_contains
==
2084 rdev2
->bdev
->bd_contains
) {
2094 static LIST_HEAD(pending_raid_disks
);
2097 * Try to register data integrity profile for an mddev
2099 * This is called when an array is started and after a disk has been kicked
2100 * from the array. It only succeeds if all working and active component devices
2101 * are integrity capable with matching profiles.
2103 int md_integrity_register(struct mddev
*mddev
)
2105 struct md_rdev
*rdev
, *reference
= NULL
;
2107 if (list_empty(&mddev
->disks
))
2108 return 0; /* nothing to do */
2109 if (!mddev
->gendisk
|| blk_get_integrity(mddev
->gendisk
))
2110 return 0; /* shouldn't register, or already is */
2111 rdev_for_each(rdev
, mddev
) {
2112 /* skip spares and non-functional disks */
2113 if (test_bit(Faulty
, &rdev
->flags
))
2115 if (rdev
->raid_disk
< 0)
2118 /* Use the first rdev as the reference */
2122 /* does this rdev's profile match the reference profile? */
2123 if (blk_integrity_compare(reference
->bdev
->bd_disk
,
2124 rdev
->bdev
->bd_disk
) < 0)
2127 if (!reference
|| !bdev_get_integrity(reference
->bdev
))
2130 * All component devices are integrity capable and have matching
2131 * profiles, register the common profile for the md device.
2133 blk_integrity_register(mddev
->gendisk
,
2134 bdev_get_integrity(reference
->bdev
));
2136 pr_debug("md: data integrity enabled on %s\n", mdname(mddev
));
2137 if (bioset_integrity_create(&mddev
->bio_set
, BIO_POOL_SIZE
)) {
2138 pr_err("md: failed to create integrity pool for %s\n",
2144 EXPORT_SYMBOL(md_integrity_register
);
2147 * Attempt to add an rdev, but only if it is consistent with the current
2150 int md_integrity_add_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
2152 struct blk_integrity
*bi_rdev
;
2153 struct blk_integrity
*bi_mddev
;
2154 char name
[BDEVNAME_SIZE
];
2156 if (!mddev
->gendisk
)
2159 bi_rdev
= bdev_get_integrity(rdev
->bdev
);
2160 bi_mddev
= blk_get_integrity(mddev
->gendisk
);
2162 if (!bi_mddev
) /* nothing to do */
2165 if (blk_integrity_compare(mddev
->gendisk
, rdev
->bdev
->bd_disk
) != 0) {
2166 pr_err("%s: incompatible integrity profile for %s\n",
2167 mdname(mddev
), bdevname(rdev
->bdev
, name
));
2173 EXPORT_SYMBOL(md_integrity_add_rdev
);
2175 static int bind_rdev_to_array(struct md_rdev
*rdev
, struct mddev
*mddev
)
2177 char b
[BDEVNAME_SIZE
];
2181 /* prevent duplicates */
2182 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
2185 if ((bdev_read_only(rdev
->bdev
) || bdev_read_only(rdev
->meta_bdev
)) &&
2189 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2190 if (!test_bit(Journal
, &rdev
->flags
) &&
2192 (mddev
->dev_sectors
== 0 || rdev
->sectors
< mddev
->dev_sectors
)) {
2194 /* Cannot change size, so fail
2195 * If mddev->level <= 0, then we don't care
2196 * about aligning sizes (e.g. linear)
2198 if (mddev
->level
> 0)
2201 mddev
->dev_sectors
= rdev
->sectors
;
2204 /* Verify rdev->desc_nr is unique.
2205 * If it is -1, assign a free number, else
2206 * check number is not in use
2209 if (rdev
->desc_nr
< 0) {
2212 choice
= mddev
->raid_disks
;
2213 while (md_find_rdev_nr_rcu(mddev
, choice
))
2215 rdev
->desc_nr
= choice
;
2217 if (md_find_rdev_nr_rcu(mddev
, rdev
->desc_nr
)) {
2223 if (!test_bit(Journal
, &rdev
->flags
) &&
2224 mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
2225 pr_warn("md: %s: array is limited to %d devices\n",
2226 mdname(mddev
), mddev
->max_disks
);
2229 bdevname(rdev
->bdev
,b
);
2230 strreplace(b
, '/', '!');
2232 rdev
->mddev
= mddev
;
2233 pr_debug("md: bind<%s>\n", b
);
2235 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
2238 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
2239 if (sysfs_create_link(&rdev
->kobj
, ko
, "block"))
2240 /* failure here is OK */;
2241 rdev
->sysfs_state
= sysfs_get_dirent_safe(rdev
->kobj
.sd
, "state");
2243 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
2244 bd_link_disk_holder(rdev
->bdev
, mddev
->gendisk
);
2246 /* May as well allow recovery to be retried once */
2247 mddev
->recovery_disabled
++;
2252 pr_warn("md: failed to register dev-%s for %s\n",
2257 static void md_delayed_delete(struct work_struct
*ws
)
2259 struct md_rdev
*rdev
= container_of(ws
, struct md_rdev
, del_work
);
2260 kobject_del(&rdev
->kobj
);
2261 kobject_put(&rdev
->kobj
);
2264 static void unbind_rdev_from_array(struct md_rdev
*rdev
)
2266 char b
[BDEVNAME_SIZE
];
2268 bd_unlink_disk_holder(rdev
->bdev
, rdev
->mddev
->gendisk
);
2269 list_del_rcu(&rdev
->same_set
);
2270 pr_debug("md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
2272 sysfs_remove_link(&rdev
->kobj
, "block");
2273 sysfs_put(rdev
->sysfs_state
);
2274 rdev
->sysfs_state
= NULL
;
2275 rdev
->badblocks
.count
= 0;
2276 /* We need to delay this, otherwise we can deadlock when
2277 * writing to 'remove' to "dev/state". We also need
2278 * to delay it due to rcu usage.
2281 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
2282 kobject_get(&rdev
->kobj
);
2283 queue_work(md_misc_wq
, &rdev
->del_work
);
2287 * prevent the device from being mounted, repartitioned or
2288 * otherwise reused by a RAID array (or any other kernel
2289 * subsystem), by bd_claiming the device.
2291 static int lock_rdev(struct md_rdev
*rdev
, dev_t dev
, int shared
)
2294 struct block_device
*bdev
;
2295 char b
[BDEVNAME_SIZE
];
2297 bdev
= blkdev_get_by_dev(dev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
,
2298 shared
? (struct md_rdev
*)lock_rdev
: rdev
);
2300 pr_warn("md: could not open %s.\n", __bdevname(dev
, b
));
2301 return PTR_ERR(bdev
);
2307 static void unlock_rdev(struct md_rdev
*rdev
)
2309 struct block_device
*bdev
= rdev
->bdev
;
2311 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
2314 void md_autodetect_dev(dev_t dev
);
2316 static void export_rdev(struct md_rdev
*rdev
)
2318 char b
[BDEVNAME_SIZE
];
2320 pr_debug("md: export_rdev(%s)\n", bdevname(rdev
->bdev
,b
));
2321 md_rdev_clear(rdev
);
2323 if (test_bit(AutoDetected
, &rdev
->flags
))
2324 md_autodetect_dev(rdev
->bdev
->bd_dev
);
2327 kobject_put(&rdev
->kobj
);
2330 void md_kick_rdev_from_array(struct md_rdev
*rdev
)
2332 unbind_rdev_from_array(rdev
);
2335 EXPORT_SYMBOL_GPL(md_kick_rdev_from_array
);
2337 static void export_array(struct mddev
*mddev
)
2339 struct md_rdev
*rdev
;
2341 while (!list_empty(&mddev
->disks
)) {
2342 rdev
= list_first_entry(&mddev
->disks
, struct md_rdev
,
2344 md_kick_rdev_from_array(rdev
);
2346 mddev
->raid_disks
= 0;
2347 mddev
->major_version
= 0;
2350 static bool set_in_sync(struct mddev
*mddev
)
2352 lockdep_assert_held(&mddev
->lock
);
2353 if (!mddev
->in_sync
) {
2354 mddev
->sync_checkers
++;
2355 spin_unlock(&mddev
->lock
);
2356 percpu_ref_switch_to_atomic_sync(&mddev
->writes_pending
);
2357 spin_lock(&mddev
->lock
);
2358 if (!mddev
->in_sync
&&
2359 percpu_ref_is_zero(&mddev
->writes_pending
)) {
2362 * Ensure ->in_sync is visible before we clear
2366 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
2367 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
2369 if (--mddev
->sync_checkers
== 0)
2370 percpu_ref_switch_to_percpu(&mddev
->writes_pending
);
2372 if (mddev
->safemode
== 1)
2373 mddev
->safemode
= 0;
2374 return mddev
->in_sync
;
2377 static void sync_sbs(struct mddev
*mddev
, int nospares
)
2379 /* Update each superblock (in-memory image), but
2380 * if we are allowed to, skip spares which already
2381 * have the right event counter, or have one earlier
2382 * (which would mean they aren't being marked as dirty
2383 * with the rest of the array)
2385 struct md_rdev
*rdev
;
2386 rdev_for_each(rdev
, mddev
) {
2387 if (rdev
->sb_events
== mddev
->events
||
2389 rdev
->raid_disk
< 0 &&
2390 rdev
->sb_events
+1 == mddev
->events
)) {
2391 /* Don't update this superblock */
2392 rdev
->sb_loaded
= 2;
2394 sync_super(mddev
, rdev
);
2395 rdev
->sb_loaded
= 1;
2400 static bool does_sb_need_changing(struct mddev
*mddev
)
2402 struct md_rdev
*rdev
;
2403 struct mdp_superblock_1
*sb
;
2406 /* Find a good rdev */
2407 rdev_for_each(rdev
, mddev
)
2408 if ((rdev
->raid_disk
>= 0) && !test_bit(Faulty
, &rdev
->flags
))
2411 /* No good device found. */
2415 sb
= page_address(rdev
->sb_page
);
2416 /* Check if a device has become faulty or a spare become active */
2417 rdev_for_each(rdev
, mddev
) {
2418 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
2419 /* Device activated? */
2420 if (role
== 0xffff && rdev
->raid_disk
>=0 &&
2421 !test_bit(Faulty
, &rdev
->flags
))
2423 /* Device turned faulty? */
2424 if (test_bit(Faulty
, &rdev
->flags
) && (role
< 0xfffd))
2428 /* Check if any mddev parameters have changed */
2429 if ((mddev
->dev_sectors
!= le64_to_cpu(sb
->size
)) ||
2430 (mddev
->reshape_position
!= le64_to_cpu(sb
->reshape_position
)) ||
2431 (mddev
->layout
!= le32_to_cpu(sb
->layout
)) ||
2432 (mddev
->raid_disks
!= le32_to_cpu(sb
->raid_disks
)) ||
2433 (mddev
->chunk_sectors
!= le32_to_cpu(sb
->chunksize
)))
2439 void md_update_sb(struct mddev
*mddev
, int force_change
)
2441 struct md_rdev
*rdev
;
2444 int any_badblocks_changed
= 0;
2449 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2454 if (mddev_is_clustered(mddev
)) {
2455 if (test_and_clear_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
))
2457 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
))
2459 ret
= md_cluster_ops
->metadata_update_start(mddev
);
2460 /* Has someone else has updated the sb */
2461 if (!does_sb_need_changing(mddev
)) {
2463 md_cluster_ops
->metadata_update_cancel(mddev
);
2464 bit_clear_unless(&mddev
->sb_flags
, BIT(MD_SB_CHANGE_PENDING
),
2465 BIT(MD_SB_CHANGE_DEVS
) |
2466 BIT(MD_SB_CHANGE_CLEAN
));
2472 * First make sure individual recovery_offsets are correct
2473 * curr_resync_completed can only be used during recovery.
2474 * During reshape/resync it might use array-addresses rather
2475 * that device addresses.
2477 rdev_for_each(rdev
, mddev
) {
2478 if (rdev
->raid_disk
>= 0 &&
2479 mddev
->delta_disks
>= 0 &&
2480 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
2481 test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
) &&
2482 !test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
2483 !test_bit(Journal
, &rdev
->flags
) &&
2484 !test_bit(In_sync
, &rdev
->flags
) &&
2485 mddev
->curr_resync_completed
> rdev
->recovery_offset
)
2486 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
2489 if (!mddev
->persistent
) {
2490 clear_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
2491 clear_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2492 if (!mddev
->external
) {
2493 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
2494 rdev_for_each(rdev
, mddev
) {
2495 if (rdev
->badblocks
.changed
) {
2496 rdev
->badblocks
.changed
= 0;
2497 ack_all_badblocks(&rdev
->badblocks
);
2498 md_error(mddev
, rdev
);
2500 clear_bit(Blocked
, &rdev
->flags
);
2501 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2502 wake_up(&rdev
->blocked_wait
);
2505 wake_up(&mddev
->sb_wait
);
2509 spin_lock(&mddev
->lock
);
2511 mddev
->utime
= ktime_get_real_seconds();
2513 if (test_and_clear_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
))
2515 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
))
2516 /* just a clean<-> dirty transition, possibly leave spares alone,
2517 * though if events isn't the right even/odd, we will have to do
2523 if (mddev
->degraded
)
2524 /* If the array is degraded, then skipping spares is both
2525 * dangerous and fairly pointless.
2526 * Dangerous because a device that was removed from the array
2527 * might have a event_count that still looks up-to-date,
2528 * so it can be re-added without a resync.
2529 * Pointless because if there are any spares to skip,
2530 * then a recovery will happen and soon that array won't
2531 * be degraded any more and the spare can go back to sleep then.
2535 sync_req
= mddev
->in_sync
;
2537 /* If this is just a dirty<->clean transition, and the array is clean
2538 * and 'events' is odd, we can roll back to the previous clean state */
2540 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
2541 && mddev
->can_decrease_events
2542 && mddev
->events
!= 1) {
2544 mddev
->can_decrease_events
= 0;
2546 /* otherwise we have to go forward and ... */
2548 mddev
->can_decrease_events
= nospares
;
2552 * This 64-bit counter should never wrap.
2553 * Either we are in around ~1 trillion A.C., assuming
2554 * 1 reboot per second, or we have a bug...
2556 WARN_ON(mddev
->events
== 0);
2558 rdev_for_each(rdev
, mddev
) {
2559 if (rdev
->badblocks
.changed
)
2560 any_badblocks_changed
++;
2561 if (test_bit(Faulty
, &rdev
->flags
))
2562 set_bit(FaultRecorded
, &rdev
->flags
);
2565 sync_sbs(mddev
, nospares
);
2566 spin_unlock(&mddev
->lock
);
2568 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2569 mdname(mddev
), mddev
->in_sync
);
2572 blk_add_trace_msg(mddev
->queue
, "md md_update_sb");
2574 bitmap_update_sb(mddev
->bitmap
);
2575 rdev_for_each(rdev
, mddev
) {
2576 char b
[BDEVNAME_SIZE
];
2578 if (rdev
->sb_loaded
!= 1)
2579 continue; /* no noise on spare devices */
2581 if (!test_bit(Faulty
, &rdev
->flags
)) {
2582 md_super_write(mddev
,rdev
,
2583 rdev
->sb_start
, rdev
->sb_size
,
2585 pr_debug("md: (write) %s's sb offset: %llu\n",
2586 bdevname(rdev
->bdev
, b
),
2587 (unsigned long long)rdev
->sb_start
);
2588 rdev
->sb_events
= mddev
->events
;
2589 if (rdev
->badblocks
.size
) {
2590 md_super_write(mddev
, rdev
,
2591 rdev
->badblocks
.sector
,
2592 rdev
->badblocks
.size
<< 9,
2594 rdev
->badblocks
.size
= 0;
2598 pr_debug("md: %s (skipping faulty)\n",
2599 bdevname(rdev
->bdev
, b
));
2601 if (mddev
->level
== LEVEL_MULTIPATH
)
2602 /* only need to write one superblock... */
2605 if (md_super_wait(mddev
) < 0)
2607 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
2609 if (mddev_is_clustered(mddev
) && ret
== 0)
2610 md_cluster_ops
->metadata_update_finish(mddev
);
2612 if (mddev
->in_sync
!= sync_req
||
2613 !bit_clear_unless(&mddev
->sb_flags
, BIT(MD_SB_CHANGE_PENDING
),
2614 BIT(MD_SB_CHANGE_DEVS
) | BIT(MD_SB_CHANGE_CLEAN
)))
2615 /* have to write it out again */
2617 wake_up(&mddev
->sb_wait
);
2618 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2619 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
2621 rdev_for_each(rdev
, mddev
) {
2622 if (test_and_clear_bit(FaultRecorded
, &rdev
->flags
))
2623 clear_bit(Blocked
, &rdev
->flags
);
2625 if (any_badblocks_changed
)
2626 ack_all_badblocks(&rdev
->badblocks
);
2627 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2628 wake_up(&rdev
->blocked_wait
);
2631 EXPORT_SYMBOL(md_update_sb
);
2633 static int add_bound_rdev(struct md_rdev
*rdev
)
2635 struct mddev
*mddev
= rdev
->mddev
;
2637 bool add_journal
= test_bit(Journal
, &rdev
->flags
);
2639 if (!mddev
->pers
->hot_remove_disk
|| add_journal
) {
2640 /* If there is hot_add_disk but no hot_remove_disk
2641 * then added disks for geometry changes,
2642 * and should be added immediately.
2644 super_types
[mddev
->major_version
].
2645 validate_super(mddev
, rdev
);
2647 mddev_suspend(mddev
);
2648 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
2650 mddev_resume(mddev
);
2652 md_kick_rdev_from_array(rdev
);
2656 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2658 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2659 if (mddev
->degraded
)
2660 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
2661 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2662 md_new_event(mddev
);
2663 md_wakeup_thread(mddev
->thread
);
2667 /* words written to sysfs files may, or may not, be \n terminated.
2668 * We want to accept with case. For this we use cmd_match.
2670 static int cmd_match(const char *cmd
, const char *str
)
2672 /* See if cmd, written into a sysfs file, matches
2673 * str. They must either be the same, or cmd can
2674 * have a trailing newline
2676 while (*cmd
&& *str
&& *cmd
== *str
) {
2687 struct rdev_sysfs_entry
{
2688 struct attribute attr
;
2689 ssize_t (*show
)(struct md_rdev
*, char *);
2690 ssize_t (*store
)(struct md_rdev
*, const char *, size_t);
2694 state_show(struct md_rdev
*rdev
, char *page
)
2698 unsigned long flags
= READ_ONCE(rdev
->flags
);
2700 if (test_bit(Faulty
, &flags
) ||
2701 (!test_bit(ExternalBbl
, &flags
) &&
2702 rdev
->badblocks
.unacked_exist
))
2703 len
+= sprintf(page
+len
, "faulty%s", sep
);
2704 if (test_bit(In_sync
, &flags
))
2705 len
+= sprintf(page
+len
, "in_sync%s", sep
);
2706 if (test_bit(Journal
, &flags
))
2707 len
+= sprintf(page
+len
, "journal%s", sep
);
2708 if (test_bit(WriteMostly
, &flags
))
2709 len
+= sprintf(page
+len
, "write_mostly%s", sep
);
2710 if (test_bit(Blocked
, &flags
) ||
2711 (rdev
->badblocks
.unacked_exist
2712 && !test_bit(Faulty
, &flags
)))
2713 len
+= sprintf(page
+len
, "blocked%s", sep
);
2714 if (!test_bit(Faulty
, &flags
) &&
2715 !test_bit(Journal
, &flags
) &&
2716 !test_bit(In_sync
, &flags
))
2717 len
+= sprintf(page
+len
, "spare%s", sep
);
2718 if (test_bit(WriteErrorSeen
, &flags
))
2719 len
+= sprintf(page
+len
, "write_error%s", sep
);
2720 if (test_bit(WantReplacement
, &flags
))
2721 len
+= sprintf(page
+len
, "want_replacement%s", sep
);
2722 if (test_bit(Replacement
, &flags
))
2723 len
+= sprintf(page
+len
, "replacement%s", sep
);
2724 if (test_bit(ExternalBbl
, &flags
))
2725 len
+= sprintf(page
+len
, "external_bbl%s", sep
);
2726 if (test_bit(FailFast
, &flags
))
2727 len
+= sprintf(page
+len
, "failfast%s", sep
);
2732 return len
+sprintf(page
+len
, "\n");
2736 state_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2739 * faulty - simulates an error
2740 * remove - disconnects the device
2741 * writemostly - sets write_mostly
2742 * -writemostly - clears write_mostly
2743 * blocked - sets the Blocked flags
2744 * -blocked - clears the Blocked and possibly simulates an error
2745 * insync - sets Insync providing device isn't active
2746 * -insync - clear Insync for a device with a slot assigned,
2747 * so that it gets rebuilt based on bitmap
2748 * write_error - sets WriteErrorSeen
2749 * -write_error - clears WriteErrorSeen
2750 * {,-}failfast - set/clear FailFast
2753 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
2754 md_error(rdev
->mddev
, rdev
);
2755 if (test_bit(Faulty
, &rdev
->flags
))
2759 } else if (cmd_match(buf
, "remove")) {
2760 if (rdev
->mddev
->pers
) {
2761 clear_bit(Blocked
, &rdev
->flags
);
2762 remove_and_add_spares(rdev
->mddev
, rdev
);
2764 if (rdev
->raid_disk
>= 0)
2767 struct mddev
*mddev
= rdev
->mddev
;
2769 if (mddev_is_clustered(mddev
))
2770 err
= md_cluster_ops
->remove_disk(mddev
, rdev
);
2773 md_kick_rdev_from_array(rdev
);
2775 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2776 md_wakeup_thread(mddev
->thread
);
2778 md_new_event(mddev
);
2781 } else if (cmd_match(buf
, "writemostly")) {
2782 set_bit(WriteMostly
, &rdev
->flags
);
2784 } else if (cmd_match(buf
, "-writemostly")) {
2785 clear_bit(WriteMostly
, &rdev
->flags
);
2787 } else if (cmd_match(buf
, "blocked")) {
2788 set_bit(Blocked
, &rdev
->flags
);
2790 } else if (cmd_match(buf
, "-blocked")) {
2791 if (!test_bit(Faulty
, &rdev
->flags
) &&
2792 !test_bit(ExternalBbl
, &rdev
->flags
) &&
2793 rdev
->badblocks
.unacked_exist
) {
2794 /* metadata handler doesn't understand badblocks,
2795 * so we need to fail the device
2797 md_error(rdev
->mddev
, rdev
);
2799 clear_bit(Blocked
, &rdev
->flags
);
2800 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2801 wake_up(&rdev
->blocked_wait
);
2802 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2803 md_wakeup_thread(rdev
->mddev
->thread
);
2806 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
2807 set_bit(In_sync
, &rdev
->flags
);
2809 } else if (cmd_match(buf
, "failfast")) {
2810 set_bit(FailFast
, &rdev
->flags
);
2812 } else if (cmd_match(buf
, "-failfast")) {
2813 clear_bit(FailFast
, &rdev
->flags
);
2815 } else if (cmd_match(buf
, "-insync") && rdev
->raid_disk
>= 0 &&
2816 !test_bit(Journal
, &rdev
->flags
)) {
2817 if (rdev
->mddev
->pers
== NULL
) {
2818 clear_bit(In_sync
, &rdev
->flags
);
2819 rdev
->saved_raid_disk
= rdev
->raid_disk
;
2820 rdev
->raid_disk
= -1;
2823 } else if (cmd_match(buf
, "write_error")) {
2824 set_bit(WriteErrorSeen
, &rdev
->flags
);
2826 } else if (cmd_match(buf
, "-write_error")) {
2827 clear_bit(WriteErrorSeen
, &rdev
->flags
);
2829 } else if (cmd_match(buf
, "want_replacement")) {
2830 /* Any non-spare device that is not a replacement can
2831 * become want_replacement at any time, but we then need to
2832 * check if recovery is needed.
2834 if (rdev
->raid_disk
>= 0 &&
2835 !test_bit(Journal
, &rdev
->flags
) &&
2836 !test_bit(Replacement
, &rdev
->flags
))
2837 set_bit(WantReplacement
, &rdev
->flags
);
2838 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2839 md_wakeup_thread(rdev
->mddev
->thread
);
2841 } else if (cmd_match(buf
, "-want_replacement")) {
2842 /* Clearing 'want_replacement' is always allowed.
2843 * Once replacements starts it is too late though.
2846 clear_bit(WantReplacement
, &rdev
->flags
);
2847 } else if (cmd_match(buf
, "replacement")) {
2848 /* Can only set a device as a replacement when array has not
2849 * yet been started. Once running, replacement is automatic
2850 * from spares, or by assigning 'slot'.
2852 if (rdev
->mddev
->pers
)
2855 set_bit(Replacement
, &rdev
->flags
);
2858 } else if (cmd_match(buf
, "-replacement")) {
2859 /* Similarly, can only clear Replacement before start */
2860 if (rdev
->mddev
->pers
)
2863 clear_bit(Replacement
, &rdev
->flags
);
2866 } else if (cmd_match(buf
, "re-add")) {
2867 if (test_bit(Faulty
, &rdev
->flags
) && (rdev
->raid_disk
== -1) &&
2868 rdev
->saved_raid_disk
>= 0) {
2869 /* clear_bit is performed _after_ all the devices
2870 * have their local Faulty bit cleared. If any writes
2871 * happen in the meantime in the local node, they
2872 * will land in the local bitmap, which will be synced
2873 * by this node eventually
2875 if (!mddev_is_clustered(rdev
->mddev
) ||
2876 (err
= md_cluster_ops
->gather_bitmaps(rdev
)) == 0) {
2877 clear_bit(Faulty
, &rdev
->flags
);
2878 err
= add_bound_rdev(rdev
);
2882 } else if (cmd_match(buf
, "external_bbl") && (rdev
->mddev
->external
)) {
2883 set_bit(ExternalBbl
, &rdev
->flags
);
2884 rdev
->badblocks
.shift
= 0;
2886 } else if (cmd_match(buf
, "-external_bbl") && (rdev
->mddev
->external
)) {
2887 clear_bit(ExternalBbl
, &rdev
->flags
);
2891 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2892 return err
? err
: len
;
2894 static struct rdev_sysfs_entry rdev_state
=
2895 __ATTR_PREALLOC(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
2898 errors_show(struct md_rdev
*rdev
, char *page
)
2900 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
2904 errors_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2909 rv
= kstrtouint(buf
, 10, &n
);
2912 atomic_set(&rdev
->corrected_errors
, n
);
2915 static struct rdev_sysfs_entry rdev_errors
=
2916 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
2919 slot_show(struct md_rdev
*rdev
, char *page
)
2921 if (test_bit(Journal
, &rdev
->flags
))
2922 return sprintf(page
, "journal\n");
2923 else if (rdev
->raid_disk
< 0)
2924 return sprintf(page
, "none\n");
2926 return sprintf(page
, "%d\n", rdev
->raid_disk
);
2930 slot_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2935 if (test_bit(Journal
, &rdev
->flags
))
2937 if (strncmp(buf
, "none", 4)==0)
2940 err
= kstrtouint(buf
, 10, (unsigned int *)&slot
);
2944 if (rdev
->mddev
->pers
&& slot
== -1) {
2945 /* Setting 'slot' on an active array requires also
2946 * updating the 'rd%d' link, and communicating
2947 * with the personality with ->hot_*_disk.
2948 * For now we only support removing
2949 * failed/spare devices. This normally happens automatically,
2950 * but not when the metadata is externally managed.
2952 if (rdev
->raid_disk
== -1)
2954 /* personality does all needed checks */
2955 if (rdev
->mddev
->pers
->hot_remove_disk
== NULL
)
2957 clear_bit(Blocked
, &rdev
->flags
);
2958 remove_and_add_spares(rdev
->mddev
, rdev
);
2959 if (rdev
->raid_disk
>= 0)
2961 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2962 md_wakeup_thread(rdev
->mddev
->thread
);
2963 } else if (rdev
->mddev
->pers
) {
2964 /* Activating a spare .. or possibly reactivating
2965 * if we ever get bitmaps working here.
2969 if (rdev
->raid_disk
!= -1)
2972 if (test_bit(MD_RECOVERY_RUNNING
, &rdev
->mddev
->recovery
))
2975 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2978 if (slot
>= rdev
->mddev
->raid_disks
&&
2979 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2982 rdev
->raid_disk
= slot
;
2983 if (test_bit(In_sync
, &rdev
->flags
))
2984 rdev
->saved_raid_disk
= slot
;
2986 rdev
->saved_raid_disk
= -1;
2987 clear_bit(In_sync
, &rdev
->flags
);
2988 clear_bit(Bitmap_sync
, &rdev
->flags
);
2989 err
= rdev
->mddev
->pers
->
2990 hot_add_disk(rdev
->mddev
, rdev
);
2992 rdev
->raid_disk
= -1;
2995 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2996 if (sysfs_link_rdev(rdev
->mddev
, rdev
))
2997 /* failure here is OK */;
2998 /* don't wakeup anyone, leave that to userspace. */
3000 if (slot
>= rdev
->mddev
->raid_disks
&&
3001 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
3003 rdev
->raid_disk
= slot
;
3004 /* assume it is working */
3005 clear_bit(Faulty
, &rdev
->flags
);
3006 clear_bit(WriteMostly
, &rdev
->flags
);
3007 set_bit(In_sync
, &rdev
->flags
);
3008 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
3013 static struct rdev_sysfs_entry rdev_slot
=
3014 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
3017 offset_show(struct md_rdev
*rdev
, char *page
)
3019 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
3023 offset_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3025 unsigned long long offset
;
3026 if (kstrtoull(buf
, 10, &offset
) < 0)
3028 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
3030 if (rdev
->sectors
&& rdev
->mddev
->external
)
3031 /* Must set offset before size, so overlap checks
3034 rdev
->data_offset
= offset
;
3035 rdev
->new_data_offset
= offset
;
3039 static struct rdev_sysfs_entry rdev_offset
=
3040 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
3042 static ssize_t
new_offset_show(struct md_rdev
*rdev
, char *page
)
3044 return sprintf(page
, "%llu\n",
3045 (unsigned long long)rdev
->new_data_offset
);
3048 static ssize_t
new_offset_store(struct md_rdev
*rdev
,
3049 const char *buf
, size_t len
)
3051 unsigned long long new_offset
;
3052 struct mddev
*mddev
= rdev
->mddev
;
3054 if (kstrtoull(buf
, 10, &new_offset
) < 0)
3057 if (mddev
->sync_thread
||
3058 test_bit(MD_RECOVERY_RUNNING
,&mddev
->recovery
))
3060 if (new_offset
== rdev
->data_offset
)
3061 /* reset is always permitted */
3063 else if (new_offset
> rdev
->data_offset
) {
3064 /* must not push array size beyond rdev_sectors */
3065 if (new_offset
- rdev
->data_offset
3066 + mddev
->dev_sectors
> rdev
->sectors
)
3069 /* Metadata worries about other space details. */
3071 /* decreasing the offset is inconsistent with a backwards
3074 if (new_offset
< rdev
->data_offset
&&
3075 mddev
->reshape_backwards
)
3077 /* Increasing offset is inconsistent with forwards
3078 * reshape. reshape_direction should be set to
3079 * 'backwards' first.
3081 if (new_offset
> rdev
->data_offset
&&
3082 !mddev
->reshape_backwards
)
3085 if (mddev
->pers
&& mddev
->persistent
&&
3086 !super_types
[mddev
->major_version
]
3087 .allow_new_offset(rdev
, new_offset
))
3089 rdev
->new_data_offset
= new_offset
;
3090 if (new_offset
> rdev
->data_offset
)
3091 mddev
->reshape_backwards
= 1;
3092 else if (new_offset
< rdev
->data_offset
)
3093 mddev
->reshape_backwards
= 0;
3097 static struct rdev_sysfs_entry rdev_new_offset
=
3098 __ATTR(new_offset
, S_IRUGO
|S_IWUSR
, new_offset_show
, new_offset_store
);
3101 rdev_size_show(struct md_rdev
*rdev
, char *page
)
3103 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
3106 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
3108 /* check if two start/length pairs overlap */
3116 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
3118 unsigned long long blocks
;
3121 if (kstrtoull(buf
, 10, &blocks
) < 0)
3124 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
3125 return -EINVAL
; /* sector conversion overflow */
3128 if (new != blocks
* 2)
3129 return -EINVAL
; /* unsigned long long to sector_t overflow */
3136 rdev_size_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3138 struct mddev
*my_mddev
= rdev
->mddev
;
3139 sector_t oldsectors
= rdev
->sectors
;
3142 if (test_bit(Journal
, &rdev
->flags
))
3144 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
3146 if (rdev
->data_offset
!= rdev
->new_data_offset
)
3147 return -EINVAL
; /* too confusing */
3148 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
3149 if (my_mddev
->persistent
) {
3150 sectors
= super_types
[my_mddev
->major_version
].
3151 rdev_size_change(rdev
, sectors
);
3154 } else if (!sectors
)
3155 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
3157 if (!my_mddev
->pers
->resize
)
3158 /* Cannot change size for RAID0 or Linear etc */
3161 if (sectors
< my_mddev
->dev_sectors
)
3162 return -EINVAL
; /* component must fit device */
3164 rdev
->sectors
= sectors
;
3165 if (sectors
> oldsectors
&& my_mddev
->external
) {
3166 /* Need to check that all other rdevs with the same
3167 * ->bdev do not overlap. 'rcu' is sufficient to walk
3168 * the rdev lists safely.
3169 * This check does not provide a hard guarantee, it
3170 * just helps avoid dangerous mistakes.
3172 struct mddev
*mddev
;
3174 struct list_head
*tmp
;
3177 for_each_mddev(mddev
, tmp
) {
3178 struct md_rdev
*rdev2
;
3180 rdev_for_each(rdev2
, mddev
)
3181 if (rdev
->bdev
== rdev2
->bdev
&&
3183 overlaps(rdev
->data_offset
, rdev
->sectors
,
3196 /* Someone else could have slipped in a size
3197 * change here, but doing so is just silly.
3198 * We put oldsectors back because we *know* it is
3199 * safe, and trust userspace not to race with
3202 rdev
->sectors
= oldsectors
;
3209 static struct rdev_sysfs_entry rdev_size
=
3210 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
3212 static ssize_t
recovery_start_show(struct md_rdev
*rdev
, char *page
)
3214 unsigned long long recovery_start
= rdev
->recovery_offset
;
3216 if (test_bit(In_sync
, &rdev
->flags
) ||
3217 recovery_start
== MaxSector
)
3218 return sprintf(page
, "none\n");
3220 return sprintf(page
, "%llu\n", recovery_start
);
3223 static ssize_t
recovery_start_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3225 unsigned long long recovery_start
;
3227 if (cmd_match(buf
, "none"))
3228 recovery_start
= MaxSector
;
3229 else if (kstrtoull(buf
, 10, &recovery_start
))
3232 if (rdev
->mddev
->pers
&&
3233 rdev
->raid_disk
>= 0)
3236 rdev
->recovery_offset
= recovery_start
;
3237 if (recovery_start
== MaxSector
)
3238 set_bit(In_sync
, &rdev
->flags
);
3240 clear_bit(In_sync
, &rdev
->flags
);
3244 static struct rdev_sysfs_entry rdev_recovery_start
=
3245 __ATTR(recovery_start
, S_IRUGO
|S_IWUSR
, recovery_start_show
, recovery_start_store
);
3247 /* sysfs access to bad-blocks list.
3248 * We present two files.
3249 * 'bad-blocks' lists sector numbers and lengths of ranges that
3250 * are recorded as bad. The list is truncated to fit within
3251 * the one-page limit of sysfs.
3252 * Writing "sector length" to this file adds an acknowledged
3254 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3255 * been acknowledged. Writing to this file adds bad blocks
3256 * without acknowledging them. This is largely for testing.
3258 static ssize_t
bb_show(struct md_rdev
*rdev
, char *page
)
3260 return badblocks_show(&rdev
->badblocks
, page
, 0);
3262 static ssize_t
bb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
3264 int rv
= badblocks_store(&rdev
->badblocks
, page
, len
, 0);
3265 /* Maybe that ack was all we needed */
3266 if (test_and_clear_bit(BlockedBadBlocks
, &rdev
->flags
))
3267 wake_up(&rdev
->blocked_wait
);
3270 static struct rdev_sysfs_entry rdev_bad_blocks
=
3271 __ATTR(bad_blocks
, S_IRUGO
|S_IWUSR
, bb_show
, bb_store
);
3273 static ssize_t
ubb_show(struct md_rdev
*rdev
, char *page
)
3275 return badblocks_show(&rdev
->badblocks
, page
, 1);
3277 static ssize_t
ubb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
3279 return badblocks_store(&rdev
->badblocks
, page
, len
, 1);
3281 static struct rdev_sysfs_entry rdev_unack_bad_blocks
=
3282 __ATTR(unacknowledged_bad_blocks
, S_IRUGO
|S_IWUSR
, ubb_show
, ubb_store
);
3285 ppl_sector_show(struct md_rdev
*rdev
, char *page
)
3287 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->ppl
.sector
);
3291 ppl_sector_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3293 unsigned long long sector
;
3295 if (kstrtoull(buf
, 10, §or
) < 0)
3297 if (sector
!= (sector_t
)sector
)
3300 if (rdev
->mddev
->pers
&& test_bit(MD_HAS_PPL
, &rdev
->mddev
->flags
) &&
3301 rdev
->raid_disk
>= 0)
3304 if (rdev
->mddev
->persistent
) {
3305 if (rdev
->mddev
->major_version
== 0)
3307 if ((sector
> rdev
->sb_start
&&
3308 sector
- rdev
->sb_start
> S16_MAX
) ||
3309 (sector
< rdev
->sb_start
&&
3310 rdev
->sb_start
- sector
> -S16_MIN
))
3312 rdev
->ppl
.offset
= sector
- rdev
->sb_start
;
3313 } else if (!rdev
->mddev
->external
) {
3316 rdev
->ppl
.sector
= sector
;
3320 static struct rdev_sysfs_entry rdev_ppl_sector
=
3321 __ATTR(ppl_sector
, S_IRUGO
|S_IWUSR
, ppl_sector_show
, ppl_sector_store
);
3324 ppl_size_show(struct md_rdev
*rdev
, char *page
)
3326 return sprintf(page
, "%u\n", rdev
->ppl
.size
);
3330 ppl_size_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3334 if (kstrtouint(buf
, 10, &size
) < 0)
3337 if (rdev
->mddev
->pers
&& test_bit(MD_HAS_PPL
, &rdev
->mddev
->flags
) &&
3338 rdev
->raid_disk
>= 0)
3341 if (rdev
->mddev
->persistent
) {
3342 if (rdev
->mddev
->major_version
== 0)
3346 } else if (!rdev
->mddev
->external
) {
3349 rdev
->ppl
.size
= size
;
3353 static struct rdev_sysfs_entry rdev_ppl_size
=
3354 __ATTR(ppl_size
, S_IRUGO
|S_IWUSR
, ppl_size_show
, ppl_size_store
);
3356 static struct attribute
*rdev_default_attrs
[] = {
3361 &rdev_new_offset
.attr
,
3363 &rdev_recovery_start
.attr
,
3364 &rdev_bad_blocks
.attr
,
3365 &rdev_unack_bad_blocks
.attr
,
3366 &rdev_ppl_sector
.attr
,
3367 &rdev_ppl_size
.attr
,
3371 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
3373 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3374 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3380 return entry
->show(rdev
, page
);
3384 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
3385 const char *page
, size_t length
)
3387 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3388 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3390 struct mddev
*mddev
= rdev
->mddev
;
3394 if (!capable(CAP_SYS_ADMIN
))
3396 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
3398 if (rdev
->mddev
== NULL
)
3401 rv
= entry
->store(rdev
, page
, length
);
3402 mddev_unlock(mddev
);
3407 static void rdev_free(struct kobject
*ko
)
3409 struct md_rdev
*rdev
= container_of(ko
, struct md_rdev
, kobj
);
3412 static const struct sysfs_ops rdev_sysfs_ops
= {
3413 .show
= rdev_attr_show
,
3414 .store
= rdev_attr_store
,
3416 static struct kobj_type rdev_ktype
= {
3417 .release
= rdev_free
,
3418 .sysfs_ops
= &rdev_sysfs_ops
,
3419 .default_attrs
= rdev_default_attrs
,
3422 int md_rdev_init(struct md_rdev
*rdev
)
3425 rdev
->saved_raid_disk
= -1;
3426 rdev
->raid_disk
= -1;
3428 rdev
->data_offset
= 0;
3429 rdev
->new_data_offset
= 0;
3430 rdev
->sb_events
= 0;
3431 rdev
->last_read_error
= 0;
3432 rdev
->sb_loaded
= 0;
3433 rdev
->bb_page
= NULL
;
3434 atomic_set(&rdev
->nr_pending
, 0);
3435 atomic_set(&rdev
->read_errors
, 0);
3436 atomic_set(&rdev
->corrected_errors
, 0);
3438 INIT_LIST_HEAD(&rdev
->same_set
);
3439 init_waitqueue_head(&rdev
->blocked_wait
);
3441 /* Add space to store bad block list.
3442 * This reserves the space even on arrays where it cannot
3443 * be used - I wonder if that matters
3445 return badblocks_init(&rdev
->badblocks
, 0);
3447 EXPORT_SYMBOL_GPL(md_rdev_init
);
3449 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3451 * mark the device faulty if:
3453 * - the device is nonexistent (zero size)
3454 * - the device has no valid superblock
3456 * a faulty rdev _never_ has rdev->sb set.
3458 static struct md_rdev
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
3460 char b
[BDEVNAME_SIZE
];
3462 struct md_rdev
*rdev
;
3465 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
3467 return ERR_PTR(-ENOMEM
);
3469 err
= md_rdev_init(rdev
);
3472 err
= alloc_disk_sb(rdev
);
3476 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
3480 kobject_init(&rdev
->kobj
, &rdev_ktype
);
3482 size
= i_size_read(rdev
->bdev
->bd_inode
) >> BLOCK_SIZE_BITS
;
3484 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
3485 bdevname(rdev
->bdev
,b
));
3490 if (super_format
>= 0) {
3491 err
= super_types
[super_format
].
3492 load_super(rdev
, NULL
, super_minor
);
3493 if (err
== -EINVAL
) {
3494 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
3495 bdevname(rdev
->bdev
,b
),
3496 super_format
, super_minor
);
3500 pr_warn("md: could not read %s's sb, not importing!\n",
3501 bdevname(rdev
->bdev
,b
));
3511 md_rdev_clear(rdev
);
3513 return ERR_PTR(err
);
3517 * Check a full RAID array for plausibility
3520 static void analyze_sbs(struct mddev
*mddev
)
3523 struct md_rdev
*rdev
, *freshest
, *tmp
;
3524 char b
[BDEVNAME_SIZE
];
3527 rdev_for_each_safe(rdev
, tmp
, mddev
)
3528 switch (super_types
[mddev
->major_version
].
3529 load_super(rdev
, freshest
, mddev
->minor_version
)) {
3536 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
3537 bdevname(rdev
->bdev
,b
));
3538 md_kick_rdev_from_array(rdev
);
3541 super_types
[mddev
->major_version
].
3542 validate_super(mddev
, freshest
);
3545 rdev_for_each_safe(rdev
, tmp
, mddev
) {
3546 if (mddev
->max_disks
&&
3547 (rdev
->desc_nr
>= mddev
->max_disks
||
3548 i
> mddev
->max_disks
)) {
3549 pr_warn("md: %s: %s: only %d devices permitted\n",
3550 mdname(mddev
), bdevname(rdev
->bdev
, b
),
3552 md_kick_rdev_from_array(rdev
);
3555 if (rdev
!= freshest
) {
3556 if (super_types
[mddev
->major_version
].
3557 validate_super(mddev
, rdev
)) {
3558 pr_warn("md: kicking non-fresh %s from array!\n",
3559 bdevname(rdev
->bdev
,b
));
3560 md_kick_rdev_from_array(rdev
);
3564 if (mddev
->level
== LEVEL_MULTIPATH
) {
3565 rdev
->desc_nr
= i
++;
3566 rdev
->raid_disk
= rdev
->desc_nr
;
3567 set_bit(In_sync
, &rdev
->flags
);
3568 } else if (rdev
->raid_disk
>=
3569 (mddev
->raid_disks
- min(0, mddev
->delta_disks
)) &&
3570 !test_bit(Journal
, &rdev
->flags
)) {
3571 rdev
->raid_disk
= -1;
3572 clear_bit(In_sync
, &rdev
->flags
);
3577 /* Read a fixed-point number.
3578 * Numbers in sysfs attributes should be in "standard" units where
3579 * possible, so time should be in seconds.
3580 * However we internally use a a much smaller unit such as
3581 * milliseconds or jiffies.
3582 * This function takes a decimal number with a possible fractional
3583 * component, and produces an integer which is the result of
3584 * multiplying that number by 10^'scale'.
3585 * all without any floating-point arithmetic.
3587 int strict_strtoul_scaled(const char *cp
, unsigned long *res
, int scale
)
3589 unsigned long result
= 0;
3591 while (isdigit(*cp
) || (*cp
== '.' && decimals
< 0)) {
3594 else if (decimals
< scale
) {
3597 result
= result
* 10 + value
;
3609 while (decimals
< scale
) {
3618 safe_delay_show(struct mddev
*mddev
, char *page
)
3620 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
3621 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
3624 safe_delay_store(struct mddev
*mddev
, const char *cbuf
, size_t len
)
3628 if (mddev_is_clustered(mddev
)) {
3629 pr_warn("md: Safemode is disabled for clustered mode\n");
3633 if (strict_strtoul_scaled(cbuf
, &msec
, 3) < 0)
3636 mddev
->safemode_delay
= 0;
3638 unsigned long old_delay
= mddev
->safemode_delay
;
3639 unsigned long new_delay
= (msec
*HZ
)/1000;
3643 mddev
->safemode_delay
= new_delay
;
3644 if (new_delay
< old_delay
|| old_delay
== 0)
3645 mod_timer(&mddev
->safemode_timer
, jiffies
+1);
3649 static struct md_sysfs_entry md_safe_delay
=
3650 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
3653 level_show(struct mddev
*mddev
, char *page
)
3655 struct md_personality
*p
;
3657 spin_lock(&mddev
->lock
);
3660 ret
= sprintf(page
, "%s\n", p
->name
);
3661 else if (mddev
->clevel
[0])
3662 ret
= sprintf(page
, "%s\n", mddev
->clevel
);
3663 else if (mddev
->level
!= LEVEL_NONE
)
3664 ret
= sprintf(page
, "%d\n", mddev
->level
);
3667 spin_unlock(&mddev
->lock
);
3672 level_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3677 struct md_personality
*pers
, *oldpers
;
3679 void *priv
, *oldpriv
;
3680 struct md_rdev
*rdev
;
3682 if (slen
== 0 || slen
>= sizeof(clevel
))
3685 rv
= mddev_lock(mddev
);
3689 if (mddev
->pers
== NULL
) {
3690 strncpy(mddev
->clevel
, buf
, slen
);
3691 if (mddev
->clevel
[slen
-1] == '\n')
3693 mddev
->clevel
[slen
] = 0;
3694 mddev
->level
= LEVEL_NONE
;
3702 /* request to change the personality. Need to ensure:
3703 * - array is not engaged in resync/recovery/reshape
3704 * - old personality can be suspended
3705 * - new personality will access other array.
3709 if (mddev
->sync_thread
||
3710 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3711 mddev
->reshape_position
!= MaxSector
||
3712 mddev
->sysfs_active
)
3716 if (!mddev
->pers
->quiesce
) {
3717 pr_warn("md: %s: %s does not support online personality change\n",
3718 mdname(mddev
), mddev
->pers
->name
);
3722 /* Now find the new personality */
3723 strncpy(clevel
, buf
, slen
);
3724 if (clevel
[slen
-1] == '\n')
3727 if (kstrtol(clevel
, 10, &level
))
3730 if (request_module("md-%s", clevel
) != 0)
3731 request_module("md-level-%s", clevel
);
3732 spin_lock(&pers_lock
);
3733 pers
= find_pers(level
, clevel
);
3734 if (!pers
|| !try_module_get(pers
->owner
)) {
3735 spin_unlock(&pers_lock
);
3736 pr_warn("md: personality %s not loaded\n", clevel
);
3740 spin_unlock(&pers_lock
);
3742 if (pers
== mddev
->pers
) {
3743 /* Nothing to do! */
3744 module_put(pers
->owner
);
3748 if (!pers
->takeover
) {
3749 module_put(pers
->owner
);
3750 pr_warn("md: %s: %s does not support personality takeover\n",
3751 mdname(mddev
), clevel
);
3756 rdev_for_each(rdev
, mddev
)
3757 rdev
->new_raid_disk
= rdev
->raid_disk
;
3759 /* ->takeover must set new_* and/or delta_disks
3760 * if it succeeds, and may set them when it fails.
3762 priv
= pers
->takeover(mddev
);
3764 mddev
->new_level
= mddev
->level
;
3765 mddev
->new_layout
= mddev
->layout
;
3766 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3767 mddev
->raid_disks
-= mddev
->delta_disks
;
3768 mddev
->delta_disks
= 0;
3769 mddev
->reshape_backwards
= 0;
3770 module_put(pers
->owner
);
3771 pr_warn("md: %s: %s would not accept array\n",
3772 mdname(mddev
), clevel
);
3777 /* Looks like we have a winner */
3778 mddev_suspend(mddev
);
3779 mddev_detach(mddev
);
3781 spin_lock(&mddev
->lock
);
3782 oldpers
= mddev
->pers
;
3783 oldpriv
= mddev
->private;
3785 mddev
->private = priv
;
3786 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3787 mddev
->level
= mddev
->new_level
;
3788 mddev
->layout
= mddev
->new_layout
;
3789 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
3790 mddev
->delta_disks
= 0;
3791 mddev
->reshape_backwards
= 0;
3792 mddev
->degraded
= 0;
3793 spin_unlock(&mddev
->lock
);
3795 if (oldpers
->sync_request
== NULL
&&
3797 /* We are converting from a no-redundancy array
3798 * to a redundancy array and metadata is managed
3799 * externally so we need to be sure that writes
3800 * won't block due to a need to transition
3802 * until external management is started.
3805 mddev
->safemode_delay
= 0;
3806 mddev
->safemode
= 0;
3809 oldpers
->free(mddev
, oldpriv
);
3811 if (oldpers
->sync_request
== NULL
&&
3812 pers
->sync_request
!= NULL
) {
3813 /* need to add the md_redundancy_group */
3814 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
3815 pr_warn("md: cannot register extra attributes for %s\n",
3817 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, "sync_action");
3819 if (oldpers
->sync_request
!= NULL
&&
3820 pers
->sync_request
== NULL
) {
3821 /* need to remove the md_redundancy_group */
3822 if (mddev
->to_remove
== NULL
)
3823 mddev
->to_remove
= &md_redundancy_group
;
3826 module_put(oldpers
->owner
);
3828 rdev_for_each(rdev
, mddev
) {
3829 if (rdev
->raid_disk
< 0)
3831 if (rdev
->new_raid_disk
>= mddev
->raid_disks
)
3832 rdev
->new_raid_disk
= -1;
3833 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3835 sysfs_unlink_rdev(mddev
, rdev
);
3837 rdev_for_each(rdev
, mddev
) {
3838 if (rdev
->raid_disk
< 0)
3840 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3842 rdev
->raid_disk
= rdev
->new_raid_disk
;
3843 if (rdev
->raid_disk
< 0)
3844 clear_bit(In_sync
, &rdev
->flags
);
3846 if (sysfs_link_rdev(mddev
, rdev
))
3847 pr_warn("md: cannot register rd%d for %s after level change\n",
3848 rdev
->raid_disk
, mdname(mddev
));
3852 if (pers
->sync_request
== NULL
) {
3853 /* this is now an array without redundancy, so
3854 * it must always be in_sync
3857 del_timer_sync(&mddev
->safemode_timer
);
3859 blk_set_stacking_limits(&mddev
->queue
->limits
);
3861 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
3862 mddev_resume(mddev
);
3864 md_update_sb(mddev
, 1);
3865 sysfs_notify(&mddev
->kobj
, NULL
, "level");
3866 md_new_event(mddev
);
3869 mddev_unlock(mddev
);
3873 static struct md_sysfs_entry md_level
=
3874 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
3877 layout_show(struct mddev
*mddev
, char *page
)
3879 /* just a number, not meaningful for all levels */
3880 if (mddev
->reshape_position
!= MaxSector
&&
3881 mddev
->layout
!= mddev
->new_layout
)
3882 return sprintf(page
, "%d (%d)\n",
3883 mddev
->new_layout
, mddev
->layout
);
3884 return sprintf(page
, "%d\n", mddev
->layout
);
3888 layout_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3893 err
= kstrtouint(buf
, 10, &n
);
3896 err
= mddev_lock(mddev
);
3901 if (mddev
->pers
->check_reshape
== NULL
)
3906 mddev
->new_layout
= n
;
3907 err
= mddev
->pers
->check_reshape(mddev
);
3909 mddev
->new_layout
= mddev
->layout
;
3912 mddev
->new_layout
= n
;
3913 if (mddev
->reshape_position
== MaxSector
)
3916 mddev_unlock(mddev
);
3919 static struct md_sysfs_entry md_layout
=
3920 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
3923 raid_disks_show(struct mddev
*mddev
, char *page
)
3925 if (mddev
->raid_disks
== 0)
3927 if (mddev
->reshape_position
!= MaxSector
&&
3928 mddev
->delta_disks
!= 0)
3929 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
3930 mddev
->raid_disks
- mddev
->delta_disks
);
3931 return sprintf(page
, "%d\n", mddev
->raid_disks
);
3934 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
);
3937 raid_disks_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3942 err
= kstrtouint(buf
, 10, &n
);
3946 err
= mddev_lock(mddev
);
3950 err
= update_raid_disks(mddev
, n
);
3951 else if (mddev
->reshape_position
!= MaxSector
) {
3952 struct md_rdev
*rdev
;
3953 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
3956 rdev_for_each(rdev
, mddev
) {
3958 rdev
->data_offset
< rdev
->new_data_offset
)
3961 rdev
->data_offset
> rdev
->new_data_offset
)
3965 mddev
->delta_disks
= n
- olddisks
;
3966 mddev
->raid_disks
= n
;
3967 mddev
->reshape_backwards
= (mddev
->delta_disks
< 0);
3969 mddev
->raid_disks
= n
;
3971 mddev_unlock(mddev
);
3972 return err
? err
: len
;
3974 static struct md_sysfs_entry md_raid_disks
=
3975 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
3978 chunk_size_show(struct mddev
*mddev
, char *page
)
3980 if (mddev
->reshape_position
!= MaxSector
&&
3981 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
3982 return sprintf(page
, "%d (%d)\n",
3983 mddev
->new_chunk_sectors
<< 9,
3984 mddev
->chunk_sectors
<< 9);
3985 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
3989 chunk_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3994 err
= kstrtoul(buf
, 10, &n
);
3998 err
= mddev_lock(mddev
);
4002 if (mddev
->pers
->check_reshape
== NULL
)
4007 mddev
->new_chunk_sectors
= n
>> 9;
4008 err
= mddev
->pers
->check_reshape(mddev
);
4010 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4013 mddev
->new_chunk_sectors
= n
>> 9;
4014 if (mddev
->reshape_position
== MaxSector
)
4015 mddev
->chunk_sectors
= n
>> 9;
4017 mddev_unlock(mddev
);
4020 static struct md_sysfs_entry md_chunk_size
=
4021 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
4024 resync_start_show(struct mddev
*mddev
, char *page
)
4026 if (mddev
->recovery_cp
== MaxSector
)
4027 return sprintf(page
, "none\n");
4028 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
4032 resync_start_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4034 unsigned long long n
;
4037 if (cmd_match(buf
, "none"))
4040 err
= kstrtoull(buf
, 10, &n
);
4043 if (n
!= (sector_t
)n
)
4047 err
= mddev_lock(mddev
);
4050 if (mddev
->pers
&& !test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
4054 mddev
->recovery_cp
= n
;
4056 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
4058 mddev_unlock(mddev
);
4061 static struct md_sysfs_entry md_resync_start
=
4062 __ATTR_PREALLOC(resync_start
, S_IRUGO
|S_IWUSR
,
4063 resync_start_show
, resync_start_store
);
4066 * The array state can be:
4069 * No devices, no size, no level
4070 * Equivalent to STOP_ARRAY ioctl
4072 * May have some settings, but array is not active
4073 * all IO results in error
4074 * When written, doesn't tear down array, but just stops it
4075 * suspended (not supported yet)
4076 * All IO requests will block. The array can be reconfigured.
4077 * Writing this, if accepted, will block until array is quiescent
4079 * no resync can happen. no superblocks get written.
4080 * write requests fail
4082 * like readonly, but behaves like 'clean' on a write request.
4084 * clean - no pending writes, but otherwise active.
4085 * When written to inactive array, starts without resync
4086 * If a write request arrives then
4087 * if metadata is known, mark 'dirty' and switch to 'active'.
4088 * if not known, block and switch to write-pending
4089 * If written to an active array that has pending writes, then fails.
4091 * fully active: IO and resync can be happening.
4092 * When written to inactive array, starts with resync
4095 * clean, but writes are blocked waiting for 'active' to be written.
4098 * like active, but no writes have been seen for a while (100msec).
4101 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
4102 write_pending
, active_idle
, bad_word
};
4103 static char *array_states
[] = {
4104 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
4105 "write-pending", "active-idle", NULL
};
4107 static int match_word(const char *word
, char **list
)
4110 for (n
=0; list
[n
]; n
++)
4111 if (cmd_match(word
, list
[n
]))
4117 array_state_show(struct mddev
*mddev
, char *page
)
4119 enum array_state st
= inactive
;
4130 spin_lock(&mddev
->lock
);
4131 if (test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
))
4133 else if (mddev
->in_sync
)
4135 else if (mddev
->safemode
)
4139 spin_unlock(&mddev
->lock
);
4142 if (list_empty(&mddev
->disks
) &&
4143 mddev
->raid_disks
== 0 &&
4144 mddev
->dev_sectors
== 0)
4149 return sprintf(page
, "%s\n", array_states
[st
]);
4152 static int do_md_stop(struct mddev
*mddev
, int ro
, struct block_device
*bdev
);
4153 static int md_set_readonly(struct mddev
*mddev
, struct block_device
*bdev
);
4154 static int do_md_run(struct mddev
*mddev
);
4155 static int restart_array(struct mddev
*mddev
);
4158 array_state_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4161 enum array_state st
= match_word(buf
, array_states
);
4163 if (mddev
->pers
&& (st
== active
|| st
== clean
) && mddev
->ro
!= 1) {
4164 /* don't take reconfig_mutex when toggling between
4167 spin_lock(&mddev
->lock
);
4169 restart_array(mddev
);
4170 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
4171 md_wakeup_thread(mddev
->thread
);
4172 wake_up(&mddev
->sb_wait
);
4173 } else /* st == clean */ {
4174 restart_array(mddev
);
4175 if (!set_in_sync(mddev
))
4179 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4180 spin_unlock(&mddev
->lock
);
4183 err
= mddev_lock(mddev
);
4191 /* stopping an active array */
4192 err
= do_md_stop(mddev
, 0, NULL
);
4195 /* stopping an active array */
4197 err
= do_md_stop(mddev
, 2, NULL
);
4199 err
= 0; /* already inactive */
4202 break; /* not supported yet */
4205 err
= md_set_readonly(mddev
, NULL
);
4208 set_disk_ro(mddev
->gendisk
, 1);
4209 err
= do_md_run(mddev
);
4215 err
= md_set_readonly(mddev
, NULL
);
4216 else if (mddev
->ro
== 1)
4217 err
= restart_array(mddev
);
4220 set_disk_ro(mddev
->gendisk
, 0);
4224 err
= do_md_run(mddev
);
4229 err
= restart_array(mddev
);
4232 spin_lock(&mddev
->lock
);
4233 if (!set_in_sync(mddev
))
4235 spin_unlock(&mddev
->lock
);
4241 err
= restart_array(mddev
);
4244 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
4245 wake_up(&mddev
->sb_wait
);
4249 set_disk_ro(mddev
->gendisk
, 0);
4250 err
= do_md_run(mddev
);
4255 /* these cannot be set */
4260 if (mddev
->hold_active
== UNTIL_IOCTL
)
4261 mddev
->hold_active
= 0;
4262 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4264 mddev_unlock(mddev
);
4267 static struct md_sysfs_entry md_array_state
=
4268 __ATTR_PREALLOC(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
4271 max_corrected_read_errors_show(struct mddev
*mddev
, char *page
) {
4272 return sprintf(page
, "%d\n",
4273 atomic_read(&mddev
->max_corr_read_errors
));
4277 max_corrected_read_errors_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4282 rv
= kstrtouint(buf
, 10, &n
);
4285 atomic_set(&mddev
->max_corr_read_errors
, n
);
4289 static struct md_sysfs_entry max_corr_read_errors
=
4290 __ATTR(max_read_errors
, S_IRUGO
|S_IWUSR
, max_corrected_read_errors_show
,
4291 max_corrected_read_errors_store
);
4294 null_show(struct mddev
*mddev
, char *page
)
4300 new_dev_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4302 /* buf must be %d:%d\n? giving major and minor numbers */
4303 /* The new device is added to the array.
4304 * If the array has a persistent superblock, we read the
4305 * superblock to initialise info and check validity.
4306 * Otherwise, only checking done is that in bind_rdev_to_array,
4307 * which mainly checks size.
4310 int major
= simple_strtoul(buf
, &e
, 10);
4313 struct md_rdev
*rdev
;
4316 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
4318 minor
= simple_strtoul(e
+1, &e
, 10);
4319 if (*e
&& *e
!= '\n')
4321 dev
= MKDEV(major
, minor
);
4322 if (major
!= MAJOR(dev
) ||
4323 minor
!= MINOR(dev
))
4326 flush_workqueue(md_misc_wq
);
4328 err
= mddev_lock(mddev
);
4331 if (mddev
->persistent
) {
4332 rdev
= md_import_device(dev
, mddev
->major_version
,
4333 mddev
->minor_version
);
4334 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
4335 struct md_rdev
*rdev0
4336 = list_entry(mddev
->disks
.next
,
4337 struct md_rdev
, same_set
);
4338 err
= super_types
[mddev
->major_version
]
4339 .load_super(rdev
, rdev0
, mddev
->minor_version
);
4343 } else if (mddev
->external
)
4344 rdev
= md_import_device(dev
, -2, -1);
4346 rdev
= md_import_device(dev
, -1, -1);
4349 mddev_unlock(mddev
);
4350 return PTR_ERR(rdev
);
4352 err
= bind_rdev_to_array(rdev
, mddev
);
4356 mddev_unlock(mddev
);
4358 md_new_event(mddev
);
4359 return err
? err
: len
;
4362 static struct md_sysfs_entry md_new_device
=
4363 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
4366 bitmap_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4369 unsigned long chunk
, end_chunk
;
4372 err
= mddev_lock(mddev
);
4377 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4379 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
4380 if (buf
== end
) break;
4381 if (*end
== '-') { /* range */
4383 end_chunk
= simple_strtoul(buf
, &end
, 0);
4384 if (buf
== end
) break;
4386 if (*end
&& !isspace(*end
)) break;
4387 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
4388 buf
= skip_spaces(end
);
4390 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
4392 mddev_unlock(mddev
);
4396 static struct md_sysfs_entry md_bitmap
=
4397 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
4400 size_show(struct mddev
*mddev
, char *page
)
4402 return sprintf(page
, "%llu\n",
4403 (unsigned long long)mddev
->dev_sectors
/ 2);
4406 static int update_size(struct mddev
*mddev
, sector_t num_sectors
);
4409 size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4411 /* If array is inactive, we can reduce the component size, but
4412 * not increase it (except from 0).
4413 * If array is active, we can try an on-line resize
4416 int err
= strict_blocks_to_sectors(buf
, §ors
);
4420 err
= mddev_lock(mddev
);
4424 err
= update_size(mddev
, sectors
);
4426 md_update_sb(mddev
, 1);
4428 if (mddev
->dev_sectors
== 0 ||
4429 mddev
->dev_sectors
> sectors
)
4430 mddev
->dev_sectors
= sectors
;
4434 mddev_unlock(mddev
);
4435 return err
? err
: len
;
4438 static struct md_sysfs_entry md_size
=
4439 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
4441 /* Metadata version.
4443 * 'none' for arrays with no metadata (good luck...)
4444 * 'external' for arrays with externally managed metadata,
4445 * or N.M for internally known formats
4448 metadata_show(struct mddev
*mddev
, char *page
)
4450 if (mddev
->persistent
)
4451 return sprintf(page
, "%d.%d\n",
4452 mddev
->major_version
, mddev
->minor_version
);
4453 else if (mddev
->external
)
4454 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
4456 return sprintf(page
, "none\n");
4460 metadata_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4465 /* Changing the details of 'external' metadata is
4466 * always permitted. Otherwise there must be
4467 * no devices attached to the array.
4470 err
= mddev_lock(mddev
);
4474 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
4476 else if (!list_empty(&mddev
->disks
))
4480 if (cmd_match(buf
, "none")) {
4481 mddev
->persistent
= 0;
4482 mddev
->external
= 0;
4483 mddev
->major_version
= 0;
4484 mddev
->minor_version
= 90;
4487 if (strncmp(buf
, "external:", 9) == 0) {
4488 size_t namelen
= len
-9;
4489 if (namelen
>= sizeof(mddev
->metadata_type
))
4490 namelen
= sizeof(mddev
->metadata_type
)-1;
4491 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
4492 mddev
->metadata_type
[namelen
] = 0;
4493 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
4494 mddev
->metadata_type
[--namelen
] = 0;
4495 mddev
->persistent
= 0;
4496 mddev
->external
= 1;
4497 mddev
->major_version
= 0;
4498 mddev
->minor_version
= 90;
4501 major
= simple_strtoul(buf
, &e
, 10);
4503 if (e
==buf
|| *e
!= '.')
4506 minor
= simple_strtoul(buf
, &e
, 10);
4507 if (e
==buf
|| (*e
&& *e
!= '\n') )
4510 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
4512 mddev
->major_version
= major
;
4513 mddev
->minor_version
= minor
;
4514 mddev
->persistent
= 1;
4515 mddev
->external
= 0;
4518 mddev_unlock(mddev
);
4522 static struct md_sysfs_entry md_metadata
=
4523 __ATTR_PREALLOC(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
4526 action_show(struct mddev
*mddev
, char *page
)
4528 char *type
= "idle";
4529 unsigned long recovery
= mddev
->recovery
;
4530 if (test_bit(MD_RECOVERY_FROZEN
, &recovery
))
4532 else if (test_bit(MD_RECOVERY_RUNNING
, &recovery
) ||
4533 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &recovery
))) {
4534 if (test_bit(MD_RECOVERY_RESHAPE
, &recovery
))
4536 else if (test_bit(MD_RECOVERY_SYNC
, &recovery
)) {
4537 if (!test_bit(MD_RECOVERY_REQUESTED
, &recovery
))
4539 else if (test_bit(MD_RECOVERY_CHECK
, &recovery
))
4543 } else if (test_bit(MD_RECOVERY_RECOVER
, &recovery
))
4545 else if (mddev
->reshape_position
!= MaxSector
)
4548 return sprintf(page
, "%s\n", type
);
4552 action_store(struct mddev
*mddev
, const char *page
, size_t len
)
4554 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
4558 if (cmd_match(page
, "idle") || cmd_match(page
, "frozen")) {
4559 if (cmd_match(page
, "frozen"))
4560 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4562 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4563 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
4564 mddev_lock(mddev
) == 0) {
4565 flush_workqueue(md_misc_wq
);
4566 if (mddev
->sync_thread
) {
4567 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4568 md_reap_sync_thread(mddev
);
4570 mddev_unlock(mddev
);
4572 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4574 else if (cmd_match(page
, "resync"))
4575 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4576 else if (cmd_match(page
, "recover")) {
4577 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4578 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
4579 } else if (cmd_match(page
, "reshape")) {
4581 if (mddev
->pers
->start_reshape
== NULL
)
4583 err
= mddev_lock(mddev
);
4585 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4588 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4589 err
= mddev
->pers
->start_reshape(mddev
);
4591 mddev_unlock(mddev
);
4595 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4597 if (cmd_match(page
, "check"))
4598 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
4599 else if (!cmd_match(page
, "repair"))
4601 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4602 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
4603 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
4605 if (mddev
->ro
== 2) {
4606 /* A write to sync_action is enough to justify
4607 * canceling read-auto mode
4610 md_wakeup_thread(mddev
->sync_thread
);
4612 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4613 md_wakeup_thread(mddev
->thread
);
4614 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4618 static struct md_sysfs_entry md_scan_mode
=
4619 __ATTR_PREALLOC(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
4622 last_sync_action_show(struct mddev
*mddev
, char *page
)
4624 return sprintf(page
, "%s\n", mddev
->last_sync_action
);
4627 static struct md_sysfs_entry md_last_scan_mode
= __ATTR_RO(last_sync_action
);
4630 mismatch_cnt_show(struct mddev
*mddev
, char *page
)
4632 return sprintf(page
, "%llu\n",
4633 (unsigned long long)
4634 atomic64_read(&mddev
->resync_mismatches
));
4637 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
4640 sync_min_show(struct mddev
*mddev
, char *page
)
4642 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
4643 mddev
->sync_speed_min
? "local": "system");
4647 sync_min_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4652 if (strncmp(buf
, "system", 6)==0) {
4655 rv
= kstrtouint(buf
, 10, &min
);
4661 mddev
->sync_speed_min
= min
;
4665 static struct md_sysfs_entry md_sync_min
=
4666 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
4669 sync_max_show(struct mddev
*mddev
, char *page
)
4671 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
4672 mddev
->sync_speed_max
? "local": "system");
4676 sync_max_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4681 if (strncmp(buf
, "system", 6)==0) {
4684 rv
= kstrtouint(buf
, 10, &max
);
4690 mddev
->sync_speed_max
= max
;
4694 static struct md_sysfs_entry md_sync_max
=
4695 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
4698 degraded_show(struct mddev
*mddev
, char *page
)
4700 return sprintf(page
, "%d\n", mddev
->degraded
);
4702 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
4705 sync_force_parallel_show(struct mddev
*mddev
, char *page
)
4707 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
4711 sync_force_parallel_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4715 if (kstrtol(buf
, 10, &n
))
4718 if (n
!= 0 && n
!= 1)
4721 mddev
->parallel_resync
= n
;
4723 if (mddev
->sync_thread
)
4724 wake_up(&resync_wait
);
4729 /* force parallel resync, even with shared block devices */
4730 static struct md_sysfs_entry md_sync_force_parallel
=
4731 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
4732 sync_force_parallel_show
, sync_force_parallel_store
);
4735 sync_speed_show(struct mddev
*mddev
, char *page
)
4737 unsigned long resync
, dt
, db
;
4738 if (mddev
->curr_resync
== 0)
4739 return sprintf(page
, "none\n");
4740 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
4741 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
4743 db
= resync
- mddev
->resync_mark_cnt
;
4744 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
4747 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
4750 sync_completed_show(struct mddev
*mddev
, char *page
)
4752 unsigned long long max_sectors
, resync
;
4754 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4755 return sprintf(page
, "none\n");
4757 if (mddev
->curr_resync
== 1 ||
4758 mddev
->curr_resync
== 2)
4759 return sprintf(page
, "delayed\n");
4761 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
4762 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
4763 max_sectors
= mddev
->resync_max_sectors
;
4765 max_sectors
= mddev
->dev_sectors
;
4767 resync
= mddev
->curr_resync_completed
;
4768 return sprintf(page
, "%llu / %llu\n", resync
, max_sectors
);
4771 static struct md_sysfs_entry md_sync_completed
=
4772 __ATTR_PREALLOC(sync_completed
, S_IRUGO
, sync_completed_show
, NULL
);
4775 min_sync_show(struct mddev
*mddev
, char *page
)
4777 return sprintf(page
, "%llu\n",
4778 (unsigned long long)mddev
->resync_min
);
4781 min_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4783 unsigned long long min
;
4786 if (kstrtoull(buf
, 10, &min
))
4789 spin_lock(&mddev
->lock
);
4791 if (min
> mddev
->resync_max
)
4795 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4798 /* Round down to multiple of 4K for safety */
4799 mddev
->resync_min
= round_down(min
, 8);
4803 spin_unlock(&mddev
->lock
);
4807 static struct md_sysfs_entry md_min_sync
=
4808 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
4811 max_sync_show(struct mddev
*mddev
, char *page
)
4813 if (mddev
->resync_max
== MaxSector
)
4814 return sprintf(page
, "max\n");
4816 return sprintf(page
, "%llu\n",
4817 (unsigned long long)mddev
->resync_max
);
4820 max_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4823 spin_lock(&mddev
->lock
);
4824 if (strncmp(buf
, "max", 3) == 0)
4825 mddev
->resync_max
= MaxSector
;
4827 unsigned long long max
;
4831 if (kstrtoull(buf
, 10, &max
))
4833 if (max
< mddev
->resync_min
)
4837 if (max
< mddev
->resync_max
&&
4839 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4842 /* Must be a multiple of chunk_size */
4843 chunk
= mddev
->chunk_sectors
;
4845 sector_t temp
= max
;
4848 if (sector_div(temp
, chunk
))
4851 mddev
->resync_max
= max
;
4853 wake_up(&mddev
->recovery_wait
);
4856 spin_unlock(&mddev
->lock
);
4860 static struct md_sysfs_entry md_max_sync
=
4861 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
4864 suspend_lo_show(struct mddev
*mddev
, char *page
)
4866 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
4870 suspend_lo_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4872 unsigned long long new;
4875 err
= kstrtoull(buf
, 10, &new);
4878 if (new != (sector_t
)new)
4881 err
= mddev_lock(mddev
);
4885 if (mddev
->pers
== NULL
||
4886 mddev
->pers
->quiesce
== NULL
)
4888 mddev_suspend(mddev
);
4889 mddev
->suspend_lo
= new;
4890 mddev_resume(mddev
);
4894 mddev_unlock(mddev
);
4897 static struct md_sysfs_entry md_suspend_lo
=
4898 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
4901 suspend_hi_show(struct mddev
*mddev
, char *page
)
4903 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
4907 suspend_hi_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4909 unsigned long long new;
4912 err
= kstrtoull(buf
, 10, &new);
4915 if (new != (sector_t
)new)
4918 err
= mddev_lock(mddev
);
4922 if (mddev
->pers
== NULL
)
4925 mddev_suspend(mddev
);
4926 mddev
->suspend_hi
= new;
4927 mddev_resume(mddev
);
4931 mddev_unlock(mddev
);
4934 static struct md_sysfs_entry md_suspend_hi
=
4935 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
4938 reshape_position_show(struct mddev
*mddev
, char *page
)
4940 if (mddev
->reshape_position
!= MaxSector
)
4941 return sprintf(page
, "%llu\n",
4942 (unsigned long long)mddev
->reshape_position
);
4943 strcpy(page
, "none\n");
4948 reshape_position_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4950 struct md_rdev
*rdev
;
4951 unsigned long long new;
4954 err
= kstrtoull(buf
, 10, &new);
4957 if (new != (sector_t
)new)
4959 err
= mddev_lock(mddev
);
4965 mddev
->reshape_position
= new;
4966 mddev
->delta_disks
= 0;
4967 mddev
->reshape_backwards
= 0;
4968 mddev
->new_level
= mddev
->level
;
4969 mddev
->new_layout
= mddev
->layout
;
4970 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4971 rdev_for_each(rdev
, mddev
)
4972 rdev
->new_data_offset
= rdev
->data_offset
;
4975 mddev_unlock(mddev
);
4979 static struct md_sysfs_entry md_reshape_position
=
4980 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
4981 reshape_position_store
);
4984 reshape_direction_show(struct mddev
*mddev
, char *page
)
4986 return sprintf(page
, "%s\n",
4987 mddev
->reshape_backwards
? "backwards" : "forwards");
4991 reshape_direction_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4996 if (cmd_match(buf
, "forwards"))
4998 else if (cmd_match(buf
, "backwards"))
5002 if (mddev
->reshape_backwards
== backwards
)
5005 err
= mddev_lock(mddev
);
5008 /* check if we are allowed to change */
5009 if (mddev
->delta_disks
)
5011 else if (mddev
->persistent
&&
5012 mddev
->major_version
== 0)
5015 mddev
->reshape_backwards
= backwards
;
5016 mddev_unlock(mddev
);
5020 static struct md_sysfs_entry md_reshape_direction
=
5021 __ATTR(reshape_direction
, S_IRUGO
|S_IWUSR
, reshape_direction_show
,
5022 reshape_direction_store
);
5025 array_size_show(struct mddev
*mddev
, char *page
)
5027 if (mddev
->external_size
)
5028 return sprintf(page
, "%llu\n",
5029 (unsigned long long)mddev
->array_sectors
/2);
5031 return sprintf(page
, "default\n");
5035 array_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5040 err
= mddev_lock(mddev
);
5044 /* cluster raid doesn't support change array_sectors */
5045 if (mddev_is_clustered(mddev
)) {
5046 mddev_unlock(mddev
);
5050 if (strncmp(buf
, "default", 7) == 0) {
5052 sectors
= mddev
->pers
->size(mddev
, 0, 0);
5054 sectors
= mddev
->array_sectors
;
5056 mddev
->external_size
= 0;
5058 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
5060 else if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
5063 mddev
->external_size
= 1;
5067 mddev
->array_sectors
= sectors
;
5069 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
5070 revalidate_disk(mddev
->gendisk
);
5073 mddev_unlock(mddev
);
5077 static struct md_sysfs_entry md_array_size
=
5078 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
5082 consistency_policy_show(struct mddev
*mddev
, char *page
)
5086 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
)) {
5087 ret
= sprintf(page
, "journal\n");
5088 } else if (test_bit(MD_HAS_PPL
, &mddev
->flags
)) {
5089 ret
= sprintf(page
, "ppl\n");
5090 } else if (mddev
->bitmap
) {
5091 ret
= sprintf(page
, "bitmap\n");
5092 } else if (mddev
->pers
) {
5093 if (mddev
->pers
->sync_request
)
5094 ret
= sprintf(page
, "resync\n");
5096 ret
= sprintf(page
, "none\n");
5098 ret
= sprintf(page
, "unknown\n");
5105 consistency_policy_store(struct mddev
*mddev
, const char *buf
, size_t len
)
5110 if (mddev
->pers
->change_consistency_policy
)
5111 err
= mddev
->pers
->change_consistency_policy(mddev
, buf
);
5114 } else if (mddev
->external
&& strncmp(buf
, "ppl", 3) == 0) {
5115 set_bit(MD_HAS_PPL
, &mddev
->flags
);
5120 return err
? err
: len
;
5123 static struct md_sysfs_entry md_consistency_policy
=
5124 __ATTR(consistency_policy
, S_IRUGO
| S_IWUSR
, consistency_policy_show
,
5125 consistency_policy_store
);
5127 static struct attribute
*md_default_attrs
[] = {
5130 &md_raid_disks
.attr
,
5131 &md_chunk_size
.attr
,
5133 &md_resync_start
.attr
,
5135 &md_new_device
.attr
,
5136 &md_safe_delay
.attr
,
5137 &md_array_state
.attr
,
5138 &md_reshape_position
.attr
,
5139 &md_reshape_direction
.attr
,
5140 &md_array_size
.attr
,
5141 &max_corr_read_errors
.attr
,
5142 &md_consistency_policy
.attr
,
5146 static struct attribute
*md_redundancy_attrs
[] = {
5148 &md_last_scan_mode
.attr
,
5149 &md_mismatches
.attr
,
5152 &md_sync_speed
.attr
,
5153 &md_sync_force_parallel
.attr
,
5154 &md_sync_completed
.attr
,
5157 &md_suspend_lo
.attr
,
5158 &md_suspend_hi
.attr
,
5163 static struct attribute_group md_redundancy_group
= {
5165 .attrs
= md_redundancy_attrs
,
5169 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
5171 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
5172 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
5177 spin_lock(&all_mddevs_lock
);
5178 if (list_empty(&mddev
->all_mddevs
)) {
5179 spin_unlock(&all_mddevs_lock
);
5183 spin_unlock(&all_mddevs_lock
);
5185 rv
= entry
->show(mddev
, page
);
5191 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
5192 const char *page
, size_t length
)
5194 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
5195 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
5200 if (!capable(CAP_SYS_ADMIN
))
5202 spin_lock(&all_mddevs_lock
);
5203 if (list_empty(&mddev
->all_mddevs
)) {
5204 spin_unlock(&all_mddevs_lock
);
5208 spin_unlock(&all_mddevs_lock
);
5209 rv
= entry
->store(mddev
, page
, length
);
5214 static void md_free(struct kobject
*ko
)
5216 struct mddev
*mddev
= container_of(ko
, struct mddev
, kobj
);
5218 if (mddev
->sysfs_state
)
5219 sysfs_put(mddev
->sysfs_state
);
5222 del_gendisk(mddev
->gendisk
);
5224 blk_cleanup_queue(mddev
->queue
);
5226 put_disk(mddev
->gendisk
);
5227 percpu_ref_exit(&mddev
->writes_pending
);
5229 bioset_exit(&mddev
->bio_set
);
5230 bioset_exit(&mddev
->sync_set
);
5234 static const struct sysfs_ops md_sysfs_ops
= {
5235 .show
= md_attr_show
,
5236 .store
= md_attr_store
,
5238 static struct kobj_type md_ktype
= {
5240 .sysfs_ops
= &md_sysfs_ops
,
5241 .default_attrs
= md_default_attrs
,
5246 static void mddev_delayed_delete(struct work_struct
*ws
)
5248 struct mddev
*mddev
= container_of(ws
, struct mddev
, del_work
);
5250 sysfs_remove_group(&mddev
->kobj
, &md_bitmap_group
);
5251 kobject_del(&mddev
->kobj
);
5252 kobject_put(&mddev
->kobj
);
5255 static void no_op(struct percpu_ref
*r
) {}
5257 int mddev_init_writes_pending(struct mddev
*mddev
)
5259 if (mddev
->writes_pending
.percpu_count_ptr
)
5261 if (percpu_ref_init(&mddev
->writes_pending
, no_op
, 0, GFP_KERNEL
) < 0)
5263 /* We want to start with the refcount at zero */
5264 percpu_ref_put(&mddev
->writes_pending
);
5267 EXPORT_SYMBOL_GPL(mddev_init_writes_pending
);
5269 static int md_alloc(dev_t dev
, char *name
)
5272 * If dev is zero, name is the name of a device to allocate with
5273 * an arbitrary minor number. It will be "md_???"
5274 * If dev is non-zero it must be a device number with a MAJOR of
5275 * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then
5276 * the device is being created by opening a node in /dev.
5277 * If "name" is not NULL, the device is being created by
5278 * writing to /sys/module/md_mod/parameters/new_array.
5280 static DEFINE_MUTEX(disks_mutex
);
5281 struct mddev
*mddev
= mddev_find(dev
);
5282 struct gendisk
*disk
;
5291 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
5292 shift
= partitioned
? MdpMinorShift
: 0;
5293 unit
= MINOR(mddev
->unit
) >> shift
;
5295 /* wait for any previous instance of this device to be
5296 * completely removed (mddev_delayed_delete).
5298 flush_workqueue(md_misc_wq
);
5300 mutex_lock(&disks_mutex
);
5306 /* Need to ensure that 'name' is not a duplicate.
5308 struct mddev
*mddev2
;
5309 spin_lock(&all_mddevs_lock
);
5311 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
5312 if (mddev2
->gendisk
&&
5313 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
5314 spin_unlock(&all_mddevs_lock
);
5317 spin_unlock(&all_mddevs_lock
);
5321 * Creating /dev/mdNNN via "newarray", so adjust hold_active.
5323 mddev
->hold_active
= UNTIL_STOP
;
5326 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
5329 mddev
->queue
->queuedata
= mddev
;
5331 blk_queue_make_request(mddev
->queue
, md_make_request
);
5332 blk_set_stacking_limits(&mddev
->queue
->limits
);
5334 disk
= alloc_disk(1 << shift
);
5336 blk_cleanup_queue(mddev
->queue
);
5337 mddev
->queue
= NULL
;
5340 disk
->major
= MAJOR(mddev
->unit
);
5341 disk
->first_minor
= unit
<< shift
;
5343 strcpy(disk
->disk_name
, name
);
5344 else if (partitioned
)
5345 sprintf(disk
->disk_name
, "md_d%d", unit
);
5347 sprintf(disk
->disk_name
, "md%d", unit
);
5348 disk
->fops
= &md_fops
;
5349 disk
->private_data
= mddev
;
5350 disk
->queue
= mddev
->queue
;
5351 blk_queue_write_cache(mddev
->queue
, true, true);
5352 /* Allow extended partitions. This makes the
5353 * 'mdp' device redundant, but we can't really
5356 disk
->flags
|= GENHD_FL_EXT_DEVT
;
5357 mddev
->gendisk
= disk
;
5358 /* As soon as we call add_disk(), another thread could get
5359 * through to md_open, so make sure it doesn't get too far
5361 mutex_lock(&mddev
->open_mutex
);
5364 error
= kobject_add(&mddev
->kobj
, &disk_to_dev(disk
)->kobj
, "%s", "md");
5366 /* This isn't possible, but as kobject_init_and_add is marked
5367 * __must_check, we must do something with the result
5369 pr_debug("md: cannot register %s/md - name in use\n",
5373 if (mddev
->kobj
.sd
&&
5374 sysfs_create_group(&mddev
->kobj
, &md_bitmap_group
))
5375 pr_debug("pointless warning\n");
5376 mutex_unlock(&mddev
->open_mutex
);
5378 mutex_unlock(&disks_mutex
);
5379 if (!error
&& mddev
->kobj
.sd
) {
5380 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
5381 mddev
->sysfs_state
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "array_state");
5387 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
5390 md_alloc(dev
, NULL
);
5394 static int add_named_array(const char *val
, const struct kernel_param
*kp
)
5397 * val must be "md_*" or "mdNNN".
5398 * For "md_*" we allocate an array with a large free minor number, and
5399 * set the name to val. val must not already be an active name.
5400 * For "mdNNN" we allocate an array with the minor number NNN
5401 * which must not already be in use.
5403 int len
= strlen(val
);
5404 char buf
[DISK_NAME_LEN
];
5405 unsigned long devnum
;
5407 while (len
&& val
[len
-1] == '\n')
5409 if (len
>= DISK_NAME_LEN
)
5411 strlcpy(buf
, val
, len
+1);
5412 if (strncmp(buf
, "md_", 3) == 0)
5413 return md_alloc(0, buf
);
5414 if (strncmp(buf
, "md", 2) == 0 &&
5416 kstrtoul(buf
+2, 10, &devnum
) == 0 &&
5417 devnum
<= MINORMASK
)
5418 return md_alloc(MKDEV(MD_MAJOR
, devnum
), NULL
);
5423 static void md_safemode_timeout(struct timer_list
*t
)
5425 struct mddev
*mddev
= from_timer(mddev
, t
, safemode_timer
);
5427 mddev
->safemode
= 1;
5428 if (mddev
->external
)
5429 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5431 md_wakeup_thread(mddev
->thread
);
5434 static int start_dirty_degraded
;
5436 int md_run(struct mddev
*mddev
)
5439 struct md_rdev
*rdev
;
5440 struct md_personality
*pers
;
5442 if (list_empty(&mddev
->disks
))
5443 /* cannot run an array with no devices.. */
5448 /* Cannot run until previous stop completes properly */
5449 if (mddev
->sysfs_active
)
5453 * Analyze all RAID superblock(s)
5455 if (!mddev
->raid_disks
) {
5456 if (!mddev
->persistent
)
5461 if (mddev
->level
!= LEVEL_NONE
)
5462 request_module("md-level-%d", mddev
->level
);
5463 else if (mddev
->clevel
[0])
5464 request_module("md-%s", mddev
->clevel
);
5467 * Drop all container device buffers, from now on
5468 * the only valid external interface is through the md
5471 mddev
->has_superblocks
= false;
5472 rdev_for_each(rdev
, mddev
) {
5473 if (test_bit(Faulty
, &rdev
->flags
))
5475 sync_blockdev(rdev
->bdev
);
5476 invalidate_bdev(rdev
->bdev
);
5477 if (mddev
->ro
!= 1 &&
5478 (bdev_read_only(rdev
->bdev
) ||
5479 bdev_read_only(rdev
->meta_bdev
))) {
5482 set_disk_ro(mddev
->gendisk
, 1);
5486 mddev
->has_superblocks
= true;
5488 /* perform some consistency tests on the device.
5489 * We don't want the data to overlap the metadata,
5490 * Internal Bitmap issues have been handled elsewhere.
5492 if (rdev
->meta_bdev
) {
5493 /* Nothing to check */;
5494 } else if (rdev
->data_offset
< rdev
->sb_start
) {
5495 if (mddev
->dev_sectors
&&
5496 rdev
->data_offset
+ mddev
->dev_sectors
5498 pr_warn("md: %s: data overlaps metadata\n",
5503 if (rdev
->sb_start
+ rdev
->sb_size
/512
5504 > rdev
->data_offset
) {
5505 pr_warn("md: %s: metadata overlaps data\n",
5510 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
5513 if (!bioset_initialized(&mddev
->bio_set
)) {
5514 err
= bioset_init(&mddev
->bio_set
, BIO_POOL_SIZE
, 0, BIOSET_NEED_BVECS
);
5518 if (!bioset_initialized(&mddev
->sync_set
)) {
5519 err
= bioset_init(&mddev
->sync_set
, BIO_POOL_SIZE
, 0, BIOSET_NEED_BVECS
);
5523 if (mddev
->flush_pool
== NULL
) {
5524 mddev
->flush_pool
= mempool_create(NR_FLUSH_INFOS
, flush_info_alloc
,
5525 flush_info_free
, mddev
);
5526 if (!mddev
->flush_pool
) {
5531 if (mddev
->flush_bio_pool
== NULL
) {
5532 mddev
->flush_bio_pool
= mempool_create(NR_FLUSH_BIOS
, flush_bio_alloc
,
5533 flush_bio_free
, mddev
);
5534 if (!mddev
->flush_bio_pool
) {
5540 spin_lock(&pers_lock
);
5541 pers
= find_pers(mddev
->level
, mddev
->clevel
);
5542 if (!pers
|| !try_module_get(pers
->owner
)) {
5543 spin_unlock(&pers_lock
);
5544 if (mddev
->level
!= LEVEL_NONE
)
5545 pr_warn("md: personality for level %d is not loaded!\n",
5548 pr_warn("md: personality for level %s is not loaded!\n",
5553 spin_unlock(&pers_lock
);
5554 if (mddev
->level
!= pers
->level
) {
5555 mddev
->level
= pers
->level
;
5556 mddev
->new_level
= pers
->level
;
5558 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
5560 if (mddev
->reshape_position
!= MaxSector
&&
5561 pers
->start_reshape
== NULL
) {
5562 /* This personality cannot handle reshaping... */
5563 module_put(pers
->owner
);
5568 if (pers
->sync_request
) {
5569 /* Warn if this is a potentially silly
5572 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
5573 struct md_rdev
*rdev2
;
5576 rdev_for_each(rdev
, mddev
)
5577 rdev_for_each(rdev2
, mddev
) {
5579 rdev
->bdev
->bd_contains
==
5580 rdev2
->bdev
->bd_contains
) {
5581 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5583 bdevname(rdev
->bdev
,b
),
5584 bdevname(rdev2
->bdev
,b2
));
5590 pr_warn("True protection against single-disk failure might be compromised.\n");
5593 mddev
->recovery
= 0;
5594 /* may be over-ridden by personality */
5595 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
5597 mddev
->ok_start_degraded
= start_dirty_degraded
;
5599 if (start_readonly
&& mddev
->ro
== 0)
5600 mddev
->ro
= 2; /* read-only, but switch on first write */
5602 err
= pers
->run(mddev
);
5604 pr_warn("md: pers->run() failed ...\n");
5605 else if (pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
5606 WARN_ONCE(!mddev
->external_size
,
5607 "%s: default size too small, but 'external_size' not in effect?\n",
5609 pr_warn("md: invalid array_size %llu > default size %llu\n",
5610 (unsigned long long)mddev
->array_sectors
/ 2,
5611 (unsigned long long)pers
->size(mddev
, 0, 0) / 2);
5614 if (err
== 0 && pers
->sync_request
&&
5615 (mddev
->bitmap_info
.file
|| mddev
->bitmap_info
.offset
)) {
5616 struct bitmap
*bitmap
;
5618 bitmap
= bitmap_create(mddev
, -1);
5619 if (IS_ERR(bitmap
)) {
5620 err
= PTR_ERR(bitmap
);
5621 pr_warn("%s: failed to create bitmap (%d)\n",
5622 mdname(mddev
), err
);
5624 mddev
->bitmap
= bitmap
;
5628 mddev_detach(mddev
);
5630 pers
->free(mddev
, mddev
->private);
5631 mddev
->private = NULL
;
5632 module_put(pers
->owner
);
5633 bitmap_destroy(mddev
);
5639 rdev_for_each(rdev
, mddev
) {
5640 if (rdev
->raid_disk
>= 0 &&
5641 !blk_queue_nonrot(bdev_get_queue(rdev
->bdev
))) {
5646 if (mddev
->degraded
)
5649 blk_queue_flag_set(QUEUE_FLAG_NONROT
, mddev
->queue
);
5651 blk_queue_flag_clear(QUEUE_FLAG_NONROT
, mddev
->queue
);
5652 mddev
->queue
->backing_dev_info
->congested_data
= mddev
;
5653 mddev
->queue
->backing_dev_info
->congested_fn
= md_congested
;
5655 if (pers
->sync_request
) {
5656 if (mddev
->kobj
.sd
&&
5657 sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
5658 pr_warn("md: cannot register extra attributes for %s\n",
5660 mddev
->sysfs_action
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "sync_action");
5661 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
5664 atomic_set(&mddev
->max_corr_read_errors
,
5665 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS
);
5666 mddev
->safemode
= 0;
5667 if (mddev_is_clustered(mddev
))
5668 mddev
->safemode_delay
= 0;
5670 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
5673 spin_lock(&mddev
->lock
);
5675 spin_unlock(&mddev
->lock
);
5676 rdev_for_each(rdev
, mddev
)
5677 if (rdev
->raid_disk
>= 0)
5678 if (sysfs_link_rdev(mddev
, rdev
))
5679 /* failure here is OK */;
5681 if (mddev
->degraded
&& !mddev
->ro
)
5682 /* This ensures that recovering status is reported immediately
5683 * via sysfs - until a lack of spares is confirmed.
5685 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5686 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5688 if (mddev
->sb_flags
)
5689 md_update_sb(mddev
, 0);
5691 md_new_event(mddev
);
5692 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5693 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
5694 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
5698 if (mddev
->flush_bio_pool
) {
5699 mempool_destroy(mddev
->flush_bio_pool
);
5700 mddev
->flush_bio_pool
= NULL
;
5702 if (mddev
->flush_pool
){
5703 mempool_destroy(mddev
->flush_pool
);
5704 mddev
->flush_pool
= NULL
;
5709 EXPORT_SYMBOL_GPL(md_run
);
5711 static int do_md_run(struct mddev
*mddev
)
5715 err
= md_run(mddev
);
5718 err
= bitmap_load(mddev
);
5720 bitmap_destroy(mddev
);
5724 if (mddev_is_clustered(mddev
))
5725 md_allow_write(mddev
);
5727 /* run start up tasks that require md_thread */
5730 md_wakeup_thread(mddev
->thread
);
5731 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
5733 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
5734 revalidate_disk(mddev
->gendisk
);
5736 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
5741 int md_start(struct mddev
*mddev
)
5745 if (mddev
->pers
->start
) {
5746 set_bit(MD_RECOVERY_WAIT
, &mddev
->recovery
);
5747 md_wakeup_thread(mddev
->thread
);
5748 ret
= mddev
->pers
->start(mddev
);
5749 clear_bit(MD_RECOVERY_WAIT
, &mddev
->recovery
);
5750 md_wakeup_thread(mddev
->sync_thread
);
5754 EXPORT_SYMBOL_GPL(md_start
);
5756 static int restart_array(struct mddev
*mddev
)
5758 struct gendisk
*disk
= mddev
->gendisk
;
5759 struct md_rdev
*rdev
;
5760 bool has_journal
= false;
5761 bool has_readonly
= false;
5763 /* Complain if it has no devices */
5764 if (list_empty(&mddev
->disks
))
5772 rdev_for_each_rcu(rdev
, mddev
) {
5773 if (test_bit(Journal
, &rdev
->flags
) &&
5774 !test_bit(Faulty
, &rdev
->flags
))
5776 if (bdev_read_only(rdev
->bdev
))
5777 has_readonly
= true;
5780 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
) && !has_journal
)
5781 /* Don't restart rw with journal missing/faulty */
5786 mddev
->safemode
= 0;
5788 set_disk_ro(disk
, 0);
5789 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev
));
5790 /* Kick recovery or resync if necessary */
5791 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5792 md_wakeup_thread(mddev
->thread
);
5793 md_wakeup_thread(mddev
->sync_thread
);
5794 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5798 static void md_clean(struct mddev
*mddev
)
5800 mddev
->array_sectors
= 0;
5801 mddev
->external_size
= 0;
5802 mddev
->dev_sectors
= 0;
5803 mddev
->raid_disks
= 0;
5804 mddev
->recovery_cp
= 0;
5805 mddev
->resync_min
= 0;
5806 mddev
->resync_max
= MaxSector
;
5807 mddev
->reshape_position
= MaxSector
;
5808 mddev
->external
= 0;
5809 mddev
->persistent
= 0;
5810 mddev
->level
= LEVEL_NONE
;
5811 mddev
->clevel
[0] = 0;
5813 mddev
->sb_flags
= 0;
5815 mddev
->metadata_type
[0] = 0;
5816 mddev
->chunk_sectors
= 0;
5817 mddev
->ctime
= mddev
->utime
= 0;
5819 mddev
->max_disks
= 0;
5821 mddev
->can_decrease_events
= 0;
5822 mddev
->delta_disks
= 0;
5823 mddev
->reshape_backwards
= 0;
5824 mddev
->new_level
= LEVEL_NONE
;
5825 mddev
->new_layout
= 0;
5826 mddev
->new_chunk_sectors
= 0;
5827 mddev
->curr_resync
= 0;
5828 atomic64_set(&mddev
->resync_mismatches
, 0);
5829 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
5830 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
5831 mddev
->recovery
= 0;
5834 mddev
->degraded
= 0;
5835 mddev
->safemode
= 0;
5836 mddev
->private = NULL
;
5837 mddev
->cluster_info
= NULL
;
5838 mddev
->bitmap_info
.offset
= 0;
5839 mddev
->bitmap_info
.default_offset
= 0;
5840 mddev
->bitmap_info
.default_space
= 0;
5841 mddev
->bitmap_info
.chunksize
= 0;
5842 mddev
->bitmap_info
.daemon_sleep
= 0;
5843 mddev
->bitmap_info
.max_write_behind
= 0;
5844 mddev
->bitmap_info
.nodes
= 0;
5847 static void __md_stop_writes(struct mddev
*mddev
)
5849 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5850 flush_workqueue(md_misc_wq
);
5851 if (mddev
->sync_thread
) {
5852 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5853 md_reap_sync_thread(mddev
);
5856 del_timer_sync(&mddev
->safemode_timer
);
5858 if (mddev
->pers
&& mddev
->pers
->quiesce
) {
5859 mddev
->pers
->quiesce(mddev
, 1);
5860 mddev
->pers
->quiesce(mddev
, 0);
5862 bitmap_flush(mddev
);
5864 if (mddev
->ro
== 0 &&
5865 ((!mddev
->in_sync
&& !mddev_is_clustered(mddev
)) ||
5867 /* mark array as shutdown cleanly */
5868 if (!mddev_is_clustered(mddev
))
5870 md_update_sb(mddev
, 1);
5874 void md_stop_writes(struct mddev
*mddev
)
5876 mddev_lock_nointr(mddev
);
5877 __md_stop_writes(mddev
);
5878 mddev_unlock(mddev
);
5880 EXPORT_SYMBOL_GPL(md_stop_writes
);
5882 static void mddev_detach(struct mddev
*mddev
)
5884 bitmap_wait_behind_writes(mddev
);
5885 if (mddev
->pers
&& mddev
->pers
->quiesce
) {
5886 mddev
->pers
->quiesce(mddev
, 1);
5887 mddev
->pers
->quiesce(mddev
, 0);
5889 md_unregister_thread(&mddev
->thread
);
5891 blk_sync_queue(mddev
->queue
); /* the unplug fn references 'conf'*/
5894 static void __md_stop(struct mddev
*mddev
)
5896 struct md_personality
*pers
= mddev
->pers
;
5897 bitmap_destroy(mddev
);
5898 mddev_detach(mddev
);
5899 /* Ensure ->event_work is done */
5900 flush_workqueue(md_misc_wq
);
5901 spin_lock(&mddev
->lock
);
5903 spin_unlock(&mddev
->lock
);
5904 pers
->free(mddev
, mddev
->private);
5905 mddev
->private = NULL
;
5906 if (pers
->sync_request
&& mddev
->to_remove
== NULL
)
5907 mddev
->to_remove
= &md_redundancy_group
;
5908 module_put(pers
->owner
);
5909 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5912 void md_stop(struct mddev
*mddev
)
5914 /* stop the array and free an attached data structures.
5915 * This is called from dm-raid
5918 if (mddev
->flush_bio_pool
) {
5919 mempool_destroy(mddev
->flush_bio_pool
);
5920 mddev
->flush_bio_pool
= NULL
;
5922 if (mddev
->flush_pool
) {
5923 mempool_destroy(mddev
->flush_pool
);
5924 mddev
->flush_pool
= NULL
;
5926 bioset_exit(&mddev
->bio_set
);
5927 bioset_exit(&mddev
->sync_set
);
5930 EXPORT_SYMBOL_GPL(md_stop
);
5932 static int md_set_readonly(struct mddev
*mddev
, struct block_device
*bdev
)
5937 if (!test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
)) {
5939 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5940 md_wakeup_thread(mddev
->thread
);
5942 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
5943 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5944 if (mddev
->sync_thread
)
5945 /* Thread might be blocked waiting for metadata update
5946 * which will now never happen */
5947 wake_up_process(mddev
->sync_thread
->tsk
);
5949 if (mddev
->external
&& test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
))
5951 mddev_unlock(mddev
);
5952 wait_event(resync_wait
, !test_bit(MD_RECOVERY_RUNNING
,
5954 wait_event(mddev
->sb_wait
,
5955 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
5956 mddev_lock_nointr(mddev
);
5958 mutex_lock(&mddev
->open_mutex
);
5959 if ((mddev
->pers
&& atomic_read(&mddev
->openers
) > !!bdev
) ||
5960 mddev
->sync_thread
||
5961 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
)) {
5962 pr_warn("md: %s still in use.\n",mdname(mddev
));
5964 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5965 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5966 md_wakeup_thread(mddev
->thread
);
5972 __md_stop_writes(mddev
);
5978 set_disk_ro(mddev
->gendisk
, 1);
5979 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5980 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5981 md_wakeup_thread(mddev
->thread
);
5982 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5986 mutex_unlock(&mddev
->open_mutex
);
5991 * 0 - completely stop and dis-assemble array
5992 * 2 - stop but do not disassemble array
5994 static int do_md_stop(struct mddev
*mddev
, int mode
,
5995 struct block_device
*bdev
)
5997 struct gendisk
*disk
= mddev
->gendisk
;
5998 struct md_rdev
*rdev
;
6001 if (!test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
)) {
6003 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
6004 md_wakeup_thread(mddev
->thread
);
6006 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
6007 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6008 if (mddev
->sync_thread
)
6009 /* Thread might be blocked waiting for metadata update
6010 * which will now never happen */
6011 wake_up_process(mddev
->sync_thread
->tsk
);
6013 mddev_unlock(mddev
);
6014 wait_event(resync_wait
, (mddev
->sync_thread
== NULL
&&
6015 !test_bit(MD_RECOVERY_RUNNING
,
6016 &mddev
->recovery
)));
6017 mddev_lock_nointr(mddev
);
6019 mutex_lock(&mddev
->open_mutex
);
6020 if ((mddev
->pers
&& atomic_read(&mddev
->openers
) > !!bdev
) ||
6021 mddev
->sysfs_active
||
6022 mddev
->sync_thread
||
6023 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
)) {
6024 pr_warn("md: %s still in use.\n",mdname(mddev
));
6025 mutex_unlock(&mddev
->open_mutex
);
6027 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
6028 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6029 md_wakeup_thread(mddev
->thread
);
6035 set_disk_ro(disk
, 0);
6037 __md_stop_writes(mddev
);
6039 mddev
->queue
->backing_dev_info
->congested_fn
= NULL
;
6041 /* tell userspace to handle 'inactive' */
6042 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6044 rdev_for_each(rdev
, mddev
)
6045 if (rdev
->raid_disk
>= 0)
6046 sysfs_unlink_rdev(mddev
, rdev
);
6048 set_capacity(disk
, 0);
6049 mutex_unlock(&mddev
->open_mutex
);
6051 revalidate_disk(disk
);
6056 mutex_unlock(&mddev
->open_mutex
);
6058 * Free resources if final stop
6061 pr_info("md: %s stopped.\n", mdname(mddev
));
6063 if (mddev
->bitmap_info
.file
) {
6064 struct file
*f
= mddev
->bitmap_info
.file
;
6065 spin_lock(&mddev
->lock
);
6066 mddev
->bitmap_info
.file
= NULL
;
6067 spin_unlock(&mddev
->lock
);
6070 mddev
->bitmap_info
.offset
= 0;
6072 export_array(mddev
);
6075 if (mddev
->hold_active
== UNTIL_STOP
)
6076 mddev
->hold_active
= 0;
6078 md_new_event(mddev
);
6079 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6084 static void autorun_array(struct mddev
*mddev
)
6086 struct md_rdev
*rdev
;
6089 if (list_empty(&mddev
->disks
))
6092 pr_info("md: running: ");
6094 rdev_for_each(rdev
, mddev
) {
6095 char b
[BDEVNAME_SIZE
];
6096 pr_cont("<%s>", bdevname(rdev
->bdev
,b
));
6100 err
= do_md_run(mddev
);
6102 pr_warn("md: do_md_run() returned %d\n", err
);
6103 do_md_stop(mddev
, 0, NULL
);
6108 * lets try to run arrays based on all disks that have arrived
6109 * until now. (those are in pending_raid_disks)
6111 * the method: pick the first pending disk, collect all disks with
6112 * the same UUID, remove all from the pending list and put them into
6113 * the 'same_array' list. Then order this list based on superblock
6114 * update time (freshest comes first), kick out 'old' disks and
6115 * compare superblocks. If everything's fine then run it.
6117 * If "unit" is allocated, then bump its reference count
6119 static void autorun_devices(int part
)
6121 struct md_rdev
*rdev0
, *rdev
, *tmp
;
6122 struct mddev
*mddev
;
6123 char b
[BDEVNAME_SIZE
];
6125 pr_info("md: autorun ...\n");
6126 while (!list_empty(&pending_raid_disks
)) {
6129 LIST_HEAD(candidates
);
6130 rdev0
= list_entry(pending_raid_disks
.next
,
6131 struct md_rdev
, same_set
);
6133 pr_debug("md: considering %s ...\n", bdevname(rdev0
->bdev
,b
));
6134 INIT_LIST_HEAD(&candidates
);
6135 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
6136 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
6137 pr_debug("md: adding %s ...\n",
6138 bdevname(rdev
->bdev
,b
));
6139 list_move(&rdev
->same_set
, &candidates
);
6142 * now we have a set of devices, with all of them having
6143 * mostly sane superblocks. It's time to allocate the
6147 dev
= MKDEV(mdp_major
,
6148 rdev0
->preferred_minor
<< MdpMinorShift
);
6149 unit
= MINOR(dev
) >> MdpMinorShift
;
6151 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
6154 if (rdev0
->preferred_minor
!= unit
) {
6155 pr_warn("md: unit number in %s is bad: %d\n",
6156 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
6160 md_probe(dev
, NULL
, NULL
);
6161 mddev
= mddev_find(dev
);
6162 if (!mddev
|| !mddev
->gendisk
) {
6167 if (mddev_lock(mddev
))
6168 pr_warn("md: %s locked, cannot run\n", mdname(mddev
));
6169 else if (mddev
->raid_disks
|| mddev
->major_version
6170 || !list_empty(&mddev
->disks
)) {
6171 pr_warn("md: %s already running, cannot run %s\n",
6172 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
6173 mddev_unlock(mddev
);
6175 pr_debug("md: created %s\n", mdname(mddev
));
6176 mddev
->persistent
= 1;
6177 rdev_for_each_list(rdev
, tmp
, &candidates
) {
6178 list_del_init(&rdev
->same_set
);
6179 if (bind_rdev_to_array(rdev
, mddev
))
6182 autorun_array(mddev
);
6183 mddev_unlock(mddev
);
6185 /* on success, candidates will be empty, on error
6188 rdev_for_each_list(rdev
, tmp
, &candidates
) {
6189 list_del_init(&rdev
->same_set
);
6194 pr_info("md: ... autorun DONE.\n");
6196 #endif /* !MODULE */
6198 static int get_version(void __user
*arg
)
6202 ver
.major
= MD_MAJOR_VERSION
;
6203 ver
.minor
= MD_MINOR_VERSION
;
6204 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
6206 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
6212 static int get_array_info(struct mddev
*mddev
, void __user
*arg
)
6214 mdu_array_info_t info
;
6215 int nr
,working
,insync
,failed
,spare
;
6216 struct md_rdev
*rdev
;
6218 nr
= working
= insync
= failed
= spare
= 0;
6220 rdev_for_each_rcu(rdev
, mddev
) {
6222 if (test_bit(Faulty
, &rdev
->flags
))
6226 if (test_bit(In_sync
, &rdev
->flags
))
6228 else if (test_bit(Journal
, &rdev
->flags
))
6229 /* TODO: add journal count to md_u.h */
6237 info
.major_version
= mddev
->major_version
;
6238 info
.minor_version
= mddev
->minor_version
;
6239 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
6240 info
.ctime
= clamp_t(time64_t
, mddev
->ctime
, 0, U32_MAX
);
6241 info
.level
= mddev
->level
;
6242 info
.size
= mddev
->dev_sectors
/ 2;
6243 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
6246 info
.raid_disks
= mddev
->raid_disks
;
6247 info
.md_minor
= mddev
->md_minor
;
6248 info
.not_persistent
= !mddev
->persistent
;
6250 info
.utime
= clamp_t(time64_t
, mddev
->utime
, 0, U32_MAX
);
6253 info
.state
= (1<<MD_SB_CLEAN
);
6254 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
6255 info
.state
|= (1<<MD_SB_BITMAP_PRESENT
);
6256 if (mddev_is_clustered(mddev
))
6257 info
.state
|= (1<<MD_SB_CLUSTERED
);
6258 info
.active_disks
= insync
;
6259 info
.working_disks
= working
;
6260 info
.failed_disks
= failed
;
6261 info
.spare_disks
= spare
;
6263 info
.layout
= mddev
->layout
;
6264 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
6266 if (copy_to_user(arg
, &info
, sizeof(info
)))
6272 static int get_bitmap_file(struct mddev
*mddev
, void __user
* arg
)
6274 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
6278 file
= kzalloc(sizeof(*file
), GFP_NOIO
);
6283 spin_lock(&mddev
->lock
);
6284 /* bitmap enabled */
6285 if (mddev
->bitmap_info
.file
) {
6286 ptr
= file_path(mddev
->bitmap_info
.file
, file
->pathname
,
6287 sizeof(file
->pathname
));
6291 memmove(file
->pathname
, ptr
,
6292 sizeof(file
->pathname
)-(ptr
-file
->pathname
));
6294 spin_unlock(&mddev
->lock
);
6297 copy_to_user(arg
, file
, sizeof(*file
)))
6304 static int get_disk_info(struct mddev
*mddev
, void __user
* arg
)
6306 mdu_disk_info_t info
;
6307 struct md_rdev
*rdev
;
6309 if (copy_from_user(&info
, arg
, sizeof(info
)))
6313 rdev
= md_find_rdev_nr_rcu(mddev
, info
.number
);
6315 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
6316 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
6317 info
.raid_disk
= rdev
->raid_disk
;
6319 if (test_bit(Faulty
, &rdev
->flags
))
6320 info
.state
|= (1<<MD_DISK_FAULTY
);
6321 else if (test_bit(In_sync
, &rdev
->flags
)) {
6322 info
.state
|= (1<<MD_DISK_ACTIVE
);
6323 info
.state
|= (1<<MD_DISK_SYNC
);
6325 if (test_bit(Journal
, &rdev
->flags
))
6326 info
.state
|= (1<<MD_DISK_JOURNAL
);
6327 if (test_bit(WriteMostly
, &rdev
->flags
))
6328 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
6329 if (test_bit(FailFast
, &rdev
->flags
))
6330 info
.state
|= (1<<MD_DISK_FAILFAST
);
6332 info
.major
= info
.minor
= 0;
6333 info
.raid_disk
= -1;
6334 info
.state
= (1<<MD_DISK_REMOVED
);
6338 if (copy_to_user(arg
, &info
, sizeof(info
)))
6344 static int add_new_disk(struct mddev
*mddev
, mdu_disk_info_t
*info
)
6346 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
6347 struct md_rdev
*rdev
;
6348 dev_t dev
= MKDEV(info
->major
,info
->minor
);
6350 if (mddev_is_clustered(mddev
) &&
6351 !(info
->state
& ((1 << MD_DISK_CLUSTER_ADD
) | (1 << MD_DISK_CANDIDATE
)))) {
6352 pr_warn("%s: Cannot add to clustered mddev.\n",
6357 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
6360 if (!mddev
->raid_disks
) {
6362 /* expecting a device which has a superblock */
6363 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
6365 pr_warn("md: md_import_device returned %ld\n",
6367 return PTR_ERR(rdev
);
6369 if (!list_empty(&mddev
->disks
)) {
6370 struct md_rdev
*rdev0
6371 = list_entry(mddev
->disks
.next
,
6372 struct md_rdev
, same_set
);
6373 err
= super_types
[mddev
->major_version
]
6374 .load_super(rdev
, rdev0
, mddev
->minor_version
);
6376 pr_warn("md: %s has different UUID to %s\n",
6377 bdevname(rdev
->bdev
,b
),
6378 bdevname(rdev0
->bdev
,b2
));
6383 err
= bind_rdev_to_array(rdev
, mddev
);
6390 * add_new_disk can be used once the array is assembled
6391 * to add "hot spares". They must already have a superblock
6396 if (!mddev
->pers
->hot_add_disk
) {
6397 pr_warn("%s: personality does not support diskops!\n",
6401 if (mddev
->persistent
)
6402 rdev
= md_import_device(dev
, mddev
->major_version
,
6403 mddev
->minor_version
);
6405 rdev
= md_import_device(dev
, -1, -1);
6407 pr_warn("md: md_import_device returned %ld\n",
6409 return PTR_ERR(rdev
);
6411 /* set saved_raid_disk if appropriate */
6412 if (!mddev
->persistent
) {
6413 if (info
->state
& (1<<MD_DISK_SYNC
) &&
6414 info
->raid_disk
< mddev
->raid_disks
) {
6415 rdev
->raid_disk
= info
->raid_disk
;
6416 set_bit(In_sync
, &rdev
->flags
);
6417 clear_bit(Bitmap_sync
, &rdev
->flags
);
6419 rdev
->raid_disk
= -1;
6420 rdev
->saved_raid_disk
= rdev
->raid_disk
;
6422 super_types
[mddev
->major_version
].
6423 validate_super(mddev
, rdev
);
6424 if ((info
->state
& (1<<MD_DISK_SYNC
)) &&
6425 rdev
->raid_disk
!= info
->raid_disk
) {
6426 /* This was a hot-add request, but events doesn't
6427 * match, so reject it.
6433 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
6434 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
6435 set_bit(WriteMostly
, &rdev
->flags
);
6437 clear_bit(WriteMostly
, &rdev
->flags
);
6438 if (info
->state
& (1<<MD_DISK_FAILFAST
))
6439 set_bit(FailFast
, &rdev
->flags
);
6441 clear_bit(FailFast
, &rdev
->flags
);
6443 if (info
->state
& (1<<MD_DISK_JOURNAL
)) {
6444 struct md_rdev
*rdev2
;
6445 bool has_journal
= false;
6447 /* make sure no existing journal disk */
6448 rdev_for_each(rdev2
, mddev
) {
6449 if (test_bit(Journal
, &rdev2
->flags
)) {
6454 if (has_journal
|| mddev
->bitmap
) {
6458 set_bit(Journal
, &rdev
->flags
);
6461 * check whether the device shows up in other nodes
6463 if (mddev_is_clustered(mddev
)) {
6464 if (info
->state
& (1 << MD_DISK_CANDIDATE
))
6465 set_bit(Candidate
, &rdev
->flags
);
6466 else if (info
->state
& (1 << MD_DISK_CLUSTER_ADD
)) {
6467 /* --add initiated by this node */
6468 err
= md_cluster_ops
->add_new_disk(mddev
, rdev
);
6476 rdev
->raid_disk
= -1;
6477 err
= bind_rdev_to_array(rdev
, mddev
);
6482 if (mddev_is_clustered(mddev
)) {
6483 if (info
->state
& (1 << MD_DISK_CANDIDATE
)) {
6485 err
= md_cluster_ops
->new_disk_ack(mddev
,
6488 md_kick_rdev_from_array(rdev
);
6492 md_cluster_ops
->add_new_disk_cancel(mddev
);
6494 err
= add_bound_rdev(rdev
);
6498 err
= add_bound_rdev(rdev
);
6503 /* otherwise, add_new_disk is only allowed
6504 * for major_version==0 superblocks
6506 if (mddev
->major_version
!= 0) {
6507 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev
));
6511 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
6513 rdev
= md_import_device(dev
, -1, 0);
6515 pr_warn("md: error, md_import_device() returned %ld\n",
6517 return PTR_ERR(rdev
);
6519 rdev
->desc_nr
= info
->number
;
6520 if (info
->raid_disk
< mddev
->raid_disks
)
6521 rdev
->raid_disk
= info
->raid_disk
;
6523 rdev
->raid_disk
= -1;
6525 if (rdev
->raid_disk
< mddev
->raid_disks
)
6526 if (info
->state
& (1<<MD_DISK_SYNC
))
6527 set_bit(In_sync
, &rdev
->flags
);
6529 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
6530 set_bit(WriteMostly
, &rdev
->flags
);
6531 if (info
->state
& (1<<MD_DISK_FAILFAST
))
6532 set_bit(FailFast
, &rdev
->flags
);
6534 if (!mddev
->persistent
) {
6535 pr_debug("md: nonpersistent superblock ...\n");
6536 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
6538 rdev
->sb_start
= calc_dev_sboffset(rdev
);
6539 rdev
->sectors
= rdev
->sb_start
;
6541 err
= bind_rdev_to_array(rdev
, mddev
);
6551 static int hot_remove_disk(struct mddev
*mddev
, dev_t dev
)
6553 char b
[BDEVNAME_SIZE
];
6554 struct md_rdev
*rdev
;
6559 rdev
= find_rdev(mddev
, dev
);
6563 if (rdev
->raid_disk
< 0)
6566 clear_bit(Blocked
, &rdev
->flags
);
6567 remove_and_add_spares(mddev
, rdev
);
6569 if (rdev
->raid_disk
>= 0)
6573 if (mddev_is_clustered(mddev
))
6574 md_cluster_ops
->remove_disk(mddev
, rdev
);
6576 md_kick_rdev_from_array(rdev
);
6577 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
6579 md_wakeup_thread(mddev
->thread
);
6581 md_update_sb(mddev
, 1);
6582 md_new_event(mddev
);
6586 pr_debug("md: cannot remove active disk %s from %s ...\n",
6587 bdevname(rdev
->bdev
,b
), mdname(mddev
));
6591 static int hot_add_disk(struct mddev
*mddev
, dev_t dev
)
6593 char b
[BDEVNAME_SIZE
];
6595 struct md_rdev
*rdev
;
6600 if (mddev
->major_version
!= 0) {
6601 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
6605 if (!mddev
->pers
->hot_add_disk
) {
6606 pr_warn("%s: personality does not support diskops!\n",
6611 rdev
= md_import_device(dev
, -1, 0);
6613 pr_warn("md: error, md_import_device() returned %ld\n",
6618 if (mddev
->persistent
)
6619 rdev
->sb_start
= calc_dev_sboffset(rdev
);
6621 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
6623 rdev
->sectors
= rdev
->sb_start
;
6625 if (test_bit(Faulty
, &rdev
->flags
)) {
6626 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
6627 bdevname(rdev
->bdev
,b
), mdname(mddev
));
6632 clear_bit(In_sync
, &rdev
->flags
);
6634 rdev
->saved_raid_disk
= -1;
6635 err
= bind_rdev_to_array(rdev
, mddev
);
6640 * The rest should better be atomic, we can have disk failures
6641 * noticed in interrupt contexts ...
6644 rdev
->raid_disk
= -1;
6646 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
6648 md_update_sb(mddev
, 1);
6650 * Kick recovery, maybe this spare has to be added to the
6651 * array immediately.
6653 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6654 md_wakeup_thread(mddev
->thread
);
6655 md_new_event(mddev
);
6663 static int set_bitmap_file(struct mddev
*mddev
, int fd
)
6668 if (!mddev
->pers
->quiesce
|| !mddev
->thread
)
6670 if (mddev
->recovery
|| mddev
->sync_thread
)
6672 /* we should be able to change the bitmap.. */
6676 struct inode
*inode
;
6679 if (mddev
->bitmap
|| mddev
->bitmap_info
.file
)
6680 return -EEXIST
; /* cannot add when bitmap is present */
6684 pr_warn("%s: error: failed to get bitmap file\n",
6689 inode
= f
->f_mapping
->host
;
6690 if (!S_ISREG(inode
->i_mode
)) {
6691 pr_warn("%s: error: bitmap file must be a regular file\n",
6694 } else if (!(f
->f_mode
& FMODE_WRITE
)) {
6695 pr_warn("%s: error: bitmap file must open for write\n",
6698 } else if (atomic_read(&inode
->i_writecount
) != 1) {
6699 pr_warn("%s: error: bitmap file is already in use\n",
6707 mddev
->bitmap_info
.file
= f
;
6708 mddev
->bitmap_info
.offset
= 0; /* file overrides offset */
6709 } else if (mddev
->bitmap
== NULL
)
6710 return -ENOENT
; /* cannot remove what isn't there */
6714 struct bitmap
*bitmap
;
6716 bitmap
= bitmap_create(mddev
, -1);
6717 mddev_suspend(mddev
);
6718 if (!IS_ERR(bitmap
)) {
6719 mddev
->bitmap
= bitmap
;
6720 err
= bitmap_load(mddev
);
6722 err
= PTR_ERR(bitmap
);
6724 bitmap_destroy(mddev
);
6727 mddev_resume(mddev
);
6728 } else if (fd
< 0) {
6729 mddev_suspend(mddev
);
6730 bitmap_destroy(mddev
);
6731 mddev_resume(mddev
);
6735 struct file
*f
= mddev
->bitmap_info
.file
;
6737 spin_lock(&mddev
->lock
);
6738 mddev
->bitmap_info
.file
= NULL
;
6739 spin_unlock(&mddev
->lock
);
6748 * set_array_info is used two different ways
6749 * The original usage is when creating a new array.
6750 * In this usage, raid_disks is > 0 and it together with
6751 * level, size, not_persistent,layout,chunksize determine the
6752 * shape of the array.
6753 * This will always create an array with a type-0.90.0 superblock.
6754 * The newer usage is when assembling an array.
6755 * In this case raid_disks will be 0, and the major_version field is
6756 * use to determine which style super-blocks are to be found on the devices.
6757 * The minor and patch _version numbers are also kept incase the
6758 * super_block handler wishes to interpret them.
6760 static int set_array_info(struct mddev
*mddev
, mdu_array_info_t
*info
)
6763 if (info
->raid_disks
== 0) {
6764 /* just setting version number for superblock loading */
6765 if (info
->major_version
< 0 ||
6766 info
->major_version
>= ARRAY_SIZE(super_types
) ||
6767 super_types
[info
->major_version
].name
== NULL
) {
6768 /* maybe try to auto-load a module? */
6769 pr_warn("md: superblock version %d not known\n",
6770 info
->major_version
);
6773 mddev
->major_version
= info
->major_version
;
6774 mddev
->minor_version
= info
->minor_version
;
6775 mddev
->patch_version
= info
->patch_version
;
6776 mddev
->persistent
= !info
->not_persistent
;
6777 /* ensure mddev_put doesn't delete this now that there
6778 * is some minimal configuration.
6780 mddev
->ctime
= ktime_get_real_seconds();
6783 mddev
->major_version
= MD_MAJOR_VERSION
;
6784 mddev
->minor_version
= MD_MINOR_VERSION
;
6785 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
6786 mddev
->ctime
= ktime_get_real_seconds();
6788 mddev
->level
= info
->level
;
6789 mddev
->clevel
[0] = 0;
6790 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
6791 mddev
->raid_disks
= info
->raid_disks
;
6792 /* don't set md_minor, it is determined by which /dev/md* was
6795 if (info
->state
& (1<<MD_SB_CLEAN
))
6796 mddev
->recovery_cp
= MaxSector
;
6798 mddev
->recovery_cp
= 0;
6799 mddev
->persistent
= ! info
->not_persistent
;
6800 mddev
->external
= 0;
6802 mddev
->layout
= info
->layout
;
6803 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
6805 if (mddev
->persistent
) {
6806 mddev
->max_disks
= MD_SB_DISKS
;
6808 mddev
->sb_flags
= 0;
6810 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
6812 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
6813 mddev
->bitmap_info
.default_space
= 64*2 - (MD_SB_BYTES
>> 9);
6814 mddev
->bitmap_info
.offset
= 0;
6816 mddev
->reshape_position
= MaxSector
;
6819 * Generate a 128 bit UUID
6821 get_random_bytes(mddev
->uuid
, 16);
6823 mddev
->new_level
= mddev
->level
;
6824 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
6825 mddev
->new_layout
= mddev
->layout
;
6826 mddev
->delta_disks
= 0;
6827 mddev
->reshape_backwards
= 0;
6832 void md_set_array_sectors(struct mddev
*mddev
, sector_t array_sectors
)
6834 lockdep_assert_held(&mddev
->reconfig_mutex
);
6836 if (mddev
->external_size
)
6839 mddev
->array_sectors
= array_sectors
;
6841 EXPORT_SYMBOL(md_set_array_sectors
);
6843 static int update_size(struct mddev
*mddev
, sector_t num_sectors
)
6845 struct md_rdev
*rdev
;
6847 int fit
= (num_sectors
== 0);
6848 sector_t old_dev_sectors
= mddev
->dev_sectors
;
6850 if (mddev
->pers
->resize
== NULL
)
6852 /* The "num_sectors" is the number of sectors of each device that
6853 * is used. This can only make sense for arrays with redundancy.
6854 * linear and raid0 always use whatever space is available. We can only
6855 * consider changing this number if no resync or reconstruction is
6856 * happening, and if the new size is acceptable. It must fit before the
6857 * sb_start or, if that is <data_offset, it must fit before the size
6858 * of each device. If num_sectors is zero, we find the largest size
6861 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
6867 rdev_for_each(rdev
, mddev
) {
6868 sector_t avail
= rdev
->sectors
;
6870 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
6871 num_sectors
= avail
;
6872 if (avail
< num_sectors
)
6875 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
6877 if (mddev_is_clustered(mddev
))
6878 md_cluster_ops
->update_size(mddev
, old_dev_sectors
);
6879 else if (mddev
->queue
) {
6880 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
6881 revalidate_disk(mddev
->gendisk
);
6887 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
)
6890 struct md_rdev
*rdev
;
6891 /* change the number of raid disks */
6892 if (mddev
->pers
->check_reshape
== NULL
)
6896 if (raid_disks
<= 0 ||
6897 (mddev
->max_disks
&& raid_disks
>= mddev
->max_disks
))
6899 if (mddev
->sync_thread
||
6900 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
6901 mddev
->reshape_position
!= MaxSector
)
6904 rdev_for_each(rdev
, mddev
) {
6905 if (mddev
->raid_disks
< raid_disks
&&
6906 rdev
->data_offset
< rdev
->new_data_offset
)
6908 if (mddev
->raid_disks
> raid_disks
&&
6909 rdev
->data_offset
> rdev
->new_data_offset
)
6913 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
6914 if (mddev
->delta_disks
< 0)
6915 mddev
->reshape_backwards
= 1;
6916 else if (mddev
->delta_disks
> 0)
6917 mddev
->reshape_backwards
= 0;
6919 rv
= mddev
->pers
->check_reshape(mddev
);
6921 mddev
->delta_disks
= 0;
6922 mddev
->reshape_backwards
= 0;
6928 * update_array_info is used to change the configuration of an
6930 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6931 * fields in the info are checked against the array.
6932 * Any differences that cannot be handled will cause an error.
6933 * Normally, only one change can be managed at a time.
6935 static int update_array_info(struct mddev
*mddev
, mdu_array_info_t
*info
)
6941 /* calculate expected state,ignoring low bits */
6942 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
6943 state
|= (1 << MD_SB_BITMAP_PRESENT
);
6945 if (mddev
->major_version
!= info
->major_version
||
6946 mddev
->minor_version
!= info
->minor_version
||
6947 /* mddev->patch_version != info->patch_version || */
6948 mddev
->ctime
!= info
->ctime
||
6949 mddev
->level
!= info
->level
||
6950 /* mddev->layout != info->layout || */
6951 mddev
->persistent
!= !info
->not_persistent
||
6952 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
6953 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6954 ((state
^info
->state
) & 0xfffffe00)
6957 /* Check there is only one change */
6958 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
6960 if (mddev
->raid_disks
!= info
->raid_disks
)
6962 if (mddev
->layout
!= info
->layout
)
6964 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
6971 if (mddev
->layout
!= info
->layout
) {
6973 * we don't need to do anything at the md level, the
6974 * personality will take care of it all.
6976 if (mddev
->pers
->check_reshape
== NULL
)
6979 mddev
->new_layout
= info
->layout
;
6980 rv
= mddev
->pers
->check_reshape(mddev
);
6982 mddev
->new_layout
= mddev
->layout
;
6986 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
6987 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
6989 if (mddev
->raid_disks
!= info
->raid_disks
)
6990 rv
= update_raid_disks(mddev
, info
->raid_disks
);
6992 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
6993 if (mddev
->pers
->quiesce
== NULL
|| mddev
->thread
== NULL
) {
6997 if (mddev
->recovery
|| mddev
->sync_thread
) {
7001 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
7002 struct bitmap
*bitmap
;
7003 /* add the bitmap */
7004 if (mddev
->bitmap
) {
7008 if (mddev
->bitmap_info
.default_offset
== 0) {
7012 mddev
->bitmap_info
.offset
=
7013 mddev
->bitmap_info
.default_offset
;
7014 mddev
->bitmap_info
.space
=
7015 mddev
->bitmap_info
.default_space
;
7016 bitmap
= bitmap_create(mddev
, -1);
7017 mddev_suspend(mddev
);
7018 if (!IS_ERR(bitmap
)) {
7019 mddev
->bitmap
= bitmap
;
7020 rv
= bitmap_load(mddev
);
7022 rv
= PTR_ERR(bitmap
);
7024 bitmap_destroy(mddev
);
7025 mddev_resume(mddev
);
7027 /* remove the bitmap */
7028 if (!mddev
->bitmap
) {
7032 if (mddev
->bitmap
->storage
.file
) {
7036 if (mddev
->bitmap_info
.nodes
) {
7037 /* hold PW on all the bitmap lock */
7038 if (md_cluster_ops
->lock_all_bitmaps(mddev
) <= 0) {
7039 pr_warn("md: can't change bitmap to none since the array is in use by more than one node\n");
7041 md_cluster_ops
->unlock_all_bitmaps(mddev
);
7045 mddev
->bitmap_info
.nodes
= 0;
7046 md_cluster_ops
->leave(mddev
);
7048 mddev_suspend(mddev
);
7049 bitmap_destroy(mddev
);
7050 mddev_resume(mddev
);
7051 mddev
->bitmap_info
.offset
= 0;
7054 md_update_sb(mddev
, 1);
7060 static int set_disk_faulty(struct mddev
*mddev
, dev_t dev
)
7062 struct md_rdev
*rdev
;
7065 if (mddev
->pers
== NULL
)
7069 rdev
= md_find_rdev_rcu(mddev
, dev
);
7073 md_error(mddev
, rdev
);
7074 if (!test_bit(Faulty
, &rdev
->flags
))
7082 * We have a problem here : there is no easy way to give a CHS
7083 * virtual geometry. We currently pretend that we have a 2 heads
7084 * 4 sectors (with a BIG number of cylinders...). This drives
7085 * dosfs just mad... ;-)
7087 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
7089 struct mddev
*mddev
= bdev
->bd_disk
->private_data
;
7093 geo
->cylinders
= mddev
->array_sectors
/ 8;
7097 static inline bool md_ioctl_valid(unsigned int cmd
)
7102 case GET_ARRAY_INFO
:
7103 case GET_BITMAP_FILE
:
7106 case HOT_REMOVE_DISK
:
7109 case RESTART_ARRAY_RW
:
7111 case SET_ARRAY_INFO
:
7112 case SET_BITMAP_FILE
:
7113 case SET_DISK_FAULTY
:
7116 case CLUSTERED_DISK_NACK
:
7123 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
7124 unsigned int cmd
, unsigned long arg
)
7127 void __user
*argp
= (void __user
*)arg
;
7128 struct mddev
*mddev
= NULL
;
7130 bool did_set_md_closing
= false;
7132 if (!md_ioctl_valid(cmd
))
7137 case GET_ARRAY_INFO
:
7141 if (!capable(CAP_SYS_ADMIN
))
7146 * Commands dealing with the RAID driver but not any
7151 err
= get_version(argp
);
7157 autostart_arrays(arg
);
7164 * Commands creating/starting a new array:
7167 mddev
= bdev
->bd_disk
->private_data
;
7174 /* Some actions do not requires the mutex */
7176 case GET_ARRAY_INFO
:
7177 if (!mddev
->raid_disks
&& !mddev
->external
)
7180 err
= get_array_info(mddev
, argp
);
7184 if (!mddev
->raid_disks
&& !mddev
->external
)
7187 err
= get_disk_info(mddev
, argp
);
7190 case SET_DISK_FAULTY
:
7191 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
7194 case GET_BITMAP_FILE
:
7195 err
= get_bitmap_file(mddev
, argp
);
7200 if (cmd
== ADD_NEW_DISK
)
7201 /* need to ensure md_delayed_delete() has completed */
7202 flush_workqueue(md_misc_wq
);
7204 if (cmd
== HOT_REMOVE_DISK
)
7205 /* need to ensure recovery thread has run */
7206 wait_event_interruptible_timeout(mddev
->sb_wait
,
7207 !test_bit(MD_RECOVERY_NEEDED
,
7209 msecs_to_jiffies(5000));
7210 if (cmd
== STOP_ARRAY
|| cmd
== STOP_ARRAY_RO
) {
7211 /* Need to flush page cache, and ensure no-one else opens
7214 mutex_lock(&mddev
->open_mutex
);
7215 if (mddev
->pers
&& atomic_read(&mddev
->openers
) > 1) {
7216 mutex_unlock(&mddev
->open_mutex
);
7220 WARN_ON_ONCE(test_bit(MD_CLOSING
, &mddev
->flags
));
7221 set_bit(MD_CLOSING
, &mddev
->flags
);
7222 did_set_md_closing
= true;
7223 mutex_unlock(&mddev
->open_mutex
);
7224 sync_blockdev(bdev
);
7226 err
= mddev_lock(mddev
);
7228 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7233 if (cmd
== SET_ARRAY_INFO
) {
7234 mdu_array_info_t info
;
7236 memset(&info
, 0, sizeof(info
));
7237 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
7242 err
= update_array_info(mddev
, &info
);
7244 pr_warn("md: couldn't update array info. %d\n", err
);
7249 if (!list_empty(&mddev
->disks
)) {
7250 pr_warn("md: array %s already has disks!\n", mdname(mddev
));
7254 if (mddev
->raid_disks
) {
7255 pr_warn("md: array %s already initialised!\n", mdname(mddev
));
7259 err
= set_array_info(mddev
, &info
);
7261 pr_warn("md: couldn't set array info. %d\n", err
);
7268 * Commands querying/configuring an existing array:
7270 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
7271 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
7272 if ((!mddev
->raid_disks
&& !mddev
->external
)
7273 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
7274 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
7275 && cmd
!= GET_BITMAP_FILE
) {
7281 * Commands even a read-only array can execute:
7284 case RESTART_ARRAY_RW
:
7285 err
= restart_array(mddev
);
7289 err
= do_md_stop(mddev
, 0, bdev
);
7293 err
= md_set_readonly(mddev
, bdev
);
7296 case HOT_REMOVE_DISK
:
7297 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
7301 /* We can support ADD_NEW_DISK on read-only arrays
7302 * only if we are re-adding a preexisting device.
7303 * So require mddev->pers and MD_DISK_SYNC.
7306 mdu_disk_info_t info
;
7307 if (copy_from_user(&info
, argp
, sizeof(info
)))
7309 else if (!(info
.state
& (1<<MD_DISK_SYNC
)))
7310 /* Need to clear read-only for this */
7313 err
= add_new_disk(mddev
, &info
);
7319 if (get_user(ro
, (int __user
*)(arg
))) {
7325 /* if the bdev is going readonly the value of mddev->ro
7326 * does not matter, no writes are coming
7331 /* are we are already prepared for writes? */
7335 /* transitioning to readauto need only happen for
7336 * arrays that call md_write_start
7339 err
= restart_array(mddev
);
7342 set_disk_ro(mddev
->gendisk
, 0);
7349 * The remaining ioctls are changing the state of the
7350 * superblock, so we do not allow them on read-only arrays.
7352 if (mddev
->ro
&& mddev
->pers
) {
7353 if (mddev
->ro
== 2) {
7355 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7356 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7357 /* mddev_unlock will wake thread */
7358 /* If a device failed while we were read-only, we
7359 * need to make sure the metadata is updated now.
7361 if (test_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
)) {
7362 mddev_unlock(mddev
);
7363 wait_event(mddev
->sb_wait
,
7364 !test_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
) &&
7365 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
7366 mddev_lock_nointr(mddev
);
7377 mdu_disk_info_t info
;
7378 if (copy_from_user(&info
, argp
, sizeof(info
)))
7381 err
= add_new_disk(mddev
, &info
);
7385 case CLUSTERED_DISK_NACK
:
7386 if (mddev_is_clustered(mddev
))
7387 md_cluster_ops
->new_disk_ack(mddev
, false);
7393 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
7397 err
= do_md_run(mddev
);
7400 case SET_BITMAP_FILE
:
7401 err
= set_bitmap_file(mddev
, (int)arg
);
7410 if (mddev
->hold_active
== UNTIL_IOCTL
&&
7412 mddev
->hold_active
= 0;
7413 mddev_unlock(mddev
);
7415 if(did_set_md_closing
)
7416 clear_bit(MD_CLOSING
, &mddev
->flags
);
7419 #ifdef CONFIG_COMPAT
7420 static int md_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
7421 unsigned int cmd
, unsigned long arg
)
7424 case HOT_REMOVE_DISK
:
7426 case SET_DISK_FAULTY
:
7427 case SET_BITMAP_FILE
:
7428 /* These take in integer arg, do not convert */
7431 arg
= (unsigned long)compat_ptr(arg
);
7435 return md_ioctl(bdev
, mode
, cmd
, arg
);
7437 #endif /* CONFIG_COMPAT */
7439 static int md_open(struct block_device
*bdev
, fmode_t mode
)
7442 * Succeed if we can lock the mddev, which confirms that
7443 * it isn't being stopped right now.
7445 struct mddev
*mddev
= mddev_find(bdev
->bd_dev
);
7451 if (mddev
->gendisk
!= bdev
->bd_disk
) {
7452 /* we are racing with mddev_put which is discarding this
7456 /* Wait until bdev->bd_disk is definitely gone */
7457 flush_workqueue(md_misc_wq
);
7458 /* Then retry the open from the top */
7459 return -ERESTARTSYS
;
7461 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
7463 if ((err
= mutex_lock_interruptible(&mddev
->open_mutex
)))
7466 if (test_bit(MD_CLOSING
, &mddev
->flags
)) {
7467 mutex_unlock(&mddev
->open_mutex
);
7473 atomic_inc(&mddev
->openers
);
7474 mutex_unlock(&mddev
->open_mutex
);
7476 check_disk_change(bdev
);
7483 static void md_release(struct gendisk
*disk
, fmode_t mode
)
7485 struct mddev
*mddev
= disk
->private_data
;
7488 atomic_dec(&mddev
->openers
);
7492 static int md_media_changed(struct gendisk
*disk
)
7494 struct mddev
*mddev
= disk
->private_data
;
7496 return mddev
->changed
;
7499 static int md_revalidate(struct gendisk
*disk
)
7501 struct mddev
*mddev
= disk
->private_data
;
7506 static const struct block_device_operations md_fops
=
7508 .owner
= THIS_MODULE
,
7510 .release
= md_release
,
7512 #ifdef CONFIG_COMPAT
7513 .compat_ioctl
= md_compat_ioctl
,
7515 .getgeo
= md_getgeo
,
7516 .media_changed
= md_media_changed
,
7517 .revalidate_disk
= md_revalidate
,
7520 static int md_thread(void *arg
)
7522 struct md_thread
*thread
= arg
;
7525 * md_thread is a 'system-thread', it's priority should be very
7526 * high. We avoid resource deadlocks individually in each
7527 * raid personality. (RAID5 does preallocation) We also use RR and
7528 * the very same RT priority as kswapd, thus we will never get
7529 * into a priority inversion deadlock.
7531 * we definitely have to have equal or higher priority than
7532 * bdflush, otherwise bdflush will deadlock if there are too
7533 * many dirty RAID5 blocks.
7536 allow_signal(SIGKILL
);
7537 while (!kthread_should_stop()) {
7539 /* We need to wait INTERRUPTIBLE so that
7540 * we don't add to the load-average.
7541 * That means we need to be sure no signals are
7544 if (signal_pending(current
))
7545 flush_signals(current
);
7547 wait_event_interruptible_timeout
7549 test_bit(THREAD_WAKEUP
, &thread
->flags
)
7550 || kthread_should_stop() || kthread_should_park(),
7553 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
7554 if (kthread_should_park())
7556 if (!kthread_should_stop())
7557 thread
->run(thread
);
7563 void md_wakeup_thread(struct md_thread
*thread
)
7566 pr_debug("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
7567 set_bit(THREAD_WAKEUP
, &thread
->flags
);
7568 wake_up(&thread
->wqueue
);
7571 EXPORT_SYMBOL(md_wakeup_thread
);
7573 struct md_thread
*md_register_thread(void (*run
) (struct md_thread
*),
7574 struct mddev
*mddev
, const char *name
)
7576 struct md_thread
*thread
;
7578 thread
= kzalloc(sizeof(struct md_thread
), GFP_KERNEL
);
7582 init_waitqueue_head(&thread
->wqueue
);
7585 thread
->mddev
= mddev
;
7586 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
7587 thread
->tsk
= kthread_run(md_thread
, thread
,
7589 mdname(thread
->mddev
),
7591 if (IS_ERR(thread
->tsk
)) {
7597 EXPORT_SYMBOL(md_register_thread
);
7599 void md_unregister_thread(struct md_thread
**threadp
)
7601 struct md_thread
*thread
= *threadp
;
7604 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
7605 /* Locking ensures that mddev_unlock does not wake_up a
7606 * non-existent thread
7608 spin_lock(&pers_lock
);
7610 spin_unlock(&pers_lock
);
7612 kthread_stop(thread
->tsk
);
7615 EXPORT_SYMBOL(md_unregister_thread
);
7617 void md_error(struct mddev
*mddev
, struct md_rdev
*rdev
)
7619 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
7622 if (!mddev
->pers
|| !mddev
->pers
->error_handler
)
7624 mddev
->pers
->error_handler(mddev
,rdev
);
7625 if (mddev
->degraded
)
7626 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7627 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
7628 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7629 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7630 md_wakeup_thread(mddev
->thread
);
7631 if (mddev
->event_work
.func
)
7632 queue_work(md_misc_wq
, &mddev
->event_work
);
7633 md_new_event(mddev
);
7635 EXPORT_SYMBOL(md_error
);
7637 /* seq_file implementation /proc/mdstat */
7639 static void status_unused(struct seq_file
*seq
)
7642 struct md_rdev
*rdev
;
7644 seq_printf(seq
, "unused devices: ");
7646 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
7647 char b
[BDEVNAME_SIZE
];
7649 seq_printf(seq
, "%s ",
7650 bdevname(rdev
->bdev
,b
));
7653 seq_printf(seq
, "<none>");
7655 seq_printf(seq
, "\n");
7658 static int status_resync(struct seq_file
*seq
, struct mddev
*mddev
)
7660 sector_t max_sectors
, resync
, res
;
7661 unsigned long dt
, db
;
7664 unsigned int per_milli
;
7666 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
7667 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
7668 max_sectors
= mddev
->resync_max_sectors
;
7670 max_sectors
= mddev
->dev_sectors
;
7672 resync
= mddev
->curr_resync
;
7674 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
7675 /* Still cleaning up */
7676 resync
= max_sectors
;
7677 } else if (resync
> max_sectors
)
7678 resync
= max_sectors
;
7680 resync
-= atomic_read(&mddev
->recovery_active
);
7683 if (mddev
->recovery_cp
< MaxSector
) {
7684 seq_printf(seq
, "\tresync=PENDING");
7690 seq_printf(seq
, "\tresync=DELAYED");
7694 WARN_ON(max_sectors
== 0);
7695 /* Pick 'scale' such that (resync>>scale)*1000 will fit
7696 * in a sector_t, and (max_sectors>>scale) will fit in a
7697 * u32, as those are the requirements for sector_div.
7698 * Thus 'scale' must be at least 10
7701 if (sizeof(sector_t
) > sizeof(unsigned long)) {
7702 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
7705 res
= (resync
>>scale
)*1000;
7706 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
7710 int i
, x
= per_milli
/50, y
= 20-x
;
7711 seq_printf(seq
, "[");
7712 for (i
= 0; i
< x
; i
++)
7713 seq_printf(seq
, "=");
7714 seq_printf(seq
, ">");
7715 for (i
= 0; i
< y
; i
++)
7716 seq_printf(seq
, ".");
7717 seq_printf(seq
, "] ");
7719 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
7720 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
7722 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
7724 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
7725 "resync" : "recovery"))),
7726 per_milli
/10, per_milli
% 10,
7727 (unsigned long long) resync
/2,
7728 (unsigned long long) max_sectors
/2);
7731 * dt: time from mark until now
7732 * db: blocks written from mark until now
7733 * rt: remaining time
7735 * rt is a sector_t, so could be 32bit or 64bit.
7736 * So we divide before multiply in case it is 32bit and close
7738 * We scale the divisor (db) by 32 to avoid losing precision
7739 * near the end of resync when the number of remaining sectors
7741 * We then divide rt by 32 after multiplying by db to compensate.
7742 * The '+1' avoids division by zero if db is very small.
7744 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
7746 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
7747 - mddev
->resync_mark_cnt
;
7749 rt
= max_sectors
- resync
; /* number of remaining sectors */
7750 sector_div(rt
, db
/32+1);
7754 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
7755 ((unsigned long)rt
% 60)/6);
7757 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
7761 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
7763 struct list_head
*tmp
;
7765 struct mddev
*mddev
;
7773 spin_lock(&all_mddevs_lock
);
7774 list_for_each(tmp
,&all_mddevs
)
7776 mddev
= list_entry(tmp
, struct mddev
, all_mddevs
);
7778 spin_unlock(&all_mddevs_lock
);
7781 spin_unlock(&all_mddevs_lock
);
7783 return (void*)2;/* tail */
7787 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
7789 struct list_head
*tmp
;
7790 struct mddev
*next_mddev
, *mddev
= v
;
7796 spin_lock(&all_mddevs_lock
);
7798 tmp
= all_mddevs
.next
;
7800 tmp
= mddev
->all_mddevs
.next
;
7801 if (tmp
!= &all_mddevs
)
7802 next_mddev
= mddev_get(list_entry(tmp
,struct mddev
,all_mddevs
));
7804 next_mddev
= (void*)2;
7807 spin_unlock(&all_mddevs_lock
);
7815 static void md_seq_stop(struct seq_file
*seq
, void *v
)
7817 struct mddev
*mddev
= v
;
7819 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
7823 static int md_seq_show(struct seq_file
*seq
, void *v
)
7825 struct mddev
*mddev
= v
;
7827 struct md_rdev
*rdev
;
7829 if (v
== (void*)1) {
7830 struct md_personality
*pers
;
7831 seq_printf(seq
, "Personalities : ");
7832 spin_lock(&pers_lock
);
7833 list_for_each_entry(pers
, &pers_list
, list
)
7834 seq_printf(seq
, "[%s] ", pers
->name
);
7836 spin_unlock(&pers_lock
);
7837 seq_printf(seq
, "\n");
7838 seq
->poll_event
= atomic_read(&md_event_count
);
7841 if (v
== (void*)2) {
7846 spin_lock(&mddev
->lock
);
7847 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
7848 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
7849 mddev
->pers
? "" : "in");
7852 seq_printf(seq
, " (read-only)");
7854 seq_printf(seq
, " (auto-read-only)");
7855 seq_printf(seq
, " %s", mddev
->pers
->name
);
7860 rdev_for_each_rcu(rdev
, mddev
) {
7861 char b
[BDEVNAME_SIZE
];
7862 seq_printf(seq
, " %s[%d]",
7863 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
7864 if (test_bit(WriteMostly
, &rdev
->flags
))
7865 seq_printf(seq
, "(W)");
7866 if (test_bit(Journal
, &rdev
->flags
))
7867 seq_printf(seq
, "(J)");
7868 if (test_bit(Faulty
, &rdev
->flags
)) {
7869 seq_printf(seq
, "(F)");
7872 if (rdev
->raid_disk
< 0)
7873 seq_printf(seq
, "(S)"); /* spare */
7874 if (test_bit(Replacement
, &rdev
->flags
))
7875 seq_printf(seq
, "(R)");
7876 sectors
+= rdev
->sectors
;
7880 if (!list_empty(&mddev
->disks
)) {
7882 seq_printf(seq
, "\n %llu blocks",
7883 (unsigned long long)
7884 mddev
->array_sectors
/ 2);
7886 seq_printf(seq
, "\n %llu blocks",
7887 (unsigned long long)sectors
/ 2);
7889 if (mddev
->persistent
) {
7890 if (mddev
->major_version
!= 0 ||
7891 mddev
->minor_version
!= 90) {
7892 seq_printf(seq
," super %d.%d",
7893 mddev
->major_version
,
7894 mddev
->minor_version
);
7896 } else if (mddev
->external
)
7897 seq_printf(seq
, " super external:%s",
7898 mddev
->metadata_type
);
7900 seq_printf(seq
, " super non-persistent");
7903 mddev
->pers
->status(seq
, mddev
);
7904 seq_printf(seq
, "\n ");
7905 if (mddev
->pers
->sync_request
) {
7906 if (status_resync(seq
, mddev
))
7907 seq_printf(seq
, "\n ");
7910 seq_printf(seq
, "\n ");
7912 bitmap_status(seq
, mddev
->bitmap
);
7914 seq_printf(seq
, "\n");
7916 spin_unlock(&mddev
->lock
);
7921 static const struct seq_operations md_seq_ops
= {
7922 .start
= md_seq_start
,
7923 .next
= md_seq_next
,
7924 .stop
= md_seq_stop
,
7925 .show
= md_seq_show
,
7928 static int md_seq_open(struct inode
*inode
, struct file
*file
)
7930 struct seq_file
*seq
;
7933 error
= seq_open(file
, &md_seq_ops
);
7937 seq
= file
->private_data
;
7938 seq
->poll_event
= atomic_read(&md_event_count
);
7942 static int md_unloading
;
7943 static __poll_t
mdstat_poll(struct file
*filp
, poll_table
*wait
)
7945 struct seq_file
*seq
= filp
->private_data
;
7949 return EPOLLIN
|EPOLLRDNORM
|EPOLLERR
|EPOLLPRI
;
7950 poll_wait(filp
, &md_event_waiters
, wait
);
7952 /* always allow read */
7953 mask
= EPOLLIN
| EPOLLRDNORM
;
7955 if (seq
->poll_event
!= atomic_read(&md_event_count
))
7956 mask
|= EPOLLERR
| EPOLLPRI
;
7960 static const struct file_operations md_seq_fops
= {
7961 .owner
= THIS_MODULE
,
7962 .open
= md_seq_open
,
7964 .llseek
= seq_lseek
,
7965 .release
= seq_release
,
7966 .poll
= mdstat_poll
,
7969 int register_md_personality(struct md_personality
*p
)
7971 pr_debug("md: %s personality registered for level %d\n",
7973 spin_lock(&pers_lock
);
7974 list_add_tail(&p
->list
, &pers_list
);
7975 spin_unlock(&pers_lock
);
7978 EXPORT_SYMBOL(register_md_personality
);
7980 int unregister_md_personality(struct md_personality
*p
)
7982 pr_debug("md: %s personality unregistered\n", p
->name
);
7983 spin_lock(&pers_lock
);
7984 list_del_init(&p
->list
);
7985 spin_unlock(&pers_lock
);
7988 EXPORT_SYMBOL(unregister_md_personality
);
7990 int register_md_cluster_operations(struct md_cluster_operations
*ops
,
7991 struct module
*module
)
7994 spin_lock(&pers_lock
);
7995 if (md_cluster_ops
!= NULL
)
7998 md_cluster_ops
= ops
;
7999 md_cluster_mod
= module
;
8001 spin_unlock(&pers_lock
);
8004 EXPORT_SYMBOL(register_md_cluster_operations
);
8006 int unregister_md_cluster_operations(void)
8008 spin_lock(&pers_lock
);
8009 md_cluster_ops
= NULL
;
8010 spin_unlock(&pers_lock
);
8013 EXPORT_SYMBOL(unregister_md_cluster_operations
);
8015 int md_setup_cluster(struct mddev
*mddev
, int nodes
)
8017 if (!md_cluster_ops
)
8018 request_module("md-cluster");
8019 spin_lock(&pers_lock
);
8020 /* ensure module won't be unloaded */
8021 if (!md_cluster_ops
|| !try_module_get(md_cluster_mod
)) {
8022 pr_warn("can't find md-cluster module or get it's reference.\n");
8023 spin_unlock(&pers_lock
);
8026 spin_unlock(&pers_lock
);
8028 return md_cluster_ops
->join(mddev
, nodes
);
8031 void md_cluster_stop(struct mddev
*mddev
)
8033 if (!md_cluster_ops
)
8035 md_cluster_ops
->leave(mddev
);
8036 module_put(md_cluster_mod
);
8039 static int is_mddev_idle(struct mddev
*mddev
, int init
)
8041 struct md_rdev
*rdev
;
8047 rdev_for_each_rcu(rdev
, mddev
) {
8048 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
8049 curr_events
= (int)part_stat_read(&disk
->part0
, sectors
[0]) +
8050 (int)part_stat_read(&disk
->part0
, sectors
[1]) -
8051 atomic_read(&disk
->sync_io
);
8052 /* sync IO will cause sync_io to increase before the disk_stats
8053 * as sync_io is counted when a request starts, and
8054 * disk_stats is counted when it completes.
8055 * So resync activity will cause curr_events to be smaller than
8056 * when there was no such activity.
8057 * non-sync IO will cause disk_stat to increase without
8058 * increasing sync_io so curr_events will (eventually)
8059 * be larger than it was before. Once it becomes
8060 * substantially larger, the test below will cause
8061 * the array to appear non-idle, and resync will slow
8063 * If there is a lot of outstanding resync activity when
8064 * we set last_event to curr_events, then all that activity
8065 * completing might cause the array to appear non-idle
8066 * and resync will be slowed down even though there might
8067 * not have been non-resync activity. This will only
8068 * happen once though. 'last_events' will soon reflect
8069 * the state where there is little or no outstanding
8070 * resync requests, and further resync activity will
8071 * always make curr_events less than last_events.
8074 if (init
|| curr_events
- rdev
->last_events
> 64) {
8075 rdev
->last_events
= curr_events
;
8083 void md_done_sync(struct mddev
*mddev
, int blocks
, int ok
)
8085 /* another "blocks" (512byte) blocks have been synced */
8086 atomic_sub(blocks
, &mddev
->recovery_active
);
8087 wake_up(&mddev
->recovery_wait
);
8089 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8090 set_bit(MD_RECOVERY_ERROR
, &mddev
->recovery
);
8091 md_wakeup_thread(mddev
->thread
);
8092 // stop recovery, signal do_sync ....
8095 EXPORT_SYMBOL(md_done_sync
);
8097 /* md_write_start(mddev, bi)
8098 * If we need to update some array metadata (e.g. 'active' flag
8099 * in superblock) before writing, schedule a superblock update
8100 * and wait for it to complete.
8101 * A return value of 'false' means that the write wasn't recorded
8102 * and cannot proceed as the array is being suspend.
8104 bool md_write_start(struct mddev
*mddev
, struct bio
*bi
)
8108 if (bio_data_dir(bi
) != WRITE
)
8111 BUG_ON(mddev
->ro
== 1);
8112 if (mddev
->ro
== 2) {
8113 /* need to switch to read/write */
8115 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8116 md_wakeup_thread(mddev
->thread
);
8117 md_wakeup_thread(mddev
->sync_thread
);
8121 percpu_ref_get(&mddev
->writes_pending
);
8122 smp_mb(); /* Match smp_mb in set_in_sync() */
8123 if (mddev
->safemode
== 1)
8124 mddev
->safemode
= 0;
8125 /* sync_checkers is always 0 when writes_pending is in per-cpu mode */
8126 if (mddev
->in_sync
|| mddev
->sync_checkers
) {
8127 spin_lock(&mddev
->lock
);
8128 if (mddev
->in_sync
) {
8130 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
8131 set_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
8132 md_wakeup_thread(mddev
->thread
);
8135 spin_unlock(&mddev
->lock
);
8139 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
8140 if (!mddev
->has_superblocks
)
8142 wait_event(mddev
->sb_wait
,
8143 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
) ||
8145 if (test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
)) {
8146 percpu_ref_put(&mddev
->writes_pending
);
8151 EXPORT_SYMBOL(md_write_start
);
8153 /* md_write_inc can only be called when md_write_start() has
8154 * already been called at least once of the current request.
8155 * It increments the counter and is useful when a single request
8156 * is split into several parts. Each part causes an increment and
8157 * so needs a matching md_write_end().
8158 * Unlike md_write_start(), it is safe to call md_write_inc() inside
8159 * a spinlocked region.
8161 void md_write_inc(struct mddev
*mddev
, struct bio
*bi
)
8163 if (bio_data_dir(bi
) != WRITE
)
8165 WARN_ON_ONCE(mddev
->in_sync
|| mddev
->ro
);
8166 percpu_ref_get(&mddev
->writes_pending
);
8168 EXPORT_SYMBOL(md_write_inc
);
8170 void md_write_end(struct mddev
*mddev
)
8172 percpu_ref_put(&mddev
->writes_pending
);
8174 if (mddev
->safemode
== 2)
8175 md_wakeup_thread(mddev
->thread
);
8176 else if (mddev
->safemode_delay
)
8177 /* The roundup() ensures this only performs locking once
8178 * every ->safemode_delay jiffies
8180 mod_timer(&mddev
->safemode_timer
,
8181 roundup(jiffies
, mddev
->safemode_delay
) +
8182 mddev
->safemode_delay
);
8185 EXPORT_SYMBOL(md_write_end
);
8187 /* md_allow_write(mddev)
8188 * Calling this ensures that the array is marked 'active' so that writes
8189 * may proceed without blocking. It is important to call this before
8190 * attempting a GFP_KERNEL allocation while holding the mddev lock.
8191 * Must be called with mddev_lock held.
8193 void md_allow_write(struct mddev
*mddev
)
8199 if (!mddev
->pers
->sync_request
)
8202 spin_lock(&mddev
->lock
);
8203 if (mddev
->in_sync
) {
8205 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
8206 set_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
8207 if (mddev
->safemode_delay
&&
8208 mddev
->safemode
== 0)
8209 mddev
->safemode
= 1;
8210 spin_unlock(&mddev
->lock
);
8211 md_update_sb(mddev
, 0);
8212 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
8213 /* wait for the dirty state to be recorded in the metadata */
8214 wait_event(mddev
->sb_wait
,
8215 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
8217 spin_unlock(&mddev
->lock
);
8219 EXPORT_SYMBOL_GPL(md_allow_write
);
8221 #define SYNC_MARKS 10
8222 #define SYNC_MARK_STEP (3*HZ)
8223 #define UPDATE_FREQUENCY (5*60*HZ)
8224 void md_do_sync(struct md_thread
*thread
)
8226 struct mddev
*mddev
= thread
->mddev
;
8227 struct mddev
*mddev2
;
8228 unsigned int currspeed
= 0,
8230 sector_t max_sectors
,j
, io_sectors
, recovery_done
;
8231 unsigned long mark
[SYNC_MARKS
];
8232 unsigned long update_time
;
8233 sector_t mark_cnt
[SYNC_MARKS
];
8235 struct list_head
*tmp
;
8236 sector_t last_check
;
8238 struct md_rdev
*rdev
;
8239 char *desc
, *action
= NULL
;
8240 struct blk_plug plug
;
8243 /* just incase thread restarts... */
8244 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
8245 test_bit(MD_RECOVERY_WAIT
, &mddev
->recovery
))
8247 if (mddev
->ro
) {/* never try to sync a read-only array */
8248 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8252 if (mddev_is_clustered(mddev
)) {
8253 ret
= md_cluster_ops
->resync_start(mddev
);
8257 set_bit(MD_CLUSTER_RESYNC_LOCKED
, &mddev
->flags
);
8258 if (!(test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
8259 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) ||
8260 test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
8261 && ((unsigned long long)mddev
->curr_resync_completed
8262 < (unsigned long long)mddev
->resync_max_sectors
))
8266 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
8267 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)) {
8268 desc
= "data-check";
8270 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
8271 desc
= "requested-resync";
8275 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
8280 mddev
->last_sync_action
= action
?: desc
;
8282 /* we overload curr_resync somewhat here.
8283 * 0 == not engaged in resync at all
8284 * 2 == checking that there is no conflict with another sync
8285 * 1 == like 2, but have yielded to allow conflicting resync to
8287 * other == active in resync - this many blocks
8289 * Before starting a resync we must have set curr_resync to
8290 * 2, and then checked that every "conflicting" array has curr_resync
8291 * less than ours. When we find one that is the same or higher
8292 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8293 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8294 * This will mean we have to start checking from the beginning again.
8299 int mddev2_minor
= -1;
8300 mddev
->curr_resync
= 2;
8303 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8305 for_each_mddev(mddev2
, tmp
) {
8306 if (mddev2
== mddev
)
8308 if (!mddev
->parallel_resync
8309 && mddev2
->curr_resync
8310 && match_mddev_units(mddev
, mddev2
)) {
8312 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
8313 /* arbitrarily yield */
8314 mddev
->curr_resync
= 1;
8315 wake_up(&resync_wait
);
8317 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
8318 /* no need to wait here, we can wait the next
8319 * time 'round when curr_resync == 2
8322 /* We need to wait 'interruptible' so as not to
8323 * contribute to the load average, and not to
8324 * be caught by 'softlockup'
8326 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
8327 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
8328 mddev2
->curr_resync
>= mddev
->curr_resync
) {
8329 if (mddev2_minor
!= mddev2
->md_minor
) {
8330 mddev2_minor
= mddev2
->md_minor
;
8331 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8332 desc
, mdname(mddev
),
8336 if (signal_pending(current
))
8337 flush_signals(current
);
8339 finish_wait(&resync_wait
, &wq
);
8342 finish_wait(&resync_wait
, &wq
);
8345 } while (mddev
->curr_resync
< 2);
8348 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
8349 /* resync follows the size requested by the personality,
8350 * which defaults to physical size, but can be virtual size
8352 max_sectors
= mddev
->resync_max_sectors
;
8353 atomic64_set(&mddev
->resync_mismatches
, 0);
8354 /* we don't use the checkpoint if there's a bitmap */
8355 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
8356 j
= mddev
->resync_min
;
8357 else if (!mddev
->bitmap
)
8358 j
= mddev
->recovery_cp
;
8360 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
8361 max_sectors
= mddev
->resync_max_sectors
;
8363 /* recovery follows the physical size of devices */
8364 max_sectors
= mddev
->dev_sectors
;
8367 rdev_for_each_rcu(rdev
, mddev
)
8368 if (rdev
->raid_disk
>= 0 &&
8369 !test_bit(Journal
, &rdev
->flags
) &&
8370 !test_bit(Faulty
, &rdev
->flags
) &&
8371 !test_bit(In_sync
, &rdev
->flags
) &&
8372 rdev
->recovery_offset
< j
)
8373 j
= rdev
->recovery_offset
;
8376 /* If there is a bitmap, we need to make sure all
8377 * writes that started before we added a spare
8378 * complete before we start doing a recovery.
8379 * Otherwise the write might complete and (via
8380 * bitmap_endwrite) set a bit in the bitmap after the
8381 * recovery has checked that bit and skipped that
8384 if (mddev
->bitmap
) {
8385 mddev
->pers
->quiesce(mddev
, 1);
8386 mddev
->pers
->quiesce(mddev
, 0);
8390 pr_info("md: %s of RAID array %s\n", desc
, mdname(mddev
));
8391 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev
));
8392 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8393 speed_max(mddev
), desc
);
8395 is_mddev_idle(mddev
, 1); /* this initializes IO event counters */
8398 for (m
= 0; m
< SYNC_MARKS
; m
++) {
8400 mark_cnt
[m
] = io_sectors
;
8403 mddev
->resync_mark
= mark
[last_mark
];
8404 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
8407 * Tune reconstruction:
8409 window
= 32*(PAGE_SIZE
/512);
8410 pr_debug("md: using %dk window, over a total of %lluk.\n",
8411 window
/2, (unsigned long long)max_sectors
/2);
8413 atomic_set(&mddev
->recovery_active
, 0);
8417 pr_debug("md: resuming %s of %s from checkpoint.\n",
8418 desc
, mdname(mddev
));
8419 mddev
->curr_resync
= j
;
8421 mddev
->curr_resync
= 3; /* no longer delayed */
8422 mddev
->curr_resync_completed
= j
;
8423 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
8424 md_new_event(mddev
);
8425 update_time
= jiffies
;
8427 blk_start_plug(&plug
);
8428 while (j
< max_sectors
) {
8433 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8434 ((mddev
->curr_resync
> mddev
->curr_resync_completed
&&
8435 (mddev
->curr_resync
- mddev
->curr_resync_completed
)
8436 > (max_sectors
>> 4)) ||
8437 time_after_eq(jiffies
, update_time
+ UPDATE_FREQUENCY
) ||
8438 (j
- mddev
->curr_resync_completed
)*2
8439 >= mddev
->resync_max
- mddev
->curr_resync_completed
||
8440 mddev
->curr_resync_completed
> mddev
->resync_max
8442 /* time to update curr_resync_completed */
8443 wait_event(mddev
->recovery_wait
,
8444 atomic_read(&mddev
->recovery_active
) == 0);
8445 mddev
->curr_resync_completed
= j
;
8446 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) &&
8447 j
> mddev
->recovery_cp
)
8448 mddev
->recovery_cp
= j
;
8449 update_time
= jiffies
;
8450 set_bit(MD_SB_CHANGE_CLEAN
, &mddev
->sb_flags
);
8451 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
8454 while (j
>= mddev
->resync_max
&&
8455 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
8456 /* As this condition is controlled by user-space,
8457 * we can block indefinitely, so use '_interruptible'
8458 * to avoid triggering warnings.
8460 flush_signals(current
); /* just in case */
8461 wait_event_interruptible(mddev
->recovery_wait
,
8462 mddev
->resync_max
> j
8463 || test_bit(MD_RECOVERY_INTR
,
8467 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8470 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
);
8472 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8476 if (!skipped
) { /* actual IO requested */
8477 io_sectors
+= sectors
;
8478 atomic_add(sectors
, &mddev
->recovery_active
);
8481 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8485 if (j
> max_sectors
)
8486 /* when skipping, extra large numbers can be returned. */
8489 mddev
->curr_resync
= j
;
8490 mddev
->curr_mark_cnt
= io_sectors
;
8491 if (last_check
== 0)
8492 /* this is the earliest that rebuild will be
8493 * visible in /proc/mdstat
8495 md_new_event(mddev
);
8497 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
8500 last_check
= io_sectors
;
8502 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
8504 int next
= (last_mark
+1) % SYNC_MARKS
;
8506 mddev
->resync_mark
= mark
[next
];
8507 mddev
->resync_mark_cnt
= mark_cnt
[next
];
8508 mark
[next
] = jiffies
;
8509 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
8513 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8517 * this loop exits only if either when we are slower than
8518 * the 'hard' speed limit, or the system was IO-idle for
8520 * the system might be non-idle CPU-wise, but we only care
8521 * about not overloading the IO subsystem. (things like an
8522 * e2fsck being done on the RAID array should execute fast)
8526 recovery_done
= io_sectors
- atomic_read(&mddev
->recovery_active
);
8527 currspeed
= ((unsigned long)(recovery_done
- mddev
->resync_mark_cnt
))/2
8528 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
8530 if (currspeed
> speed_min(mddev
)) {
8531 if (currspeed
> speed_max(mddev
)) {
8535 if (!is_mddev_idle(mddev
, 0)) {
8537 * Give other IO more of a chance.
8538 * The faster the devices, the less we wait.
8540 wait_event(mddev
->recovery_wait
,
8541 !atomic_read(&mddev
->recovery_active
));
8545 pr_info("md: %s: %s %s.\n",mdname(mddev
), desc
,
8546 test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)
8547 ? "interrupted" : "done");
8549 * this also signals 'finished resyncing' to md_stop
8551 blk_finish_plug(&plug
);
8552 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
8554 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8555 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
8556 mddev
->curr_resync
> 3) {
8557 mddev
->curr_resync_completed
= mddev
->curr_resync
;
8558 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
8560 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
);
8562 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
8563 mddev
->curr_resync
> 3) {
8564 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
8565 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
8566 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
8567 pr_debug("md: checkpointing %s of %s.\n",
8568 desc
, mdname(mddev
));
8569 if (test_bit(MD_RECOVERY_ERROR
,
8571 mddev
->recovery_cp
=
8572 mddev
->curr_resync_completed
;
8574 mddev
->recovery_cp
=
8578 mddev
->recovery_cp
= MaxSector
;
8580 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8581 mddev
->curr_resync
= MaxSector
;
8582 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8583 test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
)) {
8585 rdev_for_each_rcu(rdev
, mddev
)
8586 if (rdev
->raid_disk
>= 0 &&
8587 mddev
->delta_disks
>= 0 &&
8588 !test_bit(Journal
, &rdev
->flags
) &&
8589 !test_bit(Faulty
, &rdev
->flags
) &&
8590 !test_bit(In_sync
, &rdev
->flags
) &&
8591 rdev
->recovery_offset
< mddev
->curr_resync
)
8592 rdev
->recovery_offset
= mddev
->curr_resync
;
8598 /* set CHANGE_PENDING here since maybe another update is needed,
8599 * so other nodes are informed. It should be harmless for normal
8601 set_mask_bits(&mddev
->sb_flags
, 0,
8602 BIT(MD_SB_CHANGE_PENDING
) | BIT(MD_SB_CHANGE_DEVS
));
8604 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8605 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
8606 mddev
->delta_disks
> 0 &&
8607 mddev
->pers
->finish_reshape
&&
8608 mddev
->pers
->size
&&
8610 mddev_lock_nointr(mddev
);
8611 md_set_array_sectors(mddev
, mddev
->pers
->size(mddev
, 0, 0));
8612 mddev_unlock(mddev
);
8613 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
8614 revalidate_disk(mddev
->gendisk
);
8617 spin_lock(&mddev
->lock
);
8618 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
8619 /* We completed so min/max setting can be forgotten if used. */
8620 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
8621 mddev
->resync_min
= 0;
8622 mddev
->resync_max
= MaxSector
;
8623 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
8624 mddev
->resync_min
= mddev
->curr_resync_completed
;
8625 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
8626 mddev
->curr_resync
= 0;
8627 spin_unlock(&mddev
->lock
);
8629 wake_up(&resync_wait
);
8630 md_wakeup_thread(mddev
->thread
);
8633 EXPORT_SYMBOL_GPL(md_do_sync
);
8635 static int remove_and_add_spares(struct mddev
*mddev
,
8636 struct md_rdev
*this)
8638 struct md_rdev
*rdev
;
8641 bool remove_some
= false;
8643 if (this && test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
8644 /* Mustn't remove devices when resync thread is running */
8647 rdev_for_each(rdev
, mddev
) {
8648 if ((this == NULL
|| rdev
== this) &&
8649 rdev
->raid_disk
>= 0 &&
8650 !test_bit(Blocked
, &rdev
->flags
) &&
8651 test_bit(Faulty
, &rdev
->flags
) &&
8652 atomic_read(&rdev
->nr_pending
)==0) {
8653 /* Faulty non-Blocked devices with nr_pending == 0
8654 * never get nr_pending incremented,
8655 * never get Faulty cleared, and never get Blocked set.
8656 * So we can synchronize_rcu now rather than once per device
8659 set_bit(RemoveSynchronized
, &rdev
->flags
);
8665 rdev_for_each(rdev
, mddev
) {
8666 if ((this == NULL
|| rdev
== this) &&
8667 rdev
->raid_disk
>= 0 &&
8668 !test_bit(Blocked
, &rdev
->flags
) &&
8669 ((test_bit(RemoveSynchronized
, &rdev
->flags
) ||
8670 (!test_bit(In_sync
, &rdev
->flags
) &&
8671 !test_bit(Journal
, &rdev
->flags
))) &&
8672 atomic_read(&rdev
->nr_pending
)==0)) {
8673 if (mddev
->pers
->hot_remove_disk(
8674 mddev
, rdev
) == 0) {
8675 sysfs_unlink_rdev(mddev
, rdev
);
8676 rdev
->saved_raid_disk
= rdev
->raid_disk
;
8677 rdev
->raid_disk
= -1;
8681 if (remove_some
&& test_bit(RemoveSynchronized
, &rdev
->flags
))
8682 clear_bit(RemoveSynchronized
, &rdev
->flags
);
8685 if (removed
&& mddev
->kobj
.sd
)
8686 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
8688 if (this && removed
)
8691 rdev_for_each(rdev
, mddev
) {
8692 if (this && this != rdev
)
8694 if (test_bit(Candidate
, &rdev
->flags
))
8696 if (rdev
->raid_disk
>= 0 &&
8697 !test_bit(In_sync
, &rdev
->flags
) &&
8698 !test_bit(Journal
, &rdev
->flags
) &&
8699 !test_bit(Faulty
, &rdev
->flags
))
8701 if (rdev
->raid_disk
>= 0)
8703 if (test_bit(Faulty
, &rdev
->flags
))
8705 if (!test_bit(Journal
, &rdev
->flags
)) {
8707 ! (rdev
->saved_raid_disk
>= 0 &&
8708 !test_bit(Bitmap_sync
, &rdev
->flags
)))
8711 rdev
->recovery_offset
= 0;
8714 hot_add_disk(mddev
, rdev
) == 0) {
8715 if (sysfs_link_rdev(mddev
, rdev
))
8716 /* failure here is OK */;
8717 if (!test_bit(Journal
, &rdev
->flags
))
8719 md_new_event(mddev
);
8720 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
8725 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
8729 static void md_start_sync(struct work_struct
*ws
)
8731 struct mddev
*mddev
= container_of(ws
, struct mddev
, del_work
);
8733 mddev
->sync_thread
= md_register_thread(md_do_sync
,
8736 if (!mddev
->sync_thread
) {
8737 pr_warn("%s: could not start resync thread...\n",
8739 /* leave the spares where they are, it shouldn't hurt */
8740 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
8741 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
8742 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
8743 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
8744 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
8745 wake_up(&resync_wait
);
8746 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
8748 if (mddev
->sysfs_action
)
8749 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
8751 md_wakeup_thread(mddev
->sync_thread
);
8752 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
8753 md_new_event(mddev
);
8757 * This routine is regularly called by all per-raid-array threads to
8758 * deal with generic issues like resync and super-block update.
8759 * Raid personalities that don't have a thread (linear/raid0) do not
8760 * need this as they never do any recovery or update the superblock.
8762 * It does not do any resync itself, but rather "forks" off other threads
8763 * to do that as needed.
8764 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8765 * "->recovery" and create a thread at ->sync_thread.
8766 * When the thread finishes it sets MD_RECOVERY_DONE
8767 * and wakeups up this thread which will reap the thread and finish up.
8768 * This thread also removes any faulty devices (with nr_pending == 0).
8770 * The overall approach is:
8771 * 1/ if the superblock needs updating, update it.
8772 * 2/ If a recovery thread is running, don't do anything else.
8773 * 3/ If recovery has finished, clean up, possibly marking spares active.
8774 * 4/ If there are any faulty devices, remove them.
8775 * 5/ If array is degraded, try to add spares devices
8776 * 6/ If array has spares or is not in-sync, start a resync thread.
8778 void md_check_recovery(struct mddev
*mddev
)
8780 if (mddev
->suspended
)
8784 bitmap_daemon_work(mddev
);
8786 if (signal_pending(current
)) {
8787 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
8788 pr_debug("md: %s in immediate safe mode\n",
8790 mddev
->safemode
= 2;
8792 flush_signals(current
);
8795 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
8798 (mddev
->sb_flags
& ~ (1<<MD_SB_CHANGE_PENDING
)) ||
8799 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
8800 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
8801 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
8802 (mddev
->safemode
== 2
8803 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
8807 if (mddev_trylock(mddev
)) {
8810 if (!mddev
->external
&& mddev
->safemode
== 1)
8811 mddev
->safemode
= 0;
8814 struct md_rdev
*rdev
;
8815 if (!mddev
->external
&& mddev
->in_sync
)
8816 /* 'Blocked' flag not needed as failed devices
8817 * will be recorded if array switched to read/write.
8818 * Leaving it set will prevent the device
8819 * from being removed.
8821 rdev_for_each(rdev
, mddev
)
8822 clear_bit(Blocked
, &rdev
->flags
);
8823 /* On a read-only array we can:
8824 * - remove failed devices
8825 * - add already-in_sync devices if the array itself
8827 * As we only add devices that are already in-sync,
8828 * we can activate the spares immediately.
8830 remove_and_add_spares(mddev
, NULL
);
8831 /* There is no thread, but we need to call
8832 * ->spare_active and clear saved_raid_disk
8834 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8835 md_reap_sync_thread(mddev
);
8836 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8837 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8838 clear_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
);
8842 if (mddev_is_clustered(mddev
)) {
8843 struct md_rdev
*rdev
;
8844 /* kick the device if another node issued a
8847 rdev_for_each(rdev
, mddev
) {
8848 if (test_and_clear_bit(ClusterRemove
, &rdev
->flags
) &&
8849 rdev
->raid_disk
< 0)
8850 md_kick_rdev_from_array(rdev
);
8854 if (!mddev
->external
&& !mddev
->in_sync
) {
8855 spin_lock(&mddev
->lock
);
8857 spin_unlock(&mddev
->lock
);
8860 if (mddev
->sb_flags
)
8861 md_update_sb(mddev
, 0);
8863 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
8864 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
8865 /* resync/recovery still happening */
8866 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8869 if (mddev
->sync_thread
) {
8870 md_reap_sync_thread(mddev
);
8873 /* Set RUNNING before clearing NEEDED to avoid
8874 * any transients in the value of "sync_action".
8876 mddev
->curr_resync_completed
= 0;
8877 spin_lock(&mddev
->lock
);
8878 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
8879 spin_unlock(&mddev
->lock
);
8880 /* Clear some bits that don't mean anything, but
8883 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8884 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
8886 if (!test_and_clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
8887 test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
8889 /* no recovery is running.
8890 * remove any failed drives, then
8891 * add spares if possible.
8892 * Spares are also removed and re-added, to allow
8893 * the personality to fail the re-add.
8896 if (mddev
->reshape_position
!= MaxSector
) {
8897 if (mddev
->pers
->check_reshape
== NULL
||
8898 mddev
->pers
->check_reshape(mddev
) != 0)
8899 /* Cannot proceed */
8901 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
8902 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8903 } else if ((spares
= remove_and_add_spares(mddev
, NULL
))) {
8904 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
8905 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
8906 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
8907 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8908 } else if (mddev
->recovery_cp
< MaxSector
) {
8909 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
8910 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8911 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
8912 /* nothing to be done ... */
8915 if (mddev
->pers
->sync_request
) {
8917 /* We are adding a device or devices to an array
8918 * which has the bitmap stored on all devices.
8919 * So make sure all bitmap pages get written
8921 bitmap_write_all(mddev
->bitmap
);
8923 INIT_WORK(&mddev
->del_work
, md_start_sync
);
8924 queue_work(md_misc_wq
, &mddev
->del_work
);
8928 if (!mddev
->sync_thread
) {
8929 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
8930 wake_up(&resync_wait
);
8931 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
8933 if (mddev
->sysfs_action
)
8934 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
8937 wake_up(&mddev
->sb_wait
);
8938 mddev_unlock(mddev
);
8939 } else if (test_bit(MD_ALLOW_SB_UPDATE
, &mddev
->flags
) && mddev
->sb_flags
) {
8940 /* Write superblock - thread that called mddev_suspend()
8941 * holds reconfig_mutex for us.
8943 set_bit(MD_UPDATING_SB
, &mddev
->flags
);
8944 smp_mb__after_atomic();
8945 if (test_bit(MD_ALLOW_SB_UPDATE
, &mddev
->flags
))
8946 md_update_sb(mddev
, 0);
8947 clear_bit_unlock(MD_UPDATING_SB
, &mddev
->flags
);
8948 wake_up(&mddev
->sb_wait
);
8951 EXPORT_SYMBOL(md_check_recovery
);
8953 void md_reap_sync_thread(struct mddev
*mddev
)
8955 struct md_rdev
*rdev
;
8957 /* resync has finished, collect result */
8958 md_unregister_thread(&mddev
->sync_thread
);
8959 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
8960 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
8962 /* activate any spares */
8963 if (mddev
->pers
->spare_active(mddev
)) {
8964 sysfs_notify(&mddev
->kobj
, NULL
,
8966 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
8969 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8970 mddev
->pers
->finish_reshape
)
8971 mddev
->pers
->finish_reshape(mddev
);
8973 /* If array is no-longer degraded, then any saved_raid_disk
8974 * information must be scrapped.
8976 if (!mddev
->degraded
)
8977 rdev_for_each(rdev
, mddev
)
8978 rdev
->saved_raid_disk
= -1;
8980 md_update_sb(mddev
, 1);
8981 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
8982 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
8984 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED
, &mddev
->flags
))
8985 md_cluster_ops
->resync_finish(mddev
);
8986 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
8987 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
8988 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
8989 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
8990 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
8991 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
8992 wake_up(&resync_wait
);
8993 /* flag recovery needed just to double check */
8994 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8995 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
8996 md_new_event(mddev
);
8997 if (mddev
->event_work
.func
)
8998 queue_work(md_misc_wq
, &mddev
->event_work
);
9000 EXPORT_SYMBOL(md_reap_sync_thread
);
9002 void md_wait_for_blocked_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
9004 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
9005 wait_event_timeout(rdev
->blocked_wait
,
9006 !test_bit(Blocked
, &rdev
->flags
) &&
9007 !test_bit(BlockedBadBlocks
, &rdev
->flags
),
9008 msecs_to_jiffies(5000));
9009 rdev_dec_pending(rdev
, mddev
);
9011 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
9013 void md_finish_reshape(struct mddev
*mddev
)
9015 /* called be personality module when reshape completes. */
9016 struct md_rdev
*rdev
;
9018 rdev_for_each(rdev
, mddev
) {
9019 if (rdev
->data_offset
> rdev
->new_data_offset
)
9020 rdev
->sectors
+= rdev
->data_offset
- rdev
->new_data_offset
;
9022 rdev
->sectors
-= rdev
->new_data_offset
- rdev
->data_offset
;
9023 rdev
->data_offset
= rdev
->new_data_offset
;
9026 EXPORT_SYMBOL(md_finish_reshape
);
9028 /* Bad block management */
9030 /* Returns 1 on success, 0 on failure */
9031 int rdev_set_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
,
9034 struct mddev
*mddev
= rdev
->mddev
;
9037 s
+= rdev
->new_data_offset
;
9039 s
+= rdev
->data_offset
;
9040 rv
= badblocks_set(&rdev
->badblocks
, s
, sectors
, 0);
9042 /* Make sure they get written out promptly */
9043 if (test_bit(ExternalBbl
, &rdev
->flags
))
9044 sysfs_notify(&rdev
->kobj
, NULL
,
9045 "unacknowledged_bad_blocks");
9046 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
9047 set_mask_bits(&mddev
->sb_flags
, 0,
9048 BIT(MD_SB_CHANGE_CLEAN
) | BIT(MD_SB_CHANGE_PENDING
));
9049 md_wakeup_thread(rdev
->mddev
->thread
);
9054 EXPORT_SYMBOL_GPL(rdev_set_badblocks
);
9056 int rdev_clear_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
,
9061 s
+= rdev
->new_data_offset
;
9063 s
+= rdev
->data_offset
;
9064 rv
= badblocks_clear(&rdev
->badblocks
, s
, sectors
);
9065 if ((rv
== 0) && test_bit(ExternalBbl
, &rdev
->flags
))
9066 sysfs_notify(&rdev
->kobj
, NULL
, "bad_blocks");
9069 EXPORT_SYMBOL_GPL(rdev_clear_badblocks
);
9071 static int md_notify_reboot(struct notifier_block
*this,
9072 unsigned long code
, void *x
)
9074 struct list_head
*tmp
;
9075 struct mddev
*mddev
;
9078 for_each_mddev(mddev
, tmp
) {
9079 if (mddev_trylock(mddev
)) {
9081 __md_stop_writes(mddev
);
9082 if (mddev
->persistent
)
9083 mddev
->safemode
= 2;
9084 mddev_unlock(mddev
);
9089 * certain more exotic SCSI devices are known to be
9090 * volatile wrt too early system reboots. While the
9091 * right place to handle this issue is the given
9092 * driver, we do want to have a safe RAID driver ...
9100 static struct notifier_block md_notifier
= {
9101 .notifier_call
= md_notify_reboot
,
9103 .priority
= INT_MAX
, /* before any real devices */
9106 static void md_geninit(void)
9108 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
9110 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
9113 static int __init
md_init(void)
9117 md_wq
= alloc_workqueue("md", WQ_MEM_RECLAIM
, 0);
9121 md_misc_wq
= alloc_workqueue("md_misc", 0, 0);
9125 if ((ret
= register_blkdev(MD_MAJOR
, "md")) < 0)
9128 if ((ret
= register_blkdev(0, "mdp")) < 0)
9132 blk_register_region(MKDEV(MD_MAJOR
, 0), 512, THIS_MODULE
,
9133 md_probe
, NULL
, NULL
);
9134 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
9135 md_probe
, NULL
, NULL
);
9137 register_reboot_notifier(&md_notifier
);
9138 raid_table_header
= register_sysctl_table(raid_root_table
);
9144 unregister_blkdev(MD_MAJOR
, "md");
9146 destroy_workqueue(md_misc_wq
);
9148 destroy_workqueue(md_wq
);
9153 static void check_sb_changes(struct mddev
*mddev
, struct md_rdev
*rdev
)
9155 struct mdp_superblock_1
*sb
= page_address(rdev
->sb_page
);
9156 struct md_rdev
*rdev2
;
9158 char b
[BDEVNAME_SIZE
];
9161 * If size is changed in another node then we need to
9162 * do resize as well.
9164 if (mddev
->dev_sectors
!= le64_to_cpu(sb
->size
)) {
9165 ret
= mddev
->pers
->resize(mddev
, le64_to_cpu(sb
->size
));
9167 pr_info("md-cluster: resize failed\n");
9169 bitmap_update_sb(mddev
->bitmap
);
9172 /* Check for change of roles in the active devices */
9173 rdev_for_each(rdev2
, mddev
) {
9174 if (test_bit(Faulty
, &rdev2
->flags
))
9177 /* Check if the roles changed */
9178 role
= le16_to_cpu(sb
->dev_roles
[rdev2
->desc_nr
]);
9180 if (test_bit(Candidate
, &rdev2
->flags
)) {
9181 if (role
== 0xfffe) {
9182 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2
->bdev
,b
));
9183 md_kick_rdev_from_array(rdev2
);
9187 clear_bit(Candidate
, &rdev2
->flags
);
9190 if (role
!= rdev2
->raid_disk
) {
9192 if (rdev2
->raid_disk
== -1 && role
!= 0xffff) {
9193 rdev2
->saved_raid_disk
= role
;
9194 ret
= remove_and_add_spares(mddev
, rdev2
);
9195 pr_info("Activated spare: %s\n",
9196 bdevname(rdev2
->bdev
,b
));
9197 /* wakeup mddev->thread here, so array could
9198 * perform resync with the new activated disk */
9199 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
9200 md_wakeup_thread(mddev
->thread
);
9204 * We just want to do the minimum to mark the disk
9205 * as faulty. The recovery is performed by the
9206 * one who initiated the error.
9208 if ((role
== 0xfffe) || (role
== 0xfffd)) {
9209 md_error(mddev
, rdev2
);
9210 clear_bit(Blocked
, &rdev2
->flags
);
9215 if (mddev
->raid_disks
!= le32_to_cpu(sb
->raid_disks
))
9216 update_raid_disks(mddev
, le32_to_cpu(sb
->raid_disks
));
9218 /* Finally set the event to be up to date */
9219 mddev
->events
= le64_to_cpu(sb
->events
);
9222 static int read_rdev(struct mddev
*mddev
, struct md_rdev
*rdev
)
9225 struct page
*swapout
= rdev
->sb_page
;
9226 struct mdp_superblock_1
*sb
;
9228 /* Store the sb page of the rdev in the swapout temporary
9229 * variable in case we err in the future
9231 rdev
->sb_page
= NULL
;
9232 err
= alloc_disk_sb(rdev
);
9234 ClearPageUptodate(rdev
->sb_page
);
9235 rdev
->sb_loaded
= 0;
9236 err
= super_types
[mddev
->major_version
].
9237 load_super(rdev
, NULL
, mddev
->minor_version
);
9240 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9241 __func__
, __LINE__
, rdev
->desc_nr
, err
);
9243 put_page(rdev
->sb_page
);
9244 rdev
->sb_page
= swapout
;
9245 rdev
->sb_loaded
= 1;
9249 sb
= page_address(rdev
->sb_page
);
9250 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9254 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RECOVERY_OFFSET
))
9255 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
9257 /* The other node finished recovery, call spare_active to set
9258 * device In_sync and mddev->degraded
9260 if (rdev
->recovery_offset
== MaxSector
&&
9261 !test_bit(In_sync
, &rdev
->flags
) &&
9262 mddev
->pers
->spare_active(mddev
))
9263 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
9269 void md_reload_sb(struct mddev
*mddev
, int nr
)
9271 struct md_rdev
*rdev
;
9275 rdev_for_each_rcu(rdev
, mddev
) {
9276 if (rdev
->desc_nr
== nr
)
9280 if (!rdev
|| rdev
->desc_nr
!= nr
) {
9281 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__
, __LINE__
, nr
);
9285 err
= read_rdev(mddev
, rdev
);
9289 check_sb_changes(mddev
, rdev
);
9291 /* Read all rdev's to update recovery_offset */
9292 rdev_for_each_rcu(rdev
, mddev
) {
9293 if (!test_bit(Faulty
, &rdev
->flags
))
9294 read_rdev(mddev
, rdev
);
9297 EXPORT_SYMBOL(md_reload_sb
);
9302 * Searches all registered partitions for autorun RAID arrays
9306 static DEFINE_MUTEX(detected_devices_mutex
);
9307 static LIST_HEAD(all_detected_devices
);
9308 struct detected_devices_node
{
9309 struct list_head list
;
9313 void md_autodetect_dev(dev_t dev
)
9315 struct detected_devices_node
*node_detected_dev
;
9317 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
9318 if (node_detected_dev
) {
9319 node_detected_dev
->dev
= dev
;
9320 mutex_lock(&detected_devices_mutex
);
9321 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
9322 mutex_unlock(&detected_devices_mutex
);
9326 static void autostart_arrays(int part
)
9328 struct md_rdev
*rdev
;
9329 struct detected_devices_node
*node_detected_dev
;
9331 int i_scanned
, i_passed
;
9336 pr_info("md: Autodetecting RAID arrays.\n");
9338 mutex_lock(&detected_devices_mutex
);
9339 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
9341 node_detected_dev
= list_entry(all_detected_devices
.next
,
9342 struct detected_devices_node
, list
);
9343 list_del(&node_detected_dev
->list
);
9344 dev
= node_detected_dev
->dev
;
9345 kfree(node_detected_dev
);
9346 mutex_unlock(&detected_devices_mutex
);
9347 rdev
= md_import_device(dev
,0, 90);
9348 mutex_lock(&detected_devices_mutex
);
9352 if (test_bit(Faulty
, &rdev
->flags
))
9355 set_bit(AutoDetected
, &rdev
->flags
);
9356 list_add(&rdev
->same_set
, &pending_raid_disks
);
9359 mutex_unlock(&detected_devices_mutex
);
9361 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned
, i_passed
);
9363 autorun_devices(part
);
9366 #endif /* !MODULE */
9368 static __exit
void md_exit(void)
9370 struct mddev
*mddev
;
9371 struct list_head
*tmp
;
9374 blk_unregister_region(MKDEV(MD_MAJOR
,0), 512);
9375 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
9377 unregister_blkdev(MD_MAJOR
,"md");
9378 unregister_blkdev(mdp_major
, "mdp");
9379 unregister_reboot_notifier(&md_notifier
);
9380 unregister_sysctl_table(raid_table_header
);
9382 /* We cannot unload the modules while some process is
9383 * waiting for us in select() or poll() - wake them up
9386 while (waitqueue_active(&md_event_waiters
)) {
9387 /* not safe to leave yet */
9388 wake_up(&md_event_waiters
);
9392 remove_proc_entry("mdstat", NULL
);
9394 for_each_mddev(mddev
, tmp
) {
9395 export_array(mddev
);
9397 mddev
->hold_active
= 0;
9399 * for_each_mddev() will call mddev_put() at the end of each
9400 * iteration. As the mddev is now fully clear, this will
9401 * schedule the mddev for destruction by a workqueue, and the
9402 * destroy_workqueue() below will wait for that to complete.
9405 destroy_workqueue(md_misc_wq
);
9406 destroy_workqueue(md_wq
);
9409 subsys_initcall(md_init
);
9410 module_exit(md_exit
)
9412 static int get_ro(char *buffer
, const struct kernel_param
*kp
)
9414 return sprintf(buffer
, "%d", start_readonly
);
9416 static int set_ro(const char *val
, const struct kernel_param
*kp
)
9418 return kstrtouint(val
, 10, (unsigned int *)&start_readonly
);
9421 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
9422 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
9423 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
9424 module_param(create_on_open
, bool, S_IRUSR
|S_IWUSR
);
9426 MODULE_LICENSE("GPL");
9427 MODULE_DESCRIPTION("MD RAID framework");
9429 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);