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>
73 #include "md-cluster.h"
76 static void autostart_arrays(int part);
79 /* pers_list is a list of registered personalities protected
81 * pers_lock does extra service to protect accesses to
82 * mddev->thread when the mutex cannot be held.
84 static LIST_HEAD(pers_list);
85 static DEFINE_SPINLOCK(pers_lock);
87 struct md_cluster_operations *md_cluster_ops;
88 EXPORT_SYMBOL(md_cluster_ops);
89 struct module *md_cluster_mod;
90 EXPORT_SYMBOL(md_cluster_mod);
92 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
93 static struct workqueue_struct *md_wq;
94 static struct workqueue_struct *md_misc_wq;
96 static int remove_and_add_spares(struct mddev *mddev,
97 struct md_rdev *this);
98 static void mddev_detach(struct mddev *mddev);
101 * Default number of read corrections we'll attempt on an rdev
102 * before ejecting it from the array. We divide the read error
103 * count by 2 for every hour elapsed between read errors.
105 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
107 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
108 * is 1000 KB/sec, so the extra system load does not show up that much.
109 * Increase it if you want to have more _guaranteed_ speed. Note that
110 * the RAID driver will use the maximum available bandwidth if the IO
111 * subsystem is idle. There is also an 'absolute maximum' reconstruction
112 * speed limit - in case reconstruction slows down your system despite
115 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
116 * or /sys/block/mdX/md/sync_speed_{min,max}
119 static int sysctl_speed_limit_min = 1000;
120 static int sysctl_speed_limit_max = 200000;
121 static inline int speed_min(struct mddev *mddev)
123 return mddev->sync_speed_min ?
124 mddev->sync_speed_min : sysctl_speed_limit_min;
127 static inline int speed_max(struct mddev *mddev)
129 return mddev->sync_speed_max ?
130 mddev->sync_speed_max : sysctl_speed_limit_max;
133 static struct ctl_table_header *raid_table_header;
135 static struct ctl_table raid_table[] = {
137 .procname = "speed_limit_min",
138 .data = &sysctl_speed_limit_min,
139 .maxlen = sizeof(int),
140 .mode = S_IRUGO|S_IWUSR,
141 .proc_handler = proc_dointvec,
144 .procname = "speed_limit_max",
145 .data = &sysctl_speed_limit_max,
146 .maxlen = sizeof(int),
147 .mode = S_IRUGO|S_IWUSR,
148 .proc_handler = proc_dointvec,
153 static struct ctl_table raid_dir_table[] = {
157 .mode = S_IRUGO|S_IXUGO,
163 static struct ctl_table raid_root_table[] = {
168 .child = raid_dir_table,
173 static const struct block_device_operations md_fops;
175 static int start_readonly;
178 * The original mechanism for creating an md device is to create
179 * a device node in /dev and to open it. This causes races with device-close.
180 * The preferred method is to write to the "new_array" module parameter.
181 * This can avoid races.
182 * Setting create_on_open to false disables the original mechanism
183 * so all the races disappear.
185 static bool create_on_open = true;
188 * like bio_clone_bioset, but with a local bio set
191 struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
196 if (!mddev || !mddev->bio_set)
197 return bio_alloc(gfp_mask, nr_iovecs);
199 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
204 EXPORT_SYMBOL_GPL(bio_alloc_mddev);
207 * We have a system wide 'event count' that is incremented
208 * on any 'interesting' event, and readers of /proc/mdstat
209 * can use 'poll' or 'select' to find out when the event
213 * start array, stop array, error, add device, remove device,
214 * start build, activate spare
216 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
217 static atomic_t md_event_count;
218 void md_new_event(struct mddev *mddev)
220 atomic_inc(&md_event_count);
221 wake_up(&md_event_waiters);
223 EXPORT_SYMBOL_GPL(md_new_event);
226 * Enables to iterate over all existing md arrays
227 * all_mddevs_lock protects this list.
229 static LIST_HEAD(all_mddevs);
230 static DEFINE_SPINLOCK(all_mddevs_lock);
233 * iterates through all used mddevs in the system.
234 * We take care to grab the all_mddevs_lock whenever navigating
235 * the list, and to always hold a refcount when unlocked.
236 * Any code which breaks out of this loop while own
237 * a reference to the current mddev and must mddev_put it.
239 #define for_each_mddev(_mddev,_tmp) \
241 for (({ spin_lock(&all_mddevs_lock); \
242 _tmp = all_mddevs.next; \
244 ({ if (_tmp != &all_mddevs) \
245 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
246 spin_unlock(&all_mddevs_lock); \
247 if (_mddev) mddev_put(_mddev); \
248 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
249 _tmp != &all_mddevs;}); \
250 ({ spin_lock(&all_mddevs_lock); \
251 _tmp = _tmp->next;}) \
254 /* Rather than calling directly into the personality make_request function,
255 * IO requests come here first so that we can check if the device is
256 * being suspended pending a reconfiguration.
257 * We hold a refcount over the call to ->make_request. By the time that
258 * call has finished, the bio has been linked into some internal structure
259 * and so is visible to ->quiesce(), so we don't need the refcount any more.
261 static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio)
263 const int rw = bio_data_dir(bio);
264 struct mddev *mddev = q->queuedata;
265 unsigned int sectors;
268 blk_queue_split(q, &bio);
270 if (mddev == NULL || mddev->pers == NULL) {
272 return BLK_QC_T_NONE;
274 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
275 if (bio_sectors(bio) != 0)
276 bio->bi_status = BLK_STS_IOERR;
278 return BLK_QC_T_NONE;
280 smp_rmb(); /* Ensure implications of 'active' are visible */
282 if (mddev->suspended) {
285 prepare_to_wait(&mddev->sb_wait, &__wait,
286 TASK_UNINTERRUPTIBLE);
287 if (!mddev->suspended)
293 finish_wait(&mddev->sb_wait, &__wait);
295 atomic_inc(&mddev->active_io);
299 * save the sectors now since our bio can
300 * go away inside make_request
302 sectors = bio_sectors(bio);
303 /* bio could be mergeable after passing to underlayer */
304 bio->bi_opf &= ~REQ_NOMERGE;
305 mddev->pers->make_request(mddev, bio);
307 cpu = part_stat_lock();
308 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
309 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
312 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
313 wake_up(&mddev->sb_wait);
315 return BLK_QC_T_NONE;
318 /* mddev_suspend makes sure no new requests are submitted
319 * to the device, and that any requests that have been submitted
320 * are completely handled.
321 * Once mddev_detach() is called and completes, the module will be
324 void mddev_suspend(struct mddev *mddev)
326 WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk);
327 if (mddev->suspended++)
330 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
331 mddev->pers->quiesce(mddev, 1);
333 del_timer_sync(&mddev->safemode_timer);
335 EXPORT_SYMBOL_GPL(mddev_suspend);
337 void mddev_resume(struct mddev *mddev)
339 if (--mddev->suspended)
341 wake_up(&mddev->sb_wait);
342 mddev->pers->quiesce(mddev, 0);
344 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
345 md_wakeup_thread(mddev->thread);
346 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
348 EXPORT_SYMBOL_GPL(mddev_resume);
350 int mddev_congested(struct mddev *mddev, int bits)
352 struct md_personality *pers = mddev->pers;
356 if (mddev->suspended)
358 else if (pers && pers->congested)
359 ret = pers->congested(mddev, bits);
363 EXPORT_SYMBOL_GPL(mddev_congested);
364 static int md_congested(void *data, int bits)
366 struct mddev *mddev = data;
367 return mddev_congested(mddev, bits);
371 * Generic flush handling for md
374 static void md_end_flush(struct bio *bio)
376 struct md_rdev *rdev = bio->bi_private;
377 struct mddev *mddev = rdev->mddev;
379 rdev_dec_pending(rdev, mddev);
381 if (atomic_dec_and_test(&mddev->flush_pending)) {
382 /* The pre-request flush has finished */
383 queue_work(md_wq, &mddev->flush_work);
388 static void md_submit_flush_data(struct work_struct *ws);
390 static void submit_flushes(struct work_struct *ws)
392 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
393 struct md_rdev *rdev;
395 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
396 atomic_set(&mddev->flush_pending, 1);
398 rdev_for_each_rcu(rdev, mddev)
399 if (rdev->raid_disk >= 0 &&
400 !test_bit(Faulty, &rdev->flags)) {
401 /* Take two references, one is dropped
402 * when request finishes, one after
403 * we reclaim rcu_read_lock
406 atomic_inc(&rdev->nr_pending);
407 atomic_inc(&rdev->nr_pending);
409 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
410 bi->bi_end_io = md_end_flush;
411 bi->bi_private = rdev;
412 bi->bi_bdev = rdev->bdev;
413 bi->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
414 atomic_inc(&mddev->flush_pending);
417 rdev_dec_pending(rdev, mddev);
420 if (atomic_dec_and_test(&mddev->flush_pending))
421 queue_work(md_wq, &mddev->flush_work);
424 static void md_submit_flush_data(struct work_struct *ws)
426 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
427 struct bio *bio = mddev->flush_bio;
429 if (bio->bi_iter.bi_size == 0)
430 /* an empty barrier - all done */
433 bio->bi_opf &= ~REQ_PREFLUSH;
434 mddev->pers->make_request(mddev, bio);
437 mddev->flush_bio = NULL;
438 wake_up(&mddev->sb_wait);
441 void md_flush_request(struct mddev *mddev, struct bio *bio)
443 spin_lock_irq(&mddev->lock);
444 wait_event_lock_irq(mddev->sb_wait,
447 mddev->flush_bio = bio;
448 spin_unlock_irq(&mddev->lock);
450 INIT_WORK(&mddev->flush_work, submit_flushes);
451 queue_work(md_wq, &mddev->flush_work);
453 EXPORT_SYMBOL(md_flush_request);
455 static inline struct mddev *mddev_get(struct mddev *mddev)
457 atomic_inc(&mddev->active);
461 static void mddev_delayed_delete(struct work_struct *ws);
463 static void mddev_put(struct mddev *mddev)
465 struct bio_set *bs = NULL;
467 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
469 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
470 mddev->ctime == 0 && !mddev->hold_active) {
471 /* Array is not configured at all, and not held active,
473 list_del_init(&mddev->all_mddevs);
475 mddev->bio_set = NULL;
476 if (mddev->gendisk) {
477 /* We did a probe so need to clean up. Call
478 * queue_work inside the spinlock so that
479 * flush_workqueue() after mddev_find will
480 * succeed in waiting for the work to be done.
482 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
483 queue_work(md_misc_wq, &mddev->del_work);
487 spin_unlock(&all_mddevs_lock);
492 static void md_safemode_timeout(unsigned long data);
494 void mddev_init(struct mddev *mddev)
496 mutex_init(&mddev->open_mutex);
497 mutex_init(&mddev->reconfig_mutex);
498 mutex_init(&mddev->bitmap_info.mutex);
499 INIT_LIST_HEAD(&mddev->disks);
500 INIT_LIST_HEAD(&mddev->all_mddevs);
501 setup_timer(&mddev->safemode_timer, md_safemode_timeout,
502 (unsigned long) mddev);
503 atomic_set(&mddev->active, 1);
504 atomic_set(&mddev->openers, 0);
505 atomic_set(&mddev->active_io, 0);
506 spin_lock_init(&mddev->lock);
507 atomic_set(&mddev->flush_pending, 0);
508 init_waitqueue_head(&mddev->sb_wait);
509 init_waitqueue_head(&mddev->recovery_wait);
510 mddev->reshape_position = MaxSector;
511 mddev->reshape_backwards = 0;
512 mddev->last_sync_action = "none";
513 mddev->resync_min = 0;
514 mddev->resync_max = MaxSector;
515 mddev->level = LEVEL_NONE;
517 EXPORT_SYMBOL_GPL(mddev_init);
519 static struct mddev *mddev_find(dev_t unit)
521 struct mddev *mddev, *new = NULL;
523 if (unit && MAJOR(unit) != MD_MAJOR)
524 unit &= ~((1<<MdpMinorShift)-1);
527 spin_lock(&all_mddevs_lock);
530 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
531 if (mddev->unit == unit) {
533 spin_unlock(&all_mddevs_lock);
539 list_add(&new->all_mddevs, &all_mddevs);
540 spin_unlock(&all_mddevs_lock);
541 new->hold_active = UNTIL_IOCTL;
545 /* find an unused unit number */
546 static int next_minor = 512;
547 int start = next_minor;
551 dev = MKDEV(MD_MAJOR, next_minor);
553 if (next_minor > MINORMASK)
555 if (next_minor == start) {
556 /* Oh dear, all in use. */
557 spin_unlock(&all_mddevs_lock);
563 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
564 if (mddev->unit == dev) {
570 new->md_minor = MINOR(dev);
571 new->hold_active = UNTIL_STOP;
572 list_add(&new->all_mddevs, &all_mddevs);
573 spin_unlock(&all_mddevs_lock);
576 spin_unlock(&all_mddevs_lock);
578 new = kzalloc(sizeof(*new), GFP_KERNEL);
583 if (MAJOR(unit) == MD_MAJOR)
584 new->md_minor = MINOR(unit);
586 new->md_minor = MINOR(unit) >> MdpMinorShift;
593 static struct attribute_group md_redundancy_group;
595 void mddev_unlock(struct mddev *mddev)
597 if (mddev->to_remove) {
598 /* These cannot be removed under reconfig_mutex as
599 * an access to the files will try to take reconfig_mutex
600 * while holding the file unremovable, which leads to
602 * So hold set sysfs_active while the remove in happeing,
603 * and anything else which might set ->to_remove or my
604 * otherwise change the sysfs namespace will fail with
605 * -EBUSY if sysfs_active is still set.
606 * We set sysfs_active under reconfig_mutex and elsewhere
607 * test it under the same mutex to ensure its correct value
610 struct attribute_group *to_remove = mddev->to_remove;
611 mddev->to_remove = NULL;
612 mddev->sysfs_active = 1;
613 mutex_unlock(&mddev->reconfig_mutex);
615 if (mddev->kobj.sd) {
616 if (to_remove != &md_redundancy_group)
617 sysfs_remove_group(&mddev->kobj, to_remove);
618 if (mddev->pers == NULL ||
619 mddev->pers->sync_request == NULL) {
620 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
621 if (mddev->sysfs_action)
622 sysfs_put(mddev->sysfs_action);
623 mddev->sysfs_action = NULL;
626 mddev->sysfs_active = 0;
628 mutex_unlock(&mddev->reconfig_mutex);
630 /* As we've dropped the mutex we need a spinlock to
631 * make sure the thread doesn't disappear
633 spin_lock(&pers_lock);
634 md_wakeup_thread(mddev->thread);
635 spin_unlock(&pers_lock);
637 EXPORT_SYMBOL_GPL(mddev_unlock);
639 struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
641 struct md_rdev *rdev;
643 rdev_for_each_rcu(rdev, mddev)
644 if (rdev->desc_nr == nr)
649 EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
651 static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
653 struct md_rdev *rdev;
655 rdev_for_each(rdev, mddev)
656 if (rdev->bdev->bd_dev == dev)
662 static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
664 struct md_rdev *rdev;
666 rdev_for_each_rcu(rdev, mddev)
667 if (rdev->bdev->bd_dev == dev)
673 static struct md_personality *find_pers(int level, char *clevel)
675 struct md_personality *pers;
676 list_for_each_entry(pers, &pers_list, list) {
677 if (level != LEVEL_NONE && pers->level == level)
679 if (strcmp(pers->name, clevel)==0)
685 /* return the offset of the super block in 512byte sectors */
686 static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
688 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
689 return MD_NEW_SIZE_SECTORS(num_sectors);
692 static int alloc_disk_sb(struct md_rdev *rdev)
694 rdev->sb_page = alloc_page(GFP_KERNEL);
700 void md_rdev_clear(struct md_rdev *rdev)
703 put_page(rdev->sb_page);
705 rdev->sb_page = NULL;
710 put_page(rdev->bb_page);
711 rdev->bb_page = NULL;
713 badblocks_exit(&rdev->badblocks);
715 EXPORT_SYMBOL_GPL(md_rdev_clear);
717 static void super_written(struct bio *bio)
719 struct md_rdev *rdev = bio->bi_private;
720 struct mddev *mddev = rdev->mddev;
722 if (bio->bi_status) {
723 pr_err("md: super_written gets error=%d\n", bio->bi_status);
724 md_error(mddev, rdev);
725 if (!test_bit(Faulty, &rdev->flags)
726 && (bio->bi_opf & MD_FAILFAST)) {
727 set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags);
728 set_bit(LastDev, &rdev->flags);
731 clear_bit(LastDev, &rdev->flags);
733 if (atomic_dec_and_test(&mddev->pending_writes))
734 wake_up(&mddev->sb_wait);
735 rdev_dec_pending(rdev, mddev);
739 void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
740 sector_t sector, int size, struct page *page)
742 /* write first size bytes of page to sector of rdev
743 * Increment mddev->pending_writes before returning
744 * and decrement it on completion, waking up sb_wait
745 * if zero is reached.
746 * If an error occurred, call md_error
751 if (test_bit(Faulty, &rdev->flags))
754 bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
756 atomic_inc(&rdev->nr_pending);
758 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
759 bio->bi_iter.bi_sector = sector;
760 bio_add_page(bio, page, size, 0);
761 bio->bi_private = rdev;
762 bio->bi_end_io = super_written;
764 if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) &&
765 test_bit(FailFast, &rdev->flags) &&
766 !test_bit(LastDev, &rdev->flags))
768 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA | ff;
770 atomic_inc(&mddev->pending_writes);
774 int md_super_wait(struct mddev *mddev)
776 /* wait for all superblock writes that were scheduled to complete */
777 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
778 if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags))
783 int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
784 struct page *page, int op, int op_flags, bool metadata_op)
786 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
789 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
790 rdev->meta_bdev : rdev->bdev;
791 bio_set_op_attrs(bio, op, op_flags);
793 bio->bi_iter.bi_sector = sector + rdev->sb_start;
794 else if (rdev->mddev->reshape_position != MaxSector &&
795 (rdev->mddev->reshape_backwards ==
796 (sector >= rdev->mddev->reshape_position)))
797 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
799 bio->bi_iter.bi_sector = sector + rdev->data_offset;
800 bio_add_page(bio, page, size, 0);
802 submit_bio_wait(bio);
804 ret = !bio->bi_status;
808 EXPORT_SYMBOL_GPL(sync_page_io);
810 static int read_disk_sb(struct md_rdev *rdev, int size)
812 char b[BDEVNAME_SIZE];
817 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true))
823 pr_err("md: disabled device %s, could not read superblock.\n",
824 bdevname(rdev->bdev,b));
828 static int md_uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
830 return sb1->set_uuid0 == sb2->set_uuid0 &&
831 sb1->set_uuid1 == sb2->set_uuid1 &&
832 sb1->set_uuid2 == sb2->set_uuid2 &&
833 sb1->set_uuid3 == sb2->set_uuid3;
836 static int md_sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
839 mdp_super_t *tmp1, *tmp2;
841 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
842 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
844 if (!tmp1 || !tmp2) {
853 * nr_disks is not constant
858 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
865 static u32 md_csum_fold(u32 csum)
867 csum = (csum & 0xffff) + (csum >> 16);
868 return (csum & 0xffff) + (csum >> 16);
871 static unsigned int calc_sb_csum(mdp_super_t *sb)
874 u32 *sb32 = (u32*)sb;
876 unsigned int disk_csum, csum;
878 disk_csum = sb->sb_csum;
881 for (i = 0; i < MD_SB_BYTES/4 ; i++)
883 csum = (newcsum & 0xffffffff) + (newcsum>>32);
886 /* This used to use csum_partial, which was wrong for several
887 * reasons including that different results are returned on
888 * different architectures. It isn't critical that we get exactly
889 * the same return value as before (we always csum_fold before
890 * testing, and that removes any differences). However as we
891 * know that csum_partial always returned a 16bit value on
892 * alphas, do a fold to maximise conformity to previous behaviour.
894 sb->sb_csum = md_csum_fold(disk_csum);
896 sb->sb_csum = disk_csum;
902 * Handle superblock details.
903 * We want to be able to handle multiple superblock formats
904 * so we have a common interface to them all, and an array of
905 * different handlers.
906 * We rely on user-space to write the initial superblock, and support
907 * reading and updating of superblocks.
908 * Interface methods are:
909 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
910 * loads and validates a superblock on dev.
911 * if refdev != NULL, compare superblocks on both devices
913 * 0 - dev has a superblock that is compatible with refdev
914 * 1 - dev has a superblock that is compatible and newer than refdev
915 * so dev should be used as the refdev in future
916 * -EINVAL superblock incompatible or invalid
917 * -othererror e.g. -EIO
919 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
920 * Verify that dev is acceptable into mddev.
921 * The first time, mddev->raid_disks will be 0, and data from
922 * dev should be merged in. Subsequent calls check that dev
923 * is new enough. Return 0 or -EINVAL
925 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
926 * Update the superblock for rdev with data in mddev
927 * This does not write to disc.
933 struct module *owner;
934 int (*load_super)(struct md_rdev *rdev,
935 struct md_rdev *refdev,
937 int (*validate_super)(struct mddev *mddev,
938 struct md_rdev *rdev);
939 void (*sync_super)(struct mddev *mddev,
940 struct md_rdev *rdev);
941 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
942 sector_t num_sectors);
943 int (*allow_new_offset)(struct md_rdev *rdev,
944 unsigned long long new_offset);
948 * Check that the given mddev has no bitmap.
950 * This function is called from the run method of all personalities that do not
951 * support bitmaps. It prints an error message and returns non-zero if mddev
952 * has a bitmap. Otherwise, it returns 0.
955 int md_check_no_bitmap(struct mddev *mddev)
957 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
959 pr_warn("%s: bitmaps are not supported for %s\n",
960 mdname(mddev), mddev->pers->name);
963 EXPORT_SYMBOL(md_check_no_bitmap);
966 * load_super for 0.90.0
968 static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
970 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
975 * Calculate the position of the superblock (512byte sectors),
976 * it's at the end of the disk.
978 * It also happens to be a multiple of 4Kb.
980 rdev->sb_start = calc_dev_sboffset(rdev);
982 ret = read_disk_sb(rdev, MD_SB_BYTES);
988 bdevname(rdev->bdev, b);
989 sb = page_address(rdev->sb_page);
991 if (sb->md_magic != MD_SB_MAGIC) {
992 pr_warn("md: invalid raid superblock magic on %s\n", b);
996 if (sb->major_version != 0 ||
997 sb->minor_version < 90 ||
998 sb->minor_version > 91) {
999 pr_warn("Bad version number %d.%d on %s\n",
1000 sb->major_version, sb->minor_version, b);
1004 if (sb->raid_disks <= 0)
1007 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
1008 pr_warn("md: invalid superblock checksum on %s\n", b);
1012 rdev->preferred_minor = sb->md_minor;
1013 rdev->data_offset = 0;
1014 rdev->new_data_offset = 0;
1015 rdev->sb_size = MD_SB_BYTES;
1016 rdev->badblocks.shift = -1;
1018 if (sb->level == LEVEL_MULTIPATH)
1021 rdev->desc_nr = sb->this_disk.number;
1027 mdp_super_t *refsb = page_address(refdev->sb_page);
1028 if (!md_uuid_equal(refsb, sb)) {
1029 pr_warn("md: %s has different UUID to %s\n",
1030 b, bdevname(refdev->bdev,b2));
1033 if (!md_sb_equal(refsb, sb)) {
1034 pr_warn("md: %s has same UUID but different superblock to %s\n",
1035 b, bdevname(refdev->bdev, b2));
1039 ev2 = md_event(refsb);
1045 rdev->sectors = rdev->sb_start;
1046 /* Limit to 4TB as metadata cannot record more than that.
1047 * (not needed for Linear and RAID0 as metadata doesn't
1050 if (IS_ENABLED(CONFIG_LBDAF) && (u64)rdev->sectors >= (2ULL << 32) &&
1052 rdev->sectors = (sector_t)(2ULL << 32) - 2;
1054 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
1055 /* "this cannot possibly happen" ... */
1063 * validate_super for 0.90.0
1065 static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
1068 mdp_super_t *sb = page_address(rdev->sb_page);
1069 __u64 ev1 = md_event(sb);
1071 rdev->raid_disk = -1;
1072 clear_bit(Faulty, &rdev->flags);
1073 clear_bit(In_sync, &rdev->flags);
1074 clear_bit(Bitmap_sync, &rdev->flags);
1075 clear_bit(WriteMostly, &rdev->flags);
1077 if (mddev->raid_disks == 0) {
1078 mddev->major_version = 0;
1079 mddev->minor_version = sb->minor_version;
1080 mddev->patch_version = sb->patch_version;
1081 mddev->external = 0;
1082 mddev->chunk_sectors = sb->chunk_size >> 9;
1083 mddev->ctime = sb->ctime;
1084 mddev->utime = sb->utime;
1085 mddev->level = sb->level;
1086 mddev->clevel[0] = 0;
1087 mddev->layout = sb->layout;
1088 mddev->raid_disks = sb->raid_disks;
1089 mddev->dev_sectors = ((sector_t)sb->size) * 2;
1090 mddev->events = ev1;
1091 mddev->bitmap_info.offset = 0;
1092 mddev->bitmap_info.space = 0;
1093 /* bitmap can use 60 K after the 4K superblocks */
1094 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
1095 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
1096 mddev->reshape_backwards = 0;
1098 if (mddev->minor_version >= 91) {
1099 mddev->reshape_position = sb->reshape_position;
1100 mddev->delta_disks = sb->delta_disks;
1101 mddev->new_level = sb->new_level;
1102 mddev->new_layout = sb->new_layout;
1103 mddev->new_chunk_sectors = sb->new_chunk >> 9;
1104 if (mddev->delta_disks < 0)
1105 mddev->reshape_backwards = 1;
1107 mddev->reshape_position = MaxSector;
1108 mddev->delta_disks = 0;
1109 mddev->new_level = mddev->level;
1110 mddev->new_layout = mddev->layout;
1111 mddev->new_chunk_sectors = mddev->chunk_sectors;
1114 if (sb->state & (1<<MD_SB_CLEAN))
1115 mddev->recovery_cp = MaxSector;
1117 if (sb->events_hi == sb->cp_events_hi &&
1118 sb->events_lo == sb->cp_events_lo) {
1119 mddev->recovery_cp = sb->recovery_cp;
1121 mddev->recovery_cp = 0;
1124 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1125 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1126 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1127 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1129 mddev->max_disks = MD_SB_DISKS;
1131 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
1132 mddev->bitmap_info.file == NULL) {
1133 mddev->bitmap_info.offset =
1134 mddev->bitmap_info.default_offset;
1135 mddev->bitmap_info.space =
1136 mddev->bitmap_info.default_space;
1139 } else if (mddev->pers == NULL) {
1140 /* Insist on good event counter while assembling, except
1141 * for spares (which don't need an event count) */
1143 if (sb->disks[rdev->desc_nr].state & (
1144 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1145 if (ev1 < mddev->events)
1147 } else if (mddev->bitmap) {
1148 /* if adding to array with a bitmap, then we can accept an
1149 * older device ... but not too old.
1151 if (ev1 < mddev->bitmap->events_cleared)
1153 if (ev1 < mddev->events)
1154 set_bit(Bitmap_sync, &rdev->flags);
1156 if (ev1 < mddev->events)
1157 /* just a hot-add of a new device, leave raid_disk at -1 */
1161 if (mddev->level != LEVEL_MULTIPATH) {
1162 desc = sb->disks + rdev->desc_nr;
1164 if (desc->state & (1<<MD_DISK_FAULTY))
1165 set_bit(Faulty, &rdev->flags);
1166 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1167 desc->raid_disk < mddev->raid_disks */) {
1168 set_bit(In_sync, &rdev->flags);
1169 rdev->raid_disk = desc->raid_disk;
1170 rdev->saved_raid_disk = desc->raid_disk;
1171 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1172 /* active but not in sync implies recovery up to
1173 * reshape position. We don't know exactly where
1174 * that is, so set to zero for now */
1175 if (mddev->minor_version >= 91) {
1176 rdev->recovery_offset = 0;
1177 rdev->raid_disk = desc->raid_disk;
1180 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1181 set_bit(WriteMostly, &rdev->flags);
1182 if (desc->state & (1<<MD_DISK_FAILFAST))
1183 set_bit(FailFast, &rdev->flags);
1184 } else /* MULTIPATH are always insync */
1185 set_bit(In_sync, &rdev->flags);
1190 * sync_super for 0.90.0
1192 static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
1195 struct md_rdev *rdev2;
1196 int next_spare = mddev->raid_disks;
1198 /* make rdev->sb match mddev data..
1201 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1202 * 3/ any empty disks < next_spare become removed
1204 * disks[0] gets initialised to REMOVED because
1205 * we cannot be sure from other fields if it has
1206 * been initialised or not.
1209 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1211 rdev->sb_size = MD_SB_BYTES;
1213 sb = page_address(rdev->sb_page);
1215 memset(sb, 0, sizeof(*sb));
1217 sb->md_magic = MD_SB_MAGIC;
1218 sb->major_version = mddev->major_version;
1219 sb->patch_version = mddev->patch_version;
1220 sb->gvalid_words = 0; /* ignored */
1221 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1222 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1223 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1224 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1226 sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
1227 sb->level = mddev->level;
1228 sb->size = mddev->dev_sectors / 2;
1229 sb->raid_disks = mddev->raid_disks;
1230 sb->md_minor = mddev->md_minor;
1231 sb->not_persistent = 0;
1232 sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
1234 sb->events_hi = (mddev->events>>32);
1235 sb->events_lo = (u32)mddev->events;
1237 if (mddev->reshape_position == MaxSector)
1238 sb->minor_version = 90;
1240 sb->minor_version = 91;
1241 sb->reshape_position = mddev->reshape_position;
1242 sb->new_level = mddev->new_level;
1243 sb->delta_disks = mddev->delta_disks;
1244 sb->new_layout = mddev->new_layout;
1245 sb->new_chunk = mddev->new_chunk_sectors << 9;
1247 mddev->minor_version = sb->minor_version;
1250 sb->recovery_cp = mddev->recovery_cp;
1251 sb->cp_events_hi = (mddev->events>>32);
1252 sb->cp_events_lo = (u32)mddev->events;
1253 if (mddev->recovery_cp == MaxSector)
1254 sb->state = (1<< MD_SB_CLEAN);
1256 sb->recovery_cp = 0;
1258 sb->layout = mddev->layout;
1259 sb->chunk_size = mddev->chunk_sectors << 9;
1261 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
1262 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1264 sb->disks[0].state = (1<<MD_DISK_REMOVED);
1265 rdev_for_each(rdev2, mddev) {
1268 int is_active = test_bit(In_sync, &rdev2->flags);
1270 if (rdev2->raid_disk >= 0 &&
1271 sb->minor_version >= 91)
1272 /* we have nowhere to store the recovery_offset,
1273 * but if it is not below the reshape_position,
1274 * we can piggy-back on that.
1277 if (rdev2->raid_disk < 0 ||
1278 test_bit(Faulty, &rdev2->flags))
1281 desc_nr = rdev2->raid_disk;
1283 desc_nr = next_spare++;
1284 rdev2->desc_nr = desc_nr;
1285 d = &sb->disks[rdev2->desc_nr];
1287 d->number = rdev2->desc_nr;
1288 d->major = MAJOR(rdev2->bdev->bd_dev);
1289 d->minor = MINOR(rdev2->bdev->bd_dev);
1291 d->raid_disk = rdev2->raid_disk;
1293 d->raid_disk = rdev2->desc_nr; /* compatibility */
1294 if (test_bit(Faulty, &rdev2->flags))
1295 d->state = (1<<MD_DISK_FAULTY);
1296 else if (is_active) {
1297 d->state = (1<<MD_DISK_ACTIVE);
1298 if (test_bit(In_sync, &rdev2->flags))
1299 d->state |= (1<<MD_DISK_SYNC);
1307 if (test_bit(WriteMostly, &rdev2->flags))
1308 d->state |= (1<<MD_DISK_WRITEMOSTLY);
1309 if (test_bit(FailFast, &rdev2->flags))
1310 d->state |= (1<<MD_DISK_FAILFAST);
1312 /* now set the "removed" and "faulty" bits on any missing devices */
1313 for (i=0 ; i < mddev->raid_disks ; i++) {
1314 mdp_disk_t *d = &sb->disks[i];
1315 if (d->state == 0 && d->number == 0) {
1318 d->state = (1<<MD_DISK_REMOVED);
1319 d->state |= (1<<MD_DISK_FAULTY);
1323 sb->nr_disks = nr_disks;
1324 sb->active_disks = active;
1325 sb->working_disks = working;
1326 sb->failed_disks = failed;
1327 sb->spare_disks = spare;
1329 sb->this_disk = sb->disks[rdev->desc_nr];
1330 sb->sb_csum = calc_sb_csum(sb);
1334 * rdev_size_change for 0.90.0
1336 static unsigned long long
1337 super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
1339 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1340 return 0; /* component must fit device */
1341 if (rdev->mddev->bitmap_info.offset)
1342 return 0; /* can't move bitmap */
1343 rdev->sb_start = calc_dev_sboffset(rdev);
1344 if (!num_sectors || num_sectors > rdev->sb_start)
1345 num_sectors = rdev->sb_start;
1346 /* Limit to 4TB as metadata cannot record more than that.
1347 * 4TB == 2^32 KB, or 2*2^32 sectors.
1349 if (IS_ENABLED(CONFIG_LBDAF) && (u64)num_sectors >= (2ULL << 32) &&
1350 rdev->mddev->level >= 1)
1351 num_sectors = (sector_t)(2ULL << 32) - 2;
1353 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1355 } while (md_super_wait(rdev->mddev) < 0);
1360 super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1362 /* non-zero offset changes not possible with v0.90 */
1363 return new_offset == 0;
1367 * version 1 superblock
1370 static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
1374 unsigned long long newcsum;
1375 int size = 256 + le32_to_cpu(sb->max_dev)*2;
1376 __le32 *isuper = (__le32*)sb;
1378 disk_csum = sb->sb_csum;
1381 for (; size >= 4; size -= 4)
1382 newcsum += le32_to_cpu(*isuper++);
1385 newcsum += le16_to_cpu(*(__le16*) isuper);
1387 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1388 sb->sb_csum = disk_csum;
1389 return cpu_to_le32(csum);
1392 static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
1394 struct mdp_superblock_1 *sb;
1398 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1402 * Calculate the position of the superblock in 512byte sectors.
1403 * It is always aligned to a 4K boundary and
1404 * depeding on minor_version, it can be:
1405 * 0: At least 8K, but less than 12K, from end of device
1406 * 1: At start of device
1407 * 2: 4K from start of device.
1409 switch(minor_version) {
1411 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
1413 sb_start &= ~(sector_t)(4*2-1);
1424 rdev->sb_start = sb_start;
1426 /* superblock is rarely larger than 1K, but it can be larger,
1427 * and it is safe to read 4k, so we do that
1429 ret = read_disk_sb(rdev, 4096);
1430 if (ret) return ret;
1432 sb = page_address(rdev->sb_page);
1434 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1435 sb->major_version != cpu_to_le32(1) ||
1436 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1437 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1438 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1441 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1442 pr_warn("md: invalid superblock checksum on %s\n",
1443 bdevname(rdev->bdev,b));
1446 if (le64_to_cpu(sb->data_size) < 10) {
1447 pr_warn("md: data_size too small on %s\n",
1448 bdevname(rdev->bdev,b));
1453 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1454 /* Some padding is non-zero, might be a new feature */
1457 rdev->preferred_minor = 0xffff;
1458 rdev->data_offset = le64_to_cpu(sb->data_offset);
1459 rdev->new_data_offset = rdev->data_offset;
1460 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1461 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1462 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
1463 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1465 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1466 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1467 if (rdev->sb_size & bmask)
1468 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1471 && rdev->data_offset < sb_start + (rdev->sb_size/512))
1474 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1477 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1480 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1482 if (!rdev->bb_page) {
1483 rdev->bb_page = alloc_page(GFP_KERNEL);
1487 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1488 rdev->badblocks.count == 0) {
1489 /* need to load the bad block list.
1490 * Currently we limit it to one page.
1496 int sectors = le16_to_cpu(sb->bblog_size);
1497 if (sectors > (PAGE_SIZE / 512))
1499 offset = le32_to_cpu(sb->bblog_offset);
1502 bb_sector = (long long)offset;
1503 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1504 rdev->bb_page, REQ_OP_READ, 0, true))
1506 bbp = (u64 *)page_address(rdev->bb_page);
1507 rdev->badblocks.shift = sb->bblog_shift;
1508 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1509 u64 bb = le64_to_cpu(*bbp);
1510 int count = bb & (0x3ff);
1511 u64 sector = bb >> 10;
1512 sector <<= sb->bblog_shift;
1513 count <<= sb->bblog_shift;
1516 if (badblocks_set(&rdev->badblocks, sector, count, 1))
1519 } else if (sb->bblog_offset != 0)
1520 rdev->badblocks.shift = 0;
1522 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) {
1523 rdev->ppl.offset = (__s16)le16_to_cpu(sb->ppl.offset);
1524 rdev->ppl.size = le16_to_cpu(sb->ppl.size);
1525 rdev->ppl.sector = rdev->sb_start + rdev->ppl.offset;
1532 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
1534 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1535 sb->level != refsb->level ||
1536 sb->layout != refsb->layout ||
1537 sb->chunksize != refsb->chunksize) {
1538 pr_warn("md: %s has strangely different superblock to %s\n",
1539 bdevname(rdev->bdev,b),
1540 bdevname(refdev->bdev,b2));
1543 ev1 = le64_to_cpu(sb->events);
1544 ev2 = le64_to_cpu(refsb->events);
1551 if (minor_version) {
1552 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1553 sectors -= rdev->data_offset;
1555 sectors = rdev->sb_start;
1556 if (sectors < le64_to_cpu(sb->data_size))
1558 rdev->sectors = le64_to_cpu(sb->data_size);
1562 static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
1564 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
1565 __u64 ev1 = le64_to_cpu(sb->events);
1567 rdev->raid_disk = -1;
1568 clear_bit(Faulty, &rdev->flags);
1569 clear_bit(In_sync, &rdev->flags);
1570 clear_bit(Bitmap_sync, &rdev->flags);
1571 clear_bit(WriteMostly, &rdev->flags);
1573 if (mddev->raid_disks == 0) {
1574 mddev->major_version = 1;
1575 mddev->patch_version = 0;
1576 mddev->external = 0;
1577 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
1578 mddev->ctime = le64_to_cpu(sb->ctime);
1579 mddev->utime = le64_to_cpu(sb->utime);
1580 mddev->level = le32_to_cpu(sb->level);
1581 mddev->clevel[0] = 0;
1582 mddev->layout = le32_to_cpu(sb->layout);
1583 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1584 mddev->dev_sectors = le64_to_cpu(sb->size);
1585 mddev->events = ev1;
1586 mddev->bitmap_info.offset = 0;
1587 mddev->bitmap_info.space = 0;
1588 /* Default location for bitmap is 1K after superblock
1589 * using 3K - total of 4K
1591 mddev->bitmap_info.default_offset = 1024 >> 9;
1592 mddev->bitmap_info.default_space = (4096-1024) >> 9;
1593 mddev->reshape_backwards = 0;
1595 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1596 memcpy(mddev->uuid, sb->set_uuid, 16);
1598 mddev->max_disks = (4096-256)/2;
1600 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1601 mddev->bitmap_info.file == NULL) {
1602 mddev->bitmap_info.offset =
1603 (__s32)le32_to_cpu(sb->bitmap_offset);
1604 /* Metadata doesn't record how much space is available.
1605 * For 1.0, we assume we can use up to the superblock
1606 * if before, else to 4K beyond superblock.
1607 * For others, assume no change is possible.
1609 if (mddev->minor_version > 0)
1610 mddev->bitmap_info.space = 0;
1611 else if (mddev->bitmap_info.offset > 0)
1612 mddev->bitmap_info.space =
1613 8 - mddev->bitmap_info.offset;
1615 mddev->bitmap_info.space =
1616 -mddev->bitmap_info.offset;
1619 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1620 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1621 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1622 mddev->new_level = le32_to_cpu(sb->new_level);
1623 mddev->new_layout = le32_to_cpu(sb->new_layout);
1624 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
1625 if (mddev->delta_disks < 0 ||
1626 (mddev->delta_disks == 0 &&
1627 (le32_to_cpu(sb->feature_map)
1628 & MD_FEATURE_RESHAPE_BACKWARDS)))
1629 mddev->reshape_backwards = 1;
1631 mddev->reshape_position = MaxSector;
1632 mddev->delta_disks = 0;
1633 mddev->new_level = mddev->level;
1634 mddev->new_layout = mddev->layout;
1635 mddev->new_chunk_sectors = mddev->chunk_sectors;
1638 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
1639 set_bit(MD_HAS_JOURNAL, &mddev->flags);
1641 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) {
1642 if (le32_to_cpu(sb->feature_map) &
1643 (MD_FEATURE_BITMAP_OFFSET | MD_FEATURE_JOURNAL))
1645 set_bit(MD_HAS_PPL, &mddev->flags);
1647 } else if (mddev->pers == NULL) {
1648 /* Insist of good event counter while assembling, except for
1649 * spares (which don't need an event count) */
1651 if (rdev->desc_nr >= 0 &&
1652 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1653 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1654 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
1655 if (ev1 < mddev->events)
1657 } else if (mddev->bitmap) {
1658 /* If adding to array with a bitmap, then we can accept an
1659 * older device, but not too old.
1661 if (ev1 < mddev->bitmap->events_cleared)
1663 if (ev1 < mddev->events)
1664 set_bit(Bitmap_sync, &rdev->flags);
1666 if (ev1 < mddev->events)
1667 /* just a hot-add of a new device, leave raid_disk at -1 */
1670 if (mddev->level != LEVEL_MULTIPATH) {
1672 if (rdev->desc_nr < 0 ||
1673 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1674 role = MD_DISK_ROLE_SPARE;
1677 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1679 case MD_DISK_ROLE_SPARE: /* spare */
1681 case MD_DISK_ROLE_FAULTY: /* faulty */
1682 set_bit(Faulty, &rdev->flags);
1684 case MD_DISK_ROLE_JOURNAL: /* journal device */
1685 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1686 /* journal device without journal feature */
1687 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
1690 set_bit(Journal, &rdev->flags);
1691 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
1692 rdev->raid_disk = 0;
1695 rdev->saved_raid_disk = role;
1696 if ((le32_to_cpu(sb->feature_map) &
1697 MD_FEATURE_RECOVERY_OFFSET)) {
1698 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1699 if (!(le32_to_cpu(sb->feature_map) &
1700 MD_FEATURE_RECOVERY_BITMAP))
1701 rdev->saved_raid_disk = -1;
1703 set_bit(In_sync, &rdev->flags);
1704 rdev->raid_disk = role;
1707 if (sb->devflags & WriteMostly1)
1708 set_bit(WriteMostly, &rdev->flags);
1709 if (sb->devflags & FailFast1)
1710 set_bit(FailFast, &rdev->flags);
1711 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1712 set_bit(Replacement, &rdev->flags);
1713 } else /* MULTIPATH are always insync */
1714 set_bit(In_sync, &rdev->flags);
1719 static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
1721 struct mdp_superblock_1 *sb;
1722 struct md_rdev *rdev2;
1724 /* make rdev->sb match mddev and rdev data. */
1726 sb = page_address(rdev->sb_page);
1728 sb->feature_map = 0;
1730 sb->recovery_offset = cpu_to_le64(0);
1731 memset(sb->pad3, 0, sizeof(sb->pad3));
1733 sb->utime = cpu_to_le64((__u64)mddev->utime);
1734 sb->events = cpu_to_le64(mddev->events);
1736 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1737 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
1738 sb->resync_offset = cpu_to_le64(MaxSector);
1740 sb->resync_offset = cpu_to_le64(0);
1742 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1744 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1745 sb->size = cpu_to_le64(mddev->dev_sectors);
1746 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
1747 sb->level = cpu_to_le32(mddev->level);
1748 sb->layout = cpu_to_le32(mddev->layout);
1749 if (test_bit(FailFast, &rdev->flags))
1750 sb->devflags |= FailFast1;
1752 sb->devflags &= ~FailFast1;
1754 if (test_bit(WriteMostly, &rdev->flags))
1755 sb->devflags |= WriteMostly1;
1757 sb->devflags &= ~WriteMostly1;
1758 sb->data_offset = cpu_to_le64(rdev->data_offset);
1759 sb->data_size = cpu_to_le64(rdev->sectors);
1761 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1762 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
1763 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1766 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
1767 !test_bit(In_sync, &rdev->flags)) {
1769 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1770 sb->recovery_offset =
1771 cpu_to_le64(rdev->recovery_offset);
1772 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1774 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
1776 /* Note: recovery_offset and journal_tail share space */
1777 if (test_bit(Journal, &rdev->flags))
1778 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
1779 if (test_bit(Replacement, &rdev->flags))
1781 cpu_to_le32(MD_FEATURE_REPLACEMENT);
1783 if (mddev->reshape_position != MaxSector) {
1784 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1785 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1786 sb->new_layout = cpu_to_le32(mddev->new_layout);
1787 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1788 sb->new_level = cpu_to_le32(mddev->new_level);
1789 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
1790 if (mddev->delta_disks == 0 &&
1791 mddev->reshape_backwards)
1793 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
1794 if (rdev->new_data_offset != rdev->data_offset) {
1796 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1797 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1798 - rdev->data_offset));
1802 if (mddev_is_clustered(mddev))
1803 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
1805 if (rdev->badblocks.count == 0)
1806 /* Nothing to do for bad blocks*/ ;
1807 else if (sb->bblog_offset == 0)
1808 /* Cannot record bad blocks on this device */
1809 md_error(mddev, rdev);
1811 struct badblocks *bb = &rdev->badblocks;
1812 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1814 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1819 seq = read_seqbegin(&bb->lock);
1821 memset(bbp, 0xff, PAGE_SIZE);
1823 for (i = 0 ; i < bb->count ; i++) {
1824 u64 internal_bb = p[i];
1825 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1826 | BB_LEN(internal_bb));
1827 bbp[i] = cpu_to_le64(store_bb);
1830 if (read_seqretry(&bb->lock, seq))
1833 bb->sector = (rdev->sb_start +
1834 (int)le32_to_cpu(sb->bblog_offset));
1835 bb->size = le16_to_cpu(sb->bblog_size);
1840 rdev_for_each(rdev2, mddev)
1841 if (rdev2->desc_nr+1 > max_dev)
1842 max_dev = rdev2->desc_nr+1;
1844 if (max_dev > le32_to_cpu(sb->max_dev)) {
1846 sb->max_dev = cpu_to_le32(max_dev);
1847 rdev->sb_size = max_dev * 2 + 256;
1848 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1849 if (rdev->sb_size & bmask)
1850 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1852 max_dev = le32_to_cpu(sb->max_dev);
1854 for (i=0; i<max_dev;i++)
1855 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
1857 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
1858 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
1860 if (test_bit(MD_HAS_PPL, &mddev->flags)) {
1861 sb->feature_map |= cpu_to_le32(MD_FEATURE_PPL);
1862 sb->ppl.offset = cpu_to_le16(rdev->ppl.offset);
1863 sb->ppl.size = cpu_to_le16(rdev->ppl.size);
1866 rdev_for_each(rdev2, mddev) {
1868 if (test_bit(Faulty, &rdev2->flags))
1869 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
1870 else if (test_bit(In_sync, &rdev2->flags))
1871 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1872 else if (test_bit(Journal, &rdev2->flags))
1873 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
1874 else if (rdev2->raid_disk >= 0)
1875 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1877 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
1880 sb->sb_csum = calc_sb_1_csum(sb);
1883 static unsigned long long
1884 super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
1886 struct mdp_superblock_1 *sb;
1887 sector_t max_sectors;
1888 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1889 return 0; /* component must fit device */
1890 if (rdev->data_offset != rdev->new_data_offset)
1891 return 0; /* too confusing */
1892 if (rdev->sb_start < rdev->data_offset) {
1893 /* minor versions 1 and 2; superblock before data */
1894 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
1895 max_sectors -= rdev->data_offset;
1896 if (!num_sectors || num_sectors > max_sectors)
1897 num_sectors = max_sectors;
1898 } else if (rdev->mddev->bitmap_info.offset) {
1899 /* minor version 0 with bitmap we can't move */
1902 /* minor version 0; superblock after data */
1904 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
1905 sb_start &= ~(sector_t)(4*2 - 1);
1906 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
1907 if (!num_sectors || num_sectors > max_sectors)
1908 num_sectors = max_sectors;
1909 rdev->sb_start = sb_start;
1911 sb = page_address(rdev->sb_page);
1912 sb->data_size = cpu_to_le64(num_sectors);
1913 sb->super_offset = cpu_to_le64(rdev->sb_start);
1914 sb->sb_csum = calc_sb_1_csum(sb);
1916 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1918 } while (md_super_wait(rdev->mddev) < 0);
1924 super_1_allow_new_offset(struct md_rdev *rdev,
1925 unsigned long long new_offset)
1927 /* All necessary checks on new >= old have been done */
1928 struct bitmap *bitmap;
1929 if (new_offset >= rdev->data_offset)
1932 /* with 1.0 metadata, there is no metadata to tread on
1933 * so we can always move back */
1934 if (rdev->mddev->minor_version == 0)
1937 /* otherwise we must be sure not to step on
1938 * any metadata, so stay:
1939 * 36K beyond start of superblock
1940 * beyond end of badblocks
1941 * beyond write-intent bitmap
1943 if (rdev->sb_start + (32+4)*2 > new_offset)
1945 bitmap = rdev->mddev->bitmap;
1946 if (bitmap && !rdev->mddev->bitmap_info.file &&
1947 rdev->sb_start + rdev->mddev->bitmap_info.offset +
1948 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
1950 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1956 static struct super_type super_types[] = {
1959 .owner = THIS_MODULE,
1960 .load_super = super_90_load,
1961 .validate_super = super_90_validate,
1962 .sync_super = super_90_sync,
1963 .rdev_size_change = super_90_rdev_size_change,
1964 .allow_new_offset = super_90_allow_new_offset,
1968 .owner = THIS_MODULE,
1969 .load_super = super_1_load,
1970 .validate_super = super_1_validate,
1971 .sync_super = super_1_sync,
1972 .rdev_size_change = super_1_rdev_size_change,
1973 .allow_new_offset = super_1_allow_new_offset,
1977 static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
1979 if (mddev->sync_super) {
1980 mddev->sync_super(mddev, rdev);
1984 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1986 super_types[mddev->major_version].sync_super(mddev, rdev);
1989 static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
1991 struct md_rdev *rdev, *rdev2;
1994 rdev_for_each_rcu(rdev, mddev1) {
1995 if (test_bit(Faulty, &rdev->flags) ||
1996 test_bit(Journal, &rdev->flags) ||
1997 rdev->raid_disk == -1)
1999 rdev_for_each_rcu(rdev2, mddev2) {
2000 if (test_bit(Faulty, &rdev2->flags) ||
2001 test_bit(Journal, &rdev2->flags) ||
2002 rdev2->raid_disk == -1)
2004 if (rdev->bdev->bd_contains ==
2005 rdev2->bdev->bd_contains) {
2015 static LIST_HEAD(pending_raid_disks);
2018 * Try to register data integrity profile for an mddev
2020 * This is called when an array is started and after a disk has been kicked
2021 * from the array. It only succeeds if all working and active component devices
2022 * are integrity capable with matching profiles.
2024 int md_integrity_register(struct mddev *mddev)
2026 struct md_rdev *rdev, *reference = NULL;
2028 if (list_empty(&mddev->disks))
2029 return 0; /* nothing to do */
2030 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2031 return 0; /* shouldn't register, or already is */
2032 rdev_for_each(rdev, mddev) {
2033 /* skip spares and non-functional disks */
2034 if (test_bit(Faulty, &rdev->flags))
2036 if (rdev->raid_disk < 0)
2039 /* Use the first rdev as the reference */
2043 /* does this rdev's profile match the reference profile? */
2044 if (blk_integrity_compare(reference->bdev->bd_disk,
2045 rdev->bdev->bd_disk) < 0)
2048 if (!reference || !bdev_get_integrity(reference->bdev))
2051 * All component devices are integrity capable and have matching
2052 * profiles, register the common profile for the md device.
2054 blk_integrity_register(mddev->gendisk,
2055 bdev_get_integrity(reference->bdev));
2057 pr_debug("md: data integrity enabled on %s\n", mdname(mddev));
2058 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
2059 pr_err("md: failed to create integrity pool for %s\n",
2065 EXPORT_SYMBOL(md_integrity_register);
2068 * Attempt to add an rdev, but only if it is consistent with the current
2071 int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
2073 struct blk_integrity *bi_rdev;
2074 struct blk_integrity *bi_mddev;
2075 char name[BDEVNAME_SIZE];
2077 if (!mddev->gendisk)
2080 bi_rdev = bdev_get_integrity(rdev->bdev);
2081 bi_mddev = blk_get_integrity(mddev->gendisk);
2083 if (!bi_mddev) /* nothing to do */
2086 if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
2087 pr_err("%s: incompatible integrity profile for %s\n",
2088 mdname(mddev), bdevname(rdev->bdev, name));
2094 EXPORT_SYMBOL(md_integrity_add_rdev);
2096 static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
2098 char b[BDEVNAME_SIZE];
2102 /* prevent duplicates */
2103 if (find_rdev(mddev, rdev->bdev->bd_dev))
2106 if ((bdev_read_only(rdev->bdev) || bdev_read_only(rdev->meta_bdev)) &&
2110 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2111 if (!test_bit(Journal, &rdev->flags) &&
2113 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
2115 /* Cannot change size, so fail
2116 * If mddev->level <= 0, then we don't care
2117 * about aligning sizes (e.g. linear)
2119 if (mddev->level > 0)
2122 mddev->dev_sectors = rdev->sectors;
2125 /* Verify rdev->desc_nr is unique.
2126 * If it is -1, assign a free number, else
2127 * check number is not in use
2130 if (rdev->desc_nr < 0) {
2133 choice = mddev->raid_disks;
2134 while (md_find_rdev_nr_rcu(mddev, choice))
2136 rdev->desc_nr = choice;
2138 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
2144 if (!test_bit(Journal, &rdev->flags) &&
2145 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2146 pr_warn("md: %s: array is limited to %d devices\n",
2147 mdname(mddev), mddev->max_disks);
2150 bdevname(rdev->bdev,b);
2151 strreplace(b, '/', '!');
2153 rdev->mddev = mddev;
2154 pr_debug("md: bind<%s>\n", b);
2156 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
2159 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
2160 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2161 /* failure here is OK */;
2162 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
2164 list_add_rcu(&rdev->same_set, &mddev->disks);
2165 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
2167 /* May as well allow recovery to be retried once */
2168 mddev->recovery_disabled++;
2173 pr_warn("md: failed to register dev-%s for %s\n",
2178 static void md_delayed_delete(struct work_struct *ws)
2180 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
2181 kobject_del(&rdev->kobj);
2182 kobject_put(&rdev->kobj);
2185 static void unbind_rdev_from_array(struct md_rdev *rdev)
2187 char b[BDEVNAME_SIZE];
2189 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
2190 list_del_rcu(&rdev->same_set);
2191 pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b));
2193 sysfs_remove_link(&rdev->kobj, "block");
2194 sysfs_put(rdev->sysfs_state);
2195 rdev->sysfs_state = NULL;
2196 rdev->badblocks.count = 0;
2197 /* We need to delay this, otherwise we can deadlock when
2198 * writing to 'remove' to "dev/state". We also need
2199 * to delay it due to rcu usage.
2202 INIT_WORK(&rdev->del_work, md_delayed_delete);
2203 kobject_get(&rdev->kobj);
2204 queue_work(md_misc_wq, &rdev->del_work);
2208 * prevent the device from being mounted, repartitioned or
2209 * otherwise reused by a RAID array (or any other kernel
2210 * subsystem), by bd_claiming the device.
2212 static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
2215 struct block_device *bdev;
2216 char b[BDEVNAME_SIZE];
2218 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
2219 shared ? (struct md_rdev *)lock_rdev : rdev);
2221 pr_warn("md: could not open %s.\n", __bdevname(dev, b));
2222 return PTR_ERR(bdev);
2228 static void unlock_rdev(struct md_rdev *rdev)
2230 struct block_device *bdev = rdev->bdev;
2232 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
2235 void md_autodetect_dev(dev_t dev);
2237 static void export_rdev(struct md_rdev *rdev)
2239 char b[BDEVNAME_SIZE];
2241 pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b));
2242 md_rdev_clear(rdev);
2244 if (test_bit(AutoDetected, &rdev->flags))
2245 md_autodetect_dev(rdev->bdev->bd_dev);
2248 kobject_put(&rdev->kobj);
2251 void md_kick_rdev_from_array(struct md_rdev *rdev)
2253 unbind_rdev_from_array(rdev);
2256 EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
2258 static void export_array(struct mddev *mddev)
2260 struct md_rdev *rdev;
2262 while (!list_empty(&mddev->disks)) {
2263 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2265 md_kick_rdev_from_array(rdev);
2267 mddev->raid_disks = 0;
2268 mddev->major_version = 0;
2271 static bool set_in_sync(struct mddev *mddev)
2273 WARN_ON_ONCE(!spin_is_locked(&mddev->lock));
2274 if (!mddev->in_sync) {
2275 mddev->sync_checkers++;
2276 spin_unlock(&mddev->lock);
2277 percpu_ref_switch_to_atomic_sync(&mddev->writes_pending);
2278 spin_lock(&mddev->lock);
2279 if (!mddev->in_sync &&
2280 percpu_ref_is_zero(&mddev->writes_pending)) {
2283 * Ensure ->in_sync is visible before we clear
2287 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2288 sysfs_notify_dirent_safe(mddev->sysfs_state);
2290 if (--mddev->sync_checkers == 0)
2291 percpu_ref_switch_to_percpu(&mddev->writes_pending);
2293 if (mddev->safemode == 1)
2294 mddev->safemode = 0;
2295 return mddev->in_sync;
2298 static void sync_sbs(struct mddev *mddev, int nospares)
2300 /* Update each superblock (in-memory image), but
2301 * if we are allowed to, skip spares which already
2302 * have the right event counter, or have one earlier
2303 * (which would mean they aren't being marked as dirty
2304 * with the rest of the array)
2306 struct md_rdev *rdev;
2307 rdev_for_each(rdev, mddev) {
2308 if (rdev->sb_events == mddev->events ||
2310 rdev->raid_disk < 0 &&
2311 rdev->sb_events+1 == mddev->events)) {
2312 /* Don't update this superblock */
2313 rdev->sb_loaded = 2;
2315 sync_super(mddev, rdev);
2316 rdev->sb_loaded = 1;
2321 static bool does_sb_need_changing(struct mddev *mddev)
2323 struct md_rdev *rdev;
2324 struct mdp_superblock_1 *sb;
2327 /* Find a good rdev */
2328 rdev_for_each(rdev, mddev)
2329 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2332 /* No good device found. */
2336 sb = page_address(rdev->sb_page);
2337 /* Check if a device has become faulty or a spare become active */
2338 rdev_for_each(rdev, mddev) {
2339 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2340 /* Device activated? */
2341 if (role == 0xffff && rdev->raid_disk >=0 &&
2342 !test_bit(Faulty, &rdev->flags))
2344 /* Device turned faulty? */
2345 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2349 /* Check if any mddev parameters have changed */
2350 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2351 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
2352 (mddev->layout != le32_to_cpu(sb->layout)) ||
2353 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2354 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2360 void md_update_sb(struct mddev *mddev, int force_change)
2362 struct md_rdev *rdev;
2365 int any_badblocks_changed = 0;
2370 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2375 if (mddev_is_clustered(mddev)) {
2376 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
2378 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
2380 ret = md_cluster_ops->metadata_update_start(mddev);
2381 /* Has someone else has updated the sb */
2382 if (!does_sb_need_changing(mddev)) {
2384 md_cluster_ops->metadata_update_cancel(mddev);
2385 bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2386 BIT(MD_SB_CHANGE_DEVS) |
2387 BIT(MD_SB_CHANGE_CLEAN));
2392 /* First make sure individual recovery_offsets are correct */
2393 rdev_for_each(rdev, mddev) {
2394 if (rdev->raid_disk >= 0 &&
2395 mddev->delta_disks >= 0 &&
2396 !test_bit(Journal, &rdev->flags) &&
2397 !test_bit(In_sync, &rdev->flags) &&
2398 mddev->curr_resync_completed > rdev->recovery_offset)
2399 rdev->recovery_offset = mddev->curr_resync_completed;
2402 if (!mddev->persistent) {
2403 clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2404 clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2405 if (!mddev->external) {
2406 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
2407 rdev_for_each(rdev, mddev) {
2408 if (rdev->badblocks.changed) {
2409 rdev->badblocks.changed = 0;
2410 ack_all_badblocks(&rdev->badblocks);
2411 md_error(mddev, rdev);
2413 clear_bit(Blocked, &rdev->flags);
2414 clear_bit(BlockedBadBlocks, &rdev->flags);
2415 wake_up(&rdev->blocked_wait);
2418 wake_up(&mddev->sb_wait);
2422 spin_lock(&mddev->lock);
2424 mddev->utime = ktime_get_real_seconds();
2426 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
2428 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
2429 /* just a clean<-> dirty transition, possibly leave spares alone,
2430 * though if events isn't the right even/odd, we will have to do
2436 if (mddev->degraded)
2437 /* If the array is degraded, then skipping spares is both
2438 * dangerous and fairly pointless.
2439 * Dangerous because a device that was removed from the array
2440 * might have a event_count that still looks up-to-date,
2441 * so it can be re-added without a resync.
2442 * Pointless because if there are any spares to skip,
2443 * then a recovery will happen and soon that array won't
2444 * be degraded any more and the spare can go back to sleep then.
2448 sync_req = mddev->in_sync;
2450 /* If this is just a dirty<->clean transition, and the array is clean
2451 * and 'events' is odd, we can roll back to the previous clean state */
2453 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
2454 && mddev->can_decrease_events
2455 && mddev->events != 1) {
2457 mddev->can_decrease_events = 0;
2459 /* otherwise we have to go forward and ... */
2461 mddev->can_decrease_events = nospares;
2465 * This 64-bit counter should never wrap.
2466 * Either we are in around ~1 trillion A.C., assuming
2467 * 1 reboot per second, or we have a bug...
2469 WARN_ON(mddev->events == 0);
2471 rdev_for_each(rdev, mddev) {
2472 if (rdev->badblocks.changed)
2473 any_badblocks_changed++;
2474 if (test_bit(Faulty, &rdev->flags))
2475 set_bit(FaultRecorded, &rdev->flags);
2478 sync_sbs(mddev, nospares);
2479 spin_unlock(&mddev->lock);
2481 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2482 mdname(mddev), mddev->in_sync);
2485 blk_add_trace_msg(mddev->queue, "md md_update_sb");
2487 bitmap_update_sb(mddev->bitmap);
2488 rdev_for_each(rdev, mddev) {
2489 char b[BDEVNAME_SIZE];
2491 if (rdev->sb_loaded != 1)
2492 continue; /* no noise on spare devices */
2494 if (!test_bit(Faulty, &rdev->flags)) {
2495 md_super_write(mddev,rdev,
2496 rdev->sb_start, rdev->sb_size,
2498 pr_debug("md: (write) %s's sb offset: %llu\n",
2499 bdevname(rdev->bdev, b),
2500 (unsigned long long)rdev->sb_start);
2501 rdev->sb_events = mddev->events;
2502 if (rdev->badblocks.size) {
2503 md_super_write(mddev, rdev,
2504 rdev->badblocks.sector,
2505 rdev->badblocks.size << 9,
2507 rdev->badblocks.size = 0;
2511 pr_debug("md: %s (skipping faulty)\n",
2512 bdevname(rdev->bdev, b));
2514 if (mddev->level == LEVEL_MULTIPATH)
2515 /* only need to write one superblock... */
2518 if (md_super_wait(mddev) < 0)
2520 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
2522 if (mddev_is_clustered(mddev) && ret == 0)
2523 md_cluster_ops->metadata_update_finish(mddev);
2525 if (mddev->in_sync != sync_req ||
2526 !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2527 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN)))
2528 /* have to write it out again */
2530 wake_up(&mddev->sb_wait);
2531 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2532 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
2534 rdev_for_each(rdev, mddev) {
2535 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2536 clear_bit(Blocked, &rdev->flags);
2538 if (any_badblocks_changed)
2539 ack_all_badblocks(&rdev->badblocks);
2540 clear_bit(BlockedBadBlocks, &rdev->flags);
2541 wake_up(&rdev->blocked_wait);
2544 EXPORT_SYMBOL(md_update_sb);
2546 static int add_bound_rdev(struct md_rdev *rdev)
2548 struct mddev *mddev = rdev->mddev;
2550 bool add_journal = test_bit(Journal, &rdev->flags);
2552 if (!mddev->pers->hot_remove_disk || add_journal) {
2553 /* If there is hot_add_disk but no hot_remove_disk
2554 * then added disks for geometry changes,
2555 * and should be added immediately.
2557 super_types[mddev->major_version].
2558 validate_super(mddev, rdev);
2560 mddev_suspend(mddev);
2561 err = mddev->pers->hot_add_disk(mddev, rdev);
2563 mddev_resume(mddev);
2565 md_kick_rdev_from_array(rdev);
2569 sysfs_notify_dirent_safe(rdev->sysfs_state);
2571 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2572 if (mddev->degraded)
2573 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2574 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2575 md_new_event(mddev);
2576 md_wakeup_thread(mddev->thread);
2580 /* words written to sysfs files may, or may not, be \n terminated.
2581 * We want to accept with case. For this we use cmd_match.
2583 static int cmd_match(const char *cmd, const char *str)
2585 /* See if cmd, written into a sysfs file, matches
2586 * str. They must either be the same, or cmd can
2587 * have a trailing newline
2589 while (*cmd && *str && *cmd == *str) {
2600 struct rdev_sysfs_entry {
2601 struct attribute attr;
2602 ssize_t (*show)(struct md_rdev *, char *);
2603 ssize_t (*store)(struct md_rdev *, const char *, size_t);
2607 state_show(struct md_rdev *rdev, char *page)
2611 unsigned long flags = ACCESS_ONCE(rdev->flags);
2613 if (test_bit(Faulty, &flags) ||
2614 (!test_bit(ExternalBbl, &flags) &&
2615 rdev->badblocks.unacked_exist))
2616 len += sprintf(page+len, "faulty%s", sep);
2617 if (test_bit(In_sync, &flags))
2618 len += sprintf(page+len, "in_sync%s", sep);
2619 if (test_bit(Journal, &flags))
2620 len += sprintf(page+len, "journal%s", sep);
2621 if (test_bit(WriteMostly, &flags))
2622 len += sprintf(page+len, "write_mostly%s", sep);
2623 if (test_bit(Blocked, &flags) ||
2624 (rdev->badblocks.unacked_exist
2625 && !test_bit(Faulty, &flags)))
2626 len += sprintf(page+len, "blocked%s", sep);
2627 if (!test_bit(Faulty, &flags) &&
2628 !test_bit(Journal, &flags) &&
2629 !test_bit(In_sync, &flags))
2630 len += sprintf(page+len, "spare%s", sep);
2631 if (test_bit(WriteErrorSeen, &flags))
2632 len += sprintf(page+len, "write_error%s", sep);
2633 if (test_bit(WantReplacement, &flags))
2634 len += sprintf(page+len, "want_replacement%s", sep);
2635 if (test_bit(Replacement, &flags))
2636 len += sprintf(page+len, "replacement%s", sep);
2637 if (test_bit(ExternalBbl, &flags))
2638 len += sprintf(page+len, "external_bbl%s", sep);
2639 if (test_bit(FailFast, &flags))
2640 len += sprintf(page+len, "failfast%s", sep);
2645 return len+sprintf(page+len, "\n");
2649 state_store(struct md_rdev *rdev, const char *buf, size_t len)
2652 * faulty - simulates an error
2653 * remove - disconnects the device
2654 * writemostly - sets write_mostly
2655 * -writemostly - clears write_mostly
2656 * blocked - sets the Blocked flags
2657 * -blocked - clears the Blocked and possibly simulates an error
2658 * insync - sets Insync providing device isn't active
2659 * -insync - clear Insync for a device with a slot assigned,
2660 * so that it gets rebuilt based on bitmap
2661 * write_error - sets WriteErrorSeen
2662 * -write_error - clears WriteErrorSeen
2663 * {,-}failfast - set/clear FailFast
2666 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2667 md_error(rdev->mddev, rdev);
2668 if (test_bit(Faulty, &rdev->flags))
2672 } else if (cmd_match(buf, "remove")) {
2673 if (rdev->mddev->pers) {
2674 clear_bit(Blocked, &rdev->flags);
2675 remove_and_add_spares(rdev->mddev, rdev);
2677 if (rdev->raid_disk >= 0)
2680 struct mddev *mddev = rdev->mddev;
2682 if (mddev_is_clustered(mddev))
2683 err = md_cluster_ops->remove_disk(mddev, rdev);
2686 md_kick_rdev_from_array(rdev);
2688 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2689 md_wakeup_thread(mddev->thread);
2691 md_new_event(mddev);
2694 } else if (cmd_match(buf, "writemostly")) {
2695 set_bit(WriteMostly, &rdev->flags);
2697 } else if (cmd_match(buf, "-writemostly")) {
2698 clear_bit(WriteMostly, &rdev->flags);
2700 } else if (cmd_match(buf, "blocked")) {
2701 set_bit(Blocked, &rdev->flags);
2703 } else if (cmd_match(buf, "-blocked")) {
2704 if (!test_bit(Faulty, &rdev->flags) &&
2705 !test_bit(ExternalBbl, &rdev->flags) &&
2706 rdev->badblocks.unacked_exist) {
2707 /* metadata handler doesn't understand badblocks,
2708 * so we need to fail the device
2710 md_error(rdev->mddev, rdev);
2712 clear_bit(Blocked, &rdev->flags);
2713 clear_bit(BlockedBadBlocks, &rdev->flags);
2714 wake_up(&rdev->blocked_wait);
2715 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2716 md_wakeup_thread(rdev->mddev->thread);
2719 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2720 set_bit(In_sync, &rdev->flags);
2722 } else if (cmd_match(buf, "failfast")) {
2723 set_bit(FailFast, &rdev->flags);
2725 } else if (cmd_match(buf, "-failfast")) {
2726 clear_bit(FailFast, &rdev->flags);
2728 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
2729 !test_bit(Journal, &rdev->flags)) {
2730 if (rdev->mddev->pers == NULL) {
2731 clear_bit(In_sync, &rdev->flags);
2732 rdev->saved_raid_disk = rdev->raid_disk;
2733 rdev->raid_disk = -1;
2736 } else if (cmd_match(buf, "write_error")) {
2737 set_bit(WriteErrorSeen, &rdev->flags);
2739 } else if (cmd_match(buf, "-write_error")) {
2740 clear_bit(WriteErrorSeen, &rdev->flags);
2742 } else if (cmd_match(buf, "want_replacement")) {
2743 /* Any non-spare device that is not a replacement can
2744 * become want_replacement at any time, but we then need to
2745 * check if recovery is needed.
2747 if (rdev->raid_disk >= 0 &&
2748 !test_bit(Journal, &rdev->flags) &&
2749 !test_bit(Replacement, &rdev->flags))
2750 set_bit(WantReplacement, &rdev->flags);
2751 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2752 md_wakeup_thread(rdev->mddev->thread);
2754 } else if (cmd_match(buf, "-want_replacement")) {
2755 /* Clearing 'want_replacement' is always allowed.
2756 * Once replacements starts it is too late though.
2759 clear_bit(WantReplacement, &rdev->flags);
2760 } else if (cmd_match(buf, "replacement")) {
2761 /* Can only set a device as a replacement when array has not
2762 * yet been started. Once running, replacement is automatic
2763 * from spares, or by assigning 'slot'.
2765 if (rdev->mddev->pers)
2768 set_bit(Replacement, &rdev->flags);
2771 } else if (cmd_match(buf, "-replacement")) {
2772 /* Similarly, can only clear Replacement before start */
2773 if (rdev->mddev->pers)
2776 clear_bit(Replacement, &rdev->flags);
2779 } else if (cmd_match(buf, "re-add")) {
2780 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) {
2781 /* clear_bit is performed _after_ all the devices
2782 * have their local Faulty bit cleared. If any writes
2783 * happen in the meantime in the local node, they
2784 * will land in the local bitmap, which will be synced
2785 * by this node eventually
2787 if (!mddev_is_clustered(rdev->mddev) ||
2788 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2789 clear_bit(Faulty, &rdev->flags);
2790 err = add_bound_rdev(rdev);
2794 } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) {
2795 set_bit(ExternalBbl, &rdev->flags);
2796 rdev->badblocks.shift = 0;
2798 } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) {
2799 clear_bit(ExternalBbl, &rdev->flags);
2803 sysfs_notify_dirent_safe(rdev->sysfs_state);
2804 return err ? err : len;
2806 static struct rdev_sysfs_entry rdev_state =
2807 __ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
2810 errors_show(struct md_rdev *rdev, char *page)
2812 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2816 errors_store(struct md_rdev *rdev, const char *buf, size_t len)
2821 rv = kstrtouint(buf, 10, &n);
2824 atomic_set(&rdev->corrected_errors, n);
2827 static struct rdev_sysfs_entry rdev_errors =
2828 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
2831 slot_show(struct md_rdev *rdev, char *page)
2833 if (test_bit(Journal, &rdev->flags))
2834 return sprintf(page, "journal\n");
2835 else if (rdev->raid_disk < 0)
2836 return sprintf(page, "none\n");
2838 return sprintf(page, "%d\n", rdev->raid_disk);
2842 slot_store(struct md_rdev *rdev, const char *buf, size_t len)
2847 if (test_bit(Journal, &rdev->flags))
2849 if (strncmp(buf, "none", 4)==0)
2852 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2856 if (rdev->mddev->pers && slot == -1) {
2857 /* Setting 'slot' on an active array requires also
2858 * updating the 'rd%d' link, and communicating
2859 * with the personality with ->hot_*_disk.
2860 * For now we only support removing
2861 * failed/spare devices. This normally happens automatically,
2862 * but not when the metadata is externally managed.
2864 if (rdev->raid_disk == -1)
2866 /* personality does all needed checks */
2867 if (rdev->mddev->pers->hot_remove_disk == NULL)
2869 clear_bit(Blocked, &rdev->flags);
2870 remove_and_add_spares(rdev->mddev, rdev);
2871 if (rdev->raid_disk >= 0)
2873 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2874 md_wakeup_thread(rdev->mddev->thread);
2875 } else if (rdev->mddev->pers) {
2876 /* Activating a spare .. or possibly reactivating
2877 * if we ever get bitmaps working here.
2881 if (rdev->raid_disk != -1)
2884 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2887 if (rdev->mddev->pers->hot_add_disk == NULL)
2890 if (slot >= rdev->mddev->raid_disks &&
2891 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2894 rdev->raid_disk = slot;
2895 if (test_bit(In_sync, &rdev->flags))
2896 rdev->saved_raid_disk = slot;
2898 rdev->saved_raid_disk = -1;
2899 clear_bit(In_sync, &rdev->flags);
2900 clear_bit(Bitmap_sync, &rdev->flags);
2901 err = rdev->mddev->pers->
2902 hot_add_disk(rdev->mddev, rdev);
2904 rdev->raid_disk = -1;
2907 sysfs_notify_dirent_safe(rdev->sysfs_state);
2908 if (sysfs_link_rdev(rdev->mddev, rdev))
2909 /* failure here is OK */;
2910 /* don't wakeup anyone, leave that to userspace. */
2912 if (slot >= rdev->mddev->raid_disks &&
2913 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2915 rdev->raid_disk = slot;
2916 /* assume it is working */
2917 clear_bit(Faulty, &rdev->flags);
2918 clear_bit(WriteMostly, &rdev->flags);
2919 set_bit(In_sync, &rdev->flags);
2920 sysfs_notify_dirent_safe(rdev->sysfs_state);
2925 static struct rdev_sysfs_entry rdev_slot =
2926 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
2929 offset_show(struct md_rdev *rdev, char *page)
2931 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
2935 offset_store(struct md_rdev *rdev, const char *buf, size_t len)
2937 unsigned long long offset;
2938 if (kstrtoull(buf, 10, &offset) < 0)
2940 if (rdev->mddev->pers && rdev->raid_disk >= 0)
2942 if (rdev->sectors && rdev->mddev->external)
2943 /* Must set offset before size, so overlap checks
2946 rdev->data_offset = offset;
2947 rdev->new_data_offset = offset;
2951 static struct rdev_sysfs_entry rdev_offset =
2952 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
2954 static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2956 return sprintf(page, "%llu\n",
2957 (unsigned long long)rdev->new_data_offset);
2960 static ssize_t new_offset_store(struct md_rdev *rdev,
2961 const char *buf, size_t len)
2963 unsigned long long new_offset;
2964 struct mddev *mddev = rdev->mddev;
2966 if (kstrtoull(buf, 10, &new_offset) < 0)
2969 if (mddev->sync_thread ||
2970 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
2972 if (new_offset == rdev->data_offset)
2973 /* reset is always permitted */
2975 else if (new_offset > rdev->data_offset) {
2976 /* must not push array size beyond rdev_sectors */
2977 if (new_offset - rdev->data_offset
2978 + mddev->dev_sectors > rdev->sectors)
2981 /* Metadata worries about other space details. */
2983 /* decreasing the offset is inconsistent with a backwards
2986 if (new_offset < rdev->data_offset &&
2987 mddev->reshape_backwards)
2989 /* Increasing offset is inconsistent with forwards
2990 * reshape. reshape_direction should be set to
2991 * 'backwards' first.
2993 if (new_offset > rdev->data_offset &&
2994 !mddev->reshape_backwards)
2997 if (mddev->pers && mddev->persistent &&
2998 !super_types[mddev->major_version]
2999 .allow_new_offset(rdev, new_offset))
3001 rdev->new_data_offset = new_offset;
3002 if (new_offset > rdev->data_offset)
3003 mddev->reshape_backwards = 1;
3004 else if (new_offset < rdev->data_offset)
3005 mddev->reshape_backwards = 0;
3009 static struct rdev_sysfs_entry rdev_new_offset =
3010 __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
3013 rdev_size_show(struct md_rdev *rdev, char *page)
3015 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
3018 static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
3020 /* check if two start/length pairs overlap */
3028 static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
3030 unsigned long long blocks;
3033 if (kstrtoull(buf, 10, &blocks) < 0)
3036 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
3037 return -EINVAL; /* sector conversion overflow */
3040 if (new != blocks * 2)
3041 return -EINVAL; /* unsigned long long to sector_t overflow */
3048 rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3050 struct mddev *my_mddev = rdev->mddev;
3051 sector_t oldsectors = rdev->sectors;
3054 if (test_bit(Journal, &rdev->flags))
3056 if (strict_blocks_to_sectors(buf, §ors) < 0)
3058 if (rdev->data_offset != rdev->new_data_offset)
3059 return -EINVAL; /* too confusing */
3060 if (my_mddev->pers && rdev->raid_disk >= 0) {
3061 if (my_mddev->persistent) {
3062 sectors = super_types[my_mddev->major_version].
3063 rdev_size_change(rdev, sectors);
3066 } else if (!sectors)
3067 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
3069 if (!my_mddev->pers->resize)
3070 /* Cannot change size for RAID0 or Linear etc */
3073 if (sectors < my_mddev->dev_sectors)
3074 return -EINVAL; /* component must fit device */
3076 rdev->sectors = sectors;
3077 if (sectors > oldsectors && my_mddev->external) {
3078 /* Need to check that all other rdevs with the same
3079 * ->bdev do not overlap. 'rcu' is sufficient to walk
3080 * the rdev lists safely.
3081 * This check does not provide a hard guarantee, it
3082 * just helps avoid dangerous mistakes.
3084 struct mddev *mddev;
3086 struct list_head *tmp;
3089 for_each_mddev(mddev, tmp) {
3090 struct md_rdev *rdev2;
3092 rdev_for_each(rdev2, mddev)
3093 if (rdev->bdev == rdev2->bdev &&
3095 overlaps(rdev->data_offset, rdev->sectors,
3108 /* Someone else could have slipped in a size
3109 * change here, but doing so is just silly.
3110 * We put oldsectors back because we *know* it is
3111 * safe, and trust userspace not to race with
3114 rdev->sectors = oldsectors;
3121 static struct rdev_sysfs_entry rdev_size =
3122 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
3124 static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
3126 unsigned long long recovery_start = rdev->recovery_offset;
3128 if (test_bit(In_sync, &rdev->flags) ||
3129 recovery_start == MaxSector)
3130 return sprintf(page, "none\n");
3132 return sprintf(page, "%llu\n", recovery_start);
3135 static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
3137 unsigned long long recovery_start;
3139 if (cmd_match(buf, "none"))
3140 recovery_start = MaxSector;
3141 else if (kstrtoull(buf, 10, &recovery_start))
3144 if (rdev->mddev->pers &&
3145 rdev->raid_disk >= 0)
3148 rdev->recovery_offset = recovery_start;
3149 if (recovery_start == MaxSector)
3150 set_bit(In_sync, &rdev->flags);
3152 clear_bit(In_sync, &rdev->flags);
3156 static struct rdev_sysfs_entry rdev_recovery_start =
3157 __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3159 /* sysfs access to bad-blocks list.
3160 * We present two files.
3161 * 'bad-blocks' lists sector numbers and lengths of ranges that
3162 * are recorded as bad. The list is truncated to fit within
3163 * the one-page limit of sysfs.
3164 * Writing "sector length" to this file adds an acknowledged
3166 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3167 * been acknowledged. Writing to this file adds bad blocks
3168 * without acknowledging them. This is largely for testing.
3170 static ssize_t bb_show(struct md_rdev *rdev, char *page)
3172 return badblocks_show(&rdev->badblocks, page, 0);
3174 static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
3176 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3177 /* Maybe that ack was all we needed */
3178 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3179 wake_up(&rdev->blocked_wait);
3182 static struct rdev_sysfs_entry rdev_bad_blocks =
3183 __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3185 static ssize_t ubb_show(struct md_rdev *rdev, char *page)
3187 return badblocks_show(&rdev->badblocks, page, 1);
3189 static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
3191 return badblocks_store(&rdev->badblocks, page, len, 1);
3193 static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3194 __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3197 ppl_sector_show(struct md_rdev *rdev, char *page)
3199 return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector);
3203 ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len)
3205 unsigned long long sector;
3207 if (kstrtoull(buf, 10, §or) < 0)
3209 if (sector != (sector_t)sector)
3212 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3213 rdev->raid_disk >= 0)
3216 if (rdev->mddev->persistent) {
3217 if (rdev->mddev->major_version == 0)
3219 if ((sector > rdev->sb_start &&
3220 sector - rdev->sb_start > S16_MAX) ||
3221 (sector < rdev->sb_start &&
3222 rdev->sb_start - sector > -S16_MIN))
3224 rdev->ppl.offset = sector - rdev->sb_start;
3225 } else if (!rdev->mddev->external) {
3228 rdev->ppl.sector = sector;
3232 static struct rdev_sysfs_entry rdev_ppl_sector =
3233 __ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store);
3236 ppl_size_show(struct md_rdev *rdev, char *page)
3238 return sprintf(page, "%u\n", rdev->ppl.size);
3242 ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3246 if (kstrtouint(buf, 10, &size) < 0)
3249 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3250 rdev->raid_disk >= 0)
3253 if (rdev->mddev->persistent) {
3254 if (rdev->mddev->major_version == 0)
3258 } else if (!rdev->mddev->external) {
3261 rdev->ppl.size = size;
3265 static struct rdev_sysfs_entry rdev_ppl_size =
3266 __ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store);
3268 static struct attribute *rdev_default_attrs[] = {
3273 &rdev_new_offset.attr,
3275 &rdev_recovery_start.attr,
3276 &rdev_bad_blocks.attr,
3277 &rdev_unack_bad_blocks.attr,
3278 &rdev_ppl_sector.attr,
3279 &rdev_ppl_size.attr,
3283 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3285 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
3286 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
3292 return entry->show(rdev, page);
3296 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3297 const char *page, size_t length)
3299 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
3300 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
3302 struct mddev *mddev = rdev->mddev;
3306 if (!capable(CAP_SYS_ADMIN))
3308 rv = mddev ? mddev_lock(mddev): -EBUSY;
3310 if (rdev->mddev == NULL)
3313 rv = entry->store(rdev, page, length);
3314 mddev_unlock(mddev);
3319 static void rdev_free(struct kobject *ko)
3321 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
3324 static const struct sysfs_ops rdev_sysfs_ops = {
3325 .show = rdev_attr_show,
3326 .store = rdev_attr_store,
3328 static struct kobj_type rdev_ktype = {
3329 .release = rdev_free,
3330 .sysfs_ops = &rdev_sysfs_ops,
3331 .default_attrs = rdev_default_attrs,
3334 int md_rdev_init(struct md_rdev *rdev)
3337 rdev->saved_raid_disk = -1;
3338 rdev->raid_disk = -1;
3340 rdev->data_offset = 0;
3341 rdev->new_data_offset = 0;
3342 rdev->sb_events = 0;
3343 rdev->last_read_error = 0;
3344 rdev->sb_loaded = 0;
3345 rdev->bb_page = NULL;
3346 atomic_set(&rdev->nr_pending, 0);
3347 atomic_set(&rdev->read_errors, 0);
3348 atomic_set(&rdev->corrected_errors, 0);
3350 INIT_LIST_HEAD(&rdev->same_set);
3351 init_waitqueue_head(&rdev->blocked_wait);
3353 /* Add space to store bad block list.
3354 * This reserves the space even on arrays where it cannot
3355 * be used - I wonder if that matters
3357 return badblocks_init(&rdev->badblocks, 0);
3359 EXPORT_SYMBOL_GPL(md_rdev_init);
3361 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3363 * mark the device faulty if:
3365 * - the device is nonexistent (zero size)
3366 * - the device has no valid superblock
3368 * a faulty rdev _never_ has rdev->sb set.
3370 static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
3372 char b[BDEVNAME_SIZE];
3374 struct md_rdev *rdev;
3377 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
3379 return ERR_PTR(-ENOMEM);
3381 err = md_rdev_init(rdev);
3384 err = alloc_disk_sb(rdev);
3388 err = lock_rdev(rdev, newdev, super_format == -2);
3392 kobject_init(&rdev->kobj, &rdev_ktype);
3394 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
3396 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
3397 bdevname(rdev->bdev,b));
3402 if (super_format >= 0) {
3403 err = super_types[super_format].
3404 load_super(rdev, NULL, super_minor);
3405 if (err == -EINVAL) {
3406 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
3407 bdevname(rdev->bdev,b),
3408 super_format, super_minor);
3412 pr_warn("md: could not read %s's sb, not importing!\n",
3413 bdevname(rdev->bdev,b));
3423 md_rdev_clear(rdev);
3425 return ERR_PTR(err);
3429 * Check a full RAID array for plausibility
3432 static void analyze_sbs(struct mddev *mddev)
3435 struct md_rdev *rdev, *freshest, *tmp;
3436 char b[BDEVNAME_SIZE];
3439 rdev_for_each_safe(rdev, tmp, mddev)
3440 switch (super_types[mddev->major_version].
3441 load_super(rdev, freshest, mddev->minor_version)) {
3448 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
3449 bdevname(rdev->bdev,b));
3450 md_kick_rdev_from_array(rdev);
3453 super_types[mddev->major_version].
3454 validate_super(mddev, freshest);
3457 rdev_for_each_safe(rdev, tmp, mddev) {
3458 if (mddev->max_disks &&
3459 (rdev->desc_nr >= mddev->max_disks ||
3460 i > mddev->max_disks)) {
3461 pr_warn("md: %s: %s: only %d devices permitted\n",
3462 mdname(mddev), bdevname(rdev->bdev, b),
3464 md_kick_rdev_from_array(rdev);
3467 if (rdev != freshest) {
3468 if (super_types[mddev->major_version].
3469 validate_super(mddev, rdev)) {
3470 pr_warn("md: kicking non-fresh %s from array!\n",
3471 bdevname(rdev->bdev,b));
3472 md_kick_rdev_from_array(rdev);
3476 if (mddev->level == LEVEL_MULTIPATH) {
3477 rdev->desc_nr = i++;
3478 rdev->raid_disk = rdev->desc_nr;
3479 set_bit(In_sync, &rdev->flags);
3480 } else if (rdev->raid_disk >=
3481 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3482 !test_bit(Journal, &rdev->flags)) {
3483 rdev->raid_disk = -1;
3484 clear_bit(In_sync, &rdev->flags);
3489 /* Read a fixed-point number.
3490 * Numbers in sysfs attributes should be in "standard" units where
3491 * possible, so time should be in seconds.
3492 * However we internally use a a much smaller unit such as
3493 * milliseconds or jiffies.
3494 * This function takes a decimal number with a possible fractional
3495 * component, and produces an integer which is the result of
3496 * multiplying that number by 10^'scale'.
3497 * all without any floating-point arithmetic.
3499 int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3501 unsigned long result = 0;
3503 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3506 else if (decimals < scale) {
3509 result = result * 10 + value;
3521 while (decimals < scale) {
3530 safe_delay_show(struct mddev *mddev, char *page)
3532 int msec = (mddev->safemode_delay*1000)/HZ;
3533 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3536 safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
3540 if (mddev_is_clustered(mddev)) {
3541 pr_warn("md: Safemode is disabled for clustered mode\n");
3545 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
3548 mddev->safemode_delay = 0;
3550 unsigned long old_delay = mddev->safemode_delay;
3551 unsigned long new_delay = (msec*HZ)/1000;
3555 mddev->safemode_delay = new_delay;
3556 if (new_delay < old_delay || old_delay == 0)
3557 mod_timer(&mddev->safemode_timer, jiffies+1);
3561 static struct md_sysfs_entry md_safe_delay =
3562 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
3565 level_show(struct mddev *mddev, char *page)
3567 struct md_personality *p;
3569 spin_lock(&mddev->lock);
3572 ret = sprintf(page, "%s\n", p->name);
3573 else if (mddev->clevel[0])
3574 ret = sprintf(page, "%s\n", mddev->clevel);
3575 else if (mddev->level != LEVEL_NONE)
3576 ret = sprintf(page, "%d\n", mddev->level);
3579 spin_unlock(&mddev->lock);
3584 level_store(struct mddev *mddev, const char *buf, size_t len)
3589 struct md_personality *pers, *oldpers;
3591 void *priv, *oldpriv;
3592 struct md_rdev *rdev;
3594 if (slen == 0 || slen >= sizeof(clevel))
3597 rv = mddev_lock(mddev);
3601 if (mddev->pers == NULL) {
3602 strncpy(mddev->clevel, buf, slen);
3603 if (mddev->clevel[slen-1] == '\n')
3605 mddev->clevel[slen] = 0;
3606 mddev->level = LEVEL_NONE;
3614 /* request to change the personality. Need to ensure:
3615 * - array is not engaged in resync/recovery/reshape
3616 * - old personality can be suspended
3617 * - new personality will access other array.
3621 if (mddev->sync_thread ||
3622 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3623 mddev->reshape_position != MaxSector ||
3624 mddev->sysfs_active)
3628 if (!mddev->pers->quiesce) {
3629 pr_warn("md: %s: %s does not support online personality change\n",
3630 mdname(mddev), mddev->pers->name);
3634 /* Now find the new personality */
3635 strncpy(clevel, buf, slen);
3636 if (clevel[slen-1] == '\n')
3639 if (kstrtol(clevel, 10, &level))
3642 if (request_module("md-%s", clevel) != 0)
3643 request_module("md-level-%s", clevel);
3644 spin_lock(&pers_lock);
3645 pers = find_pers(level, clevel);
3646 if (!pers || !try_module_get(pers->owner)) {
3647 spin_unlock(&pers_lock);
3648 pr_warn("md: personality %s not loaded\n", clevel);
3652 spin_unlock(&pers_lock);
3654 if (pers == mddev->pers) {
3655 /* Nothing to do! */
3656 module_put(pers->owner);
3660 if (!pers->takeover) {
3661 module_put(pers->owner);
3662 pr_warn("md: %s: %s does not support personality takeover\n",
3663 mdname(mddev), clevel);
3668 rdev_for_each(rdev, mddev)
3669 rdev->new_raid_disk = rdev->raid_disk;
3671 /* ->takeover must set new_* and/or delta_disks
3672 * if it succeeds, and may set them when it fails.
3674 priv = pers->takeover(mddev);
3676 mddev->new_level = mddev->level;
3677 mddev->new_layout = mddev->layout;
3678 mddev->new_chunk_sectors = mddev->chunk_sectors;
3679 mddev->raid_disks -= mddev->delta_disks;
3680 mddev->delta_disks = 0;
3681 mddev->reshape_backwards = 0;
3682 module_put(pers->owner);
3683 pr_warn("md: %s: %s would not accept array\n",
3684 mdname(mddev), clevel);
3689 /* Looks like we have a winner */
3690 mddev_suspend(mddev);
3691 mddev_detach(mddev);
3693 spin_lock(&mddev->lock);
3694 oldpers = mddev->pers;
3695 oldpriv = mddev->private;
3697 mddev->private = priv;
3698 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3699 mddev->level = mddev->new_level;
3700 mddev->layout = mddev->new_layout;
3701 mddev->chunk_sectors = mddev->new_chunk_sectors;
3702 mddev->delta_disks = 0;
3703 mddev->reshape_backwards = 0;
3704 mddev->degraded = 0;
3705 spin_unlock(&mddev->lock);
3707 if (oldpers->sync_request == NULL &&
3709 /* We are converting from a no-redundancy array
3710 * to a redundancy array and metadata is managed
3711 * externally so we need to be sure that writes
3712 * won't block due to a need to transition
3714 * until external management is started.
3717 mddev->safemode_delay = 0;
3718 mddev->safemode = 0;
3721 oldpers->free(mddev, oldpriv);
3723 if (oldpers->sync_request == NULL &&
3724 pers->sync_request != NULL) {
3725 /* need to add the md_redundancy_group */
3726 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3727 pr_warn("md: cannot register extra attributes for %s\n",
3729 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3731 if (oldpers->sync_request != NULL &&
3732 pers->sync_request == NULL) {
3733 /* need to remove the md_redundancy_group */
3734 if (mddev->to_remove == NULL)
3735 mddev->to_remove = &md_redundancy_group;
3738 module_put(oldpers->owner);
3740 rdev_for_each(rdev, mddev) {
3741 if (rdev->raid_disk < 0)
3743 if (rdev->new_raid_disk >= mddev->raid_disks)
3744 rdev->new_raid_disk = -1;
3745 if (rdev->new_raid_disk == rdev->raid_disk)
3747 sysfs_unlink_rdev(mddev, rdev);
3749 rdev_for_each(rdev, mddev) {
3750 if (rdev->raid_disk < 0)
3752 if (rdev->new_raid_disk == rdev->raid_disk)
3754 rdev->raid_disk = rdev->new_raid_disk;
3755 if (rdev->raid_disk < 0)
3756 clear_bit(In_sync, &rdev->flags);
3758 if (sysfs_link_rdev(mddev, rdev))
3759 pr_warn("md: cannot register rd%d for %s after level change\n",
3760 rdev->raid_disk, mdname(mddev));
3764 if (pers->sync_request == NULL) {
3765 /* this is now an array without redundancy, so
3766 * it must always be in_sync
3769 del_timer_sync(&mddev->safemode_timer);
3771 blk_set_stacking_limits(&mddev->queue->limits);
3773 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
3774 mddev_resume(mddev);
3776 md_update_sb(mddev, 1);
3777 sysfs_notify(&mddev->kobj, NULL, "level");
3778 md_new_event(mddev);
3781 mddev_unlock(mddev);
3785 static struct md_sysfs_entry md_level =
3786 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
3789 layout_show(struct mddev *mddev, char *page)
3791 /* just a number, not meaningful for all levels */
3792 if (mddev->reshape_position != MaxSector &&
3793 mddev->layout != mddev->new_layout)
3794 return sprintf(page, "%d (%d)\n",
3795 mddev->new_layout, mddev->layout);
3796 return sprintf(page, "%d\n", mddev->layout);
3800 layout_store(struct mddev *mddev, const char *buf, size_t len)
3805 err = kstrtouint(buf, 10, &n);
3808 err = mddev_lock(mddev);
3813 if (mddev->pers->check_reshape == NULL)
3818 mddev->new_layout = n;
3819 err = mddev->pers->check_reshape(mddev);
3821 mddev->new_layout = mddev->layout;
3824 mddev->new_layout = n;
3825 if (mddev->reshape_position == MaxSector)
3828 mddev_unlock(mddev);
3831 static struct md_sysfs_entry md_layout =
3832 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
3835 raid_disks_show(struct mddev *mddev, char *page)
3837 if (mddev->raid_disks == 0)
3839 if (mddev->reshape_position != MaxSector &&
3840 mddev->delta_disks != 0)
3841 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3842 mddev->raid_disks - mddev->delta_disks);
3843 return sprintf(page, "%d\n", mddev->raid_disks);
3846 static int update_raid_disks(struct mddev *mddev, int raid_disks);
3849 raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
3854 err = kstrtouint(buf, 10, &n);
3858 err = mddev_lock(mddev);
3862 err = update_raid_disks(mddev, n);
3863 else if (mddev->reshape_position != MaxSector) {
3864 struct md_rdev *rdev;
3865 int olddisks = mddev->raid_disks - mddev->delta_disks;
3868 rdev_for_each(rdev, mddev) {
3870 rdev->data_offset < rdev->new_data_offset)
3873 rdev->data_offset > rdev->new_data_offset)
3877 mddev->delta_disks = n - olddisks;
3878 mddev->raid_disks = n;
3879 mddev->reshape_backwards = (mddev->delta_disks < 0);
3881 mddev->raid_disks = n;
3883 mddev_unlock(mddev);
3884 return err ? err : len;
3886 static struct md_sysfs_entry md_raid_disks =
3887 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
3890 chunk_size_show(struct mddev *mddev, char *page)
3892 if (mddev->reshape_position != MaxSector &&
3893 mddev->chunk_sectors != mddev->new_chunk_sectors)
3894 return sprintf(page, "%d (%d)\n",
3895 mddev->new_chunk_sectors << 9,
3896 mddev->chunk_sectors << 9);
3897 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
3901 chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
3906 err = kstrtoul(buf, 10, &n);
3910 err = mddev_lock(mddev);
3914 if (mddev->pers->check_reshape == NULL)
3919 mddev->new_chunk_sectors = n >> 9;
3920 err = mddev->pers->check_reshape(mddev);
3922 mddev->new_chunk_sectors = mddev->chunk_sectors;
3925 mddev->new_chunk_sectors = n >> 9;
3926 if (mddev->reshape_position == MaxSector)
3927 mddev->chunk_sectors = n >> 9;
3929 mddev_unlock(mddev);
3932 static struct md_sysfs_entry md_chunk_size =
3933 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
3936 resync_start_show(struct mddev *mddev, char *page)
3938 if (mddev->recovery_cp == MaxSector)
3939 return sprintf(page, "none\n");
3940 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3944 resync_start_store(struct mddev *mddev, const char *buf, size_t len)
3946 unsigned long long n;
3949 if (cmd_match(buf, "none"))
3952 err = kstrtoull(buf, 10, &n);
3955 if (n != (sector_t)n)
3959 err = mddev_lock(mddev);
3962 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3966 mddev->recovery_cp = n;
3968 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
3970 mddev_unlock(mddev);
3973 static struct md_sysfs_entry md_resync_start =
3974 __ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
3975 resync_start_show, resync_start_store);
3978 * The array state can be:
3981 * No devices, no size, no level
3982 * Equivalent to STOP_ARRAY ioctl
3984 * May have some settings, but array is not active
3985 * all IO results in error
3986 * When written, doesn't tear down array, but just stops it
3987 * suspended (not supported yet)
3988 * All IO requests will block. The array can be reconfigured.
3989 * Writing this, if accepted, will block until array is quiescent
3991 * no resync can happen. no superblocks get written.
3992 * write requests fail
3994 * like readonly, but behaves like 'clean' on a write request.
3996 * clean - no pending writes, but otherwise active.
3997 * When written to inactive array, starts without resync
3998 * If a write request arrives then
3999 * if metadata is known, mark 'dirty' and switch to 'active'.
4000 * if not known, block and switch to write-pending
4001 * If written to an active array that has pending writes, then fails.
4003 * fully active: IO and resync can be happening.
4004 * When written to inactive array, starts with resync
4007 * clean, but writes are blocked waiting for 'active' to be written.
4010 * like active, but no writes have been seen for a while (100msec).
4013 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
4014 write_pending, active_idle, bad_word};
4015 static char *array_states[] = {
4016 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
4017 "write-pending", "active-idle", NULL };
4019 static int match_word(const char *word, char **list)
4022 for (n=0; list[n]; n++)
4023 if (cmd_match(word, list[n]))
4029 array_state_show(struct mddev *mddev, char *page)
4031 enum array_state st = inactive;
4042 spin_lock(&mddev->lock);
4043 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
4045 else if (mddev->in_sync)
4047 else if (mddev->safemode)
4051 spin_unlock(&mddev->lock);
4054 if (list_empty(&mddev->disks) &&
4055 mddev->raid_disks == 0 &&
4056 mddev->dev_sectors == 0)
4061 return sprintf(page, "%s\n", array_states[st]);
4064 static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
4065 static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
4066 static int do_md_run(struct mddev *mddev);
4067 static int restart_array(struct mddev *mddev);
4070 array_state_store(struct mddev *mddev, const char *buf, size_t len)
4073 enum array_state st = match_word(buf, array_states);
4075 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
4076 /* don't take reconfig_mutex when toggling between
4079 spin_lock(&mddev->lock);
4081 restart_array(mddev);
4082 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
4083 md_wakeup_thread(mddev->thread);
4084 wake_up(&mddev->sb_wait);
4085 } else /* st == clean */ {
4086 restart_array(mddev);
4087 if (!set_in_sync(mddev))
4091 sysfs_notify_dirent_safe(mddev->sysfs_state);
4092 spin_unlock(&mddev->lock);
4095 err = mddev_lock(mddev);
4103 /* stopping an active array */
4104 err = do_md_stop(mddev, 0, NULL);
4107 /* stopping an active array */
4109 err = do_md_stop(mddev, 2, NULL);
4111 err = 0; /* already inactive */
4114 break; /* not supported yet */
4117 err = md_set_readonly(mddev, NULL);
4120 set_disk_ro(mddev->gendisk, 1);
4121 err = do_md_run(mddev);
4127 err = md_set_readonly(mddev, NULL);
4128 else if (mddev->ro == 1)
4129 err = restart_array(mddev);
4132 set_disk_ro(mddev->gendisk, 0);
4136 err = do_md_run(mddev);
4141 err = restart_array(mddev);
4144 spin_lock(&mddev->lock);
4145 if (!set_in_sync(mddev))
4147 spin_unlock(&mddev->lock);
4153 err = restart_array(mddev);
4156 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
4157 wake_up(&mddev->sb_wait);
4161 set_disk_ro(mddev->gendisk, 0);
4162 err = do_md_run(mddev);
4167 /* these cannot be set */
4172 if (mddev->hold_active == UNTIL_IOCTL)
4173 mddev->hold_active = 0;
4174 sysfs_notify_dirent_safe(mddev->sysfs_state);
4176 mddev_unlock(mddev);
4179 static struct md_sysfs_entry md_array_state =
4180 __ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
4183 max_corrected_read_errors_show(struct mddev *mddev, char *page) {
4184 return sprintf(page, "%d\n",
4185 atomic_read(&mddev->max_corr_read_errors));
4189 max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
4194 rv = kstrtouint(buf, 10, &n);
4197 atomic_set(&mddev->max_corr_read_errors, n);
4201 static struct md_sysfs_entry max_corr_read_errors =
4202 __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4203 max_corrected_read_errors_store);
4206 null_show(struct mddev *mddev, char *page)
4212 new_dev_store(struct mddev *mddev, const char *buf, size_t len)
4214 /* buf must be %d:%d\n? giving major and minor numbers */
4215 /* The new device is added to the array.
4216 * If the array has a persistent superblock, we read the
4217 * superblock to initialise info and check validity.
4218 * Otherwise, only checking done is that in bind_rdev_to_array,
4219 * which mainly checks size.
4222 int major = simple_strtoul(buf, &e, 10);
4225 struct md_rdev *rdev;
4228 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4230 minor = simple_strtoul(e+1, &e, 10);
4231 if (*e && *e != '\n')
4233 dev = MKDEV(major, minor);
4234 if (major != MAJOR(dev) ||
4235 minor != MINOR(dev))
4238 flush_workqueue(md_misc_wq);
4240 err = mddev_lock(mddev);
4243 if (mddev->persistent) {
4244 rdev = md_import_device(dev, mddev->major_version,
4245 mddev->minor_version);
4246 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
4247 struct md_rdev *rdev0
4248 = list_entry(mddev->disks.next,
4249 struct md_rdev, same_set);
4250 err = super_types[mddev->major_version]
4251 .load_super(rdev, rdev0, mddev->minor_version);
4255 } else if (mddev->external)
4256 rdev = md_import_device(dev, -2, -1);
4258 rdev = md_import_device(dev, -1, -1);
4261 mddev_unlock(mddev);
4262 return PTR_ERR(rdev);
4264 err = bind_rdev_to_array(rdev, mddev);
4268 mddev_unlock(mddev);
4269 return err ? err : len;
4272 static struct md_sysfs_entry md_new_device =
4273 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
4276 bitmap_store(struct mddev *mddev, const char *buf, size_t len)
4279 unsigned long chunk, end_chunk;
4282 err = mddev_lock(mddev);
4287 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4289 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4290 if (buf == end) break;
4291 if (*end == '-') { /* range */
4293 end_chunk = simple_strtoul(buf, &end, 0);
4294 if (buf == end) break;
4296 if (*end && !isspace(*end)) break;
4297 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
4298 buf = skip_spaces(end);
4300 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4302 mddev_unlock(mddev);
4306 static struct md_sysfs_entry md_bitmap =
4307 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4310 size_show(struct mddev *mddev, char *page)
4312 return sprintf(page, "%llu\n",
4313 (unsigned long long)mddev->dev_sectors / 2);
4316 static int update_size(struct mddev *mddev, sector_t num_sectors);
4319 size_store(struct mddev *mddev, const char *buf, size_t len)
4321 /* If array is inactive, we can reduce the component size, but
4322 * not increase it (except from 0).
4323 * If array is active, we can try an on-line resize
4326 int err = strict_blocks_to_sectors(buf, §ors);
4330 err = mddev_lock(mddev);
4334 err = update_size(mddev, sectors);
4336 md_update_sb(mddev, 1);
4338 if (mddev->dev_sectors == 0 ||
4339 mddev->dev_sectors > sectors)
4340 mddev->dev_sectors = sectors;
4344 mddev_unlock(mddev);
4345 return err ? err : len;
4348 static struct md_sysfs_entry md_size =
4349 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
4351 /* Metadata version.
4353 * 'none' for arrays with no metadata (good luck...)
4354 * 'external' for arrays with externally managed metadata,
4355 * or N.M for internally known formats
4358 metadata_show(struct mddev *mddev, char *page)
4360 if (mddev->persistent)
4361 return sprintf(page, "%d.%d\n",
4362 mddev->major_version, mddev->minor_version);
4363 else if (mddev->external)
4364 return sprintf(page, "external:%s\n", mddev->metadata_type);
4366 return sprintf(page, "none\n");
4370 metadata_store(struct mddev *mddev, const char *buf, size_t len)
4375 /* Changing the details of 'external' metadata is
4376 * always permitted. Otherwise there must be
4377 * no devices attached to the array.
4380 err = mddev_lock(mddev);
4384 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4386 else if (!list_empty(&mddev->disks))
4390 if (cmd_match(buf, "none")) {
4391 mddev->persistent = 0;
4392 mddev->external = 0;
4393 mddev->major_version = 0;
4394 mddev->minor_version = 90;
4397 if (strncmp(buf, "external:", 9) == 0) {
4398 size_t namelen = len-9;
4399 if (namelen >= sizeof(mddev->metadata_type))
4400 namelen = sizeof(mddev->metadata_type)-1;
4401 strncpy(mddev->metadata_type, buf+9, namelen);
4402 mddev->metadata_type[namelen] = 0;
4403 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4404 mddev->metadata_type[--namelen] = 0;
4405 mddev->persistent = 0;
4406 mddev->external = 1;
4407 mddev->major_version = 0;
4408 mddev->minor_version = 90;
4411 major = simple_strtoul(buf, &e, 10);
4413 if (e==buf || *e != '.')
4416 minor = simple_strtoul(buf, &e, 10);
4417 if (e==buf || (*e && *e != '\n') )
4420 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
4422 mddev->major_version = major;
4423 mddev->minor_version = minor;
4424 mddev->persistent = 1;
4425 mddev->external = 0;
4428 mddev_unlock(mddev);
4432 static struct md_sysfs_entry md_metadata =
4433 __ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
4436 action_show(struct mddev *mddev, char *page)
4438 char *type = "idle";
4439 unsigned long recovery = mddev->recovery;
4440 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
4442 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4443 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4444 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
4446 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4447 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
4449 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
4453 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
4455 else if (mddev->reshape_position != MaxSector)
4458 return sprintf(page, "%s\n", type);
4462 action_store(struct mddev *mddev, const char *page, size_t len)
4464 if (!mddev->pers || !mddev->pers->sync_request)
4468 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
4469 if (cmd_match(page, "frozen"))
4470 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4472 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4473 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4474 mddev_lock(mddev) == 0) {
4475 flush_workqueue(md_misc_wq);
4476 if (mddev->sync_thread) {
4477 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4478 md_reap_sync_thread(mddev);
4480 mddev_unlock(mddev);
4482 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4484 else if (cmd_match(page, "resync"))
4485 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4486 else if (cmd_match(page, "recover")) {
4487 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4488 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4489 } else if (cmd_match(page, "reshape")) {
4491 if (mddev->pers->start_reshape == NULL)
4493 err = mddev_lock(mddev);
4495 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4498 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4499 err = mddev->pers->start_reshape(mddev);
4501 mddev_unlock(mddev);
4505 sysfs_notify(&mddev->kobj, NULL, "degraded");
4507 if (cmd_match(page, "check"))
4508 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4509 else if (!cmd_match(page, "repair"))
4511 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4512 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4513 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4515 if (mddev->ro == 2) {
4516 /* A write to sync_action is enough to justify
4517 * canceling read-auto mode
4520 md_wakeup_thread(mddev->sync_thread);
4522 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4523 md_wakeup_thread(mddev->thread);
4524 sysfs_notify_dirent_safe(mddev->sysfs_action);
4528 static struct md_sysfs_entry md_scan_mode =
4529 __ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
4532 last_sync_action_show(struct mddev *mddev, char *page)
4534 return sprintf(page, "%s\n", mddev->last_sync_action);
4537 static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4540 mismatch_cnt_show(struct mddev *mddev, char *page)
4542 return sprintf(page, "%llu\n",
4543 (unsigned long long)
4544 atomic64_read(&mddev->resync_mismatches));
4547 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
4550 sync_min_show(struct mddev *mddev, char *page)
4552 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4553 mddev->sync_speed_min ? "local": "system");
4557 sync_min_store(struct mddev *mddev, const char *buf, size_t len)
4562 if (strncmp(buf, "system", 6)==0) {
4565 rv = kstrtouint(buf, 10, &min);
4571 mddev->sync_speed_min = min;
4575 static struct md_sysfs_entry md_sync_min =
4576 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4579 sync_max_show(struct mddev *mddev, char *page)
4581 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4582 mddev->sync_speed_max ? "local": "system");
4586 sync_max_store(struct mddev *mddev, const char *buf, size_t len)
4591 if (strncmp(buf, "system", 6)==0) {
4594 rv = kstrtouint(buf, 10, &max);
4600 mddev->sync_speed_max = max;
4604 static struct md_sysfs_entry md_sync_max =
4605 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4608 degraded_show(struct mddev *mddev, char *page)
4610 return sprintf(page, "%d\n", mddev->degraded);
4612 static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
4615 sync_force_parallel_show(struct mddev *mddev, char *page)
4617 return sprintf(page, "%d\n", mddev->parallel_resync);
4621 sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
4625 if (kstrtol(buf, 10, &n))
4628 if (n != 0 && n != 1)
4631 mddev->parallel_resync = n;
4633 if (mddev->sync_thread)
4634 wake_up(&resync_wait);
4639 /* force parallel resync, even with shared block devices */
4640 static struct md_sysfs_entry md_sync_force_parallel =
4641 __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4642 sync_force_parallel_show, sync_force_parallel_store);
4645 sync_speed_show(struct mddev *mddev, char *page)
4647 unsigned long resync, dt, db;
4648 if (mddev->curr_resync == 0)
4649 return sprintf(page, "none\n");
4650 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4651 dt = (jiffies - mddev->resync_mark) / HZ;
4653 db = resync - mddev->resync_mark_cnt;
4654 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
4657 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
4660 sync_completed_show(struct mddev *mddev, char *page)
4662 unsigned long long max_sectors, resync;
4664 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4665 return sprintf(page, "none\n");
4667 if (mddev->curr_resync == 1 ||
4668 mddev->curr_resync == 2)
4669 return sprintf(page, "delayed\n");
4671 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4672 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4673 max_sectors = mddev->resync_max_sectors;
4675 max_sectors = mddev->dev_sectors;
4677 resync = mddev->curr_resync_completed;
4678 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
4681 static struct md_sysfs_entry md_sync_completed =
4682 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
4685 min_sync_show(struct mddev *mddev, char *page)
4687 return sprintf(page, "%llu\n",
4688 (unsigned long long)mddev->resync_min);
4691 min_sync_store(struct mddev *mddev, const char *buf, size_t len)
4693 unsigned long long min;
4696 if (kstrtoull(buf, 10, &min))
4699 spin_lock(&mddev->lock);
4701 if (min > mddev->resync_max)
4705 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4708 /* Round down to multiple of 4K for safety */
4709 mddev->resync_min = round_down(min, 8);
4713 spin_unlock(&mddev->lock);
4717 static struct md_sysfs_entry md_min_sync =
4718 __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4721 max_sync_show(struct mddev *mddev, char *page)
4723 if (mddev->resync_max == MaxSector)
4724 return sprintf(page, "max\n");
4726 return sprintf(page, "%llu\n",
4727 (unsigned long long)mddev->resync_max);
4730 max_sync_store(struct mddev *mddev, const char *buf, size_t len)
4733 spin_lock(&mddev->lock);
4734 if (strncmp(buf, "max", 3) == 0)
4735 mddev->resync_max = MaxSector;
4737 unsigned long long max;
4741 if (kstrtoull(buf, 10, &max))
4743 if (max < mddev->resync_min)
4747 if (max < mddev->resync_max &&
4749 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4752 /* Must be a multiple of chunk_size */
4753 chunk = mddev->chunk_sectors;
4755 sector_t temp = max;
4758 if (sector_div(temp, chunk))
4761 mddev->resync_max = max;
4763 wake_up(&mddev->recovery_wait);
4766 spin_unlock(&mddev->lock);
4770 static struct md_sysfs_entry md_max_sync =
4771 __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4774 suspend_lo_show(struct mddev *mddev, char *page)
4776 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4780 suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
4782 unsigned long long old, new;
4785 err = kstrtoull(buf, 10, &new);
4788 if (new != (sector_t)new)
4791 err = mddev_lock(mddev);
4795 if (mddev->pers == NULL ||
4796 mddev->pers->quiesce == NULL)
4798 old = mddev->suspend_lo;
4799 mddev->suspend_lo = new;
4801 /* Shrinking suspended region */
4802 mddev->pers->quiesce(mddev, 2);
4804 /* Expanding suspended region - need to wait */
4805 mddev->pers->quiesce(mddev, 1);
4806 mddev->pers->quiesce(mddev, 0);
4810 mddev_unlock(mddev);
4813 static struct md_sysfs_entry md_suspend_lo =
4814 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4817 suspend_hi_show(struct mddev *mddev, char *page)
4819 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4823 suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
4825 unsigned long long old, new;
4828 err = kstrtoull(buf, 10, &new);
4831 if (new != (sector_t)new)
4834 err = mddev_lock(mddev);
4838 if (mddev->pers == NULL ||
4839 mddev->pers->quiesce == NULL)
4841 old = mddev->suspend_hi;
4842 mddev->suspend_hi = new;
4844 /* Shrinking suspended region */
4845 mddev->pers->quiesce(mddev, 2);
4847 /* Expanding suspended region - need to wait */
4848 mddev->pers->quiesce(mddev, 1);
4849 mddev->pers->quiesce(mddev, 0);
4853 mddev_unlock(mddev);
4856 static struct md_sysfs_entry md_suspend_hi =
4857 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4860 reshape_position_show(struct mddev *mddev, char *page)
4862 if (mddev->reshape_position != MaxSector)
4863 return sprintf(page, "%llu\n",
4864 (unsigned long long)mddev->reshape_position);
4865 strcpy(page, "none\n");
4870 reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
4872 struct md_rdev *rdev;
4873 unsigned long long new;
4876 err = kstrtoull(buf, 10, &new);
4879 if (new != (sector_t)new)
4881 err = mddev_lock(mddev);
4887 mddev->reshape_position = new;
4888 mddev->delta_disks = 0;
4889 mddev->reshape_backwards = 0;
4890 mddev->new_level = mddev->level;
4891 mddev->new_layout = mddev->layout;
4892 mddev->new_chunk_sectors = mddev->chunk_sectors;
4893 rdev_for_each(rdev, mddev)
4894 rdev->new_data_offset = rdev->data_offset;
4897 mddev_unlock(mddev);
4901 static struct md_sysfs_entry md_reshape_position =
4902 __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4903 reshape_position_store);
4906 reshape_direction_show(struct mddev *mddev, char *page)
4908 return sprintf(page, "%s\n",
4909 mddev->reshape_backwards ? "backwards" : "forwards");
4913 reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4918 if (cmd_match(buf, "forwards"))
4920 else if (cmd_match(buf, "backwards"))
4924 if (mddev->reshape_backwards == backwards)
4927 err = mddev_lock(mddev);
4930 /* check if we are allowed to change */
4931 if (mddev->delta_disks)
4933 else if (mddev->persistent &&
4934 mddev->major_version == 0)
4937 mddev->reshape_backwards = backwards;
4938 mddev_unlock(mddev);
4942 static struct md_sysfs_entry md_reshape_direction =
4943 __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4944 reshape_direction_store);
4947 array_size_show(struct mddev *mddev, char *page)
4949 if (mddev->external_size)
4950 return sprintf(page, "%llu\n",
4951 (unsigned long long)mddev->array_sectors/2);
4953 return sprintf(page, "default\n");
4957 array_size_store(struct mddev *mddev, const char *buf, size_t len)
4962 err = mddev_lock(mddev);
4966 /* cluster raid doesn't support change array_sectors */
4967 if (mddev_is_clustered(mddev)) {
4968 mddev_unlock(mddev);
4972 if (strncmp(buf, "default", 7) == 0) {
4974 sectors = mddev->pers->size(mddev, 0, 0);
4976 sectors = mddev->array_sectors;
4978 mddev->external_size = 0;
4980 if (strict_blocks_to_sectors(buf, §ors) < 0)
4982 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4985 mddev->external_size = 1;
4989 mddev->array_sectors = sectors;
4991 set_capacity(mddev->gendisk, mddev->array_sectors);
4992 revalidate_disk(mddev->gendisk);
4995 mddev_unlock(mddev);
4999 static struct md_sysfs_entry md_array_size =
5000 __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
5004 consistency_policy_show(struct mddev *mddev, char *page)
5008 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5009 ret = sprintf(page, "journal\n");
5010 } else if (test_bit(MD_HAS_PPL, &mddev->flags)) {
5011 ret = sprintf(page, "ppl\n");
5012 } else if (mddev->bitmap) {
5013 ret = sprintf(page, "bitmap\n");
5014 } else if (mddev->pers) {
5015 if (mddev->pers->sync_request)
5016 ret = sprintf(page, "resync\n");
5018 ret = sprintf(page, "none\n");
5020 ret = sprintf(page, "unknown\n");
5027 consistency_policy_store(struct mddev *mddev, const char *buf, size_t len)
5032 if (mddev->pers->change_consistency_policy)
5033 err = mddev->pers->change_consistency_policy(mddev, buf);
5036 } else if (mddev->external && strncmp(buf, "ppl", 3) == 0) {
5037 set_bit(MD_HAS_PPL, &mddev->flags);
5042 return err ? err : len;
5045 static struct md_sysfs_entry md_consistency_policy =
5046 __ATTR(consistency_policy, S_IRUGO | S_IWUSR, consistency_policy_show,
5047 consistency_policy_store);
5049 static struct attribute *md_default_attrs[] = {
5052 &md_raid_disks.attr,
5053 &md_chunk_size.attr,
5055 &md_resync_start.attr,
5057 &md_new_device.attr,
5058 &md_safe_delay.attr,
5059 &md_array_state.attr,
5060 &md_reshape_position.attr,
5061 &md_reshape_direction.attr,
5062 &md_array_size.attr,
5063 &max_corr_read_errors.attr,
5064 &md_consistency_policy.attr,
5068 static struct attribute *md_redundancy_attrs[] = {
5070 &md_last_scan_mode.attr,
5071 &md_mismatches.attr,
5074 &md_sync_speed.attr,
5075 &md_sync_force_parallel.attr,
5076 &md_sync_completed.attr,
5079 &md_suspend_lo.attr,
5080 &md_suspend_hi.attr,
5085 static struct attribute_group md_redundancy_group = {
5087 .attrs = md_redundancy_attrs,
5091 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
5093 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
5094 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
5099 spin_lock(&all_mddevs_lock);
5100 if (list_empty(&mddev->all_mddevs)) {
5101 spin_unlock(&all_mddevs_lock);
5105 spin_unlock(&all_mddevs_lock);
5107 rv = entry->show(mddev, page);
5113 md_attr_store(struct kobject *kobj, struct attribute *attr,
5114 const char *page, size_t length)
5116 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
5117 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
5122 if (!capable(CAP_SYS_ADMIN))
5124 spin_lock(&all_mddevs_lock);
5125 if (list_empty(&mddev->all_mddevs)) {
5126 spin_unlock(&all_mddevs_lock);
5130 spin_unlock(&all_mddevs_lock);
5131 rv = entry->store(mddev, page, length);
5136 static void md_free(struct kobject *ko)
5138 struct mddev *mddev = container_of(ko, struct mddev, kobj);
5140 if (mddev->sysfs_state)
5141 sysfs_put(mddev->sysfs_state);
5144 blk_cleanup_queue(mddev->queue);
5145 if (mddev->gendisk) {
5146 del_gendisk(mddev->gendisk);
5147 put_disk(mddev->gendisk);
5149 percpu_ref_exit(&mddev->writes_pending);
5154 static const struct sysfs_ops md_sysfs_ops = {
5155 .show = md_attr_show,
5156 .store = md_attr_store,
5158 static struct kobj_type md_ktype = {
5160 .sysfs_ops = &md_sysfs_ops,
5161 .default_attrs = md_default_attrs,
5166 static void mddev_delayed_delete(struct work_struct *ws)
5168 struct mddev *mddev = container_of(ws, struct mddev, del_work);
5170 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
5171 kobject_del(&mddev->kobj);
5172 kobject_put(&mddev->kobj);
5175 static void no_op(struct percpu_ref *r) {}
5177 int mddev_init_writes_pending(struct mddev *mddev)
5179 if (mddev->writes_pending.percpu_count_ptr)
5181 if (percpu_ref_init(&mddev->writes_pending, no_op, 0, GFP_KERNEL) < 0)
5183 /* We want to start with the refcount at zero */
5184 percpu_ref_put(&mddev->writes_pending);
5187 EXPORT_SYMBOL_GPL(mddev_init_writes_pending);
5189 static int md_alloc(dev_t dev, char *name)
5192 * If dev is zero, name is the name of a device to allocate with
5193 * an arbitrary minor number. It will be "md_???"
5194 * If dev is non-zero it must be a device number with a MAJOR of
5195 * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then
5196 * the device is being created by opening a node in /dev.
5197 * If "name" is not NULL, the device is being created by
5198 * writing to /sys/module/md_mod/parameters/new_array.
5200 static DEFINE_MUTEX(disks_mutex);
5201 struct mddev *mddev = mddev_find(dev);
5202 struct gendisk *disk;
5211 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
5212 shift = partitioned ? MdpMinorShift : 0;
5213 unit = MINOR(mddev->unit) >> shift;
5215 /* wait for any previous instance of this device to be
5216 * completely removed (mddev_delayed_delete).
5218 flush_workqueue(md_misc_wq);
5220 mutex_lock(&disks_mutex);
5226 /* Need to ensure that 'name' is not a duplicate.
5228 struct mddev *mddev2;
5229 spin_lock(&all_mddevs_lock);
5231 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5232 if (mddev2->gendisk &&
5233 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5234 spin_unlock(&all_mddevs_lock);
5237 spin_unlock(&all_mddevs_lock);
5241 * Creating /dev/mdNNN via "newarray", so adjust hold_active.
5243 mddev->hold_active = UNTIL_STOP;
5246 mddev->queue = blk_alloc_queue(GFP_KERNEL);
5249 mddev->queue->queuedata = mddev;
5251 blk_queue_make_request(mddev->queue, md_make_request);
5252 blk_set_stacking_limits(&mddev->queue->limits);
5254 disk = alloc_disk(1 << shift);
5256 blk_cleanup_queue(mddev->queue);
5257 mddev->queue = NULL;
5260 disk->major = MAJOR(mddev->unit);
5261 disk->first_minor = unit << shift;
5263 strcpy(disk->disk_name, name);
5264 else if (partitioned)
5265 sprintf(disk->disk_name, "md_d%d", unit);
5267 sprintf(disk->disk_name, "md%d", unit);
5268 disk->fops = &md_fops;
5269 disk->private_data = mddev;
5270 disk->queue = mddev->queue;
5271 blk_queue_write_cache(mddev->queue, true, true);
5272 /* Allow extended partitions. This makes the
5273 * 'mdp' device redundant, but we can't really
5276 disk->flags |= GENHD_FL_EXT_DEVT;
5277 mddev->gendisk = disk;
5278 /* As soon as we call add_disk(), another thread could get
5279 * through to md_open, so make sure it doesn't get too far
5281 mutex_lock(&mddev->open_mutex);
5284 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
5285 &disk_to_dev(disk)->kobj, "%s", "md");
5287 /* This isn't possible, but as kobject_init_and_add is marked
5288 * __must_check, we must do something with the result
5290 pr_debug("md: cannot register %s/md - name in use\n",
5294 if (mddev->kobj.sd &&
5295 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
5296 pr_debug("pointless warning\n");
5297 mutex_unlock(&mddev->open_mutex);
5299 mutex_unlock(&disks_mutex);
5300 if (!error && mddev->kobj.sd) {
5301 kobject_uevent(&mddev->kobj, KOBJ_ADD);
5302 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
5308 static struct kobject *md_probe(dev_t dev, int *part, void *data)
5311 md_alloc(dev, NULL);
5315 static int add_named_array(const char *val, struct kernel_param *kp)
5318 * val must be "md_*" or "mdNNN".
5319 * For "md_*" we allocate an array with a large free minor number, and
5320 * set the name to val. val must not already be an active name.
5321 * For "mdNNN" we allocate an array with the minor number NNN
5322 * which must not already be in use.
5324 int len = strlen(val);
5325 char buf[DISK_NAME_LEN];
5326 unsigned long devnum;
5328 while (len && val[len-1] == '\n')
5330 if (len >= DISK_NAME_LEN)
5332 strlcpy(buf, val, len+1);
5333 if (strncmp(buf, "md_", 3) == 0)
5334 return md_alloc(0, buf);
5335 if (strncmp(buf, "md", 2) == 0 &&
5337 kstrtoul(buf+2, 10, &devnum) == 0 &&
5338 devnum <= MINORMASK)
5339 return md_alloc(MKDEV(MD_MAJOR, devnum), NULL);
5344 static void md_safemode_timeout(unsigned long data)
5346 struct mddev *mddev = (struct mddev *) data;
5348 mddev->safemode = 1;
5349 if (mddev->external)
5350 sysfs_notify_dirent_safe(mddev->sysfs_state);
5352 md_wakeup_thread(mddev->thread);
5355 static int start_dirty_degraded;
5357 int md_run(struct mddev *mddev)
5360 struct md_rdev *rdev;
5361 struct md_personality *pers;
5363 if (list_empty(&mddev->disks))
5364 /* cannot run an array with no devices.. */
5369 /* Cannot run until previous stop completes properly */
5370 if (mddev->sysfs_active)
5374 * Analyze all RAID superblock(s)
5376 if (!mddev->raid_disks) {
5377 if (!mddev->persistent)
5382 if (mddev->level != LEVEL_NONE)
5383 request_module("md-level-%d", mddev->level);
5384 else if (mddev->clevel[0])
5385 request_module("md-%s", mddev->clevel);
5388 * Drop all container device buffers, from now on
5389 * the only valid external interface is through the md
5392 rdev_for_each(rdev, mddev) {
5393 if (test_bit(Faulty, &rdev->flags))
5395 sync_blockdev(rdev->bdev);
5396 invalidate_bdev(rdev->bdev);
5397 if (mddev->ro != 1 &&
5398 (bdev_read_only(rdev->bdev) ||
5399 bdev_read_only(rdev->meta_bdev))) {
5402 set_disk_ro(mddev->gendisk, 1);
5405 /* perform some consistency tests on the device.
5406 * We don't want the data to overlap the metadata,
5407 * Internal Bitmap issues have been handled elsewhere.
5409 if (rdev->meta_bdev) {
5410 /* Nothing to check */;
5411 } else if (rdev->data_offset < rdev->sb_start) {
5412 if (mddev->dev_sectors &&
5413 rdev->data_offset + mddev->dev_sectors
5415 pr_warn("md: %s: data overlaps metadata\n",
5420 if (rdev->sb_start + rdev->sb_size/512
5421 > rdev->data_offset) {
5422 pr_warn("md: %s: metadata overlaps data\n",
5427 sysfs_notify_dirent_safe(rdev->sysfs_state);
5430 if (mddev->bio_set == NULL) {
5431 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
5432 if (!mddev->bio_set)
5436 spin_lock(&pers_lock);
5437 pers = find_pers(mddev->level, mddev->clevel);
5438 if (!pers || !try_module_get(pers->owner)) {
5439 spin_unlock(&pers_lock);
5440 if (mddev->level != LEVEL_NONE)
5441 pr_warn("md: personality for level %d is not loaded!\n",
5444 pr_warn("md: personality for level %s is not loaded!\n",
5448 spin_unlock(&pers_lock);
5449 if (mddev->level != pers->level) {
5450 mddev->level = pers->level;
5451 mddev->new_level = pers->level;
5453 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
5455 if (mddev->reshape_position != MaxSector &&
5456 pers->start_reshape == NULL) {
5457 /* This personality cannot handle reshaping... */
5458 module_put(pers->owner);
5462 if (pers->sync_request) {
5463 /* Warn if this is a potentially silly
5466 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5467 struct md_rdev *rdev2;
5470 rdev_for_each(rdev, mddev)
5471 rdev_for_each(rdev2, mddev) {
5473 rdev->bdev->bd_contains ==
5474 rdev2->bdev->bd_contains) {
5475 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5477 bdevname(rdev->bdev,b),
5478 bdevname(rdev2->bdev,b2));
5484 pr_warn("True protection against single-disk failure might be compromised.\n");
5487 mddev->recovery = 0;
5488 /* may be over-ridden by personality */
5489 mddev->resync_max_sectors = mddev->dev_sectors;
5491 mddev->ok_start_degraded = start_dirty_degraded;
5493 if (start_readonly && mddev->ro == 0)
5494 mddev->ro = 2; /* read-only, but switch on first write */
5497 * NOTE: some pers->run(), for example r5l_recovery_log(), wakes
5498 * up mddev->thread. It is important to initialize critical
5499 * resources for mddev->thread BEFORE calling pers->run().
5501 err = pers->run(mddev);
5503 pr_warn("md: pers->run() failed ...\n");
5504 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
5505 WARN_ONCE(!mddev->external_size,
5506 "%s: default size too small, but 'external_size' not in effect?\n",
5508 pr_warn("md: invalid array_size %llu > default size %llu\n",
5509 (unsigned long long)mddev->array_sectors / 2,
5510 (unsigned long long)pers->size(mddev, 0, 0) / 2);
5513 if (err == 0 && pers->sync_request &&
5514 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
5515 struct bitmap *bitmap;
5517 bitmap = bitmap_create(mddev, -1);
5518 if (IS_ERR(bitmap)) {
5519 err = PTR_ERR(bitmap);
5520 pr_warn("%s: failed to create bitmap (%d)\n",
5521 mdname(mddev), err);
5523 mddev->bitmap = bitmap;
5527 mddev_detach(mddev);
5529 pers->free(mddev, mddev->private);
5530 mddev->private = NULL;
5531 module_put(pers->owner);
5532 bitmap_destroy(mddev);
5538 rdev_for_each(rdev, mddev) {
5539 if (rdev->raid_disk >= 0 &&
5540 !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
5545 if (mddev->degraded)
5548 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mddev->queue);
5550 queue_flag_clear_unlocked(QUEUE_FLAG_NONROT, mddev->queue);
5551 mddev->queue->backing_dev_info->congested_data = mddev;
5552 mddev->queue->backing_dev_info->congested_fn = md_congested;
5554 if (pers->sync_request) {
5555 if (mddev->kobj.sd &&
5556 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
5557 pr_warn("md: cannot register extra attributes for %s\n",
5559 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
5560 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
5563 atomic_set(&mddev->max_corr_read_errors,
5564 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
5565 mddev->safemode = 0;
5566 if (mddev_is_clustered(mddev))
5567 mddev->safemode_delay = 0;
5569 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
5572 spin_lock(&mddev->lock);
5574 spin_unlock(&mddev->lock);
5575 rdev_for_each(rdev, mddev)
5576 if (rdev->raid_disk >= 0)
5577 if (sysfs_link_rdev(mddev, rdev))
5578 /* failure here is OK */;
5580 if (mddev->degraded && !mddev->ro)
5581 /* This ensures that recovering status is reported immediately
5582 * via sysfs - until a lack of spares is confirmed.
5584 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
5585 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5587 if (mddev->sb_flags)
5588 md_update_sb(mddev, 0);
5590 md_new_event(mddev);
5591 sysfs_notify_dirent_safe(mddev->sysfs_state);
5592 sysfs_notify_dirent_safe(mddev->sysfs_action);
5593 sysfs_notify(&mddev->kobj, NULL, "degraded");
5596 EXPORT_SYMBOL_GPL(md_run);
5598 static int do_md_run(struct mddev *mddev)
5602 err = md_run(mddev);
5605 err = bitmap_load(mddev);
5607 bitmap_destroy(mddev);
5611 if (mddev_is_clustered(mddev))
5612 md_allow_write(mddev);
5614 md_wakeup_thread(mddev->thread);
5615 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5617 set_capacity(mddev->gendisk, mddev->array_sectors);
5618 revalidate_disk(mddev->gendisk);
5620 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5625 static int restart_array(struct mddev *mddev)
5627 struct gendisk *disk = mddev->gendisk;
5628 struct md_rdev *rdev;
5629 bool has_journal = false;
5630 bool has_readonly = false;
5632 /* Complain if it has no devices */
5633 if (list_empty(&mddev->disks))
5641 rdev_for_each_rcu(rdev, mddev) {
5642 if (test_bit(Journal, &rdev->flags) &&
5643 !test_bit(Faulty, &rdev->flags))
5645 if (bdev_read_only(rdev->bdev))
5646 has_readonly = true;
5649 if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && !has_journal)
5650 /* Don't restart rw with journal missing/faulty */
5655 mddev->safemode = 0;
5657 set_disk_ro(disk, 0);
5658 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev));
5659 /* Kick recovery or resync if necessary */
5660 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5661 md_wakeup_thread(mddev->thread);
5662 md_wakeup_thread(mddev->sync_thread);
5663 sysfs_notify_dirent_safe(mddev->sysfs_state);
5667 static void md_clean(struct mddev *mddev)
5669 mddev->array_sectors = 0;
5670 mddev->external_size = 0;
5671 mddev->dev_sectors = 0;
5672 mddev->raid_disks = 0;
5673 mddev->recovery_cp = 0;
5674 mddev->resync_min = 0;
5675 mddev->resync_max = MaxSector;
5676 mddev->reshape_position = MaxSector;
5677 mddev->external = 0;
5678 mddev->persistent = 0;
5679 mddev->level = LEVEL_NONE;
5680 mddev->clevel[0] = 0;
5682 mddev->sb_flags = 0;
5684 mddev->metadata_type[0] = 0;
5685 mddev->chunk_sectors = 0;
5686 mddev->ctime = mddev->utime = 0;
5688 mddev->max_disks = 0;
5690 mddev->can_decrease_events = 0;
5691 mddev->delta_disks = 0;
5692 mddev->reshape_backwards = 0;
5693 mddev->new_level = LEVEL_NONE;
5694 mddev->new_layout = 0;
5695 mddev->new_chunk_sectors = 0;
5696 mddev->curr_resync = 0;
5697 atomic64_set(&mddev->resync_mismatches, 0);
5698 mddev->suspend_lo = mddev->suspend_hi = 0;
5699 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5700 mddev->recovery = 0;
5703 mddev->degraded = 0;
5704 mddev->safemode = 0;
5705 mddev->private = NULL;
5706 mddev->cluster_info = NULL;
5707 mddev->bitmap_info.offset = 0;
5708 mddev->bitmap_info.default_offset = 0;
5709 mddev->bitmap_info.default_space = 0;
5710 mddev->bitmap_info.chunksize = 0;
5711 mddev->bitmap_info.daemon_sleep = 0;
5712 mddev->bitmap_info.max_write_behind = 0;
5713 mddev->bitmap_info.nodes = 0;
5716 static void __md_stop_writes(struct mddev *mddev)
5718 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5719 flush_workqueue(md_misc_wq);
5720 if (mddev->sync_thread) {
5721 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5722 md_reap_sync_thread(mddev);
5725 del_timer_sync(&mddev->safemode_timer);
5727 if (mddev->pers && mddev->pers->quiesce) {
5728 mddev->pers->quiesce(mddev, 1);
5729 mddev->pers->quiesce(mddev, 0);
5731 bitmap_flush(mddev);
5733 if (mddev->ro == 0 &&
5734 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
5736 /* mark array as shutdown cleanly */
5737 if (!mddev_is_clustered(mddev))
5739 md_update_sb(mddev, 1);
5743 void md_stop_writes(struct mddev *mddev)
5745 mddev_lock_nointr(mddev);
5746 __md_stop_writes(mddev);
5747 mddev_unlock(mddev);
5749 EXPORT_SYMBOL_GPL(md_stop_writes);
5751 static void mddev_detach(struct mddev *mddev)
5753 bitmap_wait_behind_writes(mddev);
5754 if (mddev->pers && mddev->pers->quiesce) {
5755 mddev->pers->quiesce(mddev, 1);
5756 mddev->pers->quiesce(mddev, 0);
5758 md_unregister_thread(&mddev->thread);
5760 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5763 static void __md_stop(struct mddev *mddev)
5765 struct md_personality *pers = mddev->pers;
5766 bitmap_destroy(mddev);
5767 mddev_detach(mddev);
5768 /* Ensure ->event_work is done */
5769 flush_workqueue(md_misc_wq);
5770 spin_lock(&mddev->lock);
5772 spin_unlock(&mddev->lock);
5773 pers->free(mddev, mddev->private);
5774 mddev->private = NULL;
5775 if (pers->sync_request && mddev->to_remove == NULL)
5776 mddev->to_remove = &md_redundancy_group;
5777 module_put(pers->owner);
5778 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5781 void md_stop(struct mddev *mddev)
5783 /* stop the array and free an attached data structures.
5784 * This is called from dm-raid
5788 bioset_free(mddev->bio_set);
5791 EXPORT_SYMBOL_GPL(md_stop);
5793 static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
5798 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5800 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5801 md_wakeup_thread(mddev->thread);
5803 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
5804 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5805 if (mddev->sync_thread)
5806 /* Thread might be blocked waiting for metadata update
5807 * which will now never happen */
5808 wake_up_process(mddev->sync_thread->tsk);
5810 if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
5812 mddev_unlock(mddev);
5813 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5815 wait_event(mddev->sb_wait,
5816 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
5817 mddev_lock_nointr(mddev);
5819 mutex_lock(&mddev->open_mutex);
5820 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
5821 mddev->sync_thread ||
5822 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
5823 pr_warn("md: %s still in use.\n",mdname(mddev));
5825 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5826 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5827 md_wakeup_thread(mddev->thread);
5833 __md_stop_writes(mddev);
5839 set_disk_ro(mddev->gendisk, 1);
5840 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5841 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5842 md_wakeup_thread(mddev->thread);
5843 sysfs_notify_dirent_safe(mddev->sysfs_state);
5847 mutex_unlock(&mddev->open_mutex);
5852 * 0 - completely stop and dis-assemble array
5853 * 2 - stop but do not disassemble array
5855 static int do_md_stop(struct mddev *mddev, int mode,
5856 struct block_device *bdev)
5858 struct gendisk *disk = mddev->gendisk;
5859 struct md_rdev *rdev;
5862 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5864 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5865 md_wakeup_thread(mddev->thread);
5867 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
5868 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5869 if (mddev->sync_thread)
5870 /* Thread might be blocked waiting for metadata update
5871 * which will now never happen */
5872 wake_up_process(mddev->sync_thread->tsk);
5874 mddev_unlock(mddev);
5875 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5876 !test_bit(MD_RECOVERY_RUNNING,
5877 &mddev->recovery)));
5878 mddev_lock_nointr(mddev);
5880 mutex_lock(&mddev->open_mutex);
5881 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
5882 mddev->sysfs_active ||
5883 mddev->sync_thread ||
5884 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
5885 pr_warn("md: %s still in use.\n",mdname(mddev));
5886 mutex_unlock(&mddev->open_mutex);
5888 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5889 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5890 md_wakeup_thread(mddev->thread);
5896 set_disk_ro(disk, 0);
5898 __md_stop_writes(mddev);
5900 mddev->queue->backing_dev_info->congested_fn = NULL;
5902 /* tell userspace to handle 'inactive' */
5903 sysfs_notify_dirent_safe(mddev->sysfs_state);
5905 rdev_for_each(rdev, mddev)
5906 if (rdev->raid_disk >= 0)
5907 sysfs_unlink_rdev(mddev, rdev);
5909 set_capacity(disk, 0);
5910 mutex_unlock(&mddev->open_mutex);
5912 revalidate_disk(disk);
5917 mutex_unlock(&mddev->open_mutex);
5919 * Free resources if final stop
5922 pr_info("md: %s stopped.\n", mdname(mddev));
5924 if (mddev->bitmap_info.file) {
5925 struct file *f = mddev->bitmap_info.file;
5926 spin_lock(&mddev->lock);
5927 mddev->bitmap_info.file = NULL;
5928 spin_unlock(&mddev->lock);
5931 mddev->bitmap_info.offset = 0;
5933 export_array(mddev);
5936 if (mddev->hold_active == UNTIL_STOP)
5937 mddev->hold_active = 0;
5939 md_new_event(mddev);
5940 sysfs_notify_dirent_safe(mddev->sysfs_state);
5945 static void autorun_array(struct mddev *mddev)
5947 struct md_rdev *rdev;
5950 if (list_empty(&mddev->disks))
5953 pr_info("md: running: ");
5955 rdev_for_each(rdev, mddev) {
5956 char b[BDEVNAME_SIZE];
5957 pr_cont("<%s>", bdevname(rdev->bdev,b));
5961 err = do_md_run(mddev);
5963 pr_warn("md: do_md_run() returned %d\n", err);
5964 do_md_stop(mddev, 0, NULL);
5969 * lets try to run arrays based on all disks that have arrived
5970 * until now. (those are in pending_raid_disks)
5972 * the method: pick the first pending disk, collect all disks with
5973 * the same UUID, remove all from the pending list and put them into
5974 * the 'same_array' list. Then order this list based on superblock
5975 * update time (freshest comes first), kick out 'old' disks and
5976 * compare superblocks. If everything's fine then run it.
5978 * If "unit" is allocated, then bump its reference count
5980 static void autorun_devices(int part)
5982 struct md_rdev *rdev0, *rdev, *tmp;
5983 struct mddev *mddev;
5984 char b[BDEVNAME_SIZE];
5986 pr_info("md: autorun ...\n");
5987 while (!list_empty(&pending_raid_disks)) {
5990 LIST_HEAD(candidates);
5991 rdev0 = list_entry(pending_raid_disks.next,
5992 struct md_rdev, same_set);
5994 pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b));
5995 INIT_LIST_HEAD(&candidates);
5996 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
5997 if (super_90_load(rdev, rdev0, 0) >= 0) {
5998 pr_debug("md: adding %s ...\n",
5999 bdevname(rdev->bdev,b));
6000 list_move(&rdev->same_set, &candidates);
6003 * now we have a set of devices, with all of them having
6004 * mostly sane superblocks. It's time to allocate the
6008 dev = MKDEV(mdp_major,
6009 rdev0->preferred_minor << MdpMinorShift);
6010 unit = MINOR(dev) >> MdpMinorShift;
6012 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
6015 if (rdev0->preferred_minor != unit) {
6016 pr_warn("md: unit number in %s is bad: %d\n",
6017 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
6021 md_probe(dev, NULL, NULL);
6022 mddev = mddev_find(dev);
6023 if (!mddev || !mddev->gendisk) {
6028 if (mddev_lock(mddev))
6029 pr_warn("md: %s locked, cannot run\n", mdname(mddev));
6030 else if (mddev->raid_disks || mddev->major_version
6031 || !list_empty(&mddev->disks)) {
6032 pr_warn("md: %s already running, cannot run %s\n",
6033 mdname(mddev), bdevname(rdev0->bdev,b));
6034 mddev_unlock(mddev);
6036 pr_debug("md: created %s\n", mdname(mddev));
6037 mddev->persistent = 1;
6038 rdev_for_each_list(rdev, tmp, &candidates) {
6039 list_del_init(&rdev->same_set);
6040 if (bind_rdev_to_array(rdev, mddev))
6043 autorun_array(mddev);
6044 mddev_unlock(mddev);
6046 /* on success, candidates will be empty, on error
6049 rdev_for_each_list(rdev, tmp, &candidates) {
6050 list_del_init(&rdev->same_set);
6055 pr_info("md: ... autorun DONE.\n");
6057 #endif /* !MODULE */
6059 static int get_version(void __user *arg)
6063 ver.major = MD_MAJOR_VERSION;
6064 ver.minor = MD_MINOR_VERSION;
6065 ver.patchlevel = MD_PATCHLEVEL_VERSION;
6067 if (copy_to_user(arg, &ver, sizeof(ver)))
6073 static int get_array_info(struct mddev *mddev, void __user *arg)
6075 mdu_array_info_t info;
6076 int nr,working,insync,failed,spare;
6077 struct md_rdev *rdev;
6079 nr = working = insync = failed = spare = 0;
6081 rdev_for_each_rcu(rdev, mddev) {
6083 if (test_bit(Faulty, &rdev->flags))
6087 if (test_bit(In_sync, &rdev->flags))
6089 else if (test_bit(Journal, &rdev->flags))
6090 /* TODO: add journal count to md_u.h */
6098 info.major_version = mddev->major_version;
6099 info.minor_version = mddev->minor_version;
6100 info.patch_version = MD_PATCHLEVEL_VERSION;
6101 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
6102 info.level = mddev->level;
6103 info.size = mddev->dev_sectors / 2;
6104 if (info.size != mddev->dev_sectors / 2) /* overflow */
6107 info.raid_disks = mddev->raid_disks;
6108 info.md_minor = mddev->md_minor;
6109 info.not_persistent= !mddev->persistent;
6111 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
6114 info.state = (1<<MD_SB_CLEAN);
6115 if (mddev->bitmap && mddev->bitmap_info.offset)
6116 info.state |= (1<<MD_SB_BITMAP_PRESENT);
6117 if (mddev_is_clustered(mddev))
6118 info.state |= (1<<MD_SB_CLUSTERED);
6119 info.active_disks = insync;
6120 info.working_disks = working;
6121 info.failed_disks = failed;
6122 info.spare_disks = spare;
6124 info.layout = mddev->layout;
6125 info.chunk_size = mddev->chunk_sectors << 9;
6127 if (copy_to_user(arg, &info, sizeof(info)))
6133 static int get_bitmap_file(struct mddev *mddev, void __user * arg)
6135 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
6139 file = kzalloc(sizeof(*file), GFP_NOIO);
6144 spin_lock(&mddev->lock);
6145 /* bitmap enabled */
6146 if (mddev->bitmap_info.file) {
6147 ptr = file_path(mddev->bitmap_info.file, file->pathname,
6148 sizeof(file->pathname));
6152 memmove(file->pathname, ptr,
6153 sizeof(file->pathname)-(ptr-file->pathname));
6155 spin_unlock(&mddev->lock);
6158 copy_to_user(arg, file, sizeof(*file)))
6165 static int get_disk_info(struct mddev *mddev, void __user * arg)
6167 mdu_disk_info_t info;
6168 struct md_rdev *rdev;
6170 if (copy_from_user(&info, arg, sizeof(info)))
6174 rdev = md_find_rdev_nr_rcu(mddev, info.number);
6176 info.major = MAJOR(rdev->bdev->bd_dev);
6177 info.minor = MINOR(rdev->bdev->bd_dev);
6178 info.raid_disk = rdev->raid_disk;
6180 if (test_bit(Faulty, &rdev->flags))
6181 info.state |= (1<<MD_DISK_FAULTY);
6182 else if (test_bit(In_sync, &rdev->flags)) {
6183 info.state |= (1<<MD_DISK_ACTIVE);
6184 info.state |= (1<<MD_DISK_SYNC);
6186 if (test_bit(Journal, &rdev->flags))
6187 info.state |= (1<<MD_DISK_JOURNAL);
6188 if (test_bit(WriteMostly, &rdev->flags))
6189 info.state |= (1<<MD_DISK_WRITEMOSTLY);
6190 if (test_bit(FailFast, &rdev->flags))
6191 info.state |= (1<<MD_DISK_FAILFAST);
6193 info.major = info.minor = 0;
6194 info.raid_disk = -1;
6195 info.state = (1<<MD_DISK_REMOVED);
6199 if (copy_to_user(arg, &info, sizeof(info)))
6205 static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
6207 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
6208 struct md_rdev *rdev;
6209 dev_t dev = MKDEV(info->major,info->minor);
6211 if (mddev_is_clustered(mddev) &&
6212 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
6213 pr_warn("%s: Cannot add to clustered mddev.\n",
6218 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
6221 if (!mddev->raid_disks) {
6223 /* expecting a device which has a superblock */
6224 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
6226 pr_warn("md: md_import_device returned %ld\n",
6228 return PTR_ERR(rdev);
6230 if (!list_empty(&mddev->disks)) {
6231 struct md_rdev *rdev0
6232 = list_entry(mddev->disks.next,
6233 struct md_rdev, same_set);
6234 err = super_types[mddev->major_version]
6235 .load_super(rdev, rdev0, mddev->minor_version);
6237 pr_warn("md: %s has different UUID to %s\n",
6238 bdevname(rdev->bdev,b),
6239 bdevname(rdev0->bdev,b2));
6244 err = bind_rdev_to_array(rdev, mddev);
6251 * add_new_disk can be used once the array is assembled
6252 * to add "hot spares". They must already have a superblock
6257 if (!mddev->pers->hot_add_disk) {
6258 pr_warn("%s: personality does not support diskops!\n",
6262 if (mddev->persistent)
6263 rdev = md_import_device(dev, mddev->major_version,
6264 mddev->minor_version);
6266 rdev = md_import_device(dev, -1, -1);
6268 pr_warn("md: md_import_device returned %ld\n",
6270 return PTR_ERR(rdev);
6272 /* set saved_raid_disk if appropriate */
6273 if (!mddev->persistent) {
6274 if (info->state & (1<<MD_DISK_SYNC) &&
6275 info->raid_disk < mddev->raid_disks) {
6276 rdev->raid_disk = info->raid_disk;
6277 set_bit(In_sync, &rdev->flags);
6278 clear_bit(Bitmap_sync, &rdev->flags);
6280 rdev->raid_disk = -1;
6281 rdev->saved_raid_disk = rdev->raid_disk;
6283 super_types[mddev->major_version].
6284 validate_super(mddev, rdev);
6285 if ((info->state & (1<<MD_DISK_SYNC)) &&
6286 rdev->raid_disk != info->raid_disk) {
6287 /* This was a hot-add request, but events doesn't
6288 * match, so reject it.
6294 clear_bit(In_sync, &rdev->flags); /* just to be sure */
6295 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6296 set_bit(WriteMostly, &rdev->flags);
6298 clear_bit(WriteMostly, &rdev->flags);
6299 if (info->state & (1<<MD_DISK_FAILFAST))
6300 set_bit(FailFast, &rdev->flags);
6302 clear_bit(FailFast, &rdev->flags);
6304 if (info->state & (1<<MD_DISK_JOURNAL)) {
6305 struct md_rdev *rdev2;
6306 bool has_journal = false;
6308 /* make sure no existing journal disk */
6309 rdev_for_each(rdev2, mddev) {
6310 if (test_bit(Journal, &rdev2->flags)) {
6319 set_bit(Journal, &rdev->flags);
6322 * check whether the device shows up in other nodes
6324 if (mddev_is_clustered(mddev)) {
6325 if (info->state & (1 << MD_DISK_CANDIDATE))
6326 set_bit(Candidate, &rdev->flags);
6327 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
6328 /* --add initiated by this node */
6329 err = md_cluster_ops->add_new_disk(mddev, rdev);
6337 rdev->raid_disk = -1;
6338 err = bind_rdev_to_array(rdev, mddev);
6343 if (mddev_is_clustered(mddev)) {
6344 if (info->state & (1 << MD_DISK_CANDIDATE)) {
6346 err = md_cluster_ops->new_disk_ack(mddev,
6349 md_kick_rdev_from_array(rdev);
6353 md_cluster_ops->add_new_disk_cancel(mddev);
6355 err = add_bound_rdev(rdev);
6359 err = add_bound_rdev(rdev);
6364 /* otherwise, add_new_disk is only allowed
6365 * for major_version==0 superblocks
6367 if (mddev->major_version != 0) {
6368 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev));
6372 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6374 rdev = md_import_device(dev, -1, 0);
6376 pr_warn("md: error, md_import_device() returned %ld\n",
6378 return PTR_ERR(rdev);
6380 rdev->desc_nr = info->number;
6381 if (info->raid_disk < mddev->raid_disks)
6382 rdev->raid_disk = info->raid_disk;
6384 rdev->raid_disk = -1;
6386 if (rdev->raid_disk < mddev->raid_disks)
6387 if (info->state & (1<<MD_DISK_SYNC))
6388 set_bit(In_sync, &rdev->flags);
6390 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6391 set_bit(WriteMostly, &rdev->flags);
6392 if (info->state & (1<<MD_DISK_FAILFAST))
6393 set_bit(FailFast, &rdev->flags);
6395 if (!mddev->persistent) {
6396 pr_debug("md: nonpersistent superblock ...\n");
6397 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6399 rdev->sb_start = calc_dev_sboffset(rdev);
6400 rdev->sectors = rdev->sb_start;
6402 err = bind_rdev_to_array(rdev, mddev);
6412 static int hot_remove_disk(struct mddev *mddev, dev_t dev)
6414 char b[BDEVNAME_SIZE];
6415 struct md_rdev *rdev;
6417 rdev = find_rdev(mddev, dev);
6421 if (rdev->raid_disk < 0)
6424 clear_bit(Blocked, &rdev->flags);
6425 remove_and_add_spares(mddev, rdev);
6427 if (rdev->raid_disk >= 0)
6431 if (mddev_is_clustered(mddev))
6432 md_cluster_ops->remove_disk(mddev, rdev);
6434 md_kick_rdev_from_array(rdev);
6435 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
6437 md_wakeup_thread(mddev->thread);
6439 md_update_sb(mddev, 1);
6440 md_new_event(mddev);
6444 pr_debug("md: cannot remove active disk %s from %s ...\n",
6445 bdevname(rdev->bdev,b), mdname(mddev));
6449 static int hot_add_disk(struct mddev *mddev, dev_t dev)
6451 char b[BDEVNAME_SIZE];
6453 struct md_rdev *rdev;
6458 if (mddev->major_version != 0) {
6459 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
6463 if (!mddev->pers->hot_add_disk) {
6464 pr_warn("%s: personality does not support diskops!\n",
6469 rdev = md_import_device(dev, -1, 0);
6471 pr_warn("md: error, md_import_device() returned %ld\n",
6476 if (mddev->persistent)
6477 rdev->sb_start = calc_dev_sboffset(rdev);
6479 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6481 rdev->sectors = rdev->sb_start;
6483 if (test_bit(Faulty, &rdev->flags)) {
6484 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
6485 bdevname(rdev->bdev,b), mdname(mddev));
6490 clear_bit(In_sync, &rdev->flags);
6492 rdev->saved_raid_disk = -1;
6493 err = bind_rdev_to_array(rdev, mddev);
6498 * The rest should better be atomic, we can have disk failures
6499 * noticed in interrupt contexts ...
6502 rdev->raid_disk = -1;
6504 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
6506 md_update_sb(mddev, 1);
6508 * Kick recovery, maybe this spare has to be added to the
6509 * array immediately.
6511 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6512 md_wakeup_thread(mddev->thread);
6513 md_new_event(mddev);
6521 static int set_bitmap_file(struct mddev *mddev, int fd)
6526 if (!mddev->pers->quiesce || !mddev->thread)
6528 if (mddev->recovery || mddev->sync_thread)
6530 /* we should be able to change the bitmap.. */
6534 struct inode *inode;
6537 if (mddev->bitmap || mddev->bitmap_info.file)
6538 return -EEXIST; /* cannot add when bitmap is present */
6542 pr_warn("%s: error: failed to get bitmap file\n",
6547 inode = f->f_mapping->host;
6548 if (!S_ISREG(inode->i_mode)) {
6549 pr_warn("%s: error: bitmap file must be a regular file\n",
6552 } else if (!(f->f_mode & FMODE_WRITE)) {
6553 pr_warn("%s: error: bitmap file must open for write\n",
6556 } else if (atomic_read(&inode->i_writecount) != 1) {
6557 pr_warn("%s: error: bitmap file is already in use\n",
6565 mddev->bitmap_info.file = f;
6566 mddev->bitmap_info.offset = 0; /* file overrides offset */
6567 } else if (mddev->bitmap == NULL)
6568 return -ENOENT; /* cannot remove what isn't there */
6571 mddev->pers->quiesce(mddev, 1);
6573 struct bitmap *bitmap;
6575 bitmap = bitmap_create(mddev, -1);
6576 if (!IS_ERR(bitmap)) {
6577 mddev->bitmap = bitmap;
6578 err = bitmap_load(mddev);
6580 err = PTR_ERR(bitmap);
6582 if (fd < 0 || err) {
6583 bitmap_destroy(mddev);
6584 fd = -1; /* make sure to put the file */
6586 mddev->pers->quiesce(mddev, 0);
6589 struct file *f = mddev->bitmap_info.file;
6591 spin_lock(&mddev->lock);
6592 mddev->bitmap_info.file = NULL;
6593 spin_unlock(&mddev->lock);
6602 * set_array_info is used two different ways
6603 * The original usage is when creating a new array.
6604 * In this usage, raid_disks is > 0 and it together with
6605 * level, size, not_persistent,layout,chunksize determine the
6606 * shape of the array.
6607 * This will always create an array with a type-0.90.0 superblock.
6608 * The newer usage is when assembling an array.
6609 * In this case raid_disks will be 0, and the major_version field is
6610 * use to determine which style super-blocks are to be found on the devices.
6611 * The minor and patch _version numbers are also kept incase the
6612 * super_block handler wishes to interpret them.
6614 static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
6617 if (info->raid_disks == 0) {
6618 /* just setting version number for superblock loading */
6619 if (info->major_version < 0 ||
6620 info->major_version >= ARRAY_SIZE(super_types) ||
6621 super_types[info->major_version].name == NULL) {
6622 /* maybe try to auto-load a module? */
6623 pr_warn("md: superblock version %d not known\n",
6624 info->major_version);
6627 mddev->major_version = info->major_version;
6628 mddev->minor_version = info->minor_version;
6629 mddev->patch_version = info->patch_version;
6630 mddev->persistent = !info->not_persistent;
6631 /* ensure mddev_put doesn't delete this now that there
6632 * is some minimal configuration.
6634 mddev->ctime = ktime_get_real_seconds();
6637 mddev->major_version = MD_MAJOR_VERSION;
6638 mddev->minor_version = MD_MINOR_VERSION;
6639 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6640 mddev->ctime = ktime_get_real_seconds();
6642 mddev->level = info->level;
6643 mddev->clevel[0] = 0;
6644 mddev->dev_sectors = 2 * (sector_t)info->size;
6645 mddev->raid_disks = info->raid_disks;
6646 /* don't set md_minor, it is determined by which /dev/md* was
6649 if (info->state & (1<<MD_SB_CLEAN))
6650 mddev->recovery_cp = MaxSector;
6652 mddev->recovery_cp = 0;
6653 mddev->persistent = ! info->not_persistent;
6654 mddev->external = 0;
6656 mddev->layout = info->layout;
6657 mddev->chunk_sectors = info->chunk_size >> 9;
6659 if (mddev->persistent) {
6660 mddev->max_disks = MD_SB_DISKS;
6662 mddev->sb_flags = 0;
6664 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
6666 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
6667 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
6668 mddev->bitmap_info.offset = 0;
6670 mddev->reshape_position = MaxSector;
6673 * Generate a 128 bit UUID
6675 get_random_bytes(mddev->uuid, 16);
6677 mddev->new_level = mddev->level;
6678 mddev->new_chunk_sectors = mddev->chunk_sectors;
6679 mddev->new_layout = mddev->layout;
6680 mddev->delta_disks = 0;
6681 mddev->reshape_backwards = 0;
6686 void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
6688 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6690 if (mddev->external_size)
6693 mddev->array_sectors = array_sectors;
6695 EXPORT_SYMBOL(md_set_array_sectors);
6697 static int update_size(struct mddev *mddev, sector_t num_sectors)
6699 struct md_rdev *rdev;
6701 int fit = (num_sectors == 0);
6702 sector_t old_dev_sectors = mddev->dev_sectors;
6704 if (mddev->pers->resize == NULL)
6706 /* The "num_sectors" is the number of sectors of each device that
6707 * is used. This can only make sense for arrays with redundancy.
6708 * linear and raid0 always use whatever space is available. We can only
6709 * consider changing this number if no resync or reconstruction is
6710 * happening, and if the new size is acceptable. It must fit before the
6711 * sb_start or, if that is <data_offset, it must fit before the size
6712 * of each device. If num_sectors is zero, we find the largest size
6715 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6721 rdev_for_each(rdev, mddev) {
6722 sector_t avail = rdev->sectors;
6724 if (fit && (num_sectors == 0 || num_sectors > avail))
6725 num_sectors = avail;
6726 if (avail < num_sectors)
6729 rv = mddev->pers->resize(mddev, num_sectors);
6731 if (mddev_is_clustered(mddev))
6732 md_cluster_ops->update_size(mddev, old_dev_sectors);
6733 else if (mddev->queue) {
6734 set_capacity(mddev->gendisk, mddev->array_sectors);
6735 revalidate_disk(mddev->gendisk);
6741 static int update_raid_disks(struct mddev *mddev, int raid_disks)
6744 struct md_rdev *rdev;
6745 /* change the number of raid disks */
6746 if (mddev->pers->check_reshape == NULL)
6750 if (raid_disks <= 0 ||
6751 (mddev->max_disks && raid_disks >= mddev->max_disks))
6753 if (mddev->sync_thread ||
6754 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6755 mddev->reshape_position != MaxSector)
6758 rdev_for_each(rdev, mddev) {
6759 if (mddev->raid_disks < raid_disks &&
6760 rdev->data_offset < rdev->new_data_offset)
6762 if (mddev->raid_disks > raid_disks &&
6763 rdev->data_offset > rdev->new_data_offset)
6767 mddev->delta_disks = raid_disks - mddev->raid_disks;
6768 if (mddev->delta_disks < 0)
6769 mddev->reshape_backwards = 1;
6770 else if (mddev->delta_disks > 0)
6771 mddev->reshape_backwards = 0;
6773 rv = mddev->pers->check_reshape(mddev);
6775 mddev->delta_disks = 0;
6776 mddev->reshape_backwards = 0;
6782 * update_array_info is used to change the configuration of an
6784 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6785 * fields in the info are checked against the array.
6786 * Any differences that cannot be handled will cause an error.
6787 * Normally, only one change can be managed at a time.
6789 static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
6795 /* calculate expected state,ignoring low bits */
6796 if (mddev->bitmap && mddev->bitmap_info.offset)
6797 state |= (1 << MD_SB_BITMAP_PRESENT);
6799 if (mddev->major_version != info->major_version ||
6800 mddev->minor_version != info->minor_version ||
6801 /* mddev->patch_version != info->patch_version || */
6802 mddev->ctime != info->ctime ||
6803 mddev->level != info->level ||
6804 /* mddev->layout != info->layout || */
6805 mddev->persistent != !info->not_persistent ||
6806 mddev->chunk_sectors != info->chunk_size >> 9 ||
6807 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6808 ((state^info->state) & 0xfffffe00)
6811 /* Check there is only one change */
6812 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6814 if (mddev->raid_disks != info->raid_disks)
6816 if (mddev->layout != info->layout)
6818 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6825 if (mddev->layout != info->layout) {
6827 * we don't need to do anything at the md level, the
6828 * personality will take care of it all.
6830 if (mddev->pers->check_reshape == NULL)
6833 mddev->new_layout = info->layout;
6834 rv = mddev->pers->check_reshape(mddev);
6836 mddev->new_layout = mddev->layout;
6840 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6841 rv = update_size(mddev, (sector_t)info->size * 2);
6843 if (mddev->raid_disks != info->raid_disks)
6844 rv = update_raid_disks(mddev, info->raid_disks);
6846 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
6847 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6851 if (mddev->recovery || mddev->sync_thread) {
6855 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6856 struct bitmap *bitmap;
6857 /* add the bitmap */
6858 if (mddev->bitmap) {
6862 if (mddev->bitmap_info.default_offset == 0) {
6866 mddev->bitmap_info.offset =
6867 mddev->bitmap_info.default_offset;
6868 mddev->bitmap_info.space =
6869 mddev->bitmap_info.default_space;
6870 mddev->pers->quiesce(mddev, 1);
6871 bitmap = bitmap_create(mddev, -1);
6872 if (!IS_ERR(bitmap)) {
6873 mddev->bitmap = bitmap;
6874 rv = bitmap_load(mddev);
6876 rv = PTR_ERR(bitmap);
6878 bitmap_destroy(mddev);
6879 mddev->pers->quiesce(mddev, 0);
6881 /* remove the bitmap */
6882 if (!mddev->bitmap) {
6886 if (mddev->bitmap->storage.file) {
6890 if (mddev->bitmap_info.nodes) {
6891 /* hold PW on all the bitmap lock */
6892 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
6893 pr_warn("md: can't change bitmap to none since the array is in use by more than one node\n");
6895 md_cluster_ops->unlock_all_bitmaps(mddev);
6899 mddev->bitmap_info.nodes = 0;
6900 md_cluster_ops->leave(mddev);
6902 mddev->pers->quiesce(mddev, 1);
6903 bitmap_destroy(mddev);
6904 mddev->pers->quiesce(mddev, 0);
6905 mddev->bitmap_info.offset = 0;
6908 md_update_sb(mddev, 1);
6914 static int set_disk_faulty(struct mddev *mddev, dev_t dev)
6916 struct md_rdev *rdev;
6919 if (mddev->pers == NULL)
6923 rdev = find_rdev_rcu(mddev, dev);
6927 md_error(mddev, rdev);
6928 if (!test_bit(Faulty, &rdev->flags))
6936 * We have a problem here : there is no easy way to give a CHS
6937 * virtual geometry. We currently pretend that we have a 2 heads
6938 * 4 sectors (with a BIG number of cylinders...). This drives
6939 * dosfs just mad... ;-)
6941 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6943 struct mddev *mddev = bdev->bd_disk->private_data;
6947 geo->cylinders = mddev->array_sectors / 8;
6951 static inline bool md_ioctl_valid(unsigned int cmd)
6956 case GET_ARRAY_INFO:
6957 case GET_BITMAP_FILE:
6960 case HOT_REMOVE_DISK:
6963 case RESTART_ARRAY_RW:
6965 case SET_ARRAY_INFO:
6966 case SET_BITMAP_FILE:
6967 case SET_DISK_FAULTY:
6970 case CLUSTERED_DISK_NACK:
6977 static int md_ioctl(struct block_device *bdev, fmode_t mode,
6978 unsigned int cmd, unsigned long arg)
6981 void __user *argp = (void __user *)arg;
6982 struct mddev *mddev = NULL;
6984 bool did_set_md_closing = false;
6986 if (!md_ioctl_valid(cmd))
6991 case GET_ARRAY_INFO:
6995 if (!capable(CAP_SYS_ADMIN))
7000 * Commands dealing with the RAID driver but not any
7005 err = get_version(argp);
7011 autostart_arrays(arg);
7018 * Commands creating/starting a new array:
7021 mddev = bdev->bd_disk->private_data;
7028 /* Some actions do not requires the mutex */
7030 case GET_ARRAY_INFO:
7031 if (!mddev->raid_disks && !mddev->external)
7034 err = get_array_info(mddev, argp);
7038 if (!mddev->raid_disks && !mddev->external)
7041 err = get_disk_info(mddev, argp);
7044 case SET_DISK_FAULTY:
7045 err = set_disk_faulty(mddev, new_decode_dev(arg));
7048 case GET_BITMAP_FILE:
7049 err = get_bitmap_file(mddev, argp);
7054 if (cmd == ADD_NEW_DISK)
7055 /* need to ensure md_delayed_delete() has completed */
7056 flush_workqueue(md_misc_wq);
7058 if (cmd == HOT_REMOVE_DISK)
7059 /* need to ensure recovery thread has run */
7060 wait_event_interruptible_timeout(mddev->sb_wait,
7061 !test_bit(MD_RECOVERY_NEEDED,
7063 msecs_to_jiffies(5000));
7064 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
7065 /* Need to flush page cache, and ensure no-one else opens
7068 mutex_lock(&mddev->open_mutex);
7069 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
7070 mutex_unlock(&mddev->open_mutex);
7074 WARN_ON_ONCE(test_bit(MD_CLOSING, &mddev->flags));
7075 set_bit(MD_CLOSING, &mddev->flags);
7076 did_set_md_closing = true;
7077 mutex_unlock(&mddev->open_mutex);
7078 sync_blockdev(bdev);
7080 err = mddev_lock(mddev);
7082 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7087 if (cmd == SET_ARRAY_INFO) {
7088 mdu_array_info_t info;
7090 memset(&info, 0, sizeof(info));
7091 else if (copy_from_user(&info, argp, sizeof(info))) {
7096 err = update_array_info(mddev, &info);
7098 pr_warn("md: couldn't update array info. %d\n", err);
7103 if (!list_empty(&mddev->disks)) {
7104 pr_warn("md: array %s already has disks!\n", mdname(mddev));
7108 if (mddev->raid_disks) {
7109 pr_warn("md: array %s already initialised!\n", mdname(mddev));
7113 err = set_array_info(mddev, &info);
7115 pr_warn("md: couldn't set array info. %d\n", err);
7122 * Commands querying/configuring an existing array:
7124 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
7125 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
7126 if ((!mddev->raid_disks && !mddev->external)
7127 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
7128 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
7129 && cmd != GET_BITMAP_FILE) {
7135 * Commands even a read-only array can execute:
7138 case RESTART_ARRAY_RW:
7139 err = restart_array(mddev);
7143 err = do_md_stop(mddev, 0, bdev);
7147 err = md_set_readonly(mddev, bdev);
7150 case HOT_REMOVE_DISK:
7151 err = hot_remove_disk(mddev, new_decode_dev(arg));
7155 /* We can support ADD_NEW_DISK on read-only arrays
7156 * only if we are re-adding a preexisting device.
7157 * So require mddev->pers and MD_DISK_SYNC.
7160 mdu_disk_info_t info;
7161 if (copy_from_user(&info, argp, sizeof(info)))
7163 else if (!(info.state & (1<<MD_DISK_SYNC)))
7164 /* Need to clear read-only for this */
7167 err = add_new_disk(mddev, &info);
7173 if (get_user(ro, (int __user *)(arg))) {
7179 /* if the bdev is going readonly the value of mddev->ro
7180 * does not matter, no writes are coming
7185 /* are we are already prepared for writes? */
7189 /* transitioning to readauto need only happen for
7190 * arrays that call md_write_start
7193 err = restart_array(mddev);
7196 set_disk_ro(mddev->gendisk, 0);
7203 * The remaining ioctls are changing the state of the
7204 * superblock, so we do not allow them on read-only arrays.
7206 if (mddev->ro && mddev->pers) {
7207 if (mddev->ro == 2) {
7209 sysfs_notify_dirent_safe(mddev->sysfs_state);
7210 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7211 /* mddev_unlock will wake thread */
7212 /* If a device failed while we were read-only, we
7213 * need to make sure the metadata is updated now.
7215 if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) {
7216 mddev_unlock(mddev);
7217 wait_event(mddev->sb_wait,
7218 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) &&
7219 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
7220 mddev_lock_nointr(mddev);
7231 mdu_disk_info_t info;
7232 if (copy_from_user(&info, argp, sizeof(info)))
7235 err = add_new_disk(mddev, &info);
7239 case CLUSTERED_DISK_NACK:
7240 if (mddev_is_clustered(mddev))
7241 md_cluster_ops->new_disk_ack(mddev, false);
7247 err = hot_add_disk(mddev, new_decode_dev(arg));
7251 err = do_md_run(mddev);
7254 case SET_BITMAP_FILE:
7255 err = set_bitmap_file(mddev, (int)arg);
7264 if (mddev->hold_active == UNTIL_IOCTL &&
7266 mddev->hold_active = 0;
7267 mddev_unlock(mddev);
7269 if(did_set_md_closing)
7270 clear_bit(MD_CLOSING, &mddev->flags);
7273 #ifdef CONFIG_COMPAT
7274 static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7275 unsigned int cmd, unsigned long arg)
7278 case HOT_REMOVE_DISK:
7280 case SET_DISK_FAULTY:
7281 case SET_BITMAP_FILE:
7282 /* These take in integer arg, do not convert */
7285 arg = (unsigned long)compat_ptr(arg);
7289 return md_ioctl(bdev, mode, cmd, arg);
7291 #endif /* CONFIG_COMPAT */
7293 static int md_open(struct block_device *bdev, fmode_t mode)
7296 * Succeed if we can lock the mddev, which confirms that
7297 * it isn't being stopped right now.
7299 struct mddev *mddev = mddev_find(bdev->bd_dev);
7305 if (mddev->gendisk != bdev->bd_disk) {
7306 /* we are racing with mddev_put which is discarding this
7310 /* Wait until bdev->bd_disk is definitely gone */
7311 flush_workqueue(md_misc_wq);
7312 /* Then retry the open from the top */
7313 return -ERESTARTSYS;
7315 BUG_ON(mddev != bdev->bd_disk->private_data);
7317 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
7320 if (test_bit(MD_CLOSING, &mddev->flags)) {
7321 mutex_unlock(&mddev->open_mutex);
7327 atomic_inc(&mddev->openers);
7328 mutex_unlock(&mddev->open_mutex);
7330 check_disk_change(bdev);
7337 static void md_release(struct gendisk *disk, fmode_t mode)
7339 struct mddev *mddev = disk->private_data;
7342 atomic_dec(&mddev->openers);
7346 static int md_media_changed(struct gendisk *disk)
7348 struct mddev *mddev = disk->private_data;
7350 return mddev->changed;
7353 static int md_revalidate(struct gendisk *disk)
7355 struct mddev *mddev = disk->private_data;
7360 static const struct block_device_operations md_fops =
7362 .owner = THIS_MODULE,
7364 .release = md_release,
7366 #ifdef CONFIG_COMPAT
7367 .compat_ioctl = md_compat_ioctl,
7369 .getgeo = md_getgeo,
7370 .media_changed = md_media_changed,
7371 .revalidate_disk= md_revalidate,
7374 static int md_thread(void *arg)
7376 struct md_thread *thread = arg;
7379 * md_thread is a 'system-thread', it's priority should be very
7380 * high. We avoid resource deadlocks individually in each
7381 * raid personality. (RAID5 does preallocation) We also use RR and
7382 * the very same RT priority as kswapd, thus we will never get
7383 * into a priority inversion deadlock.
7385 * we definitely have to have equal or higher priority than
7386 * bdflush, otherwise bdflush will deadlock if there are too
7387 * many dirty RAID5 blocks.
7390 allow_signal(SIGKILL);
7391 while (!kthread_should_stop()) {
7393 /* We need to wait INTERRUPTIBLE so that
7394 * we don't add to the load-average.
7395 * That means we need to be sure no signals are
7398 if (signal_pending(current))
7399 flush_signals(current);
7401 wait_event_interruptible_timeout
7403 test_bit(THREAD_WAKEUP, &thread->flags)
7404 || kthread_should_stop() || kthread_should_park(),
7407 clear_bit(THREAD_WAKEUP, &thread->flags);
7408 if (kthread_should_park())
7410 if (!kthread_should_stop())
7411 thread->run(thread);
7417 void md_wakeup_thread(struct md_thread *thread)
7420 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
7421 if (!test_and_set_bit(THREAD_WAKEUP, &thread->flags))
7422 wake_up(&thread->wqueue);
7425 EXPORT_SYMBOL(md_wakeup_thread);
7427 struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7428 struct mddev *mddev, const char *name)
7430 struct md_thread *thread;
7432 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
7436 init_waitqueue_head(&thread->wqueue);
7439 thread->mddev = mddev;
7440 thread->timeout = MAX_SCHEDULE_TIMEOUT;
7441 thread->tsk = kthread_run(md_thread, thread,
7443 mdname(thread->mddev),
7445 if (IS_ERR(thread->tsk)) {
7451 EXPORT_SYMBOL(md_register_thread);
7453 void md_unregister_thread(struct md_thread **threadp)
7455 struct md_thread *thread = *threadp;
7458 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
7459 /* Locking ensures that mddev_unlock does not wake_up a
7460 * non-existent thread
7462 spin_lock(&pers_lock);
7464 spin_unlock(&pers_lock);
7466 kthread_stop(thread->tsk);
7469 EXPORT_SYMBOL(md_unregister_thread);
7471 void md_error(struct mddev *mddev, struct md_rdev *rdev)
7473 if (!rdev || test_bit(Faulty, &rdev->flags))
7476 if (!mddev->pers || !mddev->pers->error_handler)
7478 mddev->pers->error_handler(mddev,rdev);
7479 if (mddev->degraded)
7480 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7481 sysfs_notify_dirent_safe(rdev->sysfs_state);
7482 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7483 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7484 md_wakeup_thread(mddev->thread);
7485 if (mddev->event_work.func)
7486 queue_work(md_misc_wq, &mddev->event_work);
7487 md_new_event(mddev);
7489 EXPORT_SYMBOL(md_error);
7491 /* seq_file implementation /proc/mdstat */
7493 static void status_unused(struct seq_file *seq)
7496 struct md_rdev *rdev;
7498 seq_printf(seq, "unused devices: ");
7500 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
7501 char b[BDEVNAME_SIZE];
7503 seq_printf(seq, "%s ",
7504 bdevname(rdev->bdev,b));
7507 seq_printf(seq, "<none>");
7509 seq_printf(seq, "\n");
7512 static int status_resync(struct seq_file *seq, struct mddev *mddev)
7514 sector_t max_sectors, resync, res;
7515 unsigned long dt, db;
7518 unsigned int per_milli;
7520 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7521 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7522 max_sectors = mddev->resync_max_sectors;
7524 max_sectors = mddev->dev_sectors;
7526 resync = mddev->curr_resync;
7528 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7529 /* Still cleaning up */
7530 resync = max_sectors;
7532 resync -= atomic_read(&mddev->recovery_active);
7535 if (mddev->recovery_cp < MaxSector) {
7536 seq_printf(seq, "\tresync=PENDING");
7542 seq_printf(seq, "\tresync=DELAYED");
7546 WARN_ON(max_sectors == 0);
7547 /* Pick 'scale' such that (resync>>scale)*1000 will fit
7548 * in a sector_t, and (max_sectors>>scale) will fit in a
7549 * u32, as those are the requirements for sector_div.
7550 * Thus 'scale' must be at least 10
7553 if (sizeof(sector_t) > sizeof(unsigned long)) {
7554 while ( max_sectors/2 > (1ULL<<(scale+32)))
7557 res = (resync>>scale)*1000;
7558 sector_div(res, (u32)((max_sectors>>scale)+1));
7562 int i, x = per_milli/50, y = 20-x;
7563 seq_printf(seq, "[");
7564 for (i = 0; i < x; i++)
7565 seq_printf(seq, "=");
7566 seq_printf(seq, ">");
7567 for (i = 0; i < y; i++)
7568 seq_printf(seq, ".");
7569 seq_printf(seq, "] ");
7571 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
7572 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7574 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7576 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7577 "resync" : "recovery"))),
7578 per_milli/10, per_milli % 10,
7579 (unsigned long long) resync/2,
7580 (unsigned long long) max_sectors/2);
7583 * dt: time from mark until now
7584 * db: blocks written from mark until now
7585 * rt: remaining time
7587 * rt is a sector_t, so could be 32bit or 64bit.
7588 * So we divide before multiply in case it is 32bit and close
7590 * We scale the divisor (db) by 32 to avoid losing precision
7591 * near the end of resync when the number of remaining sectors
7593 * We then divide rt by 32 after multiplying by db to compensate.
7594 * The '+1' avoids division by zero if db is very small.
7596 dt = ((jiffies - mddev->resync_mark) / HZ);
7598 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7599 - mddev->resync_mark_cnt;
7601 rt = max_sectors - resync; /* number of remaining sectors */
7602 sector_div(rt, db/32+1);
7606 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7607 ((unsigned long)rt % 60)/6);
7609 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
7613 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7615 struct list_head *tmp;
7617 struct mddev *mddev;
7625 spin_lock(&all_mddevs_lock);
7626 list_for_each(tmp,&all_mddevs)
7628 mddev = list_entry(tmp, struct mddev, all_mddevs);
7630 spin_unlock(&all_mddevs_lock);
7633 spin_unlock(&all_mddevs_lock);
7635 return (void*)2;/* tail */
7639 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7641 struct list_head *tmp;
7642 struct mddev *next_mddev, *mddev = v;
7648 spin_lock(&all_mddevs_lock);
7650 tmp = all_mddevs.next;
7652 tmp = mddev->all_mddevs.next;
7653 if (tmp != &all_mddevs)
7654 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
7656 next_mddev = (void*)2;
7659 spin_unlock(&all_mddevs_lock);
7667 static void md_seq_stop(struct seq_file *seq, void *v)
7669 struct mddev *mddev = v;
7671 if (mddev && v != (void*)1 && v != (void*)2)
7675 static int md_seq_show(struct seq_file *seq, void *v)
7677 struct mddev *mddev = v;
7679 struct md_rdev *rdev;
7681 if (v == (void*)1) {
7682 struct md_personality *pers;
7683 seq_printf(seq, "Personalities : ");
7684 spin_lock(&pers_lock);
7685 list_for_each_entry(pers, &pers_list, list)
7686 seq_printf(seq, "[%s] ", pers->name);
7688 spin_unlock(&pers_lock);
7689 seq_printf(seq, "\n");
7690 seq->poll_event = atomic_read(&md_event_count);
7693 if (v == (void*)2) {
7698 spin_lock(&mddev->lock);
7699 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7700 seq_printf(seq, "%s : %sactive", mdname(mddev),
7701 mddev->pers ? "" : "in");
7704 seq_printf(seq, " (read-only)");
7706 seq_printf(seq, " (auto-read-only)");
7707 seq_printf(seq, " %s", mddev->pers->name);
7712 rdev_for_each_rcu(rdev, mddev) {
7713 char b[BDEVNAME_SIZE];
7714 seq_printf(seq, " %s[%d]",
7715 bdevname(rdev->bdev,b), rdev->desc_nr);
7716 if (test_bit(WriteMostly, &rdev->flags))
7717 seq_printf(seq, "(W)");
7718 if (test_bit(Journal, &rdev->flags))
7719 seq_printf(seq, "(J)");
7720 if (test_bit(Faulty, &rdev->flags)) {
7721 seq_printf(seq, "(F)");
7724 if (rdev->raid_disk < 0)
7725 seq_printf(seq, "(S)"); /* spare */
7726 if (test_bit(Replacement, &rdev->flags))
7727 seq_printf(seq, "(R)");
7728 sectors += rdev->sectors;
7732 if (!list_empty(&mddev->disks)) {
7734 seq_printf(seq, "\n %llu blocks",
7735 (unsigned long long)
7736 mddev->array_sectors / 2);
7738 seq_printf(seq, "\n %llu blocks",
7739 (unsigned long long)sectors / 2);
7741 if (mddev->persistent) {
7742 if (mddev->major_version != 0 ||
7743 mddev->minor_version != 90) {
7744 seq_printf(seq," super %d.%d",
7745 mddev->major_version,
7746 mddev->minor_version);
7748 } else if (mddev->external)
7749 seq_printf(seq, " super external:%s",
7750 mddev->metadata_type);
7752 seq_printf(seq, " super non-persistent");
7755 mddev->pers->status(seq, mddev);
7756 seq_printf(seq, "\n ");
7757 if (mddev->pers->sync_request) {
7758 if (status_resync(seq, mddev))
7759 seq_printf(seq, "\n ");
7762 seq_printf(seq, "\n ");
7764 bitmap_status(seq, mddev->bitmap);
7766 seq_printf(seq, "\n");
7768 spin_unlock(&mddev->lock);
7773 static const struct seq_operations md_seq_ops = {
7774 .start = md_seq_start,
7775 .next = md_seq_next,
7776 .stop = md_seq_stop,
7777 .show = md_seq_show,
7780 static int md_seq_open(struct inode *inode, struct file *file)
7782 struct seq_file *seq;
7785 error = seq_open(file, &md_seq_ops);
7789 seq = file->private_data;
7790 seq->poll_event = atomic_read(&md_event_count);
7794 static int md_unloading;
7795 static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7797 struct seq_file *seq = filp->private_data;
7801 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
7802 poll_wait(filp, &md_event_waiters, wait);
7804 /* always allow read */
7805 mask = POLLIN | POLLRDNORM;
7807 if (seq->poll_event != atomic_read(&md_event_count))
7808 mask |= POLLERR | POLLPRI;
7812 static const struct file_operations md_seq_fops = {
7813 .owner = THIS_MODULE,
7814 .open = md_seq_open,
7816 .llseek = seq_lseek,
7817 .release = seq_release_private,
7818 .poll = mdstat_poll,
7821 int register_md_personality(struct md_personality *p)
7823 pr_debug("md: %s personality registered for level %d\n",
7825 spin_lock(&pers_lock);
7826 list_add_tail(&p->list, &pers_list);
7827 spin_unlock(&pers_lock);
7830 EXPORT_SYMBOL(register_md_personality);
7832 int unregister_md_personality(struct md_personality *p)
7834 pr_debug("md: %s personality unregistered\n", p->name);
7835 spin_lock(&pers_lock);
7836 list_del_init(&p->list);
7837 spin_unlock(&pers_lock);
7840 EXPORT_SYMBOL(unregister_md_personality);
7842 int register_md_cluster_operations(struct md_cluster_operations *ops,
7843 struct module *module)
7846 spin_lock(&pers_lock);
7847 if (md_cluster_ops != NULL)
7850 md_cluster_ops = ops;
7851 md_cluster_mod = module;
7853 spin_unlock(&pers_lock);
7856 EXPORT_SYMBOL(register_md_cluster_operations);
7858 int unregister_md_cluster_operations(void)
7860 spin_lock(&pers_lock);
7861 md_cluster_ops = NULL;
7862 spin_unlock(&pers_lock);
7865 EXPORT_SYMBOL(unregister_md_cluster_operations);
7867 int md_setup_cluster(struct mddev *mddev, int nodes)
7869 if (!md_cluster_ops)
7870 request_module("md-cluster");
7871 spin_lock(&pers_lock);
7872 /* ensure module won't be unloaded */
7873 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
7874 pr_warn("can't find md-cluster module or get it's reference.\n");
7875 spin_unlock(&pers_lock);
7878 spin_unlock(&pers_lock);
7880 return md_cluster_ops->join(mddev, nodes);
7883 void md_cluster_stop(struct mddev *mddev)
7885 if (!md_cluster_ops)
7887 md_cluster_ops->leave(mddev);
7888 module_put(md_cluster_mod);
7891 static int is_mddev_idle(struct mddev *mddev, int init)
7893 struct md_rdev *rdev;
7899 rdev_for_each_rcu(rdev, mddev) {
7900 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
7901 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7902 (int)part_stat_read(&disk->part0, sectors[1]) -
7903 atomic_read(&disk->sync_io);
7904 /* sync IO will cause sync_io to increase before the disk_stats
7905 * as sync_io is counted when a request starts, and
7906 * disk_stats is counted when it completes.
7907 * So resync activity will cause curr_events to be smaller than
7908 * when there was no such activity.
7909 * non-sync IO will cause disk_stat to increase without
7910 * increasing sync_io so curr_events will (eventually)
7911 * be larger than it was before. Once it becomes
7912 * substantially larger, the test below will cause
7913 * the array to appear non-idle, and resync will slow
7915 * If there is a lot of outstanding resync activity when
7916 * we set last_event to curr_events, then all that activity
7917 * completing might cause the array to appear non-idle
7918 * and resync will be slowed down even though there might
7919 * not have been non-resync activity. This will only
7920 * happen once though. 'last_events' will soon reflect
7921 * the state where there is little or no outstanding
7922 * resync requests, and further resync activity will
7923 * always make curr_events less than last_events.
7926 if (init || curr_events - rdev->last_events > 64) {
7927 rdev->last_events = curr_events;
7935 void md_done_sync(struct mddev *mddev, int blocks, int ok)
7937 /* another "blocks" (512byte) blocks have been synced */
7938 atomic_sub(blocks, &mddev->recovery_active);
7939 wake_up(&mddev->recovery_wait);
7941 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7942 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
7943 md_wakeup_thread(mddev->thread);
7944 // stop recovery, signal do_sync ....
7947 EXPORT_SYMBOL(md_done_sync);
7949 /* md_write_start(mddev, bi)
7950 * If we need to update some array metadata (e.g. 'active' flag
7951 * in superblock) before writing, schedule a superblock update
7952 * and wait for it to complete.
7954 void md_write_start(struct mddev *mddev, struct bio *bi)
7957 if (bio_data_dir(bi) != WRITE)
7960 BUG_ON(mddev->ro == 1);
7961 if (mddev->ro == 2) {
7962 /* need to switch to read/write */
7964 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7965 md_wakeup_thread(mddev->thread);
7966 md_wakeup_thread(mddev->sync_thread);
7970 percpu_ref_get(&mddev->writes_pending);
7971 smp_mb(); /* Match smp_mb in set_in_sync() */
7972 if (mddev->safemode == 1)
7973 mddev->safemode = 0;
7974 /* sync_checkers is always 0 when writes_pending is in per-cpu mode */
7975 if (mddev->in_sync || !mddev->sync_checkers) {
7976 spin_lock(&mddev->lock);
7977 if (mddev->in_sync) {
7979 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
7980 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
7981 md_wakeup_thread(mddev->thread);
7984 spin_unlock(&mddev->lock);
7988 sysfs_notify_dirent_safe(mddev->sysfs_state);
7989 wait_event(mddev->sb_wait,
7990 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
7992 EXPORT_SYMBOL(md_write_start);
7994 /* md_write_inc can only be called when md_write_start() has
7995 * already been called at least once of the current request.
7996 * It increments the counter and is useful when a single request
7997 * is split into several parts. Each part causes an increment and
7998 * so needs a matching md_write_end().
7999 * Unlike md_write_start(), it is safe to call md_write_inc() inside
8000 * a spinlocked region.
8002 void md_write_inc(struct mddev *mddev, struct bio *bi)
8004 if (bio_data_dir(bi) != WRITE)
8006 WARN_ON_ONCE(mddev->in_sync || mddev->ro);
8007 percpu_ref_get(&mddev->writes_pending);
8009 EXPORT_SYMBOL(md_write_inc);
8011 void md_write_end(struct mddev *mddev)
8013 percpu_ref_put(&mddev->writes_pending);
8015 if (mddev->safemode == 2)
8016 md_wakeup_thread(mddev->thread);
8017 else if (mddev->safemode_delay)
8018 /* The roundup() ensures this only performs locking once
8019 * every ->safemode_delay jiffies
8021 mod_timer(&mddev->safemode_timer,
8022 roundup(jiffies, mddev->safemode_delay) +
8023 mddev->safemode_delay);
8026 EXPORT_SYMBOL(md_write_end);
8028 /* md_allow_write(mddev)
8029 * Calling this ensures that the array is marked 'active' so that writes
8030 * may proceed without blocking. It is important to call this before
8031 * attempting a GFP_KERNEL allocation while holding the mddev lock.
8032 * Must be called with mddev_lock held.
8034 void md_allow_write(struct mddev *mddev)
8040 if (!mddev->pers->sync_request)
8043 spin_lock(&mddev->lock);
8044 if (mddev->in_sync) {
8046 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8047 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
8048 if (mddev->safemode_delay &&
8049 mddev->safemode == 0)
8050 mddev->safemode = 1;
8051 spin_unlock(&mddev->lock);
8052 md_update_sb(mddev, 0);
8053 sysfs_notify_dirent_safe(mddev->sysfs_state);
8054 /* wait for the dirty state to be recorded in the metadata */
8055 wait_event(mddev->sb_wait,
8056 !test_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags) &&
8057 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
8059 spin_unlock(&mddev->lock);
8061 EXPORT_SYMBOL_GPL(md_allow_write);
8063 #define SYNC_MARKS 10
8064 #define SYNC_MARK_STEP (3*HZ)
8065 #define UPDATE_FREQUENCY (5*60*HZ)
8066 void md_do_sync(struct md_thread *thread)
8068 struct mddev *mddev = thread->mddev;
8069 struct mddev *mddev2;
8070 unsigned int currspeed = 0,
8072 sector_t max_sectors,j, io_sectors, recovery_done;
8073 unsigned long mark[SYNC_MARKS];
8074 unsigned long update_time;
8075 sector_t mark_cnt[SYNC_MARKS];
8077 struct list_head *tmp;
8078 sector_t last_check;
8080 struct md_rdev *rdev;
8081 char *desc, *action = NULL;
8082 struct blk_plug plug;
8085 /* just incase thread restarts... */
8086 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
8088 if (mddev->ro) {/* never try to sync a read-only array */
8089 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
8093 if (mddev_is_clustered(mddev)) {
8094 ret = md_cluster_ops->resync_start(mddev);
8098 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
8099 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8100 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
8101 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
8102 && ((unsigned long long)mddev->curr_resync_completed
8103 < (unsigned long long)mddev->resync_max_sectors))
8107 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8108 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
8109 desc = "data-check";
8111 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8112 desc = "requested-resync";
8116 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
8121 mddev->last_sync_action = action ?: desc;
8123 /* we overload curr_resync somewhat here.
8124 * 0 == not engaged in resync at all
8125 * 2 == checking that there is no conflict with another sync
8126 * 1 == like 2, but have yielded to allow conflicting resync to
8128 * other == active in resync - this many blocks
8130 * Before starting a resync we must have set curr_resync to
8131 * 2, and then checked that every "conflicting" array has curr_resync
8132 * less than ours. When we find one that is the same or higher
8133 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8134 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8135 * This will mean we have to start checking from the beginning again.
8140 int mddev2_minor = -1;
8141 mddev->curr_resync = 2;
8144 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8146 for_each_mddev(mddev2, tmp) {
8147 if (mddev2 == mddev)
8149 if (!mddev->parallel_resync
8150 && mddev2->curr_resync
8151 && match_mddev_units(mddev, mddev2)) {
8153 if (mddev < mddev2 && mddev->curr_resync == 2) {
8154 /* arbitrarily yield */
8155 mddev->curr_resync = 1;
8156 wake_up(&resync_wait);
8158 if (mddev > mddev2 && mddev->curr_resync == 1)
8159 /* no need to wait here, we can wait the next
8160 * time 'round when curr_resync == 2
8163 /* We need to wait 'interruptible' so as not to
8164 * contribute to the load average, and not to
8165 * be caught by 'softlockup'
8167 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
8168 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8169 mddev2->curr_resync >= mddev->curr_resync) {
8170 if (mddev2_minor != mddev2->md_minor) {
8171 mddev2_minor = mddev2->md_minor;
8172 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8173 desc, mdname(mddev),
8177 if (signal_pending(current))
8178 flush_signals(current);
8180 finish_wait(&resync_wait, &wq);
8183 finish_wait(&resync_wait, &wq);
8186 } while (mddev->curr_resync < 2);
8189 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8190 /* resync follows the size requested by the personality,
8191 * which defaults to physical size, but can be virtual size
8193 max_sectors = mddev->resync_max_sectors;
8194 atomic64_set(&mddev->resync_mismatches, 0);
8195 /* we don't use the checkpoint if there's a bitmap */
8196 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8197 j = mddev->resync_min;
8198 else if (!mddev->bitmap)
8199 j = mddev->recovery_cp;
8201 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
8202 max_sectors = mddev->resync_max_sectors;
8204 /* recovery follows the physical size of devices */
8205 max_sectors = mddev->dev_sectors;
8208 rdev_for_each_rcu(rdev, mddev)
8209 if (rdev->raid_disk >= 0 &&
8210 !test_bit(Journal, &rdev->flags) &&
8211 !test_bit(Faulty, &rdev->flags) &&
8212 !test_bit(In_sync, &rdev->flags) &&
8213 rdev->recovery_offset < j)
8214 j = rdev->recovery_offset;
8217 /* If there is a bitmap, we need to make sure all
8218 * writes that started before we added a spare
8219 * complete before we start doing a recovery.
8220 * Otherwise the write might complete and (via
8221 * bitmap_endwrite) set a bit in the bitmap after the
8222 * recovery has checked that bit and skipped that
8225 if (mddev->bitmap) {
8226 mddev->pers->quiesce(mddev, 1);
8227 mddev->pers->quiesce(mddev, 0);
8231 pr_info("md: %s of RAID array %s\n", desc, mdname(mddev));
8232 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev));
8233 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8234 speed_max(mddev), desc);
8236 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
8239 for (m = 0; m < SYNC_MARKS; m++) {
8241 mark_cnt[m] = io_sectors;
8244 mddev->resync_mark = mark[last_mark];
8245 mddev->resync_mark_cnt = mark_cnt[last_mark];
8248 * Tune reconstruction:
8250 window = 32*(PAGE_SIZE/512);
8251 pr_debug("md: using %dk window, over a total of %lluk.\n",
8252 window/2, (unsigned long long)max_sectors/2);
8254 atomic_set(&mddev->recovery_active, 0);
8258 pr_debug("md: resuming %s of %s from checkpoint.\n",
8259 desc, mdname(mddev));
8260 mddev->curr_resync = j;
8262 mddev->curr_resync = 3; /* no longer delayed */
8263 mddev->curr_resync_completed = j;
8264 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8265 md_new_event(mddev);
8266 update_time = jiffies;
8268 blk_start_plug(&plug);
8269 while (j < max_sectors) {
8274 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8275 ((mddev->curr_resync > mddev->curr_resync_completed &&
8276 (mddev->curr_resync - mddev->curr_resync_completed)
8277 > (max_sectors >> 4)) ||
8278 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
8279 (j - mddev->curr_resync_completed)*2
8280 >= mddev->resync_max - mddev->curr_resync_completed ||
8281 mddev->curr_resync_completed > mddev->resync_max
8283 /* time to update curr_resync_completed */
8284 wait_event(mddev->recovery_wait,
8285 atomic_read(&mddev->recovery_active) == 0);
8286 mddev->curr_resync_completed = j;
8287 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8288 j > mddev->recovery_cp)
8289 mddev->recovery_cp = j;
8290 update_time = jiffies;
8291 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8292 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8295 while (j >= mddev->resync_max &&
8296 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8297 /* As this condition is controlled by user-space,
8298 * we can block indefinitely, so use '_interruptible'
8299 * to avoid triggering warnings.
8301 flush_signals(current); /* just in case */
8302 wait_event_interruptible(mddev->recovery_wait,
8303 mddev->resync_max > j
8304 || test_bit(MD_RECOVERY_INTR,
8308 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8311 sectors = mddev->pers->sync_request(mddev, j, &skipped);
8313 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
8317 if (!skipped) { /* actual IO requested */
8318 io_sectors += sectors;
8319 atomic_add(sectors, &mddev->recovery_active);
8322 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8326 if (j > max_sectors)
8327 /* when skipping, extra large numbers can be returned. */
8330 mddev->curr_resync = j;
8331 mddev->curr_mark_cnt = io_sectors;
8332 if (last_check == 0)
8333 /* this is the earliest that rebuild will be
8334 * visible in /proc/mdstat
8336 md_new_event(mddev);
8338 if (last_check + window > io_sectors || j == max_sectors)
8341 last_check = io_sectors;
8343 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8345 int next = (last_mark+1) % SYNC_MARKS;
8347 mddev->resync_mark = mark[next];
8348 mddev->resync_mark_cnt = mark_cnt[next];
8349 mark[next] = jiffies;
8350 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
8354 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8358 * this loop exits only if either when we are slower than
8359 * the 'hard' speed limit, or the system was IO-idle for
8361 * the system might be non-idle CPU-wise, but we only care
8362 * about not overloading the IO subsystem. (things like an
8363 * e2fsck being done on the RAID array should execute fast)
8367 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8368 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
8369 /((jiffies-mddev->resync_mark)/HZ +1) +1;
8371 if (currspeed > speed_min(mddev)) {
8372 if (currspeed > speed_max(mddev)) {
8376 if (!is_mddev_idle(mddev, 0)) {
8378 * Give other IO more of a chance.
8379 * The faster the devices, the less we wait.
8381 wait_event(mddev->recovery_wait,
8382 !atomic_read(&mddev->recovery_active));
8386 pr_info("md: %s: %s %s.\n",mdname(mddev), desc,
8387 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8388 ? "interrupted" : "done");
8390 * this also signals 'finished resyncing' to md_stop
8392 blk_finish_plug(&plug);
8393 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8395 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8396 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8397 mddev->curr_resync > 3) {
8398 mddev->curr_resync_completed = mddev->curr_resync;
8399 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8401 mddev->pers->sync_request(mddev, max_sectors, &skipped);
8403 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
8404 mddev->curr_resync > 3) {
8405 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8406 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8407 if (mddev->curr_resync >= mddev->recovery_cp) {
8408 pr_debug("md: checkpointing %s of %s.\n",
8409 desc, mdname(mddev));
8410 if (test_bit(MD_RECOVERY_ERROR,
8412 mddev->recovery_cp =
8413 mddev->curr_resync_completed;
8415 mddev->recovery_cp =
8419 mddev->recovery_cp = MaxSector;
8421 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8422 mddev->curr_resync = MaxSector;
8424 rdev_for_each_rcu(rdev, mddev)
8425 if (rdev->raid_disk >= 0 &&
8426 mddev->delta_disks >= 0 &&
8427 !test_bit(Journal, &rdev->flags) &&
8428 !test_bit(Faulty, &rdev->flags) &&
8429 !test_bit(In_sync, &rdev->flags) &&
8430 rdev->recovery_offset < mddev->curr_resync)
8431 rdev->recovery_offset = mddev->curr_resync;
8436 /* set CHANGE_PENDING here since maybe another update is needed,
8437 * so other nodes are informed. It should be harmless for normal
8439 set_mask_bits(&mddev->sb_flags, 0,
8440 BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS));
8442 spin_lock(&mddev->lock);
8443 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8444 /* We completed so min/max setting can be forgotten if used. */
8445 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8446 mddev->resync_min = 0;
8447 mddev->resync_max = MaxSector;
8448 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8449 mddev->resync_min = mddev->curr_resync_completed;
8450 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
8451 mddev->curr_resync = 0;
8452 spin_unlock(&mddev->lock);
8454 wake_up(&resync_wait);
8455 md_wakeup_thread(mddev->thread);
8458 EXPORT_SYMBOL_GPL(md_do_sync);
8460 static int remove_and_add_spares(struct mddev *mddev,
8461 struct md_rdev *this)
8463 struct md_rdev *rdev;
8466 bool remove_some = false;
8468 rdev_for_each(rdev, mddev) {
8469 if ((this == NULL || rdev == this) &&
8470 rdev->raid_disk >= 0 &&
8471 !test_bit(Blocked, &rdev->flags) &&
8472 test_bit(Faulty, &rdev->flags) &&
8473 atomic_read(&rdev->nr_pending)==0) {
8474 /* Faulty non-Blocked devices with nr_pending == 0
8475 * never get nr_pending incremented,
8476 * never get Faulty cleared, and never get Blocked set.
8477 * So we can synchronize_rcu now rather than once per device
8480 set_bit(RemoveSynchronized, &rdev->flags);
8486 rdev_for_each(rdev, mddev) {
8487 if ((this == NULL || rdev == this) &&
8488 rdev->raid_disk >= 0 &&
8489 !test_bit(Blocked, &rdev->flags) &&
8490 ((test_bit(RemoveSynchronized, &rdev->flags) ||
8491 (!test_bit(In_sync, &rdev->flags) &&
8492 !test_bit(Journal, &rdev->flags))) &&
8493 atomic_read(&rdev->nr_pending)==0)) {
8494 if (mddev->pers->hot_remove_disk(
8495 mddev, rdev) == 0) {
8496 sysfs_unlink_rdev(mddev, rdev);
8497 rdev->raid_disk = -1;
8501 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
8502 clear_bit(RemoveSynchronized, &rdev->flags);
8505 if (removed && mddev->kobj.sd)
8506 sysfs_notify(&mddev->kobj, NULL, "degraded");
8508 if (this && removed)
8511 rdev_for_each(rdev, mddev) {
8512 if (this && this != rdev)
8514 if (test_bit(Candidate, &rdev->flags))
8516 if (rdev->raid_disk >= 0 &&
8517 !test_bit(In_sync, &rdev->flags) &&
8518 !test_bit(Journal, &rdev->flags) &&
8519 !test_bit(Faulty, &rdev->flags))
8521 if (rdev->raid_disk >= 0)
8523 if (test_bit(Faulty, &rdev->flags))
8525 if (!test_bit(Journal, &rdev->flags)) {
8527 ! (rdev->saved_raid_disk >= 0 &&
8528 !test_bit(Bitmap_sync, &rdev->flags)))
8531 rdev->recovery_offset = 0;
8534 hot_add_disk(mddev, rdev) == 0) {
8535 if (sysfs_link_rdev(mddev, rdev))
8536 /* failure here is OK */;
8537 if (!test_bit(Journal, &rdev->flags))
8539 md_new_event(mddev);
8540 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
8545 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
8549 static void md_start_sync(struct work_struct *ws)
8551 struct mddev *mddev = container_of(ws, struct mddev, del_work);
8553 mddev->sync_thread = md_register_thread(md_do_sync,
8556 if (!mddev->sync_thread) {
8557 pr_warn("%s: could not start resync thread...\n",
8559 /* leave the spares where they are, it shouldn't hurt */
8560 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8561 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8562 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8563 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8564 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
8565 wake_up(&resync_wait);
8566 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8568 if (mddev->sysfs_action)
8569 sysfs_notify_dirent_safe(mddev->sysfs_action);
8571 md_wakeup_thread(mddev->sync_thread);
8572 sysfs_notify_dirent_safe(mddev->sysfs_action);
8573 md_new_event(mddev);
8577 * This routine is regularly called by all per-raid-array threads to
8578 * deal with generic issues like resync and super-block update.
8579 * Raid personalities that don't have a thread (linear/raid0) do not
8580 * need this as they never do any recovery or update the superblock.
8582 * It does not do any resync itself, but rather "forks" off other threads
8583 * to do that as needed.
8584 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8585 * "->recovery" and create a thread at ->sync_thread.
8586 * When the thread finishes it sets MD_RECOVERY_DONE
8587 * and wakeups up this thread which will reap the thread and finish up.
8588 * This thread also removes any faulty devices (with nr_pending == 0).
8590 * The overall approach is:
8591 * 1/ if the superblock needs updating, update it.
8592 * 2/ If a recovery thread is running, don't do anything else.
8593 * 3/ If recovery has finished, clean up, possibly marking spares active.
8594 * 4/ If there are any faulty devices, remove them.
8595 * 5/ If array is degraded, try to add spares devices
8596 * 6/ If array has spares or is not in-sync, start a resync thread.
8598 void md_check_recovery(struct mddev *mddev)
8600 if (mddev->suspended)
8604 bitmap_daemon_work(mddev);
8606 if (signal_pending(current)) {
8607 if (mddev->pers->sync_request && !mddev->external) {
8608 pr_debug("md: %s in immediate safe mode\n",
8610 mddev->safemode = 2;
8612 flush_signals(current);
8615 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8618 (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
8619 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8620 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
8621 (mddev->external == 0 && mddev->safemode == 1) ||
8622 (mddev->safemode == 2
8623 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
8627 if (mddev_trylock(mddev)) {
8631 struct md_rdev *rdev;
8632 if (!mddev->external && mddev->in_sync)
8633 /* 'Blocked' flag not needed as failed devices
8634 * will be recorded if array switched to read/write.
8635 * Leaving it set will prevent the device
8636 * from being removed.
8638 rdev_for_each(rdev, mddev)
8639 clear_bit(Blocked, &rdev->flags);
8640 /* On a read-only array we can:
8641 * - remove failed devices
8642 * - add already-in_sync devices if the array itself
8644 * As we only add devices that are already in-sync,
8645 * we can activate the spares immediately.
8647 remove_and_add_spares(mddev, NULL);
8648 /* There is no thread, but we need to call
8649 * ->spare_active and clear saved_raid_disk
8651 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
8652 md_reap_sync_thread(mddev);
8653 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
8654 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8655 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
8659 if (mddev_is_clustered(mddev)) {
8660 struct md_rdev *rdev;
8661 /* kick the device if another node issued a
8664 rdev_for_each(rdev, mddev) {
8665 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
8666 rdev->raid_disk < 0)
8667 md_kick_rdev_from_array(rdev);
8671 if (!mddev->external && !mddev->in_sync) {
8672 spin_lock(&mddev->lock);
8674 spin_unlock(&mddev->lock);
8677 if (mddev->sb_flags)
8678 md_update_sb(mddev, 0);
8680 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8681 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8682 /* resync/recovery still happening */
8683 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8686 if (mddev->sync_thread) {
8687 md_reap_sync_thread(mddev);
8690 /* Set RUNNING before clearing NEEDED to avoid
8691 * any transients in the value of "sync_action".
8693 mddev->curr_resync_completed = 0;
8694 spin_lock(&mddev->lock);
8695 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
8696 spin_unlock(&mddev->lock);
8697 /* Clear some bits that don't mean anything, but
8700 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8701 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
8703 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8704 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
8706 /* no recovery is running.
8707 * remove any failed drives, then
8708 * add spares if possible.
8709 * Spares are also removed and re-added, to allow
8710 * the personality to fail the re-add.
8713 if (mddev->reshape_position != MaxSector) {
8714 if (mddev->pers->check_reshape == NULL ||
8715 mddev->pers->check_reshape(mddev) != 0)
8716 /* Cannot proceed */
8718 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8719 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
8720 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
8721 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8722 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8723 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8724 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
8725 } else if (mddev->recovery_cp < MaxSector) {
8726 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8727 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
8728 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8729 /* nothing to be done ... */
8732 if (mddev->pers->sync_request) {
8734 /* We are adding a device or devices to an array
8735 * which has the bitmap stored on all devices.
8736 * So make sure all bitmap pages get written
8738 bitmap_write_all(mddev->bitmap);
8740 INIT_WORK(&mddev->del_work, md_start_sync);
8741 queue_work(md_misc_wq, &mddev->del_work);
8745 if (!mddev->sync_thread) {
8746 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
8747 wake_up(&resync_wait);
8748 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8750 if (mddev->sysfs_action)
8751 sysfs_notify_dirent_safe(mddev->sysfs_action);
8754 wake_up(&mddev->sb_wait);
8755 mddev_unlock(mddev);
8758 EXPORT_SYMBOL(md_check_recovery);
8760 void md_reap_sync_thread(struct mddev *mddev)
8762 struct md_rdev *rdev;
8764 /* resync has finished, collect result */
8765 md_unregister_thread(&mddev->sync_thread);
8766 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8767 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8769 /* activate any spares */
8770 if (mddev->pers->spare_active(mddev)) {
8771 sysfs_notify(&mddev->kobj, NULL,
8773 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
8776 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8777 mddev->pers->finish_reshape)
8778 mddev->pers->finish_reshape(mddev);
8780 /* If array is no-longer degraded, then any saved_raid_disk
8781 * information must be scrapped.
8783 if (!mddev->degraded)
8784 rdev_for_each(rdev, mddev)
8785 rdev->saved_raid_disk = -1;
8787 md_update_sb(mddev, 1);
8788 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
8789 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
8791 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
8792 md_cluster_ops->resync_finish(mddev);
8793 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
8794 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
8795 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8796 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8797 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8798 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8799 wake_up(&resync_wait);
8800 /* flag recovery needed just to double check */
8801 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8802 sysfs_notify_dirent_safe(mddev->sysfs_action);
8803 md_new_event(mddev);
8804 if (mddev->event_work.func)
8805 queue_work(md_misc_wq, &mddev->event_work);
8807 EXPORT_SYMBOL(md_reap_sync_thread);
8809 void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
8811 sysfs_notify_dirent_safe(rdev->sysfs_state);
8812 wait_event_timeout(rdev->blocked_wait,
8813 !test_bit(Blocked, &rdev->flags) &&
8814 !test_bit(BlockedBadBlocks, &rdev->flags),
8815 msecs_to_jiffies(5000));
8816 rdev_dec_pending(rdev, mddev);
8818 EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8820 void md_finish_reshape(struct mddev *mddev)
8822 /* called be personality module when reshape completes. */
8823 struct md_rdev *rdev;
8825 rdev_for_each(rdev, mddev) {
8826 if (rdev->data_offset > rdev->new_data_offset)
8827 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8829 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8830 rdev->data_offset = rdev->new_data_offset;
8833 EXPORT_SYMBOL(md_finish_reshape);
8835 /* Bad block management */
8837 /* Returns 1 on success, 0 on failure */
8838 int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8841 struct mddev *mddev = rdev->mddev;
8844 s += rdev->new_data_offset;
8846 s += rdev->data_offset;
8847 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
8849 /* Make sure they get written out promptly */
8850 if (test_bit(ExternalBbl, &rdev->flags))
8851 sysfs_notify(&rdev->kobj, NULL,
8852 "unacknowledged_bad_blocks");
8853 sysfs_notify_dirent_safe(rdev->sysfs_state);
8854 set_mask_bits(&mddev->sb_flags, 0,
8855 BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING));
8856 md_wakeup_thread(rdev->mddev->thread);
8861 EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8863 int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8868 s += rdev->new_data_offset;
8870 s += rdev->data_offset;
8871 rv = badblocks_clear(&rdev->badblocks, s, sectors);
8872 if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags))
8873 sysfs_notify(&rdev->kobj, NULL, "bad_blocks");
8876 EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8878 static int md_notify_reboot(struct notifier_block *this,
8879 unsigned long code, void *x)
8881 struct list_head *tmp;
8882 struct mddev *mddev;
8885 for_each_mddev(mddev, tmp) {
8886 if (mddev_trylock(mddev)) {
8888 __md_stop_writes(mddev);
8889 if (mddev->persistent)
8890 mddev->safemode = 2;
8891 mddev_unlock(mddev);
8896 * certain more exotic SCSI devices are known to be
8897 * volatile wrt too early system reboots. While the
8898 * right place to handle this issue is the given
8899 * driver, we do want to have a safe RAID driver ...
8907 static struct notifier_block md_notifier = {
8908 .notifier_call = md_notify_reboot,
8910 .priority = INT_MAX, /* before any real devices */
8913 static void md_geninit(void)
8915 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
8917 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
8920 static int __init md_init(void)
8924 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
8928 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8932 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8935 if ((ret = register_blkdev(0, "mdp")) < 0)
8939 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
8940 md_probe, NULL, NULL);
8941 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
8942 md_probe, NULL, NULL);
8944 register_reboot_notifier(&md_notifier);
8945 raid_table_header = register_sysctl_table(raid_root_table);
8951 unregister_blkdev(MD_MAJOR, "md");
8953 destroy_workqueue(md_misc_wq);
8955 destroy_workqueue(md_wq);
8960 static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
8962 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
8963 struct md_rdev *rdev2;
8965 char b[BDEVNAME_SIZE];
8968 * If size is changed in another node then we need to
8969 * do resize as well.
8971 if (mddev->dev_sectors != le64_to_cpu(sb->size)) {
8972 ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size));
8974 pr_info("md-cluster: resize failed\n");
8976 bitmap_update_sb(mddev->bitmap);
8979 /* Check for change of roles in the active devices */
8980 rdev_for_each(rdev2, mddev) {
8981 if (test_bit(Faulty, &rdev2->flags))
8984 /* Check if the roles changed */
8985 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
8987 if (test_bit(Candidate, &rdev2->flags)) {
8988 if (role == 0xfffe) {
8989 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
8990 md_kick_rdev_from_array(rdev2);
8994 clear_bit(Candidate, &rdev2->flags);
8997 if (role != rdev2->raid_disk) {
8999 if (rdev2->raid_disk == -1 && role != 0xffff) {
9000 rdev2->saved_raid_disk = role;
9001 ret = remove_and_add_spares(mddev, rdev2);
9002 pr_info("Activated spare: %s\n",
9003 bdevname(rdev2->bdev,b));
9004 /* wakeup mddev->thread here, so array could
9005 * perform resync with the new activated disk */
9006 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9007 md_wakeup_thread(mddev->thread);
9011 * We just want to do the minimum to mark the disk
9012 * as faulty. The recovery is performed by the
9013 * one who initiated the error.
9015 if ((role == 0xfffe) || (role == 0xfffd)) {
9016 md_error(mddev, rdev2);
9017 clear_bit(Blocked, &rdev2->flags);
9022 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
9023 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
9025 /* Finally set the event to be up to date */
9026 mddev->events = le64_to_cpu(sb->events);
9029 static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
9032 struct page *swapout = rdev->sb_page;
9033 struct mdp_superblock_1 *sb;
9035 /* Store the sb page of the rdev in the swapout temporary
9036 * variable in case we err in the future
9038 rdev->sb_page = NULL;
9039 err = alloc_disk_sb(rdev);
9041 ClearPageUptodate(rdev->sb_page);
9042 rdev->sb_loaded = 0;
9043 err = super_types[mddev->major_version].
9044 load_super(rdev, NULL, mddev->minor_version);
9047 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9048 __func__, __LINE__, rdev->desc_nr, err);
9050 put_page(rdev->sb_page);
9051 rdev->sb_page = swapout;
9052 rdev->sb_loaded = 1;
9056 sb = page_address(rdev->sb_page);
9057 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9061 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
9062 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
9064 /* The other node finished recovery, call spare_active to set
9065 * device In_sync and mddev->degraded
9067 if (rdev->recovery_offset == MaxSector &&
9068 !test_bit(In_sync, &rdev->flags) &&
9069 mddev->pers->spare_active(mddev))
9070 sysfs_notify(&mddev->kobj, NULL, "degraded");
9076 void md_reload_sb(struct mddev *mddev, int nr)
9078 struct md_rdev *rdev;
9082 rdev_for_each_rcu(rdev, mddev) {
9083 if (rdev->desc_nr == nr)
9087 if (!rdev || rdev->desc_nr != nr) {
9088 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
9092 err = read_rdev(mddev, rdev);
9096 check_sb_changes(mddev, rdev);
9098 /* Read all rdev's to update recovery_offset */
9099 rdev_for_each_rcu(rdev, mddev)
9100 read_rdev(mddev, rdev);
9102 EXPORT_SYMBOL(md_reload_sb);
9107 * Searches all registered partitions for autorun RAID arrays
9111 static DEFINE_MUTEX(detected_devices_mutex);
9112 static LIST_HEAD(all_detected_devices);
9113 struct detected_devices_node {
9114 struct list_head list;
9118 void md_autodetect_dev(dev_t dev)
9120 struct detected_devices_node *node_detected_dev;
9122 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
9123 if (node_detected_dev) {
9124 node_detected_dev->dev = dev;
9125 mutex_lock(&detected_devices_mutex);
9126 list_add_tail(&node_detected_dev->list, &all_detected_devices);
9127 mutex_unlock(&detected_devices_mutex);
9131 static void autostart_arrays(int part)
9133 struct md_rdev *rdev;
9134 struct detected_devices_node *node_detected_dev;
9136 int i_scanned, i_passed;
9141 pr_info("md: Autodetecting RAID arrays.\n");
9143 mutex_lock(&detected_devices_mutex);
9144 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
9146 node_detected_dev = list_entry(all_detected_devices.next,
9147 struct detected_devices_node, list);
9148 list_del(&node_detected_dev->list);
9149 dev = node_detected_dev->dev;
9150 kfree(node_detected_dev);
9151 mutex_unlock(&detected_devices_mutex);
9152 rdev = md_import_device(dev,0, 90);
9153 mutex_lock(&detected_devices_mutex);
9157 if (test_bit(Faulty, &rdev->flags))
9160 set_bit(AutoDetected, &rdev->flags);
9161 list_add(&rdev->same_set, &pending_raid_disks);
9164 mutex_unlock(&detected_devices_mutex);
9166 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed);
9168 autorun_devices(part);
9171 #endif /* !MODULE */
9173 static __exit void md_exit(void)
9175 struct mddev *mddev;
9176 struct list_head *tmp;
9179 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
9180 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
9182 unregister_blkdev(MD_MAJOR,"md");
9183 unregister_blkdev(mdp_major, "mdp");
9184 unregister_reboot_notifier(&md_notifier);
9185 unregister_sysctl_table(raid_table_header);
9187 /* We cannot unload the modules while some process is
9188 * waiting for us in select() or poll() - wake them up
9191 while (waitqueue_active(&md_event_waiters)) {
9192 /* not safe to leave yet */
9193 wake_up(&md_event_waiters);
9197 remove_proc_entry("mdstat", NULL);
9199 for_each_mddev(mddev, tmp) {
9200 export_array(mddev);
9202 mddev->hold_active = 0;
9204 * for_each_mddev() will call mddev_put() at the end of each
9205 * iteration. As the mddev is now fully clear, this will
9206 * schedule the mddev for destruction by a workqueue, and the
9207 * destroy_workqueue() below will wait for that to complete.
9210 destroy_workqueue(md_misc_wq);
9211 destroy_workqueue(md_wq);
9214 subsys_initcall(md_init);
9215 module_exit(md_exit)
9217 static int get_ro(char *buffer, struct kernel_param *kp)
9219 return sprintf(buffer, "%d", start_readonly);
9221 static int set_ro(const char *val, struct kernel_param *kp)
9223 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
9226 module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9227 module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
9228 module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
9229 module_param(create_on_open, bool, S_IRUSR|S_IWUSR);
9231 MODULE_LICENSE("GPL");
9232 MODULE_DESCRIPTION("MD RAID framework");
9234 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);