2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
5 completely rewritten, based on the MD driver code from Marc Zyngier
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
20 Neil Brown <neilb@cse.unsw.edu.au>.
22 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
25 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include <linux/kthread.h>
36 #include <linux/blkdev.h>
37 #include <linux/sysctl.h>
38 #include <linux/seq_file.h>
39 #include <linux/buffer_head.h> /* for invalidate_bdev */
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/reboot.h>
47 #include <linux/file.h>
48 #include <linux/compat.h>
49 #include <linux/delay.h>
50 #include <linux/raid/md_p.h>
51 #include <linux/raid/md_u.h>
52 #include <linux/slab.h>
57 #define dprintk(x...) ((void)(DEBUG && printk(x)))
61 static void autostart_arrays(int part);
64 static LIST_HEAD(pers_list);
65 static DEFINE_SPINLOCK(pers_lock);
67 static void md_print_devices(void);
69 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
71 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
74 * Default number of read corrections we'll attempt on an rdev
75 * before ejecting it from the array. We divide the read error
76 * count by 2 for every hour elapsed between read errors.
78 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
80 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
81 * is 1000 KB/sec, so the extra system load does not show up that much.
82 * Increase it if you want to have more _guaranteed_ speed. Note that
83 * the RAID driver will use the maximum available bandwidth if the IO
84 * subsystem is idle. There is also an 'absolute maximum' reconstruction
85 * speed limit - in case reconstruction slows down your system despite
88 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
89 * or /sys/block/mdX/md/sync_speed_{min,max}
92 static int sysctl_speed_limit_min = 1000;
93 static int sysctl_speed_limit_max = 200000;
94 static inline int speed_min(mddev_t *mddev)
96 return mddev->sync_speed_min ?
97 mddev->sync_speed_min : sysctl_speed_limit_min;
100 static inline int speed_max(mddev_t *mddev)
102 return mddev->sync_speed_max ?
103 mddev->sync_speed_max : sysctl_speed_limit_max;
106 static struct ctl_table_header *raid_table_header;
108 static ctl_table raid_table[] = {
110 .procname = "speed_limit_min",
111 .data = &sysctl_speed_limit_min,
112 .maxlen = sizeof(int),
113 .mode = S_IRUGO|S_IWUSR,
114 .proc_handler = proc_dointvec,
117 .procname = "speed_limit_max",
118 .data = &sysctl_speed_limit_max,
119 .maxlen = sizeof(int),
120 .mode = S_IRUGO|S_IWUSR,
121 .proc_handler = proc_dointvec,
126 static ctl_table raid_dir_table[] = {
130 .mode = S_IRUGO|S_IXUGO,
136 static ctl_table raid_root_table[] = {
141 .child = raid_dir_table,
146 static const struct block_device_operations md_fops;
148 static int start_readonly;
151 * We have a system wide 'event count' that is incremented
152 * on any 'interesting' event, and readers of /proc/mdstat
153 * can use 'poll' or 'select' to find out when the event
157 * start array, stop array, error, add device, remove device,
158 * start build, activate spare
160 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
161 static atomic_t md_event_count;
162 void md_new_event(mddev_t *mddev)
164 atomic_inc(&md_event_count);
165 wake_up(&md_event_waiters);
167 EXPORT_SYMBOL_GPL(md_new_event);
169 /* Alternate version that can be called from interrupts
170 * when calling sysfs_notify isn't needed.
172 static void md_new_event_inintr(mddev_t *mddev)
174 atomic_inc(&md_event_count);
175 wake_up(&md_event_waiters);
179 * Enables to iterate over all existing md arrays
180 * all_mddevs_lock protects this list.
182 static LIST_HEAD(all_mddevs);
183 static DEFINE_SPINLOCK(all_mddevs_lock);
187 * iterates through all used mddevs in the system.
188 * We take care to grab the all_mddevs_lock whenever navigating
189 * the list, and to always hold a refcount when unlocked.
190 * Any code which breaks out of this loop while own
191 * a reference to the current mddev and must mddev_put it.
193 #define for_each_mddev(mddev,tmp) \
195 for (({ spin_lock(&all_mddevs_lock); \
196 tmp = all_mddevs.next; \
198 ({ if (tmp != &all_mddevs) \
199 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
200 spin_unlock(&all_mddevs_lock); \
201 if (mddev) mddev_put(mddev); \
202 mddev = list_entry(tmp, mddev_t, all_mddevs); \
203 tmp != &all_mddevs;}); \
204 ({ spin_lock(&all_mddevs_lock); \
209 /* Rather than calling directly into the personality make_request function,
210 * IO requests come here first so that we can check if the device is
211 * being suspended pending a reconfiguration.
212 * We hold a refcount over the call to ->make_request. By the time that
213 * call has finished, the bio has been linked into some internal structure
214 * and so is visible to ->quiesce(), so we don't need the refcount any more.
216 static int md_make_request(struct request_queue *q, struct bio *bio)
218 const int rw = bio_data_dir(bio);
219 mddev_t *mddev = q->queuedata;
223 if (mddev == NULL || mddev->pers == NULL) {
228 if (mddev->suspended || mddev->barrier) {
231 prepare_to_wait(&mddev->sb_wait, &__wait,
232 TASK_UNINTERRUPTIBLE);
233 if (!mddev->suspended && !mddev->barrier)
239 finish_wait(&mddev->sb_wait, &__wait);
241 atomic_inc(&mddev->active_io);
244 rv = mddev->pers->make_request(mddev, bio);
246 cpu = part_stat_lock();
247 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
248 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
252 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
253 wake_up(&mddev->sb_wait);
258 /* mddev_suspend makes sure no new requests are submitted
259 * to the device, and that any requests that have been submitted
260 * are completely handled.
261 * Once ->stop is called and completes, the module will be completely
264 void mddev_suspend(mddev_t *mddev)
266 BUG_ON(mddev->suspended);
267 mddev->suspended = 1;
269 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
270 mddev->pers->quiesce(mddev, 1);
272 EXPORT_SYMBOL_GPL(mddev_suspend);
274 void mddev_resume(mddev_t *mddev)
276 mddev->suspended = 0;
277 wake_up(&mddev->sb_wait);
278 mddev->pers->quiesce(mddev, 0);
280 EXPORT_SYMBOL_GPL(mddev_resume);
282 int mddev_congested(mddev_t *mddev, int bits)
286 return mddev->suspended;
288 EXPORT_SYMBOL(mddev_congested);
291 * Generic barrier handling for md
294 #define POST_REQUEST_BARRIER ((void*)1)
296 static void md_end_barrier(struct bio *bio, int err)
298 mdk_rdev_t *rdev = bio->bi_private;
299 mddev_t *mddev = rdev->mddev;
300 if (err == -EOPNOTSUPP && mddev->barrier != POST_REQUEST_BARRIER)
301 set_bit(BIO_EOPNOTSUPP, &mddev->barrier->bi_flags);
303 rdev_dec_pending(rdev, mddev);
305 if (atomic_dec_and_test(&mddev->flush_pending)) {
306 if (mddev->barrier == POST_REQUEST_BARRIER) {
307 /* This was a post-request barrier */
308 mddev->barrier = NULL;
309 wake_up(&mddev->sb_wait);
311 /* The pre-request barrier has finished */
312 schedule_work(&mddev->barrier_work);
317 static void submit_barriers(mddev_t *mddev)
322 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
323 if (rdev->raid_disk >= 0 &&
324 !test_bit(Faulty, &rdev->flags)) {
325 /* Take two references, one is dropped
326 * when request finishes, one after
327 * we reclaim rcu_read_lock
330 atomic_inc(&rdev->nr_pending);
331 atomic_inc(&rdev->nr_pending);
333 bi = bio_alloc(GFP_KERNEL, 0);
334 bi->bi_end_io = md_end_barrier;
335 bi->bi_private = rdev;
336 bi->bi_bdev = rdev->bdev;
337 atomic_inc(&mddev->flush_pending);
338 submit_bio(WRITE_BARRIER, bi);
340 rdev_dec_pending(rdev, mddev);
345 static void md_submit_barrier(struct work_struct *ws)
347 mddev_t *mddev = container_of(ws, mddev_t, barrier_work);
348 struct bio *bio = mddev->barrier;
350 atomic_set(&mddev->flush_pending, 1);
352 if (test_bit(BIO_EOPNOTSUPP, &bio->bi_flags))
353 bio_endio(bio, -EOPNOTSUPP);
354 else if (bio->bi_size == 0)
355 /* an empty barrier - all done */
358 bio->bi_rw &= ~(1<<BIO_RW_BARRIER);
359 if (mddev->pers->make_request(mddev, bio))
360 generic_make_request(bio);
361 mddev->barrier = POST_REQUEST_BARRIER;
362 submit_barriers(mddev);
364 if (atomic_dec_and_test(&mddev->flush_pending)) {
365 mddev->barrier = NULL;
366 wake_up(&mddev->sb_wait);
370 void md_barrier_request(mddev_t *mddev, struct bio *bio)
372 spin_lock_irq(&mddev->write_lock);
373 wait_event_lock_irq(mddev->sb_wait,
375 mddev->write_lock, /*nothing*/);
376 mddev->barrier = bio;
377 spin_unlock_irq(&mddev->write_lock);
379 atomic_set(&mddev->flush_pending, 1);
380 INIT_WORK(&mddev->barrier_work, md_submit_barrier);
382 submit_barriers(mddev);
384 if (atomic_dec_and_test(&mddev->flush_pending))
385 schedule_work(&mddev->barrier_work);
387 EXPORT_SYMBOL(md_barrier_request);
389 /* Support for plugging.
390 * This mirrors the plugging support in request_queue, but does not
391 * require having a whole queue
393 static void plugger_work(struct work_struct *work)
395 struct plug_handle *plug =
396 container_of(work, struct plug_handle, unplug_work);
397 plug->unplug_fn(plug);
399 static void plugger_timeout(unsigned long data)
401 struct plug_handle *plug = (void *)data;
402 kblockd_schedule_work(NULL, &plug->unplug_work);
404 void plugger_init(struct plug_handle *plug,
405 void (*unplug_fn)(struct plug_handle *))
407 plug->unplug_flag = 0;
408 plug->unplug_fn = unplug_fn;
409 init_timer(&plug->unplug_timer);
410 plug->unplug_timer.function = plugger_timeout;
411 plug->unplug_timer.data = (unsigned long)plug;
412 INIT_WORK(&plug->unplug_work, plugger_work);
414 EXPORT_SYMBOL_GPL(plugger_init);
416 void plugger_set_plug(struct plug_handle *plug)
418 if (!test_and_set_bit(PLUGGED_FLAG, &plug->unplug_flag))
419 mod_timer(&plug->unplug_timer, jiffies + msecs_to_jiffies(3)+1);
421 EXPORT_SYMBOL_GPL(plugger_set_plug);
423 int plugger_remove_plug(struct plug_handle *plug)
425 if (test_and_clear_bit(PLUGGED_FLAG, &plug->unplug_flag)) {
426 del_timer(&plug->unplug_timer);
431 EXPORT_SYMBOL_GPL(plugger_remove_plug);
434 static inline mddev_t *mddev_get(mddev_t *mddev)
436 atomic_inc(&mddev->active);
440 static void mddev_delayed_delete(struct work_struct *ws);
442 static void mddev_put(mddev_t *mddev)
444 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
446 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
447 mddev->ctime == 0 && !mddev->hold_active) {
448 /* Array is not configured at all, and not held active,
450 list_del(&mddev->all_mddevs);
451 if (mddev->gendisk) {
452 /* we did a probe so need to clean up.
453 * Call schedule_work inside the spinlock
454 * so that flush_scheduled_work() after
455 * mddev_find will succeed in waiting for the
458 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
459 schedule_work(&mddev->del_work);
463 spin_unlock(&all_mddevs_lock);
466 void mddev_init(mddev_t *mddev)
468 mutex_init(&mddev->open_mutex);
469 mutex_init(&mddev->reconfig_mutex);
470 mutex_init(&mddev->bitmap_info.mutex);
471 INIT_LIST_HEAD(&mddev->disks);
472 INIT_LIST_HEAD(&mddev->all_mddevs);
473 init_timer(&mddev->safemode_timer);
474 atomic_set(&mddev->active, 1);
475 atomic_set(&mddev->openers, 0);
476 atomic_set(&mddev->active_io, 0);
477 spin_lock_init(&mddev->write_lock);
478 atomic_set(&mddev->flush_pending, 0);
479 init_waitqueue_head(&mddev->sb_wait);
480 init_waitqueue_head(&mddev->recovery_wait);
481 mddev->reshape_position = MaxSector;
482 mddev->resync_min = 0;
483 mddev->resync_max = MaxSector;
484 mddev->level = LEVEL_NONE;
486 EXPORT_SYMBOL_GPL(mddev_init);
488 static mddev_t * mddev_find(dev_t unit)
490 mddev_t *mddev, *new = NULL;
493 spin_lock(&all_mddevs_lock);
496 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
497 if (mddev->unit == unit) {
499 spin_unlock(&all_mddevs_lock);
505 list_add(&new->all_mddevs, &all_mddevs);
506 spin_unlock(&all_mddevs_lock);
507 new->hold_active = UNTIL_IOCTL;
511 /* find an unused unit number */
512 static int next_minor = 512;
513 int start = next_minor;
517 dev = MKDEV(MD_MAJOR, next_minor);
519 if (next_minor > MINORMASK)
521 if (next_minor == start) {
522 /* Oh dear, all in use. */
523 spin_unlock(&all_mddevs_lock);
529 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
530 if (mddev->unit == dev) {
536 new->md_minor = MINOR(dev);
537 new->hold_active = UNTIL_STOP;
538 list_add(&new->all_mddevs, &all_mddevs);
539 spin_unlock(&all_mddevs_lock);
542 spin_unlock(&all_mddevs_lock);
544 new = kzalloc(sizeof(*new), GFP_KERNEL);
549 if (MAJOR(unit) == MD_MAJOR)
550 new->md_minor = MINOR(unit);
552 new->md_minor = MINOR(unit) >> MdpMinorShift;
559 static inline int mddev_lock(mddev_t * mddev)
561 return mutex_lock_interruptible(&mddev->reconfig_mutex);
564 static inline int mddev_is_locked(mddev_t *mddev)
566 return mutex_is_locked(&mddev->reconfig_mutex);
569 static inline int mddev_trylock(mddev_t * mddev)
571 return mutex_trylock(&mddev->reconfig_mutex);
574 static struct attribute_group md_redundancy_group;
576 static void mddev_unlock(mddev_t * mddev)
578 if (mddev->to_remove) {
579 /* These cannot be removed under reconfig_mutex as
580 * an access to the files will try to take reconfig_mutex
581 * while holding the file unremovable, which leads to
583 * So hold open_mutex instead - we are allowed to take
584 * it while holding reconfig_mutex, and md_run can
585 * use it to wait for the remove to complete.
587 struct attribute_group *to_remove = mddev->to_remove;
588 mddev->to_remove = NULL;
589 mutex_lock(&mddev->open_mutex);
590 mutex_unlock(&mddev->reconfig_mutex);
592 if (mddev->kobj.sd) {
593 if (to_remove != &md_redundancy_group)
594 sysfs_remove_group(&mddev->kobj, to_remove);
595 if (mddev->pers == NULL ||
596 mddev->pers->sync_request == NULL) {
597 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
598 if (mddev->sysfs_action)
599 sysfs_put(mddev->sysfs_action);
600 mddev->sysfs_action = NULL;
603 mutex_unlock(&mddev->open_mutex);
605 mutex_unlock(&mddev->reconfig_mutex);
607 md_wakeup_thread(mddev->thread);
610 static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
614 list_for_each_entry(rdev, &mddev->disks, same_set)
615 if (rdev->desc_nr == nr)
621 static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
625 list_for_each_entry(rdev, &mddev->disks, same_set)
626 if (rdev->bdev->bd_dev == dev)
632 static struct mdk_personality *find_pers(int level, char *clevel)
634 struct mdk_personality *pers;
635 list_for_each_entry(pers, &pers_list, list) {
636 if (level != LEVEL_NONE && pers->level == level)
638 if (strcmp(pers->name, clevel)==0)
644 /* return the offset of the super block in 512byte sectors */
645 static inline sector_t calc_dev_sboffset(struct block_device *bdev)
647 sector_t num_sectors = bdev->bd_inode->i_size / 512;
648 return MD_NEW_SIZE_SECTORS(num_sectors);
651 static int alloc_disk_sb(mdk_rdev_t * rdev)
656 rdev->sb_page = alloc_page(GFP_KERNEL);
657 if (!rdev->sb_page) {
658 printk(KERN_ALERT "md: out of memory.\n");
665 static void free_disk_sb(mdk_rdev_t * rdev)
668 put_page(rdev->sb_page);
670 rdev->sb_page = NULL;
677 static void super_written(struct bio *bio, int error)
679 mdk_rdev_t *rdev = bio->bi_private;
680 mddev_t *mddev = rdev->mddev;
682 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
683 printk("md: super_written gets error=%d, uptodate=%d\n",
684 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
685 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
686 md_error(mddev, rdev);
689 if (atomic_dec_and_test(&mddev->pending_writes))
690 wake_up(&mddev->sb_wait);
694 static void super_written_barrier(struct bio *bio, int error)
696 struct bio *bio2 = bio->bi_private;
697 mdk_rdev_t *rdev = bio2->bi_private;
698 mddev_t *mddev = rdev->mddev;
700 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
701 error == -EOPNOTSUPP) {
703 /* barriers don't appear to be supported :-( */
704 set_bit(BarriersNotsupp, &rdev->flags);
705 mddev->barriers_work = 0;
706 spin_lock_irqsave(&mddev->write_lock, flags);
707 bio2->bi_next = mddev->biolist;
708 mddev->biolist = bio2;
709 spin_unlock_irqrestore(&mddev->write_lock, flags);
710 wake_up(&mddev->sb_wait);
714 bio->bi_private = rdev;
715 super_written(bio, error);
719 void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
720 sector_t sector, int size, struct page *page)
722 /* write first size bytes of page to sector of rdev
723 * Increment mddev->pending_writes before returning
724 * and decrement it on completion, waking up sb_wait
725 * if zero is reached.
726 * If an error occurred, call md_error
728 * As we might need to resubmit the request if BIO_RW_BARRIER
729 * causes ENOTSUPP, we allocate a spare bio...
731 struct bio *bio = bio_alloc(GFP_NOIO, 1);
732 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG);
734 bio->bi_bdev = rdev->bdev;
735 bio->bi_sector = sector;
736 bio_add_page(bio, page, size, 0);
737 bio->bi_private = rdev;
738 bio->bi_end_io = super_written;
741 atomic_inc(&mddev->pending_writes);
742 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
744 rw |= (1<<BIO_RW_BARRIER);
745 rbio = bio_clone(bio, GFP_NOIO);
746 rbio->bi_private = bio;
747 rbio->bi_end_io = super_written_barrier;
748 submit_bio(rw, rbio);
753 void md_super_wait(mddev_t *mddev)
755 /* wait for all superblock writes that were scheduled to complete.
756 * if any had to be retried (due to BARRIER problems), retry them
760 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
761 if (atomic_read(&mddev->pending_writes)==0)
763 while (mddev->biolist) {
765 spin_lock_irq(&mddev->write_lock);
766 bio = mddev->biolist;
767 mddev->biolist = bio->bi_next ;
769 spin_unlock_irq(&mddev->write_lock);
770 submit_bio(bio->bi_rw, bio);
774 finish_wait(&mddev->sb_wait, &wq);
777 static void bi_complete(struct bio *bio, int error)
779 complete((struct completion*)bio->bi_private);
782 int sync_page_io(struct block_device *bdev, sector_t sector, int size,
783 struct page *page, int rw)
785 struct bio *bio = bio_alloc(GFP_NOIO, 1);
786 struct completion event;
789 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
792 bio->bi_sector = sector;
793 bio_add_page(bio, page, size, 0);
794 init_completion(&event);
795 bio->bi_private = &event;
796 bio->bi_end_io = bi_complete;
798 wait_for_completion(&event);
800 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
804 EXPORT_SYMBOL_GPL(sync_page_io);
806 static int read_disk_sb(mdk_rdev_t * rdev, int size)
808 char b[BDEVNAME_SIZE];
809 if (!rdev->sb_page) {
817 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
823 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
824 bdevname(rdev->bdev,b));
828 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
830 return sb1->set_uuid0 == sb2->set_uuid0 &&
831 sb1->set_uuid1 == sb2->set_uuid1 &&
832 sb1->set_uuid2 == sb2->set_uuid2 &&
833 sb1->set_uuid3 == sb2->set_uuid3;
836 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
839 mdp_super_t *tmp1, *tmp2;
841 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
842 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
844 if (!tmp1 || !tmp2) {
846 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
854 * nr_disks is not constant
859 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
867 static u32 md_csum_fold(u32 csum)
869 csum = (csum & 0xffff) + (csum >> 16);
870 return (csum & 0xffff) + (csum >> 16);
873 static unsigned int calc_sb_csum(mdp_super_t * sb)
876 u32 *sb32 = (u32*)sb;
878 unsigned int disk_csum, csum;
880 disk_csum = sb->sb_csum;
883 for (i = 0; i < MD_SB_BYTES/4 ; i++)
885 csum = (newcsum & 0xffffffff) + (newcsum>>32);
889 /* This used to use csum_partial, which was wrong for several
890 * reasons including that different results are returned on
891 * different architectures. It isn't critical that we get exactly
892 * the same return value as before (we always csum_fold before
893 * testing, and that removes any differences). However as we
894 * know that csum_partial always returned a 16bit value on
895 * alphas, do a fold to maximise conformity to previous behaviour.
897 sb->sb_csum = md_csum_fold(disk_csum);
899 sb->sb_csum = disk_csum;
906 * Handle superblock details.
907 * We want to be able to handle multiple superblock formats
908 * so we have a common interface to them all, and an array of
909 * different handlers.
910 * We rely on user-space to write the initial superblock, and support
911 * reading and updating of superblocks.
912 * Interface methods are:
913 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
914 * loads and validates a superblock on dev.
915 * if refdev != NULL, compare superblocks on both devices
917 * 0 - dev has a superblock that is compatible with refdev
918 * 1 - dev has a superblock that is compatible and newer than refdev
919 * so dev should be used as the refdev in future
920 * -EINVAL superblock incompatible or invalid
921 * -othererror e.g. -EIO
923 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
924 * Verify that dev is acceptable into mddev.
925 * The first time, mddev->raid_disks will be 0, and data from
926 * dev should be merged in. Subsequent calls check that dev
927 * is new enough. Return 0 or -EINVAL
929 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
930 * Update the superblock for rdev with data in mddev
931 * This does not write to disc.
937 struct module *owner;
938 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
940 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
941 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
942 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
943 sector_t num_sectors);
947 * Check that the given mddev has no bitmap.
949 * This function is called from the run method of all personalities that do not
950 * support bitmaps. It prints an error message and returns non-zero if mddev
951 * has a bitmap. Otherwise, it returns 0.
954 int md_check_no_bitmap(mddev_t *mddev)
956 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
958 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
959 mdname(mddev), mddev->pers->name);
962 EXPORT_SYMBOL(md_check_no_bitmap);
965 * load_super for 0.90.0
967 static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
969 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
974 * Calculate the position of the superblock (512byte sectors),
975 * it's at the end of the disk.
977 * It also happens to be a multiple of 4Kb.
979 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
981 ret = read_disk_sb(rdev, MD_SB_BYTES);
986 bdevname(rdev->bdev, b);
987 sb = (mdp_super_t*)page_address(rdev->sb_page);
989 if (sb->md_magic != MD_SB_MAGIC) {
990 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
995 if (sb->major_version != 0 ||
996 sb->minor_version < 90 ||
997 sb->minor_version > 91) {
998 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
999 sb->major_version, sb->minor_version,
1004 if (sb->raid_disks <= 0)
1007 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
1008 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1013 rdev->preferred_minor = sb->md_minor;
1014 rdev->data_offset = 0;
1015 rdev->sb_size = MD_SB_BYTES;
1017 if (sb->level == LEVEL_MULTIPATH)
1020 rdev->desc_nr = sb->this_disk.number;
1026 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
1027 if (!uuid_equal(refsb, sb)) {
1028 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1029 b, bdevname(refdev->bdev,b2));
1032 if (!sb_equal(refsb, sb)) {
1033 printk(KERN_WARNING "md: %s has same UUID"
1034 " but different superblock to %s\n",
1035 b, bdevname(refdev->bdev, b2));
1039 ev2 = md_event(refsb);
1045 rdev->sectors = rdev->sb_start;
1047 if (rdev->sectors < sb->size * 2 && sb->level > 1)
1048 /* "this cannot possibly happen" ... */
1056 * validate_super for 0.90.0
1058 static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1061 mdp_super_t *sb = (mdp_super_t *)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(WriteMostly, &rdev->flags);
1068 clear_bit(BarriersNotsupp, &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 = sb->size * 2;
1083 mddev->events = ev1;
1084 mddev->bitmap_info.offset = 0;
1085 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
1087 if (mddev->minor_version >= 91) {
1088 mddev->reshape_position = sb->reshape_position;
1089 mddev->delta_disks = sb->delta_disks;
1090 mddev->new_level = sb->new_level;
1091 mddev->new_layout = sb->new_layout;
1092 mddev->new_chunk_sectors = sb->new_chunk >> 9;
1094 mddev->reshape_position = MaxSector;
1095 mddev->delta_disks = 0;
1096 mddev->new_level = mddev->level;
1097 mddev->new_layout = mddev->layout;
1098 mddev->new_chunk_sectors = mddev->chunk_sectors;
1101 if (sb->state & (1<<MD_SB_CLEAN))
1102 mddev->recovery_cp = MaxSector;
1104 if (sb->events_hi == sb->cp_events_hi &&
1105 sb->events_lo == sb->cp_events_lo) {
1106 mddev->recovery_cp = sb->recovery_cp;
1108 mddev->recovery_cp = 0;
1111 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1112 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1113 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1114 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1116 mddev->max_disks = MD_SB_DISKS;
1118 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
1119 mddev->bitmap_info.file == NULL)
1120 mddev->bitmap_info.offset =
1121 mddev->bitmap_info.default_offset;
1123 } else if (mddev->pers == NULL) {
1124 /* Insist on good event counter while assembling, except
1125 * for spares (which don't need an event count) */
1127 if (sb->disks[rdev->desc_nr].state & (
1128 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1129 if (ev1 < mddev->events)
1131 } else if (mddev->bitmap) {
1132 /* if adding to array with a bitmap, then we can accept an
1133 * older device ... but not too old.
1135 if (ev1 < mddev->bitmap->events_cleared)
1138 if (ev1 < mddev->events)
1139 /* just a hot-add of a new device, leave raid_disk at -1 */
1143 if (mddev->level != LEVEL_MULTIPATH) {
1144 desc = sb->disks + rdev->desc_nr;
1146 if (desc->state & (1<<MD_DISK_FAULTY))
1147 set_bit(Faulty, &rdev->flags);
1148 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1149 desc->raid_disk < mddev->raid_disks */) {
1150 set_bit(In_sync, &rdev->flags);
1151 rdev->raid_disk = desc->raid_disk;
1152 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1153 /* active but not in sync implies recovery up to
1154 * reshape position. We don't know exactly where
1155 * that is, so set to zero for now */
1156 if (mddev->minor_version >= 91) {
1157 rdev->recovery_offset = 0;
1158 rdev->raid_disk = desc->raid_disk;
1161 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1162 set_bit(WriteMostly, &rdev->flags);
1163 } else /* MULTIPATH are always insync */
1164 set_bit(In_sync, &rdev->flags);
1169 * sync_super for 0.90.0
1171 static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1175 int next_spare = mddev->raid_disks;
1178 /* make rdev->sb match mddev data..
1181 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1182 * 3/ any empty disks < next_spare become removed
1184 * disks[0] gets initialised to REMOVED because
1185 * we cannot be sure from other fields if it has
1186 * been initialised or not.
1189 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1191 rdev->sb_size = MD_SB_BYTES;
1193 sb = (mdp_super_t*)page_address(rdev->sb_page);
1195 memset(sb, 0, sizeof(*sb));
1197 sb->md_magic = MD_SB_MAGIC;
1198 sb->major_version = mddev->major_version;
1199 sb->patch_version = mddev->patch_version;
1200 sb->gvalid_words = 0; /* ignored */
1201 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1202 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1203 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1204 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1206 sb->ctime = mddev->ctime;
1207 sb->level = mddev->level;
1208 sb->size = mddev->dev_sectors / 2;
1209 sb->raid_disks = mddev->raid_disks;
1210 sb->md_minor = mddev->md_minor;
1211 sb->not_persistent = 0;
1212 sb->utime = mddev->utime;
1214 sb->events_hi = (mddev->events>>32);
1215 sb->events_lo = (u32)mddev->events;
1217 if (mddev->reshape_position == MaxSector)
1218 sb->minor_version = 90;
1220 sb->minor_version = 91;
1221 sb->reshape_position = mddev->reshape_position;
1222 sb->new_level = mddev->new_level;
1223 sb->delta_disks = mddev->delta_disks;
1224 sb->new_layout = mddev->new_layout;
1225 sb->new_chunk = mddev->new_chunk_sectors << 9;
1227 mddev->minor_version = sb->minor_version;
1230 sb->recovery_cp = mddev->recovery_cp;
1231 sb->cp_events_hi = (mddev->events>>32);
1232 sb->cp_events_lo = (u32)mddev->events;
1233 if (mddev->recovery_cp == MaxSector)
1234 sb->state = (1<< MD_SB_CLEAN);
1236 sb->recovery_cp = 0;
1238 sb->layout = mddev->layout;
1239 sb->chunk_size = mddev->chunk_sectors << 9;
1241 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
1242 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1244 sb->disks[0].state = (1<<MD_DISK_REMOVED);
1245 list_for_each_entry(rdev2, &mddev->disks, same_set) {
1248 int is_active = test_bit(In_sync, &rdev2->flags);
1250 if (rdev2->raid_disk >= 0 &&
1251 sb->minor_version >= 91)
1252 /* we have nowhere to store the recovery_offset,
1253 * but if it is not below the reshape_position,
1254 * we can piggy-back on that.
1257 if (rdev2->raid_disk < 0 ||
1258 test_bit(Faulty, &rdev2->flags))
1261 desc_nr = rdev2->raid_disk;
1263 desc_nr = next_spare++;
1264 rdev2->desc_nr = desc_nr;
1265 d = &sb->disks[rdev2->desc_nr];
1267 d->number = rdev2->desc_nr;
1268 d->major = MAJOR(rdev2->bdev->bd_dev);
1269 d->minor = MINOR(rdev2->bdev->bd_dev);
1271 d->raid_disk = rdev2->raid_disk;
1273 d->raid_disk = rdev2->desc_nr; /* compatibility */
1274 if (test_bit(Faulty, &rdev2->flags))
1275 d->state = (1<<MD_DISK_FAULTY);
1276 else if (is_active) {
1277 d->state = (1<<MD_DISK_ACTIVE);
1278 if (test_bit(In_sync, &rdev2->flags))
1279 d->state |= (1<<MD_DISK_SYNC);
1287 if (test_bit(WriteMostly, &rdev2->flags))
1288 d->state |= (1<<MD_DISK_WRITEMOSTLY);
1290 /* now set the "removed" and "faulty" bits on any missing devices */
1291 for (i=0 ; i < mddev->raid_disks ; i++) {
1292 mdp_disk_t *d = &sb->disks[i];
1293 if (d->state == 0 && d->number == 0) {
1296 d->state = (1<<MD_DISK_REMOVED);
1297 d->state |= (1<<MD_DISK_FAULTY);
1301 sb->nr_disks = nr_disks;
1302 sb->active_disks = active;
1303 sb->working_disks = working;
1304 sb->failed_disks = failed;
1305 sb->spare_disks = spare;
1307 sb->this_disk = sb->disks[rdev->desc_nr];
1308 sb->sb_csum = calc_sb_csum(sb);
1312 * rdev_size_change for 0.90.0
1314 static unsigned long long
1315 super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
1317 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1318 return 0; /* component must fit device */
1319 if (rdev->mddev->bitmap_info.offset)
1320 return 0; /* can't move bitmap */
1321 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
1322 if (!num_sectors || num_sectors > rdev->sb_start)
1323 num_sectors = rdev->sb_start;
1324 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1326 md_super_wait(rdev->mddev);
1327 return num_sectors / 2; /* kB for sysfs */
1332 * version 1 superblock
1335 static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
1339 unsigned long long newcsum;
1340 int size = 256 + le32_to_cpu(sb->max_dev)*2;
1341 __le32 *isuper = (__le32*)sb;
1344 disk_csum = sb->sb_csum;
1347 for (i=0; size>=4; size -= 4 )
1348 newcsum += le32_to_cpu(*isuper++);
1351 newcsum += le16_to_cpu(*(__le16*) isuper);
1353 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1354 sb->sb_csum = disk_csum;
1355 return cpu_to_le32(csum);
1358 static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1360 struct mdp_superblock_1 *sb;
1363 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1367 * Calculate the position of the superblock in 512byte sectors.
1368 * It is always aligned to a 4K boundary and
1369 * depeding on minor_version, it can be:
1370 * 0: At least 8K, but less than 12K, from end of device
1371 * 1: At start of device
1372 * 2: 4K from start of device.
1374 switch(minor_version) {
1376 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1378 sb_start &= ~(sector_t)(4*2-1);
1389 rdev->sb_start = sb_start;
1391 /* superblock is rarely larger than 1K, but it can be larger,
1392 * and it is safe to read 4k, so we do that
1394 ret = read_disk_sb(rdev, 4096);
1395 if (ret) return ret;
1398 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1400 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1401 sb->major_version != cpu_to_le32(1) ||
1402 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1403 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1404 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1407 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1408 printk("md: invalid superblock checksum on %s\n",
1409 bdevname(rdev->bdev,b));
1412 if (le64_to_cpu(sb->data_size) < 10) {
1413 printk("md: data_size too small on %s\n",
1414 bdevname(rdev->bdev,b));
1418 rdev->preferred_minor = 0xffff;
1419 rdev->data_offset = le64_to_cpu(sb->data_offset);
1420 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1422 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1423 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1424 if (rdev->sb_size & bmask)
1425 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1428 && rdev->data_offset < sb_start + (rdev->sb_size/512))
1431 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1434 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1440 struct mdp_superblock_1 *refsb =
1441 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1443 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1444 sb->level != refsb->level ||
1445 sb->layout != refsb->layout ||
1446 sb->chunksize != refsb->chunksize) {
1447 printk(KERN_WARNING "md: %s has strangely different"
1448 " superblock to %s\n",
1449 bdevname(rdev->bdev,b),
1450 bdevname(refdev->bdev,b2));
1453 ev1 = le64_to_cpu(sb->events);
1454 ev2 = le64_to_cpu(refsb->events);
1462 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1463 le64_to_cpu(sb->data_offset);
1465 rdev->sectors = rdev->sb_start;
1466 if (rdev->sectors < le64_to_cpu(sb->data_size))
1468 rdev->sectors = le64_to_cpu(sb->data_size);
1469 if (le64_to_cpu(sb->size) > rdev->sectors)
1474 static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1476 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1477 __u64 ev1 = le64_to_cpu(sb->events);
1479 rdev->raid_disk = -1;
1480 clear_bit(Faulty, &rdev->flags);
1481 clear_bit(In_sync, &rdev->flags);
1482 clear_bit(WriteMostly, &rdev->flags);
1483 clear_bit(BarriersNotsupp, &rdev->flags);
1485 if (mddev->raid_disks == 0) {
1486 mddev->major_version = 1;
1487 mddev->patch_version = 0;
1488 mddev->external = 0;
1489 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
1490 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1491 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1492 mddev->level = le32_to_cpu(sb->level);
1493 mddev->clevel[0] = 0;
1494 mddev->layout = le32_to_cpu(sb->layout);
1495 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1496 mddev->dev_sectors = le64_to_cpu(sb->size);
1497 mddev->events = ev1;
1498 mddev->bitmap_info.offset = 0;
1499 mddev->bitmap_info.default_offset = 1024 >> 9;
1501 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1502 memcpy(mddev->uuid, sb->set_uuid, 16);
1504 mddev->max_disks = (4096-256)/2;
1506 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1507 mddev->bitmap_info.file == NULL )
1508 mddev->bitmap_info.offset =
1509 (__s32)le32_to_cpu(sb->bitmap_offset);
1511 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1512 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1513 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1514 mddev->new_level = le32_to_cpu(sb->new_level);
1515 mddev->new_layout = le32_to_cpu(sb->new_layout);
1516 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
1518 mddev->reshape_position = MaxSector;
1519 mddev->delta_disks = 0;
1520 mddev->new_level = mddev->level;
1521 mddev->new_layout = mddev->layout;
1522 mddev->new_chunk_sectors = mddev->chunk_sectors;
1525 } else if (mddev->pers == NULL) {
1526 /* Insist of good event counter while assembling, except for
1527 * spares (which don't need an event count) */
1529 if (rdev->desc_nr >= 0 &&
1530 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1531 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1532 if (ev1 < mddev->events)
1534 } else if (mddev->bitmap) {
1535 /* If adding to array with a bitmap, then we can accept an
1536 * older device, but not too old.
1538 if (ev1 < mddev->bitmap->events_cleared)
1541 if (ev1 < mddev->events)
1542 /* just a hot-add of a new device, leave raid_disk at -1 */
1545 if (mddev->level != LEVEL_MULTIPATH) {
1547 if (rdev->desc_nr < 0 ||
1548 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1552 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1554 case 0xffff: /* spare */
1556 case 0xfffe: /* faulty */
1557 set_bit(Faulty, &rdev->flags);
1560 if ((le32_to_cpu(sb->feature_map) &
1561 MD_FEATURE_RECOVERY_OFFSET))
1562 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1564 set_bit(In_sync, &rdev->flags);
1565 rdev->raid_disk = role;
1568 if (sb->devflags & WriteMostly1)
1569 set_bit(WriteMostly, &rdev->flags);
1570 } else /* MULTIPATH are always insync */
1571 set_bit(In_sync, &rdev->flags);
1576 static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1578 struct mdp_superblock_1 *sb;
1581 /* make rdev->sb match mddev and rdev data. */
1583 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1585 sb->feature_map = 0;
1587 sb->recovery_offset = cpu_to_le64(0);
1588 memset(sb->pad1, 0, sizeof(sb->pad1));
1589 memset(sb->pad2, 0, sizeof(sb->pad2));
1590 memset(sb->pad3, 0, sizeof(sb->pad3));
1592 sb->utime = cpu_to_le64((__u64)mddev->utime);
1593 sb->events = cpu_to_le64(mddev->events);
1595 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1597 sb->resync_offset = cpu_to_le64(0);
1599 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1601 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1602 sb->size = cpu_to_le64(mddev->dev_sectors);
1603 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
1604 sb->level = cpu_to_le32(mddev->level);
1605 sb->layout = cpu_to_le32(mddev->layout);
1607 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1608 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
1609 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1612 if (rdev->raid_disk >= 0 &&
1613 !test_bit(In_sync, &rdev->flags)) {
1615 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1616 sb->recovery_offset =
1617 cpu_to_le64(rdev->recovery_offset);
1620 if (mddev->reshape_position != MaxSector) {
1621 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1622 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1623 sb->new_layout = cpu_to_le32(mddev->new_layout);
1624 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1625 sb->new_level = cpu_to_le32(mddev->new_level);
1626 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
1630 list_for_each_entry(rdev2, &mddev->disks, same_set)
1631 if (rdev2->desc_nr+1 > max_dev)
1632 max_dev = rdev2->desc_nr+1;
1634 if (max_dev > le32_to_cpu(sb->max_dev)) {
1636 sb->max_dev = cpu_to_le32(max_dev);
1637 rdev->sb_size = max_dev * 2 + 256;
1638 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1639 if (rdev->sb_size & bmask)
1640 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1642 for (i=0; i<max_dev;i++)
1643 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1645 list_for_each_entry(rdev2, &mddev->disks, same_set) {
1647 if (test_bit(Faulty, &rdev2->flags))
1648 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1649 else if (test_bit(In_sync, &rdev2->flags))
1650 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1651 else if (rdev2->raid_disk >= 0)
1652 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1654 sb->dev_roles[i] = cpu_to_le16(0xffff);
1657 sb->sb_csum = calc_sb_1_csum(sb);
1660 static unsigned long long
1661 super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
1663 struct mdp_superblock_1 *sb;
1664 sector_t max_sectors;
1665 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1666 return 0; /* component must fit device */
1667 if (rdev->sb_start < rdev->data_offset) {
1668 /* minor versions 1 and 2; superblock before data */
1669 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1670 max_sectors -= rdev->data_offset;
1671 if (!num_sectors || num_sectors > max_sectors)
1672 num_sectors = max_sectors;
1673 } else if (rdev->mddev->bitmap_info.offset) {
1674 /* minor version 0 with bitmap we can't move */
1677 /* minor version 0; superblock after data */
1679 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1680 sb_start &= ~(sector_t)(4*2 - 1);
1681 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
1682 if (!num_sectors || num_sectors > max_sectors)
1683 num_sectors = max_sectors;
1684 rdev->sb_start = sb_start;
1686 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
1687 sb->data_size = cpu_to_le64(num_sectors);
1688 sb->super_offset = rdev->sb_start;
1689 sb->sb_csum = calc_sb_1_csum(sb);
1690 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1692 md_super_wait(rdev->mddev);
1693 return num_sectors / 2; /* kB for sysfs */
1696 static struct super_type super_types[] = {
1699 .owner = THIS_MODULE,
1700 .load_super = super_90_load,
1701 .validate_super = super_90_validate,
1702 .sync_super = super_90_sync,
1703 .rdev_size_change = super_90_rdev_size_change,
1707 .owner = THIS_MODULE,
1708 .load_super = super_1_load,
1709 .validate_super = super_1_validate,
1710 .sync_super = super_1_sync,
1711 .rdev_size_change = super_1_rdev_size_change,
1715 static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1717 mdk_rdev_t *rdev, *rdev2;
1720 rdev_for_each_rcu(rdev, mddev1)
1721 rdev_for_each_rcu(rdev2, mddev2)
1722 if (rdev->bdev->bd_contains ==
1723 rdev2->bdev->bd_contains) {
1731 static LIST_HEAD(pending_raid_disks);
1734 * Try to register data integrity profile for an mddev
1736 * This is called when an array is started and after a disk has been kicked
1737 * from the array. It only succeeds if all working and active component devices
1738 * are integrity capable with matching profiles.
1740 int md_integrity_register(mddev_t *mddev)
1742 mdk_rdev_t *rdev, *reference = NULL;
1744 if (list_empty(&mddev->disks))
1745 return 0; /* nothing to do */
1746 if (blk_get_integrity(mddev->gendisk))
1747 return 0; /* already registered */
1748 list_for_each_entry(rdev, &mddev->disks, same_set) {
1749 /* skip spares and non-functional disks */
1750 if (test_bit(Faulty, &rdev->flags))
1752 if (rdev->raid_disk < 0)
1755 * If at least one rdev is not integrity capable, we can not
1756 * enable data integrity for the md device.
1758 if (!bdev_get_integrity(rdev->bdev))
1761 /* Use the first rdev as the reference */
1765 /* does this rdev's profile match the reference profile? */
1766 if (blk_integrity_compare(reference->bdev->bd_disk,
1767 rdev->bdev->bd_disk) < 0)
1771 * All component devices are integrity capable and have matching
1772 * profiles, register the common profile for the md device.
1774 if (blk_integrity_register(mddev->gendisk,
1775 bdev_get_integrity(reference->bdev)) != 0) {
1776 printk(KERN_ERR "md: failed to register integrity for %s\n",
1780 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1784 EXPORT_SYMBOL(md_integrity_register);
1786 /* Disable data integrity if non-capable/non-matching disk is being added */
1787 void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1789 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1790 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1792 if (!bi_mddev) /* nothing to do */
1794 if (rdev->raid_disk < 0) /* skip spares */
1796 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1797 rdev->bdev->bd_disk) >= 0)
1799 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1800 blk_integrity_unregister(mddev->gendisk);
1802 EXPORT_SYMBOL(md_integrity_add_rdev);
1804 static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1806 char b[BDEVNAME_SIZE];
1816 /* prevent duplicates */
1817 if (find_rdev(mddev, rdev->bdev->bd_dev))
1820 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1821 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1822 rdev->sectors < mddev->dev_sectors)) {
1824 /* Cannot change size, so fail
1825 * If mddev->level <= 0, then we don't care
1826 * about aligning sizes (e.g. linear)
1828 if (mddev->level > 0)
1831 mddev->dev_sectors = rdev->sectors;
1834 /* Verify rdev->desc_nr is unique.
1835 * If it is -1, assign a free number, else
1836 * check number is not in use
1838 if (rdev->desc_nr < 0) {
1840 if (mddev->pers) choice = mddev->raid_disks;
1841 while (find_rdev_nr(mddev, choice))
1843 rdev->desc_nr = choice;
1845 if (find_rdev_nr(mddev, rdev->desc_nr))
1848 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1849 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1850 mdname(mddev), mddev->max_disks);
1853 bdevname(rdev->bdev,b);
1854 while ( (s=strchr(b, '/')) != NULL)
1857 rdev->mddev = mddev;
1858 printk(KERN_INFO "md: bind<%s>\n", b);
1860 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
1863 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
1864 if (sysfs_create_link(&rdev->kobj, ko, "block"))
1865 /* failure here is OK */;
1866 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
1868 list_add_rcu(&rdev->same_set, &mddev->disks);
1869 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
1871 /* May as well allow recovery to be retried once */
1872 mddev->recovery_disabled = 0;
1877 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1882 static void md_delayed_delete(struct work_struct *ws)
1884 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1885 kobject_del(&rdev->kobj);
1886 kobject_put(&rdev->kobj);
1889 static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1891 char b[BDEVNAME_SIZE];
1896 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
1897 list_del_rcu(&rdev->same_set);
1898 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1900 sysfs_remove_link(&rdev->kobj, "block");
1901 sysfs_put(rdev->sysfs_state);
1902 rdev->sysfs_state = NULL;
1903 /* We need to delay this, otherwise we can deadlock when
1904 * writing to 'remove' to "dev/state". We also need
1905 * to delay it due to rcu usage.
1908 INIT_WORK(&rdev->del_work, md_delayed_delete);
1909 kobject_get(&rdev->kobj);
1910 schedule_work(&rdev->del_work);
1914 * prevent the device from being mounted, repartitioned or
1915 * otherwise reused by a RAID array (or any other kernel
1916 * subsystem), by bd_claiming the device.
1918 static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
1921 struct block_device *bdev;
1922 char b[BDEVNAME_SIZE];
1924 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1926 printk(KERN_ERR "md: could not open %s.\n",
1927 __bdevname(dev, b));
1928 return PTR_ERR(bdev);
1930 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
1932 printk(KERN_ERR "md: could not bd_claim %s.\n",
1934 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
1938 set_bit(AllReserved, &rdev->flags);
1943 static void unlock_rdev(mdk_rdev_t *rdev)
1945 struct block_device *bdev = rdev->bdev;
1950 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
1953 void md_autodetect_dev(dev_t dev);
1955 static void export_rdev(mdk_rdev_t * rdev)
1957 char b[BDEVNAME_SIZE];
1958 printk(KERN_INFO "md: export_rdev(%s)\n",
1959 bdevname(rdev->bdev,b));
1964 if (test_bit(AutoDetected, &rdev->flags))
1965 md_autodetect_dev(rdev->bdev->bd_dev);
1968 kobject_put(&rdev->kobj);
1971 static void kick_rdev_from_array(mdk_rdev_t * rdev)
1973 unbind_rdev_from_array(rdev);
1977 static void export_array(mddev_t *mddev)
1979 mdk_rdev_t *rdev, *tmp;
1981 rdev_for_each(rdev, tmp, mddev) {
1986 kick_rdev_from_array(rdev);
1988 if (!list_empty(&mddev->disks))
1990 mddev->raid_disks = 0;
1991 mddev->major_version = 0;
1994 static void print_desc(mdp_disk_t *desc)
1996 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1997 desc->major,desc->minor,desc->raid_disk,desc->state);
2000 static void print_sb_90(mdp_super_t *sb)
2005 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2006 sb->major_version, sb->minor_version, sb->patch_version,
2007 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2009 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2010 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2011 sb->md_minor, sb->layout, sb->chunk_size);
2012 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2013 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2014 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2015 sb->failed_disks, sb->spare_disks,
2016 sb->sb_csum, (unsigned long)sb->events_lo);
2019 for (i = 0; i < MD_SB_DISKS; i++) {
2022 desc = sb->disks + i;
2023 if (desc->number || desc->major || desc->minor ||
2024 desc->raid_disk || (desc->state && (desc->state != 4))) {
2025 printk(" D %2d: ", i);
2029 printk(KERN_INFO "md: THIS: ");
2030 print_desc(&sb->this_disk);
2033 static void print_sb_1(struct mdp_superblock_1 *sb)
2037 uuid = sb->set_uuid;
2039 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
2040 "md: Name: \"%s\" CT:%llu\n",
2041 le32_to_cpu(sb->major_version),
2042 le32_to_cpu(sb->feature_map),
2045 (unsigned long long)le64_to_cpu(sb->ctime)
2046 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2048 uuid = sb->device_uuid;
2050 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
2052 "md: Dev:%08x UUID: %pU\n"
2053 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2054 "md: (MaxDev:%u) \n",
2055 le32_to_cpu(sb->level),
2056 (unsigned long long)le64_to_cpu(sb->size),
2057 le32_to_cpu(sb->raid_disks),
2058 le32_to_cpu(sb->layout),
2059 le32_to_cpu(sb->chunksize),
2060 (unsigned long long)le64_to_cpu(sb->data_offset),
2061 (unsigned long long)le64_to_cpu(sb->data_size),
2062 (unsigned long long)le64_to_cpu(sb->super_offset),
2063 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2064 le32_to_cpu(sb->dev_number),
2067 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2068 (unsigned long long)le64_to_cpu(sb->events),
2069 (unsigned long long)le64_to_cpu(sb->resync_offset),
2070 le32_to_cpu(sb->sb_csum),
2071 le32_to_cpu(sb->max_dev)
2075 static void print_rdev(mdk_rdev_t *rdev, int major_version)
2077 char b[BDEVNAME_SIZE];
2078 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2079 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
2080 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2082 if (rdev->sb_loaded) {
2083 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2084 switch (major_version) {
2086 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
2089 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
2093 printk(KERN_INFO "md: no rdev superblock!\n");
2096 static void md_print_devices(void)
2098 struct list_head *tmp;
2101 char b[BDEVNAME_SIZE];
2104 printk("md: **********************************\n");
2105 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2106 printk("md: **********************************\n");
2107 for_each_mddev(mddev, tmp) {
2110 bitmap_print_sb(mddev->bitmap);
2112 printk("%s: ", mdname(mddev));
2113 list_for_each_entry(rdev, &mddev->disks, same_set)
2114 printk("<%s>", bdevname(rdev->bdev,b));
2117 list_for_each_entry(rdev, &mddev->disks, same_set)
2118 print_rdev(rdev, mddev->major_version);
2120 printk("md: **********************************\n");
2125 static void sync_sbs(mddev_t * mddev, int nospares)
2127 /* Update each superblock (in-memory image), but
2128 * if we are allowed to, skip spares which already
2129 * have the right event counter, or have one earlier
2130 * (which would mean they aren't being marked as dirty
2131 * with the rest of the array)
2135 /* First make sure individual recovery_offsets are correct */
2136 list_for_each_entry(rdev, &mddev->disks, same_set) {
2137 if (rdev->raid_disk >= 0 &&
2138 mddev->delta_disks >= 0 &&
2139 !test_bit(In_sync, &rdev->flags) &&
2140 mddev->curr_resync_completed > rdev->recovery_offset)
2141 rdev->recovery_offset = mddev->curr_resync_completed;
2144 list_for_each_entry(rdev, &mddev->disks, same_set) {
2145 if (rdev->sb_events == mddev->events ||
2147 rdev->raid_disk < 0 &&
2148 rdev->sb_events+1 == mddev->events)) {
2149 /* Don't update this superblock */
2150 rdev->sb_loaded = 2;
2152 super_types[mddev->major_version].
2153 sync_super(mddev, rdev);
2154 rdev->sb_loaded = 1;
2159 static void md_update_sb(mddev_t * mddev, int force_change)
2165 mddev->utime = get_seconds();
2166 if (mddev->external)
2169 spin_lock_irq(&mddev->write_lock);
2171 set_bit(MD_CHANGE_PENDING, &mddev->flags);
2172 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2174 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2175 /* just a clean<-> dirty transition, possibly leave spares alone,
2176 * though if events isn't the right even/odd, we will have to do
2182 if (mddev->degraded)
2183 /* If the array is degraded, then skipping spares is both
2184 * dangerous and fairly pointless.
2185 * Dangerous because a device that was removed from the array
2186 * might have a event_count that still looks up-to-date,
2187 * so it can be re-added without a resync.
2188 * Pointless because if there are any spares to skip,
2189 * then a recovery will happen and soon that array won't
2190 * be degraded any more and the spare can go back to sleep then.
2194 sync_req = mddev->in_sync;
2196 /* If this is just a dirty<->clean transition, and the array is clean
2197 * and 'events' is odd, we can roll back to the previous clean state */
2199 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
2200 && mddev->can_decrease_events
2201 && mddev->events != 1) {
2203 mddev->can_decrease_events = 0;
2205 /* otherwise we have to go forward and ... */
2207 mddev->can_decrease_events = nospares;
2210 if (!mddev->events) {
2212 * oops, this 64-bit counter should never wrap.
2213 * Either we are in around ~1 trillion A.C., assuming
2214 * 1 reboot per second, or we have a bug:
2221 * do not write anything to disk if using
2222 * nonpersistent superblocks
2224 if (!mddev->persistent) {
2225 if (!mddev->external)
2226 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2228 spin_unlock_irq(&mddev->write_lock);
2229 wake_up(&mddev->sb_wait);
2232 sync_sbs(mddev, nospares);
2233 spin_unlock_irq(&mddev->write_lock);
2236 "md: updating %s RAID superblock on device (in sync %d)\n",
2237 mdname(mddev),mddev->in_sync);
2239 bitmap_update_sb(mddev->bitmap);
2240 list_for_each_entry(rdev, &mddev->disks, same_set) {
2241 char b[BDEVNAME_SIZE];
2242 dprintk(KERN_INFO "md: ");
2243 if (rdev->sb_loaded != 1)
2244 continue; /* no noise on spare devices */
2245 if (test_bit(Faulty, &rdev->flags))
2246 dprintk("(skipping faulty ");
2248 dprintk("%s ", bdevname(rdev->bdev,b));
2249 if (!test_bit(Faulty, &rdev->flags)) {
2250 md_super_write(mddev,rdev,
2251 rdev->sb_start, rdev->sb_size,
2253 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2254 bdevname(rdev->bdev,b),
2255 (unsigned long long)rdev->sb_start);
2256 rdev->sb_events = mddev->events;
2260 if (mddev->level == LEVEL_MULTIPATH)
2261 /* only need to write one superblock... */
2264 md_super_wait(mddev);
2265 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2267 spin_lock_irq(&mddev->write_lock);
2268 if (mddev->in_sync != sync_req ||
2269 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
2270 /* have to write it out again */
2271 spin_unlock_irq(&mddev->write_lock);
2274 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2275 spin_unlock_irq(&mddev->write_lock);
2276 wake_up(&mddev->sb_wait);
2277 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2278 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
2282 /* words written to sysfs files may, or may not, be \n terminated.
2283 * We want to accept with case. For this we use cmd_match.
2285 static int cmd_match(const char *cmd, const char *str)
2287 /* See if cmd, written into a sysfs file, matches
2288 * str. They must either be the same, or cmd can
2289 * have a trailing newline
2291 while (*cmd && *str && *cmd == *str) {
2302 struct rdev_sysfs_entry {
2303 struct attribute attr;
2304 ssize_t (*show)(mdk_rdev_t *, char *);
2305 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2309 state_show(mdk_rdev_t *rdev, char *page)
2314 if (test_bit(Faulty, &rdev->flags)) {
2315 len+= sprintf(page+len, "%sfaulty",sep);
2318 if (test_bit(In_sync, &rdev->flags)) {
2319 len += sprintf(page+len, "%sin_sync",sep);
2322 if (test_bit(WriteMostly, &rdev->flags)) {
2323 len += sprintf(page+len, "%swrite_mostly",sep);
2326 if (test_bit(Blocked, &rdev->flags)) {
2327 len += sprintf(page+len, "%sblocked", sep);
2330 if (!test_bit(Faulty, &rdev->flags) &&
2331 !test_bit(In_sync, &rdev->flags)) {
2332 len += sprintf(page+len, "%sspare", sep);
2335 return len+sprintf(page+len, "\n");
2339 state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2342 * faulty - simulates and error
2343 * remove - disconnects the device
2344 * writemostly - sets write_mostly
2345 * -writemostly - clears write_mostly
2346 * blocked - sets the Blocked flag
2347 * -blocked - clears the Blocked flag
2348 * insync - sets Insync providing device isn't active
2351 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2352 md_error(rdev->mddev, rdev);
2354 } else if (cmd_match(buf, "remove")) {
2355 if (rdev->raid_disk >= 0)
2358 mddev_t *mddev = rdev->mddev;
2359 kick_rdev_from_array(rdev);
2361 md_update_sb(mddev, 1);
2362 md_new_event(mddev);
2365 } else if (cmd_match(buf, "writemostly")) {
2366 set_bit(WriteMostly, &rdev->flags);
2368 } else if (cmd_match(buf, "-writemostly")) {
2369 clear_bit(WriteMostly, &rdev->flags);
2371 } else if (cmd_match(buf, "blocked")) {
2372 set_bit(Blocked, &rdev->flags);
2374 } else if (cmd_match(buf, "-blocked")) {
2375 clear_bit(Blocked, &rdev->flags);
2376 wake_up(&rdev->blocked_wait);
2377 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2378 md_wakeup_thread(rdev->mddev->thread);
2381 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2382 set_bit(In_sync, &rdev->flags);
2386 sysfs_notify_dirent_safe(rdev->sysfs_state);
2387 return err ? err : len;
2389 static struct rdev_sysfs_entry rdev_state =
2390 __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
2393 errors_show(mdk_rdev_t *rdev, char *page)
2395 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2399 errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2402 unsigned long n = simple_strtoul(buf, &e, 10);
2403 if (*buf && (*e == 0 || *e == '\n')) {
2404 atomic_set(&rdev->corrected_errors, n);
2409 static struct rdev_sysfs_entry rdev_errors =
2410 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
2413 slot_show(mdk_rdev_t *rdev, char *page)
2415 if (rdev->raid_disk < 0)
2416 return sprintf(page, "none\n");
2418 return sprintf(page, "%d\n", rdev->raid_disk);
2422 slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2427 int slot = simple_strtoul(buf, &e, 10);
2428 if (strncmp(buf, "none", 4)==0)
2430 else if (e==buf || (*e && *e!= '\n'))
2432 if (rdev->mddev->pers && slot == -1) {
2433 /* Setting 'slot' on an active array requires also
2434 * updating the 'rd%d' link, and communicating
2435 * with the personality with ->hot_*_disk.
2436 * For now we only support removing
2437 * failed/spare devices. This normally happens automatically,
2438 * but not when the metadata is externally managed.
2440 if (rdev->raid_disk == -1)
2442 /* personality does all needed checks */
2443 if (rdev->mddev->pers->hot_add_disk == NULL)
2445 err = rdev->mddev->pers->
2446 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2449 sprintf(nm, "rd%d", rdev->raid_disk);
2450 sysfs_remove_link(&rdev->mddev->kobj, nm);
2451 rdev->raid_disk = -1;
2452 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2453 md_wakeup_thread(rdev->mddev->thread);
2454 } else if (rdev->mddev->pers) {
2456 /* Activating a spare .. or possibly reactivating
2457 * if we ever get bitmaps working here.
2460 if (rdev->raid_disk != -1)
2463 if (rdev->mddev->pers->hot_add_disk == NULL)
2466 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
2467 if (rdev2->raid_disk == slot)
2470 rdev->raid_disk = slot;
2471 if (test_bit(In_sync, &rdev->flags))
2472 rdev->saved_raid_disk = slot;
2474 rdev->saved_raid_disk = -1;
2475 err = rdev->mddev->pers->
2476 hot_add_disk(rdev->mddev, rdev);
2478 rdev->raid_disk = -1;
2481 sysfs_notify_dirent_safe(rdev->sysfs_state);
2482 sprintf(nm, "rd%d", rdev->raid_disk);
2483 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2484 /* failure here is OK */;
2485 /* don't wakeup anyone, leave that to userspace. */
2487 if (slot >= rdev->mddev->raid_disks)
2489 rdev->raid_disk = slot;
2490 /* assume it is working */
2491 clear_bit(Faulty, &rdev->flags);
2492 clear_bit(WriteMostly, &rdev->flags);
2493 set_bit(In_sync, &rdev->flags);
2494 sysfs_notify_dirent_safe(rdev->sysfs_state);
2500 static struct rdev_sysfs_entry rdev_slot =
2501 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
2504 offset_show(mdk_rdev_t *rdev, char *page)
2506 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
2510 offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2513 unsigned long long offset = simple_strtoull(buf, &e, 10);
2514 if (e==buf || (*e && *e != '\n'))
2516 if (rdev->mddev->pers && rdev->raid_disk >= 0)
2518 if (rdev->sectors && rdev->mddev->external)
2519 /* Must set offset before size, so overlap checks
2522 rdev->data_offset = offset;
2526 static struct rdev_sysfs_entry rdev_offset =
2527 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
2530 rdev_size_show(mdk_rdev_t *rdev, char *page)
2532 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
2535 static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2537 /* check if two start/length pairs overlap */
2545 static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2547 unsigned long long blocks;
2550 if (strict_strtoull(buf, 10, &blocks) < 0)
2553 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2554 return -EINVAL; /* sector conversion overflow */
2557 if (new != blocks * 2)
2558 return -EINVAL; /* unsigned long long to sector_t overflow */
2565 rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2567 mddev_t *my_mddev = rdev->mddev;
2568 sector_t oldsectors = rdev->sectors;
2571 if (strict_blocks_to_sectors(buf, §ors) < 0)
2573 if (my_mddev->pers && rdev->raid_disk >= 0) {
2574 if (my_mddev->persistent) {
2575 sectors = super_types[my_mddev->major_version].
2576 rdev_size_change(rdev, sectors);
2579 } else if (!sectors)
2580 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2583 if (sectors < my_mddev->dev_sectors)
2584 return -EINVAL; /* component must fit device */
2586 rdev->sectors = sectors;
2587 if (sectors > oldsectors && my_mddev->external) {
2588 /* need to check that all other rdevs with the same ->bdev
2589 * do not overlap. We need to unlock the mddev to avoid
2590 * a deadlock. We have already changed rdev->sectors, and if
2591 * we have to change it back, we will have the lock again.
2595 struct list_head *tmp;
2597 mddev_unlock(my_mddev);
2598 for_each_mddev(mddev, tmp) {
2602 list_for_each_entry(rdev2, &mddev->disks, same_set)
2603 if (test_bit(AllReserved, &rdev2->flags) ||
2604 (rdev->bdev == rdev2->bdev &&
2606 overlaps(rdev->data_offset, rdev->sectors,
2612 mddev_unlock(mddev);
2618 mddev_lock(my_mddev);
2620 /* Someone else could have slipped in a size
2621 * change here, but doing so is just silly.
2622 * We put oldsectors back because we *know* it is
2623 * safe, and trust userspace not to race with
2626 rdev->sectors = oldsectors;
2633 static struct rdev_sysfs_entry rdev_size =
2634 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
2637 static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
2639 unsigned long long recovery_start = rdev->recovery_offset;
2641 if (test_bit(In_sync, &rdev->flags) ||
2642 recovery_start == MaxSector)
2643 return sprintf(page, "none\n");
2645 return sprintf(page, "%llu\n", recovery_start);
2648 static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2650 unsigned long long recovery_start;
2652 if (cmd_match(buf, "none"))
2653 recovery_start = MaxSector;
2654 else if (strict_strtoull(buf, 10, &recovery_start))
2657 if (rdev->mddev->pers &&
2658 rdev->raid_disk >= 0)
2661 rdev->recovery_offset = recovery_start;
2662 if (recovery_start == MaxSector)
2663 set_bit(In_sync, &rdev->flags);
2665 clear_bit(In_sync, &rdev->flags);
2669 static struct rdev_sysfs_entry rdev_recovery_start =
2670 __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2672 static struct attribute *rdev_default_attrs[] = {
2678 &rdev_recovery_start.attr,
2682 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2684 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2685 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2686 mddev_t *mddev = rdev->mddev;
2692 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2694 if (rdev->mddev == NULL)
2697 rv = entry->show(rdev, page);
2698 mddev_unlock(mddev);
2704 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2705 const char *page, size_t length)
2707 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2708 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2710 mddev_t *mddev = rdev->mddev;
2714 if (!capable(CAP_SYS_ADMIN))
2716 rv = mddev ? mddev_lock(mddev): -EBUSY;
2718 if (rdev->mddev == NULL)
2721 rv = entry->store(rdev, page, length);
2722 mddev_unlock(mddev);
2727 static void rdev_free(struct kobject *ko)
2729 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2732 static const struct sysfs_ops rdev_sysfs_ops = {
2733 .show = rdev_attr_show,
2734 .store = rdev_attr_store,
2736 static struct kobj_type rdev_ktype = {
2737 .release = rdev_free,
2738 .sysfs_ops = &rdev_sysfs_ops,
2739 .default_attrs = rdev_default_attrs,
2742 void md_rdev_init(mdk_rdev_t *rdev)
2745 rdev->saved_raid_disk = -1;
2746 rdev->raid_disk = -1;
2748 rdev->data_offset = 0;
2749 rdev->sb_events = 0;
2750 rdev->last_read_error.tv_sec = 0;
2751 rdev->last_read_error.tv_nsec = 0;
2752 atomic_set(&rdev->nr_pending, 0);
2753 atomic_set(&rdev->read_errors, 0);
2754 atomic_set(&rdev->corrected_errors, 0);
2756 INIT_LIST_HEAD(&rdev->same_set);
2757 init_waitqueue_head(&rdev->blocked_wait);
2759 EXPORT_SYMBOL_GPL(md_rdev_init);
2761 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2763 * mark the device faulty if:
2765 * - the device is nonexistent (zero size)
2766 * - the device has no valid superblock
2768 * a faulty rdev _never_ has rdev->sb set.
2770 static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2772 char b[BDEVNAME_SIZE];
2777 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
2779 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2780 return ERR_PTR(-ENOMEM);
2784 if ((err = alloc_disk_sb(rdev)))
2787 err = lock_rdev(rdev, newdev, super_format == -2);
2791 kobject_init(&rdev->kobj, &rdev_ktype);
2793 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2796 "md: %s has zero or unknown size, marking faulty!\n",
2797 bdevname(rdev->bdev,b));
2802 if (super_format >= 0) {
2803 err = super_types[super_format].
2804 load_super(rdev, NULL, super_minor);
2805 if (err == -EINVAL) {
2807 "md: %s does not have a valid v%d.%d "
2808 "superblock, not importing!\n",
2809 bdevname(rdev->bdev,b),
2810 super_format, super_minor);
2815 "md: could not read %s's sb, not importing!\n",
2816 bdevname(rdev->bdev,b));
2824 if (rdev->sb_page) {
2830 return ERR_PTR(err);
2834 * Check a full RAID array for plausibility
2838 static void analyze_sbs(mddev_t * mddev)
2841 mdk_rdev_t *rdev, *freshest, *tmp;
2842 char b[BDEVNAME_SIZE];
2845 rdev_for_each(rdev, tmp, mddev)
2846 switch (super_types[mddev->major_version].
2847 load_super(rdev, freshest, mddev->minor_version)) {
2855 "md: fatal superblock inconsistency in %s"
2856 " -- removing from array\n",
2857 bdevname(rdev->bdev,b));
2858 kick_rdev_from_array(rdev);
2862 super_types[mddev->major_version].
2863 validate_super(mddev, freshest);
2866 rdev_for_each(rdev, tmp, mddev) {
2867 if (mddev->max_disks &&
2868 (rdev->desc_nr >= mddev->max_disks ||
2869 i > mddev->max_disks)) {
2871 "md: %s: %s: only %d devices permitted\n",
2872 mdname(mddev), bdevname(rdev->bdev, b),
2874 kick_rdev_from_array(rdev);
2877 if (rdev != freshest)
2878 if (super_types[mddev->major_version].
2879 validate_super(mddev, rdev)) {
2880 printk(KERN_WARNING "md: kicking non-fresh %s"
2882 bdevname(rdev->bdev,b));
2883 kick_rdev_from_array(rdev);
2886 if (mddev->level == LEVEL_MULTIPATH) {
2887 rdev->desc_nr = i++;
2888 rdev->raid_disk = rdev->desc_nr;
2889 set_bit(In_sync, &rdev->flags);
2890 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
2891 rdev->raid_disk = -1;
2892 clear_bit(In_sync, &rdev->flags);
2897 /* Read a fixed-point number.
2898 * Numbers in sysfs attributes should be in "standard" units where
2899 * possible, so time should be in seconds.
2900 * However we internally use a a much smaller unit such as
2901 * milliseconds or jiffies.
2902 * This function takes a decimal number with a possible fractional
2903 * component, and produces an integer which is the result of
2904 * multiplying that number by 10^'scale'.
2905 * all without any floating-point arithmetic.
2907 int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
2909 unsigned long result = 0;
2911 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
2914 else if (decimals < scale) {
2917 result = result * 10 + value;
2929 while (decimals < scale) {
2938 static void md_safemode_timeout(unsigned long data);
2941 safe_delay_show(mddev_t *mddev, char *page)
2943 int msec = (mddev->safemode_delay*1000)/HZ;
2944 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2947 safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2951 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
2954 mddev->safemode_delay = 0;
2956 unsigned long old_delay = mddev->safemode_delay;
2957 mddev->safemode_delay = (msec*HZ)/1000;
2958 if (mddev->safemode_delay == 0)
2959 mddev->safemode_delay = 1;
2960 if (mddev->safemode_delay < old_delay)
2961 md_safemode_timeout((unsigned long)mddev);
2965 static struct md_sysfs_entry md_safe_delay =
2966 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
2969 level_show(mddev_t *mddev, char *page)
2971 struct mdk_personality *p = mddev->pers;
2973 return sprintf(page, "%s\n", p->name);
2974 else if (mddev->clevel[0])
2975 return sprintf(page, "%s\n", mddev->clevel);
2976 else if (mddev->level != LEVEL_NONE)
2977 return sprintf(page, "%d\n", mddev->level);
2983 level_store(mddev_t *mddev, const char *buf, size_t len)
2987 struct mdk_personality *pers;
2992 if (mddev->pers == NULL) {
2995 if (len >= sizeof(mddev->clevel))
2997 strncpy(mddev->clevel, buf, len);
2998 if (mddev->clevel[len-1] == '\n')
3000 mddev->clevel[len] = 0;
3001 mddev->level = LEVEL_NONE;
3005 /* request to change the personality. Need to ensure:
3006 * - array is not engaged in resync/recovery/reshape
3007 * - old personality can be suspended
3008 * - new personality will access other array.
3011 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
3014 if (!mddev->pers->quiesce) {
3015 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3016 mdname(mddev), mddev->pers->name);
3020 /* Now find the new personality */
3021 if (len == 0 || len >= sizeof(clevel))
3023 strncpy(clevel, buf, len);
3024 if (clevel[len-1] == '\n')
3027 if (strict_strtol(clevel, 10, &level))
3030 if (request_module("md-%s", clevel) != 0)
3031 request_module("md-level-%s", clevel);
3032 spin_lock(&pers_lock);
3033 pers = find_pers(level, clevel);
3034 if (!pers || !try_module_get(pers->owner)) {
3035 spin_unlock(&pers_lock);
3036 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
3039 spin_unlock(&pers_lock);
3041 if (pers == mddev->pers) {
3042 /* Nothing to do! */
3043 module_put(pers->owner);
3046 if (!pers->takeover) {
3047 module_put(pers->owner);
3048 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
3049 mdname(mddev), clevel);
3053 list_for_each_entry(rdev, &mddev->disks, same_set)
3054 rdev->new_raid_disk = rdev->raid_disk;
3056 /* ->takeover must set new_* and/or delta_disks
3057 * if it succeeds, and may set them when it fails.
3059 priv = pers->takeover(mddev);
3061 mddev->new_level = mddev->level;
3062 mddev->new_layout = mddev->layout;
3063 mddev->new_chunk_sectors = mddev->chunk_sectors;
3064 mddev->raid_disks -= mddev->delta_disks;
3065 mddev->delta_disks = 0;
3066 module_put(pers->owner);
3067 printk(KERN_WARNING "md: %s: %s would not accept array\n",
3068 mdname(mddev), clevel);
3069 return PTR_ERR(priv);
3072 /* Looks like we have a winner */
3073 mddev_suspend(mddev);
3074 mddev->pers->stop(mddev);
3076 if (mddev->pers->sync_request == NULL &&
3077 pers->sync_request != NULL) {
3078 /* need to add the md_redundancy_group */
3079 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3081 "md: cannot register extra attributes for %s\n",
3083 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
3085 if (mddev->pers->sync_request != NULL &&
3086 pers->sync_request == NULL) {
3087 /* need to remove the md_redundancy_group */
3088 if (mddev->to_remove == NULL)
3089 mddev->to_remove = &md_redundancy_group;
3092 if (mddev->pers->sync_request == NULL &&
3094 /* We are converting from a no-redundancy array
3095 * to a redundancy array and metadata is managed
3096 * externally so we need to be sure that writes
3097 * won't block due to a need to transition
3099 * until external management is started.
3102 mddev->safemode_delay = 0;
3103 mddev->safemode = 0;
3106 list_for_each_entry(rdev, &mddev->disks, same_set) {
3108 if (rdev->raid_disk < 0)
3110 if (rdev->new_raid_disk > mddev->raid_disks)
3111 rdev->new_raid_disk = -1;
3112 if (rdev->new_raid_disk == rdev->raid_disk)
3114 sprintf(nm, "rd%d", rdev->raid_disk);
3115 sysfs_remove_link(&mddev->kobj, nm);
3117 list_for_each_entry(rdev, &mddev->disks, same_set) {
3118 if (rdev->raid_disk < 0)
3120 if (rdev->new_raid_disk == rdev->raid_disk)
3122 rdev->raid_disk = rdev->new_raid_disk;
3123 if (rdev->raid_disk < 0)
3124 clear_bit(In_sync, &rdev->flags);
3127 sprintf(nm, "rd%d", rdev->raid_disk);
3128 if(sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3129 printk("md: cannot register %s for %s after level change\n",
3134 module_put(mddev->pers->owner);
3136 mddev->private = priv;
3137 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3138 mddev->level = mddev->new_level;
3139 mddev->layout = mddev->new_layout;
3140 mddev->chunk_sectors = mddev->new_chunk_sectors;
3141 mddev->delta_disks = 0;
3142 if (mddev->pers->sync_request == NULL) {
3143 /* this is now an array without redundancy, so
3144 * it must always be in_sync
3147 del_timer_sync(&mddev->safemode_timer);
3150 mddev_resume(mddev);
3151 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3152 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3153 md_wakeup_thread(mddev->thread);
3154 sysfs_notify(&mddev->kobj, NULL, "level");
3155 md_new_event(mddev);
3159 static struct md_sysfs_entry md_level =
3160 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
3164 layout_show(mddev_t *mddev, char *page)
3166 /* just a number, not meaningful for all levels */
3167 if (mddev->reshape_position != MaxSector &&
3168 mddev->layout != mddev->new_layout)
3169 return sprintf(page, "%d (%d)\n",
3170 mddev->new_layout, mddev->layout);
3171 return sprintf(page, "%d\n", mddev->layout);
3175 layout_store(mddev_t *mddev, const char *buf, size_t len)
3178 unsigned long n = simple_strtoul(buf, &e, 10);
3180 if (!*buf || (*e && *e != '\n'))
3185 if (mddev->pers->check_reshape == NULL)
3187 mddev->new_layout = n;
3188 err = mddev->pers->check_reshape(mddev);
3190 mddev->new_layout = mddev->layout;
3194 mddev->new_layout = n;
3195 if (mddev->reshape_position == MaxSector)
3200 static struct md_sysfs_entry md_layout =
3201 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
3205 raid_disks_show(mddev_t *mddev, char *page)
3207 if (mddev->raid_disks == 0)
3209 if (mddev->reshape_position != MaxSector &&
3210 mddev->delta_disks != 0)
3211 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3212 mddev->raid_disks - mddev->delta_disks);
3213 return sprintf(page, "%d\n", mddev->raid_disks);
3216 static int update_raid_disks(mddev_t *mddev, int raid_disks);
3219 raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3223 unsigned long n = simple_strtoul(buf, &e, 10);
3225 if (!*buf || (*e && *e != '\n'))
3229 rv = update_raid_disks(mddev, n);
3230 else if (mddev->reshape_position != MaxSector) {
3231 int olddisks = mddev->raid_disks - mddev->delta_disks;
3232 mddev->delta_disks = n - olddisks;
3233 mddev->raid_disks = n;
3235 mddev->raid_disks = n;
3236 return rv ? rv : len;
3238 static struct md_sysfs_entry md_raid_disks =
3239 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
3242 chunk_size_show(mddev_t *mddev, char *page)
3244 if (mddev->reshape_position != MaxSector &&
3245 mddev->chunk_sectors != mddev->new_chunk_sectors)
3246 return sprintf(page, "%d (%d)\n",
3247 mddev->new_chunk_sectors << 9,
3248 mddev->chunk_sectors << 9);
3249 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
3253 chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3256 unsigned long n = simple_strtoul(buf, &e, 10);
3258 if (!*buf || (*e && *e != '\n'))
3263 if (mddev->pers->check_reshape == NULL)
3265 mddev->new_chunk_sectors = n >> 9;
3266 err = mddev->pers->check_reshape(mddev);
3268 mddev->new_chunk_sectors = mddev->chunk_sectors;
3272 mddev->new_chunk_sectors = n >> 9;
3273 if (mddev->reshape_position == MaxSector)
3274 mddev->chunk_sectors = n >> 9;
3278 static struct md_sysfs_entry md_chunk_size =
3279 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
3282 resync_start_show(mddev_t *mddev, char *page)
3284 if (mddev->recovery_cp == MaxSector)
3285 return sprintf(page, "none\n");
3286 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3290 resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3293 unsigned long long n = simple_strtoull(buf, &e, 10);
3297 if (cmd_match(buf, "none"))
3299 else if (!*buf || (*e && *e != '\n'))
3302 mddev->recovery_cp = n;
3305 static struct md_sysfs_entry md_resync_start =
3306 __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
3309 * The array state can be:
3312 * No devices, no size, no level
3313 * Equivalent to STOP_ARRAY ioctl
3315 * May have some settings, but array is not active
3316 * all IO results in error
3317 * When written, doesn't tear down array, but just stops it
3318 * suspended (not supported yet)
3319 * All IO requests will block. The array can be reconfigured.
3320 * Writing this, if accepted, will block until array is quiescent
3322 * no resync can happen. no superblocks get written.
3323 * write requests fail
3325 * like readonly, but behaves like 'clean' on a write request.
3327 * clean - no pending writes, but otherwise active.
3328 * When written to inactive array, starts without resync
3329 * If a write request arrives then
3330 * if metadata is known, mark 'dirty' and switch to 'active'.
3331 * if not known, block and switch to write-pending
3332 * If written to an active array that has pending writes, then fails.
3334 * fully active: IO and resync can be happening.
3335 * When written to inactive array, starts with resync
3338 * clean, but writes are blocked waiting for 'active' to be written.
3341 * like active, but no writes have been seen for a while (100msec).
3344 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3345 write_pending, active_idle, bad_word};
3346 static char *array_states[] = {
3347 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3348 "write-pending", "active-idle", NULL };
3350 static int match_word(const char *word, char **list)
3353 for (n=0; list[n]; n++)
3354 if (cmd_match(word, list[n]))
3360 array_state_show(mddev_t *mddev, char *page)
3362 enum array_state st = inactive;
3375 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
3377 else if (mddev->safemode)
3383 if (list_empty(&mddev->disks) &&
3384 mddev->raid_disks == 0 &&
3385 mddev->dev_sectors == 0)
3390 return sprintf(page, "%s\n", array_states[st]);
3393 static int do_md_stop(mddev_t * mddev, int ro, int is_open);
3394 static int md_set_readonly(mddev_t * mddev, int is_open);
3395 static int do_md_run(mddev_t * mddev);
3396 static int restart_array(mddev_t *mddev);
3399 array_state_store(mddev_t *mddev, const char *buf, size_t len)
3402 enum array_state st = match_word(buf, array_states);
3407 /* stopping an active array */
3408 if (atomic_read(&mddev->openers) > 0)
3410 err = do_md_stop(mddev, 0, 0);
3413 /* stopping an active array */
3415 if (atomic_read(&mddev->openers) > 0)
3417 err = do_md_stop(mddev, 2, 0);
3419 err = 0; /* already inactive */
3422 break; /* not supported yet */
3425 err = md_set_readonly(mddev, 0);
3428 set_disk_ro(mddev->gendisk, 1);
3429 err = do_md_run(mddev);
3435 err = md_set_readonly(mddev, 0);
3436 else if (mddev->ro == 1)
3437 err = restart_array(mddev);
3440 set_disk_ro(mddev->gendisk, 0);
3444 err = do_md_run(mddev);
3449 restart_array(mddev);
3450 spin_lock_irq(&mddev->write_lock);
3451 if (atomic_read(&mddev->writes_pending) == 0) {
3452 if (mddev->in_sync == 0) {
3454 if (mddev->safemode == 1)
3455 mddev->safemode = 0;
3456 if (mddev->persistent)
3457 set_bit(MD_CHANGE_CLEAN,
3463 spin_unlock_irq(&mddev->write_lock);
3469 restart_array(mddev);
3470 if (mddev->external)
3471 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
3472 wake_up(&mddev->sb_wait);
3476 set_disk_ro(mddev->gendisk, 0);
3477 err = do_md_run(mddev);
3482 /* these cannot be set */
3488 sysfs_notify_dirent_safe(mddev->sysfs_state);
3492 static struct md_sysfs_entry md_array_state =
3493 __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
3496 max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3497 return sprintf(page, "%d\n",
3498 atomic_read(&mddev->max_corr_read_errors));
3502 max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3505 unsigned long n = simple_strtoul(buf, &e, 10);
3507 if (*buf && (*e == 0 || *e == '\n')) {
3508 atomic_set(&mddev->max_corr_read_errors, n);
3514 static struct md_sysfs_entry max_corr_read_errors =
3515 __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3516 max_corrected_read_errors_store);
3519 null_show(mddev_t *mddev, char *page)
3525 new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3527 /* buf must be %d:%d\n? giving major and minor numbers */
3528 /* The new device is added to the array.
3529 * If the array has a persistent superblock, we read the
3530 * superblock to initialise info and check validity.
3531 * Otherwise, only checking done is that in bind_rdev_to_array,
3532 * which mainly checks size.
3535 int major = simple_strtoul(buf, &e, 10);
3541 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3543 minor = simple_strtoul(e+1, &e, 10);
3544 if (*e && *e != '\n')
3546 dev = MKDEV(major, minor);
3547 if (major != MAJOR(dev) ||
3548 minor != MINOR(dev))
3552 if (mddev->persistent) {
3553 rdev = md_import_device(dev, mddev->major_version,
3554 mddev->minor_version);
3555 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3556 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3557 mdk_rdev_t, same_set);
3558 err = super_types[mddev->major_version]
3559 .load_super(rdev, rdev0, mddev->minor_version);
3563 } else if (mddev->external)
3564 rdev = md_import_device(dev, -2, -1);
3566 rdev = md_import_device(dev, -1, -1);
3569 return PTR_ERR(rdev);
3570 err = bind_rdev_to_array(rdev, mddev);
3574 return err ? err : len;
3577 static struct md_sysfs_entry md_new_device =
3578 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
3581 bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3584 unsigned long chunk, end_chunk;
3588 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3590 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3591 if (buf == end) break;
3592 if (*end == '-') { /* range */
3594 end_chunk = simple_strtoul(buf, &end, 0);
3595 if (buf == end) break;
3597 if (*end && !isspace(*end)) break;
3598 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
3599 buf = skip_spaces(end);
3601 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3606 static struct md_sysfs_entry md_bitmap =
3607 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3610 size_show(mddev_t *mddev, char *page)
3612 return sprintf(page, "%llu\n",
3613 (unsigned long long)mddev->dev_sectors / 2);
3616 static int update_size(mddev_t *mddev, sector_t num_sectors);
3619 size_store(mddev_t *mddev, const char *buf, size_t len)
3621 /* If array is inactive, we can reduce the component size, but
3622 * not increase it (except from 0).
3623 * If array is active, we can try an on-line resize
3626 int err = strict_blocks_to_sectors(buf, §ors);
3631 err = update_size(mddev, sectors);
3632 md_update_sb(mddev, 1);
3634 if (mddev->dev_sectors == 0 ||
3635 mddev->dev_sectors > sectors)
3636 mddev->dev_sectors = sectors;
3640 return err ? err : len;
3643 static struct md_sysfs_entry md_size =
3644 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
3649 * 'none' for arrays with no metadata (good luck...)
3650 * 'external' for arrays with externally managed metadata,
3651 * or N.M for internally known formats
3654 metadata_show(mddev_t *mddev, char *page)
3656 if (mddev->persistent)
3657 return sprintf(page, "%d.%d\n",
3658 mddev->major_version, mddev->minor_version);
3659 else if (mddev->external)
3660 return sprintf(page, "external:%s\n", mddev->metadata_type);
3662 return sprintf(page, "none\n");
3666 metadata_store(mddev_t *mddev, const char *buf, size_t len)
3670 /* Changing the details of 'external' metadata is
3671 * always permitted. Otherwise there must be
3672 * no devices attached to the array.
3674 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3676 else if (!list_empty(&mddev->disks))
3679 if (cmd_match(buf, "none")) {
3680 mddev->persistent = 0;
3681 mddev->external = 0;
3682 mddev->major_version = 0;
3683 mddev->minor_version = 90;
3686 if (strncmp(buf, "external:", 9) == 0) {
3687 size_t namelen = len-9;
3688 if (namelen >= sizeof(mddev->metadata_type))
3689 namelen = sizeof(mddev->metadata_type)-1;
3690 strncpy(mddev->metadata_type, buf+9, namelen);
3691 mddev->metadata_type[namelen] = 0;
3692 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3693 mddev->metadata_type[--namelen] = 0;
3694 mddev->persistent = 0;
3695 mddev->external = 1;
3696 mddev->major_version = 0;
3697 mddev->minor_version = 90;
3700 major = simple_strtoul(buf, &e, 10);
3701 if (e==buf || *e != '.')
3704 minor = simple_strtoul(buf, &e, 10);
3705 if (e==buf || (*e && *e != '\n') )
3707 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
3709 mddev->major_version = major;
3710 mddev->minor_version = minor;
3711 mddev->persistent = 1;
3712 mddev->external = 0;
3716 static struct md_sysfs_entry md_metadata =
3717 __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
3720 action_show(mddev_t *mddev, char *page)
3722 char *type = "idle";
3723 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3725 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3726 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
3727 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3729 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3730 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3732 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3736 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
3739 return sprintf(page, "%s\n", type);
3743 action_store(mddev_t *mddev, const char *page, size_t len)
3745 if (!mddev->pers || !mddev->pers->sync_request)
3748 if (cmd_match(page, "frozen"))
3749 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3751 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3753 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
3754 if (mddev->sync_thread) {
3755 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3756 md_unregister_thread(mddev->sync_thread);
3757 mddev->sync_thread = NULL;
3758 mddev->recovery = 0;
3760 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3761 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
3763 else if (cmd_match(page, "resync"))
3764 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3765 else if (cmd_match(page, "recover")) {
3766 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3767 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3768 } else if (cmd_match(page, "reshape")) {
3770 if (mddev->pers->start_reshape == NULL)
3772 err = mddev->pers->start_reshape(mddev);
3775 sysfs_notify(&mddev->kobj, NULL, "degraded");
3777 if (cmd_match(page, "check"))
3778 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3779 else if (!cmd_match(page, "repair"))
3781 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3782 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3784 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3785 md_wakeup_thread(mddev->thread);
3786 sysfs_notify_dirent_safe(mddev->sysfs_action);
3791 mismatch_cnt_show(mddev_t *mddev, char *page)
3793 return sprintf(page, "%llu\n",
3794 (unsigned long long) mddev->resync_mismatches);
3797 static struct md_sysfs_entry md_scan_mode =
3798 __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
3801 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
3804 sync_min_show(mddev_t *mddev, char *page)
3806 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3807 mddev->sync_speed_min ? "local": "system");
3811 sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3815 if (strncmp(buf, "system", 6)==0) {
3816 mddev->sync_speed_min = 0;
3819 min = simple_strtoul(buf, &e, 10);
3820 if (buf == e || (*e && *e != '\n') || min <= 0)
3822 mddev->sync_speed_min = min;
3826 static struct md_sysfs_entry md_sync_min =
3827 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3830 sync_max_show(mddev_t *mddev, char *page)
3832 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3833 mddev->sync_speed_max ? "local": "system");
3837 sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3841 if (strncmp(buf, "system", 6)==0) {
3842 mddev->sync_speed_max = 0;
3845 max = simple_strtoul(buf, &e, 10);
3846 if (buf == e || (*e && *e != '\n') || max <= 0)
3848 mddev->sync_speed_max = max;
3852 static struct md_sysfs_entry md_sync_max =
3853 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3856 degraded_show(mddev_t *mddev, char *page)
3858 return sprintf(page, "%d\n", mddev->degraded);
3860 static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
3863 sync_force_parallel_show(mddev_t *mddev, char *page)
3865 return sprintf(page, "%d\n", mddev->parallel_resync);
3869 sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3873 if (strict_strtol(buf, 10, &n))
3876 if (n != 0 && n != 1)
3879 mddev->parallel_resync = n;
3881 if (mddev->sync_thread)
3882 wake_up(&resync_wait);
3887 /* force parallel resync, even with shared block devices */
3888 static struct md_sysfs_entry md_sync_force_parallel =
3889 __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3890 sync_force_parallel_show, sync_force_parallel_store);
3893 sync_speed_show(mddev_t *mddev, char *page)
3895 unsigned long resync, dt, db;
3896 if (mddev->curr_resync == 0)
3897 return sprintf(page, "none\n");
3898 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3899 dt = (jiffies - mddev->resync_mark) / HZ;
3901 db = resync - mddev->resync_mark_cnt;
3902 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
3905 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
3908 sync_completed_show(mddev_t *mddev, char *page)
3910 unsigned long max_sectors, resync;
3912 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3913 return sprintf(page, "none\n");
3915 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3916 max_sectors = mddev->resync_max_sectors;
3918 max_sectors = mddev->dev_sectors;
3920 resync = mddev->curr_resync_completed;
3921 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
3924 static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
3927 min_sync_show(mddev_t *mddev, char *page)
3929 return sprintf(page, "%llu\n",
3930 (unsigned long long)mddev->resync_min);
3933 min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3935 unsigned long long min;
3936 if (strict_strtoull(buf, 10, &min))
3938 if (min > mddev->resync_max)
3940 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3943 /* Must be a multiple of chunk_size */
3944 if (mddev->chunk_sectors) {
3945 sector_t temp = min;
3946 if (sector_div(temp, mddev->chunk_sectors))
3949 mddev->resync_min = min;
3954 static struct md_sysfs_entry md_min_sync =
3955 __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3958 max_sync_show(mddev_t *mddev, char *page)
3960 if (mddev->resync_max == MaxSector)
3961 return sprintf(page, "max\n");
3963 return sprintf(page, "%llu\n",
3964 (unsigned long long)mddev->resync_max);
3967 max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3969 if (strncmp(buf, "max", 3) == 0)
3970 mddev->resync_max = MaxSector;
3972 unsigned long long max;
3973 if (strict_strtoull(buf, 10, &max))
3975 if (max < mddev->resync_min)
3977 if (max < mddev->resync_max &&
3979 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3982 /* Must be a multiple of chunk_size */
3983 if (mddev->chunk_sectors) {
3984 sector_t temp = max;
3985 if (sector_div(temp, mddev->chunk_sectors))
3988 mddev->resync_max = max;
3990 wake_up(&mddev->recovery_wait);
3994 static struct md_sysfs_entry md_max_sync =
3995 __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3998 suspend_lo_show(mddev_t *mddev, char *page)
4000 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4004 suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
4007 unsigned long long new = simple_strtoull(buf, &e, 10);
4009 if (mddev->pers == NULL ||
4010 mddev->pers->quiesce == NULL)
4012 if (buf == e || (*e && *e != '\n'))
4014 if (new >= mddev->suspend_hi ||
4015 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
4016 mddev->suspend_lo = new;
4017 mddev->pers->quiesce(mddev, 2);
4022 static struct md_sysfs_entry md_suspend_lo =
4023 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4027 suspend_hi_show(mddev_t *mddev, char *page)
4029 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4033 suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
4036 unsigned long long new = simple_strtoull(buf, &e, 10);
4038 if (mddev->pers == NULL ||
4039 mddev->pers->quiesce == NULL)
4041 if (buf == e || (*e && *e != '\n'))
4043 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
4044 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
4045 mddev->suspend_hi = new;
4046 mddev->pers->quiesce(mddev, 1);
4047 mddev->pers->quiesce(mddev, 0);
4052 static struct md_sysfs_entry md_suspend_hi =
4053 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4056 reshape_position_show(mddev_t *mddev, char *page)
4058 if (mddev->reshape_position != MaxSector)
4059 return sprintf(page, "%llu\n",
4060 (unsigned long long)mddev->reshape_position);
4061 strcpy(page, "none\n");
4066 reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
4069 unsigned long long new = simple_strtoull(buf, &e, 10);
4072 if (buf == e || (*e && *e != '\n'))
4074 mddev->reshape_position = new;
4075 mddev->delta_disks = 0;
4076 mddev->new_level = mddev->level;
4077 mddev->new_layout = mddev->layout;
4078 mddev->new_chunk_sectors = mddev->chunk_sectors;
4082 static struct md_sysfs_entry md_reshape_position =
4083 __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4084 reshape_position_store);
4087 array_size_show(mddev_t *mddev, char *page)
4089 if (mddev->external_size)
4090 return sprintf(page, "%llu\n",
4091 (unsigned long long)mddev->array_sectors/2);
4093 return sprintf(page, "default\n");
4097 array_size_store(mddev_t *mddev, const char *buf, size_t len)
4101 if (strncmp(buf, "default", 7) == 0) {
4103 sectors = mddev->pers->size(mddev, 0, 0);
4105 sectors = mddev->array_sectors;
4107 mddev->external_size = 0;
4109 if (strict_blocks_to_sectors(buf, §ors) < 0)
4111 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4114 mddev->external_size = 1;
4117 mddev->array_sectors = sectors;
4118 set_capacity(mddev->gendisk, mddev->array_sectors);
4120 revalidate_disk(mddev->gendisk);
4125 static struct md_sysfs_entry md_array_size =
4126 __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4129 static struct attribute *md_default_attrs[] = {
4132 &md_raid_disks.attr,
4133 &md_chunk_size.attr,
4135 &md_resync_start.attr,
4137 &md_new_device.attr,
4138 &md_safe_delay.attr,
4139 &md_array_state.attr,
4140 &md_reshape_position.attr,
4141 &md_array_size.attr,
4142 &max_corr_read_errors.attr,
4146 static struct attribute *md_redundancy_attrs[] = {
4148 &md_mismatches.attr,
4151 &md_sync_speed.attr,
4152 &md_sync_force_parallel.attr,
4153 &md_sync_completed.attr,
4156 &md_suspend_lo.attr,
4157 &md_suspend_hi.attr,
4162 static struct attribute_group md_redundancy_group = {
4164 .attrs = md_redundancy_attrs,
4169 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4171 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4172 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
4177 rv = mddev_lock(mddev);
4179 rv = entry->show(mddev, page);
4180 mddev_unlock(mddev);
4186 md_attr_store(struct kobject *kobj, struct attribute *attr,
4187 const char *page, size_t length)
4189 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4190 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
4195 if (!capable(CAP_SYS_ADMIN))
4197 rv = mddev_lock(mddev);
4198 if (mddev->hold_active == UNTIL_IOCTL)
4199 mddev->hold_active = 0;
4201 rv = entry->store(mddev, page, length);
4202 mddev_unlock(mddev);
4207 static void md_free(struct kobject *ko)
4209 mddev_t *mddev = container_of(ko, mddev_t, kobj);
4211 if (mddev->sysfs_state)
4212 sysfs_put(mddev->sysfs_state);
4214 if (mddev->gendisk) {
4215 del_gendisk(mddev->gendisk);
4216 put_disk(mddev->gendisk);
4219 blk_cleanup_queue(mddev->queue);
4224 static const struct sysfs_ops md_sysfs_ops = {
4225 .show = md_attr_show,
4226 .store = md_attr_store,
4228 static struct kobj_type md_ktype = {
4230 .sysfs_ops = &md_sysfs_ops,
4231 .default_attrs = md_default_attrs,
4236 static void mddev_delayed_delete(struct work_struct *ws)
4238 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4240 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
4241 kobject_del(&mddev->kobj);
4242 kobject_put(&mddev->kobj);
4245 static int md_alloc(dev_t dev, char *name)
4247 static DEFINE_MUTEX(disks_mutex);
4248 mddev_t *mddev = mddev_find(dev);
4249 struct gendisk *disk;
4258 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4259 shift = partitioned ? MdpMinorShift : 0;
4260 unit = MINOR(mddev->unit) >> shift;
4262 /* wait for any previous instance if this device
4263 * to be completed removed (mddev_delayed_delete).
4265 flush_scheduled_work();
4267 mutex_lock(&disks_mutex);
4273 /* Need to ensure that 'name' is not a duplicate.
4276 spin_lock(&all_mddevs_lock);
4278 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4279 if (mddev2->gendisk &&
4280 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4281 spin_unlock(&all_mddevs_lock);
4284 spin_unlock(&all_mddevs_lock);
4288 mddev->queue = blk_alloc_queue(GFP_KERNEL);
4291 mddev->queue->queuedata = mddev;
4293 /* Can be unlocked because the queue is new: no concurrency */
4294 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
4296 blk_queue_make_request(mddev->queue, md_make_request);
4298 disk = alloc_disk(1 << shift);
4300 blk_cleanup_queue(mddev->queue);
4301 mddev->queue = NULL;
4304 disk->major = MAJOR(mddev->unit);
4305 disk->first_minor = unit << shift;
4307 strcpy(disk->disk_name, name);
4308 else if (partitioned)
4309 sprintf(disk->disk_name, "md_d%d", unit);
4311 sprintf(disk->disk_name, "md%d", unit);
4312 disk->fops = &md_fops;
4313 disk->private_data = mddev;
4314 disk->queue = mddev->queue;
4315 /* Allow extended partitions. This makes the
4316 * 'mdp' device redundant, but we can't really
4319 disk->flags |= GENHD_FL_EXT_DEVT;
4321 mddev->gendisk = disk;
4322 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4323 &disk_to_dev(disk)->kobj, "%s", "md");
4325 /* This isn't possible, but as kobject_init_and_add is marked
4326 * __must_check, we must do something with the result
4328 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4332 if (mddev->kobj.sd &&
4333 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
4334 printk(KERN_DEBUG "pointless warning\n");
4336 mutex_unlock(&disks_mutex);
4337 if (!error && mddev->kobj.sd) {
4338 kobject_uevent(&mddev->kobj, KOBJ_ADD);
4339 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
4345 static struct kobject *md_probe(dev_t dev, int *part, void *data)
4347 md_alloc(dev, NULL);
4351 static int add_named_array(const char *val, struct kernel_param *kp)
4353 /* val must be "md_*" where * is not all digits.
4354 * We allocate an array with a large free minor number, and
4355 * set the name to val. val must not already be an active name.
4357 int len = strlen(val);
4358 char buf[DISK_NAME_LEN];
4360 while (len && val[len-1] == '\n')
4362 if (len >= DISK_NAME_LEN)
4364 strlcpy(buf, val, len+1);
4365 if (strncmp(buf, "md_", 3) != 0)
4367 return md_alloc(0, buf);
4370 static void md_safemode_timeout(unsigned long data)
4372 mddev_t *mddev = (mddev_t *) data;
4374 if (!atomic_read(&mddev->writes_pending)) {
4375 mddev->safemode = 1;
4376 if (mddev->external)
4377 sysfs_notify_dirent_safe(mddev->sysfs_state);
4379 md_wakeup_thread(mddev->thread);
4382 static int start_dirty_degraded;
4384 int md_run(mddev_t *mddev)
4388 struct mdk_personality *pers;
4390 if (list_empty(&mddev->disks))
4391 /* cannot run an array with no devices.. */
4397 /* These two calls synchronise us with the
4398 * sysfs_remove_group calls in mddev_unlock,
4399 * so they must have completed.
4401 mutex_lock(&mddev->open_mutex);
4402 mutex_unlock(&mddev->open_mutex);
4405 * Analyze all RAID superblock(s)
4407 if (!mddev->raid_disks) {
4408 if (!mddev->persistent)
4413 if (mddev->level != LEVEL_NONE)
4414 request_module("md-level-%d", mddev->level);
4415 else if (mddev->clevel[0])
4416 request_module("md-%s", mddev->clevel);
4419 * Drop all container device buffers, from now on
4420 * the only valid external interface is through the md
4423 list_for_each_entry(rdev, &mddev->disks, same_set) {
4424 if (test_bit(Faulty, &rdev->flags))
4426 sync_blockdev(rdev->bdev);
4427 invalidate_bdev(rdev->bdev);
4429 /* perform some consistency tests on the device.
4430 * We don't want the data to overlap the metadata,
4431 * Internal Bitmap issues have been handled elsewhere.
4433 if (rdev->data_offset < rdev->sb_start) {
4434 if (mddev->dev_sectors &&
4435 rdev->data_offset + mddev->dev_sectors
4437 printk("md: %s: data overlaps metadata\n",
4442 if (rdev->sb_start + rdev->sb_size/512
4443 > rdev->data_offset) {
4444 printk("md: %s: metadata overlaps data\n",
4449 sysfs_notify_dirent_safe(rdev->sysfs_state);
4452 spin_lock(&pers_lock);
4453 pers = find_pers(mddev->level, mddev->clevel);
4454 if (!pers || !try_module_get(pers->owner)) {
4455 spin_unlock(&pers_lock);
4456 if (mddev->level != LEVEL_NONE)
4457 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4460 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4465 spin_unlock(&pers_lock);
4466 if (mddev->level != pers->level) {
4467 mddev->level = pers->level;
4468 mddev->new_level = pers->level;
4470 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
4472 if (mddev->reshape_position != MaxSector &&
4473 pers->start_reshape == NULL) {
4474 /* This personality cannot handle reshaping... */
4476 module_put(pers->owner);
4480 if (pers->sync_request) {
4481 /* Warn if this is a potentially silly
4484 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4488 list_for_each_entry(rdev, &mddev->disks, same_set)
4489 list_for_each_entry(rdev2, &mddev->disks, same_set) {
4491 rdev->bdev->bd_contains ==
4492 rdev2->bdev->bd_contains) {
4494 "%s: WARNING: %s appears to be"
4495 " on the same physical disk as"
4498 bdevname(rdev->bdev,b),
4499 bdevname(rdev2->bdev,b2));
4506 "True protection against single-disk"
4507 " failure might be compromised.\n");
4510 mddev->recovery = 0;
4511 /* may be over-ridden by personality */
4512 mddev->resync_max_sectors = mddev->dev_sectors;
4514 mddev->barriers_work = 1;
4515 mddev->ok_start_degraded = start_dirty_degraded;
4517 if (start_readonly && mddev->ro == 0)
4518 mddev->ro = 2; /* read-only, but switch on first write */
4520 err = mddev->pers->run(mddev);
4522 printk(KERN_ERR "md: pers->run() failed ...\n");
4523 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4524 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4525 " but 'external_size' not in effect?\n", __func__);
4527 "md: invalid array_size %llu > default size %llu\n",
4528 (unsigned long long)mddev->array_sectors / 2,
4529 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4531 mddev->pers->stop(mddev);
4533 if (err == 0 && mddev->pers->sync_request) {
4534 err = bitmap_create(mddev);
4536 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4537 mdname(mddev), err);
4538 mddev->pers->stop(mddev);
4542 module_put(mddev->pers->owner);
4544 bitmap_destroy(mddev);
4547 if (mddev->pers->sync_request) {
4548 if (mddev->kobj.sd &&
4549 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4551 "md: cannot register extra attributes for %s\n",
4553 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
4554 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
4557 atomic_set(&mddev->writes_pending,0);
4558 atomic_set(&mddev->max_corr_read_errors,
4559 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
4560 mddev->safemode = 0;
4561 mddev->safemode_timer.function = md_safemode_timeout;
4562 mddev->safemode_timer.data = (unsigned long) mddev;
4563 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
4566 list_for_each_entry(rdev, &mddev->disks, same_set)
4567 if (rdev->raid_disk >= 0) {
4569 sprintf(nm, "rd%d", rdev->raid_disk);
4570 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4571 /* failure here is OK */;
4574 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4577 md_update_sb(mddev, 0);
4579 md_wakeup_thread(mddev->thread);
4580 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
4582 md_new_event(mddev);
4583 sysfs_notify_dirent_safe(mddev->sysfs_state);
4584 sysfs_notify_dirent_safe(mddev->sysfs_action);
4585 sysfs_notify(&mddev->kobj, NULL, "degraded");
4588 EXPORT_SYMBOL_GPL(md_run);
4590 static int do_md_run(mddev_t *mddev)
4594 err = md_run(mddev);
4597 err = bitmap_load(mddev);
4599 bitmap_destroy(mddev);
4602 set_capacity(mddev->gendisk, mddev->array_sectors);
4603 revalidate_disk(mddev->gendisk);
4604 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4609 static int restart_array(mddev_t *mddev)
4611 struct gendisk *disk = mddev->gendisk;
4613 /* Complain if it has no devices */
4614 if (list_empty(&mddev->disks))
4620 mddev->safemode = 0;
4622 set_disk_ro(disk, 0);
4623 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4625 /* Kick recovery or resync if necessary */
4626 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4627 md_wakeup_thread(mddev->thread);
4628 md_wakeup_thread(mddev->sync_thread);
4629 sysfs_notify_dirent_safe(mddev->sysfs_state);
4633 /* similar to deny_write_access, but accounts for our holding a reference
4634 * to the file ourselves */
4635 static int deny_bitmap_write_access(struct file * file)
4637 struct inode *inode = file->f_mapping->host;
4639 spin_lock(&inode->i_lock);
4640 if (atomic_read(&inode->i_writecount) > 1) {
4641 spin_unlock(&inode->i_lock);
4644 atomic_set(&inode->i_writecount, -1);
4645 spin_unlock(&inode->i_lock);
4650 void restore_bitmap_write_access(struct file *file)
4652 struct inode *inode = file->f_mapping->host;
4654 spin_lock(&inode->i_lock);
4655 atomic_set(&inode->i_writecount, 1);
4656 spin_unlock(&inode->i_lock);
4659 static void md_clean(mddev_t *mddev)
4661 mddev->array_sectors = 0;
4662 mddev->external_size = 0;
4663 mddev->dev_sectors = 0;
4664 mddev->raid_disks = 0;
4665 mddev->recovery_cp = 0;
4666 mddev->resync_min = 0;
4667 mddev->resync_max = MaxSector;
4668 mddev->reshape_position = MaxSector;
4669 mddev->external = 0;
4670 mddev->persistent = 0;
4671 mddev->level = LEVEL_NONE;
4672 mddev->clevel[0] = 0;
4675 mddev->metadata_type[0] = 0;
4676 mddev->chunk_sectors = 0;
4677 mddev->ctime = mddev->utime = 0;
4679 mddev->max_disks = 0;
4681 mddev->can_decrease_events = 0;
4682 mddev->delta_disks = 0;
4683 mddev->new_level = LEVEL_NONE;
4684 mddev->new_layout = 0;
4685 mddev->new_chunk_sectors = 0;
4686 mddev->curr_resync = 0;
4687 mddev->resync_mismatches = 0;
4688 mddev->suspend_lo = mddev->suspend_hi = 0;
4689 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4690 mddev->recovery = 0;
4692 mddev->degraded = 0;
4693 mddev->barriers_work = 0;
4694 mddev->safemode = 0;
4695 mddev->bitmap_info.offset = 0;
4696 mddev->bitmap_info.default_offset = 0;
4697 mddev->bitmap_info.chunksize = 0;
4698 mddev->bitmap_info.daemon_sleep = 0;
4699 mddev->bitmap_info.max_write_behind = 0;
4703 void md_stop_writes(mddev_t *mddev)
4705 if (mddev->sync_thread) {
4706 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4707 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4708 md_unregister_thread(mddev->sync_thread);
4709 mddev->sync_thread = NULL;
4712 del_timer_sync(&mddev->safemode_timer);
4714 bitmap_flush(mddev);
4715 md_super_wait(mddev);
4717 if (!mddev->in_sync || mddev->flags) {
4718 /* mark array as shutdown cleanly */
4720 md_update_sb(mddev, 1);
4723 EXPORT_SYMBOL_GPL(md_stop_writes);
4725 void md_stop(mddev_t *mddev)
4727 mddev->pers->stop(mddev);
4728 if (mddev->pers->sync_request && mddev->to_remove == NULL)
4729 mddev->to_remove = &md_redundancy_group;
4730 module_put(mddev->pers->owner);
4732 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4734 EXPORT_SYMBOL_GPL(md_stop);
4736 static int md_set_readonly(mddev_t *mddev, int is_open)
4739 mutex_lock(&mddev->open_mutex);
4740 if (atomic_read(&mddev->openers) > is_open) {
4741 printk("md: %s still in use.\n",mdname(mddev));
4746 md_stop_writes(mddev);
4752 set_disk_ro(mddev->gendisk, 1);
4753 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4754 sysfs_notify_dirent_safe(mddev->sysfs_state);
4758 mutex_unlock(&mddev->open_mutex);
4763 * 0 - completely stop and dis-assemble array
4764 * 2 - stop but do not disassemble array
4766 static int do_md_stop(mddev_t * mddev, int mode, int is_open)
4768 int err = 0, revalidate = 0;
4769 struct gendisk *disk = mddev->gendisk;
4772 mutex_lock(&mddev->open_mutex);
4773 if (atomic_read(&mddev->openers) > is_open) {
4774 printk("md: %s still in use.\n",mdname(mddev));
4776 } else if (mddev->pers) {
4779 set_disk_ro(disk, 0);
4781 md_stop_writes(mddev);
4783 mddev->queue->merge_bvec_fn = NULL;
4784 mddev->queue->unplug_fn = NULL;
4785 mddev->queue->backing_dev_info.congested_fn = NULL;
4787 /* tell userspace to handle 'inactive' */
4788 sysfs_notify_dirent_safe(mddev->sysfs_state);
4790 list_for_each_entry(rdev, &mddev->disks, same_set)
4791 if (rdev->raid_disk >= 0) {
4793 sprintf(nm, "rd%d", rdev->raid_disk);
4794 sysfs_remove_link(&mddev->kobj, nm);
4797 set_capacity(disk, 0);
4805 mutex_unlock(&mddev->open_mutex);
4807 revalidate_disk(disk);
4811 * Free resources if final stop
4815 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4817 bitmap_destroy(mddev);
4818 if (mddev->bitmap_info.file) {
4819 restore_bitmap_write_access(mddev->bitmap_info.file);
4820 fput(mddev->bitmap_info.file);
4821 mddev->bitmap_info.file = NULL;
4823 mddev->bitmap_info.offset = 0;
4825 export_array(mddev);
4828 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4829 if (mddev->hold_active == UNTIL_STOP)
4830 mddev->hold_active = 0;
4834 blk_integrity_unregister(disk);
4835 md_new_event(mddev);
4836 sysfs_notify_dirent_safe(mddev->sysfs_state);
4841 static void autorun_array(mddev_t *mddev)
4846 if (list_empty(&mddev->disks))
4849 printk(KERN_INFO "md: running: ");
4851 list_for_each_entry(rdev, &mddev->disks, same_set) {
4852 char b[BDEVNAME_SIZE];
4853 printk("<%s>", bdevname(rdev->bdev,b));
4857 err = do_md_run(mddev);
4859 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
4860 do_md_stop(mddev, 0, 0);
4865 * lets try to run arrays based on all disks that have arrived
4866 * until now. (those are in pending_raid_disks)
4868 * the method: pick the first pending disk, collect all disks with
4869 * the same UUID, remove all from the pending list and put them into
4870 * the 'same_array' list. Then order this list based on superblock
4871 * update time (freshest comes first), kick out 'old' disks and
4872 * compare superblocks. If everything's fine then run it.
4874 * If "unit" is allocated, then bump its reference count
4876 static void autorun_devices(int part)
4878 mdk_rdev_t *rdev0, *rdev, *tmp;
4880 char b[BDEVNAME_SIZE];
4882 printk(KERN_INFO "md: autorun ...\n");
4883 while (!list_empty(&pending_raid_disks)) {
4886 LIST_HEAD(candidates);
4887 rdev0 = list_entry(pending_raid_disks.next,
4888 mdk_rdev_t, same_set);
4890 printk(KERN_INFO "md: considering %s ...\n",
4891 bdevname(rdev0->bdev,b));
4892 INIT_LIST_HEAD(&candidates);
4893 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
4894 if (super_90_load(rdev, rdev0, 0) >= 0) {
4895 printk(KERN_INFO "md: adding %s ...\n",
4896 bdevname(rdev->bdev,b));
4897 list_move(&rdev->same_set, &candidates);
4900 * now we have a set of devices, with all of them having
4901 * mostly sane superblocks. It's time to allocate the
4905 dev = MKDEV(mdp_major,
4906 rdev0->preferred_minor << MdpMinorShift);
4907 unit = MINOR(dev) >> MdpMinorShift;
4909 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4912 if (rdev0->preferred_minor != unit) {
4913 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4914 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4918 md_probe(dev, NULL, NULL);
4919 mddev = mddev_find(dev);
4920 if (!mddev || !mddev->gendisk) {
4924 "md: cannot allocate memory for md drive.\n");
4927 if (mddev_lock(mddev))
4928 printk(KERN_WARNING "md: %s locked, cannot run\n",
4930 else if (mddev->raid_disks || mddev->major_version
4931 || !list_empty(&mddev->disks)) {
4933 "md: %s already running, cannot run %s\n",
4934 mdname(mddev), bdevname(rdev0->bdev,b));
4935 mddev_unlock(mddev);
4937 printk(KERN_INFO "md: created %s\n", mdname(mddev));
4938 mddev->persistent = 1;
4939 rdev_for_each_list(rdev, tmp, &candidates) {
4940 list_del_init(&rdev->same_set);
4941 if (bind_rdev_to_array(rdev, mddev))
4944 autorun_array(mddev);
4945 mddev_unlock(mddev);
4947 /* on success, candidates will be empty, on error
4950 rdev_for_each_list(rdev, tmp, &candidates) {
4951 list_del_init(&rdev->same_set);
4956 printk(KERN_INFO "md: ... autorun DONE.\n");
4958 #endif /* !MODULE */
4960 static int get_version(void __user * arg)
4964 ver.major = MD_MAJOR_VERSION;
4965 ver.minor = MD_MINOR_VERSION;
4966 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4968 if (copy_to_user(arg, &ver, sizeof(ver)))
4974 static int get_array_info(mddev_t * mddev, void __user * arg)
4976 mdu_array_info_t info;
4977 int nr,working,insync,failed,spare;
4980 nr=working=insync=failed=spare=0;
4981 list_for_each_entry(rdev, &mddev->disks, same_set) {
4983 if (test_bit(Faulty, &rdev->flags))
4987 if (test_bit(In_sync, &rdev->flags))
4994 info.major_version = mddev->major_version;
4995 info.minor_version = mddev->minor_version;
4996 info.patch_version = MD_PATCHLEVEL_VERSION;
4997 info.ctime = mddev->ctime;
4998 info.level = mddev->level;
4999 info.size = mddev->dev_sectors / 2;
5000 if (info.size != mddev->dev_sectors / 2) /* overflow */
5003 info.raid_disks = mddev->raid_disks;
5004 info.md_minor = mddev->md_minor;
5005 info.not_persistent= !mddev->persistent;
5007 info.utime = mddev->utime;
5010 info.state = (1<<MD_SB_CLEAN);
5011 if (mddev->bitmap && mddev->bitmap_info.offset)
5012 info.state = (1<<MD_SB_BITMAP_PRESENT);
5013 info.active_disks = insync;
5014 info.working_disks = working;
5015 info.failed_disks = failed;
5016 info.spare_disks = spare;
5018 info.layout = mddev->layout;
5019 info.chunk_size = mddev->chunk_sectors << 9;
5021 if (copy_to_user(arg, &info, sizeof(info)))
5027 static int get_bitmap_file(mddev_t * mddev, void __user * arg)
5029 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5030 char *ptr, *buf = NULL;
5033 if (md_allow_write(mddev))
5034 file = kmalloc(sizeof(*file), GFP_NOIO);
5036 file = kmalloc(sizeof(*file), GFP_KERNEL);
5041 /* bitmap disabled, zero the first byte and copy out */
5042 if (!mddev->bitmap || !mddev->bitmap->file) {
5043 file->pathname[0] = '\0';
5047 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5051 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
5055 strcpy(file->pathname, ptr);
5059 if (copy_to_user(arg, file, sizeof(*file)))
5067 static int get_disk_info(mddev_t * mddev, void __user * arg)
5069 mdu_disk_info_t info;
5072 if (copy_from_user(&info, arg, sizeof(info)))
5075 rdev = find_rdev_nr(mddev, info.number);
5077 info.major = MAJOR(rdev->bdev->bd_dev);
5078 info.minor = MINOR(rdev->bdev->bd_dev);
5079 info.raid_disk = rdev->raid_disk;
5081 if (test_bit(Faulty, &rdev->flags))
5082 info.state |= (1<<MD_DISK_FAULTY);
5083 else if (test_bit(In_sync, &rdev->flags)) {
5084 info.state |= (1<<MD_DISK_ACTIVE);
5085 info.state |= (1<<MD_DISK_SYNC);
5087 if (test_bit(WriteMostly, &rdev->flags))
5088 info.state |= (1<<MD_DISK_WRITEMOSTLY);
5090 info.major = info.minor = 0;
5091 info.raid_disk = -1;
5092 info.state = (1<<MD_DISK_REMOVED);
5095 if (copy_to_user(arg, &info, sizeof(info)))
5101 static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
5103 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5105 dev_t dev = MKDEV(info->major,info->minor);
5107 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5110 if (!mddev->raid_disks) {
5112 /* expecting a device which has a superblock */
5113 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5116 "md: md_import_device returned %ld\n",
5118 return PTR_ERR(rdev);
5120 if (!list_empty(&mddev->disks)) {
5121 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
5122 mdk_rdev_t, same_set);
5123 err = super_types[mddev->major_version]
5124 .load_super(rdev, rdev0, mddev->minor_version);
5127 "md: %s has different UUID to %s\n",
5128 bdevname(rdev->bdev,b),
5129 bdevname(rdev0->bdev,b2));
5134 err = bind_rdev_to_array(rdev, mddev);
5141 * add_new_disk can be used once the array is assembled
5142 * to add "hot spares". They must already have a superblock
5147 if (!mddev->pers->hot_add_disk) {
5149 "%s: personality does not support diskops!\n",
5153 if (mddev->persistent)
5154 rdev = md_import_device(dev, mddev->major_version,
5155 mddev->minor_version);
5157 rdev = md_import_device(dev, -1, -1);
5160 "md: md_import_device returned %ld\n",
5162 return PTR_ERR(rdev);
5164 /* set save_raid_disk if appropriate */
5165 if (!mddev->persistent) {
5166 if (info->state & (1<<MD_DISK_SYNC) &&
5167 info->raid_disk < mddev->raid_disks)
5168 rdev->raid_disk = info->raid_disk;
5170 rdev->raid_disk = -1;
5172 super_types[mddev->major_version].
5173 validate_super(mddev, rdev);
5174 rdev->saved_raid_disk = rdev->raid_disk;
5176 clear_bit(In_sync, &rdev->flags); /* just to be sure */
5177 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5178 set_bit(WriteMostly, &rdev->flags);
5180 clear_bit(WriteMostly, &rdev->flags);
5182 rdev->raid_disk = -1;
5183 err = bind_rdev_to_array(rdev, mddev);
5184 if (!err && !mddev->pers->hot_remove_disk) {
5185 /* If there is hot_add_disk but no hot_remove_disk
5186 * then added disks for geometry changes,
5187 * and should be added immediately.
5189 super_types[mddev->major_version].
5190 validate_super(mddev, rdev);
5191 err = mddev->pers->hot_add_disk(mddev, rdev);
5193 unbind_rdev_from_array(rdev);
5198 sysfs_notify_dirent_safe(rdev->sysfs_state);
5200 md_update_sb(mddev, 1);
5201 if (mddev->degraded)
5202 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
5203 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5204 md_wakeup_thread(mddev->thread);
5208 /* otherwise, add_new_disk is only allowed
5209 * for major_version==0 superblocks
5211 if (mddev->major_version != 0) {
5212 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5217 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5219 rdev = md_import_device(dev, -1, 0);
5222 "md: error, md_import_device() returned %ld\n",
5224 return PTR_ERR(rdev);
5226 rdev->desc_nr = info->number;
5227 if (info->raid_disk < mddev->raid_disks)
5228 rdev->raid_disk = info->raid_disk;
5230 rdev->raid_disk = -1;
5232 if (rdev->raid_disk < mddev->raid_disks)
5233 if (info->state & (1<<MD_DISK_SYNC))
5234 set_bit(In_sync, &rdev->flags);
5236 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5237 set_bit(WriteMostly, &rdev->flags);
5239 if (!mddev->persistent) {
5240 printk(KERN_INFO "md: nonpersistent superblock ...\n");
5241 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
5243 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
5244 rdev->sectors = rdev->sb_start;
5246 err = bind_rdev_to_array(rdev, mddev);
5256 static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5258 char b[BDEVNAME_SIZE];
5261 rdev = find_rdev(mddev, dev);
5265 if (rdev->raid_disk >= 0)
5268 kick_rdev_from_array(rdev);
5269 md_update_sb(mddev, 1);
5270 md_new_event(mddev);
5274 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
5275 bdevname(rdev->bdev,b), mdname(mddev));
5279 static int hot_add_disk(mddev_t * mddev, dev_t dev)
5281 char b[BDEVNAME_SIZE];
5288 if (mddev->major_version != 0) {
5289 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5290 " version-0 superblocks.\n",
5294 if (!mddev->pers->hot_add_disk) {
5296 "%s: personality does not support diskops!\n",
5301 rdev = md_import_device(dev, -1, 0);
5304 "md: error, md_import_device() returned %ld\n",
5309 if (mddev->persistent)
5310 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
5312 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
5314 rdev->sectors = rdev->sb_start;
5316 if (test_bit(Faulty, &rdev->flags)) {
5318 "md: can not hot-add faulty %s disk to %s!\n",
5319 bdevname(rdev->bdev,b), mdname(mddev));
5323 clear_bit(In_sync, &rdev->flags);
5325 rdev->saved_raid_disk = -1;
5326 err = bind_rdev_to_array(rdev, mddev);
5331 * The rest should better be atomic, we can have disk failures
5332 * noticed in interrupt contexts ...
5335 rdev->raid_disk = -1;
5337 md_update_sb(mddev, 1);
5340 * Kick recovery, maybe this spare has to be added to the
5341 * array immediately.
5343 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5344 md_wakeup_thread(mddev->thread);
5345 md_new_event(mddev);
5353 static int set_bitmap_file(mddev_t *mddev, int fd)
5358 if (!mddev->pers->quiesce)
5360 if (mddev->recovery || mddev->sync_thread)
5362 /* we should be able to change the bitmap.. */
5368 return -EEXIST; /* cannot add when bitmap is present */
5369 mddev->bitmap_info.file = fget(fd);
5371 if (mddev->bitmap_info.file == NULL) {
5372 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5377 err = deny_bitmap_write_access(mddev->bitmap_info.file);
5379 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5381 fput(mddev->bitmap_info.file);
5382 mddev->bitmap_info.file = NULL;
5385 mddev->bitmap_info.offset = 0; /* file overrides offset */
5386 } else if (mddev->bitmap == NULL)
5387 return -ENOENT; /* cannot remove what isn't there */
5390 mddev->pers->quiesce(mddev, 1);
5392 err = bitmap_create(mddev);
5394 err = bitmap_load(mddev);
5396 if (fd < 0 || err) {
5397 bitmap_destroy(mddev);
5398 fd = -1; /* make sure to put the file */
5400 mddev->pers->quiesce(mddev, 0);
5403 if (mddev->bitmap_info.file) {
5404 restore_bitmap_write_access(mddev->bitmap_info.file);
5405 fput(mddev->bitmap_info.file);
5407 mddev->bitmap_info.file = NULL;
5414 * set_array_info is used two different ways
5415 * The original usage is when creating a new array.
5416 * In this usage, raid_disks is > 0 and it together with
5417 * level, size, not_persistent,layout,chunksize determine the
5418 * shape of the array.
5419 * This will always create an array with a type-0.90.0 superblock.
5420 * The newer usage is when assembling an array.
5421 * In this case raid_disks will be 0, and the major_version field is
5422 * use to determine which style super-blocks are to be found on the devices.
5423 * The minor and patch _version numbers are also kept incase the
5424 * super_block handler wishes to interpret them.
5426 static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5429 if (info->raid_disks == 0) {
5430 /* just setting version number for superblock loading */
5431 if (info->major_version < 0 ||
5432 info->major_version >= ARRAY_SIZE(super_types) ||
5433 super_types[info->major_version].name == NULL) {
5434 /* maybe try to auto-load a module? */
5436 "md: superblock version %d not known\n",
5437 info->major_version);
5440 mddev->major_version = info->major_version;
5441 mddev->minor_version = info->minor_version;
5442 mddev->patch_version = info->patch_version;
5443 mddev->persistent = !info->not_persistent;
5444 /* ensure mddev_put doesn't delete this now that there
5445 * is some minimal configuration.
5447 mddev->ctime = get_seconds();
5450 mddev->major_version = MD_MAJOR_VERSION;
5451 mddev->minor_version = MD_MINOR_VERSION;
5452 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5453 mddev->ctime = get_seconds();
5455 mddev->level = info->level;
5456 mddev->clevel[0] = 0;
5457 mddev->dev_sectors = 2 * (sector_t)info->size;
5458 mddev->raid_disks = info->raid_disks;
5459 /* don't set md_minor, it is determined by which /dev/md* was
5462 if (info->state & (1<<MD_SB_CLEAN))
5463 mddev->recovery_cp = MaxSector;
5465 mddev->recovery_cp = 0;
5466 mddev->persistent = ! info->not_persistent;
5467 mddev->external = 0;
5469 mddev->layout = info->layout;
5470 mddev->chunk_sectors = info->chunk_size >> 9;
5472 mddev->max_disks = MD_SB_DISKS;
5474 if (mddev->persistent)
5476 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5478 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5479 mddev->bitmap_info.offset = 0;
5481 mddev->reshape_position = MaxSector;
5484 * Generate a 128 bit UUID
5486 get_random_bytes(mddev->uuid, 16);
5488 mddev->new_level = mddev->level;
5489 mddev->new_chunk_sectors = mddev->chunk_sectors;
5490 mddev->new_layout = mddev->layout;
5491 mddev->delta_disks = 0;
5496 void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5498 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5500 if (mddev->external_size)
5503 mddev->array_sectors = array_sectors;
5505 EXPORT_SYMBOL(md_set_array_sectors);
5507 static int update_size(mddev_t *mddev, sector_t num_sectors)
5511 int fit = (num_sectors == 0);
5513 if (mddev->pers->resize == NULL)
5515 /* The "num_sectors" is the number of sectors of each device that
5516 * is used. This can only make sense for arrays with redundancy.
5517 * linear and raid0 always use whatever space is available. We can only
5518 * consider changing this number if no resync or reconstruction is
5519 * happening, and if the new size is acceptable. It must fit before the
5520 * sb_start or, if that is <data_offset, it must fit before the size
5521 * of each device. If num_sectors is zero, we find the largest size
5525 if (mddev->sync_thread)
5528 /* Sorry, cannot grow a bitmap yet, just remove it,
5532 list_for_each_entry(rdev, &mddev->disks, same_set) {
5533 sector_t avail = rdev->sectors;
5535 if (fit && (num_sectors == 0 || num_sectors > avail))
5536 num_sectors = avail;
5537 if (avail < num_sectors)
5540 rv = mddev->pers->resize(mddev, num_sectors);
5542 revalidate_disk(mddev->gendisk);
5546 static int update_raid_disks(mddev_t *mddev, int raid_disks)
5549 /* change the number of raid disks */
5550 if (mddev->pers->check_reshape == NULL)
5552 if (raid_disks <= 0 ||
5553 (mddev->max_disks && raid_disks >= mddev->max_disks))
5555 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
5557 mddev->delta_disks = raid_disks - mddev->raid_disks;
5559 rv = mddev->pers->check_reshape(mddev);
5565 * update_array_info is used to change the configuration of an
5567 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5568 * fields in the info are checked against the array.
5569 * Any differences that cannot be handled will cause an error.
5570 * Normally, only one change can be managed at a time.
5572 static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5578 /* calculate expected state,ignoring low bits */
5579 if (mddev->bitmap && mddev->bitmap_info.offset)
5580 state |= (1 << MD_SB_BITMAP_PRESENT);
5582 if (mddev->major_version != info->major_version ||
5583 mddev->minor_version != info->minor_version ||
5584 /* mddev->patch_version != info->patch_version || */
5585 mddev->ctime != info->ctime ||
5586 mddev->level != info->level ||
5587 /* mddev->layout != info->layout || */
5588 !mddev->persistent != info->not_persistent||
5589 mddev->chunk_sectors != info->chunk_size >> 9 ||
5590 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5591 ((state^info->state) & 0xfffffe00)
5594 /* Check there is only one change */
5595 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5597 if (mddev->raid_disks != info->raid_disks)
5599 if (mddev->layout != info->layout)
5601 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5608 if (mddev->layout != info->layout) {
5610 * we don't need to do anything at the md level, the
5611 * personality will take care of it all.
5613 if (mddev->pers->check_reshape == NULL)
5616 mddev->new_layout = info->layout;
5617 rv = mddev->pers->check_reshape(mddev);
5619 mddev->new_layout = mddev->layout;
5623 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5624 rv = update_size(mddev, (sector_t)info->size * 2);
5626 if (mddev->raid_disks != info->raid_disks)
5627 rv = update_raid_disks(mddev, info->raid_disks);
5629 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5630 if (mddev->pers->quiesce == NULL)
5632 if (mddev->recovery || mddev->sync_thread)
5634 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5635 /* add the bitmap */
5638 if (mddev->bitmap_info.default_offset == 0)
5640 mddev->bitmap_info.offset =
5641 mddev->bitmap_info.default_offset;
5642 mddev->pers->quiesce(mddev, 1);
5643 rv = bitmap_create(mddev);
5645 rv = bitmap_load(mddev);
5647 bitmap_destroy(mddev);
5648 mddev->pers->quiesce(mddev, 0);
5650 /* remove the bitmap */
5653 if (mddev->bitmap->file)
5655 mddev->pers->quiesce(mddev, 1);
5656 bitmap_destroy(mddev);
5657 mddev->pers->quiesce(mddev, 0);
5658 mddev->bitmap_info.offset = 0;
5661 md_update_sb(mddev, 1);
5665 static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5669 if (mddev->pers == NULL)
5672 rdev = find_rdev(mddev, dev);
5676 md_error(mddev, rdev);
5681 * We have a problem here : there is no easy way to give a CHS
5682 * virtual geometry. We currently pretend that we have a 2 heads
5683 * 4 sectors (with a BIG number of cylinders...). This drives
5684 * dosfs just mad... ;-)
5686 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5688 mddev_t *mddev = bdev->bd_disk->private_data;
5692 geo->cylinders = mddev->array_sectors / 8;
5696 static int md_ioctl(struct block_device *bdev, fmode_t mode,
5697 unsigned int cmd, unsigned long arg)
5700 void __user *argp = (void __user *)arg;
5701 mddev_t *mddev = NULL;
5704 if (!capable(CAP_SYS_ADMIN))
5708 * Commands dealing with the RAID driver but not any
5714 err = get_version(argp);
5717 case PRINT_RAID_DEBUG:
5725 autostart_arrays(arg);
5732 * Commands creating/starting a new array:
5735 mddev = bdev->bd_disk->private_data;
5742 err = mddev_lock(mddev);
5745 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5752 case SET_ARRAY_INFO:
5754 mdu_array_info_t info;
5756 memset(&info, 0, sizeof(info));
5757 else if (copy_from_user(&info, argp, sizeof(info))) {
5762 err = update_array_info(mddev, &info);
5764 printk(KERN_WARNING "md: couldn't update"
5765 " array info. %d\n", err);
5770 if (!list_empty(&mddev->disks)) {
5772 "md: array %s already has disks!\n",
5777 if (mddev->raid_disks) {
5779 "md: array %s already initialised!\n",
5784 err = set_array_info(mddev, &info);
5786 printk(KERN_WARNING "md: couldn't set"
5787 " array info. %d\n", err);
5797 * Commands querying/configuring an existing array:
5799 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
5800 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
5801 if ((!mddev->raid_disks && !mddev->external)
5802 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5803 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5804 && cmd != GET_BITMAP_FILE) {
5810 * Commands even a read-only array can execute:
5814 case GET_ARRAY_INFO:
5815 err = get_array_info(mddev, argp);
5818 case GET_BITMAP_FILE:
5819 err = get_bitmap_file(mddev, argp);
5823 err = get_disk_info(mddev, argp);
5826 case RESTART_ARRAY_RW:
5827 err = restart_array(mddev);
5831 err = do_md_stop(mddev, 0, 1);
5835 err = md_set_readonly(mddev, 1);
5839 if (get_user(ro, (int __user *)(arg))) {
5845 /* if the bdev is going readonly the value of mddev->ro
5846 * does not matter, no writes are coming
5851 /* are we are already prepared for writes? */
5855 /* transitioning to readauto need only happen for
5856 * arrays that call md_write_start
5859 err = restart_array(mddev);
5862 set_disk_ro(mddev->gendisk, 0);
5869 * The remaining ioctls are changing the state of the
5870 * superblock, so we do not allow them on read-only arrays.
5871 * However non-MD ioctls (e.g. get-size) will still come through
5872 * here and hit the 'default' below, so only disallow
5873 * 'md' ioctls, and switch to rw mode if started auto-readonly.
5875 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
5876 if (mddev->ro == 2) {
5878 sysfs_notify_dirent_safe(mddev->sysfs_state);
5879 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5880 md_wakeup_thread(mddev->thread);
5891 mdu_disk_info_t info;
5892 if (copy_from_user(&info, argp, sizeof(info)))
5895 err = add_new_disk(mddev, &info);
5899 case HOT_REMOVE_DISK:
5900 err = hot_remove_disk(mddev, new_decode_dev(arg));
5904 err = hot_add_disk(mddev, new_decode_dev(arg));
5907 case SET_DISK_FAULTY:
5908 err = set_disk_faulty(mddev, new_decode_dev(arg));
5912 err = do_md_run(mddev);
5915 case SET_BITMAP_FILE:
5916 err = set_bitmap_file(mddev, (int)arg);
5926 if (mddev->hold_active == UNTIL_IOCTL &&
5928 mddev->hold_active = 0;
5929 mddev_unlock(mddev);
5938 #ifdef CONFIG_COMPAT
5939 static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
5940 unsigned int cmd, unsigned long arg)
5943 case HOT_REMOVE_DISK:
5945 case SET_DISK_FAULTY:
5946 case SET_BITMAP_FILE:
5947 /* These take in integer arg, do not convert */
5950 arg = (unsigned long)compat_ptr(arg);
5954 return md_ioctl(bdev, mode, cmd, arg);
5956 #endif /* CONFIG_COMPAT */
5958 static int md_open(struct block_device *bdev, fmode_t mode)
5961 * Succeed if we can lock the mddev, which confirms that
5962 * it isn't being stopped right now.
5964 mddev_t *mddev = mddev_find(bdev->bd_dev);
5967 if (mddev->gendisk != bdev->bd_disk) {
5968 /* we are racing with mddev_put which is discarding this
5972 /* Wait until bdev->bd_disk is definitely gone */
5973 flush_scheduled_work();
5974 /* Then retry the open from the top */
5975 return -ERESTARTSYS;
5977 BUG_ON(mddev != bdev->bd_disk->private_data);
5979 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
5983 atomic_inc(&mddev->openers);
5984 mutex_unlock(&mddev->open_mutex);
5986 check_disk_size_change(mddev->gendisk, bdev);
5991 static int md_release(struct gendisk *disk, fmode_t mode)
5993 mddev_t *mddev = disk->private_data;
5996 atomic_dec(&mddev->openers);
6001 static const struct block_device_operations md_fops =
6003 .owner = THIS_MODULE,
6005 .release = md_release,
6007 #ifdef CONFIG_COMPAT
6008 .compat_ioctl = md_compat_ioctl,
6010 .getgeo = md_getgeo,
6013 static int md_thread(void * arg)
6015 mdk_thread_t *thread = arg;
6018 * md_thread is a 'system-thread', it's priority should be very
6019 * high. We avoid resource deadlocks individually in each
6020 * raid personality. (RAID5 does preallocation) We also use RR and
6021 * the very same RT priority as kswapd, thus we will never get
6022 * into a priority inversion deadlock.
6024 * we definitely have to have equal or higher priority than
6025 * bdflush, otherwise bdflush will deadlock if there are too
6026 * many dirty RAID5 blocks.
6029 allow_signal(SIGKILL);
6030 while (!kthread_should_stop()) {
6032 /* We need to wait INTERRUPTIBLE so that
6033 * we don't add to the load-average.
6034 * That means we need to be sure no signals are
6037 if (signal_pending(current))
6038 flush_signals(current);
6040 wait_event_interruptible_timeout
6042 test_bit(THREAD_WAKEUP, &thread->flags)
6043 || kthread_should_stop(),
6046 clear_bit(THREAD_WAKEUP, &thread->flags);
6048 thread->run(thread->mddev);
6054 void md_wakeup_thread(mdk_thread_t *thread)
6057 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
6058 set_bit(THREAD_WAKEUP, &thread->flags);
6059 wake_up(&thread->wqueue);
6063 mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
6066 mdk_thread_t *thread;
6068 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
6072 init_waitqueue_head(&thread->wqueue);
6075 thread->mddev = mddev;
6076 thread->timeout = MAX_SCHEDULE_TIMEOUT;
6077 thread->tsk = kthread_run(md_thread, thread,
6079 mdname(thread->mddev),
6080 name ?: mddev->pers->name);
6081 if (IS_ERR(thread->tsk)) {
6088 void md_unregister_thread(mdk_thread_t *thread)
6092 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
6094 kthread_stop(thread->tsk);
6098 void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
6105 if (!rdev || test_bit(Faulty, &rdev->flags))
6108 if (mddev->external)
6109 set_bit(Blocked, &rdev->flags);
6111 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6113 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6114 __builtin_return_address(0),__builtin_return_address(1),
6115 __builtin_return_address(2),__builtin_return_address(3));
6119 if (!mddev->pers->error_handler)
6121 mddev->pers->error_handler(mddev,rdev);
6122 if (mddev->degraded)
6123 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6124 sysfs_notify_dirent_safe(rdev->sysfs_state);
6125 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6126 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6127 md_wakeup_thread(mddev->thread);
6128 if (mddev->event_work.func)
6129 schedule_work(&mddev->event_work);
6130 md_new_event_inintr(mddev);
6133 /* seq_file implementation /proc/mdstat */
6135 static void status_unused(struct seq_file *seq)
6140 seq_printf(seq, "unused devices: ");
6142 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
6143 char b[BDEVNAME_SIZE];
6145 seq_printf(seq, "%s ",
6146 bdevname(rdev->bdev,b));
6149 seq_printf(seq, "<none>");
6151 seq_printf(seq, "\n");
6155 static void status_resync(struct seq_file *seq, mddev_t * mddev)
6157 sector_t max_sectors, resync, res;
6158 unsigned long dt, db;
6161 unsigned int per_milli;
6163 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
6165 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6166 max_sectors = mddev->resync_max_sectors;
6168 max_sectors = mddev->dev_sectors;
6171 * Should not happen.
6177 /* Pick 'scale' such that (resync>>scale)*1000 will fit
6178 * in a sector_t, and (max_sectors>>scale) will fit in a
6179 * u32, as those are the requirements for sector_div.
6180 * Thus 'scale' must be at least 10
6183 if (sizeof(sector_t) > sizeof(unsigned long)) {
6184 while ( max_sectors/2 > (1ULL<<(scale+32)))
6187 res = (resync>>scale)*1000;
6188 sector_div(res, (u32)((max_sectors>>scale)+1));
6192 int i, x = per_milli/50, y = 20-x;
6193 seq_printf(seq, "[");
6194 for (i = 0; i < x; i++)
6195 seq_printf(seq, "=");
6196 seq_printf(seq, ">");
6197 for (i = 0; i < y; i++)
6198 seq_printf(seq, ".");
6199 seq_printf(seq, "] ");
6201 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
6202 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6204 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6206 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6207 "resync" : "recovery"))),
6208 per_milli/10, per_milli % 10,
6209 (unsigned long long) resync/2,
6210 (unsigned long long) max_sectors/2);
6213 * dt: time from mark until now
6214 * db: blocks written from mark until now
6215 * rt: remaining time
6217 * rt is a sector_t, so could be 32bit or 64bit.
6218 * So we divide before multiply in case it is 32bit and close
6220 * We scale the divisor (db) by 32 to avoid loosing precision
6221 * near the end of resync when the number of remaining sectors
6223 * We then divide rt by 32 after multiplying by db to compensate.
6224 * The '+1' avoids division by zero if db is very small.
6226 dt = ((jiffies - mddev->resync_mark) / HZ);
6228 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6229 - mddev->resync_mark_cnt;
6231 rt = max_sectors - resync; /* number of remaining sectors */
6232 sector_div(rt, db/32+1);
6236 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6237 ((unsigned long)rt % 60)/6);
6239 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
6242 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6244 struct list_head *tmp;
6254 spin_lock(&all_mddevs_lock);
6255 list_for_each(tmp,&all_mddevs)
6257 mddev = list_entry(tmp, mddev_t, all_mddevs);
6259 spin_unlock(&all_mddevs_lock);
6262 spin_unlock(&all_mddevs_lock);
6264 return (void*)2;/* tail */
6268 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6270 struct list_head *tmp;
6271 mddev_t *next_mddev, *mddev = v;
6277 spin_lock(&all_mddevs_lock);
6279 tmp = all_mddevs.next;
6281 tmp = mddev->all_mddevs.next;
6282 if (tmp != &all_mddevs)
6283 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6285 next_mddev = (void*)2;
6288 spin_unlock(&all_mddevs_lock);
6296 static void md_seq_stop(struct seq_file *seq, void *v)
6300 if (mddev && v != (void*)1 && v != (void*)2)
6304 struct mdstat_info {
6308 static int md_seq_show(struct seq_file *seq, void *v)
6313 struct mdstat_info *mi = seq->private;
6314 struct bitmap *bitmap;
6316 if (v == (void*)1) {
6317 struct mdk_personality *pers;
6318 seq_printf(seq, "Personalities : ");
6319 spin_lock(&pers_lock);
6320 list_for_each_entry(pers, &pers_list, list)
6321 seq_printf(seq, "[%s] ", pers->name);
6323 spin_unlock(&pers_lock);
6324 seq_printf(seq, "\n");
6325 mi->event = atomic_read(&md_event_count);
6328 if (v == (void*)2) {
6333 if (mddev_lock(mddev) < 0)
6336 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6337 seq_printf(seq, "%s : %sactive", mdname(mddev),
6338 mddev->pers ? "" : "in");
6341 seq_printf(seq, " (read-only)");
6343 seq_printf(seq, " (auto-read-only)");
6344 seq_printf(seq, " %s", mddev->pers->name);
6348 list_for_each_entry(rdev, &mddev->disks, same_set) {
6349 char b[BDEVNAME_SIZE];
6350 seq_printf(seq, " %s[%d]",
6351 bdevname(rdev->bdev,b), rdev->desc_nr);
6352 if (test_bit(WriteMostly, &rdev->flags))
6353 seq_printf(seq, "(W)");
6354 if (test_bit(Faulty, &rdev->flags)) {
6355 seq_printf(seq, "(F)");
6357 } else if (rdev->raid_disk < 0)
6358 seq_printf(seq, "(S)"); /* spare */
6359 sectors += rdev->sectors;
6362 if (!list_empty(&mddev->disks)) {
6364 seq_printf(seq, "\n %llu blocks",
6365 (unsigned long long)
6366 mddev->array_sectors / 2);
6368 seq_printf(seq, "\n %llu blocks",
6369 (unsigned long long)sectors / 2);
6371 if (mddev->persistent) {
6372 if (mddev->major_version != 0 ||
6373 mddev->minor_version != 90) {
6374 seq_printf(seq," super %d.%d",
6375 mddev->major_version,
6376 mddev->minor_version);
6378 } else if (mddev->external)
6379 seq_printf(seq, " super external:%s",
6380 mddev->metadata_type);
6382 seq_printf(seq, " super non-persistent");
6385 mddev->pers->status(seq, mddev);
6386 seq_printf(seq, "\n ");
6387 if (mddev->pers->sync_request) {
6388 if (mddev->curr_resync > 2) {
6389 status_resync(seq, mddev);
6390 seq_printf(seq, "\n ");
6391 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6392 seq_printf(seq, "\tresync=DELAYED\n ");
6393 else if (mddev->recovery_cp < MaxSector)
6394 seq_printf(seq, "\tresync=PENDING\n ");
6397 seq_printf(seq, "\n ");
6399 if ((bitmap = mddev->bitmap)) {
6400 unsigned long chunk_kb;
6401 unsigned long flags;
6402 spin_lock_irqsave(&bitmap->lock, flags);
6403 chunk_kb = mddev->bitmap_info.chunksize >> 10;
6404 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6406 bitmap->pages - bitmap->missing_pages,
6408 (bitmap->pages - bitmap->missing_pages)
6409 << (PAGE_SHIFT - 10),
6410 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
6411 chunk_kb ? "KB" : "B");
6413 seq_printf(seq, ", file: ");
6414 seq_path(seq, &bitmap->file->f_path, " \t\n");
6417 seq_printf(seq, "\n");
6418 spin_unlock_irqrestore(&bitmap->lock, flags);
6421 seq_printf(seq, "\n");
6423 mddev_unlock(mddev);
6428 static const struct seq_operations md_seq_ops = {
6429 .start = md_seq_start,
6430 .next = md_seq_next,
6431 .stop = md_seq_stop,
6432 .show = md_seq_show,
6435 static int md_seq_open(struct inode *inode, struct file *file)
6438 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6442 error = seq_open(file, &md_seq_ops);
6446 struct seq_file *p = file->private_data;
6448 mi->event = atomic_read(&md_event_count);
6453 static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6455 struct seq_file *m = filp->private_data;
6456 struct mdstat_info *mi = m->private;
6459 poll_wait(filp, &md_event_waiters, wait);
6461 /* always allow read */
6462 mask = POLLIN | POLLRDNORM;
6464 if (mi->event != atomic_read(&md_event_count))
6465 mask |= POLLERR | POLLPRI;
6469 static const struct file_operations md_seq_fops = {
6470 .owner = THIS_MODULE,
6471 .open = md_seq_open,
6473 .llseek = seq_lseek,
6474 .release = seq_release_private,
6475 .poll = mdstat_poll,
6478 int register_md_personality(struct mdk_personality *p)
6480 spin_lock(&pers_lock);
6481 list_add_tail(&p->list, &pers_list);
6482 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
6483 spin_unlock(&pers_lock);
6487 int unregister_md_personality(struct mdk_personality *p)
6489 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
6490 spin_lock(&pers_lock);
6491 list_del_init(&p->list);
6492 spin_unlock(&pers_lock);
6496 static int is_mddev_idle(mddev_t *mddev, int init)
6504 rdev_for_each_rcu(rdev, mddev) {
6505 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
6506 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6507 (int)part_stat_read(&disk->part0, sectors[1]) -
6508 atomic_read(&disk->sync_io);
6509 /* sync IO will cause sync_io to increase before the disk_stats
6510 * as sync_io is counted when a request starts, and
6511 * disk_stats is counted when it completes.
6512 * So resync activity will cause curr_events to be smaller than
6513 * when there was no such activity.
6514 * non-sync IO will cause disk_stat to increase without
6515 * increasing sync_io so curr_events will (eventually)
6516 * be larger than it was before. Once it becomes
6517 * substantially larger, the test below will cause
6518 * the array to appear non-idle, and resync will slow
6520 * If there is a lot of outstanding resync activity when
6521 * we set last_event to curr_events, then all that activity
6522 * completing might cause the array to appear non-idle
6523 * and resync will be slowed down even though there might
6524 * not have been non-resync activity. This will only
6525 * happen once though. 'last_events' will soon reflect
6526 * the state where there is little or no outstanding
6527 * resync requests, and further resync activity will
6528 * always make curr_events less than last_events.
6531 if (init || curr_events - rdev->last_events > 64) {
6532 rdev->last_events = curr_events;
6540 void md_done_sync(mddev_t *mddev, int blocks, int ok)
6542 /* another "blocks" (512byte) blocks have been synced */
6543 atomic_sub(blocks, &mddev->recovery_active);
6544 wake_up(&mddev->recovery_wait);
6546 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6547 md_wakeup_thread(mddev->thread);
6548 // stop recovery, signal do_sync ....
6553 /* md_write_start(mddev, bi)
6554 * If we need to update some array metadata (e.g. 'active' flag
6555 * in superblock) before writing, schedule a superblock update
6556 * and wait for it to complete.
6558 void md_write_start(mddev_t *mddev, struct bio *bi)
6561 if (bio_data_dir(bi) != WRITE)
6564 BUG_ON(mddev->ro == 1);
6565 if (mddev->ro == 2) {
6566 /* need to switch to read/write */
6568 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6569 md_wakeup_thread(mddev->thread);
6570 md_wakeup_thread(mddev->sync_thread);
6573 atomic_inc(&mddev->writes_pending);
6574 if (mddev->safemode == 1)
6575 mddev->safemode = 0;
6576 if (mddev->in_sync) {
6577 spin_lock_irq(&mddev->write_lock);
6578 if (mddev->in_sync) {
6580 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6581 md_wakeup_thread(mddev->thread);
6584 spin_unlock_irq(&mddev->write_lock);
6587 sysfs_notify_dirent_safe(mddev->sysfs_state);
6588 wait_event(mddev->sb_wait,
6589 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6590 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
6593 void md_write_end(mddev_t *mddev)
6595 if (atomic_dec_and_test(&mddev->writes_pending)) {
6596 if (mddev->safemode == 2)
6597 md_wakeup_thread(mddev->thread);
6598 else if (mddev->safemode_delay)
6599 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6603 /* md_allow_write(mddev)
6604 * Calling this ensures that the array is marked 'active' so that writes
6605 * may proceed without blocking. It is important to call this before
6606 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6607 * Must be called with mddev_lock held.
6609 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6610 * is dropped, so return -EAGAIN after notifying userspace.
6612 int md_allow_write(mddev_t *mddev)
6618 if (!mddev->pers->sync_request)
6621 spin_lock_irq(&mddev->write_lock);
6622 if (mddev->in_sync) {
6624 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6625 if (mddev->safemode_delay &&
6626 mddev->safemode == 0)
6627 mddev->safemode = 1;
6628 spin_unlock_irq(&mddev->write_lock);
6629 md_update_sb(mddev, 0);
6630 sysfs_notify_dirent_safe(mddev->sysfs_state);
6632 spin_unlock_irq(&mddev->write_lock);
6634 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6639 EXPORT_SYMBOL_GPL(md_allow_write);
6641 void md_unplug(mddev_t *mddev)
6644 blk_unplug(mddev->queue);
6646 mddev->plug->unplug_fn(mddev->plug);
6649 #define SYNC_MARKS 10
6650 #define SYNC_MARK_STEP (3*HZ)
6651 void md_do_sync(mddev_t *mddev)
6654 unsigned int currspeed = 0,
6656 sector_t max_sectors,j, io_sectors;
6657 unsigned long mark[SYNC_MARKS];
6658 sector_t mark_cnt[SYNC_MARKS];
6660 struct list_head *tmp;
6661 sector_t last_check;
6666 /* just incase thread restarts... */
6667 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6669 if (mddev->ro) /* never try to sync a read-only array */
6672 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6673 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6674 desc = "data-check";
6675 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6676 desc = "requested-resync";
6679 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6684 /* we overload curr_resync somewhat here.
6685 * 0 == not engaged in resync at all
6686 * 2 == checking that there is no conflict with another sync
6687 * 1 == like 2, but have yielded to allow conflicting resync to
6689 * other == active in resync - this many blocks
6691 * Before starting a resync we must have set curr_resync to
6692 * 2, and then checked that every "conflicting" array has curr_resync
6693 * less than ours. When we find one that is the same or higher
6694 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6695 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6696 * This will mean we have to start checking from the beginning again.
6701 mddev->curr_resync = 2;
6704 if (kthread_should_stop())
6705 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6707 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6709 for_each_mddev(mddev2, tmp) {
6710 if (mddev2 == mddev)
6712 if (!mddev->parallel_resync
6713 && mddev2->curr_resync
6714 && match_mddev_units(mddev, mddev2)) {
6716 if (mddev < mddev2 && mddev->curr_resync == 2) {
6717 /* arbitrarily yield */
6718 mddev->curr_resync = 1;
6719 wake_up(&resync_wait);
6721 if (mddev > mddev2 && mddev->curr_resync == 1)
6722 /* no need to wait here, we can wait the next
6723 * time 'round when curr_resync == 2
6726 /* We need to wait 'interruptible' so as not to
6727 * contribute to the load average, and not to
6728 * be caught by 'softlockup'
6730 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
6731 if (!kthread_should_stop() &&
6732 mddev2->curr_resync >= mddev->curr_resync) {
6733 printk(KERN_INFO "md: delaying %s of %s"
6734 " until %s has finished (they"
6735 " share one or more physical units)\n",
6736 desc, mdname(mddev), mdname(mddev2));
6738 if (signal_pending(current))
6739 flush_signals(current);
6741 finish_wait(&resync_wait, &wq);
6744 finish_wait(&resync_wait, &wq);
6747 } while (mddev->curr_resync < 2);
6750 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6751 /* resync follows the size requested by the personality,
6752 * which defaults to physical size, but can be virtual size
6754 max_sectors = mddev->resync_max_sectors;
6755 mddev->resync_mismatches = 0;
6756 /* we don't use the checkpoint if there's a bitmap */
6757 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6758 j = mddev->resync_min;
6759 else if (!mddev->bitmap)
6760 j = mddev->recovery_cp;
6762 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6763 max_sectors = mddev->dev_sectors;
6765 /* recovery follows the physical size of devices */
6766 max_sectors = mddev->dev_sectors;
6769 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
6770 if (rdev->raid_disk >= 0 &&
6771 !test_bit(Faulty, &rdev->flags) &&
6772 !test_bit(In_sync, &rdev->flags) &&
6773 rdev->recovery_offset < j)
6774 j = rdev->recovery_offset;
6778 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6779 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6780 " %d KB/sec/disk.\n", speed_min(mddev));
6781 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
6782 "(but not more than %d KB/sec) for %s.\n",
6783 speed_max(mddev), desc);
6785 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
6788 for (m = 0; m < SYNC_MARKS; m++) {
6790 mark_cnt[m] = io_sectors;
6793 mddev->resync_mark = mark[last_mark];
6794 mddev->resync_mark_cnt = mark_cnt[last_mark];
6797 * Tune reconstruction:
6799 window = 32*(PAGE_SIZE/512);
6800 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6801 window/2,(unsigned long long) max_sectors/2);
6803 atomic_set(&mddev->recovery_active, 0);
6808 "md: resuming %s of %s from checkpoint.\n",
6809 desc, mdname(mddev));
6810 mddev->curr_resync = j;
6812 mddev->curr_resync_completed = mddev->curr_resync;
6814 while (j < max_sectors) {
6819 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6820 ((mddev->curr_resync > mddev->curr_resync_completed &&
6821 (mddev->curr_resync - mddev->curr_resync_completed)
6822 > (max_sectors >> 4)) ||
6823 (j - mddev->curr_resync_completed)*2
6824 >= mddev->resync_max - mddev->curr_resync_completed
6826 /* time to update curr_resync_completed */
6828 wait_event(mddev->recovery_wait,
6829 atomic_read(&mddev->recovery_active) == 0);
6830 mddev->curr_resync_completed =
6832 if (mddev->persistent)
6833 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6834 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
6837 while (j >= mddev->resync_max && !kthread_should_stop()) {
6838 /* As this condition is controlled by user-space,
6839 * we can block indefinitely, so use '_interruptible'
6840 * to avoid triggering warnings.
6842 flush_signals(current); /* just in case */
6843 wait_event_interruptible(mddev->recovery_wait,
6844 mddev->resync_max > j
6845 || kthread_should_stop());
6848 if (kthread_should_stop())
6851 sectors = mddev->pers->sync_request(mddev, j, &skipped,
6852 currspeed < speed_min(mddev));
6854 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6858 if (!skipped) { /* actual IO requested */
6859 io_sectors += sectors;
6860 atomic_add(sectors, &mddev->recovery_active);
6864 if (j>1) mddev->curr_resync = j;
6865 mddev->curr_mark_cnt = io_sectors;
6866 if (last_check == 0)
6867 /* this is the earliers that rebuilt will be
6868 * visible in /proc/mdstat
6870 md_new_event(mddev);
6872 if (last_check + window > io_sectors || j == max_sectors)
6875 last_check = io_sectors;
6877 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6881 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6883 int next = (last_mark+1) % SYNC_MARKS;
6885 mddev->resync_mark = mark[next];
6886 mddev->resync_mark_cnt = mark_cnt[next];
6887 mark[next] = jiffies;
6888 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
6893 if (kthread_should_stop())
6898 * this loop exits only if either when we are slower than
6899 * the 'hard' speed limit, or the system was IO-idle for
6901 * the system might be non-idle CPU-wise, but we only care
6902 * about not overloading the IO subsystem. (things like an
6903 * e2fsck being done on the RAID array should execute fast)
6908 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6909 /((jiffies-mddev->resync_mark)/HZ +1) +1;
6911 if (currspeed > speed_min(mddev)) {
6912 if ((currspeed > speed_max(mddev)) ||
6913 !is_mddev_idle(mddev, 0)) {
6919 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
6921 * this also signals 'finished resyncing' to md_stop
6926 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6928 /* tell personality that we are finished */
6929 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
6931 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
6932 mddev->curr_resync > 2) {
6933 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6934 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6935 if (mddev->curr_resync >= mddev->recovery_cp) {
6937 "md: checkpointing %s of %s.\n",
6938 desc, mdname(mddev));
6939 mddev->recovery_cp = mddev->curr_resync;
6942 mddev->recovery_cp = MaxSector;
6944 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6945 mddev->curr_resync = MaxSector;
6947 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
6948 if (rdev->raid_disk >= 0 &&
6949 mddev->delta_disks >= 0 &&
6950 !test_bit(Faulty, &rdev->flags) &&
6951 !test_bit(In_sync, &rdev->flags) &&
6952 rdev->recovery_offset < mddev->curr_resync)
6953 rdev->recovery_offset = mddev->curr_resync;
6957 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6960 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6961 /* We completed so min/max setting can be forgotten if used. */
6962 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6963 mddev->resync_min = 0;
6964 mddev->resync_max = MaxSector;
6965 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6966 mddev->resync_min = mddev->curr_resync_completed;
6967 mddev->curr_resync = 0;
6968 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6969 mddev->curr_resync_completed = 0;
6970 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
6971 wake_up(&resync_wait);
6972 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6973 md_wakeup_thread(mddev->thread);
6978 * got a signal, exit.
6981 "md: md_do_sync() got signal ... exiting\n");
6982 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6986 EXPORT_SYMBOL_GPL(md_do_sync);
6989 static int remove_and_add_spares(mddev_t *mddev)
6994 mddev->curr_resync_completed = 0;
6996 list_for_each_entry(rdev, &mddev->disks, same_set)
6997 if (rdev->raid_disk >= 0 &&
6998 !test_bit(Blocked, &rdev->flags) &&
6999 (test_bit(Faulty, &rdev->flags) ||
7000 ! test_bit(In_sync, &rdev->flags)) &&
7001 atomic_read(&rdev->nr_pending)==0) {
7002 if (mddev->pers->hot_remove_disk(
7003 mddev, rdev->raid_disk)==0) {
7005 sprintf(nm,"rd%d", rdev->raid_disk);
7006 sysfs_remove_link(&mddev->kobj, nm);
7007 rdev->raid_disk = -1;
7011 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
7012 list_for_each_entry(rdev, &mddev->disks, same_set) {
7013 if (rdev->raid_disk >= 0 &&
7014 !test_bit(In_sync, &rdev->flags) &&
7015 !test_bit(Blocked, &rdev->flags))
7017 if (rdev->raid_disk < 0
7018 && !test_bit(Faulty, &rdev->flags)) {
7019 rdev->recovery_offset = 0;
7021 hot_add_disk(mddev, rdev) == 0) {
7023 sprintf(nm, "rd%d", rdev->raid_disk);
7024 if (sysfs_create_link(&mddev->kobj,
7026 /* failure here is OK */;
7028 md_new_event(mddev);
7029 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7038 * This routine is regularly called by all per-raid-array threads to
7039 * deal with generic issues like resync and super-block update.
7040 * Raid personalities that don't have a thread (linear/raid0) do not
7041 * need this as they never do any recovery or update the superblock.
7043 * It does not do any resync itself, but rather "forks" off other threads
7044 * to do that as needed.
7045 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7046 * "->recovery" and create a thread at ->sync_thread.
7047 * When the thread finishes it sets MD_RECOVERY_DONE
7048 * and wakeups up this thread which will reap the thread and finish up.
7049 * This thread also removes any faulty devices (with nr_pending == 0).
7051 * The overall approach is:
7052 * 1/ if the superblock needs updating, update it.
7053 * 2/ If a recovery thread is running, don't do anything else.
7054 * 3/ If recovery has finished, clean up, possibly marking spares active.
7055 * 4/ If there are any faulty devices, remove them.
7056 * 5/ If array is degraded, try to add spares devices
7057 * 6/ If array has spares or is not in-sync, start a resync thread.
7059 void md_check_recovery(mddev_t *mddev)
7065 bitmap_daemon_work(mddev);
7070 if (signal_pending(current)) {
7071 if (mddev->pers->sync_request && !mddev->external) {
7072 printk(KERN_INFO "md: %s in immediate safe mode\n",
7074 mddev->safemode = 2;
7076 flush_signals(current);
7079 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7082 (mddev->flags && !mddev->external) ||
7083 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7084 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
7085 (mddev->external == 0 && mddev->safemode == 1) ||
7086 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7087 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
7091 if (mddev_trylock(mddev)) {
7095 /* Only thing we do on a ro array is remove
7098 remove_and_add_spares(mddev);
7099 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7103 if (!mddev->external) {
7105 spin_lock_irq(&mddev->write_lock);
7106 if (mddev->safemode &&
7107 !atomic_read(&mddev->writes_pending) &&
7109 mddev->recovery_cp == MaxSector) {
7112 if (mddev->persistent)
7113 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
7115 if (mddev->safemode == 1)
7116 mddev->safemode = 0;
7117 spin_unlock_irq(&mddev->write_lock);
7119 sysfs_notify_dirent_safe(mddev->sysfs_state);
7123 md_update_sb(mddev, 0);
7125 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7126 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7127 /* resync/recovery still happening */
7128 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7131 if (mddev->sync_thread) {
7132 /* resync has finished, collect result */
7133 md_unregister_thread(mddev->sync_thread);
7134 mddev->sync_thread = NULL;
7135 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7136 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7138 /* activate any spares */
7139 if (mddev->pers->spare_active(mddev))
7140 sysfs_notify(&mddev->kobj, NULL,
7143 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7144 mddev->pers->finish_reshape)
7145 mddev->pers->finish_reshape(mddev);
7146 md_update_sb(mddev, 1);
7148 /* if array is no-longer degraded, then any saved_raid_disk
7149 * information must be scrapped
7151 if (!mddev->degraded)
7152 list_for_each_entry(rdev, &mddev->disks, same_set)
7153 rdev->saved_raid_disk = -1;
7155 mddev->recovery = 0;
7156 /* flag recovery needed just to double check */
7157 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7158 sysfs_notify_dirent_safe(mddev->sysfs_action);
7159 md_new_event(mddev);
7162 /* Set RUNNING before clearing NEEDED to avoid
7163 * any transients in the value of "sync_action".
7165 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7166 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7167 /* Clear some bits that don't mean anything, but
7170 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7171 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
7173 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7175 /* no recovery is running.
7176 * remove any failed drives, then
7177 * add spares if possible.
7178 * Spare are also removed and re-added, to allow
7179 * the personality to fail the re-add.
7182 if (mddev->reshape_position != MaxSector) {
7183 if (mddev->pers->check_reshape == NULL ||
7184 mddev->pers->check_reshape(mddev) != 0)
7185 /* Cannot proceed */
7187 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7188 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7189 } else if ((spares = remove_and_add_spares(mddev))) {
7190 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7191 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7192 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7193 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7194 } else if (mddev->recovery_cp < MaxSector) {
7195 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7196 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7197 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7198 /* nothing to be done ... */
7201 if (mddev->pers->sync_request) {
7202 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7203 /* We are adding a device or devices to an array
7204 * which has the bitmap stored on all devices.
7205 * So make sure all bitmap pages get written
7207 bitmap_write_all(mddev->bitmap);
7209 mddev->sync_thread = md_register_thread(md_do_sync,
7212 if (!mddev->sync_thread) {
7213 printk(KERN_ERR "%s: could not start resync"
7216 /* leave the spares where they are, it shouldn't hurt */
7217 mddev->recovery = 0;
7219 md_wakeup_thread(mddev->sync_thread);
7220 sysfs_notify_dirent_safe(mddev->sysfs_action);
7221 md_new_event(mddev);
7224 if (!mddev->sync_thread) {
7225 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7226 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7228 if (mddev->sysfs_action)
7229 sysfs_notify_dirent_safe(mddev->sysfs_action);
7231 mddev_unlock(mddev);
7235 void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7237 sysfs_notify_dirent_safe(rdev->sysfs_state);
7238 wait_event_timeout(rdev->blocked_wait,
7239 !test_bit(Blocked, &rdev->flags),
7240 msecs_to_jiffies(5000));
7241 rdev_dec_pending(rdev, mddev);
7243 EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7245 static int md_notify_reboot(struct notifier_block *this,
7246 unsigned long code, void *x)
7248 struct list_head *tmp;
7251 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7253 printk(KERN_INFO "md: stopping all md devices.\n");
7255 for_each_mddev(mddev, tmp)
7256 if (mddev_trylock(mddev)) {
7257 /* Force a switch to readonly even array
7258 * appears to still be in use. Hence
7261 md_set_readonly(mddev, 100);
7262 mddev_unlock(mddev);
7265 * certain more exotic SCSI devices are known to be
7266 * volatile wrt too early system reboots. While the
7267 * right place to handle this issue is the given
7268 * driver, we do want to have a safe RAID driver ...
7275 static struct notifier_block md_notifier = {
7276 .notifier_call = md_notify_reboot,
7278 .priority = INT_MAX, /* before any real devices */
7281 static void md_geninit(void)
7283 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7285 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
7288 static int __init md_init(void)
7290 if (register_blkdev(MD_MAJOR, "md"))
7292 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
7293 unregister_blkdev(MD_MAJOR, "md");
7296 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
7297 md_probe, NULL, NULL);
7298 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
7299 md_probe, NULL, NULL);
7301 register_reboot_notifier(&md_notifier);
7302 raid_table_header = register_sysctl_table(raid_root_table);
7312 * Searches all registered partitions for autorun RAID arrays
7316 static LIST_HEAD(all_detected_devices);
7317 struct detected_devices_node {
7318 struct list_head list;
7322 void md_autodetect_dev(dev_t dev)
7324 struct detected_devices_node *node_detected_dev;
7326 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7327 if (node_detected_dev) {
7328 node_detected_dev->dev = dev;
7329 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7331 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7332 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7337 static void autostart_arrays(int part)
7340 struct detected_devices_node *node_detected_dev;
7342 int i_scanned, i_passed;
7347 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7349 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7351 node_detected_dev = list_entry(all_detected_devices.next,
7352 struct detected_devices_node, list);
7353 list_del(&node_detected_dev->list);
7354 dev = node_detected_dev->dev;
7355 kfree(node_detected_dev);
7356 rdev = md_import_device(dev,0, 90);
7360 if (test_bit(Faulty, &rdev->flags)) {
7364 set_bit(AutoDetected, &rdev->flags);
7365 list_add(&rdev->same_set, &pending_raid_disks);
7369 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7370 i_scanned, i_passed);
7372 autorun_devices(part);
7375 #endif /* !MODULE */
7377 static __exit void md_exit(void)
7380 struct list_head *tmp;
7382 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
7383 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
7385 unregister_blkdev(MD_MAJOR,"md");
7386 unregister_blkdev(mdp_major, "mdp");
7387 unregister_reboot_notifier(&md_notifier);
7388 unregister_sysctl_table(raid_table_header);
7389 remove_proc_entry("mdstat", NULL);
7390 for_each_mddev(mddev, tmp) {
7391 export_array(mddev);
7392 mddev->hold_active = 0;
7396 subsys_initcall(md_init);
7397 module_exit(md_exit)
7399 static int get_ro(char *buffer, struct kernel_param *kp)
7401 return sprintf(buffer, "%d", start_readonly);
7403 static int set_ro(const char *val, struct kernel_param *kp)
7406 int num = simple_strtoul(val, &e, 10);
7407 if (*val && (*e == '\0' || *e == '\n')) {
7408 start_readonly = num;
7414 module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7415 module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
7417 module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
7419 EXPORT_SYMBOL(register_md_personality);
7420 EXPORT_SYMBOL(unregister_md_personality);
7421 EXPORT_SYMBOL(md_error);
7422 EXPORT_SYMBOL(md_done_sync);
7423 EXPORT_SYMBOL(md_write_start);
7424 EXPORT_SYMBOL(md_write_end);
7425 EXPORT_SYMBOL(md_register_thread);
7426 EXPORT_SYMBOL(md_unregister_thread);
7427 EXPORT_SYMBOL(md_wakeup_thread);
7428 EXPORT_SYMBOL(md_check_recovery);
7429 MODULE_LICENSE("GPL");
7430 MODULE_DESCRIPTION("MD RAID framework");
7432 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);