net: ipv6: Release route when device is unregistering
[linux/fpc-iii.git] / drivers / md / md.c
blob906a4bf600ac67fe0e48c498898fb8c9666b5958
1 /*
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
7 Changes:
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)
28 any later version.
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.
35 Please use:
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>
53 #include <linux/fs.h>
54 #include <linux/poll.h>
55 #include <linux/ctype.h>
56 #include <linux/string.h>
57 #include <linux/hdreg.h>
58 #include <linux/proc_fs.h>
59 #include <linux/random.h>
60 #include <linux/module.h>
61 #include <linux/reboot.h>
62 #include <linux/file.h>
63 #include <linux/compat.h>
64 #include <linux/delay.h>
65 #include <linux/raid/md_p.h>
66 #include <linux/raid/md_u.h>
67 #include <linux/slab.h>
68 #include <trace/events/block.h>
69 #include "md.h"
70 #include "bitmap.h"
71 #include "md-cluster.h"
73 #ifndef MODULE
74 static void autostart_arrays(int part);
75 #endif
77 /* pers_list is a list of registered personalities protected
78 * by pers_lock.
79 * pers_lock does extra service to protect accesses to
80 * mddev->thread when the mutex cannot be held.
82 static LIST_HEAD(pers_list);
83 static DEFINE_SPINLOCK(pers_lock);
85 struct md_cluster_operations *md_cluster_ops;
86 EXPORT_SYMBOL(md_cluster_ops);
87 struct module *md_cluster_mod;
88 EXPORT_SYMBOL(md_cluster_mod);
90 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
91 static struct workqueue_struct *md_wq;
92 static struct workqueue_struct *md_misc_wq;
94 static int remove_and_add_spares(struct mddev *mddev,
95 struct md_rdev *this);
96 static void mddev_detach(struct mddev *mddev);
99 * Default number of read corrections we'll attempt on an rdev
100 * before ejecting it from the array. We divide the read error
101 * count by 2 for every hour elapsed between read errors.
103 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
105 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
106 * is 1000 KB/sec, so the extra system load does not show up that much.
107 * Increase it if you want to have more _guaranteed_ speed. Note that
108 * the RAID driver will use the maximum available bandwidth if the IO
109 * subsystem is idle. There is also an 'absolute maximum' reconstruction
110 * speed limit - in case reconstruction slows down your system despite
111 * idle IO detection.
113 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
114 * or /sys/block/mdX/md/sync_speed_{min,max}
117 static int sysctl_speed_limit_min = 1000;
118 static int sysctl_speed_limit_max = 200000;
119 static inline int speed_min(struct mddev *mddev)
121 return mddev->sync_speed_min ?
122 mddev->sync_speed_min : sysctl_speed_limit_min;
125 static inline int speed_max(struct mddev *mddev)
127 return mddev->sync_speed_max ?
128 mddev->sync_speed_max : sysctl_speed_limit_max;
131 static struct ctl_table_header *raid_table_header;
133 static struct ctl_table raid_table[] = {
135 .procname = "speed_limit_min",
136 .data = &sysctl_speed_limit_min,
137 .maxlen = sizeof(int),
138 .mode = S_IRUGO|S_IWUSR,
139 .proc_handler = proc_dointvec,
142 .procname = "speed_limit_max",
143 .data = &sysctl_speed_limit_max,
144 .maxlen = sizeof(int),
145 .mode = S_IRUGO|S_IWUSR,
146 .proc_handler = proc_dointvec,
151 static struct ctl_table raid_dir_table[] = {
153 .procname = "raid",
154 .maxlen = 0,
155 .mode = S_IRUGO|S_IXUGO,
156 .child = raid_table,
161 static struct ctl_table raid_root_table[] = {
163 .procname = "dev",
164 .maxlen = 0,
165 .mode = 0555,
166 .child = raid_dir_table,
171 static const struct block_device_operations md_fops;
173 static int start_readonly;
175 /* bio_clone_mddev
176 * like bio_clone, but with a local bio set
179 struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
180 struct mddev *mddev)
182 struct bio *b;
184 if (!mddev || !mddev->bio_set)
185 return bio_alloc(gfp_mask, nr_iovecs);
187 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
188 if (!b)
189 return NULL;
190 return b;
192 EXPORT_SYMBOL_GPL(bio_alloc_mddev);
195 * We have a system wide 'event count' that is incremented
196 * on any 'interesting' event, and readers of /proc/mdstat
197 * can use 'poll' or 'select' to find out when the event
198 * count increases.
200 * Events are:
201 * start array, stop array, error, add device, remove device,
202 * start build, activate spare
204 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
205 static atomic_t md_event_count;
206 void md_new_event(struct mddev *mddev)
208 atomic_inc(&md_event_count);
209 wake_up(&md_event_waiters);
211 EXPORT_SYMBOL_GPL(md_new_event);
214 * Enables to iterate over all existing md arrays
215 * all_mddevs_lock protects this list.
217 static LIST_HEAD(all_mddevs);
218 static DEFINE_SPINLOCK(all_mddevs_lock);
221 * iterates through all used mddevs in the system.
222 * We take care to grab the all_mddevs_lock whenever navigating
223 * the list, and to always hold a refcount when unlocked.
224 * Any code which breaks out of this loop while own
225 * a reference to the current mddev and must mddev_put it.
227 #define for_each_mddev(_mddev,_tmp) \
229 for (({ spin_lock(&all_mddevs_lock); \
230 _tmp = all_mddevs.next; \
231 _mddev = NULL;}); \
232 ({ if (_tmp != &all_mddevs) \
233 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
234 spin_unlock(&all_mddevs_lock); \
235 if (_mddev) mddev_put(_mddev); \
236 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
237 _tmp != &all_mddevs;}); \
238 ({ spin_lock(&all_mddevs_lock); \
239 _tmp = _tmp->next;}) \
242 /* Rather than calling directly into the personality make_request function,
243 * IO requests come here first so that we can check if the device is
244 * being suspended pending a reconfiguration.
245 * We hold a refcount over the call to ->make_request. By the time that
246 * call has finished, the bio has been linked into some internal structure
247 * and so is visible to ->quiesce(), so we don't need the refcount any more.
249 static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio)
251 const int rw = bio_data_dir(bio);
252 struct mddev *mddev = q->queuedata;
253 unsigned int sectors;
254 int cpu;
256 blk_queue_split(q, &bio, q->bio_split);
258 if (mddev == NULL || mddev->pers == NULL) {
259 bio_io_error(bio);
260 return BLK_QC_T_NONE;
262 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
263 if (bio_sectors(bio) != 0)
264 bio->bi_error = -EROFS;
265 bio_endio(bio);
266 return BLK_QC_T_NONE;
268 smp_rmb(); /* Ensure implications of 'active' are visible */
269 rcu_read_lock();
270 if (mddev->suspended) {
271 DEFINE_WAIT(__wait);
272 for (;;) {
273 prepare_to_wait(&mddev->sb_wait, &__wait,
274 TASK_UNINTERRUPTIBLE);
275 if (!mddev->suspended)
276 break;
277 rcu_read_unlock();
278 schedule();
279 rcu_read_lock();
281 finish_wait(&mddev->sb_wait, &__wait);
283 atomic_inc(&mddev->active_io);
284 rcu_read_unlock();
287 * save the sectors now since our bio can
288 * go away inside make_request
290 sectors = bio_sectors(bio);
291 /* bio could be mergeable after passing to underlayer */
292 bio->bi_opf &= ~REQ_NOMERGE;
293 mddev->pers->make_request(mddev, bio);
295 cpu = part_stat_lock();
296 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
297 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
298 part_stat_unlock();
300 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
301 wake_up(&mddev->sb_wait);
303 return BLK_QC_T_NONE;
306 /* mddev_suspend makes sure no new requests are submitted
307 * to the device, and that any requests that have been submitted
308 * are completely handled.
309 * Once mddev_detach() is called and completes, the module will be
310 * completely unused.
312 void mddev_suspend(struct mddev *mddev)
314 WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk);
315 if (mddev->suspended++)
316 return;
317 synchronize_rcu();
318 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
319 mddev->pers->quiesce(mddev, 1);
321 del_timer_sync(&mddev->safemode_timer);
323 EXPORT_SYMBOL_GPL(mddev_suspend);
325 void mddev_resume(struct mddev *mddev)
327 if (--mddev->suspended)
328 return;
329 wake_up(&mddev->sb_wait);
330 mddev->pers->quiesce(mddev, 0);
332 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
333 md_wakeup_thread(mddev->thread);
334 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
336 EXPORT_SYMBOL_GPL(mddev_resume);
338 int mddev_congested(struct mddev *mddev, int bits)
340 struct md_personality *pers = mddev->pers;
341 int ret = 0;
343 rcu_read_lock();
344 if (mddev->suspended)
345 ret = 1;
346 else if (pers && pers->congested)
347 ret = pers->congested(mddev, bits);
348 rcu_read_unlock();
349 return ret;
351 EXPORT_SYMBOL_GPL(mddev_congested);
352 static int md_congested(void *data, int bits)
354 struct mddev *mddev = data;
355 return mddev_congested(mddev, bits);
359 * Generic flush handling for md
362 static void md_end_flush(struct bio *bio)
364 struct md_rdev *rdev = bio->bi_private;
365 struct mddev *mddev = rdev->mddev;
367 rdev_dec_pending(rdev, mddev);
369 if (atomic_dec_and_test(&mddev->flush_pending)) {
370 /* The pre-request flush has finished */
371 queue_work(md_wq, &mddev->flush_work);
373 bio_put(bio);
376 static void md_submit_flush_data(struct work_struct *ws);
378 static void submit_flushes(struct work_struct *ws)
380 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
381 struct md_rdev *rdev;
383 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
384 atomic_set(&mddev->flush_pending, 1);
385 rcu_read_lock();
386 rdev_for_each_rcu(rdev, mddev)
387 if (rdev->raid_disk >= 0 &&
388 !test_bit(Faulty, &rdev->flags)) {
389 /* Take two references, one is dropped
390 * when request finishes, one after
391 * we reclaim rcu_read_lock
393 struct bio *bi;
394 atomic_inc(&rdev->nr_pending);
395 atomic_inc(&rdev->nr_pending);
396 rcu_read_unlock();
397 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
398 bi->bi_end_io = md_end_flush;
399 bi->bi_private = rdev;
400 bi->bi_bdev = rdev->bdev;
401 bi->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
402 atomic_inc(&mddev->flush_pending);
403 submit_bio(bi);
404 rcu_read_lock();
405 rdev_dec_pending(rdev, mddev);
407 rcu_read_unlock();
408 if (atomic_dec_and_test(&mddev->flush_pending))
409 queue_work(md_wq, &mddev->flush_work);
412 static void md_submit_flush_data(struct work_struct *ws)
414 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
415 struct bio *bio = mddev->flush_bio;
417 if (bio->bi_iter.bi_size == 0)
418 /* an empty barrier - all done */
419 bio_endio(bio);
420 else {
421 bio->bi_opf &= ~REQ_PREFLUSH;
422 mddev->pers->make_request(mddev, bio);
425 mddev->flush_bio = NULL;
426 wake_up(&mddev->sb_wait);
429 void md_flush_request(struct mddev *mddev, struct bio *bio)
431 spin_lock_irq(&mddev->lock);
432 wait_event_lock_irq(mddev->sb_wait,
433 !mddev->flush_bio,
434 mddev->lock);
435 mddev->flush_bio = bio;
436 spin_unlock_irq(&mddev->lock);
438 INIT_WORK(&mddev->flush_work, submit_flushes);
439 queue_work(md_wq, &mddev->flush_work);
441 EXPORT_SYMBOL(md_flush_request);
443 static inline struct mddev *mddev_get(struct mddev *mddev)
445 atomic_inc(&mddev->active);
446 return mddev;
449 static void mddev_delayed_delete(struct work_struct *ws);
451 static void mddev_put(struct mddev *mddev)
453 struct bio_set *bs = NULL;
455 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
456 return;
457 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
458 mddev->ctime == 0 && !mddev->hold_active) {
459 /* Array is not configured at all, and not held active,
460 * so destroy it */
461 list_del_init(&mddev->all_mddevs);
462 bs = mddev->bio_set;
463 mddev->bio_set = NULL;
464 if (mddev->gendisk) {
465 /* We did a probe so need to clean up. Call
466 * queue_work inside the spinlock so that
467 * flush_workqueue() after mddev_find will
468 * succeed in waiting for the work to be done.
470 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
471 queue_work(md_misc_wq, &mddev->del_work);
472 } else
473 kfree(mddev);
475 spin_unlock(&all_mddevs_lock);
476 if (bs)
477 bioset_free(bs);
480 static void md_safemode_timeout(unsigned long data);
482 void mddev_init(struct mddev *mddev)
484 mutex_init(&mddev->open_mutex);
485 mutex_init(&mddev->reconfig_mutex);
486 mutex_init(&mddev->bitmap_info.mutex);
487 INIT_LIST_HEAD(&mddev->disks);
488 INIT_LIST_HEAD(&mddev->all_mddevs);
489 setup_timer(&mddev->safemode_timer, md_safemode_timeout,
490 (unsigned long) mddev);
491 atomic_set(&mddev->active, 1);
492 atomic_set(&mddev->openers, 0);
493 atomic_set(&mddev->active_io, 0);
494 spin_lock_init(&mddev->lock);
495 atomic_set(&mddev->flush_pending, 0);
496 init_waitqueue_head(&mddev->sb_wait);
497 init_waitqueue_head(&mddev->recovery_wait);
498 mddev->reshape_position = MaxSector;
499 mddev->reshape_backwards = 0;
500 mddev->last_sync_action = "none";
501 mddev->resync_min = 0;
502 mddev->resync_max = MaxSector;
503 mddev->level = LEVEL_NONE;
505 EXPORT_SYMBOL_GPL(mddev_init);
507 static struct mddev *mddev_find(dev_t unit)
509 struct mddev *mddev, *new = NULL;
511 if (unit && MAJOR(unit) != MD_MAJOR)
512 unit &= ~((1<<MdpMinorShift)-1);
514 retry:
515 spin_lock(&all_mddevs_lock);
517 if (unit) {
518 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
519 if (mddev->unit == unit) {
520 mddev_get(mddev);
521 spin_unlock(&all_mddevs_lock);
522 kfree(new);
523 return mddev;
526 if (new) {
527 list_add(&new->all_mddevs, &all_mddevs);
528 spin_unlock(&all_mddevs_lock);
529 new->hold_active = UNTIL_IOCTL;
530 return new;
532 } else if (new) {
533 /* find an unused unit number */
534 static int next_minor = 512;
535 int start = next_minor;
536 int is_free = 0;
537 int dev = 0;
538 while (!is_free) {
539 dev = MKDEV(MD_MAJOR, next_minor);
540 next_minor++;
541 if (next_minor > MINORMASK)
542 next_minor = 0;
543 if (next_minor == start) {
544 /* Oh dear, all in use. */
545 spin_unlock(&all_mddevs_lock);
546 kfree(new);
547 return NULL;
550 is_free = 1;
551 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
552 if (mddev->unit == dev) {
553 is_free = 0;
554 break;
557 new->unit = dev;
558 new->md_minor = MINOR(dev);
559 new->hold_active = UNTIL_STOP;
560 list_add(&new->all_mddevs, &all_mddevs);
561 spin_unlock(&all_mddevs_lock);
562 return new;
564 spin_unlock(&all_mddevs_lock);
566 new = kzalloc(sizeof(*new), GFP_KERNEL);
567 if (!new)
568 return NULL;
570 new->unit = unit;
571 if (MAJOR(unit) == MD_MAJOR)
572 new->md_minor = MINOR(unit);
573 else
574 new->md_minor = MINOR(unit) >> MdpMinorShift;
576 mddev_init(new);
578 goto retry;
581 static struct attribute_group md_redundancy_group;
583 void mddev_unlock(struct mddev *mddev)
585 if (mddev->to_remove) {
586 /* These cannot be removed under reconfig_mutex as
587 * an access to the files will try to take reconfig_mutex
588 * while holding the file unremovable, which leads to
589 * a deadlock.
590 * So hold set sysfs_active while the remove in happeing,
591 * and anything else which might set ->to_remove or my
592 * otherwise change the sysfs namespace will fail with
593 * -EBUSY if sysfs_active is still set.
594 * We set sysfs_active under reconfig_mutex and elsewhere
595 * test it under the same mutex to ensure its correct value
596 * is seen.
598 struct attribute_group *to_remove = mddev->to_remove;
599 mddev->to_remove = NULL;
600 mddev->sysfs_active = 1;
601 mutex_unlock(&mddev->reconfig_mutex);
603 if (mddev->kobj.sd) {
604 if (to_remove != &md_redundancy_group)
605 sysfs_remove_group(&mddev->kobj, to_remove);
606 if (mddev->pers == NULL ||
607 mddev->pers->sync_request == NULL) {
608 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
609 if (mddev->sysfs_action)
610 sysfs_put(mddev->sysfs_action);
611 mddev->sysfs_action = NULL;
614 mddev->sysfs_active = 0;
615 } else
616 mutex_unlock(&mddev->reconfig_mutex);
618 /* As we've dropped the mutex we need a spinlock to
619 * make sure the thread doesn't disappear
621 spin_lock(&pers_lock);
622 md_wakeup_thread(mddev->thread);
623 spin_unlock(&pers_lock);
625 EXPORT_SYMBOL_GPL(mddev_unlock);
627 struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
629 struct md_rdev *rdev;
631 rdev_for_each_rcu(rdev, mddev)
632 if (rdev->desc_nr == nr)
633 return rdev;
635 return NULL;
637 EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
639 static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
641 struct md_rdev *rdev;
643 rdev_for_each(rdev, mddev)
644 if (rdev->bdev->bd_dev == dev)
645 return rdev;
647 return NULL;
650 static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
652 struct md_rdev *rdev;
654 rdev_for_each_rcu(rdev, mddev)
655 if (rdev->bdev->bd_dev == dev)
656 return rdev;
658 return NULL;
661 static struct md_personality *find_pers(int level, char *clevel)
663 struct md_personality *pers;
664 list_for_each_entry(pers, &pers_list, list) {
665 if (level != LEVEL_NONE && pers->level == level)
666 return pers;
667 if (strcmp(pers->name, clevel)==0)
668 return pers;
670 return NULL;
673 /* return the offset of the super block in 512byte sectors */
674 static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
676 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
677 return MD_NEW_SIZE_SECTORS(num_sectors);
680 static int alloc_disk_sb(struct md_rdev *rdev)
682 rdev->sb_page = alloc_page(GFP_KERNEL);
683 if (!rdev->sb_page)
684 return -ENOMEM;
685 return 0;
688 void md_rdev_clear(struct md_rdev *rdev)
690 if (rdev->sb_page) {
691 put_page(rdev->sb_page);
692 rdev->sb_loaded = 0;
693 rdev->sb_page = NULL;
694 rdev->sb_start = 0;
695 rdev->sectors = 0;
697 if (rdev->bb_page) {
698 put_page(rdev->bb_page);
699 rdev->bb_page = NULL;
701 badblocks_exit(&rdev->badblocks);
703 EXPORT_SYMBOL_GPL(md_rdev_clear);
705 static void super_written(struct bio *bio)
707 struct md_rdev *rdev = bio->bi_private;
708 struct mddev *mddev = rdev->mddev;
710 if (bio->bi_error) {
711 pr_err("md: super_written gets error=%d\n", bio->bi_error);
712 md_error(mddev, rdev);
713 if (!test_bit(Faulty, &rdev->flags)
714 && (bio->bi_opf & MD_FAILFAST)) {
715 set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags);
716 set_bit(LastDev, &rdev->flags);
718 } else
719 clear_bit(LastDev, &rdev->flags);
721 if (atomic_dec_and_test(&mddev->pending_writes))
722 wake_up(&mddev->sb_wait);
723 rdev_dec_pending(rdev, mddev);
724 bio_put(bio);
727 void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
728 sector_t sector, int size, struct page *page)
730 /* write first size bytes of page to sector of rdev
731 * Increment mddev->pending_writes before returning
732 * and decrement it on completion, waking up sb_wait
733 * if zero is reached.
734 * If an error occurred, call md_error
736 struct bio *bio;
737 int ff = 0;
739 if (test_bit(Faulty, &rdev->flags))
740 return;
742 bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
744 atomic_inc(&rdev->nr_pending);
746 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
747 bio->bi_iter.bi_sector = sector;
748 bio_add_page(bio, page, size, 0);
749 bio->bi_private = rdev;
750 bio->bi_end_io = super_written;
752 if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) &&
753 test_bit(FailFast, &rdev->flags) &&
754 !test_bit(LastDev, &rdev->flags))
755 ff = MD_FAILFAST;
756 bio->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_FUA | ff;
758 atomic_inc(&mddev->pending_writes);
759 submit_bio(bio);
762 int md_super_wait(struct mddev *mddev)
764 /* wait for all superblock writes that were scheduled to complete */
765 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
766 if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags))
767 return -EAGAIN;
768 return 0;
771 int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
772 struct page *page, int op, int op_flags, bool metadata_op)
774 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
775 int ret;
777 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
778 rdev->meta_bdev : rdev->bdev;
779 bio_set_op_attrs(bio, op, op_flags);
780 if (metadata_op)
781 bio->bi_iter.bi_sector = sector + rdev->sb_start;
782 else if (rdev->mddev->reshape_position != MaxSector &&
783 (rdev->mddev->reshape_backwards ==
784 (sector >= rdev->mddev->reshape_position)))
785 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
786 else
787 bio->bi_iter.bi_sector = sector + rdev->data_offset;
788 bio_add_page(bio, page, size, 0);
790 submit_bio_wait(bio);
792 ret = !bio->bi_error;
793 bio_put(bio);
794 return ret;
796 EXPORT_SYMBOL_GPL(sync_page_io);
798 static int read_disk_sb(struct md_rdev *rdev, int size)
800 char b[BDEVNAME_SIZE];
802 if (rdev->sb_loaded)
803 return 0;
805 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true))
806 goto fail;
807 rdev->sb_loaded = 1;
808 return 0;
810 fail:
811 pr_err("md: disabled device %s, could not read superblock.\n",
812 bdevname(rdev->bdev,b));
813 return -EINVAL;
816 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
818 return sb1->set_uuid0 == sb2->set_uuid0 &&
819 sb1->set_uuid1 == sb2->set_uuid1 &&
820 sb1->set_uuid2 == sb2->set_uuid2 &&
821 sb1->set_uuid3 == sb2->set_uuid3;
824 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
826 int ret;
827 mdp_super_t *tmp1, *tmp2;
829 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
830 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
832 if (!tmp1 || !tmp2) {
833 ret = 0;
834 goto abort;
837 *tmp1 = *sb1;
838 *tmp2 = *sb2;
841 * nr_disks is not constant
843 tmp1->nr_disks = 0;
844 tmp2->nr_disks = 0;
846 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
847 abort:
848 kfree(tmp1);
849 kfree(tmp2);
850 return ret;
853 static u32 md_csum_fold(u32 csum)
855 csum = (csum & 0xffff) + (csum >> 16);
856 return (csum & 0xffff) + (csum >> 16);
859 static unsigned int calc_sb_csum(mdp_super_t *sb)
861 u64 newcsum = 0;
862 u32 *sb32 = (u32*)sb;
863 int i;
864 unsigned int disk_csum, csum;
866 disk_csum = sb->sb_csum;
867 sb->sb_csum = 0;
869 for (i = 0; i < MD_SB_BYTES/4 ; i++)
870 newcsum += sb32[i];
871 csum = (newcsum & 0xffffffff) + (newcsum>>32);
873 #ifdef CONFIG_ALPHA
874 /* This used to use csum_partial, which was wrong for several
875 * reasons including that different results are returned on
876 * different architectures. It isn't critical that we get exactly
877 * the same return value as before (we always csum_fold before
878 * testing, and that removes any differences). However as we
879 * know that csum_partial always returned a 16bit value on
880 * alphas, do a fold to maximise conformity to previous behaviour.
882 sb->sb_csum = md_csum_fold(disk_csum);
883 #else
884 sb->sb_csum = disk_csum;
885 #endif
886 return csum;
890 * Handle superblock details.
891 * We want to be able to handle multiple superblock formats
892 * so we have a common interface to them all, and an array of
893 * different handlers.
894 * We rely on user-space to write the initial superblock, and support
895 * reading and updating of superblocks.
896 * Interface methods are:
897 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
898 * loads and validates a superblock on dev.
899 * if refdev != NULL, compare superblocks on both devices
900 * Return:
901 * 0 - dev has a superblock that is compatible with refdev
902 * 1 - dev has a superblock that is compatible and newer than refdev
903 * so dev should be used as the refdev in future
904 * -EINVAL superblock incompatible or invalid
905 * -othererror e.g. -EIO
907 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
908 * Verify that dev is acceptable into mddev.
909 * The first time, mddev->raid_disks will be 0, and data from
910 * dev should be merged in. Subsequent calls check that dev
911 * is new enough. Return 0 or -EINVAL
913 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
914 * Update the superblock for rdev with data in mddev
915 * This does not write to disc.
919 struct super_type {
920 char *name;
921 struct module *owner;
922 int (*load_super)(struct md_rdev *rdev,
923 struct md_rdev *refdev,
924 int minor_version);
925 int (*validate_super)(struct mddev *mddev,
926 struct md_rdev *rdev);
927 void (*sync_super)(struct mddev *mddev,
928 struct md_rdev *rdev);
929 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
930 sector_t num_sectors);
931 int (*allow_new_offset)(struct md_rdev *rdev,
932 unsigned long long new_offset);
936 * Check that the given mddev has no bitmap.
938 * This function is called from the run method of all personalities that do not
939 * support bitmaps. It prints an error message and returns non-zero if mddev
940 * has a bitmap. Otherwise, it returns 0.
943 int md_check_no_bitmap(struct mddev *mddev)
945 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
946 return 0;
947 pr_warn("%s: bitmaps are not supported for %s\n",
948 mdname(mddev), mddev->pers->name);
949 return 1;
951 EXPORT_SYMBOL(md_check_no_bitmap);
954 * load_super for 0.90.0
956 static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
958 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
959 mdp_super_t *sb;
960 int ret;
963 * Calculate the position of the superblock (512byte sectors),
964 * it's at the end of the disk.
966 * It also happens to be a multiple of 4Kb.
968 rdev->sb_start = calc_dev_sboffset(rdev);
970 ret = read_disk_sb(rdev, MD_SB_BYTES);
971 if (ret)
972 return ret;
974 ret = -EINVAL;
976 bdevname(rdev->bdev, b);
977 sb = page_address(rdev->sb_page);
979 if (sb->md_magic != MD_SB_MAGIC) {
980 pr_warn("md: invalid raid superblock magic on %s\n", b);
981 goto abort;
984 if (sb->major_version != 0 ||
985 sb->minor_version < 90 ||
986 sb->minor_version > 91) {
987 pr_warn("Bad version number %d.%d on %s\n",
988 sb->major_version, sb->minor_version, b);
989 goto abort;
992 if (sb->raid_disks <= 0)
993 goto abort;
995 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
996 pr_warn("md: invalid superblock checksum on %s\n", b);
997 goto abort;
1000 rdev->preferred_minor = sb->md_minor;
1001 rdev->data_offset = 0;
1002 rdev->new_data_offset = 0;
1003 rdev->sb_size = MD_SB_BYTES;
1004 rdev->badblocks.shift = -1;
1006 if (sb->level == LEVEL_MULTIPATH)
1007 rdev->desc_nr = -1;
1008 else
1009 rdev->desc_nr = sb->this_disk.number;
1011 if (!refdev) {
1012 ret = 1;
1013 } else {
1014 __u64 ev1, ev2;
1015 mdp_super_t *refsb = page_address(refdev->sb_page);
1016 if (!uuid_equal(refsb, sb)) {
1017 pr_warn("md: %s has different UUID to %s\n",
1018 b, bdevname(refdev->bdev,b2));
1019 goto abort;
1021 if (!sb_equal(refsb, sb)) {
1022 pr_warn("md: %s has same UUID but different superblock to %s\n",
1023 b, bdevname(refdev->bdev, b2));
1024 goto abort;
1026 ev1 = md_event(sb);
1027 ev2 = md_event(refsb);
1028 if (ev1 > ev2)
1029 ret = 1;
1030 else
1031 ret = 0;
1033 rdev->sectors = rdev->sb_start;
1034 /* Limit to 4TB as metadata cannot record more than that.
1035 * (not needed for Linear and RAID0 as metadata doesn't
1036 * record this size)
1038 if (IS_ENABLED(CONFIG_LBDAF) && (u64)rdev->sectors >= (2ULL << 32) &&
1039 sb->level >= 1)
1040 rdev->sectors = (sector_t)(2ULL << 32) - 2;
1042 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
1043 /* "this cannot possibly happen" ... */
1044 ret = -EINVAL;
1046 abort:
1047 return ret;
1051 * validate_super for 0.90.0
1053 static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
1055 mdp_disk_t *desc;
1056 mdp_super_t *sb = page_address(rdev->sb_page);
1057 __u64 ev1 = md_event(sb);
1059 rdev->raid_disk = -1;
1060 clear_bit(Faulty, &rdev->flags);
1061 clear_bit(In_sync, &rdev->flags);
1062 clear_bit(Bitmap_sync, &rdev->flags);
1063 clear_bit(WriteMostly, &rdev->flags);
1065 if (mddev->raid_disks == 0) {
1066 mddev->major_version = 0;
1067 mddev->minor_version = sb->minor_version;
1068 mddev->patch_version = sb->patch_version;
1069 mddev->external = 0;
1070 mddev->chunk_sectors = sb->chunk_size >> 9;
1071 mddev->ctime = sb->ctime;
1072 mddev->utime = sb->utime;
1073 mddev->level = sb->level;
1074 mddev->clevel[0] = 0;
1075 mddev->layout = sb->layout;
1076 mddev->raid_disks = sb->raid_disks;
1077 mddev->dev_sectors = ((sector_t)sb->size) * 2;
1078 mddev->events = ev1;
1079 mddev->bitmap_info.offset = 0;
1080 mddev->bitmap_info.space = 0;
1081 /* bitmap can use 60 K after the 4K superblocks */
1082 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
1083 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
1084 mddev->reshape_backwards = 0;
1086 if (mddev->minor_version >= 91) {
1087 mddev->reshape_position = sb->reshape_position;
1088 mddev->delta_disks = sb->delta_disks;
1089 mddev->new_level = sb->new_level;
1090 mddev->new_layout = sb->new_layout;
1091 mddev->new_chunk_sectors = sb->new_chunk >> 9;
1092 if (mddev->delta_disks < 0)
1093 mddev->reshape_backwards = 1;
1094 } else {
1095 mddev->reshape_position = MaxSector;
1096 mddev->delta_disks = 0;
1097 mddev->new_level = mddev->level;
1098 mddev->new_layout = mddev->layout;
1099 mddev->new_chunk_sectors = mddev->chunk_sectors;
1102 if (sb->state & (1<<MD_SB_CLEAN))
1103 mddev->recovery_cp = MaxSector;
1104 else {
1105 if (sb->events_hi == sb->cp_events_hi &&
1106 sb->events_lo == sb->cp_events_lo) {
1107 mddev->recovery_cp = sb->recovery_cp;
1108 } else
1109 mddev->recovery_cp = 0;
1112 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1113 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1114 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1115 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1117 mddev->max_disks = MD_SB_DISKS;
1119 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
1120 mddev->bitmap_info.file == NULL) {
1121 mddev->bitmap_info.offset =
1122 mddev->bitmap_info.default_offset;
1123 mddev->bitmap_info.space =
1124 mddev->bitmap_info.default_space;
1127 } else if (mddev->pers == NULL) {
1128 /* Insist on good event counter while assembling, except
1129 * for spares (which don't need an event count) */
1130 ++ev1;
1131 if (sb->disks[rdev->desc_nr].state & (
1132 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1133 if (ev1 < mddev->events)
1134 return -EINVAL;
1135 } else if (mddev->bitmap) {
1136 /* if adding to array with a bitmap, then we can accept an
1137 * older device ... but not too old.
1139 if (ev1 < mddev->bitmap->events_cleared)
1140 return 0;
1141 if (ev1 < mddev->events)
1142 set_bit(Bitmap_sync, &rdev->flags);
1143 } else {
1144 if (ev1 < mddev->events)
1145 /* just a hot-add of a new device, leave raid_disk at -1 */
1146 return 0;
1149 if (mddev->level != LEVEL_MULTIPATH) {
1150 desc = sb->disks + rdev->desc_nr;
1152 if (desc->state & (1<<MD_DISK_FAULTY))
1153 set_bit(Faulty, &rdev->flags);
1154 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1155 desc->raid_disk < mddev->raid_disks */) {
1156 set_bit(In_sync, &rdev->flags);
1157 rdev->raid_disk = desc->raid_disk;
1158 rdev->saved_raid_disk = desc->raid_disk;
1159 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1160 /* active but not in sync implies recovery up to
1161 * reshape position. We don't know exactly where
1162 * that is, so set to zero for now */
1163 if (mddev->minor_version >= 91) {
1164 rdev->recovery_offset = 0;
1165 rdev->raid_disk = desc->raid_disk;
1168 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1169 set_bit(WriteMostly, &rdev->flags);
1170 if (desc->state & (1<<MD_DISK_FAILFAST))
1171 set_bit(FailFast, &rdev->flags);
1172 } else /* MULTIPATH are always insync */
1173 set_bit(In_sync, &rdev->flags);
1174 return 0;
1178 * sync_super for 0.90.0
1180 static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
1182 mdp_super_t *sb;
1183 struct md_rdev *rdev2;
1184 int next_spare = mddev->raid_disks;
1186 /* make rdev->sb match mddev data..
1188 * 1/ zero out disks
1189 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1190 * 3/ any empty disks < next_spare become removed
1192 * disks[0] gets initialised to REMOVED because
1193 * we cannot be sure from other fields if it has
1194 * been initialised or not.
1196 int i;
1197 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1199 rdev->sb_size = MD_SB_BYTES;
1201 sb = page_address(rdev->sb_page);
1203 memset(sb, 0, sizeof(*sb));
1205 sb->md_magic = MD_SB_MAGIC;
1206 sb->major_version = mddev->major_version;
1207 sb->patch_version = mddev->patch_version;
1208 sb->gvalid_words = 0; /* ignored */
1209 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1210 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1211 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1212 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1214 sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
1215 sb->level = mddev->level;
1216 sb->size = mddev->dev_sectors / 2;
1217 sb->raid_disks = mddev->raid_disks;
1218 sb->md_minor = mddev->md_minor;
1219 sb->not_persistent = 0;
1220 sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
1221 sb->state = 0;
1222 sb->events_hi = (mddev->events>>32);
1223 sb->events_lo = (u32)mddev->events;
1225 if (mddev->reshape_position == MaxSector)
1226 sb->minor_version = 90;
1227 else {
1228 sb->minor_version = 91;
1229 sb->reshape_position = mddev->reshape_position;
1230 sb->new_level = mddev->new_level;
1231 sb->delta_disks = mddev->delta_disks;
1232 sb->new_layout = mddev->new_layout;
1233 sb->new_chunk = mddev->new_chunk_sectors << 9;
1235 mddev->minor_version = sb->minor_version;
1236 if (mddev->in_sync)
1238 sb->recovery_cp = mddev->recovery_cp;
1239 sb->cp_events_hi = (mddev->events>>32);
1240 sb->cp_events_lo = (u32)mddev->events;
1241 if (mddev->recovery_cp == MaxSector)
1242 sb->state = (1<< MD_SB_CLEAN);
1243 } else
1244 sb->recovery_cp = 0;
1246 sb->layout = mddev->layout;
1247 sb->chunk_size = mddev->chunk_sectors << 9;
1249 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
1250 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1252 sb->disks[0].state = (1<<MD_DISK_REMOVED);
1253 rdev_for_each(rdev2, mddev) {
1254 mdp_disk_t *d;
1255 int desc_nr;
1256 int is_active = test_bit(In_sync, &rdev2->flags);
1258 if (rdev2->raid_disk >= 0 &&
1259 sb->minor_version >= 91)
1260 /* we have nowhere to store the recovery_offset,
1261 * but if it is not below the reshape_position,
1262 * we can piggy-back on that.
1264 is_active = 1;
1265 if (rdev2->raid_disk < 0 ||
1266 test_bit(Faulty, &rdev2->flags))
1267 is_active = 0;
1268 if (is_active)
1269 desc_nr = rdev2->raid_disk;
1270 else
1271 desc_nr = next_spare++;
1272 rdev2->desc_nr = desc_nr;
1273 d = &sb->disks[rdev2->desc_nr];
1274 nr_disks++;
1275 d->number = rdev2->desc_nr;
1276 d->major = MAJOR(rdev2->bdev->bd_dev);
1277 d->minor = MINOR(rdev2->bdev->bd_dev);
1278 if (is_active)
1279 d->raid_disk = rdev2->raid_disk;
1280 else
1281 d->raid_disk = rdev2->desc_nr; /* compatibility */
1282 if (test_bit(Faulty, &rdev2->flags))
1283 d->state = (1<<MD_DISK_FAULTY);
1284 else if (is_active) {
1285 d->state = (1<<MD_DISK_ACTIVE);
1286 if (test_bit(In_sync, &rdev2->flags))
1287 d->state |= (1<<MD_DISK_SYNC);
1288 active++;
1289 working++;
1290 } else {
1291 d->state = 0;
1292 spare++;
1293 working++;
1295 if (test_bit(WriteMostly, &rdev2->flags))
1296 d->state |= (1<<MD_DISK_WRITEMOSTLY);
1297 if (test_bit(FailFast, &rdev2->flags))
1298 d->state |= (1<<MD_DISK_FAILFAST);
1300 /* now set the "removed" and "faulty" bits on any missing devices */
1301 for (i=0 ; i < mddev->raid_disks ; i++) {
1302 mdp_disk_t *d = &sb->disks[i];
1303 if (d->state == 0 && d->number == 0) {
1304 d->number = i;
1305 d->raid_disk = i;
1306 d->state = (1<<MD_DISK_REMOVED);
1307 d->state |= (1<<MD_DISK_FAULTY);
1308 failed++;
1311 sb->nr_disks = nr_disks;
1312 sb->active_disks = active;
1313 sb->working_disks = working;
1314 sb->failed_disks = failed;
1315 sb->spare_disks = spare;
1317 sb->this_disk = sb->disks[rdev->desc_nr];
1318 sb->sb_csum = calc_sb_csum(sb);
1322 * rdev_size_change for 0.90.0
1324 static unsigned long long
1325 super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
1327 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1328 return 0; /* component must fit device */
1329 if (rdev->mddev->bitmap_info.offset)
1330 return 0; /* can't move bitmap */
1331 rdev->sb_start = calc_dev_sboffset(rdev);
1332 if (!num_sectors || num_sectors > rdev->sb_start)
1333 num_sectors = rdev->sb_start;
1334 /* Limit to 4TB as metadata cannot record more than that.
1335 * 4TB == 2^32 KB, or 2*2^32 sectors.
1337 if (IS_ENABLED(CONFIG_LBDAF) && (u64)num_sectors >= (2ULL << 32) &&
1338 rdev->mddev->level >= 1)
1339 num_sectors = (sector_t)(2ULL << 32) - 2;
1340 do {
1341 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1342 rdev->sb_page);
1343 } while (md_super_wait(rdev->mddev) < 0);
1344 return num_sectors;
1347 static int
1348 super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1350 /* non-zero offset changes not possible with v0.90 */
1351 return new_offset == 0;
1355 * version 1 superblock
1358 static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
1360 __le32 disk_csum;
1361 u32 csum;
1362 unsigned long long newcsum;
1363 int size = 256 + le32_to_cpu(sb->max_dev)*2;
1364 __le32 *isuper = (__le32*)sb;
1366 disk_csum = sb->sb_csum;
1367 sb->sb_csum = 0;
1368 newcsum = 0;
1369 for (; size >= 4; size -= 4)
1370 newcsum += le32_to_cpu(*isuper++);
1372 if (size == 2)
1373 newcsum += le16_to_cpu(*(__le16*) isuper);
1375 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1376 sb->sb_csum = disk_csum;
1377 return cpu_to_le32(csum);
1380 static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
1382 struct mdp_superblock_1 *sb;
1383 int ret;
1384 sector_t sb_start;
1385 sector_t sectors;
1386 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1387 int bmask;
1390 * Calculate the position of the superblock in 512byte sectors.
1391 * It is always aligned to a 4K boundary and
1392 * depeding on minor_version, it can be:
1393 * 0: At least 8K, but less than 12K, from end of device
1394 * 1: At start of device
1395 * 2: 4K from start of device.
1397 switch(minor_version) {
1398 case 0:
1399 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
1400 sb_start -= 8*2;
1401 sb_start &= ~(sector_t)(4*2-1);
1402 break;
1403 case 1:
1404 sb_start = 0;
1405 break;
1406 case 2:
1407 sb_start = 8;
1408 break;
1409 default:
1410 return -EINVAL;
1412 rdev->sb_start = sb_start;
1414 /* superblock is rarely larger than 1K, but it can be larger,
1415 * and it is safe to read 4k, so we do that
1417 ret = read_disk_sb(rdev, 4096);
1418 if (ret) return ret;
1420 sb = page_address(rdev->sb_page);
1422 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1423 sb->major_version != cpu_to_le32(1) ||
1424 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1425 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1426 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1427 return -EINVAL;
1429 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1430 pr_warn("md: invalid superblock checksum on %s\n",
1431 bdevname(rdev->bdev,b));
1432 return -EINVAL;
1434 if (le64_to_cpu(sb->data_size) < 10) {
1435 pr_warn("md: data_size too small on %s\n",
1436 bdevname(rdev->bdev,b));
1437 return -EINVAL;
1439 if (sb->pad0 ||
1440 sb->pad3[0] ||
1441 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1442 /* Some padding is non-zero, might be a new feature */
1443 return -EINVAL;
1445 rdev->preferred_minor = 0xffff;
1446 rdev->data_offset = le64_to_cpu(sb->data_offset);
1447 rdev->new_data_offset = rdev->data_offset;
1448 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1449 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1450 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
1451 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1453 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1454 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1455 if (rdev->sb_size & bmask)
1456 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1458 if (minor_version
1459 && rdev->data_offset < sb_start + (rdev->sb_size/512))
1460 return -EINVAL;
1461 if (minor_version
1462 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1463 return -EINVAL;
1465 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1466 rdev->desc_nr = -1;
1467 else
1468 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1470 if (!rdev->bb_page) {
1471 rdev->bb_page = alloc_page(GFP_KERNEL);
1472 if (!rdev->bb_page)
1473 return -ENOMEM;
1475 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1476 rdev->badblocks.count == 0) {
1477 /* need to load the bad block list.
1478 * Currently we limit it to one page.
1480 s32 offset;
1481 sector_t bb_sector;
1482 u64 *bbp;
1483 int i;
1484 int sectors = le16_to_cpu(sb->bblog_size);
1485 if (sectors > (PAGE_SIZE / 512))
1486 return -EINVAL;
1487 offset = le32_to_cpu(sb->bblog_offset);
1488 if (offset == 0)
1489 return -EINVAL;
1490 bb_sector = (long long)offset;
1491 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1492 rdev->bb_page, REQ_OP_READ, 0, true))
1493 return -EIO;
1494 bbp = (u64 *)page_address(rdev->bb_page);
1495 rdev->badblocks.shift = sb->bblog_shift;
1496 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1497 u64 bb = le64_to_cpu(*bbp);
1498 int count = bb & (0x3ff);
1499 u64 sector = bb >> 10;
1500 sector <<= sb->bblog_shift;
1501 count <<= sb->bblog_shift;
1502 if (bb + 1 == 0)
1503 break;
1504 if (badblocks_set(&rdev->badblocks, sector, count, 1))
1505 return -EINVAL;
1507 } else if (sb->bblog_offset != 0)
1508 rdev->badblocks.shift = 0;
1510 if (!refdev) {
1511 ret = 1;
1512 } else {
1513 __u64 ev1, ev2;
1514 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
1516 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1517 sb->level != refsb->level ||
1518 sb->layout != refsb->layout ||
1519 sb->chunksize != refsb->chunksize) {
1520 pr_warn("md: %s has strangely different superblock to %s\n",
1521 bdevname(rdev->bdev,b),
1522 bdevname(refdev->bdev,b2));
1523 return -EINVAL;
1525 ev1 = le64_to_cpu(sb->events);
1526 ev2 = le64_to_cpu(refsb->events);
1528 if (ev1 > ev2)
1529 ret = 1;
1530 else
1531 ret = 0;
1533 if (minor_version) {
1534 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1535 sectors -= rdev->data_offset;
1536 } else
1537 sectors = rdev->sb_start;
1538 if (sectors < le64_to_cpu(sb->data_size))
1539 return -EINVAL;
1540 rdev->sectors = le64_to_cpu(sb->data_size);
1541 return ret;
1544 static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
1546 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
1547 __u64 ev1 = le64_to_cpu(sb->events);
1549 rdev->raid_disk = -1;
1550 clear_bit(Faulty, &rdev->flags);
1551 clear_bit(In_sync, &rdev->flags);
1552 clear_bit(Bitmap_sync, &rdev->flags);
1553 clear_bit(WriteMostly, &rdev->flags);
1555 if (mddev->raid_disks == 0) {
1556 mddev->major_version = 1;
1557 mddev->patch_version = 0;
1558 mddev->external = 0;
1559 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
1560 mddev->ctime = le64_to_cpu(sb->ctime);
1561 mddev->utime = le64_to_cpu(sb->utime);
1562 mddev->level = le32_to_cpu(sb->level);
1563 mddev->clevel[0] = 0;
1564 mddev->layout = le32_to_cpu(sb->layout);
1565 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1566 mddev->dev_sectors = le64_to_cpu(sb->size);
1567 mddev->events = ev1;
1568 mddev->bitmap_info.offset = 0;
1569 mddev->bitmap_info.space = 0;
1570 /* Default location for bitmap is 1K after superblock
1571 * using 3K - total of 4K
1573 mddev->bitmap_info.default_offset = 1024 >> 9;
1574 mddev->bitmap_info.default_space = (4096-1024) >> 9;
1575 mddev->reshape_backwards = 0;
1577 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1578 memcpy(mddev->uuid, sb->set_uuid, 16);
1580 mddev->max_disks = (4096-256)/2;
1582 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1583 mddev->bitmap_info.file == NULL) {
1584 mddev->bitmap_info.offset =
1585 (__s32)le32_to_cpu(sb->bitmap_offset);
1586 /* Metadata doesn't record how much space is available.
1587 * For 1.0, we assume we can use up to the superblock
1588 * if before, else to 4K beyond superblock.
1589 * For others, assume no change is possible.
1591 if (mddev->minor_version > 0)
1592 mddev->bitmap_info.space = 0;
1593 else if (mddev->bitmap_info.offset > 0)
1594 mddev->bitmap_info.space =
1595 8 - mddev->bitmap_info.offset;
1596 else
1597 mddev->bitmap_info.space =
1598 -mddev->bitmap_info.offset;
1601 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1602 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1603 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1604 mddev->new_level = le32_to_cpu(sb->new_level);
1605 mddev->new_layout = le32_to_cpu(sb->new_layout);
1606 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
1607 if (mddev->delta_disks < 0 ||
1608 (mddev->delta_disks == 0 &&
1609 (le32_to_cpu(sb->feature_map)
1610 & MD_FEATURE_RESHAPE_BACKWARDS)))
1611 mddev->reshape_backwards = 1;
1612 } else {
1613 mddev->reshape_position = MaxSector;
1614 mddev->delta_disks = 0;
1615 mddev->new_level = mddev->level;
1616 mddev->new_layout = mddev->layout;
1617 mddev->new_chunk_sectors = mddev->chunk_sectors;
1620 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
1621 set_bit(MD_HAS_JOURNAL, &mddev->flags);
1622 } else if (mddev->pers == NULL) {
1623 /* Insist of good event counter while assembling, except for
1624 * spares (which don't need an event count) */
1625 ++ev1;
1626 if (rdev->desc_nr >= 0 &&
1627 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1628 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1629 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
1630 if (ev1 < mddev->events)
1631 return -EINVAL;
1632 } else if (mddev->bitmap) {
1633 /* If adding to array with a bitmap, then we can accept an
1634 * older device, but not too old.
1636 if (ev1 < mddev->bitmap->events_cleared)
1637 return 0;
1638 if (ev1 < mddev->events)
1639 set_bit(Bitmap_sync, &rdev->flags);
1640 } else {
1641 if (ev1 < mddev->events)
1642 /* just a hot-add of a new device, leave raid_disk at -1 */
1643 return 0;
1645 if (mddev->level != LEVEL_MULTIPATH) {
1646 int role;
1647 if (rdev->desc_nr < 0 ||
1648 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1649 role = MD_DISK_ROLE_SPARE;
1650 rdev->desc_nr = -1;
1651 } else
1652 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1653 switch(role) {
1654 case MD_DISK_ROLE_SPARE: /* spare */
1655 break;
1656 case MD_DISK_ROLE_FAULTY: /* faulty */
1657 set_bit(Faulty, &rdev->flags);
1658 break;
1659 case MD_DISK_ROLE_JOURNAL: /* journal device */
1660 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1661 /* journal device without journal feature */
1662 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
1663 return -EINVAL;
1665 set_bit(Journal, &rdev->flags);
1666 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
1667 rdev->raid_disk = 0;
1668 break;
1669 default:
1670 rdev->saved_raid_disk = role;
1671 if ((le32_to_cpu(sb->feature_map) &
1672 MD_FEATURE_RECOVERY_OFFSET)) {
1673 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1674 if (!(le32_to_cpu(sb->feature_map) &
1675 MD_FEATURE_RECOVERY_BITMAP))
1676 rdev->saved_raid_disk = -1;
1677 } else
1678 set_bit(In_sync, &rdev->flags);
1679 rdev->raid_disk = role;
1680 break;
1682 if (sb->devflags & WriteMostly1)
1683 set_bit(WriteMostly, &rdev->flags);
1684 if (sb->devflags & FailFast1)
1685 set_bit(FailFast, &rdev->flags);
1686 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1687 set_bit(Replacement, &rdev->flags);
1688 } else /* MULTIPATH are always insync */
1689 set_bit(In_sync, &rdev->flags);
1691 return 0;
1694 static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
1696 struct mdp_superblock_1 *sb;
1697 struct md_rdev *rdev2;
1698 int max_dev, i;
1699 /* make rdev->sb match mddev and rdev data. */
1701 sb = page_address(rdev->sb_page);
1703 sb->feature_map = 0;
1704 sb->pad0 = 0;
1705 sb->recovery_offset = cpu_to_le64(0);
1706 memset(sb->pad3, 0, sizeof(sb->pad3));
1708 sb->utime = cpu_to_le64((__u64)mddev->utime);
1709 sb->events = cpu_to_le64(mddev->events);
1710 if (mddev->in_sync)
1711 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1712 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
1713 sb->resync_offset = cpu_to_le64(MaxSector);
1714 else
1715 sb->resync_offset = cpu_to_le64(0);
1717 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1719 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1720 sb->size = cpu_to_le64(mddev->dev_sectors);
1721 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
1722 sb->level = cpu_to_le32(mddev->level);
1723 sb->layout = cpu_to_le32(mddev->layout);
1724 if (test_bit(FailFast, &rdev->flags))
1725 sb->devflags |= FailFast1;
1726 else
1727 sb->devflags &= ~FailFast1;
1729 if (test_bit(WriteMostly, &rdev->flags))
1730 sb->devflags |= WriteMostly1;
1731 else
1732 sb->devflags &= ~WriteMostly1;
1733 sb->data_offset = cpu_to_le64(rdev->data_offset);
1734 sb->data_size = cpu_to_le64(rdev->sectors);
1736 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1737 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
1738 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1741 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
1742 !test_bit(In_sync, &rdev->flags)) {
1743 sb->feature_map |=
1744 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1745 sb->recovery_offset =
1746 cpu_to_le64(rdev->recovery_offset);
1747 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1748 sb->feature_map |=
1749 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
1751 /* Note: recovery_offset and journal_tail share space */
1752 if (test_bit(Journal, &rdev->flags))
1753 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
1754 if (test_bit(Replacement, &rdev->flags))
1755 sb->feature_map |=
1756 cpu_to_le32(MD_FEATURE_REPLACEMENT);
1758 if (mddev->reshape_position != MaxSector) {
1759 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1760 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1761 sb->new_layout = cpu_to_le32(mddev->new_layout);
1762 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1763 sb->new_level = cpu_to_le32(mddev->new_level);
1764 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
1765 if (mddev->delta_disks == 0 &&
1766 mddev->reshape_backwards)
1767 sb->feature_map
1768 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
1769 if (rdev->new_data_offset != rdev->data_offset) {
1770 sb->feature_map
1771 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1772 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1773 - rdev->data_offset));
1777 if (mddev_is_clustered(mddev))
1778 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
1780 if (rdev->badblocks.count == 0)
1781 /* Nothing to do for bad blocks*/ ;
1782 else if (sb->bblog_offset == 0)
1783 /* Cannot record bad blocks on this device */
1784 md_error(mddev, rdev);
1785 else {
1786 struct badblocks *bb = &rdev->badblocks;
1787 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1788 u64 *p = bb->page;
1789 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1790 if (bb->changed) {
1791 unsigned seq;
1793 retry:
1794 seq = read_seqbegin(&bb->lock);
1796 memset(bbp, 0xff, PAGE_SIZE);
1798 for (i = 0 ; i < bb->count ; i++) {
1799 u64 internal_bb = p[i];
1800 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1801 | BB_LEN(internal_bb));
1802 bbp[i] = cpu_to_le64(store_bb);
1804 bb->changed = 0;
1805 if (read_seqretry(&bb->lock, seq))
1806 goto retry;
1808 bb->sector = (rdev->sb_start +
1809 (int)le32_to_cpu(sb->bblog_offset));
1810 bb->size = le16_to_cpu(sb->bblog_size);
1814 max_dev = 0;
1815 rdev_for_each(rdev2, mddev)
1816 if (rdev2->desc_nr+1 > max_dev)
1817 max_dev = rdev2->desc_nr+1;
1819 if (max_dev > le32_to_cpu(sb->max_dev)) {
1820 int bmask;
1821 sb->max_dev = cpu_to_le32(max_dev);
1822 rdev->sb_size = max_dev * 2 + 256;
1823 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1824 if (rdev->sb_size & bmask)
1825 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1826 } else
1827 max_dev = le32_to_cpu(sb->max_dev);
1829 for (i=0; i<max_dev;i++)
1830 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
1832 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
1833 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
1835 rdev_for_each(rdev2, mddev) {
1836 i = rdev2->desc_nr;
1837 if (test_bit(Faulty, &rdev2->flags))
1838 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
1839 else if (test_bit(In_sync, &rdev2->flags))
1840 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1841 else if (test_bit(Journal, &rdev2->flags))
1842 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
1843 else if (rdev2->raid_disk >= 0)
1844 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1845 else
1846 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
1849 sb->sb_csum = calc_sb_1_csum(sb);
1852 static unsigned long long
1853 super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
1855 struct mdp_superblock_1 *sb;
1856 sector_t max_sectors;
1857 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1858 return 0; /* component must fit device */
1859 if (rdev->data_offset != rdev->new_data_offset)
1860 return 0; /* too confusing */
1861 if (rdev->sb_start < rdev->data_offset) {
1862 /* minor versions 1 and 2; superblock before data */
1863 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
1864 max_sectors -= rdev->data_offset;
1865 if (!num_sectors || num_sectors > max_sectors)
1866 num_sectors = max_sectors;
1867 } else if (rdev->mddev->bitmap_info.offset) {
1868 /* minor version 0 with bitmap we can't move */
1869 return 0;
1870 } else {
1871 /* minor version 0; superblock after data */
1872 sector_t sb_start;
1873 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
1874 sb_start &= ~(sector_t)(4*2 - 1);
1875 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
1876 if (!num_sectors || num_sectors > max_sectors)
1877 num_sectors = max_sectors;
1878 rdev->sb_start = sb_start;
1880 sb = page_address(rdev->sb_page);
1881 sb->data_size = cpu_to_le64(num_sectors);
1882 sb->super_offset = cpu_to_le64(rdev->sb_start);
1883 sb->sb_csum = calc_sb_1_csum(sb);
1884 do {
1885 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1886 rdev->sb_page);
1887 } while (md_super_wait(rdev->mddev) < 0);
1888 return num_sectors;
1892 static int
1893 super_1_allow_new_offset(struct md_rdev *rdev,
1894 unsigned long long new_offset)
1896 /* All necessary checks on new >= old have been done */
1897 struct bitmap *bitmap;
1898 if (new_offset >= rdev->data_offset)
1899 return 1;
1901 /* with 1.0 metadata, there is no metadata to tread on
1902 * so we can always move back */
1903 if (rdev->mddev->minor_version == 0)
1904 return 1;
1906 /* otherwise we must be sure not to step on
1907 * any metadata, so stay:
1908 * 36K beyond start of superblock
1909 * beyond end of badblocks
1910 * beyond write-intent bitmap
1912 if (rdev->sb_start + (32+4)*2 > new_offset)
1913 return 0;
1914 bitmap = rdev->mddev->bitmap;
1915 if (bitmap && !rdev->mddev->bitmap_info.file &&
1916 rdev->sb_start + rdev->mddev->bitmap_info.offset +
1917 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
1918 return 0;
1919 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1920 return 0;
1922 return 1;
1925 static struct super_type super_types[] = {
1926 [0] = {
1927 .name = "0.90.0",
1928 .owner = THIS_MODULE,
1929 .load_super = super_90_load,
1930 .validate_super = super_90_validate,
1931 .sync_super = super_90_sync,
1932 .rdev_size_change = super_90_rdev_size_change,
1933 .allow_new_offset = super_90_allow_new_offset,
1935 [1] = {
1936 .name = "md-1",
1937 .owner = THIS_MODULE,
1938 .load_super = super_1_load,
1939 .validate_super = super_1_validate,
1940 .sync_super = super_1_sync,
1941 .rdev_size_change = super_1_rdev_size_change,
1942 .allow_new_offset = super_1_allow_new_offset,
1946 static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
1948 if (mddev->sync_super) {
1949 mddev->sync_super(mddev, rdev);
1950 return;
1953 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1955 super_types[mddev->major_version].sync_super(mddev, rdev);
1958 static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
1960 struct md_rdev *rdev, *rdev2;
1962 rcu_read_lock();
1963 rdev_for_each_rcu(rdev, mddev1) {
1964 if (test_bit(Faulty, &rdev->flags) ||
1965 test_bit(Journal, &rdev->flags) ||
1966 rdev->raid_disk == -1)
1967 continue;
1968 rdev_for_each_rcu(rdev2, mddev2) {
1969 if (test_bit(Faulty, &rdev2->flags) ||
1970 test_bit(Journal, &rdev2->flags) ||
1971 rdev2->raid_disk == -1)
1972 continue;
1973 if (rdev->bdev->bd_contains ==
1974 rdev2->bdev->bd_contains) {
1975 rcu_read_unlock();
1976 return 1;
1980 rcu_read_unlock();
1981 return 0;
1984 static LIST_HEAD(pending_raid_disks);
1987 * Try to register data integrity profile for an mddev
1989 * This is called when an array is started and after a disk has been kicked
1990 * from the array. It only succeeds if all working and active component devices
1991 * are integrity capable with matching profiles.
1993 int md_integrity_register(struct mddev *mddev)
1995 struct md_rdev *rdev, *reference = NULL;
1997 if (list_empty(&mddev->disks))
1998 return 0; /* nothing to do */
1999 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2000 return 0; /* shouldn't register, or already is */
2001 rdev_for_each(rdev, mddev) {
2002 /* skip spares and non-functional disks */
2003 if (test_bit(Faulty, &rdev->flags))
2004 continue;
2005 if (rdev->raid_disk < 0)
2006 continue;
2007 if (!reference) {
2008 /* Use the first rdev as the reference */
2009 reference = rdev;
2010 continue;
2012 /* does this rdev's profile match the reference profile? */
2013 if (blk_integrity_compare(reference->bdev->bd_disk,
2014 rdev->bdev->bd_disk) < 0)
2015 return -EINVAL;
2017 if (!reference || !bdev_get_integrity(reference->bdev))
2018 return 0;
2020 * All component devices are integrity capable and have matching
2021 * profiles, register the common profile for the md device.
2023 blk_integrity_register(mddev->gendisk,
2024 bdev_get_integrity(reference->bdev));
2026 pr_debug("md: data integrity enabled on %s\n", mdname(mddev));
2027 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
2028 pr_err("md: failed to create integrity pool for %s\n",
2029 mdname(mddev));
2030 return -EINVAL;
2032 return 0;
2034 EXPORT_SYMBOL(md_integrity_register);
2037 * Attempt to add an rdev, but only if it is consistent with the current
2038 * integrity profile
2040 int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
2042 struct blk_integrity *bi_rdev;
2043 struct blk_integrity *bi_mddev;
2044 char name[BDEVNAME_SIZE];
2046 if (!mddev->gendisk)
2047 return 0;
2049 bi_rdev = bdev_get_integrity(rdev->bdev);
2050 bi_mddev = blk_get_integrity(mddev->gendisk);
2052 if (!bi_mddev) /* nothing to do */
2053 return 0;
2055 if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
2056 pr_err("%s: incompatible integrity profile for %s\n",
2057 mdname(mddev), bdevname(rdev->bdev, name));
2058 return -ENXIO;
2061 return 0;
2063 EXPORT_SYMBOL(md_integrity_add_rdev);
2065 static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
2067 char b[BDEVNAME_SIZE];
2068 struct kobject *ko;
2069 int err;
2071 /* prevent duplicates */
2072 if (find_rdev(mddev, rdev->bdev->bd_dev))
2073 return -EEXIST;
2075 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2076 if (!test_bit(Journal, &rdev->flags) &&
2077 rdev->sectors &&
2078 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
2079 if (mddev->pers) {
2080 /* Cannot change size, so fail
2081 * If mddev->level <= 0, then we don't care
2082 * about aligning sizes (e.g. linear)
2084 if (mddev->level > 0)
2085 return -ENOSPC;
2086 } else
2087 mddev->dev_sectors = rdev->sectors;
2090 /* Verify rdev->desc_nr is unique.
2091 * If it is -1, assign a free number, else
2092 * check number is not in use
2094 rcu_read_lock();
2095 if (rdev->desc_nr < 0) {
2096 int choice = 0;
2097 if (mddev->pers)
2098 choice = mddev->raid_disks;
2099 while (md_find_rdev_nr_rcu(mddev, choice))
2100 choice++;
2101 rdev->desc_nr = choice;
2102 } else {
2103 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
2104 rcu_read_unlock();
2105 return -EBUSY;
2108 rcu_read_unlock();
2109 if (!test_bit(Journal, &rdev->flags) &&
2110 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2111 pr_warn("md: %s: array is limited to %d devices\n",
2112 mdname(mddev), mddev->max_disks);
2113 return -EBUSY;
2115 bdevname(rdev->bdev,b);
2116 strreplace(b, '/', '!');
2118 rdev->mddev = mddev;
2119 pr_debug("md: bind<%s>\n", b);
2121 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
2122 goto fail;
2124 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
2125 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2126 /* failure here is OK */;
2127 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
2129 list_add_rcu(&rdev->same_set, &mddev->disks);
2130 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
2132 /* May as well allow recovery to be retried once */
2133 mddev->recovery_disabled++;
2135 return 0;
2137 fail:
2138 pr_warn("md: failed to register dev-%s for %s\n",
2139 b, mdname(mddev));
2140 return err;
2143 static void md_delayed_delete(struct work_struct *ws)
2145 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
2146 kobject_del(&rdev->kobj);
2147 kobject_put(&rdev->kobj);
2150 static void unbind_rdev_from_array(struct md_rdev *rdev)
2152 char b[BDEVNAME_SIZE];
2154 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
2155 list_del_rcu(&rdev->same_set);
2156 pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b));
2157 rdev->mddev = NULL;
2158 sysfs_remove_link(&rdev->kobj, "block");
2159 sysfs_put(rdev->sysfs_state);
2160 rdev->sysfs_state = NULL;
2161 rdev->badblocks.count = 0;
2162 /* We need to delay this, otherwise we can deadlock when
2163 * writing to 'remove' to "dev/state". We also need
2164 * to delay it due to rcu usage.
2166 synchronize_rcu();
2167 INIT_WORK(&rdev->del_work, md_delayed_delete);
2168 kobject_get(&rdev->kobj);
2169 queue_work(md_misc_wq, &rdev->del_work);
2173 * prevent the device from being mounted, repartitioned or
2174 * otherwise reused by a RAID array (or any other kernel
2175 * subsystem), by bd_claiming the device.
2177 static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
2179 int err = 0;
2180 struct block_device *bdev;
2181 char b[BDEVNAME_SIZE];
2183 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
2184 shared ? (struct md_rdev *)lock_rdev : rdev);
2185 if (IS_ERR(bdev)) {
2186 pr_warn("md: could not open %s.\n", __bdevname(dev, b));
2187 return PTR_ERR(bdev);
2189 rdev->bdev = bdev;
2190 return err;
2193 static void unlock_rdev(struct md_rdev *rdev)
2195 struct block_device *bdev = rdev->bdev;
2196 rdev->bdev = NULL;
2197 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
2200 void md_autodetect_dev(dev_t dev);
2202 static void export_rdev(struct md_rdev *rdev)
2204 char b[BDEVNAME_SIZE];
2206 pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b));
2207 md_rdev_clear(rdev);
2208 #ifndef MODULE
2209 if (test_bit(AutoDetected, &rdev->flags))
2210 md_autodetect_dev(rdev->bdev->bd_dev);
2211 #endif
2212 unlock_rdev(rdev);
2213 kobject_put(&rdev->kobj);
2216 void md_kick_rdev_from_array(struct md_rdev *rdev)
2218 unbind_rdev_from_array(rdev);
2219 export_rdev(rdev);
2221 EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
2223 static void export_array(struct mddev *mddev)
2225 struct md_rdev *rdev;
2227 while (!list_empty(&mddev->disks)) {
2228 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2229 same_set);
2230 md_kick_rdev_from_array(rdev);
2232 mddev->raid_disks = 0;
2233 mddev->major_version = 0;
2236 static void sync_sbs(struct mddev *mddev, int nospares)
2238 /* Update each superblock (in-memory image), but
2239 * if we are allowed to, skip spares which already
2240 * have the right event counter, or have one earlier
2241 * (which would mean they aren't being marked as dirty
2242 * with the rest of the array)
2244 struct md_rdev *rdev;
2245 rdev_for_each(rdev, mddev) {
2246 if (rdev->sb_events == mddev->events ||
2247 (nospares &&
2248 rdev->raid_disk < 0 &&
2249 rdev->sb_events+1 == mddev->events)) {
2250 /* Don't update this superblock */
2251 rdev->sb_loaded = 2;
2252 } else {
2253 sync_super(mddev, rdev);
2254 rdev->sb_loaded = 1;
2259 static bool does_sb_need_changing(struct mddev *mddev)
2261 struct md_rdev *rdev;
2262 struct mdp_superblock_1 *sb;
2263 int role;
2265 /* Find a good rdev */
2266 rdev_for_each(rdev, mddev)
2267 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2268 break;
2270 /* No good device found. */
2271 if (!rdev)
2272 return false;
2274 sb = page_address(rdev->sb_page);
2275 /* Check if a device has become faulty or a spare become active */
2276 rdev_for_each(rdev, mddev) {
2277 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2278 /* Device activated? */
2279 if (role == 0xffff && rdev->raid_disk >=0 &&
2280 !test_bit(Faulty, &rdev->flags))
2281 return true;
2282 /* Device turned faulty? */
2283 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2284 return true;
2287 /* Check if any mddev parameters have changed */
2288 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2289 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
2290 (mddev->layout != le32_to_cpu(sb->layout)) ||
2291 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2292 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2293 return true;
2295 return false;
2298 void md_update_sb(struct mddev *mddev, int force_change)
2300 struct md_rdev *rdev;
2301 int sync_req;
2302 int nospares = 0;
2303 int any_badblocks_changed = 0;
2304 int ret = -1;
2306 if (mddev->ro) {
2307 if (force_change)
2308 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2309 return;
2312 repeat:
2313 if (mddev_is_clustered(mddev)) {
2314 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
2315 force_change = 1;
2316 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
2317 nospares = 1;
2318 ret = md_cluster_ops->metadata_update_start(mddev);
2319 /* Has someone else has updated the sb */
2320 if (!does_sb_need_changing(mddev)) {
2321 if (ret == 0)
2322 md_cluster_ops->metadata_update_cancel(mddev);
2323 bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2324 BIT(MD_SB_CHANGE_DEVS) |
2325 BIT(MD_SB_CHANGE_CLEAN));
2326 return;
2330 /* First make sure individual recovery_offsets are correct */
2331 rdev_for_each(rdev, mddev) {
2332 if (rdev->raid_disk >= 0 &&
2333 mddev->delta_disks >= 0 &&
2334 !test_bit(Journal, &rdev->flags) &&
2335 !test_bit(In_sync, &rdev->flags) &&
2336 mddev->curr_resync_completed > rdev->recovery_offset)
2337 rdev->recovery_offset = mddev->curr_resync_completed;
2340 if (!mddev->persistent) {
2341 clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2342 clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2343 if (!mddev->external) {
2344 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
2345 rdev_for_each(rdev, mddev) {
2346 if (rdev->badblocks.changed) {
2347 rdev->badblocks.changed = 0;
2348 ack_all_badblocks(&rdev->badblocks);
2349 md_error(mddev, rdev);
2351 clear_bit(Blocked, &rdev->flags);
2352 clear_bit(BlockedBadBlocks, &rdev->flags);
2353 wake_up(&rdev->blocked_wait);
2356 wake_up(&mddev->sb_wait);
2357 return;
2360 spin_lock(&mddev->lock);
2362 mddev->utime = ktime_get_real_seconds();
2364 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
2365 force_change = 1;
2366 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
2367 /* just a clean<-> dirty transition, possibly leave spares alone,
2368 * though if events isn't the right even/odd, we will have to do
2369 * spares after all
2371 nospares = 1;
2372 if (force_change)
2373 nospares = 0;
2374 if (mddev->degraded)
2375 /* If the array is degraded, then skipping spares is both
2376 * dangerous and fairly pointless.
2377 * Dangerous because a device that was removed from the array
2378 * might have a event_count that still looks up-to-date,
2379 * so it can be re-added without a resync.
2380 * Pointless because if there are any spares to skip,
2381 * then a recovery will happen and soon that array won't
2382 * be degraded any more and the spare can go back to sleep then.
2384 nospares = 0;
2386 sync_req = mddev->in_sync;
2388 /* If this is just a dirty<->clean transition, and the array is clean
2389 * and 'events' is odd, we can roll back to the previous clean state */
2390 if (nospares
2391 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
2392 && mddev->can_decrease_events
2393 && mddev->events != 1) {
2394 mddev->events--;
2395 mddev->can_decrease_events = 0;
2396 } else {
2397 /* otherwise we have to go forward and ... */
2398 mddev->events ++;
2399 mddev->can_decrease_events = nospares;
2403 * This 64-bit counter should never wrap.
2404 * Either we are in around ~1 trillion A.C., assuming
2405 * 1 reboot per second, or we have a bug...
2407 WARN_ON(mddev->events == 0);
2409 rdev_for_each(rdev, mddev) {
2410 if (rdev->badblocks.changed)
2411 any_badblocks_changed++;
2412 if (test_bit(Faulty, &rdev->flags))
2413 set_bit(FaultRecorded, &rdev->flags);
2416 sync_sbs(mddev, nospares);
2417 spin_unlock(&mddev->lock);
2419 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2420 mdname(mddev), mddev->in_sync);
2422 if (mddev->queue)
2423 blk_add_trace_msg(mddev->queue, "md md_update_sb");
2424 rewrite:
2425 bitmap_update_sb(mddev->bitmap);
2426 rdev_for_each(rdev, mddev) {
2427 char b[BDEVNAME_SIZE];
2429 if (rdev->sb_loaded != 1)
2430 continue; /* no noise on spare devices */
2432 if (!test_bit(Faulty, &rdev->flags)) {
2433 md_super_write(mddev,rdev,
2434 rdev->sb_start, rdev->sb_size,
2435 rdev->sb_page);
2436 pr_debug("md: (write) %s's sb offset: %llu\n",
2437 bdevname(rdev->bdev, b),
2438 (unsigned long long)rdev->sb_start);
2439 rdev->sb_events = mddev->events;
2440 if (rdev->badblocks.size) {
2441 md_super_write(mddev, rdev,
2442 rdev->badblocks.sector,
2443 rdev->badblocks.size << 9,
2444 rdev->bb_page);
2445 rdev->badblocks.size = 0;
2448 } else
2449 pr_debug("md: %s (skipping faulty)\n",
2450 bdevname(rdev->bdev, b));
2452 if (mddev->level == LEVEL_MULTIPATH)
2453 /* only need to write one superblock... */
2454 break;
2456 if (md_super_wait(mddev) < 0)
2457 goto rewrite;
2458 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
2460 if (mddev_is_clustered(mddev) && ret == 0)
2461 md_cluster_ops->metadata_update_finish(mddev);
2463 if (mddev->in_sync != sync_req ||
2464 !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2465 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN)))
2466 /* have to write it out again */
2467 goto repeat;
2468 wake_up(&mddev->sb_wait);
2469 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2470 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
2472 rdev_for_each(rdev, mddev) {
2473 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2474 clear_bit(Blocked, &rdev->flags);
2476 if (any_badblocks_changed)
2477 ack_all_badblocks(&rdev->badblocks);
2478 clear_bit(BlockedBadBlocks, &rdev->flags);
2479 wake_up(&rdev->blocked_wait);
2482 EXPORT_SYMBOL(md_update_sb);
2484 static int add_bound_rdev(struct md_rdev *rdev)
2486 struct mddev *mddev = rdev->mddev;
2487 int err = 0;
2488 bool add_journal = test_bit(Journal, &rdev->flags);
2490 if (!mddev->pers->hot_remove_disk || add_journal) {
2491 /* If there is hot_add_disk but no hot_remove_disk
2492 * then added disks for geometry changes,
2493 * and should be added immediately.
2495 super_types[mddev->major_version].
2496 validate_super(mddev, rdev);
2497 if (add_journal)
2498 mddev_suspend(mddev);
2499 err = mddev->pers->hot_add_disk(mddev, rdev);
2500 if (add_journal)
2501 mddev_resume(mddev);
2502 if (err) {
2503 md_kick_rdev_from_array(rdev);
2504 return err;
2507 sysfs_notify_dirent_safe(rdev->sysfs_state);
2509 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2510 if (mddev->degraded)
2511 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2512 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2513 md_new_event(mddev);
2514 md_wakeup_thread(mddev->thread);
2515 return 0;
2518 /* words written to sysfs files may, or may not, be \n terminated.
2519 * We want to accept with case. For this we use cmd_match.
2521 static int cmd_match(const char *cmd, const char *str)
2523 /* See if cmd, written into a sysfs file, matches
2524 * str. They must either be the same, or cmd can
2525 * have a trailing newline
2527 while (*cmd && *str && *cmd == *str) {
2528 cmd++;
2529 str++;
2531 if (*cmd == '\n')
2532 cmd++;
2533 if (*str || *cmd)
2534 return 0;
2535 return 1;
2538 struct rdev_sysfs_entry {
2539 struct attribute attr;
2540 ssize_t (*show)(struct md_rdev *, char *);
2541 ssize_t (*store)(struct md_rdev *, const char *, size_t);
2544 static ssize_t
2545 state_show(struct md_rdev *rdev, char *page)
2547 char *sep = ",";
2548 size_t len = 0;
2549 unsigned long flags = ACCESS_ONCE(rdev->flags);
2551 if (test_bit(Faulty, &flags) ||
2552 (!test_bit(ExternalBbl, &flags) &&
2553 rdev->badblocks.unacked_exist))
2554 len += sprintf(page+len, "faulty%s", sep);
2555 if (test_bit(In_sync, &flags))
2556 len += sprintf(page+len, "in_sync%s", sep);
2557 if (test_bit(Journal, &flags))
2558 len += sprintf(page+len, "journal%s", sep);
2559 if (test_bit(WriteMostly, &flags))
2560 len += sprintf(page+len, "write_mostly%s", sep);
2561 if (test_bit(Blocked, &flags) ||
2562 (rdev->badblocks.unacked_exist
2563 && !test_bit(Faulty, &flags)))
2564 len += sprintf(page+len, "blocked%s", sep);
2565 if (!test_bit(Faulty, &flags) &&
2566 !test_bit(Journal, &flags) &&
2567 !test_bit(In_sync, &flags))
2568 len += sprintf(page+len, "spare%s", sep);
2569 if (test_bit(WriteErrorSeen, &flags))
2570 len += sprintf(page+len, "write_error%s", sep);
2571 if (test_bit(WantReplacement, &flags))
2572 len += sprintf(page+len, "want_replacement%s", sep);
2573 if (test_bit(Replacement, &flags))
2574 len += sprintf(page+len, "replacement%s", sep);
2575 if (test_bit(ExternalBbl, &flags))
2576 len += sprintf(page+len, "external_bbl%s", sep);
2577 if (test_bit(FailFast, &flags))
2578 len += sprintf(page+len, "failfast%s", sep);
2580 if (len)
2581 len -= strlen(sep);
2583 return len+sprintf(page+len, "\n");
2586 static ssize_t
2587 state_store(struct md_rdev *rdev, const char *buf, size_t len)
2589 /* can write
2590 * faulty - simulates an error
2591 * remove - disconnects the device
2592 * writemostly - sets write_mostly
2593 * -writemostly - clears write_mostly
2594 * blocked - sets the Blocked flags
2595 * -blocked - clears the Blocked and possibly simulates an error
2596 * insync - sets Insync providing device isn't active
2597 * -insync - clear Insync for a device with a slot assigned,
2598 * so that it gets rebuilt based on bitmap
2599 * write_error - sets WriteErrorSeen
2600 * -write_error - clears WriteErrorSeen
2601 * {,-}failfast - set/clear FailFast
2603 int err = -EINVAL;
2604 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2605 md_error(rdev->mddev, rdev);
2606 if (test_bit(Faulty, &rdev->flags))
2607 err = 0;
2608 else
2609 err = -EBUSY;
2610 } else if (cmd_match(buf, "remove")) {
2611 if (rdev->mddev->pers) {
2612 clear_bit(Blocked, &rdev->flags);
2613 remove_and_add_spares(rdev->mddev, rdev);
2615 if (rdev->raid_disk >= 0)
2616 err = -EBUSY;
2617 else {
2618 struct mddev *mddev = rdev->mddev;
2619 err = 0;
2620 if (mddev_is_clustered(mddev))
2621 err = md_cluster_ops->remove_disk(mddev, rdev);
2623 if (err == 0) {
2624 md_kick_rdev_from_array(rdev);
2625 if (mddev->pers) {
2626 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2627 md_wakeup_thread(mddev->thread);
2629 md_new_event(mddev);
2632 } else if (cmd_match(buf, "writemostly")) {
2633 set_bit(WriteMostly, &rdev->flags);
2634 err = 0;
2635 } else if (cmd_match(buf, "-writemostly")) {
2636 clear_bit(WriteMostly, &rdev->flags);
2637 err = 0;
2638 } else if (cmd_match(buf, "blocked")) {
2639 set_bit(Blocked, &rdev->flags);
2640 err = 0;
2641 } else if (cmd_match(buf, "-blocked")) {
2642 if (!test_bit(Faulty, &rdev->flags) &&
2643 !test_bit(ExternalBbl, &rdev->flags) &&
2644 rdev->badblocks.unacked_exist) {
2645 /* metadata handler doesn't understand badblocks,
2646 * so we need to fail the device
2648 md_error(rdev->mddev, rdev);
2650 clear_bit(Blocked, &rdev->flags);
2651 clear_bit(BlockedBadBlocks, &rdev->flags);
2652 wake_up(&rdev->blocked_wait);
2653 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2654 md_wakeup_thread(rdev->mddev->thread);
2656 err = 0;
2657 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2658 set_bit(In_sync, &rdev->flags);
2659 err = 0;
2660 } else if (cmd_match(buf, "failfast")) {
2661 set_bit(FailFast, &rdev->flags);
2662 err = 0;
2663 } else if (cmd_match(buf, "-failfast")) {
2664 clear_bit(FailFast, &rdev->flags);
2665 err = 0;
2666 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
2667 !test_bit(Journal, &rdev->flags)) {
2668 if (rdev->mddev->pers == NULL) {
2669 clear_bit(In_sync, &rdev->flags);
2670 rdev->saved_raid_disk = rdev->raid_disk;
2671 rdev->raid_disk = -1;
2672 err = 0;
2674 } else if (cmd_match(buf, "write_error")) {
2675 set_bit(WriteErrorSeen, &rdev->flags);
2676 err = 0;
2677 } else if (cmd_match(buf, "-write_error")) {
2678 clear_bit(WriteErrorSeen, &rdev->flags);
2679 err = 0;
2680 } else if (cmd_match(buf, "want_replacement")) {
2681 /* Any non-spare device that is not a replacement can
2682 * become want_replacement at any time, but we then need to
2683 * check if recovery is needed.
2685 if (rdev->raid_disk >= 0 &&
2686 !test_bit(Journal, &rdev->flags) &&
2687 !test_bit(Replacement, &rdev->flags))
2688 set_bit(WantReplacement, &rdev->flags);
2689 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2690 md_wakeup_thread(rdev->mddev->thread);
2691 err = 0;
2692 } else if (cmd_match(buf, "-want_replacement")) {
2693 /* Clearing 'want_replacement' is always allowed.
2694 * Once replacements starts it is too late though.
2696 err = 0;
2697 clear_bit(WantReplacement, &rdev->flags);
2698 } else if (cmd_match(buf, "replacement")) {
2699 /* Can only set a device as a replacement when array has not
2700 * yet been started. Once running, replacement is automatic
2701 * from spares, or by assigning 'slot'.
2703 if (rdev->mddev->pers)
2704 err = -EBUSY;
2705 else {
2706 set_bit(Replacement, &rdev->flags);
2707 err = 0;
2709 } else if (cmd_match(buf, "-replacement")) {
2710 /* Similarly, can only clear Replacement before start */
2711 if (rdev->mddev->pers)
2712 err = -EBUSY;
2713 else {
2714 clear_bit(Replacement, &rdev->flags);
2715 err = 0;
2717 } else if (cmd_match(buf, "re-add")) {
2718 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) {
2719 /* clear_bit is performed _after_ all the devices
2720 * have their local Faulty bit cleared. If any writes
2721 * happen in the meantime in the local node, they
2722 * will land in the local bitmap, which will be synced
2723 * by this node eventually
2725 if (!mddev_is_clustered(rdev->mddev) ||
2726 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2727 clear_bit(Faulty, &rdev->flags);
2728 err = add_bound_rdev(rdev);
2730 } else
2731 err = -EBUSY;
2732 } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) {
2733 set_bit(ExternalBbl, &rdev->flags);
2734 rdev->badblocks.shift = 0;
2735 err = 0;
2736 } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) {
2737 clear_bit(ExternalBbl, &rdev->flags);
2738 err = 0;
2740 if (!err)
2741 sysfs_notify_dirent_safe(rdev->sysfs_state);
2742 return err ? err : len;
2744 static struct rdev_sysfs_entry rdev_state =
2745 __ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
2747 static ssize_t
2748 errors_show(struct md_rdev *rdev, char *page)
2750 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2753 static ssize_t
2754 errors_store(struct md_rdev *rdev, const char *buf, size_t len)
2756 unsigned int n;
2757 int rv;
2759 rv = kstrtouint(buf, 10, &n);
2760 if (rv < 0)
2761 return rv;
2762 atomic_set(&rdev->corrected_errors, n);
2763 return len;
2765 static struct rdev_sysfs_entry rdev_errors =
2766 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
2768 static ssize_t
2769 slot_show(struct md_rdev *rdev, char *page)
2771 if (test_bit(Journal, &rdev->flags))
2772 return sprintf(page, "journal\n");
2773 else if (rdev->raid_disk < 0)
2774 return sprintf(page, "none\n");
2775 else
2776 return sprintf(page, "%d\n", rdev->raid_disk);
2779 static ssize_t
2780 slot_store(struct md_rdev *rdev, const char *buf, size_t len)
2782 int slot;
2783 int err;
2785 if (test_bit(Journal, &rdev->flags))
2786 return -EBUSY;
2787 if (strncmp(buf, "none", 4)==0)
2788 slot = -1;
2789 else {
2790 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2791 if (err < 0)
2792 return err;
2794 if (rdev->mddev->pers && slot == -1) {
2795 /* Setting 'slot' on an active array requires also
2796 * updating the 'rd%d' link, and communicating
2797 * with the personality with ->hot_*_disk.
2798 * For now we only support removing
2799 * failed/spare devices. This normally happens automatically,
2800 * but not when the metadata is externally managed.
2802 if (rdev->raid_disk == -1)
2803 return -EEXIST;
2804 /* personality does all needed checks */
2805 if (rdev->mddev->pers->hot_remove_disk == NULL)
2806 return -EINVAL;
2807 clear_bit(Blocked, &rdev->flags);
2808 remove_and_add_spares(rdev->mddev, rdev);
2809 if (rdev->raid_disk >= 0)
2810 return -EBUSY;
2811 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2812 md_wakeup_thread(rdev->mddev->thread);
2813 } else if (rdev->mddev->pers) {
2814 /* Activating a spare .. or possibly reactivating
2815 * if we ever get bitmaps working here.
2817 int err;
2819 if (rdev->raid_disk != -1)
2820 return -EBUSY;
2822 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2823 return -EBUSY;
2825 if (rdev->mddev->pers->hot_add_disk == NULL)
2826 return -EINVAL;
2828 if (slot >= rdev->mddev->raid_disks &&
2829 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2830 return -ENOSPC;
2832 rdev->raid_disk = slot;
2833 if (test_bit(In_sync, &rdev->flags))
2834 rdev->saved_raid_disk = slot;
2835 else
2836 rdev->saved_raid_disk = -1;
2837 clear_bit(In_sync, &rdev->flags);
2838 clear_bit(Bitmap_sync, &rdev->flags);
2839 err = rdev->mddev->pers->
2840 hot_add_disk(rdev->mddev, rdev);
2841 if (err) {
2842 rdev->raid_disk = -1;
2843 return err;
2844 } else
2845 sysfs_notify_dirent_safe(rdev->sysfs_state);
2846 if (sysfs_link_rdev(rdev->mddev, rdev))
2847 /* failure here is OK */;
2848 /* don't wakeup anyone, leave that to userspace. */
2849 } else {
2850 if (slot >= rdev->mddev->raid_disks &&
2851 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2852 return -ENOSPC;
2853 rdev->raid_disk = slot;
2854 /* assume it is working */
2855 clear_bit(Faulty, &rdev->flags);
2856 clear_bit(WriteMostly, &rdev->flags);
2857 set_bit(In_sync, &rdev->flags);
2858 sysfs_notify_dirent_safe(rdev->sysfs_state);
2860 return len;
2863 static struct rdev_sysfs_entry rdev_slot =
2864 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
2866 static ssize_t
2867 offset_show(struct md_rdev *rdev, char *page)
2869 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
2872 static ssize_t
2873 offset_store(struct md_rdev *rdev, const char *buf, size_t len)
2875 unsigned long long offset;
2876 if (kstrtoull(buf, 10, &offset) < 0)
2877 return -EINVAL;
2878 if (rdev->mddev->pers && rdev->raid_disk >= 0)
2879 return -EBUSY;
2880 if (rdev->sectors && rdev->mddev->external)
2881 /* Must set offset before size, so overlap checks
2882 * can be sane */
2883 return -EBUSY;
2884 rdev->data_offset = offset;
2885 rdev->new_data_offset = offset;
2886 return len;
2889 static struct rdev_sysfs_entry rdev_offset =
2890 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
2892 static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2894 return sprintf(page, "%llu\n",
2895 (unsigned long long)rdev->new_data_offset);
2898 static ssize_t new_offset_store(struct md_rdev *rdev,
2899 const char *buf, size_t len)
2901 unsigned long long new_offset;
2902 struct mddev *mddev = rdev->mddev;
2904 if (kstrtoull(buf, 10, &new_offset) < 0)
2905 return -EINVAL;
2907 if (mddev->sync_thread ||
2908 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
2909 return -EBUSY;
2910 if (new_offset == rdev->data_offset)
2911 /* reset is always permitted */
2913 else if (new_offset > rdev->data_offset) {
2914 /* must not push array size beyond rdev_sectors */
2915 if (new_offset - rdev->data_offset
2916 + mddev->dev_sectors > rdev->sectors)
2917 return -E2BIG;
2919 /* Metadata worries about other space details. */
2921 /* decreasing the offset is inconsistent with a backwards
2922 * reshape.
2924 if (new_offset < rdev->data_offset &&
2925 mddev->reshape_backwards)
2926 return -EINVAL;
2927 /* Increasing offset is inconsistent with forwards
2928 * reshape. reshape_direction should be set to
2929 * 'backwards' first.
2931 if (new_offset > rdev->data_offset &&
2932 !mddev->reshape_backwards)
2933 return -EINVAL;
2935 if (mddev->pers && mddev->persistent &&
2936 !super_types[mddev->major_version]
2937 .allow_new_offset(rdev, new_offset))
2938 return -E2BIG;
2939 rdev->new_data_offset = new_offset;
2940 if (new_offset > rdev->data_offset)
2941 mddev->reshape_backwards = 1;
2942 else if (new_offset < rdev->data_offset)
2943 mddev->reshape_backwards = 0;
2945 return len;
2947 static struct rdev_sysfs_entry rdev_new_offset =
2948 __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2950 static ssize_t
2951 rdev_size_show(struct md_rdev *rdev, char *page)
2953 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
2956 static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2958 /* check if two start/length pairs overlap */
2959 if (s1+l1 <= s2)
2960 return 0;
2961 if (s2+l2 <= s1)
2962 return 0;
2963 return 1;
2966 static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2968 unsigned long long blocks;
2969 sector_t new;
2971 if (kstrtoull(buf, 10, &blocks) < 0)
2972 return -EINVAL;
2974 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2975 return -EINVAL; /* sector conversion overflow */
2977 new = blocks * 2;
2978 if (new != blocks * 2)
2979 return -EINVAL; /* unsigned long long to sector_t overflow */
2981 *sectors = new;
2982 return 0;
2985 static ssize_t
2986 rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
2988 struct mddev *my_mddev = rdev->mddev;
2989 sector_t oldsectors = rdev->sectors;
2990 sector_t sectors;
2992 if (test_bit(Journal, &rdev->flags))
2993 return -EBUSY;
2994 if (strict_blocks_to_sectors(buf, &sectors) < 0)
2995 return -EINVAL;
2996 if (rdev->data_offset != rdev->new_data_offset)
2997 return -EINVAL; /* too confusing */
2998 if (my_mddev->pers && rdev->raid_disk >= 0) {
2999 if (my_mddev->persistent) {
3000 sectors = super_types[my_mddev->major_version].
3001 rdev_size_change(rdev, sectors);
3002 if (!sectors)
3003 return -EBUSY;
3004 } else if (!sectors)
3005 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
3006 rdev->data_offset;
3007 if (!my_mddev->pers->resize)
3008 /* Cannot change size for RAID0 or Linear etc */
3009 return -EINVAL;
3011 if (sectors < my_mddev->dev_sectors)
3012 return -EINVAL; /* component must fit device */
3014 rdev->sectors = sectors;
3015 if (sectors > oldsectors && my_mddev->external) {
3016 /* Need to check that all other rdevs with the same
3017 * ->bdev do not overlap. 'rcu' is sufficient to walk
3018 * the rdev lists safely.
3019 * This check does not provide a hard guarantee, it
3020 * just helps avoid dangerous mistakes.
3022 struct mddev *mddev;
3023 int overlap = 0;
3024 struct list_head *tmp;
3026 rcu_read_lock();
3027 for_each_mddev(mddev, tmp) {
3028 struct md_rdev *rdev2;
3030 rdev_for_each(rdev2, mddev)
3031 if (rdev->bdev == rdev2->bdev &&
3032 rdev != rdev2 &&
3033 overlaps(rdev->data_offset, rdev->sectors,
3034 rdev2->data_offset,
3035 rdev2->sectors)) {
3036 overlap = 1;
3037 break;
3039 if (overlap) {
3040 mddev_put(mddev);
3041 break;
3044 rcu_read_unlock();
3045 if (overlap) {
3046 /* Someone else could have slipped in a size
3047 * change here, but doing so is just silly.
3048 * We put oldsectors back because we *know* it is
3049 * safe, and trust userspace not to race with
3050 * itself
3052 rdev->sectors = oldsectors;
3053 return -EBUSY;
3056 return len;
3059 static struct rdev_sysfs_entry rdev_size =
3060 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
3062 static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
3064 unsigned long long recovery_start = rdev->recovery_offset;
3066 if (test_bit(In_sync, &rdev->flags) ||
3067 recovery_start == MaxSector)
3068 return sprintf(page, "none\n");
3070 return sprintf(page, "%llu\n", recovery_start);
3073 static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
3075 unsigned long long recovery_start;
3077 if (cmd_match(buf, "none"))
3078 recovery_start = MaxSector;
3079 else if (kstrtoull(buf, 10, &recovery_start))
3080 return -EINVAL;
3082 if (rdev->mddev->pers &&
3083 rdev->raid_disk >= 0)
3084 return -EBUSY;
3086 rdev->recovery_offset = recovery_start;
3087 if (recovery_start == MaxSector)
3088 set_bit(In_sync, &rdev->flags);
3089 else
3090 clear_bit(In_sync, &rdev->flags);
3091 return len;
3094 static struct rdev_sysfs_entry rdev_recovery_start =
3095 __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3097 /* sysfs access to bad-blocks list.
3098 * We present two files.
3099 * 'bad-blocks' lists sector numbers and lengths of ranges that
3100 * are recorded as bad. The list is truncated to fit within
3101 * the one-page limit of sysfs.
3102 * Writing "sector length" to this file adds an acknowledged
3103 * bad block list.
3104 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3105 * been acknowledged. Writing to this file adds bad blocks
3106 * without acknowledging them. This is largely for testing.
3108 static ssize_t bb_show(struct md_rdev *rdev, char *page)
3110 return badblocks_show(&rdev->badblocks, page, 0);
3112 static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
3114 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3115 /* Maybe that ack was all we needed */
3116 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3117 wake_up(&rdev->blocked_wait);
3118 return rv;
3120 static struct rdev_sysfs_entry rdev_bad_blocks =
3121 __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3123 static ssize_t ubb_show(struct md_rdev *rdev, char *page)
3125 return badblocks_show(&rdev->badblocks, page, 1);
3127 static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
3129 return badblocks_store(&rdev->badblocks, page, len, 1);
3131 static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3132 __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3134 static struct attribute *rdev_default_attrs[] = {
3135 &rdev_state.attr,
3136 &rdev_errors.attr,
3137 &rdev_slot.attr,
3138 &rdev_offset.attr,
3139 &rdev_new_offset.attr,
3140 &rdev_size.attr,
3141 &rdev_recovery_start.attr,
3142 &rdev_bad_blocks.attr,
3143 &rdev_unack_bad_blocks.attr,
3144 NULL,
3146 static ssize_t
3147 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3149 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
3150 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
3152 if (!entry->show)
3153 return -EIO;
3154 if (!rdev->mddev)
3155 return -EBUSY;
3156 return entry->show(rdev, page);
3159 static ssize_t
3160 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3161 const char *page, size_t length)
3163 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
3164 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
3165 ssize_t rv;
3166 struct mddev *mddev = rdev->mddev;
3168 if (!entry->store)
3169 return -EIO;
3170 if (!capable(CAP_SYS_ADMIN))
3171 return -EACCES;
3172 rv = mddev ? mddev_lock(mddev): -EBUSY;
3173 if (!rv) {
3174 if (rdev->mddev == NULL)
3175 rv = -EBUSY;
3176 else
3177 rv = entry->store(rdev, page, length);
3178 mddev_unlock(mddev);
3180 return rv;
3183 static void rdev_free(struct kobject *ko)
3185 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
3186 kfree(rdev);
3188 static const struct sysfs_ops rdev_sysfs_ops = {
3189 .show = rdev_attr_show,
3190 .store = rdev_attr_store,
3192 static struct kobj_type rdev_ktype = {
3193 .release = rdev_free,
3194 .sysfs_ops = &rdev_sysfs_ops,
3195 .default_attrs = rdev_default_attrs,
3198 int md_rdev_init(struct md_rdev *rdev)
3200 rdev->desc_nr = -1;
3201 rdev->saved_raid_disk = -1;
3202 rdev->raid_disk = -1;
3203 rdev->flags = 0;
3204 rdev->data_offset = 0;
3205 rdev->new_data_offset = 0;
3206 rdev->sb_events = 0;
3207 rdev->last_read_error = 0;
3208 rdev->sb_loaded = 0;
3209 rdev->bb_page = NULL;
3210 atomic_set(&rdev->nr_pending, 0);
3211 atomic_set(&rdev->read_errors, 0);
3212 atomic_set(&rdev->corrected_errors, 0);
3214 INIT_LIST_HEAD(&rdev->same_set);
3215 init_waitqueue_head(&rdev->blocked_wait);
3217 /* Add space to store bad block list.
3218 * This reserves the space even on arrays where it cannot
3219 * be used - I wonder if that matters
3221 return badblocks_init(&rdev->badblocks, 0);
3223 EXPORT_SYMBOL_GPL(md_rdev_init);
3225 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3227 * mark the device faulty if:
3229 * - the device is nonexistent (zero size)
3230 * - the device has no valid superblock
3232 * a faulty rdev _never_ has rdev->sb set.
3234 static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
3236 char b[BDEVNAME_SIZE];
3237 int err;
3238 struct md_rdev *rdev;
3239 sector_t size;
3241 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
3242 if (!rdev)
3243 return ERR_PTR(-ENOMEM);
3245 err = md_rdev_init(rdev);
3246 if (err)
3247 goto abort_free;
3248 err = alloc_disk_sb(rdev);
3249 if (err)
3250 goto abort_free;
3252 err = lock_rdev(rdev, newdev, super_format == -2);
3253 if (err)
3254 goto abort_free;
3256 kobject_init(&rdev->kobj, &rdev_ktype);
3258 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
3259 if (!size) {
3260 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
3261 bdevname(rdev->bdev,b));
3262 err = -EINVAL;
3263 goto abort_free;
3266 if (super_format >= 0) {
3267 err = super_types[super_format].
3268 load_super(rdev, NULL, super_minor);
3269 if (err == -EINVAL) {
3270 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
3271 bdevname(rdev->bdev,b),
3272 super_format, super_minor);
3273 goto abort_free;
3275 if (err < 0) {
3276 pr_warn("md: could not read %s's sb, not importing!\n",
3277 bdevname(rdev->bdev,b));
3278 goto abort_free;
3282 return rdev;
3284 abort_free:
3285 if (rdev->bdev)
3286 unlock_rdev(rdev);
3287 md_rdev_clear(rdev);
3288 kfree(rdev);
3289 return ERR_PTR(err);
3293 * Check a full RAID array for plausibility
3296 static void analyze_sbs(struct mddev *mddev)
3298 int i;
3299 struct md_rdev *rdev, *freshest, *tmp;
3300 char b[BDEVNAME_SIZE];
3302 freshest = NULL;
3303 rdev_for_each_safe(rdev, tmp, mddev)
3304 switch (super_types[mddev->major_version].
3305 load_super(rdev, freshest, mddev->minor_version)) {
3306 case 1:
3307 freshest = rdev;
3308 break;
3309 case 0:
3310 break;
3311 default:
3312 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
3313 bdevname(rdev->bdev,b));
3314 md_kick_rdev_from_array(rdev);
3317 super_types[mddev->major_version].
3318 validate_super(mddev, freshest);
3320 i = 0;
3321 rdev_for_each_safe(rdev, tmp, mddev) {
3322 if (mddev->max_disks &&
3323 (rdev->desc_nr >= mddev->max_disks ||
3324 i > mddev->max_disks)) {
3325 pr_warn("md: %s: %s: only %d devices permitted\n",
3326 mdname(mddev), bdevname(rdev->bdev, b),
3327 mddev->max_disks);
3328 md_kick_rdev_from_array(rdev);
3329 continue;
3331 if (rdev != freshest) {
3332 if (super_types[mddev->major_version].
3333 validate_super(mddev, rdev)) {
3334 pr_warn("md: kicking non-fresh %s from array!\n",
3335 bdevname(rdev->bdev,b));
3336 md_kick_rdev_from_array(rdev);
3337 continue;
3340 if (mddev->level == LEVEL_MULTIPATH) {
3341 rdev->desc_nr = i++;
3342 rdev->raid_disk = rdev->desc_nr;
3343 set_bit(In_sync, &rdev->flags);
3344 } else if (rdev->raid_disk >=
3345 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3346 !test_bit(Journal, &rdev->flags)) {
3347 rdev->raid_disk = -1;
3348 clear_bit(In_sync, &rdev->flags);
3353 /* Read a fixed-point number.
3354 * Numbers in sysfs attributes should be in "standard" units where
3355 * possible, so time should be in seconds.
3356 * However we internally use a a much smaller unit such as
3357 * milliseconds or jiffies.
3358 * This function takes a decimal number with a possible fractional
3359 * component, and produces an integer which is the result of
3360 * multiplying that number by 10^'scale'.
3361 * all without any floating-point arithmetic.
3363 int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3365 unsigned long result = 0;
3366 long decimals = -1;
3367 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3368 if (*cp == '.')
3369 decimals = 0;
3370 else if (decimals < scale) {
3371 unsigned int value;
3372 value = *cp - '0';
3373 result = result * 10 + value;
3374 if (decimals >= 0)
3375 decimals++;
3377 cp++;
3379 if (*cp == '\n')
3380 cp++;
3381 if (*cp)
3382 return -EINVAL;
3383 if (decimals < 0)
3384 decimals = 0;
3385 while (decimals < scale) {
3386 result *= 10;
3387 decimals ++;
3389 *res = result;
3390 return 0;
3393 static ssize_t
3394 safe_delay_show(struct mddev *mddev, char *page)
3396 int msec = (mddev->safemode_delay*1000)/HZ;
3397 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3399 static ssize_t
3400 safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
3402 unsigned long msec;
3404 if (mddev_is_clustered(mddev)) {
3405 pr_warn("md: Safemode is disabled for clustered mode\n");
3406 return -EINVAL;
3409 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
3410 return -EINVAL;
3411 if (msec == 0)
3412 mddev->safemode_delay = 0;
3413 else {
3414 unsigned long old_delay = mddev->safemode_delay;
3415 unsigned long new_delay = (msec*HZ)/1000;
3417 if (new_delay == 0)
3418 new_delay = 1;
3419 mddev->safemode_delay = new_delay;
3420 if (new_delay < old_delay || old_delay == 0)
3421 mod_timer(&mddev->safemode_timer, jiffies+1);
3423 return len;
3425 static struct md_sysfs_entry md_safe_delay =
3426 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
3428 static ssize_t
3429 level_show(struct mddev *mddev, char *page)
3431 struct md_personality *p;
3432 int ret;
3433 spin_lock(&mddev->lock);
3434 p = mddev->pers;
3435 if (p)
3436 ret = sprintf(page, "%s\n", p->name);
3437 else if (mddev->clevel[0])
3438 ret = sprintf(page, "%s\n", mddev->clevel);
3439 else if (mddev->level != LEVEL_NONE)
3440 ret = sprintf(page, "%d\n", mddev->level);
3441 else
3442 ret = 0;
3443 spin_unlock(&mddev->lock);
3444 return ret;
3447 static ssize_t
3448 level_store(struct mddev *mddev, const char *buf, size_t len)
3450 char clevel[16];
3451 ssize_t rv;
3452 size_t slen = len;
3453 struct md_personality *pers, *oldpers;
3454 long level;
3455 void *priv, *oldpriv;
3456 struct md_rdev *rdev;
3458 if (slen == 0 || slen >= sizeof(clevel))
3459 return -EINVAL;
3461 rv = mddev_lock(mddev);
3462 if (rv)
3463 return rv;
3465 if (mddev->pers == NULL) {
3466 strncpy(mddev->clevel, buf, slen);
3467 if (mddev->clevel[slen-1] == '\n')
3468 slen--;
3469 mddev->clevel[slen] = 0;
3470 mddev->level = LEVEL_NONE;
3471 rv = len;
3472 goto out_unlock;
3474 rv = -EROFS;
3475 if (mddev->ro)
3476 goto out_unlock;
3478 /* request to change the personality. Need to ensure:
3479 * - array is not engaged in resync/recovery/reshape
3480 * - old personality can be suspended
3481 * - new personality will access other array.
3484 rv = -EBUSY;
3485 if (mddev->sync_thread ||
3486 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3487 mddev->reshape_position != MaxSector ||
3488 mddev->sysfs_active)
3489 goto out_unlock;
3491 rv = -EINVAL;
3492 if (!mddev->pers->quiesce) {
3493 pr_warn("md: %s: %s does not support online personality change\n",
3494 mdname(mddev), mddev->pers->name);
3495 goto out_unlock;
3498 /* Now find the new personality */
3499 strncpy(clevel, buf, slen);
3500 if (clevel[slen-1] == '\n')
3501 slen--;
3502 clevel[slen] = 0;
3503 if (kstrtol(clevel, 10, &level))
3504 level = LEVEL_NONE;
3506 if (request_module("md-%s", clevel) != 0)
3507 request_module("md-level-%s", clevel);
3508 spin_lock(&pers_lock);
3509 pers = find_pers(level, clevel);
3510 if (!pers || !try_module_get(pers->owner)) {
3511 spin_unlock(&pers_lock);
3512 pr_warn("md: personality %s not loaded\n", clevel);
3513 rv = -EINVAL;
3514 goto out_unlock;
3516 spin_unlock(&pers_lock);
3518 if (pers == mddev->pers) {
3519 /* Nothing to do! */
3520 module_put(pers->owner);
3521 rv = len;
3522 goto out_unlock;
3524 if (!pers->takeover) {
3525 module_put(pers->owner);
3526 pr_warn("md: %s: %s does not support personality takeover\n",
3527 mdname(mddev), clevel);
3528 rv = -EINVAL;
3529 goto out_unlock;
3532 rdev_for_each(rdev, mddev)
3533 rdev->new_raid_disk = rdev->raid_disk;
3535 /* ->takeover must set new_* and/or delta_disks
3536 * if it succeeds, and may set them when it fails.
3538 priv = pers->takeover(mddev);
3539 if (IS_ERR(priv)) {
3540 mddev->new_level = mddev->level;
3541 mddev->new_layout = mddev->layout;
3542 mddev->new_chunk_sectors = mddev->chunk_sectors;
3543 mddev->raid_disks -= mddev->delta_disks;
3544 mddev->delta_disks = 0;
3545 mddev->reshape_backwards = 0;
3546 module_put(pers->owner);
3547 pr_warn("md: %s: %s would not accept array\n",
3548 mdname(mddev), clevel);
3549 rv = PTR_ERR(priv);
3550 goto out_unlock;
3553 /* Looks like we have a winner */
3554 mddev_suspend(mddev);
3555 mddev_detach(mddev);
3557 spin_lock(&mddev->lock);
3558 oldpers = mddev->pers;
3559 oldpriv = mddev->private;
3560 mddev->pers = pers;
3561 mddev->private = priv;
3562 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3563 mddev->level = mddev->new_level;
3564 mddev->layout = mddev->new_layout;
3565 mddev->chunk_sectors = mddev->new_chunk_sectors;
3566 mddev->delta_disks = 0;
3567 mddev->reshape_backwards = 0;
3568 mddev->degraded = 0;
3569 spin_unlock(&mddev->lock);
3571 if (oldpers->sync_request == NULL &&
3572 mddev->external) {
3573 /* We are converting from a no-redundancy array
3574 * to a redundancy array and metadata is managed
3575 * externally so we need to be sure that writes
3576 * won't block due to a need to transition
3577 * clean->dirty
3578 * until external management is started.
3580 mddev->in_sync = 0;
3581 mddev->safemode_delay = 0;
3582 mddev->safemode = 0;
3585 oldpers->free(mddev, oldpriv);
3587 if (oldpers->sync_request == NULL &&
3588 pers->sync_request != NULL) {
3589 /* need to add the md_redundancy_group */
3590 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3591 pr_warn("md: cannot register extra attributes for %s\n",
3592 mdname(mddev));
3593 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3595 if (oldpers->sync_request != NULL &&
3596 pers->sync_request == NULL) {
3597 /* need to remove the md_redundancy_group */
3598 if (mddev->to_remove == NULL)
3599 mddev->to_remove = &md_redundancy_group;
3602 module_put(oldpers->owner);
3604 rdev_for_each(rdev, mddev) {
3605 if (rdev->raid_disk < 0)
3606 continue;
3607 if (rdev->new_raid_disk >= mddev->raid_disks)
3608 rdev->new_raid_disk = -1;
3609 if (rdev->new_raid_disk == rdev->raid_disk)
3610 continue;
3611 sysfs_unlink_rdev(mddev, rdev);
3613 rdev_for_each(rdev, mddev) {
3614 if (rdev->raid_disk < 0)
3615 continue;
3616 if (rdev->new_raid_disk == rdev->raid_disk)
3617 continue;
3618 rdev->raid_disk = rdev->new_raid_disk;
3619 if (rdev->raid_disk < 0)
3620 clear_bit(In_sync, &rdev->flags);
3621 else {
3622 if (sysfs_link_rdev(mddev, rdev))
3623 pr_warn("md: cannot register rd%d for %s after level change\n",
3624 rdev->raid_disk, mdname(mddev));
3628 if (pers->sync_request == NULL) {
3629 /* this is now an array without redundancy, so
3630 * it must always be in_sync
3632 mddev->in_sync = 1;
3633 del_timer_sync(&mddev->safemode_timer);
3635 blk_set_stacking_limits(&mddev->queue->limits);
3636 pers->run(mddev);
3637 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
3638 mddev_resume(mddev);
3639 if (!mddev->thread)
3640 md_update_sb(mddev, 1);
3641 sysfs_notify(&mddev->kobj, NULL, "level");
3642 md_new_event(mddev);
3643 rv = len;
3644 out_unlock:
3645 mddev_unlock(mddev);
3646 return rv;
3649 static struct md_sysfs_entry md_level =
3650 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
3652 static ssize_t
3653 layout_show(struct mddev *mddev, char *page)
3655 /* just a number, not meaningful for all levels */
3656 if (mddev->reshape_position != MaxSector &&
3657 mddev->layout != mddev->new_layout)
3658 return sprintf(page, "%d (%d)\n",
3659 mddev->new_layout, mddev->layout);
3660 return sprintf(page, "%d\n", mddev->layout);
3663 static ssize_t
3664 layout_store(struct mddev *mddev, const char *buf, size_t len)
3666 unsigned int n;
3667 int err;
3669 err = kstrtouint(buf, 10, &n);
3670 if (err < 0)
3671 return err;
3672 err = mddev_lock(mddev);
3673 if (err)
3674 return err;
3676 if (mddev->pers) {
3677 if (mddev->pers->check_reshape == NULL)
3678 err = -EBUSY;
3679 else if (mddev->ro)
3680 err = -EROFS;
3681 else {
3682 mddev->new_layout = n;
3683 err = mddev->pers->check_reshape(mddev);
3684 if (err)
3685 mddev->new_layout = mddev->layout;
3687 } else {
3688 mddev->new_layout = n;
3689 if (mddev->reshape_position == MaxSector)
3690 mddev->layout = n;
3692 mddev_unlock(mddev);
3693 return err ?: len;
3695 static struct md_sysfs_entry md_layout =
3696 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
3698 static ssize_t
3699 raid_disks_show(struct mddev *mddev, char *page)
3701 if (mddev->raid_disks == 0)
3702 return 0;
3703 if (mddev->reshape_position != MaxSector &&
3704 mddev->delta_disks != 0)
3705 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3706 mddev->raid_disks - mddev->delta_disks);
3707 return sprintf(page, "%d\n", mddev->raid_disks);
3710 static int update_raid_disks(struct mddev *mddev, int raid_disks);
3712 static ssize_t
3713 raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
3715 unsigned int n;
3716 int err;
3718 err = kstrtouint(buf, 10, &n);
3719 if (err < 0)
3720 return err;
3722 err = mddev_lock(mddev);
3723 if (err)
3724 return err;
3725 if (mddev->pers)
3726 err = update_raid_disks(mddev, n);
3727 else if (mddev->reshape_position != MaxSector) {
3728 struct md_rdev *rdev;
3729 int olddisks = mddev->raid_disks - mddev->delta_disks;
3731 err = -EINVAL;
3732 rdev_for_each(rdev, mddev) {
3733 if (olddisks < n &&
3734 rdev->data_offset < rdev->new_data_offset)
3735 goto out_unlock;
3736 if (olddisks > n &&
3737 rdev->data_offset > rdev->new_data_offset)
3738 goto out_unlock;
3740 err = 0;
3741 mddev->delta_disks = n - olddisks;
3742 mddev->raid_disks = n;
3743 mddev->reshape_backwards = (mddev->delta_disks < 0);
3744 } else
3745 mddev->raid_disks = n;
3746 out_unlock:
3747 mddev_unlock(mddev);
3748 return err ? err : len;
3750 static struct md_sysfs_entry md_raid_disks =
3751 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
3753 static ssize_t
3754 chunk_size_show(struct mddev *mddev, char *page)
3756 if (mddev->reshape_position != MaxSector &&
3757 mddev->chunk_sectors != mddev->new_chunk_sectors)
3758 return sprintf(page, "%d (%d)\n",
3759 mddev->new_chunk_sectors << 9,
3760 mddev->chunk_sectors << 9);
3761 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
3764 static ssize_t
3765 chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
3767 unsigned long n;
3768 int err;
3770 err = kstrtoul(buf, 10, &n);
3771 if (err < 0)
3772 return err;
3774 err = mddev_lock(mddev);
3775 if (err)
3776 return err;
3777 if (mddev->pers) {
3778 if (mddev->pers->check_reshape == NULL)
3779 err = -EBUSY;
3780 else if (mddev->ro)
3781 err = -EROFS;
3782 else {
3783 mddev->new_chunk_sectors = n >> 9;
3784 err = mddev->pers->check_reshape(mddev);
3785 if (err)
3786 mddev->new_chunk_sectors = mddev->chunk_sectors;
3788 } else {
3789 mddev->new_chunk_sectors = n >> 9;
3790 if (mddev->reshape_position == MaxSector)
3791 mddev->chunk_sectors = n >> 9;
3793 mddev_unlock(mddev);
3794 return err ?: len;
3796 static struct md_sysfs_entry md_chunk_size =
3797 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
3799 static ssize_t
3800 resync_start_show(struct mddev *mddev, char *page)
3802 if (mddev->recovery_cp == MaxSector)
3803 return sprintf(page, "none\n");
3804 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3807 static ssize_t
3808 resync_start_store(struct mddev *mddev, const char *buf, size_t len)
3810 unsigned long long n;
3811 int err;
3813 if (cmd_match(buf, "none"))
3814 n = MaxSector;
3815 else {
3816 err = kstrtoull(buf, 10, &n);
3817 if (err < 0)
3818 return err;
3819 if (n != (sector_t)n)
3820 return -EINVAL;
3823 err = mddev_lock(mddev);
3824 if (err)
3825 return err;
3826 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3827 err = -EBUSY;
3829 if (!err) {
3830 mddev->recovery_cp = n;
3831 if (mddev->pers)
3832 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
3834 mddev_unlock(mddev);
3835 return err ?: len;
3837 static struct md_sysfs_entry md_resync_start =
3838 __ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
3839 resync_start_show, resync_start_store);
3842 * The array state can be:
3844 * clear
3845 * No devices, no size, no level
3846 * Equivalent to STOP_ARRAY ioctl
3847 * inactive
3848 * May have some settings, but array is not active
3849 * all IO results in error
3850 * When written, doesn't tear down array, but just stops it
3851 * suspended (not supported yet)
3852 * All IO requests will block. The array can be reconfigured.
3853 * Writing this, if accepted, will block until array is quiescent
3854 * readonly
3855 * no resync can happen. no superblocks get written.
3856 * write requests fail
3857 * read-auto
3858 * like readonly, but behaves like 'clean' on a write request.
3860 * clean - no pending writes, but otherwise active.
3861 * When written to inactive array, starts without resync
3862 * If a write request arrives then
3863 * if metadata is known, mark 'dirty' and switch to 'active'.
3864 * if not known, block and switch to write-pending
3865 * If written to an active array that has pending writes, then fails.
3866 * active
3867 * fully active: IO and resync can be happening.
3868 * When written to inactive array, starts with resync
3870 * write-pending
3871 * clean, but writes are blocked waiting for 'active' to be written.
3873 * active-idle
3874 * like active, but no writes have been seen for a while (100msec).
3877 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3878 write_pending, active_idle, bad_word};
3879 static char *array_states[] = {
3880 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3881 "write-pending", "active-idle", NULL };
3883 static int match_word(const char *word, char **list)
3885 int n;
3886 for (n=0; list[n]; n++)
3887 if (cmd_match(word, list[n]))
3888 break;
3889 return n;
3892 static ssize_t
3893 array_state_show(struct mddev *mddev, char *page)
3895 enum array_state st = inactive;
3897 if (mddev->pers)
3898 switch(mddev->ro) {
3899 case 1:
3900 st = readonly;
3901 break;
3902 case 2:
3903 st = read_auto;
3904 break;
3905 case 0:
3906 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
3907 st = write_pending;
3908 else if (mddev->in_sync)
3909 st = clean;
3910 else if (mddev->safemode)
3911 st = active_idle;
3912 else
3913 st = active;
3915 else {
3916 if (list_empty(&mddev->disks) &&
3917 mddev->raid_disks == 0 &&
3918 mddev->dev_sectors == 0)
3919 st = clear;
3920 else
3921 st = inactive;
3923 return sprintf(page, "%s\n", array_states[st]);
3926 static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
3927 static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
3928 static int do_md_run(struct mddev *mddev);
3929 static int restart_array(struct mddev *mddev);
3931 static ssize_t
3932 array_state_store(struct mddev *mddev, const char *buf, size_t len)
3934 int err;
3935 enum array_state st = match_word(buf, array_states);
3937 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
3938 /* don't take reconfig_mutex when toggling between
3939 * clean and active
3941 spin_lock(&mddev->lock);
3942 if (st == active) {
3943 restart_array(mddev);
3944 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
3945 md_wakeup_thread(mddev->thread);
3946 wake_up(&mddev->sb_wait);
3947 err = 0;
3948 } else /* st == clean */ {
3949 restart_array(mddev);
3950 if (atomic_read(&mddev->writes_pending) == 0) {
3951 if (mddev->in_sync == 0) {
3952 mddev->in_sync = 1;
3953 if (mddev->safemode == 1)
3954 mddev->safemode = 0;
3955 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
3957 err = 0;
3958 } else
3959 err = -EBUSY;
3961 if (!err)
3962 sysfs_notify_dirent_safe(mddev->sysfs_state);
3963 spin_unlock(&mddev->lock);
3964 return err ?: len;
3966 err = mddev_lock(mddev);
3967 if (err)
3968 return err;
3969 err = -EINVAL;
3970 switch(st) {
3971 case bad_word:
3972 break;
3973 case clear:
3974 /* stopping an active array */
3975 err = do_md_stop(mddev, 0, NULL);
3976 break;
3977 case inactive:
3978 /* stopping an active array */
3979 if (mddev->pers)
3980 err = do_md_stop(mddev, 2, NULL);
3981 else
3982 err = 0; /* already inactive */
3983 break;
3984 case suspended:
3985 break; /* not supported yet */
3986 case readonly:
3987 if (mddev->pers)
3988 err = md_set_readonly(mddev, NULL);
3989 else {
3990 mddev->ro = 1;
3991 set_disk_ro(mddev->gendisk, 1);
3992 err = do_md_run(mddev);
3994 break;
3995 case read_auto:
3996 if (mddev->pers) {
3997 if (mddev->ro == 0)
3998 err = md_set_readonly(mddev, NULL);
3999 else if (mddev->ro == 1)
4000 err = restart_array(mddev);
4001 if (err == 0) {
4002 mddev->ro = 2;
4003 set_disk_ro(mddev->gendisk, 0);
4005 } else {
4006 mddev->ro = 2;
4007 err = do_md_run(mddev);
4009 break;
4010 case clean:
4011 if (mddev->pers) {
4012 err = restart_array(mddev);
4013 if (err)
4014 break;
4015 spin_lock(&mddev->lock);
4016 if (atomic_read(&mddev->writes_pending) == 0) {
4017 if (mddev->in_sync == 0) {
4018 mddev->in_sync = 1;
4019 if (mddev->safemode == 1)
4020 mddev->safemode = 0;
4021 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
4023 err = 0;
4024 } else
4025 err = -EBUSY;
4026 spin_unlock(&mddev->lock);
4027 } else
4028 err = -EINVAL;
4029 break;
4030 case active:
4031 if (mddev->pers) {
4032 err = restart_array(mddev);
4033 if (err)
4034 break;
4035 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
4036 wake_up(&mddev->sb_wait);
4037 err = 0;
4038 } else {
4039 mddev->ro = 0;
4040 set_disk_ro(mddev->gendisk, 0);
4041 err = do_md_run(mddev);
4043 break;
4044 case write_pending:
4045 case active_idle:
4046 /* these cannot be set */
4047 break;
4050 if (!err) {
4051 if (mddev->hold_active == UNTIL_IOCTL)
4052 mddev->hold_active = 0;
4053 sysfs_notify_dirent_safe(mddev->sysfs_state);
4055 mddev_unlock(mddev);
4056 return err ?: len;
4058 static struct md_sysfs_entry md_array_state =
4059 __ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
4061 static ssize_t
4062 max_corrected_read_errors_show(struct mddev *mddev, char *page) {
4063 return sprintf(page, "%d\n",
4064 atomic_read(&mddev->max_corr_read_errors));
4067 static ssize_t
4068 max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
4070 unsigned int n;
4071 int rv;
4073 rv = kstrtouint(buf, 10, &n);
4074 if (rv < 0)
4075 return rv;
4076 atomic_set(&mddev->max_corr_read_errors, n);
4077 return len;
4080 static struct md_sysfs_entry max_corr_read_errors =
4081 __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4082 max_corrected_read_errors_store);
4084 static ssize_t
4085 null_show(struct mddev *mddev, char *page)
4087 return -EINVAL;
4090 static ssize_t
4091 new_dev_store(struct mddev *mddev, const char *buf, size_t len)
4093 /* buf must be %d:%d\n? giving major and minor numbers */
4094 /* The new device is added to the array.
4095 * If the array has a persistent superblock, we read the
4096 * superblock to initialise info and check validity.
4097 * Otherwise, only checking done is that in bind_rdev_to_array,
4098 * which mainly checks size.
4100 char *e;
4101 int major = simple_strtoul(buf, &e, 10);
4102 int minor;
4103 dev_t dev;
4104 struct md_rdev *rdev;
4105 int err;
4107 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4108 return -EINVAL;
4109 minor = simple_strtoul(e+1, &e, 10);
4110 if (*e && *e != '\n')
4111 return -EINVAL;
4112 dev = MKDEV(major, minor);
4113 if (major != MAJOR(dev) ||
4114 minor != MINOR(dev))
4115 return -EOVERFLOW;
4117 flush_workqueue(md_misc_wq);
4119 err = mddev_lock(mddev);
4120 if (err)
4121 return err;
4122 if (mddev->persistent) {
4123 rdev = md_import_device(dev, mddev->major_version,
4124 mddev->minor_version);
4125 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
4126 struct md_rdev *rdev0
4127 = list_entry(mddev->disks.next,
4128 struct md_rdev, same_set);
4129 err = super_types[mddev->major_version]
4130 .load_super(rdev, rdev0, mddev->minor_version);
4131 if (err < 0)
4132 goto out;
4134 } else if (mddev->external)
4135 rdev = md_import_device(dev, -2, -1);
4136 else
4137 rdev = md_import_device(dev, -1, -1);
4139 if (IS_ERR(rdev)) {
4140 mddev_unlock(mddev);
4141 return PTR_ERR(rdev);
4143 err = bind_rdev_to_array(rdev, mddev);
4144 out:
4145 if (err)
4146 export_rdev(rdev);
4147 mddev_unlock(mddev);
4148 return err ? err : len;
4151 static struct md_sysfs_entry md_new_device =
4152 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
4154 static ssize_t
4155 bitmap_store(struct mddev *mddev, const char *buf, size_t len)
4157 char *end;
4158 unsigned long chunk, end_chunk;
4159 int err;
4161 err = mddev_lock(mddev);
4162 if (err)
4163 return err;
4164 if (!mddev->bitmap)
4165 goto out;
4166 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4167 while (*buf) {
4168 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4169 if (buf == end) break;
4170 if (*end == '-') { /* range */
4171 buf = end + 1;
4172 end_chunk = simple_strtoul(buf, &end, 0);
4173 if (buf == end) break;
4175 if (*end && !isspace(*end)) break;
4176 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
4177 buf = skip_spaces(end);
4179 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4180 out:
4181 mddev_unlock(mddev);
4182 return len;
4185 static struct md_sysfs_entry md_bitmap =
4186 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4188 static ssize_t
4189 size_show(struct mddev *mddev, char *page)
4191 return sprintf(page, "%llu\n",
4192 (unsigned long long)mddev->dev_sectors / 2);
4195 static int update_size(struct mddev *mddev, sector_t num_sectors);
4197 static ssize_t
4198 size_store(struct mddev *mddev, const char *buf, size_t len)
4200 /* If array is inactive, we can reduce the component size, but
4201 * not increase it (except from 0).
4202 * If array is active, we can try an on-line resize
4204 sector_t sectors;
4205 int err = strict_blocks_to_sectors(buf, &sectors);
4207 if (err < 0)
4208 return err;
4209 err = mddev_lock(mddev);
4210 if (err)
4211 return err;
4212 if (mddev->pers) {
4213 err = update_size(mddev, sectors);
4214 if (err == 0)
4215 md_update_sb(mddev, 1);
4216 } else {
4217 if (mddev->dev_sectors == 0 ||
4218 mddev->dev_sectors > sectors)
4219 mddev->dev_sectors = sectors;
4220 else
4221 err = -ENOSPC;
4223 mddev_unlock(mddev);
4224 return err ? err : len;
4227 static struct md_sysfs_entry md_size =
4228 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
4230 /* Metadata version.
4231 * This is one of
4232 * 'none' for arrays with no metadata (good luck...)
4233 * 'external' for arrays with externally managed metadata,
4234 * or N.M for internally known formats
4236 static ssize_t
4237 metadata_show(struct mddev *mddev, char *page)
4239 if (mddev->persistent)
4240 return sprintf(page, "%d.%d\n",
4241 mddev->major_version, mddev->minor_version);
4242 else if (mddev->external)
4243 return sprintf(page, "external:%s\n", mddev->metadata_type);
4244 else
4245 return sprintf(page, "none\n");
4248 static ssize_t
4249 metadata_store(struct mddev *mddev, const char *buf, size_t len)
4251 int major, minor;
4252 char *e;
4253 int err;
4254 /* Changing the details of 'external' metadata is
4255 * always permitted. Otherwise there must be
4256 * no devices attached to the array.
4259 err = mddev_lock(mddev);
4260 if (err)
4261 return err;
4262 err = -EBUSY;
4263 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4265 else if (!list_empty(&mddev->disks))
4266 goto out_unlock;
4268 err = 0;
4269 if (cmd_match(buf, "none")) {
4270 mddev->persistent = 0;
4271 mddev->external = 0;
4272 mddev->major_version = 0;
4273 mddev->minor_version = 90;
4274 goto out_unlock;
4276 if (strncmp(buf, "external:", 9) == 0) {
4277 size_t namelen = len-9;
4278 if (namelen >= sizeof(mddev->metadata_type))
4279 namelen = sizeof(mddev->metadata_type)-1;
4280 strncpy(mddev->metadata_type, buf+9, namelen);
4281 mddev->metadata_type[namelen] = 0;
4282 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4283 mddev->metadata_type[--namelen] = 0;
4284 mddev->persistent = 0;
4285 mddev->external = 1;
4286 mddev->major_version = 0;
4287 mddev->minor_version = 90;
4288 goto out_unlock;
4290 major = simple_strtoul(buf, &e, 10);
4291 err = -EINVAL;
4292 if (e==buf || *e != '.')
4293 goto out_unlock;
4294 buf = e+1;
4295 minor = simple_strtoul(buf, &e, 10);
4296 if (e==buf || (*e && *e != '\n') )
4297 goto out_unlock;
4298 err = -ENOENT;
4299 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
4300 goto out_unlock;
4301 mddev->major_version = major;
4302 mddev->minor_version = minor;
4303 mddev->persistent = 1;
4304 mddev->external = 0;
4305 err = 0;
4306 out_unlock:
4307 mddev_unlock(mddev);
4308 return err ?: len;
4311 static struct md_sysfs_entry md_metadata =
4312 __ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
4314 static ssize_t
4315 action_show(struct mddev *mddev, char *page)
4317 char *type = "idle";
4318 unsigned long recovery = mddev->recovery;
4319 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
4320 type = "frozen";
4321 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4322 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4323 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
4324 type = "reshape";
4325 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4326 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
4327 type = "resync";
4328 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
4329 type = "check";
4330 else
4331 type = "repair";
4332 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
4333 type = "recover";
4334 else if (mddev->reshape_position != MaxSector)
4335 type = "reshape";
4337 return sprintf(page, "%s\n", type);
4340 static ssize_t
4341 action_store(struct mddev *mddev, const char *page, size_t len)
4343 if (!mddev->pers || !mddev->pers->sync_request)
4344 return -EINVAL;
4347 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
4348 if (cmd_match(page, "frozen"))
4349 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4350 else
4351 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4352 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4353 mddev_lock(mddev) == 0) {
4354 flush_workqueue(md_misc_wq);
4355 if (mddev->sync_thread) {
4356 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4357 md_reap_sync_thread(mddev);
4359 mddev_unlock(mddev);
4361 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4362 return -EBUSY;
4363 else if (cmd_match(page, "resync"))
4364 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4365 else if (cmd_match(page, "recover")) {
4366 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4367 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4368 } else if (cmd_match(page, "reshape")) {
4369 int err;
4370 if (mddev->pers->start_reshape == NULL)
4371 return -EINVAL;
4372 err = mddev_lock(mddev);
4373 if (!err) {
4374 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4375 err = -EBUSY;
4376 else {
4377 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4378 err = mddev->pers->start_reshape(mddev);
4380 mddev_unlock(mddev);
4382 if (err)
4383 return err;
4384 sysfs_notify(&mddev->kobj, NULL, "degraded");
4385 } else {
4386 if (cmd_match(page, "check"))
4387 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4388 else if (!cmd_match(page, "repair"))
4389 return -EINVAL;
4390 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4391 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4392 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4394 if (mddev->ro == 2) {
4395 /* A write to sync_action is enough to justify
4396 * canceling read-auto mode
4398 mddev->ro = 0;
4399 md_wakeup_thread(mddev->sync_thread);
4401 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4402 md_wakeup_thread(mddev->thread);
4403 sysfs_notify_dirent_safe(mddev->sysfs_action);
4404 return len;
4407 static struct md_sysfs_entry md_scan_mode =
4408 __ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
4410 static ssize_t
4411 last_sync_action_show(struct mddev *mddev, char *page)
4413 return sprintf(page, "%s\n", mddev->last_sync_action);
4416 static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4418 static ssize_t
4419 mismatch_cnt_show(struct mddev *mddev, char *page)
4421 return sprintf(page, "%llu\n",
4422 (unsigned long long)
4423 atomic64_read(&mddev->resync_mismatches));
4426 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
4428 static ssize_t
4429 sync_min_show(struct mddev *mddev, char *page)
4431 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4432 mddev->sync_speed_min ? "local": "system");
4435 static ssize_t
4436 sync_min_store(struct mddev *mddev, const char *buf, size_t len)
4438 unsigned int min;
4439 int rv;
4441 if (strncmp(buf, "system", 6)==0) {
4442 min = 0;
4443 } else {
4444 rv = kstrtouint(buf, 10, &min);
4445 if (rv < 0)
4446 return rv;
4447 if (min == 0)
4448 return -EINVAL;
4450 mddev->sync_speed_min = min;
4451 return len;
4454 static struct md_sysfs_entry md_sync_min =
4455 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4457 static ssize_t
4458 sync_max_show(struct mddev *mddev, char *page)
4460 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4461 mddev->sync_speed_max ? "local": "system");
4464 static ssize_t
4465 sync_max_store(struct mddev *mddev, const char *buf, size_t len)
4467 unsigned int max;
4468 int rv;
4470 if (strncmp(buf, "system", 6)==0) {
4471 max = 0;
4472 } else {
4473 rv = kstrtouint(buf, 10, &max);
4474 if (rv < 0)
4475 return rv;
4476 if (max == 0)
4477 return -EINVAL;
4479 mddev->sync_speed_max = max;
4480 return len;
4483 static struct md_sysfs_entry md_sync_max =
4484 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4486 static ssize_t
4487 degraded_show(struct mddev *mddev, char *page)
4489 return sprintf(page, "%d\n", mddev->degraded);
4491 static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
4493 static ssize_t
4494 sync_force_parallel_show(struct mddev *mddev, char *page)
4496 return sprintf(page, "%d\n", mddev->parallel_resync);
4499 static ssize_t
4500 sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
4502 long n;
4504 if (kstrtol(buf, 10, &n))
4505 return -EINVAL;
4507 if (n != 0 && n != 1)
4508 return -EINVAL;
4510 mddev->parallel_resync = n;
4512 if (mddev->sync_thread)
4513 wake_up(&resync_wait);
4515 return len;
4518 /* force parallel resync, even with shared block devices */
4519 static struct md_sysfs_entry md_sync_force_parallel =
4520 __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4521 sync_force_parallel_show, sync_force_parallel_store);
4523 static ssize_t
4524 sync_speed_show(struct mddev *mddev, char *page)
4526 unsigned long resync, dt, db;
4527 if (mddev->curr_resync == 0)
4528 return sprintf(page, "none\n");
4529 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4530 dt = (jiffies - mddev->resync_mark) / HZ;
4531 if (!dt) dt++;
4532 db = resync - mddev->resync_mark_cnt;
4533 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
4536 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
4538 static ssize_t
4539 sync_completed_show(struct mddev *mddev, char *page)
4541 unsigned long long max_sectors, resync;
4543 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4544 return sprintf(page, "none\n");
4546 if (mddev->curr_resync == 1 ||
4547 mddev->curr_resync == 2)
4548 return sprintf(page, "delayed\n");
4550 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4551 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4552 max_sectors = mddev->resync_max_sectors;
4553 else
4554 max_sectors = mddev->dev_sectors;
4556 resync = mddev->curr_resync_completed;
4557 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
4560 static struct md_sysfs_entry md_sync_completed =
4561 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
4563 static ssize_t
4564 min_sync_show(struct mddev *mddev, char *page)
4566 return sprintf(page, "%llu\n",
4567 (unsigned long long)mddev->resync_min);
4569 static ssize_t
4570 min_sync_store(struct mddev *mddev, const char *buf, size_t len)
4572 unsigned long long min;
4573 int err;
4575 if (kstrtoull(buf, 10, &min))
4576 return -EINVAL;
4578 spin_lock(&mddev->lock);
4579 err = -EINVAL;
4580 if (min > mddev->resync_max)
4581 goto out_unlock;
4583 err = -EBUSY;
4584 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4585 goto out_unlock;
4587 /* Round down to multiple of 4K for safety */
4588 mddev->resync_min = round_down(min, 8);
4589 err = 0;
4591 out_unlock:
4592 spin_unlock(&mddev->lock);
4593 return err ?: len;
4596 static struct md_sysfs_entry md_min_sync =
4597 __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4599 static ssize_t
4600 max_sync_show(struct mddev *mddev, char *page)
4602 if (mddev->resync_max == MaxSector)
4603 return sprintf(page, "max\n");
4604 else
4605 return sprintf(page, "%llu\n",
4606 (unsigned long long)mddev->resync_max);
4608 static ssize_t
4609 max_sync_store(struct mddev *mddev, const char *buf, size_t len)
4611 int err;
4612 spin_lock(&mddev->lock);
4613 if (strncmp(buf, "max", 3) == 0)
4614 mddev->resync_max = MaxSector;
4615 else {
4616 unsigned long long max;
4617 int chunk;
4619 err = -EINVAL;
4620 if (kstrtoull(buf, 10, &max))
4621 goto out_unlock;
4622 if (max < mddev->resync_min)
4623 goto out_unlock;
4625 err = -EBUSY;
4626 if (max < mddev->resync_max &&
4627 mddev->ro == 0 &&
4628 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4629 goto out_unlock;
4631 /* Must be a multiple of chunk_size */
4632 chunk = mddev->chunk_sectors;
4633 if (chunk) {
4634 sector_t temp = max;
4636 err = -EINVAL;
4637 if (sector_div(temp, chunk))
4638 goto out_unlock;
4640 mddev->resync_max = max;
4642 wake_up(&mddev->recovery_wait);
4643 err = 0;
4644 out_unlock:
4645 spin_unlock(&mddev->lock);
4646 return err ?: len;
4649 static struct md_sysfs_entry md_max_sync =
4650 __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4652 static ssize_t
4653 suspend_lo_show(struct mddev *mddev, char *page)
4655 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4658 static ssize_t
4659 suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
4661 unsigned long long old, new;
4662 int err;
4664 err = kstrtoull(buf, 10, &new);
4665 if (err < 0)
4666 return err;
4667 if (new != (sector_t)new)
4668 return -EINVAL;
4670 err = mddev_lock(mddev);
4671 if (err)
4672 return err;
4673 err = -EINVAL;
4674 if (mddev->pers == NULL ||
4675 mddev->pers->quiesce == NULL)
4676 goto unlock;
4677 old = mddev->suspend_lo;
4678 mddev->suspend_lo = new;
4679 if (new >= old)
4680 /* Shrinking suspended region */
4681 mddev->pers->quiesce(mddev, 2);
4682 else {
4683 /* Expanding suspended region - need to wait */
4684 mddev->pers->quiesce(mddev, 1);
4685 mddev->pers->quiesce(mddev, 0);
4687 err = 0;
4688 unlock:
4689 mddev_unlock(mddev);
4690 return err ?: len;
4692 static struct md_sysfs_entry md_suspend_lo =
4693 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4695 static ssize_t
4696 suspend_hi_show(struct mddev *mddev, char *page)
4698 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4701 static ssize_t
4702 suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
4704 unsigned long long old, new;
4705 int err;
4707 err = kstrtoull(buf, 10, &new);
4708 if (err < 0)
4709 return err;
4710 if (new != (sector_t)new)
4711 return -EINVAL;
4713 err = mddev_lock(mddev);
4714 if (err)
4715 return err;
4716 err = -EINVAL;
4717 if (mddev->pers == NULL ||
4718 mddev->pers->quiesce == NULL)
4719 goto unlock;
4720 old = mddev->suspend_hi;
4721 mddev->suspend_hi = new;
4722 if (new <= old)
4723 /* Shrinking suspended region */
4724 mddev->pers->quiesce(mddev, 2);
4725 else {
4726 /* Expanding suspended region - need to wait */
4727 mddev->pers->quiesce(mddev, 1);
4728 mddev->pers->quiesce(mddev, 0);
4730 err = 0;
4731 unlock:
4732 mddev_unlock(mddev);
4733 return err ?: len;
4735 static struct md_sysfs_entry md_suspend_hi =
4736 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4738 static ssize_t
4739 reshape_position_show(struct mddev *mddev, char *page)
4741 if (mddev->reshape_position != MaxSector)
4742 return sprintf(page, "%llu\n",
4743 (unsigned long long)mddev->reshape_position);
4744 strcpy(page, "none\n");
4745 return 5;
4748 static ssize_t
4749 reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
4751 struct md_rdev *rdev;
4752 unsigned long long new;
4753 int err;
4755 err = kstrtoull(buf, 10, &new);
4756 if (err < 0)
4757 return err;
4758 if (new != (sector_t)new)
4759 return -EINVAL;
4760 err = mddev_lock(mddev);
4761 if (err)
4762 return err;
4763 err = -EBUSY;
4764 if (mddev->pers)
4765 goto unlock;
4766 mddev->reshape_position = new;
4767 mddev->delta_disks = 0;
4768 mddev->reshape_backwards = 0;
4769 mddev->new_level = mddev->level;
4770 mddev->new_layout = mddev->layout;
4771 mddev->new_chunk_sectors = mddev->chunk_sectors;
4772 rdev_for_each(rdev, mddev)
4773 rdev->new_data_offset = rdev->data_offset;
4774 err = 0;
4775 unlock:
4776 mddev_unlock(mddev);
4777 return err ?: len;
4780 static struct md_sysfs_entry md_reshape_position =
4781 __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4782 reshape_position_store);
4784 static ssize_t
4785 reshape_direction_show(struct mddev *mddev, char *page)
4787 return sprintf(page, "%s\n",
4788 mddev->reshape_backwards ? "backwards" : "forwards");
4791 static ssize_t
4792 reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4794 int backwards = 0;
4795 int err;
4797 if (cmd_match(buf, "forwards"))
4798 backwards = 0;
4799 else if (cmd_match(buf, "backwards"))
4800 backwards = 1;
4801 else
4802 return -EINVAL;
4803 if (mddev->reshape_backwards == backwards)
4804 return len;
4806 err = mddev_lock(mddev);
4807 if (err)
4808 return err;
4809 /* check if we are allowed to change */
4810 if (mddev->delta_disks)
4811 err = -EBUSY;
4812 else if (mddev->persistent &&
4813 mddev->major_version == 0)
4814 err = -EINVAL;
4815 else
4816 mddev->reshape_backwards = backwards;
4817 mddev_unlock(mddev);
4818 return err ?: len;
4821 static struct md_sysfs_entry md_reshape_direction =
4822 __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4823 reshape_direction_store);
4825 static ssize_t
4826 array_size_show(struct mddev *mddev, char *page)
4828 if (mddev->external_size)
4829 return sprintf(page, "%llu\n",
4830 (unsigned long long)mddev->array_sectors/2);
4831 else
4832 return sprintf(page, "default\n");
4835 static ssize_t
4836 array_size_store(struct mddev *mddev, const char *buf, size_t len)
4838 sector_t sectors;
4839 int err;
4841 err = mddev_lock(mddev);
4842 if (err)
4843 return err;
4845 /* cluster raid doesn't support change array_sectors */
4846 if (mddev_is_clustered(mddev))
4847 return -EINVAL;
4849 if (strncmp(buf, "default", 7) == 0) {
4850 if (mddev->pers)
4851 sectors = mddev->pers->size(mddev, 0, 0);
4852 else
4853 sectors = mddev->array_sectors;
4855 mddev->external_size = 0;
4856 } else {
4857 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4858 err = -EINVAL;
4859 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4860 err = -E2BIG;
4861 else
4862 mddev->external_size = 1;
4865 if (!err) {
4866 mddev->array_sectors = sectors;
4867 if (mddev->pers) {
4868 set_capacity(mddev->gendisk, mddev->array_sectors);
4869 revalidate_disk(mddev->gendisk);
4872 mddev_unlock(mddev);
4873 return err ?: len;
4876 static struct md_sysfs_entry md_array_size =
4877 __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4878 array_size_store);
4880 static struct attribute *md_default_attrs[] = {
4881 &md_level.attr,
4882 &md_layout.attr,
4883 &md_raid_disks.attr,
4884 &md_chunk_size.attr,
4885 &md_size.attr,
4886 &md_resync_start.attr,
4887 &md_metadata.attr,
4888 &md_new_device.attr,
4889 &md_safe_delay.attr,
4890 &md_array_state.attr,
4891 &md_reshape_position.attr,
4892 &md_reshape_direction.attr,
4893 &md_array_size.attr,
4894 &max_corr_read_errors.attr,
4895 NULL,
4898 static struct attribute *md_redundancy_attrs[] = {
4899 &md_scan_mode.attr,
4900 &md_last_scan_mode.attr,
4901 &md_mismatches.attr,
4902 &md_sync_min.attr,
4903 &md_sync_max.attr,
4904 &md_sync_speed.attr,
4905 &md_sync_force_parallel.attr,
4906 &md_sync_completed.attr,
4907 &md_min_sync.attr,
4908 &md_max_sync.attr,
4909 &md_suspend_lo.attr,
4910 &md_suspend_hi.attr,
4911 &md_bitmap.attr,
4912 &md_degraded.attr,
4913 NULL,
4915 static struct attribute_group md_redundancy_group = {
4916 .name = NULL,
4917 .attrs = md_redundancy_attrs,
4920 static ssize_t
4921 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4923 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4924 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
4925 ssize_t rv;
4927 if (!entry->show)
4928 return -EIO;
4929 spin_lock(&all_mddevs_lock);
4930 if (list_empty(&mddev->all_mddevs)) {
4931 spin_unlock(&all_mddevs_lock);
4932 return -EBUSY;
4934 mddev_get(mddev);
4935 spin_unlock(&all_mddevs_lock);
4937 rv = entry->show(mddev, page);
4938 mddev_put(mddev);
4939 return rv;
4942 static ssize_t
4943 md_attr_store(struct kobject *kobj, struct attribute *attr,
4944 const char *page, size_t length)
4946 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4947 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
4948 ssize_t rv;
4950 if (!entry->store)
4951 return -EIO;
4952 if (!capable(CAP_SYS_ADMIN))
4953 return -EACCES;
4954 spin_lock(&all_mddevs_lock);
4955 if (list_empty(&mddev->all_mddevs)) {
4956 spin_unlock(&all_mddevs_lock);
4957 return -EBUSY;
4959 mddev_get(mddev);
4960 spin_unlock(&all_mddevs_lock);
4961 rv = entry->store(mddev, page, length);
4962 mddev_put(mddev);
4963 return rv;
4966 static void md_free(struct kobject *ko)
4968 struct mddev *mddev = container_of(ko, struct mddev, kobj);
4970 if (mddev->sysfs_state)
4971 sysfs_put(mddev->sysfs_state);
4973 if (mddev->queue)
4974 blk_cleanup_queue(mddev->queue);
4975 if (mddev->gendisk) {
4976 del_gendisk(mddev->gendisk);
4977 put_disk(mddev->gendisk);
4980 kfree(mddev);
4983 static const struct sysfs_ops md_sysfs_ops = {
4984 .show = md_attr_show,
4985 .store = md_attr_store,
4987 static struct kobj_type md_ktype = {
4988 .release = md_free,
4989 .sysfs_ops = &md_sysfs_ops,
4990 .default_attrs = md_default_attrs,
4993 int mdp_major = 0;
4995 static void mddev_delayed_delete(struct work_struct *ws)
4997 struct mddev *mddev = container_of(ws, struct mddev, del_work);
4999 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
5000 kobject_del(&mddev->kobj);
5001 kobject_put(&mddev->kobj);
5004 static int md_alloc(dev_t dev, char *name)
5006 static DEFINE_MUTEX(disks_mutex);
5007 struct mddev *mddev = mddev_find(dev);
5008 struct gendisk *disk;
5009 int partitioned;
5010 int shift;
5011 int unit;
5012 int error;
5014 if (!mddev)
5015 return -ENODEV;
5017 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
5018 shift = partitioned ? MdpMinorShift : 0;
5019 unit = MINOR(mddev->unit) >> shift;
5021 /* wait for any previous instance of this device to be
5022 * completely removed (mddev_delayed_delete).
5024 flush_workqueue(md_misc_wq);
5026 mutex_lock(&disks_mutex);
5027 error = -EEXIST;
5028 if (mddev->gendisk)
5029 goto abort;
5031 if (name) {
5032 /* Need to ensure that 'name' is not a duplicate.
5034 struct mddev *mddev2;
5035 spin_lock(&all_mddevs_lock);
5037 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5038 if (mddev2->gendisk &&
5039 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5040 spin_unlock(&all_mddevs_lock);
5041 goto abort;
5043 spin_unlock(&all_mddevs_lock);
5046 error = -ENOMEM;
5047 mddev->queue = blk_alloc_queue(GFP_KERNEL);
5048 if (!mddev->queue)
5049 goto abort;
5050 mddev->queue->queuedata = mddev;
5052 blk_queue_make_request(mddev->queue, md_make_request);
5053 blk_set_stacking_limits(&mddev->queue->limits);
5055 disk = alloc_disk(1 << shift);
5056 if (!disk) {
5057 blk_cleanup_queue(mddev->queue);
5058 mddev->queue = NULL;
5059 goto abort;
5061 disk->major = MAJOR(mddev->unit);
5062 disk->first_minor = unit << shift;
5063 if (name)
5064 strcpy(disk->disk_name, name);
5065 else if (partitioned)
5066 sprintf(disk->disk_name, "md_d%d", unit);
5067 else
5068 sprintf(disk->disk_name, "md%d", unit);
5069 disk->fops = &md_fops;
5070 disk->private_data = mddev;
5071 disk->queue = mddev->queue;
5072 blk_queue_write_cache(mddev->queue, true, true);
5073 /* Allow extended partitions. This makes the
5074 * 'mdp' device redundant, but we can't really
5075 * remove it now.
5077 disk->flags |= GENHD_FL_EXT_DEVT;
5078 mddev->gendisk = disk;
5079 /* As soon as we call add_disk(), another thread could get
5080 * through to md_open, so make sure it doesn't get too far
5082 mutex_lock(&mddev->open_mutex);
5083 add_disk(disk);
5085 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
5086 &disk_to_dev(disk)->kobj, "%s", "md");
5087 if (error) {
5088 /* This isn't possible, but as kobject_init_and_add is marked
5089 * __must_check, we must do something with the result
5091 pr_debug("md: cannot register %s/md - name in use\n",
5092 disk->disk_name);
5093 error = 0;
5095 if (mddev->kobj.sd &&
5096 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
5097 pr_debug("pointless warning\n");
5098 mutex_unlock(&mddev->open_mutex);
5099 abort:
5100 mutex_unlock(&disks_mutex);
5101 if (!error && mddev->kobj.sd) {
5102 kobject_uevent(&mddev->kobj, KOBJ_ADD);
5103 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
5105 mddev_put(mddev);
5106 return error;
5109 static struct kobject *md_probe(dev_t dev, int *part, void *data)
5111 md_alloc(dev, NULL);
5112 return NULL;
5115 static int add_named_array(const char *val, struct kernel_param *kp)
5117 /* val must be "md_*" where * is not all digits.
5118 * We allocate an array with a large free minor number, and
5119 * set the name to val. val must not already be an active name.
5121 int len = strlen(val);
5122 char buf[DISK_NAME_LEN];
5124 while (len && val[len-1] == '\n')
5125 len--;
5126 if (len >= DISK_NAME_LEN)
5127 return -E2BIG;
5128 strlcpy(buf, val, len+1);
5129 if (strncmp(buf, "md_", 3) != 0)
5130 return -EINVAL;
5131 return md_alloc(0, buf);
5134 static void md_safemode_timeout(unsigned long data)
5136 struct mddev *mddev = (struct mddev *) data;
5138 if (!atomic_read(&mddev->writes_pending)) {
5139 mddev->safemode = 1;
5140 if (mddev->external)
5141 sysfs_notify_dirent_safe(mddev->sysfs_state);
5143 md_wakeup_thread(mddev->thread);
5146 static int start_dirty_degraded;
5148 int md_run(struct mddev *mddev)
5150 int err;
5151 struct md_rdev *rdev;
5152 struct md_personality *pers;
5154 if (list_empty(&mddev->disks))
5155 /* cannot run an array with no devices.. */
5156 return -EINVAL;
5158 if (mddev->pers)
5159 return -EBUSY;
5160 /* Cannot run until previous stop completes properly */
5161 if (mddev->sysfs_active)
5162 return -EBUSY;
5165 * Analyze all RAID superblock(s)
5167 if (!mddev->raid_disks) {
5168 if (!mddev->persistent)
5169 return -EINVAL;
5170 analyze_sbs(mddev);
5173 if (mddev->level != LEVEL_NONE)
5174 request_module("md-level-%d", mddev->level);
5175 else if (mddev->clevel[0])
5176 request_module("md-%s", mddev->clevel);
5179 * Drop all container device buffers, from now on
5180 * the only valid external interface is through the md
5181 * device.
5183 rdev_for_each(rdev, mddev) {
5184 if (test_bit(Faulty, &rdev->flags))
5185 continue;
5186 sync_blockdev(rdev->bdev);
5187 invalidate_bdev(rdev->bdev);
5189 /* perform some consistency tests on the device.
5190 * We don't want the data to overlap the metadata,
5191 * Internal Bitmap issues have been handled elsewhere.
5193 if (rdev->meta_bdev) {
5194 /* Nothing to check */;
5195 } else if (rdev->data_offset < rdev->sb_start) {
5196 if (mddev->dev_sectors &&
5197 rdev->data_offset + mddev->dev_sectors
5198 > rdev->sb_start) {
5199 pr_warn("md: %s: data overlaps metadata\n",
5200 mdname(mddev));
5201 return -EINVAL;
5203 } else {
5204 if (rdev->sb_start + rdev->sb_size/512
5205 > rdev->data_offset) {
5206 pr_warn("md: %s: metadata overlaps data\n",
5207 mdname(mddev));
5208 return -EINVAL;
5211 sysfs_notify_dirent_safe(rdev->sysfs_state);
5214 if (mddev->bio_set == NULL) {
5215 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
5216 if (!mddev->bio_set)
5217 return -ENOMEM;
5220 spin_lock(&pers_lock);
5221 pers = find_pers(mddev->level, mddev->clevel);
5222 if (!pers || !try_module_get(pers->owner)) {
5223 spin_unlock(&pers_lock);
5224 if (mddev->level != LEVEL_NONE)
5225 pr_warn("md: personality for level %d is not loaded!\n",
5226 mddev->level);
5227 else
5228 pr_warn("md: personality for level %s is not loaded!\n",
5229 mddev->clevel);
5230 return -EINVAL;
5232 spin_unlock(&pers_lock);
5233 if (mddev->level != pers->level) {
5234 mddev->level = pers->level;
5235 mddev->new_level = pers->level;
5237 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
5239 if (mddev->reshape_position != MaxSector &&
5240 pers->start_reshape == NULL) {
5241 /* This personality cannot handle reshaping... */
5242 module_put(pers->owner);
5243 return -EINVAL;
5246 if (pers->sync_request) {
5247 /* Warn if this is a potentially silly
5248 * configuration.
5250 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5251 struct md_rdev *rdev2;
5252 int warned = 0;
5254 rdev_for_each(rdev, mddev)
5255 rdev_for_each(rdev2, mddev) {
5256 if (rdev < rdev2 &&
5257 rdev->bdev->bd_contains ==
5258 rdev2->bdev->bd_contains) {
5259 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5260 mdname(mddev),
5261 bdevname(rdev->bdev,b),
5262 bdevname(rdev2->bdev,b2));
5263 warned = 1;
5267 if (warned)
5268 pr_warn("True protection against single-disk failure might be compromised.\n");
5271 mddev->recovery = 0;
5272 /* may be over-ridden by personality */
5273 mddev->resync_max_sectors = mddev->dev_sectors;
5275 mddev->ok_start_degraded = start_dirty_degraded;
5277 if (start_readonly && mddev->ro == 0)
5278 mddev->ro = 2; /* read-only, but switch on first write */
5281 * NOTE: some pers->run(), for example r5l_recovery_log(), wakes
5282 * up mddev->thread. It is important to initialize critical
5283 * resources for mddev->thread BEFORE calling pers->run().
5285 err = pers->run(mddev);
5286 if (err)
5287 pr_warn("md: pers->run() failed ...\n");
5288 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
5289 WARN_ONCE(!mddev->external_size,
5290 "%s: default size too small, but 'external_size' not in effect?\n",
5291 __func__);
5292 pr_warn("md: invalid array_size %llu > default size %llu\n",
5293 (unsigned long long)mddev->array_sectors / 2,
5294 (unsigned long long)pers->size(mddev, 0, 0) / 2);
5295 err = -EINVAL;
5297 if (err == 0 && pers->sync_request &&
5298 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
5299 struct bitmap *bitmap;
5301 bitmap = bitmap_create(mddev, -1);
5302 if (IS_ERR(bitmap)) {
5303 err = PTR_ERR(bitmap);
5304 pr_warn("%s: failed to create bitmap (%d)\n",
5305 mdname(mddev), err);
5306 } else
5307 mddev->bitmap = bitmap;
5310 if (err) {
5311 mddev_detach(mddev);
5312 if (mddev->private)
5313 pers->free(mddev, mddev->private);
5314 mddev->private = NULL;
5315 module_put(pers->owner);
5316 bitmap_destroy(mddev);
5317 return err;
5319 if (mddev->queue) {
5320 bool nonrot = true;
5322 rdev_for_each(rdev, mddev) {
5323 if (rdev->raid_disk >= 0 &&
5324 !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
5325 nonrot = false;
5326 break;
5329 if (mddev->degraded)
5330 nonrot = false;
5331 if (nonrot)
5332 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mddev->queue);
5333 else
5334 queue_flag_clear_unlocked(QUEUE_FLAG_NONROT, mddev->queue);
5335 mddev->queue->backing_dev_info->congested_data = mddev;
5336 mddev->queue->backing_dev_info->congested_fn = md_congested;
5338 if (pers->sync_request) {
5339 if (mddev->kobj.sd &&
5340 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
5341 pr_warn("md: cannot register extra attributes for %s\n",
5342 mdname(mddev));
5343 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
5344 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
5345 mddev->ro = 0;
5347 atomic_set(&mddev->writes_pending,0);
5348 atomic_set(&mddev->max_corr_read_errors,
5349 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
5350 mddev->safemode = 0;
5351 if (mddev_is_clustered(mddev))
5352 mddev->safemode_delay = 0;
5353 else
5354 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
5355 mddev->in_sync = 1;
5356 smp_wmb();
5357 spin_lock(&mddev->lock);
5358 mddev->pers = pers;
5359 spin_unlock(&mddev->lock);
5360 rdev_for_each(rdev, mddev)
5361 if (rdev->raid_disk >= 0)
5362 if (sysfs_link_rdev(mddev, rdev))
5363 /* failure here is OK */;
5365 if (mddev->degraded && !mddev->ro)
5366 /* This ensures that recovering status is reported immediately
5367 * via sysfs - until a lack of spares is confirmed.
5369 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
5370 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5372 if (mddev->sb_flags)
5373 md_update_sb(mddev, 0);
5375 md_new_event(mddev);
5376 sysfs_notify_dirent_safe(mddev->sysfs_state);
5377 sysfs_notify_dirent_safe(mddev->sysfs_action);
5378 sysfs_notify(&mddev->kobj, NULL, "degraded");
5379 return 0;
5381 EXPORT_SYMBOL_GPL(md_run);
5383 static int do_md_run(struct mddev *mddev)
5385 int err;
5387 err = md_run(mddev);
5388 if (err)
5389 goto out;
5390 err = bitmap_load(mddev);
5391 if (err) {
5392 bitmap_destroy(mddev);
5393 goto out;
5396 if (mddev_is_clustered(mddev))
5397 md_allow_write(mddev);
5399 md_wakeup_thread(mddev->thread);
5400 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5402 set_capacity(mddev->gendisk, mddev->array_sectors);
5403 revalidate_disk(mddev->gendisk);
5404 mddev->changed = 1;
5405 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5406 out:
5407 return err;
5410 static int restart_array(struct mddev *mddev)
5412 struct gendisk *disk = mddev->gendisk;
5414 /* Complain if it has no devices */
5415 if (list_empty(&mddev->disks))
5416 return -ENXIO;
5417 if (!mddev->pers)
5418 return -EINVAL;
5419 if (!mddev->ro)
5420 return -EBUSY;
5421 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5422 struct md_rdev *rdev;
5423 bool has_journal = false;
5425 rcu_read_lock();
5426 rdev_for_each_rcu(rdev, mddev) {
5427 if (test_bit(Journal, &rdev->flags) &&
5428 !test_bit(Faulty, &rdev->flags)) {
5429 has_journal = true;
5430 break;
5433 rcu_read_unlock();
5435 /* Don't restart rw with journal missing/faulty */
5436 if (!has_journal)
5437 return -EINVAL;
5440 mddev->safemode = 0;
5441 mddev->ro = 0;
5442 set_disk_ro(disk, 0);
5443 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev));
5444 /* Kick recovery or resync if necessary */
5445 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5446 md_wakeup_thread(mddev->thread);
5447 md_wakeup_thread(mddev->sync_thread);
5448 sysfs_notify_dirent_safe(mddev->sysfs_state);
5449 return 0;
5452 static void md_clean(struct mddev *mddev)
5454 mddev->array_sectors = 0;
5455 mddev->external_size = 0;
5456 mddev->dev_sectors = 0;
5457 mddev->raid_disks = 0;
5458 mddev->recovery_cp = 0;
5459 mddev->resync_min = 0;
5460 mddev->resync_max = MaxSector;
5461 mddev->reshape_position = MaxSector;
5462 mddev->external = 0;
5463 mddev->persistent = 0;
5464 mddev->level = LEVEL_NONE;
5465 mddev->clevel[0] = 0;
5466 mddev->flags = 0;
5467 mddev->sb_flags = 0;
5468 mddev->ro = 0;
5469 mddev->metadata_type[0] = 0;
5470 mddev->chunk_sectors = 0;
5471 mddev->ctime = mddev->utime = 0;
5472 mddev->layout = 0;
5473 mddev->max_disks = 0;
5474 mddev->events = 0;
5475 mddev->can_decrease_events = 0;
5476 mddev->delta_disks = 0;
5477 mddev->reshape_backwards = 0;
5478 mddev->new_level = LEVEL_NONE;
5479 mddev->new_layout = 0;
5480 mddev->new_chunk_sectors = 0;
5481 mddev->curr_resync = 0;
5482 atomic64_set(&mddev->resync_mismatches, 0);
5483 mddev->suspend_lo = mddev->suspend_hi = 0;
5484 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5485 mddev->recovery = 0;
5486 mddev->in_sync = 0;
5487 mddev->changed = 0;
5488 mddev->degraded = 0;
5489 mddev->safemode = 0;
5490 mddev->private = NULL;
5491 mddev->cluster_info = NULL;
5492 mddev->bitmap_info.offset = 0;
5493 mddev->bitmap_info.default_offset = 0;
5494 mddev->bitmap_info.default_space = 0;
5495 mddev->bitmap_info.chunksize = 0;
5496 mddev->bitmap_info.daemon_sleep = 0;
5497 mddev->bitmap_info.max_write_behind = 0;
5498 mddev->bitmap_info.nodes = 0;
5501 static void __md_stop_writes(struct mddev *mddev)
5503 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5504 flush_workqueue(md_misc_wq);
5505 if (mddev->sync_thread) {
5506 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5507 md_reap_sync_thread(mddev);
5510 del_timer_sync(&mddev->safemode_timer);
5512 if (mddev->pers && mddev->pers->quiesce) {
5513 mddev->pers->quiesce(mddev, 1);
5514 mddev->pers->quiesce(mddev, 0);
5516 bitmap_flush(mddev);
5518 if (mddev->ro == 0 &&
5519 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
5520 mddev->sb_flags)) {
5521 /* mark array as shutdown cleanly */
5522 if (!mddev_is_clustered(mddev))
5523 mddev->in_sync = 1;
5524 md_update_sb(mddev, 1);
5528 void md_stop_writes(struct mddev *mddev)
5530 mddev_lock_nointr(mddev);
5531 __md_stop_writes(mddev);
5532 mddev_unlock(mddev);
5534 EXPORT_SYMBOL_GPL(md_stop_writes);
5536 static void mddev_detach(struct mddev *mddev)
5538 struct bitmap *bitmap = mddev->bitmap;
5539 /* wait for behind writes to complete */
5540 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
5541 pr_debug("md:%s: behind writes in progress - waiting to stop.\n",
5542 mdname(mddev));
5543 /* need to kick something here to make sure I/O goes? */
5544 wait_event(bitmap->behind_wait,
5545 atomic_read(&bitmap->behind_writes) == 0);
5547 if (mddev->pers && mddev->pers->quiesce) {
5548 mddev->pers->quiesce(mddev, 1);
5549 mddev->pers->quiesce(mddev, 0);
5551 md_unregister_thread(&mddev->thread);
5552 if (mddev->queue)
5553 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5556 static void __md_stop(struct mddev *mddev)
5558 struct md_personality *pers = mddev->pers;
5559 mddev_detach(mddev);
5560 /* Ensure ->event_work is done */
5561 flush_workqueue(md_misc_wq);
5562 spin_lock(&mddev->lock);
5563 mddev->pers = NULL;
5564 spin_unlock(&mddev->lock);
5565 pers->free(mddev, mddev->private);
5566 mddev->private = NULL;
5567 if (pers->sync_request && mddev->to_remove == NULL)
5568 mddev->to_remove = &md_redundancy_group;
5569 module_put(pers->owner);
5570 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5573 void md_stop(struct mddev *mddev)
5575 /* stop the array and free an attached data structures.
5576 * This is called from dm-raid
5578 __md_stop(mddev);
5579 bitmap_destroy(mddev);
5580 if (mddev->bio_set)
5581 bioset_free(mddev->bio_set);
5584 EXPORT_SYMBOL_GPL(md_stop);
5586 static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
5588 int err = 0;
5589 int did_freeze = 0;
5591 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5592 did_freeze = 1;
5593 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5594 md_wakeup_thread(mddev->thread);
5596 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
5597 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5598 if (mddev->sync_thread)
5599 /* Thread might be blocked waiting for metadata update
5600 * which will now never happen */
5601 wake_up_process(mddev->sync_thread->tsk);
5603 if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
5604 return -EBUSY;
5605 mddev_unlock(mddev);
5606 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5607 &mddev->recovery));
5608 wait_event(mddev->sb_wait,
5609 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
5610 mddev_lock_nointr(mddev);
5612 mutex_lock(&mddev->open_mutex);
5613 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
5614 mddev->sync_thread ||
5615 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
5616 pr_warn("md: %s still in use.\n",mdname(mddev));
5617 if (did_freeze) {
5618 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5619 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5620 md_wakeup_thread(mddev->thread);
5622 err = -EBUSY;
5623 goto out;
5625 if (mddev->pers) {
5626 __md_stop_writes(mddev);
5628 err = -ENXIO;
5629 if (mddev->ro==1)
5630 goto out;
5631 mddev->ro = 1;
5632 set_disk_ro(mddev->gendisk, 1);
5633 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5634 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5635 md_wakeup_thread(mddev->thread);
5636 sysfs_notify_dirent_safe(mddev->sysfs_state);
5637 err = 0;
5639 out:
5640 mutex_unlock(&mddev->open_mutex);
5641 return err;
5644 /* mode:
5645 * 0 - completely stop and dis-assemble array
5646 * 2 - stop but do not disassemble array
5648 static int do_md_stop(struct mddev *mddev, int mode,
5649 struct block_device *bdev)
5651 struct gendisk *disk = mddev->gendisk;
5652 struct md_rdev *rdev;
5653 int did_freeze = 0;
5655 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5656 did_freeze = 1;
5657 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5658 md_wakeup_thread(mddev->thread);
5660 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
5661 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5662 if (mddev->sync_thread)
5663 /* Thread might be blocked waiting for metadata update
5664 * which will now never happen */
5665 wake_up_process(mddev->sync_thread->tsk);
5667 mddev_unlock(mddev);
5668 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5669 !test_bit(MD_RECOVERY_RUNNING,
5670 &mddev->recovery)));
5671 mddev_lock_nointr(mddev);
5673 mutex_lock(&mddev->open_mutex);
5674 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
5675 mddev->sysfs_active ||
5676 mddev->sync_thread ||
5677 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
5678 pr_warn("md: %s still in use.\n",mdname(mddev));
5679 mutex_unlock(&mddev->open_mutex);
5680 if (did_freeze) {
5681 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5682 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5683 md_wakeup_thread(mddev->thread);
5685 return -EBUSY;
5687 if (mddev->pers) {
5688 if (mddev->ro)
5689 set_disk_ro(disk, 0);
5691 __md_stop_writes(mddev);
5692 __md_stop(mddev);
5693 mddev->queue->backing_dev_info->congested_fn = NULL;
5695 /* tell userspace to handle 'inactive' */
5696 sysfs_notify_dirent_safe(mddev->sysfs_state);
5698 rdev_for_each(rdev, mddev)
5699 if (rdev->raid_disk >= 0)
5700 sysfs_unlink_rdev(mddev, rdev);
5702 set_capacity(disk, 0);
5703 mutex_unlock(&mddev->open_mutex);
5704 mddev->changed = 1;
5705 revalidate_disk(disk);
5707 if (mddev->ro)
5708 mddev->ro = 0;
5709 } else
5710 mutex_unlock(&mddev->open_mutex);
5712 * Free resources if final stop
5714 if (mode == 0) {
5715 pr_info("md: %s stopped.\n", mdname(mddev));
5717 bitmap_destroy(mddev);
5718 if (mddev->bitmap_info.file) {
5719 struct file *f = mddev->bitmap_info.file;
5720 spin_lock(&mddev->lock);
5721 mddev->bitmap_info.file = NULL;
5722 spin_unlock(&mddev->lock);
5723 fput(f);
5725 mddev->bitmap_info.offset = 0;
5727 export_array(mddev);
5729 md_clean(mddev);
5730 if (mddev->hold_active == UNTIL_STOP)
5731 mddev->hold_active = 0;
5733 md_new_event(mddev);
5734 sysfs_notify_dirent_safe(mddev->sysfs_state);
5735 return 0;
5738 #ifndef MODULE
5739 static void autorun_array(struct mddev *mddev)
5741 struct md_rdev *rdev;
5742 int err;
5744 if (list_empty(&mddev->disks))
5745 return;
5747 pr_info("md: running: ");
5749 rdev_for_each(rdev, mddev) {
5750 char b[BDEVNAME_SIZE];
5751 pr_cont("<%s>", bdevname(rdev->bdev,b));
5753 pr_cont("\n");
5755 err = do_md_run(mddev);
5756 if (err) {
5757 pr_warn("md: do_md_run() returned %d\n", err);
5758 do_md_stop(mddev, 0, NULL);
5763 * lets try to run arrays based on all disks that have arrived
5764 * until now. (those are in pending_raid_disks)
5766 * the method: pick the first pending disk, collect all disks with
5767 * the same UUID, remove all from the pending list and put them into
5768 * the 'same_array' list. Then order this list based on superblock
5769 * update time (freshest comes first), kick out 'old' disks and
5770 * compare superblocks. If everything's fine then run it.
5772 * If "unit" is allocated, then bump its reference count
5774 static void autorun_devices(int part)
5776 struct md_rdev *rdev0, *rdev, *tmp;
5777 struct mddev *mddev;
5778 char b[BDEVNAME_SIZE];
5780 pr_info("md: autorun ...\n");
5781 while (!list_empty(&pending_raid_disks)) {
5782 int unit;
5783 dev_t dev;
5784 LIST_HEAD(candidates);
5785 rdev0 = list_entry(pending_raid_disks.next,
5786 struct md_rdev, same_set);
5788 pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b));
5789 INIT_LIST_HEAD(&candidates);
5790 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
5791 if (super_90_load(rdev, rdev0, 0) >= 0) {
5792 pr_debug("md: adding %s ...\n",
5793 bdevname(rdev->bdev,b));
5794 list_move(&rdev->same_set, &candidates);
5797 * now we have a set of devices, with all of them having
5798 * mostly sane superblocks. It's time to allocate the
5799 * mddev.
5801 if (part) {
5802 dev = MKDEV(mdp_major,
5803 rdev0->preferred_minor << MdpMinorShift);
5804 unit = MINOR(dev) >> MdpMinorShift;
5805 } else {
5806 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5807 unit = MINOR(dev);
5809 if (rdev0->preferred_minor != unit) {
5810 pr_warn("md: unit number in %s is bad: %d\n",
5811 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5812 break;
5815 md_probe(dev, NULL, NULL);
5816 mddev = mddev_find(dev);
5817 if (!mddev || !mddev->gendisk) {
5818 if (mddev)
5819 mddev_put(mddev);
5820 break;
5822 if (mddev_lock(mddev))
5823 pr_warn("md: %s locked, cannot run\n", mdname(mddev));
5824 else if (mddev->raid_disks || mddev->major_version
5825 || !list_empty(&mddev->disks)) {
5826 pr_warn("md: %s already running, cannot run %s\n",
5827 mdname(mddev), bdevname(rdev0->bdev,b));
5828 mddev_unlock(mddev);
5829 } else {
5830 pr_debug("md: created %s\n", mdname(mddev));
5831 mddev->persistent = 1;
5832 rdev_for_each_list(rdev, tmp, &candidates) {
5833 list_del_init(&rdev->same_set);
5834 if (bind_rdev_to_array(rdev, mddev))
5835 export_rdev(rdev);
5837 autorun_array(mddev);
5838 mddev_unlock(mddev);
5840 /* on success, candidates will be empty, on error
5841 * it won't...
5843 rdev_for_each_list(rdev, tmp, &candidates) {
5844 list_del_init(&rdev->same_set);
5845 export_rdev(rdev);
5847 mddev_put(mddev);
5849 pr_info("md: ... autorun DONE.\n");
5851 #endif /* !MODULE */
5853 static int get_version(void __user *arg)
5855 mdu_version_t ver;
5857 ver.major = MD_MAJOR_VERSION;
5858 ver.minor = MD_MINOR_VERSION;
5859 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5861 if (copy_to_user(arg, &ver, sizeof(ver)))
5862 return -EFAULT;
5864 return 0;
5867 static int get_array_info(struct mddev *mddev, void __user *arg)
5869 mdu_array_info_t info;
5870 int nr,working,insync,failed,spare;
5871 struct md_rdev *rdev;
5873 nr = working = insync = failed = spare = 0;
5874 rcu_read_lock();
5875 rdev_for_each_rcu(rdev, mddev) {
5876 nr++;
5877 if (test_bit(Faulty, &rdev->flags))
5878 failed++;
5879 else {
5880 working++;
5881 if (test_bit(In_sync, &rdev->flags))
5882 insync++;
5883 else if (test_bit(Journal, &rdev->flags))
5884 /* TODO: add journal count to md_u.h */
5886 else
5887 spare++;
5890 rcu_read_unlock();
5892 info.major_version = mddev->major_version;
5893 info.minor_version = mddev->minor_version;
5894 info.patch_version = MD_PATCHLEVEL_VERSION;
5895 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
5896 info.level = mddev->level;
5897 info.size = mddev->dev_sectors / 2;
5898 if (info.size != mddev->dev_sectors / 2) /* overflow */
5899 info.size = -1;
5900 info.nr_disks = nr;
5901 info.raid_disks = mddev->raid_disks;
5902 info.md_minor = mddev->md_minor;
5903 info.not_persistent= !mddev->persistent;
5905 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
5906 info.state = 0;
5907 if (mddev->in_sync)
5908 info.state = (1<<MD_SB_CLEAN);
5909 if (mddev->bitmap && mddev->bitmap_info.offset)
5910 info.state |= (1<<MD_SB_BITMAP_PRESENT);
5911 if (mddev_is_clustered(mddev))
5912 info.state |= (1<<MD_SB_CLUSTERED);
5913 info.active_disks = insync;
5914 info.working_disks = working;
5915 info.failed_disks = failed;
5916 info.spare_disks = spare;
5918 info.layout = mddev->layout;
5919 info.chunk_size = mddev->chunk_sectors << 9;
5921 if (copy_to_user(arg, &info, sizeof(info)))
5922 return -EFAULT;
5924 return 0;
5927 static int get_bitmap_file(struct mddev *mddev, void __user * arg)
5929 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5930 char *ptr;
5931 int err;
5933 file = kzalloc(sizeof(*file), GFP_NOIO);
5934 if (!file)
5935 return -ENOMEM;
5937 err = 0;
5938 spin_lock(&mddev->lock);
5939 /* bitmap enabled */
5940 if (mddev->bitmap_info.file) {
5941 ptr = file_path(mddev->bitmap_info.file, file->pathname,
5942 sizeof(file->pathname));
5943 if (IS_ERR(ptr))
5944 err = PTR_ERR(ptr);
5945 else
5946 memmove(file->pathname, ptr,
5947 sizeof(file->pathname)-(ptr-file->pathname));
5949 spin_unlock(&mddev->lock);
5951 if (err == 0 &&
5952 copy_to_user(arg, file, sizeof(*file)))
5953 err = -EFAULT;
5955 kfree(file);
5956 return err;
5959 static int get_disk_info(struct mddev *mddev, void __user * arg)
5961 mdu_disk_info_t info;
5962 struct md_rdev *rdev;
5964 if (copy_from_user(&info, arg, sizeof(info)))
5965 return -EFAULT;
5967 rcu_read_lock();
5968 rdev = md_find_rdev_nr_rcu(mddev, info.number);
5969 if (rdev) {
5970 info.major = MAJOR(rdev->bdev->bd_dev);
5971 info.minor = MINOR(rdev->bdev->bd_dev);
5972 info.raid_disk = rdev->raid_disk;
5973 info.state = 0;
5974 if (test_bit(Faulty, &rdev->flags))
5975 info.state |= (1<<MD_DISK_FAULTY);
5976 else if (test_bit(In_sync, &rdev->flags)) {
5977 info.state |= (1<<MD_DISK_ACTIVE);
5978 info.state |= (1<<MD_DISK_SYNC);
5980 if (test_bit(Journal, &rdev->flags))
5981 info.state |= (1<<MD_DISK_JOURNAL);
5982 if (test_bit(WriteMostly, &rdev->flags))
5983 info.state |= (1<<MD_DISK_WRITEMOSTLY);
5984 if (test_bit(FailFast, &rdev->flags))
5985 info.state |= (1<<MD_DISK_FAILFAST);
5986 } else {
5987 info.major = info.minor = 0;
5988 info.raid_disk = -1;
5989 info.state = (1<<MD_DISK_REMOVED);
5991 rcu_read_unlock();
5993 if (copy_to_user(arg, &info, sizeof(info)))
5994 return -EFAULT;
5996 return 0;
5999 static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
6001 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
6002 struct md_rdev *rdev;
6003 dev_t dev = MKDEV(info->major,info->minor);
6005 if (mddev_is_clustered(mddev) &&
6006 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
6007 pr_warn("%s: Cannot add to clustered mddev.\n",
6008 mdname(mddev));
6009 return -EINVAL;
6012 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
6013 return -EOVERFLOW;
6015 if (!mddev->raid_disks) {
6016 int err;
6017 /* expecting a device which has a superblock */
6018 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
6019 if (IS_ERR(rdev)) {
6020 pr_warn("md: md_import_device returned %ld\n",
6021 PTR_ERR(rdev));
6022 return PTR_ERR(rdev);
6024 if (!list_empty(&mddev->disks)) {
6025 struct md_rdev *rdev0
6026 = list_entry(mddev->disks.next,
6027 struct md_rdev, same_set);
6028 err = super_types[mddev->major_version]
6029 .load_super(rdev, rdev0, mddev->minor_version);
6030 if (err < 0) {
6031 pr_warn("md: %s has different UUID to %s\n",
6032 bdevname(rdev->bdev,b),
6033 bdevname(rdev0->bdev,b2));
6034 export_rdev(rdev);
6035 return -EINVAL;
6038 err = bind_rdev_to_array(rdev, mddev);
6039 if (err)
6040 export_rdev(rdev);
6041 return err;
6045 * add_new_disk can be used once the array is assembled
6046 * to add "hot spares". They must already have a superblock
6047 * written
6049 if (mddev->pers) {
6050 int err;
6051 if (!mddev->pers->hot_add_disk) {
6052 pr_warn("%s: personality does not support diskops!\n",
6053 mdname(mddev));
6054 return -EINVAL;
6056 if (mddev->persistent)
6057 rdev = md_import_device(dev, mddev->major_version,
6058 mddev->minor_version);
6059 else
6060 rdev = md_import_device(dev, -1, -1);
6061 if (IS_ERR(rdev)) {
6062 pr_warn("md: md_import_device returned %ld\n",
6063 PTR_ERR(rdev));
6064 return PTR_ERR(rdev);
6066 /* set saved_raid_disk if appropriate */
6067 if (!mddev->persistent) {
6068 if (info->state & (1<<MD_DISK_SYNC) &&
6069 info->raid_disk < mddev->raid_disks) {
6070 rdev->raid_disk = info->raid_disk;
6071 set_bit(In_sync, &rdev->flags);
6072 clear_bit(Bitmap_sync, &rdev->flags);
6073 } else
6074 rdev->raid_disk = -1;
6075 rdev->saved_raid_disk = rdev->raid_disk;
6076 } else
6077 super_types[mddev->major_version].
6078 validate_super(mddev, rdev);
6079 if ((info->state & (1<<MD_DISK_SYNC)) &&
6080 rdev->raid_disk != info->raid_disk) {
6081 /* This was a hot-add request, but events doesn't
6082 * match, so reject it.
6084 export_rdev(rdev);
6085 return -EINVAL;
6088 clear_bit(In_sync, &rdev->flags); /* just to be sure */
6089 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6090 set_bit(WriteMostly, &rdev->flags);
6091 else
6092 clear_bit(WriteMostly, &rdev->flags);
6093 if (info->state & (1<<MD_DISK_FAILFAST))
6094 set_bit(FailFast, &rdev->flags);
6095 else
6096 clear_bit(FailFast, &rdev->flags);
6098 if (info->state & (1<<MD_DISK_JOURNAL)) {
6099 struct md_rdev *rdev2;
6100 bool has_journal = false;
6102 /* make sure no existing journal disk */
6103 rdev_for_each(rdev2, mddev) {
6104 if (test_bit(Journal, &rdev2->flags)) {
6105 has_journal = true;
6106 break;
6109 if (has_journal) {
6110 export_rdev(rdev);
6111 return -EBUSY;
6113 set_bit(Journal, &rdev->flags);
6116 * check whether the device shows up in other nodes
6118 if (mddev_is_clustered(mddev)) {
6119 if (info->state & (1 << MD_DISK_CANDIDATE))
6120 set_bit(Candidate, &rdev->flags);
6121 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
6122 /* --add initiated by this node */
6123 err = md_cluster_ops->add_new_disk(mddev, rdev);
6124 if (err) {
6125 export_rdev(rdev);
6126 return err;
6131 rdev->raid_disk = -1;
6132 err = bind_rdev_to_array(rdev, mddev);
6134 if (err)
6135 export_rdev(rdev);
6137 if (mddev_is_clustered(mddev)) {
6138 if (info->state & (1 << MD_DISK_CANDIDATE)) {
6139 if (!err) {
6140 err = md_cluster_ops->new_disk_ack(mddev,
6141 err == 0);
6142 if (err)
6143 md_kick_rdev_from_array(rdev);
6145 } else {
6146 if (err)
6147 md_cluster_ops->add_new_disk_cancel(mddev);
6148 else
6149 err = add_bound_rdev(rdev);
6152 } else if (!err)
6153 err = add_bound_rdev(rdev);
6155 return err;
6158 /* otherwise, add_new_disk is only allowed
6159 * for major_version==0 superblocks
6161 if (mddev->major_version != 0) {
6162 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev));
6163 return -EINVAL;
6166 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6167 int err;
6168 rdev = md_import_device(dev, -1, 0);
6169 if (IS_ERR(rdev)) {
6170 pr_warn("md: error, md_import_device() returned %ld\n",
6171 PTR_ERR(rdev));
6172 return PTR_ERR(rdev);
6174 rdev->desc_nr = info->number;
6175 if (info->raid_disk < mddev->raid_disks)
6176 rdev->raid_disk = info->raid_disk;
6177 else
6178 rdev->raid_disk = -1;
6180 if (rdev->raid_disk < mddev->raid_disks)
6181 if (info->state & (1<<MD_DISK_SYNC))
6182 set_bit(In_sync, &rdev->flags);
6184 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6185 set_bit(WriteMostly, &rdev->flags);
6186 if (info->state & (1<<MD_DISK_FAILFAST))
6187 set_bit(FailFast, &rdev->flags);
6189 if (!mddev->persistent) {
6190 pr_debug("md: nonpersistent superblock ...\n");
6191 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6192 } else
6193 rdev->sb_start = calc_dev_sboffset(rdev);
6194 rdev->sectors = rdev->sb_start;
6196 err = bind_rdev_to_array(rdev, mddev);
6197 if (err) {
6198 export_rdev(rdev);
6199 return err;
6203 return 0;
6206 static int hot_remove_disk(struct mddev *mddev, dev_t dev)
6208 char b[BDEVNAME_SIZE];
6209 struct md_rdev *rdev;
6211 rdev = find_rdev(mddev, dev);
6212 if (!rdev)
6213 return -ENXIO;
6215 if (rdev->raid_disk < 0)
6216 goto kick_rdev;
6218 clear_bit(Blocked, &rdev->flags);
6219 remove_and_add_spares(mddev, rdev);
6221 if (rdev->raid_disk >= 0)
6222 goto busy;
6224 kick_rdev:
6225 if (mddev_is_clustered(mddev))
6226 md_cluster_ops->remove_disk(mddev, rdev);
6228 md_kick_rdev_from_array(rdev);
6229 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
6230 if (mddev->thread)
6231 md_wakeup_thread(mddev->thread);
6232 else
6233 md_update_sb(mddev, 1);
6234 md_new_event(mddev);
6236 return 0;
6237 busy:
6238 pr_debug("md: cannot remove active disk %s from %s ...\n",
6239 bdevname(rdev->bdev,b), mdname(mddev));
6240 return -EBUSY;
6243 static int hot_add_disk(struct mddev *mddev, dev_t dev)
6245 char b[BDEVNAME_SIZE];
6246 int err;
6247 struct md_rdev *rdev;
6249 if (!mddev->pers)
6250 return -ENODEV;
6252 if (mddev->major_version != 0) {
6253 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
6254 mdname(mddev));
6255 return -EINVAL;
6257 if (!mddev->pers->hot_add_disk) {
6258 pr_warn("%s: personality does not support diskops!\n",
6259 mdname(mddev));
6260 return -EINVAL;
6263 rdev = md_import_device(dev, -1, 0);
6264 if (IS_ERR(rdev)) {
6265 pr_warn("md: error, md_import_device() returned %ld\n",
6266 PTR_ERR(rdev));
6267 return -EINVAL;
6270 if (mddev->persistent)
6271 rdev->sb_start = calc_dev_sboffset(rdev);
6272 else
6273 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6275 rdev->sectors = rdev->sb_start;
6277 if (test_bit(Faulty, &rdev->flags)) {
6278 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
6279 bdevname(rdev->bdev,b), mdname(mddev));
6280 err = -EINVAL;
6281 goto abort_export;
6284 clear_bit(In_sync, &rdev->flags);
6285 rdev->desc_nr = -1;
6286 rdev->saved_raid_disk = -1;
6287 err = bind_rdev_to_array(rdev, mddev);
6288 if (err)
6289 goto abort_export;
6292 * The rest should better be atomic, we can have disk failures
6293 * noticed in interrupt contexts ...
6296 rdev->raid_disk = -1;
6298 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
6299 if (!mddev->thread)
6300 md_update_sb(mddev, 1);
6302 * Kick recovery, maybe this spare has to be added to the
6303 * array immediately.
6305 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6306 md_wakeup_thread(mddev->thread);
6307 md_new_event(mddev);
6308 return 0;
6310 abort_export:
6311 export_rdev(rdev);
6312 return err;
6315 static int set_bitmap_file(struct mddev *mddev, int fd)
6317 int err = 0;
6319 if (mddev->pers) {
6320 if (!mddev->pers->quiesce || !mddev->thread)
6321 return -EBUSY;
6322 if (mddev->recovery || mddev->sync_thread)
6323 return -EBUSY;
6324 /* we should be able to change the bitmap.. */
6327 if (fd >= 0) {
6328 struct inode *inode;
6329 struct file *f;
6331 if (mddev->bitmap || mddev->bitmap_info.file)
6332 return -EEXIST; /* cannot add when bitmap is present */
6333 f = fget(fd);
6335 if (f == NULL) {
6336 pr_warn("%s: error: failed to get bitmap file\n",
6337 mdname(mddev));
6338 return -EBADF;
6341 inode = f->f_mapping->host;
6342 if (!S_ISREG(inode->i_mode)) {
6343 pr_warn("%s: error: bitmap file must be a regular file\n",
6344 mdname(mddev));
6345 err = -EBADF;
6346 } else if (!(f->f_mode & FMODE_WRITE)) {
6347 pr_warn("%s: error: bitmap file must open for write\n",
6348 mdname(mddev));
6349 err = -EBADF;
6350 } else if (atomic_read(&inode->i_writecount) != 1) {
6351 pr_warn("%s: error: bitmap file is already in use\n",
6352 mdname(mddev));
6353 err = -EBUSY;
6355 if (err) {
6356 fput(f);
6357 return err;
6359 mddev->bitmap_info.file = f;
6360 mddev->bitmap_info.offset = 0; /* file overrides offset */
6361 } else if (mddev->bitmap == NULL)
6362 return -ENOENT; /* cannot remove what isn't there */
6363 err = 0;
6364 if (mddev->pers) {
6365 mddev->pers->quiesce(mddev, 1);
6366 if (fd >= 0) {
6367 struct bitmap *bitmap;
6369 bitmap = bitmap_create(mddev, -1);
6370 if (!IS_ERR(bitmap)) {
6371 mddev->bitmap = bitmap;
6372 err = bitmap_load(mddev);
6373 } else
6374 err = PTR_ERR(bitmap);
6376 if (fd < 0 || err) {
6377 bitmap_destroy(mddev);
6378 fd = -1; /* make sure to put the file */
6380 mddev->pers->quiesce(mddev, 0);
6382 if (fd < 0) {
6383 struct file *f = mddev->bitmap_info.file;
6384 if (f) {
6385 spin_lock(&mddev->lock);
6386 mddev->bitmap_info.file = NULL;
6387 spin_unlock(&mddev->lock);
6388 fput(f);
6392 return err;
6396 * set_array_info is used two different ways
6397 * The original usage is when creating a new array.
6398 * In this usage, raid_disks is > 0 and it together with
6399 * level, size, not_persistent,layout,chunksize determine the
6400 * shape of the array.
6401 * This will always create an array with a type-0.90.0 superblock.
6402 * The newer usage is when assembling an array.
6403 * In this case raid_disks will be 0, and the major_version field is
6404 * use to determine which style super-blocks are to be found on the devices.
6405 * The minor and patch _version numbers are also kept incase the
6406 * super_block handler wishes to interpret them.
6408 static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
6411 if (info->raid_disks == 0) {
6412 /* just setting version number for superblock loading */
6413 if (info->major_version < 0 ||
6414 info->major_version >= ARRAY_SIZE(super_types) ||
6415 super_types[info->major_version].name == NULL) {
6416 /* maybe try to auto-load a module? */
6417 pr_warn("md: superblock version %d not known\n",
6418 info->major_version);
6419 return -EINVAL;
6421 mddev->major_version = info->major_version;
6422 mddev->minor_version = info->minor_version;
6423 mddev->patch_version = info->patch_version;
6424 mddev->persistent = !info->not_persistent;
6425 /* ensure mddev_put doesn't delete this now that there
6426 * is some minimal configuration.
6428 mddev->ctime = ktime_get_real_seconds();
6429 return 0;
6431 mddev->major_version = MD_MAJOR_VERSION;
6432 mddev->minor_version = MD_MINOR_VERSION;
6433 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6434 mddev->ctime = ktime_get_real_seconds();
6436 mddev->level = info->level;
6437 mddev->clevel[0] = 0;
6438 mddev->dev_sectors = 2 * (sector_t)info->size;
6439 mddev->raid_disks = info->raid_disks;
6440 /* don't set md_minor, it is determined by which /dev/md* was
6441 * openned
6443 if (info->state & (1<<MD_SB_CLEAN))
6444 mddev->recovery_cp = MaxSector;
6445 else
6446 mddev->recovery_cp = 0;
6447 mddev->persistent = ! info->not_persistent;
6448 mddev->external = 0;
6450 mddev->layout = info->layout;
6451 mddev->chunk_sectors = info->chunk_size >> 9;
6453 if (mddev->persistent) {
6454 mddev->max_disks = MD_SB_DISKS;
6455 mddev->flags = 0;
6456 mddev->sb_flags = 0;
6458 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
6460 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
6461 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
6462 mddev->bitmap_info.offset = 0;
6464 mddev->reshape_position = MaxSector;
6467 * Generate a 128 bit UUID
6469 get_random_bytes(mddev->uuid, 16);
6471 mddev->new_level = mddev->level;
6472 mddev->new_chunk_sectors = mddev->chunk_sectors;
6473 mddev->new_layout = mddev->layout;
6474 mddev->delta_disks = 0;
6475 mddev->reshape_backwards = 0;
6477 return 0;
6480 void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
6482 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6484 if (mddev->external_size)
6485 return;
6487 mddev->array_sectors = array_sectors;
6489 EXPORT_SYMBOL(md_set_array_sectors);
6491 static int update_size(struct mddev *mddev, sector_t num_sectors)
6493 struct md_rdev *rdev;
6494 int rv;
6495 int fit = (num_sectors == 0);
6497 /* cluster raid doesn't support update size */
6498 if (mddev_is_clustered(mddev))
6499 return -EINVAL;
6501 if (mddev->pers->resize == NULL)
6502 return -EINVAL;
6503 /* The "num_sectors" is the number of sectors of each device that
6504 * is used. This can only make sense for arrays with redundancy.
6505 * linear and raid0 always use whatever space is available. We can only
6506 * consider changing this number if no resync or reconstruction is
6507 * happening, and if the new size is acceptable. It must fit before the
6508 * sb_start or, if that is <data_offset, it must fit before the size
6509 * of each device. If num_sectors is zero, we find the largest size
6510 * that fits.
6512 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6513 mddev->sync_thread)
6514 return -EBUSY;
6515 if (mddev->ro)
6516 return -EROFS;
6518 rdev_for_each(rdev, mddev) {
6519 sector_t avail = rdev->sectors;
6521 if (fit && (num_sectors == 0 || num_sectors > avail))
6522 num_sectors = avail;
6523 if (avail < num_sectors)
6524 return -ENOSPC;
6526 rv = mddev->pers->resize(mddev, num_sectors);
6527 if (!rv) {
6528 if (mddev->queue) {
6529 set_capacity(mddev->gendisk, mddev->array_sectors);
6530 revalidate_disk(mddev->gendisk);
6533 return rv;
6536 static int update_raid_disks(struct mddev *mddev, int raid_disks)
6538 int rv;
6539 struct md_rdev *rdev;
6540 /* change the number of raid disks */
6541 if (mddev->pers->check_reshape == NULL)
6542 return -EINVAL;
6543 if (mddev->ro)
6544 return -EROFS;
6545 if (raid_disks <= 0 ||
6546 (mddev->max_disks && raid_disks >= mddev->max_disks))
6547 return -EINVAL;
6548 if (mddev->sync_thread ||
6549 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6550 mddev->reshape_position != MaxSector)
6551 return -EBUSY;
6553 rdev_for_each(rdev, mddev) {
6554 if (mddev->raid_disks < raid_disks &&
6555 rdev->data_offset < rdev->new_data_offset)
6556 return -EINVAL;
6557 if (mddev->raid_disks > raid_disks &&
6558 rdev->data_offset > rdev->new_data_offset)
6559 return -EINVAL;
6562 mddev->delta_disks = raid_disks - mddev->raid_disks;
6563 if (mddev->delta_disks < 0)
6564 mddev->reshape_backwards = 1;
6565 else if (mddev->delta_disks > 0)
6566 mddev->reshape_backwards = 0;
6568 rv = mddev->pers->check_reshape(mddev);
6569 if (rv < 0) {
6570 mddev->delta_disks = 0;
6571 mddev->reshape_backwards = 0;
6573 return rv;
6577 * update_array_info is used to change the configuration of an
6578 * on-line array.
6579 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6580 * fields in the info are checked against the array.
6581 * Any differences that cannot be handled will cause an error.
6582 * Normally, only one change can be managed at a time.
6584 static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
6586 int rv = 0;
6587 int cnt = 0;
6588 int state = 0;
6590 /* calculate expected state,ignoring low bits */
6591 if (mddev->bitmap && mddev->bitmap_info.offset)
6592 state |= (1 << MD_SB_BITMAP_PRESENT);
6594 if (mddev->major_version != info->major_version ||
6595 mddev->minor_version != info->minor_version ||
6596 /* mddev->patch_version != info->patch_version || */
6597 mddev->ctime != info->ctime ||
6598 mddev->level != info->level ||
6599 /* mddev->layout != info->layout || */
6600 mddev->persistent != !info->not_persistent ||
6601 mddev->chunk_sectors != info->chunk_size >> 9 ||
6602 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6603 ((state^info->state) & 0xfffffe00)
6605 return -EINVAL;
6606 /* Check there is only one change */
6607 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6608 cnt++;
6609 if (mddev->raid_disks != info->raid_disks)
6610 cnt++;
6611 if (mddev->layout != info->layout)
6612 cnt++;
6613 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6614 cnt++;
6615 if (cnt == 0)
6616 return 0;
6617 if (cnt > 1)
6618 return -EINVAL;
6620 if (mddev->layout != info->layout) {
6621 /* Change layout
6622 * we don't need to do anything at the md level, the
6623 * personality will take care of it all.
6625 if (mddev->pers->check_reshape == NULL)
6626 return -EINVAL;
6627 else {
6628 mddev->new_layout = info->layout;
6629 rv = mddev->pers->check_reshape(mddev);
6630 if (rv)
6631 mddev->new_layout = mddev->layout;
6632 return rv;
6635 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6636 rv = update_size(mddev, (sector_t)info->size * 2);
6638 if (mddev->raid_disks != info->raid_disks)
6639 rv = update_raid_disks(mddev, info->raid_disks);
6641 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
6642 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6643 rv = -EINVAL;
6644 goto err;
6646 if (mddev->recovery || mddev->sync_thread) {
6647 rv = -EBUSY;
6648 goto err;
6650 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6651 struct bitmap *bitmap;
6652 /* add the bitmap */
6653 if (mddev->bitmap) {
6654 rv = -EEXIST;
6655 goto err;
6657 if (mddev->bitmap_info.default_offset == 0) {
6658 rv = -EINVAL;
6659 goto err;
6661 mddev->bitmap_info.offset =
6662 mddev->bitmap_info.default_offset;
6663 mddev->bitmap_info.space =
6664 mddev->bitmap_info.default_space;
6665 mddev->pers->quiesce(mddev, 1);
6666 bitmap = bitmap_create(mddev, -1);
6667 if (!IS_ERR(bitmap)) {
6668 mddev->bitmap = bitmap;
6669 rv = bitmap_load(mddev);
6670 } else
6671 rv = PTR_ERR(bitmap);
6672 if (rv)
6673 bitmap_destroy(mddev);
6674 mddev->pers->quiesce(mddev, 0);
6675 } else {
6676 /* remove the bitmap */
6677 if (!mddev->bitmap) {
6678 rv = -ENOENT;
6679 goto err;
6681 if (mddev->bitmap->storage.file) {
6682 rv = -EINVAL;
6683 goto err;
6685 if (mddev->bitmap_info.nodes) {
6686 /* hold PW on all the bitmap lock */
6687 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
6688 pr_warn("md: can't change bitmap to none since the array is in use by more than one node\n");
6689 rv = -EPERM;
6690 md_cluster_ops->unlock_all_bitmaps(mddev);
6691 goto err;
6694 mddev->bitmap_info.nodes = 0;
6695 md_cluster_ops->leave(mddev);
6697 mddev->pers->quiesce(mddev, 1);
6698 bitmap_destroy(mddev);
6699 mddev->pers->quiesce(mddev, 0);
6700 mddev->bitmap_info.offset = 0;
6703 md_update_sb(mddev, 1);
6704 return rv;
6705 err:
6706 return rv;
6709 static int set_disk_faulty(struct mddev *mddev, dev_t dev)
6711 struct md_rdev *rdev;
6712 int err = 0;
6714 if (mddev->pers == NULL)
6715 return -ENODEV;
6717 rcu_read_lock();
6718 rdev = find_rdev_rcu(mddev, dev);
6719 if (!rdev)
6720 err = -ENODEV;
6721 else {
6722 md_error(mddev, rdev);
6723 if (!test_bit(Faulty, &rdev->flags))
6724 err = -EBUSY;
6726 rcu_read_unlock();
6727 return err;
6731 * We have a problem here : there is no easy way to give a CHS
6732 * virtual geometry. We currently pretend that we have a 2 heads
6733 * 4 sectors (with a BIG number of cylinders...). This drives
6734 * dosfs just mad... ;-)
6736 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6738 struct mddev *mddev = bdev->bd_disk->private_data;
6740 geo->heads = 2;
6741 geo->sectors = 4;
6742 geo->cylinders = mddev->array_sectors / 8;
6743 return 0;
6746 static inline bool md_ioctl_valid(unsigned int cmd)
6748 switch (cmd) {
6749 case ADD_NEW_DISK:
6750 case BLKROSET:
6751 case GET_ARRAY_INFO:
6752 case GET_BITMAP_FILE:
6753 case GET_DISK_INFO:
6754 case HOT_ADD_DISK:
6755 case HOT_REMOVE_DISK:
6756 case RAID_AUTORUN:
6757 case RAID_VERSION:
6758 case RESTART_ARRAY_RW:
6759 case RUN_ARRAY:
6760 case SET_ARRAY_INFO:
6761 case SET_BITMAP_FILE:
6762 case SET_DISK_FAULTY:
6763 case STOP_ARRAY:
6764 case STOP_ARRAY_RO:
6765 case CLUSTERED_DISK_NACK:
6766 return true;
6767 default:
6768 return false;
6772 static int md_ioctl(struct block_device *bdev, fmode_t mode,
6773 unsigned int cmd, unsigned long arg)
6775 int err = 0;
6776 void __user *argp = (void __user *)arg;
6777 struct mddev *mddev = NULL;
6778 int ro;
6779 bool did_set_md_closing = false;
6781 if (!md_ioctl_valid(cmd))
6782 return -ENOTTY;
6784 switch (cmd) {
6785 case RAID_VERSION:
6786 case GET_ARRAY_INFO:
6787 case GET_DISK_INFO:
6788 break;
6789 default:
6790 if (!capable(CAP_SYS_ADMIN))
6791 return -EACCES;
6795 * Commands dealing with the RAID driver but not any
6796 * particular array:
6798 switch (cmd) {
6799 case RAID_VERSION:
6800 err = get_version(argp);
6801 goto out;
6803 #ifndef MODULE
6804 case RAID_AUTORUN:
6805 err = 0;
6806 autostart_arrays(arg);
6807 goto out;
6808 #endif
6809 default:;
6813 * Commands creating/starting a new array:
6816 mddev = bdev->bd_disk->private_data;
6818 if (!mddev) {
6819 BUG();
6820 goto out;
6823 /* Some actions do not requires the mutex */
6824 switch (cmd) {
6825 case GET_ARRAY_INFO:
6826 if (!mddev->raid_disks && !mddev->external)
6827 err = -ENODEV;
6828 else
6829 err = get_array_info(mddev, argp);
6830 goto out;
6832 case GET_DISK_INFO:
6833 if (!mddev->raid_disks && !mddev->external)
6834 err = -ENODEV;
6835 else
6836 err = get_disk_info(mddev, argp);
6837 goto out;
6839 case SET_DISK_FAULTY:
6840 err = set_disk_faulty(mddev, new_decode_dev(arg));
6841 goto out;
6843 case GET_BITMAP_FILE:
6844 err = get_bitmap_file(mddev, argp);
6845 goto out;
6849 if (cmd == ADD_NEW_DISK)
6850 /* need to ensure md_delayed_delete() has completed */
6851 flush_workqueue(md_misc_wq);
6853 if (cmd == HOT_REMOVE_DISK)
6854 /* need to ensure recovery thread has run */
6855 wait_event_interruptible_timeout(mddev->sb_wait,
6856 !test_bit(MD_RECOVERY_NEEDED,
6857 &mddev->recovery),
6858 msecs_to_jiffies(5000));
6859 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6860 /* Need to flush page cache, and ensure no-one else opens
6861 * and writes
6863 mutex_lock(&mddev->open_mutex);
6864 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
6865 mutex_unlock(&mddev->open_mutex);
6866 err = -EBUSY;
6867 goto out;
6869 WARN_ON_ONCE(test_bit(MD_CLOSING, &mddev->flags));
6870 set_bit(MD_CLOSING, &mddev->flags);
6871 did_set_md_closing = true;
6872 mutex_unlock(&mddev->open_mutex);
6873 sync_blockdev(bdev);
6875 err = mddev_lock(mddev);
6876 if (err) {
6877 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
6878 err, cmd);
6879 goto out;
6882 if (cmd == SET_ARRAY_INFO) {
6883 mdu_array_info_t info;
6884 if (!arg)
6885 memset(&info, 0, sizeof(info));
6886 else if (copy_from_user(&info, argp, sizeof(info))) {
6887 err = -EFAULT;
6888 goto unlock;
6890 if (mddev->pers) {
6891 err = update_array_info(mddev, &info);
6892 if (err) {
6893 pr_warn("md: couldn't update array info. %d\n", err);
6894 goto unlock;
6896 goto unlock;
6898 if (!list_empty(&mddev->disks)) {
6899 pr_warn("md: array %s already has disks!\n", mdname(mddev));
6900 err = -EBUSY;
6901 goto unlock;
6903 if (mddev->raid_disks) {
6904 pr_warn("md: array %s already initialised!\n", mdname(mddev));
6905 err = -EBUSY;
6906 goto unlock;
6908 err = set_array_info(mddev, &info);
6909 if (err) {
6910 pr_warn("md: couldn't set array info. %d\n", err);
6911 goto unlock;
6913 goto unlock;
6917 * Commands querying/configuring an existing array:
6919 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
6920 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
6921 if ((!mddev->raid_disks && !mddev->external)
6922 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6923 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6924 && cmd != GET_BITMAP_FILE) {
6925 err = -ENODEV;
6926 goto unlock;
6930 * Commands even a read-only array can execute:
6932 switch (cmd) {
6933 case RESTART_ARRAY_RW:
6934 err = restart_array(mddev);
6935 goto unlock;
6937 case STOP_ARRAY:
6938 err = do_md_stop(mddev, 0, bdev);
6939 goto unlock;
6941 case STOP_ARRAY_RO:
6942 err = md_set_readonly(mddev, bdev);
6943 goto unlock;
6945 case HOT_REMOVE_DISK:
6946 err = hot_remove_disk(mddev, new_decode_dev(arg));
6947 goto unlock;
6949 case ADD_NEW_DISK:
6950 /* We can support ADD_NEW_DISK on read-only arrays
6951 * only if we are re-adding a preexisting device.
6952 * So require mddev->pers and MD_DISK_SYNC.
6954 if (mddev->pers) {
6955 mdu_disk_info_t info;
6956 if (copy_from_user(&info, argp, sizeof(info)))
6957 err = -EFAULT;
6958 else if (!(info.state & (1<<MD_DISK_SYNC)))
6959 /* Need to clear read-only for this */
6960 break;
6961 else
6962 err = add_new_disk(mddev, &info);
6963 goto unlock;
6965 break;
6967 case BLKROSET:
6968 if (get_user(ro, (int __user *)(arg))) {
6969 err = -EFAULT;
6970 goto unlock;
6972 err = -EINVAL;
6974 /* if the bdev is going readonly the value of mddev->ro
6975 * does not matter, no writes are coming
6977 if (ro)
6978 goto unlock;
6980 /* are we are already prepared for writes? */
6981 if (mddev->ro != 1)
6982 goto unlock;
6984 /* transitioning to readauto need only happen for
6985 * arrays that call md_write_start
6987 if (mddev->pers) {
6988 err = restart_array(mddev);
6989 if (err == 0) {
6990 mddev->ro = 2;
6991 set_disk_ro(mddev->gendisk, 0);
6994 goto unlock;
6998 * The remaining ioctls are changing the state of the
6999 * superblock, so we do not allow them on read-only arrays.
7001 if (mddev->ro && mddev->pers) {
7002 if (mddev->ro == 2) {
7003 mddev->ro = 0;
7004 sysfs_notify_dirent_safe(mddev->sysfs_state);
7005 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7006 /* mddev_unlock will wake thread */
7007 /* If a device failed while we were read-only, we
7008 * need to make sure the metadata is updated now.
7010 if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) {
7011 mddev_unlock(mddev);
7012 wait_event(mddev->sb_wait,
7013 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) &&
7014 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
7015 mddev_lock_nointr(mddev);
7017 } else {
7018 err = -EROFS;
7019 goto unlock;
7023 switch (cmd) {
7024 case ADD_NEW_DISK:
7026 mdu_disk_info_t info;
7027 if (copy_from_user(&info, argp, sizeof(info)))
7028 err = -EFAULT;
7029 else
7030 err = add_new_disk(mddev, &info);
7031 goto unlock;
7034 case CLUSTERED_DISK_NACK:
7035 if (mddev_is_clustered(mddev))
7036 md_cluster_ops->new_disk_ack(mddev, false);
7037 else
7038 err = -EINVAL;
7039 goto unlock;
7041 case HOT_ADD_DISK:
7042 err = hot_add_disk(mddev, new_decode_dev(arg));
7043 goto unlock;
7045 case RUN_ARRAY:
7046 err = do_md_run(mddev);
7047 goto unlock;
7049 case SET_BITMAP_FILE:
7050 err = set_bitmap_file(mddev, (int)arg);
7051 goto unlock;
7053 default:
7054 err = -EINVAL;
7055 goto unlock;
7058 unlock:
7059 if (mddev->hold_active == UNTIL_IOCTL &&
7060 err != -EINVAL)
7061 mddev->hold_active = 0;
7062 mddev_unlock(mddev);
7063 out:
7064 if(did_set_md_closing)
7065 clear_bit(MD_CLOSING, &mddev->flags);
7066 return err;
7068 #ifdef CONFIG_COMPAT
7069 static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7070 unsigned int cmd, unsigned long arg)
7072 switch (cmd) {
7073 case HOT_REMOVE_DISK:
7074 case HOT_ADD_DISK:
7075 case SET_DISK_FAULTY:
7076 case SET_BITMAP_FILE:
7077 /* These take in integer arg, do not convert */
7078 break;
7079 default:
7080 arg = (unsigned long)compat_ptr(arg);
7081 break;
7084 return md_ioctl(bdev, mode, cmd, arg);
7086 #endif /* CONFIG_COMPAT */
7088 static int md_open(struct block_device *bdev, fmode_t mode)
7091 * Succeed if we can lock the mddev, which confirms that
7092 * it isn't being stopped right now.
7094 struct mddev *mddev = mddev_find(bdev->bd_dev);
7095 int err;
7097 if (!mddev)
7098 return -ENODEV;
7100 if (mddev->gendisk != bdev->bd_disk) {
7101 /* we are racing with mddev_put which is discarding this
7102 * bd_disk.
7104 mddev_put(mddev);
7105 /* Wait until bdev->bd_disk is definitely gone */
7106 flush_workqueue(md_misc_wq);
7107 /* Then retry the open from the top */
7108 return -ERESTARTSYS;
7110 BUG_ON(mddev != bdev->bd_disk->private_data);
7112 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
7113 goto out;
7115 if (test_bit(MD_CLOSING, &mddev->flags)) {
7116 mutex_unlock(&mddev->open_mutex);
7117 err = -ENODEV;
7118 goto out;
7121 err = 0;
7122 atomic_inc(&mddev->openers);
7123 mutex_unlock(&mddev->open_mutex);
7125 check_disk_change(bdev);
7126 out:
7127 if (err)
7128 mddev_put(mddev);
7129 return err;
7132 static void md_release(struct gendisk *disk, fmode_t mode)
7134 struct mddev *mddev = disk->private_data;
7136 BUG_ON(!mddev);
7137 atomic_dec(&mddev->openers);
7138 mddev_put(mddev);
7141 static int md_media_changed(struct gendisk *disk)
7143 struct mddev *mddev = disk->private_data;
7145 return mddev->changed;
7148 static int md_revalidate(struct gendisk *disk)
7150 struct mddev *mddev = disk->private_data;
7152 mddev->changed = 0;
7153 return 0;
7155 static const struct block_device_operations md_fops =
7157 .owner = THIS_MODULE,
7158 .open = md_open,
7159 .release = md_release,
7160 .ioctl = md_ioctl,
7161 #ifdef CONFIG_COMPAT
7162 .compat_ioctl = md_compat_ioctl,
7163 #endif
7164 .getgeo = md_getgeo,
7165 .media_changed = md_media_changed,
7166 .revalidate_disk= md_revalidate,
7169 static int md_thread(void *arg)
7171 struct md_thread *thread = arg;
7174 * md_thread is a 'system-thread', it's priority should be very
7175 * high. We avoid resource deadlocks individually in each
7176 * raid personality. (RAID5 does preallocation) We also use RR and
7177 * the very same RT priority as kswapd, thus we will never get
7178 * into a priority inversion deadlock.
7180 * we definitely have to have equal or higher priority than
7181 * bdflush, otherwise bdflush will deadlock if there are too
7182 * many dirty RAID5 blocks.
7185 allow_signal(SIGKILL);
7186 while (!kthread_should_stop()) {
7188 /* We need to wait INTERRUPTIBLE so that
7189 * we don't add to the load-average.
7190 * That means we need to be sure no signals are
7191 * pending
7193 if (signal_pending(current))
7194 flush_signals(current);
7196 wait_event_interruptible_timeout
7197 (thread->wqueue,
7198 test_bit(THREAD_WAKEUP, &thread->flags)
7199 || kthread_should_stop() || kthread_should_park(),
7200 thread->timeout);
7202 clear_bit(THREAD_WAKEUP, &thread->flags);
7203 if (kthread_should_park())
7204 kthread_parkme();
7205 if (!kthread_should_stop())
7206 thread->run(thread);
7209 return 0;
7212 void md_wakeup_thread(struct md_thread *thread)
7214 if (thread) {
7215 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
7216 set_bit(THREAD_WAKEUP, &thread->flags);
7217 wake_up(&thread->wqueue);
7220 EXPORT_SYMBOL(md_wakeup_thread);
7222 struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7223 struct mddev *mddev, const char *name)
7225 struct md_thread *thread;
7227 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
7228 if (!thread)
7229 return NULL;
7231 init_waitqueue_head(&thread->wqueue);
7233 thread->run = run;
7234 thread->mddev = mddev;
7235 thread->timeout = MAX_SCHEDULE_TIMEOUT;
7236 thread->tsk = kthread_run(md_thread, thread,
7237 "%s_%s",
7238 mdname(thread->mddev),
7239 name);
7240 if (IS_ERR(thread->tsk)) {
7241 kfree(thread);
7242 return NULL;
7244 return thread;
7246 EXPORT_SYMBOL(md_register_thread);
7248 void md_unregister_thread(struct md_thread **threadp)
7250 struct md_thread *thread = *threadp;
7251 if (!thread)
7252 return;
7253 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
7254 /* Locking ensures that mddev_unlock does not wake_up a
7255 * non-existent thread
7257 spin_lock(&pers_lock);
7258 *threadp = NULL;
7259 spin_unlock(&pers_lock);
7261 kthread_stop(thread->tsk);
7262 kfree(thread);
7264 EXPORT_SYMBOL(md_unregister_thread);
7266 void md_error(struct mddev *mddev, struct md_rdev *rdev)
7268 if (!rdev || test_bit(Faulty, &rdev->flags))
7269 return;
7271 if (!mddev->pers || !mddev->pers->error_handler)
7272 return;
7273 mddev->pers->error_handler(mddev,rdev);
7274 if (mddev->degraded)
7275 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7276 sysfs_notify_dirent_safe(rdev->sysfs_state);
7277 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7278 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7279 md_wakeup_thread(mddev->thread);
7280 if (mddev->event_work.func)
7281 queue_work(md_misc_wq, &mddev->event_work);
7282 md_new_event(mddev);
7284 EXPORT_SYMBOL(md_error);
7286 /* seq_file implementation /proc/mdstat */
7288 static void status_unused(struct seq_file *seq)
7290 int i = 0;
7291 struct md_rdev *rdev;
7293 seq_printf(seq, "unused devices: ");
7295 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
7296 char b[BDEVNAME_SIZE];
7297 i++;
7298 seq_printf(seq, "%s ",
7299 bdevname(rdev->bdev,b));
7301 if (!i)
7302 seq_printf(seq, "<none>");
7304 seq_printf(seq, "\n");
7307 static int status_resync(struct seq_file *seq, struct mddev *mddev)
7309 sector_t max_sectors, resync, res;
7310 unsigned long dt, db;
7311 sector_t rt;
7312 int scale;
7313 unsigned int per_milli;
7315 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7316 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7317 max_sectors = mddev->resync_max_sectors;
7318 else
7319 max_sectors = mddev->dev_sectors;
7321 resync = mddev->curr_resync;
7322 if (resync <= 3) {
7323 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7324 /* Still cleaning up */
7325 resync = max_sectors;
7326 } else
7327 resync -= atomic_read(&mddev->recovery_active);
7329 if (resync == 0) {
7330 if (mddev->recovery_cp < MaxSector) {
7331 seq_printf(seq, "\tresync=PENDING");
7332 return 1;
7334 return 0;
7336 if (resync < 3) {
7337 seq_printf(seq, "\tresync=DELAYED");
7338 return 1;
7341 WARN_ON(max_sectors == 0);
7342 /* Pick 'scale' such that (resync>>scale)*1000 will fit
7343 * in a sector_t, and (max_sectors>>scale) will fit in a
7344 * u32, as those are the requirements for sector_div.
7345 * Thus 'scale' must be at least 10
7347 scale = 10;
7348 if (sizeof(sector_t) > sizeof(unsigned long)) {
7349 while ( max_sectors/2 > (1ULL<<(scale+32)))
7350 scale++;
7352 res = (resync>>scale)*1000;
7353 sector_div(res, (u32)((max_sectors>>scale)+1));
7355 per_milli = res;
7357 int i, x = per_milli/50, y = 20-x;
7358 seq_printf(seq, "[");
7359 for (i = 0; i < x; i++)
7360 seq_printf(seq, "=");
7361 seq_printf(seq, ">");
7362 for (i = 0; i < y; i++)
7363 seq_printf(seq, ".");
7364 seq_printf(seq, "] ");
7366 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
7367 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7368 "reshape" :
7369 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7370 "check" :
7371 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7372 "resync" : "recovery"))),
7373 per_milli/10, per_milli % 10,
7374 (unsigned long long) resync/2,
7375 (unsigned long long) max_sectors/2);
7378 * dt: time from mark until now
7379 * db: blocks written from mark until now
7380 * rt: remaining time
7382 * rt is a sector_t, so could be 32bit or 64bit.
7383 * So we divide before multiply in case it is 32bit and close
7384 * to the limit.
7385 * We scale the divisor (db) by 32 to avoid losing precision
7386 * near the end of resync when the number of remaining sectors
7387 * is close to 'db'.
7388 * We then divide rt by 32 after multiplying by db to compensate.
7389 * The '+1' avoids division by zero if db is very small.
7391 dt = ((jiffies - mddev->resync_mark) / HZ);
7392 if (!dt) dt++;
7393 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7394 - mddev->resync_mark_cnt;
7396 rt = max_sectors - resync; /* number of remaining sectors */
7397 sector_div(rt, db/32+1);
7398 rt *= dt;
7399 rt >>= 5;
7401 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7402 ((unsigned long)rt % 60)/6);
7404 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
7405 return 1;
7408 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7410 struct list_head *tmp;
7411 loff_t l = *pos;
7412 struct mddev *mddev;
7414 if (l >= 0x10000)
7415 return NULL;
7416 if (!l--)
7417 /* header */
7418 return (void*)1;
7420 spin_lock(&all_mddevs_lock);
7421 list_for_each(tmp,&all_mddevs)
7422 if (!l--) {
7423 mddev = list_entry(tmp, struct mddev, all_mddevs);
7424 mddev_get(mddev);
7425 spin_unlock(&all_mddevs_lock);
7426 return mddev;
7428 spin_unlock(&all_mddevs_lock);
7429 if (!l--)
7430 return (void*)2;/* tail */
7431 return NULL;
7434 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7436 struct list_head *tmp;
7437 struct mddev *next_mddev, *mddev = v;
7439 ++*pos;
7440 if (v == (void*)2)
7441 return NULL;
7443 spin_lock(&all_mddevs_lock);
7444 if (v == (void*)1)
7445 tmp = all_mddevs.next;
7446 else
7447 tmp = mddev->all_mddevs.next;
7448 if (tmp != &all_mddevs)
7449 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
7450 else {
7451 next_mddev = (void*)2;
7452 *pos = 0x10000;
7454 spin_unlock(&all_mddevs_lock);
7456 if (v != (void*)1)
7457 mddev_put(mddev);
7458 return next_mddev;
7462 static void md_seq_stop(struct seq_file *seq, void *v)
7464 struct mddev *mddev = v;
7466 if (mddev && v != (void*)1 && v != (void*)2)
7467 mddev_put(mddev);
7470 static int md_seq_show(struct seq_file *seq, void *v)
7472 struct mddev *mddev = v;
7473 sector_t sectors;
7474 struct md_rdev *rdev;
7476 if (v == (void*)1) {
7477 struct md_personality *pers;
7478 seq_printf(seq, "Personalities : ");
7479 spin_lock(&pers_lock);
7480 list_for_each_entry(pers, &pers_list, list)
7481 seq_printf(seq, "[%s] ", pers->name);
7483 spin_unlock(&pers_lock);
7484 seq_printf(seq, "\n");
7485 seq->poll_event = atomic_read(&md_event_count);
7486 return 0;
7488 if (v == (void*)2) {
7489 status_unused(seq);
7490 return 0;
7493 spin_lock(&mddev->lock);
7494 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7495 seq_printf(seq, "%s : %sactive", mdname(mddev),
7496 mddev->pers ? "" : "in");
7497 if (mddev->pers) {
7498 if (mddev->ro==1)
7499 seq_printf(seq, " (read-only)");
7500 if (mddev->ro==2)
7501 seq_printf(seq, " (auto-read-only)");
7502 seq_printf(seq, " %s", mddev->pers->name);
7505 sectors = 0;
7506 rcu_read_lock();
7507 rdev_for_each_rcu(rdev, mddev) {
7508 char b[BDEVNAME_SIZE];
7509 seq_printf(seq, " %s[%d]",
7510 bdevname(rdev->bdev,b), rdev->desc_nr);
7511 if (test_bit(WriteMostly, &rdev->flags))
7512 seq_printf(seq, "(W)");
7513 if (test_bit(Journal, &rdev->flags))
7514 seq_printf(seq, "(J)");
7515 if (test_bit(Faulty, &rdev->flags)) {
7516 seq_printf(seq, "(F)");
7517 continue;
7519 if (rdev->raid_disk < 0)
7520 seq_printf(seq, "(S)"); /* spare */
7521 if (test_bit(Replacement, &rdev->flags))
7522 seq_printf(seq, "(R)");
7523 sectors += rdev->sectors;
7525 rcu_read_unlock();
7527 if (!list_empty(&mddev->disks)) {
7528 if (mddev->pers)
7529 seq_printf(seq, "\n %llu blocks",
7530 (unsigned long long)
7531 mddev->array_sectors / 2);
7532 else
7533 seq_printf(seq, "\n %llu blocks",
7534 (unsigned long long)sectors / 2);
7536 if (mddev->persistent) {
7537 if (mddev->major_version != 0 ||
7538 mddev->minor_version != 90) {
7539 seq_printf(seq," super %d.%d",
7540 mddev->major_version,
7541 mddev->minor_version);
7543 } else if (mddev->external)
7544 seq_printf(seq, " super external:%s",
7545 mddev->metadata_type);
7546 else
7547 seq_printf(seq, " super non-persistent");
7549 if (mddev->pers) {
7550 mddev->pers->status(seq, mddev);
7551 seq_printf(seq, "\n ");
7552 if (mddev->pers->sync_request) {
7553 if (status_resync(seq, mddev))
7554 seq_printf(seq, "\n ");
7556 } else
7557 seq_printf(seq, "\n ");
7559 bitmap_status(seq, mddev->bitmap);
7561 seq_printf(seq, "\n");
7563 spin_unlock(&mddev->lock);
7565 return 0;
7568 static const struct seq_operations md_seq_ops = {
7569 .start = md_seq_start,
7570 .next = md_seq_next,
7571 .stop = md_seq_stop,
7572 .show = md_seq_show,
7575 static int md_seq_open(struct inode *inode, struct file *file)
7577 struct seq_file *seq;
7578 int error;
7580 error = seq_open(file, &md_seq_ops);
7581 if (error)
7582 return error;
7584 seq = file->private_data;
7585 seq->poll_event = atomic_read(&md_event_count);
7586 return error;
7589 static int md_unloading;
7590 static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7592 struct seq_file *seq = filp->private_data;
7593 int mask;
7595 if (md_unloading)
7596 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
7597 poll_wait(filp, &md_event_waiters, wait);
7599 /* always allow read */
7600 mask = POLLIN | POLLRDNORM;
7602 if (seq->poll_event != atomic_read(&md_event_count))
7603 mask |= POLLERR | POLLPRI;
7604 return mask;
7607 static const struct file_operations md_seq_fops = {
7608 .owner = THIS_MODULE,
7609 .open = md_seq_open,
7610 .read = seq_read,
7611 .llseek = seq_lseek,
7612 .release = seq_release_private,
7613 .poll = mdstat_poll,
7616 int register_md_personality(struct md_personality *p)
7618 pr_debug("md: %s personality registered for level %d\n",
7619 p->name, p->level);
7620 spin_lock(&pers_lock);
7621 list_add_tail(&p->list, &pers_list);
7622 spin_unlock(&pers_lock);
7623 return 0;
7625 EXPORT_SYMBOL(register_md_personality);
7627 int unregister_md_personality(struct md_personality *p)
7629 pr_debug("md: %s personality unregistered\n", p->name);
7630 spin_lock(&pers_lock);
7631 list_del_init(&p->list);
7632 spin_unlock(&pers_lock);
7633 return 0;
7635 EXPORT_SYMBOL(unregister_md_personality);
7637 int register_md_cluster_operations(struct md_cluster_operations *ops,
7638 struct module *module)
7640 int ret = 0;
7641 spin_lock(&pers_lock);
7642 if (md_cluster_ops != NULL)
7643 ret = -EALREADY;
7644 else {
7645 md_cluster_ops = ops;
7646 md_cluster_mod = module;
7648 spin_unlock(&pers_lock);
7649 return ret;
7651 EXPORT_SYMBOL(register_md_cluster_operations);
7653 int unregister_md_cluster_operations(void)
7655 spin_lock(&pers_lock);
7656 md_cluster_ops = NULL;
7657 spin_unlock(&pers_lock);
7658 return 0;
7660 EXPORT_SYMBOL(unregister_md_cluster_operations);
7662 int md_setup_cluster(struct mddev *mddev, int nodes)
7664 if (!md_cluster_ops)
7665 request_module("md-cluster");
7666 spin_lock(&pers_lock);
7667 /* ensure module won't be unloaded */
7668 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
7669 pr_warn("can't find md-cluster module or get it's reference.\n");
7670 spin_unlock(&pers_lock);
7671 return -ENOENT;
7673 spin_unlock(&pers_lock);
7675 return md_cluster_ops->join(mddev, nodes);
7678 void md_cluster_stop(struct mddev *mddev)
7680 if (!md_cluster_ops)
7681 return;
7682 md_cluster_ops->leave(mddev);
7683 module_put(md_cluster_mod);
7686 static int is_mddev_idle(struct mddev *mddev, int init)
7688 struct md_rdev *rdev;
7689 int idle;
7690 int curr_events;
7692 idle = 1;
7693 rcu_read_lock();
7694 rdev_for_each_rcu(rdev, mddev) {
7695 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
7696 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7697 (int)part_stat_read(&disk->part0, sectors[1]) -
7698 atomic_read(&disk->sync_io);
7699 /* sync IO will cause sync_io to increase before the disk_stats
7700 * as sync_io is counted when a request starts, and
7701 * disk_stats is counted when it completes.
7702 * So resync activity will cause curr_events to be smaller than
7703 * when there was no such activity.
7704 * non-sync IO will cause disk_stat to increase without
7705 * increasing sync_io so curr_events will (eventually)
7706 * be larger than it was before. Once it becomes
7707 * substantially larger, the test below will cause
7708 * the array to appear non-idle, and resync will slow
7709 * down.
7710 * If there is a lot of outstanding resync activity when
7711 * we set last_event to curr_events, then all that activity
7712 * completing might cause the array to appear non-idle
7713 * and resync will be slowed down even though there might
7714 * not have been non-resync activity. This will only
7715 * happen once though. 'last_events' will soon reflect
7716 * the state where there is little or no outstanding
7717 * resync requests, and further resync activity will
7718 * always make curr_events less than last_events.
7721 if (init || curr_events - rdev->last_events > 64) {
7722 rdev->last_events = curr_events;
7723 idle = 0;
7726 rcu_read_unlock();
7727 return idle;
7730 void md_done_sync(struct mddev *mddev, int blocks, int ok)
7732 /* another "blocks" (512byte) blocks have been synced */
7733 atomic_sub(blocks, &mddev->recovery_active);
7734 wake_up(&mddev->recovery_wait);
7735 if (!ok) {
7736 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7737 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
7738 md_wakeup_thread(mddev->thread);
7739 // stop recovery, signal do_sync ....
7742 EXPORT_SYMBOL(md_done_sync);
7744 /* md_write_start(mddev, bi)
7745 * If we need to update some array metadata (e.g. 'active' flag
7746 * in superblock) before writing, schedule a superblock update
7747 * and wait for it to complete.
7749 void md_write_start(struct mddev *mddev, struct bio *bi)
7751 int did_change = 0;
7752 if (bio_data_dir(bi) != WRITE)
7753 return;
7755 BUG_ON(mddev->ro == 1);
7756 if (mddev->ro == 2) {
7757 /* need to switch to read/write */
7758 mddev->ro = 0;
7759 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7760 md_wakeup_thread(mddev->thread);
7761 md_wakeup_thread(mddev->sync_thread);
7762 did_change = 1;
7764 atomic_inc(&mddev->writes_pending);
7765 if (mddev->safemode == 1)
7766 mddev->safemode = 0;
7767 if (mddev->in_sync) {
7768 spin_lock(&mddev->lock);
7769 if (mddev->in_sync) {
7770 mddev->in_sync = 0;
7771 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
7772 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
7773 md_wakeup_thread(mddev->thread);
7774 did_change = 1;
7776 spin_unlock(&mddev->lock);
7778 if (did_change)
7779 sysfs_notify_dirent_safe(mddev->sysfs_state);
7780 wait_event(mddev->sb_wait,
7781 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
7783 EXPORT_SYMBOL(md_write_start);
7785 void md_write_end(struct mddev *mddev)
7787 if (atomic_dec_and_test(&mddev->writes_pending)) {
7788 if (mddev->safemode == 2)
7789 md_wakeup_thread(mddev->thread);
7790 else if (mddev->safemode_delay)
7791 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7794 EXPORT_SYMBOL(md_write_end);
7796 /* md_allow_write(mddev)
7797 * Calling this ensures that the array is marked 'active' so that writes
7798 * may proceed without blocking. It is important to call this before
7799 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7800 * Must be called with mddev_lock held.
7802 * In the ->external case MD_SB_CHANGE_PENDING can not be cleared until mddev->lock
7803 * is dropped, so return -EAGAIN after notifying userspace.
7805 int md_allow_write(struct mddev *mddev)
7807 if (!mddev->pers)
7808 return 0;
7809 if (mddev->ro)
7810 return 0;
7811 if (!mddev->pers->sync_request)
7812 return 0;
7814 spin_lock(&mddev->lock);
7815 if (mddev->in_sync) {
7816 mddev->in_sync = 0;
7817 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
7818 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
7819 if (mddev->safemode_delay &&
7820 mddev->safemode == 0)
7821 mddev->safemode = 1;
7822 spin_unlock(&mddev->lock);
7823 md_update_sb(mddev, 0);
7824 sysfs_notify_dirent_safe(mddev->sysfs_state);
7825 } else
7826 spin_unlock(&mddev->lock);
7828 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
7829 return -EAGAIN;
7830 else
7831 return 0;
7833 EXPORT_SYMBOL_GPL(md_allow_write);
7835 #define SYNC_MARKS 10
7836 #define SYNC_MARK_STEP (3*HZ)
7837 #define UPDATE_FREQUENCY (5*60*HZ)
7838 void md_do_sync(struct md_thread *thread)
7840 struct mddev *mddev = thread->mddev;
7841 struct mddev *mddev2;
7842 unsigned int currspeed = 0,
7843 window;
7844 sector_t max_sectors,j, io_sectors, recovery_done;
7845 unsigned long mark[SYNC_MARKS];
7846 unsigned long update_time;
7847 sector_t mark_cnt[SYNC_MARKS];
7848 int last_mark,m;
7849 struct list_head *tmp;
7850 sector_t last_check;
7851 int skipped = 0;
7852 struct md_rdev *rdev;
7853 char *desc, *action = NULL;
7854 struct blk_plug plug;
7855 int ret;
7857 /* just incase thread restarts... */
7858 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7859 return;
7860 if (mddev->ro) {/* never try to sync a read-only array */
7861 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7862 return;
7865 if (mddev_is_clustered(mddev)) {
7866 ret = md_cluster_ops->resync_start(mddev);
7867 if (ret)
7868 goto skip;
7870 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
7871 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7872 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
7873 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
7874 && ((unsigned long long)mddev->curr_resync_completed
7875 < (unsigned long long)mddev->resync_max_sectors))
7876 goto skip;
7879 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7880 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
7881 desc = "data-check";
7882 action = "check";
7883 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7884 desc = "requested-resync";
7885 action = "repair";
7886 } else
7887 desc = "resync";
7888 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7889 desc = "reshape";
7890 else
7891 desc = "recovery";
7893 mddev->last_sync_action = action ?: desc;
7895 /* we overload curr_resync somewhat here.
7896 * 0 == not engaged in resync at all
7897 * 2 == checking that there is no conflict with another sync
7898 * 1 == like 2, but have yielded to allow conflicting resync to
7899 * commense
7900 * other == active in resync - this many blocks
7902 * Before starting a resync we must have set curr_resync to
7903 * 2, and then checked that every "conflicting" array has curr_resync
7904 * less than ours. When we find one that is the same or higher
7905 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7906 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7907 * This will mean we have to start checking from the beginning again.
7911 do {
7912 int mddev2_minor = -1;
7913 mddev->curr_resync = 2;
7915 try_again:
7916 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7917 goto skip;
7918 for_each_mddev(mddev2, tmp) {
7919 if (mddev2 == mddev)
7920 continue;
7921 if (!mddev->parallel_resync
7922 && mddev2->curr_resync
7923 && match_mddev_units(mddev, mddev2)) {
7924 DEFINE_WAIT(wq);
7925 if (mddev < mddev2 && mddev->curr_resync == 2) {
7926 /* arbitrarily yield */
7927 mddev->curr_resync = 1;
7928 wake_up(&resync_wait);
7930 if (mddev > mddev2 && mddev->curr_resync == 1)
7931 /* no need to wait here, we can wait the next
7932 * time 'round when curr_resync == 2
7934 continue;
7935 /* We need to wait 'interruptible' so as not to
7936 * contribute to the load average, and not to
7937 * be caught by 'softlockup'
7939 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
7940 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7941 mddev2->curr_resync >= mddev->curr_resync) {
7942 if (mddev2_minor != mddev2->md_minor) {
7943 mddev2_minor = mddev2->md_minor;
7944 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
7945 desc, mdname(mddev),
7946 mdname(mddev2));
7948 mddev_put(mddev2);
7949 if (signal_pending(current))
7950 flush_signals(current);
7951 schedule();
7952 finish_wait(&resync_wait, &wq);
7953 goto try_again;
7955 finish_wait(&resync_wait, &wq);
7958 } while (mddev->curr_resync < 2);
7960 j = 0;
7961 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7962 /* resync follows the size requested by the personality,
7963 * which defaults to physical size, but can be virtual size
7965 max_sectors = mddev->resync_max_sectors;
7966 atomic64_set(&mddev->resync_mismatches, 0);
7967 /* we don't use the checkpoint if there's a bitmap */
7968 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7969 j = mddev->resync_min;
7970 else if (!mddev->bitmap)
7971 j = mddev->recovery_cp;
7973 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7974 max_sectors = mddev->resync_max_sectors;
7975 else {
7976 /* recovery follows the physical size of devices */
7977 max_sectors = mddev->dev_sectors;
7978 j = MaxSector;
7979 rcu_read_lock();
7980 rdev_for_each_rcu(rdev, mddev)
7981 if (rdev->raid_disk >= 0 &&
7982 !test_bit(Journal, &rdev->flags) &&
7983 !test_bit(Faulty, &rdev->flags) &&
7984 !test_bit(In_sync, &rdev->flags) &&
7985 rdev->recovery_offset < j)
7986 j = rdev->recovery_offset;
7987 rcu_read_unlock();
7989 /* If there is a bitmap, we need to make sure all
7990 * writes that started before we added a spare
7991 * complete before we start doing a recovery.
7992 * Otherwise the write might complete and (via
7993 * bitmap_endwrite) set a bit in the bitmap after the
7994 * recovery has checked that bit and skipped that
7995 * region.
7997 if (mddev->bitmap) {
7998 mddev->pers->quiesce(mddev, 1);
7999 mddev->pers->quiesce(mddev, 0);
8003 pr_info("md: %s of RAID array %s\n", desc, mdname(mddev));
8004 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev));
8005 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8006 speed_max(mddev), desc);
8008 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
8010 io_sectors = 0;
8011 for (m = 0; m < SYNC_MARKS; m++) {
8012 mark[m] = jiffies;
8013 mark_cnt[m] = io_sectors;
8015 last_mark = 0;
8016 mddev->resync_mark = mark[last_mark];
8017 mddev->resync_mark_cnt = mark_cnt[last_mark];
8020 * Tune reconstruction:
8022 window = 32*(PAGE_SIZE/512);
8023 pr_debug("md: using %dk window, over a total of %lluk.\n",
8024 window/2, (unsigned long long)max_sectors/2);
8026 atomic_set(&mddev->recovery_active, 0);
8027 last_check = 0;
8029 if (j>2) {
8030 pr_debug("md: resuming %s of %s from checkpoint.\n",
8031 desc, mdname(mddev));
8032 mddev->curr_resync = j;
8033 } else
8034 mddev->curr_resync = 3; /* no longer delayed */
8035 mddev->curr_resync_completed = j;
8036 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8037 md_new_event(mddev);
8038 update_time = jiffies;
8040 blk_start_plug(&plug);
8041 while (j < max_sectors) {
8042 sector_t sectors;
8044 skipped = 0;
8046 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8047 ((mddev->curr_resync > mddev->curr_resync_completed &&
8048 (mddev->curr_resync - mddev->curr_resync_completed)
8049 > (max_sectors >> 4)) ||
8050 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
8051 (j - mddev->curr_resync_completed)*2
8052 >= mddev->resync_max - mddev->curr_resync_completed ||
8053 mddev->curr_resync_completed > mddev->resync_max
8054 )) {
8055 /* time to update curr_resync_completed */
8056 wait_event(mddev->recovery_wait,
8057 atomic_read(&mddev->recovery_active) == 0);
8058 mddev->curr_resync_completed = j;
8059 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8060 j > mddev->recovery_cp)
8061 mddev->recovery_cp = j;
8062 update_time = jiffies;
8063 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8064 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8067 while (j >= mddev->resync_max &&
8068 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8069 /* As this condition is controlled by user-space,
8070 * we can block indefinitely, so use '_interruptible'
8071 * to avoid triggering warnings.
8073 flush_signals(current); /* just in case */
8074 wait_event_interruptible(mddev->recovery_wait,
8075 mddev->resync_max > j
8076 || test_bit(MD_RECOVERY_INTR,
8077 &mddev->recovery));
8080 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8081 break;
8083 sectors = mddev->pers->sync_request(mddev, j, &skipped);
8084 if (sectors == 0) {
8085 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
8086 break;
8089 if (!skipped) { /* actual IO requested */
8090 io_sectors += sectors;
8091 atomic_add(sectors, &mddev->recovery_active);
8094 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8095 break;
8097 j += sectors;
8098 if (j > max_sectors)
8099 /* when skipping, extra large numbers can be returned. */
8100 j = max_sectors;
8101 if (j > 2)
8102 mddev->curr_resync = j;
8103 mddev->curr_mark_cnt = io_sectors;
8104 if (last_check == 0)
8105 /* this is the earliest that rebuild will be
8106 * visible in /proc/mdstat
8108 md_new_event(mddev);
8110 if (last_check + window > io_sectors || j == max_sectors)
8111 continue;
8113 last_check = io_sectors;
8114 repeat:
8115 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8116 /* step marks */
8117 int next = (last_mark+1) % SYNC_MARKS;
8119 mddev->resync_mark = mark[next];
8120 mddev->resync_mark_cnt = mark_cnt[next];
8121 mark[next] = jiffies;
8122 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
8123 last_mark = next;
8126 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8127 break;
8130 * this loop exits only if either when we are slower than
8131 * the 'hard' speed limit, or the system was IO-idle for
8132 * a jiffy.
8133 * the system might be non-idle CPU-wise, but we only care
8134 * about not overloading the IO subsystem. (things like an
8135 * e2fsck being done on the RAID array should execute fast)
8137 cond_resched();
8139 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8140 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
8141 /((jiffies-mddev->resync_mark)/HZ +1) +1;
8143 if (currspeed > speed_min(mddev)) {
8144 if (currspeed > speed_max(mddev)) {
8145 msleep(500);
8146 goto repeat;
8148 if (!is_mddev_idle(mddev, 0)) {
8150 * Give other IO more of a chance.
8151 * The faster the devices, the less we wait.
8153 wait_event(mddev->recovery_wait,
8154 !atomic_read(&mddev->recovery_active));
8158 pr_info("md: %s: %s %s.\n",mdname(mddev), desc,
8159 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8160 ? "interrupted" : "done");
8162 * this also signals 'finished resyncing' to md_stop
8164 blk_finish_plug(&plug);
8165 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8167 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8168 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8169 mddev->curr_resync > 3) {
8170 mddev->curr_resync_completed = mddev->curr_resync;
8171 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8173 mddev->pers->sync_request(mddev, max_sectors, &skipped);
8175 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
8176 mddev->curr_resync > 3) {
8177 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8178 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8179 if (mddev->curr_resync >= mddev->recovery_cp) {
8180 pr_debug("md: checkpointing %s of %s.\n",
8181 desc, mdname(mddev));
8182 if (test_bit(MD_RECOVERY_ERROR,
8183 &mddev->recovery))
8184 mddev->recovery_cp =
8185 mddev->curr_resync_completed;
8186 else
8187 mddev->recovery_cp =
8188 mddev->curr_resync;
8190 } else
8191 mddev->recovery_cp = MaxSector;
8192 } else {
8193 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8194 mddev->curr_resync = MaxSector;
8195 rcu_read_lock();
8196 rdev_for_each_rcu(rdev, mddev)
8197 if (rdev->raid_disk >= 0 &&
8198 mddev->delta_disks >= 0 &&
8199 !test_bit(Journal, &rdev->flags) &&
8200 !test_bit(Faulty, &rdev->flags) &&
8201 !test_bit(In_sync, &rdev->flags) &&
8202 rdev->recovery_offset < mddev->curr_resync)
8203 rdev->recovery_offset = mddev->curr_resync;
8204 rcu_read_unlock();
8207 skip:
8208 /* set CHANGE_PENDING here since maybe another update is needed,
8209 * so other nodes are informed. It should be harmless for normal
8210 * raid */
8211 set_mask_bits(&mddev->sb_flags, 0,
8212 BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS));
8214 spin_lock(&mddev->lock);
8215 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8216 /* We completed so min/max setting can be forgotten if used. */
8217 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8218 mddev->resync_min = 0;
8219 mddev->resync_max = MaxSector;
8220 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8221 mddev->resync_min = mddev->curr_resync_completed;
8222 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
8223 mddev->curr_resync = 0;
8224 spin_unlock(&mddev->lock);
8226 wake_up(&resync_wait);
8227 md_wakeup_thread(mddev->thread);
8228 return;
8230 EXPORT_SYMBOL_GPL(md_do_sync);
8232 static int remove_and_add_spares(struct mddev *mddev,
8233 struct md_rdev *this)
8235 struct md_rdev *rdev;
8236 int spares = 0;
8237 int removed = 0;
8238 bool remove_some = false;
8240 rdev_for_each(rdev, mddev) {
8241 if ((this == NULL || rdev == this) &&
8242 rdev->raid_disk >= 0 &&
8243 !test_bit(Blocked, &rdev->flags) &&
8244 test_bit(Faulty, &rdev->flags) &&
8245 atomic_read(&rdev->nr_pending)==0) {
8246 /* Faulty non-Blocked devices with nr_pending == 0
8247 * never get nr_pending incremented,
8248 * never get Faulty cleared, and never get Blocked set.
8249 * So we can synchronize_rcu now rather than once per device
8251 remove_some = true;
8252 set_bit(RemoveSynchronized, &rdev->flags);
8256 if (remove_some)
8257 synchronize_rcu();
8258 rdev_for_each(rdev, mddev) {
8259 if ((this == NULL || rdev == this) &&
8260 rdev->raid_disk >= 0 &&
8261 !test_bit(Blocked, &rdev->flags) &&
8262 ((test_bit(RemoveSynchronized, &rdev->flags) ||
8263 (!test_bit(In_sync, &rdev->flags) &&
8264 !test_bit(Journal, &rdev->flags))) &&
8265 atomic_read(&rdev->nr_pending)==0)) {
8266 if (mddev->pers->hot_remove_disk(
8267 mddev, rdev) == 0) {
8268 sysfs_unlink_rdev(mddev, rdev);
8269 rdev->raid_disk = -1;
8270 removed++;
8273 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
8274 clear_bit(RemoveSynchronized, &rdev->flags);
8277 if (removed && mddev->kobj.sd)
8278 sysfs_notify(&mddev->kobj, NULL, "degraded");
8280 if (this && removed)
8281 goto no_add;
8283 rdev_for_each(rdev, mddev) {
8284 if (this && this != rdev)
8285 continue;
8286 if (test_bit(Candidate, &rdev->flags))
8287 continue;
8288 if (rdev->raid_disk >= 0 &&
8289 !test_bit(In_sync, &rdev->flags) &&
8290 !test_bit(Journal, &rdev->flags) &&
8291 !test_bit(Faulty, &rdev->flags))
8292 spares++;
8293 if (rdev->raid_disk >= 0)
8294 continue;
8295 if (test_bit(Faulty, &rdev->flags))
8296 continue;
8297 if (!test_bit(Journal, &rdev->flags)) {
8298 if (mddev->ro &&
8299 ! (rdev->saved_raid_disk >= 0 &&
8300 !test_bit(Bitmap_sync, &rdev->flags)))
8301 continue;
8303 rdev->recovery_offset = 0;
8305 if (mddev->pers->
8306 hot_add_disk(mddev, rdev) == 0) {
8307 if (sysfs_link_rdev(mddev, rdev))
8308 /* failure here is OK */;
8309 if (!test_bit(Journal, &rdev->flags))
8310 spares++;
8311 md_new_event(mddev);
8312 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
8315 no_add:
8316 if (removed)
8317 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
8318 return spares;
8321 static void md_start_sync(struct work_struct *ws)
8323 struct mddev *mddev = container_of(ws, struct mddev, del_work);
8325 mddev->sync_thread = md_register_thread(md_do_sync,
8326 mddev,
8327 "resync");
8328 if (!mddev->sync_thread) {
8329 pr_warn("%s: could not start resync thread...\n",
8330 mdname(mddev));
8331 /* leave the spares where they are, it shouldn't hurt */
8332 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8333 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8334 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8335 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8336 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
8337 wake_up(&resync_wait);
8338 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8339 &mddev->recovery))
8340 if (mddev->sysfs_action)
8341 sysfs_notify_dirent_safe(mddev->sysfs_action);
8342 } else
8343 md_wakeup_thread(mddev->sync_thread);
8344 sysfs_notify_dirent_safe(mddev->sysfs_action);
8345 md_new_event(mddev);
8349 * This routine is regularly called by all per-raid-array threads to
8350 * deal with generic issues like resync and super-block update.
8351 * Raid personalities that don't have a thread (linear/raid0) do not
8352 * need this as they never do any recovery or update the superblock.
8354 * It does not do any resync itself, but rather "forks" off other threads
8355 * to do that as needed.
8356 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8357 * "->recovery" and create a thread at ->sync_thread.
8358 * When the thread finishes it sets MD_RECOVERY_DONE
8359 * and wakeups up this thread which will reap the thread and finish up.
8360 * This thread also removes any faulty devices (with nr_pending == 0).
8362 * The overall approach is:
8363 * 1/ if the superblock needs updating, update it.
8364 * 2/ If a recovery thread is running, don't do anything else.
8365 * 3/ If recovery has finished, clean up, possibly marking spares active.
8366 * 4/ If there are any faulty devices, remove them.
8367 * 5/ If array is degraded, try to add spares devices
8368 * 6/ If array has spares or is not in-sync, start a resync thread.
8370 void md_check_recovery(struct mddev *mddev)
8372 if (mddev->suspended)
8373 return;
8375 if (mddev->bitmap)
8376 bitmap_daemon_work(mddev);
8378 if (signal_pending(current)) {
8379 if (mddev->pers->sync_request && !mddev->external) {
8380 pr_debug("md: %s in immediate safe mode\n",
8381 mdname(mddev));
8382 mddev->safemode = 2;
8384 flush_signals(current);
8387 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8388 return;
8389 if ( ! (
8390 (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
8391 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8392 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
8393 test_bit(MD_RELOAD_SB, &mddev->flags) ||
8394 (mddev->external == 0 && mddev->safemode == 1) ||
8395 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
8396 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
8398 return;
8400 if (mddev_trylock(mddev)) {
8401 int spares = 0;
8403 if (mddev->ro) {
8404 struct md_rdev *rdev;
8405 if (!mddev->external && mddev->in_sync)
8406 /* 'Blocked' flag not needed as failed devices
8407 * will be recorded if array switched to read/write.
8408 * Leaving it set will prevent the device
8409 * from being removed.
8411 rdev_for_each(rdev, mddev)
8412 clear_bit(Blocked, &rdev->flags);
8413 /* On a read-only array we can:
8414 * - remove failed devices
8415 * - add already-in_sync devices if the array itself
8416 * is in-sync.
8417 * As we only add devices that are already in-sync,
8418 * we can activate the spares immediately.
8420 remove_and_add_spares(mddev, NULL);
8421 /* There is no thread, but we need to call
8422 * ->spare_active and clear saved_raid_disk
8424 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
8425 md_reap_sync_thread(mddev);
8426 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
8427 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8428 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
8429 goto unlock;
8432 if (mddev_is_clustered(mddev)) {
8433 struct md_rdev *rdev;
8434 /* kick the device if another node issued a
8435 * remove disk.
8437 rdev_for_each(rdev, mddev) {
8438 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
8439 rdev->raid_disk < 0)
8440 md_kick_rdev_from_array(rdev);
8443 if (test_and_clear_bit(MD_RELOAD_SB, &mddev->flags))
8444 md_reload_sb(mddev, mddev->good_device_nr);
8447 if (!mddev->external) {
8448 int did_change = 0;
8449 spin_lock(&mddev->lock);
8450 if (mddev->safemode &&
8451 !atomic_read(&mddev->writes_pending) &&
8452 !mddev->in_sync &&
8453 mddev->recovery_cp == MaxSector) {
8454 mddev->in_sync = 1;
8455 did_change = 1;
8456 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8458 if (mddev->safemode == 1)
8459 mddev->safemode = 0;
8460 spin_unlock(&mddev->lock);
8461 if (did_change)
8462 sysfs_notify_dirent_safe(mddev->sysfs_state);
8465 if (mddev->sb_flags)
8466 md_update_sb(mddev, 0);
8468 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8469 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8470 /* resync/recovery still happening */
8471 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8472 goto unlock;
8474 if (mddev->sync_thread) {
8475 md_reap_sync_thread(mddev);
8476 goto unlock;
8478 /* Set RUNNING before clearing NEEDED to avoid
8479 * any transients in the value of "sync_action".
8481 mddev->curr_resync_completed = 0;
8482 spin_lock(&mddev->lock);
8483 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
8484 spin_unlock(&mddev->lock);
8485 /* Clear some bits that don't mean anything, but
8486 * might be left set
8488 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8489 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
8491 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8492 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
8493 goto not_running;
8494 /* no recovery is running.
8495 * remove any failed drives, then
8496 * add spares if possible.
8497 * Spares are also removed and re-added, to allow
8498 * the personality to fail the re-add.
8501 if (mddev->reshape_position != MaxSector) {
8502 if (mddev->pers->check_reshape == NULL ||
8503 mddev->pers->check_reshape(mddev) != 0)
8504 /* Cannot proceed */
8505 goto not_running;
8506 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8507 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
8508 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
8509 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8510 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8511 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8512 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
8513 } else if (mddev->recovery_cp < MaxSector) {
8514 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8515 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
8516 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8517 /* nothing to be done ... */
8518 goto not_running;
8520 if (mddev->pers->sync_request) {
8521 if (spares) {
8522 /* We are adding a device or devices to an array
8523 * which has the bitmap stored on all devices.
8524 * So make sure all bitmap pages get written
8526 bitmap_write_all(mddev->bitmap);
8528 INIT_WORK(&mddev->del_work, md_start_sync);
8529 queue_work(md_misc_wq, &mddev->del_work);
8530 goto unlock;
8532 not_running:
8533 if (!mddev->sync_thread) {
8534 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
8535 wake_up(&resync_wait);
8536 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8537 &mddev->recovery))
8538 if (mddev->sysfs_action)
8539 sysfs_notify_dirent_safe(mddev->sysfs_action);
8541 unlock:
8542 wake_up(&mddev->sb_wait);
8543 mddev_unlock(mddev);
8546 EXPORT_SYMBOL(md_check_recovery);
8548 void md_reap_sync_thread(struct mddev *mddev)
8550 struct md_rdev *rdev;
8552 /* resync has finished, collect result */
8553 md_unregister_thread(&mddev->sync_thread);
8554 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8555 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8556 /* success...*/
8557 /* activate any spares */
8558 if (mddev->pers->spare_active(mddev)) {
8559 sysfs_notify(&mddev->kobj, NULL,
8560 "degraded");
8561 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
8564 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8565 mddev->pers->finish_reshape)
8566 mddev->pers->finish_reshape(mddev);
8568 /* If array is no-longer degraded, then any saved_raid_disk
8569 * information must be scrapped.
8571 if (!mddev->degraded)
8572 rdev_for_each(rdev, mddev)
8573 rdev->saved_raid_disk = -1;
8575 md_update_sb(mddev, 1);
8576 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
8577 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
8578 * clustered raid */
8579 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
8580 md_cluster_ops->resync_finish(mddev);
8581 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
8582 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
8583 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8584 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8585 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8586 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8587 wake_up(&resync_wait);
8588 /* flag recovery needed just to double check */
8589 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8590 sysfs_notify_dirent_safe(mddev->sysfs_action);
8591 md_new_event(mddev);
8592 if (mddev->event_work.func)
8593 queue_work(md_misc_wq, &mddev->event_work);
8595 EXPORT_SYMBOL(md_reap_sync_thread);
8597 void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
8599 sysfs_notify_dirent_safe(rdev->sysfs_state);
8600 wait_event_timeout(rdev->blocked_wait,
8601 !test_bit(Blocked, &rdev->flags) &&
8602 !test_bit(BlockedBadBlocks, &rdev->flags),
8603 msecs_to_jiffies(5000));
8604 rdev_dec_pending(rdev, mddev);
8606 EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8608 void md_finish_reshape(struct mddev *mddev)
8610 /* called be personality module when reshape completes. */
8611 struct md_rdev *rdev;
8613 rdev_for_each(rdev, mddev) {
8614 if (rdev->data_offset > rdev->new_data_offset)
8615 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8616 else
8617 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8618 rdev->data_offset = rdev->new_data_offset;
8621 EXPORT_SYMBOL(md_finish_reshape);
8623 /* Bad block management */
8625 /* Returns 1 on success, 0 on failure */
8626 int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8627 int is_new)
8629 struct mddev *mddev = rdev->mddev;
8630 int rv;
8631 if (is_new)
8632 s += rdev->new_data_offset;
8633 else
8634 s += rdev->data_offset;
8635 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
8636 if (rv == 0) {
8637 /* Make sure they get written out promptly */
8638 if (test_bit(ExternalBbl, &rdev->flags))
8639 sysfs_notify(&rdev->kobj, NULL,
8640 "unacknowledged_bad_blocks");
8641 sysfs_notify_dirent_safe(rdev->sysfs_state);
8642 set_mask_bits(&mddev->sb_flags, 0,
8643 BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING));
8644 md_wakeup_thread(rdev->mddev->thread);
8645 return 1;
8646 } else
8647 return 0;
8649 EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8651 int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8652 int is_new)
8654 int rv;
8655 if (is_new)
8656 s += rdev->new_data_offset;
8657 else
8658 s += rdev->data_offset;
8659 rv = badblocks_clear(&rdev->badblocks, s, sectors);
8660 if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags))
8661 sysfs_notify(&rdev->kobj, NULL, "bad_blocks");
8662 return rv;
8664 EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8666 static int md_notify_reboot(struct notifier_block *this,
8667 unsigned long code, void *x)
8669 struct list_head *tmp;
8670 struct mddev *mddev;
8671 int need_delay = 0;
8673 for_each_mddev(mddev, tmp) {
8674 if (mddev_trylock(mddev)) {
8675 if (mddev->pers)
8676 __md_stop_writes(mddev);
8677 if (mddev->persistent)
8678 mddev->safemode = 2;
8679 mddev_unlock(mddev);
8681 need_delay = 1;
8684 * certain more exotic SCSI devices are known to be
8685 * volatile wrt too early system reboots. While the
8686 * right place to handle this issue is the given
8687 * driver, we do want to have a safe RAID driver ...
8689 if (need_delay)
8690 mdelay(1000*1);
8692 return NOTIFY_DONE;
8695 static struct notifier_block md_notifier = {
8696 .notifier_call = md_notify_reboot,
8697 .next = NULL,
8698 .priority = INT_MAX, /* before any real devices */
8701 static void md_geninit(void)
8703 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
8705 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
8708 static int __init md_init(void)
8710 int ret = -ENOMEM;
8712 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
8713 if (!md_wq)
8714 goto err_wq;
8716 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8717 if (!md_misc_wq)
8718 goto err_misc_wq;
8720 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8721 goto err_md;
8723 if ((ret = register_blkdev(0, "mdp")) < 0)
8724 goto err_mdp;
8725 mdp_major = ret;
8727 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
8728 md_probe, NULL, NULL);
8729 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
8730 md_probe, NULL, NULL);
8732 register_reboot_notifier(&md_notifier);
8733 raid_table_header = register_sysctl_table(raid_root_table);
8735 md_geninit();
8736 return 0;
8738 err_mdp:
8739 unregister_blkdev(MD_MAJOR, "md");
8740 err_md:
8741 destroy_workqueue(md_misc_wq);
8742 err_misc_wq:
8743 destroy_workqueue(md_wq);
8744 err_wq:
8745 return ret;
8748 static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
8750 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
8751 struct md_rdev *rdev2;
8752 int role, ret;
8753 char b[BDEVNAME_SIZE];
8755 /* Check for change of roles in the active devices */
8756 rdev_for_each(rdev2, mddev) {
8757 if (test_bit(Faulty, &rdev2->flags))
8758 continue;
8760 /* Check if the roles changed */
8761 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
8763 if (test_bit(Candidate, &rdev2->flags)) {
8764 if (role == 0xfffe) {
8765 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
8766 md_kick_rdev_from_array(rdev2);
8767 continue;
8769 else
8770 clear_bit(Candidate, &rdev2->flags);
8773 if (role != rdev2->raid_disk) {
8774 /* got activated */
8775 if (rdev2->raid_disk == -1 && role != 0xffff) {
8776 rdev2->saved_raid_disk = role;
8777 ret = remove_and_add_spares(mddev, rdev2);
8778 pr_info("Activated spare: %s\n",
8779 bdevname(rdev2->bdev,b));
8780 /* wakeup mddev->thread here, so array could
8781 * perform resync with the new activated disk */
8782 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8783 md_wakeup_thread(mddev->thread);
8786 /* device faulty
8787 * We just want to do the minimum to mark the disk
8788 * as faulty. The recovery is performed by the
8789 * one who initiated the error.
8791 if ((role == 0xfffe) || (role == 0xfffd)) {
8792 md_error(mddev, rdev2);
8793 clear_bit(Blocked, &rdev2->flags);
8798 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
8799 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
8801 /* Finally set the event to be up to date */
8802 mddev->events = le64_to_cpu(sb->events);
8805 static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
8807 int err;
8808 struct page *swapout = rdev->sb_page;
8809 struct mdp_superblock_1 *sb;
8811 /* Store the sb page of the rdev in the swapout temporary
8812 * variable in case we err in the future
8814 rdev->sb_page = NULL;
8815 err = alloc_disk_sb(rdev);
8816 if (err == 0) {
8817 ClearPageUptodate(rdev->sb_page);
8818 rdev->sb_loaded = 0;
8819 err = super_types[mddev->major_version].
8820 load_super(rdev, NULL, mddev->minor_version);
8822 if (err < 0) {
8823 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
8824 __func__, __LINE__, rdev->desc_nr, err);
8825 if (rdev->sb_page)
8826 put_page(rdev->sb_page);
8827 rdev->sb_page = swapout;
8828 rdev->sb_loaded = 1;
8829 return err;
8832 sb = page_address(rdev->sb_page);
8833 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
8834 * is not set
8837 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
8838 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
8840 /* The other node finished recovery, call spare_active to set
8841 * device In_sync and mddev->degraded
8843 if (rdev->recovery_offset == MaxSector &&
8844 !test_bit(In_sync, &rdev->flags) &&
8845 mddev->pers->spare_active(mddev))
8846 sysfs_notify(&mddev->kobj, NULL, "degraded");
8848 put_page(swapout);
8849 return 0;
8852 void md_reload_sb(struct mddev *mddev, int nr)
8854 struct md_rdev *rdev;
8855 int err;
8857 /* Find the rdev */
8858 rdev_for_each_rcu(rdev, mddev) {
8859 if (rdev->desc_nr == nr)
8860 break;
8863 if (!rdev || rdev->desc_nr != nr) {
8864 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
8865 return;
8868 err = read_rdev(mddev, rdev);
8869 if (err < 0)
8870 return;
8872 check_sb_changes(mddev, rdev);
8874 /* Read all rdev's to update recovery_offset */
8875 rdev_for_each_rcu(rdev, mddev)
8876 read_rdev(mddev, rdev);
8878 EXPORT_SYMBOL(md_reload_sb);
8880 #ifndef MODULE
8883 * Searches all registered partitions for autorun RAID arrays
8884 * at boot time.
8887 static DEFINE_MUTEX(detected_devices_mutex);
8888 static LIST_HEAD(all_detected_devices);
8889 struct detected_devices_node {
8890 struct list_head list;
8891 dev_t dev;
8894 void md_autodetect_dev(dev_t dev)
8896 struct detected_devices_node *node_detected_dev;
8898 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8899 if (node_detected_dev) {
8900 node_detected_dev->dev = dev;
8901 mutex_lock(&detected_devices_mutex);
8902 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8903 mutex_unlock(&detected_devices_mutex);
8907 static void autostart_arrays(int part)
8909 struct md_rdev *rdev;
8910 struct detected_devices_node *node_detected_dev;
8911 dev_t dev;
8912 int i_scanned, i_passed;
8914 i_scanned = 0;
8915 i_passed = 0;
8917 pr_info("md: Autodetecting RAID arrays.\n");
8919 mutex_lock(&detected_devices_mutex);
8920 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8921 i_scanned++;
8922 node_detected_dev = list_entry(all_detected_devices.next,
8923 struct detected_devices_node, list);
8924 list_del(&node_detected_dev->list);
8925 dev = node_detected_dev->dev;
8926 kfree(node_detected_dev);
8927 mutex_unlock(&detected_devices_mutex);
8928 rdev = md_import_device(dev,0, 90);
8929 mutex_lock(&detected_devices_mutex);
8930 if (IS_ERR(rdev))
8931 continue;
8933 if (test_bit(Faulty, &rdev->flags))
8934 continue;
8936 set_bit(AutoDetected, &rdev->flags);
8937 list_add(&rdev->same_set, &pending_raid_disks);
8938 i_passed++;
8940 mutex_unlock(&detected_devices_mutex);
8942 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed);
8944 autorun_devices(part);
8947 #endif /* !MODULE */
8949 static __exit void md_exit(void)
8951 struct mddev *mddev;
8952 struct list_head *tmp;
8953 int delay = 1;
8955 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
8956 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
8958 unregister_blkdev(MD_MAJOR,"md");
8959 unregister_blkdev(mdp_major, "mdp");
8960 unregister_reboot_notifier(&md_notifier);
8961 unregister_sysctl_table(raid_table_header);
8963 /* We cannot unload the modules while some process is
8964 * waiting for us in select() or poll() - wake them up
8966 md_unloading = 1;
8967 while (waitqueue_active(&md_event_waiters)) {
8968 /* not safe to leave yet */
8969 wake_up(&md_event_waiters);
8970 msleep(delay);
8971 delay += delay;
8973 remove_proc_entry("mdstat", NULL);
8975 for_each_mddev(mddev, tmp) {
8976 export_array(mddev);
8977 mddev->ctime = 0;
8978 mddev->hold_active = 0;
8980 * for_each_mddev() will call mddev_put() at the end of each
8981 * iteration. As the mddev is now fully clear, this will
8982 * schedule the mddev for destruction by a workqueue, and the
8983 * destroy_workqueue() below will wait for that to complete.
8986 destroy_workqueue(md_misc_wq);
8987 destroy_workqueue(md_wq);
8990 subsys_initcall(md_init);
8991 module_exit(md_exit)
8993 static int get_ro(char *buffer, struct kernel_param *kp)
8995 return sprintf(buffer, "%d", start_readonly);
8997 static int set_ro(const char *val, struct kernel_param *kp)
8999 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
9002 module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9003 module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
9004 module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
9006 MODULE_LICENSE("GPL");
9007 MODULE_DESCRIPTION("MD RAID framework");
9008 MODULE_ALIAS("md");
9009 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);