of: MSI: Simplify irqdomain lookup
[linux/fpc-iii.git] / drivers / md / md.c
blob61aacab424cf1860dbb5651f5a4fc6c9f7188027
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.
35 #include <linux/kthread.h>
36 #include <linux/blkdev.h>
37 #include <linux/sysctl.h>
38 #include <linux/seq_file.h>
39 #include <linux/fs.h>
40 #include <linux/poll.h>
41 #include <linux/ctype.h>
42 #include <linux/string.h>
43 #include <linux/hdreg.h>
44 #include <linux/proc_fs.h>
45 #include <linux/random.h>
46 #include <linux/module.h>
47 #include <linux/reboot.h>
48 #include <linux/file.h>
49 #include <linux/compat.h>
50 #include <linux/delay.h>
51 #include <linux/raid/md_p.h>
52 #include <linux/raid/md_u.h>
53 #include <linux/slab.h>
54 #include "md.h"
55 #include "bitmap.h"
56 #include "md-cluster.h"
58 #ifndef MODULE
59 static void autostart_arrays(int part);
60 #endif
62 /* pers_list is a list of registered personalities protected
63 * by pers_lock.
64 * pers_lock does extra service to protect accesses to
65 * mddev->thread when the mutex cannot be held.
67 static LIST_HEAD(pers_list);
68 static DEFINE_SPINLOCK(pers_lock);
70 struct md_cluster_operations *md_cluster_ops;
71 EXPORT_SYMBOL(md_cluster_ops);
72 struct module *md_cluster_mod;
73 EXPORT_SYMBOL(md_cluster_mod);
75 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
76 static struct workqueue_struct *md_wq;
77 static struct workqueue_struct *md_misc_wq;
79 static int remove_and_add_spares(struct mddev *mddev,
80 struct md_rdev *this);
81 static void mddev_detach(struct mddev *mddev);
84 * Default number of read corrections we'll attempt on an rdev
85 * before ejecting it from the array. We divide the read error
86 * count by 2 for every hour elapsed between read errors.
88 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
90 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
91 * is 1000 KB/sec, so the extra system load does not show up that much.
92 * Increase it if you want to have more _guaranteed_ speed. Note that
93 * the RAID driver will use the maximum available bandwidth if the IO
94 * subsystem is idle. There is also an 'absolute maximum' reconstruction
95 * speed limit - in case reconstruction slows down your system despite
96 * idle IO detection.
98 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
99 * or /sys/block/mdX/md/sync_speed_{min,max}
102 static int sysctl_speed_limit_min = 1000;
103 static int sysctl_speed_limit_max = 200000;
104 static inline int speed_min(struct mddev *mddev)
106 return mddev->sync_speed_min ?
107 mddev->sync_speed_min : sysctl_speed_limit_min;
110 static inline int speed_max(struct mddev *mddev)
112 return mddev->sync_speed_max ?
113 mddev->sync_speed_max : sysctl_speed_limit_max;
116 static struct ctl_table_header *raid_table_header;
118 static struct ctl_table raid_table[] = {
120 .procname = "speed_limit_min",
121 .data = &sysctl_speed_limit_min,
122 .maxlen = sizeof(int),
123 .mode = S_IRUGO|S_IWUSR,
124 .proc_handler = proc_dointvec,
127 .procname = "speed_limit_max",
128 .data = &sysctl_speed_limit_max,
129 .maxlen = sizeof(int),
130 .mode = S_IRUGO|S_IWUSR,
131 .proc_handler = proc_dointvec,
136 static struct ctl_table raid_dir_table[] = {
138 .procname = "raid",
139 .maxlen = 0,
140 .mode = S_IRUGO|S_IXUGO,
141 .child = raid_table,
146 static struct ctl_table raid_root_table[] = {
148 .procname = "dev",
149 .maxlen = 0,
150 .mode = 0555,
151 .child = raid_dir_table,
156 static const struct block_device_operations md_fops;
158 static int start_readonly;
160 /* bio_clone_mddev
161 * like bio_clone, but with a local bio set
164 struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
165 struct mddev *mddev)
167 struct bio *b;
169 if (!mddev || !mddev->bio_set)
170 return bio_alloc(gfp_mask, nr_iovecs);
172 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
173 if (!b)
174 return NULL;
175 return b;
177 EXPORT_SYMBOL_GPL(bio_alloc_mddev);
179 struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
180 struct mddev *mddev)
182 if (!mddev || !mddev->bio_set)
183 return bio_clone(bio, gfp_mask);
185 return bio_clone_bioset(bio, gfp_mask, mddev->bio_set);
187 EXPORT_SYMBOL_GPL(bio_clone_mddev);
190 * We have a system wide 'event count' that is incremented
191 * on any 'interesting' event, and readers of /proc/mdstat
192 * can use 'poll' or 'select' to find out when the event
193 * count increases.
195 * Events are:
196 * start array, stop array, error, add device, remove device,
197 * start build, activate spare
199 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
200 static atomic_t md_event_count;
201 void md_new_event(struct mddev *mddev)
203 atomic_inc(&md_event_count);
204 wake_up(&md_event_waiters);
206 EXPORT_SYMBOL_GPL(md_new_event);
208 /* Alternate version that can be called from interrupts
209 * when calling sysfs_notify isn't needed.
211 static void md_new_event_inintr(struct mddev *mddev)
213 atomic_inc(&md_event_count);
214 wake_up(&md_event_waiters);
218 * Enables to iterate over all existing md arrays
219 * all_mddevs_lock protects this list.
221 static LIST_HEAD(all_mddevs);
222 static DEFINE_SPINLOCK(all_mddevs_lock);
225 * iterates through all used mddevs in the system.
226 * We take care to grab the all_mddevs_lock whenever navigating
227 * the list, and to always hold a refcount when unlocked.
228 * Any code which breaks out of this loop while own
229 * a reference to the current mddev and must mddev_put it.
231 #define for_each_mddev(_mddev,_tmp) \
233 for (({ spin_lock(&all_mddevs_lock); \
234 _tmp = all_mddevs.next; \
235 _mddev = NULL;}); \
236 ({ if (_tmp != &all_mddevs) \
237 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
238 spin_unlock(&all_mddevs_lock); \
239 if (_mddev) mddev_put(_mddev); \
240 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
241 _tmp != &all_mddevs;}); \
242 ({ spin_lock(&all_mddevs_lock); \
243 _tmp = _tmp->next;}) \
246 /* Rather than calling directly into the personality make_request function,
247 * IO requests come here first so that we can check if the device is
248 * being suspended pending a reconfiguration.
249 * We hold a refcount over the call to ->make_request. By the time that
250 * call has finished, the bio has been linked into some internal structure
251 * and so is visible to ->quiesce(), so we don't need the refcount any more.
253 static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio)
255 const int rw = bio_data_dir(bio);
256 struct mddev *mddev = q->queuedata;
257 unsigned int sectors;
258 int cpu;
260 blk_queue_split(q, &bio, q->bio_split);
262 if (mddev == NULL || mddev->pers == NULL
263 || !mddev->ready) {
264 bio_io_error(bio);
265 return BLK_QC_T_NONE;
267 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
268 if (bio_sectors(bio) != 0)
269 bio->bi_error = -EROFS;
270 bio_endio(bio);
271 return BLK_QC_T_NONE;
273 smp_rmb(); /* Ensure implications of 'active' are visible */
274 rcu_read_lock();
275 if (mddev->suspended) {
276 DEFINE_WAIT(__wait);
277 for (;;) {
278 prepare_to_wait(&mddev->sb_wait, &__wait,
279 TASK_UNINTERRUPTIBLE);
280 if (!mddev->suspended)
281 break;
282 rcu_read_unlock();
283 schedule();
284 rcu_read_lock();
286 finish_wait(&mddev->sb_wait, &__wait);
288 atomic_inc(&mddev->active_io);
289 rcu_read_unlock();
292 * save the sectors now since our bio can
293 * go away inside make_request
295 sectors = bio_sectors(bio);
296 mddev->pers->make_request(mddev, bio);
298 cpu = part_stat_lock();
299 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
300 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
301 part_stat_unlock();
303 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
304 wake_up(&mddev->sb_wait);
306 return BLK_QC_T_NONE;
309 /* mddev_suspend makes sure no new requests are submitted
310 * to the device, and that any requests that have been submitted
311 * are completely handled.
312 * Once mddev_detach() is called and completes, the module will be
313 * completely unused.
315 void mddev_suspend(struct mddev *mddev)
317 if (mddev->suspended++)
318 return;
319 synchronize_rcu();
320 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
321 mddev->pers->quiesce(mddev, 1);
323 del_timer_sync(&mddev->safemode_timer);
325 EXPORT_SYMBOL_GPL(mddev_suspend);
327 void mddev_resume(struct mddev *mddev)
329 if (--mddev->suspended)
330 return;
331 wake_up(&mddev->sb_wait);
332 mddev->pers->quiesce(mddev, 0);
334 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
335 md_wakeup_thread(mddev->thread);
336 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
338 EXPORT_SYMBOL_GPL(mddev_resume);
340 int mddev_congested(struct mddev *mddev, int bits)
342 struct md_personality *pers = mddev->pers;
343 int ret = 0;
345 rcu_read_lock();
346 if (mddev->suspended)
347 ret = 1;
348 else if (pers && pers->congested)
349 ret = pers->congested(mddev, bits);
350 rcu_read_unlock();
351 return ret;
353 EXPORT_SYMBOL_GPL(mddev_congested);
354 static int md_congested(void *data, int bits)
356 struct mddev *mddev = data;
357 return mddev_congested(mddev, bits);
361 * Generic flush handling for md
364 static void md_end_flush(struct bio *bio)
366 struct md_rdev *rdev = bio->bi_private;
367 struct mddev *mddev = rdev->mddev;
369 rdev_dec_pending(rdev, mddev);
371 if (atomic_dec_and_test(&mddev->flush_pending)) {
372 /* The pre-request flush has finished */
373 queue_work(md_wq, &mddev->flush_work);
375 bio_put(bio);
378 static void md_submit_flush_data(struct work_struct *ws);
380 static void submit_flushes(struct work_struct *ws)
382 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
383 struct md_rdev *rdev;
385 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
386 atomic_set(&mddev->flush_pending, 1);
387 rcu_read_lock();
388 rdev_for_each_rcu(rdev, mddev)
389 if (rdev->raid_disk >= 0 &&
390 !test_bit(Faulty, &rdev->flags)) {
391 /* Take two references, one is dropped
392 * when request finishes, one after
393 * we reclaim rcu_read_lock
395 struct bio *bi;
396 atomic_inc(&rdev->nr_pending);
397 atomic_inc(&rdev->nr_pending);
398 rcu_read_unlock();
399 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
400 bi->bi_end_io = md_end_flush;
401 bi->bi_private = rdev;
402 bi->bi_bdev = rdev->bdev;
403 atomic_inc(&mddev->flush_pending);
404 submit_bio(WRITE_FLUSH, bi);
405 rcu_read_lock();
406 rdev_dec_pending(rdev, mddev);
408 rcu_read_unlock();
409 if (atomic_dec_and_test(&mddev->flush_pending))
410 queue_work(md_wq, &mddev->flush_work);
413 static void md_submit_flush_data(struct work_struct *ws)
415 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
416 struct bio *bio = mddev->flush_bio;
418 if (bio->bi_iter.bi_size == 0)
419 /* an empty barrier - all done */
420 bio_endio(bio);
421 else {
422 bio->bi_rw &= ~REQ_FLUSH;
423 mddev->pers->make_request(mddev, bio);
426 mddev->flush_bio = NULL;
427 wake_up(&mddev->sb_wait);
430 void md_flush_request(struct mddev *mddev, struct bio *bio)
432 spin_lock_irq(&mddev->lock);
433 wait_event_lock_irq(mddev->sb_wait,
434 !mddev->flush_bio,
435 mddev->lock);
436 mddev->flush_bio = bio;
437 spin_unlock_irq(&mddev->lock);
439 INIT_WORK(&mddev->flush_work, submit_flushes);
440 queue_work(md_wq, &mddev->flush_work);
442 EXPORT_SYMBOL(md_flush_request);
444 void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
446 struct mddev *mddev = cb->data;
447 md_wakeup_thread(mddev->thread);
448 kfree(cb);
450 EXPORT_SYMBOL(md_unplug);
452 static inline struct mddev *mddev_get(struct mddev *mddev)
454 atomic_inc(&mddev->active);
455 return mddev;
458 static void mddev_delayed_delete(struct work_struct *ws);
460 static void mddev_put(struct mddev *mddev)
462 struct bio_set *bs = NULL;
464 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
465 return;
466 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
467 mddev->ctime == 0 && !mddev->hold_active) {
468 /* Array is not configured at all, and not held active,
469 * so destroy it */
470 list_del_init(&mddev->all_mddevs);
471 bs = mddev->bio_set;
472 mddev->bio_set = NULL;
473 if (mddev->gendisk) {
474 /* We did a probe so need to clean up. Call
475 * queue_work inside the spinlock so that
476 * flush_workqueue() after mddev_find will
477 * succeed in waiting for the work to be done.
479 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
480 queue_work(md_misc_wq, &mddev->del_work);
481 } else
482 kfree(mddev);
484 spin_unlock(&all_mddevs_lock);
485 if (bs)
486 bioset_free(bs);
489 static void md_safemode_timeout(unsigned long data);
491 void mddev_init(struct mddev *mddev)
493 mutex_init(&mddev->open_mutex);
494 mutex_init(&mddev->reconfig_mutex);
495 mutex_init(&mddev->bitmap_info.mutex);
496 INIT_LIST_HEAD(&mddev->disks);
497 INIT_LIST_HEAD(&mddev->all_mddevs);
498 setup_timer(&mddev->safemode_timer, md_safemode_timeout,
499 (unsigned long) mddev);
500 atomic_set(&mddev->active, 1);
501 atomic_set(&mddev->openers, 0);
502 atomic_set(&mddev->active_io, 0);
503 spin_lock_init(&mddev->lock);
504 atomic_set(&mddev->flush_pending, 0);
505 init_waitqueue_head(&mddev->sb_wait);
506 init_waitqueue_head(&mddev->recovery_wait);
507 mddev->reshape_position = MaxSector;
508 mddev->reshape_backwards = 0;
509 mddev->last_sync_action = "none";
510 mddev->resync_min = 0;
511 mddev->resync_max = MaxSector;
512 mddev->level = LEVEL_NONE;
514 EXPORT_SYMBOL_GPL(mddev_init);
516 static struct mddev *mddev_find(dev_t unit)
518 struct mddev *mddev, *new = NULL;
520 if (unit && MAJOR(unit) != MD_MAJOR)
521 unit &= ~((1<<MdpMinorShift)-1);
523 retry:
524 spin_lock(&all_mddevs_lock);
526 if (unit) {
527 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
528 if (mddev->unit == unit) {
529 mddev_get(mddev);
530 spin_unlock(&all_mddevs_lock);
531 kfree(new);
532 return mddev;
535 if (new) {
536 list_add(&new->all_mddevs, &all_mddevs);
537 spin_unlock(&all_mddevs_lock);
538 new->hold_active = UNTIL_IOCTL;
539 return new;
541 } else if (new) {
542 /* find an unused unit number */
543 static int next_minor = 512;
544 int start = next_minor;
545 int is_free = 0;
546 int dev = 0;
547 while (!is_free) {
548 dev = MKDEV(MD_MAJOR, next_minor);
549 next_minor++;
550 if (next_minor > MINORMASK)
551 next_minor = 0;
552 if (next_minor == start) {
553 /* Oh dear, all in use. */
554 spin_unlock(&all_mddevs_lock);
555 kfree(new);
556 return NULL;
559 is_free = 1;
560 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
561 if (mddev->unit == dev) {
562 is_free = 0;
563 break;
566 new->unit = dev;
567 new->md_minor = MINOR(dev);
568 new->hold_active = UNTIL_STOP;
569 list_add(&new->all_mddevs, &all_mddevs);
570 spin_unlock(&all_mddevs_lock);
571 return new;
573 spin_unlock(&all_mddevs_lock);
575 new = kzalloc(sizeof(*new), GFP_KERNEL);
576 if (!new)
577 return NULL;
579 new->unit = unit;
580 if (MAJOR(unit) == MD_MAJOR)
581 new->md_minor = MINOR(unit);
582 else
583 new->md_minor = MINOR(unit) >> MdpMinorShift;
585 mddev_init(new);
587 goto retry;
590 static struct attribute_group md_redundancy_group;
592 void mddev_unlock(struct mddev *mddev)
594 if (mddev->to_remove) {
595 /* These cannot be removed under reconfig_mutex as
596 * an access to the files will try to take reconfig_mutex
597 * while holding the file unremovable, which leads to
598 * a deadlock.
599 * So hold set sysfs_active while the remove in happeing,
600 * and anything else which might set ->to_remove or my
601 * otherwise change the sysfs namespace will fail with
602 * -EBUSY if sysfs_active is still set.
603 * We set sysfs_active under reconfig_mutex and elsewhere
604 * test it under the same mutex to ensure its correct value
605 * is seen.
607 struct attribute_group *to_remove = mddev->to_remove;
608 mddev->to_remove = NULL;
609 mddev->sysfs_active = 1;
610 mutex_unlock(&mddev->reconfig_mutex);
612 if (mddev->kobj.sd) {
613 if (to_remove != &md_redundancy_group)
614 sysfs_remove_group(&mddev->kobj, to_remove);
615 if (mddev->pers == NULL ||
616 mddev->pers->sync_request == NULL) {
617 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
618 if (mddev->sysfs_action)
619 sysfs_put(mddev->sysfs_action);
620 mddev->sysfs_action = NULL;
623 mddev->sysfs_active = 0;
624 } else
625 mutex_unlock(&mddev->reconfig_mutex);
627 /* As we've dropped the mutex we need a spinlock to
628 * make sure the thread doesn't disappear
630 spin_lock(&pers_lock);
631 md_wakeup_thread(mddev->thread);
632 spin_unlock(&pers_lock);
634 EXPORT_SYMBOL_GPL(mddev_unlock);
636 struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
638 struct md_rdev *rdev;
640 rdev_for_each_rcu(rdev, mddev)
641 if (rdev->desc_nr == nr)
642 return rdev;
644 return NULL;
646 EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
648 static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
650 struct md_rdev *rdev;
652 rdev_for_each(rdev, mddev)
653 if (rdev->bdev->bd_dev == dev)
654 return rdev;
656 return NULL;
659 static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
661 struct md_rdev *rdev;
663 rdev_for_each_rcu(rdev, mddev)
664 if (rdev->bdev->bd_dev == dev)
665 return rdev;
667 return NULL;
670 static struct md_personality *find_pers(int level, char *clevel)
672 struct md_personality *pers;
673 list_for_each_entry(pers, &pers_list, list) {
674 if (level != LEVEL_NONE && pers->level == level)
675 return pers;
676 if (strcmp(pers->name, clevel)==0)
677 return pers;
679 return NULL;
682 /* return the offset of the super block in 512byte sectors */
683 static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
685 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
686 return MD_NEW_SIZE_SECTORS(num_sectors);
689 static int alloc_disk_sb(struct md_rdev *rdev)
691 rdev->sb_page = alloc_page(GFP_KERNEL);
692 if (!rdev->sb_page) {
693 printk(KERN_ALERT "md: out of memory.\n");
694 return -ENOMEM;
697 return 0;
700 void md_rdev_clear(struct md_rdev *rdev)
702 if (rdev->sb_page) {
703 put_page(rdev->sb_page);
704 rdev->sb_loaded = 0;
705 rdev->sb_page = NULL;
706 rdev->sb_start = 0;
707 rdev->sectors = 0;
709 if (rdev->bb_page) {
710 put_page(rdev->bb_page);
711 rdev->bb_page = NULL;
713 kfree(rdev->badblocks.page);
714 rdev->badblocks.page = NULL;
716 EXPORT_SYMBOL_GPL(md_rdev_clear);
718 static void super_written(struct bio *bio)
720 struct md_rdev *rdev = bio->bi_private;
721 struct mddev *mddev = rdev->mddev;
723 if (bio->bi_error) {
724 printk("md: super_written gets error=%d\n", bio->bi_error);
725 md_error(mddev, rdev);
728 if (atomic_dec_and_test(&mddev->pending_writes))
729 wake_up(&mddev->sb_wait);
730 bio_put(bio);
733 void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
734 sector_t sector, int size, struct page *page)
736 /* write first size bytes of page to sector of rdev
737 * Increment mddev->pending_writes before returning
738 * and decrement it on completion, waking up sb_wait
739 * if zero is reached.
740 * If an error occurred, call md_error
742 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
744 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
745 bio->bi_iter.bi_sector = sector;
746 bio_add_page(bio, page, size, 0);
747 bio->bi_private = rdev;
748 bio->bi_end_io = super_written;
750 atomic_inc(&mddev->pending_writes);
751 submit_bio(WRITE_FLUSH_FUA, bio);
754 void md_super_wait(struct mddev *mddev)
756 /* wait for all superblock writes that were scheduled to complete */
757 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
760 int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
761 struct page *page, int rw, bool metadata_op)
763 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
764 int ret;
766 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
767 rdev->meta_bdev : rdev->bdev;
768 if (metadata_op)
769 bio->bi_iter.bi_sector = sector + rdev->sb_start;
770 else if (rdev->mddev->reshape_position != MaxSector &&
771 (rdev->mddev->reshape_backwards ==
772 (sector >= rdev->mddev->reshape_position)))
773 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
774 else
775 bio->bi_iter.bi_sector = sector + rdev->data_offset;
776 bio_add_page(bio, page, size, 0);
777 submit_bio_wait(rw, bio);
779 ret = !bio->bi_error;
780 bio_put(bio);
781 return ret;
783 EXPORT_SYMBOL_GPL(sync_page_io);
785 static int read_disk_sb(struct md_rdev *rdev, int size)
787 char b[BDEVNAME_SIZE];
789 if (rdev->sb_loaded)
790 return 0;
792 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
793 goto fail;
794 rdev->sb_loaded = 1;
795 return 0;
797 fail:
798 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
799 bdevname(rdev->bdev,b));
800 return -EINVAL;
803 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
805 return sb1->set_uuid0 == sb2->set_uuid0 &&
806 sb1->set_uuid1 == sb2->set_uuid1 &&
807 sb1->set_uuid2 == sb2->set_uuid2 &&
808 sb1->set_uuid3 == sb2->set_uuid3;
811 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
813 int ret;
814 mdp_super_t *tmp1, *tmp2;
816 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
817 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
819 if (!tmp1 || !tmp2) {
820 ret = 0;
821 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
822 goto abort;
825 *tmp1 = *sb1;
826 *tmp2 = *sb2;
829 * nr_disks is not constant
831 tmp1->nr_disks = 0;
832 tmp2->nr_disks = 0;
834 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
835 abort:
836 kfree(tmp1);
837 kfree(tmp2);
838 return ret;
841 static u32 md_csum_fold(u32 csum)
843 csum = (csum & 0xffff) + (csum >> 16);
844 return (csum & 0xffff) + (csum >> 16);
847 static unsigned int calc_sb_csum(mdp_super_t *sb)
849 u64 newcsum = 0;
850 u32 *sb32 = (u32*)sb;
851 int i;
852 unsigned int disk_csum, csum;
854 disk_csum = sb->sb_csum;
855 sb->sb_csum = 0;
857 for (i = 0; i < MD_SB_BYTES/4 ; i++)
858 newcsum += sb32[i];
859 csum = (newcsum & 0xffffffff) + (newcsum>>32);
861 #ifdef CONFIG_ALPHA
862 /* This used to use csum_partial, which was wrong for several
863 * reasons including that different results are returned on
864 * different architectures. It isn't critical that we get exactly
865 * the same return value as before (we always csum_fold before
866 * testing, and that removes any differences). However as we
867 * know that csum_partial always returned a 16bit value on
868 * alphas, do a fold to maximise conformity to previous behaviour.
870 sb->sb_csum = md_csum_fold(disk_csum);
871 #else
872 sb->sb_csum = disk_csum;
873 #endif
874 return csum;
878 * Handle superblock details.
879 * We want to be able to handle multiple superblock formats
880 * so we have a common interface to them all, and an array of
881 * different handlers.
882 * We rely on user-space to write the initial superblock, and support
883 * reading and updating of superblocks.
884 * Interface methods are:
885 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
886 * loads and validates a superblock on dev.
887 * if refdev != NULL, compare superblocks on both devices
888 * Return:
889 * 0 - dev has a superblock that is compatible with refdev
890 * 1 - dev has a superblock that is compatible and newer than refdev
891 * so dev should be used as the refdev in future
892 * -EINVAL superblock incompatible or invalid
893 * -othererror e.g. -EIO
895 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
896 * Verify that dev is acceptable into mddev.
897 * The first time, mddev->raid_disks will be 0, and data from
898 * dev should be merged in. Subsequent calls check that dev
899 * is new enough. Return 0 or -EINVAL
901 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
902 * Update the superblock for rdev with data in mddev
903 * This does not write to disc.
907 struct super_type {
908 char *name;
909 struct module *owner;
910 int (*load_super)(struct md_rdev *rdev,
911 struct md_rdev *refdev,
912 int minor_version);
913 int (*validate_super)(struct mddev *mddev,
914 struct md_rdev *rdev);
915 void (*sync_super)(struct mddev *mddev,
916 struct md_rdev *rdev);
917 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
918 sector_t num_sectors);
919 int (*allow_new_offset)(struct md_rdev *rdev,
920 unsigned long long new_offset);
924 * Check that the given mddev has no bitmap.
926 * This function is called from the run method of all personalities that do not
927 * support bitmaps. It prints an error message and returns non-zero if mddev
928 * has a bitmap. Otherwise, it returns 0.
931 int md_check_no_bitmap(struct mddev *mddev)
933 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
934 return 0;
935 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
936 mdname(mddev), mddev->pers->name);
937 return 1;
939 EXPORT_SYMBOL(md_check_no_bitmap);
942 * load_super for 0.90.0
944 static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
946 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
947 mdp_super_t *sb;
948 int ret;
951 * Calculate the position of the superblock (512byte sectors),
952 * it's at the end of the disk.
954 * It also happens to be a multiple of 4Kb.
956 rdev->sb_start = calc_dev_sboffset(rdev);
958 ret = read_disk_sb(rdev, MD_SB_BYTES);
959 if (ret) return ret;
961 ret = -EINVAL;
963 bdevname(rdev->bdev, b);
964 sb = page_address(rdev->sb_page);
966 if (sb->md_magic != MD_SB_MAGIC) {
967 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
969 goto abort;
972 if (sb->major_version != 0 ||
973 sb->minor_version < 90 ||
974 sb->minor_version > 91) {
975 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
976 sb->major_version, sb->minor_version,
978 goto abort;
981 if (sb->raid_disks <= 0)
982 goto abort;
984 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
985 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
987 goto abort;
990 rdev->preferred_minor = sb->md_minor;
991 rdev->data_offset = 0;
992 rdev->new_data_offset = 0;
993 rdev->sb_size = MD_SB_BYTES;
994 rdev->badblocks.shift = -1;
996 if (sb->level == LEVEL_MULTIPATH)
997 rdev->desc_nr = -1;
998 else
999 rdev->desc_nr = sb->this_disk.number;
1001 if (!refdev) {
1002 ret = 1;
1003 } else {
1004 __u64 ev1, ev2;
1005 mdp_super_t *refsb = page_address(refdev->sb_page);
1006 if (!uuid_equal(refsb, sb)) {
1007 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1008 b, bdevname(refdev->bdev,b2));
1009 goto abort;
1011 if (!sb_equal(refsb, sb)) {
1012 printk(KERN_WARNING "md: %s has same UUID"
1013 " but different superblock to %s\n",
1014 b, bdevname(refdev->bdev, b2));
1015 goto abort;
1017 ev1 = md_event(sb);
1018 ev2 = md_event(refsb);
1019 if (ev1 > ev2)
1020 ret = 1;
1021 else
1022 ret = 0;
1024 rdev->sectors = rdev->sb_start;
1025 /* Limit to 4TB as metadata cannot record more than that.
1026 * (not needed for Linear and RAID0 as metadata doesn't
1027 * record this size)
1029 if (rdev->sectors >= (2ULL << 32) && sb->level >= 1)
1030 rdev->sectors = (2ULL << 32) - 2;
1032 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
1033 /* "this cannot possibly happen" ... */
1034 ret = -EINVAL;
1036 abort:
1037 return ret;
1041 * validate_super for 0.90.0
1043 static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
1045 mdp_disk_t *desc;
1046 mdp_super_t *sb = page_address(rdev->sb_page);
1047 __u64 ev1 = md_event(sb);
1049 rdev->raid_disk = -1;
1050 clear_bit(Faulty, &rdev->flags);
1051 clear_bit(In_sync, &rdev->flags);
1052 clear_bit(Bitmap_sync, &rdev->flags);
1053 clear_bit(WriteMostly, &rdev->flags);
1055 if (mddev->raid_disks == 0) {
1056 mddev->major_version = 0;
1057 mddev->minor_version = sb->minor_version;
1058 mddev->patch_version = sb->patch_version;
1059 mddev->external = 0;
1060 mddev->chunk_sectors = sb->chunk_size >> 9;
1061 mddev->ctime = sb->ctime;
1062 mddev->utime = sb->utime;
1063 mddev->level = sb->level;
1064 mddev->clevel[0] = 0;
1065 mddev->layout = sb->layout;
1066 mddev->raid_disks = sb->raid_disks;
1067 mddev->dev_sectors = ((sector_t)sb->size) * 2;
1068 mddev->events = ev1;
1069 mddev->bitmap_info.offset = 0;
1070 mddev->bitmap_info.space = 0;
1071 /* bitmap can use 60 K after the 4K superblocks */
1072 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
1073 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
1074 mddev->reshape_backwards = 0;
1076 if (mddev->minor_version >= 91) {
1077 mddev->reshape_position = sb->reshape_position;
1078 mddev->delta_disks = sb->delta_disks;
1079 mddev->new_level = sb->new_level;
1080 mddev->new_layout = sb->new_layout;
1081 mddev->new_chunk_sectors = sb->new_chunk >> 9;
1082 if (mddev->delta_disks < 0)
1083 mddev->reshape_backwards = 1;
1084 } else {
1085 mddev->reshape_position = MaxSector;
1086 mddev->delta_disks = 0;
1087 mddev->new_level = mddev->level;
1088 mddev->new_layout = mddev->layout;
1089 mddev->new_chunk_sectors = mddev->chunk_sectors;
1092 if (sb->state & (1<<MD_SB_CLEAN))
1093 mddev->recovery_cp = MaxSector;
1094 else {
1095 if (sb->events_hi == sb->cp_events_hi &&
1096 sb->events_lo == sb->cp_events_lo) {
1097 mddev->recovery_cp = sb->recovery_cp;
1098 } else
1099 mddev->recovery_cp = 0;
1102 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1103 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1104 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1105 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1107 mddev->max_disks = MD_SB_DISKS;
1109 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
1110 mddev->bitmap_info.file == NULL) {
1111 mddev->bitmap_info.offset =
1112 mddev->bitmap_info.default_offset;
1113 mddev->bitmap_info.space =
1114 mddev->bitmap_info.default_space;
1117 } else if (mddev->pers == NULL) {
1118 /* Insist on good event counter while assembling, except
1119 * for spares (which don't need an event count) */
1120 ++ev1;
1121 if (sb->disks[rdev->desc_nr].state & (
1122 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1123 if (ev1 < mddev->events)
1124 return -EINVAL;
1125 } else if (mddev->bitmap) {
1126 /* if adding to array with a bitmap, then we can accept an
1127 * older device ... but not too old.
1129 if (ev1 < mddev->bitmap->events_cleared)
1130 return 0;
1131 if (ev1 < mddev->events)
1132 set_bit(Bitmap_sync, &rdev->flags);
1133 } else {
1134 if (ev1 < mddev->events)
1135 /* just a hot-add of a new device, leave raid_disk at -1 */
1136 return 0;
1139 if (mddev->level != LEVEL_MULTIPATH) {
1140 desc = sb->disks + rdev->desc_nr;
1142 if (desc->state & (1<<MD_DISK_FAULTY))
1143 set_bit(Faulty, &rdev->flags);
1144 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1145 desc->raid_disk < mddev->raid_disks */) {
1146 set_bit(In_sync, &rdev->flags);
1147 rdev->raid_disk = desc->raid_disk;
1148 rdev->saved_raid_disk = desc->raid_disk;
1149 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1150 /* active but not in sync implies recovery up to
1151 * reshape position. We don't know exactly where
1152 * that is, so set to zero for now */
1153 if (mddev->minor_version >= 91) {
1154 rdev->recovery_offset = 0;
1155 rdev->raid_disk = desc->raid_disk;
1158 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1159 set_bit(WriteMostly, &rdev->flags);
1160 } else /* MULTIPATH are always insync */
1161 set_bit(In_sync, &rdev->flags);
1162 return 0;
1166 * sync_super for 0.90.0
1168 static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
1170 mdp_super_t *sb;
1171 struct md_rdev *rdev2;
1172 int next_spare = mddev->raid_disks;
1174 /* make rdev->sb match mddev data..
1176 * 1/ zero out disks
1177 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1178 * 3/ any empty disks < next_spare become removed
1180 * disks[0] gets initialised to REMOVED because
1181 * we cannot be sure from other fields if it has
1182 * been initialised or not.
1184 int i;
1185 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1187 rdev->sb_size = MD_SB_BYTES;
1189 sb = page_address(rdev->sb_page);
1191 memset(sb, 0, sizeof(*sb));
1193 sb->md_magic = MD_SB_MAGIC;
1194 sb->major_version = mddev->major_version;
1195 sb->patch_version = mddev->patch_version;
1196 sb->gvalid_words = 0; /* ignored */
1197 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1198 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1199 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1200 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1202 sb->ctime = mddev->ctime;
1203 sb->level = mddev->level;
1204 sb->size = mddev->dev_sectors / 2;
1205 sb->raid_disks = mddev->raid_disks;
1206 sb->md_minor = mddev->md_minor;
1207 sb->not_persistent = 0;
1208 sb->utime = mddev->utime;
1209 sb->state = 0;
1210 sb->events_hi = (mddev->events>>32);
1211 sb->events_lo = (u32)mddev->events;
1213 if (mddev->reshape_position == MaxSector)
1214 sb->minor_version = 90;
1215 else {
1216 sb->minor_version = 91;
1217 sb->reshape_position = mddev->reshape_position;
1218 sb->new_level = mddev->new_level;
1219 sb->delta_disks = mddev->delta_disks;
1220 sb->new_layout = mddev->new_layout;
1221 sb->new_chunk = mddev->new_chunk_sectors << 9;
1223 mddev->minor_version = sb->minor_version;
1224 if (mddev->in_sync)
1226 sb->recovery_cp = mddev->recovery_cp;
1227 sb->cp_events_hi = (mddev->events>>32);
1228 sb->cp_events_lo = (u32)mddev->events;
1229 if (mddev->recovery_cp == MaxSector)
1230 sb->state = (1<< MD_SB_CLEAN);
1231 } else
1232 sb->recovery_cp = 0;
1234 sb->layout = mddev->layout;
1235 sb->chunk_size = mddev->chunk_sectors << 9;
1237 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
1238 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1240 sb->disks[0].state = (1<<MD_DISK_REMOVED);
1241 rdev_for_each(rdev2, mddev) {
1242 mdp_disk_t *d;
1243 int desc_nr;
1244 int is_active = test_bit(In_sync, &rdev2->flags);
1246 if (rdev2->raid_disk >= 0 &&
1247 sb->minor_version >= 91)
1248 /* we have nowhere to store the recovery_offset,
1249 * but if it is not below the reshape_position,
1250 * we can piggy-back on that.
1252 is_active = 1;
1253 if (rdev2->raid_disk < 0 ||
1254 test_bit(Faulty, &rdev2->flags))
1255 is_active = 0;
1256 if (is_active)
1257 desc_nr = rdev2->raid_disk;
1258 else
1259 desc_nr = next_spare++;
1260 rdev2->desc_nr = desc_nr;
1261 d = &sb->disks[rdev2->desc_nr];
1262 nr_disks++;
1263 d->number = rdev2->desc_nr;
1264 d->major = MAJOR(rdev2->bdev->bd_dev);
1265 d->minor = MINOR(rdev2->bdev->bd_dev);
1266 if (is_active)
1267 d->raid_disk = rdev2->raid_disk;
1268 else
1269 d->raid_disk = rdev2->desc_nr; /* compatibility */
1270 if (test_bit(Faulty, &rdev2->flags))
1271 d->state = (1<<MD_DISK_FAULTY);
1272 else if (is_active) {
1273 d->state = (1<<MD_DISK_ACTIVE);
1274 if (test_bit(In_sync, &rdev2->flags))
1275 d->state |= (1<<MD_DISK_SYNC);
1276 active++;
1277 working++;
1278 } else {
1279 d->state = 0;
1280 spare++;
1281 working++;
1283 if (test_bit(WriteMostly, &rdev2->flags))
1284 d->state |= (1<<MD_DISK_WRITEMOSTLY);
1286 /* now set the "removed" and "faulty" bits on any missing devices */
1287 for (i=0 ; i < mddev->raid_disks ; i++) {
1288 mdp_disk_t *d = &sb->disks[i];
1289 if (d->state == 0 && d->number == 0) {
1290 d->number = i;
1291 d->raid_disk = i;
1292 d->state = (1<<MD_DISK_REMOVED);
1293 d->state |= (1<<MD_DISK_FAULTY);
1294 failed++;
1297 sb->nr_disks = nr_disks;
1298 sb->active_disks = active;
1299 sb->working_disks = working;
1300 sb->failed_disks = failed;
1301 sb->spare_disks = spare;
1303 sb->this_disk = sb->disks[rdev->desc_nr];
1304 sb->sb_csum = calc_sb_csum(sb);
1308 * rdev_size_change for 0.90.0
1310 static unsigned long long
1311 super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
1313 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1314 return 0; /* component must fit device */
1315 if (rdev->mddev->bitmap_info.offset)
1316 return 0; /* can't move bitmap */
1317 rdev->sb_start = calc_dev_sboffset(rdev);
1318 if (!num_sectors || num_sectors > rdev->sb_start)
1319 num_sectors = rdev->sb_start;
1320 /* Limit to 4TB as metadata cannot record more than that.
1321 * 4TB == 2^32 KB, or 2*2^32 sectors.
1323 if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
1324 num_sectors = (2ULL << 32) - 2;
1325 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1326 rdev->sb_page);
1327 md_super_wait(rdev->mddev);
1328 return num_sectors;
1331 static int
1332 super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1334 /* non-zero offset changes not possible with v0.90 */
1335 return new_offset == 0;
1339 * version 1 superblock
1342 static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
1344 __le32 disk_csum;
1345 u32 csum;
1346 unsigned long long newcsum;
1347 int size = 256 + le32_to_cpu(sb->max_dev)*2;
1348 __le32 *isuper = (__le32*)sb;
1350 disk_csum = sb->sb_csum;
1351 sb->sb_csum = 0;
1352 newcsum = 0;
1353 for (; size >= 4; size -= 4)
1354 newcsum += le32_to_cpu(*isuper++);
1356 if (size == 2)
1357 newcsum += le16_to_cpu(*(__le16*) isuper);
1359 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1360 sb->sb_csum = disk_csum;
1361 return cpu_to_le32(csum);
1364 static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1365 int acknowledged);
1366 static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
1368 struct mdp_superblock_1 *sb;
1369 int ret;
1370 sector_t sb_start;
1371 sector_t sectors;
1372 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1373 int bmask;
1376 * Calculate the position of the superblock in 512byte sectors.
1377 * It is always aligned to a 4K boundary and
1378 * depeding on minor_version, it can be:
1379 * 0: At least 8K, but less than 12K, from end of device
1380 * 1: At start of device
1381 * 2: 4K from start of device.
1383 switch(minor_version) {
1384 case 0:
1385 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
1386 sb_start -= 8*2;
1387 sb_start &= ~(sector_t)(4*2-1);
1388 break;
1389 case 1:
1390 sb_start = 0;
1391 break;
1392 case 2:
1393 sb_start = 8;
1394 break;
1395 default:
1396 return -EINVAL;
1398 rdev->sb_start = sb_start;
1400 /* superblock is rarely larger than 1K, but it can be larger,
1401 * and it is safe to read 4k, so we do that
1403 ret = read_disk_sb(rdev, 4096);
1404 if (ret) return ret;
1406 sb = page_address(rdev->sb_page);
1408 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1409 sb->major_version != cpu_to_le32(1) ||
1410 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1411 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1412 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1413 return -EINVAL;
1415 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1416 printk("md: invalid superblock checksum on %s\n",
1417 bdevname(rdev->bdev,b));
1418 return -EINVAL;
1420 if (le64_to_cpu(sb->data_size) < 10) {
1421 printk("md: data_size too small on %s\n",
1422 bdevname(rdev->bdev,b));
1423 return -EINVAL;
1425 if (sb->pad0 ||
1426 sb->pad3[0] ||
1427 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1428 /* Some padding is non-zero, might be a new feature */
1429 return -EINVAL;
1431 rdev->preferred_minor = 0xffff;
1432 rdev->data_offset = le64_to_cpu(sb->data_offset);
1433 rdev->new_data_offset = rdev->data_offset;
1434 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1435 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1436 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
1437 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1439 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1440 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1441 if (rdev->sb_size & bmask)
1442 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1444 if (minor_version
1445 && rdev->data_offset < sb_start + (rdev->sb_size/512))
1446 return -EINVAL;
1447 if (minor_version
1448 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1449 return -EINVAL;
1451 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1452 rdev->desc_nr = -1;
1453 else
1454 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1456 if (!rdev->bb_page) {
1457 rdev->bb_page = alloc_page(GFP_KERNEL);
1458 if (!rdev->bb_page)
1459 return -ENOMEM;
1461 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1462 rdev->badblocks.count == 0) {
1463 /* need to load the bad block list.
1464 * Currently we limit it to one page.
1466 s32 offset;
1467 sector_t bb_sector;
1468 u64 *bbp;
1469 int i;
1470 int sectors = le16_to_cpu(sb->bblog_size);
1471 if (sectors > (PAGE_SIZE / 512))
1472 return -EINVAL;
1473 offset = le32_to_cpu(sb->bblog_offset);
1474 if (offset == 0)
1475 return -EINVAL;
1476 bb_sector = (long long)offset;
1477 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1478 rdev->bb_page, READ, true))
1479 return -EIO;
1480 bbp = (u64 *)page_address(rdev->bb_page);
1481 rdev->badblocks.shift = sb->bblog_shift;
1482 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1483 u64 bb = le64_to_cpu(*bbp);
1484 int count = bb & (0x3ff);
1485 u64 sector = bb >> 10;
1486 sector <<= sb->bblog_shift;
1487 count <<= sb->bblog_shift;
1488 if (bb + 1 == 0)
1489 break;
1490 if (md_set_badblocks(&rdev->badblocks,
1491 sector, count, 1) == 0)
1492 return -EINVAL;
1494 } else if (sb->bblog_offset != 0)
1495 rdev->badblocks.shift = 0;
1497 if (!refdev) {
1498 ret = 1;
1499 } else {
1500 __u64 ev1, ev2;
1501 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
1503 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1504 sb->level != refsb->level ||
1505 sb->layout != refsb->layout ||
1506 sb->chunksize != refsb->chunksize) {
1507 printk(KERN_WARNING "md: %s has strangely different"
1508 " superblock to %s\n",
1509 bdevname(rdev->bdev,b),
1510 bdevname(refdev->bdev,b2));
1511 return -EINVAL;
1513 ev1 = le64_to_cpu(sb->events);
1514 ev2 = le64_to_cpu(refsb->events);
1516 if (ev1 > ev2)
1517 ret = 1;
1518 else
1519 ret = 0;
1521 if (minor_version) {
1522 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1523 sectors -= rdev->data_offset;
1524 } else
1525 sectors = rdev->sb_start;
1526 if (sectors < le64_to_cpu(sb->data_size))
1527 return -EINVAL;
1528 rdev->sectors = le64_to_cpu(sb->data_size);
1529 return ret;
1532 static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
1534 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
1535 __u64 ev1 = le64_to_cpu(sb->events);
1537 rdev->raid_disk = -1;
1538 clear_bit(Faulty, &rdev->flags);
1539 clear_bit(In_sync, &rdev->flags);
1540 clear_bit(Bitmap_sync, &rdev->flags);
1541 clear_bit(WriteMostly, &rdev->flags);
1543 if (mddev->raid_disks == 0) {
1544 mddev->major_version = 1;
1545 mddev->patch_version = 0;
1546 mddev->external = 0;
1547 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
1548 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1549 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1550 mddev->level = le32_to_cpu(sb->level);
1551 mddev->clevel[0] = 0;
1552 mddev->layout = le32_to_cpu(sb->layout);
1553 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1554 mddev->dev_sectors = le64_to_cpu(sb->size);
1555 mddev->events = ev1;
1556 mddev->bitmap_info.offset = 0;
1557 mddev->bitmap_info.space = 0;
1558 /* Default location for bitmap is 1K after superblock
1559 * using 3K - total of 4K
1561 mddev->bitmap_info.default_offset = 1024 >> 9;
1562 mddev->bitmap_info.default_space = (4096-1024) >> 9;
1563 mddev->reshape_backwards = 0;
1565 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1566 memcpy(mddev->uuid, sb->set_uuid, 16);
1568 mddev->max_disks = (4096-256)/2;
1570 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1571 mddev->bitmap_info.file == NULL) {
1572 mddev->bitmap_info.offset =
1573 (__s32)le32_to_cpu(sb->bitmap_offset);
1574 /* Metadata doesn't record how much space is available.
1575 * For 1.0, we assume we can use up to the superblock
1576 * if before, else to 4K beyond superblock.
1577 * For others, assume no change is possible.
1579 if (mddev->minor_version > 0)
1580 mddev->bitmap_info.space = 0;
1581 else if (mddev->bitmap_info.offset > 0)
1582 mddev->bitmap_info.space =
1583 8 - mddev->bitmap_info.offset;
1584 else
1585 mddev->bitmap_info.space =
1586 -mddev->bitmap_info.offset;
1589 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1590 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1591 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1592 mddev->new_level = le32_to_cpu(sb->new_level);
1593 mddev->new_layout = le32_to_cpu(sb->new_layout);
1594 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
1595 if (mddev->delta_disks < 0 ||
1596 (mddev->delta_disks == 0 &&
1597 (le32_to_cpu(sb->feature_map)
1598 & MD_FEATURE_RESHAPE_BACKWARDS)))
1599 mddev->reshape_backwards = 1;
1600 } else {
1601 mddev->reshape_position = MaxSector;
1602 mddev->delta_disks = 0;
1603 mddev->new_level = mddev->level;
1604 mddev->new_layout = mddev->layout;
1605 mddev->new_chunk_sectors = mddev->chunk_sectors;
1608 } else if (mddev->pers == NULL) {
1609 /* Insist of good event counter while assembling, except for
1610 * spares (which don't need an event count) */
1611 ++ev1;
1612 if (rdev->desc_nr >= 0 &&
1613 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1614 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1615 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
1616 if (ev1 < mddev->events)
1617 return -EINVAL;
1618 } else if (mddev->bitmap) {
1619 /* If adding to array with a bitmap, then we can accept an
1620 * older device, but not too old.
1622 if (ev1 < mddev->bitmap->events_cleared)
1623 return 0;
1624 if (ev1 < mddev->events)
1625 set_bit(Bitmap_sync, &rdev->flags);
1626 } else {
1627 if (ev1 < mddev->events)
1628 /* just a hot-add of a new device, leave raid_disk at -1 */
1629 return 0;
1631 if (mddev->level != LEVEL_MULTIPATH) {
1632 int role;
1633 if (rdev->desc_nr < 0 ||
1634 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1635 role = MD_DISK_ROLE_SPARE;
1636 rdev->desc_nr = -1;
1637 } else
1638 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1639 switch(role) {
1640 case MD_DISK_ROLE_SPARE: /* spare */
1641 break;
1642 case MD_DISK_ROLE_FAULTY: /* faulty */
1643 set_bit(Faulty, &rdev->flags);
1644 break;
1645 case MD_DISK_ROLE_JOURNAL: /* journal device */
1646 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1647 /* journal device without journal feature */
1648 printk(KERN_WARNING
1649 "md: journal device provided without journal feature, ignoring the device\n");
1650 return -EINVAL;
1652 set_bit(Journal, &rdev->flags);
1653 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
1654 if (mddev->recovery_cp == MaxSector)
1655 set_bit(MD_JOURNAL_CLEAN, &mddev->flags);
1656 rdev->raid_disk = 0;
1657 break;
1658 default:
1659 rdev->saved_raid_disk = role;
1660 if ((le32_to_cpu(sb->feature_map) &
1661 MD_FEATURE_RECOVERY_OFFSET)) {
1662 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1663 if (!(le32_to_cpu(sb->feature_map) &
1664 MD_FEATURE_RECOVERY_BITMAP))
1665 rdev->saved_raid_disk = -1;
1666 } else
1667 set_bit(In_sync, &rdev->flags);
1668 rdev->raid_disk = role;
1669 break;
1671 if (sb->devflags & WriteMostly1)
1672 set_bit(WriteMostly, &rdev->flags);
1673 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1674 set_bit(Replacement, &rdev->flags);
1675 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
1676 set_bit(MD_HAS_JOURNAL, &mddev->flags);
1677 } else /* MULTIPATH are always insync */
1678 set_bit(In_sync, &rdev->flags);
1680 return 0;
1683 static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
1685 struct mdp_superblock_1 *sb;
1686 struct md_rdev *rdev2;
1687 int max_dev, i;
1688 /* make rdev->sb match mddev and rdev data. */
1690 sb = page_address(rdev->sb_page);
1692 sb->feature_map = 0;
1693 sb->pad0 = 0;
1694 sb->recovery_offset = cpu_to_le64(0);
1695 memset(sb->pad3, 0, sizeof(sb->pad3));
1697 sb->utime = cpu_to_le64((__u64)mddev->utime);
1698 sb->events = cpu_to_le64(mddev->events);
1699 if (mddev->in_sync)
1700 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1701 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
1702 sb->resync_offset = cpu_to_le64(MaxSector);
1703 else
1704 sb->resync_offset = cpu_to_le64(0);
1706 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1708 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1709 sb->size = cpu_to_le64(mddev->dev_sectors);
1710 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
1711 sb->level = cpu_to_le32(mddev->level);
1712 sb->layout = cpu_to_le32(mddev->layout);
1714 if (test_bit(WriteMostly, &rdev->flags))
1715 sb->devflags |= WriteMostly1;
1716 else
1717 sb->devflags &= ~WriteMostly1;
1718 sb->data_offset = cpu_to_le64(rdev->data_offset);
1719 sb->data_size = cpu_to_le64(rdev->sectors);
1721 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1722 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
1723 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1726 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
1727 !test_bit(In_sync, &rdev->flags)) {
1728 sb->feature_map |=
1729 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1730 sb->recovery_offset =
1731 cpu_to_le64(rdev->recovery_offset);
1732 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1733 sb->feature_map |=
1734 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
1736 /* Note: recovery_offset and journal_tail share space */
1737 if (test_bit(Journal, &rdev->flags))
1738 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
1739 if (test_bit(Replacement, &rdev->flags))
1740 sb->feature_map |=
1741 cpu_to_le32(MD_FEATURE_REPLACEMENT);
1743 if (mddev->reshape_position != MaxSector) {
1744 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1745 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1746 sb->new_layout = cpu_to_le32(mddev->new_layout);
1747 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1748 sb->new_level = cpu_to_le32(mddev->new_level);
1749 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
1750 if (mddev->delta_disks == 0 &&
1751 mddev->reshape_backwards)
1752 sb->feature_map
1753 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
1754 if (rdev->new_data_offset != rdev->data_offset) {
1755 sb->feature_map
1756 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1757 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1758 - rdev->data_offset));
1762 if (mddev_is_clustered(mddev))
1763 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
1765 if (rdev->badblocks.count == 0)
1766 /* Nothing to do for bad blocks*/ ;
1767 else if (sb->bblog_offset == 0)
1768 /* Cannot record bad blocks on this device */
1769 md_error(mddev, rdev);
1770 else {
1771 struct badblocks *bb = &rdev->badblocks;
1772 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1773 u64 *p = bb->page;
1774 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1775 if (bb->changed) {
1776 unsigned seq;
1778 retry:
1779 seq = read_seqbegin(&bb->lock);
1781 memset(bbp, 0xff, PAGE_SIZE);
1783 for (i = 0 ; i < bb->count ; i++) {
1784 u64 internal_bb = p[i];
1785 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1786 | BB_LEN(internal_bb));
1787 bbp[i] = cpu_to_le64(store_bb);
1789 bb->changed = 0;
1790 if (read_seqretry(&bb->lock, seq))
1791 goto retry;
1793 bb->sector = (rdev->sb_start +
1794 (int)le32_to_cpu(sb->bblog_offset));
1795 bb->size = le16_to_cpu(sb->bblog_size);
1799 max_dev = 0;
1800 rdev_for_each(rdev2, mddev)
1801 if (rdev2->desc_nr+1 > max_dev)
1802 max_dev = rdev2->desc_nr+1;
1804 if (max_dev > le32_to_cpu(sb->max_dev)) {
1805 int bmask;
1806 sb->max_dev = cpu_to_le32(max_dev);
1807 rdev->sb_size = max_dev * 2 + 256;
1808 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1809 if (rdev->sb_size & bmask)
1810 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1811 } else
1812 max_dev = le32_to_cpu(sb->max_dev);
1814 for (i=0; i<max_dev;i++)
1815 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
1817 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
1818 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
1820 rdev_for_each(rdev2, mddev) {
1821 i = rdev2->desc_nr;
1822 if (test_bit(Faulty, &rdev2->flags))
1823 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
1824 else if (test_bit(In_sync, &rdev2->flags))
1825 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1826 else if (test_bit(Journal, &rdev2->flags))
1827 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
1828 else if (rdev2->raid_disk >= 0)
1829 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1830 else
1831 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
1834 sb->sb_csum = calc_sb_1_csum(sb);
1837 static unsigned long long
1838 super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
1840 struct mdp_superblock_1 *sb;
1841 sector_t max_sectors;
1842 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1843 return 0; /* component must fit device */
1844 if (rdev->data_offset != rdev->new_data_offset)
1845 return 0; /* too confusing */
1846 if (rdev->sb_start < rdev->data_offset) {
1847 /* minor versions 1 and 2; superblock before data */
1848 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
1849 max_sectors -= rdev->data_offset;
1850 if (!num_sectors || num_sectors > max_sectors)
1851 num_sectors = max_sectors;
1852 } else if (rdev->mddev->bitmap_info.offset) {
1853 /* minor version 0 with bitmap we can't move */
1854 return 0;
1855 } else {
1856 /* minor version 0; superblock after data */
1857 sector_t sb_start;
1858 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
1859 sb_start &= ~(sector_t)(4*2 - 1);
1860 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
1861 if (!num_sectors || num_sectors > max_sectors)
1862 num_sectors = max_sectors;
1863 rdev->sb_start = sb_start;
1865 sb = page_address(rdev->sb_page);
1866 sb->data_size = cpu_to_le64(num_sectors);
1867 sb->super_offset = rdev->sb_start;
1868 sb->sb_csum = calc_sb_1_csum(sb);
1869 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1870 rdev->sb_page);
1871 md_super_wait(rdev->mddev);
1872 return num_sectors;
1876 static int
1877 super_1_allow_new_offset(struct md_rdev *rdev,
1878 unsigned long long new_offset)
1880 /* All necessary checks on new >= old have been done */
1881 struct bitmap *bitmap;
1882 if (new_offset >= rdev->data_offset)
1883 return 1;
1885 /* with 1.0 metadata, there is no metadata to tread on
1886 * so we can always move back */
1887 if (rdev->mddev->minor_version == 0)
1888 return 1;
1890 /* otherwise we must be sure not to step on
1891 * any metadata, so stay:
1892 * 36K beyond start of superblock
1893 * beyond end of badblocks
1894 * beyond write-intent bitmap
1896 if (rdev->sb_start + (32+4)*2 > new_offset)
1897 return 0;
1898 bitmap = rdev->mddev->bitmap;
1899 if (bitmap && !rdev->mddev->bitmap_info.file &&
1900 rdev->sb_start + rdev->mddev->bitmap_info.offset +
1901 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
1902 return 0;
1903 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1904 return 0;
1906 return 1;
1909 static struct super_type super_types[] = {
1910 [0] = {
1911 .name = "0.90.0",
1912 .owner = THIS_MODULE,
1913 .load_super = super_90_load,
1914 .validate_super = super_90_validate,
1915 .sync_super = super_90_sync,
1916 .rdev_size_change = super_90_rdev_size_change,
1917 .allow_new_offset = super_90_allow_new_offset,
1919 [1] = {
1920 .name = "md-1",
1921 .owner = THIS_MODULE,
1922 .load_super = super_1_load,
1923 .validate_super = super_1_validate,
1924 .sync_super = super_1_sync,
1925 .rdev_size_change = super_1_rdev_size_change,
1926 .allow_new_offset = super_1_allow_new_offset,
1930 static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
1932 if (mddev->sync_super) {
1933 mddev->sync_super(mddev, rdev);
1934 return;
1937 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1939 super_types[mddev->major_version].sync_super(mddev, rdev);
1942 static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
1944 struct md_rdev *rdev, *rdev2;
1946 rcu_read_lock();
1947 rdev_for_each_rcu(rdev, mddev1) {
1948 if (test_bit(Faulty, &rdev->flags) ||
1949 test_bit(Journal, &rdev->flags) ||
1950 rdev->raid_disk == -1)
1951 continue;
1952 rdev_for_each_rcu(rdev2, mddev2) {
1953 if (test_bit(Faulty, &rdev2->flags) ||
1954 test_bit(Journal, &rdev2->flags) ||
1955 rdev2->raid_disk == -1)
1956 continue;
1957 if (rdev->bdev->bd_contains ==
1958 rdev2->bdev->bd_contains) {
1959 rcu_read_unlock();
1960 return 1;
1964 rcu_read_unlock();
1965 return 0;
1968 static LIST_HEAD(pending_raid_disks);
1971 * Try to register data integrity profile for an mddev
1973 * This is called when an array is started and after a disk has been kicked
1974 * from the array. It only succeeds if all working and active component devices
1975 * are integrity capable with matching profiles.
1977 int md_integrity_register(struct mddev *mddev)
1979 struct md_rdev *rdev, *reference = NULL;
1981 if (list_empty(&mddev->disks))
1982 return 0; /* nothing to do */
1983 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1984 return 0; /* shouldn't register, or already is */
1985 rdev_for_each(rdev, mddev) {
1986 /* skip spares and non-functional disks */
1987 if (test_bit(Faulty, &rdev->flags))
1988 continue;
1989 if (rdev->raid_disk < 0)
1990 continue;
1991 if (!reference) {
1992 /* Use the first rdev as the reference */
1993 reference = rdev;
1994 continue;
1996 /* does this rdev's profile match the reference profile? */
1997 if (blk_integrity_compare(reference->bdev->bd_disk,
1998 rdev->bdev->bd_disk) < 0)
1999 return -EINVAL;
2001 if (!reference || !bdev_get_integrity(reference->bdev))
2002 return 0;
2004 * All component devices are integrity capable and have matching
2005 * profiles, register the common profile for the md device.
2007 blk_integrity_register(mddev->gendisk,
2008 bdev_get_integrity(reference->bdev));
2010 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
2011 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
2012 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
2013 mdname(mddev));
2014 return -EINVAL;
2016 return 0;
2018 EXPORT_SYMBOL(md_integrity_register);
2020 /* Disable data integrity if non-capable/non-matching disk is being added */
2021 void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
2023 struct blk_integrity *bi_rdev;
2024 struct blk_integrity *bi_mddev;
2026 if (!mddev->gendisk)
2027 return;
2029 bi_rdev = bdev_get_integrity(rdev->bdev);
2030 bi_mddev = blk_get_integrity(mddev->gendisk);
2032 if (!bi_mddev) /* nothing to do */
2033 return;
2034 if (rdev->raid_disk < 0) /* skip spares */
2035 return;
2036 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2037 rdev->bdev->bd_disk) >= 0)
2038 return;
2039 WARN_ON_ONCE(!mddev->suspended);
2040 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2041 blk_integrity_unregister(mddev->gendisk);
2043 EXPORT_SYMBOL(md_integrity_add_rdev);
2045 static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
2047 char b[BDEVNAME_SIZE];
2048 struct kobject *ko;
2049 int err;
2051 /* prevent duplicates */
2052 if (find_rdev(mddev, rdev->bdev->bd_dev))
2053 return -EEXIST;
2055 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2056 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2057 rdev->sectors < mddev->dev_sectors)) {
2058 if (mddev->pers) {
2059 /* Cannot change size, so fail
2060 * If mddev->level <= 0, then we don't care
2061 * about aligning sizes (e.g. linear)
2063 if (mddev->level > 0)
2064 return -ENOSPC;
2065 } else
2066 mddev->dev_sectors = rdev->sectors;
2069 /* Verify rdev->desc_nr is unique.
2070 * If it is -1, assign a free number, else
2071 * check number is not in use
2073 rcu_read_lock();
2074 if (rdev->desc_nr < 0) {
2075 int choice = 0;
2076 if (mddev->pers)
2077 choice = mddev->raid_disks;
2078 while (md_find_rdev_nr_rcu(mddev, choice))
2079 choice++;
2080 rdev->desc_nr = choice;
2081 } else {
2082 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
2083 rcu_read_unlock();
2084 return -EBUSY;
2087 rcu_read_unlock();
2088 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2089 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2090 mdname(mddev), mddev->max_disks);
2091 return -EBUSY;
2093 bdevname(rdev->bdev,b);
2094 strreplace(b, '/', '!');
2096 rdev->mddev = mddev;
2097 printk(KERN_INFO "md: bind<%s>\n", b);
2099 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
2100 goto fail;
2102 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
2103 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2104 /* failure here is OK */;
2105 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
2107 list_add_rcu(&rdev->same_set, &mddev->disks);
2108 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
2110 /* May as well allow recovery to be retried once */
2111 mddev->recovery_disabled++;
2113 return 0;
2115 fail:
2116 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2117 b, mdname(mddev));
2118 return err;
2121 static void md_delayed_delete(struct work_struct *ws)
2123 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
2124 kobject_del(&rdev->kobj);
2125 kobject_put(&rdev->kobj);
2128 static void unbind_rdev_from_array(struct md_rdev *rdev)
2130 char b[BDEVNAME_SIZE];
2132 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
2133 list_del_rcu(&rdev->same_set);
2134 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2135 rdev->mddev = NULL;
2136 sysfs_remove_link(&rdev->kobj, "block");
2137 sysfs_put(rdev->sysfs_state);
2138 rdev->sysfs_state = NULL;
2139 rdev->badblocks.count = 0;
2140 /* We need to delay this, otherwise we can deadlock when
2141 * writing to 'remove' to "dev/state". We also need
2142 * to delay it due to rcu usage.
2144 synchronize_rcu();
2145 INIT_WORK(&rdev->del_work, md_delayed_delete);
2146 kobject_get(&rdev->kobj);
2147 queue_work(md_misc_wq, &rdev->del_work);
2151 * prevent the device from being mounted, repartitioned or
2152 * otherwise reused by a RAID array (or any other kernel
2153 * subsystem), by bd_claiming the device.
2155 static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
2157 int err = 0;
2158 struct block_device *bdev;
2159 char b[BDEVNAME_SIZE];
2161 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
2162 shared ? (struct md_rdev *)lock_rdev : rdev);
2163 if (IS_ERR(bdev)) {
2164 printk(KERN_ERR "md: could not open %s.\n",
2165 __bdevname(dev, b));
2166 return PTR_ERR(bdev);
2168 rdev->bdev = bdev;
2169 return err;
2172 static void unlock_rdev(struct md_rdev *rdev)
2174 struct block_device *bdev = rdev->bdev;
2175 rdev->bdev = NULL;
2176 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
2179 void md_autodetect_dev(dev_t dev);
2181 static void export_rdev(struct md_rdev *rdev)
2183 char b[BDEVNAME_SIZE];
2185 printk(KERN_INFO "md: export_rdev(%s)\n",
2186 bdevname(rdev->bdev,b));
2187 md_rdev_clear(rdev);
2188 #ifndef MODULE
2189 if (test_bit(AutoDetected, &rdev->flags))
2190 md_autodetect_dev(rdev->bdev->bd_dev);
2191 #endif
2192 unlock_rdev(rdev);
2193 kobject_put(&rdev->kobj);
2196 void md_kick_rdev_from_array(struct md_rdev *rdev)
2198 unbind_rdev_from_array(rdev);
2199 export_rdev(rdev);
2201 EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
2203 static void export_array(struct mddev *mddev)
2205 struct md_rdev *rdev;
2207 while (!list_empty(&mddev->disks)) {
2208 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2209 same_set);
2210 md_kick_rdev_from_array(rdev);
2212 mddev->raid_disks = 0;
2213 mddev->major_version = 0;
2216 static void sync_sbs(struct mddev *mddev, int nospares)
2218 /* Update each superblock (in-memory image), but
2219 * if we are allowed to, skip spares which already
2220 * have the right event counter, or have one earlier
2221 * (which would mean they aren't being marked as dirty
2222 * with the rest of the array)
2224 struct md_rdev *rdev;
2225 rdev_for_each(rdev, mddev) {
2226 if (rdev->sb_events == mddev->events ||
2227 (nospares &&
2228 rdev->raid_disk < 0 &&
2229 rdev->sb_events+1 == mddev->events)) {
2230 /* Don't update this superblock */
2231 rdev->sb_loaded = 2;
2232 } else {
2233 sync_super(mddev, rdev);
2234 rdev->sb_loaded = 1;
2239 static bool does_sb_need_changing(struct mddev *mddev)
2241 struct md_rdev *rdev;
2242 struct mdp_superblock_1 *sb;
2243 int role;
2245 /* Find a good rdev */
2246 rdev_for_each(rdev, mddev)
2247 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2248 break;
2250 /* No good device found. */
2251 if (!rdev)
2252 return false;
2254 sb = page_address(rdev->sb_page);
2255 /* Check if a device has become faulty or a spare become active */
2256 rdev_for_each(rdev, mddev) {
2257 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2258 /* Device activated? */
2259 if (role == 0xffff && rdev->raid_disk >=0 &&
2260 !test_bit(Faulty, &rdev->flags))
2261 return true;
2262 /* Device turned faulty? */
2263 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2264 return true;
2267 /* Check if any mddev parameters have changed */
2268 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2269 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
2270 (mddev->layout != le64_to_cpu(sb->layout)) ||
2271 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2272 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2273 return true;
2275 return false;
2278 void md_update_sb(struct mddev *mddev, int force_change)
2280 struct md_rdev *rdev;
2281 int sync_req;
2282 int nospares = 0;
2283 int any_badblocks_changed = 0;
2284 int ret = -1;
2286 if (mddev->ro) {
2287 if (force_change)
2288 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2289 return;
2292 if (mddev_is_clustered(mddev)) {
2293 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2294 force_change = 1;
2295 ret = md_cluster_ops->metadata_update_start(mddev);
2296 /* Has someone else has updated the sb */
2297 if (!does_sb_need_changing(mddev)) {
2298 if (ret == 0)
2299 md_cluster_ops->metadata_update_cancel(mddev);
2300 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2301 return;
2304 repeat:
2305 /* First make sure individual recovery_offsets are correct */
2306 rdev_for_each(rdev, mddev) {
2307 if (rdev->raid_disk >= 0 &&
2308 mddev->delta_disks >= 0 &&
2309 !test_bit(Journal, &rdev->flags) &&
2310 !test_bit(In_sync, &rdev->flags) &&
2311 mddev->curr_resync_completed > rdev->recovery_offset)
2312 rdev->recovery_offset = mddev->curr_resync_completed;
2315 if (!mddev->persistent) {
2316 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
2317 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
2318 if (!mddev->external) {
2319 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2320 rdev_for_each(rdev, mddev) {
2321 if (rdev->badblocks.changed) {
2322 rdev->badblocks.changed = 0;
2323 md_ack_all_badblocks(&rdev->badblocks);
2324 md_error(mddev, rdev);
2326 clear_bit(Blocked, &rdev->flags);
2327 clear_bit(BlockedBadBlocks, &rdev->flags);
2328 wake_up(&rdev->blocked_wait);
2331 wake_up(&mddev->sb_wait);
2332 return;
2335 spin_lock(&mddev->lock);
2337 mddev->utime = get_seconds();
2339 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2340 force_change = 1;
2341 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2342 /* just a clean<-> dirty transition, possibly leave spares alone,
2343 * though if events isn't the right even/odd, we will have to do
2344 * spares after all
2346 nospares = 1;
2347 if (force_change)
2348 nospares = 0;
2349 if (mddev->degraded)
2350 /* If the array is degraded, then skipping spares is both
2351 * dangerous and fairly pointless.
2352 * Dangerous because a device that was removed from the array
2353 * might have a event_count that still looks up-to-date,
2354 * so it can be re-added without a resync.
2355 * Pointless because if there are any spares to skip,
2356 * then a recovery will happen and soon that array won't
2357 * be degraded any more and the spare can go back to sleep then.
2359 nospares = 0;
2361 sync_req = mddev->in_sync;
2363 /* If this is just a dirty<->clean transition, and the array is clean
2364 * and 'events' is odd, we can roll back to the previous clean state */
2365 if (nospares
2366 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
2367 && mddev->can_decrease_events
2368 && mddev->events != 1) {
2369 mddev->events--;
2370 mddev->can_decrease_events = 0;
2371 } else {
2372 /* otherwise we have to go forward and ... */
2373 mddev->events ++;
2374 mddev->can_decrease_events = nospares;
2378 * This 64-bit counter should never wrap.
2379 * Either we are in around ~1 trillion A.C., assuming
2380 * 1 reboot per second, or we have a bug...
2382 WARN_ON(mddev->events == 0);
2384 rdev_for_each(rdev, mddev) {
2385 if (rdev->badblocks.changed)
2386 any_badblocks_changed++;
2387 if (test_bit(Faulty, &rdev->flags))
2388 set_bit(FaultRecorded, &rdev->flags);
2391 sync_sbs(mddev, nospares);
2392 spin_unlock(&mddev->lock);
2394 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2395 mdname(mddev), mddev->in_sync);
2397 bitmap_update_sb(mddev->bitmap);
2398 rdev_for_each(rdev, mddev) {
2399 char b[BDEVNAME_SIZE];
2401 if (rdev->sb_loaded != 1)
2402 continue; /* no noise on spare devices */
2404 if (!test_bit(Faulty, &rdev->flags)) {
2405 md_super_write(mddev,rdev,
2406 rdev->sb_start, rdev->sb_size,
2407 rdev->sb_page);
2408 pr_debug("md: (write) %s's sb offset: %llu\n",
2409 bdevname(rdev->bdev, b),
2410 (unsigned long long)rdev->sb_start);
2411 rdev->sb_events = mddev->events;
2412 if (rdev->badblocks.size) {
2413 md_super_write(mddev, rdev,
2414 rdev->badblocks.sector,
2415 rdev->badblocks.size << 9,
2416 rdev->bb_page);
2417 rdev->badblocks.size = 0;
2420 } else
2421 pr_debug("md: %s (skipping faulty)\n",
2422 bdevname(rdev->bdev, b));
2424 if (mddev->level == LEVEL_MULTIPATH)
2425 /* only need to write one superblock... */
2426 break;
2428 md_super_wait(mddev);
2429 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2431 spin_lock(&mddev->lock);
2432 if (mddev->in_sync != sync_req ||
2433 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
2434 /* have to write it out again */
2435 spin_unlock(&mddev->lock);
2436 goto repeat;
2438 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2439 spin_unlock(&mddev->lock);
2440 wake_up(&mddev->sb_wait);
2441 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2442 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
2444 rdev_for_each(rdev, mddev) {
2445 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2446 clear_bit(Blocked, &rdev->flags);
2448 if (any_badblocks_changed)
2449 md_ack_all_badblocks(&rdev->badblocks);
2450 clear_bit(BlockedBadBlocks, &rdev->flags);
2451 wake_up(&rdev->blocked_wait);
2454 if (mddev_is_clustered(mddev) && ret == 0)
2455 md_cluster_ops->metadata_update_finish(mddev);
2457 EXPORT_SYMBOL(md_update_sb);
2459 static int add_bound_rdev(struct md_rdev *rdev)
2461 struct mddev *mddev = rdev->mddev;
2462 int err = 0;
2464 if (!mddev->pers->hot_remove_disk) {
2465 /* If there is hot_add_disk but no hot_remove_disk
2466 * then added disks for geometry changes,
2467 * and should be added immediately.
2469 super_types[mddev->major_version].
2470 validate_super(mddev, rdev);
2471 err = mddev->pers->hot_add_disk(mddev, rdev);
2472 if (err) {
2473 unbind_rdev_from_array(rdev);
2474 export_rdev(rdev);
2475 return err;
2478 sysfs_notify_dirent_safe(rdev->sysfs_state);
2480 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2481 if (mddev->degraded)
2482 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2483 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2484 md_new_event(mddev);
2485 md_wakeup_thread(mddev->thread);
2486 return 0;
2489 /* words written to sysfs files may, or may not, be \n terminated.
2490 * We want to accept with case. For this we use cmd_match.
2492 static int cmd_match(const char *cmd, const char *str)
2494 /* See if cmd, written into a sysfs file, matches
2495 * str. They must either be the same, or cmd can
2496 * have a trailing newline
2498 while (*cmd && *str && *cmd == *str) {
2499 cmd++;
2500 str++;
2502 if (*cmd == '\n')
2503 cmd++;
2504 if (*str || *cmd)
2505 return 0;
2506 return 1;
2509 struct rdev_sysfs_entry {
2510 struct attribute attr;
2511 ssize_t (*show)(struct md_rdev *, char *);
2512 ssize_t (*store)(struct md_rdev *, const char *, size_t);
2515 static ssize_t
2516 state_show(struct md_rdev *rdev, char *page)
2518 char *sep = "";
2519 size_t len = 0;
2520 unsigned long flags = ACCESS_ONCE(rdev->flags);
2522 if (test_bit(Faulty, &flags) ||
2523 rdev->badblocks.unacked_exist) {
2524 len+= sprintf(page+len, "%sfaulty",sep);
2525 sep = ",";
2527 if (test_bit(In_sync, &flags)) {
2528 len += sprintf(page+len, "%sin_sync",sep);
2529 sep = ",";
2531 if (test_bit(Journal, &flags)) {
2532 len += sprintf(page+len, "%sjournal",sep);
2533 sep = ",";
2535 if (test_bit(WriteMostly, &flags)) {
2536 len += sprintf(page+len, "%swrite_mostly",sep);
2537 sep = ",";
2539 if (test_bit(Blocked, &flags) ||
2540 (rdev->badblocks.unacked_exist
2541 && !test_bit(Faulty, &flags))) {
2542 len += sprintf(page+len, "%sblocked", sep);
2543 sep = ",";
2545 if (!test_bit(Faulty, &flags) &&
2546 !test_bit(Journal, &flags) &&
2547 !test_bit(In_sync, &flags)) {
2548 len += sprintf(page+len, "%sspare", sep);
2549 sep = ",";
2551 if (test_bit(WriteErrorSeen, &flags)) {
2552 len += sprintf(page+len, "%swrite_error", sep);
2553 sep = ",";
2555 if (test_bit(WantReplacement, &flags)) {
2556 len += sprintf(page+len, "%swant_replacement", sep);
2557 sep = ",";
2559 if (test_bit(Replacement, &flags)) {
2560 len += sprintf(page+len, "%sreplacement", sep);
2561 sep = ",";
2564 return len+sprintf(page+len, "\n");
2567 static ssize_t
2568 state_store(struct md_rdev *rdev, const char *buf, size_t len)
2570 /* can write
2571 * faulty - simulates an error
2572 * remove - disconnects the device
2573 * writemostly - sets write_mostly
2574 * -writemostly - clears write_mostly
2575 * blocked - sets the Blocked flags
2576 * -blocked - clears the Blocked and possibly simulates an error
2577 * insync - sets Insync providing device isn't active
2578 * -insync - clear Insync for a device with a slot assigned,
2579 * so that it gets rebuilt based on bitmap
2580 * write_error - sets WriteErrorSeen
2581 * -write_error - clears WriteErrorSeen
2583 int err = -EINVAL;
2584 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2585 md_error(rdev->mddev, rdev);
2586 if (test_bit(Faulty, &rdev->flags))
2587 err = 0;
2588 else
2589 err = -EBUSY;
2590 } else if (cmd_match(buf, "remove")) {
2591 if (rdev->raid_disk >= 0)
2592 err = -EBUSY;
2593 else {
2594 struct mddev *mddev = rdev->mddev;
2595 err = 0;
2596 if (mddev_is_clustered(mddev))
2597 err = md_cluster_ops->remove_disk(mddev, rdev);
2599 if (err == 0) {
2600 md_kick_rdev_from_array(rdev);
2601 if (mddev->pers)
2602 md_update_sb(mddev, 1);
2603 md_new_event(mddev);
2606 } else if (cmd_match(buf, "writemostly")) {
2607 set_bit(WriteMostly, &rdev->flags);
2608 err = 0;
2609 } else if (cmd_match(buf, "-writemostly")) {
2610 clear_bit(WriteMostly, &rdev->flags);
2611 err = 0;
2612 } else if (cmd_match(buf, "blocked")) {
2613 set_bit(Blocked, &rdev->flags);
2614 err = 0;
2615 } else if (cmd_match(buf, "-blocked")) {
2616 if (!test_bit(Faulty, &rdev->flags) &&
2617 rdev->badblocks.unacked_exist) {
2618 /* metadata handler doesn't understand badblocks,
2619 * so we need to fail the device
2621 md_error(rdev->mddev, rdev);
2623 clear_bit(Blocked, &rdev->flags);
2624 clear_bit(BlockedBadBlocks, &rdev->flags);
2625 wake_up(&rdev->blocked_wait);
2626 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2627 md_wakeup_thread(rdev->mddev->thread);
2629 err = 0;
2630 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2631 set_bit(In_sync, &rdev->flags);
2632 err = 0;
2633 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
2634 !test_bit(Journal, &rdev->flags)) {
2635 if (rdev->mddev->pers == NULL) {
2636 clear_bit(In_sync, &rdev->flags);
2637 rdev->saved_raid_disk = rdev->raid_disk;
2638 rdev->raid_disk = -1;
2639 err = 0;
2641 } else if (cmd_match(buf, "write_error")) {
2642 set_bit(WriteErrorSeen, &rdev->flags);
2643 err = 0;
2644 } else if (cmd_match(buf, "-write_error")) {
2645 clear_bit(WriteErrorSeen, &rdev->flags);
2646 err = 0;
2647 } else if (cmd_match(buf, "want_replacement")) {
2648 /* Any non-spare device that is not a replacement can
2649 * become want_replacement at any time, but we then need to
2650 * check if recovery is needed.
2652 if (rdev->raid_disk >= 0 &&
2653 !test_bit(Journal, &rdev->flags) &&
2654 !test_bit(Replacement, &rdev->flags))
2655 set_bit(WantReplacement, &rdev->flags);
2656 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2657 md_wakeup_thread(rdev->mddev->thread);
2658 err = 0;
2659 } else if (cmd_match(buf, "-want_replacement")) {
2660 /* Clearing 'want_replacement' is always allowed.
2661 * Once replacements starts it is too late though.
2663 err = 0;
2664 clear_bit(WantReplacement, &rdev->flags);
2665 } else if (cmd_match(buf, "replacement")) {
2666 /* Can only set a device as a replacement when array has not
2667 * yet been started. Once running, replacement is automatic
2668 * from spares, or by assigning 'slot'.
2670 if (rdev->mddev->pers)
2671 err = -EBUSY;
2672 else {
2673 set_bit(Replacement, &rdev->flags);
2674 err = 0;
2676 } else if (cmd_match(buf, "-replacement")) {
2677 /* Similarly, can only clear Replacement before start */
2678 if (rdev->mddev->pers)
2679 err = -EBUSY;
2680 else {
2681 clear_bit(Replacement, &rdev->flags);
2682 err = 0;
2684 } else if (cmd_match(buf, "re-add")) {
2685 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) {
2686 /* clear_bit is performed _after_ all the devices
2687 * have their local Faulty bit cleared. If any writes
2688 * happen in the meantime in the local node, they
2689 * will land in the local bitmap, which will be synced
2690 * by this node eventually
2692 if (!mddev_is_clustered(rdev->mddev) ||
2693 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2694 clear_bit(Faulty, &rdev->flags);
2695 err = add_bound_rdev(rdev);
2697 } else
2698 err = -EBUSY;
2700 if (!err)
2701 sysfs_notify_dirent_safe(rdev->sysfs_state);
2702 return err ? err : len;
2704 static struct rdev_sysfs_entry rdev_state =
2705 __ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
2707 static ssize_t
2708 errors_show(struct md_rdev *rdev, char *page)
2710 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2713 static ssize_t
2714 errors_store(struct md_rdev *rdev, const char *buf, size_t len)
2716 unsigned int n;
2717 int rv;
2719 rv = kstrtouint(buf, 10, &n);
2720 if (rv < 0)
2721 return rv;
2722 atomic_set(&rdev->corrected_errors, n);
2723 return len;
2725 static struct rdev_sysfs_entry rdev_errors =
2726 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
2728 static ssize_t
2729 slot_show(struct md_rdev *rdev, char *page)
2731 if (test_bit(Journal, &rdev->flags))
2732 return sprintf(page, "journal\n");
2733 else if (rdev->raid_disk < 0)
2734 return sprintf(page, "none\n");
2735 else
2736 return sprintf(page, "%d\n", rdev->raid_disk);
2739 static ssize_t
2740 slot_store(struct md_rdev *rdev, const char *buf, size_t len)
2742 int slot;
2743 int err;
2745 if (test_bit(Journal, &rdev->flags))
2746 return -EBUSY;
2747 if (strncmp(buf, "none", 4)==0)
2748 slot = -1;
2749 else {
2750 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2751 if (err < 0)
2752 return err;
2754 if (rdev->mddev->pers && slot == -1) {
2755 /* Setting 'slot' on an active array requires also
2756 * updating the 'rd%d' link, and communicating
2757 * with the personality with ->hot_*_disk.
2758 * For now we only support removing
2759 * failed/spare devices. This normally happens automatically,
2760 * but not when the metadata is externally managed.
2762 if (rdev->raid_disk == -1)
2763 return -EEXIST;
2764 /* personality does all needed checks */
2765 if (rdev->mddev->pers->hot_remove_disk == NULL)
2766 return -EINVAL;
2767 clear_bit(Blocked, &rdev->flags);
2768 remove_and_add_spares(rdev->mddev, rdev);
2769 if (rdev->raid_disk >= 0)
2770 return -EBUSY;
2771 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2772 md_wakeup_thread(rdev->mddev->thread);
2773 } else if (rdev->mddev->pers) {
2774 /* Activating a spare .. or possibly reactivating
2775 * if we ever get bitmaps working here.
2777 int err;
2779 if (rdev->raid_disk != -1)
2780 return -EBUSY;
2782 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2783 return -EBUSY;
2785 if (rdev->mddev->pers->hot_add_disk == NULL)
2786 return -EINVAL;
2788 if (slot >= rdev->mddev->raid_disks &&
2789 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2790 return -ENOSPC;
2792 rdev->raid_disk = slot;
2793 if (test_bit(In_sync, &rdev->flags))
2794 rdev->saved_raid_disk = slot;
2795 else
2796 rdev->saved_raid_disk = -1;
2797 clear_bit(In_sync, &rdev->flags);
2798 clear_bit(Bitmap_sync, &rdev->flags);
2799 err = rdev->mddev->pers->
2800 hot_add_disk(rdev->mddev, rdev);
2801 if (err) {
2802 rdev->raid_disk = -1;
2803 return err;
2804 } else
2805 sysfs_notify_dirent_safe(rdev->sysfs_state);
2806 if (sysfs_link_rdev(rdev->mddev, rdev))
2807 /* failure here is OK */;
2808 /* don't wakeup anyone, leave that to userspace. */
2809 } else {
2810 if (slot >= rdev->mddev->raid_disks &&
2811 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2812 return -ENOSPC;
2813 rdev->raid_disk = slot;
2814 /* assume it is working */
2815 clear_bit(Faulty, &rdev->flags);
2816 clear_bit(WriteMostly, &rdev->flags);
2817 set_bit(In_sync, &rdev->flags);
2818 sysfs_notify_dirent_safe(rdev->sysfs_state);
2820 return len;
2823 static struct rdev_sysfs_entry rdev_slot =
2824 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
2826 static ssize_t
2827 offset_show(struct md_rdev *rdev, char *page)
2829 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
2832 static ssize_t
2833 offset_store(struct md_rdev *rdev, const char *buf, size_t len)
2835 unsigned long long offset;
2836 if (kstrtoull(buf, 10, &offset) < 0)
2837 return -EINVAL;
2838 if (rdev->mddev->pers && rdev->raid_disk >= 0)
2839 return -EBUSY;
2840 if (rdev->sectors && rdev->mddev->external)
2841 /* Must set offset before size, so overlap checks
2842 * can be sane */
2843 return -EBUSY;
2844 rdev->data_offset = offset;
2845 rdev->new_data_offset = offset;
2846 return len;
2849 static struct rdev_sysfs_entry rdev_offset =
2850 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
2852 static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2854 return sprintf(page, "%llu\n",
2855 (unsigned long long)rdev->new_data_offset);
2858 static ssize_t new_offset_store(struct md_rdev *rdev,
2859 const char *buf, size_t len)
2861 unsigned long long new_offset;
2862 struct mddev *mddev = rdev->mddev;
2864 if (kstrtoull(buf, 10, &new_offset) < 0)
2865 return -EINVAL;
2867 if (mddev->sync_thread ||
2868 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
2869 return -EBUSY;
2870 if (new_offset == rdev->data_offset)
2871 /* reset is always permitted */
2873 else if (new_offset > rdev->data_offset) {
2874 /* must not push array size beyond rdev_sectors */
2875 if (new_offset - rdev->data_offset
2876 + mddev->dev_sectors > rdev->sectors)
2877 return -E2BIG;
2879 /* Metadata worries about other space details. */
2881 /* decreasing the offset is inconsistent with a backwards
2882 * reshape.
2884 if (new_offset < rdev->data_offset &&
2885 mddev->reshape_backwards)
2886 return -EINVAL;
2887 /* Increasing offset is inconsistent with forwards
2888 * reshape. reshape_direction should be set to
2889 * 'backwards' first.
2891 if (new_offset > rdev->data_offset &&
2892 !mddev->reshape_backwards)
2893 return -EINVAL;
2895 if (mddev->pers && mddev->persistent &&
2896 !super_types[mddev->major_version]
2897 .allow_new_offset(rdev, new_offset))
2898 return -E2BIG;
2899 rdev->new_data_offset = new_offset;
2900 if (new_offset > rdev->data_offset)
2901 mddev->reshape_backwards = 1;
2902 else if (new_offset < rdev->data_offset)
2903 mddev->reshape_backwards = 0;
2905 return len;
2907 static struct rdev_sysfs_entry rdev_new_offset =
2908 __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2910 static ssize_t
2911 rdev_size_show(struct md_rdev *rdev, char *page)
2913 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
2916 static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2918 /* check if two start/length pairs overlap */
2919 if (s1+l1 <= s2)
2920 return 0;
2921 if (s2+l2 <= s1)
2922 return 0;
2923 return 1;
2926 static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2928 unsigned long long blocks;
2929 sector_t new;
2931 if (kstrtoull(buf, 10, &blocks) < 0)
2932 return -EINVAL;
2934 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2935 return -EINVAL; /* sector conversion overflow */
2937 new = blocks * 2;
2938 if (new != blocks * 2)
2939 return -EINVAL; /* unsigned long long to sector_t overflow */
2941 *sectors = new;
2942 return 0;
2945 static ssize_t
2946 rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
2948 struct mddev *my_mddev = rdev->mddev;
2949 sector_t oldsectors = rdev->sectors;
2950 sector_t sectors;
2952 if (test_bit(Journal, &rdev->flags))
2953 return -EBUSY;
2954 if (strict_blocks_to_sectors(buf, &sectors) < 0)
2955 return -EINVAL;
2956 if (rdev->data_offset != rdev->new_data_offset)
2957 return -EINVAL; /* too confusing */
2958 if (my_mddev->pers && rdev->raid_disk >= 0) {
2959 if (my_mddev->persistent) {
2960 sectors = super_types[my_mddev->major_version].
2961 rdev_size_change(rdev, sectors);
2962 if (!sectors)
2963 return -EBUSY;
2964 } else if (!sectors)
2965 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
2966 rdev->data_offset;
2967 if (!my_mddev->pers->resize)
2968 /* Cannot change size for RAID0 or Linear etc */
2969 return -EINVAL;
2971 if (sectors < my_mddev->dev_sectors)
2972 return -EINVAL; /* component must fit device */
2974 rdev->sectors = sectors;
2975 if (sectors > oldsectors && my_mddev->external) {
2976 /* Need to check that all other rdevs with the same
2977 * ->bdev do not overlap. 'rcu' is sufficient to walk
2978 * the rdev lists safely.
2979 * This check does not provide a hard guarantee, it
2980 * just helps avoid dangerous mistakes.
2982 struct mddev *mddev;
2983 int overlap = 0;
2984 struct list_head *tmp;
2986 rcu_read_lock();
2987 for_each_mddev(mddev, tmp) {
2988 struct md_rdev *rdev2;
2990 rdev_for_each(rdev2, mddev)
2991 if (rdev->bdev == rdev2->bdev &&
2992 rdev != rdev2 &&
2993 overlaps(rdev->data_offset, rdev->sectors,
2994 rdev2->data_offset,
2995 rdev2->sectors)) {
2996 overlap = 1;
2997 break;
2999 if (overlap) {
3000 mddev_put(mddev);
3001 break;
3004 rcu_read_unlock();
3005 if (overlap) {
3006 /* Someone else could have slipped in a size
3007 * change here, but doing so is just silly.
3008 * We put oldsectors back because we *know* it is
3009 * safe, and trust userspace not to race with
3010 * itself
3012 rdev->sectors = oldsectors;
3013 return -EBUSY;
3016 return len;
3019 static struct rdev_sysfs_entry rdev_size =
3020 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
3022 static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
3024 unsigned long long recovery_start = rdev->recovery_offset;
3026 if (test_bit(In_sync, &rdev->flags) ||
3027 recovery_start == MaxSector)
3028 return sprintf(page, "none\n");
3030 return sprintf(page, "%llu\n", recovery_start);
3033 static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
3035 unsigned long long recovery_start;
3037 if (cmd_match(buf, "none"))
3038 recovery_start = MaxSector;
3039 else if (kstrtoull(buf, 10, &recovery_start))
3040 return -EINVAL;
3042 if (rdev->mddev->pers &&
3043 rdev->raid_disk >= 0)
3044 return -EBUSY;
3046 rdev->recovery_offset = recovery_start;
3047 if (recovery_start == MaxSector)
3048 set_bit(In_sync, &rdev->flags);
3049 else
3050 clear_bit(In_sync, &rdev->flags);
3051 return len;
3054 static struct rdev_sysfs_entry rdev_recovery_start =
3055 __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3057 static ssize_t
3058 badblocks_show(struct badblocks *bb, char *page, int unack);
3059 static ssize_t
3060 badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
3062 static ssize_t bb_show(struct md_rdev *rdev, char *page)
3064 return badblocks_show(&rdev->badblocks, page, 0);
3066 static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
3068 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3069 /* Maybe that ack was all we needed */
3070 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3071 wake_up(&rdev->blocked_wait);
3072 return rv;
3074 static struct rdev_sysfs_entry rdev_bad_blocks =
3075 __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3077 static ssize_t ubb_show(struct md_rdev *rdev, char *page)
3079 return badblocks_show(&rdev->badblocks, page, 1);
3081 static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
3083 return badblocks_store(&rdev->badblocks, page, len, 1);
3085 static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3086 __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3088 static struct attribute *rdev_default_attrs[] = {
3089 &rdev_state.attr,
3090 &rdev_errors.attr,
3091 &rdev_slot.attr,
3092 &rdev_offset.attr,
3093 &rdev_new_offset.attr,
3094 &rdev_size.attr,
3095 &rdev_recovery_start.attr,
3096 &rdev_bad_blocks.attr,
3097 &rdev_unack_bad_blocks.attr,
3098 NULL,
3100 static ssize_t
3101 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3103 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
3104 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
3106 if (!entry->show)
3107 return -EIO;
3108 if (!rdev->mddev)
3109 return -EBUSY;
3110 return entry->show(rdev, page);
3113 static ssize_t
3114 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3115 const char *page, size_t length)
3117 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
3118 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
3119 ssize_t rv;
3120 struct mddev *mddev = rdev->mddev;
3122 if (!entry->store)
3123 return -EIO;
3124 if (!capable(CAP_SYS_ADMIN))
3125 return -EACCES;
3126 rv = mddev ? mddev_lock(mddev): -EBUSY;
3127 if (!rv) {
3128 if (rdev->mddev == NULL)
3129 rv = -EBUSY;
3130 else
3131 rv = entry->store(rdev, page, length);
3132 mddev_unlock(mddev);
3134 return rv;
3137 static void rdev_free(struct kobject *ko)
3139 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
3140 kfree(rdev);
3142 static const struct sysfs_ops rdev_sysfs_ops = {
3143 .show = rdev_attr_show,
3144 .store = rdev_attr_store,
3146 static struct kobj_type rdev_ktype = {
3147 .release = rdev_free,
3148 .sysfs_ops = &rdev_sysfs_ops,
3149 .default_attrs = rdev_default_attrs,
3152 int md_rdev_init(struct md_rdev *rdev)
3154 rdev->desc_nr = -1;
3155 rdev->saved_raid_disk = -1;
3156 rdev->raid_disk = -1;
3157 rdev->flags = 0;
3158 rdev->data_offset = 0;
3159 rdev->new_data_offset = 0;
3160 rdev->sb_events = 0;
3161 rdev->last_read_error.tv_sec = 0;
3162 rdev->last_read_error.tv_nsec = 0;
3163 rdev->sb_loaded = 0;
3164 rdev->bb_page = NULL;
3165 atomic_set(&rdev->nr_pending, 0);
3166 atomic_set(&rdev->read_errors, 0);
3167 atomic_set(&rdev->corrected_errors, 0);
3169 INIT_LIST_HEAD(&rdev->same_set);
3170 init_waitqueue_head(&rdev->blocked_wait);
3172 /* Add space to store bad block list.
3173 * This reserves the space even on arrays where it cannot
3174 * be used - I wonder if that matters
3176 rdev->badblocks.count = 0;
3177 rdev->badblocks.shift = -1; /* disabled until explicitly enabled */
3178 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3179 seqlock_init(&rdev->badblocks.lock);
3180 if (rdev->badblocks.page == NULL)
3181 return -ENOMEM;
3183 return 0;
3185 EXPORT_SYMBOL_GPL(md_rdev_init);
3187 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3189 * mark the device faulty if:
3191 * - the device is nonexistent (zero size)
3192 * - the device has no valid superblock
3194 * a faulty rdev _never_ has rdev->sb set.
3196 static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
3198 char b[BDEVNAME_SIZE];
3199 int err;
3200 struct md_rdev *rdev;
3201 sector_t size;
3203 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
3204 if (!rdev) {
3205 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3206 return ERR_PTR(-ENOMEM);
3209 err = md_rdev_init(rdev);
3210 if (err)
3211 goto abort_free;
3212 err = alloc_disk_sb(rdev);
3213 if (err)
3214 goto abort_free;
3216 err = lock_rdev(rdev, newdev, super_format == -2);
3217 if (err)
3218 goto abort_free;
3220 kobject_init(&rdev->kobj, &rdev_ktype);
3222 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
3223 if (!size) {
3224 printk(KERN_WARNING
3225 "md: %s has zero or unknown size, marking faulty!\n",
3226 bdevname(rdev->bdev,b));
3227 err = -EINVAL;
3228 goto abort_free;
3231 if (super_format >= 0) {
3232 err = super_types[super_format].
3233 load_super(rdev, NULL, super_minor);
3234 if (err == -EINVAL) {
3235 printk(KERN_WARNING
3236 "md: %s does not have a valid v%d.%d "
3237 "superblock, not importing!\n",
3238 bdevname(rdev->bdev,b),
3239 super_format, super_minor);
3240 goto abort_free;
3242 if (err < 0) {
3243 printk(KERN_WARNING
3244 "md: could not read %s's sb, not importing!\n",
3245 bdevname(rdev->bdev,b));
3246 goto abort_free;
3250 return rdev;
3252 abort_free:
3253 if (rdev->bdev)
3254 unlock_rdev(rdev);
3255 md_rdev_clear(rdev);
3256 kfree(rdev);
3257 return ERR_PTR(err);
3261 * Check a full RAID array for plausibility
3264 static void analyze_sbs(struct mddev *mddev)
3266 int i;
3267 struct md_rdev *rdev, *freshest, *tmp;
3268 char b[BDEVNAME_SIZE];
3270 freshest = NULL;
3271 rdev_for_each_safe(rdev, tmp, mddev)
3272 switch (super_types[mddev->major_version].
3273 load_super(rdev, freshest, mddev->minor_version)) {
3274 case 1:
3275 freshest = rdev;
3276 break;
3277 case 0:
3278 break;
3279 default:
3280 printk( KERN_ERR \
3281 "md: fatal superblock inconsistency in %s"
3282 " -- removing from array\n",
3283 bdevname(rdev->bdev,b));
3284 md_kick_rdev_from_array(rdev);
3287 super_types[mddev->major_version].
3288 validate_super(mddev, freshest);
3290 i = 0;
3291 rdev_for_each_safe(rdev, tmp, mddev) {
3292 if (mddev->max_disks &&
3293 (rdev->desc_nr >= mddev->max_disks ||
3294 i > mddev->max_disks)) {
3295 printk(KERN_WARNING
3296 "md: %s: %s: only %d devices permitted\n",
3297 mdname(mddev), bdevname(rdev->bdev, b),
3298 mddev->max_disks);
3299 md_kick_rdev_from_array(rdev);
3300 continue;
3302 if (rdev != freshest) {
3303 if (super_types[mddev->major_version].
3304 validate_super(mddev, rdev)) {
3305 printk(KERN_WARNING "md: kicking non-fresh %s"
3306 " from array!\n",
3307 bdevname(rdev->bdev,b));
3308 md_kick_rdev_from_array(rdev);
3309 continue;
3312 if (mddev->level == LEVEL_MULTIPATH) {
3313 rdev->desc_nr = i++;
3314 rdev->raid_disk = rdev->desc_nr;
3315 set_bit(In_sync, &rdev->flags);
3316 } else if (rdev->raid_disk >=
3317 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3318 !test_bit(Journal, &rdev->flags)) {
3319 rdev->raid_disk = -1;
3320 clear_bit(In_sync, &rdev->flags);
3325 /* Read a fixed-point number.
3326 * Numbers in sysfs attributes should be in "standard" units where
3327 * possible, so time should be in seconds.
3328 * However we internally use a a much smaller unit such as
3329 * milliseconds or jiffies.
3330 * This function takes a decimal number with a possible fractional
3331 * component, and produces an integer which is the result of
3332 * multiplying that number by 10^'scale'.
3333 * all without any floating-point arithmetic.
3335 int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3337 unsigned long result = 0;
3338 long decimals = -1;
3339 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3340 if (*cp == '.')
3341 decimals = 0;
3342 else if (decimals < scale) {
3343 unsigned int value;
3344 value = *cp - '0';
3345 result = result * 10 + value;
3346 if (decimals >= 0)
3347 decimals++;
3349 cp++;
3351 if (*cp == '\n')
3352 cp++;
3353 if (*cp)
3354 return -EINVAL;
3355 if (decimals < 0)
3356 decimals = 0;
3357 while (decimals < scale) {
3358 result *= 10;
3359 decimals ++;
3361 *res = result;
3362 return 0;
3365 static ssize_t
3366 safe_delay_show(struct mddev *mddev, char *page)
3368 int msec = (mddev->safemode_delay*1000)/HZ;
3369 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3371 static ssize_t
3372 safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
3374 unsigned long msec;
3376 if (mddev_is_clustered(mddev)) {
3377 pr_info("md: Safemode is disabled for clustered mode\n");
3378 return -EINVAL;
3381 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
3382 return -EINVAL;
3383 if (msec == 0)
3384 mddev->safemode_delay = 0;
3385 else {
3386 unsigned long old_delay = mddev->safemode_delay;
3387 unsigned long new_delay = (msec*HZ)/1000;
3389 if (new_delay == 0)
3390 new_delay = 1;
3391 mddev->safemode_delay = new_delay;
3392 if (new_delay < old_delay || old_delay == 0)
3393 mod_timer(&mddev->safemode_timer, jiffies+1);
3395 return len;
3397 static struct md_sysfs_entry md_safe_delay =
3398 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
3400 static ssize_t
3401 level_show(struct mddev *mddev, char *page)
3403 struct md_personality *p;
3404 int ret;
3405 spin_lock(&mddev->lock);
3406 p = mddev->pers;
3407 if (p)
3408 ret = sprintf(page, "%s\n", p->name);
3409 else if (mddev->clevel[0])
3410 ret = sprintf(page, "%s\n", mddev->clevel);
3411 else if (mddev->level != LEVEL_NONE)
3412 ret = sprintf(page, "%d\n", mddev->level);
3413 else
3414 ret = 0;
3415 spin_unlock(&mddev->lock);
3416 return ret;
3419 static ssize_t
3420 level_store(struct mddev *mddev, const char *buf, size_t len)
3422 char clevel[16];
3423 ssize_t rv;
3424 size_t slen = len;
3425 struct md_personality *pers, *oldpers;
3426 long level;
3427 void *priv, *oldpriv;
3428 struct md_rdev *rdev;
3430 if (slen == 0 || slen >= sizeof(clevel))
3431 return -EINVAL;
3433 rv = mddev_lock(mddev);
3434 if (rv)
3435 return rv;
3437 if (mddev->pers == NULL) {
3438 strncpy(mddev->clevel, buf, slen);
3439 if (mddev->clevel[slen-1] == '\n')
3440 slen--;
3441 mddev->clevel[slen] = 0;
3442 mddev->level = LEVEL_NONE;
3443 rv = len;
3444 goto out_unlock;
3446 rv = -EROFS;
3447 if (mddev->ro)
3448 goto out_unlock;
3450 /* request to change the personality. Need to ensure:
3451 * - array is not engaged in resync/recovery/reshape
3452 * - old personality can be suspended
3453 * - new personality will access other array.
3456 rv = -EBUSY;
3457 if (mddev->sync_thread ||
3458 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3459 mddev->reshape_position != MaxSector ||
3460 mddev->sysfs_active)
3461 goto out_unlock;
3463 rv = -EINVAL;
3464 if (!mddev->pers->quiesce) {
3465 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3466 mdname(mddev), mddev->pers->name);
3467 goto out_unlock;
3470 /* Now find the new personality */
3471 strncpy(clevel, buf, slen);
3472 if (clevel[slen-1] == '\n')
3473 slen--;
3474 clevel[slen] = 0;
3475 if (kstrtol(clevel, 10, &level))
3476 level = LEVEL_NONE;
3478 if (request_module("md-%s", clevel) != 0)
3479 request_module("md-level-%s", clevel);
3480 spin_lock(&pers_lock);
3481 pers = find_pers(level, clevel);
3482 if (!pers || !try_module_get(pers->owner)) {
3483 spin_unlock(&pers_lock);
3484 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
3485 rv = -EINVAL;
3486 goto out_unlock;
3488 spin_unlock(&pers_lock);
3490 if (pers == mddev->pers) {
3491 /* Nothing to do! */
3492 module_put(pers->owner);
3493 rv = len;
3494 goto out_unlock;
3496 if (!pers->takeover) {
3497 module_put(pers->owner);
3498 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
3499 mdname(mddev), clevel);
3500 rv = -EINVAL;
3501 goto out_unlock;
3504 rdev_for_each(rdev, mddev)
3505 rdev->new_raid_disk = rdev->raid_disk;
3507 /* ->takeover must set new_* and/or delta_disks
3508 * if it succeeds, and may set them when it fails.
3510 priv = pers->takeover(mddev);
3511 if (IS_ERR(priv)) {
3512 mddev->new_level = mddev->level;
3513 mddev->new_layout = mddev->layout;
3514 mddev->new_chunk_sectors = mddev->chunk_sectors;
3515 mddev->raid_disks -= mddev->delta_disks;
3516 mddev->delta_disks = 0;
3517 mddev->reshape_backwards = 0;
3518 module_put(pers->owner);
3519 printk(KERN_WARNING "md: %s: %s would not accept array\n",
3520 mdname(mddev), clevel);
3521 rv = PTR_ERR(priv);
3522 goto out_unlock;
3525 /* Looks like we have a winner */
3526 mddev_suspend(mddev);
3527 mddev_detach(mddev);
3529 spin_lock(&mddev->lock);
3530 oldpers = mddev->pers;
3531 oldpriv = mddev->private;
3532 mddev->pers = pers;
3533 mddev->private = priv;
3534 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3535 mddev->level = mddev->new_level;
3536 mddev->layout = mddev->new_layout;
3537 mddev->chunk_sectors = mddev->new_chunk_sectors;
3538 mddev->delta_disks = 0;
3539 mddev->reshape_backwards = 0;
3540 mddev->degraded = 0;
3541 spin_unlock(&mddev->lock);
3543 if (oldpers->sync_request == NULL &&
3544 mddev->external) {
3545 /* We are converting from a no-redundancy array
3546 * to a redundancy array and metadata is managed
3547 * externally so we need to be sure that writes
3548 * won't block due to a need to transition
3549 * clean->dirty
3550 * until external management is started.
3552 mddev->in_sync = 0;
3553 mddev->safemode_delay = 0;
3554 mddev->safemode = 0;
3557 oldpers->free(mddev, oldpriv);
3559 if (oldpers->sync_request == NULL &&
3560 pers->sync_request != NULL) {
3561 /* need to add the md_redundancy_group */
3562 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3563 printk(KERN_WARNING
3564 "md: cannot register extra attributes for %s\n",
3565 mdname(mddev));
3566 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3568 if (oldpers->sync_request != NULL &&
3569 pers->sync_request == NULL) {
3570 /* need to remove the md_redundancy_group */
3571 if (mddev->to_remove == NULL)
3572 mddev->to_remove = &md_redundancy_group;
3575 rdev_for_each(rdev, mddev) {
3576 if (rdev->raid_disk < 0)
3577 continue;
3578 if (rdev->new_raid_disk >= mddev->raid_disks)
3579 rdev->new_raid_disk = -1;
3580 if (rdev->new_raid_disk == rdev->raid_disk)
3581 continue;
3582 sysfs_unlink_rdev(mddev, rdev);
3584 rdev_for_each(rdev, mddev) {
3585 if (rdev->raid_disk < 0)
3586 continue;
3587 if (rdev->new_raid_disk == rdev->raid_disk)
3588 continue;
3589 rdev->raid_disk = rdev->new_raid_disk;
3590 if (rdev->raid_disk < 0)
3591 clear_bit(In_sync, &rdev->flags);
3592 else {
3593 if (sysfs_link_rdev(mddev, rdev))
3594 printk(KERN_WARNING "md: cannot register rd%d"
3595 " for %s after level change\n",
3596 rdev->raid_disk, mdname(mddev));
3600 if (pers->sync_request == NULL) {
3601 /* this is now an array without redundancy, so
3602 * it must always be in_sync
3604 mddev->in_sync = 1;
3605 del_timer_sync(&mddev->safemode_timer);
3607 blk_set_stacking_limits(&mddev->queue->limits);
3608 pers->run(mddev);
3609 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3610 mddev_resume(mddev);
3611 if (!mddev->thread)
3612 md_update_sb(mddev, 1);
3613 sysfs_notify(&mddev->kobj, NULL, "level");
3614 md_new_event(mddev);
3615 rv = len;
3616 out_unlock:
3617 mddev_unlock(mddev);
3618 return rv;
3621 static struct md_sysfs_entry md_level =
3622 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
3624 static ssize_t
3625 layout_show(struct mddev *mddev, char *page)
3627 /* just a number, not meaningful for all levels */
3628 if (mddev->reshape_position != MaxSector &&
3629 mddev->layout != mddev->new_layout)
3630 return sprintf(page, "%d (%d)\n",
3631 mddev->new_layout, mddev->layout);
3632 return sprintf(page, "%d\n", mddev->layout);
3635 static ssize_t
3636 layout_store(struct mddev *mddev, const char *buf, size_t len)
3638 unsigned int n;
3639 int err;
3641 err = kstrtouint(buf, 10, &n);
3642 if (err < 0)
3643 return err;
3644 err = mddev_lock(mddev);
3645 if (err)
3646 return err;
3648 if (mddev->pers) {
3649 if (mddev->pers->check_reshape == NULL)
3650 err = -EBUSY;
3651 else if (mddev->ro)
3652 err = -EROFS;
3653 else {
3654 mddev->new_layout = n;
3655 err = mddev->pers->check_reshape(mddev);
3656 if (err)
3657 mddev->new_layout = mddev->layout;
3659 } else {
3660 mddev->new_layout = n;
3661 if (mddev->reshape_position == MaxSector)
3662 mddev->layout = n;
3664 mddev_unlock(mddev);
3665 return err ?: len;
3667 static struct md_sysfs_entry md_layout =
3668 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
3670 static ssize_t
3671 raid_disks_show(struct mddev *mddev, char *page)
3673 if (mddev->raid_disks == 0)
3674 return 0;
3675 if (mddev->reshape_position != MaxSector &&
3676 mddev->delta_disks != 0)
3677 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3678 mddev->raid_disks - mddev->delta_disks);
3679 return sprintf(page, "%d\n", mddev->raid_disks);
3682 static int update_raid_disks(struct mddev *mddev, int raid_disks);
3684 static ssize_t
3685 raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
3687 unsigned int n;
3688 int err;
3690 err = kstrtouint(buf, 10, &n);
3691 if (err < 0)
3692 return err;
3694 err = mddev_lock(mddev);
3695 if (err)
3696 return err;
3697 if (mddev->pers)
3698 err = update_raid_disks(mddev, n);
3699 else if (mddev->reshape_position != MaxSector) {
3700 struct md_rdev *rdev;
3701 int olddisks = mddev->raid_disks - mddev->delta_disks;
3703 err = -EINVAL;
3704 rdev_for_each(rdev, mddev) {
3705 if (olddisks < n &&
3706 rdev->data_offset < rdev->new_data_offset)
3707 goto out_unlock;
3708 if (olddisks > n &&
3709 rdev->data_offset > rdev->new_data_offset)
3710 goto out_unlock;
3712 err = 0;
3713 mddev->delta_disks = n - olddisks;
3714 mddev->raid_disks = n;
3715 mddev->reshape_backwards = (mddev->delta_disks < 0);
3716 } else
3717 mddev->raid_disks = n;
3718 out_unlock:
3719 mddev_unlock(mddev);
3720 return err ? err : len;
3722 static struct md_sysfs_entry md_raid_disks =
3723 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
3725 static ssize_t
3726 chunk_size_show(struct mddev *mddev, char *page)
3728 if (mddev->reshape_position != MaxSector &&
3729 mddev->chunk_sectors != mddev->new_chunk_sectors)
3730 return sprintf(page, "%d (%d)\n",
3731 mddev->new_chunk_sectors << 9,
3732 mddev->chunk_sectors << 9);
3733 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
3736 static ssize_t
3737 chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
3739 unsigned long n;
3740 int err;
3742 err = kstrtoul(buf, 10, &n);
3743 if (err < 0)
3744 return err;
3746 err = mddev_lock(mddev);
3747 if (err)
3748 return err;
3749 if (mddev->pers) {
3750 if (mddev->pers->check_reshape == NULL)
3751 err = -EBUSY;
3752 else if (mddev->ro)
3753 err = -EROFS;
3754 else {
3755 mddev->new_chunk_sectors = n >> 9;
3756 err = mddev->pers->check_reshape(mddev);
3757 if (err)
3758 mddev->new_chunk_sectors = mddev->chunk_sectors;
3760 } else {
3761 mddev->new_chunk_sectors = n >> 9;
3762 if (mddev->reshape_position == MaxSector)
3763 mddev->chunk_sectors = n >> 9;
3765 mddev_unlock(mddev);
3766 return err ?: len;
3768 static struct md_sysfs_entry md_chunk_size =
3769 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
3771 static ssize_t
3772 resync_start_show(struct mddev *mddev, char *page)
3774 if (mddev->recovery_cp == MaxSector)
3775 return sprintf(page, "none\n");
3776 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3779 static ssize_t
3780 resync_start_store(struct mddev *mddev, const char *buf, size_t len)
3782 unsigned long long n;
3783 int err;
3785 if (cmd_match(buf, "none"))
3786 n = MaxSector;
3787 else {
3788 err = kstrtoull(buf, 10, &n);
3789 if (err < 0)
3790 return err;
3791 if (n != (sector_t)n)
3792 return -EINVAL;
3795 err = mddev_lock(mddev);
3796 if (err)
3797 return err;
3798 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3799 err = -EBUSY;
3801 if (!err) {
3802 mddev->recovery_cp = n;
3803 if (mddev->pers)
3804 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3806 mddev_unlock(mddev);
3807 return err ?: len;
3809 static struct md_sysfs_entry md_resync_start =
3810 __ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
3811 resync_start_show, resync_start_store);
3814 * The array state can be:
3816 * clear
3817 * No devices, no size, no level
3818 * Equivalent to STOP_ARRAY ioctl
3819 * inactive
3820 * May have some settings, but array is not active
3821 * all IO results in error
3822 * When written, doesn't tear down array, but just stops it
3823 * suspended (not supported yet)
3824 * All IO requests will block. The array can be reconfigured.
3825 * Writing this, if accepted, will block until array is quiescent
3826 * readonly
3827 * no resync can happen. no superblocks get written.
3828 * write requests fail
3829 * read-auto
3830 * like readonly, but behaves like 'clean' on a write request.
3832 * clean - no pending writes, but otherwise active.
3833 * When written to inactive array, starts without resync
3834 * If a write request arrives then
3835 * if metadata is known, mark 'dirty' and switch to 'active'.
3836 * if not known, block and switch to write-pending
3837 * If written to an active array that has pending writes, then fails.
3838 * active
3839 * fully active: IO and resync can be happening.
3840 * When written to inactive array, starts with resync
3842 * write-pending
3843 * clean, but writes are blocked waiting for 'active' to be written.
3845 * active-idle
3846 * like active, but no writes have been seen for a while (100msec).
3849 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3850 write_pending, active_idle, bad_word};
3851 static char *array_states[] = {
3852 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3853 "write-pending", "active-idle", NULL };
3855 static int match_word(const char *word, char **list)
3857 int n;
3858 for (n=0; list[n]; n++)
3859 if (cmd_match(word, list[n]))
3860 break;
3861 return n;
3864 static ssize_t
3865 array_state_show(struct mddev *mddev, char *page)
3867 enum array_state st = inactive;
3869 if (mddev->pers)
3870 switch(mddev->ro) {
3871 case 1:
3872 st = readonly;
3873 break;
3874 case 2:
3875 st = read_auto;
3876 break;
3877 case 0:
3878 if (mddev->in_sync)
3879 st = clean;
3880 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
3881 st = write_pending;
3882 else if (mddev->safemode)
3883 st = active_idle;
3884 else
3885 st = active;
3887 else {
3888 if (list_empty(&mddev->disks) &&
3889 mddev->raid_disks == 0 &&
3890 mddev->dev_sectors == 0)
3891 st = clear;
3892 else
3893 st = inactive;
3895 return sprintf(page, "%s\n", array_states[st]);
3898 static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
3899 static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
3900 static int do_md_run(struct mddev *mddev);
3901 static int restart_array(struct mddev *mddev);
3903 static ssize_t
3904 array_state_store(struct mddev *mddev, const char *buf, size_t len)
3906 int err;
3907 enum array_state st = match_word(buf, array_states);
3909 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
3910 /* don't take reconfig_mutex when toggling between
3911 * clean and active
3913 spin_lock(&mddev->lock);
3914 if (st == active) {
3915 restart_array(mddev);
3916 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
3917 wake_up(&mddev->sb_wait);
3918 err = 0;
3919 } else /* st == clean */ {
3920 restart_array(mddev);
3921 if (atomic_read(&mddev->writes_pending) == 0) {
3922 if (mddev->in_sync == 0) {
3923 mddev->in_sync = 1;
3924 if (mddev->safemode == 1)
3925 mddev->safemode = 0;
3926 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3928 err = 0;
3929 } else
3930 err = -EBUSY;
3932 spin_unlock(&mddev->lock);
3933 return err ?: len;
3935 err = mddev_lock(mddev);
3936 if (err)
3937 return err;
3938 err = -EINVAL;
3939 switch(st) {
3940 case bad_word:
3941 break;
3942 case clear:
3943 /* stopping an active array */
3944 err = do_md_stop(mddev, 0, NULL);
3945 break;
3946 case inactive:
3947 /* stopping an active array */
3948 if (mddev->pers)
3949 err = do_md_stop(mddev, 2, NULL);
3950 else
3951 err = 0; /* already inactive */
3952 break;
3953 case suspended:
3954 break; /* not supported yet */
3955 case readonly:
3956 if (mddev->pers)
3957 err = md_set_readonly(mddev, NULL);
3958 else {
3959 mddev->ro = 1;
3960 set_disk_ro(mddev->gendisk, 1);
3961 err = do_md_run(mddev);
3963 break;
3964 case read_auto:
3965 if (mddev->pers) {
3966 if (mddev->ro == 0)
3967 err = md_set_readonly(mddev, NULL);
3968 else if (mddev->ro == 1)
3969 err = restart_array(mddev);
3970 if (err == 0) {
3971 mddev->ro = 2;
3972 set_disk_ro(mddev->gendisk, 0);
3974 } else {
3975 mddev->ro = 2;
3976 err = do_md_run(mddev);
3978 break;
3979 case clean:
3980 if (mddev->pers) {
3981 err = restart_array(mddev);
3982 if (err)
3983 break;
3984 spin_lock(&mddev->lock);
3985 if (atomic_read(&mddev->writes_pending) == 0) {
3986 if (mddev->in_sync == 0) {
3987 mddev->in_sync = 1;
3988 if (mddev->safemode == 1)
3989 mddev->safemode = 0;
3990 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3992 err = 0;
3993 } else
3994 err = -EBUSY;
3995 spin_unlock(&mddev->lock);
3996 } else
3997 err = -EINVAL;
3998 break;
3999 case active:
4000 if (mddev->pers) {
4001 err = restart_array(mddev);
4002 if (err)
4003 break;
4004 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
4005 wake_up(&mddev->sb_wait);
4006 err = 0;
4007 } else {
4008 mddev->ro = 0;
4009 set_disk_ro(mddev->gendisk, 0);
4010 err = do_md_run(mddev);
4012 break;
4013 case write_pending:
4014 case active_idle:
4015 /* these cannot be set */
4016 break;
4019 if (!err) {
4020 if (mddev->hold_active == UNTIL_IOCTL)
4021 mddev->hold_active = 0;
4022 sysfs_notify_dirent_safe(mddev->sysfs_state);
4024 mddev_unlock(mddev);
4025 return err ?: len;
4027 static struct md_sysfs_entry md_array_state =
4028 __ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
4030 static ssize_t
4031 max_corrected_read_errors_show(struct mddev *mddev, char *page) {
4032 return sprintf(page, "%d\n",
4033 atomic_read(&mddev->max_corr_read_errors));
4036 static ssize_t
4037 max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
4039 unsigned int n;
4040 int rv;
4042 rv = kstrtouint(buf, 10, &n);
4043 if (rv < 0)
4044 return rv;
4045 atomic_set(&mddev->max_corr_read_errors, n);
4046 return len;
4049 static struct md_sysfs_entry max_corr_read_errors =
4050 __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4051 max_corrected_read_errors_store);
4053 static ssize_t
4054 null_show(struct mddev *mddev, char *page)
4056 return -EINVAL;
4059 static ssize_t
4060 new_dev_store(struct mddev *mddev, const char *buf, size_t len)
4062 /* buf must be %d:%d\n? giving major and minor numbers */
4063 /* The new device is added to the array.
4064 * If the array has a persistent superblock, we read the
4065 * superblock to initialise info and check validity.
4066 * Otherwise, only checking done is that in bind_rdev_to_array,
4067 * which mainly checks size.
4069 char *e;
4070 int major = simple_strtoul(buf, &e, 10);
4071 int minor;
4072 dev_t dev;
4073 struct md_rdev *rdev;
4074 int err;
4076 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4077 return -EINVAL;
4078 minor = simple_strtoul(e+1, &e, 10);
4079 if (*e && *e != '\n')
4080 return -EINVAL;
4081 dev = MKDEV(major, minor);
4082 if (major != MAJOR(dev) ||
4083 minor != MINOR(dev))
4084 return -EOVERFLOW;
4086 flush_workqueue(md_misc_wq);
4088 err = mddev_lock(mddev);
4089 if (err)
4090 return err;
4091 if (mddev->persistent) {
4092 rdev = md_import_device(dev, mddev->major_version,
4093 mddev->minor_version);
4094 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
4095 struct md_rdev *rdev0
4096 = list_entry(mddev->disks.next,
4097 struct md_rdev, same_set);
4098 err = super_types[mddev->major_version]
4099 .load_super(rdev, rdev0, mddev->minor_version);
4100 if (err < 0)
4101 goto out;
4103 } else if (mddev->external)
4104 rdev = md_import_device(dev, -2, -1);
4105 else
4106 rdev = md_import_device(dev, -1, -1);
4108 if (IS_ERR(rdev)) {
4109 mddev_unlock(mddev);
4110 return PTR_ERR(rdev);
4112 err = bind_rdev_to_array(rdev, mddev);
4113 out:
4114 if (err)
4115 export_rdev(rdev);
4116 mddev_unlock(mddev);
4117 return err ? err : len;
4120 static struct md_sysfs_entry md_new_device =
4121 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
4123 static ssize_t
4124 bitmap_store(struct mddev *mddev, const char *buf, size_t len)
4126 char *end;
4127 unsigned long chunk, end_chunk;
4128 int err;
4130 err = mddev_lock(mddev);
4131 if (err)
4132 return err;
4133 if (!mddev->bitmap)
4134 goto out;
4135 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4136 while (*buf) {
4137 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4138 if (buf == end) break;
4139 if (*end == '-') { /* range */
4140 buf = end + 1;
4141 end_chunk = simple_strtoul(buf, &end, 0);
4142 if (buf == end) break;
4144 if (*end && !isspace(*end)) break;
4145 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
4146 buf = skip_spaces(end);
4148 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4149 out:
4150 mddev_unlock(mddev);
4151 return len;
4154 static struct md_sysfs_entry md_bitmap =
4155 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4157 static ssize_t
4158 size_show(struct mddev *mddev, char *page)
4160 return sprintf(page, "%llu\n",
4161 (unsigned long long)mddev->dev_sectors / 2);
4164 static int update_size(struct mddev *mddev, sector_t num_sectors);
4166 static ssize_t
4167 size_store(struct mddev *mddev, const char *buf, size_t len)
4169 /* If array is inactive, we can reduce the component size, but
4170 * not increase it (except from 0).
4171 * If array is active, we can try an on-line resize
4173 sector_t sectors;
4174 int err = strict_blocks_to_sectors(buf, &sectors);
4176 if (err < 0)
4177 return err;
4178 err = mddev_lock(mddev);
4179 if (err)
4180 return err;
4181 if (mddev->pers) {
4182 err = update_size(mddev, sectors);
4183 md_update_sb(mddev, 1);
4184 } else {
4185 if (mddev->dev_sectors == 0 ||
4186 mddev->dev_sectors > sectors)
4187 mddev->dev_sectors = sectors;
4188 else
4189 err = -ENOSPC;
4191 mddev_unlock(mddev);
4192 return err ? err : len;
4195 static struct md_sysfs_entry md_size =
4196 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
4198 /* Metadata version.
4199 * This is one of
4200 * 'none' for arrays with no metadata (good luck...)
4201 * 'external' for arrays with externally managed metadata,
4202 * or N.M for internally known formats
4204 static ssize_t
4205 metadata_show(struct mddev *mddev, char *page)
4207 if (mddev->persistent)
4208 return sprintf(page, "%d.%d\n",
4209 mddev->major_version, mddev->minor_version);
4210 else if (mddev->external)
4211 return sprintf(page, "external:%s\n", mddev->metadata_type);
4212 else
4213 return sprintf(page, "none\n");
4216 static ssize_t
4217 metadata_store(struct mddev *mddev, const char *buf, size_t len)
4219 int major, minor;
4220 char *e;
4221 int err;
4222 /* Changing the details of 'external' metadata is
4223 * always permitted. Otherwise there must be
4224 * no devices attached to the array.
4227 err = mddev_lock(mddev);
4228 if (err)
4229 return err;
4230 err = -EBUSY;
4231 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4233 else if (!list_empty(&mddev->disks))
4234 goto out_unlock;
4236 err = 0;
4237 if (cmd_match(buf, "none")) {
4238 mddev->persistent = 0;
4239 mddev->external = 0;
4240 mddev->major_version = 0;
4241 mddev->minor_version = 90;
4242 goto out_unlock;
4244 if (strncmp(buf, "external:", 9) == 0) {
4245 size_t namelen = len-9;
4246 if (namelen >= sizeof(mddev->metadata_type))
4247 namelen = sizeof(mddev->metadata_type)-1;
4248 strncpy(mddev->metadata_type, buf+9, namelen);
4249 mddev->metadata_type[namelen] = 0;
4250 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4251 mddev->metadata_type[--namelen] = 0;
4252 mddev->persistent = 0;
4253 mddev->external = 1;
4254 mddev->major_version = 0;
4255 mddev->minor_version = 90;
4256 goto out_unlock;
4258 major = simple_strtoul(buf, &e, 10);
4259 err = -EINVAL;
4260 if (e==buf || *e != '.')
4261 goto out_unlock;
4262 buf = e+1;
4263 minor = simple_strtoul(buf, &e, 10);
4264 if (e==buf || (*e && *e != '\n') )
4265 goto out_unlock;
4266 err = -ENOENT;
4267 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
4268 goto out_unlock;
4269 mddev->major_version = major;
4270 mddev->minor_version = minor;
4271 mddev->persistent = 1;
4272 mddev->external = 0;
4273 err = 0;
4274 out_unlock:
4275 mddev_unlock(mddev);
4276 return err ?: len;
4279 static struct md_sysfs_entry md_metadata =
4280 __ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
4282 static ssize_t
4283 action_show(struct mddev *mddev, char *page)
4285 char *type = "idle";
4286 unsigned long recovery = mddev->recovery;
4287 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
4288 type = "frozen";
4289 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4290 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4291 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
4292 type = "reshape";
4293 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4294 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
4295 type = "resync";
4296 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
4297 type = "check";
4298 else
4299 type = "repair";
4300 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
4301 type = "recover";
4302 else if (mddev->reshape_position != MaxSector)
4303 type = "reshape";
4305 return sprintf(page, "%s\n", type);
4308 static ssize_t
4309 action_store(struct mddev *mddev, const char *page, size_t len)
4311 if (!mddev->pers || !mddev->pers->sync_request)
4312 return -EINVAL;
4315 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
4316 if (cmd_match(page, "frozen"))
4317 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4318 else
4319 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4320 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4321 mddev_lock(mddev) == 0) {
4322 flush_workqueue(md_misc_wq);
4323 if (mddev->sync_thread) {
4324 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4325 md_reap_sync_thread(mddev);
4327 mddev_unlock(mddev);
4329 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4330 return -EBUSY;
4331 else if (cmd_match(page, "resync"))
4332 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4333 else if (cmd_match(page, "recover")) {
4334 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4335 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4336 } else if (cmd_match(page, "reshape")) {
4337 int err;
4338 if (mddev->pers->start_reshape == NULL)
4339 return -EINVAL;
4340 err = mddev_lock(mddev);
4341 if (!err) {
4342 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4343 err = -EBUSY;
4344 else {
4345 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4346 err = mddev->pers->start_reshape(mddev);
4348 mddev_unlock(mddev);
4350 if (err)
4351 return err;
4352 sysfs_notify(&mddev->kobj, NULL, "degraded");
4353 } else {
4354 if (cmd_match(page, "check"))
4355 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4356 else if (!cmd_match(page, "repair"))
4357 return -EINVAL;
4358 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4359 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4360 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4362 if (mddev->ro == 2) {
4363 /* A write to sync_action is enough to justify
4364 * canceling read-auto mode
4366 mddev->ro = 0;
4367 md_wakeup_thread(mddev->sync_thread);
4369 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4370 md_wakeup_thread(mddev->thread);
4371 sysfs_notify_dirent_safe(mddev->sysfs_action);
4372 return len;
4375 static struct md_sysfs_entry md_scan_mode =
4376 __ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
4378 static ssize_t
4379 last_sync_action_show(struct mddev *mddev, char *page)
4381 return sprintf(page, "%s\n", mddev->last_sync_action);
4384 static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4386 static ssize_t
4387 mismatch_cnt_show(struct mddev *mddev, char *page)
4389 return sprintf(page, "%llu\n",
4390 (unsigned long long)
4391 atomic64_read(&mddev->resync_mismatches));
4394 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
4396 static ssize_t
4397 sync_min_show(struct mddev *mddev, char *page)
4399 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4400 mddev->sync_speed_min ? "local": "system");
4403 static ssize_t
4404 sync_min_store(struct mddev *mddev, const char *buf, size_t len)
4406 unsigned int min;
4407 int rv;
4409 if (strncmp(buf, "system", 6)==0) {
4410 min = 0;
4411 } else {
4412 rv = kstrtouint(buf, 10, &min);
4413 if (rv < 0)
4414 return rv;
4415 if (min == 0)
4416 return -EINVAL;
4418 mddev->sync_speed_min = min;
4419 return len;
4422 static struct md_sysfs_entry md_sync_min =
4423 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4425 static ssize_t
4426 sync_max_show(struct mddev *mddev, char *page)
4428 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4429 mddev->sync_speed_max ? "local": "system");
4432 static ssize_t
4433 sync_max_store(struct mddev *mddev, const char *buf, size_t len)
4435 unsigned int max;
4436 int rv;
4438 if (strncmp(buf, "system", 6)==0) {
4439 max = 0;
4440 } else {
4441 rv = kstrtouint(buf, 10, &max);
4442 if (rv < 0)
4443 return rv;
4444 if (max == 0)
4445 return -EINVAL;
4447 mddev->sync_speed_max = max;
4448 return len;
4451 static struct md_sysfs_entry md_sync_max =
4452 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4454 static ssize_t
4455 degraded_show(struct mddev *mddev, char *page)
4457 return sprintf(page, "%d\n", mddev->degraded);
4459 static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
4461 static ssize_t
4462 sync_force_parallel_show(struct mddev *mddev, char *page)
4464 return sprintf(page, "%d\n", mddev->parallel_resync);
4467 static ssize_t
4468 sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
4470 long n;
4472 if (kstrtol(buf, 10, &n))
4473 return -EINVAL;
4475 if (n != 0 && n != 1)
4476 return -EINVAL;
4478 mddev->parallel_resync = n;
4480 if (mddev->sync_thread)
4481 wake_up(&resync_wait);
4483 return len;
4486 /* force parallel resync, even with shared block devices */
4487 static struct md_sysfs_entry md_sync_force_parallel =
4488 __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4489 sync_force_parallel_show, sync_force_parallel_store);
4491 static ssize_t
4492 sync_speed_show(struct mddev *mddev, char *page)
4494 unsigned long resync, dt, db;
4495 if (mddev->curr_resync == 0)
4496 return sprintf(page, "none\n");
4497 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4498 dt = (jiffies - mddev->resync_mark) / HZ;
4499 if (!dt) dt++;
4500 db = resync - mddev->resync_mark_cnt;
4501 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
4504 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
4506 static ssize_t
4507 sync_completed_show(struct mddev *mddev, char *page)
4509 unsigned long long max_sectors, resync;
4511 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4512 return sprintf(page, "none\n");
4514 if (mddev->curr_resync == 1 ||
4515 mddev->curr_resync == 2)
4516 return sprintf(page, "delayed\n");
4518 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4519 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4520 max_sectors = mddev->resync_max_sectors;
4521 else
4522 max_sectors = mddev->dev_sectors;
4524 resync = mddev->curr_resync_completed;
4525 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
4528 static struct md_sysfs_entry md_sync_completed =
4529 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
4531 static ssize_t
4532 min_sync_show(struct mddev *mddev, char *page)
4534 return sprintf(page, "%llu\n",
4535 (unsigned long long)mddev->resync_min);
4537 static ssize_t
4538 min_sync_store(struct mddev *mddev, const char *buf, size_t len)
4540 unsigned long long min;
4541 int err;
4543 if (kstrtoull(buf, 10, &min))
4544 return -EINVAL;
4546 spin_lock(&mddev->lock);
4547 err = -EINVAL;
4548 if (min > mddev->resync_max)
4549 goto out_unlock;
4551 err = -EBUSY;
4552 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4553 goto out_unlock;
4555 /* Round down to multiple of 4K for safety */
4556 mddev->resync_min = round_down(min, 8);
4557 err = 0;
4559 out_unlock:
4560 spin_unlock(&mddev->lock);
4561 return err ?: len;
4564 static struct md_sysfs_entry md_min_sync =
4565 __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4567 static ssize_t
4568 max_sync_show(struct mddev *mddev, char *page)
4570 if (mddev->resync_max == MaxSector)
4571 return sprintf(page, "max\n");
4572 else
4573 return sprintf(page, "%llu\n",
4574 (unsigned long long)mddev->resync_max);
4576 static ssize_t
4577 max_sync_store(struct mddev *mddev, const char *buf, size_t len)
4579 int err;
4580 spin_lock(&mddev->lock);
4581 if (strncmp(buf, "max", 3) == 0)
4582 mddev->resync_max = MaxSector;
4583 else {
4584 unsigned long long max;
4585 int chunk;
4587 err = -EINVAL;
4588 if (kstrtoull(buf, 10, &max))
4589 goto out_unlock;
4590 if (max < mddev->resync_min)
4591 goto out_unlock;
4593 err = -EBUSY;
4594 if (max < mddev->resync_max &&
4595 mddev->ro == 0 &&
4596 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4597 goto out_unlock;
4599 /* Must be a multiple of chunk_size */
4600 chunk = mddev->chunk_sectors;
4601 if (chunk) {
4602 sector_t temp = max;
4604 err = -EINVAL;
4605 if (sector_div(temp, chunk))
4606 goto out_unlock;
4608 mddev->resync_max = max;
4610 wake_up(&mddev->recovery_wait);
4611 err = 0;
4612 out_unlock:
4613 spin_unlock(&mddev->lock);
4614 return err ?: len;
4617 static struct md_sysfs_entry md_max_sync =
4618 __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4620 static ssize_t
4621 suspend_lo_show(struct mddev *mddev, char *page)
4623 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4626 static ssize_t
4627 suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
4629 unsigned long long old, new;
4630 int err;
4632 err = kstrtoull(buf, 10, &new);
4633 if (err < 0)
4634 return err;
4635 if (new != (sector_t)new)
4636 return -EINVAL;
4638 err = mddev_lock(mddev);
4639 if (err)
4640 return err;
4641 err = -EINVAL;
4642 if (mddev->pers == NULL ||
4643 mddev->pers->quiesce == NULL)
4644 goto unlock;
4645 old = mddev->suspend_lo;
4646 mddev->suspend_lo = new;
4647 if (new >= old)
4648 /* Shrinking suspended region */
4649 mddev->pers->quiesce(mddev, 2);
4650 else {
4651 /* Expanding suspended region - need to wait */
4652 mddev->pers->quiesce(mddev, 1);
4653 mddev->pers->quiesce(mddev, 0);
4655 err = 0;
4656 unlock:
4657 mddev_unlock(mddev);
4658 return err ?: len;
4660 static struct md_sysfs_entry md_suspend_lo =
4661 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4663 static ssize_t
4664 suspend_hi_show(struct mddev *mddev, char *page)
4666 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4669 static ssize_t
4670 suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
4672 unsigned long long old, new;
4673 int err;
4675 err = kstrtoull(buf, 10, &new);
4676 if (err < 0)
4677 return err;
4678 if (new != (sector_t)new)
4679 return -EINVAL;
4681 err = mddev_lock(mddev);
4682 if (err)
4683 return err;
4684 err = -EINVAL;
4685 if (mddev->pers == NULL ||
4686 mddev->pers->quiesce == NULL)
4687 goto unlock;
4688 old = mddev->suspend_hi;
4689 mddev->suspend_hi = new;
4690 if (new <= old)
4691 /* Shrinking suspended region */
4692 mddev->pers->quiesce(mddev, 2);
4693 else {
4694 /* Expanding suspended region - need to wait */
4695 mddev->pers->quiesce(mddev, 1);
4696 mddev->pers->quiesce(mddev, 0);
4698 err = 0;
4699 unlock:
4700 mddev_unlock(mddev);
4701 return err ?: len;
4703 static struct md_sysfs_entry md_suspend_hi =
4704 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4706 static ssize_t
4707 reshape_position_show(struct mddev *mddev, char *page)
4709 if (mddev->reshape_position != MaxSector)
4710 return sprintf(page, "%llu\n",
4711 (unsigned long long)mddev->reshape_position);
4712 strcpy(page, "none\n");
4713 return 5;
4716 static ssize_t
4717 reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
4719 struct md_rdev *rdev;
4720 unsigned long long new;
4721 int err;
4723 err = kstrtoull(buf, 10, &new);
4724 if (err < 0)
4725 return err;
4726 if (new != (sector_t)new)
4727 return -EINVAL;
4728 err = mddev_lock(mddev);
4729 if (err)
4730 return err;
4731 err = -EBUSY;
4732 if (mddev->pers)
4733 goto unlock;
4734 mddev->reshape_position = new;
4735 mddev->delta_disks = 0;
4736 mddev->reshape_backwards = 0;
4737 mddev->new_level = mddev->level;
4738 mddev->new_layout = mddev->layout;
4739 mddev->new_chunk_sectors = mddev->chunk_sectors;
4740 rdev_for_each(rdev, mddev)
4741 rdev->new_data_offset = rdev->data_offset;
4742 err = 0;
4743 unlock:
4744 mddev_unlock(mddev);
4745 return err ?: len;
4748 static struct md_sysfs_entry md_reshape_position =
4749 __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4750 reshape_position_store);
4752 static ssize_t
4753 reshape_direction_show(struct mddev *mddev, char *page)
4755 return sprintf(page, "%s\n",
4756 mddev->reshape_backwards ? "backwards" : "forwards");
4759 static ssize_t
4760 reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4762 int backwards = 0;
4763 int err;
4765 if (cmd_match(buf, "forwards"))
4766 backwards = 0;
4767 else if (cmd_match(buf, "backwards"))
4768 backwards = 1;
4769 else
4770 return -EINVAL;
4771 if (mddev->reshape_backwards == backwards)
4772 return len;
4774 err = mddev_lock(mddev);
4775 if (err)
4776 return err;
4777 /* check if we are allowed to change */
4778 if (mddev->delta_disks)
4779 err = -EBUSY;
4780 else if (mddev->persistent &&
4781 mddev->major_version == 0)
4782 err = -EINVAL;
4783 else
4784 mddev->reshape_backwards = backwards;
4785 mddev_unlock(mddev);
4786 return err ?: len;
4789 static struct md_sysfs_entry md_reshape_direction =
4790 __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4791 reshape_direction_store);
4793 static ssize_t
4794 array_size_show(struct mddev *mddev, char *page)
4796 if (mddev->external_size)
4797 return sprintf(page, "%llu\n",
4798 (unsigned long long)mddev->array_sectors/2);
4799 else
4800 return sprintf(page, "default\n");
4803 static ssize_t
4804 array_size_store(struct mddev *mddev, const char *buf, size_t len)
4806 sector_t sectors;
4807 int err;
4809 err = mddev_lock(mddev);
4810 if (err)
4811 return err;
4813 if (strncmp(buf, "default", 7) == 0) {
4814 if (mddev->pers)
4815 sectors = mddev->pers->size(mddev, 0, 0);
4816 else
4817 sectors = mddev->array_sectors;
4819 mddev->external_size = 0;
4820 } else {
4821 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4822 err = -EINVAL;
4823 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4824 err = -E2BIG;
4825 else
4826 mddev->external_size = 1;
4829 if (!err) {
4830 mddev->array_sectors = sectors;
4831 if (mddev->pers) {
4832 set_capacity(mddev->gendisk, mddev->array_sectors);
4833 revalidate_disk(mddev->gendisk);
4836 mddev_unlock(mddev);
4837 return err ?: len;
4840 static struct md_sysfs_entry md_array_size =
4841 __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4842 array_size_store);
4844 static struct attribute *md_default_attrs[] = {
4845 &md_level.attr,
4846 &md_layout.attr,
4847 &md_raid_disks.attr,
4848 &md_chunk_size.attr,
4849 &md_size.attr,
4850 &md_resync_start.attr,
4851 &md_metadata.attr,
4852 &md_new_device.attr,
4853 &md_safe_delay.attr,
4854 &md_array_state.attr,
4855 &md_reshape_position.attr,
4856 &md_reshape_direction.attr,
4857 &md_array_size.attr,
4858 &max_corr_read_errors.attr,
4859 NULL,
4862 static struct attribute *md_redundancy_attrs[] = {
4863 &md_scan_mode.attr,
4864 &md_last_scan_mode.attr,
4865 &md_mismatches.attr,
4866 &md_sync_min.attr,
4867 &md_sync_max.attr,
4868 &md_sync_speed.attr,
4869 &md_sync_force_parallel.attr,
4870 &md_sync_completed.attr,
4871 &md_min_sync.attr,
4872 &md_max_sync.attr,
4873 &md_suspend_lo.attr,
4874 &md_suspend_hi.attr,
4875 &md_bitmap.attr,
4876 &md_degraded.attr,
4877 NULL,
4879 static struct attribute_group md_redundancy_group = {
4880 .name = NULL,
4881 .attrs = md_redundancy_attrs,
4884 static ssize_t
4885 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4887 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4888 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
4889 ssize_t rv;
4891 if (!entry->show)
4892 return -EIO;
4893 spin_lock(&all_mddevs_lock);
4894 if (list_empty(&mddev->all_mddevs)) {
4895 spin_unlock(&all_mddevs_lock);
4896 return -EBUSY;
4898 mddev_get(mddev);
4899 spin_unlock(&all_mddevs_lock);
4901 rv = entry->show(mddev, page);
4902 mddev_put(mddev);
4903 return rv;
4906 static ssize_t
4907 md_attr_store(struct kobject *kobj, struct attribute *attr,
4908 const char *page, size_t length)
4910 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4911 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
4912 ssize_t rv;
4914 if (!entry->store)
4915 return -EIO;
4916 if (!capable(CAP_SYS_ADMIN))
4917 return -EACCES;
4918 spin_lock(&all_mddevs_lock);
4919 if (list_empty(&mddev->all_mddevs)) {
4920 spin_unlock(&all_mddevs_lock);
4921 return -EBUSY;
4923 mddev_get(mddev);
4924 spin_unlock(&all_mddevs_lock);
4925 rv = entry->store(mddev, page, length);
4926 mddev_put(mddev);
4927 return rv;
4930 static void md_free(struct kobject *ko)
4932 struct mddev *mddev = container_of(ko, struct mddev, kobj);
4934 if (mddev->sysfs_state)
4935 sysfs_put(mddev->sysfs_state);
4937 if (mddev->queue)
4938 blk_cleanup_queue(mddev->queue);
4939 if (mddev->gendisk) {
4940 del_gendisk(mddev->gendisk);
4941 put_disk(mddev->gendisk);
4944 kfree(mddev);
4947 static const struct sysfs_ops md_sysfs_ops = {
4948 .show = md_attr_show,
4949 .store = md_attr_store,
4951 static struct kobj_type md_ktype = {
4952 .release = md_free,
4953 .sysfs_ops = &md_sysfs_ops,
4954 .default_attrs = md_default_attrs,
4957 int mdp_major = 0;
4959 static void mddev_delayed_delete(struct work_struct *ws)
4961 struct mddev *mddev = container_of(ws, struct mddev, del_work);
4963 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
4964 kobject_del(&mddev->kobj);
4965 kobject_put(&mddev->kobj);
4968 static int md_alloc(dev_t dev, char *name)
4970 static DEFINE_MUTEX(disks_mutex);
4971 struct mddev *mddev = mddev_find(dev);
4972 struct gendisk *disk;
4973 int partitioned;
4974 int shift;
4975 int unit;
4976 int error;
4978 if (!mddev)
4979 return -ENODEV;
4981 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4982 shift = partitioned ? MdpMinorShift : 0;
4983 unit = MINOR(mddev->unit) >> shift;
4985 /* wait for any previous instance of this device to be
4986 * completely removed (mddev_delayed_delete).
4988 flush_workqueue(md_misc_wq);
4990 mutex_lock(&disks_mutex);
4991 error = -EEXIST;
4992 if (mddev->gendisk)
4993 goto abort;
4995 if (name) {
4996 /* Need to ensure that 'name' is not a duplicate.
4998 struct mddev *mddev2;
4999 spin_lock(&all_mddevs_lock);
5001 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5002 if (mddev2->gendisk &&
5003 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5004 spin_unlock(&all_mddevs_lock);
5005 goto abort;
5007 spin_unlock(&all_mddevs_lock);
5010 error = -ENOMEM;
5011 mddev->queue = blk_alloc_queue(GFP_KERNEL);
5012 if (!mddev->queue)
5013 goto abort;
5014 mddev->queue->queuedata = mddev;
5016 blk_queue_make_request(mddev->queue, md_make_request);
5017 blk_set_stacking_limits(&mddev->queue->limits);
5019 disk = alloc_disk(1 << shift);
5020 if (!disk) {
5021 blk_cleanup_queue(mddev->queue);
5022 mddev->queue = NULL;
5023 goto abort;
5025 disk->major = MAJOR(mddev->unit);
5026 disk->first_minor = unit << shift;
5027 if (name)
5028 strcpy(disk->disk_name, name);
5029 else if (partitioned)
5030 sprintf(disk->disk_name, "md_d%d", unit);
5031 else
5032 sprintf(disk->disk_name, "md%d", unit);
5033 disk->fops = &md_fops;
5034 disk->private_data = mddev;
5035 disk->queue = mddev->queue;
5036 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
5037 /* Allow extended partitions. This makes the
5038 * 'mdp' device redundant, but we can't really
5039 * remove it now.
5041 disk->flags |= GENHD_FL_EXT_DEVT;
5042 mddev->gendisk = disk;
5043 /* As soon as we call add_disk(), another thread could get
5044 * through to md_open, so make sure it doesn't get too far
5046 mutex_lock(&mddev->open_mutex);
5047 add_disk(disk);
5049 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
5050 &disk_to_dev(disk)->kobj, "%s", "md");
5051 if (error) {
5052 /* This isn't possible, but as kobject_init_and_add is marked
5053 * __must_check, we must do something with the result
5055 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
5056 disk->disk_name);
5057 error = 0;
5059 if (mddev->kobj.sd &&
5060 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
5061 printk(KERN_DEBUG "pointless warning\n");
5062 mutex_unlock(&mddev->open_mutex);
5063 abort:
5064 mutex_unlock(&disks_mutex);
5065 if (!error && mddev->kobj.sd) {
5066 kobject_uevent(&mddev->kobj, KOBJ_ADD);
5067 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
5069 mddev_put(mddev);
5070 return error;
5073 static struct kobject *md_probe(dev_t dev, int *part, void *data)
5075 md_alloc(dev, NULL);
5076 return NULL;
5079 static int add_named_array(const char *val, struct kernel_param *kp)
5081 /* val must be "md_*" where * is not all digits.
5082 * We allocate an array with a large free minor number, and
5083 * set the name to val. val must not already be an active name.
5085 int len = strlen(val);
5086 char buf[DISK_NAME_LEN];
5088 while (len && val[len-1] == '\n')
5089 len--;
5090 if (len >= DISK_NAME_LEN)
5091 return -E2BIG;
5092 strlcpy(buf, val, len+1);
5093 if (strncmp(buf, "md_", 3) != 0)
5094 return -EINVAL;
5095 return md_alloc(0, buf);
5098 static void md_safemode_timeout(unsigned long data)
5100 struct mddev *mddev = (struct mddev *) data;
5102 if (!atomic_read(&mddev->writes_pending)) {
5103 mddev->safemode = 1;
5104 if (mddev->external)
5105 sysfs_notify_dirent_safe(mddev->sysfs_state);
5107 md_wakeup_thread(mddev->thread);
5110 static int start_dirty_degraded;
5112 int md_run(struct mddev *mddev)
5114 int err;
5115 struct md_rdev *rdev;
5116 struct md_personality *pers;
5118 if (list_empty(&mddev->disks))
5119 /* cannot run an array with no devices.. */
5120 return -EINVAL;
5122 if (mddev->pers)
5123 return -EBUSY;
5124 /* Cannot run until previous stop completes properly */
5125 if (mddev->sysfs_active)
5126 return -EBUSY;
5129 * Analyze all RAID superblock(s)
5131 if (!mddev->raid_disks) {
5132 if (!mddev->persistent)
5133 return -EINVAL;
5134 analyze_sbs(mddev);
5137 if (mddev->level != LEVEL_NONE)
5138 request_module("md-level-%d", mddev->level);
5139 else if (mddev->clevel[0])
5140 request_module("md-%s", mddev->clevel);
5143 * Drop all container device buffers, from now on
5144 * the only valid external interface is through the md
5145 * device.
5147 rdev_for_each(rdev, mddev) {
5148 if (test_bit(Faulty, &rdev->flags))
5149 continue;
5150 sync_blockdev(rdev->bdev);
5151 invalidate_bdev(rdev->bdev);
5153 /* perform some consistency tests on the device.
5154 * We don't want the data to overlap the metadata,
5155 * Internal Bitmap issues have been handled elsewhere.
5157 if (rdev->meta_bdev) {
5158 /* Nothing to check */;
5159 } else if (rdev->data_offset < rdev->sb_start) {
5160 if (mddev->dev_sectors &&
5161 rdev->data_offset + mddev->dev_sectors
5162 > rdev->sb_start) {
5163 printk("md: %s: data overlaps metadata\n",
5164 mdname(mddev));
5165 return -EINVAL;
5167 } else {
5168 if (rdev->sb_start + rdev->sb_size/512
5169 > rdev->data_offset) {
5170 printk("md: %s: metadata overlaps data\n",
5171 mdname(mddev));
5172 return -EINVAL;
5175 sysfs_notify_dirent_safe(rdev->sysfs_state);
5178 if (mddev->bio_set == NULL)
5179 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
5181 spin_lock(&pers_lock);
5182 pers = find_pers(mddev->level, mddev->clevel);
5183 if (!pers || !try_module_get(pers->owner)) {
5184 spin_unlock(&pers_lock);
5185 if (mddev->level != LEVEL_NONE)
5186 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5187 mddev->level);
5188 else
5189 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5190 mddev->clevel);
5191 return -EINVAL;
5193 spin_unlock(&pers_lock);
5194 if (mddev->level != pers->level) {
5195 mddev->level = pers->level;
5196 mddev->new_level = pers->level;
5198 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
5200 if (mddev->reshape_position != MaxSector &&
5201 pers->start_reshape == NULL) {
5202 /* This personality cannot handle reshaping... */
5203 module_put(pers->owner);
5204 return -EINVAL;
5207 if (pers->sync_request) {
5208 /* Warn if this is a potentially silly
5209 * configuration.
5211 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5212 struct md_rdev *rdev2;
5213 int warned = 0;
5215 rdev_for_each(rdev, mddev)
5216 rdev_for_each(rdev2, mddev) {
5217 if (rdev < rdev2 &&
5218 rdev->bdev->bd_contains ==
5219 rdev2->bdev->bd_contains) {
5220 printk(KERN_WARNING
5221 "%s: WARNING: %s appears to be"
5222 " on the same physical disk as"
5223 " %s.\n",
5224 mdname(mddev),
5225 bdevname(rdev->bdev,b),
5226 bdevname(rdev2->bdev,b2));
5227 warned = 1;
5231 if (warned)
5232 printk(KERN_WARNING
5233 "True protection against single-disk"
5234 " failure might be compromised.\n");
5237 mddev->recovery = 0;
5238 /* may be over-ridden by personality */
5239 mddev->resync_max_sectors = mddev->dev_sectors;
5241 mddev->ok_start_degraded = start_dirty_degraded;
5243 if (start_readonly && mddev->ro == 0)
5244 mddev->ro = 2; /* read-only, but switch on first write */
5246 err = pers->run(mddev);
5247 if (err)
5248 printk(KERN_ERR "md: pers->run() failed ...\n");
5249 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
5250 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5251 " but 'external_size' not in effect?\n", __func__);
5252 printk(KERN_ERR
5253 "md: invalid array_size %llu > default size %llu\n",
5254 (unsigned long long)mddev->array_sectors / 2,
5255 (unsigned long long)pers->size(mddev, 0, 0) / 2);
5256 err = -EINVAL;
5258 if (err == 0 && pers->sync_request &&
5259 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
5260 struct bitmap *bitmap;
5262 bitmap = bitmap_create(mddev, -1);
5263 if (IS_ERR(bitmap)) {
5264 err = PTR_ERR(bitmap);
5265 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5266 mdname(mddev), err);
5267 } else
5268 mddev->bitmap = bitmap;
5271 if (err) {
5272 mddev_detach(mddev);
5273 if (mddev->private)
5274 pers->free(mddev, mddev->private);
5275 mddev->private = NULL;
5276 module_put(pers->owner);
5277 bitmap_destroy(mddev);
5278 return err;
5280 if (mddev->queue) {
5281 mddev->queue->backing_dev_info.congested_data = mddev;
5282 mddev->queue->backing_dev_info.congested_fn = md_congested;
5284 if (pers->sync_request) {
5285 if (mddev->kobj.sd &&
5286 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
5287 printk(KERN_WARNING
5288 "md: cannot register extra attributes for %s\n",
5289 mdname(mddev));
5290 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
5291 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
5292 mddev->ro = 0;
5294 atomic_set(&mddev->writes_pending,0);
5295 atomic_set(&mddev->max_corr_read_errors,
5296 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
5297 mddev->safemode = 0;
5298 if (mddev_is_clustered(mddev))
5299 mddev->safemode_delay = 0;
5300 else
5301 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
5302 mddev->in_sync = 1;
5303 smp_wmb();
5304 spin_lock(&mddev->lock);
5305 mddev->pers = pers;
5306 mddev->ready = 1;
5307 spin_unlock(&mddev->lock);
5308 rdev_for_each(rdev, mddev)
5309 if (rdev->raid_disk >= 0)
5310 if (sysfs_link_rdev(mddev, rdev))
5311 /* failure here is OK */;
5313 if (mddev->degraded && !mddev->ro)
5314 /* This ensures that recovering status is reported immediately
5315 * via sysfs - until a lack of spares is confirmed.
5317 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
5318 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5320 if (mddev->flags & MD_UPDATE_SB_FLAGS)
5321 md_update_sb(mddev, 0);
5323 md_new_event(mddev);
5324 sysfs_notify_dirent_safe(mddev->sysfs_state);
5325 sysfs_notify_dirent_safe(mddev->sysfs_action);
5326 sysfs_notify(&mddev->kobj, NULL, "degraded");
5327 return 0;
5329 EXPORT_SYMBOL_GPL(md_run);
5331 static int do_md_run(struct mddev *mddev)
5333 int err;
5335 err = md_run(mddev);
5336 if (err)
5337 goto out;
5338 err = bitmap_load(mddev);
5339 if (err) {
5340 bitmap_destroy(mddev);
5341 goto out;
5344 if (mddev_is_clustered(mddev))
5345 md_allow_write(mddev);
5347 md_wakeup_thread(mddev->thread);
5348 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5350 set_capacity(mddev->gendisk, mddev->array_sectors);
5351 revalidate_disk(mddev->gendisk);
5352 mddev->changed = 1;
5353 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5354 out:
5355 return err;
5358 static int restart_array(struct mddev *mddev)
5360 struct gendisk *disk = mddev->gendisk;
5362 /* Complain if it has no devices */
5363 if (list_empty(&mddev->disks))
5364 return -ENXIO;
5365 if (!mddev->pers)
5366 return -EINVAL;
5367 if (!mddev->ro)
5368 return -EBUSY;
5369 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5370 struct md_rdev *rdev;
5371 bool has_journal = false;
5373 rcu_read_lock();
5374 rdev_for_each_rcu(rdev, mddev) {
5375 if (test_bit(Journal, &rdev->flags) &&
5376 !test_bit(Faulty, &rdev->flags)) {
5377 has_journal = true;
5378 break;
5381 rcu_read_unlock();
5383 /* Don't restart rw with journal missing/faulty */
5384 if (!has_journal)
5385 return -EINVAL;
5388 mddev->safemode = 0;
5389 mddev->ro = 0;
5390 set_disk_ro(disk, 0);
5391 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5392 mdname(mddev));
5393 /* Kick recovery or resync if necessary */
5394 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5395 md_wakeup_thread(mddev->thread);
5396 md_wakeup_thread(mddev->sync_thread);
5397 sysfs_notify_dirent_safe(mddev->sysfs_state);
5398 return 0;
5401 static void md_clean(struct mddev *mddev)
5403 mddev->array_sectors = 0;
5404 mddev->external_size = 0;
5405 mddev->dev_sectors = 0;
5406 mddev->raid_disks = 0;
5407 mddev->recovery_cp = 0;
5408 mddev->resync_min = 0;
5409 mddev->resync_max = MaxSector;
5410 mddev->reshape_position = MaxSector;
5411 mddev->external = 0;
5412 mddev->persistent = 0;
5413 mddev->level = LEVEL_NONE;
5414 mddev->clevel[0] = 0;
5415 mddev->flags = 0;
5416 mddev->ro = 0;
5417 mddev->metadata_type[0] = 0;
5418 mddev->chunk_sectors = 0;
5419 mddev->ctime = mddev->utime = 0;
5420 mddev->layout = 0;
5421 mddev->max_disks = 0;
5422 mddev->events = 0;
5423 mddev->can_decrease_events = 0;
5424 mddev->delta_disks = 0;
5425 mddev->reshape_backwards = 0;
5426 mddev->new_level = LEVEL_NONE;
5427 mddev->new_layout = 0;
5428 mddev->new_chunk_sectors = 0;
5429 mddev->curr_resync = 0;
5430 atomic64_set(&mddev->resync_mismatches, 0);
5431 mddev->suspend_lo = mddev->suspend_hi = 0;
5432 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5433 mddev->recovery = 0;
5434 mddev->in_sync = 0;
5435 mddev->changed = 0;
5436 mddev->degraded = 0;
5437 mddev->safemode = 0;
5438 mddev->private = NULL;
5439 mddev->bitmap_info.offset = 0;
5440 mddev->bitmap_info.default_offset = 0;
5441 mddev->bitmap_info.default_space = 0;
5442 mddev->bitmap_info.chunksize = 0;
5443 mddev->bitmap_info.daemon_sleep = 0;
5444 mddev->bitmap_info.max_write_behind = 0;
5447 static void __md_stop_writes(struct mddev *mddev)
5449 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5450 flush_workqueue(md_misc_wq);
5451 if (mddev->sync_thread) {
5452 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5453 md_reap_sync_thread(mddev);
5456 del_timer_sync(&mddev->safemode_timer);
5458 bitmap_flush(mddev);
5459 md_super_wait(mddev);
5461 if (mddev->ro == 0 &&
5462 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
5463 (mddev->flags & MD_UPDATE_SB_FLAGS))) {
5464 /* mark array as shutdown cleanly */
5465 if (!mddev_is_clustered(mddev))
5466 mddev->in_sync = 1;
5467 md_update_sb(mddev, 1);
5471 void md_stop_writes(struct mddev *mddev)
5473 mddev_lock_nointr(mddev);
5474 __md_stop_writes(mddev);
5475 mddev_unlock(mddev);
5477 EXPORT_SYMBOL_GPL(md_stop_writes);
5479 static void mddev_detach(struct mddev *mddev)
5481 struct bitmap *bitmap = mddev->bitmap;
5482 /* wait for behind writes to complete */
5483 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
5484 printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n",
5485 mdname(mddev));
5486 /* need to kick something here to make sure I/O goes? */
5487 wait_event(bitmap->behind_wait,
5488 atomic_read(&bitmap->behind_writes) == 0);
5490 if (mddev->pers && mddev->pers->quiesce) {
5491 mddev->pers->quiesce(mddev, 1);
5492 mddev->pers->quiesce(mddev, 0);
5494 md_unregister_thread(&mddev->thread);
5495 if (mddev->queue)
5496 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5499 static void __md_stop(struct mddev *mddev)
5501 struct md_personality *pers = mddev->pers;
5502 mddev_detach(mddev);
5503 /* Ensure ->event_work is done */
5504 flush_workqueue(md_misc_wq);
5505 spin_lock(&mddev->lock);
5506 mddev->ready = 0;
5507 mddev->pers = NULL;
5508 spin_unlock(&mddev->lock);
5509 pers->free(mddev, mddev->private);
5510 mddev->private = NULL;
5511 if (pers->sync_request && mddev->to_remove == NULL)
5512 mddev->to_remove = &md_redundancy_group;
5513 module_put(pers->owner);
5514 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5517 void md_stop(struct mddev *mddev)
5519 /* stop the array and free an attached data structures.
5520 * This is called from dm-raid
5522 __md_stop(mddev);
5523 bitmap_destroy(mddev);
5524 if (mddev->bio_set)
5525 bioset_free(mddev->bio_set);
5528 EXPORT_SYMBOL_GPL(md_stop);
5530 static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
5532 int err = 0;
5533 int did_freeze = 0;
5535 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5536 did_freeze = 1;
5537 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5538 md_wakeup_thread(mddev->thread);
5540 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
5541 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5542 if (mddev->sync_thread)
5543 /* Thread might be blocked waiting for metadata update
5544 * which will now never happen */
5545 wake_up_process(mddev->sync_thread->tsk);
5547 if (mddev->external && test_bit(MD_CHANGE_PENDING, &mddev->flags))
5548 return -EBUSY;
5549 mddev_unlock(mddev);
5550 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5551 &mddev->recovery));
5552 wait_event(mddev->sb_wait,
5553 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
5554 mddev_lock_nointr(mddev);
5556 mutex_lock(&mddev->open_mutex);
5557 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
5558 mddev->sync_thread ||
5559 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
5560 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
5561 printk("md: %s still in use.\n",mdname(mddev));
5562 if (did_freeze) {
5563 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5564 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5565 md_wakeup_thread(mddev->thread);
5567 err = -EBUSY;
5568 goto out;
5570 if (mddev->pers) {
5571 __md_stop_writes(mddev);
5573 err = -ENXIO;
5574 if (mddev->ro==1)
5575 goto out;
5576 mddev->ro = 1;
5577 set_disk_ro(mddev->gendisk, 1);
5578 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5579 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5580 md_wakeup_thread(mddev->thread);
5581 sysfs_notify_dirent_safe(mddev->sysfs_state);
5582 err = 0;
5584 out:
5585 mutex_unlock(&mddev->open_mutex);
5586 return err;
5589 /* mode:
5590 * 0 - completely stop and dis-assemble array
5591 * 2 - stop but do not disassemble array
5593 static int do_md_stop(struct mddev *mddev, int mode,
5594 struct block_device *bdev)
5596 struct gendisk *disk = mddev->gendisk;
5597 struct md_rdev *rdev;
5598 int did_freeze = 0;
5600 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5601 did_freeze = 1;
5602 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5603 md_wakeup_thread(mddev->thread);
5605 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
5606 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5607 if (mddev->sync_thread)
5608 /* Thread might be blocked waiting for metadata update
5609 * which will now never happen */
5610 wake_up_process(mddev->sync_thread->tsk);
5612 mddev_unlock(mddev);
5613 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5614 !test_bit(MD_RECOVERY_RUNNING,
5615 &mddev->recovery)));
5616 mddev_lock_nointr(mddev);
5618 mutex_lock(&mddev->open_mutex);
5619 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
5620 mddev->sysfs_active ||
5621 mddev->sync_thread ||
5622 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
5623 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
5624 printk("md: %s still in use.\n",mdname(mddev));
5625 mutex_unlock(&mddev->open_mutex);
5626 if (did_freeze) {
5627 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5628 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5629 md_wakeup_thread(mddev->thread);
5631 return -EBUSY;
5633 if (mddev->pers) {
5634 if (mddev->ro)
5635 set_disk_ro(disk, 0);
5637 __md_stop_writes(mddev);
5638 __md_stop(mddev);
5639 mddev->queue->backing_dev_info.congested_fn = NULL;
5641 /* tell userspace to handle 'inactive' */
5642 sysfs_notify_dirent_safe(mddev->sysfs_state);
5644 rdev_for_each(rdev, mddev)
5645 if (rdev->raid_disk >= 0)
5646 sysfs_unlink_rdev(mddev, rdev);
5648 set_capacity(disk, 0);
5649 mutex_unlock(&mddev->open_mutex);
5650 mddev->changed = 1;
5651 revalidate_disk(disk);
5653 if (mddev->ro)
5654 mddev->ro = 0;
5655 } else
5656 mutex_unlock(&mddev->open_mutex);
5658 * Free resources if final stop
5660 if (mode == 0) {
5661 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5663 bitmap_destroy(mddev);
5664 if (mddev->bitmap_info.file) {
5665 struct file *f = mddev->bitmap_info.file;
5666 spin_lock(&mddev->lock);
5667 mddev->bitmap_info.file = NULL;
5668 spin_unlock(&mddev->lock);
5669 fput(f);
5671 mddev->bitmap_info.offset = 0;
5673 export_array(mddev);
5675 md_clean(mddev);
5676 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5677 if (mddev->hold_active == UNTIL_STOP)
5678 mddev->hold_active = 0;
5680 md_new_event(mddev);
5681 sysfs_notify_dirent_safe(mddev->sysfs_state);
5682 return 0;
5685 #ifndef MODULE
5686 static void autorun_array(struct mddev *mddev)
5688 struct md_rdev *rdev;
5689 int err;
5691 if (list_empty(&mddev->disks))
5692 return;
5694 printk(KERN_INFO "md: running: ");
5696 rdev_for_each(rdev, mddev) {
5697 char b[BDEVNAME_SIZE];
5698 printk("<%s>", bdevname(rdev->bdev,b));
5700 printk("\n");
5702 err = do_md_run(mddev);
5703 if (err) {
5704 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
5705 do_md_stop(mddev, 0, NULL);
5710 * lets try to run arrays based on all disks that have arrived
5711 * until now. (those are in pending_raid_disks)
5713 * the method: pick the first pending disk, collect all disks with
5714 * the same UUID, remove all from the pending list and put them into
5715 * the 'same_array' list. Then order this list based on superblock
5716 * update time (freshest comes first), kick out 'old' disks and
5717 * compare superblocks. If everything's fine then run it.
5719 * If "unit" is allocated, then bump its reference count
5721 static void autorun_devices(int part)
5723 struct md_rdev *rdev0, *rdev, *tmp;
5724 struct mddev *mddev;
5725 char b[BDEVNAME_SIZE];
5727 printk(KERN_INFO "md: autorun ...\n");
5728 while (!list_empty(&pending_raid_disks)) {
5729 int unit;
5730 dev_t dev;
5731 LIST_HEAD(candidates);
5732 rdev0 = list_entry(pending_raid_disks.next,
5733 struct md_rdev, same_set);
5735 printk(KERN_INFO "md: considering %s ...\n",
5736 bdevname(rdev0->bdev,b));
5737 INIT_LIST_HEAD(&candidates);
5738 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
5739 if (super_90_load(rdev, rdev0, 0) >= 0) {
5740 printk(KERN_INFO "md: adding %s ...\n",
5741 bdevname(rdev->bdev,b));
5742 list_move(&rdev->same_set, &candidates);
5745 * now we have a set of devices, with all of them having
5746 * mostly sane superblocks. It's time to allocate the
5747 * mddev.
5749 if (part) {
5750 dev = MKDEV(mdp_major,
5751 rdev0->preferred_minor << MdpMinorShift);
5752 unit = MINOR(dev) >> MdpMinorShift;
5753 } else {
5754 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5755 unit = MINOR(dev);
5757 if (rdev0->preferred_minor != unit) {
5758 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5759 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5760 break;
5763 md_probe(dev, NULL, NULL);
5764 mddev = mddev_find(dev);
5765 if (!mddev || !mddev->gendisk) {
5766 if (mddev)
5767 mddev_put(mddev);
5768 printk(KERN_ERR
5769 "md: cannot allocate memory for md drive.\n");
5770 break;
5772 if (mddev_lock(mddev))
5773 printk(KERN_WARNING "md: %s locked, cannot run\n",
5774 mdname(mddev));
5775 else if (mddev->raid_disks || mddev->major_version
5776 || !list_empty(&mddev->disks)) {
5777 printk(KERN_WARNING
5778 "md: %s already running, cannot run %s\n",
5779 mdname(mddev), bdevname(rdev0->bdev,b));
5780 mddev_unlock(mddev);
5781 } else {
5782 printk(KERN_INFO "md: created %s\n", mdname(mddev));
5783 mddev->persistent = 1;
5784 rdev_for_each_list(rdev, tmp, &candidates) {
5785 list_del_init(&rdev->same_set);
5786 if (bind_rdev_to_array(rdev, mddev))
5787 export_rdev(rdev);
5789 autorun_array(mddev);
5790 mddev_unlock(mddev);
5792 /* on success, candidates will be empty, on error
5793 * it won't...
5795 rdev_for_each_list(rdev, tmp, &candidates) {
5796 list_del_init(&rdev->same_set);
5797 export_rdev(rdev);
5799 mddev_put(mddev);
5801 printk(KERN_INFO "md: ... autorun DONE.\n");
5803 #endif /* !MODULE */
5805 static int get_version(void __user *arg)
5807 mdu_version_t ver;
5809 ver.major = MD_MAJOR_VERSION;
5810 ver.minor = MD_MINOR_VERSION;
5811 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5813 if (copy_to_user(arg, &ver, sizeof(ver)))
5814 return -EFAULT;
5816 return 0;
5819 static int get_array_info(struct mddev *mddev, void __user *arg)
5821 mdu_array_info_t info;
5822 int nr,working,insync,failed,spare;
5823 struct md_rdev *rdev;
5825 nr = working = insync = failed = spare = 0;
5826 rcu_read_lock();
5827 rdev_for_each_rcu(rdev, mddev) {
5828 nr++;
5829 if (test_bit(Faulty, &rdev->flags))
5830 failed++;
5831 else {
5832 working++;
5833 if (test_bit(In_sync, &rdev->flags))
5834 insync++;
5835 else
5836 spare++;
5839 rcu_read_unlock();
5841 info.major_version = mddev->major_version;
5842 info.minor_version = mddev->minor_version;
5843 info.patch_version = MD_PATCHLEVEL_VERSION;
5844 info.ctime = mddev->ctime;
5845 info.level = mddev->level;
5846 info.size = mddev->dev_sectors / 2;
5847 if (info.size != mddev->dev_sectors / 2) /* overflow */
5848 info.size = -1;
5849 info.nr_disks = nr;
5850 info.raid_disks = mddev->raid_disks;
5851 info.md_minor = mddev->md_minor;
5852 info.not_persistent= !mddev->persistent;
5854 info.utime = mddev->utime;
5855 info.state = 0;
5856 if (mddev->in_sync)
5857 info.state = (1<<MD_SB_CLEAN);
5858 if (mddev->bitmap && mddev->bitmap_info.offset)
5859 info.state |= (1<<MD_SB_BITMAP_PRESENT);
5860 if (mddev_is_clustered(mddev))
5861 info.state |= (1<<MD_SB_CLUSTERED);
5862 info.active_disks = insync;
5863 info.working_disks = working;
5864 info.failed_disks = failed;
5865 info.spare_disks = spare;
5867 info.layout = mddev->layout;
5868 info.chunk_size = mddev->chunk_sectors << 9;
5870 if (copy_to_user(arg, &info, sizeof(info)))
5871 return -EFAULT;
5873 return 0;
5876 static int get_bitmap_file(struct mddev *mddev, void __user * arg)
5878 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5879 char *ptr;
5880 int err;
5882 file = kzalloc(sizeof(*file), GFP_NOIO);
5883 if (!file)
5884 return -ENOMEM;
5886 err = 0;
5887 spin_lock(&mddev->lock);
5888 /* bitmap enabled */
5889 if (mddev->bitmap_info.file) {
5890 ptr = file_path(mddev->bitmap_info.file, file->pathname,
5891 sizeof(file->pathname));
5892 if (IS_ERR(ptr))
5893 err = PTR_ERR(ptr);
5894 else
5895 memmove(file->pathname, ptr,
5896 sizeof(file->pathname)-(ptr-file->pathname));
5898 spin_unlock(&mddev->lock);
5900 if (err == 0 &&
5901 copy_to_user(arg, file, sizeof(*file)))
5902 err = -EFAULT;
5904 kfree(file);
5905 return err;
5908 static int get_disk_info(struct mddev *mddev, void __user * arg)
5910 mdu_disk_info_t info;
5911 struct md_rdev *rdev;
5913 if (copy_from_user(&info, arg, sizeof(info)))
5914 return -EFAULT;
5916 rcu_read_lock();
5917 rdev = md_find_rdev_nr_rcu(mddev, info.number);
5918 if (rdev) {
5919 info.major = MAJOR(rdev->bdev->bd_dev);
5920 info.minor = MINOR(rdev->bdev->bd_dev);
5921 info.raid_disk = rdev->raid_disk;
5922 info.state = 0;
5923 if (test_bit(Faulty, &rdev->flags))
5924 info.state |= (1<<MD_DISK_FAULTY);
5925 else if (test_bit(In_sync, &rdev->flags)) {
5926 info.state |= (1<<MD_DISK_ACTIVE);
5927 info.state |= (1<<MD_DISK_SYNC);
5929 if (test_bit(Journal, &rdev->flags))
5930 info.state |= (1<<MD_DISK_JOURNAL);
5931 if (test_bit(WriteMostly, &rdev->flags))
5932 info.state |= (1<<MD_DISK_WRITEMOSTLY);
5933 } else {
5934 info.major = info.minor = 0;
5935 info.raid_disk = -1;
5936 info.state = (1<<MD_DISK_REMOVED);
5938 rcu_read_unlock();
5940 if (copy_to_user(arg, &info, sizeof(info)))
5941 return -EFAULT;
5943 return 0;
5946 static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
5948 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5949 struct md_rdev *rdev;
5950 dev_t dev = MKDEV(info->major,info->minor);
5952 if (mddev_is_clustered(mddev) &&
5953 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
5954 pr_err("%s: Cannot add to clustered mddev.\n",
5955 mdname(mddev));
5956 return -EINVAL;
5959 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5960 return -EOVERFLOW;
5962 if (!mddev->raid_disks) {
5963 int err;
5964 /* expecting a device which has a superblock */
5965 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5966 if (IS_ERR(rdev)) {
5967 printk(KERN_WARNING
5968 "md: md_import_device returned %ld\n",
5969 PTR_ERR(rdev));
5970 return PTR_ERR(rdev);
5972 if (!list_empty(&mddev->disks)) {
5973 struct md_rdev *rdev0
5974 = list_entry(mddev->disks.next,
5975 struct md_rdev, same_set);
5976 err = super_types[mddev->major_version]
5977 .load_super(rdev, rdev0, mddev->minor_version);
5978 if (err < 0) {
5979 printk(KERN_WARNING
5980 "md: %s has different UUID to %s\n",
5981 bdevname(rdev->bdev,b),
5982 bdevname(rdev0->bdev,b2));
5983 export_rdev(rdev);
5984 return -EINVAL;
5987 err = bind_rdev_to_array(rdev, mddev);
5988 if (err)
5989 export_rdev(rdev);
5990 return err;
5994 * add_new_disk can be used once the array is assembled
5995 * to add "hot spares". They must already have a superblock
5996 * written
5998 if (mddev->pers) {
5999 int err;
6000 if (!mddev->pers->hot_add_disk) {
6001 printk(KERN_WARNING
6002 "%s: personality does not support diskops!\n",
6003 mdname(mddev));
6004 return -EINVAL;
6006 if (mddev->persistent)
6007 rdev = md_import_device(dev, mddev->major_version,
6008 mddev->minor_version);
6009 else
6010 rdev = md_import_device(dev, -1, -1);
6011 if (IS_ERR(rdev)) {
6012 printk(KERN_WARNING
6013 "md: md_import_device returned %ld\n",
6014 PTR_ERR(rdev));
6015 return PTR_ERR(rdev);
6017 /* set saved_raid_disk if appropriate */
6018 if (!mddev->persistent) {
6019 if (info->state & (1<<MD_DISK_SYNC) &&
6020 info->raid_disk < mddev->raid_disks) {
6021 rdev->raid_disk = info->raid_disk;
6022 set_bit(In_sync, &rdev->flags);
6023 clear_bit(Bitmap_sync, &rdev->flags);
6024 } else
6025 rdev->raid_disk = -1;
6026 rdev->saved_raid_disk = rdev->raid_disk;
6027 } else
6028 super_types[mddev->major_version].
6029 validate_super(mddev, rdev);
6030 if ((info->state & (1<<MD_DISK_SYNC)) &&
6031 rdev->raid_disk != info->raid_disk) {
6032 /* This was a hot-add request, but events doesn't
6033 * match, so reject it.
6035 export_rdev(rdev);
6036 return -EINVAL;
6039 clear_bit(In_sync, &rdev->flags); /* just to be sure */
6040 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6041 set_bit(WriteMostly, &rdev->flags);
6042 else
6043 clear_bit(WriteMostly, &rdev->flags);
6045 if (info->state & (1<<MD_DISK_JOURNAL))
6046 set_bit(Journal, &rdev->flags);
6048 * check whether the device shows up in other nodes
6050 if (mddev_is_clustered(mddev)) {
6051 if (info->state & (1 << MD_DISK_CANDIDATE))
6052 set_bit(Candidate, &rdev->flags);
6053 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
6054 /* --add initiated by this node */
6055 err = md_cluster_ops->add_new_disk(mddev, rdev);
6056 if (err) {
6057 export_rdev(rdev);
6058 return err;
6063 rdev->raid_disk = -1;
6064 err = bind_rdev_to_array(rdev, mddev);
6066 if (err)
6067 export_rdev(rdev);
6069 if (mddev_is_clustered(mddev)) {
6070 if (info->state & (1 << MD_DISK_CANDIDATE))
6071 md_cluster_ops->new_disk_ack(mddev, (err == 0));
6072 else {
6073 if (err)
6074 md_cluster_ops->add_new_disk_cancel(mddev);
6075 else
6076 err = add_bound_rdev(rdev);
6079 } else if (!err)
6080 err = add_bound_rdev(rdev);
6082 return err;
6085 /* otherwise, add_new_disk is only allowed
6086 * for major_version==0 superblocks
6088 if (mddev->major_version != 0) {
6089 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
6090 mdname(mddev));
6091 return -EINVAL;
6094 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6095 int err;
6096 rdev = md_import_device(dev, -1, 0);
6097 if (IS_ERR(rdev)) {
6098 printk(KERN_WARNING
6099 "md: error, md_import_device() returned %ld\n",
6100 PTR_ERR(rdev));
6101 return PTR_ERR(rdev);
6103 rdev->desc_nr = info->number;
6104 if (info->raid_disk < mddev->raid_disks)
6105 rdev->raid_disk = info->raid_disk;
6106 else
6107 rdev->raid_disk = -1;
6109 if (rdev->raid_disk < mddev->raid_disks)
6110 if (info->state & (1<<MD_DISK_SYNC))
6111 set_bit(In_sync, &rdev->flags);
6113 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6114 set_bit(WriteMostly, &rdev->flags);
6116 if (!mddev->persistent) {
6117 printk(KERN_INFO "md: nonpersistent superblock ...\n");
6118 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6119 } else
6120 rdev->sb_start = calc_dev_sboffset(rdev);
6121 rdev->sectors = rdev->sb_start;
6123 err = bind_rdev_to_array(rdev, mddev);
6124 if (err) {
6125 export_rdev(rdev);
6126 return err;
6130 return 0;
6133 static int hot_remove_disk(struct mddev *mddev, dev_t dev)
6135 char b[BDEVNAME_SIZE];
6136 struct md_rdev *rdev;
6137 int ret = -1;
6139 rdev = find_rdev(mddev, dev);
6140 if (!rdev)
6141 return -ENXIO;
6143 if (mddev_is_clustered(mddev))
6144 ret = md_cluster_ops->metadata_update_start(mddev);
6146 if (rdev->raid_disk < 0)
6147 goto kick_rdev;
6149 clear_bit(Blocked, &rdev->flags);
6150 remove_and_add_spares(mddev, rdev);
6152 if (rdev->raid_disk >= 0)
6153 goto busy;
6155 kick_rdev:
6156 if (mddev_is_clustered(mddev) && ret == 0)
6157 md_cluster_ops->remove_disk(mddev, rdev);
6159 md_kick_rdev_from_array(rdev);
6160 md_update_sb(mddev, 1);
6161 md_new_event(mddev);
6163 return 0;
6164 busy:
6165 if (mddev_is_clustered(mddev) && ret == 0)
6166 md_cluster_ops->metadata_update_cancel(mddev);
6168 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
6169 bdevname(rdev->bdev,b), mdname(mddev));
6170 return -EBUSY;
6173 static int hot_add_disk(struct mddev *mddev, dev_t dev)
6175 char b[BDEVNAME_SIZE];
6176 int err;
6177 struct md_rdev *rdev;
6179 if (!mddev->pers)
6180 return -ENODEV;
6182 if (mddev->major_version != 0) {
6183 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
6184 " version-0 superblocks.\n",
6185 mdname(mddev));
6186 return -EINVAL;
6188 if (!mddev->pers->hot_add_disk) {
6189 printk(KERN_WARNING
6190 "%s: personality does not support diskops!\n",
6191 mdname(mddev));
6192 return -EINVAL;
6195 rdev = md_import_device(dev, -1, 0);
6196 if (IS_ERR(rdev)) {
6197 printk(KERN_WARNING
6198 "md: error, md_import_device() returned %ld\n",
6199 PTR_ERR(rdev));
6200 return -EINVAL;
6203 if (mddev->persistent)
6204 rdev->sb_start = calc_dev_sboffset(rdev);
6205 else
6206 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6208 rdev->sectors = rdev->sb_start;
6210 if (test_bit(Faulty, &rdev->flags)) {
6211 printk(KERN_WARNING
6212 "md: can not hot-add faulty %s disk to %s!\n",
6213 bdevname(rdev->bdev,b), mdname(mddev));
6214 err = -EINVAL;
6215 goto abort_export;
6218 clear_bit(In_sync, &rdev->flags);
6219 rdev->desc_nr = -1;
6220 rdev->saved_raid_disk = -1;
6221 err = bind_rdev_to_array(rdev, mddev);
6222 if (err)
6223 goto abort_export;
6226 * The rest should better be atomic, we can have disk failures
6227 * noticed in interrupt contexts ...
6230 rdev->raid_disk = -1;
6232 md_update_sb(mddev, 1);
6234 * Kick recovery, maybe this spare has to be added to the
6235 * array immediately.
6237 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6238 md_wakeup_thread(mddev->thread);
6239 md_new_event(mddev);
6240 return 0;
6242 abort_export:
6243 export_rdev(rdev);
6244 return err;
6247 static int set_bitmap_file(struct mddev *mddev, int fd)
6249 int err = 0;
6251 if (mddev->pers) {
6252 if (!mddev->pers->quiesce || !mddev->thread)
6253 return -EBUSY;
6254 if (mddev->recovery || mddev->sync_thread)
6255 return -EBUSY;
6256 /* we should be able to change the bitmap.. */
6259 if (fd >= 0) {
6260 struct inode *inode;
6261 struct file *f;
6263 if (mddev->bitmap || mddev->bitmap_info.file)
6264 return -EEXIST; /* cannot add when bitmap is present */
6265 f = fget(fd);
6267 if (f == NULL) {
6268 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
6269 mdname(mddev));
6270 return -EBADF;
6273 inode = f->f_mapping->host;
6274 if (!S_ISREG(inode->i_mode)) {
6275 printk(KERN_ERR "%s: error: bitmap file must be a regular file\n",
6276 mdname(mddev));
6277 err = -EBADF;
6278 } else if (!(f->f_mode & FMODE_WRITE)) {
6279 printk(KERN_ERR "%s: error: bitmap file must open for write\n",
6280 mdname(mddev));
6281 err = -EBADF;
6282 } else if (atomic_read(&inode->i_writecount) != 1) {
6283 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
6284 mdname(mddev));
6285 err = -EBUSY;
6287 if (err) {
6288 fput(f);
6289 return err;
6291 mddev->bitmap_info.file = f;
6292 mddev->bitmap_info.offset = 0; /* file overrides offset */
6293 } else if (mddev->bitmap == NULL)
6294 return -ENOENT; /* cannot remove what isn't there */
6295 err = 0;
6296 if (mddev->pers) {
6297 mddev->pers->quiesce(mddev, 1);
6298 if (fd >= 0) {
6299 struct bitmap *bitmap;
6301 bitmap = bitmap_create(mddev, -1);
6302 if (!IS_ERR(bitmap)) {
6303 mddev->bitmap = bitmap;
6304 err = bitmap_load(mddev);
6305 } else
6306 err = PTR_ERR(bitmap);
6308 if (fd < 0 || err) {
6309 bitmap_destroy(mddev);
6310 fd = -1; /* make sure to put the file */
6312 mddev->pers->quiesce(mddev, 0);
6314 if (fd < 0) {
6315 struct file *f = mddev->bitmap_info.file;
6316 if (f) {
6317 spin_lock(&mddev->lock);
6318 mddev->bitmap_info.file = NULL;
6319 spin_unlock(&mddev->lock);
6320 fput(f);
6324 return err;
6328 * set_array_info is used two different ways
6329 * The original usage is when creating a new array.
6330 * In this usage, raid_disks is > 0 and it together with
6331 * level, size, not_persistent,layout,chunksize determine the
6332 * shape of the array.
6333 * This will always create an array with a type-0.90.0 superblock.
6334 * The newer usage is when assembling an array.
6335 * In this case raid_disks will be 0, and the major_version field is
6336 * use to determine which style super-blocks are to be found on the devices.
6337 * The minor and patch _version numbers are also kept incase the
6338 * super_block handler wishes to interpret them.
6340 static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
6343 if (info->raid_disks == 0) {
6344 /* just setting version number for superblock loading */
6345 if (info->major_version < 0 ||
6346 info->major_version >= ARRAY_SIZE(super_types) ||
6347 super_types[info->major_version].name == NULL) {
6348 /* maybe try to auto-load a module? */
6349 printk(KERN_INFO
6350 "md: superblock version %d not known\n",
6351 info->major_version);
6352 return -EINVAL;
6354 mddev->major_version = info->major_version;
6355 mddev->minor_version = info->minor_version;
6356 mddev->patch_version = info->patch_version;
6357 mddev->persistent = !info->not_persistent;
6358 /* ensure mddev_put doesn't delete this now that there
6359 * is some minimal configuration.
6361 mddev->ctime = get_seconds();
6362 return 0;
6364 mddev->major_version = MD_MAJOR_VERSION;
6365 mddev->minor_version = MD_MINOR_VERSION;
6366 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6367 mddev->ctime = get_seconds();
6369 mddev->level = info->level;
6370 mddev->clevel[0] = 0;
6371 mddev->dev_sectors = 2 * (sector_t)info->size;
6372 mddev->raid_disks = info->raid_disks;
6373 /* don't set md_minor, it is determined by which /dev/md* was
6374 * openned
6376 if (info->state & (1<<MD_SB_CLEAN))
6377 mddev->recovery_cp = MaxSector;
6378 else
6379 mddev->recovery_cp = 0;
6380 mddev->persistent = ! info->not_persistent;
6381 mddev->external = 0;
6383 mddev->layout = info->layout;
6384 mddev->chunk_sectors = info->chunk_size >> 9;
6386 mddev->max_disks = MD_SB_DISKS;
6388 if (mddev->persistent)
6389 mddev->flags = 0;
6390 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6392 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
6393 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
6394 mddev->bitmap_info.offset = 0;
6396 mddev->reshape_position = MaxSector;
6399 * Generate a 128 bit UUID
6401 get_random_bytes(mddev->uuid, 16);
6403 mddev->new_level = mddev->level;
6404 mddev->new_chunk_sectors = mddev->chunk_sectors;
6405 mddev->new_layout = mddev->layout;
6406 mddev->delta_disks = 0;
6407 mddev->reshape_backwards = 0;
6409 return 0;
6412 void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
6414 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6416 if (mddev->external_size)
6417 return;
6419 mddev->array_sectors = array_sectors;
6421 EXPORT_SYMBOL(md_set_array_sectors);
6423 static int update_size(struct mddev *mddev, sector_t num_sectors)
6425 struct md_rdev *rdev;
6426 int rv;
6427 int fit = (num_sectors == 0);
6429 if (mddev->pers->resize == NULL)
6430 return -EINVAL;
6431 /* The "num_sectors" is the number of sectors of each device that
6432 * is used. This can only make sense for arrays with redundancy.
6433 * linear and raid0 always use whatever space is available. We can only
6434 * consider changing this number if no resync or reconstruction is
6435 * happening, and if the new size is acceptable. It must fit before the
6436 * sb_start or, if that is <data_offset, it must fit before the size
6437 * of each device. If num_sectors is zero, we find the largest size
6438 * that fits.
6440 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6441 mddev->sync_thread)
6442 return -EBUSY;
6443 if (mddev->ro)
6444 return -EROFS;
6446 rdev_for_each(rdev, mddev) {
6447 sector_t avail = rdev->sectors;
6449 if (fit && (num_sectors == 0 || num_sectors > avail))
6450 num_sectors = avail;
6451 if (avail < num_sectors)
6452 return -ENOSPC;
6454 rv = mddev->pers->resize(mddev, num_sectors);
6455 if (!rv)
6456 revalidate_disk(mddev->gendisk);
6457 return rv;
6460 static int update_raid_disks(struct mddev *mddev, int raid_disks)
6462 int rv;
6463 struct md_rdev *rdev;
6464 /* change the number of raid disks */
6465 if (mddev->pers->check_reshape == NULL)
6466 return -EINVAL;
6467 if (mddev->ro)
6468 return -EROFS;
6469 if (raid_disks <= 0 ||
6470 (mddev->max_disks && raid_disks >= mddev->max_disks))
6471 return -EINVAL;
6472 if (mddev->sync_thread ||
6473 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6474 mddev->reshape_position != MaxSector)
6475 return -EBUSY;
6477 rdev_for_each(rdev, mddev) {
6478 if (mddev->raid_disks < raid_disks &&
6479 rdev->data_offset < rdev->new_data_offset)
6480 return -EINVAL;
6481 if (mddev->raid_disks > raid_disks &&
6482 rdev->data_offset > rdev->new_data_offset)
6483 return -EINVAL;
6486 mddev->delta_disks = raid_disks - mddev->raid_disks;
6487 if (mddev->delta_disks < 0)
6488 mddev->reshape_backwards = 1;
6489 else if (mddev->delta_disks > 0)
6490 mddev->reshape_backwards = 0;
6492 rv = mddev->pers->check_reshape(mddev);
6493 if (rv < 0) {
6494 mddev->delta_disks = 0;
6495 mddev->reshape_backwards = 0;
6497 return rv;
6501 * update_array_info is used to change the configuration of an
6502 * on-line array.
6503 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6504 * fields in the info are checked against the array.
6505 * Any differences that cannot be handled will cause an error.
6506 * Normally, only one change can be managed at a time.
6508 static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
6510 int rv = 0;
6511 int cnt = 0;
6512 int state = 0;
6514 /* calculate expected state,ignoring low bits */
6515 if (mddev->bitmap && mddev->bitmap_info.offset)
6516 state |= (1 << MD_SB_BITMAP_PRESENT);
6518 if (mddev->major_version != info->major_version ||
6519 mddev->minor_version != info->minor_version ||
6520 /* mddev->patch_version != info->patch_version || */
6521 mddev->ctime != info->ctime ||
6522 mddev->level != info->level ||
6523 /* mddev->layout != info->layout || */
6524 mddev->persistent != !info->not_persistent ||
6525 mddev->chunk_sectors != info->chunk_size >> 9 ||
6526 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6527 ((state^info->state) & 0xfffffe00)
6529 return -EINVAL;
6530 /* Check there is only one change */
6531 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6532 cnt++;
6533 if (mddev->raid_disks != info->raid_disks)
6534 cnt++;
6535 if (mddev->layout != info->layout)
6536 cnt++;
6537 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6538 cnt++;
6539 if (cnt == 0)
6540 return 0;
6541 if (cnt > 1)
6542 return -EINVAL;
6544 if (mddev->layout != info->layout) {
6545 /* Change layout
6546 * we don't need to do anything at the md level, the
6547 * personality will take care of it all.
6549 if (mddev->pers->check_reshape == NULL)
6550 return -EINVAL;
6551 else {
6552 mddev->new_layout = info->layout;
6553 rv = mddev->pers->check_reshape(mddev);
6554 if (rv)
6555 mddev->new_layout = mddev->layout;
6556 return rv;
6559 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6560 rv = update_size(mddev, (sector_t)info->size * 2);
6562 if (mddev->raid_disks != info->raid_disks)
6563 rv = update_raid_disks(mddev, info->raid_disks);
6565 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
6566 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6567 rv = -EINVAL;
6568 goto err;
6570 if (mddev->recovery || mddev->sync_thread) {
6571 rv = -EBUSY;
6572 goto err;
6574 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6575 struct bitmap *bitmap;
6576 /* add the bitmap */
6577 if (mddev->bitmap) {
6578 rv = -EEXIST;
6579 goto err;
6581 if (mddev->bitmap_info.default_offset == 0) {
6582 rv = -EINVAL;
6583 goto err;
6585 mddev->bitmap_info.offset =
6586 mddev->bitmap_info.default_offset;
6587 mddev->bitmap_info.space =
6588 mddev->bitmap_info.default_space;
6589 mddev->pers->quiesce(mddev, 1);
6590 bitmap = bitmap_create(mddev, -1);
6591 if (!IS_ERR(bitmap)) {
6592 mddev->bitmap = bitmap;
6593 rv = bitmap_load(mddev);
6594 } else
6595 rv = PTR_ERR(bitmap);
6596 if (rv)
6597 bitmap_destroy(mddev);
6598 mddev->pers->quiesce(mddev, 0);
6599 } else {
6600 /* remove the bitmap */
6601 if (!mddev->bitmap) {
6602 rv = -ENOENT;
6603 goto err;
6605 if (mddev->bitmap->storage.file) {
6606 rv = -EINVAL;
6607 goto err;
6609 mddev->pers->quiesce(mddev, 1);
6610 bitmap_destroy(mddev);
6611 mddev->pers->quiesce(mddev, 0);
6612 mddev->bitmap_info.offset = 0;
6615 md_update_sb(mddev, 1);
6616 return rv;
6617 err:
6618 return rv;
6621 static int set_disk_faulty(struct mddev *mddev, dev_t dev)
6623 struct md_rdev *rdev;
6624 int err = 0;
6626 if (mddev->pers == NULL)
6627 return -ENODEV;
6629 rcu_read_lock();
6630 rdev = find_rdev_rcu(mddev, dev);
6631 if (!rdev)
6632 err = -ENODEV;
6633 else {
6634 md_error(mddev, rdev);
6635 if (!test_bit(Faulty, &rdev->flags))
6636 err = -EBUSY;
6638 rcu_read_unlock();
6639 return err;
6643 * We have a problem here : there is no easy way to give a CHS
6644 * virtual geometry. We currently pretend that we have a 2 heads
6645 * 4 sectors (with a BIG number of cylinders...). This drives
6646 * dosfs just mad... ;-)
6648 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6650 struct mddev *mddev = bdev->bd_disk->private_data;
6652 geo->heads = 2;
6653 geo->sectors = 4;
6654 geo->cylinders = mddev->array_sectors / 8;
6655 return 0;
6658 static inline bool md_ioctl_valid(unsigned int cmd)
6660 switch (cmd) {
6661 case ADD_NEW_DISK:
6662 case BLKROSET:
6663 case GET_ARRAY_INFO:
6664 case GET_BITMAP_FILE:
6665 case GET_DISK_INFO:
6666 case HOT_ADD_DISK:
6667 case HOT_REMOVE_DISK:
6668 case RAID_AUTORUN:
6669 case RAID_VERSION:
6670 case RESTART_ARRAY_RW:
6671 case RUN_ARRAY:
6672 case SET_ARRAY_INFO:
6673 case SET_BITMAP_FILE:
6674 case SET_DISK_FAULTY:
6675 case STOP_ARRAY:
6676 case STOP_ARRAY_RO:
6677 case CLUSTERED_DISK_NACK:
6678 return true;
6679 default:
6680 return false;
6684 static int md_ioctl(struct block_device *bdev, fmode_t mode,
6685 unsigned int cmd, unsigned long arg)
6687 int err = 0;
6688 void __user *argp = (void __user *)arg;
6689 struct mddev *mddev = NULL;
6690 int ro;
6692 if (!md_ioctl_valid(cmd))
6693 return -ENOTTY;
6695 switch (cmd) {
6696 case RAID_VERSION:
6697 case GET_ARRAY_INFO:
6698 case GET_DISK_INFO:
6699 break;
6700 default:
6701 if (!capable(CAP_SYS_ADMIN))
6702 return -EACCES;
6706 * Commands dealing with the RAID driver but not any
6707 * particular array:
6709 switch (cmd) {
6710 case RAID_VERSION:
6711 err = get_version(argp);
6712 goto out;
6714 #ifndef MODULE
6715 case RAID_AUTORUN:
6716 err = 0;
6717 autostart_arrays(arg);
6718 goto out;
6719 #endif
6720 default:;
6724 * Commands creating/starting a new array:
6727 mddev = bdev->bd_disk->private_data;
6729 if (!mddev) {
6730 BUG();
6731 goto out;
6734 /* Some actions do not requires the mutex */
6735 switch (cmd) {
6736 case GET_ARRAY_INFO:
6737 if (!mddev->raid_disks && !mddev->external)
6738 err = -ENODEV;
6739 else
6740 err = get_array_info(mddev, argp);
6741 goto out;
6743 case GET_DISK_INFO:
6744 if (!mddev->raid_disks && !mddev->external)
6745 err = -ENODEV;
6746 else
6747 err = get_disk_info(mddev, argp);
6748 goto out;
6750 case SET_DISK_FAULTY:
6751 err = set_disk_faulty(mddev, new_decode_dev(arg));
6752 goto out;
6754 case GET_BITMAP_FILE:
6755 err = get_bitmap_file(mddev, argp);
6756 goto out;
6760 if (cmd == ADD_NEW_DISK)
6761 /* need to ensure md_delayed_delete() has completed */
6762 flush_workqueue(md_misc_wq);
6764 if (cmd == HOT_REMOVE_DISK)
6765 /* need to ensure recovery thread has run */
6766 wait_event_interruptible_timeout(mddev->sb_wait,
6767 !test_bit(MD_RECOVERY_NEEDED,
6768 &mddev->flags),
6769 msecs_to_jiffies(5000));
6770 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6771 /* Need to flush page cache, and ensure no-one else opens
6772 * and writes
6774 mutex_lock(&mddev->open_mutex);
6775 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
6776 mutex_unlock(&mddev->open_mutex);
6777 err = -EBUSY;
6778 goto out;
6780 set_bit(MD_STILL_CLOSED, &mddev->flags);
6781 mutex_unlock(&mddev->open_mutex);
6782 sync_blockdev(bdev);
6784 err = mddev_lock(mddev);
6785 if (err) {
6786 printk(KERN_INFO
6787 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6788 err, cmd);
6789 goto out;
6792 if (cmd == SET_ARRAY_INFO) {
6793 mdu_array_info_t info;
6794 if (!arg)
6795 memset(&info, 0, sizeof(info));
6796 else if (copy_from_user(&info, argp, sizeof(info))) {
6797 err = -EFAULT;
6798 goto unlock;
6800 if (mddev->pers) {
6801 err = update_array_info(mddev, &info);
6802 if (err) {
6803 printk(KERN_WARNING "md: couldn't update"
6804 " array info. %d\n", err);
6805 goto unlock;
6807 goto unlock;
6809 if (!list_empty(&mddev->disks)) {
6810 printk(KERN_WARNING
6811 "md: array %s already has disks!\n",
6812 mdname(mddev));
6813 err = -EBUSY;
6814 goto unlock;
6816 if (mddev->raid_disks) {
6817 printk(KERN_WARNING
6818 "md: array %s already initialised!\n",
6819 mdname(mddev));
6820 err = -EBUSY;
6821 goto unlock;
6823 err = set_array_info(mddev, &info);
6824 if (err) {
6825 printk(KERN_WARNING "md: couldn't set"
6826 " array info. %d\n", err);
6827 goto unlock;
6829 goto unlock;
6833 * Commands querying/configuring an existing array:
6835 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
6836 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
6837 if ((!mddev->raid_disks && !mddev->external)
6838 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6839 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6840 && cmd != GET_BITMAP_FILE) {
6841 err = -ENODEV;
6842 goto unlock;
6846 * Commands even a read-only array can execute:
6848 switch (cmd) {
6849 case RESTART_ARRAY_RW:
6850 err = restart_array(mddev);
6851 goto unlock;
6853 case STOP_ARRAY:
6854 err = do_md_stop(mddev, 0, bdev);
6855 goto unlock;
6857 case STOP_ARRAY_RO:
6858 err = md_set_readonly(mddev, bdev);
6859 goto unlock;
6861 case HOT_REMOVE_DISK:
6862 err = hot_remove_disk(mddev, new_decode_dev(arg));
6863 goto unlock;
6865 case ADD_NEW_DISK:
6866 /* We can support ADD_NEW_DISK on read-only arrays
6867 * on if we are re-adding a preexisting device.
6868 * So require mddev->pers and MD_DISK_SYNC.
6870 if (mddev->pers) {
6871 mdu_disk_info_t info;
6872 if (copy_from_user(&info, argp, sizeof(info)))
6873 err = -EFAULT;
6874 else if (!(info.state & (1<<MD_DISK_SYNC)))
6875 /* Need to clear read-only for this */
6876 break;
6877 else
6878 err = add_new_disk(mddev, &info);
6879 goto unlock;
6881 break;
6883 case BLKROSET:
6884 if (get_user(ro, (int __user *)(arg))) {
6885 err = -EFAULT;
6886 goto unlock;
6888 err = -EINVAL;
6890 /* if the bdev is going readonly the value of mddev->ro
6891 * does not matter, no writes are coming
6893 if (ro)
6894 goto unlock;
6896 /* are we are already prepared for writes? */
6897 if (mddev->ro != 1)
6898 goto unlock;
6900 /* transitioning to readauto need only happen for
6901 * arrays that call md_write_start
6903 if (mddev->pers) {
6904 err = restart_array(mddev);
6905 if (err == 0) {
6906 mddev->ro = 2;
6907 set_disk_ro(mddev->gendisk, 0);
6910 goto unlock;
6914 * The remaining ioctls are changing the state of the
6915 * superblock, so we do not allow them on read-only arrays.
6917 if (mddev->ro && mddev->pers) {
6918 if (mddev->ro == 2) {
6919 mddev->ro = 0;
6920 sysfs_notify_dirent_safe(mddev->sysfs_state);
6921 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6922 /* mddev_unlock will wake thread */
6923 /* If a device failed while we were read-only, we
6924 * need to make sure the metadata is updated now.
6926 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6927 mddev_unlock(mddev);
6928 wait_event(mddev->sb_wait,
6929 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6930 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
6931 mddev_lock_nointr(mddev);
6933 } else {
6934 err = -EROFS;
6935 goto unlock;
6939 switch (cmd) {
6940 case ADD_NEW_DISK:
6942 mdu_disk_info_t info;
6943 if (copy_from_user(&info, argp, sizeof(info)))
6944 err = -EFAULT;
6945 else
6946 err = add_new_disk(mddev, &info);
6947 goto unlock;
6950 case CLUSTERED_DISK_NACK:
6951 if (mddev_is_clustered(mddev))
6952 md_cluster_ops->new_disk_ack(mddev, false);
6953 else
6954 err = -EINVAL;
6955 goto unlock;
6957 case HOT_ADD_DISK:
6958 err = hot_add_disk(mddev, new_decode_dev(arg));
6959 goto unlock;
6961 case RUN_ARRAY:
6962 err = do_md_run(mddev);
6963 goto unlock;
6965 case SET_BITMAP_FILE:
6966 err = set_bitmap_file(mddev, (int)arg);
6967 goto unlock;
6969 default:
6970 err = -EINVAL;
6971 goto unlock;
6974 unlock:
6975 if (mddev->hold_active == UNTIL_IOCTL &&
6976 err != -EINVAL)
6977 mddev->hold_active = 0;
6978 mddev_unlock(mddev);
6979 out:
6980 return err;
6982 #ifdef CONFIG_COMPAT
6983 static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6984 unsigned int cmd, unsigned long arg)
6986 switch (cmd) {
6987 case HOT_REMOVE_DISK:
6988 case HOT_ADD_DISK:
6989 case SET_DISK_FAULTY:
6990 case SET_BITMAP_FILE:
6991 /* These take in integer arg, do not convert */
6992 break;
6993 default:
6994 arg = (unsigned long)compat_ptr(arg);
6995 break;
6998 return md_ioctl(bdev, mode, cmd, arg);
7000 #endif /* CONFIG_COMPAT */
7002 static int md_open(struct block_device *bdev, fmode_t mode)
7005 * Succeed if we can lock the mddev, which confirms that
7006 * it isn't being stopped right now.
7008 struct mddev *mddev = mddev_find(bdev->bd_dev);
7009 int err;
7011 if (!mddev)
7012 return -ENODEV;
7014 if (mddev->gendisk != bdev->bd_disk) {
7015 /* we are racing with mddev_put which is discarding this
7016 * bd_disk.
7018 mddev_put(mddev);
7019 /* Wait until bdev->bd_disk is definitely gone */
7020 flush_workqueue(md_misc_wq);
7021 /* Then retry the open from the top */
7022 return -ERESTARTSYS;
7024 BUG_ON(mddev != bdev->bd_disk->private_data);
7026 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
7027 goto out;
7029 err = 0;
7030 atomic_inc(&mddev->openers);
7031 clear_bit(MD_STILL_CLOSED, &mddev->flags);
7032 mutex_unlock(&mddev->open_mutex);
7034 check_disk_change(bdev);
7035 out:
7036 return err;
7039 static void md_release(struct gendisk *disk, fmode_t mode)
7041 struct mddev *mddev = disk->private_data;
7043 BUG_ON(!mddev);
7044 atomic_dec(&mddev->openers);
7045 mddev_put(mddev);
7048 static int md_media_changed(struct gendisk *disk)
7050 struct mddev *mddev = disk->private_data;
7052 return mddev->changed;
7055 static int md_revalidate(struct gendisk *disk)
7057 struct mddev *mddev = disk->private_data;
7059 mddev->changed = 0;
7060 return 0;
7062 static const struct block_device_operations md_fops =
7064 .owner = THIS_MODULE,
7065 .open = md_open,
7066 .release = md_release,
7067 .ioctl = md_ioctl,
7068 #ifdef CONFIG_COMPAT
7069 .compat_ioctl = md_compat_ioctl,
7070 #endif
7071 .getgeo = md_getgeo,
7072 .media_changed = md_media_changed,
7073 .revalidate_disk= md_revalidate,
7076 static int md_thread(void *arg)
7078 struct md_thread *thread = arg;
7081 * md_thread is a 'system-thread', it's priority should be very
7082 * high. We avoid resource deadlocks individually in each
7083 * raid personality. (RAID5 does preallocation) We also use RR and
7084 * the very same RT priority as kswapd, thus we will never get
7085 * into a priority inversion deadlock.
7087 * we definitely have to have equal or higher priority than
7088 * bdflush, otherwise bdflush will deadlock if there are too
7089 * many dirty RAID5 blocks.
7092 allow_signal(SIGKILL);
7093 while (!kthread_should_stop()) {
7095 /* We need to wait INTERRUPTIBLE so that
7096 * we don't add to the load-average.
7097 * That means we need to be sure no signals are
7098 * pending
7100 if (signal_pending(current))
7101 flush_signals(current);
7103 wait_event_interruptible_timeout
7104 (thread->wqueue,
7105 test_bit(THREAD_WAKEUP, &thread->flags)
7106 || kthread_should_stop(),
7107 thread->timeout);
7109 clear_bit(THREAD_WAKEUP, &thread->flags);
7110 if (!kthread_should_stop())
7111 thread->run(thread);
7114 return 0;
7117 void md_wakeup_thread(struct md_thread *thread)
7119 if (thread) {
7120 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
7121 set_bit(THREAD_WAKEUP, &thread->flags);
7122 wake_up(&thread->wqueue);
7125 EXPORT_SYMBOL(md_wakeup_thread);
7127 struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7128 struct mddev *mddev, const char *name)
7130 struct md_thread *thread;
7132 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
7133 if (!thread)
7134 return NULL;
7136 init_waitqueue_head(&thread->wqueue);
7138 thread->run = run;
7139 thread->mddev = mddev;
7140 thread->timeout = MAX_SCHEDULE_TIMEOUT;
7141 thread->tsk = kthread_run(md_thread, thread,
7142 "%s_%s",
7143 mdname(thread->mddev),
7144 name);
7145 if (IS_ERR(thread->tsk)) {
7146 kfree(thread);
7147 return NULL;
7149 return thread;
7151 EXPORT_SYMBOL(md_register_thread);
7153 void md_unregister_thread(struct md_thread **threadp)
7155 struct md_thread *thread = *threadp;
7156 if (!thread)
7157 return;
7158 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
7159 /* Locking ensures that mddev_unlock does not wake_up a
7160 * non-existent thread
7162 spin_lock(&pers_lock);
7163 *threadp = NULL;
7164 spin_unlock(&pers_lock);
7166 kthread_stop(thread->tsk);
7167 kfree(thread);
7169 EXPORT_SYMBOL(md_unregister_thread);
7171 void md_error(struct mddev *mddev, struct md_rdev *rdev)
7173 if (!rdev || test_bit(Faulty, &rdev->flags))
7174 return;
7176 if (!mddev->pers || !mddev->pers->error_handler)
7177 return;
7178 mddev->pers->error_handler(mddev,rdev);
7179 if (mddev->degraded)
7180 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7181 sysfs_notify_dirent_safe(rdev->sysfs_state);
7182 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7183 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7184 md_wakeup_thread(mddev->thread);
7185 if (mddev->event_work.func)
7186 queue_work(md_misc_wq, &mddev->event_work);
7187 md_new_event_inintr(mddev);
7189 EXPORT_SYMBOL(md_error);
7191 /* seq_file implementation /proc/mdstat */
7193 static void status_unused(struct seq_file *seq)
7195 int i = 0;
7196 struct md_rdev *rdev;
7198 seq_printf(seq, "unused devices: ");
7200 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
7201 char b[BDEVNAME_SIZE];
7202 i++;
7203 seq_printf(seq, "%s ",
7204 bdevname(rdev->bdev,b));
7206 if (!i)
7207 seq_printf(seq, "<none>");
7209 seq_printf(seq, "\n");
7212 static int status_resync(struct seq_file *seq, struct mddev *mddev)
7214 sector_t max_sectors, resync, res;
7215 unsigned long dt, db;
7216 sector_t rt;
7217 int scale;
7218 unsigned int per_milli;
7220 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7221 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7222 max_sectors = mddev->resync_max_sectors;
7223 else
7224 max_sectors = mddev->dev_sectors;
7226 resync = mddev->curr_resync;
7227 if (resync <= 3) {
7228 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7229 /* Still cleaning up */
7230 resync = max_sectors;
7231 } else
7232 resync -= atomic_read(&mddev->recovery_active);
7234 if (resync == 0) {
7235 if (mddev->recovery_cp < MaxSector) {
7236 seq_printf(seq, "\tresync=PENDING");
7237 return 1;
7239 return 0;
7241 if (resync < 3) {
7242 seq_printf(seq, "\tresync=DELAYED");
7243 return 1;
7246 WARN_ON(max_sectors == 0);
7247 /* Pick 'scale' such that (resync>>scale)*1000 will fit
7248 * in a sector_t, and (max_sectors>>scale) will fit in a
7249 * u32, as those are the requirements for sector_div.
7250 * Thus 'scale' must be at least 10
7252 scale = 10;
7253 if (sizeof(sector_t) > sizeof(unsigned long)) {
7254 while ( max_sectors/2 > (1ULL<<(scale+32)))
7255 scale++;
7257 res = (resync>>scale)*1000;
7258 sector_div(res, (u32)((max_sectors>>scale)+1));
7260 per_milli = res;
7262 int i, x = per_milli/50, y = 20-x;
7263 seq_printf(seq, "[");
7264 for (i = 0; i < x; i++)
7265 seq_printf(seq, "=");
7266 seq_printf(seq, ">");
7267 for (i = 0; i < y; i++)
7268 seq_printf(seq, ".");
7269 seq_printf(seq, "] ");
7271 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
7272 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7273 "reshape" :
7274 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7275 "check" :
7276 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7277 "resync" : "recovery"))),
7278 per_milli/10, per_milli % 10,
7279 (unsigned long long) resync/2,
7280 (unsigned long long) max_sectors/2);
7283 * dt: time from mark until now
7284 * db: blocks written from mark until now
7285 * rt: remaining time
7287 * rt is a sector_t, so could be 32bit or 64bit.
7288 * So we divide before multiply in case it is 32bit and close
7289 * to the limit.
7290 * We scale the divisor (db) by 32 to avoid losing precision
7291 * near the end of resync when the number of remaining sectors
7292 * is close to 'db'.
7293 * We then divide rt by 32 after multiplying by db to compensate.
7294 * The '+1' avoids division by zero if db is very small.
7296 dt = ((jiffies - mddev->resync_mark) / HZ);
7297 if (!dt) dt++;
7298 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7299 - mddev->resync_mark_cnt;
7301 rt = max_sectors - resync; /* number of remaining sectors */
7302 sector_div(rt, db/32+1);
7303 rt *= dt;
7304 rt >>= 5;
7306 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7307 ((unsigned long)rt % 60)/6);
7309 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
7310 return 1;
7313 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7315 struct list_head *tmp;
7316 loff_t l = *pos;
7317 struct mddev *mddev;
7319 if (l >= 0x10000)
7320 return NULL;
7321 if (!l--)
7322 /* header */
7323 return (void*)1;
7325 spin_lock(&all_mddevs_lock);
7326 list_for_each(tmp,&all_mddevs)
7327 if (!l--) {
7328 mddev = list_entry(tmp, struct mddev, all_mddevs);
7329 mddev_get(mddev);
7330 spin_unlock(&all_mddevs_lock);
7331 return mddev;
7333 spin_unlock(&all_mddevs_lock);
7334 if (!l--)
7335 return (void*)2;/* tail */
7336 return NULL;
7339 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7341 struct list_head *tmp;
7342 struct mddev *next_mddev, *mddev = v;
7344 ++*pos;
7345 if (v == (void*)2)
7346 return NULL;
7348 spin_lock(&all_mddevs_lock);
7349 if (v == (void*)1)
7350 tmp = all_mddevs.next;
7351 else
7352 tmp = mddev->all_mddevs.next;
7353 if (tmp != &all_mddevs)
7354 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
7355 else {
7356 next_mddev = (void*)2;
7357 *pos = 0x10000;
7359 spin_unlock(&all_mddevs_lock);
7361 if (v != (void*)1)
7362 mddev_put(mddev);
7363 return next_mddev;
7367 static void md_seq_stop(struct seq_file *seq, void *v)
7369 struct mddev *mddev = v;
7371 if (mddev && v != (void*)1 && v != (void*)2)
7372 mddev_put(mddev);
7375 static int md_seq_show(struct seq_file *seq, void *v)
7377 struct mddev *mddev = v;
7378 sector_t sectors;
7379 struct md_rdev *rdev;
7381 if (v == (void*)1) {
7382 struct md_personality *pers;
7383 seq_printf(seq, "Personalities : ");
7384 spin_lock(&pers_lock);
7385 list_for_each_entry(pers, &pers_list, list)
7386 seq_printf(seq, "[%s] ", pers->name);
7388 spin_unlock(&pers_lock);
7389 seq_printf(seq, "\n");
7390 seq->poll_event = atomic_read(&md_event_count);
7391 return 0;
7393 if (v == (void*)2) {
7394 status_unused(seq);
7395 return 0;
7398 spin_lock(&mddev->lock);
7399 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7400 seq_printf(seq, "%s : %sactive", mdname(mddev),
7401 mddev->pers ? "" : "in");
7402 if (mddev->pers) {
7403 if (mddev->ro==1)
7404 seq_printf(seq, " (read-only)");
7405 if (mddev->ro==2)
7406 seq_printf(seq, " (auto-read-only)");
7407 seq_printf(seq, " %s", mddev->pers->name);
7410 sectors = 0;
7411 rcu_read_lock();
7412 rdev_for_each_rcu(rdev, mddev) {
7413 char b[BDEVNAME_SIZE];
7414 seq_printf(seq, " %s[%d]",
7415 bdevname(rdev->bdev,b), rdev->desc_nr);
7416 if (test_bit(WriteMostly, &rdev->flags))
7417 seq_printf(seq, "(W)");
7418 if (test_bit(Journal, &rdev->flags))
7419 seq_printf(seq, "(J)");
7420 if (test_bit(Faulty, &rdev->flags)) {
7421 seq_printf(seq, "(F)");
7422 continue;
7424 if (rdev->raid_disk < 0)
7425 seq_printf(seq, "(S)"); /* spare */
7426 if (test_bit(Replacement, &rdev->flags))
7427 seq_printf(seq, "(R)");
7428 sectors += rdev->sectors;
7430 rcu_read_unlock();
7432 if (!list_empty(&mddev->disks)) {
7433 if (mddev->pers)
7434 seq_printf(seq, "\n %llu blocks",
7435 (unsigned long long)
7436 mddev->array_sectors / 2);
7437 else
7438 seq_printf(seq, "\n %llu blocks",
7439 (unsigned long long)sectors / 2);
7441 if (mddev->persistent) {
7442 if (mddev->major_version != 0 ||
7443 mddev->minor_version != 90) {
7444 seq_printf(seq," super %d.%d",
7445 mddev->major_version,
7446 mddev->minor_version);
7448 } else if (mddev->external)
7449 seq_printf(seq, " super external:%s",
7450 mddev->metadata_type);
7451 else
7452 seq_printf(seq, " super non-persistent");
7454 if (mddev->pers) {
7455 mddev->pers->status(seq, mddev);
7456 seq_printf(seq, "\n ");
7457 if (mddev->pers->sync_request) {
7458 if (status_resync(seq, mddev))
7459 seq_printf(seq, "\n ");
7461 } else
7462 seq_printf(seq, "\n ");
7464 bitmap_status(seq, mddev->bitmap);
7466 seq_printf(seq, "\n");
7468 spin_unlock(&mddev->lock);
7470 return 0;
7473 static const struct seq_operations md_seq_ops = {
7474 .start = md_seq_start,
7475 .next = md_seq_next,
7476 .stop = md_seq_stop,
7477 .show = md_seq_show,
7480 static int md_seq_open(struct inode *inode, struct file *file)
7482 struct seq_file *seq;
7483 int error;
7485 error = seq_open(file, &md_seq_ops);
7486 if (error)
7487 return error;
7489 seq = file->private_data;
7490 seq->poll_event = atomic_read(&md_event_count);
7491 return error;
7494 static int md_unloading;
7495 static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7497 struct seq_file *seq = filp->private_data;
7498 int mask;
7500 if (md_unloading)
7501 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
7502 poll_wait(filp, &md_event_waiters, wait);
7504 /* always allow read */
7505 mask = POLLIN | POLLRDNORM;
7507 if (seq->poll_event != atomic_read(&md_event_count))
7508 mask |= POLLERR | POLLPRI;
7509 return mask;
7512 static const struct file_operations md_seq_fops = {
7513 .owner = THIS_MODULE,
7514 .open = md_seq_open,
7515 .read = seq_read,
7516 .llseek = seq_lseek,
7517 .release = seq_release_private,
7518 .poll = mdstat_poll,
7521 int register_md_personality(struct md_personality *p)
7523 printk(KERN_INFO "md: %s personality registered for level %d\n",
7524 p->name, p->level);
7525 spin_lock(&pers_lock);
7526 list_add_tail(&p->list, &pers_list);
7527 spin_unlock(&pers_lock);
7528 return 0;
7530 EXPORT_SYMBOL(register_md_personality);
7532 int unregister_md_personality(struct md_personality *p)
7534 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
7535 spin_lock(&pers_lock);
7536 list_del_init(&p->list);
7537 spin_unlock(&pers_lock);
7538 return 0;
7540 EXPORT_SYMBOL(unregister_md_personality);
7542 int register_md_cluster_operations(struct md_cluster_operations *ops,
7543 struct module *module)
7545 int ret = 0;
7546 spin_lock(&pers_lock);
7547 if (md_cluster_ops != NULL)
7548 ret = -EALREADY;
7549 else {
7550 md_cluster_ops = ops;
7551 md_cluster_mod = module;
7553 spin_unlock(&pers_lock);
7554 return ret;
7556 EXPORT_SYMBOL(register_md_cluster_operations);
7558 int unregister_md_cluster_operations(void)
7560 spin_lock(&pers_lock);
7561 md_cluster_ops = NULL;
7562 spin_unlock(&pers_lock);
7563 return 0;
7565 EXPORT_SYMBOL(unregister_md_cluster_operations);
7567 int md_setup_cluster(struct mddev *mddev, int nodes)
7569 int err;
7571 err = request_module("md-cluster");
7572 if (err) {
7573 pr_err("md-cluster module not found.\n");
7574 return -ENOENT;
7577 spin_lock(&pers_lock);
7578 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
7579 spin_unlock(&pers_lock);
7580 return -ENOENT;
7582 spin_unlock(&pers_lock);
7584 return md_cluster_ops->join(mddev, nodes);
7587 void md_cluster_stop(struct mddev *mddev)
7589 if (!md_cluster_ops)
7590 return;
7591 md_cluster_ops->leave(mddev);
7592 module_put(md_cluster_mod);
7595 static int is_mddev_idle(struct mddev *mddev, int init)
7597 struct md_rdev *rdev;
7598 int idle;
7599 int curr_events;
7601 idle = 1;
7602 rcu_read_lock();
7603 rdev_for_each_rcu(rdev, mddev) {
7604 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
7605 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7606 (int)part_stat_read(&disk->part0, sectors[1]) -
7607 atomic_read(&disk->sync_io);
7608 /* sync IO will cause sync_io to increase before the disk_stats
7609 * as sync_io is counted when a request starts, and
7610 * disk_stats is counted when it completes.
7611 * So resync activity will cause curr_events to be smaller than
7612 * when there was no such activity.
7613 * non-sync IO will cause disk_stat to increase without
7614 * increasing sync_io so curr_events will (eventually)
7615 * be larger than it was before. Once it becomes
7616 * substantially larger, the test below will cause
7617 * the array to appear non-idle, and resync will slow
7618 * down.
7619 * If there is a lot of outstanding resync activity when
7620 * we set last_event to curr_events, then all that activity
7621 * completing might cause the array to appear non-idle
7622 * and resync will be slowed down even though there might
7623 * not have been non-resync activity. This will only
7624 * happen once though. 'last_events' will soon reflect
7625 * the state where there is little or no outstanding
7626 * resync requests, and further resync activity will
7627 * always make curr_events less than last_events.
7630 if (init || curr_events - rdev->last_events > 64) {
7631 rdev->last_events = curr_events;
7632 idle = 0;
7635 rcu_read_unlock();
7636 return idle;
7639 void md_done_sync(struct mddev *mddev, int blocks, int ok)
7641 /* another "blocks" (512byte) blocks have been synced */
7642 atomic_sub(blocks, &mddev->recovery_active);
7643 wake_up(&mddev->recovery_wait);
7644 if (!ok) {
7645 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7646 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
7647 md_wakeup_thread(mddev->thread);
7648 // stop recovery, signal do_sync ....
7651 EXPORT_SYMBOL(md_done_sync);
7653 /* md_write_start(mddev, bi)
7654 * If we need to update some array metadata (e.g. 'active' flag
7655 * in superblock) before writing, schedule a superblock update
7656 * and wait for it to complete.
7658 void md_write_start(struct mddev *mddev, struct bio *bi)
7660 int did_change = 0;
7661 if (bio_data_dir(bi) != WRITE)
7662 return;
7664 BUG_ON(mddev->ro == 1);
7665 if (mddev->ro == 2) {
7666 /* need to switch to read/write */
7667 mddev->ro = 0;
7668 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7669 md_wakeup_thread(mddev->thread);
7670 md_wakeup_thread(mddev->sync_thread);
7671 did_change = 1;
7673 atomic_inc(&mddev->writes_pending);
7674 if (mddev->safemode == 1)
7675 mddev->safemode = 0;
7676 if (mddev->in_sync) {
7677 spin_lock(&mddev->lock);
7678 if (mddev->in_sync) {
7679 mddev->in_sync = 0;
7680 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
7681 set_bit(MD_CHANGE_PENDING, &mddev->flags);
7682 md_wakeup_thread(mddev->thread);
7683 did_change = 1;
7685 spin_unlock(&mddev->lock);
7687 if (did_change)
7688 sysfs_notify_dirent_safe(mddev->sysfs_state);
7689 wait_event(mddev->sb_wait,
7690 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
7692 EXPORT_SYMBOL(md_write_start);
7694 void md_write_end(struct mddev *mddev)
7696 if (atomic_dec_and_test(&mddev->writes_pending)) {
7697 if (mddev->safemode == 2)
7698 md_wakeup_thread(mddev->thread);
7699 else if (mddev->safemode_delay)
7700 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7703 EXPORT_SYMBOL(md_write_end);
7705 /* md_allow_write(mddev)
7706 * Calling this ensures that the array is marked 'active' so that writes
7707 * may proceed without blocking. It is important to call this before
7708 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7709 * Must be called with mddev_lock held.
7711 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7712 * is dropped, so return -EAGAIN after notifying userspace.
7714 int md_allow_write(struct mddev *mddev)
7716 if (!mddev->pers)
7717 return 0;
7718 if (mddev->ro)
7719 return 0;
7720 if (!mddev->pers->sync_request)
7721 return 0;
7723 spin_lock(&mddev->lock);
7724 if (mddev->in_sync) {
7725 mddev->in_sync = 0;
7726 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
7727 set_bit(MD_CHANGE_PENDING, &mddev->flags);
7728 if (mddev->safemode_delay &&
7729 mddev->safemode == 0)
7730 mddev->safemode = 1;
7731 spin_unlock(&mddev->lock);
7732 md_update_sb(mddev, 0);
7733 sysfs_notify_dirent_safe(mddev->sysfs_state);
7734 } else
7735 spin_unlock(&mddev->lock);
7737 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
7738 return -EAGAIN;
7739 else
7740 return 0;
7742 EXPORT_SYMBOL_GPL(md_allow_write);
7744 #define SYNC_MARKS 10
7745 #define SYNC_MARK_STEP (3*HZ)
7746 #define UPDATE_FREQUENCY (5*60*HZ)
7747 void md_do_sync(struct md_thread *thread)
7749 struct mddev *mddev = thread->mddev;
7750 struct mddev *mddev2;
7751 unsigned int currspeed = 0,
7752 window;
7753 sector_t max_sectors,j, io_sectors, recovery_done;
7754 unsigned long mark[SYNC_MARKS];
7755 unsigned long update_time;
7756 sector_t mark_cnt[SYNC_MARKS];
7757 int last_mark,m;
7758 struct list_head *tmp;
7759 sector_t last_check;
7760 int skipped = 0;
7761 struct md_rdev *rdev;
7762 char *desc, *action = NULL;
7763 struct blk_plug plug;
7764 bool cluster_resync_finished = false;
7766 /* just incase thread restarts... */
7767 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7768 return;
7769 if (mddev->ro) {/* never try to sync a read-only array */
7770 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7771 return;
7774 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7775 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
7776 desc = "data-check";
7777 action = "check";
7778 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7779 desc = "requested-resync";
7780 action = "repair";
7781 } else
7782 desc = "resync";
7783 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7784 desc = "reshape";
7785 else
7786 desc = "recovery";
7788 mddev->last_sync_action = action ?: desc;
7790 /* we overload curr_resync somewhat here.
7791 * 0 == not engaged in resync at all
7792 * 2 == checking that there is no conflict with another sync
7793 * 1 == like 2, but have yielded to allow conflicting resync to
7794 * commense
7795 * other == active in resync - this many blocks
7797 * Before starting a resync we must have set curr_resync to
7798 * 2, and then checked that every "conflicting" array has curr_resync
7799 * less than ours. When we find one that is the same or higher
7800 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7801 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7802 * This will mean we have to start checking from the beginning again.
7806 do {
7807 mddev->curr_resync = 2;
7809 try_again:
7810 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7811 goto skip;
7812 for_each_mddev(mddev2, tmp) {
7813 if (mddev2 == mddev)
7814 continue;
7815 if (!mddev->parallel_resync
7816 && mddev2->curr_resync
7817 && match_mddev_units(mddev, mddev2)) {
7818 DEFINE_WAIT(wq);
7819 if (mddev < mddev2 && mddev->curr_resync == 2) {
7820 /* arbitrarily yield */
7821 mddev->curr_resync = 1;
7822 wake_up(&resync_wait);
7824 if (mddev > mddev2 && mddev->curr_resync == 1)
7825 /* no need to wait here, we can wait the next
7826 * time 'round when curr_resync == 2
7828 continue;
7829 /* We need to wait 'interruptible' so as not to
7830 * contribute to the load average, and not to
7831 * be caught by 'softlockup'
7833 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
7834 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7835 mddev2->curr_resync >= mddev->curr_resync) {
7836 printk(KERN_INFO "md: delaying %s of %s"
7837 " until %s has finished (they"
7838 " share one or more physical units)\n",
7839 desc, mdname(mddev), mdname(mddev2));
7840 mddev_put(mddev2);
7841 if (signal_pending(current))
7842 flush_signals(current);
7843 schedule();
7844 finish_wait(&resync_wait, &wq);
7845 goto try_again;
7847 finish_wait(&resync_wait, &wq);
7850 } while (mddev->curr_resync < 2);
7852 j = 0;
7853 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7854 /* resync follows the size requested by the personality,
7855 * which defaults to physical size, but can be virtual size
7857 max_sectors = mddev->resync_max_sectors;
7858 atomic64_set(&mddev->resync_mismatches, 0);
7859 /* we don't use the checkpoint if there's a bitmap */
7860 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7861 j = mddev->resync_min;
7862 else if (!mddev->bitmap)
7863 j = mddev->recovery_cp;
7865 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7866 max_sectors = mddev->resync_max_sectors;
7867 else {
7868 /* recovery follows the physical size of devices */
7869 max_sectors = mddev->dev_sectors;
7870 j = MaxSector;
7871 rcu_read_lock();
7872 rdev_for_each_rcu(rdev, mddev)
7873 if (rdev->raid_disk >= 0 &&
7874 !test_bit(Journal, &rdev->flags) &&
7875 !test_bit(Faulty, &rdev->flags) &&
7876 !test_bit(In_sync, &rdev->flags) &&
7877 rdev->recovery_offset < j)
7878 j = rdev->recovery_offset;
7879 rcu_read_unlock();
7881 /* If there is a bitmap, we need to make sure all
7882 * writes that started before we added a spare
7883 * complete before we start doing a recovery.
7884 * Otherwise the write might complete and (via
7885 * bitmap_endwrite) set a bit in the bitmap after the
7886 * recovery has checked that bit and skipped that
7887 * region.
7889 if (mddev->bitmap) {
7890 mddev->pers->quiesce(mddev, 1);
7891 mddev->pers->quiesce(mddev, 0);
7895 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7896 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7897 " %d KB/sec/disk.\n", speed_min(mddev));
7898 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
7899 "(but not more than %d KB/sec) for %s.\n",
7900 speed_max(mddev), desc);
7902 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
7904 io_sectors = 0;
7905 for (m = 0; m < SYNC_MARKS; m++) {
7906 mark[m] = jiffies;
7907 mark_cnt[m] = io_sectors;
7909 last_mark = 0;
7910 mddev->resync_mark = mark[last_mark];
7911 mddev->resync_mark_cnt = mark_cnt[last_mark];
7914 * Tune reconstruction:
7916 window = 32*(PAGE_SIZE/512);
7917 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7918 window/2, (unsigned long long)max_sectors/2);
7920 atomic_set(&mddev->recovery_active, 0);
7921 last_check = 0;
7923 if (j>2) {
7924 printk(KERN_INFO
7925 "md: resuming %s of %s from checkpoint.\n",
7926 desc, mdname(mddev));
7927 mddev->curr_resync = j;
7928 } else
7929 mddev->curr_resync = 3; /* no longer delayed */
7930 mddev->curr_resync_completed = j;
7931 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7932 md_new_event(mddev);
7933 update_time = jiffies;
7935 blk_start_plug(&plug);
7936 while (j < max_sectors) {
7937 sector_t sectors;
7939 skipped = 0;
7941 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7942 ((mddev->curr_resync > mddev->curr_resync_completed &&
7943 (mddev->curr_resync - mddev->curr_resync_completed)
7944 > (max_sectors >> 4)) ||
7945 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
7946 (j - mddev->curr_resync_completed)*2
7947 >= mddev->resync_max - mddev->curr_resync_completed ||
7948 mddev->curr_resync_completed > mddev->resync_max
7949 )) {
7950 /* time to update curr_resync_completed */
7951 wait_event(mddev->recovery_wait,
7952 atomic_read(&mddev->recovery_active) == 0);
7953 mddev->curr_resync_completed = j;
7954 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7955 j > mddev->recovery_cp)
7956 mddev->recovery_cp = j;
7957 update_time = jiffies;
7958 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
7959 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7962 while (j >= mddev->resync_max &&
7963 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7964 /* As this condition is controlled by user-space,
7965 * we can block indefinitely, so use '_interruptible'
7966 * to avoid triggering warnings.
7968 flush_signals(current); /* just in case */
7969 wait_event_interruptible(mddev->recovery_wait,
7970 mddev->resync_max > j
7971 || test_bit(MD_RECOVERY_INTR,
7972 &mddev->recovery));
7975 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7976 break;
7978 sectors = mddev->pers->sync_request(mddev, j, &skipped);
7979 if (sectors == 0) {
7980 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7981 break;
7984 if (!skipped) { /* actual IO requested */
7985 io_sectors += sectors;
7986 atomic_add(sectors, &mddev->recovery_active);
7989 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7990 break;
7992 j += sectors;
7993 if (j > max_sectors)
7994 /* when skipping, extra large numbers can be returned. */
7995 j = max_sectors;
7996 if (j > 2)
7997 mddev->curr_resync = j;
7998 mddev->curr_mark_cnt = io_sectors;
7999 if (last_check == 0)
8000 /* this is the earliest that rebuild will be
8001 * visible in /proc/mdstat
8003 md_new_event(mddev);
8005 if (last_check + window > io_sectors || j == max_sectors)
8006 continue;
8008 last_check = io_sectors;
8009 repeat:
8010 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8011 /* step marks */
8012 int next = (last_mark+1) % SYNC_MARKS;
8014 mddev->resync_mark = mark[next];
8015 mddev->resync_mark_cnt = mark_cnt[next];
8016 mark[next] = jiffies;
8017 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
8018 last_mark = next;
8021 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8022 break;
8025 * this loop exits only if either when we are slower than
8026 * the 'hard' speed limit, or the system was IO-idle for
8027 * a jiffy.
8028 * the system might be non-idle CPU-wise, but we only care
8029 * about not overloading the IO subsystem. (things like an
8030 * e2fsck being done on the RAID array should execute fast)
8032 cond_resched();
8034 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8035 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
8036 /((jiffies-mddev->resync_mark)/HZ +1) +1;
8038 if (currspeed > speed_min(mddev)) {
8039 if (currspeed > speed_max(mddev)) {
8040 msleep(500);
8041 goto repeat;
8043 if (!is_mddev_idle(mddev, 0)) {
8045 * Give other IO more of a chance.
8046 * The faster the devices, the less we wait.
8048 wait_event(mddev->recovery_wait,
8049 !atomic_read(&mddev->recovery_active));
8053 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
8054 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8055 ? "interrupted" : "done");
8057 * this also signals 'finished resyncing' to md_stop
8059 blk_finish_plug(&plug);
8060 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8062 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8063 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8064 mddev->curr_resync > 2) {
8065 mddev->curr_resync_completed = mddev->curr_resync;
8066 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8068 /* tell personality and other nodes that we are finished */
8069 if (mddev_is_clustered(mddev)) {
8070 md_cluster_ops->resync_finish(mddev);
8071 cluster_resync_finished = true;
8073 mddev->pers->sync_request(mddev, max_sectors, &skipped);
8075 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
8076 mddev->curr_resync > 2) {
8077 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8078 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8079 if (mddev->curr_resync >= mddev->recovery_cp) {
8080 printk(KERN_INFO
8081 "md: checkpointing %s of %s.\n",
8082 desc, mdname(mddev));
8083 if (test_bit(MD_RECOVERY_ERROR,
8084 &mddev->recovery))
8085 mddev->recovery_cp =
8086 mddev->curr_resync_completed;
8087 else
8088 mddev->recovery_cp =
8089 mddev->curr_resync;
8091 } else
8092 mddev->recovery_cp = MaxSector;
8093 } else {
8094 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8095 mddev->curr_resync = MaxSector;
8096 rcu_read_lock();
8097 rdev_for_each_rcu(rdev, mddev)
8098 if (rdev->raid_disk >= 0 &&
8099 mddev->delta_disks >= 0 &&
8100 !test_bit(Journal, &rdev->flags) &&
8101 !test_bit(Faulty, &rdev->flags) &&
8102 !test_bit(In_sync, &rdev->flags) &&
8103 rdev->recovery_offset < mddev->curr_resync)
8104 rdev->recovery_offset = mddev->curr_resync;
8105 rcu_read_unlock();
8108 skip:
8109 set_bit(MD_CHANGE_DEVS, &mddev->flags);
8111 if (mddev_is_clustered(mddev) &&
8112 test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8113 !cluster_resync_finished)
8114 md_cluster_ops->resync_finish(mddev);
8116 spin_lock(&mddev->lock);
8117 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8118 /* We completed so min/max setting can be forgotten if used. */
8119 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8120 mddev->resync_min = 0;
8121 mddev->resync_max = MaxSector;
8122 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8123 mddev->resync_min = mddev->curr_resync_completed;
8124 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
8125 mddev->curr_resync = 0;
8126 spin_unlock(&mddev->lock);
8128 wake_up(&resync_wait);
8129 md_wakeup_thread(mddev->thread);
8130 return;
8132 EXPORT_SYMBOL_GPL(md_do_sync);
8134 static int remove_and_add_spares(struct mddev *mddev,
8135 struct md_rdev *this)
8137 struct md_rdev *rdev;
8138 int spares = 0;
8139 int removed = 0;
8141 rdev_for_each(rdev, mddev)
8142 if ((this == NULL || rdev == this) &&
8143 rdev->raid_disk >= 0 &&
8144 !test_bit(Blocked, &rdev->flags) &&
8145 (test_bit(Faulty, &rdev->flags) ||
8146 (!test_bit(In_sync, &rdev->flags) &&
8147 !test_bit(Journal, &rdev->flags))) &&
8148 atomic_read(&rdev->nr_pending)==0) {
8149 if (mddev->pers->hot_remove_disk(
8150 mddev, rdev) == 0) {
8151 sysfs_unlink_rdev(mddev, rdev);
8152 rdev->raid_disk = -1;
8153 removed++;
8156 if (removed && mddev->kobj.sd)
8157 sysfs_notify(&mddev->kobj, NULL, "degraded");
8159 if (this && removed)
8160 goto no_add;
8162 rdev_for_each(rdev, mddev) {
8163 if (this && this != rdev)
8164 continue;
8165 if (test_bit(Candidate, &rdev->flags))
8166 continue;
8167 if (rdev->raid_disk >= 0 &&
8168 !test_bit(In_sync, &rdev->flags) &&
8169 !test_bit(Journal, &rdev->flags) &&
8170 !test_bit(Faulty, &rdev->flags))
8171 spares++;
8172 if (rdev->raid_disk >= 0)
8173 continue;
8174 if (test_bit(Faulty, &rdev->flags))
8175 continue;
8176 if (test_bit(Journal, &rdev->flags))
8177 continue;
8178 if (mddev->ro &&
8179 ! (rdev->saved_raid_disk >= 0 &&
8180 !test_bit(Bitmap_sync, &rdev->flags)))
8181 continue;
8183 rdev->recovery_offset = 0;
8184 if (mddev->pers->
8185 hot_add_disk(mddev, rdev) == 0) {
8186 if (sysfs_link_rdev(mddev, rdev))
8187 /* failure here is OK */;
8188 spares++;
8189 md_new_event(mddev);
8190 set_bit(MD_CHANGE_DEVS, &mddev->flags);
8193 no_add:
8194 if (removed)
8195 set_bit(MD_CHANGE_DEVS, &mddev->flags);
8196 return spares;
8199 static void md_start_sync(struct work_struct *ws)
8201 struct mddev *mddev = container_of(ws, struct mddev, del_work);
8202 int ret = 0;
8204 if (mddev_is_clustered(mddev)) {
8205 ret = md_cluster_ops->resync_start(mddev);
8206 if (ret) {
8207 mddev->sync_thread = NULL;
8208 goto out;
8212 mddev->sync_thread = md_register_thread(md_do_sync,
8213 mddev,
8214 "resync");
8215 out:
8216 if (!mddev->sync_thread) {
8217 if (!(mddev_is_clustered(mddev) && ret == -EAGAIN))
8218 printk(KERN_ERR "%s: could not start resync"
8219 " thread...\n",
8220 mdname(mddev));
8221 /* leave the spares where they are, it shouldn't hurt */
8222 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8223 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8224 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8225 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8226 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
8227 wake_up(&resync_wait);
8228 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8229 &mddev->recovery))
8230 if (mddev->sysfs_action)
8231 sysfs_notify_dirent_safe(mddev->sysfs_action);
8232 } else
8233 md_wakeup_thread(mddev->sync_thread);
8234 sysfs_notify_dirent_safe(mddev->sysfs_action);
8235 md_new_event(mddev);
8239 * This routine is regularly called by all per-raid-array threads to
8240 * deal with generic issues like resync and super-block update.
8241 * Raid personalities that don't have a thread (linear/raid0) do not
8242 * need this as they never do any recovery or update the superblock.
8244 * It does not do any resync itself, but rather "forks" off other threads
8245 * to do that as needed.
8246 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8247 * "->recovery" and create a thread at ->sync_thread.
8248 * When the thread finishes it sets MD_RECOVERY_DONE
8249 * and wakeups up this thread which will reap the thread and finish up.
8250 * This thread also removes any faulty devices (with nr_pending == 0).
8252 * The overall approach is:
8253 * 1/ if the superblock needs updating, update it.
8254 * 2/ If a recovery thread is running, don't do anything else.
8255 * 3/ If recovery has finished, clean up, possibly marking spares active.
8256 * 4/ If there are any faulty devices, remove them.
8257 * 5/ If array is degraded, try to add spares devices
8258 * 6/ If array has spares or is not in-sync, start a resync thread.
8260 void md_check_recovery(struct mddev *mddev)
8262 if (mddev->suspended)
8263 return;
8265 if (mddev->bitmap)
8266 bitmap_daemon_work(mddev);
8268 if (signal_pending(current)) {
8269 if (mddev->pers->sync_request && !mddev->external) {
8270 printk(KERN_INFO "md: %s in immediate safe mode\n",
8271 mdname(mddev));
8272 mddev->safemode = 2;
8274 flush_signals(current);
8277 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8278 return;
8279 if ( ! (
8280 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
8281 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8282 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
8283 (mddev->external == 0 && mddev->safemode == 1) ||
8284 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
8285 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
8287 return;
8289 if (mddev_trylock(mddev)) {
8290 int spares = 0;
8292 if (mddev->ro) {
8293 struct md_rdev *rdev;
8294 if (!mddev->external && mddev->in_sync)
8295 /* 'Blocked' flag not needed as failed devices
8296 * will be recorded if array switched to read/write.
8297 * Leaving it set will prevent the device
8298 * from being removed.
8300 rdev_for_each(rdev, mddev)
8301 clear_bit(Blocked, &rdev->flags);
8302 /* On a read-only array we can:
8303 * - remove failed devices
8304 * - add already-in_sync devices if the array itself
8305 * is in-sync.
8306 * As we only add devices that are already in-sync,
8307 * we can activate the spares immediately.
8309 remove_and_add_spares(mddev, NULL);
8310 /* There is no thread, but we need to call
8311 * ->spare_active and clear saved_raid_disk
8313 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
8314 md_reap_sync_thread(mddev);
8315 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
8316 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8317 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
8318 goto unlock;
8321 if (!mddev->external) {
8322 int did_change = 0;
8323 spin_lock(&mddev->lock);
8324 if (mddev->safemode &&
8325 !atomic_read(&mddev->writes_pending) &&
8326 !mddev->in_sync &&
8327 mddev->recovery_cp == MaxSector) {
8328 mddev->in_sync = 1;
8329 did_change = 1;
8330 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
8332 if (mddev->safemode == 1)
8333 mddev->safemode = 0;
8334 spin_unlock(&mddev->lock);
8335 if (did_change)
8336 sysfs_notify_dirent_safe(mddev->sysfs_state);
8339 if (mddev->flags & MD_UPDATE_SB_FLAGS)
8340 md_update_sb(mddev, 0);
8342 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8343 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8344 /* resync/recovery still happening */
8345 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8346 goto unlock;
8348 if (mddev->sync_thread) {
8349 md_reap_sync_thread(mddev);
8350 goto unlock;
8352 /* Set RUNNING before clearing NEEDED to avoid
8353 * any transients in the value of "sync_action".
8355 mddev->curr_resync_completed = 0;
8356 spin_lock(&mddev->lock);
8357 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
8358 spin_unlock(&mddev->lock);
8359 /* Clear some bits that don't mean anything, but
8360 * might be left set
8362 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8363 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
8365 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8366 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
8367 goto not_running;
8368 /* no recovery is running.
8369 * remove any failed drives, then
8370 * add spares if possible.
8371 * Spares are also removed and re-added, to allow
8372 * the personality to fail the re-add.
8375 if (mddev->reshape_position != MaxSector) {
8376 if (mddev->pers->check_reshape == NULL ||
8377 mddev->pers->check_reshape(mddev) != 0)
8378 /* Cannot proceed */
8379 goto not_running;
8380 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8381 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
8382 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
8383 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8384 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8385 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8386 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
8387 } else if (mddev->recovery_cp < MaxSector) {
8388 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8389 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
8390 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8391 /* nothing to be done ... */
8392 goto not_running;
8394 if (mddev->pers->sync_request) {
8395 if (spares) {
8396 /* We are adding a device or devices to an array
8397 * which has the bitmap stored on all devices.
8398 * So make sure all bitmap pages get written
8400 bitmap_write_all(mddev->bitmap);
8402 INIT_WORK(&mddev->del_work, md_start_sync);
8403 queue_work(md_misc_wq, &mddev->del_work);
8404 goto unlock;
8406 not_running:
8407 if (!mddev->sync_thread) {
8408 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
8409 wake_up(&resync_wait);
8410 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8411 &mddev->recovery))
8412 if (mddev->sysfs_action)
8413 sysfs_notify_dirent_safe(mddev->sysfs_action);
8415 unlock:
8416 wake_up(&mddev->sb_wait);
8417 mddev_unlock(mddev);
8420 EXPORT_SYMBOL(md_check_recovery);
8422 void md_reap_sync_thread(struct mddev *mddev)
8424 struct md_rdev *rdev;
8426 /* resync has finished, collect result */
8427 md_unregister_thread(&mddev->sync_thread);
8428 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8429 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8430 /* success...*/
8431 /* activate any spares */
8432 if (mddev->pers->spare_active(mddev)) {
8433 sysfs_notify(&mddev->kobj, NULL,
8434 "degraded");
8435 set_bit(MD_CHANGE_DEVS, &mddev->flags);
8438 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8439 mddev->pers->finish_reshape)
8440 mddev->pers->finish_reshape(mddev);
8442 /* If array is no-longer degraded, then any saved_raid_disk
8443 * information must be scrapped.
8445 if (!mddev->degraded)
8446 rdev_for_each(rdev, mddev)
8447 rdev->saved_raid_disk = -1;
8449 md_update_sb(mddev, 1);
8450 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
8451 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
8452 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8453 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8454 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8455 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8456 wake_up(&resync_wait);
8457 /* flag recovery needed just to double check */
8458 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8459 sysfs_notify_dirent_safe(mddev->sysfs_action);
8460 md_new_event(mddev);
8461 if (mddev->event_work.func)
8462 queue_work(md_misc_wq, &mddev->event_work);
8464 EXPORT_SYMBOL(md_reap_sync_thread);
8466 void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
8468 sysfs_notify_dirent_safe(rdev->sysfs_state);
8469 wait_event_timeout(rdev->blocked_wait,
8470 !test_bit(Blocked, &rdev->flags) &&
8471 !test_bit(BlockedBadBlocks, &rdev->flags),
8472 msecs_to_jiffies(5000));
8473 rdev_dec_pending(rdev, mddev);
8475 EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8477 void md_finish_reshape(struct mddev *mddev)
8479 /* called be personality module when reshape completes. */
8480 struct md_rdev *rdev;
8482 rdev_for_each(rdev, mddev) {
8483 if (rdev->data_offset > rdev->new_data_offset)
8484 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8485 else
8486 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8487 rdev->data_offset = rdev->new_data_offset;
8490 EXPORT_SYMBOL(md_finish_reshape);
8492 /* Bad block management.
8493 * We can record which blocks on each device are 'bad' and so just
8494 * fail those blocks, or that stripe, rather than the whole device.
8495 * Entries in the bad-block table are 64bits wide. This comprises:
8496 * Length of bad-range, in sectors: 0-511 for lengths 1-512
8497 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
8498 * A 'shift' can be set so that larger blocks are tracked and
8499 * consequently larger devices can be covered.
8500 * 'Acknowledged' flag - 1 bit. - the most significant bit.
8502 * Locking of the bad-block table uses a seqlock so md_is_badblock
8503 * might need to retry if it is very unlucky.
8504 * We will sometimes want to check for bad blocks in a bi_end_io function,
8505 * so we use the write_seqlock_irq variant.
8507 * When looking for a bad block we specify a range and want to
8508 * know if any block in the range is bad. So we binary-search
8509 * to the last range that starts at-or-before the given endpoint,
8510 * (or "before the sector after the target range")
8511 * then see if it ends after the given start.
8512 * We return
8513 * 0 if there are no known bad blocks in the range
8514 * 1 if there are known bad block which are all acknowledged
8515 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
8516 * plus the start/length of the first bad section we overlap.
8518 int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
8519 sector_t *first_bad, int *bad_sectors)
8521 int hi;
8522 int lo;
8523 u64 *p = bb->page;
8524 int rv;
8525 sector_t target = s + sectors;
8526 unsigned seq;
8528 if (bb->shift > 0) {
8529 /* round the start down, and the end up */
8530 s >>= bb->shift;
8531 target += (1<<bb->shift) - 1;
8532 target >>= bb->shift;
8533 sectors = target - s;
8535 /* 'target' is now the first block after the bad range */
8537 retry:
8538 seq = read_seqbegin(&bb->lock);
8539 lo = 0;
8540 rv = 0;
8541 hi = bb->count;
8543 /* Binary search between lo and hi for 'target'
8544 * i.e. for the last range that starts before 'target'
8546 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8547 * are known not to be the last range before target.
8548 * VARIANT: hi-lo is the number of possible
8549 * ranges, and decreases until it reaches 1
8551 while (hi - lo > 1) {
8552 int mid = (lo + hi) / 2;
8553 sector_t a = BB_OFFSET(p[mid]);
8554 if (a < target)
8555 /* This could still be the one, earlier ranges
8556 * could not. */
8557 lo = mid;
8558 else
8559 /* This and later ranges are definitely out. */
8560 hi = mid;
8562 /* 'lo' might be the last that started before target, but 'hi' isn't */
8563 if (hi > lo) {
8564 /* need to check all range that end after 's' to see if
8565 * any are unacknowledged.
8567 while (lo >= 0 &&
8568 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8569 if (BB_OFFSET(p[lo]) < target) {
8570 /* starts before the end, and finishes after
8571 * the start, so they must overlap
8573 if (rv != -1 && BB_ACK(p[lo]))
8574 rv = 1;
8575 else
8576 rv = -1;
8577 *first_bad = BB_OFFSET(p[lo]);
8578 *bad_sectors = BB_LEN(p[lo]);
8580 lo--;
8584 if (read_seqretry(&bb->lock, seq))
8585 goto retry;
8587 return rv;
8589 EXPORT_SYMBOL_GPL(md_is_badblock);
8592 * Add a range of bad blocks to the table.
8593 * This might extend the table, or might contract it
8594 * if two adjacent ranges can be merged.
8595 * We binary-search to find the 'insertion' point, then
8596 * decide how best to handle it.
8598 static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8599 int acknowledged)
8601 u64 *p;
8602 int lo, hi;
8603 int rv = 1;
8604 unsigned long flags;
8606 if (bb->shift < 0)
8607 /* badblocks are disabled */
8608 return 0;
8610 if (bb->shift) {
8611 /* round the start down, and the end up */
8612 sector_t next = s + sectors;
8613 s >>= bb->shift;
8614 next += (1<<bb->shift) - 1;
8615 next >>= bb->shift;
8616 sectors = next - s;
8619 write_seqlock_irqsave(&bb->lock, flags);
8621 p = bb->page;
8622 lo = 0;
8623 hi = bb->count;
8624 /* Find the last range that starts at-or-before 's' */
8625 while (hi - lo > 1) {
8626 int mid = (lo + hi) / 2;
8627 sector_t a = BB_OFFSET(p[mid]);
8628 if (a <= s)
8629 lo = mid;
8630 else
8631 hi = mid;
8633 if (hi > lo && BB_OFFSET(p[lo]) > s)
8634 hi = lo;
8636 if (hi > lo) {
8637 /* we found a range that might merge with the start
8638 * of our new range
8640 sector_t a = BB_OFFSET(p[lo]);
8641 sector_t e = a + BB_LEN(p[lo]);
8642 int ack = BB_ACK(p[lo]);
8643 if (e >= s) {
8644 /* Yes, we can merge with a previous range */
8645 if (s == a && s + sectors >= e)
8646 /* new range covers old */
8647 ack = acknowledged;
8648 else
8649 ack = ack && acknowledged;
8651 if (e < s + sectors)
8652 e = s + sectors;
8653 if (e - a <= BB_MAX_LEN) {
8654 p[lo] = BB_MAKE(a, e-a, ack);
8655 s = e;
8656 } else {
8657 /* does not all fit in one range,
8658 * make p[lo] maximal
8660 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8661 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8662 s = a + BB_MAX_LEN;
8664 sectors = e - s;
8667 if (sectors && hi < bb->count) {
8668 /* 'hi' points to the first range that starts after 's'.
8669 * Maybe we can merge with the start of that range */
8670 sector_t a = BB_OFFSET(p[hi]);
8671 sector_t e = a + BB_LEN(p[hi]);
8672 int ack = BB_ACK(p[hi]);
8673 if (a <= s + sectors) {
8674 /* merging is possible */
8675 if (e <= s + sectors) {
8676 /* full overlap */
8677 e = s + sectors;
8678 ack = acknowledged;
8679 } else
8680 ack = ack && acknowledged;
8682 a = s;
8683 if (e - a <= BB_MAX_LEN) {
8684 p[hi] = BB_MAKE(a, e-a, ack);
8685 s = e;
8686 } else {
8687 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8688 s = a + BB_MAX_LEN;
8690 sectors = e - s;
8691 lo = hi;
8692 hi++;
8695 if (sectors == 0 && hi < bb->count) {
8696 /* we might be able to combine lo and hi */
8697 /* Note: 's' is at the end of 'lo' */
8698 sector_t a = BB_OFFSET(p[hi]);
8699 int lolen = BB_LEN(p[lo]);
8700 int hilen = BB_LEN(p[hi]);
8701 int newlen = lolen + hilen - (s - a);
8702 if (s >= a && newlen < BB_MAX_LEN) {
8703 /* yes, we can combine them */
8704 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8705 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8706 memmove(p + hi, p + hi + 1,
8707 (bb->count - hi - 1) * 8);
8708 bb->count--;
8711 while (sectors) {
8712 /* didn't merge (it all).
8713 * Need to add a range just before 'hi' */
8714 if (bb->count >= MD_MAX_BADBLOCKS) {
8715 /* No room for more */
8716 rv = 0;
8717 break;
8718 } else {
8719 int this_sectors = sectors;
8720 memmove(p + hi + 1, p + hi,
8721 (bb->count - hi) * 8);
8722 bb->count++;
8724 if (this_sectors > BB_MAX_LEN)
8725 this_sectors = BB_MAX_LEN;
8726 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8727 sectors -= this_sectors;
8728 s += this_sectors;
8732 bb->changed = 1;
8733 if (!acknowledged)
8734 bb->unacked_exist = 1;
8735 write_sequnlock_irqrestore(&bb->lock, flags);
8737 return rv;
8740 int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8741 int is_new)
8743 int rv;
8744 if (is_new)
8745 s += rdev->new_data_offset;
8746 else
8747 s += rdev->data_offset;
8748 rv = md_set_badblocks(&rdev->badblocks,
8749 s, sectors, 0);
8750 if (rv) {
8751 /* Make sure they get written out promptly */
8752 sysfs_notify_dirent_safe(rdev->sysfs_state);
8753 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8754 set_bit(MD_CHANGE_PENDING, &rdev->mddev->flags);
8755 md_wakeup_thread(rdev->mddev->thread);
8757 return rv;
8759 EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8762 * Remove a range of bad blocks from the table.
8763 * This may involve extending the table if we spilt a region,
8764 * but it must not fail. So if the table becomes full, we just
8765 * drop the remove request.
8767 static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8769 u64 *p;
8770 int lo, hi;
8771 sector_t target = s + sectors;
8772 int rv = 0;
8774 if (bb->shift > 0) {
8775 /* When clearing we round the start up and the end down.
8776 * This should not matter as the shift should align with
8777 * the block size and no rounding should ever be needed.
8778 * However it is better the think a block is bad when it
8779 * isn't than to think a block is not bad when it is.
8781 s += (1<<bb->shift) - 1;
8782 s >>= bb->shift;
8783 target >>= bb->shift;
8784 sectors = target - s;
8787 write_seqlock_irq(&bb->lock);
8789 p = bb->page;
8790 lo = 0;
8791 hi = bb->count;
8792 /* Find the last range that starts before 'target' */
8793 while (hi - lo > 1) {
8794 int mid = (lo + hi) / 2;
8795 sector_t a = BB_OFFSET(p[mid]);
8796 if (a < target)
8797 lo = mid;
8798 else
8799 hi = mid;
8801 if (hi > lo) {
8802 /* p[lo] is the last range that could overlap the
8803 * current range. Earlier ranges could also overlap,
8804 * but only this one can overlap the end of the range.
8806 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8807 /* Partial overlap, leave the tail of this range */
8808 int ack = BB_ACK(p[lo]);
8809 sector_t a = BB_OFFSET(p[lo]);
8810 sector_t end = a + BB_LEN(p[lo]);
8812 if (a < s) {
8813 /* we need to split this range */
8814 if (bb->count >= MD_MAX_BADBLOCKS) {
8815 rv = -ENOSPC;
8816 goto out;
8818 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8819 bb->count++;
8820 p[lo] = BB_MAKE(a, s-a, ack);
8821 lo++;
8823 p[lo] = BB_MAKE(target, end - target, ack);
8824 /* there is no longer an overlap */
8825 hi = lo;
8826 lo--;
8828 while (lo >= 0 &&
8829 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8830 /* This range does overlap */
8831 if (BB_OFFSET(p[lo]) < s) {
8832 /* Keep the early parts of this range. */
8833 int ack = BB_ACK(p[lo]);
8834 sector_t start = BB_OFFSET(p[lo]);
8835 p[lo] = BB_MAKE(start, s - start, ack);
8836 /* now low doesn't overlap, so.. */
8837 break;
8839 lo--;
8841 /* 'lo' is strictly before, 'hi' is strictly after,
8842 * anything between needs to be discarded
8844 if (hi - lo > 1) {
8845 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8846 bb->count -= (hi - lo - 1);
8850 bb->changed = 1;
8851 out:
8852 write_sequnlock_irq(&bb->lock);
8853 return rv;
8856 int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8857 int is_new)
8859 if (is_new)
8860 s += rdev->new_data_offset;
8861 else
8862 s += rdev->data_offset;
8863 return md_clear_badblocks(&rdev->badblocks,
8864 s, sectors);
8866 EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8869 * Acknowledge all bad blocks in a list.
8870 * This only succeeds if ->changed is clear. It is used by
8871 * in-kernel metadata updates
8873 void md_ack_all_badblocks(struct badblocks *bb)
8875 if (bb->page == NULL || bb->changed)
8876 /* no point even trying */
8877 return;
8878 write_seqlock_irq(&bb->lock);
8880 if (bb->changed == 0 && bb->unacked_exist) {
8881 u64 *p = bb->page;
8882 int i;
8883 for (i = 0; i < bb->count ; i++) {
8884 if (!BB_ACK(p[i])) {
8885 sector_t start = BB_OFFSET(p[i]);
8886 int len = BB_LEN(p[i]);
8887 p[i] = BB_MAKE(start, len, 1);
8890 bb->unacked_exist = 0;
8892 write_sequnlock_irq(&bb->lock);
8894 EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8896 /* sysfs access to bad-blocks list.
8897 * We present two files.
8898 * 'bad-blocks' lists sector numbers and lengths of ranges that
8899 * are recorded as bad. The list is truncated to fit within
8900 * the one-page limit of sysfs.
8901 * Writing "sector length" to this file adds an acknowledged
8902 * bad block list.
8903 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8904 * been acknowledged. Writing to this file adds bad blocks
8905 * without acknowledging them. This is largely for testing.
8908 static ssize_t
8909 badblocks_show(struct badblocks *bb, char *page, int unack)
8911 size_t len;
8912 int i;
8913 u64 *p = bb->page;
8914 unsigned seq;
8916 if (bb->shift < 0)
8917 return 0;
8919 retry:
8920 seq = read_seqbegin(&bb->lock);
8922 len = 0;
8923 i = 0;
8925 while (len < PAGE_SIZE && i < bb->count) {
8926 sector_t s = BB_OFFSET(p[i]);
8927 unsigned int length = BB_LEN(p[i]);
8928 int ack = BB_ACK(p[i]);
8929 i++;
8931 if (unack && ack)
8932 continue;
8934 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8935 (unsigned long long)s << bb->shift,
8936 length << bb->shift);
8938 if (unack && len == 0)
8939 bb->unacked_exist = 0;
8941 if (read_seqretry(&bb->lock, seq))
8942 goto retry;
8944 return len;
8947 #define DO_DEBUG 1
8949 static ssize_t
8950 badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8952 unsigned long long sector;
8953 int length;
8954 char newline;
8955 #ifdef DO_DEBUG
8956 /* Allow clearing via sysfs *only* for testing/debugging.
8957 * Normally only a successful write may clear a badblock
8959 int clear = 0;
8960 if (page[0] == '-') {
8961 clear = 1;
8962 page++;
8964 #endif /* DO_DEBUG */
8966 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8967 case 3:
8968 if (newline != '\n')
8969 return -EINVAL;
8970 case 2:
8971 if (length <= 0)
8972 return -EINVAL;
8973 break;
8974 default:
8975 return -EINVAL;
8978 #ifdef DO_DEBUG
8979 if (clear) {
8980 md_clear_badblocks(bb, sector, length);
8981 return len;
8983 #endif /* DO_DEBUG */
8984 if (md_set_badblocks(bb, sector, length, !unack))
8985 return len;
8986 else
8987 return -ENOSPC;
8990 static int md_notify_reboot(struct notifier_block *this,
8991 unsigned long code, void *x)
8993 struct list_head *tmp;
8994 struct mddev *mddev;
8995 int need_delay = 0;
8997 for_each_mddev(mddev, tmp) {
8998 if (mddev_trylock(mddev)) {
8999 if (mddev->pers)
9000 __md_stop_writes(mddev);
9001 if (mddev->persistent)
9002 mddev->safemode = 2;
9003 mddev_unlock(mddev);
9005 need_delay = 1;
9008 * certain more exotic SCSI devices are known to be
9009 * volatile wrt too early system reboots. While the
9010 * right place to handle this issue is the given
9011 * driver, we do want to have a safe RAID driver ...
9013 if (need_delay)
9014 mdelay(1000*1);
9016 return NOTIFY_DONE;
9019 static struct notifier_block md_notifier = {
9020 .notifier_call = md_notify_reboot,
9021 .next = NULL,
9022 .priority = INT_MAX, /* before any real devices */
9025 static void md_geninit(void)
9027 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
9029 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
9032 static int __init md_init(void)
9034 int ret = -ENOMEM;
9036 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
9037 if (!md_wq)
9038 goto err_wq;
9040 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
9041 if (!md_misc_wq)
9042 goto err_misc_wq;
9044 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
9045 goto err_md;
9047 if ((ret = register_blkdev(0, "mdp")) < 0)
9048 goto err_mdp;
9049 mdp_major = ret;
9051 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
9052 md_probe, NULL, NULL);
9053 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
9054 md_probe, NULL, NULL);
9056 register_reboot_notifier(&md_notifier);
9057 raid_table_header = register_sysctl_table(raid_root_table);
9059 md_geninit();
9060 return 0;
9062 err_mdp:
9063 unregister_blkdev(MD_MAJOR, "md");
9064 err_md:
9065 destroy_workqueue(md_misc_wq);
9066 err_misc_wq:
9067 destroy_workqueue(md_wq);
9068 err_wq:
9069 return ret;
9072 static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
9074 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
9075 struct md_rdev *rdev2;
9076 int role, ret;
9077 char b[BDEVNAME_SIZE];
9079 /* Check for change of roles in the active devices */
9080 rdev_for_each(rdev2, mddev) {
9081 if (test_bit(Faulty, &rdev2->flags))
9082 continue;
9084 /* Check if the roles changed */
9085 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
9087 if (test_bit(Candidate, &rdev2->flags)) {
9088 if (role == 0xfffe) {
9089 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
9090 md_kick_rdev_from_array(rdev2);
9091 continue;
9093 else
9094 clear_bit(Candidate, &rdev2->flags);
9097 if (role != rdev2->raid_disk) {
9098 /* got activated */
9099 if (rdev2->raid_disk == -1 && role != 0xffff) {
9100 rdev2->saved_raid_disk = role;
9101 ret = remove_and_add_spares(mddev, rdev2);
9102 pr_info("Activated spare: %s\n",
9103 bdevname(rdev2->bdev,b));
9104 continue;
9106 /* device faulty
9107 * We just want to do the minimum to mark the disk
9108 * as faulty. The recovery is performed by the
9109 * one who initiated the error.
9111 if ((role == 0xfffe) || (role == 0xfffd)) {
9112 md_error(mddev, rdev2);
9113 clear_bit(Blocked, &rdev2->flags);
9118 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
9119 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
9121 /* Finally set the event to be up to date */
9122 mddev->events = le64_to_cpu(sb->events);
9125 static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
9127 int err;
9128 struct page *swapout = rdev->sb_page;
9129 struct mdp_superblock_1 *sb;
9131 /* Store the sb page of the rdev in the swapout temporary
9132 * variable in case we err in the future
9134 rdev->sb_page = NULL;
9135 alloc_disk_sb(rdev);
9136 ClearPageUptodate(rdev->sb_page);
9137 rdev->sb_loaded = 0;
9138 err = super_types[mddev->major_version].load_super(rdev, NULL, mddev->minor_version);
9140 if (err < 0) {
9141 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9142 __func__, __LINE__, rdev->desc_nr, err);
9143 put_page(rdev->sb_page);
9144 rdev->sb_page = swapout;
9145 rdev->sb_loaded = 1;
9146 return err;
9149 sb = page_address(rdev->sb_page);
9150 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9151 * is not set
9154 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
9155 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
9157 /* The other node finished recovery, call spare_active to set
9158 * device In_sync and mddev->degraded
9160 if (rdev->recovery_offset == MaxSector &&
9161 !test_bit(In_sync, &rdev->flags) &&
9162 mddev->pers->spare_active(mddev))
9163 sysfs_notify(&mddev->kobj, NULL, "degraded");
9165 put_page(swapout);
9166 return 0;
9169 void md_reload_sb(struct mddev *mddev, int nr)
9171 struct md_rdev *rdev;
9172 int err;
9174 /* Find the rdev */
9175 rdev_for_each_rcu(rdev, mddev) {
9176 if (rdev->desc_nr == nr)
9177 break;
9180 if (!rdev || rdev->desc_nr != nr) {
9181 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
9182 return;
9185 err = read_rdev(mddev, rdev);
9186 if (err < 0)
9187 return;
9189 check_sb_changes(mddev, rdev);
9191 /* Read all rdev's to update recovery_offset */
9192 rdev_for_each_rcu(rdev, mddev)
9193 read_rdev(mddev, rdev);
9195 EXPORT_SYMBOL(md_reload_sb);
9197 #ifndef MODULE
9200 * Searches all registered partitions for autorun RAID arrays
9201 * at boot time.
9204 static LIST_HEAD(all_detected_devices);
9205 struct detected_devices_node {
9206 struct list_head list;
9207 dev_t dev;
9210 void md_autodetect_dev(dev_t dev)
9212 struct detected_devices_node *node_detected_dev;
9214 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
9215 if (node_detected_dev) {
9216 node_detected_dev->dev = dev;
9217 list_add_tail(&node_detected_dev->list, &all_detected_devices);
9218 } else {
9219 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
9220 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
9224 static void autostart_arrays(int part)
9226 struct md_rdev *rdev;
9227 struct detected_devices_node *node_detected_dev;
9228 dev_t dev;
9229 int i_scanned, i_passed;
9231 i_scanned = 0;
9232 i_passed = 0;
9234 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
9236 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
9237 i_scanned++;
9238 node_detected_dev = list_entry(all_detected_devices.next,
9239 struct detected_devices_node, list);
9240 list_del(&node_detected_dev->list);
9241 dev = node_detected_dev->dev;
9242 kfree(node_detected_dev);
9243 rdev = md_import_device(dev,0, 90);
9244 if (IS_ERR(rdev))
9245 continue;
9247 if (test_bit(Faulty, &rdev->flags))
9248 continue;
9250 set_bit(AutoDetected, &rdev->flags);
9251 list_add(&rdev->same_set, &pending_raid_disks);
9252 i_passed++;
9255 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
9256 i_scanned, i_passed);
9258 autorun_devices(part);
9261 #endif /* !MODULE */
9263 static __exit void md_exit(void)
9265 struct mddev *mddev;
9266 struct list_head *tmp;
9267 int delay = 1;
9269 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
9270 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
9272 unregister_blkdev(MD_MAJOR,"md");
9273 unregister_blkdev(mdp_major, "mdp");
9274 unregister_reboot_notifier(&md_notifier);
9275 unregister_sysctl_table(raid_table_header);
9277 /* We cannot unload the modules while some process is
9278 * waiting for us in select() or poll() - wake them up
9280 md_unloading = 1;
9281 while (waitqueue_active(&md_event_waiters)) {
9282 /* not safe to leave yet */
9283 wake_up(&md_event_waiters);
9284 msleep(delay);
9285 delay += delay;
9287 remove_proc_entry("mdstat", NULL);
9289 for_each_mddev(mddev, tmp) {
9290 export_array(mddev);
9291 mddev->hold_active = 0;
9293 destroy_workqueue(md_misc_wq);
9294 destroy_workqueue(md_wq);
9297 subsys_initcall(md_init);
9298 module_exit(md_exit)
9300 static int get_ro(char *buffer, struct kernel_param *kp)
9302 return sprintf(buffer, "%d", start_readonly);
9304 static int set_ro(const char *val, struct kernel_param *kp)
9306 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
9309 module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9310 module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
9311 module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
9313 MODULE_LICENSE("GPL");
9314 MODULE_DESCRIPTION("MD RAID framework");
9315 MODULE_ALIAS("md");
9316 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);