Fix ACPI processor power block initialization
[linux-2.6.22.y-op.git] / drivers / md / md.c
blobf3fed662f32ea603dbe76625369ea8ffaf24672f
1 /*
2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
5 completely rewritten, based on the MD driver code from Marc Zyngier
7 Changes:
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
20 Neil Brown <neilb@cse.unsw.edu.au>.
22 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
25 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include <linux/module.h>
36 #include <linux/config.h>
37 #include <linux/kthread.h>
38 #include <linux/linkage.h>
39 #include <linux/raid/md.h>
40 #include <linux/raid/bitmap.h>
41 #include <linux/sysctl.h>
42 #include <linux/devfs_fs_kernel.h>
43 #include <linux/buffer_head.h> /* for invalidate_bdev */
44 #include <linux/suspend.h>
46 #include <linux/init.h>
48 #include <linux/file.h>
50 #ifdef CONFIG_KMOD
51 #include <linux/kmod.h>
52 #endif
54 #include <asm/unaligned.h>
56 #define MAJOR_NR MD_MAJOR
57 #define MD_DRIVER
59 /* 63 partitions with the alternate major number (mdp) */
60 #define MdpMinorShift 6
62 #define DEBUG 0
63 #define dprintk(x...) ((void)(DEBUG && printk(x)))
66 #ifndef MODULE
67 static void autostart_arrays (int part);
68 #endif
70 static mdk_personality_t *pers[MAX_PERSONALITY];
71 static DEFINE_SPINLOCK(pers_lock);
74 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
75 * is 1000 KB/sec, so the extra system load does not show up that much.
76 * Increase it if you want to have more _guaranteed_ speed. Note that
77 * the RAID driver will use the maximum available bandwidth if the IO
78 * subsystem is idle. There is also an 'absolute maximum' reconstruction
79 * speed limit - in case reconstruction slows down your system despite
80 * idle IO detection.
82 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
85 static int sysctl_speed_limit_min = 1000;
86 static int sysctl_speed_limit_max = 200000;
88 static struct ctl_table_header *raid_table_header;
90 static ctl_table raid_table[] = {
92 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
93 .procname = "speed_limit_min",
94 .data = &sysctl_speed_limit_min,
95 .maxlen = sizeof(int),
96 .mode = 0644,
97 .proc_handler = &proc_dointvec,
100 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
101 .procname = "speed_limit_max",
102 .data = &sysctl_speed_limit_max,
103 .maxlen = sizeof(int),
104 .mode = 0644,
105 .proc_handler = &proc_dointvec,
107 { .ctl_name = 0 }
110 static ctl_table raid_dir_table[] = {
112 .ctl_name = DEV_RAID,
113 .procname = "raid",
114 .maxlen = 0,
115 .mode = 0555,
116 .child = raid_table,
118 { .ctl_name = 0 }
121 static ctl_table raid_root_table[] = {
123 .ctl_name = CTL_DEV,
124 .procname = "dev",
125 .maxlen = 0,
126 .mode = 0555,
127 .child = raid_dir_table,
129 { .ctl_name = 0 }
132 static struct block_device_operations md_fops;
134 static int start_readonly;
137 * Enables to iterate over all existing md arrays
138 * all_mddevs_lock protects this list.
140 static LIST_HEAD(all_mddevs);
141 static DEFINE_SPINLOCK(all_mddevs_lock);
145 * iterates through all used mddevs in the system.
146 * We take care to grab the all_mddevs_lock whenever navigating
147 * the list, and to always hold a refcount when unlocked.
148 * Any code which breaks out of this loop while own
149 * a reference to the current mddev and must mddev_put it.
151 #define ITERATE_MDDEV(mddev,tmp) \
153 for (({ spin_lock(&all_mddevs_lock); \
154 tmp = all_mddevs.next; \
155 mddev = NULL;}); \
156 ({ if (tmp != &all_mddevs) \
157 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
158 spin_unlock(&all_mddevs_lock); \
159 if (mddev) mddev_put(mddev); \
160 mddev = list_entry(tmp, mddev_t, all_mddevs); \
161 tmp != &all_mddevs;}); \
162 ({ spin_lock(&all_mddevs_lock); \
163 tmp = tmp->next;}) \
167 static int md_fail_request (request_queue_t *q, struct bio *bio)
169 bio_io_error(bio, bio->bi_size);
170 return 0;
173 static inline mddev_t *mddev_get(mddev_t *mddev)
175 atomic_inc(&mddev->active);
176 return mddev;
179 static void mddev_put(mddev_t *mddev)
181 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
182 return;
183 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
184 list_del(&mddev->all_mddevs);
185 blk_put_queue(mddev->queue);
186 kobject_unregister(&mddev->kobj);
188 spin_unlock(&all_mddevs_lock);
191 static mddev_t * mddev_find(dev_t unit)
193 mddev_t *mddev, *new = NULL;
195 retry:
196 spin_lock(&all_mddevs_lock);
197 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
198 if (mddev->unit == unit) {
199 mddev_get(mddev);
200 spin_unlock(&all_mddevs_lock);
201 kfree(new);
202 return mddev;
205 if (new) {
206 list_add(&new->all_mddevs, &all_mddevs);
207 spin_unlock(&all_mddevs_lock);
208 return new;
210 spin_unlock(&all_mddevs_lock);
212 new = (mddev_t *) kmalloc(sizeof(*new), GFP_KERNEL);
213 if (!new)
214 return NULL;
216 memset(new, 0, sizeof(*new));
218 new->unit = unit;
219 if (MAJOR(unit) == MD_MAJOR)
220 new->md_minor = MINOR(unit);
221 else
222 new->md_minor = MINOR(unit) >> MdpMinorShift;
224 init_MUTEX(&new->reconfig_sem);
225 INIT_LIST_HEAD(&new->disks);
226 INIT_LIST_HEAD(&new->all_mddevs);
227 init_timer(&new->safemode_timer);
228 atomic_set(&new->active, 1);
229 spin_lock_init(&new->write_lock);
230 init_waitqueue_head(&new->sb_wait);
232 new->queue = blk_alloc_queue(GFP_KERNEL);
233 if (!new->queue) {
234 kfree(new);
235 return NULL;
238 blk_queue_make_request(new->queue, md_fail_request);
240 goto retry;
243 static inline int mddev_lock(mddev_t * mddev)
245 return down_interruptible(&mddev->reconfig_sem);
248 static inline void mddev_lock_uninterruptible(mddev_t * mddev)
250 down(&mddev->reconfig_sem);
253 static inline int mddev_trylock(mddev_t * mddev)
255 return down_trylock(&mddev->reconfig_sem);
258 static inline void mddev_unlock(mddev_t * mddev)
260 up(&mddev->reconfig_sem);
262 md_wakeup_thread(mddev->thread);
265 mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
267 mdk_rdev_t * rdev;
268 struct list_head *tmp;
270 ITERATE_RDEV(mddev,rdev,tmp) {
271 if (rdev->desc_nr == nr)
272 return rdev;
274 return NULL;
277 static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
279 struct list_head *tmp;
280 mdk_rdev_t *rdev;
282 ITERATE_RDEV(mddev,rdev,tmp) {
283 if (rdev->bdev->bd_dev == dev)
284 return rdev;
286 return NULL;
289 static inline sector_t calc_dev_sboffset(struct block_device *bdev)
291 sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
292 return MD_NEW_SIZE_BLOCKS(size);
295 static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
297 sector_t size;
299 size = rdev->sb_offset;
301 if (chunk_size)
302 size &= ~((sector_t)chunk_size/1024 - 1);
303 return size;
306 static int alloc_disk_sb(mdk_rdev_t * rdev)
308 if (rdev->sb_page)
309 MD_BUG();
311 rdev->sb_page = alloc_page(GFP_KERNEL);
312 if (!rdev->sb_page) {
313 printk(KERN_ALERT "md: out of memory.\n");
314 return -EINVAL;
317 return 0;
320 static void free_disk_sb(mdk_rdev_t * rdev)
322 if (rdev->sb_page) {
323 page_cache_release(rdev->sb_page);
324 rdev->sb_loaded = 0;
325 rdev->sb_page = NULL;
326 rdev->sb_offset = 0;
327 rdev->size = 0;
332 static int super_written(struct bio *bio, unsigned int bytes_done, int error)
334 mdk_rdev_t *rdev = bio->bi_private;
335 mddev_t *mddev = rdev->mddev;
336 if (bio->bi_size)
337 return 1;
339 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags))
340 md_error(mddev, rdev);
342 if (atomic_dec_and_test(&mddev->pending_writes))
343 wake_up(&mddev->sb_wait);
344 bio_put(bio);
345 return 0;
348 static int super_written_barrier(struct bio *bio, unsigned int bytes_done, int error)
350 struct bio *bio2 = bio->bi_private;
351 mdk_rdev_t *rdev = bio2->bi_private;
352 mddev_t *mddev = rdev->mddev;
353 if (bio->bi_size)
354 return 1;
356 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
357 error == -EOPNOTSUPP) {
358 unsigned long flags;
359 /* barriers don't appear to be supported :-( */
360 set_bit(BarriersNotsupp, &rdev->flags);
361 mddev->barriers_work = 0;
362 spin_lock_irqsave(&mddev->write_lock, flags);
363 bio2->bi_next = mddev->biolist;
364 mddev->biolist = bio2;
365 spin_unlock_irqrestore(&mddev->write_lock, flags);
366 wake_up(&mddev->sb_wait);
367 bio_put(bio);
368 return 0;
370 bio_put(bio2);
371 bio->bi_private = rdev;
372 return super_written(bio, bytes_done, error);
375 void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
376 sector_t sector, int size, struct page *page)
378 /* write first size bytes of page to sector of rdev
379 * Increment mddev->pending_writes before returning
380 * and decrement it on completion, waking up sb_wait
381 * if zero is reached.
382 * If an error occurred, call md_error
384 * As we might need to resubmit the request if BIO_RW_BARRIER
385 * causes ENOTSUPP, we allocate a spare bio...
387 struct bio *bio = bio_alloc(GFP_NOIO, 1);
388 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
390 bio->bi_bdev = rdev->bdev;
391 bio->bi_sector = sector;
392 bio_add_page(bio, page, size, 0);
393 bio->bi_private = rdev;
394 bio->bi_end_io = super_written;
395 bio->bi_rw = rw;
397 atomic_inc(&mddev->pending_writes);
398 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
399 struct bio *rbio;
400 rw |= (1<<BIO_RW_BARRIER);
401 rbio = bio_clone(bio, GFP_NOIO);
402 rbio->bi_private = bio;
403 rbio->bi_end_io = super_written_barrier;
404 submit_bio(rw, rbio);
405 } else
406 submit_bio(rw, bio);
409 void md_super_wait(mddev_t *mddev)
411 /* wait for all superblock writes that were scheduled to complete.
412 * if any had to be retried (due to BARRIER problems), retry them
414 DEFINE_WAIT(wq);
415 for(;;) {
416 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
417 if (atomic_read(&mddev->pending_writes)==0)
418 break;
419 while (mddev->biolist) {
420 struct bio *bio;
421 spin_lock_irq(&mddev->write_lock);
422 bio = mddev->biolist;
423 mddev->biolist = bio->bi_next ;
424 bio->bi_next = NULL;
425 spin_unlock_irq(&mddev->write_lock);
426 submit_bio(bio->bi_rw, bio);
428 schedule();
430 finish_wait(&mddev->sb_wait, &wq);
433 static int bi_complete(struct bio *bio, unsigned int bytes_done, int error)
435 if (bio->bi_size)
436 return 1;
438 complete((struct completion*)bio->bi_private);
439 return 0;
442 int sync_page_io(struct block_device *bdev, sector_t sector, int size,
443 struct page *page, int rw)
445 struct bio *bio = bio_alloc(GFP_NOIO, 1);
446 struct completion event;
447 int ret;
449 rw |= (1 << BIO_RW_SYNC);
451 bio->bi_bdev = bdev;
452 bio->bi_sector = sector;
453 bio_add_page(bio, page, size, 0);
454 init_completion(&event);
455 bio->bi_private = &event;
456 bio->bi_end_io = bi_complete;
457 submit_bio(rw, bio);
458 wait_for_completion(&event);
460 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
461 bio_put(bio);
462 return ret;
465 static int read_disk_sb(mdk_rdev_t * rdev, int size)
467 char b[BDEVNAME_SIZE];
468 if (!rdev->sb_page) {
469 MD_BUG();
470 return -EINVAL;
472 if (rdev->sb_loaded)
473 return 0;
476 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
477 goto fail;
478 rdev->sb_loaded = 1;
479 return 0;
481 fail:
482 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
483 bdevname(rdev->bdev,b));
484 return -EINVAL;
487 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
489 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
490 (sb1->set_uuid1 == sb2->set_uuid1) &&
491 (sb1->set_uuid2 == sb2->set_uuid2) &&
492 (sb1->set_uuid3 == sb2->set_uuid3))
494 return 1;
496 return 0;
500 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
502 int ret;
503 mdp_super_t *tmp1, *tmp2;
505 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
506 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
508 if (!tmp1 || !tmp2) {
509 ret = 0;
510 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
511 goto abort;
514 *tmp1 = *sb1;
515 *tmp2 = *sb2;
518 * nr_disks is not constant
520 tmp1->nr_disks = 0;
521 tmp2->nr_disks = 0;
523 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
524 ret = 0;
525 else
526 ret = 1;
528 abort:
529 kfree(tmp1);
530 kfree(tmp2);
531 return ret;
534 static unsigned int calc_sb_csum(mdp_super_t * sb)
536 unsigned int disk_csum, csum;
538 disk_csum = sb->sb_csum;
539 sb->sb_csum = 0;
540 csum = csum_partial((void *)sb, MD_SB_BYTES, 0);
541 sb->sb_csum = disk_csum;
542 return csum;
547 * Handle superblock details.
548 * We want to be able to handle multiple superblock formats
549 * so we have a common interface to them all, and an array of
550 * different handlers.
551 * We rely on user-space to write the initial superblock, and support
552 * reading and updating of superblocks.
553 * Interface methods are:
554 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
555 * loads and validates a superblock on dev.
556 * if refdev != NULL, compare superblocks on both devices
557 * Return:
558 * 0 - dev has a superblock that is compatible with refdev
559 * 1 - dev has a superblock that is compatible and newer than refdev
560 * so dev should be used as the refdev in future
561 * -EINVAL superblock incompatible or invalid
562 * -othererror e.g. -EIO
564 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
565 * Verify that dev is acceptable into mddev.
566 * The first time, mddev->raid_disks will be 0, and data from
567 * dev should be merged in. Subsequent calls check that dev
568 * is new enough. Return 0 or -EINVAL
570 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
571 * Update the superblock for rdev with data in mddev
572 * This does not write to disc.
576 struct super_type {
577 char *name;
578 struct module *owner;
579 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
580 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
581 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
585 * load_super for 0.90.0
587 static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
589 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
590 mdp_super_t *sb;
591 int ret;
592 sector_t sb_offset;
595 * Calculate the position of the superblock,
596 * it's at the end of the disk.
598 * It also happens to be a multiple of 4Kb.
600 sb_offset = calc_dev_sboffset(rdev->bdev);
601 rdev->sb_offset = sb_offset;
603 ret = read_disk_sb(rdev, MD_SB_BYTES);
604 if (ret) return ret;
606 ret = -EINVAL;
608 bdevname(rdev->bdev, b);
609 sb = (mdp_super_t*)page_address(rdev->sb_page);
611 if (sb->md_magic != MD_SB_MAGIC) {
612 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
614 goto abort;
617 if (sb->major_version != 0 ||
618 sb->minor_version != 90) {
619 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
620 sb->major_version, sb->minor_version,
622 goto abort;
625 if (sb->raid_disks <= 0)
626 goto abort;
628 if (csum_fold(calc_sb_csum(sb)) != csum_fold(sb->sb_csum)) {
629 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
631 goto abort;
634 rdev->preferred_minor = sb->md_minor;
635 rdev->data_offset = 0;
636 rdev->sb_size = MD_SB_BYTES;
638 if (sb->level == LEVEL_MULTIPATH)
639 rdev->desc_nr = -1;
640 else
641 rdev->desc_nr = sb->this_disk.number;
643 if (refdev == 0)
644 ret = 1;
645 else {
646 __u64 ev1, ev2;
647 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
648 if (!uuid_equal(refsb, sb)) {
649 printk(KERN_WARNING "md: %s has different UUID to %s\n",
650 b, bdevname(refdev->bdev,b2));
651 goto abort;
653 if (!sb_equal(refsb, sb)) {
654 printk(KERN_WARNING "md: %s has same UUID"
655 " but different superblock to %s\n",
656 b, bdevname(refdev->bdev, b2));
657 goto abort;
659 ev1 = md_event(sb);
660 ev2 = md_event(refsb);
661 if (ev1 > ev2)
662 ret = 1;
663 else
664 ret = 0;
666 rdev->size = calc_dev_size(rdev, sb->chunk_size);
668 abort:
669 return ret;
673 * validate_super for 0.90.0
675 static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
677 mdp_disk_t *desc;
678 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
680 rdev->raid_disk = -1;
681 rdev->flags = 0;
682 if (mddev->raid_disks == 0) {
683 mddev->major_version = 0;
684 mddev->minor_version = sb->minor_version;
685 mddev->patch_version = sb->patch_version;
686 mddev->persistent = ! sb->not_persistent;
687 mddev->chunk_size = sb->chunk_size;
688 mddev->ctime = sb->ctime;
689 mddev->utime = sb->utime;
690 mddev->level = sb->level;
691 mddev->layout = sb->layout;
692 mddev->raid_disks = sb->raid_disks;
693 mddev->size = sb->size;
694 mddev->events = md_event(sb);
695 mddev->bitmap_offset = 0;
696 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
698 if (sb->state & (1<<MD_SB_CLEAN))
699 mddev->recovery_cp = MaxSector;
700 else {
701 if (sb->events_hi == sb->cp_events_hi &&
702 sb->events_lo == sb->cp_events_lo) {
703 mddev->recovery_cp = sb->recovery_cp;
704 } else
705 mddev->recovery_cp = 0;
708 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
709 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
710 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
711 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
713 mddev->max_disks = MD_SB_DISKS;
715 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
716 mddev->bitmap_file == NULL) {
717 if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6) {
718 /* FIXME use a better test */
719 printk(KERN_WARNING "md: bitmaps only support for raid1\n");
720 return -EINVAL;
722 mddev->bitmap_offset = mddev->default_bitmap_offset;
725 } else if (mddev->pers == NULL) {
726 /* Insist on good event counter while assembling */
727 __u64 ev1 = md_event(sb);
728 ++ev1;
729 if (ev1 < mddev->events)
730 return -EINVAL;
731 } else if (mddev->bitmap) {
732 /* if adding to array with a bitmap, then we can accept an
733 * older device ... but not too old.
735 __u64 ev1 = md_event(sb);
736 if (ev1 < mddev->bitmap->events_cleared)
737 return 0;
738 } else /* just a hot-add of a new device, leave raid_disk at -1 */
739 return 0;
741 if (mddev->level != LEVEL_MULTIPATH) {
742 desc = sb->disks + rdev->desc_nr;
744 if (desc->state & (1<<MD_DISK_FAULTY))
745 set_bit(Faulty, &rdev->flags);
746 else if (desc->state & (1<<MD_DISK_SYNC) &&
747 desc->raid_disk < mddev->raid_disks) {
748 set_bit(In_sync, &rdev->flags);
749 rdev->raid_disk = desc->raid_disk;
751 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
752 set_bit(WriteMostly, &rdev->flags);
753 } else /* MULTIPATH are always insync */
754 set_bit(In_sync, &rdev->flags);
755 return 0;
759 * sync_super for 0.90.0
761 static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
763 mdp_super_t *sb;
764 struct list_head *tmp;
765 mdk_rdev_t *rdev2;
766 int next_spare = mddev->raid_disks;
769 /* make rdev->sb match mddev data..
771 * 1/ zero out disks
772 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
773 * 3/ any empty disks < next_spare become removed
775 * disks[0] gets initialised to REMOVED because
776 * we cannot be sure from other fields if it has
777 * been initialised or not.
779 int i;
780 int active=0, working=0,failed=0,spare=0,nr_disks=0;
782 rdev->sb_size = MD_SB_BYTES;
784 sb = (mdp_super_t*)page_address(rdev->sb_page);
786 memset(sb, 0, sizeof(*sb));
788 sb->md_magic = MD_SB_MAGIC;
789 sb->major_version = mddev->major_version;
790 sb->minor_version = mddev->minor_version;
791 sb->patch_version = mddev->patch_version;
792 sb->gvalid_words = 0; /* ignored */
793 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
794 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
795 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
796 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
798 sb->ctime = mddev->ctime;
799 sb->level = mddev->level;
800 sb->size = mddev->size;
801 sb->raid_disks = mddev->raid_disks;
802 sb->md_minor = mddev->md_minor;
803 sb->not_persistent = !mddev->persistent;
804 sb->utime = mddev->utime;
805 sb->state = 0;
806 sb->events_hi = (mddev->events>>32);
807 sb->events_lo = (u32)mddev->events;
809 if (mddev->in_sync)
811 sb->recovery_cp = mddev->recovery_cp;
812 sb->cp_events_hi = (mddev->events>>32);
813 sb->cp_events_lo = (u32)mddev->events;
814 if (mddev->recovery_cp == MaxSector)
815 sb->state = (1<< MD_SB_CLEAN);
816 } else
817 sb->recovery_cp = 0;
819 sb->layout = mddev->layout;
820 sb->chunk_size = mddev->chunk_size;
822 if (mddev->bitmap && mddev->bitmap_file == NULL)
823 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
825 sb->disks[0].state = (1<<MD_DISK_REMOVED);
826 ITERATE_RDEV(mddev,rdev2,tmp) {
827 mdp_disk_t *d;
828 int desc_nr;
829 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
830 && !test_bit(Faulty, &rdev2->flags))
831 desc_nr = rdev2->raid_disk;
832 else
833 desc_nr = next_spare++;
834 rdev2->desc_nr = desc_nr;
835 d = &sb->disks[rdev2->desc_nr];
836 nr_disks++;
837 d->number = rdev2->desc_nr;
838 d->major = MAJOR(rdev2->bdev->bd_dev);
839 d->minor = MINOR(rdev2->bdev->bd_dev);
840 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
841 && !test_bit(Faulty, &rdev2->flags))
842 d->raid_disk = rdev2->raid_disk;
843 else
844 d->raid_disk = rdev2->desc_nr; /* compatibility */
845 if (test_bit(Faulty, &rdev2->flags)) {
846 d->state = (1<<MD_DISK_FAULTY);
847 failed++;
848 } else if (test_bit(In_sync, &rdev2->flags)) {
849 d->state = (1<<MD_DISK_ACTIVE);
850 d->state |= (1<<MD_DISK_SYNC);
851 active++;
852 working++;
853 } else {
854 d->state = 0;
855 spare++;
856 working++;
858 if (test_bit(WriteMostly, &rdev2->flags))
859 d->state |= (1<<MD_DISK_WRITEMOSTLY);
861 /* now set the "removed" and "faulty" bits on any missing devices */
862 for (i=0 ; i < mddev->raid_disks ; i++) {
863 mdp_disk_t *d = &sb->disks[i];
864 if (d->state == 0 && d->number == 0) {
865 d->number = i;
866 d->raid_disk = i;
867 d->state = (1<<MD_DISK_REMOVED);
868 d->state |= (1<<MD_DISK_FAULTY);
869 failed++;
872 sb->nr_disks = nr_disks;
873 sb->active_disks = active;
874 sb->working_disks = working;
875 sb->failed_disks = failed;
876 sb->spare_disks = spare;
878 sb->this_disk = sb->disks[rdev->desc_nr];
879 sb->sb_csum = calc_sb_csum(sb);
883 * version 1 superblock
886 static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
888 unsigned int disk_csum, csum;
889 unsigned long long newcsum;
890 int size = 256 + le32_to_cpu(sb->max_dev)*2;
891 unsigned int *isuper = (unsigned int*)sb;
892 int i;
894 disk_csum = sb->sb_csum;
895 sb->sb_csum = 0;
896 newcsum = 0;
897 for (i=0; size>=4; size -= 4 )
898 newcsum += le32_to_cpu(*isuper++);
900 if (size == 2)
901 newcsum += le16_to_cpu(*(unsigned short*) isuper);
903 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
904 sb->sb_csum = disk_csum;
905 return cpu_to_le32(csum);
908 static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
910 struct mdp_superblock_1 *sb;
911 int ret;
912 sector_t sb_offset;
913 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
914 int bmask;
917 * Calculate the position of the superblock.
918 * It is always aligned to a 4K boundary and
919 * depeding on minor_version, it can be:
920 * 0: At least 8K, but less than 12K, from end of device
921 * 1: At start of device
922 * 2: 4K from start of device.
924 switch(minor_version) {
925 case 0:
926 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
927 sb_offset -= 8*2;
928 sb_offset &= ~(sector_t)(4*2-1);
929 /* convert from sectors to K */
930 sb_offset /= 2;
931 break;
932 case 1:
933 sb_offset = 0;
934 break;
935 case 2:
936 sb_offset = 4;
937 break;
938 default:
939 return -EINVAL;
941 rdev->sb_offset = sb_offset;
943 /* superblock is rarely larger than 1K, but it can be larger,
944 * and it is safe to read 4k, so we do that
946 ret = read_disk_sb(rdev, 4096);
947 if (ret) return ret;
950 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
952 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
953 sb->major_version != cpu_to_le32(1) ||
954 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
955 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
956 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
957 return -EINVAL;
959 if (calc_sb_1_csum(sb) != sb->sb_csum) {
960 printk("md: invalid superblock checksum on %s\n",
961 bdevname(rdev->bdev,b));
962 return -EINVAL;
964 if (le64_to_cpu(sb->data_size) < 10) {
965 printk("md: data_size too small on %s\n",
966 bdevname(rdev->bdev,b));
967 return -EINVAL;
969 rdev->preferred_minor = 0xffff;
970 rdev->data_offset = le64_to_cpu(sb->data_offset);
972 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
973 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
974 if (rdev->sb_size & bmask)
975 rdev-> sb_size = (rdev->sb_size | bmask)+1;
977 if (refdev == 0)
978 return 1;
979 else {
980 __u64 ev1, ev2;
981 struct mdp_superblock_1 *refsb =
982 (struct mdp_superblock_1*)page_address(refdev->sb_page);
984 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
985 sb->level != refsb->level ||
986 sb->layout != refsb->layout ||
987 sb->chunksize != refsb->chunksize) {
988 printk(KERN_WARNING "md: %s has strangely different"
989 " superblock to %s\n",
990 bdevname(rdev->bdev,b),
991 bdevname(refdev->bdev,b2));
992 return -EINVAL;
994 ev1 = le64_to_cpu(sb->events);
995 ev2 = le64_to_cpu(refsb->events);
997 if (ev1 > ev2)
998 return 1;
1000 if (minor_version)
1001 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1002 else
1003 rdev->size = rdev->sb_offset;
1004 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1005 return -EINVAL;
1006 rdev->size = le64_to_cpu(sb->data_size)/2;
1007 if (le32_to_cpu(sb->chunksize))
1008 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
1009 return 0;
1012 static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1014 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1016 rdev->raid_disk = -1;
1017 rdev->flags = 0;
1018 if (mddev->raid_disks == 0) {
1019 mddev->major_version = 1;
1020 mddev->patch_version = 0;
1021 mddev->persistent = 1;
1022 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1023 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1024 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1025 mddev->level = le32_to_cpu(sb->level);
1026 mddev->layout = le32_to_cpu(sb->layout);
1027 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1028 mddev->size = le64_to_cpu(sb->size)/2;
1029 mddev->events = le64_to_cpu(sb->events);
1030 mddev->bitmap_offset = 0;
1031 mddev->default_bitmap_offset = 0;
1032 mddev->default_bitmap_offset = 1024;
1034 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1035 memcpy(mddev->uuid, sb->set_uuid, 16);
1037 mddev->max_disks = (4096-256)/2;
1039 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1040 mddev->bitmap_file == NULL ) {
1041 if (mddev->level != 1) {
1042 printk(KERN_WARNING "md: bitmaps only supported for raid1\n");
1043 return -EINVAL;
1045 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1047 } else if (mddev->pers == NULL) {
1048 /* Insist of good event counter while assembling */
1049 __u64 ev1 = le64_to_cpu(sb->events);
1050 ++ev1;
1051 if (ev1 < mddev->events)
1052 return -EINVAL;
1053 } else if (mddev->bitmap) {
1054 /* If adding to array with a bitmap, then we can accept an
1055 * older device, but not too old.
1057 __u64 ev1 = le64_to_cpu(sb->events);
1058 if (ev1 < mddev->bitmap->events_cleared)
1059 return 0;
1060 } else /* just a hot-add of a new device, leave raid_disk at -1 */
1061 return 0;
1063 if (mddev->level != LEVEL_MULTIPATH) {
1064 int role;
1065 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1066 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1067 switch(role) {
1068 case 0xffff: /* spare */
1069 break;
1070 case 0xfffe: /* faulty */
1071 set_bit(Faulty, &rdev->flags);
1072 break;
1073 default:
1074 set_bit(In_sync, &rdev->flags);
1075 rdev->raid_disk = role;
1076 break;
1078 if (sb->devflags & WriteMostly1)
1079 set_bit(WriteMostly, &rdev->flags);
1080 } else /* MULTIPATH are always insync */
1081 set_bit(In_sync, &rdev->flags);
1083 return 0;
1086 static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1088 struct mdp_superblock_1 *sb;
1089 struct list_head *tmp;
1090 mdk_rdev_t *rdev2;
1091 int max_dev, i;
1092 /* make rdev->sb match mddev and rdev data. */
1094 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1096 sb->feature_map = 0;
1097 sb->pad0 = 0;
1098 memset(sb->pad1, 0, sizeof(sb->pad1));
1099 memset(sb->pad2, 0, sizeof(sb->pad2));
1100 memset(sb->pad3, 0, sizeof(sb->pad3));
1102 sb->utime = cpu_to_le64((__u64)mddev->utime);
1103 sb->events = cpu_to_le64(mddev->events);
1104 if (mddev->in_sync)
1105 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1106 else
1107 sb->resync_offset = cpu_to_le64(0);
1109 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1110 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
1111 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1114 max_dev = 0;
1115 ITERATE_RDEV(mddev,rdev2,tmp)
1116 if (rdev2->desc_nr+1 > max_dev)
1117 max_dev = rdev2->desc_nr+1;
1119 sb->max_dev = cpu_to_le32(max_dev);
1120 for (i=0; i<max_dev;i++)
1121 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1123 ITERATE_RDEV(mddev,rdev2,tmp) {
1124 i = rdev2->desc_nr;
1125 if (test_bit(Faulty, &rdev2->flags))
1126 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1127 else if (test_bit(In_sync, &rdev2->flags))
1128 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1129 else
1130 sb->dev_roles[i] = cpu_to_le16(0xffff);
1133 sb->recovery_offset = cpu_to_le64(0); /* not supported yet */
1134 sb->sb_csum = calc_sb_1_csum(sb);
1138 static struct super_type super_types[] = {
1139 [0] = {
1140 .name = "0.90.0",
1141 .owner = THIS_MODULE,
1142 .load_super = super_90_load,
1143 .validate_super = super_90_validate,
1144 .sync_super = super_90_sync,
1146 [1] = {
1147 .name = "md-1",
1148 .owner = THIS_MODULE,
1149 .load_super = super_1_load,
1150 .validate_super = super_1_validate,
1151 .sync_super = super_1_sync,
1155 static mdk_rdev_t * match_dev_unit(mddev_t *mddev, mdk_rdev_t *dev)
1157 struct list_head *tmp;
1158 mdk_rdev_t *rdev;
1160 ITERATE_RDEV(mddev,rdev,tmp)
1161 if (rdev->bdev->bd_contains == dev->bdev->bd_contains)
1162 return rdev;
1164 return NULL;
1167 static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1169 struct list_head *tmp;
1170 mdk_rdev_t *rdev;
1172 ITERATE_RDEV(mddev1,rdev,tmp)
1173 if (match_dev_unit(mddev2, rdev))
1174 return 1;
1176 return 0;
1179 static LIST_HEAD(pending_raid_disks);
1181 static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1183 mdk_rdev_t *same_pdev;
1184 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1185 struct kobject *ko;
1187 if (rdev->mddev) {
1188 MD_BUG();
1189 return -EINVAL;
1191 same_pdev = match_dev_unit(mddev, rdev);
1192 if (same_pdev)
1193 printk(KERN_WARNING
1194 "%s: WARNING: %s appears to be on the same physical"
1195 " disk as %s. True\n protection against single-disk"
1196 " failure might be compromised.\n",
1197 mdname(mddev), bdevname(rdev->bdev,b),
1198 bdevname(same_pdev->bdev,b2));
1200 /* Verify rdev->desc_nr is unique.
1201 * If it is -1, assign a free number, else
1202 * check number is not in use
1204 if (rdev->desc_nr < 0) {
1205 int choice = 0;
1206 if (mddev->pers) choice = mddev->raid_disks;
1207 while (find_rdev_nr(mddev, choice))
1208 choice++;
1209 rdev->desc_nr = choice;
1210 } else {
1211 if (find_rdev_nr(mddev, rdev->desc_nr))
1212 return -EBUSY;
1214 bdevname(rdev->bdev,b);
1215 if (kobject_set_name(&rdev->kobj, "dev-%s", b) < 0)
1216 return -ENOMEM;
1218 list_add(&rdev->same_set, &mddev->disks);
1219 rdev->mddev = mddev;
1220 printk(KERN_INFO "md: bind<%s>\n", b);
1222 rdev->kobj.parent = &mddev->kobj;
1223 kobject_add(&rdev->kobj);
1225 if (rdev->bdev->bd_part)
1226 ko = &rdev->bdev->bd_part->kobj;
1227 else
1228 ko = &rdev->bdev->bd_disk->kobj;
1229 sysfs_create_link(&rdev->kobj, ko, "block");
1230 return 0;
1233 static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1235 char b[BDEVNAME_SIZE];
1236 if (!rdev->mddev) {
1237 MD_BUG();
1238 return;
1240 list_del_init(&rdev->same_set);
1241 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1242 rdev->mddev = NULL;
1243 sysfs_remove_link(&rdev->kobj, "block");
1244 kobject_del(&rdev->kobj);
1248 * prevent the device from being mounted, repartitioned or
1249 * otherwise reused by a RAID array (or any other kernel
1250 * subsystem), by bd_claiming the device.
1252 static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1254 int err = 0;
1255 struct block_device *bdev;
1256 char b[BDEVNAME_SIZE];
1258 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1259 if (IS_ERR(bdev)) {
1260 printk(KERN_ERR "md: could not open %s.\n",
1261 __bdevname(dev, b));
1262 return PTR_ERR(bdev);
1264 err = bd_claim(bdev, rdev);
1265 if (err) {
1266 printk(KERN_ERR "md: could not bd_claim %s.\n",
1267 bdevname(bdev, b));
1268 blkdev_put(bdev);
1269 return err;
1271 rdev->bdev = bdev;
1272 return err;
1275 static void unlock_rdev(mdk_rdev_t *rdev)
1277 struct block_device *bdev = rdev->bdev;
1278 rdev->bdev = NULL;
1279 if (!bdev)
1280 MD_BUG();
1281 bd_release(bdev);
1282 blkdev_put(bdev);
1285 void md_autodetect_dev(dev_t dev);
1287 static void export_rdev(mdk_rdev_t * rdev)
1289 char b[BDEVNAME_SIZE];
1290 printk(KERN_INFO "md: export_rdev(%s)\n",
1291 bdevname(rdev->bdev,b));
1292 if (rdev->mddev)
1293 MD_BUG();
1294 free_disk_sb(rdev);
1295 list_del_init(&rdev->same_set);
1296 #ifndef MODULE
1297 md_autodetect_dev(rdev->bdev->bd_dev);
1298 #endif
1299 unlock_rdev(rdev);
1300 kobject_put(&rdev->kobj);
1303 static void kick_rdev_from_array(mdk_rdev_t * rdev)
1305 unbind_rdev_from_array(rdev);
1306 export_rdev(rdev);
1309 static void export_array(mddev_t *mddev)
1311 struct list_head *tmp;
1312 mdk_rdev_t *rdev;
1314 ITERATE_RDEV(mddev,rdev,tmp) {
1315 if (!rdev->mddev) {
1316 MD_BUG();
1317 continue;
1319 kick_rdev_from_array(rdev);
1321 if (!list_empty(&mddev->disks))
1322 MD_BUG();
1323 mddev->raid_disks = 0;
1324 mddev->major_version = 0;
1327 static void print_desc(mdp_disk_t *desc)
1329 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1330 desc->major,desc->minor,desc->raid_disk,desc->state);
1333 static void print_sb(mdp_super_t *sb)
1335 int i;
1337 printk(KERN_INFO
1338 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1339 sb->major_version, sb->minor_version, sb->patch_version,
1340 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1341 sb->ctime);
1342 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1343 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1344 sb->md_minor, sb->layout, sb->chunk_size);
1345 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1346 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1347 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1348 sb->failed_disks, sb->spare_disks,
1349 sb->sb_csum, (unsigned long)sb->events_lo);
1351 printk(KERN_INFO);
1352 for (i = 0; i < MD_SB_DISKS; i++) {
1353 mdp_disk_t *desc;
1355 desc = sb->disks + i;
1356 if (desc->number || desc->major || desc->minor ||
1357 desc->raid_disk || (desc->state && (desc->state != 4))) {
1358 printk(" D %2d: ", i);
1359 print_desc(desc);
1362 printk(KERN_INFO "md: THIS: ");
1363 print_desc(&sb->this_disk);
1367 static void print_rdev(mdk_rdev_t *rdev)
1369 char b[BDEVNAME_SIZE];
1370 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1371 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
1372 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1373 rdev->desc_nr);
1374 if (rdev->sb_loaded) {
1375 printk(KERN_INFO "md: rdev superblock:\n");
1376 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1377 } else
1378 printk(KERN_INFO "md: no rdev superblock!\n");
1381 void md_print_devices(void)
1383 struct list_head *tmp, *tmp2;
1384 mdk_rdev_t *rdev;
1385 mddev_t *mddev;
1386 char b[BDEVNAME_SIZE];
1388 printk("\n");
1389 printk("md: **********************************\n");
1390 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1391 printk("md: **********************************\n");
1392 ITERATE_MDDEV(mddev,tmp) {
1394 if (mddev->bitmap)
1395 bitmap_print_sb(mddev->bitmap);
1396 else
1397 printk("%s: ", mdname(mddev));
1398 ITERATE_RDEV(mddev,rdev,tmp2)
1399 printk("<%s>", bdevname(rdev->bdev,b));
1400 printk("\n");
1402 ITERATE_RDEV(mddev,rdev,tmp2)
1403 print_rdev(rdev);
1405 printk("md: **********************************\n");
1406 printk("\n");
1410 static void sync_sbs(mddev_t * mddev)
1412 mdk_rdev_t *rdev;
1413 struct list_head *tmp;
1415 ITERATE_RDEV(mddev,rdev,tmp) {
1416 super_types[mddev->major_version].
1417 sync_super(mddev, rdev);
1418 rdev->sb_loaded = 1;
1422 static void md_update_sb(mddev_t * mddev)
1424 int err;
1425 struct list_head *tmp;
1426 mdk_rdev_t *rdev;
1427 int sync_req;
1429 repeat:
1430 spin_lock_irq(&mddev->write_lock);
1431 sync_req = mddev->in_sync;
1432 mddev->utime = get_seconds();
1433 mddev->events ++;
1435 if (!mddev->events) {
1437 * oops, this 64-bit counter should never wrap.
1438 * Either we are in around ~1 trillion A.C., assuming
1439 * 1 reboot per second, or we have a bug:
1441 MD_BUG();
1442 mddev->events --;
1444 mddev->sb_dirty = 2;
1445 sync_sbs(mddev);
1448 * do not write anything to disk if using
1449 * nonpersistent superblocks
1451 if (!mddev->persistent) {
1452 mddev->sb_dirty = 0;
1453 spin_unlock_irq(&mddev->write_lock);
1454 wake_up(&mddev->sb_wait);
1455 return;
1457 spin_unlock_irq(&mddev->write_lock);
1459 dprintk(KERN_INFO
1460 "md: updating %s RAID superblock on device (in sync %d)\n",
1461 mdname(mddev),mddev->in_sync);
1463 err = bitmap_update_sb(mddev->bitmap);
1464 ITERATE_RDEV(mddev,rdev,tmp) {
1465 char b[BDEVNAME_SIZE];
1466 dprintk(KERN_INFO "md: ");
1467 if (test_bit(Faulty, &rdev->flags))
1468 dprintk("(skipping faulty ");
1470 dprintk("%s ", bdevname(rdev->bdev,b));
1471 if (!test_bit(Faulty, &rdev->flags)) {
1472 md_super_write(mddev,rdev,
1473 rdev->sb_offset<<1, rdev->sb_size,
1474 rdev->sb_page);
1475 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1476 bdevname(rdev->bdev,b),
1477 (unsigned long long)rdev->sb_offset);
1479 } else
1480 dprintk(")\n");
1481 if (mddev->level == LEVEL_MULTIPATH)
1482 /* only need to write one superblock... */
1483 break;
1485 md_super_wait(mddev);
1486 /* if there was a failure, sb_dirty was set to 1, and we re-write super */
1488 spin_lock_irq(&mddev->write_lock);
1489 if (mddev->in_sync != sync_req|| mddev->sb_dirty == 1) {
1490 /* have to write it out again */
1491 spin_unlock_irq(&mddev->write_lock);
1492 goto repeat;
1494 mddev->sb_dirty = 0;
1495 spin_unlock_irq(&mddev->write_lock);
1496 wake_up(&mddev->sb_wait);
1500 struct rdev_sysfs_entry {
1501 struct attribute attr;
1502 ssize_t (*show)(mdk_rdev_t *, char *);
1503 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1506 static ssize_t
1507 state_show(mdk_rdev_t *rdev, char *page)
1509 char *sep = "";
1510 int len=0;
1512 if (test_bit(Faulty, &rdev->flags)) {
1513 len+= sprintf(page+len, "%sfaulty",sep);
1514 sep = ",";
1516 if (test_bit(In_sync, &rdev->flags)) {
1517 len += sprintf(page+len, "%sin_sync",sep);
1518 sep = ",";
1520 if (!test_bit(Faulty, &rdev->flags) &&
1521 !test_bit(In_sync, &rdev->flags)) {
1522 len += sprintf(page+len, "%sspare", sep);
1523 sep = ",";
1525 return len+sprintf(page+len, "\n");
1528 static struct rdev_sysfs_entry
1529 rdev_state = __ATTR_RO(state);
1531 static ssize_t
1532 super_show(mdk_rdev_t *rdev, char *page)
1534 if (rdev->sb_loaded && rdev->sb_size) {
1535 memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
1536 return rdev->sb_size;
1537 } else
1538 return 0;
1540 static struct rdev_sysfs_entry rdev_super = __ATTR_RO(super);
1542 static struct attribute *rdev_default_attrs[] = {
1543 &rdev_state.attr,
1544 &rdev_super.attr,
1545 NULL,
1547 static ssize_t
1548 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1550 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1551 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1553 if (!entry->show)
1554 return -EIO;
1555 return entry->show(rdev, page);
1558 static ssize_t
1559 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
1560 const char *page, size_t length)
1562 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1563 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1565 if (!entry->store)
1566 return -EIO;
1567 return entry->store(rdev, page, length);
1570 static void rdev_free(struct kobject *ko)
1572 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
1573 kfree(rdev);
1575 static struct sysfs_ops rdev_sysfs_ops = {
1576 .show = rdev_attr_show,
1577 .store = rdev_attr_store,
1579 static struct kobj_type rdev_ktype = {
1580 .release = rdev_free,
1581 .sysfs_ops = &rdev_sysfs_ops,
1582 .default_attrs = rdev_default_attrs,
1586 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1588 * mark the device faulty if:
1590 * - the device is nonexistent (zero size)
1591 * - the device has no valid superblock
1593 * a faulty rdev _never_ has rdev->sb set.
1595 static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
1597 char b[BDEVNAME_SIZE];
1598 int err;
1599 mdk_rdev_t *rdev;
1600 sector_t size;
1602 rdev = (mdk_rdev_t *) kmalloc(sizeof(*rdev), GFP_KERNEL);
1603 if (!rdev) {
1604 printk(KERN_ERR "md: could not alloc mem for new device!\n");
1605 return ERR_PTR(-ENOMEM);
1607 memset(rdev, 0, sizeof(*rdev));
1609 if ((err = alloc_disk_sb(rdev)))
1610 goto abort_free;
1612 err = lock_rdev(rdev, newdev);
1613 if (err)
1614 goto abort_free;
1616 rdev->kobj.parent = NULL;
1617 rdev->kobj.ktype = &rdev_ktype;
1618 kobject_init(&rdev->kobj);
1620 rdev->desc_nr = -1;
1621 rdev->flags = 0;
1622 rdev->data_offset = 0;
1623 atomic_set(&rdev->nr_pending, 0);
1624 atomic_set(&rdev->read_errors, 0);
1626 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
1627 if (!size) {
1628 printk(KERN_WARNING
1629 "md: %s has zero or unknown size, marking faulty!\n",
1630 bdevname(rdev->bdev,b));
1631 err = -EINVAL;
1632 goto abort_free;
1635 if (super_format >= 0) {
1636 err = super_types[super_format].
1637 load_super(rdev, NULL, super_minor);
1638 if (err == -EINVAL) {
1639 printk(KERN_WARNING
1640 "md: %s has invalid sb, not importing!\n",
1641 bdevname(rdev->bdev,b));
1642 goto abort_free;
1644 if (err < 0) {
1645 printk(KERN_WARNING
1646 "md: could not read %s's sb, not importing!\n",
1647 bdevname(rdev->bdev,b));
1648 goto abort_free;
1651 INIT_LIST_HEAD(&rdev->same_set);
1653 return rdev;
1655 abort_free:
1656 if (rdev->sb_page) {
1657 if (rdev->bdev)
1658 unlock_rdev(rdev);
1659 free_disk_sb(rdev);
1661 kfree(rdev);
1662 return ERR_PTR(err);
1666 * Check a full RAID array for plausibility
1670 static void analyze_sbs(mddev_t * mddev)
1672 int i;
1673 struct list_head *tmp;
1674 mdk_rdev_t *rdev, *freshest;
1675 char b[BDEVNAME_SIZE];
1677 freshest = NULL;
1678 ITERATE_RDEV(mddev,rdev,tmp)
1679 switch (super_types[mddev->major_version].
1680 load_super(rdev, freshest, mddev->minor_version)) {
1681 case 1:
1682 freshest = rdev;
1683 break;
1684 case 0:
1685 break;
1686 default:
1687 printk( KERN_ERR \
1688 "md: fatal superblock inconsistency in %s"
1689 " -- removing from array\n",
1690 bdevname(rdev->bdev,b));
1691 kick_rdev_from_array(rdev);
1695 super_types[mddev->major_version].
1696 validate_super(mddev, freshest);
1698 i = 0;
1699 ITERATE_RDEV(mddev,rdev,tmp) {
1700 if (rdev != freshest)
1701 if (super_types[mddev->major_version].
1702 validate_super(mddev, rdev)) {
1703 printk(KERN_WARNING "md: kicking non-fresh %s"
1704 " from array!\n",
1705 bdevname(rdev->bdev,b));
1706 kick_rdev_from_array(rdev);
1707 continue;
1709 if (mddev->level == LEVEL_MULTIPATH) {
1710 rdev->desc_nr = i++;
1711 rdev->raid_disk = rdev->desc_nr;
1712 set_bit(In_sync, &rdev->flags);
1718 if (mddev->recovery_cp != MaxSector &&
1719 mddev->level >= 1)
1720 printk(KERN_ERR "md: %s: raid array is not clean"
1721 " -- starting background reconstruction\n",
1722 mdname(mddev));
1726 static ssize_t
1727 level_show(mddev_t *mddev, char *page)
1729 mdk_personality_t *p = mddev->pers;
1730 if (p == NULL && mddev->raid_disks == 0)
1731 return 0;
1732 if (mddev->level >= 0)
1733 return sprintf(page, "RAID-%d\n", mddev->level);
1734 else
1735 return sprintf(page, "%s\n", p->name);
1738 static struct md_sysfs_entry md_level = __ATTR_RO(level);
1740 static ssize_t
1741 raid_disks_show(mddev_t *mddev, char *page)
1743 if (mddev->raid_disks == 0)
1744 return 0;
1745 return sprintf(page, "%d\n", mddev->raid_disks);
1748 static struct md_sysfs_entry md_raid_disks = __ATTR_RO(raid_disks);
1750 static ssize_t
1751 action_show(mddev_t *mddev, char *page)
1753 char *type = "idle";
1754 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
1755 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) {
1756 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1757 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
1758 type = "resync";
1759 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1760 type = "check";
1761 else
1762 type = "repair";
1763 } else
1764 type = "recover";
1766 return sprintf(page, "%s\n", type);
1769 static ssize_t
1770 action_store(mddev_t *mddev, const char *page, size_t len)
1772 if (!mddev->pers || !mddev->pers->sync_request)
1773 return -EINVAL;
1775 if (strcmp(page, "idle")==0 || strcmp(page, "idle\n")==0) {
1776 if (mddev->sync_thread) {
1777 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1778 md_unregister_thread(mddev->sync_thread);
1779 mddev->sync_thread = NULL;
1780 mddev->recovery = 0;
1782 return len;
1785 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
1786 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
1787 return -EBUSY;
1788 if (strcmp(page, "resync")==0 || strcmp(page, "resync\n")==0 ||
1789 strcmp(page, "recover")==0 || strcmp(page, "recover\n")==0)
1790 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1791 else {
1792 if (strcmp(page, "check")==0 || strcmp(page, "check\n")==0)
1793 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
1794 else if (strcmp(page, "repair")!=0 && strcmp(page, "repair\n")!=0)
1795 return -EINVAL;
1796 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
1797 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
1798 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1800 md_wakeup_thread(mddev->thread);
1801 return len;
1804 static ssize_t
1805 mismatch_cnt_show(mddev_t *mddev, char *page)
1807 return sprintf(page, "%llu\n",
1808 (unsigned long long) mddev->resync_mismatches);
1811 static struct md_sysfs_entry
1812 md_scan_mode = __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
1815 static struct md_sysfs_entry
1816 md_mismatches = __ATTR_RO(mismatch_cnt);
1818 static struct attribute *md_default_attrs[] = {
1819 &md_level.attr,
1820 &md_raid_disks.attr,
1821 NULL,
1824 static struct attribute *md_redundancy_attrs[] = {
1825 &md_scan_mode.attr,
1826 &md_mismatches.attr,
1827 NULL,
1829 static struct attribute_group md_redundancy_group = {
1830 .name = NULL,
1831 .attrs = md_redundancy_attrs,
1835 static ssize_t
1836 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1838 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
1839 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
1840 ssize_t rv;
1842 if (!entry->show)
1843 return -EIO;
1844 mddev_lock(mddev);
1845 rv = entry->show(mddev, page);
1846 mddev_unlock(mddev);
1847 return rv;
1850 static ssize_t
1851 md_attr_store(struct kobject *kobj, struct attribute *attr,
1852 const char *page, size_t length)
1854 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
1855 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
1856 ssize_t rv;
1858 if (!entry->store)
1859 return -EIO;
1860 mddev_lock(mddev);
1861 rv = entry->store(mddev, page, length);
1862 mddev_unlock(mddev);
1863 return rv;
1866 static void md_free(struct kobject *ko)
1868 mddev_t *mddev = container_of(ko, mddev_t, kobj);
1869 kfree(mddev);
1872 static struct sysfs_ops md_sysfs_ops = {
1873 .show = md_attr_show,
1874 .store = md_attr_store,
1876 static struct kobj_type md_ktype = {
1877 .release = md_free,
1878 .sysfs_ops = &md_sysfs_ops,
1879 .default_attrs = md_default_attrs,
1882 int mdp_major = 0;
1884 static struct kobject *md_probe(dev_t dev, int *part, void *data)
1886 static DECLARE_MUTEX(disks_sem);
1887 mddev_t *mddev = mddev_find(dev);
1888 struct gendisk *disk;
1889 int partitioned = (MAJOR(dev) != MD_MAJOR);
1890 int shift = partitioned ? MdpMinorShift : 0;
1891 int unit = MINOR(dev) >> shift;
1893 if (!mddev)
1894 return NULL;
1896 down(&disks_sem);
1897 if (mddev->gendisk) {
1898 up(&disks_sem);
1899 mddev_put(mddev);
1900 return NULL;
1902 disk = alloc_disk(1 << shift);
1903 if (!disk) {
1904 up(&disks_sem);
1905 mddev_put(mddev);
1906 return NULL;
1908 disk->major = MAJOR(dev);
1909 disk->first_minor = unit << shift;
1910 if (partitioned) {
1911 sprintf(disk->disk_name, "md_d%d", unit);
1912 sprintf(disk->devfs_name, "md/d%d", unit);
1913 } else {
1914 sprintf(disk->disk_name, "md%d", unit);
1915 sprintf(disk->devfs_name, "md/%d", unit);
1917 disk->fops = &md_fops;
1918 disk->private_data = mddev;
1919 disk->queue = mddev->queue;
1920 add_disk(disk);
1921 mddev->gendisk = disk;
1922 up(&disks_sem);
1923 mddev->kobj.parent = &disk->kobj;
1924 mddev->kobj.k_name = NULL;
1925 snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md");
1926 mddev->kobj.ktype = &md_ktype;
1927 kobject_register(&mddev->kobj);
1928 return NULL;
1931 void md_wakeup_thread(mdk_thread_t *thread);
1933 static void md_safemode_timeout(unsigned long data)
1935 mddev_t *mddev = (mddev_t *) data;
1937 mddev->safemode = 1;
1938 md_wakeup_thread(mddev->thread);
1942 static int do_md_run(mddev_t * mddev)
1944 int pnum, err;
1945 int chunk_size;
1946 struct list_head *tmp;
1947 mdk_rdev_t *rdev;
1948 struct gendisk *disk;
1949 char b[BDEVNAME_SIZE];
1951 if (list_empty(&mddev->disks))
1952 /* cannot run an array with no devices.. */
1953 return -EINVAL;
1955 if (mddev->pers)
1956 return -EBUSY;
1959 * Analyze all RAID superblock(s)
1961 if (!mddev->raid_disks)
1962 analyze_sbs(mddev);
1964 chunk_size = mddev->chunk_size;
1965 pnum = level_to_pers(mddev->level);
1967 if ((pnum != MULTIPATH) && (pnum != RAID1)) {
1968 if (!chunk_size) {
1970 * 'default chunksize' in the old md code used to
1971 * be PAGE_SIZE, baaad.
1972 * we abort here to be on the safe side. We don't
1973 * want to continue the bad practice.
1975 printk(KERN_ERR
1976 "no chunksize specified, see 'man raidtab'\n");
1977 return -EINVAL;
1979 if (chunk_size > MAX_CHUNK_SIZE) {
1980 printk(KERN_ERR "too big chunk_size: %d > %d\n",
1981 chunk_size, MAX_CHUNK_SIZE);
1982 return -EINVAL;
1985 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
1987 if ( (1 << ffz(~chunk_size)) != chunk_size) {
1988 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
1989 return -EINVAL;
1991 if (chunk_size < PAGE_SIZE) {
1992 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
1993 chunk_size, PAGE_SIZE);
1994 return -EINVAL;
1997 /* devices must have minimum size of one chunk */
1998 ITERATE_RDEV(mddev,rdev,tmp) {
1999 if (test_bit(Faulty, &rdev->flags))
2000 continue;
2001 if (rdev->size < chunk_size / 1024) {
2002 printk(KERN_WARNING
2003 "md: Dev %s smaller than chunk_size:"
2004 " %lluk < %dk\n",
2005 bdevname(rdev->bdev,b),
2006 (unsigned long long)rdev->size,
2007 chunk_size / 1024);
2008 return -EINVAL;
2013 #ifdef CONFIG_KMOD
2014 if (!pers[pnum])
2016 request_module("md-personality-%d", pnum);
2018 #endif
2021 * Drop all container device buffers, from now on
2022 * the only valid external interface is through the md
2023 * device.
2024 * Also find largest hardsector size
2026 ITERATE_RDEV(mddev,rdev,tmp) {
2027 if (test_bit(Faulty, &rdev->flags))
2028 continue;
2029 sync_blockdev(rdev->bdev);
2030 invalidate_bdev(rdev->bdev, 0);
2033 md_probe(mddev->unit, NULL, NULL);
2034 disk = mddev->gendisk;
2035 if (!disk)
2036 return -ENOMEM;
2038 spin_lock(&pers_lock);
2039 if (!pers[pnum] || !try_module_get(pers[pnum]->owner)) {
2040 spin_unlock(&pers_lock);
2041 printk(KERN_WARNING "md: personality %d is not loaded!\n",
2042 pnum);
2043 return -EINVAL;
2046 mddev->pers = pers[pnum];
2047 spin_unlock(&pers_lock);
2049 mddev->recovery = 0;
2050 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
2051 mddev->barriers_work = 1;
2053 if (start_readonly)
2054 mddev->ro = 2; /* read-only, but switch on first write */
2056 /* before we start the array running, initialise the bitmap */
2057 err = bitmap_create(mddev);
2058 if (err)
2059 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
2060 mdname(mddev), err);
2061 else
2062 err = mddev->pers->run(mddev);
2063 if (err) {
2064 printk(KERN_ERR "md: pers->run() failed ...\n");
2065 module_put(mddev->pers->owner);
2066 mddev->pers = NULL;
2067 bitmap_destroy(mddev);
2068 return err;
2070 if (mddev->pers->sync_request)
2071 sysfs_create_group(&mddev->kobj, &md_redundancy_group);
2072 else if (mddev->ro == 2) /* auto-readonly not meaningful */
2073 mddev->ro = 0;
2075 atomic_set(&mddev->writes_pending,0);
2076 mddev->safemode = 0;
2077 mddev->safemode_timer.function = md_safemode_timeout;
2078 mddev->safemode_timer.data = (unsigned long) mddev;
2079 mddev->safemode_delay = (20 * HZ)/1000 +1; /* 20 msec delay */
2080 mddev->in_sync = 1;
2082 ITERATE_RDEV(mddev,rdev,tmp)
2083 if (rdev->raid_disk >= 0) {
2084 char nm[20];
2085 sprintf(nm, "rd%d", rdev->raid_disk);
2086 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
2089 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2090 md_wakeup_thread(mddev->thread);
2092 if (mddev->sb_dirty)
2093 md_update_sb(mddev);
2095 set_capacity(disk, mddev->array_size<<1);
2097 /* If we call blk_queue_make_request here, it will
2098 * re-initialise max_sectors etc which may have been
2099 * refined inside -> run. So just set the bits we need to set.
2100 * Most initialisation happended when we called
2101 * blk_queue_make_request(..., md_fail_request)
2102 * earlier.
2104 mddev->queue->queuedata = mddev;
2105 mddev->queue->make_request_fn = mddev->pers->make_request;
2107 mddev->changed = 1;
2108 return 0;
2111 static int restart_array(mddev_t *mddev)
2113 struct gendisk *disk = mddev->gendisk;
2114 int err;
2117 * Complain if it has no devices
2119 err = -ENXIO;
2120 if (list_empty(&mddev->disks))
2121 goto out;
2123 if (mddev->pers) {
2124 err = -EBUSY;
2125 if (!mddev->ro)
2126 goto out;
2128 mddev->safemode = 0;
2129 mddev->ro = 0;
2130 set_disk_ro(disk, 0);
2132 printk(KERN_INFO "md: %s switched to read-write mode.\n",
2133 mdname(mddev));
2135 * Kick recovery or resync if necessary
2137 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2138 md_wakeup_thread(mddev->thread);
2139 err = 0;
2140 } else {
2141 printk(KERN_ERR "md: %s has no personality assigned.\n",
2142 mdname(mddev));
2143 err = -EINVAL;
2146 out:
2147 return err;
2150 static int do_md_stop(mddev_t * mddev, int ro)
2152 int err = 0;
2153 struct gendisk *disk = mddev->gendisk;
2155 if (mddev->pers) {
2156 if (atomic_read(&mddev->active)>2) {
2157 printk("md: %s still in use.\n",mdname(mddev));
2158 return -EBUSY;
2161 if (mddev->sync_thread) {
2162 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2163 md_unregister_thread(mddev->sync_thread);
2164 mddev->sync_thread = NULL;
2167 del_timer_sync(&mddev->safemode_timer);
2169 invalidate_partition(disk, 0);
2171 if (ro) {
2172 err = -ENXIO;
2173 if (mddev->ro==1)
2174 goto out;
2175 mddev->ro = 1;
2176 } else {
2177 bitmap_flush(mddev);
2178 md_super_wait(mddev);
2179 if (mddev->ro)
2180 set_disk_ro(disk, 0);
2181 blk_queue_make_request(mddev->queue, md_fail_request);
2182 mddev->pers->stop(mddev);
2183 if (mddev->pers->sync_request)
2184 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
2186 module_put(mddev->pers->owner);
2187 mddev->pers = NULL;
2188 if (mddev->ro)
2189 mddev->ro = 0;
2191 if (!mddev->in_sync) {
2192 /* mark array as shutdown cleanly */
2193 mddev->in_sync = 1;
2194 md_update_sb(mddev);
2196 if (ro)
2197 set_disk_ro(disk, 1);
2200 bitmap_destroy(mddev);
2201 if (mddev->bitmap_file) {
2202 atomic_set(&mddev->bitmap_file->f_dentry->d_inode->i_writecount, 1);
2203 fput(mddev->bitmap_file);
2204 mddev->bitmap_file = NULL;
2206 mddev->bitmap_offset = 0;
2209 * Free resources if final stop
2211 if (!ro) {
2212 mdk_rdev_t *rdev;
2213 struct list_head *tmp;
2214 struct gendisk *disk;
2215 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
2217 ITERATE_RDEV(mddev,rdev,tmp)
2218 if (rdev->raid_disk >= 0) {
2219 char nm[20];
2220 sprintf(nm, "rd%d", rdev->raid_disk);
2221 sysfs_remove_link(&mddev->kobj, nm);
2224 export_array(mddev);
2226 mddev->array_size = 0;
2227 disk = mddev->gendisk;
2228 if (disk)
2229 set_capacity(disk, 0);
2230 mddev->changed = 1;
2231 } else
2232 printk(KERN_INFO "md: %s switched to read-only mode.\n",
2233 mdname(mddev));
2234 err = 0;
2235 out:
2236 return err;
2239 static void autorun_array(mddev_t *mddev)
2241 mdk_rdev_t *rdev;
2242 struct list_head *tmp;
2243 int err;
2245 if (list_empty(&mddev->disks))
2246 return;
2248 printk(KERN_INFO "md: running: ");
2250 ITERATE_RDEV(mddev,rdev,tmp) {
2251 char b[BDEVNAME_SIZE];
2252 printk("<%s>", bdevname(rdev->bdev,b));
2254 printk("\n");
2256 err = do_md_run (mddev);
2257 if (err) {
2258 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
2259 do_md_stop (mddev, 0);
2264 * lets try to run arrays based on all disks that have arrived
2265 * until now. (those are in pending_raid_disks)
2267 * the method: pick the first pending disk, collect all disks with
2268 * the same UUID, remove all from the pending list and put them into
2269 * the 'same_array' list. Then order this list based on superblock
2270 * update time (freshest comes first), kick out 'old' disks and
2271 * compare superblocks. If everything's fine then run it.
2273 * If "unit" is allocated, then bump its reference count
2275 static void autorun_devices(int part)
2277 struct list_head candidates;
2278 struct list_head *tmp;
2279 mdk_rdev_t *rdev0, *rdev;
2280 mddev_t *mddev;
2281 char b[BDEVNAME_SIZE];
2283 printk(KERN_INFO "md: autorun ...\n");
2284 while (!list_empty(&pending_raid_disks)) {
2285 dev_t dev;
2286 rdev0 = list_entry(pending_raid_disks.next,
2287 mdk_rdev_t, same_set);
2289 printk(KERN_INFO "md: considering %s ...\n",
2290 bdevname(rdev0->bdev,b));
2291 INIT_LIST_HEAD(&candidates);
2292 ITERATE_RDEV_PENDING(rdev,tmp)
2293 if (super_90_load(rdev, rdev0, 0) >= 0) {
2294 printk(KERN_INFO "md: adding %s ...\n",
2295 bdevname(rdev->bdev,b));
2296 list_move(&rdev->same_set, &candidates);
2299 * now we have a set of devices, with all of them having
2300 * mostly sane superblocks. It's time to allocate the
2301 * mddev.
2303 if (rdev0->preferred_minor < 0 || rdev0->preferred_minor >= MAX_MD_DEVS) {
2304 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
2305 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
2306 break;
2308 if (part)
2309 dev = MKDEV(mdp_major,
2310 rdev0->preferred_minor << MdpMinorShift);
2311 else
2312 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
2314 md_probe(dev, NULL, NULL);
2315 mddev = mddev_find(dev);
2316 if (!mddev) {
2317 printk(KERN_ERR
2318 "md: cannot allocate memory for md drive.\n");
2319 break;
2321 if (mddev_lock(mddev))
2322 printk(KERN_WARNING "md: %s locked, cannot run\n",
2323 mdname(mddev));
2324 else if (mddev->raid_disks || mddev->major_version
2325 || !list_empty(&mddev->disks)) {
2326 printk(KERN_WARNING
2327 "md: %s already running, cannot run %s\n",
2328 mdname(mddev), bdevname(rdev0->bdev,b));
2329 mddev_unlock(mddev);
2330 } else {
2331 printk(KERN_INFO "md: created %s\n", mdname(mddev));
2332 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
2333 list_del_init(&rdev->same_set);
2334 if (bind_rdev_to_array(rdev, mddev))
2335 export_rdev(rdev);
2337 autorun_array(mddev);
2338 mddev_unlock(mddev);
2340 /* on success, candidates will be empty, on error
2341 * it won't...
2343 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
2344 export_rdev(rdev);
2345 mddev_put(mddev);
2347 printk(KERN_INFO "md: ... autorun DONE.\n");
2351 * import RAID devices based on one partition
2352 * if possible, the array gets run as well.
2355 static int autostart_array(dev_t startdev)
2357 char b[BDEVNAME_SIZE];
2358 int err = -EINVAL, i;
2359 mdp_super_t *sb = NULL;
2360 mdk_rdev_t *start_rdev = NULL, *rdev;
2362 start_rdev = md_import_device(startdev, 0, 0);
2363 if (IS_ERR(start_rdev))
2364 return err;
2367 /* NOTE: this can only work for 0.90.0 superblocks */
2368 sb = (mdp_super_t*)page_address(start_rdev->sb_page);
2369 if (sb->major_version != 0 ||
2370 sb->minor_version != 90 ) {
2371 printk(KERN_WARNING "md: can only autostart 0.90.0 arrays\n");
2372 export_rdev(start_rdev);
2373 return err;
2376 if (test_bit(Faulty, &start_rdev->flags)) {
2377 printk(KERN_WARNING
2378 "md: can not autostart based on faulty %s!\n",
2379 bdevname(start_rdev->bdev,b));
2380 export_rdev(start_rdev);
2381 return err;
2383 list_add(&start_rdev->same_set, &pending_raid_disks);
2385 for (i = 0; i < MD_SB_DISKS; i++) {
2386 mdp_disk_t *desc = sb->disks + i;
2387 dev_t dev = MKDEV(desc->major, desc->minor);
2389 if (!dev)
2390 continue;
2391 if (dev == startdev)
2392 continue;
2393 if (MAJOR(dev) != desc->major || MINOR(dev) != desc->minor)
2394 continue;
2395 rdev = md_import_device(dev, 0, 0);
2396 if (IS_ERR(rdev))
2397 continue;
2399 list_add(&rdev->same_set, &pending_raid_disks);
2403 * possibly return codes
2405 autorun_devices(0);
2406 return 0;
2411 static int get_version(void __user * arg)
2413 mdu_version_t ver;
2415 ver.major = MD_MAJOR_VERSION;
2416 ver.minor = MD_MINOR_VERSION;
2417 ver.patchlevel = MD_PATCHLEVEL_VERSION;
2419 if (copy_to_user(arg, &ver, sizeof(ver)))
2420 return -EFAULT;
2422 return 0;
2425 static int get_array_info(mddev_t * mddev, void __user * arg)
2427 mdu_array_info_t info;
2428 int nr,working,active,failed,spare;
2429 mdk_rdev_t *rdev;
2430 struct list_head *tmp;
2432 nr=working=active=failed=spare=0;
2433 ITERATE_RDEV(mddev,rdev,tmp) {
2434 nr++;
2435 if (test_bit(Faulty, &rdev->flags))
2436 failed++;
2437 else {
2438 working++;
2439 if (test_bit(In_sync, &rdev->flags))
2440 active++;
2441 else
2442 spare++;
2446 info.major_version = mddev->major_version;
2447 info.minor_version = mddev->minor_version;
2448 info.patch_version = MD_PATCHLEVEL_VERSION;
2449 info.ctime = mddev->ctime;
2450 info.level = mddev->level;
2451 info.size = mddev->size;
2452 info.nr_disks = nr;
2453 info.raid_disks = mddev->raid_disks;
2454 info.md_minor = mddev->md_minor;
2455 info.not_persistent= !mddev->persistent;
2457 info.utime = mddev->utime;
2458 info.state = 0;
2459 if (mddev->in_sync)
2460 info.state = (1<<MD_SB_CLEAN);
2461 if (mddev->bitmap && mddev->bitmap_offset)
2462 info.state = (1<<MD_SB_BITMAP_PRESENT);
2463 info.active_disks = active;
2464 info.working_disks = working;
2465 info.failed_disks = failed;
2466 info.spare_disks = spare;
2468 info.layout = mddev->layout;
2469 info.chunk_size = mddev->chunk_size;
2471 if (copy_to_user(arg, &info, sizeof(info)))
2472 return -EFAULT;
2474 return 0;
2477 static int get_bitmap_file(mddev_t * mddev, void __user * arg)
2479 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
2480 char *ptr, *buf = NULL;
2481 int err = -ENOMEM;
2483 file = kmalloc(sizeof(*file), GFP_KERNEL);
2484 if (!file)
2485 goto out;
2487 /* bitmap disabled, zero the first byte and copy out */
2488 if (!mddev->bitmap || !mddev->bitmap->file) {
2489 file->pathname[0] = '\0';
2490 goto copy_out;
2493 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
2494 if (!buf)
2495 goto out;
2497 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
2498 if (!ptr)
2499 goto out;
2501 strcpy(file->pathname, ptr);
2503 copy_out:
2504 err = 0;
2505 if (copy_to_user(arg, file, sizeof(*file)))
2506 err = -EFAULT;
2507 out:
2508 kfree(buf);
2509 kfree(file);
2510 return err;
2513 static int get_disk_info(mddev_t * mddev, void __user * arg)
2515 mdu_disk_info_t info;
2516 unsigned int nr;
2517 mdk_rdev_t *rdev;
2519 if (copy_from_user(&info, arg, sizeof(info)))
2520 return -EFAULT;
2522 nr = info.number;
2524 rdev = find_rdev_nr(mddev, nr);
2525 if (rdev) {
2526 info.major = MAJOR(rdev->bdev->bd_dev);
2527 info.minor = MINOR(rdev->bdev->bd_dev);
2528 info.raid_disk = rdev->raid_disk;
2529 info.state = 0;
2530 if (test_bit(Faulty, &rdev->flags))
2531 info.state |= (1<<MD_DISK_FAULTY);
2532 else if (test_bit(In_sync, &rdev->flags)) {
2533 info.state |= (1<<MD_DISK_ACTIVE);
2534 info.state |= (1<<MD_DISK_SYNC);
2536 if (test_bit(WriteMostly, &rdev->flags))
2537 info.state |= (1<<MD_DISK_WRITEMOSTLY);
2538 } else {
2539 info.major = info.minor = 0;
2540 info.raid_disk = -1;
2541 info.state = (1<<MD_DISK_REMOVED);
2544 if (copy_to_user(arg, &info, sizeof(info)))
2545 return -EFAULT;
2547 return 0;
2550 static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
2552 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
2553 mdk_rdev_t *rdev;
2554 dev_t dev = MKDEV(info->major,info->minor);
2556 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
2557 return -EOVERFLOW;
2559 if (!mddev->raid_disks) {
2560 int err;
2561 /* expecting a device which has a superblock */
2562 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
2563 if (IS_ERR(rdev)) {
2564 printk(KERN_WARNING
2565 "md: md_import_device returned %ld\n",
2566 PTR_ERR(rdev));
2567 return PTR_ERR(rdev);
2569 if (!list_empty(&mddev->disks)) {
2570 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2571 mdk_rdev_t, same_set);
2572 int err = super_types[mddev->major_version]
2573 .load_super(rdev, rdev0, mddev->minor_version);
2574 if (err < 0) {
2575 printk(KERN_WARNING
2576 "md: %s has different UUID to %s\n",
2577 bdevname(rdev->bdev,b),
2578 bdevname(rdev0->bdev,b2));
2579 export_rdev(rdev);
2580 return -EINVAL;
2583 err = bind_rdev_to_array(rdev, mddev);
2584 if (err)
2585 export_rdev(rdev);
2586 return err;
2590 * add_new_disk can be used once the array is assembled
2591 * to add "hot spares". They must already have a superblock
2592 * written
2594 if (mddev->pers) {
2595 int err;
2596 if (!mddev->pers->hot_add_disk) {
2597 printk(KERN_WARNING
2598 "%s: personality does not support diskops!\n",
2599 mdname(mddev));
2600 return -EINVAL;
2602 if (mddev->persistent)
2603 rdev = md_import_device(dev, mddev->major_version,
2604 mddev->minor_version);
2605 else
2606 rdev = md_import_device(dev, -1, -1);
2607 if (IS_ERR(rdev)) {
2608 printk(KERN_WARNING
2609 "md: md_import_device returned %ld\n",
2610 PTR_ERR(rdev));
2611 return PTR_ERR(rdev);
2613 /* set save_raid_disk if appropriate */
2614 if (!mddev->persistent) {
2615 if (info->state & (1<<MD_DISK_SYNC) &&
2616 info->raid_disk < mddev->raid_disks)
2617 rdev->raid_disk = info->raid_disk;
2618 else
2619 rdev->raid_disk = -1;
2620 } else
2621 super_types[mddev->major_version].
2622 validate_super(mddev, rdev);
2623 rdev->saved_raid_disk = rdev->raid_disk;
2625 clear_bit(In_sync, &rdev->flags); /* just to be sure */
2626 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
2627 set_bit(WriteMostly, &rdev->flags);
2629 rdev->raid_disk = -1;
2630 err = bind_rdev_to_array(rdev, mddev);
2631 if (err)
2632 export_rdev(rdev);
2634 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2635 md_wakeup_thread(mddev->thread);
2636 return err;
2639 /* otherwise, add_new_disk is only allowed
2640 * for major_version==0 superblocks
2642 if (mddev->major_version != 0) {
2643 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
2644 mdname(mddev));
2645 return -EINVAL;
2648 if (!(info->state & (1<<MD_DISK_FAULTY))) {
2649 int err;
2650 rdev = md_import_device (dev, -1, 0);
2651 if (IS_ERR(rdev)) {
2652 printk(KERN_WARNING
2653 "md: error, md_import_device() returned %ld\n",
2654 PTR_ERR(rdev));
2655 return PTR_ERR(rdev);
2657 rdev->desc_nr = info->number;
2658 if (info->raid_disk < mddev->raid_disks)
2659 rdev->raid_disk = info->raid_disk;
2660 else
2661 rdev->raid_disk = -1;
2663 rdev->flags = 0;
2665 if (rdev->raid_disk < mddev->raid_disks)
2666 if (info->state & (1<<MD_DISK_SYNC))
2667 set_bit(In_sync, &rdev->flags);
2669 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
2670 set_bit(WriteMostly, &rdev->flags);
2672 err = bind_rdev_to_array(rdev, mddev);
2673 if (err) {
2674 export_rdev(rdev);
2675 return err;
2678 if (!mddev->persistent) {
2679 printk(KERN_INFO "md: nonpersistent superblock ...\n");
2680 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2681 } else
2682 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2683 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
2685 if (!mddev->size || (mddev->size > rdev->size))
2686 mddev->size = rdev->size;
2689 return 0;
2692 static int hot_remove_disk(mddev_t * mddev, dev_t dev)
2694 char b[BDEVNAME_SIZE];
2695 mdk_rdev_t *rdev;
2697 if (!mddev->pers)
2698 return -ENODEV;
2700 rdev = find_rdev(mddev, dev);
2701 if (!rdev)
2702 return -ENXIO;
2704 if (rdev->raid_disk >= 0)
2705 goto busy;
2707 kick_rdev_from_array(rdev);
2708 md_update_sb(mddev);
2710 return 0;
2711 busy:
2712 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
2713 bdevname(rdev->bdev,b), mdname(mddev));
2714 return -EBUSY;
2717 static int hot_add_disk(mddev_t * mddev, dev_t dev)
2719 char b[BDEVNAME_SIZE];
2720 int err;
2721 unsigned int size;
2722 mdk_rdev_t *rdev;
2724 if (!mddev->pers)
2725 return -ENODEV;
2727 if (mddev->major_version != 0) {
2728 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
2729 " version-0 superblocks.\n",
2730 mdname(mddev));
2731 return -EINVAL;
2733 if (!mddev->pers->hot_add_disk) {
2734 printk(KERN_WARNING
2735 "%s: personality does not support diskops!\n",
2736 mdname(mddev));
2737 return -EINVAL;
2740 rdev = md_import_device (dev, -1, 0);
2741 if (IS_ERR(rdev)) {
2742 printk(KERN_WARNING
2743 "md: error, md_import_device() returned %ld\n",
2744 PTR_ERR(rdev));
2745 return -EINVAL;
2748 if (mddev->persistent)
2749 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2750 else
2751 rdev->sb_offset =
2752 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2754 size = calc_dev_size(rdev, mddev->chunk_size);
2755 rdev->size = size;
2757 if (size < mddev->size) {
2758 printk(KERN_WARNING
2759 "%s: disk size %llu blocks < array size %llu\n",
2760 mdname(mddev), (unsigned long long)size,
2761 (unsigned long long)mddev->size);
2762 err = -ENOSPC;
2763 goto abort_export;
2766 if (test_bit(Faulty, &rdev->flags)) {
2767 printk(KERN_WARNING
2768 "md: can not hot-add faulty %s disk to %s!\n",
2769 bdevname(rdev->bdev,b), mdname(mddev));
2770 err = -EINVAL;
2771 goto abort_export;
2773 clear_bit(In_sync, &rdev->flags);
2774 rdev->desc_nr = -1;
2775 bind_rdev_to_array(rdev, mddev);
2778 * The rest should better be atomic, we can have disk failures
2779 * noticed in interrupt contexts ...
2782 if (rdev->desc_nr == mddev->max_disks) {
2783 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
2784 mdname(mddev));
2785 err = -EBUSY;
2786 goto abort_unbind_export;
2789 rdev->raid_disk = -1;
2791 md_update_sb(mddev);
2794 * Kick recovery, maybe this spare has to be added to the
2795 * array immediately.
2797 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2798 md_wakeup_thread(mddev->thread);
2800 return 0;
2802 abort_unbind_export:
2803 unbind_rdev_from_array(rdev);
2805 abort_export:
2806 export_rdev(rdev);
2807 return err;
2810 /* similar to deny_write_access, but accounts for our holding a reference
2811 * to the file ourselves */
2812 static int deny_bitmap_write_access(struct file * file)
2814 struct inode *inode = file->f_mapping->host;
2816 spin_lock(&inode->i_lock);
2817 if (atomic_read(&inode->i_writecount) > 1) {
2818 spin_unlock(&inode->i_lock);
2819 return -ETXTBSY;
2821 atomic_set(&inode->i_writecount, -1);
2822 spin_unlock(&inode->i_lock);
2824 return 0;
2827 static int set_bitmap_file(mddev_t *mddev, int fd)
2829 int err;
2831 if (mddev->pers) {
2832 if (!mddev->pers->quiesce)
2833 return -EBUSY;
2834 if (mddev->recovery || mddev->sync_thread)
2835 return -EBUSY;
2836 /* we should be able to change the bitmap.. */
2840 if (fd >= 0) {
2841 if (mddev->bitmap)
2842 return -EEXIST; /* cannot add when bitmap is present */
2843 mddev->bitmap_file = fget(fd);
2845 if (mddev->bitmap_file == NULL) {
2846 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
2847 mdname(mddev));
2848 return -EBADF;
2851 err = deny_bitmap_write_access(mddev->bitmap_file);
2852 if (err) {
2853 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
2854 mdname(mddev));
2855 fput(mddev->bitmap_file);
2856 mddev->bitmap_file = NULL;
2857 return err;
2859 mddev->bitmap_offset = 0; /* file overrides offset */
2860 } else if (mddev->bitmap == NULL)
2861 return -ENOENT; /* cannot remove what isn't there */
2862 err = 0;
2863 if (mddev->pers) {
2864 mddev->pers->quiesce(mddev, 1);
2865 if (fd >= 0)
2866 err = bitmap_create(mddev);
2867 if (fd < 0 || err)
2868 bitmap_destroy(mddev);
2869 mddev->pers->quiesce(mddev, 0);
2870 } else if (fd < 0) {
2871 if (mddev->bitmap_file)
2872 fput(mddev->bitmap_file);
2873 mddev->bitmap_file = NULL;
2876 return err;
2880 * set_array_info is used two different ways
2881 * The original usage is when creating a new array.
2882 * In this usage, raid_disks is > 0 and it together with
2883 * level, size, not_persistent,layout,chunksize determine the
2884 * shape of the array.
2885 * This will always create an array with a type-0.90.0 superblock.
2886 * The newer usage is when assembling an array.
2887 * In this case raid_disks will be 0, and the major_version field is
2888 * use to determine which style super-blocks are to be found on the devices.
2889 * The minor and patch _version numbers are also kept incase the
2890 * super_block handler wishes to interpret them.
2892 static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
2895 if (info->raid_disks == 0) {
2896 /* just setting version number for superblock loading */
2897 if (info->major_version < 0 ||
2898 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
2899 super_types[info->major_version].name == NULL) {
2900 /* maybe try to auto-load a module? */
2901 printk(KERN_INFO
2902 "md: superblock version %d not known\n",
2903 info->major_version);
2904 return -EINVAL;
2906 mddev->major_version = info->major_version;
2907 mddev->minor_version = info->minor_version;
2908 mddev->patch_version = info->patch_version;
2909 return 0;
2911 mddev->major_version = MD_MAJOR_VERSION;
2912 mddev->minor_version = MD_MINOR_VERSION;
2913 mddev->patch_version = MD_PATCHLEVEL_VERSION;
2914 mddev->ctime = get_seconds();
2916 mddev->level = info->level;
2917 mddev->size = info->size;
2918 mddev->raid_disks = info->raid_disks;
2919 /* don't set md_minor, it is determined by which /dev/md* was
2920 * openned
2922 if (info->state & (1<<MD_SB_CLEAN))
2923 mddev->recovery_cp = MaxSector;
2924 else
2925 mddev->recovery_cp = 0;
2926 mddev->persistent = ! info->not_persistent;
2928 mddev->layout = info->layout;
2929 mddev->chunk_size = info->chunk_size;
2931 mddev->max_disks = MD_SB_DISKS;
2933 mddev->sb_dirty = 1;
2936 * Generate a 128 bit UUID
2938 get_random_bytes(mddev->uuid, 16);
2940 return 0;
2944 * update_array_info is used to change the configuration of an
2945 * on-line array.
2946 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
2947 * fields in the info are checked against the array.
2948 * Any differences that cannot be handled will cause an error.
2949 * Normally, only one change can be managed at a time.
2951 static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
2953 int rv = 0;
2954 int cnt = 0;
2955 int state = 0;
2957 /* calculate expected state,ignoring low bits */
2958 if (mddev->bitmap && mddev->bitmap_offset)
2959 state |= (1 << MD_SB_BITMAP_PRESENT);
2961 if (mddev->major_version != info->major_version ||
2962 mddev->minor_version != info->minor_version ||
2963 /* mddev->patch_version != info->patch_version || */
2964 mddev->ctime != info->ctime ||
2965 mddev->level != info->level ||
2966 /* mddev->layout != info->layout || */
2967 !mddev->persistent != info->not_persistent||
2968 mddev->chunk_size != info->chunk_size ||
2969 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
2970 ((state^info->state) & 0xfffffe00)
2972 return -EINVAL;
2973 /* Check there is only one change */
2974 if (mddev->size != info->size) cnt++;
2975 if (mddev->raid_disks != info->raid_disks) cnt++;
2976 if (mddev->layout != info->layout) cnt++;
2977 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
2978 if (cnt == 0) return 0;
2979 if (cnt > 1) return -EINVAL;
2981 if (mddev->layout != info->layout) {
2982 /* Change layout
2983 * we don't need to do anything at the md level, the
2984 * personality will take care of it all.
2986 if (mddev->pers->reconfig == NULL)
2987 return -EINVAL;
2988 else
2989 return mddev->pers->reconfig(mddev, info->layout, -1);
2991 if (mddev->size != info->size) {
2992 mdk_rdev_t * rdev;
2993 struct list_head *tmp;
2994 if (mddev->pers->resize == NULL)
2995 return -EINVAL;
2996 /* The "size" is the amount of each device that is used.
2997 * This can only make sense for arrays with redundancy.
2998 * linear and raid0 always use whatever space is available
2999 * We can only consider changing the size if no resync
3000 * or reconstruction is happening, and if the new size
3001 * is acceptable. It must fit before the sb_offset or,
3002 * if that is <data_offset, it must fit before the
3003 * size of each device.
3004 * If size is zero, we find the largest size that fits.
3006 if (mddev->sync_thread)
3007 return -EBUSY;
3008 ITERATE_RDEV(mddev,rdev,tmp) {
3009 sector_t avail;
3010 int fit = (info->size == 0);
3011 if (rdev->sb_offset > rdev->data_offset)
3012 avail = (rdev->sb_offset*2) - rdev->data_offset;
3013 else
3014 avail = get_capacity(rdev->bdev->bd_disk)
3015 - rdev->data_offset;
3016 if (fit && (info->size == 0 || info->size > avail/2))
3017 info->size = avail/2;
3018 if (avail < ((sector_t)info->size << 1))
3019 return -ENOSPC;
3021 rv = mddev->pers->resize(mddev, (sector_t)info->size *2);
3022 if (!rv) {
3023 struct block_device *bdev;
3025 bdev = bdget_disk(mddev->gendisk, 0);
3026 if (bdev) {
3027 down(&bdev->bd_inode->i_sem);
3028 i_size_write(bdev->bd_inode, mddev->array_size << 10);
3029 up(&bdev->bd_inode->i_sem);
3030 bdput(bdev);
3034 if (mddev->raid_disks != info->raid_disks) {
3035 /* change the number of raid disks */
3036 if (mddev->pers->reshape == NULL)
3037 return -EINVAL;
3038 if (info->raid_disks <= 0 ||
3039 info->raid_disks >= mddev->max_disks)
3040 return -EINVAL;
3041 if (mddev->sync_thread)
3042 return -EBUSY;
3043 rv = mddev->pers->reshape(mddev, info->raid_disks);
3044 if (!rv) {
3045 struct block_device *bdev;
3047 bdev = bdget_disk(mddev->gendisk, 0);
3048 if (bdev) {
3049 down(&bdev->bd_inode->i_sem);
3050 i_size_write(bdev->bd_inode, mddev->array_size << 10);
3051 up(&bdev->bd_inode->i_sem);
3052 bdput(bdev);
3056 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
3057 if (mddev->pers->quiesce == NULL)
3058 return -EINVAL;
3059 if (mddev->recovery || mddev->sync_thread)
3060 return -EBUSY;
3061 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
3062 /* add the bitmap */
3063 if (mddev->bitmap)
3064 return -EEXIST;
3065 if (mddev->default_bitmap_offset == 0)
3066 return -EINVAL;
3067 mddev->bitmap_offset = mddev->default_bitmap_offset;
3068 mddev->pers->quiesce(mddev, 1);
3069 rv = bitmap_create(mddev);
3070 if (rv)
3071 bitmap_destroy(mddev);
3072 mddev->pers->quiesce(mddev, 0);
3073 } else {
3074 /* remove the bitmap */
3075 if (!mddev->bitmap)
3076 return -ENOENT;
3077 if (mddev->bitmap->file)
3078 return -EINVAL;
3079 mddev->pers->quiesce(mddev, 1);
3080 bitmap_destroy(mddev);
3081 mddev->pers->quiesce(mddev, 0);
3082 mddev->bitmap_offset = 0;
3085 md_update_sb(mddev);
3086 return rv;
3089 static int set_disk_faulty(mddev_t *mddev, dev_t dev)
3091 mdk_rdev_t *rdev;
3093 if (mddev->pers == NULL)
3094 return -ENODEV;
3096 rdev = find_rdev(mddev, dev);
3097 if (!rdev)
3098 return -ENODEV;
3100 md_error(mddev, rdev);
3101 return 0;
3104 static int md_ioctl(struct inode *inode, struct file *file,
3105 unsigned int cmd, unsigned long arg)
3107 int err = 0;
3108 void __user *argp = (void __user *)arg;
3109 struct hd_geometry __user *loc = argp;
3110 mddev_t *mddev = NULL;
3112 if (!capable(CAP_SYS_ADMIN))
3113 return -EACCES;
3116 * Commands dealing with the RAID driver but not any
3117 * particular array:
3119 switch (cmd)
3121 case RAID_VERSION:
3122 err = get_version(argp);
3123 goto done;
3125 case PRINT_RAID_DEBUG:
3126 err = 0;
3127 md_print_devices();
3128 goto done;
3130 #ifndef MODULE
3131 case RAID_AUTORUN:
3132 err = 0;
3133 autostart_arrays(arg);
3134 goto done;
3135 #endif
3136 default:;
3140 * Commands creating/starting a new array:
3143 mddev = inode->i_bdev->bd_disk->private_data;
3145 if (!mddev) {
3146 BUG();
3147 goto abort;
3151 if (cmd == START_ARRAY) {
3152 /* START_ARRAY doesn't need to lock the array as autostart_array
3153 * does the locking, and it could even be a different array
3155 static int cnt = 3;
3156 if (cnt > 0 ) {
3157 printk(KERN_WARNING
3158 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
3159 "This will not be supported beyond July 2006\n",
3160 current->comm, current->pid);
3161 cnt--;
3163 err = autostart_array(new_decode_dev(arg));
3164 if (err) {
3165 printk(KERN_WARNING "md: autostart failed!\n");
3166 goto abort;
3168 goto done;
3171 err = mddev_lock(mddev);
3172 if (err) {
3173 printk(KERN_INFO
3174 "md: ioctl lock interrupted, reason %d, cmd %d\n",
3175 err, cmd);
3176 goto abort;
3179 switch (cmd)
3181 case SET_ARRAY_INFO:
3183 mdu_array_info_t info;
3184 if (!arg)
3185 memset(&info, 0, sizeof(info));
3186 else if (copy_from_user(&info, argp, sizeof(info))) {
3187 err = -EFAULT;
3188 goto abort_unlock;
3190 if (mddev->pers) {
3191 err = update_array_info(mddev, &info);
3192 if (err) {
3193 printk(KERN_WARNING "md: couldn't update"
3194 " array info. %d\n", err);
3195 goto abort_unlock;
3197 goto done_unlock;
3199 if (!list_empty(&mddev->disks)) {
3200 printk(KERN_WARNING
3201 "md: array %s already has disks!\n",
3202 mdname(mddev));
3203 err = -EBUSY;
3204 goto abort_unlock;
3206 if (mddev->raid_disks) {
3207 printk(KERN_WARNING
3208 "md: array %s already initialised!\n",
3209 mdname(mddev));
3210 err = -EBUSY;
3211 goto abort_unlock;
3213 err = set_array_info(mddev, &info);
3214 if (err) {
3215 printk(KERN_WARNING "md: couldn't set"
3216 " array info. %d\n", err);
3217 goto abort_unlock;
3220 goto done_unlock;
3222 default:;
3226 * Commands querying/configuring an existing array:
3228 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
3229 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
3230 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
3231 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE) {
3232 err = -ENODEV;
3233 goto abort_unlock;
3237 * Commands even a read-only array can execute:
3239 switch (cmd)
3241 case GET_ARRAY_INFO:
3242 err = get_array_info(mddev, argp);
3243 goto done_unlock;
3245 case GET_BITMAP_FILE:
3246 err = get_bitmap_file(mddev, argp);
3247 goto done_unlock;
3249 case GET_DISK_INFO:
3250 err = get_disk_info(mddev, argp);
3251 goto done_unlock;
3253 case RESTART_ARRAY_RW:
3254 err = restart_array(mddev);
3255 goto done_unlock;
3257 case STOP_ARRAY:
3258 err = do_md_stop (mddev, 0);
3259 goto done_unlock;
3261 case STOP_ARRAY_RO:
3262 err = do_md_stop (mddev, 1);
3263 goto done_unlock;
3266 * We have a problem here : there is no easy way to give a CHS
3267 * virtual geometry. We currently pretend that we have a 2 heads
3268 * 4 sectors (with a BIG number of cylinders...). This drives
3269 * dosfs just mad... ;-)
3271 case HDIO_GETGEO:
3272 if (!loc) {
3273 err = -EINVAL;
3274 goto abort_unlock;
3276 err = put_user (2, (char __user *) &loc->heads);
3277 if (err)
3278 goto abort_unlock;
3279 err = put_user (4, (char __user *) &loc->sectors);
3280 if (err)
3281 goto abort_unlock;
3282 err = put_user(get_capacity(mddev->gendisk)/8,
3283 (short __user *) &loc->cylinders);
3284 if (err)
3285 goto abort_unlock;
3286 err = put_user (get_start_sect(inode->i_bdev),
3287 (long __user *) &loc->start);
3288 goto done_unlock;
3292 * The remaining ioctls are changing the state of the
3293 * superblock, so we do not allow them on read-only arrays.
3294 * However non-MD ioctls (e.g. get-size) will still come through
3295 * here and hit the 'default' below, so only disallow
3296 * 'md' ioctls, and switch to rw mode if started auto-readonly.
3298 if (_IOC_TYPE(cmd) == MD_MAJOR &&
3299 mddev->ro && mddev->pers) {
3300 if (mddev->ro == 2) {
3301 mddev->ro = 0;
3302 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3303 md_wakeup_thread(mddev->thread);
3305 } else {
3306 err = -EROFS;
3307 goto abort_unlock;
3311 switch (cmd)
3313 case ADD_NEW_DISK:
3315 mdu_disk_info_t info;
3316 if (copy_from_user(&info, argp, sizeof(info)))
3317 err = -EFAULT;
3318 else
3319 err = add_new_disk(mddev, &info);
3320 goto done_unlock;
3323 case HOT_REMOVE_DISK:
3324 err = hot_remove_disk(mddev, new_decode_dev(arg));
3325 goto done_unlock;
3327 case HOT_ADD_DISK:
3328 err = hot_add_disk(mddev, new_decode_dev(arg));
3329 goto done_unlock;
3331 case SET_DISK_FAULTY:
3332 err = set_disk_faulty(mddev, new_decode_dev(arg));
3333 goto done_unlock;
3335 case RUN_ARRAY:
3336 err = do_md_run (mddev);
3337 goto done_unlock;
3339 case SET_BITMAP_FILE:
3340 err = set_bitmap_file(mddev, (int)arg);
3341 goto done_unlock;
3343 default:
3344 if (_IOC_TYPE(cmd) == MD_MAJOR)
3345 printk(KERN_WARNING "md: %s(pid %d) used"
3346 " obsolete MD ioctl, upgrade your"
3347 " software to use new ictls.\n",
3348 current->comm, current->pid);
3349 err = -EINVAL;
3350 goto abort_unlock;
3353 done_unlock:
3354 abort_unlock:
3355 mddev_unlock(mddev);
3357 return err;
3358 done:
3359 if (err)
3360 MD_BUG();
3361 abort:
3362 return err;
3365 static int md_open(struct inode *inode, struct file *file)
3368 * Succeed if we can lock the mddev, which confirms that
3369 * it isn't being stopped right now.
3371 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3372 int err;
3374 if ((err = mddev_lock(mddev)))
3375 goto out;
3377 err = 0;
3378 mddev_get(mddev);
3379 mddev_unlock(mddev);
3381 check_disk_change(inode->i_bdev);
3382 out:
3383 return err;
3386 static int md_release(struct inode *inode, struct file * file)
3388 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3390 if (!mddev)
3391 BUG();
3392 mddev_put(mddev);
3394 return 0;
3397 static int md_media_changed(struct gendisk *disk)
3399 mddev_t *mddev = disk->private_data;
3401 return mddev->changed;
3404 static int md_revalidate(struct gendisk *disk)
3406 mddev_t *mddev = disk->private_data;
3408 mddev->changed = 0;
3409 return 0;
3411 static struct block_device_operations md_fops =
3413 .owner = THIS_MODULE,
3414 .open = md_open,
3415 .release = md_release,
3416 .ioctl = md_ioctl,
3417 .media_changed = md_media_changed,
3418 .revalidate_disk= md_revalidate,
3421 static int md_thread(void * arg)
3423 mdk_thread_t *thread = arg;
3426 * md_thread is a 'system-thread', it's priority should be very
3427 * high. We avoid resource deadlocks individually in each
3428 * raid personality. (RAID5 does preallocation) We also use RR and
3429 * the very same RT priority as kswapd, thus we will never get
3430 * into a priority inversion deadlock.
3432 * we definitely have to have equal or higher priority than
3433 * bdflush, otherwise bdflush will deadlock if there are too
3434 * many dirty RAID5 blocks.
3437 allow_signal(SIGKILL);
3438 while (!kthread_should_stop()) {
3440 /* We need to wait INTERRUPTIBLE so that
3441 * we don't add to the load-average.
3442 * That means we need to be sure no signals are
3443 * pending
3445 if (signal_pending(current))
3446 flush_signals(current);
3448 wait_event_interruptible_timeout
3449 (thread->wqueue,
3450 test_bit(THREAD_WAKEUP, &thread->flags)
3451 || kthread_should_stop(),
3452 thread->timeout);
3453 try_to_freeze();
3455 clear_bit(THREAD_WAKEUP, &thread->flags);
3457 thread->run(thread->mddev);
3460 return 0;
3463 void md_wakeup_thread(mdk_thread_t *thread)
3465 if (thread) {
3466 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
3467 set_bit(THREAD_WAKEUP, &thread->flags);
3468 wake_up(&thread->wqueue);
3472 mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
3473 const char *name)
3475 mdk_thread_t *thread;
3477 thread = kmalloc(sizeof(mdk_thread_t), GFP_KERNEL);
3478 if (!thread)
3479 return NULL;
3481 memset(thread, 0, sizeof(mdk_thread_t));
3482 init_waitqueue_head(&thread->wqueue);
3484 thread->run = run;
3485 thread->mddev = mddev;
3486 thread->timeout = MAX_SCHEDULE_TIMEOUT;
3487 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
3488 if (IS_ERR(thread->tsk)) {
3489 kfree(thread);
3490 return NULL;
3492 return thread;
3495 void md_unregister_thread(mdk_thread_t *thread)
3497 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
3499 kthread_stop(thread->tsk);
3500 kfree(thread);
3503 void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
3505 if (!mddev) {
3506 MD_BUG();
3507 return;
3510 if (!rdev || test_bit(Faulty, &rdev->flags))
3511 return;
3513 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
3514 mdname(mddev),
3515 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
3516 __builtin_return_address(0),__builtin_return_address(1),
3517 __builtin_return_address(2),__builtin_return_address(3));
3519 if (!mddev->pers->error_handler)
3520 return;
3521 mddev->pers->error_handler(mddev,rdev);
3522 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3523 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3524 md_wakeup_thread(mddev->thread);
3527 /* seq_file implementation /proc/mdstat */
3529 static void status_unused(struct seq_file *seq)
3531 int i = 0;
3532 mdk_rdev_t *rdev;
3533 struct list_head *tmp;
3535 seq_printf(seq, "unused devices: ");
3537 ITERATE_RDEV_PENDING(rdev,tmp) {
3538 char b[BDEVNAME_SIZE];
3539 i++;
3540 seq_printf(seq, "%s ",
3541 bdevname(rdev->bdev,b));
3543 if (!i)
3544 seq_printf(seq, "<none>");
3546 seq_printf(seq, "\n");
3550 static void status_resync(struct seq_file *seq, mddev_t * mddev)
3552 unsigned long max_blocks, resync, res, dt, db, rt;
3554 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
3556 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3557 max_blocks = mddev->resync_max_sectors >> 1;
3558 else
3559 max_blocks = mddev->size;
3562 * Should not happen.
3564 if (!max_blocks) {
3565 MD_BUG();
3566 return;
3568 res = (resync/1024)*1000/(max_blocks/1024 + 1);
3570 int i, x = res/50, y = 20-x;
3571 seq_printf(seq, "[");
3572 for (i = 0; i < x; i++)
3573 seq_printf(seq, "=");
3574 seq_printf(seq, ">");
3575 for (i = 0; i < y; i++)
3576 seq_printf(seq, ".");
3577 seq_printf(seq, "] ");
3579 seq_printf(seq, " %s =%3lu.%lu%% (%lu/%lu)",
3580 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
3581 "resync" : "recovery"),
3582 res/10, res % 10, resync, max_blocks);
3585 * We do not want to overflow, so the order of operands and
3586 * the * 100 / 100 trick are important. We do a +1 to be
3587 * safe against division by zero. We only estimate anyway.
3589 * dt: time from mark until now
3590 * db: blocks written from mark until now
3591 * rt: remaining time
3593 dt = ((jiffies - mddev->resync_mark) / HZ);
3594 if (!dt) dt++;
3595 db = resync - (mddev->resync_mark_cnt/2);
3596 rt = (dt * ((max_blocks-resync) / (db/100+1)))/100;
3598 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
3600 seq_printf(seq, " speed=%ldK/sec", db/dt);
3603 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
3605 struct list_head *tmp;
3606 loff_t l = *pos;
3607 mddev_t *mddev;
3609 if (l >= 0x10000)
3610 return NULL;
3611 if (!l--)
3612 /* header */
3613 return (void*)1;
3615 spin_lock(&all_mddevs_lock);
3616 list_for_each(tmp,&all_mddevs)
3617 if (!l--) {
3618 mddev = list_entry(tmp, mddev_t, all_mddevs);
3619 mddev_get(mddev);
3620 spin_unlock(&all_mddevs_lock);
3621 return mddev;
3623 spin_unlock(&all_mddevs_lock);
3624 if (!l--)
3625 return (void*)2;/* tail */
3626 return NULL;
3629 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3631 struct list_head *tmp;
3632 mddev_t *next_mddev, *mddev = v;
3634 ++*pos;
3635 if (v == (void*)2)
3636 return NULL;
3638 spin_lock(&all_mddevs_lock);
3639 if (v == (void*)1)
3640 tmp = all_mddevs.next;
3641 else
3642 tmp = mddev->all_mddevs.next;
3643 if (tmp != &all_mddevs)
3644 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
3645 else {
3646 next_mddev = (void*)2;
3647 *pos = 0x10000;
3649 spin_unlock(&all_mddevs_lock);
3651 if (v != (void*)1)
3652 mddev_put(mddev);
3653 return next_mddev;
3657 static void md_seq_stop(struct seq_file *seq, void *v)
3659 mddev_t *mddev = v;
3661 if (mddev && v != (void*)1 && v != (void*)2)
3662 mddev_put(mddev);
3665 static int md_seq_show(struct seq_file *seq, void *v)
3667 mddev_t *mddev = v;
3668 sector_t size;
3669 struct list_head *tmp2;
3670 mdk_rdev_t *rdev;
3671 int i;
3672 struct bitmap *bitmap;
3674 if (v == (void*)1) {
3675 seq_printf(seq, "Personalities : ");
3676 spin_lock(&pers_lock);
3677 for (i = 0; i < MAX_PERSONALITY; i++)
3678 if (pers[i])
3679 seq_printf(seq, "[%s] ", pers[i]->name);
3681 spin_unlock(&pers_lock);
3682 seq_printf(seq, "\n");
3683 return 0;
3685 if (v == (void*)2) {
3686 status_unused(seq);
3687 return 0;
3690 if (mddev_lock(mddev)!=0)
3691 return -EINTR;
3692 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
3693 seq_printf(seq, "%s : %sactive", mdname(mddev),
3694 mddev->pers ? "" : "in");
3695 if (mddev->pers) {
3696 if (mddev->ro==1)
3697 seq_printf(seq, " (read-only)");
3698 if (mddev->ro==2)
3699 seq_printf(seq, "(auto-read-only)");
3700 seq_printf(seq, " %s", mddev->pers->name);
3703 size = 0;
3704 ITERATE_RDEV(mddev,rdev,tmp2) {
3705 char b[BDEVNAME_SIZE];
3706 seq_printf(seq, " %s[%d]",
3707 bdevname(rdev->bdev,b), rdev->desc_nr);
3708 if (test_bit(WriteMostly, &rdev->flags))
3709 seq_printf(seq, "(W)");
3710 if (test_bit(Faulty, &rdev->flags)) {
3711 seq_printf(seq, "(F)");
3712 continue;
3713 } else if (rdev->raid_disk < 0)
3714 seq_printf(seq, "(S)"); /* spare */
3715 size += rdev->size;
3718 if (!list_empty(&mddev->disks)) {
3719 if (mddev->pers)
3720 seq_printf(seq, "\n %llu blocks",
3721 (unsigned long long)mddev->array_size);
3722 else
3723 seq_printf(seq, "\n %llu blocks",
3724 (unsigned long long)size);
3726 if (mddev->persistent) {
3727 if (mddev->major_version != 0 ||
3728 mddev->minor_version != 90) {
3729 seq_printf(seq," super %d.%d",
3730 mddev->major_version,
3731 mddev->minor_version);
3733 } else
3734 seq_printf(seq, " super non-persistent");
3736 if (mddev->pers) {
3737 mddev->pers->status (seq, mddev);
3738 seq_printf(seq, "\n ");
3739 if (mddev->pers->sync_request) {
3740 if (mddev->curr_resync > 2) {
3741 status_resync (seq, mddev);
3742 seq_printf(seq, "\n ");
3743 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
3744 seq_printf(seq, "\tresync=DELAYED\n ");
3745 else if (mddev->recovery_cp < MaxSector)
3746 seq_printf(seq, "\tresync=PENDING\n ");
3748 } else
3749 seq_printf(seq, "\n ");
3751 if ((bitmap = mddev->bitmap)) {
3752 unsigned long chunk_kb;
3753 unsigned long flags;
3754 spin_lock_irqsave(&bitmap->lock, flags);
3755 chunk_kb = bitmap->chunksize >> 10;
3756 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
3757 "%lu%s chunk",
3758 bitmap->pages - bitmap->missing_pages,
3759 bitmap->pages,
3760 (bitmap->pages - bitmap->missing_pages)
3761 << (PAGE_SHIFT - 10),
3762 chunk_kb ? chunk_kb : bitmap->chunksize,
3763 chunk_kb ? "KB" : "B");
3764 if (bitmap->file) {
3765 seq_printf(seq, ", file: ");
3766 seq_path(seq, bitmap->file->f_vfsmnt,
3767 bitmap->file->f_dentry," \t\n");
3770 seq_printf(seq, "\n");
3771 spin_unlock_irqrestore(&bitmap->lock, flags);
3774 seq_printf(seq, "\n");
3776 mddev_unlock(mddev);
3778 return 0;
3781 static struct seq_operations md_seq_ops = {
3782 .start = md_seq_start,
3783 .next = md_seq_next,
3784 .stop = md_seq_stop,
3785 .show = md_seq_show,
3788 static int md_seq_open(struct inode *inode, struct file *file)
3790 int error;
3792 error = seq_open(file, &md_seq_ops);
3793 return error;
3796 static struct file_operations md_seq_fops = {
3797 .open = md_seq_open,
3798 .read = seq_read,
3799 .llseek = seq_lseek,
3800 .release = seq_release,
3803 int register_md_personality(int pnum, mdk_personality_t *p)
3805 if (pnum >= MAX_PERSONALITY) {
3806 printk(KERN_ERR
3807 "md: tried to install personality %s as nr %d, but max is %lu\n",
3808 p->name, pnum, MAX_PERSONALITY-1);
3809 return -EINVAL;
3812 spin_lock(&pers_lock);
3813 if (pers[pnum]) {
3814 spin_unlock(&pers_lock);
3815 return -EBUSY;
3818 pers[pnum] = p;
3819 printk(KERN_INFO "md: %s personality registered as nr %d\n", p->name, pnum);
3820 spin_unlock(&pers_lock);
3821 return 0;
3824 int unregister_md_personality(int pnum)
3826 if (pnum >= MAX_PERSONALITY)
3827 return -EINVAL;
3829 printk(KERN_INFO "md: %s personality unregistered\n", pers[pnum]->name);
3830 spin_lock(&pers_lock);
3831 pers[pnum] = NULL;
3832 spin_unlock(&pers_lock);
3833 return 0;
3836 static int is_mddev_idle(mddev_t *mddev)
3838 mdk_rdev_t * rdev;
3839 struct list_head *tmp;
3840 int idle;
3841 unsigned long curr_events;
3843 idle = 1;
3844 ITERATE_RDEV(mddev,rdev,tmp) {
3845 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
3846 curr_events = disk_stat_read(disk, sectors[0]) +
3847 disk_stat_read(disk, sectors[1]) -
3848 atomic_read(&disk->sync_io);
3849 /* Allow some slack between valud of curr_events and last_events,
3850 * as there are some uninteresting races.
3851 * Note: the following is an unsigned comparison.
3853 if ((curr_events - rdev->last_events + 32) > 64) {
3854 rdev->last_events = curr_events;
3855 idle = 0;
3858 return idle;
3861 void md_done_sync(mddev_t *mddev, int blocks, int ok)
3863 /* another "blocks" (512byte) blocks have been synced */
3864 atomic_sub(blocks, &mddev->recovery_active);
3865 wake_up(&mddev->recovery_wait);
3866 if (!ok) {
3867 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
3868 md_wakeup_thread(mddev->thread);
3869 // stop recovery, signal do_sync ....
3874 /* md_write_start(mddev, bi)
3875 * If we need to update some array metadata (e.g. 'active' flag
3876 * in superblock) before writing, schedule a superblock update
3877 * and wait for it to complete.
3879 void md_write_start(mddev_t *mddev, struct bio *bi)
3881 if (bio_data_dir(bi) != WRITE)
3882 return;
3884 BUG_ON(mddev->ro == 1);
3885 if (mddev->ro == 2) {
3886 /* need to switch to read/write */
3887 mddev->ro = 0;
3888 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3889 md_wakeup_thread(mddev->thread);
3891 atomic_inc(&mddev->writes_pending);
3892 if (mddev->in_sync) {
3893 spin_lock_irq(&mddev->write_lock);
3894 if (mddev->in_sync) {
3895 mddev->in_sync = 0;
3896 mddev->sb_dirty = 1;
3897 md_wakeup_thread(mddev->thread);
3899 spin_unlock_irq(&mddev->write_lock);
3901 wait_event(mddev->sb_wait, mddev->sb_dirty==0);
3904 void md_write_end(mddev_t *mddev)
3906 if (atomic_dec_and_test(&mddev->writes_pending)) {
3907 if (mddev->safemode == 2)
3908 md_wakeup_thread(mddev->thread);
3909 else
3910 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
3914 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
3916 #define SYNC_MARKS 10
3917 #define SYNC_MARK_STEP (3*HZ)
3918 static void md_do_sync(mddev_t *mddev)
3920 mddev_t *mddev2;
3921 unsigned int currspeed = 0,
3922 window;
3923 sector_t max_sectors,j, io_sectors;
3924 unsigned long mark[SYNC_MARKS];
3925 sector_t mark_cnt[SYNC_MARKS];
3926 int last_mark,m;
3927 struct list_head *tmp;
3928 sector_t last_check;
3929 int skipped = 0;
3931 /* just incase thread restarts... */
3932 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
3933 return;
3935 /* we overload curr_resync somewhat here.
3936 * 0 == not engaged in resync at all
3937 * 2 == checking that there is no conflict with another sync
3938 * 1 == like 2, but have yielded to allow conflicting resync to
3939 * commense
3940 * other == active in resync - this many blocks
3942 * Before starting a resync we must have set curr_resync to
3943 * 2, and then checked that every "conflicting" array has curr_resync
3944 * less than ours. When we find one that is the same or higher
3945 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
3946 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
3947 * This will mean we have to start checking from the beginning again.
3951 do {
3952 mddev->curr_resync = 2;
3954 try_again:
3955 if (kthread_should_stop()) {
3956 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3957 goto skip;
3959 ITERATE_MDDEV(mddev2,tmp) {
3960 if (mddev2 == mddev)
3961 continue;
3962 if (mddev2->curr_resync &&
3963 match_mddev_units(mddev,mddev2)) {
3964 DEFINE_WAIT(wq);
3965 if (mddev < mddev2 && mddev->curr_resync == 2) {
3966 /* arbitrarily yield */
3967 mddev->curr_resync = 1;
3968 wake_up(&resync_wait);
3970 if (mddev > mddev2 && mddev->curr_resync == 1)
3971 /* no need to wait here, we can wait the next
3972 * time 'round when curr_resync == 2
3974 continue;
3975 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
3976 if (!kthread_should_stop() &&
3977 mddev2->curr_resync >= mddev->curr_resync) {
3978 printk(KERN_INFO "md: delaying resync of %s"
3979 " until %s has finished resync (they"
3980 " share one or more physical units)\n",
3981 mdname(mddev), mdname(mddev2));
3982 mddev_put(mddev2);
3983 schedule();
3984 finish_wait(&resync_wait, &wq);
3985 goto try_again;
3987 finish_wait(&resync_wait, &wq);
3990 } while (mddev->curr_resync < 2);
3992 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3993 /* resync follows the size requested by the personality,
3994 * which defaults to physical size, but can be virtual size
3996 max_sectors = mddev->resync_max_sectors;
3997 mddev->resync_mismatches = 0;
3998 } else
3999 /* recovery follows the physical size of devices */
4000 max_sectors = mddev->size << 1;
4002 printk(KERN_INFO "md: syncing RAID array %s\n", mdname(mddev));
4003 printk(KERN_INFO "md: minimum _guaranteed_ reconstruction speed:"
4004 " %d KB/sec/disc.\n", sysctl_speed_limit_min);
4005 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
4006 "(but not more than %d KB/sec) for reconstruction.\n",
4007 sysctl_speed_limit_max);
4009 is_mddev_idle(mddev); /* this also initializes IO event counters */
4010 /* we don't use the checkpoint if there's a bitmap */
4011 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && !mddev->bitmap
4012 && ! test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4013 j = mddev->recovery_cp;
4014 else
4015 j = 0;
4016 io_sectors = 0;
4017 for (m = 0; m < SYNC_MARKS; m++) {
4018 mark[m] = jiffies;
4019 mark_cnt[m] = io_sectors;
4021 last_mark = 0;
4022 mddev->resync_mark = mark[last_mark];
4023 mddev->resync_mark_cnt = mark_cnt[last_mark];
4026 * Tune reconstruction:
4028 window = 32*(PAGE_SIZE/512);
4029 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
4030 window/2,(unsigned long long) max_sectors/2);
4032 atomic_set(&mddev->recovery_active, 0);
4033 init_waitqueue_head(&mddev->recovery_wait);
4034 last_check = 0;
4036 if (j>2) {
4037 printk(KERN_INFO
4038 "md: resuming recovery of %s from checkpoint.\n",
4039 mdname(mddev));
4040 mddev->curr_resync = j;
4043 while (j < max_sectors) {
4044 sector_t sectors;
4046 skipped = 0;
4047 sectors = mddev->pers->sync_request(mddev, j, &skipped,
4048 currspeed < sysctl_speed_limit_min);
4049 if (sectors == 0) {
4050 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
4051 goto out;
4054 if (!skipped) { /* actual IO requested */
4055 io_sectors += sectors;
4056 atomic_add(sectors, &mddev->recovery_active);
4059 j += sectors;
4060 if (j>1) mddev->curr_resync = j;
4063 if (last_check + window > io_sectors || j == max_sectors)
4064 continue;
4066 last_check = io_sectors;
4068 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
4069 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
4070 break;
4072 repeat:
4073 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
4074 /* step marks */
4075 int next = (last_mark+1) % SYNC_MARKS;
4077 mddev->resync_mark = mark[next];
4078 mddev->resync_mark_cnt = mark_cnt[next];
4079 mark[next] = jiffies;
4080 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
4081 last_mark = next;
4085 if (kthread_should_stop()) {
4087 * got a signal, exit.
4089 printk(KERN_INFO
4090 "md: md_do_sync() got signal ... exiting\n");
4091 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4092 goto out;
4096 * this loop exits only if either when we are slower than
4097 * the 'hard' speed limit, or the system was IO-idle for
4098 * a jiffy.
4099 * the system might be non-idle CPU-wise, but we only care
4100 * about not overloading the IO subsystem. (things like an
4101 * e2fsck being done on the RAID array should execute fast)
4103 mddev->queue->unplug_fn(mddev->queue);
4104 cond_resched();
4106 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
4107 /((jiffies-mddev->resync_mark)/HZ +1) +1;
4109 if (currspeed > sysctl_speed_limit_min) {
4110 if ((currspeed > sysctl_speed_limit_max) ||
4111 !is_mddev_idle(mddev)) {
4112 msleep(250);
4113 goto repeat;
4117 printk(KERN_INFO "md: %s: sync done.\n",mdname(mddev));
4119 * this also signals 'finished resyncing' to md_stop
4121 out:
4122 mddev->queue->unplug_fn(mddev->queue);
4124 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
4126 /* tell personality that we are finished */
4127 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
4129 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4130 mddev->curr_resync > 2 &&
4131 mddev->curr_resync >= mddev->recovery_cp) {
4132 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4133 printk(KERN_INFO
4134 "md: checkpointing recovery of %s.\n",
4135 mdname(mddev));
4136 mddev->recovery_cp = mddev->curr_resync;
4137 } else
4138 mddev->recovery_cp = MaxSector;
4141 skip:
4142 mddev->curr_resync = 0;
4143 wake_up(&resync_wait);
4144 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
4145 md_wakeup_thread(mddev->thread);
4150 * This routine is regularly called by all per-raid-array threads to
4151 * deal with generic issues like resync and super-block update.
4152 * Raid personalities that don't have a thread (linear/raid0) do not
4153 * need this as they never do any recovery or update the superblock.
4155 * It does not do any resync itself, but rather "forks" off other threads
4156 * to do that as needed.
4157 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
4158 * "->recovery" and create a thread at ->sync_thread.
4159 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
4160 * and wakeups up this thread which will reap the thread and finish up.
4161 * This thread also removes any faulty devices (with nr_pending == 0).
4163 * The overall approach is:
4164 * 1/ if the superblock needs updating, update it.
4165 * 2/ If a recovery thread is running, don't do anything else.
4166 * 3/ If recovery has finished, clean up, possibly marking spares active.
4167 * 4/ If there are any faulty devices, remove them.
4168 * 5/ If array is degraded, try to add spares devices
4169 * 6/ If array has spares or is not in-sync, start a resync thread.
4171 void md_check_recovery(mddev_t *mddev)
4173 mdk_rdev_t *rdev;
4174 struct list_head *rtmp;
4177 if (mddev->bitmap)
4178 bitmap_daemon_work(mddev->bitmap);
4180 if (mddev->ro)
4181 return;
4183 if (signal_pending(current)) {
4184 if (mddev->pers->sync_request) {
4185 printk(KERN_INFO "md: %s in immediate safe mode\n",
4186 mdname(mddev));
4187 mddev->safemode = 2;
4189 flush_signals(current);
4192 if ( ! (
4193 mddev->sb_dirty ||
4194 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
4195 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
4196 (mddev->safemode == 1) ||
4197 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
4198 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
4200 return;
4202 if (mddev_trylock(mddev)==0) {
4203 int spares =0;
4205 spin_lock_irq(&mddev->write_lock);
4206 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
4207 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
4208 mddev->in_sync = 1;
4209 mddev->sb_dirty = 1;
4211 if (mddev->safemode == 1)
4212 mddev->safemode = 0;
4213 spin_unlock_irq(&mddev->write_lock);
4215 if (mddev->sb_dirty)
4216 md_update_sb(mddev);
4219 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4220 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
4221 /* resync/recovery still happening */
4222 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4223 goto unlock;
4225 if (mddev->sync_thread) {
4226 /* resync has finished, collect result */
4227 md_unregister_thread(mddev->sync_thread);
4228 mddev->sync_thread = NULL;
4229 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4230 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4231 /* success...*/
4232 /* activate any spares */
4233 mddev->pers->spare_active(mddev);
4235 md_update_sb(mddev);
4237 /* if array is no-longer degraded, then any saved_raid_disk
4238 * information must be scrapped
4240 if (!mddev->degraded)
4241 ITERATE_RDEV(mddev,rdev,rtmp)
4242 rdev->saved_raid_disk = -1;
4244 mddev->recovery = 0;
4245 /* flag recovery needed just to double check */
4246 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4247 goto unlock;
4249 /* Clear some bits that don't mean anything, but
4250 * might be left set
4252 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4253 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
4254 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
4255 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
4257 /* no recovery is running.
4258 * remove any failed drives, then
4259 * add spares if possible.
4260 * Spare are also removed and re-added, to allow
4261 * the personality to fail the re-add.
4263 ITERATE_RDEV(mddev,rdev,rtmp)
4264 if (rdev->raid_disk >= 0 &&
4265 (test_bit(Faulty, &rdev->flags) || ! test_bit(In_sync, &rdev->flags)) &&
4266 atomic_read(&rdev->nr_pending)==0) {
4267 if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0) {
4268 char nm[20];
4269 sprintf(nm,"rd%d", rdev->raid_disk);
4270 sysfs_remove_link(&mddev->kobj, nm);
4271 rdev->raid_disk = -1;
4275 if (mddev->degraded) {
4276 ITERATE_RDEV(mddev,rdev,rtmp)
4277 if (rdev->raid_disk < 0
4278 && !test_bit(Faulty, &rdev->flags)) {
4279 if (mddev->pers->hot_add_disk(mddev,rdev)) {
4280 char nm[20];
4281 sprintf(nm, "rd%d", rdev->raid_disk);
4282 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
4283 spares++;
4284 } else
4285 break;
4289 if (spares) {
4290 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4291 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4292 } else if (mddev->recovery_cp < MaxSector) {
4293 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4294 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4295 /* nothing to be done ... */
4296 goto unlock;
4298 if (mddev->pers->sync_request) {
4299 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4300 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
4301 /* We are adding a device or devices to an array
4302 * which has the bitmap stored on all devices.
4303 * So make sure all bitmap pages get written
4305 bitmap_write_all(mddev->bitmap);
4307 mddev->sync_thread = md_register_thread(md_do_sync,
4308 mddev,
4309 "%s_resync");
4310 if (!mddev->sync_thread) {
4311 printk(KERN_ERR "%s: could not start resync"
4312 " thread...\n",
4313 mdname(mddev));
4314 /* leave the spares where they are, it shouldn't hurt */
4315 mddev->recovery = 0;
4316 } else {
4317 md_wakeup_thread(mddev->sync_thread);
4320 unlock:
4321 mddev_unlock(mddev);
4325 static int md_notify_reboot(struct notifier_block *this,
4326 unsigned long code, void *x)
4328 struct list_head *tmp;
4329 mddev_t *mddev;
4331 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
4333 printk(KERN_INFO "md: stopping all md devices.\n");
4335 ITERATE_MDDEV(mddev,tmp)
4336 if (mddev_trylock(mddev)==0)
4337 do_md_stop (mddev, 1);
4339 * certain more exotic SCSI devices are known to be
4340 * volatile wrt too early system reboots. While the
4341 * right place to handle this issue is the given
4342 * driver, we do want to have a safe RAID driver ...
4344 mdelay(1000*1);
4346 return NOTIFY_DONE;
4349 static struct notifier_block md_notifier = {
4350 .notifier_call = md_notify_reboot,
4351 .next = NULL,
4352 .priority = INT_MAX, /* before any real devices */
4355 static void md_geninit(void)
4357 struct proc_dir_entry *p;
4359 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
4361 p = create_proc_entry("mdstat", S_IRUGO, NULL);
4362 if (p)
4363 p->proc_fops = &md_seq_fops;
4366 static int __init md_init(void)
4368 int minor;
4370 printk(KERN_INFO "md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
4371 " MD_SB_DISKS=%d\n",
4372 MD_MAJOR_VERSION, MD_MINOR_VERSION,
4373 MD_PATCHLEVEL_VERSION, MAX_MD_DEVS, MD_SB_DISKS);
4374 printk(KERN_INFO "md: bitmap version %d.%d\n", BITMAP_MAJOR_HI,
4375 BITMAP_MINOR);
4377 if (register_blkdev(MAJOR_NR, "md"))
4378 return -1;
4379 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
4380 unregister_blkdev(MAJOR_NR, "md");
4381 return -1;
4383 devfs_mk_dir("md");
4384 blk_register_region(MKDEV(MAJOR_NR, 0), MAX_MD_DEVS, THIS_MODULE,
4385 md_probe, NULL, NULL);
4386 blk_register_region(MKDEV(mdp_major, 0), MAX_MD_DEVS<<MdpMinorShift, THIS_MODULE,
4387 md_probe, NULL, NULL);
4389 for (minor=0; minor < MAX_MD_DEVS; ++minor)
4390 devfs_mk_bdev(MKDEV(MAJOR_NR, minor),
4391 S_IFBLK|S_IRUSR|S_IWUSR,
4392 "md/%d", minor);
4394 for (minor=0; minor < MAX_MD_DEVS; ++minor)
4395 devfs_mk_bdev(MKDEV(mdp_major, minor<<MdpMinorShift),
4396 S_IFBLK|S_IRUSR|S_IWUSR,
4397 "md/mdp%d", minor);
4400 register_reboot_notifier(&md_notifier);
4401 raid_table_header = register_sysctl_table(raid_root_table, 1);
4403 md_geninit();
4404 return (0);
4408 #ifndef MODULE
4411 * Searches all registered partitions for autorun RAID arrays
4412 * at boot time.
4414 static dev_t detected_devices[128];
4415 static int dev_cnt;
4417 void md_autodetect_dev(dev_t dev)
4419 if (dev_cnt >= 0 && dev_cnt < 127)
4420 detected_devices[dev_cnt++] = dev;
4424 static void autostart_arrays(int part)
4426 mdk_rdev_t *rdev;
4427 int i;
4429 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
4431 for (i = 0; i < dev_cnt; i++) {
4432 dev_t dev = detected_devices[i];
4434 rdev = md_import_device(dev,0, 0);
4435 if (IS_ERR(rdev))
4436 continue;
4438 if (test_bit(Faulty, &rdev->flags)) {
4439 MD_BUG();
4440 continue;
4442 list_add(&rdev->same_set, &pending_raid_disks);
4444 dev_cnt = 0;
4446 autorun_devices(part);
4449 #endif
4451 static __exit void md_exit(void)
4453 mddev_t *mddev;
4454 struct list_head *tmp;
4455 int i;
4456 blk_unregister_region(MKDEV(MAJOR_NR,0), MAX_MD_DEVS);
4457 blk_unregister_region(MKDEV(mdp_major,0), MAX_MD_DEVS << MdpMinorShift);
4458 for (i=0; i < MAX_MD_DEVS; i++)
4459 devfs_remove("md/%d", i);
4460 for (i=0; i < MAX_MD_DEVS; i++)
4461 devfs_remove("md/d%d", i);
4463 devfs_remove("md");
4465 unregister_blkdev(MAJOR_NR,"md");
4466 unregister_blkdev(mdp_major, "mdp");
4467 unregister_reboot_notifier(&md_notifier);
4468 unregister_sysctl_table(raid_table_header);
4469 remove_proc_entry("mdstat", NULL);
4470 ITERATE_MDDEV(mddev,tmp) {
4471 struct gendisk *disk = mddev->gendisk;
4472 if (!disk)
4473 continue;
4474 export_array(mddev);
4475 del_gendisk(disk);
4476 put_disk(disk);
4477 mddev->gendisk = NULL;
4478 mddev_put(mddev);
4482 module_init(md_init)
4483 module_exit(md_exit)
4485 static int get_ro(char *buffer, struct kernel_param *kp)
4487 return sprintf(buffer, "%d", start_readonly);
4489 static int set_ro(const char *val, struct kernel_param *kp)
4491 char *e;
4492 int num = simple_strtoul(val, &e, 10);
4493 if (*val && (*e == '\0' || *e == '\n')) {
4494 start_readonly = num;
4495 return 0;;
4497 return -EINVAL;
4500 module_param_call(start_ro, set_ro, get_ro, NULL, 0600);
4502 EXPORT_SYMBOL(register_md_personality);
4503 EXPORT_SYMBOL(unregister_md_personality);
4504 EXPORT_SYMBOL(md_error);
4505 EXPORT_SYMBOL(md_done_sync);
4506 EXPORT_SYMBOL(md_write_start);
4507 EXPORT_SYMBOL(md_write_end);
4508 EXPORT_SYMBOL(md_register_thread);
4509 EXPORT_SYMBOL(md_unregister_thread);
4510 EXPORT_SYMBOL(md_wakeup_thread);
4511 EXPORT_SYMBOL(md_print_devices);
4512 EXPORT_SYMBOL(md_check_recovery);
4513 MODULE_LICENSE("GPL");
4514 MODULE_ALIAS("md");
4515 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);