bna: remove oper_state_cbfn from struct bna_rxf
[linux/fpc-iii.git] / drivers / md / md.c
blob27506302eb7aa42557bfc01547274957ccbace50
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 void 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 if (mddev == NULL || mddev->pers == NULL
261 || !mddev->ready) {
262 bio_io_error(bio);
263 return;
265 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
266 bio_endio(bio, bio_sectors(bio) == 0 ? 0 : -EROFS);
267 return;
269 smp_rmb(); /* Ensure implications of 'active' are visible */
270 rcu_read_lock();
271 if (mddev->suspended) {
272 DEFINE_WAIT(__wait);
273 for (;;) {
274 prepare_to_wait(&mddev->sb_wait, &__wait,
275 TASK_UNINTERRUPTIBLE);
276 if (!mddev->suspended)
277 break;
278 rcu_read_unlock();
279 schedule();
280 rcu_read_lock();
282 finish_wait(&mddev->sb_wait, &__wait);
284 atomic_inc(&mddev->active_io);
285 rcu_read_unlock();
288 * save the sectors now since our bio can
289 * go away inside make_request
291 sectors = bio_sectors(bio);
292 mddev->pers->make_request(mddev, bio);
294 cpu = part_stat_lock();
295 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
296 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
297 part_stat_unlock();
299 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
300 wake_up(&mddev->sb_wait);
303 /* mddev_suspend makes sure no new requests are submitted
304 * to the device, and that any requests that have been submitted
305 * are completely handled.
306 * Once mddev_detach() is called and completes, the module will be
307 * completely unused.
309 void mddev_suspend(struct mddev *mddev)
311 BUG_ON(mddev->suspended);
312 mddev->suspended = 1;
313 synchronize_rcu();
314 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
315 mddev->pers->quiesce(mddev, 1);
317 del_timer_sync(&mddev->safemode_timer);
319 EXPORT_SYMBOL_GPL(mddev_suspend);
321 void mddev_resume(struct mddev *mddev)
323 mddev->suspended = 0;
324 wake_up(&mddev->sb_wait);
325 mddev->pers->quiesce(mddev, 0);
327 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
328 md_wakeup_thread(mddev->thread);
329 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
331 EXPORT_SYMBOL_GPL(mddev_resume);
333 int mddev_congested(struct mddev *mddev, int bits)
335 struct md_personality *pers = mddev->pers;
336 int ret = 0;
338 rcu_read_lock();
339 if (mddev->suspended)
340 ret = 1;
341 else if (pers && pers->congested)
342 ret = pers->congested(mddev, bits);
343 rcu_read_unlock();
344 return ret;
346 EXPORT_SYMBOL_GPL(mddev_congested);
347 static int md_congested(void *data, int bits)
349 struct mddev *mddev = data;
350 return mddev_congested(mddev, bits);
353 static int md_mergeable_bvec(struct request_queue *q,
354 struct bvec_merge_data *bvm,
355 struct bio_vec *biovec)
357 struct mddev *mddev = q->queuedata;
358 int ret;
359 rcu_read_lock();
360 if (mddev->suspended) {
361 /* Must always allow one vec */
362 if (bvm->bi_size == 0)
363 ret = biovec->bv_len;
364 else
365 ret = 0;
366 } else {
367 struct md_personality *pers = mddev->pers;
368 if (pers && pers->mergeable_bvec)
369 ret = pers->mergeable_bvec(mddev, bvm, biovec);
370 else
371 ret = biovec->bv_len;
373 rcu_read_unlock();
374 return ret;
377 * Generic flush handling for md
380 static void md_end_flush(struct bio *bio, int err)
382 struct md_rdev *rdev = bio->bi_private;
383 struct mddev *mddev = rdev->mddev;
385 rdev_dec_pending(rdev, mddev);
387 if (atomic_dec_and_test(&mddev->flush_pending)) {
388 /* The pre-request flush has finished */
389 queue_work(md_wq, &mddev->flush_work);
391 bio_put(bio);
394 static void md_submit_flush_data(struct work_struct *ws);
396 static void submit_flushes(struct work_struct *ws)
398 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
399 struct md_rdev *rdev;
401 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
402 atomic_set(&mddev->flush_pending, 1);
403 rcu_read_lock();
404 rdev_for_each_rcu(rdev, mddev)
405 if (rdev->raid_disk >= 0 &&
406 !test_bit(Faulty, &rdev->flags)) {
407 /* Take two references, one is dropped
408 * when request finishes, one after
409 * we reclaim rcu_read_lock
411 struct bio *bi;
412 atomic_inc(&rdev->nr_pending);
413 atomic_inc(&rdev->nr_pending);
414 rcu_read_unlock();
415 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
416 bi->bi_end_io = md_end_flush;
417 bi->bi_private = rdev;
418 bi->bi_bdev = rdev->bdev;
419 atomic_inc(&mddev->flush_pending);
420 submit_bio(WRITE_FLUSH, bi);
421 rcu_read_lock();
422 rdev_dec_pending(rdev, mddev);
424 rcu_read_unlock();
425 if (atomic_dec_and_test(&mddev->flush_pending))
426 queue_work(md_wq, &mddev->flush_work);
429 static void md_submit_flush_data(struct work_struct *ws)
431 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
432 struct bio *bio = mddev->flush_bio;
434 if (bio->bi_iter.bi_size == 0)
435 /* an empty barrier - all done */
436 bio_endio(bio, 0);
437 else {
438 bio->bi_rw &= ~REQ_FLUSH;
439 mddev->pers->make_request(mddev, bio);
442 mddev->flush_bio = NULL;
443 wake_up(&mddev->sb_wait);
446 void md_flush_request(struct mddev *mddev, struct bio *bio)
448 spin_lock_irq(&mddev->lock);
449 wait_event_lock_irq(mddev->sb_wait,
450 !mddev->flush_bio,
451 mddev->lock);
452 mddev->flush_bio = bio;
453 spin_unlock_irq(&mddev->lock);
455 INIT_WORK(&mddev->flush_work, submit_flushes);
456 queue_work(md_wq, &mddev->flush_work);
458 EXPORT_SYMBOL(md_flush_request);
460 void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
462 struct mddev *mddev = cb->data;
463 md_wakeup_thread(mddev->thread);
464 kfree(cb);
466 EXPORT_SYMBOL(md_unplug);
468 static inline struct mddev *mddev_get(struct mddev *mddev)
470 atomic_inc(&mddev->active);
471 return mddev;
474 static void mddev_delayed_delete(struct work_struct *ws);
476 static void mddev_put(struct mddev *mddev)
478 struct bio_set *bs = NULL;
480 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
481 return;
482 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
483 mddev->ctime == 0 && !mddev->hold_active) {
484 /* Array is not configured at all, and not held active,
485 * so destroy it */
486 list_del_init(&mddev->all_mddevs);
487 bs = mddev->bio_set;
488 mddev->bio_set = NULL;
489 if (mddev->gendisk) {
490 /* We did a probe so need to clean up. Call
491 * queue_work inside the spinlock so that
492 * flush_workqueue() after mddev_find will
493 * succeed in waiting for the work to be done.
495 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
496 queue_work(md_misc_wq, &mddev->del_work);
497 } else
498 kfree(mddev);
500 spin_unlock(&all_mddevs_lock);
501 if (bs)
502 bioset_free(bs);
505 void mddev_init(struct mddev *mddev)
507 mutex_init(&mddev->open_mutex);
508 mutex_init(&mddev->reconfig_mutex);
509 mutex_init(&mddev->bitmap_info.mutex);
510 INIT_LIST_HEAD(&mddev->disks);
511 INIT_LIST_HEAD(&mddev->all_mddevs);
512 init_timer(&mddev->safemode_timer);
513 atomic_set(&mddev->active, 1);
514 atomic_set(&mddev->openers, 0);
515 atomic_set(&mddev->active_io, 0);
516 spin_lock_init(&mddev->lock);
517 atomic_set(&mddev->flush_pending, 0);
518 init_waitqueue_head(&mddev->sb_wait);
519 init_waitqueue_head(&mddev->recovery_wait);
520 mddev->reshape_position = MaxSector;
521 mddev->reshape_backwards = 0;
522 mddev->last_sync_action = "none";
523 mddev->resync_min = 0;
524 mddev->resync_max = MaxSector;
525 mddev->level = LEVEL_NONE;
527 EXPORT_SYMBOL_GPL(mddev_init);
529 static struct mddev *mddev_find(dev_t unit)
531 struct mddev *mddev, *new = NULL;
533 if (unit && MAJOR(unit) != MD_MAJOR)
534 unit &= ~((1<<MdpMinorShift)-1);
536 retry:
537 spin_lock(&all_mddevs_lock);
539 if (unit) {
540 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
541 if (mddev->unit == unit) {
542 mddev_get(mddev);
543 spin_unlock(&all_mddevs_lock);
544 kfree(new);
545 return mddev;
548 if (new) {
549 list_add(&new->all_mddevs, &all_mddevs);
550 spin_unlock(&all_mddevs_lock);
551 new->hold_active = UNTIL_IOCTL;
552 return new;
554 } else if (new) {
555 /* find an unused unit number */
556 static int next_minor = 512;
557 int start = next_minor;
558 int is_free = 0;
559 int dev = 0;
560 while (!is_free) {
561 dev = MKDEV(MD_MAJOR, next_minor);
562 next_minor++;
563 if (next_minor > MINORMASK)
564 next_minor = 0;
565 if (next_minor == start) {
566 /* Oh dear, all in use. */
567 spin_unlock(&all_mddevs_lock);
568 kfree(new);
569 return NULL;
572 is_free = 1;
573 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
574 if (mddev->unit == dev) {
575 is_free = 0;
576 break;
579 new->unit = dev;
580 new->md_minor = MINOR(dev);
581 new->hold_active = UNTIL_STOP;
582 list_add(&new->all_mddevs, &all_mddevs);
583 spin_unlock(&all_mddevs_lock);
584 return new;
586 spin_unlock(&all_mddevs_lock);
588 new = kzalloc(sizeof(*new), GFP_KERNEL);
589 if (!new)
590 return NULL;
592 new->unit = unit;
593 if (MAJOR(unit) == MD_MAJOR)
594 new->md_minor = MINOR(unit);
595 else
596 new->md_minor = MINOR(unit) >> MdpMinorShift;
598 mddev_init(new);
600 goto retry;
603 static struct attribute_group md_redundancy_group;
605 void mddev_unlock(struct mddev *mddev)
607 if (mddev->to_remove) {
608 /* These cannot be removed under reconfig_mutex as
609 * an access to the files will try to take reconfig_mutex
610 * while holding the file unremovable, which leads to
611 * a deadlock.
612 * So hold set sysfs_active while the remove in happeing,
613 * and anything else which might set ->to_remove or my
614 * otherwise change the sysfs namespace will fail with
615 * -EBUSY if sysfs_active is still set.
616 * We set sysfs_active under reconfig_mutex and elsewhere
617 * test it under the same mutex to ensure its correct value
618 * is seen.
620 struct attribute_group *to_remove = mddev->to_remove;
621 mddev->to_remove = NULL;
622 mddev->sysfs_active = 1;
623 mutex_unlock(&mddev->reconfig_mutex);
625 if (mddev->kobj.sd) {
626 if (to_remove != &md_redundancy_group)
627 sysfs_remove_group(&mddev->kobj, to_remove);
628 if (mddev->pers == NULL ||
629 mddev->pers->sync_request == NULL) {
630 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
631 if (mddev->sysfs_action)
632 sysfs_put(mddev->sysfs_action);
633 mddev->sysfs_action = NULL;
636 mddev->sysfs_active = 0;
637 } else
638 mutex_unlock(&mddev->reconfig_mutex);
640 /* As we've dropped the mutex we need a spinlock to
641 * make sure the thread doesn't disappear
643 spin_lock(&pers_lock);
644 md_wakeup_thread(mddev->thread);
645 spin_unlock(&pers_lock);
647 EXPORT_SYMBOL_GPL(mddev_unlock);
649 struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
651 struct md_rdev *rdev;
653 rdev_for_each_rcu(rdev, mddev)
654 if (rdev->desc_nr == nr)
655 return rdev;
657 return NULL;
659 EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
661 static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
663 struct md_rdev *rdev;
665 rdev_for_each(rdev, mddev)
666 if (rdev->bdev->bd_dev == dev)
667 return rdev;
669 return NULL;
672 static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
674 struct md_rdev *rdev;
676 rdev_for_each_rcu(rdev, mddev)
677 if (rdev->bdev->bd_dev == dev)
678 return rdev;
680 return NULL;
683 static struct md_personality *find_pers(int level, char *clevel)
685 struct md_personality *pers;
686 list_for_each_entry(pers, &pers_list, list) {
687 if (level != LEVEL_NONE && pers->level == level)
688 return pers;
689 if (strcmp(pers->name, clevel)==0)
690 return pers;
692 return NULL;
695 /* return the offset of the super block in 512byte sectors */
696 static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
698 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
699 return MD_NEW_SIZE_SECTORS(num_sectors);
702 static int alloc_disk_sb(struct md_rdev *rdev)
704 rdev->sb_page = alloc_page(GFP_KERNEL);
705 if (!rdev->sb_page) {
706 printk(KERN_ALERT "md: out of memory.\n");
707 return -ENOMEM;
710 return 0;
713 void md_rdev_clear(struct md_rdev *rdev)
715 if (rdev->sb_page) {
716 put_page(rdev->sb_page);
717 rdev->sb_loaded = 0;
718 rdev->sb_page = NULL;
719 rdev->sb_start = 0;
720 rdev->sectors = 0;
722 if (rdev->bb_page) {
723 put_page(rdev->bb_page);
724 rdev->bb_page = NULL;
726 kfree(rdev->badblocks.page);
727 rdev->badblocks.page = NULL;
729 EXPORT_SYMBOL_GPL(md_rdev_clear);
731 static void super_written(struct bio *bio, int error)
733 struct md_rdev *rdev = bio->bi_private;
734 struct mddev *mddev = rdev->mddev;
736 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
737 printk("md: super_written gets error=%d, uptodate=%d\n",
738 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
739 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
740 md_error(mddev, rdev);
743 if (atomic_dec_and_test(&mddev->pending_writes))
744 wake_up(&mddev->sb_wait);
745 bio_put(bio);
748 void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
749 sector_t sector, int size, struct page *page)
751 /* write first size bytes of page to sector of rdev
752 * Increment mddev->pending_writes before returning
753 * and decrement it on completion, waking up sb_wait
754 * if zero is reached.
755 * If an error occurred, call md_error
757 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
759 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
760 bio->bi_iter.bi_sector = sector;
761 bio_add_page(bio, page, size, 0);
762 bio->bi_private = rdev;
763 bio->bi_end_io = super_written;
765 atomic_inc(&mddev->pending_writes);
766 submit_bio(WRITE_FLUSH_FUA, bio);
769 void md_super_wait(struct mddev *mddev)
771 /* wait for all superblock writes that were scheduled to complete */
772 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
775 int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
776 struct page *page, int rw, bool metadata_op)
778 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
779 int ret;
781 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
782 rdev->meta_bdev : rdev->bdev;
783 if (metadata_op)
784 bio->bi_iter.bi_sector = sector + rdev->sb_start;
785 else if (rdev->mddev->reshape_position != MaxSector &&
786 (rdev->mddev->reshape_backwards ==
787 (sector >= rdev->mddev->reshape_position)))
788 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
789 else
790 bio->bi_iter.bi_sector = sector + rdev->data_offset;
791 bio_add_page(bio, page, size, 0);
792 submit_bio_wait(rw, bio);
794 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
795 bio_put(bio);
796 return ret;
798 EXPORT_SYMBOL_GPL(sync_page_io);
800 static int read_disk_sb(struct md_rdev *rdev, int size)
802 char b[BDEVNAME_SIZE];
804 if (rdev->sb_loaded)
805 return 0;
807 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
808 goto fail;
809 rdev->sb_loaded = 1;
810 return 0;
812 fail:
813 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
814 bdevname(rdev->bdev,b));
815 return -EINVAL;
818 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
820 return sb1->set_uuid0 == sb2->set_uuid0 &&
821 sb1->set_uuid1 == sb2->set_uuid1 &&
822 sb1->set_uuid2 == sb2->set_uuid2 &&
823 sb1->set_uuid3 == sb2->set_uuid3;
826 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
828 int ret;
829 mdp_super_t *tmp1, *tmp2;
831 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
832 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
834 if (!tmp1 || !tmp2) {
835 ret = 0;
836 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
837 goto abort;
840 *tmp1 = *sb1;
841 *tmp2 = *sb2;
844 * nr_disks is not constant
846 tmp1->nr_disks = 0;
847 tmp2->nr_disks = 0;
849 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
850 abort:
851 kfree(tmp1);
852 kfree(tmp2);
853 return ret;
856 static u32 md_csum_fold(u32 csum)
858 csum = (csum & 0xffff) + (csum >> 16);
859 return (csum & 0xffff) + (csum >> 16);
862 static unsigned int calc_sb_csum(mdp_super_t *sb)
864 u64 newcsum = 0;
865 u32 *sb32 = (u32*)sb;
866 int i;
867 unsigned int disk_csum, csum;
869 disk_csum = sb->sb_csum;
870 sb->sb_csum = 0;
872 for (i = 0; i < MD_SB_BYTES/4 ; i++)
873 newcsum += sb32[i];
874 csum = (newcsum & 0xffffffff) + (newcsum>>32);
876 #ifdef CONFIG_ALPHA
877 /* This used to use csum_partial, which was wrong for several
878 * reasons including that different results are returned on
879 * different architectures. It isn't critical that we get exactly
880 * the same return value as before (we always csum_fold before
881 * testing, and that removes any differences). However as we
882 * know that csum_partial always returned a 16bit value on
883 * alphas, do a fold to maximise conformity to previous behaviour.
885 sb->sb_csum = md_csum_fold(disk_csum);
886 #else
887 sb->sb_csum = disk_csum;
888 #endif
889 return csum;
893 * Handle superblock details.
894 * We want to be able to handle multiple superblock formats
895 * so we have a common interface to them all, and an array of
896 * different handlers.
897 * We rely on user-space to write the initial superblock, and support
898 * reading and updating of superblocks.
899 * Interface methods are:
900 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
901 * loads and validates a superblock on dev.
902 * if refdev != NULL, compare superblocks on both devices
903 * Return:
904 * 0 - dev has a superblock that is compatible with refdev
905 * 1 - dev has a superblock that is compatible and newer than refdev
906 * so dev should be used as the refdev in future
907 * -EINVAL superblock incompatible or invalid
908 * -othererror e.g. -EIO
910 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
911 * Verify that dev is acceptable into mddev.
912 * The first time, mddev->raid_disks will be 0, and data from
913 * dev should be merged in. Subsequent calls check that dev
914 * is new enough. Return 0 or -EINVAL
916 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
917 * Update the superblock for rdev with data in mddev
918 * This does not write to disc.
922 struct super_type {
923 char *name;
924 struct module *owner;
925 int (*load_super)(struct md_rdev *rdev,
926 struct md_rdev *refdev,
927 int minor_version);
928 int (*validate_super)(struct mddev *mddev,
929 struct md_rdev *rdev);
930 void (*sync_super)(struct mddev *mddev,
931 struct md_rdev *rdev);
932 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
933 sector_t num_sectors);
934 int (*allow_new_offset)(struct md_rdev *rdev,
935 unsigned long long new_offset);
939 * Check that the given mddev has no bitmap.
941 * This function is called from the run method of all personalities that do not
942 * support bitmaps. It prints an error message and returns non-zero if mddev
943 * has a bitmap. Otherwise, it returns 0.
946 int md_check_no_bitmap(struct mddev *mddev)
948 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
949 return 0;
950 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
951 mdname(mddev), mddev->pers->name);
952 return 1;
954 EXPORT_SYMBOL(md_check_no_bitmap);
957 * load_super for 0.90.0
959 static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
961 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
962 mdp_super_t *sb;
963 int ret;
966 * Calculate the position of the superblock (512byte sectors),
967 * it's at the end of the disk.
969 * It also happens to be a multiple of 4Kb.
971 rdev->sb_start = calc_dev_sboffset(rdev);
973 ret = read_disk_sb(rdev, MD_SB_BYTES);
974 if (ret) return ret;
976 ret = -EINVAL;
978 bdevname(rdev->bdev, b);
979 sb = page_address(rdev->sb_page);
981 if (sb->md_magic != MD_SB_MAGIC) {
982 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
984 goto abort;
987 if (sb->major_version != 0 ||
988 sb->minor_version < 90 ||
989 sb->minor_version > 91) {
990 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
991 sb->major_version, sb->minor_version,
993 goto abort;
996 if (sb->raid_disks <= 0)
997 goto abort;
999 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
1000 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1002 goto abort;
1005 rdev->preferred_minor = sb->md_minor;
1006 rdev->data_offset = 0;
1007 rdev->new_data_offset = 0;
1008 rdev->sb_size = MD_SB_BYTES;
1009 rdev->badblocks.shift = -1;
1011 if (sb->level == LEVEL_MULTIPATH)
1012 rdev->desc_nr = -1;
1013 else
1014 rdev->desc_nr = sb->this_disk.number;
1016 if (!refdev) {
1017 ret = 1;
1018 } else {
1019 __u64 ev1, ev2;
1020 mdp_super_t *refsb = page_address(refdev->sb_page);
1021 if (!uuid_equal(refsb, sb)) {
1022 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1023 b, bdevname(refdev->bdev,b2));
1024 goto abort;
1026 if (!sb_equal(refsb, sb)) {
1027 printk(KERN_WARNING "md: %s has same UUID"
1028 " but different superblock to %s\n",
1029 b, bdevname(refdev->bdev, b2));
1030 goto abort;
1032 ev1 = md_event(sb);
1033 ev2 = md_event(refsb);
1034 if (ev1 > ev2)
1035 ret = 1;
1036 else
1037 ret = 0;
1039 rdev->sectors = rdev->sb_start;
1040 /* Limit to 4TB as metadata cannot record more than that.
1041 * (not needed for Linear and RAID0 as metadata doesn't
1042 * record this size)
1044 if (rdev->sectors >= (2ULL << 32) && sb->level >= 1)
1045 rdev->sectors = (2ULL << 32) - 2;
1047 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
1048 /* "this cannot possibly happen" ... */
1049 ret = -EINVAL;
1051 abort:
1052 return ret;
1056 * validate_super for 0.90.0
1058 static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
1060 mdp_disk_t *desc;
1061 mdp_super_t *sb = page_address(rdev->sb_page);
1062 __u64 ev1 = md_event(sb);
1064 rdev->raid_disk = -1;
1065 clear_bit(Faulty, &rdev->flags);
1066 clear_bit(In_sync, &rdev->flags);
1067 clear_bit(Bitmap_sync, &rdev->flags);
1068 clear_bit(WriteMostly, &rdev->flags);
1070 if (mddev->raid_disks == 0) {
1071 mddev->major_version = 0;
1072 mddev->minor_version = sb->minor_version;
1073 mddev->patch_version = sb->patch_version;
1074 mddev->external = 0;
1075 mddev->chunk_sectors = sb->chunk_size >> 9;
1076 mddev->ctime = sb->ctime;
1077 mddev->utime = sb->utime;
1078 mddev->level = sb->level;
1079 mddev->clevel[0] = 0;
1080 mddev->layout = sb->layout;
1081 mddev->raid_disks = sb->raid_disks;
1082 mddev->dev_sectors = ((sector_t)sb->size) * 2;
1083 mddev->events = ev1;
1084 mddev->bitmap_info.offset = 0;
1085 mddev->bitmap_info.space = 0;
1086 /* bitmap can use 60 K after the 4K superblocks */
1087 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
1088 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
1089 mddev->reshape_backwards = 0;
1091 if (mddev->minor_version >= 91) {
1092 mddev->reshape_position = sb->reshape_position;
1093 mddev->delta_disks = sb->delta_disks;
1094 mddev->new_level = sb->new_level;
1095 mddev->new_layout = sb->new_layout;
1096 mddev->new_chunk_sectors = sb->new_chunk >> 9;
1097 if (mddev->delta_disks < 0)
1098 mddev->reshape_backwards = 1;
1099 } else {
1100 mddev->reshape_position = MaxSector;
1101 mddev->delta_disks = 0;
1102 mddev->new_level = mddev->level;
1103 mddev->new_layout = mddev->layout;
1104 mddev->new_chunk_sectors = mddev->chunk_sectors;
1107 if (sb->state & (1<<MD_SB_CLEAN))
1108 mddev->recovery_cp = MaxSector;
1109 else {
1110 if (sb->events_hi == sb->cp_events_hi &&
1111 sb->events_lo == sb->cp_events_lo) {
1112 mddev->recovery_cp = sb->recovery_cp;
1113 } else
1114 mddev->recovery_cp = 0;
1117 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1118 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1119 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1120 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1122 mddev->max_disks = MD_SB_DISKS;
1124 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
1125 mddev->bitmap_info.file == NULL) {
1126 mddev->bitmap_info.offset =
1127 mddev->bitmap_info.default_offset;
1128 mddev->bitmap_info.space =
1129 mddev->bitmap_info.default_space;
1132 } else if (mddev->pers == NULL) {
1133 /* Insist on good event counter while assembling, except
1134 * for spares (which don't need an event count) */
1135 ++ev1;
1136 if (sb->disks[rdev->desc_nr].state & (
1137 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1138 if (ev1 < mddev->events)
1139 return -EINVAL;
1140 } else if (mddev->bitmap) {
1141 /* if adding to array with a bitmap, then we can accept an
1142 * older device ... but not too old.
1144 if (ev1 < mddev->bitmap->events_cleared)
1145 return 0;
1146 if (ev1 < mddev->events)
1147 set_bit(Bitmap_sync, &rdev->flags);
1148 } else {
1149 if (ev1 < mddev->events)
1150 /* just a hot-add of a new device, leave raid_disk at -1 */
1151 return 0;
1154 if (mddev->level != LEVEL_MULTIPATH) {
1155 desc = sb->disks + rdev->desc_nr;
1157 if (desc->state & (1<<MD_DISK_FAULTY))
1158 set_bit(Faulty, &rdev->flags);
1159 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1160 desc->raid_disk < mddev->raid_disks */) {
1161 set_bit(In_sync, &rdev->flags);
1162 rdev->raid_disk = desc->raid_disk;
1163 rdev->saved_raid_disk = desc->raid_disk;
1164 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1165 /* active but not in sync implies recovery up to
1166 * reshape position. We don't know exactly where
1167 * that is, so set to zero for now */
1168 if (mddev->minor_version >= 91) {
1169 rdev->recovery_offset = 0;
1170 rdev->raid_disk = desc->raid_disk;
1173 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1174 set_bit(WriteMostly, &rdev->flags);
1175 } else /* MULTIPATH are always insync */
1176 set_bit(In_sync, &rdev->flags);
1177 return 0;
1181 * sync_super for 0.90.0
1183 static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
1185 mdp_super_t *sb;
1186 struct md_rdev *rdev2;
1187 int next_spare = mddev->raid_disks;
1189 /* make rdev->sb match mddev data..
1191 * 1/ zero out disks
1192 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1193 * 3/ any empty disks < next_spare become removed
1195 * disks[0] gets initialised to REMOVED because
1196 * we cannot be sure from other fields if it has
1197 * been initialised or not.
1199 int i;
1200 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1202 rdev->sb_size = MD_SB_BYTES;
1204 sb = page_address(rdev->sb_page);
1206 memset(sb, 0, sizeof(*sb));
1208 sb->md_magic = MD_SB_MAGIC;
1209 sb->major_version = mddev->major_version;
1210 sb->patch_version = mddev->patch_version;
1211 sb->gvalid_words = 0; /* ignored */
1212 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1213 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1214 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1215 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1217 sb->ctime = mddev->ctime;
1218 sb->level = mddev->level;
1219 sb->size = mddev->dev_sectors / 2;
1220 sb->raid_disks = mddev->raid_disks;
1221 sb->md_minor = mddev->md_minor;
1222 sb->not_persistent = 0;
1223 sb->utime = mddev->utime;
1224 sb->state = 0;
1225 sb->events_hi = (mddev->events>>32);
1226 sb->events_lo = (u32)mddev->events;
1228 if (mddev->reshape_position == MaxSector)
1229 sb->minor_version = 90;
1230 else {
1231 sb->minor_version = 91;
1232 sb->reshape_position = mddev->reshape_position;
1233 sb->new_level = mddev->new_level;
1234 sb->delta_disks = mddev->delta_disks;
1235 sb->new_layout = mddev->new_layout;
1236 sb->new_chunk = mddev->new_chunk_sectors << 9;
1238 mddev->minor_version = sb->minor_version;
1239 if (mddev->in_sync)
1241 sb->recovery_cp = mddev->recovery_cp;
1242 sb->cp_events_hi = (mddev->events>>32);
1243 sb->cp_events_lo = (u32)mddev->events;
1244 if (mddev->recovery_cp == MaxSector)
1245 sb->state = (1<< MD_SB_CLEAN);
1246 } else
1247 sb->recovery_cp = 0;
1249 sb->layout = mddev->layout;
1250 sb->chunk_size = mddev->chunk_sectors << 9;
1252 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
1253 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1255 sb->disks[0].state = (1<<MD_DISK_REMOVED);
1256 rdev_for_each(rdev2, mddev) {
1257 mdp_disk_t *d;
1258 int desc_nr;
1259 int is_active = test_bit(In_sync, &rdev2->flags);
1261 if (rdev2->raid_disk >= 0 &&
1262 sb->minor_version >= 91)
1263 /* we have nowhere to store the recovery_offset,
1264 * but if it is not below the reshape_position,
1265 * we can piggy-back on that.
1267 is_active = 1;
1268 if (rdev2->raid_disk < 0 ||
1269 test_bit(Faulty, &rdev2->flags))
1270 is_active = 0;
1271 if (is_active)
1272 desc_nr = rdev2->raid_disk;
1273 else
1274 desc_nr = next_spare++;
1275 rdev2->desc_nr = desc_nr;
1276 d = &sb->disks[rdev2->desc_nr];
1277 nr_disks++;
1278 d->number = rdev2->desc_nr;
1279 d->major = MAJOR(rdev2->bdev->bd_dev);
1280 d->minor = MINOR(rdev2->bdev->bd_dev);
1281 if (is_active)
1282 d->raid_disk = rdev2->raid_disk;
1283 else
1284 d->raid_disk = rdev2->desc_nr; /* compatibility */
1285 if (test_bit(Faulty, &rdev2->flags))
1286 d->state = (1<<MD_DISK_FAULTY);
1287 else if (is_active) {
1288 d->state = (1<<MD_DISK_ACTIVE);
1289 if (test_bit(In_sync, &rdev2->flags))
1290 d->state |= (1<<MD_DISK_SYNC);
1291 active++;
1292 working++;
1293 } else {
1294 d->state = 0;
1295 spare++;
1296 working++;
1298 if (test_bit(WriteMostly, &rdev2->flags))
1299 d->state |= (1<<MD_DISK_WRITEMOSTLY);
1301 /* now set the "removed" and "faulty" bits on any missing devices */
1302 for (i=0 ; i < mddev->raid_disks ; i++) {
1303 mdp_disk_t *d = &sb->disks[i];
1304 if (d->state == 0 && d->number == 0) {
1305 d->number = i;
1306 d->raid_disk = i;
1307 d->state = (1<<MD_DISK_REMOVED);
1308 d->state |= (1<<MD_DISK_FAULTY);
1309 failed++;
1312 sb->nr_disks = nr_disks;
1313 sb->active_disks = active;
1314 sb->working_disks = working;
1315 sb->failed_disks = failed;
1316 sb->spare_disks = spare;
1318 sb->this_disk = sb->disks[rdev->desc_nr];
1319 sb->sb_csum = calc_sb_csum(sb);
1323 * rdev_size_change for 0.90.0
1325 static unsigned long long
1326 super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
1328 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1329 return 0; /* component must fit device */
1330 if (rdev->mddev->bitmap_info.offset)
1331 return 0; /* can't move bitmap */
1332 rdev->sb_start = calc_dev_sboffset(rdev);
1333 if (!num_sectors || num_sectors > rdev->sb_start)
1334 num_sectors = rdev->sb_start;
1335 /* Limit to 4TB as metadata cannot record more than that.
1336 * 4TB == 2^32 KB, or 2*2^32 sectors.
1338 if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
1339 num_sectors = (2ULL << 32) - 2;
1340 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1341 rdev->sb_page);
1342 md_super_wait(rdev->mddev);
1343 return num_sectors;
1346 static int
1347 super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1349 /* non-zero offset changes not possible with v0.90 */
1350 return new_offset == 0;
1354 * version 1 superblock
1357 static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
1359 __le32 disk_csum;
1360 u32 csum;
1361 unsigned long long newcsum;
1362 int size = 256 + le32_to_cpu(sb->max_dev)*2;
1363 __le32 *isuper = (__le32*)sb;
1365 disk_csum = sb->sb_csum;
1366 sb->sb_csum = 0;
1367 newcsum = 0;
1368 for (; size >= 4; size -= 4)
1369 newcsum += le32_to_cpu(*isuper++);
1371 if (size == 2)
1372 newcsum += le16_to_cpu(*(__le16*) isuper);
1374 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1375 sb->sb_csum = disk_csum;
1376 return cpu_to_le32(csum);
1379 static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1380 int acknowledged);
1381 static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
1383 struct mdp_superblock_1 *sb;
1384 int ret;
1385 sector_t sb_start;
1386 sector_t sectors;
1387 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1388 int bmask;
1391 * Calculate the position of the superblock in 512byte sectors.
1392 * It is always aligned to a 4K boundary and
1393 * depeding on minor_version, it can be:
1394 * 0: At least 8K, but less than 12K, from end of device
1395 * 1: At start of device
1396 * 2: 4K from start of device.
1398 switch(minor_version) {
1399 case 0:
1400 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
1401 sb_start -= 8*2;
1402 sb_start &= ~(sector_t)(4*2-1);
1403 break;
1404 case 1:
1405 sb_start = 0;
1406 break;
1407 case 2:
1408 sb_start = 8;
1409 break;
1410 default:
1411 return -EINVAL;
1413 rdev->sb_start = sb_start;
1415 /* superblock is rarely larger than 1K, but it can be larger,
1416 * and it is safe to read 4k, so we do that
1418 ret = read_disk_sb(rdev, 4096);
1419 if (ret) return ret;
1421 sb = page_address(rdev->sb_page);
1423 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1424 sb->major_version != cpu_to_le32(1) ||
1425 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1426 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1427 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1428 return -EINVAL;
1430 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1431 printk("md: invalid superblock checksum on %s\n",
1432 bdevname(rdev->bdev,b));
1433 return -EINVAL;
1435 if (le64_to_cpu(sb->data_size) < 10) {
1436 printk("md: data_size too small on %s\n",
1437 bdevname(rdev->bdev,b));
1438 return -EINVAL;
1440 if (sb->pad0 ||
1441 sb->pad3[0] ||
1442 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1443 /* Some padding is non-zero, might be a new feature */
1444 return -EINVAL;
1446 rdev->preferred_minor = 0xffff;
1447 rdev->data_offset = le64_to_cpu(sb->data_offset);
1448 rdev->new_data_offset = rdev->data_offset;
1449 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1450 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1451 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
1452 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1454 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1455 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1456 if (rdev->sb_size & bmask)
1457 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1459 if (minor_version
1460 && rdev->data_offset < sb_start + (rdev->sb_size/512))
1461 return -EINVAL;
1462 if (minor_version
1463 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1464 return -EINVAL;
1466 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1467 rdev->desc_nr = -1;
1468 else
1469 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1471 if (!rdev->bb_page) {
1472 rdev->bb_page = alloc_page(GFP_KERNEL);
1473 if (!rdev->bb_page)
1474 return -ENOMEM;
1476 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1477 rdev->badblocks.count == 0) {
1478 /* need to load the bad block list.
1479 * Currently we limit it to one page.
1481 s32 offset;
1482 sector_t bb_sector;
1483 u64 *bbp;
1484 int i;
1485 int sectors = le16_to_cpu(sb->bblog_size);
1486 if (sectors > (PAGE_SIZE / 512))
1487 return -EINVAL;
1488 offset = le32_to_cpu(sb->bblog_offset);
1489 if (offset == 0)
1490 return -EINVAL;
1491 bb_sector = (long long)offset;
1492 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1493 rdev->bb_page, READ, true))
1494 return -EIO;
1495 bbp = (u64 *)page_address(rdev->bb_page);
1496 rdev->badblocks.shift = sb->bblog_shift;
1497 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1498 u64 bb = le64_to_cpu(*bbp);
1499 int count = bb & (0x3ff);
1500 u64 sector = bb >> 10;
1501 sector <<= sb->bblog_shift;
1502 count <<= sb->bblog_shift;
1503 if (bb + 1 == 0)
1504 break;
1505 if (md_set_badblocks(&rdev->badblocks,
1506 sector, count, 1) == 0)
1507 return -EINVAL;
1509 } else if (sb->bblog_offset != 0)
1510 rdev->badblocks.shift = 0;
1512 if (!refdev) {
1513 ret = 1;
1514 } else {
1515 __u64 ev1, ev2;
1516 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
1518 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1519 sb->level != refsb->level ||
1520 sb->layout != refsb->layout ||
1521 sb->chunksize != refsb->chunksize) {
1522 printk(KERN_WARNING "md: %s has strangely different"
1523 " superblock to %s\n",
1524 bdevname(rdev->bdev,b),
1525 bdevname(refdev->bdev,b2));
1526 return -EINVAL;
1528 ev1 = le64_to_cpu(sb->events);
1529 ev2 = le64_to_cpu(refsb->events);
1531 if (ev1 > ev2)
1532 ret = 1;
1533 else
1534 ret = 0;
1536 if (minor_version) {
1537 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1538 sectors -= rdev->data_offset;
1539 } else
1540 sectors = rdev->sb_start;
1541 if (sectors < le64_to_cpu(sb->data_size))
1542 return -EINVAL;
1543 rdev->sectors = le64_to_cpu(sb->data_size);
1544 return ret;
1547 static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
1549 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
1550 __u64 ev1 = le64_to_cpu(sb->events);
1552 rdev->raid_disk = -1;
1553 clear_bit(Faulty, &rdev->flags);
1554 clear_bit(In_sync, &rdev->flags);
1555 clear_bit(Bitmap_sync, &rdev->flags);
1556 clear_bit(WriteMostly, &rdev->flags);
1558 if (mddev->raid_disks == 0) {
1559 mddev->major_version = 1;
1560 mddev->patch_version = 0;
1561 mddev->external = 0;
1562 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
1563 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1564 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1565 mddev->level = le32_to_cpu(sb->level);
1566 mddev->clevel[0] = 0;
1567 mddev->layout = le32_to_cpu(sb->layout);
1568 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1569 mddev->dev_sectors = le64_to_cpu(sb->size);
1570 mddev->events = ev1;
1571 mddev->bitmap_info.offset = 0;
1572 mddev->bitmap_info.space = 0;
1573 /* Default location for bitmap is 1K after superblock
1574 * using 3K - total of 4K
1576 mddev->bitmap_info.default_offset = 1024 >> 9;
1577 mddev->bitmap_info.default_space = (4096-1024) >> 9;
1578 mddev->reshape_backwards = 0;
1580 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1581 memcpy(mddev->uuid, sb->set_uuid, 16);
1583 mddev->max_disks = (4096-256)/2;
1585 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1586 mddev->bitmap_info.file == NULL) {
1587 mddev->bitmap_info.offset =
1588 (__s32)le32_to_cpu(sb->bitmap_offset);
1589 /* Metadata doesn't record how much space is available.
1590 * For 1.0, we assume we can use up to the superblock
1591 * if before, else to 4K beyond superblock.
1592 * For others, assume no change is possible.
1594 if (mddev->minor_version > 0)
1595 mddev->bitmap_info.space = 0;
1596 else if (mddev->bitmap_info.offset > 0)
1597 mddev->bitmap_info.space =
1598 8 - mddev->bitmap_info.offset;
1599 else
1600 mddev->bitmap_info.space =
1601 -mddev->bitmap_info.offset;
1604 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1605 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1606 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1607 mddev->new_level = le32_to_cpu(sb->new_level);
1608 mddev->new_layout = le32_to_cpu(sb->new_layout);
1609 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
1610 if (mddev->delta_disks < 0 ||
1611 (mddev->delta_disks == 0 &&
1612 (le32_to_cpu(sb->feature_map)
1613 & MD_FEATURE_RESHAPE_BACKWARDS)))
1614 mddev->reshape_backwards = 1;
1615 } else {
1616 mddev->reshape_position = MaxSector;
1617 mddev->delta_disks = 0;
1618 mddev->new_level = mddev->level;
1619 mddev->new_layout = mddev->layout;
1620 mddev->new_chunk_sectors = mddev->chunk_sectors;
1623 } else if (mddev->pers == NULL) {
1624 /* Insist of good event counter while assembling, except for
1625 * spares (which don't need an event count) */
1626 ++ev1;
1627 if (rdev->desc_nr >= 0 &&
1628 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1629 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1630 if (ev1 < mddev->events)
1631 return -EINVAL;
1632 } else if (mddev->bitmap) {
1633 /* If adding to array with a bitmap, then we can accept an
1634 * older device, but not too old.
1636 if (ev1 < mddev->bitmap->events_cleared)
1637 return 0;
1638 if (ev1 < mddev->events)
1639 set_bit(Bitmap_sync, &rdev->flags);
1640 } else {
1641 if (ev1 < mddev->events)
1642 /* just a hot-add of a new device, leave raid_disk at -1 */
1643 return 0;
1645 if (mddev->level != LEVEL_MULTIPATH) {
1646 int role;
1647 if (rdev->desc_nr < 0 ||
1648 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1649 role = 0xffff;
1650 rdev->desc_nr = -1;
1651 } else
1652 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1653 switch(role) {
1654 case 0xffff: /* spare */
1655 break;
1656 case 0xfffe: /* faulty */
1657 set_bit(Faulty, &rdev->flags);
1658 break;
1659 default:
1660 rdev->saved_raid_disk = role;
1661 if ((le32_to_cpu(sb->feature_map) &
1662 MD_FEATURE_RECOVERY_OFFSET)) {
1663 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1664 if (!(le32_to_cpu(sb->feature_map) &
1665 MD_FEATURE_RECOVERY_BITMAP))
1666 rdev->saved_raid_disk = -1;
1667 } else
1668 set_bit(In_sync, &rdev->flags);
1669 rdev->raid_disk = role;
1670 break;
1672 if (sb->devflags & WriteMostly1)
1673 set_bit(WriteMostly, &rdev->flags);
1674 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1675 set_bit(Replacement, &rdev->flags);
1676 } else /* MULTIPATH are always insync */
1677 set_bit(In_sync, &rdev->flags);
1679 return 0;
1682 static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
1684 struct mdp_superblock_1 *sb;
1685 struct md_rdev *rdev2;
1686 int max_dev, i;
1687 /* make rdev->sb match mddev and rdev data. */
1689 sb = page_address(rdev->sb_page);
1691 sb->feature_map = 0;
1692 sb->pad0 = 0;
1693 sb->recovery_offset = cpu_to_le64(0);
1694 memset(sb->pad3, 0, sizeof(sb->pad3));
1696 sb->utime = cpu_to_le64((__u64)mddev->utime);
1697 sb->events = cpu_to_le64(mddev->events);
1698 if (mddev->in_sync)
1699 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1700 else
1701 sb->resync_offset = cpu_to_le64(0);
1703 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1705 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1706 sb->size = cpu_to_le64(mddev->dev_sectors);
1707 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
1708 sb->level = cpu_to_le32(mddev->level);
1709 sb->layout = cpu_to_le32(mddev->layout);
1711 if (test_bit(WriteMostly, &rdev->flags))
1712 sb->devflags |= WriteMostly1;
1713 else
1714 sb->devflags &= ~WriteMostly1;
1715 sb->data_offset = cpu_to_le64(rdev->data_offset);
1716 sb->data_size = cpu_to_le64(rdev->sectors);
1718 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1719 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
1720 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1723 if (rdev->raid_disk >= 0 &&
1724 !test_bit(In_sync, &rdev->flags)) {
1725 sb->feature_map |=
1726 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1727 sb->recovery_offset =
1728 cpu_to_le64(rdev->recovery_offset);
1729 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1730 sb->feature_map |=
1731 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
1733 if (test_bit(Replacement, &rdev->flags))
1734 sb->feature_map |=
1735 cpu_to_le32(MD_FEATURE_REPLACEMENT);
1737 if (mddev->reshape_position != MaxSector) {
1738 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1739 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1740 sb->new_layout = cpu_to_le32(mddev->new_layout);
1741 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1742 sb->new_level = cpu_to_le32(mddev->new_level);
1743 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
1744 if (mddev->delta_disks == 0 &&
1745 mddev->reshape_backwards)
1746 sb->feature_map
1747 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
1748 if (rdev->new_data_offset != rdev->data_offset) {
1749 sb->feature_map
1750 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1751 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1752 - rdev->data_offset));
1756 if (rdev->badblocks.count == 0)
1757 /* Nothing to do for bad blocks*/ ;
1758 else if (sb->bblog_offset == 0)
1759 /* Cannot record bad blocks on this device */
1760 md_error(mddev, rdev);
1761 else {
1762 struct badblocks *bb = &rdev->badblocks;
1763 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1764 u64 *p = bb->page;
1765 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1766 if (bb->changed) {
1767 unsigned seq;
1769 retry:
1770 seq = read_seqbegin(&bb->lock);
1772 memset(bbp, 0xff, PAGE_SIZE);
1774 for (i = 0 ; i < bb->count ; i++) {
1775 u64 internal_bb = p[i];
1776 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1777 | BB_LEN(internal_bb));
1778 bbp[i] = cpu_to_le64(store_bb);
1780 bb->changed = 0;
1781 if (read_seqretry(&bb->lock, seq))
1782 goto retry;
1784 bb->sector = (rdev->sb_start +
1785 (int)le32_to_cpu(sb->bblog_offset));
1786 bb->size = le16_to_cpu(sb->bblog_size);
1790 max_dev = 0;
1791 rdev_for_each(rdev2, mddev)
1792 if (rdev2->desc_nr+1 > max_dev)
1793 max_dev = rdev2->desc_nr+1;
1795 if (max_dev > le32_to_cpu(sb->max_dev)) {
1796 int bmask;
1797 sb->max_dev = cpu_to_le32(max_dev);
1798 rdev->sb_size = max_dev * 2 + 256;
1799 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1800 if (rdev->sb_size & bmask)
1801 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1802 } else
1803 max_dev = le32_to_cpu(sb->max_dev);
1805 for (i=0; i<max_dev;i++)
1806 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1808 rdev_for_each(rdev2, mddev) {
1809 i = rdev2->desc_nr;
1810 if (test_bit(Faulty, &rdev2->flags))
1811 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1812 else if (test_bit(In_sync, &rdev2->flags))
1813 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1814 else if (rdev2->raid_disk >= 0)
1815 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1816 else
1817 sb->dev_roles[i] = cpu_to_le16(0xffff);
1820 sb->sb_csum = calc_sb_1_csum(sb);
1823 static unsigned long long
1824 super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
1826 struct mdp_superblock_1 *sb;
1827 sector_t max_sectors;
1828 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1829 return 0; /* component must fit device */
1830 if (rdev->data_offset != rdev->new_data_offset)
1831 return 0; /* too confusing */
1832 if (rdev->sb_start < rdev->data_offset) {
1833 /* minor versions 1 and 2; superblock before data */
1834 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
1835 max_sectors -= rdev->data_offset;
1836 if (!num_sectors || num_sectors > max_sectors)
1837 num_sectors = max_sectors;
1838 } else if (rdev->mddev->bitmap_info.offset) {
1839 /* minor version 0 with bitmap we can't move */
1840 return 0;
1841 } else {
1842 /* minor version 0; superblock after data */
1843 sector_t sb_start;
1844 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
1845 sb_start &= ~(sector_t)(4*2 - 1);
1846 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
1847 if (!num_sectors || num_sectors > max_sectors)
1848 num_sectors = max_sectors;
1849 rdev->sb_start = sb_start;
1851 sb = page_address(rdev->sb_page);
1852 sb->data_size = cpu_to_le64(num_sectors);
1853 sb->super_offset = rdev->sb_start;
1854 sb->sb_csum = calc_sb_1_csum(sb);
1855 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1856 rdev->sb_page);
1857 md_super_wait(rdev->mddev);
1858 return num_sectors;
1862 static int
1863 super_1_allow_new_offset(struct md_rdev *rdev,
1864 unsigned long long new_offset)
1866 /* All necessary checks on new >= old have been done */
1867 struct bitmap *bitmap;
1868 if (new_offset >= rdev->data_offset)
1869 return 1;
1871 /* with 1.0 metadata, there is no metadata to tread on
1872 * so we can always move back */
1873 if (rdev->mddev->minor_version == 0)
1874 return 1;
1876 /* otherwise we must be sure not to step on
1877 * any metadata, so stay:
1878 * 36K beyond start of superblock
1879 * beyond end of badblocks
1880 * beyond write-intent bitmap
1882 if (rdev->sb_start + (32+4)*2 > new_offset)
1883 return 0;
1884 bitmap = rdev->mddev->bitmap;
1885 if (bitmap && !rdev->mddev->bitmap_info.file &&
1886 rdev->sb_start + rdev->mddev->bitmap_info.offset +
1887 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
1888 return 0;
1889 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1890 return 0;
1892 return 1;
1895 static struct super_type super_types[] = {
1896 [0] = {
1897 .name = "0.90.0",
1898 .owner = THIS_MODULE,
1899 .load_super = super_90_load,
1900 .validate_super = super_90_validate,
1901 .sync_super = super_90_sync,
1902 .rdev_size_change = super_90_rdev_size_change,
1903 .allow_new_offset = super_90_allow_new_offset,
1905 [1] = {
1906 .name = "md-1",
1907 .owner = THIS_MODULE,
1908 .load_super = super_1_load,
1909 .validate_super = super_1_validate,
1910 .sync_super = super_1_sync,
1911 .rdev_size_change = super_1_rdev_size_change,
1912 .allow_new_offset = super_1_allow_new_offset,
1916 static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
1918 if (mddev->sync_super) {
1919 mddev->sync_super(mddev, rdev);
1920 return;
1923 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1925 super_types[mddev->major_version].sync_super(mddev, rdev);
1928 static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
1930 struct md_rdev *rdev, *rdev2;
1932 rcu_read_lock();
1933 rdev_for_each_rcu(rdev, mddev1)
1934 rdev_for_each_rcu(rdev2, mddev2)
1935 if (rdev->bdev->bd_contains ==
1936 rdev2->bdev->bd_contains) {
1937 rcu_read_unlock();
1938 return 1;
1940 rcu_read_unlock();
1941 return 0;
1944 static LIST_HEAD(pending_raid_disks);
1947 * Try to register data integrity profile for an mddev
1949 * This is called when an array is started and after a disk has been kicked
1950 * from the array. It only succeeds if all working and active component devices
1951 * are integrity capable with matching profiles.
1953 int md_integrity_register(struct mddev *mddev)
1955 struct md_rdev *rdev, *reference = NULL;
1957 if (list_empty(&mddev->disks))
1958 return 0; /* nothing to do */
1959 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1960 return 0; /* shouldn't register, or already is */
1961 rdev_for_each(rdev, mddev) {
1962 /* skip spares and non-functional disks */
1963 if (test_bit(Faulty, &rdev->flags))
1964 continue;
1965 if (rdev->raid_disk < 0)
1966 continue;
1967 if (!reference) {
1968 /* Use the first rdev as the reference */
1969 reference = rdev;
1970 continue;
1972 /* does this rdev's profile match the reference profile? */
1973 if (blk_integrity_compare(reference->bdev->bd_disk,
1974 rdev->bdev->bd_disk) < 0)
1975 return -EINVAL;
1977 if (!reference || !bdev_get_integrity(reference->bdev))
1978 return 0;
1980 * All component devices are integrity capable and have matching
1981 * profiles, register the common profile for the md device.
1983 if (blk_integrity_register(mddev->gendisk,
1984 bdev_get_integrity(reference->bdev)) != 0) {
1985 printk(KERN_ERR "md: failed to register integrity for %s\n",
1986 mdname(mddev));
1987 return -EINVAL;
1989 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1990 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1991 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1992 mdname(mddev));
1993 return -EINVAL;
1995 return 0;
1997 EXPORT_SYMBOL(md_integrity_register);
1999 /* Disable data integrity if non-capable/non-matching disk is being added */
2000 void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
2002 struct blk_integrity *bi_rdev;
2003 struct blk_integrity *bi_mddev;
2005 if (!mddev->gendisk)
2006 return;
2008 bi_rdev = bdev_get_integrity(rdev->bdev);
2009 bi_mddev = blk_get_integrity(mddev->gendisk);
2011 if (!bi_mddev) /* nothing to do */
2012 return;
2013 if (rdev->raid_disk < 0) /* skip spares */
2014 return;
2015 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2016 rdev->bdev->bd_disk) >= 0)
2017 return;
2018 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2019 blk_integrity_unregister(mddev->gendisk);
2021 EXPORT_SYMBOL(md_integrity_add_rdev);
2023 static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
2025 char b[BDEVNAME_SIZE];
2026 struct kobject *ko;
2027 char *s;
2028 int err;
2030 /* prevent duplicates */
2031 if (find_rdev(mddev, rdev->bdev->bd_dev))
2032 return -EEXIST;
2034 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2035 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2036 rdev->sectors < mddev->dev_sectors)) {
2037 if (mddev->pers) {
2038 /* Cannot change size, so fail
2039 * If mddev->level <= 0, then we don't care
2040 * about aligning sizes (e.g. linear)
2042 if (mddev->level > 0)
2043 return -ENOSPC;
2044 } else
2045 mddev->dev_sectors = rdev->sectors;
2048 /* Verify rdev->desc_nr is unique.
2049 * If it is -1, assign a free number, else
2050 * check number is not in use
2052 rcu_read_lock();
2053 if (rdev->desc_nr < 0) {
2054 int choice = 0;
2055 if (mddev->pers)
2056 choice = mddev->raid_disks;
2057 while (md_find_rdev_nr_rcu(mddev, choice))
2058 choice++;
2059 rdev->desc_nr = choice;
2060 } else {
2061 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
2062 rcu_read_unlock();
2063 return -EBUSY;
2066 rcu_read_unlock();
2067 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2068 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2069 mdname(mddev), mddev->max_disks);
2070 return -EBUSY;
2072 bdevname(rdev->bdev,b);
2073 while ( (s=strchr(b, '/')) != NULL)
2074 *s = '!';
2076 rdev->mddev = mddev;
2077 printk(KERN_INFO "md: bind<%s>\n", b);
2079 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
2080 goto fail;
2082 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
2083 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2084 /* failure here is OK */;
2085 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
2087 list_add_rcu(&rdev->same_set, &mddev->disks);
2088 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
2090 /* May as well allow recovery to be retried once */
2091 mddev->recovery_disabled++;
2093 return 0;
2095 fail:
2096 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2097 b, mdname(mddev));
2098 return err;
2101 static void md_delayed_delete(struct work_struct *ws)
2103 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
2104 kobject_del(&rdev->kobj);
2105 kobject_put(&rdev->kobj);
2108 static void unbind_rdev_from_array(struct md_rdev *rdev)
2110 char b[BDEVNAME_SIZE];
2112 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
2113 list_del_rcu(&rdev->same_set);
2114 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2115 rdev->mddev = NULL;
2116 sysfs_remove_link(&rdev->kobj, "block");
2117 sysfs_put(rdev->sysfs_state);
2118 rdev->sysfs_state = NULL;
2119 rdev->badblocks.count = 0;
2120 /* We need to delay this, otherwise we can deadlock when
2121 * writing to 'remove' to "dev/state". We also need
2122 * to delay it due to rcu usage.
2124 synchronize_rcu();
2125 INIT_WORK(&rdev->del_work, md_delayed_delete);
2126 kobject_get(&rdev->kobj);
2127 queue_work(md_misc_wq, &rdev->del_work);
2131 * prevent the device from being mounted, repartitioned or
2132 * otherwise reused by a RAID array (or any other kernel
2133 * subsystem), by bd_claiming the device.
2135 static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
2137 int err = 0;
2138 struct block_device *bdev;
2139 char b[BDEVNAME_SIZE];
2141 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
2142 shared ? (struct md_rdev *)lock_rdev : rdev);
2143 if (IS_ERR(bdev)) {
2144 printk(KERN_ERR "md: could not open %s.\n",
2145 __bdevname(dev, b));
2146 return PTR_ERR(bdev);
2148 rdev->bdev = bdev;
2149 return err;
2152 static void unlock_rdev(struct md_rdev *rdev)
2154 struct block_device *bdev = rdev->bdev;
2155 rdev->bdev = NULL;
2156 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
2159 void md_autodetect_dev(dev_t dev);
2161 static void export_rdev(struct md_rdev *rdev)
2163 char b[BDEVNAME_SIZE];
2165 printk(KERN_INFO "md: export_rdev(%s)\n",
2166 bdevname(rdev->bdev,b));
2167 md_rdev_clear(rdev);
2168 #ifndef MODULE
2169 if (test_bit(AutoDetected, &rdev->flags))
2170 md_autodetect_dev(rdev->bdev->bd_dev);
2171 #endif
2172 unlock_rdev(rdev);
2173 kobject_put(&rdev->kobj);
2176 void md_kick_rdev_from_array(struct md_rdev *rdev)
2178 unbind_rdev_from_array(rdev);
2179 export_rdev(rdev);
2181 EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
2183 static void export_array(struct mddev *mddev)
2185 struct md_rdev *rdev;
2187 while (!list_empty(&mddev->disks)) {
2188 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2189 same_set);
2190 md_kick_rdev_from_array(rdev);
2192 mddev->raid_disks = 0;
2193 mddev->major_version = 0;
2196 static void sync_sbs(struct mddev *mddev, int nospares)
2198 /* Update each superblock (in-memory image), but
2199 * if we are allowed to, skip spares which already
2200 * have the right event counter, or have one earlier
2201 * (which would mean they aren't being marked as dirty
2202 * with the rest of the array)
2204 struct md_rdev *rdev;
2205 rdev_for_each(rdev, mddev) {
2206 if (rdev->sb_events == mddev->events ||
2207 (nospares &&
2208 rdev->raid_disk < 0 &&
2209 rdev->sb_events+1 == mddev->events)) {
2210 /* Don't update this superblock */
2211 rdev->sb_loaded = 2;
2212 } else {
2213 sync_super(mddev, rdev);
2214 rdev->sb_loaded = 1;
2219 void md_update_sb(struct mddev *mddev, int force_change)
2221 struct md_rdev *rdev;
2222 int sync_req;
2223 int nospares = 0;
2224 int any_badblocks_changed = 0;
2226 if (mddev->ro) {
2227 if (force_change)
2228 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2229 return;
2231 repeat:
2232 /* First make sure individual recovery_offsets are correct */
2233 rdev_for_each(rdev, mddev) {
2234 if (rdev->raid_disk >= 0 &&
2235 mddev->delta_disks >= 0 &&
2236 !test_bit(In_sync, &rdev->flags) &&
2237 mddev->curr_resync_completed > rdev->recovery_offset)
2238 rdev->recovery_offset = mddev->curr_resync_completed;
2241 if (!mddev->persistent) {
2242 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
2243 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
2244 if (!mddev->external) {
2245 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2246 rdev_for_each(rdev, mddev) {
2247 if (rdev->badblocks.changed) {
2248 rdev->badblocks.changed = 0;
2249 md_ack_all_badblocks(&rdev->badblocks);
2250 md_error(mddev, rdev);
2252 clear_bit(Blocked, &rdev->flags);
2253 clear_bit(BlockedBadBlocks, &rdev->flags);
2254 wake_up(&rdev->blocked_wait);
2257 wake_up(&mddev->sb_wait);
2258 return;
2261 spin_lock(&mddev->lock);
2263 mddev->utime = get_seconds();
2265 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2266 force_change = 1;
2267 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2268 /* just a clean<-> dirty transition, possibly leave spares alone,
2269 * though if events isn't the right even/odd, we will have to do
2270 * spares after all
2272 nospares = 1;
2273 if (force_change)
2274 nospares = 0;
2275 if (mddev->degraded)
2276 /* If the array is degraded, then skipping spares is both
2277 * dangerous and fairly pointless.
2278 * Dangerous because a device that was removed from the array
2279 * might have a event_count that still looks up-to-date,
2280 * so it can be re-added without a resync.
2281 * Pointless because if there are any spares to skip,
2282 * then a recovery will happen and soon that array won't
2283 * be degraded any more and the spare can go back to sleep then.
2285 nospares = 0;
2287 sync_req = mddev->in_sync;
2289 /* If this is just a dirty<->clean transition, and the array is clean
2290 * and 'events' is odd, we can roll back to the previous clean state */
2291 if (nospares
2292 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
2293 && mddev->can_decrease_events
2294 && mddev->events != 1) {
2295 mddev->events--;
2296 mddev->can_decrease_events = 0;
2297 } else {
2298 /* otherwise we have to go forward and ... */
2299 mddev->events ++;
2300 mddev->can_decrease_events = nospares;
2304 * This 64-bit counter should never wrap.
2305 * Either we are in around ~1 trillion A.C., assuming
2306 * 1 reboot per second, or we have a bug...
2308 WARN_ON(mddev->events == 0);
2310 rdev_for_each(rdev, mddev) {
2311 if (rdev->badblocks.changed)
2312 any_badblocks_changed++;
2313 if (test_bit(Faulty, &rdev->flags))
2314 set_bit(FaultRecorded, &rdev->flags);
2317 sync_sbs(mddev, nospares);
2318 spin_unlock(&mddev->lock);
2320 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2321 mdname(mddev), mddev->in_sync);
2323 bitmap_update_sb(mddev->bitmap);
2324 rdev_for_each(rdev, mddev) {
2325 char b[BDEVNAME_SIZE];
2327 if (rdev->sb_loaded != 1)
2328 continue; /* no noise on spare devices */
2330 if (!test_bit(Faulty, &rdev->flags)) {
2331 md_super_write(mddev,rdev,
2332 rdev->sb_start, rdev->sb_size,
2333 rdev->sb_page);
2334 pr_debug("md: (write) %s's sb offset: %llu\n",
2335 bdevname(rdev->bdev, b),
2336 (unsigned long long)rdev->sb_start);
2337 rdev->sb_events = mddev->events;
2338 if (rdev->badblocks.size) {
2339 md_super_write(mddev, rdev,
2340 rdev->badblocks.sector,
2341 rdev->badblocks.size << 9,
2342 rdev->bb_page);
2343 rdev->badblocks.size = 0;
2346 } else
2347 pr_debug("md: %s (skipping faulty)\n",
2348 bdevname(rdev->bdev, b));
2350 if (mddev->level == LEVEL_MULTIPATH)
2351 /* only need to write one superblock... */
2352 break;
2354 md_super_wait(mddev);
2355 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2357 spin_lock(&mddev->lock);
2358 if (mddev->in_sync != sync_req ||
2359 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
2360 /* have to write it out again */
2361 spin_unlock(&mddev->lock);
2362 goto repeat;
2364 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2365 spin_unlock(&mddev->lock);
2366 wake_up(&mddev->sb_wait);
2367 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2368 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
2370 rdev_for_each(rdev, mddev) {
2371 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2372 clear_bit(Blocked, &rdev->flags);
2374 if (any_badblocks_changed)
2375 md_ack_all_badblocks(&rdev->badblocks);
2376 clear_bit(BlockedBadBlocks, &rdev->flags);
2377 wake_up(&rdev->blocked_wait);
2380 EXPORT_SYMBOL(md_update_sb);
2382 static int add_bound_rdev(struct md_rdev *rdev)
2384 struct mddev *mddev = rdev->mddev;
2385 int err = 0;
2387 if (!mddev->pers->hot_remove_disk) {
2388 /* If there is hot_add_disk but no hot_remove_disk
2389 * then added disks for geometry changes,
2390 * and should be added immediately.
2392 super_types[mddev->major_version].
2393 validate_super(mddev, rdev);
2394 err = mddev->pers->hot_add_disk(mddev, rdev);
2395 if (err) {
2396 unbind_rdev_from_array(rdev);
2397 export_rdev(rdev);
2398 return err;
2401 sysfs_notify_dirent_safe(rdev->sysfs_state);
2403 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2404 if (mddev->degraded)
2405 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2406 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2407 md_new_event(mddev);
2408 md_wakeup_thread(mddev->thread);
2409 return 0;
2412 /* words written to sysfs files may, or may not, be \n terminated.
2413 * We want to accept with case. For this we use cmd_match.
2415 static int cmd_match(const char *cmd, const char *str)
2417 /* See if cmd, written into a sysfs file, matches
2418 * str. They must either be the same, or cmd can
2419 * have a trailing newline
2421 while (*cmd && *str && *cmd == *str) {
2422 cmd++;
2423 str++;
2425 if (*cmd == '\n')
2426 cmd++;
2427 if (*str || *cmd)
2428 return 0;
2429 return 1;
2432 struct rdev_sysfs_entry {
2433 struct attribute attr;
2434 ssize_t (*show)(struct md_rdev *, char *);
2435 ssize_t (*store)(struct md_rdev *, const char *, size_t);
2438 static ssize_t
2439 state_show(struct md_rdev *rdev, char *page)
2441 char *sep = "";
2442 size_t len = 0;
2443 unsigned long flags = ACCESS_ONCE(rdev->flags);
2445 if (test_bit(Faulty, &flags) ||
2446 rdev->badblocks.unacked_exist) {
2447 len+= sprintf(page+len, "%sfaulty",sep);
2448 sep = ",";
2450 if (test_bit(In_sync, &flags)) {
2451 len += sprintf(page+len, "%sin_sync",sep);
2452 sep = ",";
2454 if (test_bit(WriteMostly, &flags)) {
2455 len += sprintf(page+len, "%swrite_mostly",sep);
2456 sep = ",";
2458 if (test_bit(Blocked, &flags) ||
2459 (rdev->badblocks.unacked_exist
2460 && !test_bit(Faulty, &flags))) {
2461 len += sprintf(page+len, "%sblocked", sep);
2462 sep = ",";
2464 if (!test_bit(Faulty, &flags) &&
2465 !test_bit(In_sync, &flags)) {
2466 len += sprintf(page+len, "%sspare", sep);
2467 sep = ",";
2469 if (test_bit(WriteErrorSeen, &flags)) {
2470 len += sprintf(page+len, "%swrite_error", sep);
2471 sep = ",";
2473 if (test_bit(WantReplacement, &flags)) {
2474 len += sprintf(page+len, "%swant_replacement", sep);
2475 sep = ",";
2477 if (test_bit(Replacement, &flags)) {
2478 len += sprintf(page+len, "%sreplacement", sep);
2479 sep = ",";
2482 return len+sprintf(page+len, "\n");
2485 static ssize_t
2486 state_store(struct md_rdev *rdev, const char *buf, size_t len)
2488 /* can write
2489 * faulty - simulates an error
2490 * remove - disconnects the device
2491 * writemostly - sets write_mostly
2492 * -writemostly - clears write_mostly
2493 * blocked - sets the Blocked flags
2494 * -blocked - clears the Blocked and possibly simulates an error
2495 * insync - sets Insync providing device isn't active
2496 * -insync - clear Insync for a device with a slot assigned,
2497 * so that it gets rebuilt based on bitmap
2498 * write_error - sets WriteErrorSeen
2499 * -write_error - clears WriteErrorSeen
2501 int err = -EINVAL;
2502 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2503 md_error(rdev->mddev, rdev);
2504 if (test_bit(Faulty, &rdev->flags))
2505 err = 0;
2506 else
2507 err = -EBUSY;
2508 } else if (cmd_match(buf, "remove")) {
2509 if (rdev->raid_disk >= 0)
2510 err = -EBUSY;
2511 else {
2512 struct mddev *mddev = rdev->mddev;
2513 if (mddev_is_clustered(mddev))
2514 md_cluster_ops->remove_disk(mddev, rdev);
2515 md_kick_rdev_from_array(rdev);
2516 if (mddev_is_clustered(mddev))
2517 md_cluster_ops->metadata_update_start(mddev);
2518 if (mddev->pers)
2519 md_update_sb(mddev, 1);
2520 md_new_event(mddev);
2521 if (mddev_is_clustered(mddev))
2522 md_cluster_ops->metadata_update_finish(mddev);
2523 err = 0;
2525 } else if (cmd_match(buf, "writemostly")) {
2526 set_bit(WriteMostly, &rdev->flags);
2527 err = 0;
2528 } else if (cmd_match(buf, "-writemostly")) {
2529 clear_bit(WriteMostly, &rdev->flags);
2530 err = 0;
2531 } else if (cmd_match(buf, "blocked")) {
2532 set_bit(Blocked, &rdev->flags);
2533 err = 0;
2534 } else if (cmd_match(buf, "-blocked")) {
2535 if (!test_bit(Faulty, &rdev->flags) &&
2536 rdev->badblocks.unacked_exist) {
2537 /* metadata handler doesn't understand badblocks,
2538 * so we need to fail the device
2540 md_error(rdev->mddev, rdev);
2542 clear_bit(Blocked, &rdev->flags);
2543 clear_bit(BlockedBadBlocks, &rdev->flags);
2544 wake_up(&rdev->blocked_wait);
2545 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2546 md_wakeup_thread(rdev->mddev->thread);
2548 err = 0;
2549 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2550 set_bit(In_sync, &rdev->flags);
2551 err = 0;
2552 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0) {
2553 if (rdev->mddev->pers == NULL) {
2554 clear_bit(In_sync, &rdev->flags);
2555 rdev->saved_raid_disk = rdev->raid_disk;
2556 rdev->raid_disk = -1;
2557 err = 0;
2559 } else if (cmd_match(buf, "write_error")) {
2560 set_bit(WriteErrorSeen, &rdev->flags);
2561 err = 0;
2562 } else if (cmd_match(buf, "-write_error")) {
2563 clear_bit(WriteErrorSeen, &rdev->flags);
2564 err = 0;
2565 } else if (cmd_match(buf, "want_replacement")) {
2566 /* Any non-spare device that is not a replacement can
2567 * become want_replacement at any time, but we then need to
2568 * check if recovery is needed.
2570 if (rdev->raid_disk >= 0 &&
2571 !test_bit(Replacement, &rdev->flags))
2572 set_bit(WantReplacement, &rdev->flags);
2573 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2574 md_wakeup_thread(rdev->mddev->thread);
2575 err = 0;
2576 } else if (cmd_match(buf, "-want_replacement")) {
2577 /* Clearing 'want_replacement' is always allowed.
2578 * Once replacements starts it is too late though.
2580 err = 0;
2581 clear_bit(WantReplacement, &rdev->flags);
2582 } else if (cmd_match(buf, "replacement")) {
2583 /* Can only set a device as a replacement when array has not
2584 * yet been started. Once running, replacement is automatic
2585 * from spares, or by assigning 'slot'.
2587 if (rdev->mddev->pers)
2588 err = -EBUSY;
2589 else {
2590 set_bit(Replacement, &rdev->flags);
2591 err = 0;
2593 } else if (cmd_match(buf, "-replacement")) {
2594 /* Similarly, can only clear Replacement before start */
2595 if (rdev->mddev->pers)
2596 err = -EBUSY;
2597 else {
2598 clear_bit(Replacement, &rdev->flags);
2599 err = 0;
2601 } else if (cmd_match(buf, "re-add")) {
2602 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) {
2603 /* clear_bit is performed _after_ all the devices
2604 * have their local Faulty bit cleared. If any writes
2605 * happen in the meantime in the local node, they
2606 * will land in the local bitmap, which will be synced
2607 * by this node eventually
2609 if (!mddev_is_clustered(rdev->mddev) ||
2610 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2611 clear_bit(Faulty, &rdev->flags);
2612 err = add_bound_rdev(rdev);
2614 } else
2615 err = -EBUSY;
2617 if (!err)
2618 sysfs_notify_dirent_safe(rdev->sysfs_state);
2619 return err ? err : len;
2621 static struct rdev_sysfs_entry rdev_state =
2622 __ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
2624 static ssize_t
2625 errors_show(struct md_rdev *rdev, char *page)
2627 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2630 static ssize_t
2631 errors_store(struct md_rdev *rdev, const char *buf, size_t len)
2633 char *e;
2634 unsigned long n = simple_strtoul(buf, &e, 10);
2635 if (*buf && (*e == 0 || *e == '\n')) {
2636 atomic_set(&rdev->corrected_errors, n);
2637 return len;
2639 return -EINVAL;
2641 static struct rdev_sysfs_entry rdev_errors =
2642 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
2644 static ssize_t
2645 slot_show(struct md_rdev *rdev, char *page)
2647 if (rdev->raid_disk < 0)
2648 return sprintf(page, "none\n");
2649 else
2650 return sprintf(page, "%d\n", rdev->raid_disk);
2653 static ssize_t
2654 slot_store(struct md_rdev *rdev, const char *buf, size_t len)
2656 char *e;
2657 int err;
2658 int slot = simple_strtoul(buf, &e, 10);
2659 if (strncmp(buf, "none", 4)==0)
2660 slot = -1;
2661 else if (e==buf || (*e && *e!= '\n'))
2662 return -EINVAL;
2663 if (rdev->mddev->pers && slot == -1) {
2664 /* Setting 'slot' on an active array requires also
2665 * updating the 'rd%d' link, and communicating
2666 * with the personality with ->hot_*_disk.
2667 * For now we only support removing
2668 * failed/spare devices. This normally happens automatically,
2669 * but not when the metadata is externally managed.
2671 if (rdev->raid_disk == -1)
2672 return -EEXIST;
2673 /* personality does all needed checks */
2674 if (rdev->mddev->pers->hot_remove_disk == NULL)
2675 return -EINVAL;
2676 clear_bit(Blocked, &rdev->flags);
2677 remove_and_add_spares(rdev->mddev, rdev);
2678 if (rdev->raid_disk >= 0)
2679 return -EBUSY;
2680 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2681 md_wakeup_thread(rdev->mddev->thread);
2682 } else if (rdev->mddev->pers) {
2683 /* Activating a spare .. or possibly reactivating
2684 * if we ever get bitmaps working here.
2687 if (rdev->raid_disk != -1)
2688 return -EBUSY;
2690 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2691 return -EBUSY;
2693 if (rdev->mddev->pers->hot_add_disk == NULL)
2694 return -EINVAL;
2696 if (slot >= rdev->mddev->raid_disks &&
2697 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2698 return -ENOSPC;
2700 rdev->raid_disk = slot;
2701 if (test_bit(In_sync, &rdev->flags))
2702 rdev->saved_raid_disk = slot;
2703 else
2704 rdev->saved_raid_disk = -1;
2705 clear_bit(In_sync, &rdev->flags);
2706 clear_bit(Bitmap_sync, &rdev->flags);
2707 err = rdev->mddev->pers->
2708 hot_add_disk(rdev->mddev, rdev);
2709 if (err) {
2710 rdev->raid_disk = -1;
2711 return err;
2712 } else
2713 sysfs_notify_dirent_safe(rdev->sysfs_state);
2714 if (sysfs_link_rdev(rdev->mddev, rdev))
2715 /* failure here is OK */;
2716 /* don't wakeup anyone, leave that to userspace. */
2717 } else {
2718 if (slot >= rdev->mddev->raid_disks &&
2719 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2720 return -ENOSPC;
2721 rdev->raid_disk = slot;
2722 /* assume it is working */
2723 clear_bit(Faulty, &rdev->flags);
2724 clear_bit(WriteMostly, &rdev->flags);
2725 set_bit(In_sync, &rdev->flags);
2726 sysfs_notify_dirent_safe(rdev->sysfs_state);
2728 return len;
2731 static struct rdev_sysfs_entry rdev_slot =
2732 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
2734 static ssize_t
2735 offset_show(struct md_rdev *rdev, char *page)
2737 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
2740 static ssize_t
2741 offset_store(struct md_rdev *rdev, const char *buf, size_t len)
2743 unsigned long long offset;
2744 if (kstrtoull(buf, 10, &offset) < 0)
2745 return -EINVAL;
2746 if (rdev->mddev->pers && rdev->raid_disk >= 0)
2747 return -EBUSY;
2748 if (rdev->sectors && rdev->mddev->external)
2749 /* Must set offset before size, so overlap checks
2750 * can be sane */
2751 return -EBUSY;
2752 rdev->data_offset = offset;
2753 rdev->new_data_offset = offset;
2754 return len;
2757 static struct rdev_sysfs_entry rdev_offset =
2758 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
2760 static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2762 return sprintf(page, "%llu\n",
2763 (unsigned long long)rdev->new_data_offset);
2766 static ssize_t new_offset_store(struct md_rdev *rdev,
2767 const char *buf, size_t len)
2769 unsigned long long new_offset;
2770 struct mddev *mddev = rdev->mddev;
2772 if (kstrtoull(buf, 10, &new_offset) < 0)
2773 return -EINVAL;
2775 if (mddev->sync_thread ||
2776 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
2777 return -EBUSY;
2778 if (new_offset == rdev->data_offset)
2779 /* reset is always permitted */
2781 else if (new_offset > rdev->data_offset) {
2782 /* must not push array size beyond rdev_sectors */
2783 if (new_offset - rdev->data_offset
2784 + mddev->dev_sectors > rdev->sectors)
2785 return -E2BIG;
2787 /* Metadata worries about other space details. */
2789 /* decreasing the offset is inconsistent with a backwards
2790 * reshape.
2792 if (new_offset < rdev->data_offset &&
2793 mddev->reshape_backwards)
2794 return -EINVAL;
2795 /* Increasing offset is inconsistent with forwards
2796 * reshape. reshape_direction should be set to
2797 * 'backwards' first.
2799 if (new_offset > rdev->data_offset &&
2800 !mddev->reshape_backwards)
2801 return -EINVAL;
2803 if (mddev->pers && mddev->persistent &&
2804 !super_types[mddev->major_version]
2805 .allow_new_offset(rdev, new_offset))
2806 return -E2BIG;
2807 rdev->new_data_offset = new_offset;
2808 if (new_offset > rdev->data_offset)
2809 mddev->reshape_backwards = 1;
2810 else if (new_offset < rdev->data_offset)
2811 mddev->reshape_backwards = 0;
2813 return len;
2815 static struct rdev_sysfs_entry rdev_new_offset =
2816 __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2818 static ssize_t
2819 rdev_size_show(struct md_rdev *rdev, char *page)
2821 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
2824 static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2826 /* check if two start/length pairs overlap */
2827 if (s1+l1 <= s2)
2828 return 0;
2829 if (s2+l2 <= s1)
2830 return 0;
2831 return 1;
2834 static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2836 unsigned long long blocks;
2837 sector_t new;
2839 if (kstrtoull(buf, 10, &blocks) < 0)
2840 return -EINVAL;
2842 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2843 return -EINVAL; /* sector conversion overflow */
2845 new = blocks * 2;
2846 if (new != blocks * 2)
2847 return -EINVAL; /* unsigned long long to sector_t overflow */
2849 *sectors = new;
2850 return 0;
2853 static ssize_t
2854 rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
2856 struct mddev *my_mddev = rdev->mddev;
2857 sector_t oldsectors = rdev->sectors;
2858 sector_t sectors;
2860 if (strict_blocks_to_sectors(buf, &sectors) < 0)
2861 return -EINVAL;
2862 if (rdev->data_offset != rdev->new_data_offset)
2863 return -EINVAL; /* too confusing */
2864 if (my_mddev->pers && rdev->raid_disk >= 0) {
2865 if (my_mddev->persistent) {
2866 sectors = super_types[my_mddev->major_version].
2867 rdev_size_change(rdev, sectors);
2868 if (!sectors)
2869 return -EBUSY;
2870 } else if (!sectors)
2871 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
2872 rdev->data_offset;
2873 if (!my_mddev->pers->resize)
2874 /* Cannot change size for RAID0 or Linear etc */
2875 return -EINVAL;
2877 if (sectors < my_mddev->dev_sectors)
2878 return -EINVAL; /* component must fit device */
2880 rdev->sectors = sectors;
2881 if (sectors > oldsectors && my_mddev->external) {
2882 /* Need to check that all other rdevs with the same
2883 * ->bdev do not overlap. 'rcu' is sufficient to walk
2884 * the rdev lists safely.
2885 * This check does not provide a hard guarantee, it
2886 * just helps avoid dangerous mistakes.
2888 struct mddev *mddev;
2889 int overlap = 0;
2890 struct list_head *tmp;
2892 rcu_read_lock();
2893 for_each_mddev(mddev, tmp) {
2894 struct md_rdev *rdev2;
2896 rdev_for_each(rdev2, mddev)
2897 if (rdev->bdev == rdev2->bdev &&
2898 rdev != rdev2 &&
2899 overlaps(rdev->data_offset, rdev->sectors,
2900 rdev2->data_offset,
2901 rdev2->sectors)) {
2902 overlap = 1;
2903 break;
2905 if (overlap) {
2906 mddev_put(mddev);
2907 break;
2910 rcu_read_unlock();
2911 if (overlap) {
2912 /* Someone else could have slipped in a size
2913 * change here, but doing so is just silly.
2914 * We put oldsectors back because we *know* it is
2915 * safe, and trust userspace not to race with
2916 * itself
2918 rdev->sectors = oldsectors;
2919 return -EBUSY;
2922 return len;
2925 static struct rdev_sysfs_entry rdev_size =
2926 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
2928 static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
2930 unsigned long long recovery_start = rdev->recovery_offset;
2932 if (test_bit(In_sync, &rdev->flags) ||
2933 recovery_start == MaxSector)
2934 return sprintf(page, "none\n");
2936 return sprintf(page, "%llu\n", recovery_start);
2939 static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
2941 unsigned long long recovery_start;
2943 if (cmd_match(buf, "none"))
2944 recovery_start = MaxSector;
2945 else if (kstrtoull(buf, 10, &recovery_start))
2946 return -EINVAL;
2948 if (rdev->mddev->pers &&
2949 rdev->raid_disk >= 0)
2950 return -EBUSY;
2952 rdev->recovery_offset = recovery_start;
2953 if (recovery_start == MaxSector)
2954 set_bit(In_sync, &rdev->flags);
2955 else
2956 clear_bit(In_sync, &rdev->flags);
2957 return len;
2960 static struct rdev_sysfs_entry rdev_recovery_start =
2961 __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2963 static ssize_t
2964 badblocks_show(struct badblocks *bb, char *page, int unack);
2965 static ssize_t
2966 badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
2968 static ssize_t bb_show(struct md_rdev *rdev, char *page)
2970 return badblocks_show(&rdev->badblocks, page, 0);
2972 static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
2974 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
2975 /* Maybe that ack was all we needed */
2976 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
2977 wake_up(&rdev->blocked_wait);
2978 return rv;
2980 static struct rdev_sysfs_entry rdev_bad_blocks =
2981 __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
2983 static ssize_t ubb_show(struct md_rdev *rdev, char *page)
2985 return badblocks_show(&rdev->badblocks, page, 1);
2987 static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
2989 return badblocks_store(&rdev->badblocks, page, len, 1);
2991 static struct rdev_sysfs_entry rdev_unack_bad_blocks =
2992 __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
2994 static struct attribute *rdev_default_attrs[] = {
2995 &rdev_state.attr,
2996 &rdev_errors.attr,
2997 &rdev_slot.attr,
2998 &rdev_offset.attr,
2999 &rdev_new_offset.attr,
3000 &rdev_size.attr,
3001 &rdev_recovery_start.attr,
3002 &rdev_bad_blocks.attr,
3003 &rdev_unack_bad_blocks.attr,
3004 NULL,
3006 static ssize_t
3007 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3009 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
3010 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
3012 if (!entry->show)
3013 return -EIO;
3014 if (!rdev->mddev)
3015 return -EBUSY;
3016 return entry->show(rdev, page);
3019 static ssize_t
3020 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3021 const char *page, size_t length)
3023 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
3024 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
3025 ssize_t rv;
3026 struct mddev *mddev = rdev->mddev;
3028 if (!entry->store)
3029 return -EIO;
3030 if (!capable(CAP_SYS_ADMIN))
3031 return -EACCES;
3032 rv = mddev ? mddev_lock(mddev): -EBUSY;
3033 if (!rv) {
3034 if (rdev->mddev == NULL)
3035 rv = -EBUSY;
3036 else
3037 rv = entry->store(rdev, page, length);
3038 mddev_unlock(mddev);
3040 return rv;
3043 static void rdev_free(struct kobject *ko)
3045 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
3046 kfree(rdev);
3048 static const struct sysfs_ops rdev_sysfs_ops = {
3049 .show = rdev_attr_show,
3050 .store = rdev_attr_store,
3052 static struct kobj_type rdev_ktype = {
3053 .release = rdev_free,
3054 .sysfs_ops = &rdev_sysfs_ops,
3055 .default_attrs = rdev_default_attrs,
3058 int md_rdev_init(struct md_rdev *rdev)
3060 rdev->desc_nr = -1;
3061 rdev->saved_raid_disk = -1;
3062 rdev->raid_disk = -1;
3063 rdev->flags = 0;
3064 rdev->data_offset = 0;
3065 rdev->new_data_offset = 0;
3066 rdev->sb_events = 0;
3067 rdev->last_read_error.tv_sec = 0;
3068 rdev->last_read_error.tv_nsec = 0;
3069 rdev->sb_loaded = 0;
3070 rdev->bb_page = NULL;
3071 atomic_set(&rdev->nr_pending, 0);
3072 atomic_set(&rdev->read_errors, 0);
3073 atomic_set(&rdev->corrected_errors, 0);
3075 INIT_LIST_HEAD(&rdev->same_set);
3076 init_waitqueue_head(&rdev->blocked_wait);
3078 /* Add space to store bad block list.
3079 * This reserves the space even on arrays where it cannot
3080 * be used - I wonder if that matters
3082 rdev->badblocks.count = 0;
3083 rdev->badblocks.shift = -1; /* disabled until explicitly enabled */
3084 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3085 seqlock_init(&rdev->badblocks.lock);
3086 if (rdev->badblocks.page == NULL)
3087 return -ENOMEM;
3089 return 0;
3091 EXPORT_SYMBOL_GPL(md_rdev_init);
3093 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3095 * mark the device faulty if:
3097 * - the device is nonexistent (zero size)
3098 * - the device has no valid superblock
3100 * a faulty rdev _never_ has rdev->sb set.
3102 static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
3104 char b[BDEVNAME_SIZE];
3105 int err;
3106 struct md_rdev *rdev;
3107 sector_t size;
3109 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
3110 if (!rdev) {
3111 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3112 return ERR_PTR(-ENOMEM);
3115 err = md_rdev_init(rdev);
3116 if (err)
3117 goto abort_free;
3118 err = alloc_disk_sb(rdev);
3119 if (err)
3120 goto abort_free;
3122 err = lock_rdev(rdev, newdev, super_format == -2);
3123 if (err)
3124 goto abort_free;
3126 kobject_init(&rdev->kobj, &rdev_ktype);
3128 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
3129 if (!size) {
3130 printk(KERN_WARNING
3131 "md: %s has zero or unknown size, marking faulty!\n",
3132 bdevname(rdev->bdev,b));
3133 err = -EINVAL;
3134 goto abort_free;
3137 if (super_format >= 0) {
3138 err = super_types[super_format].
3139 load_super(rdev, NULL, super_minor);
3140 if (err == -EINVAL) {
3141 printk(KERN_WARNING
3142 "md: %s does not have a valid v%d.%d "
3143 "superblock, not importing!\n",
3144 bdevname(rdev->bdev,b),
3145 super_format, super_minor);
3146 goto abort_free;
3148 if (err < 0) {
3149 printk(KERN_WARNING
3150 "md: could not read %s's sb, not importing!\n",
3151 bdevname(rdev->bdev,b));
3152 goto abort_free;
3156 return rdev;
3158 abort_free:
3159 if (rdev->bdev)
3160 unlock_rdev(rdev);
3161 md_rdev_clear(rdev);
3162 kfree(rdev);
3163 return ERR_PTR(err);
3167 * Check a full RAID array for plausibility
3170 static void analyze_sbs(struct mddev *mddev)
3172 int i;
3173 struct md_rdev *rdev, *freshest, *tmp;
3174 char b[BDEVNAME_SIZE];
3176 freshest = NULL;
3177 rdev_for_each_safe(rdev, tmp, mddev)
3178 switch (super_types[mddev->major_version].
3179 load_super(rdev, freshest, mddev->minor_version)) {
3180 case 1:
3181 freshest = rdev;
3182 break;
3183 case 0:
3184 break;
3185 default:
3186 printk( KERN_ERR \
3187 "md: fatal superblock inconsistency in %s"
3188 " -- removing from array\n",
3189 bdevname(rdev->bdev,b));
3190 md_kick_rdev_from_array(rdev);
3193 super_types[mddev->major_version].
3194 validate_super(mddev, freshest);
3196 i = 0;
3197 rdev_for_each_safe(rdev, tmp, mddev) {
3198 if (mddev->max_disks &&
3199 (rdev->desc_nr >= mddev->max_disks ||
3200 i > mddev->max_disks)) {
3201 printk(KERN_WARNING
3202 "md: %s: %s: only %d devices permitted\n",
3203 mdname(mddev), bdevname(rdev->bdev, b),
3204 mddev->max_disks);
3205 md_kick_rdev_from_array(rdev);
3206 continue;
3208 if (rdev != freshest) {
3209 if (super_types[mddev->major_version].
3210 validate_super(mddev, rdev)) {
3211 printk(KERN_WARNING "md: kicking non-fresh %s"
3212 " from array!\n",
3213 bdevname(rdev->bdev,b));
3214 md_kick_rdev_from_array(rdev);
3215 continue;
3217 /* No device should have a Candidate flag
3218 * when reading devices
3220 if (test_bit(Candidate, &rdev->flags)) {
3221 pr_info("md: kicking Cluster Candidate %s from array!\n",
3222 bdevname(rdev->bdev, b));
3223 md_kick_rdev_from_array(rdev);
3226 if (mddev->level == LEVEL_MULTIPATH) {
3227 rdev->desc_nr = i++;
3228 rdev->raid_disk = rdev->desc_nr;
3229 set_bit(In_sync, &rdev->flags);
3230 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
3231 rdev->raid_disk = -1;
3232 clear_bit(In_sync, &rdev->flags);
3237 /* Read a fixed-point number.
3238 * Numbers in sysfs attributes should be in "standard" units where
3239 * possible, so time should be in seconds.
3240 * However we internally use a a much smaller unit such as
3241 * milliseconds or jiffies.
3242 * This function takes a decimal number with a possible fractional
3243 * component, and produces an integer which is the result of
3244 * multiplying that number by 10^'scale'.
3245 * all without any floating-point arithmetic.
3247 int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3249 unsigned long result = 0;
3250 long decimals = -1;
3251 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3252 if (*cp == '.')
3253 decimals = 0;
3254 else if (decimals < scale) {
3255 unsigned int value;
3256 value = *cp - '0';
3257 result = result * 10 + value;
3258 if (decimals >= 0)
3259 decimals++;
3261 cp++;
3263 if (*cp == '\n')
3264 cp++;
3265 if (*cp)
3266 return -EINVAL;
3267 if (decimals < 0)
3268 decimals = 0;
3269 while (decimals < scale) {
3270 result *= 10;
3271 decimals ++;
3273 *res = result;
3274 return 0;
3277 static void md_safemode_timeout(unsigned long data);
3279 static ssize_t
3280 safe_delay_show(struct mddev *mddev, char *page)
3282 int msec = (mddev->safemode_delay*1000)/HZ;
3283 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3285 static ssize_t
3286 safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
3288 unsigned long msec;
3290 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
3291 return -EINVAL;
3292 if (msec == 0)
3293 mddev->safemode_delay = 0;
3294 else {
3295 unsigned long old_delay = mddev->safemode_delay;
3296 unsigned long new_delay = (msec*HZ)/1000;
3298 if (new_delay == 0)
3299 new_delay = 1;
3300 mddev->safemode_delay = new_delay;
3301 if (new_delay < old_delay || old_delay == 0)
3302 mod_timer(&mddev->safemode_timer, jiffies+1);
3304 return len;
3306 static struct md_sysfs_entry md_safe_delay =
3307 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
3309 static ssize_t
3310 level_show(struct mddev *mddev, char *page)
3312 struct md_personality *p;
3313 int ret;
3314 spin_lock(&mddev->lock);
3315 p = mddev->pers;
3316 if (p)
3317 ret = sprintf(page, "%s\n", p->name);
3318 else if (mddev->clevel[0])
3319 ret = sprintf(page, "%s\n", mddev->clevel);
3320 else if (mddev->level != LEVEL_NONE)
3321 ret = sprintf(page, "%d\n", mddev->level);
3322 else
3323 ret = 0;
3324 spin_unlock(&mddev->lock);
3325 return ret;
3328 static ssize_t
3329 level_store(struct mddev *mddev, const char *buf, size_t len)
3331 char clevel[16];
3332 ssize_t rv;
3333 size_t slen = len;
3334 struct md_personality *pers, *oldpers;
3335 long level;
3336 void *priv, *oldpriv;
3337 struct md_rdev *rdev;
3339 if (slen == 0 || slen >= sizeof(clevel))
3340 return -EINVAL;
3342 rv = mddev_lock(mddev);
3343 if (rv)
3344 return rv;
3346 if (mddev->pers == NULL) {
3347 strncpy(mddev->clevel, buf, slen);
3348 if (mddev->clevel[slen-1] == '\n')
3349 slen--;
3350 mddev->clevel[slen] = 0;
3351 mddev->level = LEVEL_NONE;
3352 rv = len;
3353 goto out_unlock;
3355 rv = -EROFS;
3356 if (mddev->ro)
3357 goto out_unlock;
3359 /* request to change the personality. Need to ensure:
3360 * - array is not engaged in resync/recovery/reshape
3361 * - old personality can be suspended
3362 * - new personality will access other array.
3365 rv = -EBUSY;
3366 if (mddev->sync_thread ||
3367 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3368 mddev->reshape_position != MaxSector ||
3369 mddev->sysfs_active)
3370 goto out_unlock;
3372 rv = -EINVAL;
3373 if (!mddev->pers->quiesce) {
3374 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3375 mdname(mddev), mddev->pers->name);
3376 goto out_unlock;
3379 /* Now find the new personality */
3380 strncpy(clevel, buf, slen);
3381 if (clevel[slen-1] == '\n')
3382 slen--;
3383 clevel[slen] = 0;
3384 if (kstrtol(clevel, 10, &level))
3385 level = LEVEL_NONE;
3387 if (request_module("md-%s", clevel) != 0)
3388 request_module("md-level-%s", clevel);
3389 spin_lock(&pers_lock);
3390 pers = find_pers(level, clevel);
3391 if (!pers || !try_module_get(pers->owner)) {
3392 spin_unlock(&pers_lock);
3393 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
3394 rv = -EINVAL;
3395 goto out_unlock;
3397 spin_unlock(&pers_lock);
3399 if (pers == mddev->pers) {
3400 /* Nothing to do! */
3401 module_put(pers->owner);
3402 rv = len;
3403 goto out_unlock;
3405 if (!pers->takeover) {
3406 module_put(pers->owner);
3407 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
3408 mdname(mddev), clevel);
3409 rv = -EINVAL;
3410 goto out_unlock;
3413 rdev_for_each(rdev, mddev)
3414 rdev->new_raid_disk = rdev->raid_disk;
3416 /* ->takeover must set new_* and/or delta_disks
3417 * if it succeeds, and may set them when it fails.
3419 priv = pers->takeover(mddev);
3420 if (IS_ERR(priv)) {
3421 mddev->new_level = mddev->level;
3422 mddev->new_layout = mddev->layout;
3423 mddev->new_chunk_sectors = mddev->chunk_sectors;
3424 mddev->raid_disks -= mddev->delta_disks;
3425 mddev->delta_disks = 0;
3426 mddev->reshape_backwards = 0;
3427 module_put(pers->owner);
3428 printk(KERN_WARNING "md: %s: %s would not accept array\n",
3429 mdname(mddev), clevel);
3430 rv = PTR_ERR(priv);
3431 goto out_unlock;
3434 /* Looks like we have a winner */
3435 mddev_suspend(mddev);
3436 mddev_detach(mddev);
3438 spin_lock(&mddev->lock);
3439 oldpers = mddev->pers;
3440 oldpriv = mddev->private;
3441 mddev->pers = pers;
3442 mddev->private = priv;
3443 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3444 mddev->level = mddev->new_level;
3445 mddev->layout = mddev->new_layout;
3446 mddev->chunk_sectors = mddev->new_chunk_sectors;
3447 mddev->delta_disks = 0;
3448 mddev->reshape_backwards = 0;
3449 mddev->degraded = 0;
3450 spin_unlock(&mddev->lock);
3452 if (oldpers->sync_request == NULL &&
3453 mddev->external) {
3454 /* We are converting from a no-redundancy array
3455 * to a redundancy array and metadata is managed
3456 * externally so we need to be sure that writes
3457 * won't block due to a need to transition
3458 * clean->dirty
3459 * until external management is started.
3461 mddev->in_sync = 0;
3462 mddev->safemode_delay = 0;
3463 mddev->safemode = 0;
3466 oldpers->free(mddev, oldpriv);
3468 if (oldpers->sync_request == NULL &&
3469 pers->sync_request != NULL) {
3470 /* need to add the md_redundancy_group */
3471 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3472 printk(KERN_WARNING
3473 "md: cannot register extra attributes for %s\n",
3474 mdname(mddev));
3475 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3477 if (oldpers->sync_request != NULL &&
3478 pers->sync_request == NULL) {
3479 /* need to remove the md_redundancy_group */
3480 if (mddev->to_remove == NULL)
3481 mddev->to_remove = &md_redundancy_group;
3484 rdev_for_each(rdev, mddev) {
3485 if (rdev->raid_disk < 0)
3486 continue;
3487 if (rdev->new_raid_disk >= mddev->raid_disks)
3488 rdev->new_raid_disk = -1;
3489 if (rdev->new_raid_disk == rdev->raid_disk)
3490 continue;
3491 sysfs_unlink_rdev(mddev, rdev);
3493 rdev_for_each(rdev, mddev) {
3494 if (rdev->raid_disk < 0)
3495 continue;
3496 if (rdev->new_raid_disk == rdev->raid_disk)
3497 continue;
3498 rdev->raid_disk = rdev->new_raid_disk;
3499 if (rdev->raid_disk < 0)
3500 clear_bit(In_sync, &rdev->flags);
3501 else {
3502 if (sysfs_link_rdev(mddev, rdev))
3503 printk(KERN_WARNING "md: cannot register rd%d"
3504 " for %s after level change\n",
3505 rdev->raid_disk, mdname(mddev));
3509 if (pers->sync_request == NULL) {
3510 /* this is now an array without redundancy, so
3511 * it must always be in_sync
3513 mddev->in_sync = 1;
3514 del_timer_sync(&mddev->safemode_timer);
3516 blk_set_stacking_limits(&mddev->queue->limits);
3517 pers->run(mddev);
3518 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3519 mddev_resume(mddev);
3520 if (!mddev->thread)
3521 md_update_sb(mddev, 1);
3522 sysfs_notify(&mddev->kobj, NULL, "level");
3523 md_new_event(mddev);
3524 rv = len;
3525 out_unlock:
3526 mddev_unlock(mddev);
3527 return rv;
3530 static struct md_sysfs_entry md_level =
3531 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
3533 static ssize_t
3534 layout_show(struct mddev *mddev, char *page)
3536 /* just a number, not meaningful for all levels */
3537 if (mddev->reshape_position != MaxSector &&
3538 mddev->layout != mddev->new_layout)
3539 return sprintf(page, "%d (%d)\n",
3540 mddev->new_layout, mddev->layout);
3541 return sprintf(page, "%d\n", mddev->layout);
3544 static ssize_t
3545 layout_store(struct mddev *mddev, const char *buf, size_t len)
3547 char *e;
3548 unsigned long n = simple_strtoul(buf, &e, 10);
3549 int err;
3551 if (!*buf || (*e && *e != '\n'))
3552 return -EINVAL;
3553 err = mddev_lock(mddev);
3554 if (err)
3555 return err;
3557 if (mddev->pers) {
3558 if (mddev->pers->check_reshape == NULL)
3559 err = -EBUSY;
3560 else if (mddev->ro)
3561 err = -EROFS;
3562 else {
3563 mddev->new_layout = n;
3564 err = mddev->pers->check_reshape(mddev);
3565 if (err)
3566 mddev->new_layout = mddev->layout;
3568 } else {
3569 mddev->new_layout = n;
3570 if (mddev->reshape_position == MaxSector)
3571 mddev->layout = n;
3573 mddev_unlock(mddev);
3574 return err ?: len;
3576 static struct md_sysfs_entry md_layout =
3577 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
3579 static ssize_t
3580 raid_disks_show(struct mddev *mddev, char *page)
3582 if (mddev->raid_disks == 0)
3583 return 0;
3584 if (mddev->reshape_position != MaxSector &&
3585 mddev->delta_disks != 0)
3586 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3587 mddev->raid_disks - mddev->delta_disks);
3588 return sprintf(page, "%d\n", mddev->raid_disks);
3591 static int update_raid_disks(struct mddev *mddev, int raid_disks);
3593 static ssize_t
3594 raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
3596 char *e;
3597 int err;
3598 unsigned long n = simple_strtoul(buf, &e, 10);
3600 if (!*buf || (*e && *e != '\n'))
3601 return -EINVAL;
3603 err = mddev_lock(mddev);
3604 if (err)
3605 return err;
3606 if (mddev->pers)
3607 err = update_raid_disks(mddev, n);
3608 else if (mddev->reshape_position != MaxSector) {
3609 struct md_rdev *rdev;
3610 int olddisks = mddev->raid_disks - mddev->delta_disks;
3612 err = -EINVAL;
3613 rdev_for_each(rdev, mddev) {
3614 if (olddisks < n &&
3615 rdev->data_offset < rdev->new_data_offset)
3616 goto out_unlock;
3617 if (olddisks > n &&
3618 rdev->data_offset > rdev->new_data_offset)
3619 goto out_unlock;
3621 err = 0;
3622 mddev->delta_disks = n - olddisks;
3623 mddev->raid_disks = n;
3624 mddev->reshape_backwards = (mddev->delta_disks < 0);
3625 } else
3626 mddev->raid_disks = n;
3627 out_unlock:
3628 mddev_unlock(mddev);
3629 return err ? err : len;
3631 static struct md_sysfs_entry md_raid_disks =
3632 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
3634 static ssize_t
3635 chunk_size_show(struct mddev *mddev, char *page)
3637 if (mddev->reshape_position != MaxSector &&
3638 mddev->chunk_sectors != mddev->new_chunk_sectors)
3639 return sprintf(page, "%d (%d)\n",
3640 mddev->new_chunk_sectors << 9,
3641 mddev->chunk_sectors << 9);
3642 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
3645 static ssize_t
3646 chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
3648 int err;
3649 char *e;
3650 unsigned long n = simple_strtoul(buf, &e, 10);
3652 if (!*buf || (*e && *e != '\n'))
3653 return -EINVAL;
3655 err = mddev_lock(mddev);
3656 if (err)
3657 return err;
3658 if (mddev->pers) {
3659 if (mddev->pers->check_reshape == NULL)
3660 err = -EBUSY;
3661 else if (mddev->ro)
3662 err = -EROFS;
3663 else {
3664 mddev->new_chunk_sectors = n >> 9;
3665 err = mddev->pers->check_reshape(mddev);
3666 if (err)
3667 mddev->new_chunk_sectors = mddev->chunk_sectors;
3669 } else {
3670 mddev->new_chunk_sectors = n >> 9;
3671 if (mddev->reshape_position == MaxSector)
3672 mddev->chunk_sectors = n >> 9;
3674 mddev_unlock(mddev);
3675 return err ?: len;
3677 static struct md_sysfs_entry md_chunk_size =
3678 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
3680 static ssize_t
3681 resync_start_show(struct mddev *mddev, char *page)
3683 if (mddev->recovery_cp == MaxSector)
3684 return sprintf(page, "none\n");
3685 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3688 static ssize_t
3689 resync_start_store(struct mddev *mddev, const char *buf, size_t len)
3691 int err;
3692 char *e;
3693 unsigned long long n = simple_strtoull(buf, &e, 10);
3695 err = mddev_lock(mddev);
3696 if (err)
3697 return err;
3698 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3699 err = -EBUSY;
3700 else if (cmd_match(buf, "none"))
3701 n = MaxSector;
3702 else if (!*buf || (*e && *e != '\n'))
3703 err = -EINVAL;
3705 if (!err) {
3706 mddev->recovery_cp = n;
3707 if (mddev->pers)
3708 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3710 mddev_unlock(mddev);
3711 return err ?: len;
3713 static struct md_sysfs_entry md_resync_start =
3714 __ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
3715 resync_start_show, resync_start_store);
3718 * The array state can be:
3720 * clear
3721 * No devices, no size, no level
3722 * Equivalent to STOP_ARRAY ioctl
3723 * inactive
3724 * May have some settings, but array is not active
3725 * all IO results in error
3726 * When written, doesn't tear down array, but just stops it
3727 * suspended (not supported yet)
3728 * All IO requests will block. The array can be reconfigured.
3729 * Writing this, if accepted, will block until array is quiescent
3730 * readonly
3731 * no resync can happen. no superblocks get written.
3732 * write requests fail
3733 * read-auto
3734 * like readonly, but behaves like 'clean' on a write request.
3736 * clean - no pending writes, but otherwise active.
3737 * When written to inactive array, starts without resync
3738 * If a write request arrives then
3739 * if metadata is known, mark 'dirty' and switch to 'active'.
3740 * if not known, block and switch to write-pending
3741 * If written to an active array that has pending writes, then fails.
3742 * active
3743 * fully active: IO and resync can be happening.
3744 * When written to inactive array, starts with resync
3746 * write-pending
3747 * clean, but writes are blocked waiting for 'active' to be written.
3749 * active-idle
3750 * like active, but no writes have been seen for a while (100msec).
3753 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3754 write_pending, active_idle, bad_word};
3755 static char *array_states[] = {
3756 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3757 "write-pending", "active-idle", NULL };
3759 static int match_word(const char *word, char **list)
3761 int n;
3762 for (n=0; list[n]; n++)
3763 if (cmd_match(word, list[n]))
3764 break;
3765 return n;
3768 static ssize_t
3769 array_state_show(struct mddev *mddev, char *page)
3771 enum array_state st = inactive;
3773 if (mddev->pers)
3774 switch(mddev->ro) {
3775 case 1:
3776 st = readonly;
3777 break;
3778 case 2:
3779 st = read_auto;
3780 break;
3781 case 0:
3782 if (mddev->in_sync)
3783 st = clean;
3784 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
3785 st = write_pending;
3786 else if (mddev->safemode)
3787 st = active_idle;
3788 else
3789 st = active;
3791 else {
3792 if (list_empty(&mddev->disks) &&
3793 mddev->raid_disks == 0 &&
3794 mddev->dev_sectors == 0)
3795 st = clear;
3796 else
3797 st = inactive;
3799 return sprintf(page, "%s\n", array_states[st]);
3802 static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
3803 static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
3804 static int do_md_run(struct mddev *mddev);
3805 static int restart_array(struct mddev *mddev);
3807 static ssize_t
3808 array_state_store(struct mddev *mddev, const char *buf, size_t len)
3810 int err;
3811 enum array_state st = match_word(buf, array_states);
3813 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
3814 /* don't take reconfig_mutex when toggling between
3815 * clean and active
3817 spin_lock(&mddev->lock);
3818 if (st == active) {
3819 restart_array(mddev);
3820 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
3821 wake_up(&mddev->sb_wait);
3822 err = 0;
3823 } else /* st == clean */ {
3824 restart_array(mddev);
3825 if (atomic_read(&mddev->writes_pending) == 0) {
3826 if (mddev->in_sync == 0) {
3827 mddev->in_sync = 1;
3828 if (mddev->safemode == 1)
3829 mddev->safemode = 0;
3830 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3832 err = 0;
3833 } else
3834 err = -EBUSY;
3836 spin_unlock(&mddev->lock);
3837 return err;
3839 err = mddev_lock(mddev);
3840 if (err)
3841 return err;
3842 err = -EINVAL;
3843 switch(st) {
3844 case bad_word:
3845 break;
3846 case clear:
3847 /* stopping an active array */
3848 err = do_md_stop(mddev, 0, NULL);
3849 break;
3850 case inactive:
3851 /* stopping an active array */
3852 if (mddev->pers)
3853 err = do_md_stop(mddev, 2, NULL);
3854 else
3855 err = 0; /* already inactive */
3856 break;
3857 case suspended:
3858 break; /* not supported yet */
3859 case readonly:
3860 if (mddev->pers)
3861 err = md_set_readonly(mddev, NULL);
3862 else {
3863 mddev->ro = 1;
3864 set_disk_ro(mddev->gendisk, 1);
3865 err = do_md_run(mddev);
3867 break;
3868 case read_auto:
3869 if (mddev->pers) {
3870 if (mddev->ro == 0)
3871 err = md_set_readonly(mddev, NULL);
3872 else if (mddev->ro == 1)
3873 err = restart_array(mddev);
3874 if (err == 0) {
3875 mddev->ro = 2;
3876 set_disk_ro(mddev->gendisk, 0);
3878 } else {
3879 mddev->ro = 2;
3880 err = do_md_run(mddev);
3882 break;
3883 case clean:
3884 if (mddev->pers) {
3885 restart_array(mddev);
3886 spin_lock(&mddev->lock);
3887 if (atomic_read(&mddev->writes_pending) == 0) {
3888 if (mddev->in_sync == 0) {
3889 mddev->in_sync = 1;
3890 if (mddev->safemode == 1)
3891 mddev->safemode = 0;
3892 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3894 err = 0;
3895 } else
3896 err = -EBUSY;
3897 spin_unlock(&mddev->lock);
3898 } else
3899 err = -EINVAL;
3900 break;
3901 case active:
3902 if (mddev->pers) {
3903 restart_array(mddev);
3904 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
3905 wake_up(&mddev->sb_wait);
3906 err = 0;
3907 } else {
3908 mddev->ro = 0;
3909 set_disk_ro(mddev->gendisk, 0);
3910 err = do_md_run(mddev);
3912 break;
3913 case write_pending:
3914 case active_idle:
3915 /* these cannot be set */
3916 break;
3919 if (!err) {
3920 if (mddev->hold_active == UNTIL_IOCTL)
3921 mddev->hold_active = 0;
3922 sysfs_notify_dirent_safe(mddev->sysfs_state);
3924 mddev_unlock(mddev);
3925 return err ?: len;
3927 static struct md_sysfs_entry md_array_state =
3928 __ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
3930 static ssize_t
3931 max_corrected_read_errors_show(struct mddev *mddev, char *page) {
3932 return sprintf(page, "%d\n",
3933 atomic_read(&mddev->max_corr_read_errors));
3936 static ssize_t
3937 max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
3939 char *e;
3940 unsigned long n = simple_strtoul(buf, &e, 10);
3942 if (*buf && (*e == 0 || *e == '\n')) {
3943 atomic_set(&mddev->max_corr_read_errors, n);
3944 return len;
3946 return -EINVAL;
3949 static struct md_sysfs_entry max_corr_read_errors =
3950 __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3951 max_corrected_read_errors_store);
3953 static ssize_t
3954 null_show(struct mddev *mddev, char *page)
3956 return -EINVAL;
3959 static ssize_t
3960 new_dev_store(struct mddev *mddev, const char *buf, size_t len)
3962 /* buf must be %d:%d\n? giving major and minor numbers */
3963 /* The new device is added to the array.
3964 * If the array has a persistent superblock, we read the
3965 * superblock to initialise info and check validity.
3966 * Otherwise, only checking done is that in bind_rdev_to_array,
3967 * which mainly checks size.
3969 char *e;
3970 int major = simple_strtoul(buf, &e, 10);
3971 int minor;
3972 dev_t dev;
3973 struct md_rdev *rdev;
3974 int err;
3976 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3977 return -EINVAL;
3978 minor = simple_strtoul(e+1, &e, 10);
3979 if (*e && *e != '\n')
3980 return -EINVAL;
3981 dev = MKDEV(major, minor);
3982 if (major != MAJOR(dev) ||
3983 minor != MINOR(dev))
3984 return -EOVERFLOW;
3986 flush_workqueue(md_misc_wq);
3988 err = mddev_lock(mddev);
3989 if (err)
3990 return err;
3991 if (mddev->persistent) {
3992 rdev = md_import_device(dev, mddev->major_version,
3993 mddev->minor_version);
3994 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3995 struct md_rdev *rdev0
3996 = list_entry(mddev->disks.next,
3997 struct md_rdev, same_set);
3998 err = super_types[mddev->major_version]
3999 .load_super(rdev, rdev0, mddev->minor_version);
4000 if (err < 0)
4001 goto out;
4003 } else if (mddev->external)
4004 rdev = md_import_device(dev, -2, -1);
4005 else
4006 rdev = md_import_device(dev, -1, -1);
4008 if (IS_ERR(rdev))
4009 return PTR_ERR(rdev);
4010 err = bind_rdev_to_array(rdev, mddev);
4011 out:
4012 if (err)
4013 export_rdev(rdev);
4014 mddev_unlock(mddev);
4015 return err ? err : len;
4018 static struct md_sysfs_entry md_new_device =
4019 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
4021 static ssize_t
4022 bitmap_store(struct mddev *mddev, const char *buf, size_t len)
4024 char *end;
4025 unsigned long chunk, end_chunk;
4026 int err;
4028 err = mddev_lock(mddev);
4029 if (err)
4030 return err;
4031 if (!mddev->bitmap)
4032 goto out;
4033 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4034 while (*buf) {
4035 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4036 if (buf == end) break;
4037 if (*end == '-') { /* range */
4038 buf = end + 1;
4039 end_chunk = simple_strtoul(buf, &end, 0);
4040 if (buf == end) break;
4042 if (*end && !isspace(*end)) break;
4043 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
4044 buf = skip_spaces(end);
4046 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4047 out:
4048 mddev_unlock(mddev);
4049 return len;
4052 static struct md_sysfs_entry md_bitmap =
4053 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4055 static ssize_t
4056 size_show(struct mddev *mddev, char *page)
4058 return sprintf(page, "%llu\n",
4059 (unsigned long long)mddev->dev_sectors / 2);
4062 static int update_size(struct mddev *mddev, sector_t num_sectors);
4064 static ssize_t
4065 size_store(struct mddev *mddev, const char *buf, size_t len)
4067 /* If array is inactive, we can reduce the component size, but
4068 * not increase it (except from 0).
4069 * If array is active, we can try an on-line resize
4071 sector_t sectors;
4072 int err = strict_blocks_to_sectors(buf, &sectors);
4074 if (err < 0)
4075 return err;
4076 err = mddev_lock(mddev);
4077 if (err)
4078 return err;
4079 if (mddev->pers) {
4080 if (mddev_is_clustered(mddev))
4081 md_cluster_ops->metadata_update_start(mddev);
4082 err = update_size(mddev, sectors);
4083 md_update_sb(mddev, 1);
4084 if (mddev_is_clustered(mddev))
4085 md_cluster_ops->metadata_update_finish(mddev);
4086 } else {
4087 if (mddev->dev_sectors == 0 ||
4088 mddev->dev_sectors > sectors)
4089 mddev->dev_sectors = sectors;
4090 else
4091 err = -ENOSPC;
4093 mddev_unlock(mddev);
4094 return err ? err : len;
4097 static struct md_sysfs_entry md_size =
4098 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
4100 /* Metadata version.
4101 * This is one of
4102 * 'none' for arrays with no metadata (good luck...)
4103 * 'external' for arrays with externally managed metadata,
4104 * or N.M for internally known formats
4106 static ssize_t
4107 metadata_show(struct mddev *mddev, char *page)
4109 if (mddev->persistent)
4110 return sprintf(page, "%d.%d\n",
4111 mddev->major_version, mddev->minor_version);
4112 else if (mddev->external)
4113 return sprintf(page, "external:%s\n", mddev->metadata_type);
4114 else
4115 return sprintf(page, "none\n");
4118 static ssize_t
4119 metadata_store(struct mddev *mddev, const char *buf, size_t len)
4121 int major, minor;
4122 char *e;
4123 int err;
4124 /* Changing the details of 'external' metadata is
4125 * always permitted. Otherwise there must be
4126 * no devices attached to the array.
4129 err = mddev_lock(mddev);
4130 if (err)
4131 return err;
4132 err = -EBUSY;
4133 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4135 else if (!list_empty(&mddev->disks))
4136 goto out_unlock;
4138 err = 0;
4139 if (cmd_match(buf, "none")) {
4140 mddev->persistent = 0;
4141 mddev->external = 0;
4142 mddev->major_version = 0;
4143 mddev->minor_version = 90;
4144 goto out_unlock;
4146 if (strncmp(buf, "external:", 9) == 0) {
4147 size_t namelen = len-9;
4148 if (namelen >= sizeof(mddev->metadata_type))
4149 namelen = sizeof(mddev->metadata_type)-1;
4150 strncpy(mddev->metadata_type, buf+9, namelen);
4151 mddev->metadata_type[namelen] = 0;
4152 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4153 mddev->metadata_type[--namelen] = 0;
4154 mddev->persistent = 0;
4155 mddev->external = 1;
4156 mddev->major_version = 0;
4157 mddev->minor_version = 90;
4158 goto out_unlock;
4160 major = simple_strtoul(buf, &e, 10);
4161 err = -EINVAL;
4162 if (e==buf || *e != '.')
4163 goto out_unlock;
4164 buf = e+1;
4165 minor = simple_strtoul(buf, &e, 10);
4166 if (e==buf || (*e && *e != '\n') )
4167 goto out_unlock;
4168 err = -ENOENT;
4169 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
4170 goto out_unlock;
4171 mddev->major_version = major;
4172 mddev->minor_version = minor;
4173 mddev->persistent = 1;
4174 mddev->external = 0;
4175 err = 0;
4176 out_unlock:
4177 mddev_unlock(mddev);
4178 return err ?: len;
4181 static struct md_sysfs_entry md_metadata =
4182 __ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
4184 static ssize_t
4185 action_show(struct mddev *mddev, char *page)
4187 char *type = "idle";
4188 unsigned long recovery = mddev->recovery;
4189 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
4190 type = "frozen";
4191 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4192 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4193 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
4194 type = "reshape";
4195 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4196 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
4197 type = "resync";
4198 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
4199 type = "check";
4200 else
4201 type = "repair";
4202 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
4203 type = "recover";
4205 return sprintf(page, "%s\n", type);
4208 static ssize_t
4209 action_store(struct mddev *mddev, const char *page, size_t len)
4211 if (!mddev->pers || !mddev->pers->sync_request)
4212 return -EINVAL;
4215 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
4216 if (cmd_match(page, "frozen"))
4217 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4218 else
4219 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4220 flush_workqueue(md_misc_wq);
4221 if (mddev->sync_thread) {
4222 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4223 if (mddev_lock(mddev) == 0) {
4224 md_reap_sync_thread(mddev);
4225 mddev_unlock(mddev);
4228 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4229 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
4230 return -EBUSY;
4231 else if (cmd_match(page, "resync"))
4232 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4233 else if (cmd_match(page, "recover")) {
4234 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4235 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4236 } else if (cmd_match(page, "reshape")) {
4237 int err;
4238 if (mddev->pers->start_reshape == NULL)
4239 return -EINVAL;
4240 err = mddev_lock(mddev);
4241 if (!err) {
4242 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4243 err = mddev->pers->start_reshape(mddev);
4244 mddev_unlock(mddev);
4246 if (err)
4247 return err;
4248 sysfs_notify(&mddev->kobj, NULL, "degraded");
4249 } else {
4250 if (cmd_match(page, "check"))
4251 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4252 else if (!cmd_match(page, "repair"))
4253 return -EINVAL;
4254 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4255 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4256 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4258 if (mddev->ro == 2) {
4259 /* A write to sync_action is enough to justify
4260 * canceling read-auto mode
4262 mddev->ro = 0;
4263 md_wakeup_thread(mddev->sync_thread);
4265 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4266 md_wakeup_thread(mddev->thread);
4267 sysfs_notify_dirent_safe(mddev->sysfs_action);
4268 return len;
4271 static struct md_sysfs_entry md_scan_mode =
4272 __ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
4274 static ssize_t
4275 last_sync_action_show(struct mddev *mddev, char *page)
4277 return sprintf(page, "%s\n", mddev->last_sync_action);
4280 static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4282 static ssize_t
4283 mismatch_cnt_show(struct mddev *mddev, char *page)
4285 return sprintf(page, "%llu\n",
4286 (unsigned long long)
4287 atomic64_read(&mddev->resync_mismatches));
4290 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
4292 static ssize_t
4293 sync_min_show(struct mddev *mddev, char *page)
4295 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4296 mddev->sync_speed_min ? "local": "system");
4299 static ssize_t
4300 sync_min_store(struct mddev *mddev, const char *buf, size_t len)
4302 int min;
4303 char *e;
4304 if (strncmp(buf, "system", 6)==0) {
4305 mddev->sync_speed_min = 0;
4306 return len;
4308 min = simple_strtoul(buf, &e, 10);
4309 if (buf == e || (*e && *e != '\n') || min <= 0)
4310 return -EINVAL;
4311 mddev->sync_speed_min = min;
4312 return len;
4315 static struct md_sysfs_entry md_sync_min =
4316 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4318 static ssize_t
4319 sync_max_show(struct mddev *mddev, char *page)
4321 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4322 mddev->sync_speed_max ? "local": "system");
4325 static ssize_t
4326 sync_max_store(struct mddev *mddev, const char *buf, size_t len)
4328 int max;
4329 char *e;
4330 if (strncmp(buf, "system", 6)==0) {
4331 mddev->sync_speed_max = 0;
4332 return len;
4334 max = simple_strtoul(buf, &e, 10);
4335 if (buf == e || (*e && *e != '\n') || max <= 0)
4336 return -EINVAL;
4337 mddev->sync_speed_max = max;
4338 return len;
4341 static struct md_sysfs_entry md_sync_max =
4342 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4344 static ssize_t
4345 degraded_show(struct mddev *mddev, char *page)
4347 return sprintf(page, "%d\n", mddev->degraded);
4349 static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
4351 static ssize_t
4352 sync_force_parallel_show(struct mddev *mddev, char *page)
4354 return sprintf(page, "%d\n", mddev->parallel_resync);
4357 static ssize_t
4358 sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
4360 long n;
4362 if (kstrtol(buf, 10, &n))
4363 return -EINVAL;
4365 if (n != 0 && n != 1)
4366 return -EINVAL;
4368 mddev->parallel_resync = n;
4370 if (mddev->sync_thread)
4371 wake_up(&resync_wait);
4373 return len;
4376 /* force parallel resync, even with shared block devices */
4377 static struct md_sysfs_entry md_sync_force_parallel =
4378 __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4379 sync_force_parallel_show, sync_force_parallel_store);
4381 static ssize_t
4382 sync_speed_show(struct mddev *mddev, char *page)
4384 unsigned long resync, dt, db;
4385 if (mddev->curr_resync == 0)
4386 return sprintf(page, "none\n");
4387 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4388 dt = (jiffies - mddev->resync_mark) / HZ;
4389 if (!dt) dt++;
4390 db = resync - mddev->resync_mark_cnt;
4391 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
4394 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
4396 static ssize_t
4397 sync_completed_show(struct mddev *mddev, char *page)
4399 unsigned long long max_sectors, resync;
4401 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4402 return sprintf(page, "none\n");
4404 if (mddev->curr_resync == 1 ||
4405 mddev->curr_resync == 2)
4406 return sprintf(page, "delayed\n");
4408 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4409 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4410 max_sectors = mddev->resync_max_sectors;
4411 else
4412 max_sectors = mddev->dev_sectors;
4414 resync = mddev->curr_resync_completed;
4415 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
4418 static struct md_sysfs_entry md_sync_completed =
4419 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
4421 static ssize_t
4422 min_sync_show(struct mddev *mddev, char *page)
4424 return sprintf(page, "%llu\n",
4425 (unsigned long long)mddev->resync_min);
4427 static ssize_t
4428 min_sync_store(struct mddev *mddev, const char *buf, size_t len)
4430 unsigned long long min;
4431 int err;
4433 if (kstrtoull(buf, 10, &min))
4434 return -EINVAL;
4436 spin_lock(&mddev->lock);
4437 err = -EINVAL;
4438 if (min > mddev->resync_max)
4439 goto out_unlock;
4441 err = -EBUSY;
4442 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4443 goto out_unlock;
4445 /* Round down to multiple of 4K for safety */
4446 mddev->resync_min = round_down(min, 8);
4447 err = 0;
4449 out_unlock:
4450 spin_unlock(&mddev->lock);
4451 return err ?: len;
4454 static struct md_sysfs_entry md_min_sync =
4455 __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4457 static ssize_t
4458 max_sync_show(struct mddev *mddev, char *page)
4460 if (mddev->resync_max == MaxSector)
4461 return sprintf(page, "max\n");
4462 else
4463 return sprintf(page, "%llu\n",
4464 (unsigned long long)mddev->resync_max);
4466 static ssize_t
4467 max_sync_store(struct mddev *mddev, const char *buf, size_t len)
4469 int err;
4470 spin_lock(&mddev->lock);
4471 if (strncmp(buf, "max", 3) == 0)
4472 mddev->resync_max = MaxSector;
4473 else {
4474 unsigned long long max;
4475 int chunk;
4477 err = -EINVAL;
4478 if (kstrtoull(buf, 10, &max))
4479 goto out_unlock;
4480 if (max < mddev->resync_min)
4481 goto out_unlock;
4483 err = -EBUSY;
4484 if (max < mddev->resync_max &&
4485 mddev->ro == 0 &&
4486 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4487 goto out_unlock;
4489 /* Must be a multiple of chunk_size */
4490 chunk = mddev->chunk_sectors;
4491 if (chunk) {
4492 sector_t temp = max;
4494 err = -EINVAL;
4495 if (sector_div(temp, chunk))
4496 goto out_unlock;
4498 mddev->resync_max = max;
4500 wake_up(&mddev->recovery_wait);
4501 err = 0;
4502 out_unlock:
4503 spin_unlock(&mddev->lock);
4504 return err ?: len;
4507 static struct md_sysfs_entry md_max_sync =
4508 __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4510 static ssize_t
4511 suspend_lo_show(struct mddev *mddev, char *page)
4513 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4516 static ssize_t
4517 suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
4519 char *e;
4520 unsigned long long new = simple_strtoull(buf, &e, 10);
4521 unsigned long long old;
4522 int err;
4524 if (buf == e || (*e && *e != '\n'))
4525 return -EINVAL;
4527 err = mddev_lock(mddev);
4528 if (err)
4529 return err;
4530 err = -EINVAL;
4531 if (mddev->pers == NULL ||
4532 mddev->pers->quiesce == NULL)
4533 goto unlock;
4534 old = mddev->suspend_lo;
4535 mddev->suspend_lo = new;
4536 if (new >= old)
4537 /* Shrinking suspended region */
4538 mddev->pers->quiesce(mddev, 2);
4539 else {
4540 /* Expanding suspended region - need to wait */
4541 mddev->pers->quiesce(mddev, 1);
4542 mddev->pers->quiesce(mddev, 0);
4544 err = 0;
4545 unlock:
4546 mddev_unlock(mddev);
4547 return err ?: len;
4549 static struct md_sysfs_entry md_suspend_lo =
4550 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4552 static ssize_t
4553 suspend_hi_show(struct mddev *mddev, char *page)
4555 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4558 static ssize_t
4559 suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
4561 char *e;
4562 unsigned long long new = simple_strtoull(buf, &e, 10);
4563 unsigned long long old;
4564 int err;
4566 if (buf == e || (*e && *e != '\n'))
4567 return -EINVAL;
4569 err = mddev_lock(mddev);
4570 if (err)
4571 return err;
4572 err = -EINVAL;
4573 if (mddev->pers == NULL ||
4574 mddev->pers->quiesce == NULL)
4575 goto unlock;
4576 old = mddev->suspend_hi;
4577 mddev->suspend_hi = new;
4578 if (new <= old)
4579 /* Shrinking suspended region */
4580 mddev->pers->quiesce(mddev, 2);
4581 else {
4582 /* Expanding suspended region - need to wait */
4583 mddev->pers->quiesce(mddev, 1);
4584 mddev->pers->quiesce(mddev, 0);
4586 err = 0;
4587 unlock:
4588 mddev_unlock(mddev);
4589 return err ?: len;
4591 static struct md_sysfs_entry md_suspend_hi =
4592 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4594 static ssize_t
4595 reshape_position_show(struct mddev *mddev, char *page)
4597 if (mddev->reshape_position != MaxSector)
4598 return sprintf(page, "%llu\n",
4599 (unsigned long long)mddev->reshape_position);
4600 strcpy(page, "none\n");
4601 return 5;
4604 static ssize_t
4605 reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
4607 struct md_rdev *rdev;
4608 char *e;
4609 int err;
4610 unsigned long long new = simple_strtoull(buf, &e, 10);
4612 if (buf == e || (*e && *e != '\n'))
4613 return -EINVAL;
4614 err = mddev_lock(mddev);
4615 if (err)
4616 return err;
4617 err = -EBUSY;
4618 if (mddev->pers)
4619 goto unlock;
4620 mddev->reshape_position = new;
4621 mddev->delta_disks = 0;
4622 mddev->reshape_backwards = 0;
4623 mddev->new_level = mddev->level;
4624 mddev->new_layout = mddev->layout;
4625 mddev->new_chunk_sectors = mddev->chunk_sectors;
4626 rdev_for_each(rdev, mddev)
4627 rdev->new_data_offset = rdev->data_offset;
4628 err = 0;
4629 unlock:
4630 mddev_unlock(mddev);
4631 return err ?: len;
4634 static struct md_sysfs_entry md_reshape_position =
4635 __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4636 reshape_position_store);
4638 static ssize_t
4639 reshape_direction_show(struct mddev *mddev, char *page)
4641 return sprintf(page, "%s\n",
4642 mddev->reshape_backwards ? "backwards" : "forwards");
4645 static ssize_t
4646 reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4648 int backwards = 0;
4649 int err;
4651 if (cmd_match(buf, "forwards"))
4652 backwards = 0;
4653 else if (cmd_match(buf, "backwards"))
4654 backwards = 1;
4655 else
4656 return -EINVAL;
4657 if (mddev->reshape_backwards == backwards)
4658 return len;
4660 err = mddev_lock(mddev);
4661 if (err)
4662 return err;
4663 /* check if we are allowed to change */
4664 if (mddev->delta_disks)
4665 err = -EBUSY;
4666 else if (mddev->persistent &&
4667 mddev->major_version == 0)
4668 err = -EINVAL;
4669 else
4670 mddev->reshape_backwards = backwards;
4671 mddev_unlock(mddev);
4672 return err ?: len;
4675 static struct md_sysfs_entry md_reshape_direction =
4676 __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4677 reshape_direction_store);
4679 static ssize_t
4680 array_size_show(struct mddev *mddev, char *page)
4682 if (mddev->external_size)
4683 return sprintf(page, "%llu\n",
4684 (unsigned long long)mddev->array_sectors/2);
4685 else
4686 return sprintf(page, "default\n");
4689 static ssize_t
4690 array_size_store(struct mddev *mddev, const char *buf, size_t len)
4692 sector_t sectors;
4693 int err;
4695 err = mddev_lock(mddev);
4696 if (err)
4697 return err;
4699 if (strncmp(buf, "default", 7) == 0) {
4700 if (mddev->pers)
4701 sectors = mddev->pers->size(mddev, 0, 0);
4702 else
4703 sectors = mddev->array_sectors;
4705 mddev->external_size = 0;
4706 } else {
4707 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4708 err = -EINVAL;
4709 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4710 err = -E2BIG;
4711 else
4712 mddev->external_size = 1;
4715 if (!err) {
4716 mddev->array_sectors = sectors;
4717 if (mddev->pers) {
4718 set_capacity(mddev->gendisk, mddev->array_sectors);
4719 revalidate_disk(mddev->gendisk);
4722 mddev_unlock(mddev);
4723 return err ?: len;
4726 static struct md_sysfs_entry md_array_size =
4727 __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4728 array_size_store);
4730 static struct attribute *md_default_attrs[] = {
4731 &md_level.attr,
4732 &md_layout.attr,
4733 &md_raid_disks.attr,
4734 &md_chunk_size.attr,
4735 &md_size.attr,
4736 &md_resync_start.attr,
4737 &md_metadata.attr,
4738 &md_new_device.attr,
4739 &md_safe_delay.attr,
4740 &md_array_state.attr,
4741 &md_reshape_position.attr,
4742 &md_reshape_direction.attr,
4743 &md_array_size.attr,
4744 &max_corr_read_errors.attr,
4745 NULL,
4748 static struct attribute *md_redundancy_attrs[] = {
4749 &md_scan_mode.attr,
4750 &md_last_scan_mode.attr,
4751 &md_mismatches.attr,
4752 &md_sync_min.attr,
4753 &md_sync_max.attr,
4754 &md_sync_speed.attr,
4755 &md_sync_force_parallel.attr,
4756 &md_sync_completed.attr,
4757 &md_min_sync.attr,
4758 &md_max_sync.attr,
4759 &md_suspend_lo.attr,
4760 &md_suspend_hi.attr,
4761 &md_bitmap.attr,
4762 &md_degraded.attr,
4763 NULL,
4765 static struct attribute_group md_redundancy_group = {
4766 .name = NULL,
4767 .attrs = md_redundancy_attrs,
4770 static ssize_t
4771 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4773 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4774 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
4775 ssize_t rv;
4777 if (!entry->show)
4778 return -EIO;
4779 spin_lock(&all_mddevs_lock);
4780 if (list_empty(&mddev->all_mddevs)) {
4781 spin_unlock(&all_mddevs_lock);
4782 return -EBUSY;
4784 mddev_get(mddev);
4785 spin_unlock(&all_mddevs_lock);
4787 rv = entry->show(mddev, page);
4788 mddev_put(mddev);
4789 return rv;
4792 static ssize_t
4793 md_attr_store(struct kobject *kobj, struct attribute *attr,
4794 const char *page, size_t length)
4796 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4797 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
4798 ssize_t rv;
4800 if (!entry->store)
4801 return -EIO;
4802 if (!capable(CAP_SYS_ADMIN))
4803 return -EACCES;
4804 spin_lock(&all_mddevs_lock);
4805 if (list_empty(&mddev->all_mddevs)) {
4806 spin_unlock(&all_mddevs_lock);
4807 return -EBUSY;
4809 mddev_get(mddev);
4810 spin_unlock(&all_mddevs_lock);
4811 rv = entry->store(mddev, page, length);
4812 mddev_put(mddev);
4813 return rv;
4816 static void md_free(struct kobject *ko)
4818 struct mddev *mddev = container_of(ko, struct mddev, kobj);
4820 if (mddev->sysfs_state)
4821 sysfs_put(mddev->sysfs_state);
4823 if (mddev->queue)
4824 blk_cleanup_queue(mddev->queue);
4825 if (mddev->gendisk) {
4826 del_gendisk(mddev->gendisk);
4827 put_disk(mddev->gendisk);
4830 kfree(mddev);
4833 static const struct sysfs_ops md_sysfs_ops = {
4834 .show = md_attr_show,
4835 .store = md_attr_store,
4837 static struct kobj_type md_ktype = {
4838 .release = md_free,
4839 .sysfs_ops = &md_sysfs_ops,
4840 .default_attrs = md_default_attrs,
4843 int mdp_major = 0;
4845 static void mddev_delayed_delete(struct work_struct *ws)
4847 struct mddev *mddev = container_of(ws, struct mddev, del_work);
4849 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
4850 kobject_del(&mddev->kobj);
4851 kobject_put(&mddev->kobj);
4854 static int md_alloc(dev_t dev, char *name)
4856 static DEFINE_MUTEX(disks_mutex);
4857 struct mddev *mddev = mddev_find(dev);
4858 struct gendisk *disk;
4859 int partitioned;
4860 int shift;
4861 int unit;
4862 int error;
4864 if (!mddev)
4865 return -ENODEV;
4867 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4868 shift = partitioned ? MdpMinorShift : 0;
4869 unit = MINOR(mddev->unit) >> shift;
4871 /* wait for any previous instance of this device to be
4872 * completely removed (mddev_delayed_delete).
4874 flush_workqueue(md_misc_wq);
4876 mutex_lock(&disks_mutex);
4877 error = -EEXIST;
4878 if (mddev->gendisk)
4879 goto abort;
4881 if (name) {
4882 /* Need to ensure that 'name' is not a duplicate.
4884 struct mddev *mddev2;
4885 spin_lock(&all_mddevs_lock);
4887 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4888 if (mddev2->gendisk &&
4889 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4890 spin_unlock(&all_mddevs_lock);
4891 goto abort;
4893 spin_unlock(&all_mddevs_lock);
4896 error = -ENOMEM;
4897 mddev->queue = blk_alloc_queue(GFP_KERNEL);
4898 if (!mddev->queue)
4899 goto abort;
4900 mddev->queue->queuedata = mddev;
4902 blk_queue_make_request(mddev->queue, md_make_request);
4903 blk_set_stacking_limits(&mddev->queue->limits);
4905 disk = alloc_disk(1 << shift);
4906 if (!disk) {
4907 blk_cleanup_queue(mddev->queue);
4908 mddev->queue = NULL;
4909 goto abort;
4911 disk->major = MAJOR(mddev->unit);
4912 disk->first_minor = unit << shift;
4913 if (name)
4914 strcpy(disk->disk_name, name);
4915 else if (partitioned)
4916 sprintf(disk->disk_name, "md_d%d", unit);
4917 else
4918 sprintf(disk->disk_name, "md%d", unit);
4919 disk->fops = &md_fops;
4920 disk->private_data = mddev;
4921 disk->queue = mddev->queue;
4922 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
4923 /* Allow extended partitions. This makes the
4924 * 'mdp' device redundant, but we can't really
4925 * remove it now.
4927 disk->flags |= GENHD_FL_EXT_DEVT;
4928 mddev->gendisk = disk;
4929 /* As soon as we call add_disk(), another thread could get
4930 * through to md_open, so make sure it doesn't get too far
4932 mutex_lock(&mddev->open_mutex);
4933 add_disk(disk);
4935 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4936 &disk_to_dev(disk)->kobj, "%s", "md");
4937 if (error) {
4938 /* This isn't possible, but as kobject_init_and_add is marked
4939 * __must_check, we must do something with the result
4941 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4942 disk->disk_name);
4943 error = 0;
4945 if (mddev->kobj.sd &&
4946 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
4947 printk(KERN_DEBUG "pointless warning\n");
4948 mutex_unlock(&mddev->open_mutex);
4949 abort:
4950 mutex_unlock(&disks_mutex);
4951 if (!error && mddev->kobj.sd) {
4952 kobject_uevent(&mddev->kobj, KOBJ_ADD);
4953 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
4955 mddev_put(mddev);
4956 return error;
4959 static struct kobject *md_probe(dev_t dev, int *part, void *data)
4961 md_alloc(dev, NULL);
4962 return NULL;
4965 static int add_named_array(const char *val, struct kernel_param *kp)
4967 /* val must be "md_*" where * is not all digits.
4968 * We allocate an array with a large free minor number, and
4969 * set the name to val. val must not already be an active name.
4971 int len = strlen(val);
4972 char buf[DISK_NAME_LEN];
4974 while (len && val[len-1] == '\n')
4975 len--;
4976 if (len >= DISK_NAME_LEN)
4977 return -E2BIG;
4978 strlcpy(buf, val, len+1);
4979 if (strncmp(buf, "md_", 3) != 0)
4980 return -EINVAL;
4981 return md_alloc(0, buf);
4984 static void md_safemode_timeout(unsigned long data)
4986 struct mddev *mddev = (struct mddev *) data;
4988 if (!atomic_read(&mddev->writes_pending)) {
4989 mddev->safemode = 1;
4990 if (mddev->external)
4991 sysfs_notify_dirent_safe(mddev->sysfs_state);
4993 md_wakeup_thread(mddev->thread);
4996 static int start_dirty_degraded;
4998 int md_run(struct mddev *mddev)
5000 int err;
5001 struct md_rdev *rdev;
5002 struct md_personality *pers;
5004 if (list_empty(&mddev->disks))
5005 /* cannot run an array with no devices.. */
5006 return -EINVAL;
5008 if (mddev->pers)
5009 return -EBUSY;
5010 /* Cannot run until previous stop completes properly */
5011 if (mddev->sysfs_active)
5012 return -EBUSY;
5015 * Analyze all RAID superblock(s)
5017 if (!mddev->raid_disks) {
5018 if (!mddev->persistent)
5019 return -EINVAL;
5020 analyze_sbs(mddev);
5023 if (mddev->level != LEVEL_NONE)
5024 request_module("md-level-%d", mddev->level);
5025 else if (mddev->clevel[0])
5026 request_module("md-%s", mddev->clevel);
5029 * Drop all container device buffers, from now on
5030 * the only valid external interface is through the md
5031 * device.
5033 rdev_for_each(rdev, mddev) {
5034 if (test_bit(Faulty, &rdev->flags))
5035 continue;
5036 sync_blockdev(rdev->bdev);
5037 invalidate_bdev(rdev->bdev);
5039 /* perform some consistency tests on the device.
5040 * We don't want the data to overlap the metadata,
5041 * Internal Bitmap issues have been handled elsewhere.
5043 if (rdev->meta_bdev) {
5044 /* Nothing to check */;
5045 } else if (rdev->data_offset < rdev->sb_start) {
5046 if (mddev->dev_sectors &&
5047 rdev->data_offset + mddev->dev_sectors
5048 > rdev->sb_start) {
5049 printk("md: %s: data overlaps metadata\n",
5050 mdname(mddev));
5051 return -EINVAL;
5053 } else {
5054 if (rdev->sb_start + rdev->sb_size/512
5055 > rdev->data_offset) {
5056 printk("md: %s: metadata overlaps data\n",
5057 mdname(mddev));
5058 return -EINVAL;
5061 sysfs_notify_dirent_safe(rdev->sysfs_state);
5064 if (mddev->bio_set == NULL)
5065 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
5067 spin_lock(&pers_lock);
5068 pers = find_pers(mddev->level, mddev->clevel);
5069 if (!pers || !try_module_get(pers->owner)) {
5070 spin_unlock(&pers_lock);
5071 if (mddev->level != LEVEL_NONE)
5072 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5073 mddev->level);
5074 else
5075 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5076 mddev->clevel);
5077 return -EINVAL;
5079 spin_unlock(&pers_lock);
5080 if (mddev->level != pers->level) {
5081 mddev->level = pers->level;
5082 mddev->new_level = pers->level;
5084 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
5086 if (mddev->reshape_position != MaxSector &&
5087 pers->start_reshape == NULL) {
5088 /* This personality cannot handle reshaping... */
5089 module_put(pers->owner);
5090 return -EINVAL;
5093 if (pers->sync_request) {
5094 /* Warn if this is a potentially silly
5095 * configuration.
5097 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5098 struct md_rdev *rdev2;
5099 int warned = 0;
5101 rdev_for_each(rdev, mddev)
5102 rdev_for_each(rdev2, mddev) {
5103 if (rdev < rdev2 &&
5104 rdev->bdev->bd_contains ==
5105 rdev2->bdev->bd_contains) {
5106 printk(KERN_WARNING
5107 "%s: WARNING: %s appears to be"
5108 " on the same physical disk as"
5109 " %s.\n",
5110 mdname(mddev),
5111 bdevname(rdev->bdev,b),
5112 bdevname(rdev2->bdev,b2));
5113 warned = 1;
5117 if (warned)
5118 printk(KERN_WARNING
5119 "True protection against single-disk"
5120 " failure might be compromised.\n");
5123 mddev->recovery = 0;
5124 /* may be over-ridden by personality */
5125 mddev->resync_max_sectors = mddev->dev_sectors;
5127 mddev->ok_start_degraded = start_dirty_degraded;
5129 if (start_readonly && mddev->ro == 0)
5130 mddev->ro = 2; /* read-only, but switch on first write */
5132 err = pers->run(mddev);
5133 if (err)
5134 printk(KERN_ERR "md: pers->run() failed ...\n");
5135 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
5136 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5137 " but 'external_size' not in effect?\n", __func__);
5138 printk(KERN_ERR
5139 "md: invalid array_size %llu > default size %llu\n",
5140 (unsigned long long)mddev->array_sectors / 2,
5141 (unsigned long long)pers->size(mddev, 0, 0) / 2);
5142 err = -EINVAL;
5144 if (err == 0 && pers->sync_request &&
5145 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
5146 struct bitmap *bitmap;
5148 bitmap = bitmap_create(mddev, -1);
5149 if (IS_ERR(bitmap)) {
5150 err = PTR_ERR(bitmap);
5151 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5152 mdname(mddev), err);
5153 } else
5154 mddev->bitmap = bitmap;
5157 if (err) {
5158 mddev_detach(mddev);
5159 if (mddev->private)
5160 pers->free(mddev, mddev->private);
5161 module_put(pers->owner);
5162 bitmap_destroy(mddev);
5163 return err;
5165 if (mddev->queue) {
5166 mddev->queue->backing_dev_info.congested_data = mddev;
5167 mddev->queue->backing_dev_info.congested_fn = md_congested;
5168 blk_queue_merge_bvec(mddev->queue, md_mergeable_bvec);
5170 if (pers->sync_request) {
5171 if (mddev->kobj.sd &&
5172 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
5173 printk(KERN_WARNING
5174 "md: cannot register extra attributes for %s\n",
5175 mdname(mddev));
5176 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
5177 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
5178 mddev->ro = 0;
5180 atomic_set(&mddev->writes_pending,0);
5181 atomic_set(&mddev->max_corr_read_errors,
5182 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
5183 mddev->safemode = 0;
5184 mddev->safemode_timer.function = md_safemode_timeout;
5185 mddev->safemode_timer.data = (unsigned long) mddev;
5186 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
5187 mddev->in_sync = 1;
5188 smp_wmb();
5189 spin_lock(&mddev->lock);
5190 mddev->pers = pers;
5191 mddev->ready = 1;
5192 spin_unlock(&mddev->lock);
5193 rdev_for_each(rdev, mddev)
5194 if (rdev->raid_disk >= 0)
5195 if (sysfs_link_rdev(mddev, rdev))
5196 /* failure here is OK */;
5198 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5200 if (mddev->flags & MD_UPDATE_SB_FLAGS)
5201 md_update_sb(mddev, 0);
5203 md_new_event(mddev);
5204 sysfs_notify_dirent_safe(mddev->sysfs_state);
5205 sysfs_notify_dirent_safe(mddev->sysfs_action);
5206 sysfs_notify(&mddev->kobj, NULL, "degraded");
5207 return 0;
5209 EXPORT_SYMBOL_GPL(md_run);
5211 static int do_md_run(struct mddev *mddev)
5213 int err;
5215 err = md_run(mddev);
5216 if (err)
5217 goto out;
5218 err = bitmap_load(mddev);
5219 if (err) {
5220 bitmap_destroy(mddev);
5221 goto out;
5224 md_wakeup_thread(mddev->thread);
5225 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5227 set_capacity(mddev->gendisk, mddev->array_sectors);
5228 revalidate_disk(mddev->gendisk);
5229 mddev->changed = 1;
5230 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5231 out:
5232 return err;
5235 static int restart_array(struct mddev *mddev)
5237 struct gendisk *disk = mddev->gendisk;
5239 /* Complain if it has no devices */
5240 if (list_empty(&mddev->disks))
5241 return -ENXIO;
5242 if (!mddev->pers)
5243 return -EINVAL;
5244 if (!mddev->ro)
5245 return -EBUSY;
5246 mddev->safemode = 0;
5247 mddev->ro = 0;
5248 set_disk_ro(disk, 0);
5249 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5250 mdname(mddev));
5251 /* Kick recovery or resync if necessary */
5252 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5253 md_wakeup_thread(mddev->thread);
5254 md_wakeup_thread(mddev->sync_thread);
5255 sysfs_notify_dirent_safe(mddev->sysfs_state);
5256 return 0;
5259 static void md_clean(struct mddev *mddev)
5261 mddev->array_sectors = 0;
5262 mddev->external_size = 0;
5263 mddev->dev_sectors = 0;
5264 mddev->raid_disks = 0;
5265 mddev->recovery_cp = 0;
5266 mddev->resync_min = 0;
5267 mddev->resync_max = MaxSector;
5268 mddev->reshape_position = MaxSector;
5269 mddev->external = 0;
5270 mddev->persistent = 0;
5271 mddev->level = LEVEL_NONE;
5272 mddev->clevel[0] = 0;
5273 mddev->flags = 0;
5274 mddev->ro = 0;
5275 mddev->metadata_type[0] = 0;
5276 mddev->chunk_sectors = 0;
5277 mddev->ctime = mddev->utime = 0;
5278 mddev->layout = 0;
5279 mddev->max_disks = 0;
5280 mddev->events = 0;
5281 mddev->can_decrease_events = 0;
5282 mddev->delta_disks = 0;
5283 mddev->reshape_backwards = 0;
5284 mddev->new_level = LEVEL_NONE;
5285 mddev->new_layout = 0;
5286 mddev->new_chunk_sectors = 0;
5287 mddev->curr_resync = 0;
5288 atomic64_set(&mddev->resync_mismatches, 0);
5289 mddev->suspend_lo = mddev->suspend_hi = 0;
5290 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5291 mddev->recovery = 0;
5292 mddev->in_sync = 0;
5293 mddev->changed = 0;
5294 mddev->degraded = 0;
5295 mddev->safemode = 0;
5296 mddev->merge_check_needed = 0;
5297 mddev->bitmap_info.offset = 0;
5298 mddev->bitmap_info.default_offset = 0;
5299 mddev->bitmap_info.default_space = 0;
5300 mddev->bitmap_info.chunksize = 0;
5301 mddev->bitmap_info.daemon_sleep = 0;
5302 mddev->bitmap_info.max_write_behind = 0;
5305 static void __md_stop_writes(struct mddev *mddev)
5307 if (mddev_is_clustered(mddev))
5308 md_cluster_ops->metadata_update_start(mddev);
5309 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5310 flush_workqueue(md_misc_wq);
5311 if (mddev->sync_thread) {
5312 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5313 md_reap_sync_thread(mddev);
5316 del_timer_sync(&mddev->safemode_timer);
5318 bitmap_flush(mddev);
5319 md_super_wait(mddev);
5321 if (mddev->ro == 0 &&
5322 (!mddev->in_sync || (mddev->flags & MD_UPDATE_SB_FLAGS))) {
5323 /* mark array as shutdown cleanly */
5324 mddev->in_sync = 1;
5325 md_update_sb(mddev, 1);
5327 if (mddev_is_clustered(mddev))
5328 md_cluster_ops->metadata_update_finish(mddev);
5331 void md_stop_writes(struct mddev *mddev)
5333 mddev_lock_nointr(mddev);
5334 __md_stop_writes(mddev);
5335 mddev_unlock(mddev);
5337 EXPORT_SYMBOL_GPL(md_stop_writes);
5339 static void mddev_detach(struct mddev *mddev)
5341 struct bitmap *bitmap = mddev->bitmap;
5342 /* wait for behind writes to complete */
5343 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
5344 printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n",
5345 mdname(mddev));
5346 /* need to kick something here to make sure I/O goes? */
5347 wait_event(bitmap->behind_wait,
5348 atomic_read(&bitmap->behind_writes) == 0);
5350 if (mddev->pers && mddev->pers->quiesce) {
5351 mddev->pers->quiesce(mddev, 1);
5352 mddev->pers->quiesce(mddev, 0);
5354 md_unregister_thread(&mddev->thread);
5355 if (mddev->queue)
5356 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5359 static void __md_stop(struct mddev *mddev)
5361 struct md_personality *pers = mddev->pers;
5362 mddev_detach(mddev);
5363 spin_lock(&mddev->lock);
5364 mddev->ready = 0;
5365 mddev->pers = NULL;
5366 spin_unlock(&mddev->lock);
5367 pers->free(mddev, mddev->private);
5368 if (pers->sync_request && mddev->to_remove == NULL)
5369 mddev->to_remove = &md_redundancy_group;
5370 module_put(pers->owner);
5371 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5374 void md_stop(struct mddev *mddev)
5376 /* stop the array and free an attached data structures.
5377 * This is called from dm-raid
5379 __md_stop(mddev);
5380 bitmap_destroy(mddev);
5381 if (mddev->bio_set)
5382 bioset_free(mddev->bio_set);
5385 EXPORT_SYMBOL_GPL(md_stop);
5387 static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
5389 int err = 0;
5390 int did_freeze = 0;
5392 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5393 did_freeze = 1;
5394 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5395 md_wakeup_thread(mddev->thread);
5397 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
5398 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5399 if (mddev->sync_thread)
5400 /* Thread might be blocked waiting for metadata update
5401 * which will now never happen */
5402 wake_up_process(mddev->sync_thread->tsk);
5404 mddev_unlock(mddev);
5405 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5406 &mddev->recovery));
5407 mddev_lock_nointr(mddev);
5409 mutex_lock(&mddev->open_mutex);
5410 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
5411 mddev->sync_thread ||
5412 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
5413 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
5414 printk("md: %s still in use.\n",mdname(mddev));
5415 if (did_freeze) {
5416 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5417 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5418 md_wakeup_thread(mddev->thread);
5420 err = -EBUSY;
5421 goto out;
5423 if (mddev->pers) {
5424 __md_stop_writes(mddev);
5426 err = -ENXIO;
5427 if (mddev->ro==1)
5428 goto out;
5429 mddev->ro = 1;
5430 set_disk_ro(mddev->gendisk, 1);
5431 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5432 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5433 md_wakeup_thread(mddev->thread);
5434 sysfs_notify_dirent_safe(mddev->sysfs_state);
5435 err = 0;
5437 out:
5438 mutex_unlock(&mddev->open_mutex);
5439 return err;
5442 /* mode:
5443 * 0 - completely stop and dis-assemble array
5444 * 2 - stop but do not disassemble array
5446 static int do_md_stop(struct mddev *mddev, int mode,
5447 struct block_device *bdev)
5449 struct gendisk *disk = mddev->gendisk;
5450 struct md_rdev *rdev;
5451 int did_freeze = 0;
5453 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5454 did_freeze = 1;
5455 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5456 md_wakeup_thread(mddev->thread);
5458 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
5459 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5460 if (mddev->sync_thread)
5461 /* Thread might be blocked waiting for metadata update
5462 * which will now never happen */
5463 wake_up_process(mddev->sync_thread->tsk);
5465 mddev_unlock(mddev);
5466 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5467 !test_bit(MD_RECOVERY_RUNNING,
5468 &mddev->recovery)));
5469 mddev_lock_nointr(mddev);
5471 mutex_lock(&mddev->open_mutex);
5472 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
5473 mddev->sysfs_active ||
5474 mddev->sync_thread ||
5475 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
5476 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
5477 printk("md: %s still in use.\n",mdname(mddev));
5478 mutex_unlock(&mddev->open_mutex);
5479 if (did_freeze) {
5480 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5481 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5482 md_wakeup_thread(mddev->thread);
5484 return -EBUSY;
5486 if (mddev->pers) {
5487 if (mddev->ro)
5488 set_disk_ro(disk, 0);
5490 __md_stop_writes(mddev);
5491 __md_stop(mddev);
5492 mddev->queue->merge_bvec_fn = NULL;
5493 mddev->queue->backing_dev_info.congested_fn = NULL;
5495 /* tell userspace to handle 'inactive' */
5496 sysfs_notify_dirent_safe(mddev->sysfs_state);
5498 rdev_for_each(rdev, mddev)
5499 if (rdev->raid_disk >= 0)
5500 sysfs_unlink_rdev(mddev, rdev);
5502 set_capacity(disk, 0);
5503 mutex_unlock(&mddev->open_mutex);
5504 mddev->changed = 1;
5505 revalidate_disk(disk);
5507 if (mddev->ro)
5508 mddev->ro = 0;
5509 } else
5510 mutex_unlock(&mddev->open_mutex);
5512 * Free resources if final stop
5514 if (mode == 0) {
5515 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5517 bitmap_destroy(mddev);
5518 if (mddev->bitmap_info.file) {
5519 struct file *f = mddev->bitmap_info.file;
5520 spin_lock(&mddev->lock);
5521 mddev->bitmap_info.file = NULL;
5522 spin_unlock(&mddev->lock);
5523 fput(f);
5525 mddev->bitmap_info.offset = 0;
5527 export_array(mddev);
5529 md_clean(mddev);
5530 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5531 if (mddev->hold_active == UNTIL_STOP)
5532 mddev->hold_active = 0;
5534 blk_integrity_unregister(disk);
5535 md_new_event(mddev);
5536 sysfs_notify_dirent_safe(mddev->sysfs_state);
5537 return 0;
5540 #ifndef MODULE
5541 static void autorun_array(struct mddev *mddev)
5543 struct md_rdev *rdev;
5544 int err;
5546 if (list_empty(&mddev->disks))
5547 return;
5549 printk(KERN_INFO "md: running: ");
5551 rdev_for_each(rdev, mddev) {
5552 char b[BDEVNAME_SIZE];
5553 printk("<%s>", bdevname(rdev->bdev,b));
5555 printk("\n");
5557 err = do_md_run(mddev);
5558 if (err) {
5559 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
5560 do_md_stop(mddev, 0, NULL);
5565 * lets try to run arrays based on all disks that have arrived
5566 * until now. (those are in pending_raid_disks)
5568 * the method: pick the first pending disk, collect all disks with
5569 * the same UUID, remove all from the pending list and put them into
5570 * the 'same_array' list. Then order this list based on superblock
5571 * update time (freshest comes first), kick out 'old' disks and
5572 * compare superblocks. If everything's fine then run it.
5574 * If "unit" is allocated, then bump its reference count
5576 static void autorun_devices(int part)
5578 struct md_rdev *rdev0, *rdev, *tmp;
5579 struct mddev *mddev;
5580 char b[BDEVNAME_SIZE];
5582 printk(KERN_INFO "md: autorun ...\n");
5583 while (!list_empty(&pending_raid_disks)) {
5584 int unit;
5585 dev_t dev;
5586 LIST_HEAD(candidates);
5587 rdev0 = list_entry(pending_raid_disks.next,
5588 struct md_rdev, same_set);
5590 printk(KERN_INFO "md: considering %s ...\n",
5591 bdevname(rdev0->bdev,b));
5592 INIT_LIST_HEAD(&candidates);
5593 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
5594 if (super_90_load(rdev, rdev0, 0) >= 0) {
5595 printk(KERN_INFO "md: adding %s ...\n",
5596 bdevname(rdev->bdev,b));
5597 list_move(&rdev->same_set, &candidates);
5600 * now we have a set of devices, with all of them having
5601 * mostly sane superblocks. It's time to allocate the
5602 * mddev.
5604 if (part) {
5605 dev = MKDEV(mdp_major,
5606 rdev0->preferred_minor << MdpMinorShift);
5607 unit = MINOR(dev) >> MdpMinorShift;
5608 } else {
5609 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5610 unit = MINOR(dev);
5612 if (rdev0->preferred_minor != unit) {
5613 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5614 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5615 break;
5618 md_probe(dev, NULL, NULL);
5619 mddev = mddev_find(dev);
5620 if (!mddev || !mddev->gendisk) {
5621 if (mddev)
5622 mddev_put(mddev);
5623 printk(KERN_ERR
5624 "md: cannot allocate memory for md drive.\n");
5625 break;
5627 if (mddev_lock(mddev))
5628 printk(KERN_WARNING "md: %s locked, cannot run\n",
5629 mdname(mddev));
5630 else if (mddev->raid_disks || mddev->major_version
5631 || !list_empty(&mddev->disks)) {
5632 printk(KERN_WARNING
5633 "md: %s already running, cannot run %s\n",
5634 mdname(mddev), bdevname(rdev0->bdev,b));
5635 mddev_unlock(mddev);
5636 } else {
5637 printk(KERN_INFO "md: created %s\n", mdname(mddev));
5638 mddev->persistent = 1;
5639 rdev_for_each_list(rdev, tmp, &candidates) {
5640 list_del_init(&rdev->same_set);
5641 if (bind_rdev_to_array(rdev, mddev))
5642 export_rdev(rdev);
5644 autorun_array(mddev);
5645 mddev_unlock(mddev);
5647 /* on success, candidates will be empty, on error
5648 * it won't...
5650 rdev_for_each_list(rdev, tmp, &candidates) {
5651 list_del_init(&rdev->same_set);
5652 export_rdev(rdev);
5654 mddev_put(mddev);
5656 printk(KERN_INFO "md: ... autorun DONE.\n");
5658 #endif /* !MODULE */
5660 static int get_version(void __user *arg)
5662 mdu_version_t ver;
5664 ver.major = MD_MAJOR_VERSION;
5665 ver.minor = MD_MINOR_VERSION;
5666 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5668 if (copy_to_user(arg, &ver, sizeof(ver)))
5669 return -EFAULT;
5671 return 0;
5674 static int get_array_info(struct mddev *mddev, void __user *arg)
5676 mdu_array_info_t info;
5677 int nr,working,insync,failed,spare;
5678 struct md_rdev *rdev;
5680 nr = working = insync = failed = spare = 0;
5681 rcu_read_lock();
5682 rdev_for_each_rcu(rdev, mddev) {
5683 nr++;
5684 if (test_bit(Faulty, &rdev->flags))
5685 failed++;
5686 else {
5687 working++;
5688 if (test_bit(In_sync, &rdev->flags))
5689 insync++;
5690 else
5691 spare++;
5694 rcu_read_unlock();
5696 info.major_version = mddev->major_version;
5697 info.minor_version = mddev->minor_version;
5698 info.patch_version = MD_PATCHLEVEL_VERSION;
5699 info.ctime = mddev->ctime;
5700 info.level = mddev->level;
5701 info.size = mddev->dev_sectors / 2;
5702 if (info.size != mddev->dev_sectors / 2) /* overflow */
5703 info.size = -1;
5704 info.nr_disks = nr;
5705 info.raid_disks = mddev->raid_disks;
5706 info.md_minor = mddev->md_minor;
5707 info.not_persistent= !mddev->persistent;
5709 info.utime = mddev->utime;
5710 info.state = 0;
5711 if (mddev->in_sync)
5712 info.state = (1<<MD_SB_CLEAN);
5713 if (mddev->bitmap && mddev->bitmap_info.offset)
5714 info.state |= (1<<MD_SB_BITMAP_PRESENT);
5715 if (mddev_is_clustered(mddev))
5716 info.state |= (1<<MD_SB_CLUSTERED);
5717 info.active_disks = insync;
5718 info.working_disks = working;
5719 info.failed_disks = failed;
5720 info.spare_disks = spare;
5722 info.layout = mddev->layout;
5723 info.chunk_size = mddev->chunk_sectors << 9;
5725 if (copy_to_user(arg, &info, sizeof(info)))
5726 return -EFAULT;
5728 return 0;
5731 static int get_bitmap_file(struct mddev *mddev, void __user * arg)
5733 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5734 char *ptr;
5735 int err;
5737 file = kmalloc(sizeof(*file), GFP_NOIO);
5738 if (!file)
5739 return -ENOMEM;
5741 err = 0;
5742 spin_lock(&mddev->lock);
5743 /* bitmap disabled, zero the first byte and copy out */
5744 if (!mddev->bitmap_info.file)
5745 file->pathname[0] = '\0';
5746 else if ((ptr = d_path(&mddev->bitmap_info.file->f_path,
5747 file->pathname, sizeof(file->pathname))),
5748 IS_ERR(ptr))
5749 err = PTR_ERR(ptr);
5750 else
5751 memmove(file->pathname, ptr,
5752 sizeof(file->pathname)-(ptr-file->pathname));
5753 spin_unlock(&mddev->lock);
5755 if (err == 0 &&
5756 copy_to_user(arg, file, sizeof(*file)))
5757 err = -EFAULT;
5759 kfree(file);
5760 return err;
5763 static int get_disk_info(struct mddev *mddev, void __user * arg)
5765 mdu_disk_info_t info;
5766 struct md_rdev *rdev;
5768 if (copy_from_user(&info, arg, sizeof(info)))
5769 return -EFAULT;
5771 rcu_read_lock();
5772 rdev = md_find_rdev_nr_rcu(mddev, info.number);
5773 if (rdev) {
5774 info.major = MAJOR(rdev->bdev->bd_dev);
5775 info.minor = MINOR(rdev->bdev->bd_dev);
5776 info.raid_disk = rdev->raid_disk;
5777 info.state = 0;
5778 if (test_bit(Faulty, &rdev->flags))
5779 info.state |= (1<<MD_DISK_FAULTY);
5780 else if (test_bit(In_sync, &rdev->flags)) {
5781 info.state |= (1<<MD_DISK_ACTIVE);
5782 info.state |= (1<<MD_DISK_SYNC);
5784 if (test_bit(WriteMostly, &rdev->flags))
5785 info.state |= (1<<MD_DISK_WRITEMOSTLY);
5786 } else {
5787 info.major = info.minor = 0;
5788 info.raid_disk = -1;
5789 info.state = (1<<MD_DISK_REMOVED);
5791 rcu_read_unlock();
5793 if (copy_to_user(arg, &info, sizeof(info)))
5794 return -EFAULT;
5796 return 0;
5799 static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
5801 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5802 struct md_rdev *rdev;
5803 dev_t dev = MKDEV(info->major,info->minor);
5805 if (mddev_is_clustered(mddev) &&
5806 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
5807 pr_err("%s: Cannot add to clustered mddev.\n",
5808 mdname(mddev));
5809 return -EINVAL;
5812 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5813 return -EOVERFLOW;
5815 if (!mddev->raid_disks) {
5816 int err;
5817 /* expecting a device which has a superblock */
5818 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5819 if (IS_ERR(rdev)) {
5820 printk(KERN_WARNING
5821 "md: md_import_device returned %ld\n",
5822 PTR_ERR(rdev));
5823 return PTR_ERR(rdev);
5825 if (!list_empty(&mddev->disks)) {
5826 struct md_rdev *rdev0
5827 = list_entry(mddev->disks.next,
5828 struct md_rdev, same_set);
5829 err = super_types[mddev->major_version]
5830 .load_super(rdev, rdev0, mddev->minor_version);
5831 if (err < 0) {
5832 printk(KERN_WARNING
5833 "md: %s has different UUID to %s\n",
5834 bdevname(rdev->bdev,b),
5835 bdevname(rdev0->bdev,b2));
5836 export_rdev(rdev);
5837 return -EINVAL;
5840 err = bind_rdev_to_array(rdev, mddev);
5841 if (err)
5842 export_rdev(rdev);
5843 return err;
5847 * add_new_disk can be used once the array is assembled
5848 * to add "hot spares". They must already have a superblock
5849 * written
5851 if (mddev->pers) {
5852 int err;
5853 if (!mddev->pers->hot_add_disk) {
5854 printk(KERN_WARNING
5855 "%s: personality does not support diskops!\n",
5856 mdname(mddev));
5857 return -EINVAL;
5859 if (mddev->persistent)
5860 rdev = md_import_device(dev, mddev->major_version,
5861 mddev->minor_version);
5862 else
5863 rdev = md_import_device(dev, -1, -1);
5864 if (IS_ERR(rdev)) {
5865 printk(KERN_WARNING
5866 "md: md_import_device returned %ld\n",
5867 PTR_ERR(rdev));
5868 return PTR_ERR(rdev);
5870 /* set saved_raid_disk if appropriate */
5871 if (!mddev->persistent) {
5872 if (info->state & (1<<MD_DISK_SYNC) &&
5873 info->raid_disk < mddev->raid_disks) {
5874 rdev->raid_disk = info->raid_disk;
5875 set_bit(In_sync, &rdev->flags);
5876 clear_bit(Bitmap_sync, &rdev->flags);
5877 } else
5878 rdev->raid_disk = -1;
5879 rdev->saved_raid_disk = rdev->raid_disk;
5880 } else
5881 super_types[mddev->major_version].
5882 validate_super(mddev, rdev);
5883 if ((info->state & (1<<MD_DISK_SYNC)) &&
5884 rdev->raid_disk != info->raid_disk) {
5885 /* This was a hot-add request, but events doesn't
5886 * match, so reject it.
5888 export_rdev(rdev);
5889 return -EINVAL;
5892 clear_bit(In_sync, &rdev->flags); /* just to be sure */
5893 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5894 set_bit(WriteMostly, &rdev->flags);
5895 else
5896 clear_bit(WriteMostly, &rdev->flags);
5899 * check whether the device shows up in other nodes
5901 if (mddev_is_clustered(mddev)) {
5902 if (info->state & (1 << MD_DISK_CANDIDATE)) {
5903 /* Through --cluster-confirm */
5904 set_bit(Candidate, &rdev->flags);
5905 err = md_cluster_ops->new_disk_ack(mddev, true);
5906 if (err) {
5907 export_rdev(rdev);
5908 return err;
5910 } else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
5911 /* --add initiated by this node */
5912 err = md_cluster_ops->add_new_disk_start(mddev, rdev);
5913 if (err) {
5914 md_cluster_ops->add_new_disk_finish(mddev);
5915 export_rdev(rdev);
5916 return err;
5921 rdev->raid_disk = -1;
5922 err = bind_rdev_to_array(rdev, mddev);
5923 if (err)
5924 export_rdev(rdev);
5925 else
5926 err = add_bound_rdev(rdev);
5927 if (mddev_is_clustered(mddev) &&
5928 (info->state & (1 << MD_DISK_CLUSTER_ADD)))
5929 md_cluster_ops->add_new_disk_finish(mddev);
5930 return err;
5933 /* otherwise, add_new_disk is only allowed
5934 * for major_version==0 superblocks
5936 if (mddev->major_version != 0) {
5937 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5938 mdname(mddev));
5939 return -EINVAL;
5942 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5943 int err;
5944 rdev = md_import_device(dev, -1, 0);
5945 if (IS_ERR(rdev)) {
5946 printk(KERN_WARNING
5947 "md: error, md_import_device() returned %ld\n",
5948 PTR_ERR(rdev));
5949 return PTR_ERR(rdev);
5951 rdev->desc_nr = info->number;
5952 if (info->raid_disk < mddev->raid_disks)
5953 rdev->raid_disk = info->raid_disk;
5954 else
5955 rdev->raid_disk = -1;
5957 if (rdev->raid_disk < mddev->raid_disks)
5958 if (info->state & (1<<MD_DISK_SYNC))
5959 set_bit(In_sync, &rdev->flags);
5961 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5962 set_bit(WriteMostly, &rdev->flags);
5964 if (!mddev->persistent) {
5965 printk(KERN_INFO "md: nonpersistent superblock ...\n");
5966 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5967 } else
5968 rdev->sb_start = calc_dev_sboffset(rdev);
5969 rdev->sectors = rdev->sb_start;
5971 err = bind_rdev_to_array(rdev, mddev);
5972 if (err) {
5973 export_rdev(rdev);
5974 return err;
5978 return 0;
5981 static int hot_remove_disk(struct mddev *mddev, dev_t dev)
5983 char b[BDEVNAME_SIZE];
5984 struct md_rdev *rdev;
5986 rdev = find_rdev(mddev, dev);
5987 if (!rdev)
5988 return -ENXIO;
5990 if (mddev_is_clustered(mddev))
5991 md_cluster_ops->metadata_update_start(mddev);
5993 clear_bit(Blocked, &rdev->flags);
5994 remove_and_add_spares(mddev, rdev);
5996 if (rdev->raid_disk >= 0)
5997 goto busy;
5999 if (mddev_is_clustered(mddev))
6000 md_cluster_ops->remove_disk(mddev, rdev);
6002 md_kick_rdev_from_array(rdev);
6003 md_update_sb(mddev, 1);
6004 md_new_event(mddev);
6006 if (mddev_is_clustered(mddev))
6007 md_cluster_ops->metadata_update_finish(mddev);
6009 return 0;
6010 busy:
6011 if (mddev_is_clustered(mddev))
6012 md_cluster_ops->metadata_update_cancel(mddev);
6013 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
6014 bdevname(rdev->bdev,b), mdname(mddev));
6015 return -EBUSY;
6018 static int hot_add_disk(struct mddev *mddev, dev_t dev)
6020 char b[BDEVNAME_SIZE];
6021 int err;
6022 struct md_rdev *rdev;
6024 if (!mddev->pers)
6025 return -ENODEV;
6027 if (mddev->major_version != 0) {
6028 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
6029 " version-0 superblocks.\n",
6030 mdname(mddev));
6031 return -EINVAL;
6033 if (!mddev->pers->hot_add_disk) {
6034 printk(KERN_WARNING
6035 "%s: personality does not support diskops!\n",
6036 mdname(mddev));
6037 return -EINVAL;
6040 rdev = md_import_device(dev, -1, 0);
6041 if (IS_ERR(rdev)) {
6042 printk(KERN_WARNING
6043 "md: error, md_import_device() returned %ld\n",
6044 PTR_ERR(rdev));
6045 return -EINVAL;
6048 if (mddev->persistent)
6049 rdev->sb_start = calc_dev_sboffset(rdev);
6050 else
6051 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6053 rdev->sectors = rdev->sb_start;
6055 if (test_bit(Faulty, &rdev->flags)) {
6056 printk(KERN_WARNING
6057 "md: can not hot-add faulty %s disk to %s!\n",
6058 bdevname(rdev->bdev,b), mdname(mddev));
6059 err = -EINVAL;
6060 goto abort_export;
6063 if (mddev_is_clustered(mddev))
6064 md_cluster_ops->metadata_update_start(mddev);
6065 clear_bit(In_sync, &rdev->flags);
6066 rdev->desc_nr = -1;
6067 rdev->saved_raid_disk = -1;
6068 err = bind_rdev_to_array(rdev, mddev);
6069 if (err)
6070 goto abort_clustered;
6073 * The rest should better be atomic, we can have disk failures
6074 * noticed in interrupt contexts ...
6077 rdev->raid_disk = -1;
6079 md_update_sb(mddev, 1);
6081 if (mddev_is_clustered(mddev))
6082 md_cluster_ops->metadata_update_finish(mddev);
6084 * Kick recovery, maybe this spare has to be added to the
6085 * array immediately.
6087 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6088 md_wakeup_thread(mddev->thread);
6089 md_new_event(mddev);
6090 return 0;
6092 abort_clustered:
6093 if (mddev_is_clustered(mddev))
6094 md_cluster_ops->metadata_update_cancel(mddev);
6095 abort_export:
6096 export_rdev(rdev);
6097 return err;
6100 static int set_bitmap_file(struct mddev *mddev, int fd)
6102 int err = 0;
6104 if (mddev->pers) {
6105 if (!mddev->pers->quiesce || !mddev->thread)
6106 return -EBUSY;
6107 if (mddev->recovery || mddev->sync_thread)
6108 return -EBUSY;
6109 /* we should be able to change the bitmap.. */
6112 if (fd >= 0) {
6113 struct inode *inode;
6114 struct file *f;
6116 if (mddev->bitmap || mddev->bitmap_info.file)
6117 return -EEXIST; /* cannot add when bitmap is present */
6118 f = fget(fd);
6120 if (f == NULL) {
6121 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
6122 mdname(mddev));
6123 return -EBADF;
6126 inode = f->f_mapping->host;
6127 if (!S_ISREG(inode->i_mode)) {
6128 printk(KERN_ERR "%s: error: bitmap file must be a regular file\n",
6129 mdname(mddev));
6130 err = -EBADF;
6131 } else if (!(f->f_mode & FMODE_WRITE)) {
6132 printk(KERN_ERR "%s: error: bitmap file must open for write\n",
6133 mdname(mddev));
6134 err = -EBADF;
6135 } else if (atomic_read(&inode->i_writecount) != 1) {
6136 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
6137 mdname(mddev));
6138 err = -EBUSY;
6140 if (err) {
6141 fput(f);
6142 return err;
6144 mddev->bitmap_info.file = f;
6145 mddev->bitmap_info.offset = 0; /* file overrides offset */
6146 } else if (mddev->bitmap == NULL)
6147 return -ENOENT; /* cannot remove what isn't there */
6148 err = 0;
6149 if (mddev->pers) {
6150 mddev->pers->quiesce(mddev, 1);
6151 if (fd >= 0) {
6152 struct bitmap *bitmap;
6154 bitmap = bitmap_create(mddev, -1);
6155 if (!IS_ERR(bitmap)) {
6156 mddev->bitmap = bitmap;
6157 err = bitmap_load(mddev);
6158 } else
6159 err = PTR_ERR(bitmap);
6161 if (fd < 0 || err) {
6162 bitmap_destroy(mddev);
6163 fd = -1; /* make sure to put the file */
6165 mddev->pers->quiesce(mddev, 0);
6167 if (fd < 0) {
6168 struct file *f = mddev->bitmap_info.file;
6169 if (f) {
6170 spin_lock(&mddev->lock);
6171 mddev->bitmap_info.file = NULL;
6172 spin_unlock(&mddev->lock);
6173 fput(f);
6177 return err;
6181 * set_array_info is used two different ways
6182 * The original usage is when creating a new array.
6183 * In this usage, raid_disks is > 0 and it together with
6184 * level, size, not_persistent,layout,chunksize determine the
6185 * shape of the array.
6186 * This will always create an array with a type-0.90.0 superblock.
6187 * The newer usage is when assembling an array.
6188 * In this case raid_disks will be 0, and the major_version field is
6189 * use to determine which style super-blocks are to be found on the devices.
6190 * The minor and patch _version numbers are also kept incase the
6191 * super_block handler wishes to interpret them.
6193 static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
6196 if (info->raid_disks == 0) {
6197 /* just setting version number for superblock loading */
6198 if (info->major_version < 0 ||
6199 info->major_version >= ARRAY_SIZE(super_types) ||
6200 super_types[info->major_version].name == NULL) {
6201 /* maybe try to auto-load a module? */
6202 printk(KERN_INFO
6203 "md: superblock version %d not known\n",
6204 info->major_version);
6205 return -EINVAL;
6207 mddev->major_version = info->major_version;
6208 mddev->minor_version = info->minor_version;
6209 mddev->patch_version = info->patch_version;
6210 mddev->persistent = !info->not_persistent;
6211 /* ensure mddev_put doesn't delete this now that there
6212 * is some minimal configuration.
6214 mddev->ctime = get_seconds();
6215 return 0;
6217 mddev->major_version = MD_MAJOR_VERSION;
6218 mddev->minor_version = MD_MINOR_VERSION;
6219 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6220 mddev->ctime = get_seconds();
6222 mddev->level = info->level;
6223 mddev->clevel[0] = 0;
6224 mddev->dev_sectors = 2 * (sector_t)info->size;
6225 mddev->raid_disks = info->raid_disks;
6226 /* don't set md_minor, it is determined by which /dev/md* was
6227 * openned
6229 if (info->state & (1<<MD_SB_CLEAN))
6230 mddev->recovery_cp = MaxSector;
6231 else
6232 mddev->recovery_cp = 0;
6233 mddev->persistent = ! info->not_persistent;
6234 mddev->external = 0;
6236 mddev->layout = info->layout;
6237 mddev->chunk_sectors = info->chunk_size >> 9;
6239 mddev->max_disks = MD_SB_DISKS;
6241 if (mddev->persistent)
6242 mddev->flags = 0;
6243 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6245 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
6246 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
6247 mddev->bitmap_info.offset = 0;
6249 mddev->reshape_position = MaxSector;
6252 * Generate a 128 bit UUID
6254 get_random_bytes(mddev->uuid, 16);
6256 mddev->new_level = mddev->level;
6257 mddev->new_chunk_sectors = mddev->chunk_sectors;
6258 mddev->new_layout = mddev->layout;
6259 mddev->delta_disks = 0;
6260 mddev->reshape_backwards = 0;
6262 return 0;
6265 void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
6267 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6269 if (mddev->external_size)
6270 return;
6272 mddev->array_sectors = array_sectors;
6274 EXPORT_SYMBOL(md_set_array_sectors);
6276 static int update_size(struct mddev *mddev, sector_t num_sectors)
6278 struct md_rdev *rdev;
6279 int rv;
6280 int fit = (num_sectors == 0);
6282 if (mddev->pers->resize == NULL)
6283 return -EINVAL;
6284 /* The "num_sectors" is the number of sectors of each device that
6285 * is used. This can only make sense for arrays with redundancy.
6286 * linear and raid0 always use whatever space is available. We can only
6287 * consider changing this number if no resync or reconstruction is
6288 * happening, and if the new size is acceptable. It must fit before the
6289 * sb_start or, if that is <data_offset, it must fit before the size
6290 * of each device. If num_sectors is zero, we find the largest size
6291 * that fits.
6293 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6294 mddev->sync_thread)
6295 return -EBUSY;
6296 if (mddev->ro)
6297 return -EROFS;
6299 rdev_for_each(rdev, mddev) {
6300 sector_t avail = rdev->sectors;
6302 if (fit && (num_sectors == 0 || num_sectors > avail))
6303 num_sectors = avail;
6304 if (avail < num_sectors)
6305 return -ENOSPC;
6307 rv = mddev->pers->resize(mddev, num_sectors);
6308 if (!rv)
6309 revalidate_disk(mddev->gendisk);
6310 return rv;
6313 static int update_raid_disks(struct mddev *mddev, int raid_disks)
6315 int rv;
6316 struct md_rdev *rdev;
6317 /* change the number of raid disks */
6318 if (mddev->pers->check_reshape == NULL)
6319 return -EINVAL;
6320 if (mddev->ro)
6321 return -EROFS;
6322 if (raid_disks <= 0 ||
6323 (mddev->max_disks && raid_disks >= mddev->max_disks))
6324 return -EINVAL;
6325 if (mddev->sync_thread ||
6326 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6327 mddev->reshape_position != MaxSector)
6328 return -EBUSY;
6330 rdev_for_each(rdev, mddev) {
6331 if (mddev->raid_disks < raid_disks &&
6332 rdev->data_offset < rdev->new_data_offset)
6333 return -EINVAL;
6334 if (mddev->raid_disks > raid_disks &&
6335 rdev->data_offset > rdev->new_data_offset)
6336 return -EINVAL;
6339 mddev->delta_disks = raid_disks - mddev->raid_disks;
6340 if (mddev->delta_disks < 0)
6341 mddev->reshape_backwards = 1;
6342 else if (mddev->delta_disks > 0)
6343 mddev->reshape_backwards = 0;
6345 rv = mddev->pers->check_reshape(mddev);
6346 if (rv < 0) {
6347 mddev->delta_disks = 0;
6348 mddev->reshape_backwards = 0;
6350 return rv;
6354 * update_array_info is used to change the configuration of an
6355 * on-line array.
6356 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6357 * fields in the info are checked against the array.
6358 * Any differences that cannot be handled will cause an error.
6359 * Normally, only one change can be managed at a time.
6361 static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
6363 int rv = 0;
6364 int cnt = 0;
6365 int state = 0;
6367 /* calculate expected state,ignoring low bits */
6368 if (mddev->bitmap && mddev->bitmap_info.offset)
6369 state |= (1 << MD_SB_BITMAP_PRESENT);
6371 if (mddev->major_version != info->major_version ||
6372 mddev->minor_version != info->minor_version ||
6373 /* mddev->patch_version != info->patch_version || */
6374 mddev->ctime != info->ctime ||
6375 mddev->level != info->level ||
6376 /* mddev->layout != info->layout || */
6377 !mddev->persistent != info->not_persistent||
6378 mddev->chunk_sectors != info->chunk_size >> 9 ||
6379 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6380 ((state^info->state) & 0xfffffe00)
6382 return -EINVAL;
6383 /* Check there is only one change */
6384 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6385 cnt++;
6386 if (mddev->raid_disks != info->raid_disks)
6387 cnt++;
6388 if (mddev->layout != info->layout)
6389 cnt++;
6390 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6391 cnt++;
6392 if (cnt == 0)
6393 return 0;
6394 if (cnt > 1)
6395 return -EINVAL;
6397 if (mddev->layout != info->layout) {
6398 /* Change layout
6399 * we don't need to do anything at the md level, the
6400 * personality will take care of it all.
6402 if (mddev->pers->check_reshape == NULL)
6403 return -EINVAL;
6404 else {
6405 mddev->new_layout = info->layout;
6406 rv = mddev->pers->check_reshape(mddev);
6407 if (rv)
6408 mddev->new_layout = mddev->layout;
6409 return rv;
6412 if (mddev_is_clustered(mddev))
6413 md_cluster_ops->metadata_update_start(mddev);
6414 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6415 rv = update_size(mddev, (sector_t)info->size * 2);
6417 if (mddev->raid_disks != info->raid_disks)
6418 rv = update_raid_disks(mddev, info->raid_disks);
6420 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
6421 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6422 rv = -EINVAL;
6423 goto err;
6425 if (mddev->recovery || mddev->sync_thread) {
6426 rv = -EBUSY;
6427 goto err;
6429 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6430 struct bitmap *bitmap;
6431 /* add the bitmap */
6432 if (mddev->bitmap) {
6433 rv = -EEXIST;
6434 goto err;
6436 if (mddev->bitmap_info.default_offset == 0) {
6437 rv = -EINVAL;
6438 goto err;
6440 mddev->bitmap_info.offset =
6441 mddev->bitmap_info.default_offset;
6442 mddev->bitmap_info.space =
6443 mddev->bitmap_info.default_space;
6444 mddev->pers->quiesce(mddev, 1);
6445 bitmap = bitmap_create(mddev, -1);
6446 if (!IS_ERR(bitmap)) {
6447 mddev->bitmap = bitmap;
6448 rv = bitmap_load(mddev);
6449 } else
6450 rv = PTR_ERR(bitmap);
6451 if (rv)
6452 bitmap_destroy(mddev);
6453 mddev->pers->quiesce(mddev, 0);
6454 } else {
6455 /* remove the bitmap */
6456 if (!mddev->bitmap) {
6457 rv = -ENOENT;
6458 goto err;
6460 if (mddev->bitmap->storage.file) {
6461 rv = -EINVAL;
6462 goto err;
6464 mddev->pers->quiesce(mddev, 1);
6465 bitmap_destroy(mddev);
6466 mddev->pers->quiesce(mddev, 0);
6467 mddev->bitmap_info.offset = 0;
6470 md_update_sb(mddev, 1);
6471 if (mddev_is_clustered(mddev))
6472 md_cluster_ops->metadata_update_finish(mddev);
6473 return rv;
6474 err:
6475 if (mddev_is_clustered(mddev))
6476 md_cluster_ops->metadata_update_cancel(mddev);
6477 return rv;
6480 static int set_disk_faulty(struct mddev *mddev, dev_t dev)
6482 struct md_rdev *rdev;
6483 int err = 0;
6485 if (mddev->pers == NULL)
6486 return -ENODEV;
6488 rcu_read_lock();
6489 rdev = find_rdev_rcu(mddev, dev);
6490 if (!rdev)
6491 err = -ENODEV;
6492 else {
6493 md_error(mddev, rdev);
6494 if (!test_bit(Faulty, &rdev->flags))
6495 err = -EBUSY;
6497 rcu_read_unlock();
6498 return err;
6502 * We have a problem here : there is no easy way to give a CHS
6503 * virtual geometry. We currently pretend that we have a 2 heads
6504 * 4 sectors (with a BIG number of cylinders...). This drives
6505 * dosfs just mad... ;-)
6507 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6509 struct mddev *mddev = bdev->bd_disk->private_data;
6511 geo->heads = 2;
6512 geo->sectors = 4;
6513 geo->cylinders = mddev->array_sectors / 8;
6514 return 0;
6517 static inline bool md_ioctl_valid(unsigned int cmd)
6519 switch (cmd) {
6520 case ADD_NEW_DISK:
6521 case BLKROSET:
6522 case GET_ARRAY_INFO:
6523 case GET_BITMAP_FILE:
6524 case GET_DISK_INFO:
6525 case HOT_ADD_DISK:
6526 case HOT_REMOVE_DISK:
6527 case RAID_AUTORUN:
6528 case RAID_VERSION:
6529 case RESTART_ARRAY_RW:
6530 case RUN_ARRAY:
6531 case SET_ARRAY_INFO:
6532 case SET_BITMAP_FILE:
6533 case SET_DISK_FAULTY:
6534 case STOP_ARRAY:
6535 case STOP_ARRAY_RO:
6536 case CLUSTERED_DISK_NACK:
6537 return true;
6538 default:
6539 return false;
6543 static int md_ioctl(struct block_device *bdev, fmode_t mode,
6544 unsigned int cmd, unsigned long arg)
6546 int err = 0;
6547 void __user *argp = (void __user *)arg;
6548 struct mddev *mddev = NULL;
6549 int ro;
6551 if (!md_ioctl_valid(cmd))
6552 return -ENOTTY;
6554 switch (cmd) {
6555 case RAID_VERSION:
6556 case GET_ARRAY_INFO:
6557 case GET_DISK_INFO:
6558 break;
6559 default:
6560 if (!capable(CAP_SYS_ADMIN))
6561 return -EACCES;
6565 * Commands dealing with the RAID driver but not any
6566 * particular array:
6568 switch (cmd) {
6569 case RAID_VERSION:
6570 err = get_version(argp);
6571 goto out;
6573 #ifndef MODULE
6574 case RAID_AUTORUN:
6575 err = 0;
6576 autostart_arrays(arg);
6577 goto out;
6578 #endif
6579 default:;
6583 * Commands creating/starting a new array:
6586 mddev = bdev->bd_disk->private_data;
6588 if (!mddev) {
6589 BUG();
6590 goto out;
6593 /* Some actions do not requires the mutex */
6594 switch (cmd) {
6595 case GET_ARRAY_INFO:
6596 if (!mddev->raid_disks && !mddev->external)
6597 err = -ENODEV;
6598 else
6599 err = get_array_info(mddev, argp);
6600 goto out;
6602 case GET_DISK_INFO:
6603 if (!mddev->raid_disks && !mddev->external)
6604 err = -ENODEV;
6605 else
6606 err = get_disk_info(mddev, argp);
6607 goto out;
6609 case SET_DISK_FAULTY:
6610 err = set_disk_faulty(mddev, new_decode_dev(arg));
6611 goto out;
6613 case GET_BITMAP_FILE:
6614 err = get_bitmap_file(mddev, argp);
6615 goto out;
6619 if (cmd == ADD_NEW_DISK)
6620 /* need to ensure md_delayed_delete() has completed */
6621 flush_workqueue(md_misc_wq);
6623 if (cmd == HOT_REMOVE_DISK)
6624 /* need to ensure recovery thread has run */
6625 wait_event_interruptible_timeout(mddev->sb_wait,
6626 !test_bit(MD_RECOVERY_NEEDED,
6627 &mddev->flags),
6628 msecs_to_jiffies(5000));
6629 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6630 /* Need to flush page cache, and ensure no-one else opens
6631 * and writes
6633 mutex_lock(&mddev->open_mutex);
6634 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
6635 mutex_unlock(&mddev->open_mutex);
6636 err = -EBUSY;
6637 goto out;
6639 set_bit(MD_STILL_CLOSED, &mddev->flags);
6640 mutex_unlock(&mddev->open_mutex);
6641 sync_blockdev(bdev);
6643 err = mddev_lock(mddev);
6644 if (err) {
6645 printk(KERN_INFO
6646 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6647 err, cmd);
6648 goto out;
6651 if (cmd == SET_ARRAY_INFO) {
6652 mdu_array_info_t info;
6653 if (!arg)
6654 memset(&info, 0, sizeof(info));
6655 else if (copy_from_user(&info, argp, sizeof(info))) {
6656 err = -EFAULT;
6657 goto unlock;
6659 if (mddev->pers) {
6660 err = update_array_info(mddev, &info);
6661 if (err) {
6662 printk(KERN_WARNING "md: couldn't update"
6663 " array info. %d\n", err);
6664 goto unlock;
6666 goto unlock;
6668 if (!list_empty(&mddev->disks)) {
6669 printk(KERN_WARNING
6670 "md: array %s already has disks!\n",
6671 mdname(mddev));
6672 err = -EBUSY;
6673 goto unlock;
6675 if (mddev->raid_disks) {
6676 printk(KERN_WARNING
6677 "md: array %s already initialised!\n",
6678 mdname(mddev));
6679 err = -EBUSY;
6680 goto unlock;
6682 err = set_array_info(mddev, &info);
6683 if (err) {
6684 printk(KERN_WARNING "md: couldn't set"
6685 " array info. %d\n", err);
6686 goto unlock;
6688 goto unlock;
6692 * Commands querying/configuring an existing array:
6694 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
6695 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
6696 if ((!mddev->raid_disks && !mddev->external)
6697 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6698 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6699 && cmd != GET_BITMAP_FILE) {
6700 err = -ENODEV;
6701 goto unlock;
6705 * Commands even a read-only array can execute:
6707 switch (cmd) {
6708 case RESTART_ARRAY_RW:
6709 err = restart_array(mddev);
6710 goto unlock;
6712 case STOP_ARRAY:
6713 err = do_md_stop(mddev, 0, bdev);
6714 goto unlock;
6716 case STOP_ARRAY_RO:
6717 err = md_set_readonly(mddev, bdev);
6718 goto unlock;
6720 case HOT_REMOVE_DISK:
6721 err = hot_remove_disk(mddev, new_decode_dev(arg));
6722 goto unlock;
6724 case ADD_NEW_DISK:
6725 /* We can support ADD_NEW_DISK on read-only arrays
6726 * on if we are re-adding a preexisting device.
6727 * So require mddev->pers and MD_DISK_SYNC.
6729 if (mddev->pers) {
6730 mdu_disk_info_t info;
6731 if (copy_from_user(&info, argp, sizeof(info)))
6732 err = -EFAULT;
6733 else if (!(info.state & (1<<MD_DISK_SYNC)))
6734 /* Need to clear read-only for this */
6735 break;
6736 else
6737 err = add_new_disk(mddev, &info);
6738 goto unlock;
6740 break;
6742 case BLKROSET:
6743 if (get_user(ro, (int __user *)(arg))) {
6744 err = -EFAULT;
6745 goto unlock;
6747 err = -EINVAL;
6749 /* if the bdev is going readonly the value of mddev->ro
6750 * does not matter, no writes are coming
6752 if (ro)
6753 goto unlock;
6755 /* are we are already prepared for writes? */
6756 if (mddev->ro != 1)
6757 goto unlock;
6759 /* transitioning to readauto need only happen for
6760 * arrays that call md_write_start
6762 if (mddev->pers) {
6763 err = restart_array(mddev);
6764 if (err == 0) {
6765 mddev->ro = 2;
6766 set_disk_ro(mddev->gendisk, 0);
6769 goto unlock;
6773 * The remaining ioctls are changing the state of the
6774 * superblock, so we do not allow them on read-only arrays.
6776 if (mddev->ro && mddev->pers) {
6777 if (mddev->ro == 2) {
6778 mddev->ro = 0;
6779 sysfs_notify_dirent_safe(mddev->sysfs_state);
6780 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6781 /* mddev_unlock will wake thread */
6782 /* If a device failed while we were read-only, we
6783 * need to make sure the metadata is updated now.
6785 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6786 mddev_unlock(mddev);
6787 wait_event(mddev->sb_wait,
6788 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6789 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
6790 mddev_lock_nointr(mddev);
6792 } else {
6793 err = -EROFS;
6794 goto unlock;
6798 switch (cmd) {
6799 case ADD_NEW_DISK:
6801 mdu_disk_info_t info;
6802 if (copy_from_user(&info, argp, sizeof(info)))
6803 err = -EFAULT;
6804 else
6805 err = add_new_disk(mddev, &info);
6806 goto unlock;
6809 case CLUSTERED_DISK_NACK:
6810 if (mddev_is_clustered(mddev))
6811 md_cluster_ops->new_disk_ack(mddev, false);
6812 else
6813 err = -EINVAL;
6814 goto unlock;
6816 case HOT_ADD_DISK:
6817 err = hot_add_disk(mddev, new_decode_dev(arg));
6818 goto unlock;
6820 case RUN_ARRAY:
6821 err = do_md_run(mddev);
6822 goto unlock;
6824 case SET_BITMAP_FILE:
6825 err = set_bitmap_file(mddev, (int)arg);
6826 goto unlock;
6828 default:
6829 err = -EINVAL;
6830 goto unlock;
6833 unlock:
6834 if (mddev->hold_active == UNTIL_IOCTL &&
6835 err != -EINVAL)
6836 mddev->hold_active = 0;
6837 mddev_unlock(mddev);
6838 out:
6839 return err;
6841 #ifdef CONFIG_COMPAT
6842 static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6843 unsigned int cmd, unsigned long arg)
6845 switch (cmd) {
6846 case HOT_REMOVE_DISK:
6847 case HOT_ADD_DISK:
6848 case SET_DISK_FAULTY:
6849 case SET_BITMAP_FILE:
6850 /* These take in integer arg, do not convert */
6851 break;
6852 default:
6853 arg = (unsigned long)compat_ptr(arg);
6854 break;
6857 return md_ioctl(bdev, mode, cmd, arg);
6859 #endif /* CONFIG_COMPAT */
6861 static int md_open(struct block_device *bdev, fmode_t mode)
6864 * Succeed if we can lock the mddev, which confirms that
6865 * it isn't being stopped right now.
6867 struct mddev *mddev = mddev_find(bdev->bd_dev);
6868 int err;
6870 if (!mddev)
6871 return -ENODEV;
6873 if (mddev->gendisk != bdev->bd_disk) {
6874 /* we are racing with mddev_put which is discarding this
6875 * bd_disk.
6877 mddev_put(mddev);
6878 /* Wait until bdev->bd_disk is definitely gone */
6879 flush_workqueue(md_misc_wq);
6880 /* Then retry the open from the top */
6881 return -ERESTARTSYS;
6883 BUG_ON(mddev != bdev->bd_disk->private_data);
6885 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
6886 goto out;
6888 err = 0;
6889 atomic_inc(&mddev->openers);
6890 clear_bit(MD_STILL_CLOSED, &mddev->flags);
6891 mutex_unlock(&mddev->open_mutex);
6893 check_disk_change(bdev);
6894 out:
6895 return err;
6898 static void md_release(struct gendisk *disk, fmode_t mode)
6900 struct mddev *mddev = disk->private_data;
6902 BUG_ON(!mddev);
6903 atomic_dec(&mddev->openers);
6904 mddev_put(mddev);
6907 static int md_media_changed(struct gendisk *disk)
6909 struct mddev *mddev = disk->private_data;
6911 return mddev->changed;
6914 static int md_revalidate(struct gendisk *disk)
6916 struct mddev *mddev = disk->private_data;
6918 mddev->changed = 0;
6919 return 0;
6921 static const struct block_device_operations md_fops =
6923 .owner = THIS_MODULE,
6924 .open = md_open,
6925 .release = md_release,
6926 .ioctl = md_ioctl,
6927 #ifdef CONFIG_COMPAT
6928 .compat_ioctl = md_compat_ioctl,
6929 #endif
6930 .getgeo = md_getgeo,
6931 .media_changed = md_media_changed,
6932 .revalidate_disk= md_revalidate,
6935 static int md_thread(void *arg)
6937 struct md_thread *thread = arg;
6940 * md_thread is a 'system-thread', it's priority should be very
6941 * high. We avoid resource deadlocks individually in each
6942 * raid personality. (RAID5 does preallocation) We also use RR and
6943 * the very same RT priority as kswapd, thus we will never get
6944 * into a priority inversion deadlock.
6946 * we definitely have to have equal or higher priority than
6947 * bdflush, otherwise bdflush will deadlock if there are too
6948 * many dirty RAID5 blocks.
6951 allow_signal(SIGKILL);
6952 while (!kthread_should_stop()) {
6954 /* We need to wait INTERRUPTIBLE so that
6955 * we don't add to the load-average.
6956 * That means we need to be sure no signals are
6957 * pending
6959 if (signal_pending(current))
6960 flush_signals(current);
6962 wait_event_interruptible_timeout
6963 (thread->wqueue,
6964 test_bit(THREAD_WAKEUP, &thread->flags)
6965 || kthread_should_stop(),
6966 thread->timeout);
6968 clear_bit(THREAD_WAKEUP, &thread->flags);
6969 if (!kthread_should_stop())
6970 thread->run(thread);
6973 return 0;
6976 void md_wakeup_thread(struct md_thread *thread)
6978 if (thread) {
6979 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
6980 set_bit(THREAD_WAKEUP, &thread->flags);
6981 wake_up(&thread->wqueue);
6984 EXPORT_SYMBOL(md_wakeup_thread);
6986 struct md_thread *md_register_thread(void (*run) (struct md_thread *),
6987 struct mddev *mddev, const char *name)
6989 struct md_thread *thread;
6991 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
6992 if (!thread)
6993 return NULL;
6995 init_waitqueue_head(&thread->wqueue);
6997 thread->run = run;
6998 thread->mddev = mddev;
6999 thread->timeout = MAX_SCHEDULE_TIMEOUT;
7000 thread->tsk = kthread_run(md_thread, thread,
7001 "%s_%s",
7002 mdname(thread->mddev),
7003 name);
7004 if (IS_ERR(thread->tsk)) {
7005 kfree(thread);
7006 return NULL;
7008 return thread;
7010 EXPORT_SYMBOL(md_register_thread);
7012 void md_unregister_thread(struct md_thread **threadp)
7014 struct md_thread *thread = *threadp;
7015 if (!thread)
7016 return;
7017 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
7018 /* Locking ensures that mddev_unlock does not wake_up a
7019 * non-existent thread
7021 spin_lock(&pers_lock);
7022 *threadp = NULL;
7023 spin_unlock(&pers_lock);
7025 kthread_stop(thread->tsk);
7026 kfree(thread);
7028 EXPORT_SYMBOL(md_unregister_thread);
7030 void md_error(struct mddev *mddev, struct md_rdev *rdev)
7032 if (!rdev || test_bit(Faulty, &rdev->flags))
7033 return;
7035 if (!mddev->pers || !mddev->pers->error_handler)
7036 return;
7037 mddev->pers->error_handler(mddev,rdev);
7038 if (mddev->degraded)
7039 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7040 sysfs_notify_dirent_safe(rdev->sysfs_state);
7041 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7042 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7043 md_wakeup_thread(mddev->thread);
7044 if (mddev->event_work.func)
7045 queue_work(md_misc_wq, &mddev->event_work);
7046 md_new_event_inintr(mddev);
7048 EXPORT_SYMBOL(md_error);
7050 /* seq_file implementation /proc/mdstat */
7052 static void status_unused(struct seq_file *seq)
7054 int i = 0;
7055 struct md_rdev *rdev;
7057 seq_printf(seq, "unused devices: ");
7059 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
7060 char b[BDEVNAME_SIZE];
7061 i++;
7062 seq_printf(seq, "%s ",
7063 bdevname(rdev->bdev,b));
7065 if (!i)
7066 seq_printf(seq, "<none>");
7068 seq_printf(seq, "\n");
7071 static void status_resync(struct seq_file *seq, struct mddev *mddev)
7073 sector_t max_sectors, resync, res;
7074 unsigned long dt, db;
7075 sector_t rt;
7076 int scale;
7077 unsigned int per_milli;
7079 if (mddev->curr_resync <= 3)
7080 resync = 0;
7081 else
7082 resync = mddev->curr_resync
7083 - atomic_read(&mddev->recovery_active);
7085 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7086 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7087 max_sectors = mddev->resync_max_sectors;
7088 else
7089 max_sectors = mddev->dev_sectors;
7091 WARN_ON(max_sectors == 0);
7092 /* Pick 'scale' such that (resync>>scale)*1000 will fit
7093 * in a sector_t, and (max_sectors>>scale) will fit in a
7094 * u32, as those are the requirements for sector_div.
7095 * Thus 'scale' must be at least 10
7097 scale = 10;
7098 if (sizeof(sector_t) > sizeof(unsigned long)) {
7099 while ( max_sectors/2 > (1ULL<<(scale+32)))
7100 scale++;
7102 res = (resync>>scale)*1000;
7103 sector_div(res, (u32)((max_sectors>>scale)+1));
7105 per_milli = res;
7107 int i, x = per_milli/50, y = 20-x;
7108 seq_printf(seq, "[");
7109 for (i = 0; i < x; i++)
7110 seq_printf(seq, "=");
7111 seq_printf(seq, ">");
7112 for (i = 0; i < y; i++)
7113 seq_printf(seq, ".");
7114 seq_printf(seq, "] ");
7116 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
7117 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7118 "reshape" :
7119 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7120 "check" :
7121 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7122 "resync" : "recovery"))),
7123 per_milli/10, per_milli % 10,
7124 (unsigned long long) resync/2,
7125 (unsigned long long) max_sectors/2);
7128 * dt: time from mark until now
7129 * db: blocks written from mark until now
7130 * rt: remaining time
7132 * rt is a sector_t, so could be 32bit or 64bit.
7133 * So we divide before multiply in case it is 32bit and close
7134 * to the limit.
7135 * We scale the divisor (db) by 32 to avoid losing precision
7136 * near the end of resync when the number of remaining sectors
7137 * is close to 'db'.
7138 * We then divide rt by 32 after multiplying by db to compensate.
7139 * The '+1' avoids division by zero if db is very small.
7141 dt = ((jiffies - mddev->resync_mark) / HZ);
7142 if (!dt) dt++;
7143 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7144 - mddev->resync_mark_cnt;
7146 rt = max_sectors - resync; /* number of remaining sectors */
7147 sector_div(rt, db/32+1);
7148 rt *= dt;
7149 rt >>= 5;
7151 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7152 ((unsigned long)rt % 60)/6);
7154 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
7157 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7159 struct list_head *tmp;
7160 loff_t l = *pos;
7161 struct mddev *mddev;
7163 if (l >= 0x10000)
7164 return NULL;
7165 if (!l--)
7166 /* header */
7167 return (void*)1;
7169 spin_lock(&all_mddevs_lock);
7170 list_for_each(tmp,&all_mddevs)
7171 if (!l--) {
7172 mddev = list_entry(tmp, struct mddev, all_mddevs);
7173 mddev_get(mddev);
7174 spin_unlock(&all_mddevs_lock);
7175 return mddev;
7177 spin_unlock(&all_mddevs_lock);
7178 if (!l--)
7179 return (void*)2;/* tail */
7180 return NULL;
7183 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7185 struct list_head *tmp;
7186 struct mddev *next_mddev, *mddev = v;
7188 ++*pos;
7189 if (v == (void*)2)
7190 return NULL;
7192 spin_lock(&all_mddevs_lock);
7193 if (v == (void*)1)
7194 tmp = all_mddevs.next;
7195 else
7196 tmp = mddev->all_mddevs.next;
7197 if (tmp != &all_mddevs)
7198 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
7199 else {
7200 next_mddev = (void*)2;
7201 *pos = 0x10000;
7203 spin_unlock(&all_mddevs_lock);
7205 if (v != (void*)1)
7206 mddev_put(mddev);
7207 return next_mddev;
7211 static void md_seq_stop(struct seq_file *seq, void *v)
7213 struct mddev *mddev = v;
7215 if (mddev && v != (void*)1 && v != (void*)2)
7216 mddev_put(mddev);
7219 static int md_seq_show(struct seq_file *seq, void *v)
7221 struct mddev *mddev = v;
7222 sector_t sectors;
7223 struct md_rdev *rdev;
7225 if (v == (void*)1) {
7226 struct md_personality *pers;
7227 seq_printf(seq, "Personalities : ");
7228 spin_lock(&pers_lock);
7229 list_for_each_entry(pers, &pers_list, list)
7230 seq_printf(seq, "[%s] ", pers->name);
7232 spin_unlock(&pers_lock);
7233 seq_printf(seq, "\n");
7234 seq->poll_event = atomic_read(&md_event_count);
7235 return 0;
7237 if (v == (void*)2) {
7238 status_unused(seq);
7239 return 0;
7242 spin_lock(&mddev->lock);
7243 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7244 seq_printf(seq, "%s : %sactive", mdname(mddev),
7245 mddev->pers ? "" : "in");
7246 if (mddev->pers) {
7247 if (mddev->ro==1)
7248 seq_printf(seq, " (read-only)");
7249 if (mddev->ro==2)
7250 seq_printf(seq, " (auto-read-only)");
7251 seq_printf(seq, " %s", mddev->pers->name);
7254 sectors = 0;
7255 rcu_read_lock();
7256 rdev_for_each_rcu(rdev, mddev) {
7257 char b[BDEVNAME_SIZE];
7258 seq_printf(seq, " %s[%d]",
7259 bdevname(rdev->bdev,b), rdev->desc_nr);
7260 if (test_bit(WriteMostly, &rdev->flags))
7261 seq_printf(seq, "(W)");
7262 if (test_bit(Faulty, &rdev->flags)) {
7263 seq_printf(seq, "(F)");
7264 continue;
7266 if (rdev->raid_disk < 0)
7267 seq_printf(seq, "(S)"); /* spare */
7268 if (test_bit(Replacement, &rdev->flags))
7269 seq_printf(seq, "(R)");
7270 sectors += rdev->sectors;
7272 rcu_read_unlock();
7274 if (!list_empty(&mddev->disks)) {
7275 if (mddev->pers)
7276 seq_printf(seq, "\n %llu blocks",
7277 (unsigned long long)
7278 mddev->array_sectors / 2);
7279 else
7280 seq_printf(seq, "\n %llu blocks",
7281 (unsigned long long)sectors / 2);
7283 if (mddev->persistent) {
7284 if (mddev->major_version != 0 ||
7285 mddev->minor_version != 90) {
7286 seq_printf(seq," super %d.%d",
7287 mddev->major_version,
7288 mddev->minor_version);
7290 } else if (mddev->external)
7291 seq_printf(seq, " super external:%s",
7292 mddev->metadata_type);
7293 else
7294 seq_printf(seq, " super non-persistent");
7296 if (mddev->pers) {
7297 mddev->pers->status(seq, mddev);
7298 seq_printf(seq, "\n ");
7299 if (mddev->pers->sync_request) {
7300 if (mddev->curr_resync > 2) {
7301 status_resync(seq, mddev);
7302 seq_printf(seq, "\n ");
7303 } else if (mddev->curr_resync >= 1)
7304 seq_printf(seq, "\tresync=DELAYED\n ");
7305 else if (mddev->recovery_cp < MaxSector)
7306 seq_printf(seq, "\tresync=PENDING\n ");
7308 } else
7309 seq_printf(seq, "\n ");
7311 bitmap_status(seq, mddev->bitmap);
7313 seq_printf(seq, "\n");
7315 spin_unlock(&mddev->lock);
7317 return 0;
7320 static const struct seq_operations md_seq_ops = {
7321 .start = md_seq_start,
7322 .next = md_seq_next,
7323 .stop = md_seq_stop,
7324 .show = md_seq_show,
7327 static int md_seq_open(struct inode *inode, struct file *file)
7329 struct seq_file *seq;
7330 int error;
7332 error = seq_open(file, &md_seq_ops);
7333 if (error)
7334 return error;
7336 seq = file->private_data;
7337 seq->poll_event = atomic_read(&md_event_count);
7338 return error;
7341 static int md_unloading;
7342 static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7344 struct seq_file *seq = filp->private_data;
7345 int mask;
7347 if (md_unloading)
7348 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
7349 poll_wait(filp, &md_event_waiters, wait);
7351 /* always allow read */
7352 mask = POLLIN | POLLRDNORM;
7354 if (seq->poll_event != atomic_read(&md_event_count))
7355 mask |= POLLERR | POLLPRI;
7356 return mask;
7359 static const struct file_operations md_seq_fops = {
7360 .owner = THIS_MODULE,
7361 .open = md_seq_open,
7362 .read = seq_read,
7363 .llseek = seq_lseek,
7364 .release = seq_release_private,
7365 .poll = mdstat_poll,
7368 int register_md_personality(struct md_personality *p)
7370 printk(KERN_INFO "md: %s personality registered for level %d\n",
7371 p->name, p->level);
7372 spin_lock(&pers_lock);
7373 list_add_tail(&p->list, &pers_list);
7374 spin_unlock(&pers_lock);
7375 return 0;
7377 EXPORT_SYMBOL(register_md_personality);
7379 int unregister_md_personality(struct md_personality *p)
7381 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
7382 spin_lock(&pers_lock);
7383 list_del_init(&p->list);
7384 spin_unlock(&pers_lock);
7385 return 0;
7387 EXPORT_SYMBOL(unregister_md_personality);
7389 int register_md_cluster_operations(struct md_cluster_operations *ops, struct module *module)
7391 if (md_cluster_ops != NULL)
7392 return -EALREADY;
7393 spin_lock(&pers_lock);
7394 md_cluster_ops = ops;
7395 md_cluster_mod = module;
7396 spin_unlock(&pers_lock);
7397 return 0;
7399 EXPORT_SYMBOL(register_md_cluster_operations);
7401 int unregister_md_cluster_operations(void)
7403 spin_lock(&pers_lock);
7404 md_cluster_ops = NULL;
7405 spin_unlock(&pers_lock);
7406 return 0;
7408 EXPORT_SYMBOL(unregister_md_cluster_operations);
7410 int md_setup_cluster(struct mddev *mddev, int nodes)
7412 int err;
7414 err = request_module("md-cluster");
7415 if (err) {
7416 pr_err("md-cluster module not found.\n");
7417 return err;
7420 spin_lock(&pers_lock);
7421 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
7422 spin_unlock(&pers_lock);
7423 return -ENOENT;
7425 spin_unlock(&pers_lock);
7427 return md_cluster_ops->join(mddev, nodes);
7430 void md_cluster_stop(struct mddev *mddev)
7432 if (!md_cluster_ops)
7433 return;
7434 md_cluster_ops->leave(mddev);
7435 module_put(md_cluster_mod);
7438 static int is_mddev_idle(struct mddev *mddev, int init)
7440 struct md_rdev *rdev;
7441 int idle;
7442 int curr_events;
7444 idle = 1;
7445 rcu_read_lock();
7446 rdev_for_each_rcu(rdev, mddev) {
7447 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
7448 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7449 (int)part_stat_read(&disk->part0, sectors[1]) -
7450 atomic_read(&disk->sync_io);
7451 /* sync IO will cause sync_io to increase before the disk_stats
7452 * as sync_io is counted when a request starts, and
7453 * disk_stats is counted when it completes.
7454 * So resync activity will cause curr_events to be smaller than
7455 * when there was no such activity.
7456 * non-sync IO will cause disk_stat to increase without
7457 * increasing sync_io so curr_events will (eventually)
7458 * be larger than it was before. Once it becomes
7459 * substantially larger, the test below will cause
7460 * the array to appear non-idle, and resync will slow
7461 * down.
7462 * If there is a lot of outstanding resync activity when
7463 * we set last_event to curr_events, then all that activity
7464 * completing might cause the array to appear non-idle
7465 * and resync will be slowed down even though there might
7466 * not have been non-resync activity. This will only
7467 * happen once though. 'last_events' will soon reflect
7468 * the state where there is little or no outstanding
7469 * resync requests, and further resync activity will
7470 * always make curr_events less than last_events.
7473 if (init || curr_events - rdev->last_events > 64) {
7474 rdev->last_events = curr_events;
7475 idle = 0;
7478 rcu_read_unlock();
7479 return idle;
7482 void md_done_sync(struct mddev *mddev, int blocks, int ok)
7484 /* another "blocks" (512byte) blocks have been synced */
7485 atomic_sub(blocks, &mddev->recovery_active);
7486 wake_up(&mddev->recovery_wait);
7487 if (!ok) {
7488 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7489 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
7490 md_wakeup_thread(mddev->thread);
7491 // stop recovery, signal do_sync ....
7494 EXPORT_SYMBOL(md_done_sync);
7496 /* md_write_start(mddev, bi)
7497 * If we need to update some array metadata (e.g. 'active' flag
7498 * in superblock) before writing, schedule a superblock update
7499 * and wait for it to complete.
7501 void md_write_start(struct mddev *mddev, struct bio *bi)
7503 int did_change = 0;
7504 if (bio_data_dir(bi) != WRITE)
7505 return;
7507 BUG_ON(mddev->ro == 1);
7508 if (mddev->ro == 2) {
7509 /* need to switch to read/write */
7510 mddev->ro = 0;
7511 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7512 md_wakeup_thread(mddev->thread);
7513 md_wakeup_thread(mddev->sync_thread);
7514 did_change = 1;
7516 atomic_inc(&mddev->writes_pending);
7517 if (mddev->safemode == 1)
7518 mddev->safemode = 0;
7519 if (mddev->in_sync) {
7520 spin_lock(&mddev->lock);
7521 if (mddev->in_sync) {
7522 mddev->in_sync = 0;
7523 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
7524 set_bit(MD_CHANGE_PENDING, &mddev->flags);
7525 md_wakeup_thread(mddev->thread);
7526 did_change = 1;
7528 spin_unlock(&mddev->lock);
7530 if (did_change)
7531 sysfs_notify_dirent_safe(mddev->sysfs_state);
7532 wait_event(mddev->sb_wait,
7533 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
7535 EXPORT_SYMBOL(md_write_start);
7537 void md_write_end(struct mddev *mddev)
7539 if (atomic_dec_and_test(&mddev->writes_pending)) {
7540 if (mddev->safemode == 2)
7541 md_wakeup_thread(mddev->thread);
7542 else if (mddev->safemode_delay)
7543 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7546 EXPORT_SYMBOL(md_write_end);
7548 /* md_allow_write(mddev)
7549 * Calling this ensures that the array is marked 'active' so that writes
7550 * may proceed without blocking. It is important to call this before
7551 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7552 * Must be called with mddev_lock held.
7554 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7555 * is dropped, so return -EAGAIN after notifying userspace.
7557 int md_allow_write(struct mddev *mddev)
7559 if (!mddev->pers)
7560 return 0;
7561 if (mddev->ro)
7562 return 0;
7563 if (!mddev->pers->sync_request)
7564 return 0;
7566 spin_lock(&mddev->lock);
7567 if (mddev->in_sync) {
7568 mddev->in_sync = 0;
7569 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
7570 set_bit(MD_CHANGE_PENDING, &mddev->flags);
7571 if (mddev->safemode_delay &&
7572 mddev->safemode == 0)
7573 mddev->safemode = 1;
7574 spin_unlock(&mddev->lock);
7575 if (mddev_is_clustered(mddev))
7576 md_cluster_ops->metadata_update_start(mddev);
7577 md_update_sb(mddev, 0);
7578 if (mddev_is_clustered(mddev))
7579 md_cluster_ops->metadata_update_finish(mddev);
7580 sysfs_notify_dirent_safe(mddev->sysfs_state);
7581 } else
7582 spin_unlock(&mddev->lock);
7584 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
7585 return -EAGAIN;
7586 else
7587 return 0;
7589 EXPORT_SYMBOL_GPL(md_allow_write);
7591 #define SYNC_MARKS 10
7592 #define SYNC_MARK_STEP (3*HZ)
7593 #define UPDATE_FREQUENCY (5*60*HZ)
7594 void md_do_sync(struct md_thread *thread)
7596 struct mddev *mddev = thread->mddev;
7597 struct mddev *mddev2;
7598 unsigned int currspeed = 0,
7599 window;
7600 sector_t max_sectors,j, io_sectors, recovery_done;
7601 unsigned long mark[SYNC_MARKS];
7602 unsigned long update_time;
7603 sector_t mark_cnt[SYNC_MARKS];
7604 int last_mark,m;
7605 struct list_head *tmp;
7606 sector_t last_check;
7607 int skipped = 0;
7608 struct md_rdev *rdev;
7609 char *desc, *action = NULL;
7610 struct blk_plug plug;
7612 /* just incase thread restarts... */
7613 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7614 return;
7615 if (mddev->ro) {/* never try to sync a read-only array */
7616 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7617 return;
7620 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7621 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
7622 desc = "data-check";
7623 action = "check";
7624 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7625 desc = "requested-resync";
7626 action = "repair";
7627 } else
7628 desc = "resync";
7629 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7630 desc = "reshape";
7631 else
7632 desc = "recovery";
7634 mddev->last_sync_action = action ?: desc;
7636 /* we overload curr_resync somewhat here.
7637 * 0 == not engaged in resync at all
7638 * 2 == checking that there is no conflict with another sync
7639 * 1 == like 2, but have yielded to allow conflicting resync to
7640 * commense
7641 * other == active in resync - this many blocks
7643 * Before starting a resync we must have set curr_resync to
7644 * 2, and then checked that every "conflicting" array has curr_resync
7645 * less than ours. When we find one that is the same or higher
7646 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7647 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7648 * This will mean we have to start checking from the beginning again.
7652 do {
7653 mddev->curr_resync = 2;
7655 try_again:
7656 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7657 goto skip;
7658 for_each_mddev(mddev2, tmp) {
7659 if (mddev2 == mddev)
7660 continue;
7661 if (!mddev->parallel_resync
7662 && mddev2->curr_resync
7663 && match_mddev_units(mddev, mddev2)) {
7664 DEFINE_WAIT(wq);
7665 if (mddev < mddev2 && mddev->curr_resync == 2) {
7666 /* arbitrarily yield */
7667 mddev->curr_resync = 1;
7668 wake_up(&resync_wait);
7670 if (mddev > mddev2 && mddev->curr_resync == 1)
7671 /* no need to wait here, we can wait the next
7672 * time 'round when curr_resync == 2
7674 continue;
7675 /* We need to wait 'interruptible' so as not to
7676 * contribute to the load average, and not to
7677 * be caught by 'softlockup'
7679 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
7680 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7681 mddev2->curr_resync >= mddev->curr_resync) {
7682 printk(KERN_INFO "md: delaying %s of %s"
7683 " until %s has finished (they"
7684 " share one or more physical units)\n",
7685 desc, mdname(mddev), mdname(mddev2));
7686 mddev_put(mddev2);
7687 if (signal_pending(current))
7688 flush_signals(current);
7689 schedule();
7690 finish_wait(&resync_wait, &wq);
7691 goto try_again;
7693 finish_wait(&resync_wait, &wq);
7696 } while (mddev->curr_resync < 2);
7698 j = 0;
7699 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7700 /* resync follows the size requested by the personality,
7701 * which defaults to physical size, but can be virtual size
7703 max_sectors = mddev->resync_max_sectors;
7704 atomic64_set(&mddev->resync_mismatches, 0);
7705 /* we don't use the checkpoint if there's a bitmap */
7706 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7707 j = mddev->resync_min;
7708 else if (!mddev->bitmap)
7709 j = mddev->recovery_cp;
7711 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7712 max_sectors = mddev->resync_max_sectors;
7713 else {
7714 /* recovery follows the physical size of devices */
7715 max_sectors = mddev->dev_sectors;
7716 j = MaxSector;
7717 rcu_read_lock();
7718 rdev_for_each_rcu(rdev, mddev)
7719 if (rdev->raid_disk >= 0 &&
7720 !test_bit(Faulty, &rdev->flags) &&
7721 !test_bit(In_sync, &rdev->flags) &&
7722 rdev->recovery_offset < j)
7723 j = rdev->recovery_offset;
7724 rcu_read_unlock();
7726 /* If there is a bitmap, we need to make sure all
7727 * writes that started before we added a spare
7728 * complete before we start doing a recovery.
7729 * Otherwise the write might complete and (via
7730 * bitmap_endwrite) set a bit in the bitmap after the
7731 * recovery has checked that bit and skipped that
7732 * region.
7734 if (mddev->bitmap) {
7735 mddev->pers->quiesce(mddev, 1);
7736 mddev->pers->quiesce(mddev, 0);
7740 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7741 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7742 " %d KB/sec/disk.\n", speed_min(mddev));
7743 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
7744 "(but not more than %d KB/sec) for %s.\n",
7745 speed_max(mddev), desc);
7747 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
7749 io_sectors = 0;
7750 for (m = 0; m < SYNC_MARKS; m++) {
7751 mark[m] = jiffies;
7752 mark_cnt[m] = io_sectors;
7754 last_mark = 0;
7755 mddev->resync_mark = mark[last_mark];
7756 mddev->resync_mark_cnt = mark_cnt[last_mark];
7759 * Tune reconstruction:
7761 window = 32*(PAGE_SIZE/512);
7762 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7763 window/2, (unsigned long long)max_sectors/2);
7765 atomic_set(&mddev->recovery_active, 0);
7766 last_check = 0;
7768 if (j>2) {
7769 printk(KERN_INFO
7770 "md: resuming %s of %s from checkpoint.\n",
7771 desc, mdname(mddev));
7772 mddev->curr_resync = j;
7773 } else
7774 mddev->curr_resync = 3; /* no longer delayed */
7775 mddev->curr_resync_completed = j;
7776 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7777 md_new_event(mddev);
7778 update_time = jiffies;
7780 if (mddev_is_clustered(mddev))
7781 md_cluster_ops->resync_start(mddev, j, max_sectors);
7783 blk_start_plug(&plug);
7784 while (j < max_sectors) {
7785 sector_t sectors;
7787 skipped = 0;
7789 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7790 ((mddev->curr_resync > mddev->curr_resync_completed &&
7791 (mddev->curr_resync - mddev->curr_resync_completed)
7792 > (max_sectors >> 4)) ||
7793 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
7794 (j - mddev->curr_resync_completed)*2
7795 >= mddev->resync_max - mddev->curr_resync_completed
7796 )) {
7797 /* time to update curr_resync_completed */
7798 wait_event(mddev->recovery_wait,
7799 atomic_read(&mddev->recovery_active) == 0);
7800 mddev->curr_resync_completed = j;
7801 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7802 j > mddev->recovery_cp)
7803 mddev->recovery_cp = j;
7804 update_time = jiffies;
7805 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
7806 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7809 while (j >= mddev->resync_max &&
7810 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7811 /* As this condition is controlled by user-space,
7812 * we can block indefinitely, so use '_interruptible'
7813 * to avoid triggering warnings.
7815 flush_signals(current); /* just in case */
7816 wait_event_interruptible(mddev->recovery_wait,
7817 mddev->resync_max > j
7818 || test_bit(MD_RECOVERY_INTR,
7819 &mddev->recovery));
7822 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7823 break;
7825 sectors = mddev->pers->sync_request(mddev, j, &skipped);
7826 if (sectors == 0) {
7827 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7828 break;
7831 if (!skipped) { /* actual IO requested */
7832 io_sectors += sectors;
7833 atomic_add(sectors, &mddev->recovery_active);
7836 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7837 break;
7839 j += sectors;
7840 if (j > 2)
7841 mddev->curr_resync = j;
7842 if (mddev_is_clustered(mddev))
7843 md_cluster_ops->resync_info_update(mddev, j, max_sectors);
7844 mddev->curr_mark_cnt = io_sectors;
7845 if (last_check == 0)
7846 /* this is the earliest that rebuild will be
7847 * visible in /proc/mdstat
7849 md_new_event(mddev);
7851 if (last_check + window > io_sectors || j == max_sectors)
7852 continue;
7854 last_check = io_sectors;
7855 repeat:
7856 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7857 /* step marks */
7858 int next = (last_mark+1) % SYNC_MARKS;
7860 mddev->resync_mark = mark[next];
7861 mddev->resync_mark_cnt = mark_cnt[next];
7862 mark[next] = jiffies;
7863 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
7864 last_mark = next;
7867 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7868 break;
7871 * this loop exits only if either when we are slower than
7872 * the 'hard' speed limit, or the system was IO-idle for
7873 * a jiffy.
7874 * the system might be non-idle CPU-wise, but we only care
7875 * about not overloading the IO subsystem. (things like an
7876 * e2fsck being done on the RAID array should execute fast)
7878 cond_resched();
7880 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
7881 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
7882 /((jiffies-mddev->resync_mark)/HZ +1) +1;
7884 if (currspeed > speed_min(mddev)) {
7885 if (currspeed > speed_max(mddev)) {
7886 msleep(500);
7887 goto repeat;
7889 if (!is_mddev_idle(mddev, 0)) {
7891 * Give other IO more of a chance.
7892 * The faster the devices, the less we wait.
7894 wait_event(mddev->recovery_wait,
7895 !atomic_read(&mddev->recovery_active));
7899 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
7900 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
7901 ? "interrupted" : "done");
7903 * this also signals 'finished resyncing' to md_stop
7905 blk_finish_plug(&plug);
7906 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7908 /* tell personality that we are finished */
7909 mddev->pers->sync_request(mddev, max_sectors, &skipped);
7911 if (mddev_is_clustered(mddev))
7912 md_cluster_ops->resync_finish(mddev);
7914 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
7915 mddev->curr_resync > 2) {
7916 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7917 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7918 if (mddev->curr_resync >= mddev->recovery_cp) {
7919 printk(KERN_INFO
7920 "md: checkpointing %s of %s.\n",
7921 desc, mdname(mddev));
7922 if (test_bit(MD_RECOVERY_ERROR,
7923 &mddev->recovery))
7924 mddev->recovery_cp =
7925 mddev->curr_resync_completed;
7926 else
7927 mddev->recovery_cp =
7928 mddev->curr_resync;
7930 } else
7931 mddev->recovery_cp = MaxSector;
7932 } else {
7933 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7934 mddev->curr_resync = MaxSector;
7935 rcu_read_lock();
7936 rdev_for_each_rcu(rdev, mddev)
7937 if (rdev->raid_disk >= 0 &&
7938 mddev->delta_disks >= 0 &&
7939 !test_bit(Faulty, &rdev->flags) &&
7940 !test_bit(In_sync, &rdev->flags) &&
7941 rdev->recovery_offset < mddev->curr_resync)
7942 rdev->recovery_offset = mddev->curr_resync;
7943 rcu_read_unlock();
7946 skip:
7947 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7949 spin_lock(&mddev->lock);
7950 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7951 /* We completed so min/max setting can be forgotten if used. */
7952 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7953 mddev->resync_min = 0;
7954 mddev->resync_max = MaxSector;
7955 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7956 mddev->resync_min = mddev->curr_resync_completed;
7957 mddev->curr_resync = 0;
7958 spin_unlock(&mddev->lock);
7960 wake_up(&resync_wait);
7961 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7962 md_wakeup_thread(mddev->thread);
7963 return;
7965 EXPORT_SYMBOL_GPL(md_do_sync);
7967 static int remove_and_add_spares(struct mddev *mddev,
7968 struct md_rdev *this)
7970 struct md_rdev *rdev;
7971 int spares = 0;
7972 int removed = 0;
7974 rdev_for_each(rdev, mddev)
7975 if ((this == NULL || rdev == this) &&
7976 rdev->raid_disk >= 0 &&
7977 !test_bit(Blocked, &rdev->flags) &&
7978 (test_bit(Faulty, &rdev->flags) ||
7979 ! test_bit(In_sync, &rdev->flags)) &&
7980 atomic_read(&rdev->nr_pending)==0) {
7981 if (mddev->pers->hot_remove_disk(
7982 mddev, rdev) == 0) {
7983 sysfs_unlink_rdev(mddev, rdev);
7984 rdev->raid_disk = -1;
7985 removed++;
7988 if (removed && mddev->kobj.sd)
7989 sysfs_notify(&mddev->kobj, NULL, "degraded");
7991 if (this)
7992 goto no_add;
7994 rdev_for_each(rdev, mddev) {
7995 if (rdev->raid_disk >= 0 &&
7996 !test_bit(In_sync, &rdev->flags) &&
7997 !test_bit(Faulty, &rdev->flags))
7998 spares++;
7999 if (rdev->raid_disk >= 0)
8000 continue;
8001 if (test_bit(Faulty, &rdev->flags))
8002 continue;
8003 if (mddev->ro &&
8004 ! (rdev->saved_raid_disk >= 0 &&
8005 !test_bit(Bitmap_sync, &rdev->flags)))
8006 continue;
8008 if (rdev->saved_raid_disk < 0)
8009 rdev->recovery_offset = 0;
8010 if (mddev->pers->
8011 hot_add_disk(mddev, rdev) == 0) {
8012 if (sysfs_link_rdev(mddev, rdev))
8013 /* failure here is OK */;
8014 spares++;
8015 md_new_event(mddev);
8016 set_bit(MD_CHANGE_DEVS, &mddev->flags);
8019 no_add:
8020 if (removed)
8021 set_bit(MD_CHANGE_DEVS, &mddev->flags);
8022 return spares;
8025 static void md_start_sync(struct work_struct *ws)
8027 struct mddev *mddev = container_of(ws, struct mddev, del_work);
8029 mddev->sync_thread = md_register_thread(md_do_sync,
8030 mddev,
8031 "resync");
8032 if (!mddev->sync_thread) {
8033 printk(KERN_ERR "%s: could not start resync"
8034 " thread...\n",
8035 mdname(mddev));
8036 /* leave the spares where they are, it shouldn't hurt */
8037 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8038 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8039 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8040 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8041 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
8042 wake_up(&resync_wait);
8043 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8044 &mddev->recovery))
8045 if (mddev->sysfs_action)
8046 sysfs_notify_dirent_safe(mddev->sysfs_action);
8047 } else
8048 md_wakeup_thread(mddev->sync_thread);
8049 sysfs_notify_dirent_safe(mddev->sysfs_action);
8050 md_new_event(mddev);
8054 * This routine is regularly called by all per-raid-array threads to
8055 * deal with generic issues like resync and super-block update.
8056 * Raid personalities that don't have a thread (linear/raid0) do not
8057 * need this as they never do any recovery or update the superblock.
8059 * It does not do any resync itself, but rather "forks" off other threads
8060 * to do that as needed.
8061 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8062 * "->recovery" and create a thread at ->sync_thread.
8063 * When the thread finishes it sets MD_RECOVERY_DONE
8064 * and wakeups up this thread which will reap the thread and finish up.
8065 * This thread also removes any faulty devices (with nr_pending == 0).
8067 * The overall approach is:
8068 * 1/ if the superblock needs updating, update it.
8069 * 2/ If a recovery thread is running, don't do anything else.
8070 * 3/ If recovery has finished, clean up, possibly marking spares active.
8071 * 4/ If there are any faulty devices, remove them.
8072 * 5/ If array is degraded, try to add spares devices
8073 * 6/ If array has spares or is not in-sync, start a resync thread.
8075 void md_check_recovery(struct mddev *mddev)
8077 if (mddev->suspended)
8078 return;
8080 if (mddev->bitmap)
8081 bitmap_daemon_work(mddev);
8083 if (signal_pending(current)) {
8084 if (mddev->pers->sync_request && !mddev->external) {
8085 printk(KERN_INFO "md: %s in immediate safe mode\n",
8086 mdname(mddev));
8087 mddev->safemode = 2;
8089 flush_signals(current);
8092 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8093 return;
8094 if ( ! (
8095 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
8096 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8097 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
8098 (mddev->external == 0 && mddev->safemode == 1) ||
8099 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
8100 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
8102 return;
8104 if (mddev_trylock(mddev)) {
8105 int spares = 0;
8107 if (mddev->ro) {
8108 /* On a read-only array we can:
8109 * - remove failed devices
8110 * - add already-in_sync devices if the array itself
8111 * is in-sync.
8112 * As we only add devices that are already in-sync,
8113 * we can activate the spares immediately.
8115 remove_and_add_spares(mddev, NULL);
8116 /* There is no thread, but we need to call
8117 * ->spare_active and clear saved_raid_disk
8119 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
8120 md_reap_sync_thread(mddev);
8121 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8122 goto unlock;
8125 if (!mddev->external) {
8126 int did_change = 0;
8127 spin_lock(&mddev->lock);
8128 if (mddev->safemode &&
8129 !atomic_read(&mddev->writes_pending) &&
8130 !mddev->in_sync &&
8131 mddev->recovery_cp == MaxSector) {
8132 mddev->in_sync = 1;
8133 did_change = 1;
8134 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
8136 if (mddev->safemode == 1)
8137 mddev->safemode = 0;
8138 spin_unlock(&mddev->lock);
8139 if (did_change)
8140 sysfs_notify_dirent_safe(mddev->sysfs_state);
8143 if (mddev->flags & MD_UPDATE_SB_FLAGS) {
8144 if (mddev_is_clustered(mddev))
8145 md_cluster_ops->metadata_update_start(mddev);
8146 md_update_sb(mddev, 0);
8147 if (mddev_is_clustered(mddev))
8148 md_cluster_ops->metadata_update_finish(mddev);
8151 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8152 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8153 /* resync/recovery still happening */
8154 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8155 goto unlock;
8157 if (mddev->sync_thread) {
8158 md_reap_sync_thread(mddev);
8159 goto unlock;
8161 /* Set RUNNING before clearing NEEDED to avoid
8162 * any transients in the value of "sync_action".
8164 mddev->curr_resync_completed = 0;
8165 spin_lock(&mddev->lock);
8166 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
8167 spin_unlock(&mddev->lock);
8168 /* Clear some bits that don't mean anything, but
8169 * might be left set
8171 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8172 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
8174 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8175 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
8176 goto not_running;
8177 /* no recovery is running.
8178 * remove any failed drives, then
8179 * add spares if possible.
8180 * Spares are also removed and re-added, to allow
8181 * the personality to fail the re-add.
8184 if (mddev->reshape_position != MaxSector) {
8185 if (mddev->pers->check_reshape == NULL ||
8186 mddev->pers->check_reshape(mddev) != 0)
8187 /* Cannot proceed */
8188 goto not_running;
8189 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8190 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
8191 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
8192 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8193 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8194 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8195 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
8196 } else if (mddev->recovery_cp < MaxSector) {
8197 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8198 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
8199 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8200 /* nothing to be done ... */
8201 goto not_running;
8203 if (mddev->pers->sync_request) {
8204 if (spares) {
8205 /* We are adding a device or devices to an array
8206 * which has the bitmap stored on all devices.
8207 * So make sure all bitmap pages get written
8209 bitmap_write_all(mddev->bitmap);
8211 INIT_WORK(&mddev->del_work, md_start_sync);
8212 queue_work(md_misc_wq, &mddev->del_work);
8213 goto unlock;
8215 not_running:
8216 if (!mddev->sync_thread) {
8217 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
8218 wake_up(&resync_wait);
8219 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8220 &mddev->recovery))
8221 if (mddev->sysfs_action)
8222 sysfs_notify_dirent_safe(mddev->sysfs_action);
8224 unlock:
8225 wake_up(&mddev->sb_wait);
8226 mddev_unlock(mddev);
8229 EXPORT_SYMBOL(md_check_recovery);
8231 void md_reap_sync_thread(struct mddev *mddev)
8233 struct md_rdev *rdev;
8235 /* resync has finished, collect result */
8236 md_unregister_thread(&mddev->sync_thread);
8237 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8238 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8239 /* success...*/
8240 /* activate any spares */
8241 if (mddev->pers->spare_active(mddev)) {
8242 sysfs_notify(&mddev->kobj, NULL,
8243 "degraded");
8244 set_bit(MD_CHANGE_DEVS, &mddev->flags);
8247 if (mddev_is_clustered(mddev))
8248 md_cluster_ops->metadata_update_start(mddev);
8249 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8250 mddev->pers->finish_reshape)
8251 mddev->pers->finish_reshape(mddev);
8253 /* If array is no-longer degraded, then any saved_raid_disk
8254 * information must be scrapped.
8256 if (!mddev->degraded)
8257 rdev_for_each(rdev, mddev)
8258 rdev->saved_raid_disk = -1;
8260 md_update_sb(mddev, 1);
8261 if (mddev_is_clustered(mddev))
8262 md_cluster_ops->metadata_update_finish(mddev);
8263 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
8264 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8265 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8266 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8267 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8268 wake_up(&resync_wait);
8269 /* flag recovery needed just to double check */
8270 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8271 sysfs_notify_dirent_safe(mddev->sysfs_action);
8272 md_new_event(mddev);
8273 if (mddev->event_work.func)
8274 queue_work(md_misc_wq, &mddev->event_work);
8276 EXPORT_SYMBOL(md_reap_sync_thread);
8278 void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
8280 sysfs_notify_dirent_safe(rdev->sysfs_state);
8281 wait_event_timeout(rdev->blocked_wait,
8282 !test_bit(Blocked, &rdev->flags) &&
8283 !test_bit(BlockedBadBlocks, &rdev->flags),
8284 msecs_to_jiffies(5000));
8285 rdev_dec_pending(rdev, mddev);
8287 EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8289 void md_finish_reshape(struct mddev *mddev)
8291 /* called be personality module when reshape completes. */
8292 struct md_rdev *rdev;
8294 rdev_for_each(rdev, mddev) {
8295 if (rdev->data_offset > rdev->new_data_offset)
8296 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8297 else
8298 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8299 rdev->data_offset = rdev->new_data_offset;
8302 EXPORT_SYMBOL(md_finish_reshape);
8304 /* Bad block management.
8305 * We can record which blocks on each device are 'bad' and so just
8306 * fail those blocks, or that stripe, rather than the whole device.
8307 * Entries in the bad-block table are 64bits wide. This comprises:
8308 * Length of bad-range, in sectors: 0-511 for lengths 1-512
8309 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
8310 * A 'shift' can be set so that larger blocks are tracked and
8311 * consequently larger devices can be covered.
8312 * 'Acknowledged' flag - 1 bit. - the most significant bit.
8314 * Locking of the bad-block table uses a seqlock so md_is_badblock
8315 * might need to retry if it is very unlucky.
8316 * We will sometimes want to check for bad blocks in a bi_end_io function,
8317 * so we use the write_seqlock_irq variant.
8319 * When looking for a bad block we specify a range and want to
8320 * know if any block in the range is bad. So we binary-search
8321 * to the last range that starts at-or-before the given endpoint,
8322 * (or "before the sector after the target range")
8323 * then see if it ends after the given start.
8324 * We return
8325 * 0 if there are no known bad blocks in the range
8326 * 1 if there are known bad block which are all acknowledged
8327 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
8328 * plus the start/length of the first bad section we overlap.
8330 int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
8331 sector_t *first_bad, int *bad_sectors)
8333 int hi;
8334 int lo;
8335 u64 *p = bb->page;
8336 int rv;
8337 sector_t target = s + sectors;
8338 unsigned seq;
8340 if (bb->shift > 0) {
8341 /* round the start down, and the end up */
8342 s >>= bb->shift;
8343 target += (1<<bb->shift) - 1;
8344 target >>= bb->shift;
8345 sectors = target - s;
8347 /* 'target' is now the first block after the bad range */
8349 retry:
8350 seq = read_seqbegin(&bb->lock);
8351 lo = 0;
8352 rv = 0;
8353 hi = bb->count;
8355 /* Binary search between lo and hi for 'target'
8356 * i.e. for the last range that starts before 'target'
8358 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8359 * are known not to be the last range before target.
8360 * VARIANT: hi-lo is the number of possible
8361 * ranges, and decreases until it reaches 1
8363 while (hi - lo > 1) {
8364 int mid = (lo + hi) / 2;
8365 sector_t a = BB_OFFSET(p[mid]);
8366 if (a < target)
8367 /* This could still be the one, earlier ranges
8368 * could not. */
8369 lo = mid;
8370 else
8371 /* This and later ranges are definitely out. */
8372 hi = mid;
8374 /* 'lo' might be the last that started before target, but 'hi' isn't */
8375 if (hi > lo) {
8376 /* need to check all range that end after 's' to see if
8377 * any are unacknowledged.
8379 while (lo >= 0 &&
8380 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8381 if (BB_OFFSET(p[lo]) < target) {
8382 /* starts before the end, and finishes after
8383 * the start, so they must overlap
8385 if (rv != -1 && BB_ACK(p[lo]))
8386 rv = 1;
8387 else
8388 rv = -1;
8389 *first_bad = BB_OFFSET(p[lo]);
8390 *bad_sectors = BB_LEN(p[lo]);
8392 lo--;
8396 if (read_seqretry(&bb->lock, seq))
8397 goto retry;
8399 return rv;
8401 EXPORT_SYMBOL_GPL(md_is_badblock);
8404 * Add a range of bad blocks to the table.
8405 * This might extend the table, or might contract it
8406 * if two adjacent ranges can be merged.
8407 * We binary-search to find the 'insertion' point, then
8408 * decide how best to handle it.
8410 static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8411 int acknowledged)
8413 u64 *p;
8414 int lo, hi;
8415 int rv = 1;
8416 unsigned long flags;
8418 if (bb->shift < 0)
8419 /* badblocks are disabled */
8420 return 0;
8422 if (bb->shift) {
8423 /* round the start down, and the end up */
8424 sector_t next = s + sectors;
8425 s >>= bb->shift;
8426 next += (1<<bb->shift) - 1;
8427 next >>= bb->shift;
8428 sectors = next - s;
8431 write_seqlock_irqsave(&bb->lock, flags);
8433 p = bb->page;
8434 lo = 0;
8435 hi = bb->count;
8436 /* Find the last range that starts at-or-before 's' */
8437 while (hi - lo > 1) {
8438 int mid = (lo + hi) / 2;
8439 sector_t a = BB_OFFSET(p[mid]);
8440 if (a <= s)
8441 lo = mid;
8442 else
8443 hi = mid;
8445 if (hi > lo && BB_OFFSET(p[lo]) > s)
8446 hi = lo;
8448 if (hi > lo) {
8449 /* we found a range that might merge with the start
8450 * of our new range
8452 sector_t a = BB_OFFSET(p[lo]);
8453 sector_t e = a + BB_LEN(p[lo]);
8454 int ack = BB_ACK(p[lo]);
8455 if (e >= s) {
8456 /* Yes, we can merge with a previous range */
8457 if (s == a && s + sectors >= e)
8458 /* new range covers old */
8459 ack = acknowledged;
8460 else
8461 ack = ack && acknowledged;
8463 if (e < s + sectors)
8464 e = s + sectors;
8465 if (e - a <= BB_MAX_LEN) {
8466 p[lo] = BB_MAKE(a, e-a, ack);
8467 s = e;
8468 } else {
8469 /* does not all fit in one range,
8470 * make p[lo] maximal
8472 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8473 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8474 s = a + BB_MAX_LEN;
8476 sectors = e - s;
8479 if (sectors && hi < bb->count) {
8480 /* 'hi' points to the first range that starts after 's'.
8481 * Maybe we can merge with the start of that range */
8482 sector_t a = BB_OFFSET(p[hi]);
8483 sector_t e = a + BB_LEN(p[hi]);
8484 int ack = BB_ACK(p[hi]);
8485 if (a <= s + sectors) {
8486 /* merging is possible */
8487 if (e <= s + sectors) {
8488 /* full overlap */
8489 e = s + sectors;
8490 ack = acknowledged;
8491 } else
8492 ack = ack && acknowledged;
8494 a = s;
8495 if (e - a <= BB_MAX_LEN) {
8496 p[hi] = BB_MAKE(a, e-a, ack);
8497 s = e;
8498 } else {
8499 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8500 s = a + BB_MAX_LEN;
8502 sectors = e - s;
8503 lo = hi;
8504 hi++;
8507 if (sectors == 0 && hi < bb->count) {
8508 /* we might be able to combine lo and hi */
8509 /* Note: 's' is at the end of 'lo' */
8510 sector_t a = BB_OFFSET(p[hi]);
8511 int lolen = BB_LEN(p[lo]);
8512 int hilen = BB_LEN(p[hi]);
8513 int newlen = lolen + hilen - (s - a);
8514 if (s >= a && newlen < BB_MAX_LEN) {
8515 /* yes, we can combine them */
8516 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8517 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8518 memmove(p + hi, p + hi + 1,
8519 (bb->count - hi - 1) * 8);
8520 bb->count--;
8523 while (sectors) {
8524 /* didn't merge (it all).
8525 * Need to add a range just before 'hi' */
8526 if (bb->count >= MD_MAX_BADBLOCKS) {
8527 /* No room for more */
8528 rv = 0;
8529 break;
8530 } else {
8531 int this_sectors = sectors;
8532 memmove(p + hi + 1, p + hi,
8533 (bb->count - hi) * 8);
8534 bb->count++;
8536 if (this_sectors > BB_MAX_LEN)
8537 this_sectors = BB_MAX_LEN;
8538 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8539 sectors -= this_sectors;
8540 s += this_sectors;
8544 bb->changed = 1;
8545 if (!acknowledged)
8546 bb->unacked_exist = 1;
8547 write_sequnlock_irqrestore(&bb->lock, flags);
8549 return rv;
8552 int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8553 int is_new)
8555 int rv;
8556 if (is_new)
8557 s += rdev->new_data_offset;
8558 else
8559 s += rdev->data_offset;
8560 rv = md_set_badblocks(&rdev->badblocks,
8561 s, sectors, 0);
8562 if (rv) {
8563 /* Make sure they get written out promptly */
8564 sysfs_notify_dirent_safe(rdev->sysfs_state);
8565 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8566 md_wakeup_thread(rdev->mddev->thread);
8568 return rv;
8570 EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8573 * Remove a range of bad blocks from the table.
8574 * This may involve extending the table if we spilt a region,
8575 * but it must not fail. So if the table becomes full, we just
8576 * drop the remove request.
8578 static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8580 u64 *p;
8581 int lo, hi;
8582 sector_t target = s + sectors;
8583 int rv = 0;
8585 if (bb->shift > 0) {
8586 /* When clearing we round the start up and the end down.
8587 * This should not matter as the shift should align with
8588 * the block size and no rounding should ever be needed.
8589 * However it is better the think a block is bad when it
8590 * isn't than to think a block is not bad when it is.
8592 s += (1<<bb->shift) - 1;
8593 s >>= bb->shift;
8594 target >>= bb->shift;
8595 sectors = target - s;
8598 write_seqlock_irq(&bb->lock);
8600 p = bb->page;
8601 lo = 0;
8602 hi = bb->count;
8603 /* Find the last range that starts before 'target' */
8604 while (hi - lo > 1) {
8605 int mid = (lo + hi) / 2;
8606 sector_t a = BB_OFFSET(p[mid]);
8607 if (a < target)
8608 lo = mid;
8609 else
8610 hi = mid;
8612 if (hi > lo) {
8613 /* p[lo] is the last range that could overlap the
8614 * current range. Earlier ranges could also overlap,
8615 * but only this one can overlap the end of the range.
8617 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8618 /* Partial overlap, leave the tail of this range */
8619 int ack = BB_ACK(p[lo]);
8620 sector_t a = BB_OFFSET(p[lo]);
8621 sector_t end = a + BB_LEN(p[lo]);
8623 if (a < s) {
8624 /* we need to split this range */
8625 if (bb->count >= MD_MAX_BADBLOCKS) {
8626 rv = -ENOSPC;
8627 goto out;
8629 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8630 bb->count++;
8631 p[lo] = BB_MAKE(a, s-a, ack);
8632 lo++;
8634 p[lo] = BB_MAKE(target, end - target, ack);
8635 /* there is no longer an overlap */
8636 hi = lo;
8637 lo--;
8639 while (lo >= 0 &&
8640 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8641 /* This range does overlap */
8642 if (BB_OFFSET(p[lo]) < s) {
8643 /* Keep the early parts of this range. */
8644 int ack = BB_ACK(p[lo]);
8645 sector_t start = BB_OFFSET(p[lo]);
8646 p[lo] = BB_MAKE(start, s - start, ack);
8647 /* now low doesn't overlap, so.. */
8648 break;
8650 lo--;
8652 /* 'lo' is strictly before, 'hi' is strictly after,
8653 * anything between needs to be discarded
8655 if (hi - lo > 1) {
8656 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8657 bb->count -= (hi - lo - 1);
8661 bb->changed = 1;
8662 out:
8663 write_sequnlock_irq(&bb->lock);
8664 return rv;
8667 int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8668 int is_new)
8670 if (is_new)
8671 s += rdev->new_data_offset;
8672 else
8673 s += rdev->data_offset;
8674 return md_clear_badblocks(&rdev->badblocks,
8675 s, sectors);
8677 EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8680 * Acknowledge all bad blocks in a list.
8681 * This only succeeds if ->changed is clear. It is used by
8682 * in-kernel metadata updates
8684 void md_ack_all_badblocks(struct badblocks *bb)
8686 if (bb->page == NULL || bb->changed)
8687 /* no point even trying */
8688 return;
8689 write_seqlock_irq(&bb->lock);
8691 if (bb->changed == 0 && bb->unacked_exist) {
8692 u64 *p = bb->page;
8693 int i;
8694 for (i = 0; i < bb->count ; i++) {
8695 if (!BB_ACK(p[i])) {
8696 sector_t start = BB_OFFSET(p[i]);
8697 int len = BB_LEN(p[i]);
8698 p[i] = BB_MAKE(start, len, 1);
8701 bb->unacked_exist = 0;
8703 write_sequnlock_irq(&bb->lock);
8705 EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8707 /* sysfs access to bad-blocks list.
8708 * We present two files.
8709 * 'bad-blocks' lists sector numbers and lengths of ranges that
8710 * are recorded as bad. The list is truncated to fit within
8711 * the one-page limit of sysfs.
8712 * Writing "sector length" to this file adds an acknowledged
8713 * bad block list.
8714 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8715 * been acknowledged. Writing to this file adds bad blocks
8716 * without acknowledging them. This is largely for testing.
8719 static ssize_t
8720 badblocks_show(struct badblocks *bb, char *page, int unack)
8722 size_t len;
8723 int i;
8724 u64 *p = bb->page;
8725 unsigned seq;
8727 if (bb->shift < 0)
8728 return 0;
8730 retry:
8731 seq = read_seqbegin(&bb->lock);
8733 len = 0;
8734 i = 0;
8736 while (len < PAGE_SIZE && i < bb->count) {
8737 sector_t s = BB_OFFSET(p[i]);
8738 unsigned int length = BB_LEN(p[i]);
8739 int ack = BB_ACK(p[i]);
8740 i++;
8742 if (unack && ack)
8743 continue;
8745 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8746 (unsigned long long)s << bb->shift,
8747 length << bb->shift);
8749 if (unack && len == 0)
8750 bb->unacked_exist = 0;
8752 if (read_seqretry(&bb->lock, seq))
8753 goto retry;
8755 return len;
8758 #define DO_DEBUG 1
8760 static ssize_t
8761 badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8763 unsigned long long sector;
8764 int length;
8765 char newline;
8766 #ifdef DO_DEBUG
8767 /* Allow clearing via sysfs *only* for testing/debugging.
8768 * Normally only a successful write may clear a badblock
8770 int clear = 0;
8771 if (page[0] == '-') {
8772 clear = 1;
8773 page++;
8775 #endif /* DO_DEBUG */
8777 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8778 case 3:
8779 if (newline != '\n')
8780 return -EINVAL;
8781 case 2:
8782 if (length <= 0)
8783 return -EINVAL;
8784 break;
8785 default:
8786 return -EINVAL;
8789 #ifdef DO_DEBUG
8790 if (clear) {
8791 md_clear_badblocks(bb, sector, length);
8792 return len;
8794 #endif /* DO_DEBUG */
8795 if (md_set_badblocks(bb, sector, length, !unack))
8796 return len;
8797 else
8798 return -ENOSPC;
8801 static int md_notify_reboot(struct notifier_block *this,
8802 unsigned long code, void *x)
8804 struct list_head *tmp;
8805 struct mddev *mddev;
8806 int need_delay = 0;
8808 for_each_mddev(mddev, tmp) {
8809 if (mddev_trylock(mddev)) {
8810 if (mddev->pers)
8811 __md_stop_writes(mddev);
8812 if (mddev->persistent)
8813 mddev->safemode = 2;
8814 mddev_unlock(mddev);
8816 need_delay = 1;
8819 * certain more exotic SCSI devices are known to be
8820 * volatile wrt too early system reboots. While the
8821 * right place to handle this issue is the given
8822 * driver, we do want to have a safe RAID driver ...
8824 if (need_delay)
8825 mdelay(1000*1);
8827 return NOTIFY_DONE;
8830 static struct notifier_block md_notifier = {
8831 .notifier_call = md_notify_reboot,
8832 .next = NULL,
8833 .priority = INT_MAX, /* before any real devices */
8836 static void md_geninit(void)
8838 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
8840 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
8843 static int __init md_init(void)
8845 int ret = -ENOMEM;
8847 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
8848 if (!md_wq)
8849 goto err_wq;
8851 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8852 if (!md_misc_wq)
8853 goto err_misc_wq;
8855 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8856 goto err_md;
8858 if ((ret = register_blkdev(0, "mdp")) < 0)
8859 goto err_mdp;
8860 mdp_major = ret;
8862 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
8863 md_probe, NULL, NULL);
8864 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
8865 md_probe, NULL, NULL);
8867 register_reboot_notifier(&md_notifier);
8868 raid_table_header = register_sysctl_table(raid_root_table);
8870 md_geninit();
8871 return 0;
8873 err_mdp:
8874 unregister_blkdev(MD_MAJOR, "md");
8875 err_md:
8876 destroy_workqueue(md_misc_wq);
8877 err_misc_wq:
8878 destroy_workqueue(md_wq);
8879 err_wq:
8880 return ret;
8883 void md_reload_sb(struct mddev *mddev)
8885 struct md_rdev *rdev, *tmp;
8887 rdev_for_each_safe(rdev, tmp, mddev) {
8888 rdev->sb_loaded = 0;
8889 ClearPageUptodate(rdev->sb_page);
8891 mddev->raid_disks = 0;
8892 analyze_sbs(mddev);
8893 rdev_for_each_safe(rdev, tmp, mddev) {
8894 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
8895 /* since we don't write to faulty devices, we figure out if the
8896 * disk is faulty by comparing events
8898 if (mddev->events > sb->events)
8899 set_bit(Faulty, &rdev->flags);
8903 EXPORT_SYMBOL(md_reload_sb);
8905 #ifndef MODULE
8908 * Searches all registered partitions for autorun RAID arrays
8909 * at boot time.
8912 static LIST_HEAD(all_detected_devices);
8913 struct detected_devices_node {
8914 struct list_head list;
8915 dev_t dev;
8918 void md_autodetect_dev(dev_t dev)
8920 struct detected_devices_node *node_detected_dev;
8922 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8923 if (node_detected_dev) {
8924 node_detected_dev->dev = dev;
8925 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8926 } else {
8927 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8928 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8932 static void autostart_arrays(int part)
8934 struct md_rdev *rdev;
8935 struct detected_devices_node *node_detected_dev;
8936 dev_t dev;
8937 int i_scanned, i_passed;
8939 i_scanned = 0;
8940 i_passed = 0;
8942 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8944 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8945 i_scanned++;
8946 node_detected_dev = list_entry(all_detected_devices.next,
8947 struct detected_devices_node, list);
8948 list_del(&node_detected_dev->list);
8949 dev = node_detected_dev->dev;
8950 kfree(node_detected_dev);
8951 rdev = md_import_device(dev,0, 90);
8952 if (IS_ERR(rdev))
8953 continue;
8955 if (test_bit(Faulty, &rdev->flags))
8956 continue;
8958 set_bit(AutoDetected, &rdev->flags);
8959 list_add(&rdev->same_set, &pending_raid_disks);
8960 i_passed++;
8963 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8964 i_scanned, i_passed);
8966 autorun_devices(part);
8969 #endif /* !MODULE */
8971 static __exit void md_exit(void)
8973 struct mddev *mddev;
8974 struct list_head *tmp;
8975 int delay = 1;
8977 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
8978 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
8980 unregister_blkdev(MD_MAJOR,"md");
8981 unregister_blkdev(mdp_major, "mdp");
8982 unregister_reboot_notifier(&md_notifier);
8983 unregister_sysctl_table(raid_table_header);
8985 /* We cannot unload the modules while some process is
8986 * waiting for us in select() or poll() - wake them up
8988 md_unloading = 1;
8989 while (waitqueue_active(&md_event_waiters)) {
8990 /* not safe to leave yet */
8991 wake_up(&md_event_waiters);
8992 msleep(delay);
8993 delay += delay;
8995 remove_proc_entry("mdstat", NULL);
8997 for_each_mddev(mddev, tmp) {
8998 export_array(mddev);
8999 mddev->hold_active = 0;
9001 destroy_workqueue(md_misc_wq);
9002 destroy_workqueue(md_wq);
9005 subsys_initcall(md_init);
9006 module_exit(md_exit)
9008 static int get_ro(char *buffer, struct kernel_param *kp)
9010 return sprintf(buffer, "%d", start_readonly);
9012 static int set_ro(const char *val, struct kernel_param *kp)
9014 char *e;
9015 int num = simple_strtoul(val, &e, 10);
9016 if (*val && (*e == '\0' || *e == '\n')) {
9017 start_readonly = num;
9018 return 0;
9020 return -EINVAL;
9023 module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9024 module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
9025 module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
9027 MODULE_LICENSE("GPL");
9028 MODULE_DESCRIPTION("MD RAID framework");
9029 MODULE_ALIAS("md");
9030 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);