MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / md / md.c
blob745f7e7bd3ee445f0782285355d811023ae9ca70
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 This program is free software; you can redistribute it and/or modify
23 it under the terms of the GNU General Public License as published by
24 the Free Software Foundation; either version 2, or (at your option)
25 any later version.
27 You should have received a copy of the GNU General Public License
28 (for example /usr/src/linux/COPYING); if not, write to the Free
29 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 #include <linux/module.h>
33 #include <linux/config.h>
34 #include <linux/linkage.h>
35 #include <linux/raid/md.h>
36 #include <linux/sysctl.h>
37 #include <linux/devfs_fs_kernel.h>
38 #include <linux/buffer_head.h> /* for invalidate_bdev */
39 #include <linux/suspend.h>
41 #include <linux/init.h>
43 #ifdef CONFIG_KMOD
44 #include <linux/kmod.h>
45 #endif
47 #include <asm/unaligned.h>
49 #define MAJOR_NR MD_MAJOR
50 #define MD_DRIVER
52 /* 63 partitions with the alternate major number (mdp) */
53 #define MdpMinorShift 6
55 #define DEBUG 0
56 #define dprintk(x...) ((void)(DEBUG && printk(x)))
59 #ifndef MODULE
60 static void autostart_arrays (int part);
61 #endif
63 static mdk_personality_t *pers[MAX_PERSONALITY];
64 static spinlock_t pers_lock = SPIN_LOCK_UNLOCKED;
67 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
68 * is 1000 KB/sec, so the extra system load does not show up that much.
69 * Increase it if you want to have more _guaranteed_ speed. Note that
70 * the RAID driver will use the maximum available bandwith if the IO
71 * subsystem is idle. There is also an 'absolute maximum' reconstruction
72 * speed limit - in case reconstruction slows down your system despite
73 * idle IO detection.
75 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
78 static int sysctl_speed_limit_min = 1000;
79 static int sysctl_speed_limit_max = 200000;
81 static struct ctl_table_header *raid_table_header;
83 static ctl_table raid_table[] = {
85 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
86 .procname = "speed_limit_min",
87 .data = &sysctl_speed_limit_min,
88 .maxlen = sizeof(int),
89 .mode = 0644,
90 .proc_handler = &proc_dointvec,
93 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
94 .procname = "speed_limit_max",
95 .data = &sysctl_speed_limit_max,
96 .maxlen = sizeof(int),
97 .mode = 0644,
98 .proc_handler = &proc_dointvec,
100 { .ctl_name = 0 }
103 static ctl_table raid_dir_table[] = {
105 .ctl_name = DEV_RAID,
106 .procname = "raid",
107 .maxlen = 0,
108 .mode = 0555,
109 .child = raid_table,
111 { .ctl_name = 0 }
114 static ctl_table raid_root_table[] = {
116 .ctl_name = CTL_DEV,
117 .procname = "dev",
118 .maxlen = 0,
119 .mode = 0555,
120 .child = raid_dir_table,
122 { .ctl_name = 0 }
125 static struct block_device_operations md_fops;
128 * Enables to iterate over all existing md arrays
129 * all_mddevs_lock protects this list.
131 static LIST_HEAD(all_mddevs);
132 static spinlock_t all_mddevs_lock = SPIN_LOCK_UNLOCKED;
136 * iterates through all used mddevs in the system.
137 * We take care to grab the all_mddevs_lock whenever navigating
138 * the list, and to always hold a refcount when unlocked.
139 * Any code which breaks out of this loop while own
140 * a reference to the current mddev and must mddev_put it.
142 #define ITERATE_MDDEV(mddev,tmp) \
144 for (({ spin_lock(&all_mddevs_lock); \
145 tmp = all_mddevs.next; \
146 mddev = NULL;}); \
147 ({ if (tmp != &all_mddevs) \
148 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
149 spin_unlock(&all_mddevs_lock); \
150 if (mddev) mddev_put(mddev); \
151 mddev = list_entry(tmp, mddev_t, all_mddevs); \
152 tmp != &all_mddevs;}); \
153 ({ spin_lock(&all_mddevs_lock); \
154 tmp = tmp->next;}) \
157 int md_flush_mddev(mddev_t *mddev, sector_t *error_sector)
159 struct list_head *tmp;
160 mdk_rdev_t *rdev;
161 int ret = 0;
164 * this list iteration is done without any locking in md?!
166 ITERATE_RDEV(mddev, rdev, tmp) {
167 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
168 int err;
170 if (!r_queue->issue_flush_fn)
171 err = -EOPNOTSUPP;
172 else
173 err = r_queue->issue_flush_fn(r_queue, rdev->bdev->bd_disk, error_sector);
175 if (!ret)
176 ret = err;
179 return ret;
182 static int md_flush_all(request_queue_t *q, struct gendisk *disk,
183 sector_t *error_sector)
185 mddev_t *mddev = q->queuedata;
187 return md_flush_mddev(mddev, error_sector);
190 static int md_fail_request (request_queue_t *q, struct bio *bio)
192 bio_io_error(bio, bio->bi_size);
193 return 0;
196 static inline mddev_t *mddev_get(mddev_t *mddev)
198 atomic_inc(&mddev->active);
199 return mddev;
202 static void mddev_put(mddev_t *mddev)
204 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
205 return;
206 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
207 list_del(&mddev->all_mddevs);
208 blk_put_queue(mddev->queue);
209 kfree(mddev);
211 spin_unlock(&all_mddevs_lock);
214 static mddev_t * mddev_find(dev_t unit)
216 mddev_t *mddev, *new = NULL;
218 retry:
219 spin_lock(&all_mddevs_lock);
220 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
221 if (mddev->unit == unit) {
222 mddev_get(mddev);
223 spin_unlock(&all_mddevs_lock);
224 if (new)
225 kfree(new);
226 return mddev;
229 if (new) {
230 list_add(&new->all_mddevs, &all_mddevs);
231 spin_unlock(&all_mddevs_lock);
232 return new;
234 spin_unlock(&all_mddevs_lock);
236 new = (mddev_t *) kmalloc(sizeof(*new), GFP_KERNEL);
237 if (!new)
238 return NULL;
240 memset(new, 0, sizeof(*new));
242 new->unit = unit;
243 if (MAJOR(unit) == MD_MAJOR)
244 new->md_minor = MINOR(unit);
245 else
246 new->md_minor = MINOR(unit) >> MdpMinorShift;
248 init_MUTEX(&new->reconfig_sem);
249 INIT_LIST_HEAD(&new->disks);
250 INIT_LIST_HEAD(&new->all_mddevs);
251 init_timer(&new->safemode_timer);
252 atomic_set(&new->active, 1);
254 new->queue = blk_alloc_queue(GFP_KERNEL);
255 if (!new->queue) {
256 kfree(new);
257 return NULL;
260 blk_queue_make_request(new->queue, md_fail_request);
262 goto retry;
265 static inline int mddev_lock(mddev_t * mddev)
267 return down_interruptible(&mddev->reconfig_sem);
270 static inline void mddev_lock_uninterruptible(mddev_t * mddev)
272 down(&mddev->reconfig_sem);
275 static inline int mddev_trylock(mddev_t * mddev)
277 return down_trylock(&mddev->reconfig_sem);
280 static inline void mddev_unlock(mddev_t * mddev)
282 up(&mddev->reconfig_sem);
284 if (mddev->thread)
285 md_wakeup_thread(mddev->thread);
288 mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
290 mdk_rdev_t * rdev;
291 struct list_head *tmp;
293 ITERATE_RDEV(mddev,rdev,tmp) {
294 if (rdev->desc_nr == nr)
295 return rdev;
297 return NULL;
300 static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
302 struct list_head *tmp;
303 mdk_rdev_t *rdev;
305 ITERATE_RDEV(mddev,rdev,tmp) {
306 if (rdev->bdev->bd_dev == dev)
307 return rdev;
309 return NULL;
312 inline static sector_t calc_dev_sboffset(struct block_device *bdev)
314 sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
315 return MD_NEW_SIZE_BLOCKS(size);
318 static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
320 sector_t size;
322 size = rdev->sb_offset;
324 if (chunk_size)
325 size &= ~((sector_t)chunk_size/1024 - 1);
326 return size;
329 static int alloc_disk_sb(mdk_rdev_t * rdev)
331 if (rdev->sb_page)
332 MD_BUG();
334 rdev->sb_page = alloc_page(GFP_KERNEL);
335 if (!rdev->sb_page) {
336 printk(KERN_ALERT "md: out of memory.\n");
337 return -EINVAL;
340 return 0;
343 static void free_disk_sb(mdk_rdev_t * rdev)
345 if (rdev->sb_page) {
346 page_cache_release(rdev->sb_page);
347 rdev->sb_loaded = 0;
348 rdev->sb_page = NULL;
349 rdev->sb_offset = 0;
350 rdev->size = 0;
355 static int bi_complete(struct bio *bio, unsigned int bytes_done, int error)
357 if (bio->bi_size)
358 return 1;
360 complete((struct completion*)bio->bi_private);
361 return 0;
364 static int sync_page_io(struct block_device *bdev, sector_t sector, int size,
365 struct page *page, int rw)
367 struct bio bio;
368 struct bio_vec vec;
369 struct completion event;
371 rw |= (1 << BIO_RW_SYNC);
373 bio_init(&bio);
374 bio.bi_io_vec = &vec;
375 vec.bv_page = page;
376 vec.bv_len = size;
377 vec.bv_offset = 0;
378 bio.bi_vcnt = 1;
379 bio.bi_idx = 0;
380 bio.bi_size = size;
381 bio.bi_bdev = bdev;
382 bio.bi_sector = sector;
383 init_completion(&event);
384 bio.bi_private = &event;
385 bio.bi_end_io = bi_complete;
386 submit_bio(rw, &bio);
387 wait_for_completion(&event);
389 return test_bit(BIO_UPTODATE, &bio.bi_flags);
392 static int read_disk_sb(mdk_rdev_t * rdev)
394 char b[BDEVNAME_SIZE];
395 if (!rdev->sb_page) {
396 MD_BUG();
397 return -EINVAL;
399 if (rdev->sb_loaded)
400 return 0;
403 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, MD_SB_BYTES, rdev->sb_page, READ))
404 goto fail;
405 rdev->sb_loaded = 1;
406 return 0;
408 fail:
409 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
410 bdevname(rdev->bdev,b));
411 return -EINVAL;
414 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
416 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
417 (sb1->set_uuid1 == sb2->set_uuid1) &&
418 (sb1->set_uuid2 == sb2->set_uuid2) &&
419 (sb1->set_uuid3 == sb2->set_uuid3))
421 return 1;
423 return 0;
427 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
429 int ret;
430 mdp_super_t *tmp1, *tmp2;
432 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
433 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
435 if (!tmp1 || !tmp2) {
436 ret = 0;
437 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
438 goto abort;
441 *tmp1 = *sb1;
442 *tmp2 = *sb2;
445 * nr_disks is not constant
447 tmp1->nr_disks = 0;
448 tmp2->nr_disks = 0;
450 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
451 ret = 0;
452 else
453 ret = 1;
455 abort:
456 if (tmp1)
457 kfree(tmp1);
458 if (tmp2)
459 kfree(tmp2);
461 return ret;
464 static unsigned int calc_sb_csum(mdp_super_t * sb)
466 unsigned int disk_csum, csum;
468 disk_csum = sb->sb_csum;
469 sb->sb_csum = 0;
470 csum = csum_partial((void *)sb, MD_SB_BYTES, 0);
471 sb->sb_csum = disk_csum;
472 return csum;
475 /* csum_partial is not consistent between different architectures.
476 * Some (i386) do a 32bit csum. Some (alpha) do 16 bit.
477 * This makes it hard for user-space to know what to do.
478 * So we use calc_sb_csum to set the checksum to allow working
479 * with older kernels, but allow calc_sb_csum_common to
480 * be used when checking if a checksum is correct, to
481 * make life easier for user-space tools that might write
482 * a superblock.
484 static unsigned int calc_sb_csum_common(mdp_super_t *super)
486 unsigned int disk_csum = super->sb_csum;
487 unsigned long long newcsum = 0;
488 unsigned int csum;
489 int i;
490 unsigned int *superc = (int*) super;
491 super->sb_csum = 0;
493 for (i=0; i<MD_SB_BYTES/4; i++)
494 newcsum+= superc[i];
495 csum = (newcsum& 0xffffffff) + (newcsum>>32);
496 super->sb_csum = disk_csum;
497 return csum;
501 * Handle superblock details.
502 * We want to be able to handle multiple superblock formats
503 * so we have a common interface to them all, and an array of
504 * different handlers.
505 * We rely on user-space to write the initial superblock, and support
506 * reading and updating of superblocks.
507 * Interface methods are:
508 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
509 * loads and validates a superblock on dev.
510 * if refdev != NULL, compare superblocks on both devices
511 * Return:
512 * 0 - dev has a superblock that is compatible with refdev
513 * 1 - dev has a superblock that is compatible and newer than refdev
514 * so dev should be used as the refdev in future
515 * -EINVAL superblock incompatible or invalid
516 * -othererror e.g. -EIO
518 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
519 * Verify that dev is acceptable into mddev.
520 * The first time, mddev->raid_disks will be 0, and data from
521 * dev should be merged in. Subsequent calls check that dev
522 * is new enough. Return 0 or -EINVAL
524 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
525 * Update the superblock for rdev with data in mddev
526 * This does not write to disc.
530 struct super_type {
531 char *name;
532 struct module *owner;
533 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
534 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
535 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
539 * load_super for 0.90.0
541 static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
543 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
544 mdp_super_t *sb;
545 int ret;
546 sector_t sb_offset;
549 * Calculate the position of the superblock,
550 * it's at the end of the disk.
552 * It also happens to be a multiple of 4Kb.
554 sb_offset = calc_dev_sboffset(rdev->bdev);
555 rdev->sb_offset = sb_offset;
557 ret = read_disk_sb(rdev);
558 if (ret) return ret;
560 ret = -EINVAL;
562 bdevname(rdev->bdev, b);
563 sb = (mdp_super_t*)page_address(rdev->sb_page);
565 if (sb->md_magic != MD_SB_MAGIC) {
566 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
568 goto abort;
571 if (sb->major_version != 0 ||
572 sb->minor_version != 90) {
573 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
574 sb->major_version, sb->minor_version,
576 goto abort;
579 if (sb->raid_disks <= 0)
580 goto abort;
582 if (calc_sb_csum(sb) != sb->sb_csum &&
583 calc_sb_csum_common(sb) != sb->sb_csum) {
584 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
586 goto abort;
589 rdev->preferred_minor = sb->md_minor;
590 rdev->data_offset = 0;
592 if (sb->level == MULTIPATH)
593 rdev->desc_nr = -1;
594 else
595 rdev->desc_nr = sb->this_disk.number;
597 if (refdev == 0)
598 ret = 1;
599 else {
600 __u64 ev1, ev2;
601 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
602 if (!uuid_equal(refsb, sb)) {
603 printk(KERN_WARNING "md: %s has different UUID to %s\n",
604 b, bdevname(refdev->bdev,b2));
605 goto abort;
607 if (!sb_equal(refsb, sb)) {
608 printk(KERN_WARNING "md: %s has same UUID"
609 " but different superblock to %s\n",
610 b, bdevname(refdev->bdev, b2));
611 goto abort;
613 ev1 = md_event(sb);
614 ev2 = md_event(refsb);
615 if (ev1 > ev2)
616 ret = 1;
617 else
618 ret = 0;
620 rdev->size = calc_dev_size(rdev, sb->chunk_size);
622 abort:
623 return ret;
627 * validate_super for 0.90.0
629 static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
631 mdp_disk_t *desc;
632 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
634 if (mddev->raid_disks == 0) {
635 mddev->major_version = 0;
636 mddev->minor_version = sb->minor_version;
637 mddev->patch_version = sb->patch_version;
638 mddev->persistent = ! sb->not_persistent;
639 mddev->chunk_size = sb->chunk_size;
640 mddev->ctime = sb->ctime;
641 mddev->utime = sb->utime;
642 mddev->level = sb->level;
643 mddev->layout = sb->layout;
644 mddev->raid_disks = sb->raid_disks;
645 mddev->size = sb->size;
646 mddev->events = md_event(sb);
648 if (sb->state & (1<<MD_SB_CLEAN))
649 mddev->recovery_cp = MaxSector;
650 else {
651 if (sb->events_hi == sb->cp_events_hi &&
652 sb->events_lo == sb->cp_events_lo) {
653 mddev->recovery_cp = sb->recovery_cp;
654 } else
655 mddev->recovery_cp = 0;
658 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
659 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
660 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
661 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
663 mddev->max_disks = MD_SB_DISKS;
664 } else {
665 __u64 ev1;
666 ev1 = md_event(sb);
667 ++ev1;
668 if (ev1 < mddev->events)
669 return -EINVAL;
671 if (mddev->level != LEVEL_MULTIPATH) {
672 rdev->raid_disk = -1;
673 rdev->in_sync = rdev->faulty = 0;
674 desc = sb->disks + rdev->desc_nr;
676 if (desc->state & (1<<MD_DISK_FAULTY))
677 rdev->faulty = 1;
678 else if (desc->state & (1<<MD_DISK_SYNC) &&
679 desc->raid_disk < mddev->raid_disks) {
680 rdev->in_sync = 1;
681 rdev->raid_disk = desc->raid_disk;
684 return 0;
688 * sync_super for 0.90.0
690 static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
692 mdp_super_t *sb;
693 struct list_head *tmp;
694 mdk_rdev_t *rdev2;
695 int next_spare = mddev->raid_disks;
697 /* make rdev->sb match mddev data..
699 * 1/ zero out disks
700 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
701 * 3/ any empty disks < next_spare become removed
703 * disks[0] gets initialised to REMOVED because
704 * we cannot be sure from other fields if it has
705 * been initialised or not.
707 int i;
708 int active=0, working=0,failed=0,spare=0,nr_disks=0;
710 sb = (mdp_super_t*)page_address(rdev->sb_page);
712 memset(sb, 0, sizeof(*sb));
714 sb->md_magic = MD_SB_MAGIC;
715 sb->major_version = mddev->major_version;
716 sb->minor_version = mddev->minor_version;
717 sb->patch_version = mddev->patch_version;
718 sb->gvalid_words = 0; /* ignored */
719 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
720 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
721 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
722 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
724 sb->ctime = mddev->ctime;
725 sb->level = mddev->level;
726 sb->size = mddev->size;
727 sb->raid_disks = mddev->raid_disks;
728 sb->md_minor = mddev->md_minor;
729 sb->not_persistent = !mddev->persistent;
730 sb->utime = mddev->utime;
731 sb->state = 0;
732 sb->events_hi = (mddev->events>>32);
733 sb->events_lo = (u32)mddev->events;
735 if (mddev->in_sync)
737 sb->recovery_cp = mddev->recovery_cp;
738 sb->cp_events_hi = (mddev->events>>32);
739 sb->cp_events_lo = (u32)mddev->events;
740 if (mddev->recovery_cp == MaxSector)
741 sb->state = (1<< MD_SB_CLEAN);
742 } else
743 sb->recovery_cp = 0;
745 sb->layout = mddev->layout;
746 sb->chunk_size = mddev->chunk_size;
748 sb->disks[0].state = (1<<MD_DISK_REMOVED);
749 ITERATE_RDEV(mddev,rdev2,tmp) {
750 mdp_disk_t *d;
751 if (rdev2->raid_disk >= 0 && rdev2->in_sync && !rdev2->faulty)
752 rdev2->desc_nr = rdev2->raid_disk;
753 else
754 rdev2->desc_nr = next_spare++;
755 d = &sb->disks[rdev2->desc_nr];
756 nr_disks++;
757 d->number = rdev2->desc_nr;
758 d->major = MAJOR(rdev2->bdev->bd_dev);
759 d->minor = MINOR(rdev2->bdev->bd_dev);
760 if (rdev2->raid_disk >= 0 && rdev->in_sync && !rdev2->faulty)
761 d->raid_disk = rdev2->raid_disk;
762 else
763 d->raid_disk = rdev2->desc_nr; /* compatibility */
764 if (rdev2->faulty) {
765 d->state = (1<<MD_DISK_FAULTY);
766 failed++;
767 } else if (rdev2->in_sync) {
768 d->state = (1<<MD_DISK_ACTIVE);
769 d->state |= (1<<MD_DISK_SYNC);
770 active++;
771 working++;
772 } else {
773 d->state = 0;
774 spare++;
775 working++;
779 /* now set the "removed" and "faulty" bits on any missing devices */
780 for (i=0 ; i < mddev->raid_disks ; i++) {
781 mdp_disk_t *d = &sb->disks[i];
782 if (d->state == 0 && d->number == 0) {
783 d->number = i;
784 d->raid_disk = i;
785 d->state = (1<<MD_DISK_REMOVED);
786 d->state |= (1<<MD_DISK_FAULTY);
787 failed++;
790 sb->nr_disks = nr_disks;
791 sb->active_disks = active;
792 sb->working_disks = working;
793 sb->failed_disks = failed;
794 sb->spare_disks = spare;
796 sb->this_disk = sb->disks[rdev->desc_nr];
797 sb->sb_csum = calc_sb_csum(sb);
801 * version 1 superblock
804 static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
806 unsigned int disk_csum, csum;
807 unsigned long long newcsum;
808 int size = 256 + sb->max_dev*2;
809 unsigned int *isuper = (unsigned int*)sb;
810 int i;
812 disk_csum = sb->sb_csum;
813 sb->sb_csum = 0;
814 newcsum = 0;
815 for (i=0; size>=4; size -= 4 )
816 newcsum += le32_to_cpu(*isuper++);
818 if (size == 2)
819 newcsum += le16_to_cpu(*(unsigned short*) isuper);
821 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
822 sb->sb_csum = disk_csum;
823 return csum;
826 static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
828 struct mdp_superblock_1 *sb;
829 int ret;
830 sector_t sb_offset;
831 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
834 * Calculate the position of the superblock.
835 * It is always aligned to a 4K boundary and
836 * depeding on minor_version, it can be:
837 * 0: At least 8K, but less than 12K, from end of device
838 * 1: At start of device
839 * 2: 4K from start of device.
841 switch(minor_version) {
842 case 0:
843 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
844 sb_offset -= 8*2;
845 sb_offset &= ~(4*2);
846 /* convert from sectors to K */
847 sb_offset /= 2;
848 break;
849 case 1:
850 sb_offset = 0;
851 break;
852 case 2:
853 sb_offset = 4;
854 break;
855 default:
856 return -EINVAL;
858 rdev->sb_offset = sb_offset;
860 ret = read_disk_sb(rdev);
861 if (ret) return ret;
864 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
866 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
867 sb->major_version != cpu_to_le32(1) ||
868 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
869 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
870 sb->feature_map != 0)
871 return -EINVAL;
873 if (calc_sb_1_csum(sb) != sb->sb_csum) {
874 printk("md: invalid superblock checksum on %s\n",
875 bdevname(rdev->bdev,b));
876 return -EINVAL;
878 rdev->preferred_minor = 0xffff;
879 rdev->data_offset = le64_to_cpu(sb->data_offset);
881 if (refdev == 0)
882 return 1;
883 else {
884 __u64 ev1, ev2;
885 struct mdp_superblock_1 *refsb =
886 (struct mdp_superblock_1*)page_address(refdev->sb_page);
888 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
889 sb->level != refsb->level ||
890 sb->layout != refsb->layout ||
891 sb->chunksize != refsb->chunksize) {
892 printk(KERN_WARNING "md: %s has strangely different"
893 " superblock to %s\n",
894 bdevname(rdev->bdev,b),
895 bdevname(refdev->bdev,b2));
896 return -EINVAL;
898 ev1 = le64_to_cpu(sb->events);
899 ev2 = le64_to_cpu(refsb->events);
901 if (ev1 > ev2)
902 return 1;
904 if (minor_version)
905 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
906 else
907 rdev->size = rdev->sb_offset;
908 if (rdev->size < le64_to_cpu(sb->data_size)/2)
909 return -EINVAL;
910 rdev->size = le64_to_cpu(sb->data_size)/2;
911 if (le32_to_cpu(sb->chunksize))
912 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
913 return 0;
916 static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
918 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
920 if (mddev->raid_disks == 0) {
921 mddev->major_version = 1;
922 mddev->minor_version = 0;
923 mddev->patch_version = 0;
924 mddev->persistent = 1;
925 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
926 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
927 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
928 mddev->level = le32_to_cpu(sb->level);
929 mddev->layout = le32_to_cpu(sb->layout);
930 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
931 mddev->size = (u32)le64_to_cpu(sb->size);
932 mddev->events = le64_to_cpu(sb->events);
934 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
935 memcpy(mddev->uuid, sb->set_uuid, 16);
937 mddev->max_disks = (4096-256)/2;
938 } else {
939 __u64 ev1;
940 ev1 = le64_to_cpu(sb->events);
941 ++ev1;
942 if (ev1 < mddev->events)
943 return -EINVAL;
946 if (mddev->level != LEVEL_MULTIPATH) {
947 int role;
948 rdev->desc_nr = le32_to_cpu(sb->dev_number);
949 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
950 switch(role) {
951 case 0xffff: /* spare */
952 rdev->in_sync = 0;
953 rdev->faulty = 0;
954 rdev->raid_disk = -1;
955 break;
956 case 0xfffe: /* faulty */
957 rdev->in_sync = 0;
958 rdev->faulty = 1;
959 rdev->raid_disk = -1;
960 break;
961 default:
962 rdev->in_sync = 1;
963 rdev->faulty = 0;
964 rdev->raid_disk = role;
965 break;
968 return 0;
971 static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
973 struct mdp_superblock_1 *sb;
974 struct list_head *tmp;
975 mdk_rdev_t *rdev2;
976 int max_dev, i;
977 /* make rdev->sb match mddev and rdev data. */
979 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
981 sb->feature_map = 0;
982 sb->pad0 = 0;
983 memset(sb->pad1, 0, sizeof(sb->pad1));
984 memset(sb->pad2, 0, sizeof(sb->pad2));
985 memset(sb->pad3, 0, sizeof(sb->pad3));
987 sb->utime = cpu_to_le64((__u64)mddev->utime);
988 sb->events = cpu_to_le64(mddev->events);
989 if (mddev->in_sync)
990 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
991 else
992 sb->resync_offset = cpu_to_le64(0);
994 max_dev = 0;
995 ITERATE_RDEV(mddev,rdev2,tmp)
996 if (rdev2->desc_nr > max_dev)
997 max_dev = rdev2->desc_nr;
999 sb->max_dev = max_dev;
1000 for (i=0; i<max_dev;i++)
1001 sb->dev_roles[max_dev] = cpu_to_le16(0xfffe);
1003 ITERATE_RDEV(mddev,rdev2,tmp) {
1004 i = rdev2->desc_nr;
1005 if (rdev2->faulty)
1006 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1007 else if (rdev2->in_sync)
1008 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1009 else
1010 sb->dev_roles[i] = cpu_to_le16(0xffff);
1013 sb->recovery_offset = cpu_to_le64(0); /* not supported yet */
1017 struct super_type super_types[] = {
1018 [0] = {
1019 .name = "0.90.0",
1020 .owner = THIS_MODULE,
1021 .load_super = super_90_load,
1022 .validate_super = super_90_validate,
1023 .sync_super = super_90_sync,
1025 [1] = {
1026 .name = "md-1",
1027 .owner = THIS_MODULE,
1028 .load_super = super_1_load,
1029 .validate_super = super_1_validate,
1030 .sync_super = super_1_sync,
1034 static mdk_rdev_t * match_dev_unit(mddev_t *mddev, mdk_rdev_t *dev)
1036 struct list_head *tmp;
1037 mdk_rdev_t *rdev;
1039 ITERATE_RDEV(mddev,rdev,tmp)
1040 if (rdev->bdev->bd_contains == dev->bdev->bd_contains)
1041 return rdev;
1043 return NULL;
1046 static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1048 struct list_head *tmp;
1049 mdk_rdev_t *rdev;
1051 ITERATE_RDEV(mddev1,rdev,tmp)
1052 if (match_dev_unit(mddev2, rdev))
1053 return 1;
1055 return 0;
1058 static LIST_HEAD(pending_raid_disks);
1060 static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1062 mdk_rdev_t *same_pdev;
1063 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1065 if (rdev->mddev) {
1066 MD_BUG();
1067 return -EINVAL;
1069 same_pdev = match_dev_unit(mddev, rdev);
1070 if (same_pdev)
1071 printk(KERN_WARNING
1072 "%s: WARNING: %s appears to be on the same physical"
1073 " disk as %s. True\n protection against single-disk"
1074 " failure might be compromised.\n",
1075 mdname(mddev), bdevname(rdev->bdev,b),
1076 bdevname(same_pdev->bdev,b2));
1078 /* Verify rdev->desc_nr is unique.
1079 * If it is -1, assign a free number, else
1080 * check number is not in use
1082 if (rdev->desc_nr < 0) {
1083 int choice = 0;
1084 if (mddev->pers) choice = mddev->raid_disks;
1085 while (find_rdev_nr(mddev, choice))
1086 choice++;
1087 rdev->desc_nr = choice;
1088 } else {
1089 if (find_rdev_nr(mddev, rdev->desc_nr))
1090 return -EBUSY;
1093 list_add(&rdev->same_set, &mddev->disks);
1094 rdev->mddev = mddev;
1095 printk(KERN_INFO "md: bind<%s>\n", bdevname(rdev->bdev,b));
1096 return 0;
1099 static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1101 char b[BDEVNAME_SIZE];
1102 if (!rdev->mddev) {
1103 MD_BUG();
1104 return;
1106 list_del_init(&rdev->same_set);
1107 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1108 rdev->mddev = NULL;
1112 * prevent the device from being mounted, repartitioned or
1113 * otherwise reused by a RAID array (or any other kernel
1114 * subsystem), by bd_claiming the device.
1116 static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1118 int err = 0;
1119 struct block_device *bdev;
1120 char b[BDEVNAME_SIZE];
1122 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1123 if (IS_ERR(bdev)) {
1124 printk(KERN_ERR "md: could not open %s.\n",
1125 __bdevname(dev, b));
1126 return PTR_ERR(bdev);
1128 err = bd_claim(bdev, rdev);
1129 if (err) {
1130 printk(KERN_ERR "md: could not bd_claim %s.\n",
1131 bdevname(bdev, b));
1132 blkdev_put(bdev);
1133 return err;
1135 rdev->bdev = bdev;
1136 return err;
1139 static void unlock_rdev(mdk_rdev_t *rdev)
1141 struct block_device *bdev = rdev->bdev;
1142 rdev->bdev = NULL;
1143 if (!bdev)
1144 MD_BUG();
1145 bd_release(bdev);
1146 blkdev_put(bdev);
1149 void md_autodetect_dev(dev_t dev);
1151 static void export_rdev(mdk_rdev_t * rdev)
1153 char b[BDEVNAME_SIZE];
1154 printk(KERN_INFO "md: export_rdev(%s)\n",
1155 bdevname(rdev->bdev,b));
1156 if (rdev->mddev)
1157 MD_BUG();
1158 free_disk_sb(rdev);
1159 list_del_init(&rdev->same_set);
1160 #ifndef MODULE
1161 md_autodetect_dev(rdev->bdev->bd_dev);
1162 #endif
1163 unlock_rdev(rdev);
1164 kfree(rdev);
1167 static void kick_rdev_from_array(mdk_rdev_t * rdev)
1169 unbind_rdev_from_array(rdev);
1170 export_rdev(rdev);
1173 static void export_array(mddev_t *mddev)
1175 struct list_head *tmp;
1176 mdk_rdev_t *rdev;
1178 ITERATE_RDEV(mddev,rdev,tmp) {
1179 if (!rdev->mddev) {
1180 MD_BUG();
1181 continue;
1183 kick_rdev_from_array(rdev);
1185 if (!list_empty(&mddev->disks))
1186 MD_BUG();
1187 mddev->raid_disks = 0;
1188 mddev->major_version = 0;
1191 static void print_desc(mdp_disk_t *desc)
1193 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1194 desc->major,desc->minor,desc->raid_disk,desc->state);
1197 static void print_sb(mdp_super_t *sb)
1199 int i;
1201 printk(KERN_INFO
1202 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1203 sb->major_version, sb->minor_version, sb->patch_version,
1204 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1205 sb->ctime);
1206 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1207 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1208 sb->md_minor, sb->layout, sb->chunk_size);
1209 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1210 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1211 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1212 sb->failed_disks, sb->spare_disks,
1213 sb->sb_csum, (unsigned long)sb->events_lo);
1215 printk(KERN_INFO);
1216 for (i = 0; i < MD_SB_DISKS; i++) {
1217 mdp_disk_t *desc;
1219 desc = sb->disks + i;
1220 if (desc->number || desc->major || desc->minor ||
1221 desc->raid_disk || (desc->state && (desc->state != 4))) {
1222 printk(" D %2d: ", i);
1223 print_desc(desc);
1226 printk(KERN_INFO "md: THIS: ");
1227 print_desc(&sb->this_disk);
1231 static void print_rdev(mdk_rdev_t *rdev)
1233 char b[BDEVNAME_SIZE];
1234 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1235 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
1236 rdev->faulty, rdev->in_sync, rdev->desc_nr);
1237 if (rdev->sb_loaded) {
1238 printk(KERN_INFO "md: rdev superblock:\n");
1239 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1240 } else
1241 printk(KERN_INFO "md: no rdev superblock!\n");
1244 void md_print_devices(void)
1246 struct list_head *tmp, *tmp2;
1247 mdk_rdev_t *rdev;
1248 mddev_t *mddev;
1249 char b[BDEVNAME_SIZE];
1251 printk("\n");
1252 printk("md: **********************************\n");
1253 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1254 printk("md: **********************************\n");
1255 ITERATE_MDDEV(mddev,tmp) {
1256 printk("%s: ", mdname(mddev));
1258 ITERATE_RDEV(mddev,rdev,tmp2)
1259 printk("<%s>", bdevname(rdev->bdev,b));
1260 printk("\n");
1262 ITERATE_RDEV(mddev,rdev,tmp2)
1263 print_rdev(rdev);
1265 printk("md: **********************************\n");
1266 printk("\n");
1270 static int write_disk_sb(mdk_rdev_t * rdev)
1272 char b[BDEVNAME_SIZE];
1273 if (!rdev->sb_loaded) {
1274 MD_BUG();
1275 return 1;
1277 if (rdev->faulty) {
1278 MD_BUG();
1279 return 1;
1282 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1283 bdevname(rdev->bdev,b),
1284 (unsigned long long)rdev->sb_offset);
1286 if (sync_page_io(rdev->bdev, rdev->sb_offset<<1, MD_SB_BYTES, rdev->sb_page, WRITE))
1287 return 0;
1289 printk("md: write_disk_sb failed for device %s\n",
1290 bdevname(rdev->bdev,b));
1291 return 1;
1294 static void sync_sbs(mddev_t * mddev)
1296 mdk_rdev_t *rdev;
1297 struct list_head *tmp;
1299 ITERATE_RDEV(mddev,rdev,tmp) {
1300 super_types[mddev->major_version].
1301 sync_super(mddev, rdev);
1302 rdev->sb_loaded = 1;
1306 static void md_update_sb(mddev_t * mddev)
1308 int err, count = 100;
1309 struct list_head *tmp;
1310 mdk_rdev_t *rdev;
1312 mddev->sb_dirty = 0;
1313 repeat:
1314 mddev->utime = get_seconds();
1315 mddev->events ++;
1317 if (!mddev->events) {
1319 * oops, this 64-bit counter should never wrap.
1320 * Either we are in around ~1 trillion A.C., assuming
1321 * 1 reboot per second, or we have a bug:
1323 MD_BUG();
1324 mddev->events --;
1326 sync_sbs(mddev);
1329 * do not write anything to disk if using
1330 * nonpersistent superblocks
1332 if (!mddev->persistent)
1333 return;
1335 dprintk(KERN_INFO
1336 "md: updating %s RAID superblock on device (in sync %d)\n",
1337 mdname(mddev),mddev->in_sync);
1339 err = 0;
1340 ITERATE_RDEV(mddev,rdev,tmp) {
1341 char b[BDEVNAME_SIZE];
1342 dprintk(KERN_INFO "md: ");
1343 if (rdev->faulty)
1344 dprintk("(skipping faulty ");
1346 dprintk("%s ", bdevname(rdev->bdev,b));
1347 if (!rdev->faulty) {
1348 err += write_disk_sb(rdev);
1349 } else
1350 dprintk(")\n");
1351 if (!err && mddev->level == LEVEL_MULTIPATH)
1352 /* only need to write one superblock... */
1353 break;
1355 if (err) {
1356 if (--count) {
1357 printk(KERN_ERR "md: errors occurred during superblock"
1358 " update, repeating\n");
1359 goto repeat;
1361 printk(KERN_ERR \
1362 "md: excessive errors occurred during superblock update, exiting\n");
1367 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1369 * mark the device faulty if:
1371 * - the device is nonexistent (zero size)
1372 * - the device has no valid superblock
1374 * a faulty rdev _never_ has rdev->sb set.
1376 static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
1378 char b[BDEVNAME_SIZE];
1379 int err;
1380 mdk_rdev_t *rdev;
1381 sector_t size;
1383 rdev = (mdk_rdev_t *) kmalloc(sizeof(*rdev), GFP_KERNEL);
1384 if (!rdev) {
1385 printk(KERN_ERR "md: could not alloc mem for new device!\n");
1386 return ERR_PTR(-ENOMEM);
1388 memset(rdev, 0, sizeof(*rdev));
1390 if ((err = alloc_disk_sb(rdev)))
1391 goto abort_free;
1393 err = lock_rdev(rdev, newdev);
1394 if (err)
1395 goto abort_free;
1397 rdev->desc_nr = -1;
1398 rdev->faulty = 0;
1399 rdev->in_sync = 0;
1400 rdev->data_offset = 0;
1401 atomic_set(&rdev->nr_pending, 0);
1403 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
1404 if (!size) {
1405 printk(KERN_WARNING
1406 "md: %s has zero or unknown size, marking faulty!\n",
1407 bdevname(rdev->bdev,b));
1408 err = -EINVAL;
1409 goto abort_free;
1412 if (super_format >= 0) {
1413 err = super_types[super_format].
1414 load_super(rdev, NULL, super_minor);
1415 if (err == -EINVAL) {
1416 printk(KERN_WARNING
1417 "md: %s has invalid sb, not importing!\n",
1418 bdevname(rdev->bdev,b));
1419 goto abort_free;
1421 if (err < 0) {
1422 printk(KERN_WARNING
1423 "md: could not read %s's sb, not importing!\n",
1424 bdevname(rdev->bdev,b));
1425 goto abort_free;
1428 INIT_LIST_HEAD(&rdev->same_set);
1430 return rdev;
1432 abort_free:
1433 if (rdev->sb_page) {
1434 if (rdev->bdev)
1435 unlock_rdev(rdev);
1436 free_disk_sb(rdev);
1438 kfree(rdev);
1439 return ERR_PTR(err);
1443 * Check a full RAID array for plausibility
1447 static int analyze_sbs(mddev_t * mddev)
1449 int i;
1450 struct list_head *tmp;
1451 mdk_rdev_t *rdev, *freshest;
1452 char b[BDEVNAME_SIZE];
1454 freshest = NULL;
1455 ITERATE_RDEV(mddev,rdev,tmp)
1456 switch (super_types[mddev->major_version].
1457 load_super(rdev, freshest, mddev->minor_version)) {
1458 case 1:
1459 freshest = rdev;
1460 break;
1461 case 0:
1462 break;
1463 default:
1464 printk( KERN_ERR \
1465 "md: fatal superblock inconsistency in %s"
1466 " -- removing from array\n",
1467 bdevname(rdev->bdev,b));
1468 kick_rdev_from_array(rdev);
1472 super_types[mddev->major_version].
1473 validate_super(mddev, freshest);
1475 i = 0;
1476 ITERATE_RDEV(mddev,rdev,tmp) {
1477 if (rdev != freshest)
1478 if (super_types[mddev->major_version].
1479 validate_super(mddev, rdev)) {
1480 printk(KERN_WARNING "md: kicking non-fresh %s"
1481 " from array!\n",
1482 bdevname(rdev->bdev,b));
1483 kick_rdev_from_array(rdev);
1484 continue;
1486 if (mddev->level == LEVEL_MULTIPATH) {
1487 rdev->desc_nr = i++;
1488 rdev->raid_disk = rdev->desc_nr;
1489 rdev->in_sync = 1;
1495 * Check if we can support this RAID array
1497 if (mddev->major_version != MD_MAJOR_VERSION ||
1498 mddev->minor_version > MD_MINOR_VERSION) {
1499 printk(KERN_ALERT
1500 "md: %s: unsupported raid array version %d.%d.%d\n",
1501 mdname(mddev), mddev->major_version,
1502 mddev->minor_version, mddev->patch_version);
1503 goto abort;
1506 if ((mddev->recovery_cp != MaxSector) &&
1507 ((mddev->level == 1) ||
1508 ((mddev->level >= 4) && (mddev->level <= 6))))
1509 printk(KERN_ERR "md: %s: raid array is not clean"
1510 " -- starting background reconstruction\n",
1511 mdname(mddev));
1513 return 0;
1514 abort:
1515 return 1;
1518 int mdp_major = 0;
1520 static struct kobject *md_probe(dev_t dev, int *part, void *data)
1522 static DECLARE_MUTEX(disks_sem);
1523 mddev_t *mddev = mddev_find(dev);
1524 struct gendisk *disk;
1525 int partitioned = (MAJOR(dev) != MD_MAJOR);
1526 int shift = partitioned ? MdpMinorShift : 0;
1527 int unit = MINOR(dev) >> shift;
1529 if (!mddev)
1530 return NULL;
1532 down(&disks_sem);
1533 if (mddev->gendisk) {
1534 up(&disks_sem);
1535 mddev_put(mddev);
1536 return NULL;
1538 disk = alloc_disk(1 << shift);
1539 if (!disk) {
1540 up(&disks_sem);
1541 mddev_put(mddev);
1542 return NULL;
1544 disk->major = MAJOR(dev);
1545 disk->first_minor = unit << shift;
1546 if (partitioned)
1547 sprintf(disk->disk_name, "md_d%d", unit);
1548 else
1549 sprintf(disk->disk_name, "md%d", unit);
1550 disk->fops = &md_fops;
1551 disk->private_data = mddev;
1552 disk->queue = mddev->queue;
1553 add_disk(disk);
1554 mddev->gendisk = disk;
1555 up(&disks_sem);
1556 return NULL;
1559 void md_wakeup_thread(mdk_thread_t *thread);
1561 static void md_safemode_timeout(unsigned long data)
1563 mddev_t *mddev = (mddev_t *) data;
1565 mddev->safemode = 1;
1566 md_wakeup_thread(mddev->thread);
1570 static int do_md_run(mddev_t * mddev)
1572 int pnum, err;
1573 int chunk_size;
1574 struct list_head *tmp;
1575 mdk_rdev_t *rdev;
1576 struct gendisk *disk;
1577 char b[BDEVNAME_SIZE];
1579 if (list_empty(&mddev->disks)) {
1580 MD_BUG();
1581 return -EINVAL;
1584 if (mddev->pers)
1585 return -EBUSY;
1588 * Analyze all RAID superblock(s)
1590 if (!mddev->raid_disks && analyze_sbs(mddev)) {
1591 MD_BUG();
1592 return -EINVAL;
1595 chunk_size = mddev->chunk_size;
1596 pnum = level_to_pers(mddev->level);
1598 if ((pnum != MULTIPATH) && (pnum != RAID1)) {
1599 if (!chunk_size) {
1601 * 'default chunksize' in the old md code used to
1602 * be PAGE_SIZE, baaad.
1603 * we abort here to be on the safe side. We don't
1604 * want to continue the bad practice.
1606 printk(KERN_ERR
1607 "no chunksize specified, see 'man raidtab'\n");
1608 return -EINVAL;
1610 if (chunk_size > MAX_CHUNK_SIZE) {
1611 printk(KERN_ERR "too big chunk_size: %d > %d\n",
1612 chunk_size, MAX_CHUNK_SIZE);
1613 return -EINVAL;
1616 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
1618 if ( (1 << ffz(~chunk_size)) != chunk_size) {
1619 MD_BUG();
1620 return -EINVAL;
1622 if (chunk_size < PAGE_SIZE) {
1623 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
1624 chunk_size, PAGE_SIZE);
1625 return -EINVAL;
1628 /* devices must have minimum size of one chunk */
1629 ITERATE_RDEV(mddev,rdev,tmp) {
1630 if (rdev->faulty)
1631 continue;
1632 if (rdev->size < chunk_size / 1024) {
1633 printk(KERN_WARNING
1634 "md: Dev %s smaller than chunk_size:"
1635 " %lluk < %dk\n",
1636 bdevname(rdev->bdev,b),
1637 (unsigned long long)rdev->size,
1638 chunk_size / 1024);
1639 return -EINVAL;
1644 if (pnum >= MAX_PERSONALITY) {
1645 MD_BUG();
1646 return -EINVAL;
1649 #ifdef CONFIG_KMOD
1650 if (!pers[pnum])
1652 request_module("md-personality-%d", pnum);
1654 #endif
1657 * Drop all container device buffers, from now on
1658 * the only valid external interface is through the md
1659 * device.
1660 * Also find largest hardsector size
1662 ITERATE_RDEV(mddev,rdev,tmp) {
1663 if (rdev->faulty)
1664 continue;
1665 sync_blockdev(rdev->bdev);
1666 invalidate_bdev(rdev->bdev, 0);
1669 md_probe(mddev->unit, NULL, NULL);
1670 disk = mddev->gendisk;
1671 if (!disk)
1672 return -ENOMEM;
1674 spin_lock(&pers_lock);
1675 if (!pers[pnum] || !try_module_get(pers[pnum]->owner)) {
1676 spin_unlock(&pers_lock);
1677 printk(KERN_WARNING "md: personality %d is not loaded!\n",
1678 pnum);
1679 return -EINVAL;
1682 mddev->pers = pers[pnum];
1683 spin_unlock(&pers_lock);
1685 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
1687 err = mddev->pers->run(mddev);
1688 if (err) {
1689 printk(KERN_ERR "md: pers->run() failed ...\n");
1690 module_put(mddev->pers->owner);
1691 mddev->pers = NULL;
1692 return -EINVAL;
1694 atomic_set(&mddev->writes_pending,0);
1695 mddev->safemode = 0;
1696 mddev->safemode_timer.function = md_safemode_timeout;
1697 mddev->safemode_timer.data = (unsigned long) mddev;
1698 mddev->safemode_delay = (20 * HZ)/1000 +1; /* 20 msec delay */
1699 mddev->in_sync = 1;
1701 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1703 if (mddev->sb_dirty)
1704 md_update_sb(mddev);
1706 set_capacity(disk, mddev->array_size<<1);
1708 /* If we call blk_queue_make_request here, it will
1709 * re-initialise max_sectors etc which may have been
1710 * refined inside -> run. So just set the bits we need to set.
1711 * Most initialisation happended when we called
1712 * blk_queue_make_request(..., md_fail_request)
1713 * earlier.
1715 mddev->queue->queuedata = mddev;
1716 mddev->queue->make_request_fn = mddev->pers->make_request;
1717 mddev->queue->issue_flush_fn = md_flush_all;
1719 mddev->changed = 1;
1720 return 0;
1723 static int restart_array(mddev_t *mddev)
1725 struct gendisk *disk = mddev->gendisk;
1726 int err;
1729 * Complain if it has no devices
1731 err = -ENXIO;
1732 if (list_empty(&mddev->disks))
1733 goto out;
1735 if (mddev->pers) {
1736 err = -EBUSY;
1737 if (!mddev->ro)
1738 goto out;
1740 mddev->safemode = 0;
1741 mddev->ro = 0;
1742 set_disk_ro(disk, 0);
1744 printk(KERN_INFO "md: %s switched to read-write mode.\n",
1745 mdname(mddev));
1747 * Kick recovery or resync if necessary
1749 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1750 md_wakeup_thread(mddev->thread);
1751 err = 0;
1752 } else {
1753 printk(KERN_ERR "md: %s has no personality assigned.\n",
1754 mdname(mddev));
1755 err = -EINVAL;
1758 out:
1759 return err;
1762 static int do_md_stop(mddev_t * mddev, int ro)
1764 int err = 0;
1765 struct gendisk *disk = mddev->gendisk;
1767 if (mddev->pers) {
1768 if (atomic_read(&mddev->active)>2) {
1769 printk("md: %s still in use.\n",mdname(mddev));
1770 return -EBUSY;
1773 if (mddev->sync_thread) {
1774 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1775 md_unregister_thread(mddev->sync_thread);
1776 mddev->sync_thread = NULL;
1779 del_timer_sync(&mddev->safemode_timer);
1781 invalidate_partition(disk, 0);
1783 if (ro) {
1784 err = -ENXIO;
1785 if (mddev->ro)
1786 goto out;
1787 mddev->ro = 1;
1788 } else {
1789 if (mddev->ro)
1790 set_disk_ro(disk, 0);
1791 blk_queue_make_request(mddev->queue, md_fail_request);
1792 mddev->pers->stop(mddev);
1793 module_put(mddev->pers->owner);
1794 mddev->pers = NULL;
1795 if (mddev->ro)
1796 mddev->ro = 0;
1798 if (!mddev->in_sync) {
1799 /* mark array as shutdown cleanly */
1800 mddev->in_sync = 1;
1801 md_update_sb(mddev);
1803 if (ro)
1804 set_disk_ro(disk, 1);
1807 * Free resources if final stop
1809 if (!ro) {
1810 struct gendisk *disk;
1811 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
1813 export_array(mddev);
1815 mddev->array_size = 0;
1816 disk = mddev->gendisk;
1817 if (disk)
1818 set_capacity(disk, 0);
1819 mddev->changed = 1;
1820 } else
1821 printk(KERN_INFO "md: %s switched to read-only mode.\n",
1822 mdname(mddev));
1823 err = 0;
1824 out:
1825 return err;
1828 static void autorun_array(mddev_t *mddev)
1830 mdk_rdev_t *rdev;
1831 struct list_head *tmp;
1832 int err;
1834 if (list_empty(&mddev->disks)) {
1835 MD_BUG();
1836 return;
1839 printk(KERN_INFO "md: running: ");
1841 ITERATE_RDEV(mddev,rdev,tmp) {
1842 char b[BDEVNAME_SIZE];
1843 printk("<%s>", bdevname(rdev->bdev,b));
1845 printk("\n");
1847 err = do_md_run (mddev);
1848 if (err) {
1849 printk(KERN_WARNING "md :do_md_run() returned %d\n", err);
1850 do_md_stop (mddev, 0);
1855 * lets try to run arrays based on all disks that have arrived
1856 * until now. (those are in pending_raid_disks)
1858 * the method: pick the first pending disk, collect all disks with
1859 * the same UUID, remove all from the pending list and put them into
1860 * the 'same_array' list. Then order this list based on superblock
1861 * update time (freshest comes first), kick out 'old' disks and
1862 * compare superblocks. If everything's fine then run it.
1864 * If "unit" is allocated, then bump its reference count
1866 static void autorun_devices(int part)
1868 struct list_head candidates;
1869 struct list_head *tmp;
1870 mdk_rdev_t *rdev0, *rdev;
1871 mddev_t *mddev;
1872 char b[BDEVNAME_SIZE];
1874 printk(KERN_INFO "md: autorun ...\n");
1875 while (!list_empty(&pending_raid_disks)) {
1876 dev_t dev;
1877 rdev0 = list_entry(pending_raid_disks.next,
1878 mdk_rdev_t, same_set);
1880 printk(KERN_INFO "md: considering %s ...\n",
1881 bdevname(rdev0->bdev,b));
1882 INIT_LIST_HEAD(&candidates);
1883 ITERATE_RDEV_PENDING(rdev,tmp)
1884 if (super_90_load(rdev, rdev0, 0) >= 0) {
1885 printk(KERN_INFO "md: adding %s ...\n",
1886 bdevname(rdev->bdev,b));
1887 list_move(&rdev->same_set, &candidates);
1890 * now we have a set of devices, with all of them having
1891 * mostly sane superblocks. It's time to allocate the
1892 * mddev.
1894 if (rdev0->preferred_minor < 0 || rdev0->preferred_minor >= MAX_MD_DEVS) {
1895 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
1896 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
1897 break;
1899 if (part)
1900 dev = MKDEV(mdp_major,
1901 rdev0->preferred_minor << MdpMinorShift);
1902 else
1903 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
1905 md_probe(dev, NULL, NULL);
1906 mddev = mddev_find(dev);
1907 if (!mddev) {
1908 printk(KERN_ERR
1909 "md: cannot allocate memory for md drive.\n");
1910 break;
1912 if (mddev_lock(mddev))
1913 printk(KERN_WARNING "md: %s locked, cannot run\n",
1914 mdname(mddev));
1915 else if (mddev->raid_disks || mddev->major_version
1916 || !list_empty(&mddev->disks)) {
1917 printk(KERN_WARNING
1918 "md: %s already running, cannot run %s\n",
1919 mdname(mddev), bdevname(rdev0->bdev,b));
1920 mddev_unlock(mddev);
1921 } else {
1922 printk(KERN_INFO "md: created %s\n", mdname(mddev));
1923 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
1924 list_del_init(&rdev->same_set);
1925 if (bind_rdev_to_array(rdev, mddev))
1926 export_rdev(rdev);
1928 autorun_array(mddev);
1929 mddev_unlock(mddev);
1931 /* on success, candidates will be empty, on error
1932 * it won't...
1934 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
1935 export_rdev(rdev);
1936 mddev_put(mddev);
1938 printk(KERN_INFO "md: ... autorun DONE.\n");
1942 * import RAID devices based on one partition
1943 * if possible, the array gets run as well.
1946 static int autostart_array(dev_t startdev)
1948 char b[BDEVNAME_SIZE];
1949 int err = -EINVAL, i;
1950 mdp_super_t *sb = NULL;
1951 mdk_rdev_t *start_rdev = NULL, *rdev;
1953 start_rdev = md_import_device(startdev, 0, 0);
1954 if (IS_ERR(start_rdev))
1955 return err;
1958 /* NOTE: this can only work for 0.90.0 superblocks */
1959 sb = (mdp_super_t*)page_address(start_rdev->sb_page);
1960 if (sb->major_version != 0 ||
1961 sb->minor_version != 90 ) {
1962 printk(KERN_WARNING "md: can only autostart 0.90.0 arrays\n");
1963 export_rdev(start_rdev);
1964 return err;
1967 if (start_rdev->faulty) {
1968 printk(KERN_WARNING
1969 "md: can not autostart based on faulty %s!\n",
1970 bdevname(start_rdev->bdev,b));
1971 export_rdev(start_rdev);
1972 return err;
1974 list_add(&start_rdev->same_set, &pending_raid_disks);
1976 for (i = 0; i < MD_SB_DISKS; i++) {
1977 mdp_disk_t *desc = sb->disks + i;
1978 dev_t dev = MKDEV(desc->major, desc->minor);
1980 if (!dev)
1981 continue;
1982 if (dev == startdev)
1983 continue;
1984 if (MAJOR(dev) != desc->major || MINOR(dev) != desc->minor)
1985 continue;
1986 rdev = md_import_device(dev, 0, 0);
1987 if (IS_ERR(rdev))
1988 continue;
1990 list_add(&rdev->same_set, &pending_raid_disks);
1994 * possibly return codes
1996 autorun_devices(0);
1997 return 0;
2002 static int get_version(void __user * arg)
2004 mdu_version_t ver;
2006 ver.major = MD_MAJOR_VERSION;
2007 ver.minor = MD_MINOR_VERSION;
2008 ver.patchlevel = MD_PATCHLEVEL_VERSION;
2010 if (copy_to_user(arg, &ver, sizeof(ver)))
2011 return -EFAULT;
2013 return 0;
2016 static int get_array_info(mddev_t * mddev, void __user * arg)
2018 mdu_array_info_t info;
2019 int nr,working,active,failed,spare;
2020 mdk_rdev_t *rdev;
2021 struct list_head *tmp;
2023 nr=working=active=failed=spare=0;
2024 ITERATE_RDEV(mddev,rdev,tmp) {
2025 nr++;
2026 if (rdev->faulty)
2027 failed++;
2028 else {
2029 working++;
2030 if (rdev->in_sync)
2031 active++;
2032 else
2033 spare++;
2037 info.major_version = mddev->major_version;
2038 info.minor_version = mddev->minor_version;
2039 info.patch_version = 1;
2040 info.ctime = mddev->ctime;
2041 info.level = mddev->level;
2042 info.size = mddev->size;
2043 info.nr_disks = nr;
2044 info.raid_disks = mddev->raid_disks;
2045 info.md_minor = mddev->md_minor;
2046 info.not_persistent= !mddev->persistent;
2048 info.utime = mddev->utime;
2049 info.state = 0;
2050 if (mddev->in_sync)
2051 info.state = (1<<MD_SB_CLEAN);
2052 info.active_disks = active;
2053 info.working_disks = working;
2054 info.failed_disks = failed;
2055 info.spare_disks = spare;
2057 info.layout = mddev->layout;
2058 info.chunk_size = mddev->chunk_size;
2060 if (copy_to_user(arg, &info, sizeof(info)))
2061 return -EFAULT;
2063 return 0;
2066 static int get_disk_info(mddev_t * mddev, void __user * arg)
2068 mdu_disk_info_t info;
2069 unsigned int nr;
2070 mdk_rdev_t *rdev;
2072 if (copy_from_user(&info, arg, sizeof(info)))
2073 return -EFAULT;
2075 nr = info.number;
2077 rdev = find_rdev_nr(mddev, nr);
2078 if (rdev) {
2079 info.major = MAJOR(rdev->bdev->bd_dev);
2080 info.minor = MINOR(rdev->bdev->bd_dev);
2081 info.raid_disk = rdev->raid_disk;
2082 info.state = 0;
2083 if (rdev->faulty)
2084 info.state |= (1<<MD_DISK_FAULTY);
2085 else if (rdev->in_sync) {
2086 info.state |= (1<<MD_DISK_ACTIVE);
2087 info.state |= (1<<MD_DISK_SYNC);
2089 } else {
2090 info.major = info.minor = 0;
2091 info.raid_disk = -1;
2092 info.state = (1<<MD_DISK_REMOVED);
2095 if (copy_to_user(arg, &info, sizeof(info)))
2096 return -EFAULT;
2098 return 0;
2101 static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
2103 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
2104 mdk_rdev_t *rdev;
2105 dev_t dev = MKDEV(info->major,info->minor);
2107 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
2108 return -EOVERFLOW;
2110 if (!mddev->raid_disks) {
2111 int err;
2112 /* expecting a device which has a superblock */
2113 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
2114 if (IS_ERR(rdev)) {
2115 printk(KERN_WARNING
2116 "md: md_import_device returned %ld\n",
2117 PTR_ERR(rdev));
2118 return PTR_ERR(rdev);
2120 if (!list_empty(&mddev->disks)) {
2121 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2122 mdk_rdev_t, same_set);
2123 int err = super_types[mddev->major_version]
2124 .load_super(rdev, rdev0, mddev->minor_version);
2125 if (err < 0) {
2126 printk(KERN_WARNING
2127 "md: %s has different UUID to %s\n",
2128 bdevname(rdev->bdev,b),
2129 bdevname(rdev0->bdev,b2));
2130 export_rdev(rdev);
2131 return -EINVAL;
2134 err = bind_rdev_to_array(rdev, mddev);
2135 if (err)
2136 export_rdev(rdev);
2137 return err;
2141 * add_new_disk can be used once the array is assembled
2142 * to add "hot spares". They must already have a superblock
2143 * written
2145 if (mddev->pers) {
2146 int err;
2147 if (!mddev->pers->hot_add_disk) {
2148 printk(KERN_WARNING
2149 "%s: personality does not support diskops!\n",
2150 mdname(mddev));
2151 return -EINVAL;
2153 rdev = md_import_device(dev, mddev->major_version,
2154 mddev->minor_version);
2155 if (IS_ERR(rdev)) {
2156 printk(KERN_WARNING
2157 "md: md_import_device returned %ld\n",
2158 PTR_ERR(rdev));
2159 return PTR_ERR(rdev);
2161 rdev->in_sync = 0; /* just to be sure */
2162 rdev->raid_disk = -1;
2163 err = bind_rdev_to_array(rdev, mddev);
2164 if (err)
2165 export_rdev(rdev);
2166 if (mddev->thread)
2167 md_wakeup_thread(mddev->thread);
2168 return err;
2171 /* otherwise, add_new_disk is only allowed
2172 * for major_version==0 superblocks
2174 if (mddev->major_version != 0) {
2175 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
2176 mdname(mddev));
2177 return -EINVAL;
2180 if (!(info->state & (1<<MD_DISK_FAULTY))) {
2181 int err;
2182 rdev = md_import_device (dev, -1, 0);
2183 if (IS_ERR(rdev)) {
2184 printk(KERN_WARNING
2185 "md: error, md_import_device() returned %ld\n",
2186 PTR_ERR(rdev));
2187 return PTR_ERR(rdev);
2189 rdev->desc_nr = info->number;
2190 if (info->raid_disk < mddev->raid_disks)
2191 rdev->raid_disk = info->raid_disk;
2192 else
2193 rdev->raid_disk = -1;
2195 rdev->faulty = 0;
2196 if (rdev->raid_disk < mddev->raid_disks)
2197 rdev->in_sync = (info->state & (1<<MD_DISK_SYNC));
2198 else
2199 rdev->in_sync = 0;
2201 err = bind_rdev_to_array(rdev, mddev);
2202 if (err) {
2203 export_rdev(rdev);
2204 return err;
2207 if (!mddev->persistent) {
2208 printk(KERN_INFO "md: nonpersistent superblock ...\n");
2209 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2210 } else
2211 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2212 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
2214 if (!mddev->size || (mddev->size > rdev->size))
2215 mddev->size = rdev->size;
2218 return 0;
2221 static int hot_remove_disk(mddev_t * mddev, dev_t dev)
2223 char b[BDEVNAME_SIZE];
2224 mdk_rdev_t *rdev;
2226 if (!mddev->pers)
2227 return -ENODEV;
2229 rdev = find_rdev(mddev, dev);
2230 if (!rdev)
2231 return -ENXIO;
2233 if (rdev->raid_disk >= 0)
2234 goto busy;
2236 kick_rdev_from_array(rdev);
2237 md_update_sb(mddev);
2239 return 0;
2240 busy:
2241 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
2242 bdevname(rdev->bdev,b), mdname(mddev));
2243 return -EBUSY;
2246 static int hot_add_disk(mddev_t * mddev, dev_t dev)
2248 char b[BDEVNAME_SIZE];
2249 int err;
2250 unsigned int size;
2251 mdk_rdev_t *rdev;
2253 if (!mddev->pers)
2254 return -ENODEV;
2256 if (mddev->major_version != 0) {
2257 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
2258 " version-0 superblocks.\n",
2259 mdname(mddev));
2260 return -EINVAL;
2262 if (!mddev->pers->hot_add_disk) {
2263 printk(KERN_WARNING
2264 "%s: personality does not support diskops!\n",
2265 mdname(mddev));
2266 return -EINVAL;
2269 rdev = md_import_device (dev, -1, 0);
2270 if (IS_ERR(rdev)) {
2271 printk(KERN_WARNING
2272 "md: error, md_import_device() returned %ld\n",
2273 PTR_ERR(rdev));
2274 return -EINVAL;
2277 if (mddev->persistent)
2278 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2279 else
2280 rdev->sb_offset =
2281 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2283 size = calc_dev_size(rdev, mddev->chunk_size);
2284 rdev->size = size;
2286 if (size < mddev->size) {
2287 printk(KERN_WARNING
2288 "%s: disk size %llu blocks < array size %llu\n",
2289 mdname(mddev), (unsigned long long)size,
2290 (unsigned long long)mddev->size);
2291 err = -ENOSPC;
2292 goto abort_export;
2295 if (rdev->faulty) {
2296 printk(KERN_WARNING
2297 "md: can not hot-add faulty %s disk to %s!\n",
2298 bdevname(rdev->bdev,b), mdname(mddev));
2299 err = -EINVAL;
2300 goto abort_export;
2302 rdev->in_sync = 0;
2303 rdev->desc_nr = -1;
2304 bind_rdev_to_array(rdev, mddev);
2307 * The rest should better be atomic, we can have disk failures
2308 * noticed in interrupt contexts ...
2311 if (rdev->desc_nr == mddev->max_disks) {
2312 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
2313 mdname(mddev));
2314 err = -EBUSY;
2315 goto abort_unbind_export;
2318 rdev->raid_disk = -1;
2320 md_update_sb(mddev);
2323 * Kick recovery, maybe this spare has to be added to the
2324 * array immediately.
2326 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2327 md_wakeup_thread(mddev->thread);
2329 return 0;
2331 abort_unbind_export:
2332 unbind_rdev_from_array(rdev);
2334 abort_export:
2335 export_rdev(rdev);
2336 return err;
2340 * set_array_info is used two different ways
2341 * The original usage is when creating a new array.
2342 * In this usage, raid_disks is > 0 and it together with
2343 * level, size, not_persistent,layout,chunksize determine the
2344 * shape of the array.
2345 * This will always create an array with a type-0.90.0 superblock.
2346 * The newer usage is when assembling an array.
2347 * In this case raid_disks will be 0, and the major_version field is
2348 * use to determine which style super-blocks are to be found on the devices.
2349 * The minor and patch _version numbers are also kept incase the
2350 * super_block handler wishes to interpret them.
2352 static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
2355 if (info->raid_disks == 0) {
2356 /* just setting version number for superblock loading */
2357 if (info->major_version < 0 ||
2358 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
2359 super_types[info->major_version].name == NULL) {
2360 /* maybe try to auto-load a module? */
2361 printk(KERN_INFO
2362 "md: superblock version %d not known\n",
2363 info->major_version);
2364 return -EINVAL;
2366 mddev->major_version = info->major_version;
2367 mddev->minor_version = info->minor_version;
2368 mddev->patch_version = info->patch_version;
2369 return 0;
2371 mddev->major_version = MD_MAJOR_VERSION;
2372 mddev->minor_version = MD_MINOR_VERSION;
2373 mddev->patch_version = MD_PATCHLEVEL_VERSION;
2374 mddev->ctime = get_seconds();
2376 mddev->level = info->level;
2377 mddev->size = info->size;
2378 mddev->raid_disks = info->raid_disks;
2379 /* don't set md_minor, it is determined by which /dev/md* was
2380 * openned
2382 if (info->state & (1<<MD_SB_CLEAN))
2383 mddev->recovery_cp = MaxSector;
2384 else
2385 mddev->recovery_cp = 0;
2386 mddev->persistent = ! info->not_persistent;
2388 mddev->layout = info->layout;
2389 mddev->chunk_size = info->chunk_size;
2391 mddev->max_disks = MD_SB_DISKS;
2393 mddev->sb_dirty = 1;
2396 * Generate a 128 bit UUID
2398 get_random_bytes(mddev->uuid, 16);
2400 return 0;
2404 * update_array_info is used to change the configuration of an
2405 * on-line array.
2406 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
2407 * fields in the info are checked against the array.
2408 * Any differences that cannot be handled will cause an error.
2409 * Normally, only one change can be managed at a time.
2411 static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
2413 int rv = 0;
2414 int cnt = 0;
2416 if (mddev->major_version != info->major_version ||
2417 mddev->minor_version != info->minor_version ||
2418 /* mddev->patch_version != info->patch_version || */
2419 mddev->ctime != info->ctime ||
2420 mddev->level != info->level ||
2421 mddev->layout != info->layout ||
2422 !mddev->persistent != info->not_persistent||
2423 mddev->chunk_size != info->chunk_size )
2424 return -EINVAL;
2425 /* Check there is only one change */
2426 if (mddev->size != info->size) cnt++;
2427 if (mddev->raid_disks != info->raid_disks) cnt++;
2428 if (cnt == 0) return 0;
2429 if (cnt > 1) return -EINVAL;
2431 if (mddev->size != info->size) {
2432 mdk_rdev_t * rdev;
2433 struct list_head *tmp;
2434 if (mddev->pers->resize == NULL)
2435 return -EINVAL;
2436 /* The "size" is the amount of each device that is used.
2437 * This can only make sense for arrays with redundancy.
2438 * linear and raid0 always use whatever space is available
2439 * We can only consider changing the size of no resync
2440 * or reconstruction is happening, and if the new size
2441 * is acceptable. It must fit before the sb_offset or,
2442 * if that is <data_offset, it must fit before the
2443 * size of each device.
2444 * If size is zero, we find the largest size that fits.
2446 if (mddev->sync_thread)
2447 return -EBUSY;
2448 ITERATE_RDEV(mddev,rdev,tmp) {
2449 sector_t avail;
2450 int fit = (info->size == 0);
2451 if (rdev->sb_offset > rdev->data_offset)
2452 avail = (rdev->sb_offset*2) - rdev->data_offset;
2453 else
2454 avail = get_capacity(rdev->bdev->bd_disk)
2455 - rdev->data_offset;
2456 if (fit && (info->size == 0 || info->size > avail/2))
2457 info->size = avail/2;
2458 if (avail < ((sector_t)info->size << 1))
2459 return -ENOSPC;
2461 rv = mddev->pers->resize(mddev, (sector_t)info->size *2);
2462 if (!rv) {
2463 struct block_device *bdev;
2465 bdev = bdget_disk(mddev->gendisk, 0);
2466 if (bdev) {
2467 down(&bdev->bd_inode->i_sem);
2468 i_size_write(bdev->bd_inode, mddev->array_size << 10);
2469 up(&bdev->bd_inode->i_sem);
2470 bdput(bdev);
2474 if (mddev->raid_disks != info->raid_disks) {
2475 /* change the number of raid disks */
2476 if (mddev->pers->reshape == NULL)
2477 return -EINVAL;
2478 if (info->raid_disks <= 0 ||
2479 info->raid_disks >= mddev->max_disks)
2480 return -EINVAL;
2481 if (mddev->sync_thread)
2482 return -EBUSY;
2483 rv = mddev->pers->reshape(mddev, info->raid_disks);
2484 if (!rv) {
2485 struct block_device *bdev;
2487 bdev = bdget_disk(mddev->gendisk, 0);
2488 if (bdev) {
2489 down(&bdev->bd_inode->i_sem);
2490 i_size_write(bdev->bd_inode, mddev->array_size << 10);
2491 up(&bdev->bd_inode->i_sem);
2492 bdput(bdev);
2496 md_update_sb(mddev);
2497 return rv;
2500 static int set_disk_faulty(mddev_t *mddev, dev_t dev)
2502 mdk_rdev_t *rdev;
2504 rdev = find_rdev(mddev, dev);
2505 if (!rdev)
2506 return -ENODEV;
2508 md_error(mddev, rdev);
2509 return 0;
2512 static int md_ioctl(struct inode *inode, struct file *file,
2513 unsigned int cmd, unsigned long arg)
2515 int err = 0;
2516 void __user *argp = (void __user *)arg;
2517 struct hd_geometry __user *loc = argp;
2518 mddev_t *mddev = NULL;
2520 if (!capable(CAP_SYS_ADMIN))
2521 return -EACCES;
2524 * Commands dealing with the RAID driver but not any
2525 * particular array:
2527 switch (cmd)
2529 case RAID_VERSION:
2530 err = get_version(argp);
2531 goto done;
2533 case PRINT_RAID_DEBUG:
2534 err = 0;
2535 md_print_devices();
2536 goto done;
2538 #ifndef MODULE
2539 case RAID_AUTORUN:
2540 err = 0;
2541 autostart_arrays(arg);
2542 goto done;
2543 #endif
2544 default:;
2548 * Commands creating/starting a new array:
2551 mddev = inode->i_bdev->bd_disk->private_data;
2553 if (!mddev) {
2554 BUG();
2555 goto abort;
2559 if (cmd == START_ARRAY) {
2560 /* START_ARRAY doesn't need to lock the array as autostart_array
2561 * does the locking, and it could even be a different array
2563 static int cnt = 3;
2564 if (cnt > 0 ) {
2565 printk(KERN_WARNING
2566 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
2567 "This will not be supported beyond 2.6\n",
2568 current->comm, current->pid);
2569 cnt--;
2571 err = autostart_array(new_decode_dev(arg));
2572 if (err) {
2573 printk(KERN_WARNING "md: autostart failed!\n");
2574 goto abort;
2576 goto done;
2579 err = mddev_lock(mddev);
2580 if (err) {
2581 printk(KERN_INFO
2582 "md: ioctl lock interrupted, reason %d, cmd %d\n",
2583 err, cmd);
2584 goto abort;
2587 switch (cmd)
2589 case SET_ARRAY_INFO:
2591 mdu_array_info_t info;
2592 if (!arg)
2593 memset(&info, 0, sizeof(info));
2594 else if (copy_from_user(&info, argp, sizeof(info))) {
2595 err = -EFAULT;
2596 goto abort_unlock;
2598 if (mddev->pers) {
2599 err = update_array_info(mddev, &info);
2600 if (err) {
2601 printk(KERN_WARNING "md: couldn't update"
2602 " array info. %d\n", err);
2603 goto abort_unlock;
2605 goto done_unlock;
2607 if (!list_empty(&mddev->disks)) {
2608 printk(KERN_WARNING
2609 "md: array %s already has disks!\n",
2610 mdname(mddev));
2611 err = -EBUSY;
2612 goto abort_unlock;
2614 if (mddev->raid_disks) {
2615 printk(KERN_WARNING
2616 "md: array %s already initialised!\n",
2617 mdname(mddev));
2618 err = -EBUSY;
2619 goto abort_unlock;
2621 err = set_array_info(mddev, &info);
2622 if (err) {
2623 printk(KERN_WARNING "md: couldn't set"
2624 " array info. %d\n", err);
2625 goto abort_unlock;
2628 goto done_unlock;
2630 default:;
2634 * Commands querying/configuring an existing array:
2636 /* if we are initialised yet, only ADD_NEW_DISK or STOP_ARRAY is allowed */
2637 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY && cmd != RUN_ARRAY) {
2638 err = -ENODEV;
2639 goto abort_unlock;
2643 * Commands even a read-only array can execute:
2645 switch (cmd)
2647 case GET_ARRAY_INFO:
2648 err = get_array_info(mddev, argp);
2649 goto done_unlock;
2651 case GET_DISK_INFO:
2652 err = get_disk_info(mddev, argp);
2653 goto done_unlock;
2655 case RESTART_ARRAY_RW:
2656 err = restart_array(mddev);
2657 goto done_unlock;
2659 case STOP_ARRAY:
2660 err = do_md_stop (mddev, 0);
2661 goto done_unlock;
2663 case STOP_ARRAY_RO:
2664 err = do_md_stop (mddev, 1);
2665 goto done_unlock;
2668 * We have a problem here : there is no easy way to give a CHS
2669 * virtual geometry. We currently pretend that we have a 2 heads
2670 * 4 sectors (with a BIG number of cylinders...). This drives
2671 * dosfs just mad... ;-)
2673 case HDIO_GETGEO:
2674 if (!loc) {
2675 err = -EINVAL;
2676 goto abort_unlock;
2678 err = put_user (2, (char __user *) &loc->heads);
2679 if (err)
2680 goto abort_unlock;
2681 err = put_user (4, (char __user *) &loc->sectors);
2682 if (err)
2683 goto abort_unlock;
2684 err = put_user(get_capacity(mddev->gendisk)/8,
2685 (short __user *) &loc->cylinders);
2686 if (err)
2687 goto abort_unlock;
2688 err = put_user (get_start_sect(inode->i_bdev),
2689 (long __user *) &loc->start);
2690 goto done_unlock;
2694 * The remaining ioctls are changing the state of the
2695 * superblock, so we do not allow read-only arrays
2696 * here:
2698 if (mddev->ro) {
2699 err = -EROFS;
2700 goto abort_unlock;
2703 switch (cmd)
2705 case ADD_NEW_DISK:
2707 mdu_disk_info_t info;
2708 if (copy_from_user(&info, argp, sizeof(info)))
2709 err = -EFAULT;
2710 else
2711 err = add_new_disk(mddev, &info);
2712 goto done_unlock;
2715 case HOT_REMOVE_DISK:
2716 err = hot_remove_disk(mddev, new_decode_dev(arg));
2717 goto done_unlock;
2719 case HOT_ADD_DISK:
2720 err = hot_add_disk(mddev, new_decode_dev(arg));
2721 goto done_unlock;
2723 case SET_DISK_FAULTY:
2724 err = set_disk_faulty(mddev, new_decode_dev(arg));
2725 goto done_unlock;
2727 case RUN_ARRAY:
2728 err = do_md_run (mddev);
2729 goto done_unlock;
2731 default:
2732 if (_IOC_TYPE(cmd) == MD_MAJOR)
2733 printk(KERN_WARNING "md: %s(pid %d) used"
2734 " obsolete MD ioctl, upgrade your"
2735 " software to use new ictls.\n",
2736 current->comm, current->pid);
2737 err = -EINVAL;
2738 goto abort_unlock;
2741 done_unlock:
2742 abort_unlock:
2743 mddev_unlock(mddev);
2745 return err;
2746 done:
2747 if (err)
2748 MD_BUG();
2749 abort:
2750 return err;
2753 static int md_open(struct inode *inode, struct file *file)
2756 * Succeed if we can lock the mddev, which confirms that
2757 * it isn't being stopped right now.
2759 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
2760 int err;
2762 if ((err = mddev_lock(mddev)))
2763 goto out;
2765 err = 0;
2766 mddev_get(mddev);
2767 mddev_unlock(mddev);
2769 check_disk_change(inode->i_bdev);
2770 out:
2771 return err;
2774 static int md_release(struct inode *inode, struct file * file)
2776 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
2778 if (!mddev)
2779 BUG();
2780 mddev_put(mddev);
2782 return 0;
2785 static int md_media_changed(struct gendisk *disk)
2787 mddev_t *mddev = disk->private_data;
2789 return mddev->changed;
2792 static int md_revalidate(struct gendisk *disk)
2794 mddev_t *mddev = disk->private_data;
2796 mddev->changed = 0;
2797 return 0;
2799 static struct block_device_operations md_fops =
2801 .owner = THIS_MODULE,
2802 .open = md_open,
2803 .release = md_release,
2804 .ioctl = md_ioctl,
2805 .media_changed = md_media_changed,
2806 .revalidate_disk= md_revalidate,
2809 int md_thread(void * arg)
2811 mdk_thread_t *thread = arg;
2813 lock_kernel();
2816 * Detach thread
2819 daemonize(thread->name, mdname(thread->mddev));
2821 current->exit_signal = SIGCHLD;
2822 allow_signal(SIGKILL);
2823 thread->tsk = current;
2826 * md_thread is a 'system-thread', it's priority should be very
2827 * high. We avoid resource deadlocks individually in each
2828 * raid personality. (RAID5 does preallocation) We also use RR and
2829 * the very same RT priority as kswapd, thus we will never get
2830 * into a priority inversion deadlock.
2832 * we definitely have to have equal or higher priority than
2833 * bdflush, otherwise bdflush will deadlock if there are too
2834 * many dirty RAID5 blocks.
2836 unlock_kernel();
2838 complete(thread->event);
2839 while (thread->run) {
2840 void (*run)(mddev_t *);
2842 wait_event_interruptible(thread->wqueue,
2843 test_bit(THREAD_WAKEUP, &thread->flags));
2844 if (current->flags & PF_FREEZE)
2845 refrigerator(PF_FREEZE);
2847 clear_bit(THREAD_WAKEUP, &thread->flags);
2849 run = thread->run;
2850 if (run)
2851 run(thread->mddev);
2853 if (signal_pending(current))
2854 flush_signals(current);
2856 complete(thread->event);
2857 return 0;
2860 void md_wakeup_thread(mdk_thread_t *thread)
2862 if (thread) {
2863 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
2864 set_bit(THREAD_WAKEUP, &thread->flags);
2865 wake_up(&thread->wqueue);
2869 mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
2870 const char *name)
2872 mdk_thread_t *thread;
2873 int ret;
2874 struct completion event;
2876 thread = (mdk_thread_t *) kmalloc
2877 (sizeof(mdk_thread_t), GFP_KERNEL);
2878 if (!thread)
2879 return NULL;
2881 memset(thread, 0, sizeof(mdk_thread_t));
2882 init_waitqueue_head(&thread->wqueue);
2884 init_completion(&event);
2885 thread->event = &event;
2886 thread->run = run;
2887 thread->mddev = mddev;
2888 thread->name = name;
2889 ret = kernel_thread(md_thread, thread, 0);
2890 if (ret < 0) {
2891 kfree(thread);
2892 return NULL;
2894 wait_for_completion(&event);
2895 return thread;
2898 static void md_interrupt_thread(mdk_thread_t *thread)
2900 if (!thread->tsk) {
2901 MD_BUG();
2902 return;
2904 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
2905 send_sig(SIGKILL, thread->tsk, 1);
2908 void md_unregister_thread(mdk_thread_t *thread)
2910 struct completion event;
2912 init_completion(&event);
2914 thread->event = &event;
2915 thread->run = NULL;
2916 thread->name = NULL;
2917 md_interrupt_thread(thread);
2918 wait_for_completion(&event);
2919 kfree(thread);
2922 void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
2924 if (!mddev) {
2925 MD_BUG();
2926 return;
2929 if (!rdev || rdev->faulty)
2930 return;
2932 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
2933 mdname(mddev),
2934 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
2935 __builtin_return_address(0),__builtin_return_address(1),
2936 __builtin_return_address(2),__builtin_return_address(3));
2938 if (!mddev->pers->error_handler)
2939 return;
2940 mddev->pers->error_handler(mddev,rdev);
2941 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2942 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2943 md_wakeup_thread(mddev->thread);
2946 /* seq_file implementation /proc/mdstat */
2948 static void status_unused(struct seq_file *seq)
2950 int i = 0;
2951 mdk_rdev_t *rdev;
2952 struct list_head *tmp;
2954 seq_printf(seq, "unused devices: ");
2956 ITERATE_RDEV_PENDING(rdev,tmp) {
2957 char b[BDEVNAME_SIZE];
2958 i++;
2959 seq_printf(seq, "%s ",
2960 bdevname(rdev->bdev,b));
2962 if (!i)
2963 seq_printf(seq, "<none>");
2965 seq_printf(seq, "\n");
2969 static void status_resync(struct seq_file *seq, mddev_t * mddev)
2971 unsigned long max_blocks, resync, res, dt, db, rt;
2973 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
2975 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2976 max_blocks = mddev->resync_max_sectors >> 1;
2977 else
2978 max_blocks = mddev->size;
2981 * Should not happen.
2983 if (!max_blocks) {
2984 MD_BUG();
2985 return;
2987 res = (resync/1024)*1000/(max_blocks/1024 + 1);
2989 int i, x = res/50, y = 20-x;
2990 seq_printf(seq, "[");
2991 for (i = 0; i < x; i++)
2992 seq_printf(seq, "=");
2993 seq_printf(seq, ">");
2994 for (i = 0; i < y; i++)
2995 seq_printf(seq, ".");
2996 seq_printf(seq, "] ");
2998 seq_printf(seq, " %s =%3lu.%lu%% (%lu/%lu)",
2999 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
3000 "resync" : "recovery"),
3001 res/10, res % 10, resync, max_blocks);
3004 * We do not want to overflow, so the order of operands and
3005 * the * 100 / 100 trick are important. We do a +1 to be
3006 * safe against division by zero. We only estimate anyway.
3008 * dt: time from mark until now
3009 * db: blocks written from mark until now
3010 * rt: remaining time
3012 dt = ((jiffies - mddev->resync_mark) / HZ);
3013 if (!dt) dt++;
3014 db = resync - (mddev->resync_mark_cnt/2);
3015 rt = (dt * ((max_blocks-resync) / (db/100+1)))/100;
3017 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
3019 seq_printf(seq, " speed=%ldK/sec", db/dt);
3022 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
3024 struct list_head *tmp;
3025 loff_t l = *pos;
3026 mddev_t *mddev;
3028 if (l >= 0x10000)
3029 return NULL;
3030 if (!l--)
3031 /* header */
3032 return (void*)1;
3034 spin_lock(&all_mddevs_lock);
3035 list_for_each(tmp,&all_mddevs)
3036 if (!l--) {
3037 mddev = list_entry(tmp, mddev_t, all_mddevs);
3038 mddev_get(mddev);
3039 spin_unlock(&all_mddevs_lock);
3040 return mddev;
3042 spin_unlock(&all_mddevs_lock);
3043 if (!l--)
3044 return (void*)2;/* tail */
3045 return NULL;
3048 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3050 struct list_head *tmp;
3051 mddev_t *next_mddev, *mddev = v;
3053 ++*pos;
3054 if (v == (void*)2)
3055 return NULL;
3057 spin_lock(&all_mddevs_lock);
3058 if (v == (void*)1)
3059 tmp = all_mddevs.next;
3060 else
3061 tmp = mddev->all_mddevs.next;
3062 if (tmp != &all_mddevs)
3063 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
3064 else {
3065 next_mddev = (void*)2;
3066 *pos = 0x10000;
3068 spin_unlock(&all_mddevs_lock);
3070 if (v != (void*)1)
3071 mddev_put(mddev);
3072 return next_mddev;
3076 static void md_seq_stop(struct seq_file *seq, void *v)
3078 mddev_t *mddev = v;
3080 if (mddev && v != (void*)1 && v != (void*)2)
3081 mddev_put(mddev);
3084 static int md_seq_show(struct seq_file *seq, void *v)
3086 mddev_t *mddev = v;
3087 sector_t size;
3088 struct list_head *tmp2;
3089 mdk_rdev_t *rdev;
3090 int i;
3092 if (v == (void*)1) {
3093 seq_printf(seq, "Personalities : ");
3094 spin_lock(&pers_lock);
3095 for (i = 0; i < MAX_PERSONALITY; i++)
3096 if (pers[i])
3097 seq_printf(seq, "[%s] ", pers[i]->name);
3099 spin_unlock(&pers_lock);
3100 seq_printf(seq, "\n");
3101 return 0;
3103 if (v == (void*)2) {
3104 status_unused(seq);
3105 return 0;
3108 if (mddev_lock(mddev)!=0)
3109 return -EINTR;
3110 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
3111 seq_printf(seq, "%s : %sactive", mdname(mddev),
3112 mddev->pers ? "" : "in");
3113 if (mddev->pers) {
3114 if (mddev->ro)
3115 seq_printf(seq, " (read-only)");
3116 seq_printf(seq, " %s", mddev->pers->name);
3119 size = 0;
3120 ITERATE_RDEV(mddev,rdev,tmp2) {
3121 char b[BDEVNAME_SIZE];
3122 seq_printf(seq, " %s[%d]",
3123 bdevname(rdev->bdev,b), rdev->desc_nr);
3124 if (rdev->faulty) {
3125 seq_printf(seq, "(F)");
3126 continue;
3128 size += rdev->size;
3131 if (!list_empty(&mddev->disks)) {
3132 if (mddev->pers)
3133 seq_printf(seq, "\n %llu blocks",
3134 (unsigned long long)mddev->array_size);
3135 else
3136 seq_printf(seq, "\n %llu blocks",
3137 (unsigned long long)size);
3140 if (mddev->pers) {
3141 mddev->pers->status (seq, mddev);
3142 seq_printf(seq, "\n ");
3143 if (mddev->curr_resync > 2)
3144 status_resync (seq, mddev);
3145 else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
3146 seq_printf(seq, " resync=DELAYED");
3149 seq_printf(seq, "\n");
3151 mddev_unlock(mddev);
3153 return 0;
3156 static struct seq_operations md_seq_ops = {
3157 .start = md_seq_start,
3158 .next = md_seq_next,
3159 .stop = md_seq_stop,
3160 .show = md_seq_show,
3163 static int md_seq_open(struct inode *inode, struct file *file)
3165 int error;
3167 error = seq_open(file, &md_seq_ops);
3168 return error;
3171 static struct file_operations md_seq_fops = {
3172 .open = md_seq_open,
3173 .read = seq_read,
3174 .llseek = seq_lseek,
3175 .release = seq_release,
3178 int register_md_personality(int pnum, mdk_personality_t *p)
3180 if (pnum >= MAX_PERSONALITY) {
3181 printk(KERN_ERR
3182 "md: tried to install personality %s as nr %d, but max is %lu\n",
3183 p->name, pnum, MAX_PERSONALITY-1);
3184 return -EINVAL;
3187 spin_lock(&pers_lock);
3188 if (pers[pnum]) {
3189 spin_unlock(&pers_lock);
3190 MD_BUG();
3191 return -EBUSY;
3194 pers[pnum] = p;
3195 printk(KERN_INFO "md: %s personality registered as nr %d\n", p->name, pnum);
3196 spin_unlock(&pers_lock);
3197 return 0;
3200 int unregister_md_personality(int pnum)
3202 if (pnum >= MAX_PERSONALITY) {
3203 MD_BUG();
3204 return -EINVAL;
3207 printk(KERN_INFO "md: %s personality unregistered\n", pers[pnum]->name);
3208 spin_lock(&pers_lock);
3209 pers[pnum] = NULL;
3210 spin_unlock(&pers_lock);
3211 return 0;
3214 static int is_mddev_idle(mddev_t *mddev)
3216 mdk_rdev_t * rdev;
3217 struct list_head *tmp;
3218 int idle;
3219 unsigned long curr_events;
3221 idle = 1;
3222 ITERATE_RDEV(mddev,rdev,tmp) {
3223 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
3224 curr_events = disk_stat_read(disk, read_sectors) +
3225 disk_stat_read(disk, write_sectors) -
3226 atomic_read(&disk->sync_io);
3227 /* Allow some slack between valud of curr_events and last_events,
3228 * as there are some uninteresting races.
3229 * Note: the following is an unsigned comparison.
3231 if ((curr_events - rdev->last_events + 32) > 64) {
3232 rdev->last_events = curr_events;
3233 idle = 0;
3236 return idle;
3239 void md_done_sync(mddev_t *mddev, int blocks, int ok)
3241 /* another "blocks" (512byte) blocks have been synced */
3242 atomic_sub(blocks, &mddev->recovery_active);
3243 wake_up(&mddev->recovery_wait);
3244 if (!ok) {
3245 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
3246 md_wakeup_thread(mddev->thread);
3247 // stop recovery, signal do_sync ....
3252 void md_write_start(mddev_t *mddev)
3254 if (!atomic_read(&mddev->writes_pending)) {
3255 mddev_lock_uninterruptible(mddev);
3256 if (mddev->in_sync) {
3257 mddev->in_sync = 0;
3258 del_timer(&mddev->safemode_timer);
3259 md_update_sb(mddev);
3261 atomic_inc(&mddev->writes_pending);
3262 mddev_unlock(mddev);
3263 } else
3264 atomic_inc(&mddev->writes_pending);
3267 void md_write_end(mddev_t *mddev)
3269 if (atomic_dec_and_test(&mddev->writes_pending)) {
3270 if (mddev->safemode == 2)
3271 md_wakeup_thread(mddev->thread);
3272 else
3273 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
3277 static inline void md_enter_safemode(mddev_t *mddev)
3279 if (!mddev->safemode) return;
3280 if (mddev->safemode == 2 &&
3281 (atomic_read(&mddev->writes_pending) || mddev->in_sync ||
3282 mddev->recovery_cp != MaxSector))
3283 return; /* avoid the lock */
3284 mddev_lock_uninterruptible(mddev);
3285 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
3286 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
3287 mddev->in_sync = 1;
3288 md_update_sb(mddev);
3290 mddev_unlock(mddev);
3292 if (mddev->safemode == 1)
3293 mddev->safemode = 0;
3296 void md_handle_safemode(mddev_t *mddev)
3298 if (signal_pending(current)) {
3299 printk(KERN_INFO "md: %s in immediate safe mode\n",
3300 mdname(mddev));
3301 mddev->safemode = 2;
3302 flush_signals(current);
3304 md_enter_safemode(mddev);
3308 DECLARE_WAIT_QUEUE_HEAD(resync_wait);
3310 #define SYNC_MARKS 10
3311 #define SYNC_MARK_STEP (3*HZ)
3312 static void md_do_sync(mddev_t *mddev)
3314 mddev_t *mddev2;
3315 unsigned int currspeed = 0,
3316 window;
3317 sector_t max_sectors,j;
3318 unsigned long mark[SYNC_MARKS];
3319 sector_t mark_cnt[SYNC_MARKS];
3320 int last_mark,m;
3321 struct list_head *tmp;
3322 sector_t last_check;
3324 /* just incase thread restarts... */
3325 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
3326 return;
3328 /* we overload curr_resync somewhat here.
3329 * 0 == not engaged in resync at all
3330 * 2 == checking that there is no conflict with another sync
3331 * 1 == like 2, but have yielded to allow conflicting resync to
3332 * commense
3333 * other == active in resync - this many blocks
3335 do {
3336 mddev->curr_resync = 2;
3338 ITERATE_MDDEV(mddev2,tmp) {
3339 if (mddev2 == mddev)
3340 continue;
3341 if (mddev2->curr_resync &&
3342 match_mddev_units(mddev,mddev2)) {
3343 printk(KERN_INFO "md: delaying resync of %s"
3344 " until %s has finished resync (they"
3345 " share one or more physical units)\n",
3346 mdname(mddev), mdname(mddev2));
3347 if (mddev < mddev2) {/* arbitrarily yield */
3348 mddev->curr_resync = 1;
3349 wake_up(&resync_wait);
3351 if (wait_event_interruptible(resync_wait,
3352 mddev2->curr_resync < mddev->curr_resync)) {
3353 flush_signals(current);
3354 mddev_put(mddev2);
3355 goto skip;
3358 if (mddev->curr_resync == 1) {
3359 mddev_put(mddev2);
3360 break;
3363 } while (mddev->curr_resync < 2);
3365 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3366 /* resync follows the size requested by the personality,
3367 * which default to physical size, but can be virtual size
3369 max_sectors = mddev->resync_max_sectors;
3370 else
3371 /* recovery follows the physical size of devices */
3372 max_sectors = mddev->size << 1;
3374 printk(KERN_INFO "md: syncing RAID array %s\n", mdname(mddev));
3375 printk(KERN_INFO "md: minimum _guaranteed_ reconstruction speed:"
3376 " %d KB/sec/disc.\n", sysctl_speed_limit_min);
3377 printk(KERN_INFO "md: using maximum available idle IO bandwith "
3378 "(but not more than %d KB/sec) for reconstruction.\n",
3379 sysctl_speed_limit_max);
3381 is_mddev_idle(mddev); /* this also initializes IO event counters */
3382 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3383 j = mddev->recovery_cp;
3384 else
3385 j = 0;
3386 for (m = 0; m < SYNC_MARKS; m++) {
3387 mark[m] = jiffies;
3388 mark_cnt[m] = j;
3390 last_mark = 0;
3391 mddev->resync_mark = mark[last_mark];
3392 mddev->resync_mark_cnt = mark_cnt[last_mark];
3395 * Tune reconstruction:
3397 window = 32*(PAGE_SIZE/512);
3398 printk(KERN_INFO "md: using %dk window, over a total of %Lu blocks.\n",
3399 window/2,(unsigned long long) max_sectors/2);
3401 atomic_set(&mddev->recovery_active, 0);
3402 init_waitqueue_head(&mddev->recovery_wait);
3403 last_check = 0;
3405 if (j)
3406 printk(KERN_INFO
3407 "md: resuming recovery of %s from checkpoint.\n",
3408 mdname(mddev));
3410 while (j < max_sectors) {
3411 int sectors;
3413 sectors = mddev->pers->sync_request(mddev, j, currspeed < sysctl_speed_limit_min);
3414 if (sectors < 0) {
3415 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
3416 goto out;
3418 atomic_add(sectors, &mddev->recovery_active);
3419 j += sectors;
3420 if (j>1) mddev->curr_resync = j;
3422 if (last_check + window > j || j == max_sectors)
3423 continue;
3425 last_check = j;
3427 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
3428 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
3429 break;
3431 repeat:
3432 if (jiffies >= mark[last_mark] + SYNC_MARK_STEP ) {
3433 /* step marks */
3434 int next = (last_mark+1) % SYNC_MARKS;
3436 mddev->resync_mark = mark[next];
3437 mddev->resync_mark_cnt = mark_cnt[next];
3438 mark[next] = jiffies;
3439 mark_cnt[next] = j - atomic_read(&mddev->recovery_active);
3440 last_mark = next;
3444 if (signal_pending(current)) {
3446 * got a signal, exit.
3448 printk(KERN_INFO
3449 "md: md_do_sync() got signal ... exiting\n");
3450 flush_signals(current);
3451 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3452 goto out;
3456 * this loop exits only if either when we are slower than
3457 * the 'hard' speed limit, or the system was IO-idle for
3458 * a jiffy.
3459 * the system might be non-idle CPU-wise, but we only care
3460 * about not overloading the IO subsystem. (things like an
3461 * e2fsck being done on the RAID array should execute fast)
3463 mddev->queue->unplug_fn(mddev->queue);
3464 cond_resched();
3466 currspeed = ((unsigned long)(j-mddev->resync_mark_cnt))/2/((jiffies-mddev->resync_mark)/HZ +1) +1;
3468 if (currspeed > sysctl_speed_limit_min) {
3469 if ((currspeed > sysctl_speed_limit_max) ||
3470 !is_mddev_idle(mddev)) {
3471 current->state = TASK_INTERRUPTIBLE;
3472 schedule_timeout(HZ/4);
3473 goto repeat;
3477 printk(KERN_INFO "md: %s: sync done.\n",mdname(mddev));
3479 * this also signals 'finished resyncing' to md_stop
3481 out:
3482 mddev->queue->unplug_fn(mddev->queue);
3484 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
3486 /* tell personality that we are finished */
3487 mddev->pers->sync_request(mddev, max_sectors, 1);
3489 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
3490 mddev->curr_resync > 2 &&
3491 mddev->curr_resync > mddev->recovery_cp) {
3492 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
3493 printk(KERN_INFO
3494 "md: checkpointing recovery of %s.\n",
3495 mdname(mddev));
3496 mddev->recovery_cp = mddev->curr_resync;
3497 } else
3498 mddev->recovery_cp = MaxSector;
3501 md_enter_safemode(mddev);
3502 skip:
3503 mddev->curr_resync = 0;
3504 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
3505 md_wakeup_thread(mddev->thread);
3510 * This routine is regularly called by all per-raid-array threads to
3511 * deal with generic issues like resync and super-block update.
3512 * Raid personalities that don't have a thread (linear/raid0) do not
3513 * need this as they never do any recovery or update the superblock.
3515 * It does not do any resync itself, but rather "forks" off other threads
3516 * to do that as needed.
3517 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
3518 * "->recovery" and create a thread at ->sync_thread.
3519 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
3520 * and wakeups up this thread which will reap the thread and finish up.
3521 * This thread also removes any faulty devices (with nr_pending == 0).
3523 * The overall approach is:
3524 * 1/ if the superblock needs updating, update it.
3525 * 2/ If a recovery thread is running, don't do anything else.
3526 * 3/ If recovery has finished, clean up, possibly marking spares active.
3527 * 4/ If there are any faulty devices, remove them.
3528 * 5/ If array is degraded, try to add spares devices
3529 * 6/ If array has spares or is not in-sync, start a resync thread.
3531 void md_check_recovery(mddev_t *mddev)
3533 mdk_rdev_t *rdev;
3534 struct list_head *rtmp;
3537 dprintk(KERN_INFO "md: recovery thread got woken up ...\n");
3539 if (mddev->ro)
3540 return;
3541 if ( ! (
3542 mddev->sb_dirty ||
3543 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
3544 test_bit(MD_RECOVERY_DONE, &mddev->recovery)
3546 return;
3547 if (mddev_trylock(mddev)==0) {
3548 int spares =0;
3549 if (mddev->sb_dirty)
3550 md_update_sb(mddev);
3551 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
3552 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
3553 /* resync/recovery still happening */
3554 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3555 goto unlock;
3557 if (mddev->sync_thread) {
3558 /* resync has finished, collect result */
3559 md_unregister_thread(mddev->sync_thread);
3560 mddev->sync_thread = NULL;
3561 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
3562 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
3563 /* success...*/
3564 /* activate any spares */
3565 mddev->pers->spare_active(mddev);
3567 md_update_sb(mddev);
3568 mddev->recovery = 0;
3569 /* flag recovery needed just to double check */
3570 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3571 wake_up(&resync_wait);
3572 goto unlock;
3574 if (mddev->recovery) {
3575 /* probably just the RECOVERY_NEEDED flag */
3576 mddev->recovery = 0;
3577 wake_up(&resync_wait);
3580 /* no recovery is running.
3581 * remove any failed drives, then
3582 * add spares if possible
3584 ITERATE_RDEV(mddev,rdev,rtmp) {
3585 if (rdev->raid_disk >= 0 &&
3586 rdev->faulty &&
3587 atomic_read(&rdev->nr_pending)==0) {
3588 if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0)
3589 rdev->raid_disk = -1;
3591 if (!rdev->faulty && rdev->raid_disk >= 0 && !rdev->in_sync)
3592 spares++;
3594 if (mddev->degraded) {
3595 ITERATE_RDEV(mddev,rdev,rtmp)
3596 if (rdev->raid_disk < 0
3597 && !rdev->faulty) {
3598 if (mddev->pers->hot_add_disk(mddev,rdev))
3599 spares++;
3600 else
3601 break;
3605 if (!spares && (mddev->recovery_cp == MaxSector )) {
3606 /* nothing we can do ... */
3607 goto unlock;
3609 if (mddev->pers->sync_request) {
3610 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3611 if (!spares)
3612 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3613 mddev->sync_thread = md_register_thread(md_do_sync,
3614 mddev,
3615 "%s_resync");
3616 if (!mddev->sync_thread) {
3617 printk(KERN_ERR "%s: could not start resync"
3618 " thread...\n",
3619 mdname(mddev));
3620 /* leave the spares where they are, it shouldn't hurt */
3621 mddev->recovery = 0;
3622 } else {
3623 md_wakeup_thread(mddev->sync_thread);
3626 unlock:
3627 mddev_unlock(mddev);
3631 int md_notify_reboot(struct notifier_block *this,
3632 unsigned long code, void *x)
3634 struct list_head *tmp;
3635 mddev_t *mddev;
3637 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
3639 printk(KERN_INFO "md: stopping all md devices.\n");
3641 ITERATE_MDDEV(mddev,tmp)
3642 if (mddev_trylock(mddev)==0)
3643 do_md_stop (mddev, 1);
3645 * certain more exotic SCSI devices are known to be
3646 * volatile wrt too early system reboots. While the
3647 * right place to handle this issue is the given
3648 * driver, we do want to have a safe RAID driver ...
3650 mdelay(1000*1);
3652 return NOTIFY_DONE;
3655 struct notifier_block md_notifier = {
3656 .notifier_call = md_notify_reboot,
3657 .next = NULL,
3658 .priority = INT_MAX, /* before any real devices */
3661 static void md_geninit(void)
3663 struct proc_dir_entry *p;
3665 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
3667 p = create_proc_entry("mdstat", S_IRUGO, NULL);
3668 if (p)
3669 p->proc_fops = &md_seq_fops;
3672 int __init md_init(void)
3674 int minor;
3676 printk(KERN_INFO "md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
3677 " MD_SB_DISKS=%d\n",
3678 MD_MAJOR_VERSION, MD_MINOR_VERSION,
3679 MD_PATCHLEVEL_VERSION, MAX_MD_DEVS, MD_SB_DISKS);
3681 if (register_blkdev(MAJOR_NR, "md"))
3682 return -1;
3683 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
3684 unregister_blkdev(MAJOR_NR, "md");
3685 return -1;
3687 devfs_mk_dir("md");
3688 blk_register_region(MKDEV(MAJOR_NR, 0), MAX_MD_DEVS, THIS_MODULE,
3689 md_probe, NULL, NULL);
3690 blk_register_region(MKDEV(mdp_major, 0), MAX_MD_DEVS<<MdpMinorShift, THIS_MODULE,
3691 md_probe, NULL, NULL);
3693 for (minor=0; minor < MAX_MD_DEVS; ++minor)
3694 devfs_mk_bdev(MKDEV(MAJOR_NR, minor),
3695 S_IFBLK|S_IRUSR|S_IWUSR,
3696 "md/%d", minor);
3698 for (minor=0; minor < MAX_MD_DEVS; ++minor)
3699 devfs_mk_bdev(MKDEV(mdp_major, minor<<MdpMinorShift),
3700 S_IFBLK|S_IRUSR|S_IWUSR,
3701 "md/d%d", minor);
3704 register_reboot_notifier(&md_notifier);
3705 raid_table_header = register_sysctl_table(raid_root_table, 1);
3707 md_geninit();
3708 return (0);
3712 #ifndef MODULE
3715 * Searches all registered partitions for autorun RAID arrays
3716 * at boot time.
3718 static dev_t detected_devices[128];
3719 static int dev_cnt;
3721 void md_autodetect_dev(dev_t dev)
3723 if (dev_cnt >= 0 && dev_cnt < 127)
3724 detected_devices[dev_cnt++] = dev;
3728 static void autostart_arrays(int part)
3730 mdk_rdev_t *rdev;
3731 int i;
3733 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
3735 for (i = 0; i < dev_cnt; i++) {
3736 dev_t dev = detected_devices[i];
3738 rdev = md_import_device(dev,0, 0);
3739 if (IS_ERR(rdev))
3740 continue;
3742 if (rdev->faulty) {
3743 MD_BUG();
3744 continue;
3746 list_add(&rdev->same_set, &pending_raid_disks);
3748 dev_cnt = 0;
3750 autorun_devices(part);
3753 #endif
3755 static __exit void md_exit(void)
3757 mddev_t *mddev;
3758 struct list_head *tmp;
3759 int i;
3760 blk_unregister_region(MKDEV(MAJOR_NR,0), MAX_MD_DEVS);
3761 blk_unregister_region(MKDEV(mdp_major,0), MAX_MD_DEVS << MdpMinorShift);
3762 for (i=0; i < MAX_MD_DEVS; i++)
3763 devfs_remove("md/%d", i);
3764 for (i=0; i < MAX_MD_DEVS; i++)
3765 devfs_remove("md/d%d", i);
3767 devfs_remove("md");
3769 unregister_blkdev(MAJOR_NR,"md");
3770 unregister_blkdev(mdp_major, "mdp");
3771 unregister_reboot_notifier(&md_notifier);
3772 unregister_sysctl_table(raid_table_header);
3773 remove_proc_entry("mdstat", NULL);
3774 ITERATE_MDDEV(mddev,tmp) {
3775 struct gendisk *disk = mddev->gendisk;
3776 if (!disk)
3777 continue;
3778 export_array(mddev);
3779 del_gendisk(disk);
3780 put_disk(disk);
3781 mddev->gendisk = NULL;
3782 mddev_put(mddev);
3786 module_init(md_init)
3787 module_exit(md_exit)
3789 EXPORT_SYMBOL(register_md_personality);
3790 EXPORT_SYMBOL(unregister_md_personality);
3791 EXPORT_SYMBOL(md_error);
3792 EXPORT_SYMBOL(md_done_sync);
3793 EXPORT_SYMBOL(md_write_start);
3794 EXPORT_SYMBOL(md_write_end);
3795 EXPORT_SYMBOL(md_handle_safemode);
3796 EXPORT_SYMBOL(md_register_thread);
3797 EXPORT_SYMBOL(md_unregister_thread);
3798 EXPORT_SYMBOL(md_wakeup_thread);
3799 EXPORT_SYMBOL(md_print_devices);
3800 EXPORT_SYMBOL(md_check_recovery);
3801 MODULE_LICENSE("GPL");