2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
5 completely rewritten, based on the MD driver code from Marc Zyngier
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
13 - kmod support by: Cyrus Durgin
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
22 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
25 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include <linux/kthread.h>
36 #include <linux/blkdev.h>
37 #include <linux/sysctl.h>
38 #include <linux/seq_file.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>
56 #include "md-cluster.h"
59 static void autostart_arrays(int part);
62 /* pers_list is a list of registered personalities protected
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
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[] = {
140 .mode = S_IRUGO|S_IXUGO,
146 static struct ctl_table raid_root_table[] = {
151 .child = raid_dir_table,
156 static const struct block_device_operations md_fops;
158 static int start_readonly;
161 * like bio_clone, but with a local bio set
164 struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
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);
177 EXPORT_SYMBOL_GPL(bio_alloc_mddev);
179 struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
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
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; \
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;
260 if (mddev == NULL || mddev->pers == NULL
265 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
266 bio_endio(bio, bio_sectors(bio) == 0 ? 0 : -EROFS);
269 smp_rmb(); /* Ensure implications of 'active' are visible */
271 if (mddev->suspended) {
274 prepare_to_wait(&mddev->sb_wait, &__wait,
275 TASK_UNINTERRUPTIBLE);
276 if (!mddev->suspended)
282 finish_wait(&mddev->sb_wait, &__wait);
284 atomic_inc(&mddev->active_io);
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);
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
309 void mddev_suspend(struct mddev *mddev)
311 BUG_ON(mddev->suspended);
312 mddev->suspended = 1;
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;
339 if (mddev->suspended)
341 else if (pers && pers->congested)
342 ret = pers->congested(mddev, bits);
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;
360 if (mddev->suspended) {
361 /* Must always allow one vec */
362 if (bvm->bi_size == 0)
363 ret = biovec->bv_len;
367 struct md_personality *pers = mddev->pers;
368 if (pers && pers->mergeable_bvec)
369 ret = pers->mergeable_bvec(mddev, bvm, biovec);
371 ret = biovec->bv_len;
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);
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);
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
412 atomic_inc(&rdev->nr_pending);
413 atomic_inc(&rdev->nr_pending);
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);
422 rdev_dec_pending(rdev, mddev);
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 */
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,
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);
466 EXPORT_SYMBOL(md_unplug);
468 static inline struct mddev *mddev_get(struct mddev *mddev)
470 atomic_inc(&mddev->active);
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))
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,
486 list_del_init(&mddev->all_mddevs);
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);
500 spin_unlock(&all_mddevs_lock);
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);
537 spin_lock(&all_mddevs_lock);
540 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
541 if (mddev->unit == unit) {
543 spin_unlock(&all_mddevs_lock);
549 list_add(&new->all_mddevs, &all_mddevs);
550 spin_unlock(&all_mddevs_lock);
551 new->hold_active = UNTIL_IOCTL;
555 /* find an unused unit number */
556 static int next_minor = 512;
557 int start = next_minor;
561 dev = MKDEV(MD_MAJOR, next_minor);
563 if (next_minor > MINORMASK)
565 if (next_minor == start) {
566 /* Oh dear, all in use. */
567 spin_unlock(&all_mddevs_lock);
573 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
574 if (mddev->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);
586 spin_unlock(&all_mddevs_lock);
588 new = kzalloc(sizeof(*new), GFP_KERNEL);
593 if (MAJOR(unit) == MD_MAJOR)
594 new->md_minor = MINOR(unit);
596 new->md_minor = MINOR(unit) >> MdpMinorShift;
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
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
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;
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)
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)
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)
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)
689 if (strcmp(pers->name, clevel)==0)
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");
713 void md_rdev_clear(struct md_rdev *rdev)
716 put_page(rdev->sb_page);
718 rdev->sb_page = NULL;
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);
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);
781 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
782 rdev->meta_bdev : rdev->bdev;
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;
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);
798 EXPORT_SYMBOL_GPL(sync_page_io);
800 static int read_disk_sb(struct md_rdev *rdev, int size)
802 char b[BDEVNAME_SIZE];
807 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
813 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
814 bdevname(rdev->bdev,b));
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)
829 mdp_super_t *tmp1, *tmp2;
831 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
832 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
834 if (!tmp1 || !tmp2) {
836 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
844 * nr_disks is not constant
849 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
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)
865 u32 *sb32 = (u32*)sb;
867 unsigned int disk_csum, csum;
869 disk_csum = sb->sb_csum;
872 for (i = 0; i < MD_SB_BYTES/4 ; i++)
874 csum = (newcsum & 0xffffffff) + (newcsum>>32);
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);
887 sb->sb_csum = disk_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
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.
924 struct module *owner;
925 int (*load_super)(struct md_rdev *rdev,
926 struct md_rdev *refdev,
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)
950 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
951 mdname(mddev), mddev->pers->name);
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];
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);
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",
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,
996 if (sb->raid_disks <= 0)
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",
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)
1014 rdev->desc_nr = sb->this_disk.number;
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));
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));
1033 ev2 = md_event(refsb);
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
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" ... */
1056 * validate_super for 0.90.0
1058 static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
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;
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;
1110 if (sb->events_hi == sb->cp_events_hi &&
1111 sb->events_lo == sb->cp_events_lo) {
1112 mddev->recovery_cp = sb->recovery_cp;
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) */
1136 if (sb->disks[rdev->desc_nr].state & (
1137 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1138 if (ev1 < mddev->events)
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)
1146 if (ev1 < mddev->events)
1147 set_bit(Bitmap_sync, &rdev->flags);
1149 if (ev1 < mddev->events)
1150 /* just a hot-add of a new device, leave raid_disk at -1 */
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);
1181 * sync_super for 0.90.0
1183 static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
1186 struct md_rdev *rdev2;
1187 int next_spare = mddev->raid_disks;
1189 /* make rdev->sb match mddev data..
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.
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;
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;
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;
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);
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) {
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.
1268 if (rdev2->raid_disk < 0 ||
1269 test_bit(Faulty, &rdev2->flags))
1272 desc_nr = rdev2->raid_disk;
1274 desc_nr = next_spare++;
1275 rdev2->desc_nr = desc_nr;
1276 d = &sb->disks[rdev2->desc_nr];
1278 d->number = rdev2->desc_nr;
1279 d->major = MAJOR(rdev2->bdev->bd_dev);
1280 d->minor = MINOR(rdev2->bdev->bd_dev);
1282 d->raid_disk = rdev2->raid_disk;
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);
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) {
1307 d->state = (1<<MD_DISK_REMOVED);
1308 d->state |= (1<<MD_DISK_FAULTY);
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,
1342 md_super_wait(rdev->mddev);
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)
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;
1368 for (; size >= 4; size -= 4)
1369 newcsum += le32_to_cpu(*isuper++);
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,
1381 static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
1383 struct mdp_superblock_1 *sb;
1387 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
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) {
1400 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
1402 sb_start &= ~(sector_t)(4*2-1);
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)
1430 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1431 printk("md: invalid superblock checksum on %s\n",
1432 bdevname(rdev->bdev,b));
1435 if (le64_to_cpu(sb->data_size) < 10) {
1436 printk("md: data_size too small on %s\n",
1437 bdevname(rdev->bdev,b));
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 */
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;
1460 && rdev->data_offset < sb_start + (rdev->sb_size/512))
1463 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1466 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1469 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1471 if (!rdev->bb_page) {
1472 rdev->bb_page = alloc_page(GFP_KERNEL);
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.
1485 int sectors = le16_to_cpu(sb->bblog_size);
1486 if (sectors > (PAGE_SIZE / 512))
1488 offset = le32_to_cpu(sb->bblog_offset);
1491 bb_sector = (long long)offset;
1492 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1493 rdev->bb_page, READ, true))
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;
1505 if (md_set_badblocks(&rdev->badblocks,
1506 sector, count, 1) == 0)
1509 } else if (sb->bblog_offset != 0)
1510 rdev->badblocks.shift = 0;
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));
1528 ev1 = le64_to_cpu(sb->events);
1529 ev2 = le64_to_cpu(refsb->events);
1536 if (minor_version) {
1537 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1538 sectors -= rdev->data_offset;
1540 sectors = rdev->sb_start;
1541 if (sectors < le64_to_cpu(sb->data_size))
1543 rdev->sectors = le64_to_cpu(sb->data_size);
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;
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;
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) */
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)
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)
1638 if (ev1 < mddev->events)
1639 set_bit(Bitmap_sync, &rdev->flags);
1641 if (ev1 < mddev->events)
1642 /* just a hot-add of a new device, leave raid_disk at -1 */
1645 if (mddev->level != LEVEL_MULTIPATH) {
1647 if (rdev->desc_nr < 0 ||
1648 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1652 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1654 case 0xffff: /* spare */
1656 case 0xfffe: /* faulty */
1657 set_bit(Faulty, &rdev->flags);
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;
1668 set_bit(In_sync, &rdev->flags);
1669 rdev->raid_disk = role;
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);
1682 static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
1684 struct mdp_superblock_1 *sb;
1685 struct md_rdev *rdev2;
1687 /* make rdev->sb match mddev and rdev data. */
1689 sb = page_address(rdev->sb_page);
1691 sb->feature_map = 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);
1699 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
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;
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)) {
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)
1731 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
1733 if (test_bit(Replacement, &rdev->flags))
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)
1747 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
1748 if (rdev->new_data_offset != rdev->data_offset) {
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);
1762 struct badblocks *bb = &rdev->badblocks;
1763 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1765 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
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);
1781 if (read_seqretry(&bb->lock, seq))
1784 bb->sector = (rdev->sb_start +
1785 (int)le32_to_cpu(sb->bblog_offset));
1786 bb->size = le16_to_cpu(sb->bblog_size);
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)) {
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;
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) {
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);
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 */
1842 /* minor version 0; superblock after data */
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,
1857 md_super_wait(rdev->mddev);
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)
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)
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)
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)
1889 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1895 static struct super_type super_types[] = {
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,
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);
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;
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) {
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))
1965 if (rdev->raid_disk < 0)
1968 /* Use the first rdev as the reference */
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)
1977 if (!reference || !bdev_get_integrity(reference->bdev))
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",
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",
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)
2008 bi_rdev = bdev_get_integrity(rdev->bdev);
2009 bi_mddev = blk_get_integrity(mddev->gendisk);
2011 if (!bi_mddev) /* nothing to do */
2013 if (rdev->raid_disk < 0) /* skip spares */
2015 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2016 rdev->bdev->bd_disk) >= 0)
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];
2030 /* prevent duplicates */
2031 if (find_rdev(mddev, rdev->bdev->bd_dev))
2034 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2035 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2036 rdev->sectors < mddev->dev_sectors)) {
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)
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
2053 if (rdev->desc_nr < 0) {
2056 choice = mddev->raid_disks;
2057 while (md_find_rdev_nr_rcu(mddev, choice))
2059 rdev->desc_nr = choice;
2061 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
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);
2072 bdevname(rdev->bdev,b);
2073 while ( (s=strchr(b, '/')) != NULL)
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)))
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++;
2096 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
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));
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.
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)
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);
2144 printk(KERN_ERR "md: could not open %s.\n",
2145 __bdevname(dev, b));
2146 return PTR_ERR(bdev);
2152 static void unlock_rdev(struct md_rdev *rdev)
2154 struct block_device *bdev = rdev->bdev;
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);
2169 if (test_bit(AutoDetected, &rdev->flags))
2170 md_autodetect_dev(rdev->bdev->bd_dev);
2173 kobject_put(&rdev->kobj);
2176 void md_kick_rdev_from_array(struct md_rdev *rdev)
2178 unbind_rdev_from_array(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,
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 ||
2208 rdev->raid_disk < 0 &&
2209 rdev->sb_events+1 == mddev->events)) {
2210 /* Don't update this superblock */
2211 rdev->sb_loaded = 2;
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;
2224 int any_badblocks_changed = 0;
2228 set_bit(MD_CHANGE_DEVS, &mddev->flags);
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);
2261 spin_lock(&mddev->lock);
2263 mddev->utime = get_seconds();
2265 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
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
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.
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 */
2292 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
2293 && mddev->can_decrease_events
2294 && mddev->events != 1) {
2296 mddev->can_decrease_events = 0;
2298 /* otherwise we have to go forward and ... */
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,
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,
2343 rdev->badblocks.size = 0;
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... */
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);
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;
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);
2396 unbind_rdev_from_array(rdev);
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);
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) {
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);
2439 state_show(struct md_rdev *rdev, char *page)
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);
2450 if (test_bit(In_sync, &flags)) {
2451 len += sprintf(page+len, "%sin_sync",sep);
2454 if (test_bit(WriteMostly, &flags)) {
2455 len += sprintf(page+len, "%swrite_mostly",sep);
2458 if (test_bit(Blocked, &flags) ||
2459 (rdev->badblocks.unacked_exist
2460 && !test_bit(Faulty, &flags))) {
2461 len += sprintf(page+len, "%sblocked", sep);
2464 if (!test_bit(Faulty, &flags) &&
2465 !test_bit(In_sync, &flags)) {
2466 len += sprintf(page+len, "%sspare", sep);
2469 if (test_bit(WriteErrorSeen, &flags)) {
2470 len += sprintf(page+len, "%swrite_error", sep);
2473 if (test_bit(WantReplacement, &flags)) {
2474 len += sprintf(page+len, "%swant_replacement", sep);
2477 if (test_bit(Replacement, &flags)) {
2478 len += sprintf(page+len, "%sreplacement", sep);
2482 return len+sprintf(page+len, "\n");
2486 state_store(struct md_rdev *rdev, const char *buf, size_t len)
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
2502 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2503 md_error(rdev->mddev, rdev);
2504 if (test_bit(Faulty, &rdev->flags))
2508 } else if (cmd_match(buf, "remove")) {
2509 if (rdev->raid_disk >= 0)
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);
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);
2525 } else if (cmd_match(buf, "writemostly")) {
2526 set_bit(WriteMostly, &rdev->flags);
2528 } else if (cmd_match(buf, "-writemostly")) {
2529 clear_bit(WriteMostly, &rdev->flags);
2531 } else if (cmd_match(buf, "blocked")) {
2532 set_bit(Blocked, &rdev->flags);
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);
2549 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2550 set_bit(In_sync, &rdev->flags);
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;
2559 } else if (cmd_match(buf, "write_error")) {
2560 set_bit(WriteErrorSeen, &rdev->flags);
2562 } else if (cmd_match(buf, "-write_error")) {
2563 clear_bit(WriteErrorSeen, &rdev->flags);
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);
2576 } else if (cmd_match(buf, "-want_replacement")) {
2577 /* Clearing 'want_replacement' is always allowed.
2578 * Once replacements starts it is too late though.
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)
2590 set_bit(Replacement, &rdev->flags);
2593 } else if (cmd_match(buf, "-replacement")) {
2594 /* Similarly, can only clear Replacement before start */
2595 if (rdev->mddev->pers)
2598 clear_bit(Replacement, &rdev->flags);
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);
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);
2625 errors_show(struct md_rdev *rdev, char *page)
2627 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2631 errors_store(struct md_rdev *rdev, const char *buf, size_t len)
2634 unsigned long n = simple_strtoul(buf, &e, 10);
2635 if (*buf && (*e == 0 || *e == '\n')) {
2636 atomic_set(&rdev->corrected_errors, n);
2641 static struct rdev_sysfs_entry rdev_errors =
2642 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
2645 slot_show(struct md_rdev *rdev, char *page)
2647 if (rdev->raid_disk < 0)
2648 return sprintf(page, "none\n");
2650 return sprintf(page, "%d\n", rdev->raid_disk);
2654 slot_store(struct md_rdev *rdev, const char *buf, size_t len)
2658 int slot = simple_strtoul(buf, &e, 10);
2659 if (strncmp(buf, "none", 4)==0)
2661 else if (e==buf || (*e && *e!= '\n'))
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)
2673 /* personality does all needed checks */
2674 if (rdev->mddev->pers->hot_remove_disk == NULL)
2676 clear_bit(Blocked, &rdev->flags);
2677 remove_and_add_spares(rdev->mddev, rdev);
2678 if (rdev->raid_disk >= 0)
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)
2690 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2693 if (rdev->mddev->pers->hot_add_disk == NULL)
2696 if (slot >= rdev->mddev->raid_disks &&
2697 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2700 rdev->raid_disk = slot;
2701 if (test_bit(In_sync, &rdev->flags))
2702 rdev->saved_raid_disk = slot;
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);
2710 rdev->raid_disk = -1;
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. */
2718 if (slot >= rdev->mddev->raid_disks &&
2719 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
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);
2731 static struct rdev_sysfs_entry rdev_slot =
2732 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
2735 offset_show(struct md_rdev *rdev, char *page)
2737 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
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)
2746 if (rdev->mddev->pers && rdev->raid_disk >= 0)
2748 if (rdev->sectors && rdev->mddev->external)
2749 /* Must set offset before size, so overlap checks
2752 rdev->data_offset = offset;
2753 rdev->new_data_offset = offset;
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)
2775 if (mddev->sync_thread ||
2776 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
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)
2787 /* Metadata worries about other space details. */
2789 /* decreasing the offset is inconsistent with a backwards
2792 if (new_offset < rdev->data_offset &&
2793 mddev->reshape_backwards)
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)
2803 if (mddev->pers && mddev->persistent &&
2804 !super_types[mddev->major_version]
2805 .allow_new_offset(rdev, new_offset))
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;
2815 static struct rdev_sysfs_entry rdev_new_offset =
2816 __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
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 */
2834 static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2836 unsigned long long blocks;
2839 if (kstrtoull(buf, 10, &blocks) < 0)
2842 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2843 return -EINVAL; /* sector conversion overflow */
2846 if (new != blocks * 2)
2847 return -EINVAL; /* unsigned long long to sector_t overflow */
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;
2860 if (strict_blocks_to_sectors(buf, §ors) < 0)
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);
2870 } else if (!sectors)
2871 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
2873 if (!my_mddev->pers->resize)
2874 /* Cannot change size for RAID0 or Linear etc */
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;
2890 struct list_head *tmp;
2893 for_each_mddev(mddev, tmp) {
2894 struct md_rdev *rdev2;
2896 rdev_for_each(rdev2, mddev)
2897 if (rdev->bdev == rdev2->bdev &&
2899 overlaps(rdev->data_offset, rdev->sectors,
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
2918 rdev->sectors = oldsectors;
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))
2948 if (rdev->mddev->pers &&
2949 rdev->raid_disk >= 0)
2952 rdev->recovery_offset = recovery_start;
2953 if (recovery_start == MaxSector)
2954 set_bit(In_sync, &rdev->flags);
2956 clear_bit(In_sync, &rdev->flags);
2960 static struct rdev_sysfs_entry rdev_recovery_start =
2961 __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2964 badblocks_show(struct badblocks *bb, char *page, int unack);
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);
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[] = {
2999 &rdev_new_offset.attr,
3001 &rdev_recovery_start.attr,
3002 &rdev_bad_blocks.attr,
3003 &rdev_unack_bad_blocks.attr,
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);
3016 return entry->show(rdev, page);
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);
3026 struct mddev *mddev = rdev->mddev;
3030 if (!capable(CAP_SYS_ADMIN))
3032 rv = mddev ? mddev_lock(mddev): -EBUSY;
3034 if (rdev->mddev == NULL)
3037 rv = entry->store(rdev, page, length);
3038 mddev_unlock(mddev);
3043 static void rdev_free(struct kobject *ko)
3045 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
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)
3061 rdev->saved_raid_disk = -1;
3062 rdev->raid_disk = -1;
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)
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];
3106 struct md_rdev *rdev;
3109 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
3111 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3112 return ERR_PTR(-ENOMEM);
3115 err = md_rdev_init(rdev);
3118 err = alloc_disk_sb(rdev);
3122 err = lock_rdev(rdev, newdev, super_format == -2);
3126 kobject_init(&rdev->kobj, &rdev_ktype);
3128 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
3131 "md: %s has zero or unknown size, marking faulty!\n",
3132 bdevname(rdev->bdev,b));
3137 if (super_format >= 0) {
3138 err = super_types[super_format].
3139 load_super(rdev, NULL, super_minor);
3140 if (err == -EINVAL) {
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);
3150 "md: could not read %s's sb, not importing!\n",
3151 bdevname(rdev->bdev,b));
3161 md_rdev_clear(rdev);
3163 return ERR_PTR(err);
3167 * Check a full RAID array for plausibility
3170 static void analyze_sbs(struct mddev *mddev)
3173 struct md_rdev *rdev, *freshest, *tmp;
3174 char b[BDEVNAME_SIZE];
3177 rdev_for_each_safe(rdev, tmp, mddev)
3178 switch (super_types[mddev->major_version].
3179 load_super(rdev, freshest, mddev->minor_version)) {
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);
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)) {
3202 "md: %s: %s: only %d devices permitted\n",
3203 mdname(mddev), bdevname(rdev->bdev, b),
3205 md_kick_rdev_from_array(rdev);
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"
3213 bdevname(rdev->bdev,b));
3214 md_kick_rdev_from_array(rdev);
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;
3251 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3254 else if (decimals < scale) {
3257 result = result * 10 + value;
3269 while (decimals < scale) {
3277 static void md_safemode_timeout(unsigned long data);
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);
3286 safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
3290 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
3293 mddev->safemode_delay = 0;
3295 unsigned long old_delay = mddev->safemode_delay;
3296 unsigned long new_delay = (msec*HZ)/1000;
3300 mddev->safemode_delay = new_delay;
3301 if (new_delay < old_delay || old_delay == 0)
3302 mod_timer(&mddev->safemode_timer, jiffies+1);
3306 static struct md_sysfs_entry md_safe_delay =
3307 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
3310 level_show(struct mddev *mddev, char *page)
3312 struct md_personality *p;
3314 spin_lock(&mddev->lock);
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);
3324 spin_unlock(&mddev->lock);
3329 level_store(struct mddev *mddev, const char *buf, size_t len)
3334 struct md_personality *pers, *oldpers;
3336 void *priv, *oldpriv;
3337 struct md_rdev *rdev;
3339 if (slen == 0 || slen >= sizeof(clevel))
3342 rv = mddev_lock(mddev);
3346 if (mddev->pers == NULL) {
3347 strncpy(mddev->clevel, buf, slen);
3348 if (mddev->clevel[slen-1] == '\n')
3350 mddev->clevel[slen] = 0;
3351 mddev->level = LEVEL_NONE;
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.
3366 if (mddev->sync_thread ||
3367 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3368 mddev->reshape_position != MaxSector ||
3369 mddev->sysfs_active)
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);
3379 /* Now find the new personality */
3380 strncpy(clevel, buf, slen);
3381 if (clevel[slen-1] == '\n')
3384 if (kstrtol(clevel, 10, &level))
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);
3397 spin_unlock(&pers_lock);
3399 if (pers == mddev->pers) {
3400 /* Nothing to do! */
3401 module_put(pers->owner);
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);
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);
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);
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;
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 &&
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
3459 * until external management is started.
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))
3473 "md: cannot register extra attributes for %s\n",
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)
3487 if (rdev->new_raid_disk >= mddev->raid_disks)
3488 rdev->new_raid_disk = -1;
3489 if (rdev->new_raid_disk == rdev->raid_disk)
3491 sysfs_unlink_rdev(mddev, rdev);
3493 rdev_for_each(rdev, mddev) {
3494 if (rdev->raid_disk < 0)
3496 if (rdev->new_raid_disk == rdev->raid_disk)
3498 rdev->raid_disk = rdev->new_raid_disk;
3499 if (rdev->raid_disk < 0)
3500 clear_bit(In_sync, &rdev->flags);
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
3514 del_timer_sync(&mddev->safemode_timer);
3516 blk_set_stacking_limits(&mddev->queue->limits);
3518 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3519 mddev_resume(mddev);
3521 md_update_sb(mddev, 1);
3522 sysfs_notify(&mddev->kobj, NULL, "level");
3523 md_new_event(mddev);
3526 mddev_unlock(mddev);
3530 static struct md_sysfs_entry md_level =
3531 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
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);
3545 layout_store(struct mddev *mddev, const char *buf, size_t len)
3548 unsigned long n = simple_strtoul(buf, &e, 10);
3551 if (!*buf || (*e && *e != '\n'))
3553 err = mddev_lock(mddev);
3558 if (mddev->pers->check_reshape == NULL)
3563 mddev->new_layout = n;
3564 err = mddev->pers->check_reshape(mddev);
3566 mddev->new_layout = mddev->layout;
3569 mddev->new_layout = n;
3570 if (mddev->reshape_position == MaxSector)
3573 mddev_unlock(mddev);
3576 static struct md_sysfs_entry md_layout =
3577 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
3580 raid_disks_show(struct mddev *mddev, char *page)
3582 if (mddev->raid_disks == 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);
3594 raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
3598 unsigned long n = simple_strtoul(buf, &e, 10);
3600 if (!*buf || (*e && *e != '\n'))
3603 err = mddev_lock(mddev);
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;
3613 rdev_for_each(rdev, mddev) {
3615 rdev->data_offset < rdev->new_data_offset)
3618 rdev->data_offset > rdev->new_data_offset)
3622 mddev->delta_disks = n - olddisks;
3623 mddev->raid_disks = n;
3624 mddev->reshape_backwards = (mddev->delta_disks < 0);
3626 mddev->raid_disks = n;
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);
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);
3646 chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
3650 unsigned long n = simple_strtoul(buf, &e, 10);
3652 if (!*buf || (*e && *e != '\n'))
3655 err = mddev_lock(mddev);
3659 if (mddev->pers->check_reshape == NULL)
3664 mddev->new_chunk_sectors = n >> 9;
3665 err = mddev->pers->check_reshape(mddev);
3667 mddev->new_chunk_sectors = mddev->chunk_sectors;
3670 mddev->new_chunk_sectors = n >> 9;
3671 if (mddev->reshape_position == MaxSector)
3672 mddev->chunk_sectors = n >> 9;
3674 mddev_unlock(mddev);
3677 static struct md_sysfs_entry md_chunk_size =
3678 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
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);
3689 resync_start_store(struct mddev *mddev, const char *buf, size_t len)
3693 unsigned long long n = simple_strtoull(buf, &e, 10);
3695 err = mddev_lock(mddev);
3698 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3700 else if (cmd_match(buf, "none"))
3702 else if (!*buf || (*e && *e != '\n'))
3706 mddev->recovery_cp = n;
3708 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3710 mddev_unlock(mddev);
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:
3721 * No devices, no size, no level
3722 * Equivalent to STOP_ARRAY ioctl
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
3731 * no resync can happen. no superblocks get written.
3732 * write requests fail
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.
3743 * fully active: IO and resync can be happening.
3744 * When written to inactive array, starts with resync
3747 * clean, but writes are blocked waiting for 'active' to be written.
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)
3762 for (n=0; list[n]; n++)
3763 if (cmd_match(word, list[n]))
3769 array_state_show(struct mddev *mddev, char *page)
3771 enum array_state st = inactive;
3784 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
3786 else if (mddev->safemode)
3792 if (list_empty(&mddev->disks) &&
3793 mddev->raid_disks == 0 &&
3794 mddev->dev_sectors == 0)
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);
3808 array_state_store(struct mddev *mddev, const char *buf, size_t len)
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
3817 spin_lock(&mddev->lock);
3819 restart_array(mddev);
3820 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
3821 wake_up(&mddev->sb_wait);
3823 } else /* st == clean */ {
3824 restart_array(mddev);
3825 if (atomic_read(&mddev->writes_pending) == 0) {
3826 if (mddev->in_sync == 0) {
3828 if (mddev->safemode == 1)
3829 mddev->safemode = 0;
3830 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3836 spin_unlock(&mddev->lock);
3839 err = mddev_lock(mddev);
3847 /* stopping an active array */
3848 err = do_md_stop(mddev, 0, NULL);
3851 /* stopping an active array */
3853 err = do_md_stop(mddev, 2, NULL);
3855 err = 0; /* already inactive */
3858 break; /* not supported yet */
3861 err = md_set_readonly(mddev, NULL);
3864 set_disk_ro(mddev->gendisk, 1);
3865 err = do_md_run(mddev);
3871 err = md_set_readonly(mddev, NULL);
3872 else if (mddev->ro == 1)
3873 err = restart_array(mddev);
3876 set_disk_ro(mddev->gendisk, 0);
3880 err = do_md_run(mddev);
3885 restart_array(mddev);
3886 spin_lock(&mddev->lock);
3887 if (atomic_read(&mddev->writes_pending) == 0) {
3888 if (mddev->in_sync == 0) {
3890 if (mddev->safemode == 1)
3891 mddev->safemode = 0;
3892 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3897 spin_unlock(&mddev->lock);
3903 restart_array(mddev);
3904 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
3905 wake_up(&mddev->sb_wait);
3909 set_disk_ro(mddev->gendisk, 0);
3910 err = do_md_run(mddev);
3915 /* these cannot be set */
3920 if (mddev->hold_active == UNTIL_IOCTL)
3921 mddev->hold_active = 0;
3922 sysfs_notify_dirent_safe(mddev->sysfs_state);
3924 mddev_unlock(mddev);
3927 static struct md_sysfs_entry md_array_state =
3928 __ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
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));
3937 max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
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);
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);
3954 null_show(struct mddev *mddev, char *page)
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.
3970 int major = simple_strtoul(buf, &e, 10);
3973 struct md_rdev *rdev;
3976 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3978 minor = simple_strtoul(e+1, &e, 10);
3979 if (*e && *e != '\n')
3981 dev = MKDEV(major, minor);
3982 if (major != MAJOR(dev) ||
3983 minor != MINOR(dev))
3986 flush_workqueue(md_misc_wq);
3988 err = mddev_lock(mddev);
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);
4003 } else if (mddev->external)
4004 rdev = md_import_device(dev, -2, -1);
4006 rdev = md_import_device(dev, -1, -1);
4009 return PTR_ERR(rdev);
4010 err = bind_rdev_to_array(rdev, mddev);
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);
4022 bitmap_store(struct mddev *mddev, const char *buf, size_t len)
4025 unsigned long chunk, end_chunk;
4028 err = mddev_lock(mddev);
4033 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4035 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4036 if (buf == end) break;
4037 if (*end == '-') { /* range */
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 */
4048 mddev_unlock(mddev);
4052 static struct md_sysfs_entry md_bitmap =
4053 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
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);
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
4072 int err = strict_blocks_to_sectors(buf, §ors);
4076 err = mddev_lock(mddev);
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);
4087 if (mddev->dev_sectors == 0 ||
4088 mddev->dev_sectors > sectors)
4089 mddev->dev_sectors = sectors;
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.
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
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);
4115 return sprintf(page, "none\n");
4119 metadata_store(struct mddev *mddev, const char *buf, size_t len)
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);
4133 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4135 else if (!list_empty(&mddev->disks))
4139 if (cmd_match(buf, "none")) {
4140 mddev->persistent = 0;
4141 mddev->external = 0;
4142 mddev->major_version = 0;
4143 mddev->minor_version = 90;
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;
4160 major = simple_strtoul(buf, &e, 10);
4162 if (e==buf || *e != '.')
4165 minor = simple_strtoul(buf, &e, 10);
4166 if (e==buf || (*e && *e != '\n') )
4169 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
4171 mddev->major_version = major;
4172 mddev->minor_version = minor;
4173 mddev->persistent = 1;
4174 mddev->external = 0;
4177 mddev_unlock(mddev);
4181 static struct md_sysfs_entry md_metadata =
4182 __ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
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))
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))
4195 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4196 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
4198 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
4202 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
4205 return sprintf(page, "%s\n", type);
4209 action_store(struct mddev *mddev, const char *page, size_t len)
4211 if (!mddev->pers || !mddev->pers->sync_request)
4215 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
4216 if (cmd_match(page, "frozen"))
4217 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
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))
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")) {
4238 if (mddev->pers->start_reshape == NULL)
4240 err = mddev_lock(mddev);
4242 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4243 err = mddev->pers->start_reshape(mddev);
4244 mddev_unlock(mddev);
4248 sysfs_notify(&mddev->kobj, NULL, "degraded");
4250 if (cmd_match(page, "check"))
4251 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4252 else if (!cmd_match(page, "repair"))
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
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);
4271 static struct md_sysfs_entry md_scan_mode =
4272 __ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
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);
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);
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");
4300 sync_min_store(struct mddev *mddev, const char *buf, size_t len)
4304 if (strncmp(buf, "system", 6)==0) {
4305 mddev->sync_speed_min = 0;
4308 min = simple_strtoul(buf, &e, 10);
4309 if (buf == e || (*e && *e != '\n') || min <= 0)
4311 mddev->sync_speed_min = min;
4315 static struct md_sysfs_entry md_sync_min =
4316 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
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");
4326 sync_max_store(struct mddev *mddev, const char *buf, size_t len)
4330 if (strncmp(buf, "system", 6)==0) {
4331 mddev->sync_speed_max = 0;
4334 max = simple_strtoul(buf, &e, 10);
4335 if (buf == e || (*e && *e != '\n') || max <= 0)
4337 mddev->sync_speed_max = max;
4341 static struct md_sysfs_entry md_sync_max =
4342 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
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);
4352 sync_force_parallel_show(struct mddev *mddev, char *page)
4354 return sprintf(page, "%d\n", mddev->parallel_resync);
4358 sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
4362 if (kstrtol(buf, 10, &n))
4365 if (n != 0 && n != 1)
4368 mddev->parallel_resync = n;
4370 if (mddev->sync_thread)
4371 wake_up(&resync_wait);
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);
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;
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);
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;
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);
4422 min_sync_show(struct mddev *mddev, char *page)
4424 return sprintf(page, "%llu\n",
4425 (unsigned long long)mddev->resync_min);
4428 min_sync_store(struct mddev *mddev, const char *buf, size_t len)
4430 unsigned long long min;
4433 if (kstrtoull(buf, 10, &min))
4436 spin_lock(&mddev->lock);
4438 if (min > mddev->resync_max)
4442 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4445 /* Round down to multiple of 4K for safety */
4446 mddev->resync_min = round_down(min, 8);
4450 spin_unlock(&mddev->lock);
4454 static struct md_sysfs_entry md_min_sync =
4455 __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4458 max_sync_show(struct mddev *mddev, char *page)
4460 if (mddev->resync_max == MaxSector)
4461 return sprintf(page, "max\n");
4463 return sprintf(page, "%llu\n",
4464 (unsigned long long)mddev->resync_max);
4467 max_sync_store(struct mddev *mddev, const char *buf, size_t len)
4470 spin_lock(&mddev->lock);
4471 if (strncmp(buf, "max", 3) == 0)
4472 mddev->resync_max = MaxSector;
4474 unsigned long long max;
4478 if (kstrtoull(buf, 10, &max))
4480 if (max < mddev->resync_min)
4484 if (max < mddev->resync_max &&
4486 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4489 /* Must be a multiple of chunk_size */
4490 chunk = mddev->chunk_sectors;
4492 sector_t temp = max;
4495 if (sector_div(temp, chunk))
4498 mddev->resync_max = max;
4500 wake_up(&mddev->recovery_wait);
4503 spin_unlock(&mddev->lock);
4507 static struct md_sysfs_entry md_max_sync =
4508 __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4511 suspend_lo_show(struct mddev *mddev, char *page)
4513 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4517 suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
4520 unsigned long long new = simple_strtoull(buf, &e, 10);
4521 unsigned long long old;
4524 if (buf == e || (*e && *e != '\n'))
4527 err = mddev_lock(mddev);
4531 if (mddev->pers == NULL ||
4532 mddev->pers->quiesce == NULL)
4534 old = mddev->suspend_lo;
4535 mddev->suspend_lo = new;
4537 /* Shrinking suspended region */
4538 mddev->pers->quiesce(mddev, 2);
4540 /* Expanding suspended region - need to wait */
4541 mddev->pers->quiesce(mddev, 1);
4542 mddev->pers->quiesce(mddev, 0);
4546 mddev_unlock(mddev);
4549 static struct md_sysfs_entry md_suspend_lo =
4550 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4553 suspend_hi_show(struct mddev *mddev, char *page)
4555 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4559 suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
4562 unsigned long long new = simple_strtoull(buf, &e, 10);
4563 unsigned long long old;
4566 if (buf == e || (*e && *e != '\n'))
4569 err = mddev_lock(mddev);
4573 if (mddev->pers == NULL ||
4574 mddev->pers->quiesce == NULL)
4576 old = mddev->suspend_hi;
4577 mddev->suspend_hi = new;
4579 /* Shrinking suspended region */
4580 mddev->pers->quiesce(mddev, 2);
4582 /* Expanding suspended region - need to wait */
4583 mddev->pers->quiesce(mddev, 1);
4584 mddev->pers->quiesce(mddev, 0);
4588 mddev_unlock(mddev);
4591 static struct md_sysfs_entry md_suspend_hi =
4592 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
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");
4605 reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
4607 struct md_rdev *rdev;
4610 unsigned long long new = simple_strtoull(buf, &e, 10);
4612 if (buf == e || (*e && *e != '\n'))
4614 err = mddev_lock(mddev);
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;
4630 mddev_unlock(mddev);
4634 static struct md_sysfs_entry md_reshape_position =
4635 __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4636 reshape_position_store);
4639 reshape_direction_show(struct mddev *mddev, char *page)
4641 return sprintf(page, "%s\n",
4642 mddev->reshape_backwards ? "backwards" : "forwards");
4646 reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4651 if (cmd_match(buf, "forwards"))
4653 else if (cmd_match(buf, "backwards"))
4657 if (mddev->reshape_backwards == backwards)
4660 err = mddev_lock(mddev);
4663 /* check if we are allowed to change */
4664 if (mddev->delta_disks)
4666 else if (mddev->persistent &&
4667 mddev->major_version == 0)
4670 mddev->reshape_backwards = backwards;
4671 mddev_unlock(mddev);
4675 static struct md_sysfs_entry md_reshape_direction =
4676 __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4677 reshape_direction_store);
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);
4686 return sprintf(page, "default\n");
4690 array_size_store(struct mddev *mddev, const char *buf, size_t len)
4695 err = mddev_lock(mddev);
4699 if (strncmp(buf, "default", 7) == 0) {
4701 sectors = mddev->pers->size(mddev, 0, 0);
4703 sectors = mddev->array_sectors;
4705 mddev->external_size = 0;
4707 if (strict_blocks_to_sectors(buf, §ors) < 0)
4709 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4712 mddev->external_size = 1;
4716 mddev->array_sectors = sectors;
4718 set_capacity(mddev->gendisk, mddev->array_sectors);
4719 revalidate_disk(mddev->gendisk);
4722 mddev_unlock(mddev);
4726 static struct md_sysfs_entry md_array_size =
4727 __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4730 static struct attribute *md_default_attrs[] = {
4733 &md_raid_disks.attr,
4734 &md_chunk_size.attr,
4736 &md_resync_start.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,
4748 static struct attribute *md_redundancy_attrs[] = {
4750 &md_last_scan_mode.attr,
4751 &md_mismatches.attr,
4754 &md_sync_speed.attr,
4755 &md_sync_force_parallel.attr,
4756 &md_sync_completed.attr,
4759 &md_suspend_lo.attr,
4760 &md_suspend_hi.attr,
4765 static struct attribute_group md_redundancy_group = {
4767 .attrs = md_redundancy_attrs,
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);
4779 spin_lock(&all_mddevs_lock);
4780 if (list_empty(&mddev->all_mddevs)) {
4781 spin_unlock(&all_mddevs_lock);
4785 spin_unlock(&all_mddevs_lock);
4787 rv = entry->show(mddev, page);
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);
4802 if (!capable(CAP_SYS_ADMIN))
4804 spin_lock(&all_mddevs_lock);
4805 if (list_empty(&mddev->all_mddevs)) {
4806 spin_unlock(&all_mddevs_lock);
4810 spin_unlock(&all_mddevs_lock);
4811 rv = entry->store(mddev, page, length);
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);
4824 blk_cleanup_queue(mddev->queue);
4825 if (mddev->gendisk) {
4826 del_gendisk(mddev->gendisk);
4827 put_disk(mddev->gendisk);
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 = {
4839 .sysfs_ops = &md_sysfs_ops,
4840 .default_attrs = md_default_attrs,
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;
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);
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);
4893 spin_unlock(&all_mddevs_lock);
4897 mddev->queue = blk_alloc_queue(GFP_KERNEL);
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);
4907 blk_cleanup_queue(mddev->queue);
4908 mddev->queue = NULL;
4911 disk->major = MAJOR(mddev->unit);
4912 disk->first_minor = unit << shift;
4914 strcpy(disk->disk_name, name);
4915 else if (partitioned)
4916 sprintf(disk->disk_name, "md_d%d", unit);
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
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);
4935 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4936 &disk_to_dev(disk)->kobj, "%s", "md");
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",
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);
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");
4959 static struct kobject *md_probe(dev_t dev, int *part, void *data)
4961 md_alloc(dev, 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')
4976 if (len >= DISK_NAME_LEN)
4978 strlcpy(buf, val, len+1);
4979 if (strncmp(buf, "md_", 3) != 0)
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)
5001 struct md_rdev *rdev;
5002 struct md_personality *pers;
5004 if (list_empty(&mddev->disks))
5005 /* cannot run an array with no devices.. */
5010 /* Cannot run until previous stop completes properly */
5011 if (mddev->sysfs_active)
5015 * Analyze all RAID superblock(s)
5017 if (!mddev->raid_disks) {
5018 if (!mddev->persistent)
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
5033 rdev_for_each(rdev, mddev) {
5034 if (test_bit(Faulty, &rdev->flags))
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
5049 printk("md: %s: data overlaps metadata\n",
5054 if (rdev->sb_start + rdev->sb_size/512
5055 > rdev->data_offset) {
5056 printk("md: %s: metadata overlaps data\n",
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",
5075 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
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);
5093 if (pers->sync_request) {
5094 /* Warn if this is a potentially silly
5097 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5098 struct md_rdev *rdev2;
5101 rdev_for_each(rdev, mddev)
5102 rdev_for_each(rdev2, mddev) {
5104 rdev->bdev->bd_contains ==
5105 rdev2->bdev->bd_contains) {
5107 "%s: WARNING: %s appears to be"
5108 " on the same physical disk as"
5111 bdevname(rdev->bdev,b),
5112 bdevname(rdev2->bdev,b2));
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);
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__);
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);
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);
5154 mddev->bitmap = bitmap;
5158 mddev_detach(mddev);
5160 pers->free(mddev, mddev->private);
5161 module_put(pers->owner);
5162 bitmap_destroy(mddev);
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))
5174 "md: cannot register extra attributes for %s\n",
5176 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
5177 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
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 */
5189 spin_lock(&mddev->lock);
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");
5209 EXPORT_SYMBOL_GPL(md_run);
5211 static int do_md_run(struct mddev *mddev)
5215 err = md_run(mddev);
5218 err = bitmap_load(mddev);
5220 bitmap_destroy(mddev);
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);
5230 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
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))
5246 mddev->safemode = 0;
5248 set_disk_ro(disk, 0);
5249 printk(KERN_INFO "md: %s switched to read-write mode.\n",
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);
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;
5275 mddev->metadata_type[0] = 0;
5276 mddev->chunk_sectors = 0;
5277 mddev->ctime = mddev->utime = 0;
5279 mddev->max_disks = 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;
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 */
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",
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);
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);
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
5380 bitmap_destroy(mddev);
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)
5392 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
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,
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));
5416 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5417 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5418 md_wakeup_thread(mddev->thread);
5424 __md_stop_writes(mddev);
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);
5438 mutex_unlock(&mddev->open_mutex);
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;
5453 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
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);
5480 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5481 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5482 md_wakeup_thread(mddev->thread);
5488 set_disk_ro(disk, 0);
5490 __md_stop_writes(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);
5505 revalidate_disk(disk);
5510 mutex_unlock(&mddev->open_mutex);
5512 * Free resources if final stop
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);
5525 mddev->bitmap_info.offset = 0;
5527 export_array(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);
5541 static void autorun_array(struct mddev *mddev)
5543 struct md_rdev *rdev;
5546 if (list_empty(&mddev->disks))
5549 printk(KERN_INFO "md: running: ");
5551 rdev_for_each(rdev, mddev) {
5552 char b[BDEVNAME_SIZE];
5553 printk("<%s>", bdevname(rdev->bdev,b));
5557 err = do_md_run(mddev);
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)) {
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
5605 dev = MKDEV(mdp_major,
5606 rdev0->preferred_minor << MdpMinorShift);
5607 unit = MINOR(dev) >> MdpMinorShift;
5609 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
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);
5618 md_probe(dev, NULL, NULL);
5619 mddev = mddev_find(dev);
5620 if (!mddev || !mddev->gendisk) {
5624 "md: cannot allocate memory for md drive.\n");
5627 if (mddev_lock(mddev))
5628 printk(KERN_WARNING "md: %s locked, cannot run\n",
5630 else if (mddev->raid_disks || mddev->major_version
5631 || !list_empty(&mddev->disks)) {
5633 "md: %s already running, cannot run %s\n",
5634 mdname(mddev), bdevname(rdev0->bdev,b));
5635 mddev_unlock(mddev);
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))
5644 autorun_array(mddev);
5645 mddev_unlock(mddev);
5647 /* on success, candidates will be empty, on error
5650 rdev_for_each_list(rdev, tmp, &candidates) {
5651 list_del_init(&rdev->same_set);
5656 printk(KERN_INFO "md: ... autorun DONE.\n");
5658 #endif /* !MODULE */
5660 static int get_version(void __user *arg)
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)))
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;
5682 rdev_for_each_rcu(rdev, mddev) {
5684 if (test_bit(Faulty, &rdev->flags))
5688 if (test_bit(In_sync, &rdev->flags))
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 */
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;
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)))
5731 static int get_bitmap_file(struct mddev *mddev, void __user * arg)
5733 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5737 file = kmalloc(sizeof(*file), GFP_NOIO);
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))),
5751 memmove(file->pathname, ptr,
5752 sizeof(file->pathname)-(ptr-file->pathname));
5753 spin_unlock(&mddev->lock);
5756 copy_to_user(arg, file, sizeof(*file)))
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)))
5772 rdev = md_find_rdev_nr_rcu(mddev, info.number);
5774 info.major = MAJOR(rdev->bdev->bd_dev);
5775 info.minor = MINOR(rdev->bdev->bd_dev);
5776 info.raid_disk = rdev->raid_disk;
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);
5787 info.major = info.minor = 0;
5788 info.raid_disk = -1;
5789 info.state = (1<<MD_DISK_REMOVED);
5793 if (copy_to_user(arg, &info, sizeof(info)))
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",
5812 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5815 if (!mddev->raid_disks) {
5817 /* expecting a device which has a superblock */
5818 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5821 "md: md_import_device returned %ld\n",
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);
5833 "md: %s has different UUID to %s\n",
5834 bdevname(rdev->bdev,b),
5835 bdevname(rdev0->bdev,b2));
5840 err = bind_rdev_to_array(rdev, mddev);
5847 * add_new_disk can be used once the array is assembled
5848 * to add "hot spares". They must already have a superblock
5853 if (!mddev->pers->hot_add_disk) {
5855 "%s: personality does not support diskops!\n",
5859 if (mddev->persistent)
5860 rdev = md_import_device(dev, mddev->major_version,
5861 mddev->minor_version);
5863 rdev = md_import_device(dev, -1, -1);
5866 "md: md_import_device returned %ld\n",
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);
5878 rdev->raid_disk = -1;
5879 rdev->saved_raid_disk = rdev->raid_disk;
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.
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);
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);
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);
5914 md_cluster_ops->add_new_disk_finish(mddev);
5921 rdev->raid_disk = -1;
5922 err = bind_rdev_to_array(rdev, mddev);
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);
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",
5942 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5944 rdev = md_import_device(dev, -1, 0);
5947 "md: error, md_import_device() returned %ld\n",
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;
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;
5968 rdev->sb_start = calc_dev_sboffset(rdev);
5969 rdev->sectors = rdev->sb_start;
5971 err = bind_rdev_to_array(rdev, mddev);
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);
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)
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);
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));
6018 static int hot_add_disk(struct mddev *mddev, dev_t dev)
6020 char b[BDEVNAME_SIZE];
6022 struct md_rdev *rdev;
6027 if (mddev->major_version != 0) {
6028 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
6029 " version-0 superblocks.\n",
6033 if (!mddev->pers->hot_add_disk) {
6035 "%s: personality does not support diskops!\n",
6040 rdev = md_import_device(dev, -1, 0);
6043 "md: error, md_import_device() returned %ld\n",
6048 if (mddev->persistent)
6049 rdev->sb_start = calc_dev_sboffset(rdev);
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)) {
6057 "md: can not hot-add faulty %s disk to %s!\n",
6058 bdevname(rdev->bdev,b), mdname(mddev));
6063 if (mddev_is_clustered(mddev))
6064 md_cluster_ops->metadata_update_start(mddev);
6065 clear_bit(In_sync, &rdev->flags);
6067 rdev->saved_raid_disk = -1;
6068 err = bind_rdev_to_array(rdev, mddev);
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);
6093 if (mddev_is_clustered(mddev))
6094 md_cluster_ops->metadata_update_cancel(mddev);
6100 static int set_bitmap_file(struct mddev *mddev, int fd)
6105 if (!mddev->pers->quiesce || !mddev->thread)
6107 if (mddev->recovery || mddev->sync_thread)
6109 /* we should be able to change the bitmap.. */
6113 struct inode *inode;
6116 if (mddev->bitmap || mddev->bitmap_info.file)
6117 return -EEXIST; /* cannot add when bitmap is present */
6121 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
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",
6131 } else if (!(f->f_mode & FMODE_WRITE)) {
6132 printk(KERN_ERR "%s: error: bitmap file must open for write\n",
6135 } else if (atomic_read(&inode->i_writecount) != 1) {
6136 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
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 */
6150 mddev->pers->quiesce(mddev, 1);
6152 struct bitmap *bitmap;
6154 bitmap = bitmap_create(mddev, -1);
6155 if (!IS_ERR(bitmap)) {
6156 mddev->bitmap = bitmap;
6157 err = bitmap_load(mddev);
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);
6168 struct file *f = mddev->bitmap_info.file;
6170 spin_lock(&mddev->lock);
6171 mddev->bitmap_info.file = NULL;
6172 spin_unlock(&mddev->lock);
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? */
6203 "md: superblock version %d not known\n",
6204 info->major_version);
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();
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
6229 if (info->state & (1<<MD_SB_CLEAN))
6230 mddev->recovery_cp = MaxSector;
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)
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;
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)
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;
6280 int fit = (num_sectors == 0);
6282 if (mddev->pers->resize == NULL)
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
6293 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
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)
6307 rv = mddev->pers->resize(mddev, num_sectors);
6309 revalidate_disk(mddev->gendisk);
6313 static int update_raid_disks(struct mddev *mddev, int raid_disks)
6316 struct md_rdev *rdev;
6317 /* change the number of raid disks */
6318 if (mddev->pers->check_reshape == NULL)
6322 if (raid_disks <= 0 ||
6323 (mddev->max_disks && raid_disks >= mddev->max_disks))
6325 if (mddev->sync_thread ||
6326 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6327 mddev->reshape_position != MaxSector)
6330 rdev_for_each(rdev, mddev) {
6331 if (mddev->raid_disks < raid_disks &&
6332 rdev->data_offset < rdev->new_data_offset)
6334 if (mddev->raid_disks > raid_disks &&
6335 rdev->data_offset > rdev->new_data_offset)
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);
6347 mddev->delta_disks = 0;
6348 mddev->reshape_backwards = 0;
6354 * update_array_info is used to change the configuration of an
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)
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)
6383 /* Check there is only one change */
6384 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6386 if (mddev->raid_disks != info->raid_disks)
6388 if (mddev->layout != info->layout)
6390 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6397 if (mddev->layout != info->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)
6405 mddev->new_layout = info->layout;
6406 rv = mddev->pers->check_reshape(mddev);
6408 mddev->new_layout = mddev->layout;
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) {
6425 if (mddev->recovery || mddev->sync_thread) {
6429 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6430 struct bitmap *bitmap;
6431 /* add the bitmap */
6432 if (mddev->bitmap) {
6436 if (mddev->bitmap_info.default_offset == 0) {
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);
6450 rv = PTR_ERR(bitmap);
6452 bitmap_destroy(mddev);
6453 mddev->pers->quiesce(mddev, 0);
6455 /* remove the bitmap */
6456 if (!mddev->bitmap) {
6460 if (mddev->bitmap->storage.file) {
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);
6475 if (mddev_is_clustered(mddev))
6476 md_cluster_ops->metadata_update_cancel(mddev);
6480 static int set_disk_faulty(struct mddev *mddev, dev_t dev)
6482 struct md_rdev *rdev;
6485 if (mddev->pers == NULL)
6489 rdev = find_rdev_rcu(mddev, dev);
6493 md_error(mddev, rdev);
6494 if (!test_bit(Faulty, &rdev->flags))
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;
6513 geo->cylinders = mddev->array_sectors / 8;
6517 static inline bool md_ioctl_valid(unsigned int cmd)
6522 case GET_ARRAY_INFO:
6523 case GET_BITMAP_FILE:
6526 case HOT_REMOVE_DISK:
6529 case RESTART_ARRAY_RW:
6531 case SET_ARRAY_INFO:
6532 case SET_BITMAP_FILE:
6533 case SET_DISK_FAULTY:
6536 case CLUSTERED_DISK_NACK:
6543 static int md_ioctl(struct block_device *bdev, fmode_t mode,
6544 unsigned int cmd, unsigned long arg)
6547 void __user *argp = (void __user *)arg;
6548 struct mddev *mddev = NULL;
6551 if (!md_ioctl_valid(cmd))
6556 case GET_ARRAY_INFO:
6560 if (!capable(CAP_SYS_ADMIN))
6565 * Commands dealing with the RAID driver but not any
6570 err = get_version(argp);
6576 autostart_arrays(arg);
6583 * Commands creating/starting a new array:
6586 mddev = bdev->bd_disk->private_data;
6593 /* Some actions do not requires the mutex */
6595 case GET_ARRAY_INFO:
6596 if (!mddev->raid_disks && !mddev->external)
6599 err = get_array_info(mddev, argp);
6603 if (!mddev->raid_disks && !mddev->external)
6606 err = get_disk_info(mddev, argp);
6609 case SET_DISK_FAULTY:
6610 err = set_disk_faulty(mddev, new_decode_dev(arg));
6613 case GET_BITMAP_FILE:
6614 err = get_bitmap_file(mddev, argp);
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,
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
6633 mutex_lock(&mddev->open_mutex);
6634 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
6635 mutex_unlock(&mddev->open_mutex);
6639 set_bit(MD_STILL_CLOSED, &mddev->flags);
6640 mutex_unlock(&mddev->open_mutex);
6641 sync_blockdev(bdev);
6643 err = mddev_lock(mddev);
6646 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6651 if (cmd == SET_ARRAY_INFO) {
6652 mdu_array_info_t info;
6654 memset(&info, 0, sizeof(info));
6655 else if (copy_from_user(&info, argp, sizeof(info))) {
6660 err = update_array_info(mddev, &info);
6662 printk(KERN_WARNING "md: couldn't update"
6663 " array info. %d\n", err);
6668 if (!list_empty(&mddev->disks)) {
6670 "md: array %s already has disks!\n",
6675 if (mddev->raid_disks) {
6677 "md: array %s already initialised!\n",
6682 err = set_array_info(mddev, &info);
6684 printk(KERN_WARNING "md: couldn't set"
6685 " array info. %d\n", err);
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) {
6705 * Commands even a read-only array can execute:
6708 case RESTART_ARRAY_RW:
6709 err = restart_array(mddev);
6713 err = do_md_stop(mddev, 0, bdev);
6717 err = md_set_readonly(mddev, bdev);
6720 case HOT_REMOVE_DISK:
6721 err = hot_remove_disk(mddev, new_decode_dev(arg));
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.
6730 mdu_disk_info_t info;
6731 if (copy_from_user(&info, argp, sizeof(info)))
6733 else if (!(info.state & (1<<MD_DISK_SYNC)))
6734 /* Need to clear read-only for this */
6737 err = add_new_disk(mddev, &info);
6743 if (get_user(ro, (int __user *)(arg))) {
6749 /* if the bdev is going readonly the value of mddev->ro
6750 * does not matter, no writes are coming
6755 /* are we are already prepared for writes? */
6759 /* transitioning to readauto need only happen for
6760 * arrays that call md_write_start
6763 err = restart_array(mddev);
6766 set_disk_ro(mddev->gendisk, 0);
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) {
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);
6801 mdu_disk_info_t info;
6802 if (copy_from_user(&info, argp, sizeof(info)))
6805 err = add_new_disk(mddev, &info);
6809 case CLUSTERED_DISK_NACK:
6810 if (mddev_is_clustered(mddev))
6811 md_cluster_ops->new_disk_ack(mddev, false);
6817 err = hot_add_disk(mddev, new_decode_dev(arg));
6821 err = do_md_run(mddev);
6824 case SET_BITMAP_FILE:
6825 err = set_bitmap_file(mddev, (int)arg);
6834 if (mddev->hold_active == UNTIL_IOCTL &&
6836 mddev->hold_active = 0;
6837 mddev_unlock(mddev);
6841 #ifdef CONFIG_COMPAT
6842 static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6843 unsigned int cmd, unsigned long arg)
6846 case HOT_REMOVE_DISK:
6848 case SET_DISK_FAULTY:
6849 case SET_BITMAP_FILE:
6850 /* These take in integer arg, do not convert */
6853 arg = (unsigned long)compat_ptr(arg);
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);
6873 if (mddev->gendisk != bdev->bd_disk) {
6874 /* we are racing with mddev_put which is discarding this
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)))
6889 atomic_inc(&mddev->openers);
6890 clear_bit(MD_STILL_CLOSED, &mddev->flags);
6891 mutex_unlock(&mddev->open_mutex);
6893 check_disk_change(bdev);
6898 static void md_release(struct gendisk *disk, fmode_t mode)
6900 struct mddev *mddev = disk->private_data;
6903 atomic_dec(&mddev->openers);
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;
6921 static const struct block_device_operations md_fops =
6923 .owner = THIS_MODULE,
6925 .release = md_release,
6927 #ifdef CONFIG_COMPAT
6928 .compat_ioctl = md_compat_ioctl,
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
6959 if (signal_pending(current))
6960 flush_signals(current);
6962 wait_event_interruptible_timeout
6964 test_bit(THREAD_WAKEUP, &thread->flags)
6965 || kthread_should_stop(),
6968 clear_bit(THREAD_WAKEUP, &thread->flags);
6969 if (!kthread_should_stop())
6970 thread->run(thread);
6976 void md_wakeup_thread(struct md_thread *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);
6995 init_waitqueue_head(&thread->wqueue);
6998 thread->mddev = mddev;
6999 thread->timeout = MAX_SCHEDULE_TIMEOUT;
7000 thread->tsk = kthread_run(md_thread, thread,
7002 mdname(thread->mddev),
7004 if (IS_ERR(thread->tsk)) {
7010 EXPORT_SYMBOL(md_register_thread);
7012 void md_unregister_thread(struct md_thread **threadp)
7014 struct md_thread *thread = *threadp;
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);
7023 spin_unlock(&pers_lock);
7025 kthread_stop(thread->tsk);
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))
7035 if (!mddev->pers || !mddev->pers->error_handler)
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)
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];
7062 seq_printf(seq, "%s ",
7063 bdevname(rdev->bdev,b));
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;
7077 unsigned int per_milli;
7079 if (mddev->curr_resync <= 3)
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;
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
7098 if (sizeof(sector_t) > sizeof(unsigned long)) {
7099 while ( max_sectors/2 > (1ULL<<(scale+32)))
7102 res = (resync>>scale)*1000;
7103 sector_div(res, (u32)((max_sectors>>scale)+1));
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)?
7119 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
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
7135 * We scale the divisor (db) by 32 to avoid losing precision
7136 * near the end of resync when the number of remaining sectors
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);
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);
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;
7161 struct mddev *mddev;
7169 spin_lock(&all_mddevs_lock);
7170 list_for_each(tmp,&all_mddevs)
7172 mddev = list_entry(tmp, struct mddev, all_mddevs);
7174 spin_unlock(&all_mddevs_lock);
7177 spin_unlock(&all_mddevs_lock);
7179 return (void*)2;/* tail */
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;
7192 spin_lock(&all_mddevs_lock);
7194 tmp = all_mddevs.next;
7196 tmp = mddev->all_mddevs.next;
7197 if (tmp != &all_mddevs)
7198 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
7200 next_mddev = (void*)2;
7203 spin_unlock(&all_mddevs_lock);
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)
7219 static int md_seq_show(struct seq_file *seq, void *v)
7221 struct mddev *mddev = v;
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);
7237 if (v == (void*)2) {
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");
7248 seq_printf(seq, " (read-only)");
7250 seq_printf(seq, " (auto-read-only)");
7251 seq_printf(seq, " %s", mddev->pers->name);
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)");
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;
7274 if (!list_empty(&mddev->disks)) {
7276 seq_printf(seq, "\n %llu blocks",
7277 (unsigned long long)
7278 mddev->array_sectors / 2);
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);
7294 seq_printf(seq, " super non-persistent");
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 ");
7309 seq_printf(seq, "\n ");
7311 bitmap_status(seq, mddev->bitmap);
7313 seq_printf(seq, "\n");
7315 spin_unlock(&mddev->lock);
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;
7332 error = seq_open(file, &md_seq_ops);
7336 seq = file->private_data;
7337 seq->poll_event = atomic_read(&md_event_count);
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;
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;
7359 static const struct file_operations md_seq_fops = {
7360 .owner = THIS_MODULE,
7361 .open = md_seq_open,
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",
7372 spin_lock(&pers_lock);
7373 list_add_tail(&p->list, &pers_list);
7374 spin_unlock(&pers_lock);
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);
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)
7393 spin_lock(&pers_lock);
7394 md_cluster_ops = ops;
7395 md_cluster_mod = module;
7396 spin_unlock(&pers_lock);
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);
7408 EXPORT_SYMBOL(unregister_md_cluster_operations);
7410 int md_setup_cluster(struct mddev *mddev, int nodes)
7414 err = request_module("md-cluster");
7416 pr_err("md-cluster module not found.\n");
7420 spin_lock(&pers_lock);
7421 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
7422 spin_unlock(&pers_lock);
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)
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;
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
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;
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);
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)
7504 if (bio_data_dir(bi) != WRITE)
7507 BUG_ON(mddev->ro == 1);
7508 if (mddev->ro == 2) {
7509 /* need to switch to read/write */
7511 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7512 md_wakeup_thread(mddev->thread);
7513 md_wakeup_thread(mddev->sync_thread);
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) {
7523 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
7524 set_bit(MD_CHANGE_PENDING, &mddev->flags);
7525 md_wakeup_thread(mddev->thread);
7528 spin_unlock(&mddev->lock);
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)
7563 if (!mddev->pers->sync_request)
7566 spin_lock(&mddev->lock);
7567 if (mddev->in_sync) {
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);
7582 spin_unlock(&mddev->lock);
7584 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
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,
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];
7605 struct list_head *tmp;
7606 sector_t last_check;
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))
7615 if (mddev->ro) {/* never try to sync a read-only array */
7616 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7620 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7621 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
7622 desc = "data-check";
7624 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7625 desc = "requested-resync";
7629 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->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
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.
7653 mddev->curr_resync = 2;
7656 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7658 for_each_mddev(mddev2, tmp) {
7659 if (mddev2 == mddev)
7661 if (!mddev->parallel_resync
7662 && mddev2->curr_resync
7663 && match_mddev_units(mddev, mddev2)) {
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
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));
7687 if (signal_pending(current))
7688 flush_signals(current);
7690 finish_wait(&resync_wait, &wq);
7693 finish_wait(&resync_wait, &wq);
7696 } while (mddev->curr_resync < 2);
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;
7714 /* recovery follows the physical size of devices */
7715 max_sectors = mddev->dev_sectors;
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;
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
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 */
7750 for (m = 0; m < SYNC_MARKS; m++) {
7752 mark_cnt[m] = io_sectors;
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);
7770 "md: resuming %s of %s from checkpoint.\n",
7771 desc, mdname(mddev));
7772 mddev->curr_resync = j;
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) {
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
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,
7822 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7825 sectors = mddev->pers->sync_request(mddev, j, &skipped);
7827 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
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))
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)
7854 last_check = io_sectors;
7856 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
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);
7867 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7871 * this loop exits only if either when we are slower than
7872 * the 'hard' speed limit, or the system was IO-idle for
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)
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)) {
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) {
7920 "md: checkpointing %s of %s.\n",
7921 desc, mdname(mddev));
7922 if (test_bit(MD_RECOVERY_ERROR,
7924 mddev->recovery_cp =
7925 mddev->curr_resync_completed;
7927 mddev->recovery_cp =
7931 mddev->recovery_cp = MaxSector;
7933 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7934 mddev->curr_resync = MaxSector;
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;
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);
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;
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;
7988 if (removed && mddev->kobj.sd)
7989 sysfs_notify(&mddev->kobj, NULL, "degraded");
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))
7999 if (rdev->raid_disk >= 0)
8001 if (test_bit(Faulty, &rdev->flags))
8004 ! (rdev->saved_raid_disk >= 0 &&
8005 !test_bit(Bitmap_sync, &rdev->flags)))
8008 if (rdev->saved_raid_disk < 0)
8009 rdev->recovery_offset = 0;
8011 hot_add_disk(mddev, rdev) == 0) {
8012 if (sysfs_link_rdev(mddev, rdev))
8013 /* failure here is OK */;
8015 md_new_event(mddev);
8016 set_bit(MD_CHANGE_DEVS, &mddev->flags);
8021 set_bit(MD_CHANGE_DEVS, &mddev->flags);
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,
8032 if (!mddev->sync_thread) {
8033 printk(KERN_ERR "%s: could not start resync"
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,
8045 if (mddev->sysfs_action)
8046 sysfs_notify_dirent_safe(mddev->sysfs_action);
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)
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",
8087 mddev->safemode = 2;
8089 flush_signals(current);
8092 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
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)
8104 if (mddev_trylock(mddev)) {
8108 /* On a read-only array we can:
8109 * - remove failed devices
8110 * - add already-in_sync devices if the array itself
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);
8125 if (!mddev->external) {
8127 spin_lock(&mddev->lock);
8128 if (mddev->safemode &&
8129 !atomic_read(&mddev->writes_pending) &&
8131 mddev->recovery_cp == MaxSector) {
8134 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
8136 if (mddev->safemode == 1)
8137 mddev->safemode = 0;
8138 spin_unlock(&mddev->lock);
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);
8157 if (mddev->sync_thread) {
8158 md_reap_sync_thread(mddev);
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
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))
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 */
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 ... */
8203 if (mddev->pers->sync_request) {
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);
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,
8221 if (mddev->sysfs_action)
8222 sysfs_notify_dirent_safe(mddev->sysfs_action);
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)) {
8240 /* activate any spares */
8241 if (mddev->pers->spare_active(mddev)) {
8242 sysfs_notify(&mddev->kobj, NULL,
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;
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.
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)
8337 sector_t target = s + sectors;
8340 if (bb->shift > 0) {
8341 /* round the start down, and the end up */
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 */
8350 seq = read_seqbegin(&bb->lock);
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]);
8367 /* This could still be the one, earlier ranges
8371 /* This and later ranges are definitely out. */
8374 /* 'lo' might be the last that started before target, but 'hi' isn't */
8376 /* need to check all range that end after 's' to see if
8377 * any are unacknowledged.
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]))
8389 *first_bad = BB_OFFSET(p[lo]);
8390 *bad_sectors = BB_LEN(p[lo]);
8396 if (read_seqretry(&bb->lock, seq))
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,
8416 unsigned long flags;
8419 /* badblocks are disabled */
8423 /* round the start down, and the end up */
8424 sector_t next = s + sectors;
8426 next += (1<<bb->shift) - 1;
8431 write_seqlock_irqsave(&bb->lock, flags);
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]);
8445 if (hi > lo && BB_OFFSET(p[lo]) > s)
8449 /* we found a range that might merge with the start
8452 sector_t a = BB_OFFSET(p[lo]);
8453 sector_t e = a + BB_LEN(p[lo]);
8454 int ack = BB_ACK(p[lo]);
8456 /* Yes, we can merge with a previous range */
8457 if (s == a && s + sectors >= e)
8458 /* new range covers old */
8461 ack = ack && acknowledged;
8463 if (e < s + sectors)
8465 if (e - a <= BB_MAX_LEN) {
8466 p[lo] = BB_MAKE(a, e-a, ack);
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);
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) {
8492 ack = ack && acknowledged;
8495 if (e - a <= BB_MAX_LEN) {
8496 p[hi] = BB_MAKE(a, e-a, ack);
8499 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
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);
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 */
8531 int this_sectors = sectors;
8532 memmove(p + hi + 1, p + hi,
8533 (bb->count - hi) * 8);
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;
8546 bb->unacked_exist = 1;
8547 write_sequnlock_irqrestore(&bb->lock, flags);
8552 int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8557 s += rdev->new_data_offset;
8559 s += rdev->data_offset;
8560 rv = md_set_badblocks(&rdev->badblocks,
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);
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)
8582 sector_t target = s + sectors;
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;
8594 target >>= bb->shift;
8595 sectors = target - s;
8598 write_seqlock_irq(&bb->lock);
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]);
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]);
8624 /* we need to split this range */
8625 if (bb->count >= MD_MAX_BADBLOCKS) {
8629 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8631 p[lo] = BB_MAKE(a, s-a, ack);
8634 p[lo] = BB_MAKE(target, end - target, ack);
8635 /* there is no longer an overlap */
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.. */
8652 /* 'lo' is strictly before, 'hi' is strictly after,
8653 * anything between needs to be discarded
8656 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8657 bb->count -= (hi - lo - 1);
8663 write_sequnlock_irq(&bb->lock);
8667 int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8671 s += rdev->new_data_offset;
8673 s += rdev->data_offset;
8674 return md_clear_badblocks(&rdev->badblocks,
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 */
8689 write_seqlock_irq(&bb->lock);
8691 if (bb->changed == 0 && bb->unacked_exist) {
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
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.
8720 badblocks_show(struct badblocks *bb, char *page, int unack)
8731 seq = read_seqbegin(&bb->lock);
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]);
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))
8761 badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8763 unsigned long long sector;
8767 /* Allow clearing via sysfs *only* for testing/debugging.
8768 * Normally only a successful write may clear a badblock
8771 if (page[0] == '-') {
8775 #endif /* DO_DEBUG */
8777 switch (sscanf(page, "%llu %d%c", §or, &length, &newline)) {
8779 if (newline != '\n')
8791 md_clear_badblocks(bb, sector, length);
8794 #endif /* DO_DEBUG */
8795 if (md_set_badblocks(bb, sector, length, !unack))
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;
8808 for_each_mddev(mddev, tmp) {
8809 if (mddev_trylock(mddev)) {
8811 __md_stop_writes(mddev);
8812 if (mddev->persistent)
8813 mddev->safemode = 2;
8814 mddev_unlock(mddev);
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 ...
8830 static struct notifier_block md_notifier = {
8831 .notifier_call = md_notify_reboot,
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)
8847 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
8851 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8855 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8858 if ((ret = register_blkdev(0, "mdp")) < 0)
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);
8874 unregister_blkdev(MD_MAJOR, "md");
8876 destroy_workqueue(md_misc_wq);
8878 destroy_workqueue(md_wq);
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;
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);
8908 * Searches all registered partitions for autorun RAID arrays
8912 static LIST_HEAD(all_detected_devices);
8913 struct detected_devices_node {
8914 struct list_head list;
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);
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;
8937 int i_scanned, i_passed;
8942 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8944 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
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);
8955 if (test_bit(Faulty, &rdev->flags))
8958 set_bit(AutoDetected, &rdev->flags);
8959 list_add(&rdev->same_set, &pending_raid_disks);
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;
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
8989 while (waitqueue_active(&md_event_waiters)) {
8990 /* not safe to leave yet */
8991 wake_up(&md_event_waiters);
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)
9015 int num = simple_strtoul(val, &e, 10);
9016 if (*val && (*e == '\0' || *e == '\n')) {
9017 start_readonly = num;
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");
9030 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);