V4L/DVB (9023): cx88: HVR3000 / 4000 GPIO related changes
[zen-stable.git] / drivers / md / md.c
blob0a3a4bdcd4afd55ad52fd08ecc3d99713bcb9a87
1 /*
2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
5 completely rewritten, based on the MD driver code from Marc Zyngier
7 Changes:
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
20 Neil Brown <neilb@cse.unsw.edu.au>.
22 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
25 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include <linux/module.h>
36 #include <linux/kernel.h>
37 #include <linux/kthread.h>
38 #include <linux/linkage.h>
39 #include <linux/raid/md.h>
40 #include <linux/raid/bitmap.h>
41 #include <linux/sysctl.h>
42 #include <linux/buffer_head.h> /* for invalidate_bdev */
43 #include <linux/poll.h>
44 #include <linux/mutex.h>
45 #include <linux/ctype.h>
46 #include <linux/freezer.h>
48 #include <linux/init.h>
50 #include <linux/file.h>
52 #ifdef CONFIG_KMOD
53 #include <linux/kmod.h>
54 #endif
56 #include <asm/unaligned.h>
58 #define MAJOR_NR MD_MAJOR
59 #define MD_DRIVER
61 /* 63 partitions with the alternate major number (mdp) */
62 #define MdpMinorShift 6
64 #define DEBUG 0
65 #define dprintk(x...) ((void)(DEBUG && printk(x)))
68 #ifndef MODULE
69 static void autostart_arrays (int part);
70 #endif
72 static LIST_HEAD(pers_list);
73 static DEFINE_SPINLOCK(pers_lock);
75 static void md_print_devices(void);
77 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
79 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
82 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
83 * is 1000 KB/sec, so the extra system load does not show up that much.
84 * Increase it if you want to have more _guaranteed_ speed. Note that
85 * the RAID driver will use the maximum available bandwidth if the IO
86 * subsystem is idle. There is also an 'absolute maximum' reconstruction
87 * speed limit - in case reconstruction slows down your system despite
88 * idle IO detection.
90 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
91 * or /sys/block/mdX/md/sync_speed_{min,max}
94 static int sysctl_speed_limit_min = 1000;
95 static int sysctl_speed_limit_max = 200000;
96 static inline int speed_min(mddev_t *mddev)
98 return mddev->sync_speed_min ?
99 mddev->sync_speed_min : sysctl_speed_limit_min;
102 static inline int speed_max(mddev_t *mddev)
104 return mddev->sync_speed_max ?
105 mddev->sync_speed_max : sysctl_speed_limit_max;
108 static struct ctl_table_header *raid_table_header;
110 static ctl_table raid_table[] = {
112 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
113 .procname = "speed_limit_min",
114 .data = &sysctl_speed_limit_min,
115 .maxlen = sizeof(int),
116 .mode = S_IRUGO|S_IWUSR,
117 .proc_handler = &proc_dointvec,
120 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
121 .procname = "speed_limit_max",
122 .data = &sysctl_speed_limit_max,
123 .maxlen = sizeof(int),
124 .mode = S_IRUGO|S_IWUSR,
125 .proc_handler = &proc_dointvec,
127 { .ctl_name = 0 }
130 static ctl_table raid_dir_table[] = {
132 .ctl_name = DEV_RAID,
133 .procname = "raid",
134 .maxlen = 0,
135 .mode = S_IRUGO|S_IXUGO,
136 .child = raid_table,
138 { .ctl_name = 0 }
141 static ctl_table raid_root_table[] = {
143 .ctl_name = CTL_DEV,
144 .procname = "dev",
145 .maxlen = 0,
146 .mode = 0555,
147 .child = raid_dir_table,
149 { .ctl_name = 0 }
152 static struct block_device_operations md_fops;
154 static int start_readonly;
157 * We have a system wide 'event count' that is incremented
158 * on any 'interesting' event, and readers of /proc/mdstat
159 * can use 'poll' or 'select' to find out when the event
160 * count increases.
162 * Events are:
163 * start array, stop array, error, add device, remove device,
164 * start build, activate spare
166 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
167 static atomic_t md_event_count;
168 void md_new_event(mddev_t *mddev)
170 atomic_inc(&md_event_count);
171 wake_up(&md_event_waiters);
173 EXPORT_SYMBOL_GPL(md_new_event);
175 /* Alternate version that can be called from interrupts
176 * when calling sysfs_notify isn't needed.
178 static void md_new_event_inintr(mddev_t *mddev)
180 atomic_inc(&md_event_count);
181 wake_up(&md_event_waiters);
185 * Enables to iterate over all existing md arrays
186 * all_mddevs_lock protects this list.
188 static LIST_HEAD(all_mddevs);
189 static DEFINE_SPINLOCK(all_mddevs_lock);
193 * iterates through all used mddevs in the system.
194 * We take care to grab the all_mddevs_lock whenever navigating
195 * the list, and to always hold a refcount when unlocked.
196 * Any code which breaks out of this loop while own
197 * a reference to the current mddev and must mddev_put it.
199 #define for_each_mddev(mddev,tmp) \
201 for (({ spin_lock(&all_mddevs_lock); \
202 tmp = all_mddevs.next; \
203 mddev = NULL;}); \
204 ({ if (tmp != &all_mddevs) \
205 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
206 spin_unlock(&all_mddevs_lock); \
207 if (mddev) mddev_put(mddev); \
208 mddev = list_entry(tmp, mddev_t, all_mddevs); \
209 tmp != &all_mddevs;}); \
210 ({ spin_lock(&all_mddevs_lock); \
211 tmp = tmp->next;}) \
215 static int md_fail_request (struct request_queue *q, struct bio *bio)
217 bio_io_error(bio);
218 return 0;
221 static inline mddev_t *mddev_get(mddev_t *mddev)
223 atomic_inc(&mddev->active);
224 return mddev;
227 static void mddev_put(mddev_t *mddev)
229 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
230 return;
231 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
232 list_del(&mddev->all_mddevs);
233 spin_unlock(&all_mddevs_lock);
234 blk_cleanup_queue(mddev->queue);
235 kobject_put(&mddev->kobj);
236 } else
237 spin_unlock(&all_mddevs_lock);
240 static mddev_t * mddev_find(dev_t unit)
242 mddev_t *mddev, *new = NULL;
244 retry:
245 spin_lock(&all_mddevs_lock);
246 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
247 if (mddev->unit == unit) {
248 mddev_get(mddev);
249 spin_unlock(&all_mddevs_lock);
250 kfree(new);
251 return mddev;
254 if (new) {
255 list_add(&new->all_mddevs, &all_mddevs);
256 spin_unlock(&all_mddevs_lock);
257 return new;
259 spin_unlock(&all_mddevs_lock);
261 new = kzalloc(sizeof(*new), GFP_KERNEL);
262 if (!new)
263 return NULL;
265 new->unit = unit;
266 if (MAJOR(unit) == MD_MAJOR)
267 new->md_minor = MINOR(unit);
268 else
269 new->md_minor = MINOR(unit) >> MdpMinorShift;
271 mutex_init(&new->reconfig_mutex);
272 INIT_LIST_HEAD(&new->disks);
273 INIT_LIST_HEAD(&new->all_mddevs);
274 init_timer(&new->safemode_timer);
275 atomic_set(&new->active, 1);
276 atomic_set(&new->openers, 0);
277 spin_lock_init(&new->write_lock);
278 init_waitqueue_head(&new->sb_wait);
279 init_waitqueue_head(&new->recovery_wait);
280 new->reshape_position = MaxSector;
281 new->resync_min = 0;
282 new->resync_max = MaxSector;
283 new->level = LEVEL_NONE;
285 new->queue = blk_alloc_queue(GFP_KERNEL);
286 if (!new->queue) {
287 kfree(new);
288 return NULL;
290 /* Can be unlocked because the queue is new: no concurrency */
291 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, new->queue);
293 blk_queue_make_request(new->queue, md_fail_request);
295 goto retry;
298 static inline int mddev_lock(mddev_t * mddev)
300 return mutex_lock_interruptible(&mddev->reconfig_mutex);
303 static inline int mddev_trylock(mddev_t * mddev)
305 return mutex_trylock(&mddev->reconfig_mutex);
308 static inline void mddev_unlock(mddev_t * mddev)
310 mutex_unlock(&mddev->reconfig_mutex);
312 md_wakeup_thread(mddev->thread);
315 static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
317 mdk_rdev_t * rdev;
318 struct list_head *tmp;
320 rdev_for_each(rdev, tmp, mddev) {
321 if (rdev->desc_nr == nr)
322 return rdev;
324 return NULL;
327 static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
329 struct list_head *tmp;
330 mdk_rdev_t *rdev;
332 rdev_for_each(rdev, tmp, mddev) {
333 if (rdev->bdev->bd_dev == dev)
334 return rdev;
336 return NULL;
339 static struct mdk_personality *find_pers(int level, char *clevel)
341 struct mdk_personality *pers;
342 list_for_each_entry(pers, &pers_list, list) {
343 if (level != LEVEL_NONE && pers->level == level)
344 return pers;
345 if (strcmp(pers->name, clevel)==0)
346 return pers;
348 return NULL;
351 /* return the offset of the super block in 512byte sectors */
352 static inline sector_t calc_dev_sboffset(struct block_device *bdev)
354 sector_t num_sectors = bdev->bd_inode->i_size / 512;
355 return MD_NEW_SIZE_SECTORS(num_sectors);
358 static sector_t calc_num_sectors(mdk_rdev_t *rdev, unsigned chunk_size)
360 sector_t num_sectors = rdev->sb_start;
362 if (chunk_size)
363 num_sectors &= ~((sector_t)chunk_size/512 - 1);
364 return num_sectors;
367 static int alloc_disk_sb(mdk_rdev_t * rdev)
369 if (rdev->sb_page)
370 MD_BUG();
372 rdev->sb_page = alloc_page(GFP_KERNEL);
373 if (!rdev->sb_page) {
374 printk(KERN_ALERT "md: out of memory.\n");
375 return -ENOMEM;
378 return 0;
381 static void free_disk_sb(mdk_rdev_t * rdev)
383 if (rdev->sb_page) {
384 put_page(rdev->sb_page);
385 rdev->sb_loaded = 0;
386 rdev->sb_page = NULL;
387 rdev->sb_start = 0;
388 rdev->size = 0;
393 static void super_written(struct bio *bio, int error)
395 mdk_rdev_t *rdev = bio->bi_private;
396 mddev_t *mddev = rdev->mddev;
398 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
399 printk("md: super_written gets error=%d, uptodate=%d\n",
400 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
401 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
402 md_error(mddev, rdev);
405 if (atomic_dec_and_test(&mddev->pending_writes))
406 wake_up(&mddev->sb_wait);
407 bio_put(bio);
410 static void super_written_barrier(struct bio *bio, int error)
412 struct bio *bio2 = bio->bi_private;
413 mdk_rdev_t *rdev = bio2->bi_private;
414 mddev_t *mddev = rdev->mddev;
416 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
417 error == -EOPNOTSUPP) {
418 unsigned long flags;
419 /* barriers don't appear to be supported :-( */
420 set_bit(BarriersNotsupp, &rdev->flags);
421 mddev->barriers_work = 0;
422 spin_lock_irqsave(&mddev->write_lock, flags);
423 bio2->bi_next = mddev->biolist;
424 mddev->biolist = bio2;
425 spin_unlock_irqrestore(&mddev->write_lock, flags);
426 wake_up(&mddev->sb_wait);
427 bio_put(bio);
428 } else {
429 bio_put(bio2);
430 bio->bi_private = rdev;
431 super_written(bio, error);
435 void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
436 sector_t sector, int size, struct page *page)
438 /* write first size bytes of page to sector of rdev
439 * Increment mddev->pending_writes before returning
440 * and decrement it on completion, waking up sb_wait
441 * if zero is reached.
442 * If an error occurred, call md_error
444 * As we might need to resubmit the request if BIO_RW_BARRIER
445 * causes ENOTSUPP, we allocate a spare bio...
447 struct bio *bio = bio_alloc(GFP_NOIO, 1);
448 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
450 bio->bi_bdev = rdev->bdev;
451 bio->bi_sector = sector;
452 bio_add_page(bio, page, size, 0);
453 bio->bi_private = rdev;
454 bio->bi_end_io = super_written;
455 bio->bi_rw = rw;
457 atomic_inc(&mddev->pending_writes);
458 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
459 struct bio *rbio;
460 rw |= (1<<BIO_RW_BARRIER);
461 rbio = bio_clone(bio, GFP_NOIO);
462 rbio->bi_private = bio;
463 rbio->bi_end_io = super_written_barrier;
464 submit_bio(rw, rbio);
465 } else
466 submit_bio(rw, bio);
469 void md_super_wait(mddev_t *mddev)
471 /* wait for all superblock writes that were scheduled to complete.
472 * if any had to be retried (due to BARRIER problems), retry them
474 DEFINE_WAIT(wq);
475 for(;;) {
476 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
477 if (atomic_read(&mddev->pending_writes)==0)
478 break;
479 while (mddev->biolist) {
480 struct bio *bio;
481 spin_lock_irq(&mddev->write_lock);
482 bio = mddev->biolist;
483 mddev->biolist = bio->bi_next ;
484 bio->bi_next = NULL;
485 spin_unlock_irq(&mddev->write_lock);
486 submit_bio(bio->bi_rw, bio);
488 schedule();
490 finish_wait(&mddev->sb_wait, &wq);
493 static void bi_complete(struct bio *bio, int error)
495 complete((struct completion*)bio->bi_private);
498 int sync_page_io(struct block_device *bdev, sector_t sector, int size,
499 struct page *page, int rw)
501 struct bio *bio = bio_alloc(GFP_NOIO, 1);
502 struct completion event;
503 int ret;
505 rw |= (1 << BIO_RW_SYNC);
507 bio->bi_bdev = bdev;
508 bio->bi_sector = sector;
509 bio_add_page(bio, page, size, 0);
510 init_completion(&event);
511 bio->bi_private = &event;
512 bio->bi_end_io = bi_complete;
513 submit_bio(rw, bio);
514 wait_for_completion(&event);
516 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
517 bio_put(bio);
518 return ret;
520 EXPORT_SYMBOL_GPL(sync_page_io);
522 static int read_disk_sb(mdk_rdev_t * rdev, int size)
524 char b[BDEVNAME_SIZE];
525 if (!rdev->sb_page) {
526 MD_BUG();
527 return -EINVAL;
529 if (rdev->sb_loaded)
530 return 0;
533 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
534 goto fail;
535 rdev->sb_loaded = 1;
536 return 0;
538 fail:
539 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
540 bdevname(rdev->bdev,b));
541 return -EINVAL;
544 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
546 return sb1->set_uuid0 == sb2->set_uuid0 &&
547 sb1->set_uuid1 == sb2->set_uuid1 &&
548 sb1->set_uuid2 == sb2->set_uuid2 &&
549 sb1->set_uuid3 == sb2->set_uuid3;
552 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
554 int ret;
555 mdp_super_t *tmp1, *tmp2;
557 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
558 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
560 if (!tmp1 || !tmp2) {
561 ret = 0;
562 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
563 goto abort;
566 *tmp1 = *sb1;
567 *tmp2 = *sb2;
570 * nr_disks is not constant
572 tmp1->nr_disks = 0;
573 tmp2->nr_disks = 0;
575 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
576 abort:
577 kfree(tmp1);
578 kfree(tmp2);
579 return ret;
583 static u32 md_csum_fold(u32 csum)
585 csum = (csum & 0xffff) + (csum >> 16);
586 return (csum & 0xffff) + (csum >> 16);
589 static unsigned int calc_sb_csum(mdp_super_t * sb)
591 u64 newcsum = 0;
592 u32 *sb32 = (u32*)sb;
593 int i;
594 unsigned int disk_csum, csum;
596 disk_csum = sb->sb_csum;
597 sb->sb_csum = 0;
599 for (i = 0; i < MD_SB_BYTES/4 ; i++)
600 newcsum += sb32[i];
601 csum = (newcsum & 0xffffffff) + (newcsum>>32);
604 #ifdef CONFIG_ALPHA
605 /* This used to use csum_partial, which was wrong for several
606 * reasons including that different results are returned on
607 * different architectures. It isn't critical that we get exactly
608 * the same return value as before (we always csum_fold before
609 * testing, and that removes any differences). However as we
610 * know that csum_partial always returned a 16bit value on
611 * alphas, do a fold to maximise conformity to previous behaviour.
613 sb->sb_csum = md_csum_fold(disk_csum);
614 #else
615 sb->sb_csum = disk_csum;
616 #endif
617 return csum;
622 * Handle superblock details.
623 * We want to be able to handle multiple superblock formats
624 * so we have a common interface to them all, and an array of
625 * different handlers.
626 * We rely on user-space to write the initial superblock, and support
627 * reading and updating of superblocks.
628 * Interface methods are:
629 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
630 * loads and validates a superblock on dev.
631 * if refdev != NULL, compare superblocks on both devices
632 * Return:
633 * 0 - dev has a superblock that is compatible with refdev
634 * 1 - dev has a superblock that is compatible and newer than refdev
635 * so dev should be used as the refdev in future
636 * -EINVAL superblock incompatible or invalid
637 * -othererror e.g. -EIO
639 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
640 * Verify that dev is acceptable into mddev.
641 * The first time, mddev->raid_disks will be 0, and data from
642 * dev should be merged in. Subsequent calls check that dev
643 * is new enough. Return 0 or -EINVAL
645 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
646 * Update the superblock for rdev with data in mddev
647 * This does not write to disc.
651 struct super_type {
652 char *name;
653 struct module *owner;
654 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
655 int minor_version);
656 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
657 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
658 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
659 sector_t num_sectors);
663 * load_super for 0.90.0
665 static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
667 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
668 mdp_super_t *sb;
669 int ret;
672 * Calculate the position of the superblock (512byte sectors),
673 * it's at the end of the disk.
675 * It also happens to be a multiple of 4Kb.
677 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
679 ret = read_disk_sb(rdev, MD_SB_BYTES);
680 if (ret) return ret;
682 ret = -EINVAL;
684 bdevname(rdev->bdev, b);
685 sb = (mdp_super_t*)page_address(rdev->sb_page);
687 if (sb->md_magic != MD_SB_MAGIC) {
688 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
690 goto abort;
693 if (sb->major_version != 0 ||
694 sb->minor_version < 90 ||
695 sb->minor_version > 91) {
696 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
697 sb->major_version, sb->minor_version,
699 goto abort;
702 if (sb->raid_disks <= 0)
703 goto abort;
705 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
706 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
708 goto abort;
711 rdev->preferred_minor = sb->md_minor;
712 rdev->data_offset = 0;
713 rdev->sb_size = MD_SB_BYTES;
715 if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
716 if (sb->level != 1 && sb->level != 4
717 && sb->level != 5 && sb->level != 6
718 && sb->level != 10) {
719 /* FIXME use a better test */
720 printk(KERN_WARNING
721 "md: bitmaps not supported for this level.\n");
722 goto abort;
726 if (sb->level == LEVEL_MULTIPATH)
727 rdev->desc_nr = -1;
728 else
729 rdev->desc_nr = sb->this_disk.number;
731 if (!refdev) {
732 ret = 1;
733 } else {
734 __u64 ev1, ev2;
735 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
736 if (!uuid_equal(refsb, sb)) {
737 printk(KERN_WARNING "md: %s has different UUID to %s\n",
738 b, bdevname(refdev->bdev,b2));
739 goto abort;
741 if (!sb_equal(refsb, sb)) {
742 printk(KERN_WARNING "md: %s has same UUID"
743 " but different superblock to %s\n",
744 b, bdevname(refdev->bdev, b2));
745 goto abort;
747 ev1 = md_event(sb);
748 ev2 = md_event(refsb);
749 if (ev1 > ev2)
750 ret = 1;
751 else
752 ret = 0;
754 rdev->size = calc_num_sectors(rdev, sb->chunk_size) / 2;
756 if (rdev->size < sb->size && sb->level > 1)
757 /* "this cannot possibly happen" ... */
758 ret = -EINVAL;
760 abort:
761 return ret;
765 * validate_super for 0.90.0
767 static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
769 mdp_disk_t *desc;
770 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
771 __u64 ev1 = md_event(sb);
773 rdev->raid_disk = -1;
774 clear_bit(Faulty, &rdev->flags);
775 clear_bit(In_sync, &rdev->flags);
776 clear_bit(WriteMostly, &rdev->flags);
777 clear_bit(BarriersNotsupp, &rdev->flags);
779 if (mddev->raid_disks == 0) {
780 mddev->major_version = 0;
781 mddev->minor_version = sb->minor_version;
782 mddev->patch_version = sb->patch_version;
783 mddev->external = 0;
784 mddev->chunk_size = sb->chunk_size;
785 mddev->ctime = sb->ctime;
786 mddev->utime = sb->utime;
787 mddev->level = sb->level;
788 mddev->clevel[0] = 0;
789 mddev->layout = sb->layout;
790 mddev->raid_disks = sb->raid_disks;
791 mddev->size = sb->size;
792 mddev->events = ev1;
793 mddev->bitmap_offset = 0;
794 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
796 if (mddev->minor_version >= 91) {
797 mddev->reshape_position = sb->reshape_position;
798 mddev->delta_disks = sb->delta_disks;
799 mddev->new_level = sb->new_level;
800 mddev->new_layout = sb->new_layout;
801 mddev->new_chunk = sb->new_chunk;
802 } else {
803 mddev->reshape_position = MaxSector;
804 mddev->delta_disks = 0;
805 mddev->new_level = mddev->level;
806 mddev->new_layout = mddev->layout;
807 mddev->new_chunk = mddev->chunk_size;
810 if (sb->state & (1<<MD_SB_CLEAN))
811 mddev->recovery_cp = MaxSector;
812 else {
813 if (sb->events_hi == sb->cp_events_hi &&
814 sb->events_lo == sb->cp_events_lo) {
815 mddev->recovery_cp = sb->recovery_cp;
816 } else
817 mddev->recovery_cp = 0;
820 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
821 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
822 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
823 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
825 mddev->max_disks = MD_SB_DISKS;
827 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
828 mddev->bitmap_file == NULL)
829 mddev->bitmap_offset = mddev->default_bitmap_offset;
831 } else if (mddev->pers == NULL) {
832 /* Insist on good event counter while assembling */
833 ++ev1;
834 if (ev1 < mddev->events)
835 return -EINVAL;
836 } else if (mddev->bitmap) {
837 /* if adding to array with a bitmap, then we can accept an
838 * older device ... but not too old.
840 if (ev1 < mddev->bitmap->events_cleared)
841 return 0;
842 } else {
843 if (ev1 < mddev->events)
844 /* just a hot-add of a new device, leave raid_disk at -1 */
845 return 0;
848 if (mddev->level != LEVEL_MULTIPATH) {
849 desc = sb->disks + rdev->desc_nr;
851 if (desc->state & (1<<MD_DISK_FAULTY))
852 set_bit(Faulty, &rdev->flags);
853 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
854 desc->raid_disk < mddev->raid_disks */) {
855 set_bit(In_sync, &rdev->flags);
856 rdev->raid_disk = desc->raid_disk;
858 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
859 set_bit(WriteMostly, &rdev->flags);
860 } else /* MULTIPATH are always insync */
861 set_bit(In_sync, &rdev->flags);
862 return 0;
866 * sync_super for 0.90.0
868 static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
870 mdp_super_t *sb;
871 struct list_head *tmp;
872 mdk_rdev_t *rdev2;
873 int next_spare = mddev->raid_disks;
876 /* make rdev->sb match mddev data..
878 * 1/ zero out disks
879 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
880 * 3/ any empty disks < next_spare become removed
882 * disks[0] gets initialised to REMOVED because
883 * we cannot be sure from other fields if it has
884 * been initialised or not.
886 int i;
887 int active=0, working=0,failed=0,spare=0,nr_disks=0;
889 rdev->sb_size = MD_SB_BYTES;
891 sb = (mdp_super_t*)page_address(rdev->sb_page);
893 memset(sb, 0, sizeof(*sb));
895 sb->md_magic = MD_SB_MAGIC;
896 sb->major_version = mddev->major_version;
897 sb->patch_version = mddev->patch_version;
898 sb->gvalid_words = 0; /* ignored */
899 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
900 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
901 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
902 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
904 sb->ctime = mddev->ctime;
905 sb->level = mddev->level;
906 sb->size = mddev->size;
907 sb->raid_disks = mddev->raid_disks;
908 sb->md_minor = mddev->md_minor;
909 sb->not_persistent = 0;
910 sb->utime = mddev->utime;
911 sb->state = 0;
912 sb->events_hi = (mddev->events>>32);
913 sb->events_lo = (u32)mddev->events;
915 if (mddev->reshape_position == MaxSector)
916 sb->minor_version = 90;
917 else {
918 sb->minor_version = 91;
919 sb->reshape_position = mddev->reshape_position;
920 sb->new_level = mddev->new_level;
921 sb->delta_disks = mddev->delta_disks;
922 sb->new_layout = mddev->new_layout;
923 sb->new_chunk = mddev->new_chunk;
925 mddev->minor_version = sb->minor_version;
926 if (mddev->in_sync)
928 sb->recovery_cp = mddev->recovery_cp;
929 sb->cp_events_hi = (mddev->events>>32);
930 sb->cp_events_lo = (u32)mddev->events;
931 if (mddev->recovery_cp == MaxSector)
932 sb->state = (1<< MD_SB_CLEAN);
933 } else
934 sb->recovery_cp = 0;
936 sb->layout = mddev->layout;
937 sb->chunk_size = mddev->chunk_size;
939 if (mddev->bitmap && mddev->bitmap_file == NULL)
940 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
942 sb->disks[0].state = (1<<MD_DISK_REMOVED);
943 rdev_for_each(rdev2, tmp, mddev) {
944 mdp_disk_t *d;
945 int desc_nr;
946 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
947 && !test_bit(Faulty, &rdev2->flags))
948 desc_nr = rdev2->raid_disk;
949 else
950 desc_nr = next_spare++;
951 rdev2->desc_nr = desc_nr;
952 d = &sb->disks[rdev2->desc_nr];
953 nr_disks++;
954 d->number = rdev2->desc_nr;
955 d->major = MAJOR(rdev2->bdev->bd_dev);
956 d->minor = MINOR(rdev2->bdev->bd_dev);
957 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
958 && !test_bit(Faulty, &rdev2->flags))
959 d->raid_disk = rdev2->raid_disk;
960 else
961 d->raid_disk = rdev2->desc_nr; /* compatibility */
962 if (test_bit(Faulty, &rdev2->flags))
963 d->state = (1<<MD_DISK_FAULTY);
964 else if (test_bit(In_sync, &rdev2->flags)) {
965 d->state = (1<<MD_DISK_ACTIVE);
966 d->state |= (1<<MD_DISK_SYNC);
967 active++;
968 working++;
969 } else {
970 d->state = 0;
971 spare++;
972 working++;
974 if (test_bit(WriteMostly, &rdev2->flags))
975 d->state |= (1<<MD_DISK_WRITEMOSTLY);
977 /* now set the "removed" and "faulty" bits on any missing devices */
978 for (i=0 ; i < mddev->raid_disks ; i++) {
979 mdp_disk_t *d = &sb->disks[i];
980 if (d->state == 0 && d->number == 0) {
981 d->number = i;
982 d->raid_disk = i;
983 d->state = (1<<MD_DISK_REMOVED);
984 d->state |= (1<<MD_DISK_FAULTY);
985 failed++;
988 sb->nr_disks = nr_disks;
989 sb->active_disks = active;
990 sb->working_disks = working;
991 sb->failed_disks = failed;
992 sb->spare_disks = spare;
994 sb->this_disk = sb->disks[rdev->desc_nr];
995 sb->sb_csum = calc_sb_csum(sb);
999 * rdev_size_change for 0.90.0
1001 static unsigned long long
1002 super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
1004 if (num_sectors && num_sectors < rdev->mddev->size * 2)
1005 return 0; /* component must fit device */
1006 if (rdev->mddev->bitmap_offset)
1007 return 0; /* can't move bitmap */
1008 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
1009 if (!num_sectors || num_sectors > rdev->sb_start)
1010 num_sectors = rdev->sb_start;
1011 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1012 rdev->sb_page);
1013 md_super_wait(rdev->mddev);
1014 return num_sectors / 2; /* kB for sysfs */
1019 * version 1 superblock
1022 static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
1024 __le32 disk_csum;
1025 u32 csum;
1026 unsigned long long newcsum;
1027 int size = 256 + le32_to_cpu(sb->max_dev)*2;
1028 __le32 *isuper = (__le32*)sb;
1029 int i;
1031 disk_csum = sb->sb_csum;
1032 sb->sb_csum = 0;
1033 newcsum = 0;
1034 for (i=0; size>=4; size -= 4 )
1035 newcsum += le32_to_cpu(*isuper++);
1037 if (size == 2)
1038 newcsum += le16_to_cpu(*(__le16*) isuper);
1040 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1041 sb->sb_csum = disk_csum;
1042 return cpu_to_le32(csum);
1045 static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1047 struct mdp_superblock_1 *sb;
1048 int ret;
1049 sector_t sb_start;
1050 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1051 int bmask;
1054 * Calculate the position of the superblock in 512byte sectors.
1055 * It is always aligned to a 4K boundary and
1056 * depeding on minor_version, it can be:
1057 * 0: At least 8K, but less than 12K, from end of device
1058 * 1: At start of device
1059 * 2: 4K from start of device.
1061 switch(minor_version) {
1062 case 0:
1063 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1064 sb_start -= 8*2;
1065 sb_start &= ~(sector_t)(4*2-1);
1066 break;
1067 case 1:
1068 sb_start = 0;
1069 break;
1070 case 2:
1071 sb_start = 8;
1072 break;
1073 default:
1074 return -EINVAL;
1076 rdev->sb_start = sb_start;
1078 /* superblock is rarely larger than 1K, but it can be larger,
1079 * and it is safe to read 4k, so we do that
1081 ret = read_disk_sb(rdev, 4096);
1082 if (ret) return ret;
1085 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1087 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1088 sb->major_version != cpu_to_le32(1) ||
1089 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1090 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1091 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1092 return -EINVAL;
1094 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1095 printk("md: invalid superblock checksum on %s\n",
1096 bdevname(rdev->bdev,b));
1097 return -EINVAL;
1099 if (le64_to_cpu(sb->data_size) < 10) {
1100 printk("md: data_size too small on %s\n",
1101 bdevname(rdev->bdev,b));
1102 return -EINVAL;
1104 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) {
1105 if (sb->level != cpu_to_le32(1) &&
1106 sb->level != cpu_to_le32(4) &&
1107 sb->level != cpu_to_le32(5) &&
1108 sb->level != cpu_to_le32(6) &&
1109 sb->level != cpu_to_le32(10)) {
1110 printk(KERN_WARNING
1111 "md: bitmaps not supported for this level.\n");
1112 return -EINVAL;
1116 rdev->preferred_minor = 0xffff;
1117 rdev->data_offset = le64_to_cpu(sb->data_offset);
1118 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1120 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1121 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
1122 if (rdev->sb_size & bmask)
1123 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1125 if (minor_version
1126 && rdev->data_offset < sb_start + (rdev->sb_size/512))
1127 return -EINVAL;
1129 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1130 rdev->desc_nr = -1;
1131 else
1132 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1134 if (!refdev) {
1135 ret = 1;
1136 } else {
1137 __u64 ev1, ev2;
1138 struct mdp_superblock_1 *refsb =
1139 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1141 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1142 sb->level != refsb->level ||
1143 sb->layout != refsb->layout ||
1144 sb->chunksize != refsb->chunksize) {
1145 printk(KERN_WARNING "md: %s has strangely different"
1146 " superblock to %s\n",
1147 bdevname(rdev->bdev,b),
1148 bdevname(refdev->bdev,b2));
1149 return -EINVAL;
1151 ev1 = le64_to_cpu(sb->events);
1152 ev2 = le64_to_cpu(refsb->events);
1154 if (ev1 > ev2)
1155 ret = 1;
1156 else
1157 ret = 0;
1159 if (minor_version)
1160 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1161 else
1162 rdev->size = rdev->sb_start / 2;
1163 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1164 return -EINVAL;
1165 rdev->size = le64_to_cpu(sb->data_size)/2;
1166 if (le32_to_cpu(sb->chunksize))
1167 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
1169 if (le64_to_cpu(sb->size) > rdev->size*2)
1170 return -EINVAL;
1171 return ret;
1174 static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1176 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1177 __u64 ev1 = le64_to_cpu(sb->events);
1179 rdev->raid_disk = -1;
1180 clear_bit(Faulty, &rdev->flags);
1181 clear_bit(In_sync, &rdev->flags);
1182 clear_bit(WriteMostly, &rdev->flags);
1183 clear_bit(BarriersNotsupp, &rdev->flags);
1185 if (mddev->raid_disks == 0) {
1186 mddev->major_version = 1;
1187 mddev->patch_version = 0;
1188 mddev->external = 0;
1189 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1190 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1191 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1192 mddev->level = le32_to_cpu(sb->level);
1193 mddev->clevel[0] = 0;
1194 mddev->layout = le32_to_cpu(sb->layout);
1195 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1196 mddev->size = le64_to_cpu(sb->size)/2;
1197 mddev->events = ev1;
1198 mddev->bitmap_offset = 0;
1199 mddev->default_bitmap_offset = 1024 >> 9;
1201 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1202 memcpy(mddev->uuid, sb->set_uuid, 16);
1204 mddev->max_disks = (4096-256)/2;
1206 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1207 mddev->bitmap_file == NULL )
1208 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1210 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1211 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1212 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1213 mddev->new_level = le32_to_cpu(sb->new_level);
1214 mddev->new_layout = le32_to_cpu(sb->new_layout);
1215 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1216 } else {
1217 mddev->reshape_position = MaxSector;
1218 mddev->delta_disks = 0;
1219 mddev->new_level = mddev->level;
1220 mddev->new_layout = mddev->layout;
1221 mddev->new_chunk = mddev->chunk_size;
1224 } else if (mddev->pers == NULL) {
1225 /* Insist of good event counter while assembling */
1226 ++ev1;
1227 if (ev1 < mddev->events)
1228 return -EINVAL;
1229 } else if (mddev->bitmap) {
1230 /* If adding to array with a bitmap, then we can accept an
1231 * older device, but not too old.
1233 if (ev1 < mddev->bitmap->events_cleared)
1234 return 0;
1235 } else {
1236 if (ev1 < mddev->events)
1237 /* just a hot-add of a new device, leave raid_disk at -1 */
1238 return 0;
1240 if (mddev->level != LEVEL_MULTIPATH) {
1241 int role;
1242 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1243 switch(role) {
1244 case 0xffff: /* spare */
1245 break;
1246 case 0xfffe: /* faulty */
1247 set_bit(Faulty, &rdev->flags);
1248 break;
1249 default:
1250 if ((le32_to_cpu(sb->feature_map) &
1251 MD_FEATURE_RECOVERY_OFFSET))
1252 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1253 else
1254 set_bit(In_sync, &rdev->flags);
1255 rdev->raid_disk = role;
1256 break;
1258 if (sb->devflags & WriteMostly1)
1259 set_bit(WriteMostly, &rdev->flags);
1260 } else /* MULTIPATH are always insync */
1261 set_bit(In_sync, &rdev->flags);
1263 return 0;
1266 static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1268 struct mdp_superblock_1 *sb;
1269 struct list_head *tmp;
1270 mdk_rdev_t *rdev2;
1271 int max_dev, i;
1272 /* make rdev->sb match mddev and rdev data. */
1274 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1276 sb->feature_map = 0;
1277 sb->pad0 = 0;
1278 sb->recovery_offset = cpu_to_le64(0);
1279 memset(sb->pad1, 0, sizeof(sb->pad1));
1280 memset(sb->pad2, 0, sizeof(sb->pad2));
1281 memset(sb->pad3, 0, sizeof(sb->pad3));
1283 sb->utime = cpu_to_le64((__u64)mddev->utime);
1284 sb->events = cpu_to_le64(mddev->events);
1285 if (mddev->in_sync)
1286 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1287 else
1288 sb->resync_offset = cpu_to_le64(0);
1290 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1292 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1293 sb->size = cpu_to_le64(mddev->size<<1);
1295 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1296 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
1297 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1300 if (rdev->raid_disk >= 0 &&
1301 !test_bit(In_sync, &rdev->flags) &&
1302 rdev->recovery_offset > 0) {
1303 sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1304 sb->recovery_offset = cpu_to_le64(rdev->recovery_offset);
1307 if (mddev->reshape_position != MaxSector) {
1308 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1309 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1310 sb->new_layout = cpu_to_le32(mddev->new_layout);
1311 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1312 sb->new_level = cpu_to_le32(mddev->new_level);
1313 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1316 max_dev = 0;
1317 rdev_for_each(rdev2, tmp, mddev)
1318 if (rdev2->desc_nr+1 > max_dev)
1319 max_dev = rdev2->desc_nr+1;
1321 if (max_dev > le32_to_cpu(sb->max_dev))
1322 sb->max_dev = cpu_to_le32(max_dev);
1323 for (i=0; i<max_dev;i++)
1324 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1326 rdev_for_each(rdev2, tmp, mddev) {
1327 i = rdev2->desc_nr;
1328 if (test_bit(Faulty, &rdev2->flags))
1329 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1330 else if (test_bit(In_sync, &rdev2->flags))
1331 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1332 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1333 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1334 else
1335 sb->dev_roles[i] = cpu_to_le16(0xffff);
1338 sb->sb_csum = calc_sb_1_csum(sb);
1341 static unsigned long long
1342 super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
1344 struct mdp_superblock_1 *sb;
1345 sector_t max_sectors;
1346 if (num_sectors && num_sectors < rdev->mddev->size * 2)
1347 return 0; /* component must fit device */
1348 if (rdev->sb_start < rdev->data_offset) {
1349 /* minor versions 1 and 2; superblock before data */
1350 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1351 max_sectors -= rdev->data_offset;
1352 if (!num_sectors || num_sectors > max_sectors)
1353 num_sectors = max_sectors;
1354 } else if (rdev->mddev->bitmap_offset) {
1355 /* minor version 0 with bitmap we can't move */
1356 return 0;
1357 } else {
1358 /* minor version 0; superblock after data */
1359 sector_t sb_start;
1360 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1361 sb_start &= ~(sector_t)(4*2 - 1);
1362 max_sectors = rdev->size * 2 + sb_start - rdev->sb_start;
1363 if (!num_sectors || num_sectors > max_sectors)
1364 num_sectors = max_sectors;
1365 rdev->sb_start = sb_start;
1367 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
1368 sb->data_size = cpu_to_le64(num_sectors);
1369 sb->super_offset = rdev->sb_start;
1370 sb->sb_csum = calc_sb_1_csum(sb);
1371 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1372 rdev->sb_page);
1373 md_super_wait(rdev->mddev);
1374 return num_sectors / 2; /* kB for sysfs */
1377 static struct super_type super_types[] = {
1378 [0] = {
1379 .name = "0.90.0",
1380 .owner = THIS_MODULE,
1381 .load_super = super_90_load,
1382 .validate_super = super_90_validate,
1383 .sync_super = super_90_sync,
1384 .rdev_size_change = super_90_rdev_size_change,
1386 [1] = {
1387 .name = "md-1",
1388 .owner = THIS_MODULE,
1389 .load_super = super_1_load,
1390 .validate_super = super_1_validate,
1391 .sync_super = super_1_sync,
1392 .rdev_size_change = super_1_rdev_size_change,
1396 static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1398 mdk_rdev_t *rdev, *rdev2;
1400 rcu_read_lock();
1401 rdev_for_each_rcu(rdev, mddev1)
1402 rdev_for_each_rcu(rdev2, mddev2)
1403 if (rdev->bdev->bd_contains ==
1404 rdev2->bdev->bd_contains) {
1405 rcu_read_unlock();
1406 return 1;
1408 rcu_read_unlock();
1409 return 0;
1412 static LIST_HEAD(pending_raid_disks);
1414 static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1416 char b[BDEVNAME_SIZE];
1417 struct kobject *ko;
1418 char *s;
1419 int err;
1421 if (rdev->mddev) {
1422 MD_BUG();
1423 return -EINVAL;
1426 /* prevent duplicates */
1427 if (find_rdev(mddev, rdev->bdev->bd_dev))
1428 return -EEXIST;
1430 /* make sure rdev->size exceeds mddev->size */
1431 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
1432 if (mddev->pers) {
1433 /* Cannot change size, so fail
1434 * If mddev->level <= 0, then we don't care
1435 * about aligning sizes (e.g. linear)
1437 if (mddev->level > 0)
1438 return -ENOSPC;
1439 } else
1440 mddev->size = rdev->size;
1443 /* Verify rdev->desc_nr is unique.
1444 * If it is -1, assign a free number, else
1445 * check number is not in use
1447 if (rdev->desc_nr < 0) {
1448 int choice = 0;
1449 if (mddev->pers) choice = mddev->raid_disks;
1450 while (find_rdev_nr(mddev, choice))
1451 choice++;
1452 rdev->desc_nr = choice;
1453 } else {
1454 if (find_rdev_nr(mddev, rdev->desc_nr))
1455 return -EBUSY;
1457 bdevname(rdev->bdev,b);
1458 while ( (s=strchr(b, '/')) != NULL)
1459 *s = '!';
1461 rdev->mddev = mddev;
1462 printk(KERN_INFO "md: bind<%s>\n", b);
1464 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
1465 goto fail;
1467 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
1468 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1469 kobject_del(&rdev->kobj);
1470 goto fail;
1472 list_add_rcu(&rdev->same_set, &mddev->disks);
1473 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
1474 return 0;
1476 fail:
1477 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1478 b, mdname(mddev));
1479 return err;
1482 static void md_delayed_delete(struct work_struct *ws)
1484 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1485 kobject_del(&rdev->kobj);
1486 kobject_put(&rdev->kobj);
1489 static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1491 char b[BDEVNAME_SIZE];
1492 if (!rdev->mddev) {
1493 MD_BUG();
1494 return;
1496 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
1497 list_del_rcu(&rdev->same_set);
1498 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1499 rdev->mddev = NULL;
1500 sysfs_remove_link(&rdev->kobj, "block");
1502 /* We need to delay this, otherwise we can deadlock when
1503 * writing to 'remove' to "dev/state". We also need
1504 * to delay it due to rcu usage.
1506 synchronize_rcu();
1507 INIT_WORK(&rdev->del_work, md_delayed_delete);
1508 kobject_get(&rdev->kobj);
1509 schedule_work(&rdev->del_work);
1513 * prevent the device from being mounted, repartitioned or
1514 * otherwise reused by a RAID array (or any other kernel
1515 * subsystem), by bd_claiming the device.
1517 static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
1519 int err = 0;
1520 struct block_device *bdev;
1521 char b[BDEVNAME_SIZE];
1523 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1524 if (IS_ERR(bdev)) {
1525 printk(KERN_ERR "md: could not open %s.\n",
1526 __bdevname(dev, b));
1527 return PTR_ERR(bdev);
1529 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
1530 if (err) {
1531 printk(KERN_ERR "md: could not bd_claim %s.\n",
1532 bdevname(bdev, b));
1533 blkdev_put(bdev);
1534 return err;
1536 if (!shared)
1537 set_bit(AllReserved, &rdev->flags);
1538 rdev->bdev = bdev;
1539 return err;
1542 static void unlock_rdev(mdk_rdev_t *rdev)
1544 struct block_device *bdev = rdev->bdev;
1545 rdev->bdev = NULL;
1546 if (!bdev)
1547 MD_BUG();
1548 bd_release(bdev);
1549 blkdev_put(bdev);
1552 void md_autodetect_dev(dev_t dev);
1554 static void export_rdev(mdk_rdev_t * rdev)
1556 char b[BDEVNAME_SIZE];
1557 printk(KERN_INFO "md: export_rdev(%s)\n",
1558 bdevname(rdev->bdev,b));
1559 if (rdev->mddev)
1560 MD_BUG();
1561 free_disk_sb(rdev);
1562 #ifndef MODULE
1563 if (test_bit(AutoDetected, &rdev->flags))
1564 md_autodetect_dev(rdev->bdev->bd_dev);
1565 #endif
1566 unlock_rdev(rdev);
1567 kobject_put(&rdev->kobj);
1570 static void kick_rdev_from_array(mdk_rdev_t * rdev)
1572 unbind_rdev_from_array(rdev);
1573 export_rdev(rdev);
1576 static void export_array(mddev_t *mddev)
1578 struct list_head *tmp;
1579 mdk_rdev_t *rdev;
1581 rdev_for_each(rdev, tmp, mddev) {
1582 if (!rdev->mddev) {
1583 MD_BUG();
1584 continue;
1586 kick_rdev_from_array(rdev);
1588 if (!list_empty(&mddev->disks))
1589 MD_BUG();
1590 mddev->raid_disks = 0;
1591 mddev->major_version = 0;
1594 static void print_desc(mdp_disk_t *desc)
1596 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1597 desc->major,desc->minor,desc->raid_disk,desc->state);
1600 static void print_sb(mdp_super_t *sb)
1602 int i;
1604 printk(KERN_INFO
1605 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1606 sb->major_version, sb->minor_version, sb->patch_version,
1607 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1608 sb->ctime);
1609 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1610 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1611 sb->md_minor, sb->layout, sb->chunk_size);
1612 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1613 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1614 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1615 sb->failed_disks, sb->spare_disks,
1616 sb->sb_csum, (unsigned long)sb->events_lo);
1618 printk(KERN_INFO);
1619 for (i = 0; i < MD_SB_DISKS; i++) {
1620 mdp_disk_t *desc;
1622 desc = sb->disks + i;
1623 if (desc->number || desc->major || desc->minor ||
1624 desc->raid_disk || (desc->state && (desc->state != 4))) {
1625 printk(" D %2d: ", i);
1626 print_desc(desc);
1629 printk(KERN_INFO "md: THIS: ");
1630 print_desc(&sb->this_disk);
1634 static void print_rdev(mdk_rdev_t *rdev)
1636 char b[BDEVNAME_SIZE];
1637 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1638 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
1639 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1640 rdev->desc_nr);
1641 if (rdev->sb_loaded) {
1642 printk(KERN_INFO "md: rdev superblock:\n");
1643 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1644 } else
1645 printk(KERN_INFO "md: no rdev superblock!\n");
1648 static void md_print_devices(void)
1650 struct list_head *tmp, *tmp2;
1651 mdk_rdev_t *rdev;
1652 mddev_t *mddev;
1653 char b[BDEVNAME_SIZE];
1655 printk("\n");
1656 printk("md: **********************************\n");
1657 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1658 printk("md: **********************************\n");
1659 for_each_mddev(mddev, tmp) {
1661 if (mddev->bitmap)
1662 bitmap_print_sb(mddev->bitmap);
1663 else
1664 printk("%s: ", mdname(mddev));
1665 rdev_for_each(rdev, tmp2, mddev)
1666 printk("<%s>", bdevname(rdev->bdev,b));
1667 printk("\n");
1669 rdev_for_each(rdev, tmp2, mddev)
1670 print_rdev(rdev);
1672 printk("md: **********************************\n");
1673 printk("\n");
1677 static void sync_sbs(mddev_t * mddev, int nospares)
1679 /* Update each superblock (in-memory image), but
1680 * if we are allowed to, skip spares which already
1681 * have the right event counter, or have one earlier
1682 * (which would mean they aren't being marked as dirty
1683 * with the rest of the array)
1685 mdk_rdev_t *rdev;
1686 struct list_head *tmp;
1688 rdev_for_each(rdev, tmp, mddev) {
1689 if (rdev->sb_events == mddev->events ||
1690 (nospares &&
1691 rdev->raid_disk < 0 &&
1692 (rdev->sb_events&1)==0 &&
1693 rdev->sb_events+1 == mddev->events)) {
1694 /* Don't update this superblock */
1695 rdev->sb_loaded = 2;
1696 } else {
1697 super_types[mddev->major_version].
1698 sync_super(mddev, rdev);
1699 rdev->sb_loaded = 1;
1704 static void md_update_sb(mddev_t * mddev, int force_change)
1706 struct list_head *tmp;
1707 mdk_rdev_t *rdev;
1708 int sync_req;
1709 int nospares = 0;
1711 if (mddev->external)
1712 return;
1713 repeat:
1714 spin_lock_irq(&mddev->write_lock);
1716 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1717 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1718 force_change = 1;
1719 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1720 /* just a clean<-> dirty transition, possibly leave spares alone,
1721 * though if events isn't the right even/odd, we will have to do
1722 * spares after all
1724 nospares = 1;
1725 if (force_change)
1726 nospares = 0;
1727 if (mddev->degraded)
1728 /* If the array is degraded, then skipping spares is both
1729 * dangerous and fairly pointless.
1730 * Dangerous because a device that was removed from the array
1731 * might have a event_count that still looks up-to-date,
1732 * so it can be re-added without a resync.
1733 * Pointless because if there are any spares to skip,
1734 * then a recovery will happen and soon that array won't
1735 * be degraded any more and the spare can go back to sleep then.
1737 nospares = 0;
1739 sync_req = mddev->in_sync;
1740 mddev->utime = get_seconds();
1742 /* If this is just a dirty<->clean transition, and the array is clean
1743 * and 'events' is odd, we can roll back to the previous clean state */
1744 if (nospares
1745 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
1746 && (mddev->events & 1)
1747 && mddev->events != 1)
1748 mddev->events--;
1749 else {
1750 /* otherwise we have to go forward and ... */
1751 mddev->events ++;
1752 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1753 /* .. if the array isn't clean, insist on an odd 'events' */
1754 if ((mddev->events&1)==0) {
1755 mddev->events++;
1756 nospares = 0;
1758 } else {
1759 /* otherwise insist on an even 'events' (for clean states) */
1760 if ((mddev->events&1)) {
1761 mddev->events++;
1762 nospares = 0;
1767 if (!mddev->events) {
1769 * oops, this 64-bit counter should never wrap.
1770 * Either we are in around ~1 trillion A.C., assuming
1771 * 1 reboot per second, or we have a bug:
1773 MD_BUG();
1774 mddev->events --;
1778 * do not write anything to disk if using
1779 * nonpersistent superblocks
1781 if (!mddev->persistent) {
1782 if (!mddev->external)
1783 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1785 spin_unlock_irq(&mddev->write_lock);
1786 wake_up(&mddev->sb_wait);
1787 return;
1789 sync_sbs(mddev, nospares);
1790 spin_unlock_irq(&mddev->write_lock);
1792 dprintk(KERN_INFO
1793 "md: updating %s RAID superblock on device (in sync %d)\n",
1794 mdname(mddev),mddev->in_sync);
1796 bitmap_update_sb(mddev->bitmap);
1797 rdev_for_each(rdev, tmp, mddev) {
1798 char b[BDEVNAME_SIZE];
1799 dprintk(KERN_INFO "md: ");
1800 if (rdev->sb_loaded != 1)
1801 continue; /* no noise on spare devices */
1802 if (test_bit(Faulty, &rdev->flags))
1803 dprintk("(skipping faulty ");
1805 dprintk("%s ", bdevname(rdev->bdev,b));
1806 if (!test_bit(Faulty, &rdev->flags)) {
1807 md_super_write(mddev,rdev,
1808 rdev->sb_start, rdev->sb_size,
1809 rdev->sb_page);
1810 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1811 bdevname(rdev->bdev,b),
1812 (unsigned long long)rdev->sb_start);
1813 rdev->sb_events = mddev->events;
1815 } else
1816 dprintk(")\n");
1817 if (mddev->level == LEVEL_MULTIPATH)
1818 /* only need to write one superblock... */
1819 break;
1821 md_super_wait(mddev);
1822 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
1824 spin_lock_irq(&mddev->write_lock);
1825 if (mddev->in_sync != sync_req ||
1826 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
1827 /* have to write it out again */
1828 spin_unlock_irq(&mddev->write_lock);
1829 goto repeat;
1831 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1832 spin_unlock_irq(&mddev->write_lock);
1833 wake_up(&mddev->sb_wait);
1837 /* words written to sysfs files may, or may not, be \n terminated.
1838 * We want to accept with case. For this we use cmd_match.
1840 static int cmd_match(const char *cmd, const char *str)
1842 /* See if cmd, written into a sysfs file, matches
1843 * str. They must either be the same, or cmd can
1844 * have a trailing newline
1846 while (*cmd && *str && *cmd == *str) {
1847 cmd++;
1848 str++;
1850 if (*cmd == '\n')
1851 cmd++;
1852 if (*str || *cmd)
1853 return 0;
1854 return 1;
1857 struct rdev_sysfs_entry {
1858 struct attribute attr;
1859 ssize_t (*show)(mdk_rdev_t *, char *);
1860 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1863 static ssize_t
1864 state_show(mdk_rdev_t *rdev, char *page)
1866 char *sep = "";
1867 size_t len = 0;
1869 if (test_bit(Faulty, &rdev->flags)) {
1870 len+= sprintf(page+len, "%sfaulty",sep);
1871 sep = ",";
1873 if (test_bit(In_sync, &rdev->flags)) {
1874 len += sprintf(page+len, "%sin_sync",sep);
1875 sep = ",";
1877 if (test_bit(WriteMostly, &rdev->flags)) {
1878 len += sprintf(page+len, "%swrite_mostly",sep);
1879 sep = ",";
1881 if (test_bit(Blocked, &rdev->flags)) {
1882 len += sprintf(page+len, "%sblocked", sep);
1883 sep = ",";
1885 if (!test_bit(Faulty, &rdev->flags) &&
1886 !test_bit(In_sync, &rdev->flags)) {
1887 len += sprintf(page+len, "%sspare", sep);
1888 sep = ",";
1890 return len+sprintf(page+len, "\n");
1893 static ssize_t
1894 state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1896 /* can write
1897 * faulty - simulates and error
1898 * remove - disconnects the device
1899 * writemostly - sets write_mostly
1900 * -writemostly - clears write_mostly
1901 * blocked - sets the Blocked flag
1902 * -blocked - clears the Blocked flag
1904 int err = -EINVAL;
1905 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
1906 md_error(rdev->mddev, rdev);
1907 err = 0;
1908 } else if (cmd_match(buf, "remove")) {
1909 if (rdev->raid_disk >= 0)
1910 err = -EBUSY;
1911 else {
1912 mddev_t *mddev = rdev->mddev;
1913 kick_rdev_from_array(rdev);
1914 if (mddev->pers)
1915 md_update_sb(mddev, 1);
1916 md_new_event(mddev);
1917 err = 0;
1919 } else if (cmd_match(buf, "writemostly")) {
1920 set_bit(WriteMostly, &rdev->flags);
1921 err = 0;
1922 } else if (cmd_match(buf, "-writemostly")) {
1923 clear_bit(WriteMostly, &rdev->flags);
1924 err = 0;
1925 } else if (cmd_match(buf, "blocked")) {
1926 set_bit(Blocked, &rdev->flags);
1927 err = 0;
1928 } else if (cmd_match(buf, "-blocked")) {
1929 clear_bit(Blocked, &rdev->flags);
1930 wake_up(&rdev->blocked_wait);
1931 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
1932 md_wakeup_thread(rdev->mddev->thread);
1934 err = 0;
1936 if (!err)
1937 sysfs_notify(&rdev->kobj, NULL, "state");
1938 return err ? err : len;
1940 static struct rdev_sysfs_entry rdev_state =
1941 __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
1943 static ssize_t
1944 errors_show(mdk_rdev_t *rdev, char *page)
1946 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1949 static ssize_t
1950 errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1952 char *e;
1953 unsigned long n = simple_strtoul(buf, &e, 10);
1954 if (*buf && (*e == 0 || *e == '\n')) {
1955 atomic_set(&rdev->corrected_errors, n);
1956 return len;
1958 return -EINVAL;
1960 static struct rdev_sysfs_entry rdev_errors =
1961 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
1963 static ssize_t
1964 slot_show(mdk_rdev_t *rdev, char *page)
1966 if (rdev->raid_disk < 0)
1967 return sprintf(page, "none\n");
1968 else
1969 return sprintf(page, "%d\n", rdev->raid_disk);
1972 static ssize_t
1973 slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1975 char *e;
1976 int err;
1977 char nm[20];
1978 int slot = simple_strtoul(buf, &e, 10);
1979 if (strncmp(buf, "none", 4)==0)
1980 slot = -1;
1981 else if (e==buf || (*e && *e!= '\n'))
1982 return -EINVAL;
1983 if (rdev->mddev->pers && slot == -1) {
1984 /* Setting 'slot' on an active array requires also
1985 * updating the 'rd%d' link, and communicating
1986 * with the personality with ->hot_*_disk.
1987 * For now we only support removing
1988 * failed/spare devices. This normally happens automatically,
1989 * but not when the metadata is externally managed.
1991 if (rdev->raid_disk == -1)
1992 return -EEXIST;
1993 /* personality does all needed checks */
1994 if (rdev->mddev->pers->hot_add_disk == NULL)
1995 return -EINVAL;
1996 err = rdev->mddev->pers->
1997 hot_remove_disk(rdev->mddev, rdev->raid_disk);
1998 if (err)
1999 return err;
2000 sprintf(nm, "rd%d", rdev->raid_disk);
2001 sysfs_remove_link(&rdev->mddev->kobj, nm);
2002 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2003 md_wakeup_thread(rdev->mddev->thread);
2004 } else if (rdev->mddev->pers) {
2005 mdk_rdev_t *rdev2;
2006 struct list_head *tmp;
2007 /* Activating a spare .. or possibly reactivating
2008 * if we every get bitmaps working here.
2011 if (rdev->raid_disk != -1)
2012 return -EBUSY;
2014 if (rdev->mddev->pers->hot_add_disk == NULL)
2015 return -EINVAL;
2017 rdev_for_each(rdev2, tmp, rdev->mddev)
2018 if (rdev2->raid_disk == slot)
2019 return -EEXIST;
2021 rdev->raid_disk = slot;
2022 if (test_bit(In_sync, &rdev->flags))
2023 rdev->saved_raid_disk = slot;
2024 else
2025 rdev->saved_raid_disk = -1;
2026 err = rdev->mddev->pers->
2027 hot_add_disk(rdev->mddev, rdev);
2028 if (err) {
2029 rdev->raid_disk = -1;
2030 return err;
2031 } else
2032 sysfs_notify(&rdev->kobj, NULL, "state");
2033 sprintf(nm, "rd%d", rdev->raid_disk);
2034 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2035 printk(KERN_WARNING
2036 "md: cannot register "
2037 "%s for %s\n",
2038 nm, mdname(rdev->mddev));
2040 /* don't wakeup anyone, leave that to userspace. */
2041 } else {
2042 if (slot >= rdev->mddev->raid_disks)
2043 return -ENOSPC;
2044 rdev->raid_disk = slot;
2045 /* assume it is working */
2046 clear_bit(Faulty, &rdev->flags);
2047 clear_bit(WriteMostly, &rdev->flags);
2048 set_bit(In_sync, &rdev->flags);
2049 sysfs_notify(&rdev->kobj, NULL, "state");
2051 return len;
2055 static struct rdev_sysfs_entry rdev_slot =
2056 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
2058 static ssize_t
2059 offset_show(mdk_rdev_t *rdev, char *page)
2061 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
2064 static ssize_t
2065 offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2067 char *e;
2068 unsigned long long offset = simple_strtoull(buf, &e, 10);
2069 if (e==buf || (*e && *e != '\n'))
2070 return -EINVAL;
2071 if (rdev->mddev->pers && rdev->raid_disk >= 0)
2072 return -EBUSY;
2073 if (rdev->size && rdev->mddev->external)
2074 /* Must set offset before size, so overlap checks
2075 * can be sane */
2076 return -EBUSY;
2077 rdev->data_offset = offset;
2078 return len;
2081 static struct rdev_sysfs_entry rdev_offset =
2082 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
2084 static ssize_t
2085 rdev_size_show(mdk_rdev_t *rdev, char *page)
2087 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
2090 static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2092 /* check if two start/length pairs overlap */
2093 if (s1+l1 <= s2)
2094 return 0;
2095 if (s2+l2 <= s1)
2096 return 0;
2097 return 1;
2100 static ssize_t
2101 rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2103 unsigned long long size;
2104 unsigned long long oldsize = rdev->size;
2105 mddev_t *my_mddev = rdev->mddev;
2107 if (strict_strtoull(buf, 10, &size) < 0)
2108 return -EINVAL;
2109 if (size < my_mddev->size)
2110 return -EINVAL;
2111 if (my_mddev->pers && rdev->raid_disk >= 0) {
2112 if (my_mddev->persistent) {
2113 size = super_types[my_mddev->major_version].
2114 rdev_size_change(rdev, size * 2);
2115 if (!size)
2116 return -EBUSY;
2117 } else if (!size) {
2118 size = (rdev->bdev->bd_inode->i_size >> 10);
2119 size -= rdev->data_offset/2;
2121 if (size < my_mddev->size)
2122 return -EINVAL; /* component must fit device */
2125 rdev->size = size;
2126 if (size > oldsize && my_mddev->external) {
2127 /* need to check that all other rdevs with the same ->bdev
2128 * do not overlap. We need to unlock the mddev to avoid
2129 * a deadlock. We have already changed rdev->size, and if
2130 * we have to change it back, we will have the lock again.
2132 mddev_t *mddev;
2133 int overlap = 0;
2134 struct list_head *tmp, *tmp2;
2136 mddev_unlock(my_mddev);
2137 for_each_mddev(mddev, tmp) {
2138 mdk_rdev_t *rdev2;
2140 mddev_lock(mddev);
2141 rdev_for_each(rdev2, tmp2, mddev)
2142 if (test_bit(AllReserved, &rdev2->flags) ||
2143 (rdev->bdev == rdev2->bdev &&
2144 rdev != rdev2 &&
2145 overlaps(rdev->data_offset, rdev->size * 2,
2146 rdev2->data_offset,
2147 rdev2->size * 2))) {
2148 overlap = 1;
2149 break;
2151 mddev_unlock(mddev);
2152 if (overlap) {
2153 mddev_put(mddev);
2154 break;
2157 mddev_lock(my_mddev);
2158 if (overlap) {
2159 /* Someone else could have slipped in a size
2160 * change here, but doing so is just silly.
2161 * We put oldsize back because we *know* it is
2162 * safe, and trust userspace not to race with
2163 * itself
2165 rdev->size = oldsize;
2166 return -EBUSY;
2169 return len;
2172 static struct rdev_sysfs_entry rdev_size =
2173 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
2175 static struct attribute *rdev_default_attrs[] = {
2176 &rdev_state.attr,
2177 &rdev_errors.attr,
2178 &rdev_slot.attr,
2179 &rdev_offset.attr,
2180 &rdev_size.attr,
2181 NULL,
2183 static ssize_t
2184 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2186 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2187 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2188 mddev_t *mddev = rdev->mddev;
2189 ssize_t rv;
2191 if (!entry->show)
2192 return -EIO;
2194 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2195 if (!rv) {
2196 if (rdev->mddev == NULL)
2197 rv = -EBUSY;
2198 else
2199 rv = entry->show(rdev, page);
2200 mddev_unlock(mddev);
2202 return rv;
2205 static ssize_t
2206 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2207 const char *page, size_t length)
2209 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2210 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2211 ssize_t rv;
2212 mddev_t *mddev = rdev->mddev;
2214 if (!entry->store)
2215 return -EIO;
2216 if (!capable(CAP_SYS_ADMIN))
2217 return -EACCES;
2218 rv = mddev ? mddev_lock(mddev): -EBUSY;
2219 if (!rv) {
2220 if (rdev->mddev == NULL)
2221 rv = -EBUSY;
2222 else
2223 rv = entry->store(rdev, page, length);
2224 mddev_unlock(mddev);
2226 return rv;
2229 static void rdev_free(struct kobject *ko)
2231 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2232 kfree(rdev);
2234 static struct sysfs_ops rdev_sysfs_ops = {
2235 .show = rdev_attr_show,
2236 .store = rdev_attr_store,
2238 static struct kobj_type rdev_ktype = {
2239 .release = rdev_free,
2240 .sysfs_ops = &rdev_sysfs_ops,
2241 .default_attrs = rdev_default_attrs,
2245 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2247 * mark the device faulty if:
2249 * - the device is nonexistent (zero size)
2250 * - the device has no valid superblock
2252 * a faulty rdev _never_ has rdev->sb set.
2254 static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2256 char b[BDEVNAME_SIZE];
2257 int err;
2258 mdk_rdev_t *rdev;
2259 sector_t size;
2261 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
2262 if (!rdev) {
2263 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2264 return ERR_PTR(-ENOMEM);
2267 if ((err = alloc_disk_sb(rdev)))
2268 goto abort_free;
2270 err = lock_rdev(rdev, newdev, super_format == -2);
2271 if (err)
2272 goto abort_free;
2274 kobject_init(&rdev->kobj, &rdev_ktype);
2276 rdev->desc_nr = -1;
2277 rdev->saved_raid_disk = -1;
2278 rdev->raid_disk = -1;
2279 rdev->flags = 0;
2280 rdev->data_offset = 0;
2281 rdev->sb_events = 0;
2282 atomic_set(&rdev->nr_pending, 0);
2283 atomic_set(&rdev->read_errors, 0);
2284 atomic_set(&rdev->corrected_errors, 0);
2286 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2287 if (!size) {
2288 printk(KERN_WARNING
2289 "md: %s has zero or unknown size, marking faulty!\n",
2290 bdevname(rdev->bdev,b));
2291 err = -EINVAL;
2292 goto abort_free;
2295 if (super_format >= 0) {
2296 err = super_types[super_format].
2297 load_super(rdev, NULL, super_minor);
2298 if (err == -EINVAL) {
2299 printk(KERN_WARNING
2300 "md: %s does not have a valid v%d.%d "
2301 "superblock, not importing!\n",
2302 bdevname(rdev->bdev,b),
2303 super_format, super_minor);
2304 goto abort_free;
2306 if (err < 0) {
2307 printk(KERN_WARNING
2308 "md: could not read %s's sb, not importing!\n",
2309 bdevname(rdev->bdev,b));
2310 goto abort_free;
2314 INIT_LIST_HEAD(&rdev->same_set);
2315 init_waitqueue_head(&rdev->blocked_wait);
2317 return rdev;
2319 abort_free:
2320 if (rdev->sb_page) {
2321 if (rdev->bdev)
2322 unlock_rdev(rdev);
2323 free_disk_sb(rdev);
2325 kfree(rdev);
2326 return ERR_PTR(err);
2330 * Check a full RAID array for plausibility
2334 static void analyze_sbs(mddev_t * mddev)
2336 int i;
2337 struct list_head *tmp;
2338 mdk_rdev_t *rdev, *freshest;
2339 char b[BDEVNAME_SIZE];
2341 freshest = NULL;
2342 rdev_for_each(rdev, tmp, mddev)
2343 switch (super_types[mddev->major_version].
2344 load_super(rdev, freshest, mddev->minor_version)) {
2345 case 1:
2346 freshest = rdev;
2347 break;
2348 case 0:
2349 break;
2350 default:
2351 printk( KERN_ERR \
2352 "md: fatal superblock inconsistency in %s"
2353 " -- removing from array\n",
2354 bdevname(rdev->bdev,b));
2355 kick_rdev_from_array(rdev);
2359 super_types[mddev->major_version].
2360 validate_super(mddev, freshest);
2362 i = 0;
2363 rdev_for_each(rdev, tmp, mddev) {
2364 if (rdev != freshest)
2365 if (super_types[mddev->major_version].
2366 validate_super(mddev, rdev)) {
2367 printk(KERN_WARNING "md: kicking non-fresh %s"
2368 " from array!\n",
2369 bdevname(rdev->bdev,b));
2370 kick_rdev_from_array(rdev);
2371 continue;
2373 if (mddev->level == LEVEL_MULTIPATH) {
2374 rdev->desc_nr = i++;
2375 rdev->raid_disk = rdev->desc_nr;
2376 set_bit(In_sync, &rdev->flags);
2377 } else if (rdev->raid_disk >= mddev->raid_disks) {
2378 rdev->raid_disk = -1;
2379 clear_bit(In_sync, &rdev->flags);
2385 if (mddev->recovery_cp != MaxSector &&
2386 mddev->level >= 1)
2387 printk(KERN_ERR "md: %s: raid array is not clean"
2388 " -- starting background reconstruction\n",
2389 mdname(mddev));
2393 static void md_safemode_timeout(unsigned long data);
2395 static ssize_t
2396 safe_delay_show(mddev_t *mddev, char *page)
2398 int msec = (mddev->safemode_delay*1000)/HZ;
2399 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2401 static ssize_t
2402 safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2404 int scale=1;
2405 int dot=0;
2406 int i;
2407 unsigned long msec;
2408 char buf[30];
2409 char *e;
2410 /* remove a period, and count digits after it */
2411 if (len >= sizeof(buf))
2412 return -EINVAL;
2413 strlcpy(buf, cbuf, len);
2414 buf[len] = 0;
2415 for (i=0; i<len; i++) {
2416 if (dot) {
2417 if (isdigit(buf[i])) {
2418 buf[i-1] = buf[i];
2419 scale *= 10;
2421 buf[i] = 0;
2422 } else if (buf[i] == '.') {
2423 dot=1;
2424 buf[i] = 0;
2427 msec = simple_strtoul(buf, &e, 10);
2428 if (e == buf || (*e && *e != '\n'))
2429 return -EINVAL;
2430 msec = (msec * 1000) / scale;
2431 if (msec == 0)
2432 mddev->safemode_delay = 0;
2433 else {
2434 unsigned long old_delay = mddev->safemode_delay;
2435 mddev->safemode_delay = (msec*HZ)/1000;
2436 if (mddev->safemode_delay == 0)
2437 mddev->safemode_delay = 1;
2438 if (mddev->safemode_delay < old_delay)
2439 md_safemode_timeout((unsigned long)mddev);
2441 return len;
2443 static struct md_sysfs_entry md_safe_delay =
2444 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
2446 static ssize_t
2447 level_show(mddev_t *mddev, char *page)
2449 struct mdk_personality *p = mddev->pers;
2450 if (p)
2451 return sprintf(page, "%s\n", p->name);
2452 else if (mddev->clevel[0])
2453 return sprintf(page, "%s\n", mddev->clevel);
2454 else if (mddev->level != LEVEL_NONE)
2455 return sprintf(page, "%d\n", mddev->level);
2456 else
2457 return 0;
2460 static ssize_t
2461 level_store(mddev_t *mddev, const char *buf, size_t len)
2463 ssize_t rv = len;
2464 if (mddev->pers)
2465 return -EBUSY;
2466 if (len == 0)
2467 return 0;
2468 if (len >= sizeof(mddev->clevel))
2469 return -ENOSPC;
2470 strncpy(mddev->clevel, buf, len);
2471 if (mddev->clevel[len-1] == '\n')
2472 len--;
2473 mddev->clevel[len] = 0;
2474 mddev->level = LEVEL_NONE;
2475 return rv;
2478 static struct md_sysfs_entry md_level =
2479 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
2482 static ssize_t
2483 layout_show(mddev_t *mddev, char *page)
2485 /* just a number, not meaningful for all levels */
2486 if (mddev->reshape_position != MaxSector &&
2487 mddev->layout != mddev->new_layout)
2488 return sprintf(page, "%d (%d)\n",
2489 mddev->new_layout, mddev->layout);
2490 return sprintf(page, "%d\n", mddev->layout);
2493 static ssize_t
2494 layout_store(mddev_t *mddev, const char *buf, size_t len)
2496 char *e;
2497 unsigned long n = simple_strtoul(buf, &e, 10);
2499 if (!*buf || (*e && *e != '\n'))
2500 return -EINVAL;
2502 if (mddev->pers)
2503 return -EBUSY;
2504 if (mddev->reshape_position != MaxSector)
2505 mddev->new_layout = n;
2506 else
2507 mddev->layout = n;
2508 return len;
2510 static struct md_sysfs_entry md_layout =
2511 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
2514 static ssize_t
2515 raid_disks_show(mddev_t *mddev, char *page)
2517 if (mddev->raid_disks == 0)
2518 return 0;
2519 if (mddev->reshape_position != MaxSector &&
2520 mddev->delta_disks != 0)
2521 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2522 mddev->raid_disks - mddev->delta_disks);
2523 return sprintf(page, "%d\n", mddev->raid_disks);
2526 static int update_raid_disks(mddev_t *mddev, int raid_disks);
2528 static ssize_t
2529 raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2531 char *e;
2532 int rv = 0;
2533 unsigned long n = simple_strtoul(buf, &e, 10);
2535 if (!*buf || (*e && *e != '\n'))
2536 return -EINVAL;
2538 if (mddev->pers)
2539 rv = update_raid_disks(mddev, n);
2540 else if (mddev->reshape_position != MaxSector) {
2541 int olddisks = mddev->raid_disks - mddev->delta_disks;
2542 mddev->delta_disks = n - olddisks;
2543 mddev->raid_disks = n;
2544 } else
2545 mddev->raid_disks = n;
2546 return rv ? rv : len;
2548 static struct md_sysfs_entry md_raid_disks =
2549 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
2551 static ssize_t
2552 chunk_size_show(mddev_t *mddev, char *page)
2554 if (mddev->reshape_position != MaxSector &&
2555 mddev->chunk_size != mddev->new_chunk)
2556 return sprintf(page, "%d (%d)\n", mddev->new_chunk,
2557 mddev->chunk_size);
2558 return sprintf(page, "%d\n", mddev->chunk_size);
2561 static ssize_t
2562 chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2564 /* can only set chunk_size if array is not yet active */
2565 char *e;
2566 unsigned long n = simple_strtoul(buf, &e, 10);
2568 if (!*buf || (*e && *e != '\n'))
2569 return -EINVAL;
2571 if (mddev->pers)
2572 return -EBUSY;
2573 else if (mddev->reshape_position != MaxSector)
2574 mddev->new_chunk = n;
2575 else
2576 mddev->chunk_size = n;
2577 return len;
2579 static struct md_sysfs_entry md_chunk_size =
2580 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
2582 static ssize_t
2583 resync_start_show(mddev_t *mddev, char *page)
2585 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2588 static ssize_t
2589 resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2591 char *e;
2592 unsigned long long n = simple_strtoull(buf, &e, 10);
2594 if (mddev->pers)
2595 return -EBUSY;
2596 if (!*buf || (*e && *e != '\n'))
2597 return -EINVAL;
2599 mddev->recovery_cp = n;
2600 return len;
2602 static struct md_sysfs_entry md_resync_start =
2603 __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
2606 * The array state can be:
2608 * clear
2609 * No devices, no size, no level
2610 * Equivalent to STOP_ARRAY ioctl
2611 * inactive
2612 * May have some settings, but array is not active
2613 * all IO results in error
2614 * When written, doesn't tear down array, but just stops it
2615 * suspended (not supported yet)
2616 * All IO requests will block. The array can be reconfigured.
2617 * Writing this, if accepted, will block until array is quiescent
2618 * readonly
2619 * no resync can happen. no superblocks get written.
2620 * write requests fail
2621 * read-auto
2622 * like readonly, but behaves like 'clean' on a write request.
2624 * clean - no pending writes, but otherwise active.
2625 * When written to inactive array, starts without resync
2626 * If a write request arrives then
2627 * if metadata is known, mark 'dirty' and switch to 'active'.
2628 * if not known, block and switch to write-pending
2629 * If written to an active array that has pending writes, then fails.
2630 * active
2631 * fully active: IO and resync can be happening.
2632 * When written to inactive array, starts with resync
2634 * write-pending
2635 * clean, but writes are blocked waiting for 'active' to be written.
2637 * active-idle
2638 * like active, but no writes have been seen for a while (100msec).
2641 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2642 write_pending, active_idle, bad_word};
2643 static char *array_states[] = {
2644 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2645 "write-pending", "active-idle", NULL };
2647 static int match_word(const char *word, char **list)
2649 int n;
2650 for (n=0; list[n]; n++)
2651 if (cmd_match(word, list[n]))
2652 break;
2653 return n;
2656 static ssize_t
2657 array_state_show(mddev_t *mddev, char *page)
2659 enum array_state st = inactive;
2661 if (mddev->pers)
2662 switch(mddev->ro) {
2663 case 1:
2664 st = readonly;
2665 break;
2666 case 2:
2667 st = read_auto;
2668 break;
2669 case 0:
2670 if (mddev->in_sync)
2671 st = clean;
2672 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
2673 st = write_pending;
2674 else if (mddev->safemode)
2675 st = active_idle;
2676 else
2677 st = active;
2679 else {
2680 if (list_empty(&mddev->disks) &&
2681 mddev->raid_disks == 0 &&
2682 mddev->size == 0)
2683 st = clear;
2684 else
2685 st = inactive;
2687 return sprintf(page, "%s\n", array_states[st]);
2690 static int do_md_stop(mddev_t * mddev, int ro, int is_open);
2691 static int do_md_run(mddev_t * mddev);
2692 static int restart_array(mddev_t *mddev);
2694 static ssize_t
2695 array_state_store(mddev_t *mddev, const char *buf, size_t len)
2697 int err = -EINVAL;
2698 enum array_state st = match_word(buf, array_states);
2699 switch(st) {
2700 case bad_word:
2701 break;
2702 case clear:
2703 /* stopping an active array */
2704 if (atomic_read(&mddev->openers) > 0)
2705 return -EBUSY;
2706 err = do_md_stop(mddev, 0, 0);
2707 break;
2708 case inactive:
2709 /* stopping an active array */
2710 if (mddev->pers) {
2711 if (atomic_read(&mddev->openers) > 0)
2712 return -EBUSY;
2713 err = do_md_stop(mddev, 2, 0);
2714 } else
2715 err = 0; /* already inactive */
2716 break;
2717 case suspended:
2718 break; /* not supported yet */
2719 case readonly:
2720 if (mddev->pers)
2721 err = do_md_stop(mddev, 1, 0);
2722 else {
2723 mddev->ro = 1;
2724 set_disk_ro(mddev->gendisk, 1);
2725 err = do_md_run(mddev);
2727 break;
2728 case read_auto:
2729 if (mddev->pers) {
2730 if (mddev->ro != 1)
2731 err = do_md_stop(mddev, 1, 0);
2732 else
2733 err = restart_array(mddev);
2734 if (err == 0) {
2735 mddev->ro = 2;
2736 set_disk_ro(mddev->gendisk, 0);
2738 } else {
2739 mddev->ro = 2;
2740 err = do_md_run(mddev);
2742 break;
2743 case clean:
2744 if (mddev->pers) {
2745 restart_array(mddev);
2746 spin_lock_irq(&mddev->write_lock);
2747 if (atomic_read(&mddev->writes_pending) == 0) {
2748 if (mddev->in_sync == 0) {
2749 mddev->in_sync = 1;
2750 if (mddev->safemode == 1)
2751 mddev->safemode = 0;
2752 if (mddev->persistent)
2753 set_bit(MD_CHANGE_CLEAN,
2754 &mddev->flags);
2756 err = 0;
2757 } else
2758 err = -EBUSY;
2759 spin_unlock_irq(&mddev->write_lock);
2760 } else {
2761 mddev->ro = 0;
2762 mddev->recovery_cp = MaxSector;
2763 err = do_md_run(mddev);
2765 break;
2766 case active:
2767 if (mddev->pers) {
2768 restart_array(mddev);
2769 if (mddev->external)
2770 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
2771 wake_up(&mddev->sb_wait);
2772 err = 0;
2773 } else {
2774 mddev->ro = 0;
2775 set_disk_ro(mddev->gendisk, 0);
2776 err = do_md_run(mddev);
2778 break;
2779 case write_pending:
2780 case active_idle:
2781 /* these cannot be set */
2782 break;
2784 if (err)
2785 return err;
2786 else {
2787 sysfs_notify(&mddev->kobj, NULL, "array_state");
2788 return len;
2791 static struct md_sysfs_entry md_array_state =
2792 __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
2794 static ssize_t
2795 null_show(mddev_t *mddev, char *page)
2797 return -EINVAL;
2800 static ssize_t
2801 new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2803 /* buf must be %d:%d\n? giving major and minor numbers */
2804 /* The new device is added to the array.
2805 * If the array has a persistent superblock, we read the
2806 * superblock to initialise info and check validity.
2807 * Otherwise, only checking done is that in bind_rdev_to_array,
2808 * which mainly checks size.
2810 char *e;
2811 int major = simple_strtoul(buf, &e, 10);
2812 int minor;
2813 dev_t dev;
2814 mdk_rdev_t *rdev;
2815 int err;
2817 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2818 return -EINVAL;
2819 minor = simple_strtoul(e+1, &e, 10);
2820 if (*e && *e != '\n')
2821 return -EINVAL;
2822 dev = MKDEV(major, minor);
2823 if (major != MAJOR(dev) ||
2824 minor != MINOR(dev))
2825 return -EOVERFLOW;
2828 if (mddev->persistent) {
2829 rdev = md_import_device(dev, mddev->major_version,
2830 mddev->minor_version);
2831 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2832 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2833 mdk_rdev_t, same_set);
2834 err = super_types[mddev->major_version]
2835 .load_super(rdev, rdev0, mddev->minor_version);
2836 if (err < 0)
2837 goto out;
2839 } else if (mddev->external)
2840 rdev = md_import_device(dev, -2, -1);
2841 else
2842 rdev = md_import_device(dev, -1, -1);
2844 if (IS_ERR(rdev))
2845 return PTR_ERR(rdev);
2846 err = bind_rdev_to_array(rdev, mddev);
2847 out:
2848 if (err)
2849 export_rdev(rdev);
2850 return err ? err : len;
2853 static struct md_sysfs_entry md_new_device =
2854 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
2856 static ssize_t
2857 bitmap_store(mddev_t *mddev, const char *buf, size_t len)
2859 char *end;
2860 unsigned long chunk, end_chunk;
2862 if (!mddev->bitmap)
2863 goto out;
2864 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2865 while (*buf) {
2866 chunk = end_chunk = simple_strtoul(buf, &end, 0);
2867 if (buf == end) break;
2868 if (*end == '-') { /* range */
2869 buf = end + 1;
2870 end_chunk = simple_strtoul(buf, &end, 0);
2871 if (buf == end) break;
2873 if (*end && !isspace(*end)) break;
2874 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
2875 buf = end;
2876 while (isspace(*buf)) buf++;
2878 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
2879 out:
2880 return len;
2883 static struct md_sysfs_entry md_bitmap =
2884 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
2886 static ssize_t
2887 size_show(mddev_t *mddev, char *page)
2889 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2892 static int update_size(mddev_t *mddev, sector_t num_sectors);
2894 static ssize_t
2895 size_store(mddev_t *mddev, const char *buf, size_t len)
2897 /* If array is inactive, we can reduce the component size, but
2898 * not increase it (except from 0).
2899 * If array is active, we can try an on-line resize
2901 char *e;
2902 int err = 0;
2903 unsigned long long size = simple_strtoull(buf, &e, 10);
2904 if (!*buf || *buf == '\n' ||
2905 (*e && *e != '\n'))
2906 return -EINVAL;
2908 if (mddev->pers) {
2909 err = update_size(mddev, size * 2);
2910 md_update_sb(mddev, 1);
2911 } else {
2912 if (mddev->size == 0 ||
2913 mddev->size > size)
2914 mddev->size = size;
2915 else
2916 err = -ENOSPC;
2918 return err ? err : len;
2921 static struct md_sysfs_entry md_size =
2922 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
2925 /* Metdata version.
2926 * This is one of
2927 * 'none' for arrays with no metadata (good luck...)
2928 * 'external' for arrays with externally managed metadata,
2929 * or N.M for internally known formats
2931 static ssize_t
2932 metadata_show(mddev_t *mddev, char *page)
2934 if (mddev->persistent)
2935 return sprintf(page, "%d.%d\n",
2936 mddev->major_version, mddev->minor_version);
2937 else if (mddev->external)
2938 return sprintf(page, "external:%s\n", mddev->metadata_type);
2939 else
2940 return sprintf(page, "none\n");
2943 static ssize_t
2944 metadata_store(mddev_t *mddev, const char *buf, size_t len)
2946 int major, minor;
2947 char *e;
2948 if (!list_empty(&mddev->disks))
2949 return -EBUSY;
2951 if (cmd_match(buf, "none")) {
2952 mddev->persistent = 0;
2953 mddev->external = 0;
2954 mddev->major_version = 0;
2955 mddev->minor_version = 90;
2956 return len;
2958 if (strncmp(buf, "external:", 9) == 0) {
2959 size_t namelen = len-9;
2960 if (namelen >= sizeof(mddev->metadata_type))
2961 namelen = sizeof(mddev->metadata_type)-1;
2962 strncpy(mddev->metadata_type, buf+9, namelen);
2963 mddev->metadata_type[namelen] = 0;
2964 if (namelen && mddev->metadata_type[namelen-1] == '\n')
2965 mddev->metadata_type[--namelen] = 0;
2966 mddev->persistent = 0;
2967 mddev->external = 1;
2968 mddev->major_version = 0;
2969 mddev->minor_version = 90;
2970 return len;
2972 major = simple_strtoul(buf, &e, 10);
2973 if (e==buf || *e != '.')
2974 return -EINVAL;
2975 buf = e+1;
2976 minor = simple_strtoul(buf, &e, 10);
2977 if (e==buf || (*e && *e != '\n') )
2978 return -EINVAL;
2979 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
2980 return -ENOENT;
2981 mddev->major_version = major;
2982 mddev->minor_version = minor;
2983 mddev->persistent = 1;
2984 mddev->external = 0;
2985 return len;
2988 static struct md_sysfs_entry md_metadata =
2989 __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
2991 static ssize_t
2992 action_show(mddev_t *mddev, char *page)
2994 char *type = "idle";
2995 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2996 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
2997 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2998 type = "reshape";
2999 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3000 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3001 type = "resync";
3002 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3003 type = "check";
3004 else
3005 type = "repair";
3006 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
3007 type = "recover";
3009 return sprintf(page, "%s\n", type);
3012 static ssize_t
3013 action_store(mddev_t *mddev, const char *page, size_t len)
3015 if (!mddev->pers || !mddev->pers->sync_request)
3016 return -EINVAL;
3018 if (cmd_match(page, "idle")) {
3019 if (mddev->sync_thread) {
3020 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3021 md_unregister_thread(mddev->sync_thread);
3022 mddev->sync_thread = NULL;
3023 mddev->recovery = 0;
3025 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3026 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
3027 return -EBUSY;
3028 else if (cmd_match(page, "resync"))
3029 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3030 else if (cmd_match(page, "recover")) {
3031 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3032 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3033 } else if (cmd_match(page, "reshape")) {
3034 int err;
3035 if (mddev->pers->start_reshape == NULL)
3036 return -EINVAL;
3037 err = mddev->pers->start_reshape(mddev);
3038 if (err)
3039 return err;
3040 sysfs_notify(&mddev->kobj, NULL, "degraded");
3041 } else {
3042 if (cmd_match(page, "check"))
3043 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3044 else if (!cmd_match(page, "repair"))
3045 return -EINVAL;
3046 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3047 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3049 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3050 md_wakeup_thread(mddev->thread);
3051 sysfs_notify(&mddev->kobj, NULL, "sync_action");
3052 return len;
3055 static ssize_t
3056 mismatch_cnt_show(mddev_t *mddev, char *page)
3058 return sprintf(page, "%llu\n",
3059 (unsigned long long) mddev->resync_mismatches);
3062 static struct md_sysfs_entry md_scan_mode =
3063 __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
3066 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
3068 static ssize_t
3069 sync_min_show(mddev_t *mddev, char *page)
3071 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3072 mddev->sync_speed_min ? "local": "system");
3075 static ssize_t
3076 sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3078 int min;
3079 char *e;
3080 if (strncmp(buf, "system", 6)==0) {
3081 mddev->sync_speed_min = 0;
3082 return len;
3084 min = simple_strtoul(buf, &e, 10);
3085 if (buf == e || (*e && *e != '\n') || min <= 0)
3086 return -EINVAL;
3087 mddev->sync_speed_min = min;
3088 return len;
3091 static struct md_sysfs_entry md_sync_min =
3092 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3094 static ssize_t
3095 sync_max_show(mddev_t *mddev, char *page)
3097 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3098 mddev->sync_speed_max ? "local": "system");
3101 static ssize_t
3102 sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3104 int max;
3105 char *e;
3106 if (strncmp(buf, "system", 6)==0) {
3107 mddev->sync_speed_max = 0;
3108 return len;
3110 max = simple_strtoul(buf, &e, 10);
3111 if (buf == e || (*e && *e != '\n') || max <= 0)
3112 return -EINVAL;
3113 mddev->sync_speed_max = max;
3114 return len;
3117 static struct md_sysfs_entry md_sync_max =
3118 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3120 static ssize_t
3121 degraded_show(mddev_t *mddev, char *page)
3123 return sprintf(page, "%d\n", mddev->degraded);
3125 static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
3127 static ssize_t
3128 sync_force_parallel_show(mddev_t *mddev, char *page)
3130 return sprintf(page, "%d\n", mddev->parallel_resync);
3133 static ssize_t
3134 sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3136 long n;
3138 if (strict_strtol(buf, 10, &n))
3139 return -EINVAL;
3141 if (n != 0 && n != 1)
3142 return -EINVAL;
3144 mddev->parallel_resync = n;
3146 if (mddev->sync_thread)
3147 wake_up(&resync_wait);
3149 return len;
3152 /* force parallel resync, even with shared block devices */
3153 static struct md_sysfs_entry md_sync_force_parallel =
3154 __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3155 sync_force_parallel_show, sync_force_parallel_store);
3157 static ssize_t
3158 sync_speed_show(mddev_t *mddev, char *page)
3160 unsigned long resync, dt, db;
3161 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3162 dt = (jiffies - mddev->resync_mark) / HZ;
3163 if (!dt) dt++;
3164 db = resync - mddev->resync_mark_cnt;
3165 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
3168 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
3170 static ssize_t
3171 sync_completed_show(mddev_t *mddev, char *page)
3173 unsigned long max_blocks, resync;
3175 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3176 max_blocks = mddev->resync_max_sectors;
3177 else
3178 max_blocks = mddev->size << 1;
3180 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
3181 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
3184 static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
3186 static ssize_t
3187 min_sync_show(mddev_t *mddev, char *page)
3189 return sprintf(page, "%llu\n",
3190 (unsigned long long)mddev->resync_min);
3192 static ssize_t
3193 min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3195 unsigned long long min;
3196 if (strict_strtoull(buf, 10, &min))
3197 return -EINVAL;
3198 if (min > mddev->resync_max)
3199 return -EINVAL;
3200 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3201 return -EBUSY;
3203 /* Must be a multiple of chunk_size */
3204 if (mddev->chunk_size) {
3205 if (min & (sector_t)((mddev->chunk_size>>9)-1))
3206 return -EINVAL;
3208 mddev->resync_min = min;
3210 return len;
3213 static struct md_sysfs_entry md_min_sync =
3214 __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3216 static ssize_t
3217 max_sync_show(mddev_t *mddev, char *page)
3219 if (mddev->resync_max == MaxSector)
3220 return sprintf(page, "max\n");
3221 else
3222 return sprintf(page, "%llu\n",
3223 (unsigned long long)mddev->resync_max);
3225 static ssize_t
3226 max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3228 if (strncmp(buf, "max", 3) == 0)
3229 mddev->resync_max = MaxSector;
3230 else {
3231 unsigned long long max;
3232 if (strict_strtoull(buf, 10, &max))
3233 return -EINVAL;
3234 if (max < mddev->resync_min)
3235 return -EINVAL;
3236 if (max < mddev->resync_max &&
3237 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3238 return -EBUSY;
3240 /* Must be a multiple of chunk_size */
3241 if (mddev->chunk_size) {
3242 if (max & (sector_t)((mddev->chunk_size>>9)-1))
3243 return -EINVAL;
3245 mddev->resync_max = max;
3247 wake_up(&mddev->recovery_wait);
3248 return len;
3251 static struct md_sysfs_entry md_max_sync =
3252 __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3254 static ssize_t
3255 suspend_lo_show(mddev_t *mddev, char *page)
3257 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3260 static ssize_t
3261 suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3263 char *e;
3264 unsigned long long new = simple_strtoull(buf, &e, 10);
3266 if (mddev->pers->quiesce == NULL)
3267 return -EINVAL;
3268 if (buf == e || (*e && *e != '\n'))
3269 return -EINVAL;
3270 if (new >= mddev->suspend_hi ||
3271 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3272 mddev->suspend_lo = new;
3273 mddev->pers->quiesce(mddev, 2);
3274 return len;
3275 } else
3276 return -EINVAL;
3278 static struct md_sysfs_entry md_suspend_lo =
3279 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3282 static ssize_t
3283 suspend_hi_show(mddev_t *mddev, char *page)
3285 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3288 static ssize_t
3289 suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3291 char *e;
3292 unsigned long long new = simple_strtoull(buf, &e, 10);
3294 if (mddev->pers->quiesce == NULL)
3295 return -EINVAL;
3296 if (buf == e || (*e && *e != '\n'))
3297 return -EINVAL;
3298 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3299 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3300 mddev->suspend_hi = new;
3301 mddev->pers->quiesce(mddev, 1);
3302 mddev->pers->quiesce(mddev, 0);
3303 return len;
3304 } else
3305 return -EINVAL;
3307 static struct md_sysfs_entry md_suspend_hi =
3308 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3310 static ssize_t
3311 reshape_position_show(mddev_t *mddev, char *page)
3313 if (mddev->reshape_position != MaxSector)
3314 return sprintf(page, "%llu\n",
3315 (unsigned long long)mddev->reshape_position);
3316 strcpy(page, "none\n");
3317 return 5;
3320 static ssize_t
3321 reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3323 char *e;
3324 unsigned long long new = simple_strtoull(buf, &e, 10);
3325 if (mddev->pers)
3326 return -EBUSY;
3327 if (buf == e || (*e && *e != '\n'))
3328 return -EINVAL;
3329 mddev->reshape_position = new;
3330 mddev->delta_disks = 0;
3331 mddev->new_level = mddev->level;
3332 mddev->new_layout = mddev->layout;
3333 mddev->new_chunk = mddev->chunk_size;
3334 return len;
3337 static struct md_sysfs_entry md_reshape_position =
3338 __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3339 reshape_position_store);
3342 static struct attribute *md_default_attrs[] = {
3343 &md_level.attr,
3344 &md_layout.attr,
3345 &md_raid_disks.attr,
3346 &md_chunk_size.attr,
3347 &md_size.attr,
3348 &md_resync_start.attr,
3349 &md_metadata.attr,
3350 &md_new_device.attr,
3351 &md_safe_delay.attr,
3352 &md_array_state.attr,
3353 &md_reshape_position.attr,
3354 NULL,
3357 static struct attribute *md_redundancy_attrs[] = {
3358 &md_scan_mode.attr,
3359 &md_mismatches.attr,
3360 &md_sync_min.attr,
3361 &md_sync_max.attr,
3362 &md_sync_speed.attr,
3363 &md_sync_force_parallel.attr,
3364 &md_sync_completed.attr,
3365 &md_min_sync.attr,
3366 &md_max_sync.attr,
3367 &md_suspend_lo.attr,
3368 &md_suspend_hi.attr,
3369 &md_bitmap.attr,
3370 &md_degraded.attr,
3371 NULL,
3373 static struct attribute_group md_redundancy_group = {
3374 .name = NULL,
3375 .attrs = md_redundancy_attrs,
3379 static ssize_t
3380 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3382 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3383 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
3384 ssize_t rv;
3386 if (!entry->show)
3387 return -EIO;
3388 rv = mddev_lock(mddev);
3389 if (!rv) {
3390 rv = entry->show(mddev, page);
3391 mddev_unlock(mddev);
3393 return rv;
3396 static ssize_t
3397 md_attr_store(struct kobject *kobj, struct attribute *attr,
3398 const char *page, size_t length)
3400 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3401 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
3402 ssize_t rv;
3404 if (!entry->store)
3405 return -EIO;
3406 if (!capable(CAP_SYS_ADMIN))
3407 return -EACCES;
3408 rv = mddev_lock(mddev);
3409 if (!rv) {
3410 rv = entry->store(mddev, page, length);
3411 mddev_unlock(mddev);
3413 return rv;
3416 static void md_free(struct kobject *ko)
3418 mddev_t *mddev = container_of(ko, mddev_t, kobj);
3419 kfree(mddev);
3422 static struct sysfs_ops md_sysfs_ops = {
3423 .show = md_attr_show,
3424 .store = md_attr_store,
3426 static struct kobj_type md_ktype = {
3427 .release = md_free,
3428 .sysfs_ops = &md_sysfs_ops,
3429 .default_attrs = md_default_attrs,
3432 int mdp_major = 0;
3434 static struct kobject *md_probe(dev_t dev, int *part, void *data)
3436 static DEFINE_MUTEX(disks_mutex);
3437 mddev_t *mddev = mddev_find(dev);
3438 struct gendisk *disk;
3439 int partitioned = (MAJOR(dev) != MD_MAJOR);
3440 int shift = partitioned ? MdpMinorShift : 0;
3441 int unit = MINOR(dev) >> shift;
3442 int error;
3444 if (!mddev)
3445 return NULL;
3447 mutex_lock(&disks_mutex);
3448 if (mddev->gendisk) {
3449 mutex_unlock(&disks_mutex);
3450 mddev_put(mddev);
3451 return NULL;
3453 disk = alloc_disk(1 << shift);
3454 if (!disk) {
3455 mutex_unlock(&disks_mutex);
3456 mddev_put(mddev);
3457 return NULL;
3459 disk->major = MAJOR(dev);
3460 disk->first_minor = unit << shift;
3461 if (partitioned)
3462 sprintf(disk->disk_name, "md_d%d", unit);
3463 else
3464 sprintf(disk->disk_name, "md%d", unit);
3465 disk->fops = &md_fops;
3466 disk->private_data = mddev;
3467 disk->queue = mddev->queue;
3468 add_disk(disk);
3469 mddev->gendisk = disk;
3470 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
3471 &disk_to_dev(disk)->kobj, "%s", "md");
3472 mutex_unlock(&disks_mutex);
3473 if (error)
3474 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3475 disk->disk_name);
3476 else
3477 kobject_uevent(&mddev->kobj, KOBJ_ADD);
3478 return NULL;
3481 static void md_safemode_timeout(unsigned long data)
3483 mddev_t *mddev = (mddev_t *) data;
3485 if (!atomic_read(&mddev->writes_pending)) {
3486 mddev->safemode = 1;
3487 if (mddev->external)
3488 set_bit(MD_NOTIFY_ARRAY_STATE, &mddev->flags);
3490 md_wakeup_thread(mddev->thread);
3493 static int start_dirty_degraded;
3495 static int do_md_run(mddev_t * mddev)
3497 int err;
3498 int chunk_size;
3499 struct list_head *tmp;
3500 mdk_rdev_t *rdev;
3501 struct gendisk *disk;
3502 struct mdk_personality *pers;
3503 char b[BDEVNAME_SIZE];
3505 if (list_empty(&mddev->disks))
3506 /* cannot run an array with no devices.. */
3507 return -EINVAL;
3509 if (mddev->pers)
3510 return -EBUSY;
3513 * Analyze all RAID superblock(s)
3515 if (!mddev->raid_disks) {
3516 if (!mddev->persistent)
3517 return -EINVAL;
3518 analyze_sbs(mddev);
3521 chunk_size = mddev->chunk_size;
3523 if (chunk_size) {
3524 if (chunk_size > MAX_CHUNK_SIZE) {
3525 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3526 chunk_size, MAX_CHUNK_SIZE);
3527 return -EINVAL;
3530 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
3532 if ( (1 << ffz(~chunk_size)) != chunk_size) {
3533 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
3534 return -EINVAL;
3536 if (chunk_size < PAGE_SIZE) {
3537 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
3538 chunk_size, PAGE_SIZE);
3539 return -EINVAL;
3542 /* devices must have minimum size of one chunk */
3543 rdev_for_each(rdev, tmp, mddev) {
3544 if (test_bit(Faulty, &rdev->flags))
3545 continue;
3546 if (rdev->size < chunk_size / 1024) {
3547 printk(KERN_WARNING
3548 "md: Dev %s smaller than chunk_size:"
3549 " %lluk < %dk\n",
3550 bdevname(rdev->bdev,b),
3551 (unsigned long long)rdev->size,
3552 chunk_size / 1024);
3553 return -EINVAL;
3558 #ifdef CONFIG_KMOD
3559 if (mddev->level != LEVEL_NONE)
3560 request_module("md-level-%d", mddev->level);
3561 else if (mddev->clevel[0])
3562 request_module("md-%s", mddev->clevel);
3563 #endif
3566 * Drop all container device buffers, from now on
3567 * the only valid external interface is through the md
3568 * device.
3570 rdev_for_each(rdev, tmp, mddev) {
3571 if (test_bit(Faulty, &rdev->flags))
3572 continue;
3573 sync_blockdev(rdev->bdev);
3574 invalidate_bdev(rdev->bdev);
3576 /* perform some consistency tests on the device.
3577 * We don't want the data to overlap the metadata,
3578 * Internal Bitmap issues has handled elsewhere.
3580 if (rdev->data_offset < rdev->sb_start) {
3581 if (mddev->size &&
3582 rdev->data_offset + mddev->size*2
3583 > rdev->sb_start) {
3584 printk("md: %s: data overlaps metadata\n",
3585 mdname(mddev));
3586 return -EINVAL;
3588 } else {
3589 if (rdev->sb_start + rdev->sb_size/512
3590 > rdev->data_offset) {
3591 printk("md: %s: metadata overlaps data\n",
3592 mdname(mddev));
3593 return -EINVAL;
3596 sysfs_notify(&rdev->kobj, NULL, "state");
3599 md_probe(mddev->unit, NULL, NULL);
3600 disk = mddev->gendisk;
3601 if (!disk)
3602 return -ENOMEM;
3604 spin_lock(&pers_lock);
3605 pers = find_pers(mddev->level, mddev->clevel);
3606 if (!pers || !try_module_get(pers->owner)) {
3607 spin_unlock(&pers_lock);
3608 if (mddev->level != LEVEL_NONE)
3609 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3610 mddev->level);
3611 else
3612 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3613 mddev->clevel);
3614 return -EINVAL;
3616 mddev->pers = pers;
3617 spin_unlock(&pers_lock);
3618 mddev->level = pers->level;
3619 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3621 if (mddev->reshape_position != MaxSector &&
3622 pers->start_reshape == NULL) {
3623 /* This personality cannot handle reshaping... */
3624 mddev->pers = NULL;
3625 module_put(pers->owner);
3626 return -EINVAL;
3629 if (pers->sync_request) {
3630 /* Warn if this is a potentially silly
3631 * configuration.
3633 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3634 mdk_rdev_t *rdev2;
3635 struct list_head *tmp2;
3636 int warned = 0;
3637 rdev_for_each(rdev, tmp, mddev) {
3638 rdev_for_each(rdev2, tmp2, mddev) {
3639 if (rdev < rdev2 &&
3640 rdev->bdev->bd_contains ==
3641 rdev2->bdev->bd_contains) {
3642 printk(KERN_WARNING
3643 "%s: WARNING: %s appears to be"
3644 " on the same physical disk as"
3645 " %s.\n",
3646 mdname(mddev),
3647 bdevname(rdev->bdev,b),
3648 bdevname(rdev2->bdev,b2));
3649 warned = 1;
3653 if (warned)
3654 printk(KERN_WARNING
3655 "True protection against single-disk"
3656 " failure might be compromised.\n");
3659 mddev->recovery = 0;
3660 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
3661 mddev->barriers_work = 1;
3662 mddev->ok_start_degraded = start_dirty_degraded;
3664 if (start_readonly)
3665 mddev->ro = 2; /* read-only, but switch on first write */
3667 err = mddev->pers->run(mddev);
3668 if (err)
3669 printk(KERN_ERR "md: pers->run() failed ...\n");
3670 else if (mddev->pers->sync_request) {
3671 err = bitmap_create(mddev);
3672 if (err) {
3673 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3674 mdname(mddev), err);
3675 mddev->pers->stop(mddev);
3678 if (err) {
3679 module_put(mddev->pers->owner);
3680 mddev->pers = NULL;
3681 bitmap_destroy(mddev);
3682 return err;
3684 if (mddev->pers->sync_request) {
3685 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3686 printk(KERN_WARNING
3687 "md: cannot register extra attributes for %s\n",
3688 mdname(mddev));
3689 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
3690 mddev->ro = 0;
3692 atomic_set(&mddev->writes_pending,0);
3693 mddev->safemode = 0;
3694 mddev->safemode_timer.function = md_safemode_timeout;
3695 mddev->safemode_timer.data = (unsigned long) mddev;
3696 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
3697 mddev->in_sync = 1;
3699 rdev_for_each(rdev, tmp, mddev)
3700 if (rdev->raid_disk >= 0) {
3701 char nm[20];
3702 sprintf(nm, "rd%d", rdev->raid_disk);
3703 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3704 printk("md: cannot register %s for %s\n",
3705 nm, mdname(mddev));
3708 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3710 if (mddev->flags)
3711 md_update_sb(mddev, 0);
3713 set_capacity(disk, mddev->array_sectors);
3715 /* If we call blk_queue_make_request here, it will
3716 * re-initialise max_sectors etc which may have been
3717 * refined inside -> run. So just set the bits we need to set.
3718 * Most initialisation happended when we called
3719 * blk_queue_make_request(..., md_fail_request)
3720 * earlier.
3722 mddev->queue->queuedata = mddev;
3723 mddev->queue->make_request_fn = mddev->pers->make_request;
3725 /* If there is a partially-recovered drive we need to
3726 * start recovery here. If we leave it to md_check_recovery,
3727 * it will remove the drives and not do the right thing
3729 if (mddev->degraded && !mddev->sync_thread) {
3730 struct list_head *rtmp;
3731 int spares = 0;
3732 rdev_for_each(rdev, rtmp, mddev)
3733 if (rdev->raid_disk >= 0 &&
3734 !test_bit(In_sync, &rdev->flags) &&
3735 !test_bit(Faulty, &rdev->flags))
3736 /* complete an interrupted recovery */
3737 spares++;
3738 if (spares && mddev->pers->sync_request) {
3739 mddev->recovery = 0;
3740 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3741 mddev->sync_thread = md_register_thread(md_do_sync,
3742 mddev,
3743 "%s_resync");
3744 if (!mddev->sync_thread) {
3745 printk(KERN_ERR "%s: could not start resync"
3746 " thread...\n",
3747 mdname(mddev));
3748 /* leave the spares where they are, it shouldn't hurt */
3749 mddev->recovery = 0;
3753 md_wakeup_thread(mddev->thread);
3754 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
3756 mddev->changed = 1;
3757 md_new_event(mddev);
3758 sysfs_notify(&mddev->kobj, NULL, "array_state");
3759 sysfs_notify(&mddev->kobj, NULL, "sync_action");
3760 sysfs_notify(&mddev->kobj, NULL, "degraded");
3761 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
3762 return 0;
3765 static int restart_array(mddev_t *mddev)
3767 struct gendisk *disk = mddev->gendisk;
3769 /* Complain if it has no devices */
3770 if (list_empty(&mddev->disks))
3771 return -ENXIO;
3772 if (!mddev->pers)
3773 return -EINVAL;
3774 if (!mddev->ro)
3775 return -EBUSY;
3776 mddev->safemode = 0;
3777 mddev->ro = 0;
3778 set_disk_ro(disk, 0);
3779 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3780 mdname(mddev));
3781 /* Kick recovery or resync if necessary */
3782 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3783 md_wakeup_thread(mddev->thread);
3784 md_wakeup_thread(mddev->sync_thread);
3785 sysfs_notify(&mddev->kobj, NULL, "array_state");
3786 return 0;
3789 /* similar to deny_write_access, but accounts for our holding a reference
3790 * to the file ourselves */
3791 static int deny_bitmap_write_access(struct file * file)
3793 struct inode *inode = file->f_mapping->host;
3795 spin_lock(&inode->i_lock);
3796 if (atomic_read(&inode->i_writecount) > 1) {
3797 spin_unlock(&inode->i_lock);
3798 return -ETXTBSY;
3800 atomic_set(&inode->i_writecount, -1);
3801 spin_unlock(&inode->i_lock);
3803 return 0;
3806 static void restore_bitmap_write_access(struct file *file)
3808 struct inode *inode = file->f_mapping->host;
3810 spin_lock(&inode->i_lock);
3811 atomic_set(&inode->i_writecount, 1);
3812 spin_unlock(&inode->i_lock);
3815 /* mode:
3816 * 0 - completely stop and dis-assemble array
3817 * 1 - switch to readonly
3818 * 2 - stop but do not disassemble array
3820 static int do_md_stop(mddev_t * mddev, int mode, int is_open)
3822 int err = 0;
3823 struct gendisk *disk = mddev->gendisk;
3825 if (atomic_read(&mddev->openers) > is_open) {
3826 printk("md: %s still in use.\n",mdname(mddev));
3827 return -EBUSY;
3830 if (mddev->pers) {
3832 if (mddev->sync_thread) {
3833 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3834 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3835 md_unregister_thread(mddev->sync_thread);
3836 mddev->sync_thread = NULL;
3839 del_timer_sync(&mddev->safemode_timer);
3841 switch(mode) {
3842 case 1: /* readonly */
3843 err = -ENXIO;
3844 if (mddev->ro==1)
3845 goto out;
3846 mddev->ro = 1;
3847 break;
3848 case 0: /* disassemble */
3849 case 2: /* stop */
3850 bitmap_flush(mddev);
3851 md_super_wait(mddev);
3852 if (mddev->ro)
3853 set_disk_ro(disk, 0);
3854 blk_queue_make_request(mddev->queue, md_fail_request);
3855 mddev->pers->stop(mddev);
3856 mddev->queue->merge_bvec_fn = NULL;
3857 mddev->queue->unplug_fn = NULL;
3858 mddev->queue->backing_dev_info.congested_fn = NULL;
3859 if (mddev->pers->sync_request)
3860 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3862 module_put(mddev->pers->owner);
3863 mddev->pers = NULL;
3864 /* tell userspace to handle 'inactive' */
3865 sysfs_notify(&mddev->kobj, NULL, "array_state");
3867 set_capacity(disk, 0);
3868 mddev->changed = 1;
3870 if (mddev->ro)
3871 mddev->ro = 0;
3873 if (!mddev->in_sync || mddev->flags) {
3874 /* mark array as shutdown cleanly */
3875 mddev->in_sync = 1;
3876 md_update_sb(mddev, 1);
3878 if (mode == 1)
3879 set_disk_ro(disk, 1);
3880 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3884 * Free resources if final stop
3886 if (mode == 0) {
3887 mdk_rdev_t *rdev;
3888 struct list_head *tmp;
3890 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
3892 bitmap_destroy(mddev);
3893 if (mddev->bitmap_file) {
3894 restore_bitmap_write_access(mddev->bitmap_file);
3895 fput(mddev->bitmap_file);
3896 mddev->bitmap_file = NULL;
3898 mddev->bitmap_offset = 0;
3900 rdev_for_each(rdev, tmp, mddev)
3901 if (rdev->raid_disk >= 0) {
3902 char nm[20];
3903 sprintf(nm, "rd%d", rdev->raid_disk);
3904 sysfs_remove_link(&mddev->kobj, nm);
3907 /* make sure all md_delayed_delete calls have finished */
3908 flush_scheduled_work();
3910 export_array(mddev);
3912 mddev->array_sectors = 0;
3913 mddev->size = 0;
3914 mddev->raid_disks = 0;
3915 mddev->recovery_cp = 0;
3916 mddev->resync_min = 0;
3917 mddev->resync_max = MaxSector;
3918 mddev->reshape_position = MaxSector;
3919 mddev->external = 0;
3920 mddev->persistent = 0;
3921 mddev->level = LEVEL_NONE;
3922 mddev->clevel[0] = 0;
3923 mddev->flags = 0;
3924 mddev->ro = 0;
3925 mddev->metadata_type[0] = 0;
3926 mddev->chunk_size = 0;
3927 mddev->ctime = mddev->utime = 0;
3928 mddev->layout = 0;
3929 mddev->max_disks = 0;
3930 mddev->events = 0;
3931 mddev->delta_disks = 0;
3932 mddev->new_level = LEVEL_NONE;
3933 mddev->new_layout = 0;
3934 mddev->new_chunk = 0;
3935 mddev->curr_resync = 0;
3936 mddev->resync_mismatches = 0;
3937 mddev->suspend_lo = mddev->suspend_hi = 0;
3938 mddev->sync_speed_min = mddev->sync_speed_max = 0;
3939 mddev->recovery = 0;
3940 mddev->in_sync = 0;
3941 mddev->changed = 0;
3942 mddev->degraded = 0;
3943 mddev->barriers_work = 0;
3944 mddev->safemode = 0;
3946 } else if (mddev->pers)
3947 printk(KERN_INFO "md: %s switched to read-only mode.\n",
3948 mdname(mddev));
3949 err = 0;
3950 md_new_event(mddev);
3951 sysfs_notify(&mddev->kobj, NULL, "array_state");
3952 out:
3953 return err;
3956 #ifndef MODULE
3957 static void autorun_array(mddev_t *mddev)
3959 mdk_rdev_t *rdev;
3960 struct list_head *tmp;
3961 int err;
3963 if (list_empty(&mddev->disks))
3964 return;
3966 printk(KERN_INFO "md: running: ");
3968 rdev_for_each(rdev, tmp, mddev) {
3969 char b[BDEVNAME_SIZE];
3970 printk("<%s>", bdevname(rdev->bdev,b));
3972 printk("\n");
3974 err = do_md_run (mddev);
3975 if (err) {
3976 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
3977 do_md_stop (mddev, 0, 0);
3982 * lets try to run arrays based on all disks that have arrived
3983 * until now. (those are in pending_raid_disks)
3985 * the method: pick the first pending disk, collect all disks with
3986 * the same UUID, remove all from the pending list and put them into
3987 * the 'same_array' list. Then order this list based on superblock
3988 * update time (freshest comes first), kick out 'old' disks and
3989 * compare superblocks. If everything's fine then run it.
3991 * If "unit" is allocated, then bump its reference count
3993 static void autorun_devices(int part)
3995 struct list_head *tmp;
3996 mdk_rdev_t *rdev0, *rdev;
3997 mddev_t *mddev;
3998 char b[BDEVNAME_SIZE];
4000 printk(KERN_INFO "md: autorun ...\n");
4001 while (!list_empty(&pending_raid_disks)) {
4002 int unit;
4003 dev_t dev;
4004 LIST_HEAD(candidates);
4005 rdev0 = list_entry(pending_raid_disks.next,
4006 mdk_rdev_t, same_set);
4008 printk(KERN_INFO "md: considering %s ...\n",
4009 bdevname(rdev0->bdev,b));
4010 INIT_LIST_HEAD(&candidates);
4011 rdev_for_each_list(rdev, tmp, pending_raid_disks)
4012 if (super_90_load(rdev, rdev0, 0) >= 0) {
4013 printk(KERN_INFO "md: adding %s ...\n",
4014 bdevname(rdev->bdev,b));
4015 list_move(&rdev->same_set, &candidates);
4018 * now we have a set of devices, with all of them having
4019 * mostly sane superblocks. It's time to allocate the
4020 * mddev.
4022 if (part) {
4023 dev = MKDEV(mdp_major,
4024 rdev0->preferred_minor << MdpMinorShift);
4025 unit = MINOR(dev) >> MdpMinorShift;
4026 } else {
4027 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4028 unit = MINOR(dev);
4030 if (rdev0->preferred_minor != unit) {
4031 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4032 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4033 break;
4036 md_probe(dev, NULL, NULL);
4037 mddev = mddev_find(dev);
4038 if (!mddev || !mddev->gendisk) {
4039 if (mddev)
4040 mddev_put(mddev);
4041 printk(KERN_ERR
4042 "md: cannot allocate memory for md drive.\n");
4043 break;
4045 if (mddev_lock(mddev))
4046 printk(KERN_WARNING "md: %s locked, cannot run\n",
4047 mdname(mddev));
4048 else if (mddev->raid_disks || mddev->major_version
4049 || !list_empty(&mddev->disks)) {
4050 printk(KERN_WARNING
4051 "md: %s already running, cannot run %s\n",
4052 mdname(mddev), bdevname(rdev0->bdev,b));
4053 mddev_unlock(mddev);
4054 } else {
4055 printk(KERN_INFO "md: created %s\n", mdname(mddev));
4056 mddev->persistent = 1;
4057 rdev_for_each_list(rdev, tmp, candidates) {
4058 list_del_init(&rdev->same_set);
4059 if (bind_rdev_to_array(rdev, mddev))
4060 export_rdev(rdev);
4062 autorun_array(mddev);
4063 mddev_unlock(mddev);
4065 /* on success, candidates will be empty, on error
4066 * it won't...
4068 rdev_for_each_list(rdev, tmp, candidates) {
4069 list_del_init(&rdev->same_set);
4070 export_rdev(rdev);
4072 mddev_put(mddev);
4074 printk(KERN_INFO "md: ... autorun DONE.\n");
4076 #endif /* !MODULE */
4078 static int get_version(void __user * arg)
4080 mdu_version_t ver;
4082 ver.major = MD_MAJOR_VERSION;
4083 ver.minor = MD_MINOR_VERSION;
4084 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4086 if (copy_to_user(arg, &ver, sizeof(ver)))
4087 return -EFAULT;
4089 return 0;
4092 static int get_array_info(mddev_t * mddev, void __user * arg)
4094 mdu_array_info_t info;
4095 int nr,working,active,failed,spare;
4096 mdk_rdev_t *rdev;
4097 struct list_head *tmp;
4099 nr=working=active=failed=spare=0;
4100 rdev_for_each(rdev, tmp, mddev) {
4101 nr++;
4102 if (test_bit(Faulty, &rdev->flags))
4103 failed++;
4104 else {
4105 working++;
4106 if (test_bit(In_sync, &rdev->flags))
4107 active++;
4108 else
4109 spare++;
4113 info.major_version = mddev->major_version;
4114 info.minor_version = mddev->minor_version;
4115 info.patch_version = MD_PATCHLEVEL_VERSION;
4116 info.ctime = mddev->ctime;
4117 info.level = mddev->level;
4118 info.size = mddev->size;
4119 if (info.size != mddev->size) /* overflow */
4120 info.size = -1;
4121 info.nr_disks = nr;
4122 info.raid_disks = mddev->raid_disks;
4123 info.md_minor = mddev->md_minor;
4124 info.not_persistent= !mddev->persistent;
4126 info.utime = mddev->utime;
4127 info.state = 0;
4128 if (mddev->in_sync)
4129 info.state = (1<<MD_SB_CLEAN);
4130 if (mddev->bitmap && mddev->bitmap_offset)
4131 info.state = (1<<MD_SB_BITMAP_PRESENT);
4132 info.active_disks = active;
4133 info.working_disks = working;
4134 info.failed_disks = failed;
4135 info.spare_disks = spare;
4137 info.layout = mddev->layout;
4138 info.chunk_size = mddev->chunk_size;
4140 if (copy_to_user(arg, &info, sizeof(info)))
4141 return -EFAULT;
4143 return 0;
4146 static int get_bitmap_file(mddev_t * mddev, void __user * arg)
4148 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4149 char *ptr, *buf = NULL;
4150 int err = -ENOMEM;
4152 if (md_allow_write(mddev))
4153 file = kmalloc(sizeof(*file), GFP_NOIO);
4154 else
4155 file = kmalloc(sizeof(*file), GFP_KERNEL);
4157 if (!file)
4158 goto out;
4160 /* bitmap disabled, zero the first byte and copy out */
4161 if (!mddev->bitmap || !mddev->bitmap->file) {
4162 file->pathname[0] = '\0';
4163 goto copy_out;
4166 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4167 if (!buf)
4168 goto out;
4170 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4171 if (IS_ERR(ptr))
4172 goto out;
4174 strcpy(file->pathname, ptr);
4176 copy_out:
4177 err = 0;
4178 if (copy_to_user(arg, file, sizeof(*file)))
4179 err = -EFAULT;
4180 out:
4181 kfree(buf);
4182 kfree(file);
4183 return err;
4186 static int get_disk_info(mddev_t * mddev, void __user * arg)
4188 mdu_disk_info_t info;
4189 mdk_rdev_t *rdev;
4191 if (copy_from_user(&info, arg, sizeof(info)))
4192 return -EFAULT;
4194 rdev = find_rdev_nr(mddev, info.number);
4195 if (rdev) {
4196 info.major = MAJOR(rdev->bdev->bd_dev);
4197 info.minor = MINOR(rdev->bdev->bd_dev);
4198 info.raid_disk = rdev->raid_disk;
4199 info.state = 0;
4200 if (test_bit(Faulty, &rdev->flags))
4201 info.state |= (1<<MD_DISK_FAULTY);
4202 else if (test_bit(In_sync, &rdev->flags)) {
4203 info.state |= (1<<MD_DISK_ACTIVE);
4204 info.state |= (1<<MD_DISK_SYNC);
4206 if (test_bit(WriteMostly, &rdev->flags))
4207 info.state |= (1<<MD_DISK_WRITEMOSTLY);
4208 } else {
4209 info.major = info.minor = 0;
4210 info.raid_disk = -1;
4211 info.state = (1<<MD_DISK_REMOVED);
4214 if (copy_to_user(arg, &info, sizeof(info)))
4215 return -EFAULT;
4217 return 0;
4220 static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4222 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4223 mdk_rdev_t *rdev;
4224 dev_t dev = MKDEV(info->major,info->minor);
4226 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4227 return -EOVERFLOW;
4229 if (!mddev->raid_disks) {
4230 int err;
4231 /* expecting a device which has a superblock */
4232 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4233 if (IS_ERR(rdev)) {
4234 printk(KERN_WARNING
4235 "md: md_import_device returned %ld\n",
4236 PTR_ERR(rdev));
4237 return PTR_ERR(rdev);
4239 if (!list_empty(&mddev->disks)) {
4240 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4241 mdk_rdev_t, same_set);
4242 int err = super_types[mddev->major_version]
4243 .load_super(rdev, rdev0, mddev->minor_version);
4244 if (err < 0) {
4245 printk(KERN_WARNING
4246 "md: %s has different UUID to %s\n",
4247 bdevname(rdev->bdev,b),
4248 bdevname(rdev0->bdev,b2));
4249 export_rdev(rdev);
4250 return -EINVAL;
4253 err = bind_rdev_to_array(rdev, mddev);
4254 if (err)
4255 export_rdev(rdev);
4256 return err;
4260 * add_new_disk can be used once the array is assembled
4261 * to add "hot spares". They must already have a superblock
4262 * written
4264 if (mddev->pers) {
4265 int err;
4266 if (!mddev->pers->hot_add_disk) {
4267 printk(KERN_WARNING
4268 "%s: personality does not support diskops!\n",
4269 mdname(mddev));
4270 return -EINVAL;
4272 if (mddev->persistent)
4273 rdev = md_import_device(dev, mddev->major_version,
4274 mddev->minor_version);
4275 else
4276 rdev = md_import_device(dev, -1, -1);
4277 if (IS_ERR(rdev)) {
4278 printk(KERN_WARNING
4279 "md: md_import_device returned %ld\n",
4280 PTR_ERR(rdev));
4281 return PTR_ERR(rdev);
4283 /* set save_raid_disk if appropriate */
4284 if (!mddev->persistent) {
4285 if (info->state & (1<<MD_DISK_SYNC) &&
4286 info->raid_disk < mddev->raid_disks)
4287 rdev->raid_disk = info->raid_disk;
4288 else
4289 rdev->raid_disk = -1;
4290 } else
4291 super_types[mddev->major_version].
4292 validate_super(mddev, rdev);
4293 rdev->saved_raid_disk = rdev->raid_disk;
4295 clear_bit(In_sync, &rdev->flags); /* just to be sure */
4296 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4297 set_bit(WriteMostly, &rdev->flags);
4299 rdev->raid_disk = -1;
4300 err = bind_rdev_to_array(rdev, mddev);
4301 if (!err && !mddev->pers->hot_remove_disk) {
4302 /* If there is hot_add_disk but no hot_remove_disk
4303 * then added disks for geometry changes,
4304 * and should be added immediately.
4306 super_types[mddev->major_version].
4307 validate_super(mddev, rdev);
4308 err = mddev->pers->hot_add_disk(mddev, rdev);
4309 if (err)
4310 unbind_rdev_from_array(rdev);
4312 if (err)
4313 export_rdev(rdev);
4314 else
4315 sysfs_notify(&rdev->kobj, NULL, "state");
4317 md_update_sb(mddev, 1);
4318 if (mddev->degraded)
4319 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4320 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4321 md_wakeup_thread(mddev->thread);
4322 return err;
4325 /* otherwise, add_new_disk is only allowed
4326 * for major_version==0 superblocks
4328 if (mddev->major_version != 0) {
4329 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4330 mdname(mddev));
4331 return -EINVAL;
4334 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4335 int err;
4336 rdev = md_import_device (dev, -1, 0);
4337 if (IS_ERR(rdev)) {
4338 printk(KERN_WARNING
4339 "md: error, md_import_device() returned %ld\n",
4340 PTR_ERR(rdev));
4341 return PTR_ERR(rdev);
4343 rdev->desc_nr = info->number;
4344 if (info->raid_disk < mddev->raid_disks)
4345 rdev->raid_disk = info->raid_disk;
4346 else
4347 rdev->raid_disk = -1;
4349 if (rdev->raid_disk < mddev->raid_disks)
4350 if (info->state & (1<<MD_DISK_SYNC))
4351 set_bit(In_sync, &rdev->flags);
4353 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4354 set_bit(WriteMostly, &rdev->flags);
4356 if (!mddev->persistent) {
4357 printk(KERN_INFO "md: nonpersistent superblock ...\n");
4358 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
4359 } else
4360 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
4361 rdev->size = calc_num_sectors(rdev, mddev->chunk_size) / 2;
4363 err = bind_rdev_to_array(rdev, mddev);
4364 if (err) {
4365 export_rdev(rdev);
4366 return err;
4370 return 0;
4373 static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4375 char b[BDEVNAME_SIZE];
4376 mdk_rdev_t *rdev;
4378 rdev = find_rdev(mddev, dev);
4379 if (!rdev)
4380 return -ENXIO;
4382 if (rdev->raid_disk >= 0)
4383 goto busy;
4385 kick_rdev_from_array(rdev);
4386 md_update_sb(mddev, 1);
4387 md_new_event(mddev);
4389 return 0;
4390 busy:
4391 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
4392 bdevname(rdev->bdev,b), mdname(mddev));
4393 return -EBUSY;
4396 static int hot_add_disk(mddev_t * mddev, dev_t dev)
4398 char b[BDEVNAME_SIZE];
4399 int err;
4400 mdk_rdev_t *rdev;
4402 if (!mddev->pers)
4403 return -ENODEV;
4405 if (mddev->major_version != 0) {
4406 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4407 " version-0 superblocks.\n",
4408 mdname(mddev));
4409 return -EINVAL;
4411 if (!mddev->pers->hot_add_disk) {
4412 printk(KERN_WARNING
4413 "%s: personality does not support diskops!\n",
4414 mdname(mddev));
4415 return -EINVAL;
4418 rdev = md_import_device (dev, -1, 0);
4419 if (IS_ERR(rdev)) {
4420 printk(KERN_WARNING
4421 "md: error, md_import_device() returned %ld\n",
4422 PTR_ERR(rdev));
4423 return -EINVAL;
4426 if (mddev->persistent)
4427 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
4428 else
4429 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
4431 rdev->size = calc_num_sectors(rdev, mddev->chunk_size) / 2;
4433 if (test_bit(Faulty, &rdev->flags)) {
4434 printk(KERN_WARNING
4435 "md: can not hot-add faulty %s disk to %s!\n",
4436 bdevname(rdev->bdev,b), mdname(mddev));
4437 err = -EINVAL;
4438 goto abort_export;
4440 clear_bit(In_sync, &rdev->flags);
4441 rdev->desc_nr = -1;
4442 rdev->saved_raid_disk = -1;
4443 err = bind_rdev_to_array(rdev, mddev);
4444 if (err)
4445 goto abort_export;
4448 * The rest should better be atomic, we can have disk failures
4449 * noticed in interrupt contexts ...
4452 if (rdev->desc_nr == mddev->max_disks) {
4453 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
4454 mdname(mddev));
4455 err = -EBUSY;
4456 goto abort_unbind_export;
4459 rdev->raid_disk = -1;
4461 md_update_sb(mddev, 1);
4464 * Kick recovery, maybe this spare has to be added to the
4465 * array immediately.
4467 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4468 md_wakeup_thread(mddev->thread);
4469 md_new_event(mddev);
4470 return 0;
4472 abort_unbind_export:
4473 unbind_rdev_from_array(rdev);
4475 abort_export:
4476 export_rdev(rdev);
4477 return err;
4480 static int set_bitmap_file(mddev_t *mddev, int fd)
4482 int err;
4484 if (mddev->pers) {
4485 if (!mddev->pers->quiesce)
4486 return -EBUSY;
4487 if (mddev->recovery || mddev->sync_thread)
4488 return -EBUSY;
4489 /* we should be able to change the bitmap.. */
4493 if (fd >= 0) {
4494 if (mddev->bitmap)
4495 return -EEXIST; /* cannot add when bitmap is present */
4496 mddev->bitmap_file = fget(fd);
4498 if (mddev->bitmap_file == NULL) {
4499 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4500 mdname(mddev));
4501 return -EBADF;
4504 err = deny_bitmap_write_access(mddev->bitmap_file);
4505 if (err) {
4506 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4507 mdname(mddev));
4508 fput(mddev->bitmap_file);
4509 mddev->bitmap_file = NULL;
4510 return err;
4512 mddev->bitmap_offset = 0; /* file overrides offset */
4513 } else if (mddev->bitmap == NULL)
4514 return -ENOENT; /* cannot remove what isn't there */
4515 err = 0;
4516 if (mddev->pers) {
4517 mddev->pers->quiesce(mddev, 1);
4518 if (fd >= 0)
4519 err = bitmap_create(mddev);
4520 if (fd < 0 || err) {
4521 bitmap_destroy(mddev);
4522 fd = -1; /* make sure to put the file */
4524 mddev->pers->quiesce(mddev, 0);
4526 if (fd < 0) {
4527 if (mddev->bitmap_file) {
4528 restore_bitmap_write_access(mddev->bitmap_file);
4529 fput(mddev->bitmap_file);
4531 mddev->bitmap_file = NULL;
4534 return err;
4538 * set_array_info is used two different ways
4539 * The original usage is when creating a new array.
4540 * In this usage, raid_disks is > 0 and it together with
4541 * level, size, not_persistent,layout,chunksize determine the
4542 * shape of the array.
4543 * This will always create an array with a type-0.90.0 superblock.
4544 * The newer usage is when assembling an array.
4545 * In this case raid_disks will be 0, and the major_version field is
4546 * use to determine which style super-blocks are to be found on the devices.
4547 * The minor and patch _version numbers are also kept incase the
4548 * super_block handler wishes to interpret them.
4550 static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
4553 if (info->raid_disks == 0) {
4554 /* just setting version number for superblock loading */
4555 if (info->major_version < 0 ||
4556 info->major_version >= ARRAY_SIZE(super_types) ||
4557 super_types[info->major_version].name == NULL) {
4558 /* maybe try to auto-load a module? */
4559 printk(KERN_INFO
4560 "md: superblock version %d not known\n",
4561 info->major_version);
4562 return -EINVAL;
4564 mddev->major_version = info->major_version;
4565 mddev->minor_version = info->minor_version;
4566 mddev->patch_version = info->patch_version;
4567 mddev->persistent = !info->not_persistent;
4568 return 0;
4570 mddev->major_version = MD_MAJOR_VERSION;
4571 mddev->minor_version = MD_MINOR_VERSION;
4572 mddev->patch_version = MD_PATCHLEVEL_VERSION;
4573 mddev->ctime = get_seconds();
4575 mddev->level = info->level;
4576 mddev->clevel[0] = 0;
4577 mddev->size = info->size;
4578 mddev->raid_disks = info->raid_disks;
4579 /* don't set md_minor, it is determined by which /dev/md* was
4580 * openned
4582 if (info->state & (1<<MD_SB_CLEAN))
4583 mddev->recovery_cp = MaxSector;
4584 else
4585 mddev->recovery_cp = 0;
4586 mddev->persistent = ! info->not_persistent;
4587 mddev->external = 0;
4589 mddev->layout = info->layout;
4590 mddev->chunk_size = info->chunk_size;
4592 mddev->max_disks = MD_SB_DISKS;
4594 if (mddev->persistent)
4595 mddev->flags = 0;
4596 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4598 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4599 mddev->bitmap_offset = 0;
4601 mddev->reshape_position = MaxSector;
4604 * Generate a 128 bit UUID
4606 get_random_bytes(mddev->uuid, 16);
4608 mddev->new_level = mddev->level;
4609 mddev->new_chunk = mddev->chunk_size;
4610 mddev->new_layout = mddev->layout;
4611 mddev->delta_disks = 0;
4613 return 0;
4616 static int update_size(mddev_t *mddev, sector_t num_sectors)
4618 mdk_rdev_t * rdev;
4619 int rv;
4620 struct list_head *tmp;
4621 int fit = (num_sectors == 0);
4623 if (mddev->pers->resize == NULL)
4624 return -EINVAL;
4625 /* The "num_sectors" is the number of sectors of each device that
4626 * is used. This can only make sense for arrays with redundancy.
4627 * linear and raid0 always use whatever space is available. We can only
4628 * consider changing this number if no resync or reconstruction is
4629 * happening, and if the new size is acceptable. It must fit before the
4630 * sb_start or, if that is <data_offset, it must fit before the size
4631 * of each device. If num_sectors is zero, we find the largest size
4632 * that fits.
4635 if (mddev->sync_thread)
4636 return -EBUSY;
4637 if (mddev->bitmap)
4638 /* Sorry, cannot grow a bitmap yet, just remove it,
4639 * grow, and re-add.
4641 return -EBUSY;
4642 rdev_for_each(rdev, tmp, mddev) {
4643 sector_t avail;
4644 avail = rdev->size * 2;
4646 if (fit && (num_sectors == 0 || num_sectors > avail))
4647 num_sectors = avail;
4648 if (avail < num_sectors)
4649 return -ENOSPC;
4651 rv = mddev->pers->resize(mddev, num_sectors);
4652 if (!rv) {
4653 struct block_device *bdev;
4655 bdev = bdget_disk(mddev->gendisk, 0);
4656 if (bdev) {
4657 mutex_lock(&bdev->bd_inode->i_mutex);
4658 i_size_write(bdev->bd_inode,
4659 (loff_t)mddev->array_sectors << 9);
4660 mutex_unlock(&bdev->bd_inode->i_mutex);
4661 bdput(bdev);
4664 return rv;
4667 static int update_raid_disks(mddev_t *mddev, int raid_disks)
4669 int rv;
4670 /* change the number of raid disks */
4671 if (mddev->pers->check_reshape == NULL)
4672 return -EINVAL;
4673 if (raid_disks <= 0 ||
4674 raid_disks >= mddev->max_disks)
4675 return -EINVAL;
4676 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
4677 return -EBUSY;
4678 mddev->delta_disks = raid_disks - mddev->raid_disks;
4680 rv = mddev->pers->check_reshape(mddev);
4681 return rv;
4686 * update_array_info is used to change the configuration of an
4687 * on-line array.
4688 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4689 * fields in the info are checked against the array.
4690 * Any differences that cannot be handled will cause an error.
4691 * Normally, only one change can be managed at a time.
4693 static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4695 int rv = 0;
4696 int cnt = 0;
4697 int state = 0;
4699 /* calculate expected state,ignoring low bits */
4700 if (mddev->bitmap && mddev->bitmap_offset)
4701 state |= (1 << MD_SB_BITMAP_PRESENT);
4703 if (mddev->major_version != info->major_version ||
4704 mddev->minor_version != info->minor_version ||
4705 /* mddev->patch_version != info->patch_version || */
4706 mddev->ctime != info->ctime ||
4707 mddev->level != info->level ||
4708 /* mddev->layout != info->layout || */
4709 !mddev->persistent != info->not_persistent||
4710 mddev->chunk_size != info->chunk_size ||
4711 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4712 ((state^info->state) & 0xfffffe00)
4714 return -EINVAL;
4715 /* Check there is only one change */
4716 if (info->size >= 0 && mddev->size != info->size) cnt++;
4717 if (mddev->raid_disks != info->raid_disks) cnt++;
4718 if (mddev->layout != info->layout) cnt++;
4719 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
4720 if (cnt == 0) return 0;
4721 if (cnt > 1) return -EINVAL;
4723 if (mddev->layout != info->layout) {
4724 /* Change layout
4725 * we don't need to do anything at the md level, the
4726 * personality will take care of it all.
4728 if (mddev->pers->reconfig == NULL)
4729 return -EINVAL;
4730 else
4731 return mddev->pers->reconfig(mddev, info->layout, -1);
4733 if (info->size >= 0 && mddev->size != info->size)
4734 rv = update_size(mddev, (sector_t)info->size * 2);
4736 if (mddev->raid_disks != info->raid_disks)
4737 rv = update_raid_disks(mddev, info->raid_disks);
4739 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4740 if (mddev->pers->quiesce == NULL)
4741 return -EINVAL;
4742 if (mddev->recovery || mddev->sync_thread)
4743 return -EBUSY;
4744 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4745 /* add the bitmap */
4746 if (mddev->bitmap)
4747 return -EEXIST;
4748 if (mddev->default_bitmap_offset == 0)
4749 return -EINVAL;
4750 mddev->bitmap_offset = mddev->default_bitmap_offset;
4751 mddev->pers->quiesce(mddev, 1);
4752 rv = bitmap_create(mddev);
4753 if (rv)
4754 bitmap_destroy(mddev);
4755 mddev->pers->quiesce(mddev, 0);
4756 } else {
4757 /* remove the bitmap */
4758 if (!mddev->bitmap)
4759 return -ENOENT;
4760 if (mddev->bitmap->file)
4761 return -EINVAL;
4762 mddev->pers->quiesce(mddev, 1);
4763 bitmap_destroy(mddev);
4764 mddev->pers->quiesce(mddev, 0);
4765 mddev->bitmap_offset = 0;
4768 md_update_sb(mddev, 1);
4769 return rv;
4772 static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4774 mdk_rdev_t *rdev;
4776 if (mddev->pers == NULL)
4777 return -ENODEV;
4779 rdev = find_rdev(mddev, dev);
4780 if (!rdev)
4781 return -ENODEV;
4783 md_error(mddev, rdev);
4784 return 0;
4788 * We have a problem here : there is no easy way to give a CHS
4789 * virtual geometry. We currently pretend that we have a 2 heads
4790 * 4 sectors (with a BIG number of cylinders...). This drives
4791 * dosfs just mad... ;-)
4793 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4795 mddev_t *mddev = bdev->bd_disk->private_data;
4797 geo->heads = 2;
4798 geo->sectors = 4;
4799 geo->cylinders = get_capacity(mddev->gendisk) / 8;
4800 return 0;
4803 static int md_ioctl(struct inode *inode, struct file *file,
4804 unsigned int cmd, unsigned long arg)
4806 int err = 0;
4807 void __user *argp = (void __user *)arg;
4808 mddev_t *mddev = NULL;
4810 if (!capable(CAP_SYS_ADMIN))
4811 return -EACCES;
4814 * Commands dealing with the RAID driver but not any
4815 * particular array:
4817 switch (cmd)
4819 case RAID_VERSION:
4820 err = get_version(argp);
4821 goto done;
4823 case PRINT_RAID_DEBUG:
4824 err = 0;
4825 md_print_devices();
4826 goto done;
4828 #ifndef MODULE
4829 case RAID_AUTORUN:
4830 err = 0;
4831 autostart_arrays(arg);
4832 goto done;
4833 #endif
4834 default:;
4838 * Commands creating/starting a new array:
4841 mddev = inode->i_bdev->bd_disk->private_data;
4843 if (!mddev) {
4844 BUG();
4845 goto abort;
4848 err = mddev_lock(mddev);
4849 if (err) {
4850 printk(KERN_INFO
4851 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4852 err, cmd);
4853 goto abort;
4856 switch (cmd)
4858 case SET_ARRAY_INFO:
4860 mdu_array_info_t info;
4861 if (!arg)
4862 memset(&info, 0, sizeof(info));
4863 else if (copy_from_user(&info, argp, sizeof(info))) {
4864 err = -EFAULT;
4865 goto abort_unlock;
4867 if (mddev->pers) {
4868 err = update_array_info(mddev, &info);
4869 if (err) {
4870 printk(KERN_WARNING "md: couldn't update"
4871 " array info. %d\n", err);
4872 goto abort_unlock;
4874 goto done_unlock;
4876 if (!list_empty(&mddev->disks)) {
4877 printk(KERN_WARNING
4878 "md: array %s already has disks!\n",
4879 mdname(mddev));
4880 err = -EBUSY;
4881 goto abort_unlock;
4883 if (mddev->raid_disks) {
4884 printk(KERN_WARNING
4885 "md: array %s already initialised!\n",
4886 mdname(mddev));
4887 err = -EBUSY;
4888 goto abort_unlock;
4890 err = set_array_info(mddev, &info);
4891 if (err) {
4892 printk(KERN_WARNING "md: couldn't set"
4893 " array info. %d\n", err);
4894 goto abort_unlock;
4897 goto done_unlock;
4899 default:;
4903 * Commands querying/configuring an existing array:
4905 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
4906 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
4907 if ((!mddev->raid_disks && !mddev->external)
4908 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
4909 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
4910 && cmd != GET_BITMAP_FILE) {
4911 err = -ENODEV;
4912 goto abort_unlock;
4916 * Commands even a read-only array can execute:
4918 switch (cmd)
4920 case GET_ARRAY_INFO:
4921 err = get_array_info(mddev, argp);
4922 goto done_unlock;
4924 case GET_BITMAP_FILE:
4925 err = get_bitmap_file(mddev, argp);
4926 goto done_unlock;
4928 case GET_DISK_INFO:
4929 err = get_disk_info(mddev, argp);
4930 goto done_unlock;
4932 case RESTART_ARRAY_RW:
4933 err = restart_array(mddev);
4934 goto done_unlock;
4936 case STOP_ARRAY:
4937 err = do_md_stop (mddev, 0, 1);
4938 goto done_unlock;
4940 case STOP_ARRAY_RO:
4941 err = do_md_stop (mddev, 1, 1);
4942 goto done_unlock;
4947 * The remaining ioctls are changing the state of the
4948 * superblock, so we do not allow them on read-only arrays.
4949 * However non-MD ioctls (e.g. get-size) will still come through
4950 * here and hit the 'default' below, so only disallow
4951 * 'md' ioctls, and switch to rw mode if started auto-readonly.
4953 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
4954 if (mddev->ro == 2) {
4955 mddev->ro = 0;
4956 sysfs_notify(&mddev->kobj, NULL, "array_state");
4957 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4958 md_wakeup_thread(mddev->thread);
4959 } else {
4960 err = -EROFS;
4961 goto abort_unlock;
4965 switch (cmd)
4967 case ADD_NEW_DISK:
4969 mdu_disk_info_t info;
4970 if (copy_from_user(&info, argp, sizeof(info)))
4971 err = -EFAULT;
4972 else
4973 err = add_new_disk(mddev, &info);
4974 goto done_unlock;
4977 case HOT_REMOVE_DISK:
4978 err = hot_remove_disk(mddev, new_decode_dev(arg));
4979 goto done_unlock;
4981 case HOT_ADD_DISK:
4982 err = hot_add_disk(mddev, new_decode_dev(arg));
4983 goto done_unlock;
4985 case SET_DISK_FAULTY:
4986 err = set_disk_faulty(mddev, new_decode_dev(arg));
4987 goto done_unlock;
4989 case RUN_ARRAY:
4990 err = do_md_run (mddev);
4991 goto done_unlock;
4993 case SET_BITMAP_FILE:
4994 err = set_bitmap_file(mddev, (int)arg);
4995 goto done_unlock;
4997 default:
4998 err = -EINVAL;
4999 goto abort_unlock;
5002 done_unlock:
5003 abort_unlock:
5004 mddev_unlock(mddev);
5006 return err;
5007 done:
5008 if (err)
5009 MD_BUG();
5010 abort:
5011 return err;
5014 static int md_open(struct inode *inode, struct file *file)
5017 * Succeed if we can lock the mddev, which confirms that
5018 * it isn't being stopped right now.
5020 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
5021 int err;
5023 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
5024 goto out;
5026 err = 0;
5027 mddev_get(mddev);
5028 atomic_inc(&mddev->openers);
5029 mddev_unlock(mddev);
5031 check_disk_change(inode->i_bdev);
5032 out:
5033 return err;
5036 static int md_release(struct inode *inode, struct file * file)
5038 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
5040 BUG_ON(!mddev);
5041 atomic_dec(&mddev->openers);
5042 mddev_put(mddev);
5044 return 0;
5047 static int md_media_changed(struct gendisk *disk)
5049 mddev_t *mddev = disk->private_data;
5051 return mddev->changed;
5054 static int md_revalidate(struct gendisk *disk)
5056 mddev_t *mddev = disk->private_data;
5058 mddev->changed = 0;
5059 return 0;
5061 static struct block_device_operations md_fops =
5063 .owner = THIS_MODULE,
5064 .open = md_open,
5065 .release = md_release,
5066 .ioctl = md_ioctl,
5067 .getgeo = md_getgeo,
5068 .media_changed = md_media_changed,
5069 .revalidate_disk= md_revalidate,
5072 static int md_thread(void * arg)
5074 mdk_thread_t *thread = arg;
5077 * md_thread is a 'system-thread', it's priority should be very
5078 * high. We avoid resource deadlocks individually in each
5079 * raid personality. (RAID5 does preallocation) We also use RR and
5080 * the very same RT priority as kswapd, thus we will never get
5081 * into a priority inversion deadlock.
5083 * we definitely have to have equal or higher priority than
5084 * bdflush, otherwise bdflush will deadlock if there are too
5085 * many dirty RAID5 blocks.
5088 allow_signal(SIGKILL);
5089 while (!kthread_should_stop()) {
5091 /* We need to wait INTERRUPTIBLE so that
5092 * we don't add to the load-average.
5093 * That means we need to be sure no signals are
5094 * pending
5096 if (signal_pending(current))
5097 flush_signals(current);
5099 wait_event_interruptible_timeout
5100 (thread->wqueue,
5101 test_bit(THREAD_WAKEUP, &thread->flags)
5102 || kthread_should_stop(),
5103 thread->timeout);
5105 clear_bit(THREAD_WAKEUP, &thread->flags);
5107 thread->run(thread->mddev);
5110 return 0;
5113 void md_wakeup_thread(mdk_thread_t *thread)
5115 if (thread) {
5116 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5117 set_bit(THREAD_WAKEUP, &thread->flags);
5118 wake_up(&thread->wqueue);
5122 mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5123 const char *name)
5125 mdk_thread_t *thread;
5127 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
5128 if (!thread)
5129 return NULL;
5131 init_waitqueue_head(&thread->wqueue);
5133 thread->run = run;
5134 thread->mddev = mddev;
5135 thread->timeout = MAX_SCHEDULE_TIMEOUT;
5136 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
5137 if (IS_ERR(thread->tsk)) {
5138 kfree(thread);
5139 return NULL;
5141 return thread;
5144 void md_unregister_thread(mdk_thread_t *thread)
5146 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
5148 kthread_stop(thread->tsk);
5149 kfree(thread);
5152 void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5154 if (!mddev) {
5155 MD_BUG();
5156 return;
5159 if (!rdev || test_bit(Faulty, &rdev->flags))
5160 return;
5162 if (mddev->external)
5163 set_bit(Blocked, &rdev->flags);
5165 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5166 mdname(mddev),
5167 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5168 __builtin_return_address(0),__builtin_return_address(1),
5169 __builtin_return_address(2),__builtin_return_address(3));
5171 if (!mddev->pers)
5172 return;
5173 if (!mddev->pers->error_handler)
5174 return;
5175 mddev->pers->error_handler(mddev,rdev);
5176 if (mddev->degraded)
5177 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
5178 set_bit(StateChanged, &rdev->flags);
5179 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5180 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5181 md_wakeup_thread(mddev->thread);
5182 md_new_event_inintr(mddev);
5185 /* seq_file implementation /proc/mdstat */
5187 static void status_unused(struct seq_file *seq)
5189 int i = 0;
5190 mdk_rdev_t *rdev;
5191 struct list_head *tmp;
5193 seq_printf(seq, "unused devices: ");
5195 rdev_for_each_list(rdev, tmp, pending_raid_disks) {
5196 char b[BDEVNAME_SIZE];
5197 i++;
5198 seq_printf(seq, "%s ",
5199 bdevname(rdev->bdev,b));
5201 if (!i)
5202 seq_printf(seq, "<none>");
5204 seq_printf(seq, "\n");
5208 static void status_resync(struct seq_file *seq, mddev_t * mddev)
5210 sector_t max_blocks, resync, res;
5211 unsigned long dt, db, rt;
5212 int scale;
5213 unsigned int per_milli;
5215 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
5217 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5218 max_blocks = mddev->resync_max_sectors >> 1;
5219 else
5220 max_blocks = mddev->size;
5223 * Should not happen.
5225 if (!max_blocks) {
5226 MD_BUG();
5227 return;
5229 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5230 * in a sector_t, and (max_blocks>>scale) will fit in a
5231 * u32, as those are the requirements for sector_div.
5232 * Thus 'scale' must be at least 10
5234 scale = 10;
5235 if (sizeof(sector_t) > sizeof(unsigned long)) {
5236 while ( max_blocks/2 > (1ULL<<(scale+32)))
5237 scale++;
5239 res = (resync>>scale)*1000;
5240 sector_div(res, (u32)((max_blocks>>scale)+1));
5242 per_milli = res;
5244 int i, x = per_milli/50, y = 20-x;
5245 seq_printf(seq, "[");
5246 for (i = 0; i < x; i++)
5247 seq_printf(seq, "=");
5248 seq_printf(seq, ">");
5249 for (i = 0; i < y; i++)
5250 seq_printf(seq, ".");
5251 seq_printf(seq, "] ");
5253 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
5254 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5255 "reshape" :
5256 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5257 "check" :
5258 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5259 "resync" : "recovery"))),
5260 per_milli/10, per_milli % 10,
5261 (unsigned long long) resync,
5262 (unsigned long long) max_blocks);
5265 * We do not want to overflow, so the order of operands and
5266 * the * 100 / 100 trick are important. We do a +1 to be
5267 * safe against division by zero. We only estimate anyway.
5269 * dt: time from mark until now
5270 * db: blocks written from mark until now
5271 * rt: remaining time
5273 dt = ((jiffies - mddev->resync_mark) / HZ);
5274 if (!dt) dt++;
5275 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5276 - mddev->resync_mark_cnt;
5277 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100;
5279 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
5281 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
5284 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5286 struct list_head *tmp;
5287 loff_t l = *pos;
5288 mddev_t *mddev;
5290 if (l >= 0x10000)
5291 return NULL;
5292 if (!l--)
5293 /* header */
5294 return (void*)1;
5296 spin_lock(&all_mddevs_lock);
5297 list_for_each(tmp,&all_mddevs)
5298 if (!l--) {
5299 mddev = list_entry(tmp, mddev_t, all_mddevs);
5300 mddev_get(mddev);
5301 spin_unlock(&all_mddevs_lock);
5302 return mddev;
5304 spin_unlock(&all_mddevs_lock);
5305 if (!l--)
5306 return (void*)2;/* tail */
5307 return NULL;
5310 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5312 struct list_head *tmp;
5313 mddev_t *next_mddev, *mddev = v;
5315 ++*pos;
5316 if (v == (void*)2)
5317 return NULL;
5319 spin_lock(&all_mddevs_lock);
5320 if (v == (void*)1)
5321 tmp = all_mddevs.next;
5322 else
5323 tmp = mddev->all_mddevs.next;
5324 if (tmp != &all_mddevs)
5325 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5326 else {
5327 next_mddev = (void*)2;
5328 *pos = 0x10000;
5330 spin_unlock(&all_mddevs_lock);
5332 if (v != (void*)1)
5333 mddev_put(mddev);
5334 return next_mddev;
5338 static void md_seq_stop(struct seq_file *seq, void *v)
5340 mddev_t *mddev = v;
5342 if (mddev && v != (void*)1 && v != (void*)2)
5343 mddev_put(mddev);
5346 struct mdstat_info {
5347 int event;
5350 static int md_seq_show(struct seq_file *seq, void *v)
5352 mddev_t *mddev = v;
5353 sector_t size;
5354 struct list_head *tmp2;
5355 mdk_rdev_t *rdev;
5356 struct mdstat_info *mi = seq->private;
5357 struct bitmap *bitmap;
5359 if (v == (void*)1) {
5360 struct mdk_personality *pers;
5361 seq_printf(seq, "Personalities : ");
5362 spin_lock(&pers_lock);
5363 list_for_each_entry(pers, &pers_list, list)
5364 seq_printf(seq, "[%s] ", pers->name);
5366 spin_unlock(&pers_lock);
5367 seq_printf(seq, "\n");
5368 mi->event = atomic_read(&md_event_count);
5369 return 0;
5371 if (v == (void*)2) {
5372 status_unused(seq);
5373 return 0;
5376 if (mddev_lock(mddev) < 0)
5377 return -EINTR;
5379 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5380 seq_printf(seq, "%s : %sactive", mdname(mddev),
5381 mddev->pers ? "" : "in");
5382 if (mddev->pers) {
5383 if (mddev->ro==1)
5384 seq_printf(seq, " (read-only)");
5385 if (mddev->ro==2)
5386 seq_printf(seq, " (auto-read-only)");
5387 seq_printf(seq, " %s", mddev->pers->name);
5390 size = 0;
5391 rdev_for_each(rdev, tmp2, mddev) {
5392 char b[BDEVNAME_SIZE];
5393 seq_printf(seq, " %s[%d]",
5394 bdevname(rdev->bdev,b), rdev->desc_nr);
5395 if (test_bit(WriteMostly, &rdev->flags))
5396 seq_printf(seq, "(W)");
5397 if (test_bit(Faulty, &rdev->flags)) {
5398 seq_printf(seq, "(F)");
5399 continue;
5400 } else if (rdev->raid_disk < 0)
5401 seq_printf(seq, "(S)"); /* spare */
5402 size += rdev->size;
5405 if (!list_empty(&mddev->disks)) {
5406 if (mddev->pers)
5407 seq_printf(seq, "\n %llu blocks",
5408 (unsigned long long)
5409 mddev->array_sectors / 2);
5410 else
5411 seq_printf(seq, "\n %llu blocks",
5412 (unsigned long long)size);
5414 if (mddev->persistent) {
5415 if (mddev->major_version != 0 ||
5416 mddev->minor_version != 90) {
5417 seq_printf(seq," super %d.%d",
5418 mddev->major_version,
5419 mddev->minor_version);
5421 } else if (mddev->external)
5422 seq_printf(seq, " super external:%s",
5423 mddev->metadata_type);
5424 else
5425 seq_printf(seq, " super non-persistent");
5427 if (mddev->pers) {
5428 mddev->pers->status (seq, mddev);
5429 seq_printf(seq, "\n ");
5430 if (mddev->pers->sync_request) {
5431 if (mddev->curr_resync > 2) {
5432 status_resync (seq, mddev);
5433 seq_printf(seq, "\n ");
5434 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5435 seq_printf(seq, "\tresync=DELAYED\n ");
5436 else if (mddev->recovery_cp < MaxSector)
5437 seq_printf(seq, "\tresync=PENDING\n ");
5439 } else
5440 seq_printf(seq, "\n ");
5442 if ((bitmap = mddev->bitmap)) {
5443 unsigned long chunk_kb;
5444 unsigned long flags;
5445 spin_lock_irqsave(&bitmap->lock, flags);
5446 chunk_kb = bitmap->chunksize >> 10;
5447 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5448 "%lu%s chunk",
5449 bitmap->pages - bitmap->missing_pages,
5450 bitmap->pages,
5451 (bitmap->pages - bitmap->missing_pages)
5452 << (PAGE_SHIFT - 10),
5453 chunk_kb ? chunk_kb : bitmap->chunksize,
5454 chunk_kb ? "KB" : "B");
5455 if (bitmap->file) {
5456 seq_printf(seq, ", file: ");
5457 seq_path(seq, &bitmap->file->f_path, " \t\n");
5460 seq_printf(seq, "\n");
5461 spin_unlock_irqrestore(&bitmap->lock, flags);
5464 seq_printf(seq, "\n");
5466 mddev_unlock(mddev);
5468 return 0;
5471 static struct seq_operations md_seq_ops = {
5472 .start = md_seq_start,
5473 .next = md_seq_next,
5474 .stop = md_seq_stop,
5475 .show = md_seq_show,
5478 static int md_seq_open(struct inode *inode, struct file *file)
5480 int error;
5481 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5482 if (mi == NULL)
5483 return -ENOMEM;
5485 error = seq_open(file, &md_seq_ops);
5486 if (error)
5487 kfree(mi);
5488 else {
5489 struct seq_file *p = file->private_data;
5490 p->private = mi;
5491 mi->event = atomic_read(&md_event_count);
5493 return error;
5496 static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
5498 struct seq_file *m = filp->private_data;
5499 struct mdstat_info *mi = m->private;
5500 int mask;
5502 poll_wait(filp, &md_event_waiters, wait);
5504 /* always allow read */
5505 mask = POLLIN | POLLRDNORM;
5507 if (mi->event != atomic_read(&md_event_count))
5508 mask |= POLLERR | POLLPRI;
5509 return mask;
5512 static const struct file_operations md_seq_fops = {
5513 .owner = THIS_MODULE,
5514 .open = md_seq_open,
5515 .read = seq_read,
5516 .llseek = seq_lseek,
5517 .release = seq_release_private,
5518 .poll = mdstat_poll,
5521 int register_md_personality(struct mdk_personality *p)
5523 spin_lock(&pers_lock);
5524 list_add_tail(&p->list, &pers_list);
5525 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
5526 spin_unlock(&pers_lock);
5527 return 0;
5530 int unregister_md_personality(struct mdk_personality *p)
5532 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
5533 spin_lock(&pers_lock);
5534 list_del_init(&p->list);
5535 spin_unlock(&pers_lock);
5536 return 0;
5539 static int is_mddev_idle(mddev_t *mddev)
5541 mdk_rdev_t * rdev;
5542 int idle;
5543 long curr_events;
5545 idle = 1;
5546 rcu_read_lock();
5547 rdev_for_each_rcu(rdev, mddev) {
5548 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
5549 curr_events = part_stat_read(&disk->part0, sectors[0]) +
5550 part_stat_read(&disk->part0, sectors[1]) -
5551 atomic_read(&disk->sync_io);
5552 /* sync IO will cause sync_io to increase before the disk_stats
5553 * as sync_io is counted when a request starts, and
5554 * disk_stats is counted when it completes.
5555 * So resync activity will cause curr_events to be smaller than
5556 * when there was no such activity.
5557 * non-sync IO will cause disk_stat to increase without
5558 * increasing sync_io so curr_events will (eventually)
5559 * be larger than it was before. Once it becomes
5560 * substantially larger, the test below will cause
5561 * the array to appear non-idle, and resync will slow
5562 * down.
5563 * If there is a lot of outstanding resync activity when
5564 * we set last_event to curr_events, then all that activity
5565 * completing might cause the array to appear non-idle
5566 * and resync will be slowed down even though there might
5567 * not have been non-resync activity. This will only
5568 * happen once though. 'last_events' will soon reflect
5569 * the state where there is little or no outstanding
5570 * resync requests, and further resync activity will
5571 * always make curr_events less than last_events.
5574 if (curr_events - rdev->last_events > 4096) {
5575 rdev->last_events = curr_events;
5576 idle = 0;
5579 rcu_read_unlock();
5580 return idle;
5583 void md_done_sync(mddev_t *mddev, int blocks, int ok)
5585 /* another "blocks" (512byte) blocks have been synced */
5586 atomic_sub(blocks, &mddev->recovery_active);
5587 wake_up(&mddev->recovery_wait);
5588 if (!ok) {
5589 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5590 md_wakeup_thread(mddev->thread);
5591 // stop recovery, signal do_sync ....
5596 /* md_write_start(mddev, bi)
5597 * If we need to update some array metadata (e.g. 'active' flag
5598 * in superblock) before writing, schedule a superblock update
5599 * and wait for it to complete.
5601 void md_write_start(mddev_t *mddev, struct bio *bi)
5603 int did_change = 0;
5604 if (bio_data_dir(bi) != WRITE)
5605 return;
5607 BUG_ON(mddev->ro == 1);
5608 if (mddev->ro == 2) {
5609 /* need to switch to read/write */
5610 mddev->ro = 0;
5611 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5612 md_wakeup_thread(mddev->thread);
5613 md_wakeup_thread(mddev->sync_thread);
5614 did_change = 1;
5616 atomic_inc(&mddev->writes_pending);
5617 if (mddev->safemode == 1)
5618 mddev->safemode = 0;
5619 if (mddev->in_sync) {
5620 spin_lock_irq(&mddev->write_lock);
5621 if (mddev->in_sync) {
5622 mddev->in_sync = 0;
5623 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5624 md_wakeup_thread(mddev->thread);
5625 did_change = 1;
5627 spin_unlock_irq(&mddev->write_lock);
5629 if (did_change)
5630 sysfs_notify(&mddev->kobj, NULL, "array_state");
5631 wait_event(mddev->sb_wait,
5632 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
5633 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
5636 void md_write_end(mddev_t *mddev)
5638 if (atomic_dec_and_test(&mddev->writes_pending)) {
5639 if (mddev->safemode == 2)
5640 md_wakeup_thread(mddev->thread);
5641 else if (mddev->safemode_delay)
5642 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5646 /* md_allow_write(mddev)
5647 * Calling this ensures that the array is marked 'active' so that writes
5648 * may proceed without blocking. It is important to call this before
5649 * attempting a GFP_KERNEL allocation while holding the mddev lock.
5650 * Must be called with mddev_lock held.
5652 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
5653 * is dropped, so return -EAGAIN after notifying userspace.
5655 int md_allow_write(mddev_t *mddev)
5657 if (!mddev->pers)
5658 return 0;
5659 if (mddev->ro)
5660 return 0;
5661 if (!mddev->pers->sync_request)
5662 return 0;
5664 spin_lock_irq(&mddev->write_lock);
5665 if (mddev->in_sync) {
5666 mddev->in_sync = 0;
5667 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5668 if (mddev->safemode_delay &&
5669 mddev->safemode == 0)
5670 mddev->safemode = 1;
5671 spin_unlock_irq(&mddev->write_lock);
5672 md_update_sb(mddev, 0);
5673 sysfs_notify(&mddev->kobj, NULL, "array_state");
5674 } else
5675 spin_unlock_irq(&mddev->write_lock);
5677 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
5678 return -EAGAIN;
5679 else
5680 return 0;
5682 EXPORT_SYMBOL_GPL(md_allow_write);
5684 #define SYNC_MARKS 10
5685 #define SYNC_MARK_STEP (3*HZ)
5686 void md_do_sync(mddev_t *mddev)
5688 mddev_t *mddev2;
5689 unsigned int currspeed = 0,
5690 window;
5691 sector_t max_sectors,j, io_sectors;
5692 unsigned long mark[SYNC_MARKS];
5693 sector_t mark_cnt[SYNC_MARKS];
5694 int last_mark,m;
5695 struct list_head *tmp;
5696 sector_t last_check;
5697 int skipped = 0;
5698 struct list_head *rtmp;
5699 mdk_rdev_t *rdev;
5700 char *desc;
5702 /* just incase thread restarts... */
5703 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5704 return;
5705 if (mddev->ro) /* never try to sync a read-only array */
5706 return;
5708 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5709 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
5710 desc = "data-check";
5711 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5712 desc = "requested-resync";
5713 else
5714 desc = "resync";
5715 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5716 desc = "reshape";
5717 else
5718 desc = "recovery";
5720 /* we overload curr_resync somewhat here.
5721 * 0 == not engaged in resync at all
5722 * 2 == checking that there is no conflict with another sync
5723 * 1 == like 2, but have yielded to allow conflicting resync to
5724 * commense
5725 * other == active in resync - this many blocks
5727 * Before starting a resync we must have set curr_resync to
5728 * 2, and then checked that every "conflicting" array has curr_resync
5729 * less than ours. When we find one that is the same or higher
5730 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5731 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5732 * This will mean we have to start checking from the beginning again.
5736 do {
5737 mddev->curr_resync = 2;
5739 try_again:
5740 if (kthread_should_stop()) {
5741 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5742 goto skip;
5744 for_each_mddev(mddev2, tmp) {
5745 if (mddev2 == mddev)
5746 continue;
5747 if (!mddev->parallel_resync
5748 && mddev2->curr_resync
5749 && match_mddev_units(mddev, mddev2)) {
5750 DEFINE_WAIT(wq);
5751 if (mddev < mddev2 && mddev->curr_resync == 2) {
5752 /* arbitrarily yield */
5753 mddev->curr_resync = 1;
5754 wake_up(&resync_wait);
5756 if (mddev > mddev2 && mddev->curr_resync == 1)
5757 /* no need to wait here, we can wait the next
5758 * time 'round when curr_resync == 2
5760 continue;
5761 /* We need to wait 'interruptible' so as not to
5762 * contribute to the load average, and not to
5763 * be caught by 'softlockup'
5765 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
5766 if (!kthread_should_stop() &&
5767 mddev2->curr_resync >= mddev->curr_resync) {
5768 printk(KERN_INFO "md: delaying %s of %s"
5769 " until %s has finished (they"
5770 " share one or more physical units)\n",
5771 desc, mdname(mddev), mdname(mddev2));
5772 mddev_put(mddev2);
5773 if (signal_pending(current))
5774 flush_signals(current);
5775 schedule();
5776 finish_wait(&resync_wait, &wq);
5777 goto try_again;
5779 finish_wait(&resync_wait, &wq);
5782 } while (mddev->curr_resync < 2);
5784 j = 0;
5785 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5786 /* resync follows the size requested by the personality,
5787 * which defaults to physical size, but can be virtual size
5789 max_sectors = mddev->resync_max_sectors;
5790 mddev->resync_mismatches = 0;
5791 /* we don't use the checkpoint if there's a bitmap */
5792 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5793 j = mddev->resync_min;
5794 else if (!mddev->bitmap)
5795 j = mddev->recovery_cp;
5797 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5798 max_sectors = mddev->size << 1;
5799 else {
5800 /* recovery follows the physical size of devices */
5801 max_sectors = mddev->size << 1;
5802 j = MaxSector;
5803 rdev_for_each(rdev, rtmp, mddev)
5804 if (rdev->raid_disk >= 0 &&
5805 !test_bit(Faulty, &rdev->flags) &&
5806 !test_bit(In_sync, &rdev->flags) &&
5807 rdev->recovery_offset < j)
5808 j = rdev->recovery_offset;
5811 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
5812 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
5813 " %d KB/sec/disk.\n", speed_min(mddev));
5814 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
5815 "(but not more than %d KB/sec) for %s.\n",
5816 speed_max(mddev), desc);
5818 is_mddev_idle(mddev); /* this also initializes IO event counters */
5820 io_sectors = 0;
5821 for (m = 0; m < SYNC_MARKS; m++) {
5822 mark[m] = jiffies;
5823 mark_cnt[m] = io_sectors;
5825 last_mark = 0;
5826 mddev->resync_mark = mark[last_mark];
5827 mddev->resync_mark_cnt = mark_cnt[last_mark];
5830 * Tune reconstruction:
5832 window = 32*(PAGE_SIZE/512);
5833 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
5834 window/2,(unsigned long long) max_sectors/2);
5836 atomic_set(&mddev->recovery_active, 0);
5837 last_check = 0;
5839 if (j>2) {
5840 printk(KERN_INFO
5841 "md: resuming %s of %s from checkpoint.\n",
5842 desc, mdname(mddev));
5843 mddev->curr_resync = j;
5846 while (j < max_sectors) {
5847 sector_t sectors;
5849 skipped = 0;
5850 if (j >= mddev->resync_max) {
5851 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
5852 wait_event(mddev->recovery_wait,
5853 mddev->resync_max > j
5854 || kthread_should_stop());
5856 if (kthread_should_stop())
5857 goto interrupted;
5858 sectors = mddev->pers->sync_request(mddev, j, &skipped,
5859 currspeed < speed_min(mddev));
5860 if (sectors == 0) {
5861 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5862 goto out;
5865 if (!skipped) { /* actual IO requested */
5866 io_sectors += sectors;
5867 atomic_add(sectors, &mddev->recovery_active);
5870 j += sectors;
5871 if (j>1) mddev->curr_resync = j;
5872 mddev->curr_mark_cnt = io_sectors;
5873 if (last_check == 0)
5874 /* this is the earliers that rebuilt will be
5875 * visible in /proc/mdstat
5877 md_new_event(mddev);
5879 if (last_check + window > io_sectors || j == max_sectors)
5880 continue;
5882 last_check = io_sectors;
5884 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5885 break;
5887 repeat:
5888 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
5889 /* step marks */
5890 int next = (last_mark+1) % SYNC_MARKS;
5892 mddev->resync_mark = mark[next];
5893 mddev->resync_mark_cnt = mark_cnt[next];
5894 mark[next] = jiffies;
5895 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
5896 last_mark = next;
5900 if (kthread_should_stop())
5901 goto interrupted;
5905 * this loop exits only if either when we are slower than
5906 * the 'hard' speed limit, or the system was IO-idle for
5907 * a jiffy.
5908 * the system might be non-idle CPU-wise, but we only care
5909 * about not overloading the IO subsystem. (things like an
5910 * e2fsck being done on the RAID array should execute fast)
5912 blk_unplug(mddev->queue);
5913 cond_resched();
5915 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
5916 /((jiffies-mddev->resync_mark)/HZ +1) +1;
5918 if (currspeed > speed_min(mddev)) {
5919 if ((currspeed > speed_max(mddev)) ||
5920 !is_mddev_idle(mddev)) {
5921 msleep(500);
5922 goto repeat;
5926 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
5928 * this also signals 'finished resyncing' to md_stop
5930 out:
5931 blk_unplug(mddev->queue);
5933 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
5935 /* tell personality that we are finished */
5936 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
5938 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
5939 mddev->curr_resync > 2) {
5940 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5941 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5942 if (mddev->curr_resync >= mddev->recovery_cp) {
5943 printk(KERN_INFO
5944 "md: checkpointing %s of %s.\n",
5945 desc, mdname(mddev));
5946 mddev->recovery_cp = mddev->curr_resync;
5948 } else
5949 mddev->recovery_cp = MaxSector;
5950 } else {
5951 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5952 mddev->curr_resync = MaxSector;
5953 rdev_for_each(rdev, rtmp, mddev)
5954 if (rdev->raid_disk >= 0 &&
5955 !test_bit(Faulty, &rdev->flags) &&
5956 !test_bit(In_sync, &rdev->flags) &&
5957 rdev->recovery_offset < mddev->curr_resync)
5958 rdev->recovery_offset = mddev->curr_resync;
5961 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5963 skip:
5964 mddev->curr_resync = 0;
5965 mddev->resync_min = 0;
5966 mddev->resync_max = MaxSector;
5967 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
5968 wake_up(&resync_wait);
5969 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
5970 md_wakeup_thread(mddev->thread);
5971 return;
5973 interrupted:
5975 * got a signal, exit.
5977 printk(KERN_INFO
5978 "md: md_do_sync() got signal ... exiting\n");
5979 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5980 goto out;
5983 EXPORT_SYMBOL_GPL(md_do_sync);
5986 static int remove_and_add_spares(mddev_t *mddev)
5988 mdk_rdev_t *rdev;
5989 struct list_head *rtmp;
5990 int spares = 0;
5992 rdev_for_each(rdev, rtmp, mddev)
5993 if (rdev->raid_disk >= 0 &&
5994 !test_bit(Blocked, &rdev->flags) &&
5995 (test_bit(Faulty, &rdev->flags) ||
5996 ! test_bit(In_sync, &rdev->flags)) &&
5997 atomic_read(&rdev->nr_pending)==0) {
5998 if (mddev->pers->hot_remove_disk(
5999 mddev, rdev->raid_disk)==0) {
6000 char nm[20];
6001 sprintf(nm,"rd%d", rdev->raid_disk);
6002 sysfs_remove_link(&mddev->kobj, nm);
6003 rdev->raid_disk = -1;
6007 if (mddev->degraded && ! mddev->ro) {
6008 rdev_for_each(rdev, rtmp, mddev) {
6009 if (rdev->raid_disk >= 0 &&
6010 !test_bit(In_sync, &rdev->flags) &&
6011 !test_bit(Blocked, &rdev->flags))
6012 spares++;
6013 if (rdev->raid_disk < 0
6014 && !test_bit(Faulty, &rdev->flags)) {
6015 rdev->recovery_offset = 0;
6016 if (mddev->pers->
6017 hot_add_disk(mddev, rdev) == 0) {
6018 char nm[20];
6019 sprintf(nm, "rd%d", rdev->raid_disk);
6020 if (sysfs_create_link(&mddev->kobj,
6021 &rdev->kobj, nm))
6022 printk(KERN_WARNING
6023 "md: cannot register "
6024 "%s for %s\n",
6025 nm, mdname(mddev));
6026 spares++;
6027 md_new_event(mddev);
6028 } else
6029 break;
6033 return spares;
6036 * This routine is regularly called by all per-raid-array threads to
6037 * deal with generic issues like resync and super-block update.
6038 * Raid personalities that don't have a thread (linear/raid0) do not
6039 * need this as they never do any recovery or update the superblock.
6041 * It does not do any resync itself, but rather "forks" off other threads
6042 * to do that as needed.
6043 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6044 * "->recovery" and create a thread at ->sync_thread.
6045 * When the thread finishes it sets MD_RECOVERY_DONE
6046 * and wakeups up this thread which will reap the thread and finish up.
6047 * This thread also removes any faulty devices (with nr_pending == 0).
6049 * The overall approach is:
6050 * 1/ if the superblock needs updating, update it.
6051 * 2/ If a recovery thread is running, don't do anything else.
6052 * 3/ If recovery has finished, clean up, possibly marking spares active.
6053 * 4/ If there are any faulty devices, remove them.
6054 * 5/ If array is degraded, try to add spares devices
6055 * 6/ If array has spares or is not in-sync, start a resync thread.
6057 void md_check_recovery(mddev_t *mddev)
6059 mdk_rdev_t *rdev;
6060 struct list_head *rtmp;
6063 if (mddev->bitmap)
6064 bitmap_daemon_work(mddev->bitmap);
6066 if (test_and_clear_bit(MD_NOTIFY_ARRAY_STATE, &mddev->flags))
6067 sysfs_notify(&mddev->kobj, NULL, "array_state");
6069 if (mddev->ro)
6070 return;
6072 if (signal_pending(current)) {
6073 if (mddev->pers->sync_request && !mddev->external) {
6074 printk(KERN_INFO "md: %s in immediate safe mode\n",
6075 mdname(mddev));
6076 mddev->safemode = 2;
6078 flush_signals(current);
6081 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6082 return;
6083 if ( ! (
6084 (mddev->flags && !mddev->external) ||
6085 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
6086 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
6087 (mddev->external == 0 && mddev->safemode == 1) ||
6088 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6089 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
6091 return;
6093 if (mddev_trylock(mddev)) {
6094 int spares = 0;
6096 if (mddev->ro) {
6097 /* Only thing we do on a ro array is remove
6098 * failed devices.
6100 remove_and_add_spares(mddev);
6101 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6102 goto unlock;
6105 if (!mddev->external) {
6106 int did_change = 0;
6107 spin_lock_irq(&mddev->write_lock);
6108 if (mddev->safemode &&
6109 !atomic_read(&mddev->writes_pending) &&
6110 !mddev->in_sync &&
6111 mddev->recovery_cp == MaxSector) {
6112 mddev->in_sync = 1;
6113 did_change = 1;
6114 if (mddev->persistent)
6115 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6117 if (mddev->safemode == 1)
6118 mddev->safemode = 0;
6119 spin_unlock_irq(&mddev->write_lock);
6120 if (did_change)
6121 sysfs_notify(&mddev->kobj, NULL, "array_state");
6124 if (mddev->flags)
6125 md_update_sb(mddev, 0);
6127 rdev_for_each(rdev, rtmp, mddev)
6128 if (test_and_clear_bit(StateChanged, &rdev->flags))
6129 sysfs_notify(&rdev->kobj, NULL, "state");
6132 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6133 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6134 /* resync/recovery still happening */
6135 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6136 goto unlock;
6138 if (mddev->sync_thread) {
6139 /* resync has finished, collect result */
6140 md_unregister_thread(mddev->sync_thread);
6141 mddev->sync_thread = NULL;
6142 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6143 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
6144 /* success...*/
6145 /* activate any spares */
6146 if (mddev->pers->spare_active(mddev))
6147 sysfs_notify(&mddev->kobj, NULL,
6148 "degraded");
6150 md_update_sb(mddev, 1);
6152 /* if array is no-longer degraded, then any saved_raid_disk
6153 * information must be scrapped
6155 if (!mddev->degraded)
6156 rdev_for_each(rdev, rtmp, mddev)
6157 rdev->saved_raid_disk = -1;
6159 mddev->recovery = 0;
6160 /* flag recovery needed just to double check */
6161 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6162 sysfs_notify(&mddev->kobj, NULL, "sync_action");
6163 md_new_event(mddev);
6164 goto unlock;
6166 /* Set RUNNING before clearing NEEDED to avoid
6167 * any transients in the value of "sync_action".
6169 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6170 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6171 /* Clear some bits that don't mean anything, but
6172 * might be left set
6174 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6175 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
6177 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6178 goto unlock;
6179 /* no recovery is running.
6180 * remove any failed drives, then
6181 * add spares if possible.
6182 * Spare are also removed and re-added, to allow
6183 * the personality to fail the re-add.
6186 if (mddev->reshape_position != MaxSector) {
6187 if (mddev->pers->check_reshape(mddev) != 0)
6188 /* Cannot proceed */
6189 goto unlock;
6190 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6191 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6192 } else if ((spares = remove_and_add_spares(mddev))) {
6193 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6194 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6195 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
6196 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6197 } else if (mddev->recovery_cp < MaxSector) {
6198 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6199 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6200 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6201 /* nothing to be done ... */
6202 goto unlock;
6204 if (mddev->pers->sync_request) {
6205 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6206 /* We are adding a device or devices to an array
6207 * which has the bitmap stored on all devices.
6208 * So make sure all bitmap pages get written
6210 bitmap_write_all(mddev->bitmap);
6212 mddev->sync_thread = md_register_thread(md_do_sync,
6213 mddev,
6214 "%s_resync");
6215 if (!mddev->sync_thread) {
6216 printk(KERN_ERR "%s: could not start resync"
6217 " thread...\n",
6218 mdname(mddev));
6219 /* leave the spares where they are, it shouldn't hurt */
6220 mddev->recovery = 0;
6221 } else
6222 md_wakeup_thread(mddev->sync_thread);
6223 sysfs_notify(&mddev->kobj, NULL, "sync_action");
6224 md_new_event(mddev);
6226 unlock:
6227 if (!mddev->sync_thread) {
6228 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6229 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6230 &mddev->recovery))
6231 sysfs_notify(&mddev->kobj, NULL, "sync_action");
6233 mddev_unlock(mddev);
6237 void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6239 sysfs_notify(&rdev->kobj, NULL, "state");
6240 wait_event_timeout(rdev->blocked_wait,
6241 !test_bit(Blocked, &rdev->flags),
6242 msecs_to_jiffies(5000));
6243 rdev_dec_pending(rdev, mddev);
6245 EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6247 static int md_notify_reboot(struct notifier_block *this,
6248 unsigned long code, void *x)
6250 struct list_head *tmp;
6251 mddev_t *mddev;
6253 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6255 printk(KERN_INFO "md: stopping all md devices.\n");
6257 for_each_mddev(mddev, tmp)
6258 if (mddev_trylock(mddev)) {
6259 /* Force a switch to readonly even array
6260 * appears to still be in use. Hence
6261 * the '100'.
6263 do_md_stop (mddev, 1, 100);
6264 mddev_unlock(mddev);
6267 * certain more exotic SCSI devices are known to be
6268 * volatile wrt too early system reboots. While the
6269 * right place to handle this issue is the given
6270 * driver, we do want to have a safe RAID driver ...
6272 mdelay(1000*1);
6274 return NOTIFY_DONE;
6277 static struct notifier_block md_notifier = {
6278 .notifier_call = md_notify_reboot,
6279 .next = NULL,
6280 .priority = INT_MAX, /* before any real devices */
6283 static void md_geninit(void)
6285 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6287 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
6290 static int __init md_init(void)
6292 if (register_blkdev(MAJOR_NR, "md"))
6293 return -1;
6294 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
6295 unregister_blkdev(MAJOR_NR, "md");
6296 return -1;
6298 blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE,
6299 md_probe, NULL, NULL);
6300 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
6301 md_probe, NULL, NULL);
6303 register_reboot_notifier(&md_notifier);
6304 raid_table_header = register_sysctl_table(raid_root_table);
6306 md_geninit();
6307 return (0);
6311 #ifndef MODULE
6314 * Searches all registered partitions for autorun RAID arrays
6315 * at boot time.
6318 static LIST_HEAD(all_detected_devices);
6319 struct detected_devices_node {
6320 struct list_head list;
6321 dev_t dev;
6324 void md_autodetect_dev(dev_t dev)
6326 struct detected_devices_node *node_detected_dev;
6328 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6329 if (node_detected_dev) {
6330 node_detected_dev->dev = dev;
6331 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6332 } else {
6333 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6334 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6339 static void autostart_arrays(int part)
6341 mdk_rdev_t *rdev;
6342 struct detected_devices_node *node_detected_dev;
6343 dev_t dev;
6344 int i_scanned, i_passed;
6346 i_scanned = 0;
6347 i_passed = 0;
6349 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6351 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6352 i_scanned++;
6353 node_detected_dev = list_entry(all_detected_devices.next,
6354 struct detected_devices_node, list);
6355 list_del(&node_detected_dev->list);
6356 dev = node_detected_dev->dev;
6357 kfree(node_detected_dev);
6358 rdev = md_import_device(dev,0, 90);
6359 if (IS_ERR(rdev))
6360 continue;
6362 if (test_bit(Faulty, &rdev->flags)) {
6363 MD_BUG();
6364 continue;
6366 set_bit(AutoDetected, &rdev->flags);
6367 list_add(&rdev->same_set, &pending_raid_disks);
6368 i_passed++;
6371 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6372 i_scanned, i_passed);
6374 autorun_devices(part);
6377 #endif /* !MODULE */
6379 static __exit void md_exit(void)
6381 mddev_t *mddev;
6382 struct list_head *tmp;
6384 blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS);
6385 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
6387 unregister_blkdev(MAJOR_NR,"md");
6388 unregister_blkdev(mdp_major, "mdp");
6389 unregister_reboot_notifier(&md_notifier);
6390 unregister_sysctl_table(raid_table_header);
6391 remove_proc_entry("mdstat", NULL);
6392 for_each_mddev(mddev, tmp) {
6393 struct gendisk *disk = mddev->gendisk;
6394 if (!disk)
6395 continue;
6396 export_array(mddev);
6397 del_gendisk(disk);
6398 put_disk(disk);
6399 mddev->gendisk = NULL;
6400 mddev_put(mddev);
6404 subsys_initcall(md_init);
6405 module_exit(md_exit)
6407 static int get_ro(char *buffer, struct kernel_param *kp)
6409 return sprintf(buffer, "%d", start_readonly);
6411 static int set_ro(const char *val, struct kernel_param *kp)
6413 char *e;
6414 int num = simple_strtoul(val, &e, 10);
6415 if (*val && (*e == '\0' || *e == '\n')) {
6416 start_readonly = num;
6417 return 0;
6419 return -EINVAL;
6422 module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6423 module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
6426 EXPORT_SYMBOL(register_md_personality);
6427 EXPORT_SYMBOL(unregister_md_personality);
6428 EXPORT_SYMBOL(md_error);
6429 EXPORT_SYMBOL(md_done_sync);
6430 EXPORT_SYMBOL(md_write_start);
6431 EXPORT_SYMBOL(md_write_end);
6432 EXPORT_SYMBOL(md_register_thread);
6433 EXPORT_SYMBOL(md_unregister_thread);
6434 EXPORT_SYMBOL(md_wakeup_thread);
6435 EXPORT_SYMBOL(md_check_recovery);
6436 MODULE_LICENSE("GPL");
6437 MODULE_ALIAS("md");
6438 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);